Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 4e7e262

Browse filesBrowse files
authored
fix: doc vec equality (#1641)
Signed-off-by: Nikos Pitsillos <npitsillos@gmail.com>
1 parent 7f91e21 commit 4e7e262
Copy full SHA for 4e7e262

3 files changed

+58Lines changed: 58 additions & 0 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎docarray/array/doc_vec/column_storage.py‎

Copy file name to clipboardExpand all lines: docarray/array/doc_vec/column_storage.py
+15Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,21 @@ def __getitem__(self: T, item: IndexIterType) -> T:
9191
self.tensor_type,
9292
)
9393

94+
def __eq__(self, other: Any) -> bool:
95+
if not isinstance(other, ColumnStorage):
96+
return False
97+
if self.tensor_type != other.tensor_type:
98+
return False
99+
for col_map_self, col_map_other in zip(self.columns.maps, other.columns.maps):
100+
if col_map_self.keys() != col_map_other.keys():
101+
return False
102+
for key_self in col_map_self.keys():
103+
if key_self == 'id':
104+
continue
105+
if col_map_self[key_self] != col_map_other[key_self]:
106+
return False
107+
return True
108+
94109

95110
class ColumnStorageView(dict, MutableMapping[str, Any]):
96111
index: int
Collapse file

‎docarray/array/doc_vec/doc_vec.py‎

Copy file name to clipboardExpand all lines: docarray/array/doc_vec/doc_vec.py
+11Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,17 @@ def __iter__(self):
584584
def __len__(self):
585585
return len(self._storage)
586586

587+
def __eq__(self, other: Any) -> bool:
588+
if not isinstance(other, DocVec):
589+
return False
590+
if self.doc_type != other.doc_type:
591+
return False
592+
if self.tensor_type != other.tensor_type:
593+
return False
594+
if self._storage != other._storage:
595+
return False
596+
return True
597+
587598
####################
588599
# IO related #
589600
####################
Collapse file

‎tests/units/array/stack/test_array_stacked.py‎

Copy file name to clipboardExpand all lines: tests/units/array/stack/test_array_stacked.py
+32Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,3 +585,35 @@ def test_doc_view_dict(batch):
585585
d = doc_view_two.dict()
586586
assert d['tensor'].shape == (3, 224, 224)
587587
assert d['id'] == doc_view_two.id
588+
589+
590+
def test_doc_vec_equality():
591+
class Text(BaseDoc):
592+
text: str
593+
594+
da = DocVec[Text]([Text(text='hello') for _ in range(10)])
595+
da2 = DocList[Text]([Text(text='hello') for _ in range(10)])
596+
597+
assert da != da2
598+
assert da == da2.to_doc_vec()
599+
600+
601+
def test_doc_vec_nested(batch_nested_doc):
602+
batch, Doc, Inner = batch_nested_doc
603+
batch2 = DocVec[Doc]([Doc(inner=Inner(hello='hello')) for _ in range(10)])
604+
605+
assert batch == batch2
606+
607+
608+
def test_doc_vec_tensor_type():
609+
class ImageDoc(BaseDoc):
610+
tensor: AnyTensor
611+
612+
da = DocVec[ImageDoc]([ImageDoc(tensor=np.zeros((3, 224, 224))) for _ in range(10)])
613+
614+
da2 = DocVec[ImageDoc](
615+
[ImageDoc(tensor=torch.zeros(3, 224, 224)) for _ in range(10)],
616+
tensor_type=TorchTensor,
617+
)
618+
619+
assert da != da2

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.