Skip to content

Commit

Permalink
TermSet minor test updates, _repr_html_, name field (#967)
Browse files Browse the repository at this point in the history
  • Loading branch information
mavaylon1 committed Oct 27, 2023
1 parent 75c686a commit 56956ae
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Enhancements
- Added `target_tables` attribute to `DynamicTable` to allow users to specify the target table of any predefined
`DynamicTableRegion` columns of a `DynamicTable` subclass. @rly [#971](https://github.com/hdmf-dev/hdmf/pull/971)
- Updated `TermSet` to include `_repr_html_` for easy to read notebook representation. @mavaylon1 [967](https://github.com/hdmf-dev/hdmf/pull/967)

### Bug fixes
- Updated custom class generation to handle specs with fixed values and required names. @rly [#800](https://github.com/hdmf-dev/hdmf/pull/800)
Expand Down
36 changes: 34 additions & 2 deletions src/hdmf/term_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,43 @@ def __init__(self,
self.expanded_termset_path = self.__enum_expander()
self.view = SchemaView(self.expanded_termset_path)

self.name = self.view.schema.name
self.sources = self.view.schema.prefixes

def __repr__(self):
re = "class: %s\n" % str(self.__class__)
re += "term_schema_path: %s\n" % self.term_schema_path
terms = list(self.view_set.keys())

re = "Schema Path: %s\n" % self.term_schema_path
re += "Sources: " + ", ".join(list(self.sources.keys()))+"\n"
re += "Terms: \n"
if len(terms) > 4:
re += " - %s\n" % terms[0]
re += " - %s\n" % terms[1]
re += " - %s\n" % terms[2]
re += " ... ... \n"
re += " - %s\n" % terms[-1]
else:
for term in terms:
re += " - %s\n" % term
re += "Number of terms: %s" % len(terms)
return re

def _repr_html_(self):
terms = list(self.view_set.keys())

re = "<b>" + "Schema Path: " + "</b>" + self.term_schema_path + "<br>"
re += "<b>" + "Sources: " + "</b>" + ", ".join(list(self.sources.keys())) + "<br>"
re += "<b> Terms: </b>"
if len(terms) > 4:
re += "<li> %s </li>" % terms[0]
re += "<li> %s </li>" % terms[1]
re += "<li> %s </li>" % terms[2]
re += "... ..."
re += "<li> %s </li>" % terms[-1]
else:
for term in terms:
re += "<li> %s </li>" % term
re += "<i> Number of terms:</i> %s" % len(terms)
return re

def __perm_value_key_info(self, perm_values_dict: dict, key: str):
Expand Down
3 changes: 3 additions & 0 deletions tests/unit/example_test_term_set.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,6 @@ enums:
Myrmecophaga tridactyla:
description: the species is an anteater
meaning: NCBI_TAXON:71006
Ailuropoda melanoleuca:
description: the species is a panda
meaning: NCBI_TAXON:9646
21 changes: 21 additions & 0 deletions tests/unit/example_test_term_set2.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
id: termset/species_example2
name: Species
version: 0.0.1
prefixes:
NCBI_TAXON: https://www.ncbi.nlm.nih.gov/Taxonomy/Browser/wwwtax.cgi?mode=Info&id=
imports:
- linkml:types
default_range: string

enums:
Species:
permissible_values:
Homo sapiens:
description: the species is human
meaning: NCBI_TAXON:9606
Mus musculus:
description: the species is a house mouse
meaning: NCBI_TAXON:10090
Ursus arctos horribilis:
description: the species is a grizzly bear
meaning: NCBI_TAXON:116960
37 changes: 35 additions & 2 deletions tests/unit/test_term_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,41 @@ def setUp(self):

def test_termset_setup(self):
termset = TermSet(term_schema_path='tests/unit/example_test_term_set.yaml')
self.assertEqual(termset.name, 'Species')
self.assertEqual(list(termset.sources), ['NCBI_TAXON'])

def test_repr_short(self):
termset = TermSet(term_schema_path='tests/unit/example_test_term_set2.yaml')
output = ('Schema Path: tests/unit/example_test_term_set2.yaml\nSources: NCBI_TAXON\nTerms: \n'
' - Homo sapiens\n - Mus musculus\n - Ursus arctos horribilis\nNumber of terms: 3')
self.assertEqual(repr(termset), output)

def test_repr_html_short(self):
termset = TermSet(term_schema_path='tests/unit/example_test_term_set2.yaml')
output = ('<b>Schema Path: </b>tests/unit/example_test_term_set2.yaml<br><b>Sources:'
' </b>NCBI_TAXON<br><b> Terms: </b><li> Homo sapiens </li><li> Mus musculus'
' </li><li> Ursus arctos horribilis </li><i> Number of terms:</i> 3')
self.assertEqual(termset._repr_html_(), output)

def test_repr_long(self):
termset = TermSet(term_schema_path='tests/unit/example_test_term_set.yaml')
output = ('Schema Path: tests/unit/example_test_term_set.yaml\nSources: NCBI_TAXON\nTerms: \n'
' - Homo sapiens\n - Mus musculus\n - Ursus arctos horribilis\n ... ... \n'
' - Ailuropoda melanoleuca\nNumber of terms: 5')
self.assertEqual(repr(termset), output)

def test_repr_html_long(self):
termset = TermSet(term_schema_path='tests/unit/example_test_term_set.yaml')
output = ('<b>Schema Path: </b>tests/unit/example_test_term_set.yaml<br><b>Sources:'
' </b>NCBI_TAXON<br><b> Terms: </b><li> Homo sapiens </li><li> Mus musculus'
' </li><li> Ursus arctos horribilis </li>... ...<li> Ailuropoda melanoleuca'
' </li><i> Number of terms:</i> 5')
self.assertEqual(termset._repr_html_(), output)

def test_view_set(self):
termset = TermSet(term_schema_path='tests/unit/example_test_term_set.yaml')
expected = ['Homo sapiens', 'Mus musculus', 'Ursus arctos horribilis', 'Myrmecophaga tridactyla']
expected = ['Homo sapiens', 'Mus musculus', 'Ursus arctos horribilis', 'Myrmecophaga tridactyla',
'Ailuropoda melanoleuca']
self.assertEqual(list(termset.view_set), expected)
self.assertIsInstance(termset.view, SchemaView)

Expand All @@ -46,7 +76,10 @@ def test_get_item(self):
termset = TermSet(term_schema_path='tests/unit/example_test_term_set.yaml')
self.assertEqual(termset['Homo sapiens'].id, 'NCBI_TAXON:9606')
self.assertEqual(termset['Homo sapiens'].description, 'the species is human')
self.assertEqual(termset['Homo sapiens'].meaning, 'https://www.ncbi.nlm.nih.gov/Taxonomy/Browser/wwwtax.cgi?mode=Info&id=9606')
self.assertEqual(
termset['Homo sapiens'].meaning,
'https://www.ncbi.nlm.nih.gov/Taxonomy/Browser/wwwtax.cgi?mode=Info&id=9606'
)

def test_get_item_key_error(self):
termset = TermSet(term_schema_path='tests/unit/example_test_term_set.yaml')
Expand Down

0 comments on commit 56956ae

Please sign in to comment.