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

BUG: Fix bug with rounding of empty tables #217

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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: 2 additions & 1 deletion woltka/biom.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,8 @@ def f(num):
return round(num, digits)

tmd = table.matrix_data
tmd.data = np.vectorize(f)(tmd.data).astype('float64')
if tmd.nnz:
tmd.data = np.vectorize(f)(tmd.data).astype('float64')
tmd.eliminate_zeros()
table.remove_empty(axis='observation')

Expand Down
24 changes: 24 additions & 0 deletions woltka/tests/test_biom.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,30 @@ def test_round_biom(self):
'S2': {'G1': 1.55, 'G2': 0.17, 'G3': 1.5}})))
self.assertBIOMEqual(obs, exp)

def test_round_biom_empty_table(self):
# https://github.com/qiyunzhu/woltka/issues/216
exp = Table(data=[], observation_ids=[], sample_ids=[])
round_biom(exp)
obs = Table(data=[], observation_ids=[], sample_ids=[])
self.assertBIOMEqual(obs, exp)

def test_round_biom_empty_table_with_labels(self):
# https://github.com/qiyunzhu/woltka/issues/216
observation_ids = ['G1', 'G2', 'G3']
sample_ids = ['S1', 'S2']
data = np.zeros((len(observation_ids), len(sample_ids)))

obs = Table(data, observation_ids=observation_ids,
sample_ids=sample_ids)
round_biom(obs)

data = np.zeros((len(observation_ids), len(sample_ids)))
exp = Table(data, observation_ids=observation_ids,
sample_ids=sample_ids)
exp.remove_empty(axis='observation')

self.assertBIOMEqual(obs, exp)

def test_biom_add_metacol(self):
obs = Table(*map(np.array, prep_table({
'S1': {'G1': 4, 'G2': 5, 'G3': 8, 'G4': 0, 'G5': 3},
Expand Down
Loading