def test_unifrac_matrix(self): """unifrac_matrix should return correct results for model tree""" m = array([[1,0,1],[1,1,0],[0,1,0],[0,0,1],[0,1,0],[0,1,1],[1,1,1],\ [0,1,1],[1,1,1]]) bl = self.branch_lengths result = unifrac_matrix(bl, m) self.assertEqual(result, array([[0, 10/16.,8/13.],[10/16.,0,8/17.],\ [8/13.,8/17.,0]])) #should work if we tell it the measure is asymmetric result = unifrac_matrix(bl, m, is_symmetric=False) self.assertEqual(result, array([[0, 10/16.,8/13.],[10/16.,0,8/17.],\ [8/13.,8/17.,0]])) #should work if the measure really is asymmetric result = unifrac_matrix(bl, m, metric=unnormalized_G, is_symmetric=False) self.assertEqual(result, array([[0, 1/17.,2/17.],[9/17.,0,6/17.],\ [6/17.,2/17.,0]])) #should also match web site calculations envs = self.count_array bound_indices = bind_to_array(self.nodes, envs) bool_descendants(bound_indices) result = unifrac_matrix(bl, envs) exp = array([[0, 0.6250, 0.6154], [0.6250, 0, \ 0.4706], [0.6154, 0.4707, 0]]) assert (abs(result - exp)).max() < 0.001
def test_unifrac_one_sample(self): """unifrac_one_sample should match unifrac_matrix""" m = array([[1, 0, 1], [1, 1, 0], [0, 1, 0], [0, 0, 1], [0, 1, 0], [0, 1, 1], [1, 1, 1], [0, 1, 1], [1, 1, 1]]) bl = self.branch_lengths result = unifrac_matrix(bl, m) for i in range(len(result)): one_sam_res = unifrac_one_sample(i, bl, m) self.assertEqual(result[i], one_sam_res) self.assertEqual(result[:, i], one_sam_res) # should work ok on asymmetric metrics result = unifrac_matrix(bl, m, metric=unnormalized_G, is_symmetric=False) for i in range(len(result)): one_sam_res = unifrac_one_sample(i, bl, m, metric=unnormalized_G) self.assertEqual(result[i], one_sam_res)
def test_unifrac_matrix(self): """unifrac_matrix should return correct results for model tree""" m = array([[1, 0, 1], [1, 1, 0], [0, 1, 0], [0, 0, 1], [0, 1, 0], [0, 1, 1], [1, 1, 1], [0, 1, 1], [1, 1, 1]]) bl = self.branch_lengths result = unifrac_matrix(bl, m) self.assertEqual(result, array([[0, 10 / 16.0, 8 / 13.0], [10 / 16.0, 0, 8 / 17.0], [8 / 13.0, 8 / 17.0, 0]])) # should work if we tell it the measure is asymmetric result = unifrac_matrix(bl, m, is_symmetric=False) self.assertEqual(result, array([[0, 10 / 16.0, 8 / 13.0], [10 / 16.0, 0, 8 / 17.0], [8 / 13.0, 8 / 17.0, 0]])) # should work if the measure really is asymmetric result = unifrac_matrix(bl, m, metric=unnormalized_G, is_symmetric=False) self.assertEqual(result, array([[0, 1 / 17.0, 2 / 17.0], [9 / 17.0, 0, 6 / 17.0], [6 / 17.0, 2 / 17.0, 0]])) # should also match web site calculations envs = self.count_array bound_indices = bind_to_array(self.nodes, envs) bool_descendants(bound_indices) result = unifrac_matrix(bl, envs) exp = array([[0, 0.6250, 0.6154], [0.6250, 0, 0.4706], [0.6154, 0.4707, 0]]) assert (abs(result - exp)).max() < 0.001
def test_unifrac_one_sample(self): """unifrac_one_sample should match unifrac_matrix""" m = array([[1,0,1],[1,1,0],[0,1,0],[0,0,1],[0,1,0],[0,1,1],[1,1,1],\ [0,1,1],[1,1,1]]) bl = self.branch_lengths result = unifrac_matrix(bl, m) for i in range(len(result)): one_sam_res = unifrac_one_sample(i, bl, m) self.assertEqual(result[i], one_sam_res) self.assertEqual(result[:, i], one_sam_res) #should work ok on asymmetric metrics result = unifrac_matrix(bl, m, metric=unnormalized_G, is_symmetric=False) for i in range(len(result)): one_sam_res = unifrac_one_sample(i, bl, m, metric=unnormalized_G) self.assertEqual(result[i], one_sam_res)