Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TermSet minor test updates, _repr_html_, name field #967

Merged
merged 20 commits into from
Oct 27, 2023
Merged
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# HDMF Changelog

## HDMF 3.10.1 (Upcoming)

### Enhancements
- Updated `TermSet` to include `_repr_html_` for easy to read notebook representation. @mavaylon1 [967](https://github.com/hdmf-dev/hdmf/pull/967)

## HDMF 3.10.0 (October 3, 2023)

Since version 3.9.1 should have been released as 3.10.0 but failed to release on PyPI and conda-forge, this release
Expand Down
6 changes: 3 additions & 3 deletions docs/gallery/plot_term_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
# spreadsheet, the spreadsheet needs to be saved as individual tsv files, i.e., one tsv file per spreadsheet tab. Please
# refer to the Schemasheets tutorial link above for more details on the required syntax structure within the sheets.
# Once the tsv files are in a folder, the user simply provides the path to the folder with ``schemasheets_folder``.
termset = TermSet(schemasheets_folder=schemasheets_folder)
termset = TermSet(name='example', schemasheets_folder=schemasheets_folder)

# Use Dynamic Enumerations to populate TermSet
# --------------------------------------------
Expand All @@ -102,7 +102,7 @@
# Please refer to the LinkMl Dynamic Enumeration tutorial for more information on these sources and how to setup Dynamic
# Enumerations within the schema. Once the schema is ready, the user provides a path to the schema and set
# ``dynamic=True``. A new schema, with the populated permissible values, will be created in the same directory.
termset = TermSet(term_schema_path=dynamic_schema_path, dynamic=True)
termset = TermSet(name='example', term_schema_path=dynamic_schema_path, dynamic=True)

######################################################
# Viewing TermSet values
Expand All @@ -111,7 +111,7 @@
# method will return a dictionary of all the terms and the corresponding information for each term.
# Users can index specific terms from the :py:class:`~hdmf.term_set.TermSet`. LinkML runtime will need to be installed.
# You can do so by first running ``pip install linkml-runtime``.
terms = TermSet(term_schema_path=yaml_file)
terms = TermSet(name='example', term_schema_path=yaml_file)
print(terms.view_set)

# Retrieve a specific term
Expand Down
14 changes: 11 additions & 3 deletions src/hdmf/term_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ class TermSet:
:ivar expanded_termset_path: The path to the schema with the expanded enumerations
"""
def __init__(self,
name: str,
term_schema_path: str=None,
schemasheets_folder: str=None,
dynamic: bool=False
):
"""
:param name: The name of the TermSet
:param term_schema_path: The path to the LinkML YAML enumeration schema
:param schemasheets_folder: The path to the folder containing the LinkML TSV files
:param dynamic: Boolean parameter denoting whether the schema uses Dynamic Enumerations
Expand All @@ -35,6 +37,7 @@ def __init__(self,
msg = "Install linkml_runtime"
raise ValueError(msg)

self.name = name
mavaylon1 marked this conversation as resolved.
Show resolved Hide resolved
self.term_schema_path = term_schema_path
self.schemasheets_folder = schemasheets_folder

Expand All @@ -55,9 +58,14 @@ def __init__(self,

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
def __repr__(self): # pragma: no cover
re = "Class: %s\n" % str(self.__class__.__name__)
re += "Schema Path: %s\n" % self.term_schema_path
return re

def _repr_html_(self): # pragma: no cover
re = "<p><b>" + "Schema Path: " + "</b>" + self.term_schema_path + "<br>" + "</p>"
re += "<p><b>" + "Sources: " + "</b>" + ", ".join(list(self.sources.keys())) + "</p>"
mavaylon1 marked this conversation as resolved.
Show resolved Hide resolved
return re

def __perm_value_key_info(self, perm_values_dict: dict, key: str):
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/common/test_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ def test_add_ref_search_for_file_error(self):

@unittest.skipIf(not LINKML_INSTALLED, "optional LinkML module is not installed")
def test_check_termset_wrapper(self):
terms = TermSet(term_schema_path='tests/unit/example_test_term_set.yaml')
terms = TermSet(name='example', term_schema_path='tests/unit/example_test_term_set.yaml')

# create children and add parent
col1 = VectorData(
Expand All @@ -290,7 +290,7 @@ def test_check_termset_wrapper(self):

@unittest.skipIf(not LINKML_INSTALLED, "optional LinkML module is not installed")
def test_add_ref_termset_data(self):
terms = TermSet(term_schema_path='tests/unit/example_test_term_set.yaml')
terms = TermSet(name='example', term_schema_path='tests/unit/example_test_term_set.yaml')
er = HERD()
em = HERDManagerContainer()
em.link_resources(er)
Expand All @@ -313,7 +313,7 @@ def test_add_ref_termset_data(self):

@unittest.skipIf(not LINKML_INSTALLED, "optional LinkML module is not installed")
def test_add_ref_termset_attr(self):
terms = TermSet(term_schema_path='tests/unit/example_test_term_set.yaml')
terms = TermSet(name='example', term_schema_path='tests/unit/example_test_term_set.yaml')
er = HERD()
em = HERDManagerContainer()
em.link_resources(er)
Expand Down
10 changes: 5 additions & 5 deletions tests/unit/common/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def test_constructor_all_columns_are_iterators(self):

@unittest.skipIf(not LINKML_INSTALLED, "optional LinkML module is not installed")
def test_add_col_validate(self):
terms = TermSet(term_schema_path='tests/unit/example_test_term_set.yaml')
terms = TermSet(name='example', term_schema_path='tests/unit/example_test_term_set.yaml')
col1 = VectorData(
name='Species_1',
description='...',
Expand All @@ -139,7 +139,7 @@ def test_add_col_validate(self):

@unittest.skipIf(not LINKML_INSTALLED, "optional LinkML module is not installed")
def test_add_col_validate_bad_data(self):
terms = TermSet(term_schema_path='tests/unit/example_test_term_set.yaml')
terms = TermSet(name='example', term_schema_path='tests/unit/example_test_term_set.yaml')
col1 = VectorData(
name='Species_1',
description='...',
Expand All @@ -154,7 +154,7 @@ def test_add_col_validate_bad_data(self):

@unittest.skipIf(not LINKML_INSTALLED, "optional LinkML module is not installed")
def test_add_row_validate(self):
terms = TermSet(term_schema_path='tests/unit/example_test_term_set.yaml')
terms = TermSet(name='example', term_schema_path='tests/unit/example_test_term_set.yaml')
col1 = VectorData(
name='Species_1',
description='...',
Expand All @@ -176,7 +176,7 @@ def test_add_row_validate(self):

@unittest.skipIf(not LINKML_INSTALLED, "optional LinkML module is not installed")
def test_add_row_validate_bad_data_one_col(self):
terms = TermSet(term_schema_path='tests/unit/example_test_term_set.yaml')
terms = TermSet(name='example', term_schema_path='tests/unit/example_test_term_set.yaml')
col1 = VectorData(
name='Species_1',
description='...',
Expand All @@ -193,7 +193,7 @@ def test_add_row_validate_bad_data_one_col(self):

@unittest.skipIf(not LINKML_INSTALLED, "optional LinkML module is not installed")
def test_add_row_validate_bad_data_all_col(self):
terms = TermSet(term_schema_path='tests/unit/example_test_term_set.yaml')
terms = TermSet(name='example', term_schema_path='tests/unit/example_test_term_set.yaml')
col1 = VectorData(
name='Species_1',
description='...',
Expand Down
8 changes: 4 additions & 4 deletions tests/unit/test_io_hdf5_h5tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def test_write_dataset_string(self):
##########################################
@unittest.skipIf(not LINKML_INSTALLED, "optional LinkML module is not installed")
def test_write_dataset_TermSetWrapper(self):
terms = TermSet(term_schema_path='tests/unit/example_test_term_set.yaml')
terms = TermSet(name='example', term_schema_path='tests/unit/example_test_term_set.yaml')
a = TermSetWrapper(value=['Homo sapiens'], termset=terms)
self.io.write_dataset(self.f, DatasetBuilder('test_dataset', a, attributes={}))
dset = self.f['test_dataset']
Expand Down Expand Up @@ -826,7 +826,7 @@ def test_roundtrip_pathlib_path(self):

@unittest.skipIf(not LINKML_INSTALLED, "optional LinkML module is not installed")
def test_roundtrip_TermSetWrapper_dataset(self):
terms = TermSet(term_schema_path='tests/unit/example_test_term_set.yaml')
terms = TermSet(name='example', term_schema_path='tests/unit/example_test_term_set.yaml')
foo = Foo(name="species", attr1='attr1', attr2=0,
my_data=TermSetWrapper(value=['Homo sapiens', 'Mus musculus'],
termset=terms))
Expand All @@ -845,7 +845,7 @@ def test_roundtrip_TermSetWrapper_dataset(self):

@unittest.skipIf(not LINKML_INSTALLED, "optional LinkML module is not installed")
def test_roundtrip_TermSetWrapper_attribute(self):
terms = TermSet(term_schema_path='tests/unit/example_test_term_set.yaml')
terms = TermSet(name='example', term_schema_path='tests/unit/example_test_term_set.yaml')
foo = Foo(name="species", attr1=TermSetWrapper(value='Homo sapiens', termset=terms),
attr2=0, my_data=[1,2,3])
foobucket = FooBucket('bucket1', [foo])
Expand Down Expand Up @@ -1076,7 +1076,7 @@ def test_io_write_extend_herd(self):
"""
Test the optional write of HERD with extending an existing HERD instance.
"""
terms = TermSet(term_schema_path='tests/unit/example_test_term_set.yaml')
terms = TermSet(name='example', term_schema_path='tests/unit/example_test_term_set.yaml')
foo = Foo(name="species", attr1='attr1', attr2=0,
my_data=TermSetWrapper(value=['Homo sapiens'],
termset=terms))
Expand Down
25 changes: 13 additions & 12 deletions tests/unit/test_term_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,50 +25,51 @@ def setUp(self):
self.skipTest("optional LinkML module is not installed")

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

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

def test_termset_validate(self):
termset = TermSet(term_schema_path='tests/unit/example_test_term_set.yaml')
termset = TermSet(name='test', term_schema_path='tests/unit/example_test_term_set.yaml')
self.assertEqual(termset.validate('Homo sapiens'), True)

def test_termset_validate_false(self):
termset = TermSet(term_schema_path='tests/unit/example_test_term_set.yaml')
termset = TermSet(name='test', term_schema_path='tests/unit/example_test_term_set.yaml')
self.assertEqual(termset.validate('missing_term'), False)

def test_get_item(self):
termset = TermSet(term_schema_path='tests/unit/example_test_term_set.yaml')
termset = TermSet(name='test', 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')

def test_get_item_key_error(self):
termset = TermSet(term_schema_path='tests/unit/example_test_term_set.yaml')
termset = TermSet(name='test', term_schema_path='tests/unit/example_test_term_set.yaml')
with self.assertRaises(ValueError):
termset['Homo Ssapiens']

def test_schema_sheets_and_path_provided_error(self):
folder = os.path.join(CUR_DIR, "test_term_set_input", "schemasheets")
with self.assertRaises(ValueError):
TermSet(term_schema_path='tests/unit/example_test_term_set.yaml', schemasheets_folder=folder)
TermSet(name='test', term_schema_path='tests/unit/example_test_term_set.yaml', schemasheets_folder=folder)

def test_view_set_sheets(self):
folder = os.path.join(CUR_DIR, "test_term_set_input", "schemasheets")
termset = TermSet(schemasheets_folder=folder)
termset = TermSet(name='name', schemasheets_folder=folder)
expected = ['ASTROCYTE', 'INTERNEURON', 'MICROGLIAL_CELL', 'MOTOR_NEURON',
'OLIGODENDROCYTE', 'PYRAMIDAL_NEURON']
self.assertEqual(list(termset.view_set), expected)
self.assertIsInstance(termset.view, SchemaView)

def test_enum_expander(self):
schema_path = 'tests/unit/example_dynamic_term_set.yaml'
termset = TermSet(term_schema_path=schema_path, dynamic=True)
termset = TermSet(name='test', term_schema_path=schema_path, dynamic=True)
# check that interneuron term is in materialized schema
self.assertIn("CL:0000099", termset.view_set)
# check that motor neuron term is in materialized schema
Expand All @@ -88,7 +89,7 @@ def test_enum_expander(self):

def test_enum_expander_output(self):
schema_path = 'tests/unit/example_dynamic_term_set.yaml'
termset = TermSet(term_schema_path=schema_path, dynamic=True)
termset = TermSet(name='test', term_schema_path=schema_path, dynamic=True)
convert_path = termset._TermSet__enum_expander()
convert_path = os.path.normpath(convert_path)

Expand All @@ -102,7 +103,7 @@ def test_enum_expander_output(self):

def test_folder_output(self):
folder = os.path.join(CUR_DIR, "test_term_set_input", "schemasheets")
termset = TermSet(schemasheets_folder=folder)
termset = TermSet(name='test', schemasheets_folder=folder)
actual_path = termset._TermSet__schemasheets_convert()
expected_path = os.path.normpath(os.path.join(os.path.dirname(folder), "schemasheets/nwb_static_enums.yaml"))
self.assertEqual(actual_path, expected_path)
Expand All @@ -114,7 +115,7 @@ def setUp(self):
if not REQUIREMENTS_INSTALLED:
self.skipTest("optional LinkML module is not installed")

self.termset = TermSet(term_schema_path='tests/unit/example_test_term_set.yaml')
self.termset = TermSet(name='test', term_schema_path='tests/unit/example_test_term_set.yaml')

self.wrapped_array = TermSetWrapper(value=np.array(['Homo sapiens']), termset=self.termset)
self.wrapped_list = TermSetWrapper(value=['Homo sapiens'], termset=self.termset)
Expand Down