Skip to content

Commit dfd045a

Browse files
committed
feat(array): add traverse sugar
1 parent 7093585 commit dfd045a

File tree

13 files changed

+50
-31
lines changed

13 files changed

+50
-31
lines changed

docarray/array/document.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,10 @@ def __getitem__(
9898
if isinstance(index, (int, np.generic)):
9999
return self._data[int(index)]
100100
elif isinstance(index, str):
101-
return self._data[self._id2offset[index]]
101+
if index.startswith('@'):
102+
return self.traverse_flat(index[1:])
103+
else:
104+
return self._data[self._id2offset[index]]
102105
elif isinstance(index, slice):
103106
return DocumentArray(self._data[index])
104107
elif index is Ellipsis:
@@ -129,9 +132,14 @@ def __setitem__(
129132
self._data[index] = value
130133
self._id2offset[value.id] = index
131134
elif isinstance(index, str):
132-
old_idx = self._id2offset.pop(index)
133-
self._data[old_idx] = value
134-
self._id2offset[value.id] = old_idx
135+
if index.startswith('@'):
136+
for _d, _v in zip(self.traverse_flat(index[1:]), value):
137+
_d._data = _v._data
138+
self._rebuild_id2offset()
139+
else:
140+
old_idx = self._id2offset.pop(index)
141+
self._data[old_idx] = value
142+
self._id2offset[value.id] = old_idx
135143
elif isinstance(index, slice):
136144
self._data[index] = value
137145
self._rebuild_id2offset()
@@ -177,7 +185,10 @@ def __delitem__(self, index: 'DocumentArrayIndexType'):
177185
self._id2offset.pop(self._data[index].id)
178186
del self._data[index]
179187
elif isinstance(index, str):
180-
del self._data[self._id2offset[index]]
188+
if index.startswith('@'):
189+
raise NotImplementedError('Delete elements along traversal paths is not implemented')
190+
else:
191+
del self._data[self._id2offset[index]]
181192
self._id2offset.pop(index)
182193
elif isinstance(index, slice):
183194
del self._data[index]

docarray/array/mixins/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,6 @@ class AllMixins(
4545
DataframeIOMixin,
4646
ABC,
4747
):
48-
"""All plugins that can be used in :class:`DocumentArray` or :class:`DocumentArrayMemmap`. """
48+
"""All plugins that can be used in :class:`DocumentArray`. """
4949

5050
...

docarray/array/mixins/content.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def blobs(self) -> Optional['ArrayType']:
5454
5555
.. warning:: This operation assumes all blobs have the same shape and dtype.
5656
All dtype and shape values are assumed to be equal to the values of the
57-
first element in the DocumentArray / DocumentArrayMemmap
57+
first element in the DocumentArray
5858
5959
:return: a :class:`ArrayType` of blobs
6060
"""

docarray/array/mixins/empty.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ class EmptyMixin:
1111

1212
@classmethod
1313
def empty(cls: Type['T'], size: int = 0) -> 'T':
14-
"""Create a :class:`DocumentArray` or :class:`DocumentArrayMemmap` object with :attr:`size` empty
14+
"""Create a :class:`DocumentArray` object with :attr:`size` empty
1515
:class:`Document` objects.
1616
1717
:param size: the number of empty Documents in this container
18-
:return: a :class:`DocumentArray` or :class:`DocumentArrayMemmap` object
18+
:return: a :class:`DocumentArray` object
1919
"""
2020
return cls(Document() for _ in range(size))

docarray/array/mixins/group.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
class GroupMixin:
1212
"""These helpers yield groups of :class:`DocumentArray` from
13-
a source :class:`DocumentArray` or :class:`DocumentArrayMemmap`."""
13+
a source :class:`DocumentArray`."""
1414

1515
def split(self, tag: str) -> Dict[Any, 'DocumentArray']:
1616
"""Split the `DocumentArray` into multiple DocumentArray according to the tag value of each `Document`.

docarray/array/mixins/match.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def match(
4343
- To invert the distance as score and make all values in range [0, 1],
4444
use ``dA.match(dB, normalization=(1, 0))``. Note, how ``normalization`` differs from the previous.
4545
- If a custom metric distance is provided. Make sure that it returns scores as distances and not similarity, meaning the smaller the better.
46-
:param darray: the other DocumentArray or DocumentArrayMemmap to match against
46+
:param darray: the other DocumentArray to match against
4747
:param metric: the distance metric
4848
:param limit: the maximum number of matches, when not given defaults to 20.
4949
:param normalization: a tuple [a, b] to be used with min-max normalization,
@@ -136,7 +136,7 @@ def match(
136136
def _match(self, darray, cdist, limit, normalization, metric_name):
137137
"""
138138
Computes the matches between self and `darray` loading `darray` into main memory.
139-
:param darray: the other DocumentArray or DocumentArrayMemmap to match against
139+
:param darray: the other DocumentArray or to match against
140140
:param cdist: the distance metric
141141
:param limit: the maximum number of matches, when not given
142142
all Documents in `darray` are considered as matches
@@ -173,7 +173,7 @@ def _match_online(
173173
"""
174174
Computes the matches between self and `darray` loading `darray` into main memory in chunks of size `batch_size`.
175175
176-
:param darray: the other DocumentArray or DocumentArrayMemmap to match against
176+
:param darray: the other DocumentArray or to match against
177177
:param cdist: the distance metric
178178
:param limit: the maximum number of matches, when not given
179179
all Documents in `another` are considered as matches

docarray/array/mixins/parallel.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111

1212
class ParallelMixin:
13-
"""Helper functions that provide parallel map to :class:`DocumentArray` or :class:`DocumentArrayMemmap`."""
13+
"""Helper functions that provide parallel map to :class:`DocumentArray`"""
1414

1515
@overload
1616
def apply(
@@ -42,7 +42,7 @@ def apply(self: 'T', *args, **kwargs) -> 'T':
4242
# noqa: DAR102
4343
# noqa: DAR101
4444
# noqa: DAR201
45-
:return: a new :class:`DocumentArray` or :class:`DocumentArrayMemmap`
45+
:return: a new :class:`DocumentArray`
4646
"""
4747
new_da = type(self)()
4848
new_da.extend(self.map(*args, **kwargs))
@@ -60,7 +60,7 @@ def map(
6060
6161
.. seealso::
6262
- To process on a batch of elements, please use :meth:`.map_batch`;
63-
- To return a :class:`DocumentArray`/:class:`DocumentArrayMemmap`, please use :meth:`.apply`.
63+
- To return a :class:`DocumentArray`, please use :meth:`.apply`.
6464
6565
:param func: a function that takes :class:`Document` as input and outputs anything. You can either modify elements
6666
in-place (only with `thread` backend) or work later on return elements.
@@ -116,7 +116,7 @@ def apply_batch(self: 'T', *args, **kwargs) -> 'T':
116116
# noqa: DAR102
117117
# noqa: DAR101
118118
# noqa: DAR201
119-
:return: a new :class:`DocumentArray` or :class:`DocumentArrayMemmap`
119+
:return: a new :class:`DocumentArray`
120120
"""
121121
new_da = type(self)()
122122
for _b in self.map_batch(*args, **kwargs):
@@ -138,7 +138,7 @@ def map_batch(
138138
139139
.. seealso::
140140
- To process single element, please use :meth:`.map`;
141-
- To return :class:`DocumentArray` or :class:`DocumentArrayMemmap`, please use :meth:`.apply_batch`.
141+
- To return :class:`DocumentArray`, please use :meth:`.apply_batch`.
142142
143143
:param batch_size: Size of each generated batch (except the last one, which might be smaller, default: 32)
144144
:param shuffle: If set, shuffle the Documents before dividing into minibatches.

docarray/array/mixins/reduce.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def _reduce_doc_props(doc1: 'Document', doc2: 'Document'):
2323

2424
class ReduceMixin:
2525
"""
26-
A mixin that provides reducing logic for :class:`DocumentArray` or :class:`DocumentArrayMemmap`
26+
A mixin that provides reducing logic for :class:`DocumentArray`
2727
Reducing 2 or more DocumentArrays consists in merging all Documents into the same DocumentArray.
2828
If a Document belongs to 2 or more DocumentArrays, it is added once and data attributes are merged with priority to
2929
the Document belonging to the left-most DocumentArray. Matches and chunks are also reduced in the same way.

docarray/array/mixins/sample.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,8 @@ def sample(self, k: int, seed: Optional[int] = None) -> 'DocumentArray':
2222
random.seed(seed)
2323
# NOTE, this could simplified to random.sample(self, k)
2424
# without getting indices and itemgetter etc.
25-
# however it's only work on DocumentArray, not DocumentArrayMemmap.
26-
indices = random.sample(range(len(self)), k)
27-
sampled = operator.itemgetter(*indices)(self)
25+
# however it's only work on DocumentArray.
26+
sampled = random.sample(self, k)
2827

2928
from ..document import DocumentArray
3029

docarray/array/mixins/traverse.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def _check_traversal_path_type(tp):
3434

3535
class TraverseMixin:
3636
"""
37-
A mixin used for traversing :class:`DocumentArray` or :class:`DocumentArrayMemmap`.
37+
A mixin used for traversing :class:`DocumentArray`.
3838
"""
3939

4040
def traverse(

0 commit comments

Comments
 (0)