def test_traversal_path(): da = DocumentArray([Document() for _ in range(6)]) assert len(da) == 6 da.traverse_flat(['r']) with pytest.raises(ValueError): da.traverse_flat('r') da.traverse(['r']) with pytest.raises(ValueError): for _ in da.traverse('r'): pass da.traverse(['r']) with pytest.raises(ValueError): for _ in da.traverse('r'): pass
def test_traversal_path(): da = DocumentArray([Document() for _ in range(6)]) assert len(da) == 6 da.traverse_flat('r') with pytest.warns(DeprecationWarning): da.traverse_flat(['r']) da.traverse('r') with pytest.warns(DeprecationWarning): for _ in da.traverse(['r']): pass da.traverse('r') with pytest.warns(DeprecationWarning): for _ in da.traverse(['r']): pass
def craft(self, docs: DocumentArray, **kwargs): filtered_docs = DocumentArray( d for d in docs.traverse_flat(['c']) if d.mime_type == 'image/jpeg' ) target_size = 224 for doc in filtered_docs: doc.load_uri_to_image_blob() doc.set_image_blob_shape(shape=(target_size, target_size)) doc.set_image_blob_channel_axis(-1, 0) return filtered_docs
def test_match_inclusive(only_id): """Call match function, while the other :class:`DocumentArray` is itself or have same :class:`Document`. """ # The document array da1 match with itself. da1 = DocumentArray([ Document(embedding=np.array([1, 2, 3])), Document(embedding=np.array([1, 0, 1])), Document(embedding=np.array([1, 1, 2])), ]) da1.match(da1, only_id=only_id) assert len(da1) == 3 traversed = da1.traverse_flat(traversal_paths=['m', 'mm', 'mmm']) assert len(traversed) == 9 # The document array da2 shares same documents with da1 da2 = DocumentArray( [Document(embedding=np.array([4, 1, 3])), da1[0], da1[1]]) da1.match(da2, only_id=only_id) assert len(da2) == 3 traversed = da1.traverse_flat(traversal_paths=['m', 'mm', 'mmm']) assert len(traversed) == 9
def craft(self, docs: DocumentArray, fn): filtered_docs = DocumentArray(d for d in docs.traverse_flat(['c']) if d.mime_type == 'image/jpeg') for doc in filtered_docs: getattr(doc, fn)() raw_img = _load_image(doc.blob, self.channel_axis) img = self._normalize(raw_img) # move the channel_axis to target_channel_axis to better fit different models if self.channel_axis != self.target_channel_axis: img = np.moveaxis(img, self.channel_axis, self.target_channel_axis) doc.blob = img return filtered_docs
def filter(self, docs: DocumentArray, **kwargs): filtered_docs = DocumentArray(d for d in docs.traverse_flat(['c']) if d.mime_type == 'text/plain') return filtered_docs
def test_traverse_flat_root_itself(): da = DocumentArray([Document() for _ in range(100)]) res = da.traverse_flat('r') assert id(res) == id(da)