def test_block_diagonal_error(self): with self.assertRaises(ValueError): block_diagonal(3, 4, 1) with self.assertRaises(ValueError): block_diagonal(3, 4, 0)
def test_block_diagonal_3x4(self): np.random.seed(0) res = block_diagonal(3, 4, 2) exp = np.array([[0.548814, 0., 0.], [0.715189, 0., 0.], [0., 0.602763, 0.544883], [0., 0.423655, 0.645894]]) npt.assert_allclose(res, exp, rtol=1e-5, atol=1e-5)
def test_block_diagonal_4x4(self): np.random.seed(0) res = block_diagonal(4, 4, 2) exp = np.array([[0.5488135, 0.71518937, 0., 0.], [0.60276338, 0.54488318, 0., 0.], [0., 0., 0.4236548, 0.64589411], [0., 0., 0.43758721, 0.891773]]) npt.assert_allclose(res, exp, rtol=1e-5, atol=1e-5)
def test_highlights(self): table = pd.DataFrame(block_diagonal(ncols=5, nrows=5, nblocks=2), index=['0', '1', '2', '3', '4'], columns=['0', '1', '2', '3', '4']) t = rank_linkage( pd.Series([1, 2, 3, 4, 5], index=['0', '1', '2', '3', '4'])) t = SquareDendrogram.from_tree(t) md = pd.Series(['a', 'a', 'a', 'b', 'b'], index=['0', '1', '2', '3', '4']) for i, n in enumerate(t.postorder()): if not n.is_tip(): n.name = "y%d" % i n.length = np.random.rand() * 3 highlights = pd.DataFrame({ 'y8': ['#FF0000', '#00FF00'], 'y7': ['#0000FF', '#F0000F'] }).T fig = heatmap(table, t, md, highlights) # Test to see if the lineages of the tree are ok lines = list(fig.get_axes()[0].get_lines()) pts = self.t.coords(width=20, height=self.table.shape[0]) pts['y'] = pts['y'] - 0.5 # account for offset pts['x'] = pts['x'].astype(np.float) pts['y'] = pts['y'].astype(np.float) exp_coords = np.array([[6.33333333, 3.5], [6.33333333, 4.], [6.33333333, 4.], [20., 4.], [12.66666667, 0.5], [12.66666667, 1.], [12.66666667, 1.], [20., 1.], [6.33333333, 1.25], [6.33333333, 2.], [6.33333333, 2.], [20., 2.], [0., 2.375], [0., 3.5], [0., 3.5], [6.33333333, 3.5], [6.33333333, 3.5], [6.33333333, 3.], [6.33333333, 3.], [20., 3.], [12.66666667, 0.5], [12.66666667, 0.], [12.66666667, 0.], [20., 0.], [6.33333333, 1.25], [6.33333333, 0.5], [6.33333333, 0.5], [12.66666667, 0.5], [0., 2.375], [0., 1.25], [0., 1.25], [6.33333333, 1.25]]) res = np.vstack([i._xy for i in lines]) npt.assert_allclose(exp_coords, res) # Make sure that the metadata labels are set properly res = str(fig.get_axes()[2].get_xticklabels(minor=True)[0]) self.assertEqual(res, "Text(0, 0, 'a')") res = str(fig.get_axes()[2].get_xticklabels(minor=True)[1]) self.assertEqual(res, "Text(0, 0, 'b')") print([str(i) for i in fig.get_axes()[1].get_xticklabels()]) # Make sure that the highlight labels are set properly res = str(fig.get_axes()[1].get_xticklabels()[0]) self.assertEqual(res, "Text(0, 0, 'y8')") res = str(fig.get_axes()[1].get_xticklabels()[1]) self.assertEqual(res, "Text(0, 0, 'y7')") # Test to see if the highlights are ok res = fig.get_axes()[2].get_position()._points exp = np.array([[0.24, 0.1], [0.808, 0.9]]) npt.assert_allclose(res, exp)
def test_highlights(self): table = pd.DataFrame(block_diagonal(ncols=5, nrows=5, nblocks=2), index=['0', '1', '2', '3', '4'], columns=['0', '1', '2', '3', '4']) t = rank_linkage(pd.Series([1, 2, 3, 4, 5], index=['0', '1', '2', '3', '4'])) t = SquareDendrogram.from_tree(t) md = pd.Series(['a', 'a', 'a', 'b', 'b'], index=['0', '1', '2', '3', '4']) for i, n in enumerate(t.postorder()): if not n.is_tip(): n.name = "y%d" % i n.length = np.random.rand()*3 highlights = pd.DataFrame({'y8': ['#FF0000', '#00FF00'], 'y7': ['#0000FF', '#F0000F']}).T fig = heatmap(table, t, md, highlights) # Test to see if the lineages of the tree are ok lines = list(fig.get_axes()[0].get_lines()) pts = self.t.coords(width=20, height=self.table.shape[0]) pts['y'] = pts['y'] - 0.5 # account for offset pts['x'] = pts['x'].astype(np.float) pts['y'] = pts['y'].astype(np.float) exp_coords = np.array([[6.33333333, 3.5], [6.33333333, 4.], [6.33333333, 4.], [20., 4.], [12.66666667, 0.5], [12.66666667, 1.], [12.66666667, 1.], [20., 1.], [6.33333333, 1.25], [6.33333333, 2.], [6.33333333, 2.], [20., 2.], [0., 2.375], [0., 3.5], [0., 3.5], [6.33333333, 3.5], [6.33333333, 3.5], [6.33333333, 3.], [6.33333333, 3.], [20., 3.], [12.66666667, 0.5], [12.66666667, 0.], [12.66666667, 0.], [20., 0.], [6.33333333, 1.25], [6.33333333, 0.5], [6.33333333, 0.5], [12.66666667, 0.5], [0., 2.375], [0., 1.25], [0., 1.25], [6.33333333, 1.25]]) res = np.vstack([i._xy for i in lines]) npt.assert_allclose(exp_coords, res) # Make sure that the metadata labels are set properly res = str(fig.get_axes()[2].get_xticklabels(minor=True)[0]) self.assertEqual(res, "Text(0,0,'a')") res = str(fig.get_axes()[2].get_xticklabels(minor=True)[1]) self.assertEqual(res, "Text(0,0,'b')") # Make sure that the highlight labels are set properly res = str(fig.get_axes()[1].get_xticklabels()[0]) self.assertEqual(res, "Text(0,0,'y7')") res = str(fig.get_axes()[1].get_xticklabels()[1]) self.assertEqual(res, "Text(0,0,'y8')") # Test to see if the highlights are ok res = fig.get_axes()[2].get_position()._points exp = np.array([[0.24, 0.1], [0.808, 0.9]]) npt.assert_allclose(res, exp)