Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add __eq__ method to SparseEmbedding #7574

Merged
merged 4 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions haystack/dataclasses/sparse_embedding.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ def __init__(self, indices: List[int], values: List[float]):
self.indices = indices
self.values = values

def __eq__(self, other):
return self.indices == other.indices and self.values == other.values

def to_dict(self):
"""
Convert the sparse embedding to a dictionary.
Expand Down
4 changes: 4 additions & 0 deletions releasenotes/notes/sparse-emb-eq-773ef04ae3ed83ea.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
enhancements:
- |
Add an `__eq__` method to `SparseEmbedding` class to compare two `SparseEmbedding` objects.
8 changes: 8 additions & 0 deletions test/dataclasses/test_sparse_embedding.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,11 @@ def test_from_dict(self):
se = SparseEmbedding.from_dict({"indices": [0, 2, 4], "values": [0.1, 0.2, 0.3]})
assert se.indices == [0, 2, 4]
assert se.values == [0.1, 0.2, 0.3]

def test_eq(self):
se1 = SparseEmbedding(indices=[0, 2, 4], values=[0.1, 0.2, 0.3])
se2 = SparseEmbedding(indices=[0, 2, 4], values=[0.1, 0.2, 0.3])
assert se1 == se2

se3 = SparseEmbedding(indices=[0, 2, 4], values=[0.1, 0.2, 0.4])
assert se1 != se3