def setUp(self):
     self.tstr = '(((((feature-2)e)d,feature-1)c)b)a;'
     self.obj = GroupTaxonomy(name='sample-2',
                              taxonomy=self.tstr,
                              features=['feature-1', 'feature-2'],
                              feature_values=[1. / 5, 4. / 5],
                              feature_variances=[0.0, 0.0])
class GroupTaxonomyTests(unittest.TestCase):
    def setUp(self):
        self.tstr = '(((((feature-2)e)d,feature-1)c)b)a;'
        self.obj = GroupTaxonomy(name='sample-2',
                                 taxonomy=self.tstr,
                                 features=['feature-1', 'feature-2'],
                                 feature_values=[1. / 5, 4. / 5],
                                 feature_variances=[0.0, 0.0])

    def test_init(self):
        self.assertEqual(self.obj.name, 'sample-2')
        self.assertEqual(self.obj.taxonomy, self.tstr)
        self.assertEqual(self.obj.features, ['feature-1', 'feature-2'])
        self.assertEqual(self.obj.feature_values, [1. / 5, 4. / 5])
        self.assertEqual(self.obj.feature_variances, [0.0, 0.0])

    def test_init_tree_missing_feature(self):
        with self.assertRaisesRegex(UnknownID, "is not in the taxonomy."):
            GroupTaxonomy(name='sample-2',
                          taxonomy=self.tstr,
                          features=['feature-1', 'feature-3'],
                          feature_values=[1. / 5, 4. / 5],
                          feature_variances=[0.0, 0.0])

    def test_init_feature_value_lengths(self):
        with self.assertRaisesRegex(ValueError, "length mismatch"):
            GroupTaxonomy(name='sample-2',
                          taxonomy=self.tstr + 'feature-3',
                          features=['feature-1', 'feature-2', 'feature-3'],
                          feature_values=[1. / 5, 4. / 5],
                          feature_variances=[0.0, 0.0])

        with self.assertRaisesRegex(ValueError, "length mismatch"):
            GroupTaxonomy(name='sample-2',
                          taxonomy=self.tstr,
                          features=['feature-1', 'feature-2'],
                          feature_values=[
                              1. / 5,
                          ],
                          feature_variances=[0.0, 0.0])

    def test_to_dict(self):
        exp = {
            'name': 'sample-2',
            'taxonomy': self.tstr,
            'features': ['feature-1', 'feature-2'],
            'feature_values': [1. / 5, 4. / 5],
            'feature_variances': [0.0, 0.0]
        }
        obs = self.obj.to_dict()
        self.assertEqual(obs, exp)

    def test_str(self):
        exp = str(self.obj.to_dict())
        obs = str(self.obj)
        self.assertEqual(obs, exp)
    def test_init_feature_value_lengths(self):
        with self.assertRaisesRegex(ValueError, "length mismatch"):
            GroupTaxonomy(name='sample-2',
                          taxonomy=self.tstr + 'feature-3',
                          features=['feature-1', 'feature-2', 'feature-3'],
                          feature_values=[1. / 5, 4. / 5],
                          feature_variances=[0.0, 0.0])

        with self.assertRaisesRegex(ValueError, "length mismatch"):
            GroupTaxonomy(name='sample-2',
                          taxonomy=self.tstr,
                          features=['feature-1', 'feature-2'],
                          feature_values=[
                              1. / 5,
                          ],
                          feature_variances=[0.0, 0.0])
 def test_init_tree_missing_feature(self):
     with self.assertRaisesRegex(UnknownID, "is not in the taxonomy."):
         GroupTaxonomy(name='sample-2',
                       taxonomy=self.tstr,
                       features=['feature-1', 'feature-3'],
                       feature_values=[1. / 5, 4. / 5],
                       feature_variances=[0.0, 0.0])
 def test_get_group(self):
     taxonomy = Taxonomy(self.table, self.taxonomy_df)
     exp = GroupTaxonomy(name='sample-2',
                         taxonomy='((((feature-1,((feature-2)e)d)c)b)a);',
                         features=['feature-1', 'feature-2'],
                         feature_values=[1. / 5, 4. / 5],
                         feature_variances=[0.0, 0.0])
     obs = taxonomy.get_group(['sample-2'])
     self.assertEqual(obs, exp)
 def test_get_group_with_variances(self):
     taxonomy = Taxonomy(self.table, self.taxonomy_df, self.table_vars)
     exp = GroupTaxonomy(
         name='sample-1',
         taxonomy='((((((feature-2)e)d)c)b,(((feature-3)h)g)f)a);',  # noqa
         features=['feature-2', 'feature-3'],
         feature_values=[2. / 5, 3. / 5],
         feature_variances=[2.0, 3.0])
     obs = taxonomy.get_group(['sample-1'])
     self.assertEqual(obs, exp)
 def test_get_group_multiple(self):
     taxonomy = Taxonomy(self.table, self.taxonomy_df)
     exp = GroupTaxonomy(
         name='foo',
         taxonomy=
         '((((feature-1,((feature-2)e)d)c)b,(((feature-3)h)g)f)a);',  # noqa
         features=['feature-1', 'feature-2', 'feature-3'],
         feature_values=[1. / 10, 6. / 10, 3. / 10],
         feature_variances=[0.0, 0.0, 0.0])
     obs = taxonomy.get_group(['sample-1', 'sample-2'], 'foo')
     self.assertEqual(obs.name, exp.name)
     self.assertEqual(obs.taxonomy, exp.taxonomy)
     self.assertEqual(obs.features, exp.features)
     npt.assert_almost_equal(obs.feature_values, exp.feature_values)
     self.assertEqual(obs.feature_variances, exp.feature_variances)