Skip to content

Commit

Permalink
testing
Browse files Browse the repository at this point in the history
  • Loading branch information
mavaylon1 committed Jan 18, 2024
1 parent fa9e0f0 commit 66d0752
Show file tree
Hide file tree
Showing 8 changed files with 162 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/hdmf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@


# a global TermSetConfigurator
TS_CONFIG = TermSetConfigurator()
TS_CONFIG = TermSetConfigurator(path='src/hdmf/hdmf_config.yaml')

def get_termset_config():
return TS_CONFIG.config
Expand Down
38 changes: 27 additions & 11 deletions src/hdmf/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,21 +234,22 @@ def __init__(self, **kwargs):
self.__read_io = None
self.__obj = None

@docval({'name': 'fields', 'type': dict, 'doc': 'The fields/parameters/attibutes for the object.'})
def init_validation(self, fields):
@docval({'name': 'constructor_args', 'type': dict, 'doc': 'The fields/parameters/attibutes for the object.'})

Check failure on line 237 in src/hdmf/container.py

View workflow job for this annotation

GitHub Actions / Check for spelling errors

attibutes ==> attributes
def init_validation(self, constructor_args):
# load termset configuartion file from global Config

Check failure on line 239 in src/hdmf/container.py

View workflow job for this annotation

GitHub Actions / Check for spelling errors

configuartion ==> configuration
from . import TS_CONFIG #update path

termset_config = TS_CONFIG.config
breakpoint()
if termset_config is None:
msg = 'TermSet Configuration is not loaded.'
raise ValueError(msg)
object_name = self.__class__.__name__

# Check that the object data_type is in the loaded namespace
from .common import get_type_map
container_types_dict = get_type_map().container_types
tm = get_type_map()

container_types_dict = tm.container_types
object_exists = False
for namespace in container_types_dict:
if object_name in container_types_dict[namespace]:
Expand All @@ -258,22 +259,37 @@ def init_validation(self, fields):

if not object_exists:
msg = "%s is not in the loaded Namespace(s)." % object_name
raise ValueError(msg)
raise warn(msg)

# Wrap supported fields with TermSetWrapper
obj_wrapped = False
if object_name in termset_config:
obj_wrapped = True
for attr in termset_config[object_name]['fields']:
if attr in fields: # make sure any custom fields are not handled (i.e., make an extension)
obj_mapper = tm.get_map(self)
# get the spec according to attr name in schema
# Note: this is the name for the field in the config
spec = obj_mapper.get_attr_spec(attr)

# In the case of dealing with datasets directly or not defined in the spec.
# (Data/VectorData/DynamicTable/etc)
if spec is None:
# constr_name= attr
msg = "Spec not found for %s" % attr
raise warn(msg)
# From the spec, get the corresponding constructor name
else:
constr_name = obj_mapper.get_const_arg(spec)

if constr_name in constructor_args: # make sure any custom fields are not handled (i.e., make an extension)
termset_path = termset_config[object_name]['fields'][attr]
termset = TermSet(term_schema_path=termset_path)
fields[attr] = TermSetWrapper(value=fields[attr], termset=termset)
constructor_args[attr] = TermSetWrapper(value=constructor_args[attr], termset=termset)

# Even if the data_type is in the namespace, it might not be in the configuration.
if not obj_wrapped:
if object_exists and not obj_wrapped:
msg = "%s is not in the loaded TermSet Configuration." % object_name
raise ValueError(msg)
raise warn(msg)

@property
def read_io(self):
Expand Down Expand Up @@ -827,8 +843,8 @@ class Data(AbstractContainer):
@docval({'name': 'name', 'type': str, 'doc': 'the name of this container'},
{'name': 'data', 'type': ('scalar_data', 'array_data', 'data'), 'doc': 'the source of the data'})
def __init__(self, **kwargs):
self.init_validation(fields=kwargs)
breakpoint()
self.init_validation(constructor_args=kwargs)
# breakpoint()
data = popargs('data', kwargs)
super().__init__(**kwargs)

Expand Down
12 changes: 12 additions & 0 deletions src/hdmf/hdmf_config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
VectorData:
namespace:
namespace_version:
fields:
data: /Users/mavaylon/Research/NWB/hdmf2/hdmf/tests/unit/example_test_term_set.yaml
field2: ...
DataType2:
namespace:
namespace_version:
fields:
data: /Users/mavaylon/Research/NWB/hdmf2/hdmf/tests/unit/example_test_term_set.yaml
field2: ...
9 changes: 5 additions & 4 deletions src/hdmf/term_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,8 +310,10 @@ class TermSetConfigurator:
"""
"""
def __init__(self):
self.path = ['/Users/mavaylon/Research/NWB/hdmf2/hdmf/docs/gallery/example_config.yaml']
@docval({'name': 'path', 'type': str, 'doc': 'Path to the configuartion file.',

Check failure on line 313 in src/hdmf/term_set.py

View workflow job for this annotation

GitHub Actions / Check for spelling errors

configuartion ==> configuration
'default': None})
def __init__(self, **kwargs):
self.path = [kwargs['path']]
self.config = None
self.load_termset_config()

