From ed81fc6b3ff58ba1207a22e6277cc2f3640c6ef4 Mon Sep 17 00:00:00 2001 From: Jamie Morton Date: Wed, 17 Aug 2016 22:40:56 -0700 Subject: [PATCH] TST: Adding scenario where intersection yield empty tree, table, metadata --- gneiss/_formula.py | 7 +++++++ gneiss/tests/test_formula.py | 40 ++++++++++++++++++++++++++++++++++-- 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/gneiss/_formula.py b/gneiss/_formula.py index 420226d..ab63a6f 100644 --- a/gneiss/_formula.py +++ b/gneiss/_formula.py @@ -22,6 +22,13 @@ def _intersect_of_table_metadata_tree(table, metadata, tree): _table, _tree = match_tips(_table, tree) non_tips_no_name = [(n.name is None) for n in _tree.levelorder() if not n.is_tip()] + if len(non_tips_no_name) == 0: + raise ValueError('There are no internal nodes in `tree` after' + 'intersection with `table`.') + + if len(_table.index) == 0: + raise ValueError('There are no internal nodes in `table` after ' + 'intersection with `metadata`.') if any(non_tips_no_name): _tree = rename_internal_nodes(_tree) diff --git a/gneiss/tests/test_formula.py b/gneiss/tests/test_formula.py index 419879c..c45259f 100644 --- a/gneiss/tests/test_formula.py +++ b/gneiss/tests/test_formula.py @@ -111,8 +111,44 @@ def test_ols_immutable(self): self.assertEqual(str(table), str(exp_table)) self.assertEqual(str(exp_tree), str(tree)) - def test_ols_empty(self): - pass + def test_ols_empty_table(self): + A = np.array # aliasing for the sake of pep8 + table = pd.DataFrame({ + 's1': ilr_inv(A([1., 1.])), + 's2': ilr_inv(A([1., 2.])), + 's3': ilr_inv(A([1., 3.])), + 's4': ilr_inv(A([1., 4.])), + 's5': ilr_inv(A([1., 5.])), + 's6': ilr_inv(A([1., 5.]))}, + index=['x', 'y', 'z']).T + + tree = TreeNode.read(['((c,d),(b,a)Y2)Y1;']) + metadata = pd.DataFrame({ + 'lame': [1, 1, 1, 1, 1], + 'real': [1, 2, 3, 4, 5] + }, index=['s1', 's2', 's3', 's4', 's5']) + with self.assertRaises(ValueError): + ols('real + lame', table, metadata, tree) + + def test_ols_empty_metadata(self): + A = np.array # aliasing for the sake of pep8 + table = pd.DataFrame({ + 'k1': ilr_inv(A([1., 1.])), + 'k2': ilr_inv(A([1., 2.])), + 'k3': ilr_inv(A([1., 3.])), + 'k4': ilr_inv(A([1., 4.])), + 'k5': ilr_inv(A([1., 5.])), + 'k6': ilr_inv(A([1., 5.]))}, + index=['a', 'b', 'c']).T + + tree = TreeNode.read(['((c,d),(b,a)Y2)Y1;']) + metadata = pd.DataFrame({ + 'lame': [1, 1, 1, 1, 1], + 'real': [1, 2, 3, 4, 5] + }, index=['s1', 's2', 's3', 's4', 's5']) + with self.assertRaises(ValueError): + ols('real + lame', table, metadata, tree) + if __name__ == '__main__': unittest.main()