| 1 |
import os |
| 2 |
import numpy as np |
| 3 |
|
| 4 |
from hdmf import Container |
| 5 |
from hdmf.term_set import TermSet, TermSetWrapper, TypeConfigurator |
| 6 |
from hdmf.testing import TestCase, remove_test_file |
| 7 |
from hdmf.common import (VectorData, unload_type_config, |
| 8 |
get_loaded_type_config, load_type_config) |
| 9 |
from hdmf.utils import popargs |
| 10 |
|
| 11 |
|
| 12 |
CUR_DIR = os.path.dirname(os.path.realpath(__file__)) |
| 13 |
|
| 14 |
try: |
| 15 |
from linkml_runtime.utils.schemaview import SchemaView # noqa: F401 |
| 16 |
import schemasheets # noqa: F401 |
| 17 |
import oaklib # noqa: F401 |
| 18 |
import yaml # noqa: F401 |
| 19 |
|
| 20 |
REQUIREMENTS_INSTALLED = True |
| 21 |
except ImportError: |
| 22 |
REQUIREMENTS_INSTALLED = False |
| 23 |
|
| 24 |
class TestTermSet(TestCase): |
| 25 |
"""Tests for TermSet""" |
| 26 |
def setUp(self): |
| 27 |
if not REQUIREMENTS_INSTALLED: |
| 28 |
self.skipTest("optional LinkML module is not installed") |
| 29 |
|
| 30 |
def test_termset_setup(self): |
| 31 |
termset = TermSet(term_schema_path='tests/unit/example_test_term_set.yaml') |
| 32 |
self.assertEqual(termset.name, 'Species') |
| 33 |
self.assertEqual(list(termset.sources), ['NCBI_TAXON']) |
| 34 |
|
| 35 |
def test_repr_short(self): |
| 36 |
termset = TermSet(term_schema_path='tests/unit/example_test_term_set2.yaml') |
| 37 |
output = ('Schema Path: tests/unit/example_test_term_set2.yaml\nSources: NCBI_TAXON\nTerms: \n' |
| 38 |
' - Homo sapiens\n - Mus musculus\n - Ursus arctos horribilis\nNumber of terms: 3') |
| 39 |
self.assertEqual(repr(termset), output) |
| 40 |
|
| 41 |
def test_repr_html_short(self): |
| 42 |
termset = TermSet(term_schema_path='tests/unit/example_test_term_set2.yaml') |
| 43 |
output = ('<b>Schema Path: </b>tests/unit/example_test_term_set2.yaml<br><b>Sources:' |
| 44 |
' </b>NCBI_TAXON<br><b> Terms: </b><li> Homo sapiens </li><li> Mus musculus' |
| 45 |
' </li><li> Ursus arctos horribilis </li><i> Number of terms:</i> 3') |
| 46 |
self.assertEqual(termset._repr_html_(), output) |
| 47 |
|
| 48 |
def test_repr_long(self): |
| 49 |
termset = TermSet(term_schema_path='tests/unit/example_test_term_set.yaml') |
| 50 |
output = ('Schema Path: tests/unit/example_test_term_set.yaml\nSources: NCBI_TAXON\nTerms: \n' |
| 51 |
' - Homo sapiens\n - Mus musculus\n - Ursus arctos horribilis\n ... ... \n' |
| 52 |
' - Ailuropoda melanoleuca\nNumber of terms: 5') |
| 53 |
self.assertEqual(repr(termset), output) |
| 54 |
|
| 55 |
def test_repr_html_long(self): |
| 56 |
termset = TermSet(term_schema_path='tests/unit/example_test_term_set.yaml') |
| 57 |
output = ('<b>Schema Path: </b>tests/unit/example_test_term_set.yaml<br><b>Sources:' |
| 58 |
' </b>NCBI_TAXON<br><b> Terms: </b><li> Homo sapiens </li><li> Mus musculus' |
| 59 |
' </li><li> Ursus arctos horribilis </li>... ...<li> Ailuropoda melanoleuca' |
| 60 |
' </li><i> Number of terms:</i> 5') |
| 61 |
self.assertEqual(termset._repr_html_(), output) |
| 62 |
|
| 63 |
def test_view_set(self): |
| 64 |
termset = TermSet(term_schema_path='tests/unit/example_test_term_set.yaml') |
| 65 |
expected = ['Homo sapiens', 'Mus musculus', 'Ursus arctos horribilis', 'Myrmecophaga tridactyla', |
| 66 |
'Ailuropoda melanoleuca'] |
| 67 |
self.assertEqual(list(termset.view_set), expected) |
| 68 |
self.assertIsInstance(termset.view, SchemaView) |
| 69 |
|
| 70 |
def test_termset_validate(self): |
| 71 |
termset = TermSet(term_schema_path='tests/unit/example_test_term_set.yaml') |
| 72 |
self.assertEqual(termset.validate('Homo sapiens'), True) |
| 73 |
|
| 74 |
def test_termset_validate_false(self): |
| 75 |
termset = TermSet(term_schema_path='tests/unit/example_test_term_set.yaml') |
| 76 |
self.assertEqual(termset.validate('missing_term'), False) |
| 77 |
|
| 78 |
def test_get_item(self): |
| 79 |
termset = TermSet(term_schema_path='tests/unit/example_test_term_set.yaml') |
| 80 |
self.assertEqual(termset['Homo sapiens'].id, 'NCBI_TAXON:9606') |
| 81 |
self.assertEqual(termset['Homo sapiens'].description, 'the species is human') |
| 82 |
self.assertEqual( |
| 83 |
termset['Homo sapiens'].meaning, |
| 84 |
'https://www.ncbi.nlm.nih.gov/Taxonomy/Browser/wwwtax.cgi?mode=Info&id=9606' |
| 85 |
) |
| 86 |
|
| 87 |
def test_get_item_key_error(self): |
| 88 |
termset = TermSet(term_schema_path='tests/unit/example_test_term_set.yaml') |
| 89 |
with self.assertRaises(ValueError): |
| 90 |
termset['Homo Ssapiens'] |
| 91 |
|
| 92 |
def test_schema_sheets_and_path_provided_error(self): |
| 93 |
folder = os.path.join(CUR_DIR, "test_term_set_input", "schemasheets") |
| 94 |
with self.assertRaises(ValueError): |
| 95 |
TermSet(term_schema_path='tests/unit/example_test_term_set.yaml', schemasheets_folder=folder) |
| 96 |
|
| 97 |
def test_view_set_sheets(self): |
| 98 |
folder = os.path.join(CUR_DIR, "test_term_set_input", "schemasheets") |
| 99 |
termset = TermSet(schemasheets_folder=folder) |
| 100 |
expected = ['ASTROCYTE', 'INTERNEURON', 'MICROGLIAL_CELL', 'MOTOR_NEURON', |
| 101 |
'OLIGODENDROCYTE', 'PYRAMIDAL_NEURON'] |
| 102 |
self.assertEqual(list(termset.view_set), expected) |
| 103 |
self.assertIsInstance(termset.view, SchemaView) |
| 104 |
|
| 105 |
def test_enum_expander(self): |
| 106 |
schema_path = 'tests/unit/example_dynamic_term_set.yaml' |
| 107 |
termset = TermSet(term_schema_path=schema_path, dynamic=True) |
| 108 |
# check that interneuron term is in materialized schema |
| 109 |
self.assertIn("CL:0000099", termset.view_set) |
| 110 |
# check that motor neuron term is in materialized schema |
| 111 |
self.assertIn("CL:0000100", termset.view_set) |
| 112 |
# check that pyramidal neuron is in materialized schema |
| 113 |
self.assertIn("CL:0000598", termset.view_set) |
| 114 |
|
| 115 |
self.assertIsInstance(termset.view, SchemaView) |
| 116 |
expected_path = os.path.join("tests", "unit", "expanded_example_dynamic_term_set.yaml") |
| 117 |
expected_path = os.path.normpath(expected_path) |
| 118 |
actual_path = os.path.normpath(termset.expanded_termset_path) |
| 119 |
|
| 120 |
self.assertEqual(actual_path, expected_path) |
| 121 |
|
| 122 |
filename = os.path.splitext(os.path.basename(schema_path))[0] |
| 123 |
remove_test_file(f"tests/unit/expanded_{filename}.yaml") |
| 124 |
|
| 125 |
def test_enum_expander_output(self): |
| 126 |
schema_path = 'tests/unit/example_dynamic_term_set.yaml' |
| 127 |
termset = TermSet(term_schema_path=schema_path, dynamic=True) |
| 128 |
convert_path = termset._TermSet__enum_expander() |
| 129 |
convert_path = os.path.normpath(convert_path) |
| 130 |
|
| 131 |
expected_path = os.path.join("tests", "unit", "expanded_example_dynamic_term_set.yaml") |
| 132 |
expected_path = os.path.normpath(expected_path) |
| 133 |
|
| 134 |
self.assertEqual(convert_path, expected_path) |
| 135 |
|
| 136 |
filename = os.path.splitext(os.path.basename(schema_path))[0] |
| 137 |
remove_test_file(f"tests/unit/expanded_{filename}.yaml") |
| 138 |
|
| 139 |
def test_folder_output(self): |
| 140 |
folder = os.path.join(CUR_DIR, "test_term_set_input", "schemasheets") |
| 141 |
termset = TermSet(schemasheets_folder=folder) |
| 142 |
actual_path = termset._TermSet__schemasheets_convert() |
| 143 |
expected_path = os.path.normpath(os.path.join(os.path.dirname(folder), "schemasheets/nwb_static_enums.yaml")) |
| 144 |
self.assertEqual(actual_path, expected_path) |
| 145 |
|
| 146 |
|
| 147 |
class TestTermSetWrapper(TestCase): |
| 148 |
"""Tests for the TermSetWrapper""" |
| 149 |
def setUp(self): |
| 150 |
if not REQUIREMENTS_INSTALLED: |
| 151 |
self.skipTest("optional LinkML module is not installed") |
| 152 |
|
| 153 |
self.termset = TermSet(term_schema_path='tests/unit/example_test_term_set.yaml') |
| 154 |
|
| 155 |
self.wrapped_array = TermSetWrapper(value=np.array(['Homo sapiens']), termset=self.termset) |
| 156 |
self.wrapped_list = TermSetWrapper(value=['Homo sapiens'], termset=self.termset) |
| 157 |
|
| 158 |
c_data = np.array([('Homo sapiens', 24)], dtype=[('species', 'U50'), ('age', 'i4')]) |
| 159 |
self.wrapped_comp_array = TermSetWrapper(value=c_data, |
| 160 |
termset=self.termset, |
| 161 |
field='species') |
| 162 |
|
| 163 |
self.np_data = VectorData( |
| 164 |
name='Species_1', |
| 165 |
description='...', |
| 166 |
data=self.wrapped_array |
| 167 |
) |
| 168 |
|
| 169 |
def test_properties(self): |
| 170 |
self.assertEqual(self.wrapped_array.value, ['Homo sapiens']) |
| 171 |
self.assertEqual(self.wrapped_array.termset.view_set, self.termset.view_set) |
| 172 |
self.assertEqual(self.wrapped_array.dtype, 'U12') # this covers __getattr__ |
| 173 |
self.assertEqual(self.wrapped_comp_array.field, 'species') |
| 174 |
|
| 175 |
def test_get_item(self): |
| 176 |
self.assertEqual(self.np_data.data[0], 'Homo sapiens') |
| 177 |
|
| 178 |
def test_validate_error(self): |
| 179 |
with self.assertRaises(ValueError): |
| 180 |
VectorData(name='Species_1', |
| 181 |
description='...', |
| 182 |
data=TermSetWrapper(value=['Missing Term'], |
| 183 |
termset=self.termset)) |
| 184 |
|
| 185 |
def test_wrapper_validate_attribute(self): |
| 186 |
col1 = VectorData( |
| 187 |
name='Species_1', |
| 188 |
description=TermSetWrapper(value='Homo sapiens', |
| 189 |
termset=self.termset), |
| 190 |
data=['Human'] |
| 191 |
) |
| 192 |
self.assertTrue(isinstance(col1.description, TermSetWrapper)) |
| 193 |
|
| 194 |
def test_wrapper_validate_dataset(self): |
| 195 |
col1 = VectorData( |
| 196 |
name='Species_1', |
| 197 |
description='...', |
| 198 |
data=TermSetWrapper(value=['Homo sapiens'], |
| 199 |
termset=self.termset) |
| 200 |
) |
| 201 |
self.assertTrue(isinstance(col1.data, TermSetWrapper)) |
| 202 |
|
| 203 |
def test_wrapper_append(self): |
| 204 |
data_obj = VectorData(name='species', description='...', data=self.wrapped_list) |
| 205 |
data_obj.append('Mus musculus') |
| 206 |
self.assertEqual(data_obj.data.value, ['Homo sapiens', 'Mus musculus']) |
| 207 |
|
| 208 |
def test_wrapper_append_error(self): |
| 209 |
data_obj = VectorData(name='species', description='...', data=self.wrapped_list) |
| 210 |
with self.assertRaises(ValueError): |
| 211 |
data_obj.append('bad_data') |
| 212 |
|
| 213 |
def test_wrapper_extend(self): |
| 214 |
data_obj = VectorData(name='species', description='...', data=self.wrapped_list) |
| 215 |
data_obj.extend(['Mus musculus']) |
| 216 |
self.assertEqual(data_obj.data.value, ['Homo sapiens', 'Mus musculus']) |
| 217 |
|
| 218 |
def test_wrapper_extend_error(self): |
| 219 |
data_obj = VectorData(name='species', description='...', data=self.wrapped_list) |
| 220 |
with self.assertRaises(ValueError): |
| 221 |
data_obj.extend(['bad_data']) |
| 222 |
|
| 223 |
class TestTypeConfig(TestCase): |
| 224 |
def setUp(self): |
| 225 |
if not REQUIREMENTS_INSTALLED: |
| 226 |
self.skipTest("optional LinkML module is not installed") |
| 227 |
|
| 228 |
def tearDown(self): |
| 229 |
unload_type_config() |
| 230 |
|
| 231 |
def test_get_loaded_type_config_error(self): |
| 232 |
with self.assertRaises(ValueError): |
| 233 |
get_loaded_type_config() |
| 234 |
|
| 235 |
def test_config_path(self): |
| 236 |
path = 'tests/unit/hdmf_config.yaml' |
| 237 |
tc = TypeConfigurator(path=path) |
| 238 |
self.assertEqual(tc.path, [path]) |
| 239 |
|
| 240 |
def test_get_config(self): |
| 241 |
path = 'tests/unit/hdmf_config.yaml' |
| 242 |
tc = TypeConfigurator(path=path) |
| 243 |
self.assertEqual(tc.get_config('VectorData', 'hdmf-common'), |
| 244 |
{'description': {'termset': 'example_test_term_set.yaml'}}) |
| 245 |
|
| 246 |
def test_get_config_namespace_error(self): |
| 247 |
path = 'tests/unit/hdmf_config.yaml' |
| 248 |
tc = TypeConfigurator(path=path) |
| 249 |
with self.assertRaises(ValueError): |
| 250 |
tc.get_config('VectorData', 'hdmf-common11') |
| 251 |
|
| 252 |
def test_get_config_container_error(self): |
| 253 |
path = 'tests/unit/hdmf_config.yaml' |
| 254 |
tc = TypeConfigurator(path=path) |
| 255 |
with self.assertRaises(ValueError): |
| 256 |
tc.get_config('VectorData11', 'hdmf-common') |
| 257 |
|
| 258 |
def test_already_loaded_path_error(self): |
| 259 |
path = 'tests/unit/hdmf_config.yaml' |
| 260 |
tc = TypeConfigurator(path=path) |
| 261 |
with self.assertRaises(ValueError): |
| 262 |
tc.load_type_config(config_path=path) |
| 263 |
|
| 264 |
def test_load_two_unique_configs(self): |
| 265 |
path = 'tests/unit/hdmf_config.yaml' |
| 266 |
path2 = 'tests/unit/hdmf_config2.yaml' |
| 267 |
tc = TypeConfigurator(path=path) |
| 268 |
tc.load_type_config(config_path=path2) |
| 269 |
config = {'namespaces': {'hdmf-common': {'version': '3.12.2', |
| 270 |
'data_types': {'VectorData': {'name': None}, |
| 271 |
'VectorIndex': {'data': '...'}, |
| 272 |
'Data': {'description': {'termset': 'example_test_term_set.yaml'}}, |
| 273 |
'EnumData': {'description': {'termset': 'example_test_term_set.yaml'}}}}, |
| 274 |
'foo_namespace': {'version': '...', |
| 275 |
'data_types': {'ExtensionContainer': {'description': None}}}, |
| 276 |
'namespace2': {'version': 0, 'data_types': |
| 277 |
{'MythicData': {'description': |
| 278 |
{'termset': 'example_test_term_set.yaml'}}}}}} |
| 279 |
self.assertEqual(tc.path, [path, path2]) |
| 280 |
self.assertEqual(tc.config, config) |
| 281 |
|
| 282 |
|
| 283 |
class ExtensionContainer(Container): |
| 284 |
__fields__ = ("description",) |
| 285 |
|
| 286 |
def __init__(self, **kwargs): |
| 287 |
description, namespace = popargs('description', 'namespace', kwargs) |
| 288 |
self.namespace = namespace |
| 289 |
super().__init__(**kwargs) |
| 290 |
self.description = description |
| 291 |
|
| 292 |
@property |
| 293 |
def data_type(self): |
| 294 |
""" |
| 295 |
Return the spec data type associated with this container. |
| 296 |
""" |
| 297 |
return "ExtensionContainer" |
| 298 |
|
| 299 |
|
| 300 |
class TestGlobalTypeConfig(TestCase): |
| 301 |
def setUp(self): |
| 302 |
if not REQUIREMENTS_INSTALLED: |
| 303 |
self.skipTest("optional LinkML module is not installed") |
| 304 |
load_type_config(config_path='tests/unit/hdmf_config.yaml') |
| 305 |
|
| 306 |
def tearDown(self): |
| 307 |
unload_type_config() |
| 308 |
|
| 309 |
def test_load_config(self): |
| 310 |
config = get_loaded_type_config() |
| 311 |
self.assertEqual(config, |
| 312 |
{'namespaces': {'hdmf-common': {'version': '3.12.2', |
| 313 |
'data_types': {'VectorData': |
| 314 |
{'description': {'termset': 'example_test_term_set.yaml'}}, |
| 315 |
'VectorIndex': {'data': '...'}}}, 'foo_namespace': |
| 316 |
{'version': '...', 'data_types': |
| 317 |
{'ExtensionContainer': {'description': None}}}}} |
| 318 |
) |
| 319 |
|
| 320 |
def test_validate_with_config(self): |
| 321 |
data = VectorData(name='foo', data=[0], description='Homo sapiens') |
| 322 |
self.assertEqual(data.description.value, 'Homo sapiens') |
| 323 |
|
| 324 |
def test_namespace_warn(self): |
| 325 |
with self.assertWarns(Warning): |
| 326 |
ExtensionContainer(name='foo', |
| 327 |
namespace='foo', |
| 328 |
description='Homo sapiens') |
| 329 |
|
| 330 |
def test_container_type_warn(self): |
| 331 |
with self.assertWarns(Warning): |
| 332 |
ExtensionContainer(name='foo', |
| 333 |
namespace='hdmf-common', |
| 334 |
description='Homo sapiens') |
| 335 |
|
| 336 |
def test_already_wrapped_warn(self): |
| 337 |
terms = TermSet(term_schema_path='tests/unit/example_test_term_set.yaml') |
| 338 |
with self.assertWarns(Warning): |
| 339 |
VectorData(name='foo', |
| 340 |
data=[0], |
| 341 |
description=TermSetWrapper(value='Homo sapiens', termset=terms)) |
| 342 |
|
| 343 |
def test_field_not_in_config(self): |
| 344 |
unload_type_config() |
| 345 |
load_type_config(config_path='tests/unit/hdmf_config2.yaml') |
| 346 |
|
| 347 |
VectorData(name='foo', data=[0], description='Homo sapiens') |
| 348 |
|
| 349 |
def test_spec_none(self): |
| 350 |
with self.assertWarns(Warning): |
| 351 |
ExtensionContainer(name='foo', |
| 352 |
namespace='foo_namespace', |
| 353 |
description='Homo sapiens') |