def test_add_single_synonym(self): synonym_key = "synonym_key_1" TaxonomyManager.register_synonym_key("synonym_key_1") data = [ { 'id': 0, 'full_name': 'Family One', 'rank_name': 'One', 'rank': niamoto_db_meta.TaxonRankEnum.FAMILIA, 'parent_id': None, 'synonyms': {}, 'mptt_left': 0, 'mptt_right': 0, 'mptt_tree_id': 0, 'mptt_depth': 0, }, ] ins = niamoto_db_meta.taxon.insert().values(data) with Connector.get_connection() as connection: connection.execute(ins) TaxonomyManager.add_synonym_for_single_taxon(0, synonym_key, 1) df1 = TaxonomyManager.get_raw_taxon_dataframe() self.assertEqual(df1.loc[0]['synonyms'], {synonym_key: 1}) TaxonomyManager.add_synonym_for_single_taxon(0, synonym_key, 2) df2 = TaxonomyManager.get_raw_taxon_dataframe() self.assertEqual(df2.loc[0]['synonyms'], { synonym_key: 1, synonym_key: 2, })
def test_get_not_empty_raw_taxon_dataset(self): data = [ { 'id': 0, 'full_name': 'Family One', 'rank_name': 'One', 'rank': niamoto_db_meta.TaxonRankEnum.FAMILIA, 'parent_id': None, 'synonyms': {}, 'mptt_left': 0, 'mptt_right': 0, 'mptt_tree_id': 0, 'mptt_depth': 0, }, { 'id': 1, 'full_name': 'Genus Two', 'rank_name': 'Two', 'rank': niamoto_db_meta.TaxonRankEnum.GENUS, 'parent_id': 0, 'synonyms': {}, 'mptt_left': 0, 'mptt_right': 0, 'mptt_tree_id': 0, 'mptt_depth': 0, }, { 'id': 2, 'full_name': 'Species Three', 'rank_name': 'Three', 'rank': niamoto_db_meta.TaxonRankEnum.SPECIES, 'parent_id': None, 'synonyms': {}, 'mptt_left': 1, 'mptt_right': 0, 'mptt_tree_id': 0, 'mptt_depth': 0, }, ] ins = niamoto_db_meta.taxon.insert().values(data) with Connector.get_connection() as connection: connection.execute(ins) df1 = TaxonomyManager.get_raw_taxon_dataframe() self.assertEqual(len(df1), 3)
def test_construct_mptt_less_simple_tree(self): tree = [[1, [1, [ 1, ]]], [ 1, ], [ 1, ], [ 1, ], [1, [3, [1, [1, [ 1, ]]]]]] data, last_id = make_taxon_tree(tree) self.assertEqual(len(data), 24) ins = niamoto_db_meta.taxon.insert().values(data) with Connector.get_connection() as connection: connection.execute(ins) TaxonomyManager.make_mptt() mptt = TaxonomyManager.get_raw_taxon_dataframe() self.assertEqual(list(mptt['mptt_tree_id']), [ 1, 1, 1, 1, 1, 1, 7, 7, 9, 9, 11, 11, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13 ]) self.assertEqual(list(mptt['mptt_left']), [ 1, 2, 4, 5, 8, 9, 1, 2, 1, 2, 1, 2, 1, 2, 4, 5, 7, 9, 12, 13, 15, 16, 19, 20 ]) self.assertEqual(list(mptt['mptt_right']), [ 12, 3, 7, 6, 11, 10, 4, 3, 4, 3, 4, 3, 24, 3, 11, 6, 8, 10, 23, 14, 18, 17, 22, 21 ]) self.assertEqual(list(mptt['mptt_depth']), [ 0, 1, 1, 2, 1, 2, 0, 1, 0, 1, 0, 1, 0, 1, 1, 2, 2, 2, 1, 2, 2, 3, 2, 3 ]) self.assertEqual([ int(i) if pd.notnull(i) else None for i in mptt['parent_id'].tolist() ], [ None, 1, 1, 3, 1, 5, None, 7, None, 9, None, 11, None, 13, 13, 15, 15, 15, 13, 19, 19, 21, 19, 23 ])
def test_construct_mptt_simple_tree(self): tree = [[1, [ 1, ]], [ 1, ]] data, last_id = make_taxon_tree(tree) ins = niamoto_db_meta.taxon.insert().values(data) with Connector.get_connection() as connection: connection.execute(ins) TaxonomyManager.make_mptt() mptt = TaxonomyManager.get_raw_taxon_dataframe() self.assertEqual(list(mptt['mptt_tree_id']), [1, 1, 1, 1, 5, 5]) self.assertEqual(list(mptt['mptt_left']), [1, 2, 4, 5, 1, 2]) self.assertEqual(list(mptt['mptt_right']), [8, 3, 7, 6, 4, 3]) self.assertEqual(list(mptt['mptt_depth']), [0, 1, 1, 2, 0, 1]) self.assertEqual([ int(i) if pd.notnull(i) else None for i in mptt['parent_id'].tolist() ], [None, 1, 1, 3, None, 5])
def test_get_empty_raw_taxon_dataset(self): df1 = TaxonomyManager.get_raw_taxon_dataframe() self.assertEqual(len(df1), 0)
def test_set_taxonomy(self): result, synonyms = TaxonomyManager.set_taxonomy( pd.DataFrame( columns=['full_name', 'rank_name', 'rank', 'parent_id'], )) self.assertEqual(result, 0) data = pd.DataFrame.from_records([ { 'id': 0, 'full_name': 'Family One', 'rank_name': 'One', 'rank': niamoto_db_meta.TaxonRankEnum.FAMILIA, 'parent_id': None, }, ], index='id') result, synonyms = TaxonomyManager.set_taxonomy(data) self.assertEqual(result, 1) data = pd.DataFrame.from_records([ { 'id': 0, 'full_name': 'Family One', 'rank_name': 'One', 'rank': niamoto_db_meta.TaxonRankEnum.FAMILIA, 'parent_id': None, 'gbif': 5, 'taxref': 1, }, { 'id': 1, 'full_name': 'Genus Two', 'rank_name': 'Two', 'rank': niamoto_db_meta.TaxonRankEnum.GENUS, 'parent_id': 0, 'gbif': 10, 'taxref': 2, }, { 'id': 2, 'full_name': 'Species Three', 'rank_name': 'Three', 'rank': niamoto_db_meta.TaxonRankEnum.SPECIES, 'parent_id': None, 'gbif': 7, 'taxref': 3, }, ], index='id') result, synonyms = TaxonomyManager.set_taxonomy(data) self.assertEqual(result, 3) df = TaxonomyManager.get_raw_taxon_dataframe() self.assertEqual(len(df), 3) synonym_keys = TaxonomyManager.get_synonym_keys() self.assertEqual(len(synonym_keys), 2) identity_synonyms = TaxonomyManager.get_synonyms_for_key( TaxonomyManager.IDENTITY_SYNONYM_KEY) self.assertEqual(len(identity_synonyms), 3) null_synonyms = TaxonomyManager.get_synonyms_for_key(None) self.assertEqual(len(null_synonyms), 0) gbif_synonyms = TaxonomyManager.get_synonyms_for_key("gbif") self.assertEqual(len(gbif_synonyms), 3) taxref_synonyms = TaxonomyManager.get_synonyms_for_key('taxref') self.assertEqual(len(taxref_synonyms), 3)