diff --git a/empress/tools.py b/empress/tools.py index 850a51fc..678e5c3b 100644 --- a/empress/tools.py +++ b/empress/tools.py @@ -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 ---------- @@ -370,6 +374,11 @@ 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') + # Convert all the bits to ints -- if they are np.uint8, etc. instead and + # we don't convert them, then this can cause problems: see + # https://github.com/biocore/empress/issues/562 + bitlist = [int(x) for x in bitlist] + values = [iter(bitlist)] * size ints = [] for num in zip_longest(*values): diff --git a/tests/python/test_taxonomy_utils.py b/tests/python/test_taxonomy_utils.py index 657b451a..cb8ab23d 100644 --- a/tests/python/test_taxonomy_utils.py +++ b/tests/python/test_taxonomy_utils.py @@ -57,9 +57,9 @@ def setUp(self): ) def _check_basic_case_worked(self, split_fm, taxcols): - """Checks that a given DataFrame (and list of split-up taxonomy columns) - matches the expected output from running split_taxonomy() on - self.feature_metadata. + """Checks that a given DataFrame (and list of split-up taxonomy + columns) matches the expected output from running split_taxonomy() + on self.feature_metadata. """ # Let's verify that split_fm looks how we expect it to look. diff --git a/tests/python/test_tools.py b/tests/python/test_tools.py index 6f3b87b6..90dc3751 100644 --- a/tests/python/test_tools.py +++ b/tests/python/test_tools.py @@ -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