From 58f0f1fbbdd72db419e5f307555aa78de95dd869 Mon Sep 17 00:00:00 2001 From: mavaylon1 Date: Thu, 16 Nov 2023 12:11:59 -0800 Subject: [PATCH 1/2] TermSet Config --- src/pynwb/core.py | 19 +++++++++++++++++-- src/pynwb/file.py | 7 +++++-- src/pynwb/nwb-schema | 2 +- 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/src/pynwb/core.py b/src/pynwb/core.py index b54f3e147..4fd83456e 100644 --- a/src/pynwb/core.py +++ b/src/pynwb/core.py @@ -2,11 +2,13 @@ import numpy as np -from hdmf import Container, Data +import yaml + +from hdmf import Container, Data, TermSet, TermSetWrapper from hdmf.container import AbstractContainer, MultiContainerInterface as hdmf_MultiContainerInterface, Table from hdmf.common import DynamicTable, DynamicTableRegion # noqa: F401 from hdmf.common import VectorData, VectorIndex, ElementIdentifiers # noqa: F401 -from hdmf.utils import docval, popargs +from hdmf.utils import docval, popargs, get_docval from hdmf.utils import LabelledDict # noqa: F401 from . import CORE_NAMESPACE, register_class @@ -54,6 +56,19 @@ class NWBContainer(NWBMixin, Container): __nwbfields__ = tuple() + def init_validation(self, kwargs): + # Before calling super().__init__() and before setting fields, check for termset_config.yaml. + # This file will be stored within nwb-schema + with open('src/pynwb/nwb-schema/termset_config/termset_config.yaml', 'r') as config: + termset_config = yaml.safe_load(config) + object_name = self.__class__.__name__ # the name field is not set yet + if object_name in termset_config: + for field in termset_config[object_name]: + if field in kwargs: # make sure any custom fields are not handled (i.e., make an extension) + termset_path = termset_config[object_name][field] + termset = TermSet(term_schema_path=termset_path) + kwargs[field] = TermSetWrapper(value=kwargs[field], termset=termset) + @register_class('NWBDataInterface', CORE_NAMESPACE) class NWBDataInterface(NWBContainer): diff --git a/src/pynwb/file.py b/src/pynwb/file.py index b473e571a..7ef155e7d 100644 --- a/src/pynwb/file.py +++ b/src/pynwb/file.py @@ -109,6 +109,7 @@ class Subject(NWBContainer): {'name': 'strain', 'type': str, 'doc': 'The strain of the subject, e.g., "C57BL/6J"', 'default': None}, ) def __init__(self, **kwargs): + self.init_validation(kwargs) keys_to_set = ( "age", "age__reference", @@ -122,8 +123,10 @@ def __init__(self, **kwargs): "strain", ) args_to_set = popargs_to_dict(keys_to_set, kwargs) - super().__init__(name="subject", **kwargs) - + kwargs['name'] = 'subject' + super().__init__(**kwargs) + # if kwargs['validate']: + # breakpoint() # NOTE when the Subject I/O mapper (see pynwb.io.file.py) reads an age__reference value of None from an # NWB 2.0-2.5 file, it sets the value to "unspecified" so that when Subject.__init__ is called, the incoming # age__reference value is NOT replaced by the default value ("birth") specified in the docval. diff --git a/src/pynwb/nwb-schema b/src/pynwb/nwb-schema index b4f8838cb..19c4bbb36 160000 --- a/src/pynwb/nwb-schema +++ b/src/pynwb/nwb-schema @@ -1 +1 @@ -Subproject commit b4f8838cbfbb7f8a117bd7e0aad19133d26868b4 +Subproject commit 19c4bbb36effb1454253072f70ebf2c83f2ea095 From 1d7a66e94096dde011c6e17d33b1dc5a4e8ae6bc Mon Sep 17 00:00:00 2001 From: mavaylon1 Date: Thu, 16 Nov 2023 12:33:19 -0800 Subject: [PATCH 2/2] clean --- src/pynwb/file.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/pynwb/file.py b/src/pynwb/file.py index 7ef155e7d..3d710002d 100644 --- a/src/pynwb/file.py +++ b/src/pynwb/file.py @@ -125,8 +125,6 @@ def __init__(self, **kwargs): args_to_set = popargs_to_dict(keys_to_set, kwargs) kwargs['name'] = 'subject' super().__init__(**kwargs) - # if kwargs['validate']: - # breakpoint() # NOTE when the Subject I/O mapper (see pynwb.io.file.py) reads an age__reference value of None from an # NWB 2.0-2.5 file, it sets the value to "unspecified" so that when Subject.__init__ is called, the incoming # age__reference value is NOT replaced by the default value ("birth") specified in the docval.