| 1 |
import copy |
| 2 |
|
| 3 |
from hdmf.spec import GroupSpec, DatasetSpec, AttributeSpec, SpecCatalog |
| 4 |
from hdmf.testing import TestCase |
| 5 |
|
| 6 |
|
| 7 |
class SpecCatalogTest(TestCase): |
| 8 |
|
| 9 |
def setUp(self): |
| 10 |
self.catalog = SpecCatalog() |
| 11 |
self.attributes = [ |
| 12 |
AttributeSpec('attribute1', 'my first attribute', 'text'), |
| 13 |
AttributeSpec('attribute2', 'my second attribute', 'text') |
| 14 |
] |
| 15 |
self.spec = DatasetSpec('my first dataset', |
| 16 |
'int', |
| 17 |
name='dataset1', |
| 18 |
dims=(None, None), |
| 19 |
attributes=self.attributes, |
| 20 |
linkable=False, |
| 21 |
data_type_def='EphysData') |
| 22 |
|
| 23 |
def test_register_spec(self): |
| 24 |
self.catalog.register_spec(self.spec, 'test.yaml') |
| 25 |
result = self.catalog.get_spec('EphysData') |
| 26 |
self.assertIs(result, self.spec) |
| 27 |
|
| 28 |
def test_hierarchy(self): |
| 29 |
spikes_spec = DatasetSpec('my extending dataset', 'int', |
| 30 |
data_type_inc='EphysData', |
| 31 |
data_type_def='SpikeData') |
| 32 |
|
| 33 |
lfp_spec = DatasetSpec('my second extending dataset', 'int', |
| 34 |
data_type_inc='EphysData', |
| 35 |
data_type_def='LFPData') |
| 36 |
|
| 37 |
self.catalog.register_spec(self.spec, 'test.yaml') |
| 38 |
self.catalog.register_spec(spikes_spec, 'test.yaml') |
| 39 |
self.catalog.register_spec(lfp_spec, 'test.yaml') |
| 40 |
|
| 41 |
spike_hierarchy = self.catalog.get_hierarchy('SpikeData') |
| 42 |
lfp_hierarchy = self.catalog.get_hierarchy('LFPData') |
| 43 |
ephys_hierarchy = self.catalog.get_hierarchy('EphysData') |
| 44 |
self.assertTupleEqual(spike_hierarchy, ('SpikeData', 'EphysData')) |
| 45 |
self.assertTupleEqual(lfp_hierarchy, ('LFPData', 'EphysData')) |
| 46 |
self.assertTupleEqual(ephys_hierarchy, ('EphysData',)) |
| 47 |
|
| 48 |
def test_subtypes(self): |
| 49 |
""" |
| 50 |
-BaseContainer--+-->AContainer--->ADContainer |
| 51 |
| |
| 52 |
+-->BContainer |
| 53 |
""" |
| 54 |
base_spec = GroupSpec(doc='Base container', |
| 55 |
data_type_def='BaseContainer') |
| 56 |
acontainer = GroupSpec(doc='AContainer', |
| 57 |
data_type_inc='BaseContainer', |
| 58 |
data_type_def='AContainer') |
| 59 |
adcontainer = GroupSpec(doc='ADContainer', |
| 60 |
data_type_inc='AContainer', |
| 61 |
data_type_def='ADContainer') |
| 62 |
bcontainer = GroupSpec(doc='BContainer', |
| 63 |
data_type_inc='BaseContainer', |
| 64 |
data_type_def='BContainer') |
| 65 |
self.catalog.register_spec(base_spec, 'test.yaml') |
| 66 |
self.catalog.register_spec(acontainer, 'test.yaml') |
| 67 |
self.catalog.register_spec(adcontainer, 'test.yaml') |
| 68 |
self.catalog.register_spec(bcontainer, 'test.yaml') |
| 69 |
base_spec_subtypes = self.catalog.get_subtypes('BaseContainer') |
| 70 |
base_spec_subtypes = tuple(sorted(base_spec_subtypes)) # Sort so we have a guaranteed order for comparison |
| 71 |
acontainer_subtypes = self.catalog.get_subtypes('AContainer') |
| 72 |
bcontainer_substypes = self.catalog.get_subtypes('BContainer') |
| 73 |
adcontainer_subtypes = self.catalog.get_subtypes('ADContainer') |
| 74 |
self.assertTupleEqual(adcontainer_subtypes, ()) |
| 75 |
self.assertTupleEqual(bcontainer_substypes, ()) |
| 76 |
self.assertTupleEqual(acontainer_subtypes, ('ADContainer',)) |
| 77 |
self.assertTupleEqual(base_spec_subtypes, ('AContainer', 'ADContainer', 'BContainer')) |
| 78 |
|
| 79 |
def test_subtypes_norecursion(self): |
| 80 |
""" |
| 81 |
-BaseContainer--+-->AContainer--->ADContainer |
| 82 |
| |
| 83 |
+-->BContainer |
| 84 |
""" |
| 85 |
base_spec = GroupSpec(doc='Base container', |
| 86 |
data_type_def='BaseContainer') |
| 87 |
acontainer = GroupSpec(doc='AContainer', |
| 88 |
data_type_inc='BaseContainer', |
| 89 |
data_type_def='AContainer') |
| 90 |
adcontainer = GroupSpec(doc='ADContainer', |
| 91 |
data_type_inc='AContainer', |
| 92 |
data_type_def='ADContainer') |
| 93 |
bcontainer = GroupSpec(doc='BContainer', |
| 94 |
data_type_inc='BaseContainer', |
| 95 |
data_type_def='BContainer') |
| 96 |
self.catalog.register_spec(base_spec, 'test.yaml') |
| 97 |
self.catalog.register_spec(acontainer, 'test.yaml') |
| 98 |
self.catalog.register_spec(adcontainer, 'test.yaml') |
| 99 |
self.catalog.register_spec(bcontainer, 'test.yaml') |
| 100 |
base_spec_subtypes = self.catalog.get_subtypes('BaseContainer', recursive=False) |
| 101 |
base_spec_subtypes = tuple(sorted(base_spec_subtypes)) # Sort so we have a guaranteed order for comparison |
| 102 |
acontainer_subtypes = self.catalog.get_subtypes('AContainer', recursive=False) |
| 103 |
bcontainer_substypes = self.catalog.get_subtypes('BContainer', recursive=False) |
| 104 |
adcontainer_subtypes = self.catalog.get_subtypes('ADContainer', recursive=False) |
| 105 |
self.assertTupleEqual(adcontainer_subtypes, ()) |
| 106 |
self.assertTupleEqual(bcontainer_substypes, ()) |
| 107 |
self.assertTupleEqual(acontainer_subtypes, ('ADContainer',)) |
| 108 |
self.assertTupleEqual(base_spec_subtypes, ('AContainer', 'BContainer')) |
| 109 |
|
| 110 |
def test_subtypes_unknown_type(self): |
| 111 |
subtypes_of_bad_type = self.catalog.get_subtypes('UnknownType') |
| 112 |
self.assertTupleEqual(subtypes_of_bad_type, ()) |
| 113 |
|
| 114 |
def test_get_spec_source_file(self): |
| 115 |
spikes_spec = GroupSpec('test group', |
| 116 |
data_type_def='SpikeData') |
| 117 |
source_file_path = '/test/myt/test.yaml' |
| 118 |
self.catalog.auto_register(spikes_spec, source_file_path) |
| 119 |
recorded_source_file_path = self.catalog.get_spec_source_file('SpikeData') |
| 120 |
self.assertEqual(recorded_source_file_path, source_file_path) |
| 121 |
|
| 122 |
def test_get_full_hierarchy(self): |
| 123 |
""" |
| 124 |
BaseContainer--+-->AContainer--->ADContainer |
| 125 |
| |
| 126 |
+-->BContainer |
| 127 |
|
| 128 |
Expected output: |
| 129 |
>> print(json.dumps(full_hierarchy, indent=4)) |
| 130 |
>> { |
| 131 |
>> "BaseContainer": { |
| 132 |
>> "AContainer": { |
| 133 |
>> "ADContainer": {} |
| 134 |
>> }, |
| 135 |
>> "BContainer": {} |
| 136 |
>> } |
| 137 |
""" |
| 138 |
base_spec = GroupSpec(doc='Base container', |
| 139 |
data_type_def='BaseContainer') |
| 140 |
acontainer = GroupSpec(doc='AContainer', |
| 141 |
data_type_inc='BaseContainer', |
| 142 |
data_type_def='AContainer') |
| 143 |
adcontainer = GroupSpec(doc='ADContainer', |
| 144 |
data_type_inc='AContainer', |
| 145 |
data_type_def='ADContainer') |
| 146 |
bcontainer = GroupSpec(doc='BContainer', |
| 147 |
data_type_inc='BaseContainer', |
| 148 |
data_type_def='BContainer') |
| 149 |
self.catalog.register_spec(base_spec, 'test.yaml') |
| 150 |
self.catalog.register_spec(acontainer, 'test.yaml') |
| 151 |
self.catalog.register_spec(adcontainer, 'test.yaml') |
| 152 |
self.catalog.register_spec(bcontainer, 'test.yaml') |
| 153 |
full_hierarchy = self.catalog.get_full_hierarchy() |
| 154 |
expected_hierarchy = { |
| 155 |
"BaseContainer": { |
| 156 |
"AContainer": { |
| 157 |
"ADContainer": {} |
| 158 |
}, |
| 159 |
"BContainer": {} |
| 160 |
} |
| 161 |
} |
| 162 |
self.assertDictEqual(full_hierarchy, expected_hierarchy) |
| 163 |
|
| 164 |
def test_copy_spec_catalog(self): |
| 165 |
# Register the spec first |
| 166 |
self.catalog.register_spec(self.spec, 'test.yaml') |
| 167 |
result = self.catalog.get_spec('EphysData') |
| 168 |
self.assertIs(result, self.spec) |
| 169 |
# Now test the copy |
| 170 |
re = copy.copy(self.catalog) |
| 171 |
self.assertTupleEqual(self.catalog.get_registered_types(), |
| 172 |
re.get_registered_types()) |
| 173 |
|
| 174 |
def test_deepcopy_spec_catalog(self): |
| 175 |
# Register the spec first |
| 176 |
self.catalog.register_spec(self.spec, 'test.yaml') |
| 177 |
result = self.catalog.get_spec('EphysData') |
| 178 |
self.assertIs(result, self.spec) |
| 179 |
# Now test the copy |
| 180 |
re = copy.deepcopy(self.catalog) |
| 181 |
self.assertTupleEqual(self.catalog.get_registered_types(), |
| 182 |
re.get_registered_types()) |
| 183 |
|
| 184 |
def test_catch_duplicate_spec_nested(self): |
| 185 |
spec1 = GroupSpec( |
| 186 |
data_type_def='Group1', |
| 187 |
doc='This is my new group 1', |
| 188 |
) |
| 189 |
spec2 = GroupSpec( |
| 190 |
data_type_def='Group2', |
| 191 |
doc='This is my new group 2', |
| 192 |
groups=[spec1], # nested definition |
| 193 |
) |
| 194 |
source = 'test_extension.yaml' |
| 195 |
self.catalog.register_spec(spec1, source) |
| 196 |
self.catalog.register_spec(spec2, source) # this is OK because Group1 is the same spec |
| 197 |
ret = self.catalog.get_spec('Group1') |
| 198 |
self.assertIs(ret, spec1) |
| 199 |
|
| 200 |
def test_catch_duplicate_spec_different(self): |
| 201 |
spec1 = GroupSpec( |
| 202 |
data_type_def='Group1', |
| 203 |
doc='This is my new group 1', |
| 204 |
) |
| 205 |
spec2 = GroupSpec( |
| 206 |
data_type_def='Group1', |
| 207 |
doc='This is my other group 1', |
| 208 |
) |
| 209 |
source = 'test_extension.yaml' |
| 210 |
self.catalog.register_spec(spec1, source) |
| 211 |
msg = "'Group1' - cannot overwrite existing specification" |
| 212 |
with self.assertRaisesWith(ValueError, msg): |
| 213 |
self.catalog.register_spec(spec2, source) |
| 214 |
|
| 215 |
def test_catch_duplicate_spec_different_source(self): |
| 216 |
spec1 = GroupSpec( |
| 217 |
data_type_def='Group1', |
| 218 |
doc='This is my new group 1', |
| 219 |
) |
| 220 |
spec2 = GroupSpec( |
| 221 |
data_type_def='Group1', |
| 222 |
doc='This is my new group 1', |
| 223 |
) |
| 224 |
source1 = 'test_extension1.yaml' |
| 225 |
source2 = 'test_extension2.yaml' |
| 226 |
self.catalog.register_spec(spec1, source1) |
| 227 |
msg = "'Group1' - cannot overwrite existing specification" |
| 228 |
with self.assertRaisesWith(ValueError, msg): |
| 229 |
self.catalog.register_spec(spec2, source2) |