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

[MERGE] fix divide by zero issue in MinHash.contained_by #572

Merged
merged 2 commits into from
Dec 5, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 2 additions & 0 deletions sourmash/_minhash.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,8 @@ cdef class MinHash(object):
"""\
Calculate how much of self is contained by other.
"""
if not self.get_mins():
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if not self.get_mins():
if len(self) == 0:

will avoid building mins (len() check the underlying mins in C++ without having to build it)

return 0.0
return self.count_common(other) / len(self.get_mins())

def similarity_ignore_maxhash(self, MinHash other):
Expand Down
20 changes: 20 additions & 0 deletions tests/test__minhash.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,26 @@ def test_basic_dna(track_abundance):
assert len(b) == 1


def test_div_zero(track_abundance):
# verify that empty MHs do not yield divide by zero errors for similarity
mh = MinHash(1, 4, track_abundance=track_abundance)
mh2 = mh.copy_and_clear()

mh.add_sequence('ATGC')
assert mh.similarity(mh2) == 0
assert mh2.similarity(mh) == 0


def test_div_zero_contained(track_abundance):
# verify that empty MHs do not yield divide by zero errors for contained_by
mh = MinHash(1, 4, track_abundance=track_abundance)
mh2 = mh.copy_and_clear()

mh.add_sequence('ATGC')
assert mh.contained_by(mh2) == 0
assert mh2.contained_by(mh) == 0


def test_bytes_dna(track_abundance):
mh = MinHash(1, 4, track_abundance=track_abundance)
mh.add_sequence('ATGC')
Expand Down