Skip to content

Commit

Permalink
BUG: Fix biocore#562
Browse files Browse the repository at this point in the history
Ideally we would test this on numpy v2, but I can't seem to get empress
running with numpy v2 on my system (get a cython error from iow).

in any case this should address the problem
  • Loading branch information
fedarko committed Aug 3, 2024
1 parent bf6f755 commit cd57f45
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
12 changes: 9 additions & 3 deletions empress/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,19 +345,23 @@ def shifting(bitlist, size=51):
Parameters
----------
bitlist: list of int
The input list of 0-1
The input list of bits (0 or 1). Depending on the version of iow
installed, the entries in this list might be of a slightly different
type (e.g. np.uint8).
size: int
The size of the buffer
Returns
-------
list of int
Representation of the 0-1s as a list of int
Representation of the bits as a list of int. Regardless of the types in
the input bitlist, the entries in the output list will always have type
int.
Raises
------
ValueError
If any of the list values is different than 0 or 1
If any of the entries in bitlist is not equal to 0 or 1.
References
----------
Expand All @@ -370,6 +374,8 @@ def shifting(bitlist, size=51):
if not all(x in [0, 1] for x in bitlist):
raise ValueError('Your list has values other than 0-1s')

bitlist = [int(x) for x in bitlist]

values = [iter(bitlist)] * size
ints = []
for num in zip_longest(*values):
Expand Down
16 changes: 16 additions & 0 deletions tests/python/test_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,22 @@ def _count_bits(n):
"than 0-1s"):
tools.shifting([10])

def test_shifting_np_uint8(self):
# Verifies that https://github.com/biocore/empress/issues/562 is fixed.
# Checks that, whether the inputs are ints or np.uint8s, the output is
# the same.
bits = [1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1]
np_bits = [np.uint8(b) for b in bits]
expected = [4035]

o1 = tools.shifting(bits)
assert o1 == expected
assert type(o1[0]) is int

o2 = tools.shifting(np_bits)
assert o2 == expected
assert type(o2[0]) is int

def test_filter_feature_metadata_to_tree_1_tip_filtered(self):
ft, fi = tools.filter_feature_metadata_to_tree(
self.tip_md, self.int_md, self.shorn_tree
Expand Down

0 comments on commit cd57f45

Please sign in to comment.