Skip to content

Commit

Permalink
partial clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
mavaylon1 committed Jan 31, 2024
1 parent ec36ee3 commit fd8995a
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 129 deletions.
2 changes: 0 additions & 2 deletions src/hdmf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
from .utils import docval, getargs
from .term_set import TermSet, TermSetWrapper, TermSetConfigurator

import os


@docval(
{"name": "dataset", "type": None, "doc": "the HDF5 dataset to slice"},
Expand Down
84 changes: 20 additions & 64 deletions src/hdmf/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,11 @@ class AbstractContainer(metaclass=ExtenderMeta):
# Override the _setter factor function, so directives that apply to
# Container do not get used on Data
@classmethod
def _setter(cls, field): # dada
def _setter(cls, field):
"""
Make a setter function for creating a :py:func:`property`
"""
name = field['name']
# if name == 'electrodes':
# breakpoint()
if not field.get('settable', True):
return None

Expand All @@ -96,7 +94,9 @@ def setter(self, val):

def _field_config(self, arg_name, val):
"""
This method will be called in the setter. The termset configuration will be used (if loaded)
to check for a defined TermSet associated with the field. If found, the value of the field
will be wrapped with a TermSetWrapper.
"""
# load termset configuartion file from global Config

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

View workflow job for this annotation

GitHub Actions / Check for spelling errors

configuartion ==> configuration
configurator = self.type_map.ts_config
Expand All @@ -109,20 +109,19 @@ def _field_config(self, arg_name, val):
if self.namespace not in self.type_map.container_types:
msg = "%s not found within loaded configuration." % self.namespace
warn(msg)
return val
else:
# check to see that the container type is in the config under the namespace
config_namespace = termset_config['namespaces'][self.namespace]
object_name = self.__class__.__name__

if object_name not in config_namespace['data_types']:
msg = '%s not found within the configuration for %s' % (object_name, self.namespace)
warn(msg)
return val
else:
for attr in config_namespace['data_types'][object_name]:
# if object_name == 'NWBFile':
# breakpoint()
# if the attr has been manually wrapped then skip checking the config for the attr
if type(attr) == TermSetWrapper:
continue


obj_mapper = self.type_map.get_map(self)
# get the spec according to attr name in schema
Expand All @@ -133,18 +132,25 @@ def _field_config(self, arg_name, val):
# In the case of dealing with datasets directly or not defined in the spec.
# (Data/VectorData/DynamicTable/etc)
if spec is None:
msg = "Spec not found for %s" % attr
msg = "Spec not found for %s." % attr
warn(msg)
return val
# From the spec, get the corresponding constructor name
else:
constr_name = obj_mapper.get_const_arg(spec)
if constr_name == arg_name: # make sure any custom fields are not handled (i.e., make an extension)
if constr_name == arg_name:
termset_path = os.path.join(CUR_DIR, config_namespace['data_types'][object_name][attr])
termset = TermSet(term_schema_path=termset_path)

val = TermSetWrapper(value=val, termset=termset)
# If the val has been manually wrapped then skip checking the config for the attr
if type(val) == TermSetWrapper:
return val
else:
val = TermSetWrapper(value=val, termset=termset)
return val
else:
msg = "The name of the field does not match the field name in the schema. Field value not wrapped."
warn(msg)
return val
return val
else:
return val

Expand Down Expand Up @@ -292,54 +298,6 @@ def __init__(self, **kwargs):
self.__read_io = None
self.__obj = None

@docval({'name': 'constructor_args', 'type': dict,
'doc': 'The fields/parameters/attibutes for the object.'})
def init_validation(self, constructor_args):
"""
"""
# load termset configuartion file from global Config
configurator = self.type_map.ts_config
CUR_DIR = os.path.dirname(os.path.realpath(configurator.path[0]))
termset_config = configurator.config

if termset_config is not None:
# check to see that the namespace for the container is in the config
if self.namespace not in self.type_map.container_types:
msg = "%s not found within loaded configuration." % self.namespace
warn(msg)
else:
# check to see that the container type is in the config under the namespace
config_namespace = termset_config['namespaces'][self.namespace]
object_name = self.__class__.__name__

if object_name not in config_namespace['data_types']:
msg = '%s not found within the configuration for %s' % (object_name, self.namespace)
else:
for attr in config_namespace['data_types'][object_name]:
# if the attr has been manually wrapped then skip checking the config for the attr
if type(attr) == TermSetWrapper:
continue

obj_mapper = self.type_map.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:
msg = "Spec not found for %s" % attr
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 = os.path.join(CUR_DIR, config_namespace['data_types'][object_name][attr])
termset = TermSet(term_schema_path=termset_path)
constructor_args[attr] = TermSetWrapper(value=constructor_args[attr], termset=termset)

@property
def read_io(self):
"""
Expand Down Expand Up @@ -608,7 +566,6 @@ class Container(AbstractContainer):
def _setter(cls, field):
"""Returns a list of setter functions for the given field to be added to the class during class declaration."""
super_setter = AbstractContainer._setter(field)
# breakpoint()
ret = [super_setter]
# create setter with check for required name
# the AbstractContainer that is passed to the setter must have name = required_name
Expand All @@ -635,7 +592,6 @@ def container_setter(self, val):
idx2 = len(ret) - 1

def container_setter(self, val):
# breakpoint()
ret[idx2](self, val) # call the previous setter
if val is not None:
if isinstance(val, (tuple, list)):
Expand Down
126 changes: 63 additions & 63 deletions tests/unit/test_termset_config.py
Original file line number Diff line number Diff line change
@@ -1,63 +1,63 @@
from hdmf.testing import TestCase
from hdmf import 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_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
# from hdmf.testing import TestCase
# from hdmf import 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_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 fd8995a

Please sign in to comment.