diff --git a/q2_diversity/_alpha/_visualizer.py b/q2_diversity/_alpha/_visualizer.py index 25387553..19446ad3 100644 --- a/q2_diversity/_alpha/_visualizer.py +++ b/q2_diversity/_alpha/_visualizer.py @@ -237,12 +237,15 @@ def alpha_correlation(output_dir: str, def _reindex_with_metadata(column, columns, merged): - merged.set_index(column, inplace=True) - merged.sort_index(axis=0, ascending=True, inplace=True) - merged = merged.groupby(level=[column]) - counts = merged.count() - counts.drop(columns, axis=1, inplace=True, level=0) - median_ = merged.median() + reindexed = merged.set_index(column) + reindexed.sort_index(axis=0, ascending=True, inplace=True) + grouped = reindexed.groupby(level=[column]) + counts = grouped.count() + # Removes the column name used to set the index of `merged` above + col_diff = set(columns) - set([column]) + if col_diff: + counts.drop(col_diff, axis=1, inplace=True, level=0) + median_ = grouped.median() return median_, counts diff --git a/q2_diversity/tests/test_alpha_rarefaction.py b/q2_diversity/tests/test_alpha_rarefaction.py index 22603115..cef2c10f 100644 --- a/q2_diversity/tests/test_alpha_rarefaction.py +++ b/q2_diversity/tests/test_alpha_rarefaction.py @@ -418,21 +418,21 @@ def test_unique_metadata_groups(self): [9, 10, 11, 12, 'peanut']], columns=columns, index=['S1', 'S2', 'S3']) - obs = _reindex_with_metadata('pet', ['pet'], data) + median, counts = _reindex_with_metadata('pet', ['pet'], data) exp_col = pd.MultiIndex(levels=[[1, 200, 'pet'], [1, 2, '']], - labels=[[0, 0, 1, 1], [0, 1, 0, 1]], + codes=[[0, 0, 1, 1], [0, 1, 0, 1]], names=['depth', 'iter']) exp_ind = pd.Index(['milo', 'peanut', 'russ'], name='pet') exp = pd.DataFrame(data=[[5, 6, 7, 8], [9, 10, 11, 12], [1, 2, 3, 4]], columns=exp_col, index=exp_ind) - pdt.assert_frame_equal(exp, obs[0]) + pdt.assert_frame_equal(exp, median) exp = pd.DataFrame(data=[[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]], columns=exp_col, index=exp_ind) - pdt.assert_frame_equal(exp, obs[1]) + pdt.assert_frame_equal(exp, counts) def test_some_duplicates_in_column(self): columns = pd.MultiIndex.from_tuples([(1, 1), (1, 2), (200, 1), @@ -442,21 +442,21 @@ def test_some_duplicates_in_column(self): [9, 10, 11, 12, 'russ']], columns=columns, index=['S1', 'S2', 'S3']) - obs = _reindex_with_metadata('pet', ['pet'], data) + median, counts = _reindex_with_metadata('pet', ['pet'], data) exp_col = pd.MultiIndex(levels=[[1, 200, 'pet'], [1, 2, '']], - labels=[[0, 0, 1, 1], [0, 1, 0, 1]], + codes=[[0, 0, 1, 1], [0, 1, 0, 1]], names=['depth', 'iter']) exp_ind = pd.Index(['milo', 'russ'], name='pet') exp = pd.DataFrame(data=[[5, 6, 7, 8], [5, 6, 7, 8]], columns=exp_col, index=exp_ind) - pdt.assert_frame_equal(exp, obs[0]) + pdt.assert_frame_equal(exp, median) exp = pd.DataFrame(data=[[1, 1, 1, 1], [2, 2, 2, 2]], columns=exp_col, index=exp_ind) - pdt.assert_frame_equal(exp, obs[1]) + pdt.assert_frame_equal(exp, counts) def test_all_identical(self): columns = pd.MultiIndex.from_tuples([(1, 1), (1, 2), (200, 1), @@ -466,21 +466,21 @@ def test_all_identical(self): [9, 10, 11, 12, 'russ']], columns=columns, index=['S1', 'S2', 'S3']) - obs = _reindex_with_metadata('pet', ['pet'], data) + median, counts = _reindex_with_metadata('pet', ['pet'], data) exp_col = pd.MultiIndex(levels=[[1, 200, 'pet'], [1, 2, '']], - labels=[[0, 0, 1, 1], [0, 1, 0, 1]], + codes=[[0, 0, 1, 1], [0, 1, 0, 1]], names=['depth', 'iter']) exp_ind = pd.Index(['russ'], name='pet') exp = pd.DataFrame(data=[[5, 6, 7, 8]], columns=exp_col, index=exp_ind) - pdt.assert_frame_equal(exp, obs[0]) + pdt.assert_frame_equal(exp, median) exp = pd.DataFrame(data=[[3, 3, 3, 3]], columns=exp_col, index=exp_ind) - pdt.assert_frame_equal(exp, obs[1]) + pdt.assert_frame_equal(exp, counts) def test_multiple_columns(self): columns = pd.MultiIndex.from_tuples([(1, 1), (1, 2), (200, 1), @@ -492,34 +492,34 @@ def test_multiple_columns(self): [9, 10, 11, 12, 'peanut', 'stick']], columns=columns, index=['S1', 'S2', 'S3']) - obs = _reindex_with_metadata('pet', ['pet', 'toy'], data) + median, counts = _reindex_with_metadata('pet', ['pet', 'toy'], data) exp_col = pd.MultiIndex(levels=[[1, 200, 'pet', 'toy'], [1, 2, '']], - labels=[[0, 0, 1, 1], [0, 1, 0, 1]], + codes=[[0, 0, 1, 1], [0, 1, 0, 1]], names=['depth', 'iter']) exp_ind = pd.Index(['milo', 'peanut', 'russ'], name='pet') exp = pd.DataFrame(data=[[5, 6, 7, 8], [9, 10, 11, 12], [1, 2, 3, 4]], columns=exp_col, index=exp_ind) - pdt.assert_frame_equal(exp, obs[0]) + pdt.assert_frame_equal(exp, median) exp = pd.DataFrame(data=[[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]], columns=exp_col, index=exp_ind) - pdt.assert_frame_equal(exp, obs[1]) + pdt.assert_frame_equal(exp, counts) - obs = _reindex_with_metadata('toy', ['pet', 'toy'], data) + median, counts = _reindex_with_metadata('toy', ['pet', 'toy'], data) exp_ind = pd.Index(['stick', 'yeti'], name='toy') exp = pd.DataFrame(data=[[5, 6, 7, 8], [5, 6, 7, 8]], columns=exp_col, index=exp_ind) - pdt.assert_frame_equal(exp, obs[0]) + pdt.assert_frame_equal(exp, median) exp = pd.DataFrame(data=[[2, 2, 2, 2], [1, 1, 1, 1]], columns=exp_col, index=exp_ind) - pdt.assert_frame_equal(exp, obs[1]) + pdt.assert_frame_equal(exp, counts) class AlphaRarefactionJSONPTests(unittest.TestCase):