diff --git a/CHANGELOG.md b/CHANGELOG.md
index 580c4fca6..fcb4908f7 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -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)
diff --git a/src/hdmf/term_set.py b/src/hdmf/term_set.py
index c545e2d90..f7169bdfd 100644
--- a/src/hdmf/term_set.py
+++ b/src/hdmf/term_set.py
@@ -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 = "" + "Schema Path: " + "" + self.term_schema_path + "
"
+ re += "" + "Sources: " + "" + ", ".join(list(self.sources.keys())) + "
"
+ re += " Terms: "
+ if len(terms) > 4:
+ re += "
%s " % terms[0]
+ re += " %s " % terms[1]
+ re += " %s " % terms[2]
+ re += "... ..."
+ re += " %s " % terms[-1]
+ else:
+ for term in terms:
+ re += " %s " % term
+ re += " Number of terms: %s" % len(terms)
return re
def __perm_value_key_info(self, perm_values_dict: dict, key: str):
diff --git a/tests/unit/example_test_term_set.yaml b/tests/unit/example_test_term_set.yaml
index 6595cdc0b..e952c6776 100644
--- a/tests/unit/example_test_term_set.yaml
+++ b/tests/unit/example_test_term_set.yaml
@@ -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
diff --git a/tests/unit/example_test_term_set2.yaml b/tests/unit/example_test_term_set2.yaml
new file mode 100644
index 000000000..2a20b6e5c
--- /dev/null
+++ b/tests/unit/example_test_term_set2.yaml
@@ -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
diff --git a/tests/unit/test_term_set.py b/tests/unit/test_term_set.py
index 43a3f92b9..b4a469438 100644
--- a/tests/unit/test_term_set.py
+++ b/tests/unit/test_term_set.py
@@ -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 = ('Schema Path: tests/unit/example_test_term_set2.yaml
Sources:'
+ ' NCBI_TAXON
Terms: Homo sapiens Mus musculus'
+ ' Ursus arctos horribilis Number of terms: 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 = ('Schema Path: tests/unit/example_test_term_set.yaml
Sources:'
+ ' NCBI_TAXON
Terms: Homo sapiens Mus musculus'
+ ' Ursus arctos horribilis ... ... Ailuropoda melanoleuca'
+ ' Number of terms: 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)
@@ -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')