Expand Down Expand Up @@ -339,10 +341,9 @@ def load_termset_config(self,config_path):
# append path to new config to self.path
self.path.append(config_path)


def unload_termset_config():
"""
Remove validation according to termset configuration file.
"""
self.path = ['/Users/mavaylon/Research/NWB/hdmf2/hdmf/docs/gallery/example_config.yaml']
self.path = ['src/hdmf/hdmf_config.yaml']
self.config = None
25 changes: 25 additions & 0 deletions tests/unit/common/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,31 @@
class TestVDConfig(TestCase):
def test_init_config(self):
vd = VectorData(name='data', description='',data=['Homo sapiens'])
tb = DynamicTable(name="with_table_columns", description='a test table', columns=[vd])
from hdmf.common import get_type_map
htm = get_type_map()
om = htm.get_map(tb)
from datetime import datetime
from uuid import uuid4

# import numpy as np
# from dateutil.tz import tzlocal
#
# from pynwb import NWBHDF5IO, NWBFile
# from pynwb.behavior import SpatialSeries
# from pynwb import get_type_map
# tm = get_type_map()
# timestamps = np.linspace(0, 50) / 200
# position_data = np.array([np.linspace(0, 10, 50), np.linspace(0, 8, 50)]).T
#
# position_spatial_series = SpatialSeries(
# name="SpatialSeries",
# description="Position (x, y) in an open field.",
# data=position_data,
# timestamps=timestamps,
# reference_frame="(0,0) is bottom left corner",
# )
breakpoint()


class TestDynamicTable(TestCase):
Expand Down
12 changes: 12 additions & 0 deletions tests/unit/test_config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
VectorData:
namespace:
namespace_version:
fields:
data: /Users/mavaylon/Research/NWB/hdmf2/hdmf/tests/unit/example_test_term_set.yaml
field2: ...
DataType2:
namespace:
namespace_version:
fields:
data: /Users/mavaylon/Research/NWB/hdmf2/hdmf/tests/unit/example_test_term_set.yaml
field2: ...
12 changes: 12 additions & 0 deletions tests/unit/test_extension_config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
VectorData:
namespace:
namespace_version:
fields:
data: /Users/mavaylon/Research/NWB/hdmf2/hdmf/tests/unit/example_test_term_set.yaml
field2: ...
DataType2:
namespace:
namespace_version:
fields:
data: /Users/mavaylon/Research/NWB/hdmf2/hdmf/tests/unit/example_test_term_set.yaml
field2: ...
68 changes: 68 additions & 0 deletions tests/unit/test_termset_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
from hdmf.testing import TestCase
from hdmf import get_termset_config, load_termset_config, unload_termset_config
from hdmf.term_set import TermSetConfigurator

class TestConfig(TestCase):
self.test_config = {}
self.test_merged_extension_config = {}
self.hdmf_config = {}

def test_construct_config(self):
# add asserts for self.path and self.config
test_config = TermSetConfigurator(path='tests/unit/test_config.yaml')
self.assertEqual(test_config.path, ['tests/unit/test_config.yaml'])
# self.assertEqual(test_config.config, None)

def test_load_termset_config(self):
test_config = TermSetConfigurator(path='tests/unit/test_config.yaml')
test_config.load_termset_config(path='tests/unit/test_extension_config.yaml')
self.assertEqual(config.path, ['tests/unit/test_config.yaml', 'tests/unit/test_extension_config.yaml'])
# self.assertEqual(config.config, None)

def test_unload_termset_config(self):
test_config = TermSetConfigurator(path='tests/unit/test_config.yaml')
test_config.unload_termset_config()
self.assertEqual(config.path, ['src/hdmf/hdmf_config.yaml'])
self.assertEqual(config.config, None)

def test_get_termset_config(self):
config = get_termset_config()
self.assertEqual(config.path, ['src/hdmf/hdmf_config.yaml'])
# self.assertEqual(config.config, None)

def test_unload_global_config(self):
config = get_termset_config()
unload_termset_config()
self.assertEqual(config.path, ['src/hdmf/hdmf_config.yaml'])
self.assertEqual(config.config, None)

def test_load_global_config_reset(self):
load_termset_config()
self.assertEqual(config.path, ['src/hdmf/hdmf_config.yaml'])
# self.assertEqual(config.config, None)

def test_load_global_config_extension_config(self):
load_termset_config()
self.assertEqual(config.path, ['tests/unit/test_config.yaml', 'tests/unit/test_extension_config.yaml'])
# self.assertEqual(config.config, None)

def test_data(self):
pass

def test_dataset_not_in_spec(self):
pass

def test_attribute_not_in_spec(self):
pass

def test_attriute_in_spec(self):
pass

def test_dataset_in_spec(self):
pass

def test_data_type_not_in_namespace(self):
pass

def test_warn_not_wrapped(self):
pass

0 comments on commit 66d0752

Please sign in to comment.