Skip to content

Commit

Permalink
new way of thinking draft
Browse files Browse the repository at this point in the history
  • Loading branch information
mavaylon1 committed Jan 18, 2024
1 parent 988a442 commit 0b83e58
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 44 deletions.
28 changes: 12 additions & 16 deletions docs/gallery/example_config.yaml
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
- data_type: VectorData
namespace:
namespace_version:
fields:
data: /Users/mavaylon/Research/NWB/hdmf2/hdmf/tests/unit/example_test_term_set.yaml
field2: ...
# - data_type: #Container2
# namespace:
# namespace_version:
# fields:
# - name:
# doc:
# termset_path:
# - name:
# doc:
# termset_path:
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: ...
24 changes: 20 additions & 4 deletions src/hdmf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,30 @@
# a global TermSetConfigurator
TS_CONFIG = TermSetConfigurator()

def get_termset_config():
return TS_CONFIG.config

def get_config_types():
return TS_CONFIG.get_data_types()

@docval({'name': 'config_path', 'type': str, 'doc': 'Path to the configuartion file.',

Check failure on line 18 in src/hdmf/__init__.py

View workflow job for this annotation

GitHub Actions / Check for spelling errors

configuartion ==> configuration
'default': '/Users/mavaylon/Research/NWB/hdmf2/hdmf/docs/gallery/example_config.yaml'}) #update path
'default': None})
def load_termset_config(config_path: str):
"""
Load the configuration file for validation on the fields defined for the objects within the file.
By default, the curated configuration file is used, but can take in a custom file.
If a user does not provide a config_path, then this method will unload any present configuration
and load the default curated configuration.
If a user provides a config_path, then this method will:
- Search the current configuation for data_types that are already present. These data_types will be

Check failure on line 26 in src/hdmf/__init__.py

View workflow job for this annotation

GitHub Actions / Check for spelling errors

configuation ==> configuration
replaced with the new configuration.
- If the data_type is not present, then they will be loaded alongside the default curated configuration.
"""
return TS_CONFIG.load_termset_config(config_path)
if config_path is None:
TS_CONFIG.path = "/Users/mavaylon/Research/NWB/hdmf2/hdmf/docs/gallery/example_config.yaml"
TS_CONFIG.load_termset_config()





def unload_termset_config():
Expand Down
1 change: 0 additions & 1 deletion src/hdmf/common/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,4 +233,3 @@ def get_hdf5io(**kwargs):
HERD = get_class('HERD', EXP_NAMESPACE)
SimpleMultiContainer = get_class('SimpleMultiContainer', CORE_NAMESPACE)
AlignedDynamicTable = get_class('AlignedDynamicTable', CORE_NAMESPACE)
breakpoint()
44 changes: 34 additions & 10 deletions src/hdmf/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,18 +238,42 @@ def __init__(self, **kwargs):
def init_validation(self, fields):
# 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
# Before calling super().__init__() and before setting fields, check for config file for
# validation via TermSetWrapper.
termset_config = TS_CONFIG.load_termset_config()

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__

for obj_config in termset_config:
if obj_config['data_type'] == object_name:
for attr in obj_config['fields']:
if attr in fields: # make sure any custom fields are not handled (i.e., make an extension)
termset_path = obj_config['fields'][attr]
termset = TermSet(term_schema_path=termset_path)
fields[attr] = TermSetWrapper(value=fields[attr], termset=termset)
# 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
object_exists = False
for namespace in container_types_dict:
if object_name in container_types_dict[namespace]:
object_exists = True
else:
continue

if not object_exists:
msg = "%s is not in the loaded Namespace(s)." % object_name
raise ValueError(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)
termset_path = termset_config[object_name]['fields'][attr]
termset = TermSet(term_schema_path=termset_path)
fields[attr] = TermSetWrapper(value=fields[attr], termset=termset)

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

@property
def read_io(self):
Expand Down
43 changes: 31 additions & 12 deletions src/hdmf/term_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import warnings
import numpy as np
from .data_utils import append_data, extend_data
import yaml


class TermSet:
Expand Down Expand Up @@ -310,28 +311,46 @@ class TermSetConfigurator:
"""
def __init__(self):
self.path = '/Users/mavaylon/Research/NWB/hdmf2/hdmf/docs/gallery/example_config.yaml'
self.path = ['/Users/mavaylon/Research/NWB/hdmf2/hdmf/docs/gallery/example_config.yaml']
self.config = None
self.load_termset_config()

# @property
# def config_path(self):
# return self.__config_path
def get_data_types(self):
"""
Return list of data_types within current configuration file.
"""
data_types = []
for data_type_dict in self.config:
data_types.append(data_type_dict['data_type'])

@docval({'name': 'config_path', 'type': str, 'doc': 'Path to the configuartion file.',

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

View workflow job for this annotation

GitHub Actions / Check for spelling errors

configuartion ==> configuration
'default': None})
def load_termset_config(config_path: str):
def load_termset_config(self,config_path):
"""
Load the configuration file for validation on the fields defined for the objects within the file.
By default, the curated configuration file is used, but can take in a custom file.
"""
if config_path not None:
self.path = config_path
# Set self.config for __init__
if self.config is None:
with open(self.path[0], 'r') as config:
termset_config = yaml.safe_load(config)
self.config = termset_config

# # Check data_types within new config to see if they already exist in the current config
# with open(config_path, 'r') as config:
# termset_config = yaml.safe_load(config)
# for data_type_dict in termset_config:
# if data_type_dict['data_type'] in self.get_data_types():
# pass
#
#
# # append path to new config to self.path
# if config_path is not None:
# self.path.append(config_path)

with open(self.path, 'r') as config:
termset_config = yaml.safe_load(config)
return termset_config

def unload_termset_config():
"""
Remove validation according to termset configuration file.
"""
self.path = None
self.path = ['/Users/mavaylon/Research/NWB/hdmf2/hdmf/docs/gallery/example_config.yaml']
self.config = None

0 comments on commit 0b83e58

Please sign in to comment.