Skip to content

Commit

Permalink
Issue 957 (#963)
Browse files Browse the repository at this point in the history
* TST: regression test for issue #957

* BUG: update_ids data type width was sensitive to strict setting, issue #957

* DOC: mention of issue #957
  • Loading branch information
wasade authored May 7, 2024
1 parent e8e6ed6 commit 0cb7fcc
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
1 change: 1 addition & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Bug Fixes:

* Fixed an edge case on in `align_tree` when a feature was empty, see issue [#948](https://github.com/biocore/biom-format/issues/948)
* In `subsample(..., with_replacement=True)`, it was possible to trigger a numerical stability on sum, see issue [#952](https://github.com/biocore/biom-format/issues/952)
* `update_ids(..., strict=False)` could yield truncated IDs, see issue [#957](https://github.com/biocore/biom-format/issues/957)

Performance improvements:

Expand Down
7 changes: 6 additions & 1 deletion biom/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -1422,7 +1422,12 @@ def update_ids(self, id_map, axis='sample', strict=True, inplace=True):
>>> print(updated_table.ids(axis='sample'))
['s1.1' 's2.2' 's3.3']
"""
str_dtype = 'U%d' % max([len(v) for v in id_map.values()])
max_str_len = max([len(v) for v in id_map.values()])
if not strict:
ids = self.ids(axis=axis)
max_str_len = max(max_str_len, max([len(i) for i in ids]))

str_dtype = 'U%d' % max_str_len
updated_ids = zeros(self.ids(axis=axis).size, dtype=str_dtype)
for idx, old_id in enumerate(self.ids(axis=axis)):
if strict and old_id not in id_map:
Expand Down
10 changes: 10 additions & 0 deletions biom/tests/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -2412,6 +2412,16 @@ def test_transpose(self):
self.st_rich.data('2', 'observation'))
self.assertEqual(obs.transpose(), self.st_rich)

def test_update_ids_strict_dtype_bug_issue_957(self):
t = Table(np.arange(6).reshape(2, 3),
['O1', 'O2'],
['ab', 'cdef', 'ghijkl'])
exp = Table(np.arange(6).reshape(2, 3),
['O1', 'O2'],
['AB', 'cdef', 'ghijkl'])
obs = t.update_ids({'ab': 'AB'}, strict=False, inplace=False)
self.assertEqual(obs, exp)

def test_update_ids_inplace_bug_892(self):
t = example_table.copy()
exp = t.ids().copy()
Expand Down

0 comments on commit 0cb7fcc

Please sign in to comment.