Skip to content

Commit bf3294e

Browse files
rlystephprince
andauthored
Do not set data_type_inc=NWBContainer for NWBContainer (#2135)
Co-authored-by: Steph Prince <[email protected]>
1 parent fa6a214 commit bf3294e

File tree

3 files changed

+86
-9
lines changed

3 files changed

+86
-9
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
### Fixed
66
- Fixed incorrect warning for path not ending in `.nwb` when no path argument was provided. @t-b [#2130](https://github.com/NeurodataWithoutBorders/pynwb/pull/2130)
7+
- Fixed issue with setting `neurodata_type_inc` when reading NWB files with cached schema versions less than 2.2.0. @rly [#2135](https://github.com/NeurodataWithoutBorders/pynwb/pull/2135)
78
- Fixed import structure test. @rly [#2136](https://github.com/NeurodataWithoutBorders/pynwb/pull/2136)
9+
810
### Documentation and tutorial enhancements
911
- Change UI of assistant to be an accordion that is always visible. [#2124](https://github.com/NeurodataWithoutBorders/pynwb/pull/2124)
1012

src/pynwb/spec.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,8 @@ class NWBDatasetSpec(BaseStorageOverride, DatasetSpec):
150150
def __init__(self, **kwargs):
151151
kwargs = self._translate_kwargs(kwargs)
152152
# set data_type_inc to NWBData only if it is not specified and the type is not an HDMF base type
153-
if kwargs['data_type_inc'] is None and kwargs['data_type_def'] not in (None, 'Data'):
153+
exclude_set_data_type_inc_types = (None, 'Data', 'NWBData')
154+
if kwargs['data_type_inc'] is None and kwargs['data_type_def'] not in exclude_set_data_type_inc_types:
154155
kwargs['data_type_inc'] = 'NWBData'
155156
super().__init__(**kwargs)
156157

@@ -167,10 +168,13 @@ class NWBGroupSpec(BaseStorageOverride, GroupSpec):
167168
@docval(*deepcopy(_group_docval))
168169
def __init__(self, **kwargs):
169170
kwargs = self._translate_kwargs(kwargs)
170-
# set data_type_inc to NWBData only if it is not specified and the type is not an HDMF base type
171-
# NOTE: CSRMatrix in hdmf-common-schema does not have a data_type_inc but should not inherit from
172-
# NWBContainer. This will be fixed in hdmf-common-schema 1.2.1.
173-
if kwargs['data_type_inc'] is None and kwargs['data_type_def'] not in (None, 'Container', 'CSRMatrix'):
171+
# set data_type_inc to NWBContainer only if it is not specified or special cases
172+
# NOTE: NWBContainer in nwb-schema < 2.2.0 had no neurodata_type_inc, so we need to exclude it here to avoid
173+
# setting data_type_inc to NWBContainer for NWBContainer itself
174+
# NOTE: CSRMatrix in hdmf-common-schema < 1.2.1 had no data_type_inc, but should not inherit from
175+
# NWBContainer.
176+
exclude_set_data_type_inc_types = (None, 'Container', 'NWBContainer', 'CSRMatrix')
177+
if kwargs['data_type_inc'] is None and kwargs['data_type_def'] not in exclude_set_data_type_inc_types:
174178
kwargs['data_type_inc'] = 'NWBContainer'
175179
super().__init__(**kwargs)
176180

tests/unit/test_spec.py

Lines changed: 75 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
import json
88

99
from pynwb.spec import (
10-
NWBNamespaceBuilder,
11-
NWBRefSpec,
12-
NWBLinkSpec,
10+
NWBNamespaceBuilder,
11+
NWBRefSpec,
12+
NWBLinkSpec,
1313
NWBDtypeSpec,
14-
NWBGroupSpec,
14+
NWBGroupSpec,
1515
NWBDatasetSpec
1616
)
1717
from pynwb.testing import TestCase
@@ -116,3 +116,74 @@ def test_add_dataset(self):
116116
self.assertEqual(len(spec.datasets), 1)
117117
self.assertEqual(spec.datasets[0].name, 'dataset1')
118118
self.assertIsInstance(spec.datasets[0], NWBDatasetSpec)
119+
120+
def test_set_neurodata_type_inc(self):
121+
# neurodata_type_inc should be set to NWBContainer if not specified and neurodata_type_def is not a base type
122+
spec = NWBGroupSpec(
123+
doc='A test group',
124+
neurodata_type_def='MyCustomType',
125+
name='Group1',
126+
)
127+
self.assertEqual(spec.neurodata_type_inc, 'NWBContainer')
128+
129+
# neurodata_type_inc should not be set to NWBContainer if neurodata_type_def is a base type
130+
spec = NWBGroupSpec(
131+
doc='A test group',
132+
neurodata_type_def='Container',
133+
name='Group1',
134+
)
135+
self.assertIsNone(spec.neurodata_type_inc)
136+
137+
# neurodata_type_inc should not be set to NWBContainer if neurodata_type_def is a base type
138+
spec = NWBGroupSpec(
139+
doc='A test group',
140+
neurodata_type_def='NWBContainer',
141+
name='Group1',
142+
)
143+
self.assertIsNone(spec.neurodata_type_inc)
144+
145+
# neurodata_type_inc should not be set to NWBContainer if it is explicitly specified
146+
spec = NWBGroupSpec(
147+
doc='A test group',
148+
neurodata_type_def='MyCustomType',
149+
neurodata_type_inc='SomeOtherType',
150+
name='Group1',
151+
)
152+
self.assertEqual(spec.neurodata_type_inc, 'SomeOtherType')
153+
154+
155+
class NWBDatasetSpecTest(TestCase):
156+
157+
def test_set_neurodata_type_inc(self):
158+
# neurodata_type_inc should be set to NWBData if not specified and neurodata_type_def is not a base type
159+
spec = NWBDatasetSpec(
160+
doc='A test dataset',
161+
neurodata_type_def='MyCustomType',
162+
name='dataset1',
163+
)
164+
self.assertEqual(spec.neurodata_type_inc, 'NWBData')
165+
166+
# neurodata_type_inc should not be set to NWBData if neurodata_type_def is a base type
167+
spec = NWBDatasetSpec(
168+
doc='A test dataset',
169+
neurodata_type_def='Data',
170+
name='dataset1',
171+
)
172+
self.assertIsNone(spec.neurodata_type_inc)
173+
174+
# neurodata_type_inc should not be set to NWBDataset if neurodata_type_def is a base type
175+
spec = NWBDatasetSpec(
176+
doc='A test dataset',
177+
neurodata_type_def='NWBData',
178+
name='dataset1',
179+
)
180+
self.assertIsNone(spec.neurodata_type_inc)
181+
182+
# neurodata_type_inc should not be set to NWBData if it is explicitly specified
183+
spec = NWBDatasetSpec(
184+
doc='A test dataset',
185+
neurodata_type_def='MyCustomType',
186+
neurodata_type_inc='SomeOtherType',
187+
name='dataset1',
188+
)
189+
self.assertEqual(spec.neurodata_type_inc, 'SomeOtherType')

0 commit comments

Comments
 (0)