| 1 |
import json |
| 2 |
|
| 3 |
from hdmf.spec import GroupSpec, DatasetSpec, AttributeSpec, DtypeSpec, RefSpec |
| 4 |
from hdmf.testing import TestCase |
| 5 |
|
| 6 |
|
| 7 |
class DatasetSpecTests(TestCase): |
| 8 |
def setUp(self): |
| 9 |
self.attributes = [ |
| 10 |
AttributeSpec('attribute1', 'my first attribute', 'text'), |
| 11 |
AttributeSpec('attribute2', 'my second attribute', 'text') |
| 12 |
] |
| 13 |
|
| 14 |
def test_constructor(self): |
| 15 |
spec = DatasetSpec('my first dataset', |
| 16 |
'int', |
| 17 |
name='dataset1', |
| 18 |
attributes=self.attributes) |
| 19 |
self.assertEqual(spec['dtype'], 'int') |
| 20 |
self.assertEqual(spec['name'], 'dataset1') |
| 21 |
self.assertEqual(spec['doc'], 'my first dataset') |
| 22 |
self.assertNotIn('linkable', spec) |
| 23 |
self.assertNotIn('data_type_def', spec) |
| 24 |
self.assertListEqual(spec['attributes'], self.attributes) |
| 25 |
self.assertIs(spec, self.attributes[0].parent) |
| 26 |
self.assertIs(spec, self.attributes[1].parent) |
| 27 |
json.dumps(spec) |
| 28 |
|
| 29 |
def test_constructor_datatype(self): |
| 30 |
spec = DatasetSpec('my first dataset', |
| 31 |
'int', |
| 32 |
name='dataset1', |
| 33 |
attributes=self.attributes, |
| 34 |
linkable=False, |
| 35 |
data_type_def='EphysData') |
| 36 |
self.assertEqual(spec['dtype'], 'int') |
| 37 |
self.assertEqual(spec['name'], 'dataset1') |
| 38 |
self.assertEqual(spec['doc'], 'my first dataset') |
| 39 |
self.assertEqual(spec['data_type_def'], 'EphysData') |
| 40 |
self.assertFalse(spec['linkable']) |
| 41 |
self.assertListEqual(spec['attributes'], self.attributes) |
| 42 |
self.assertIs(spec, self.attributes[0].parent) |
| 43 |
self.assertIs(spec, self.attributes[1].parent) |
| 44 |
|
| 45 |
def test_constructor_shape(self): |
| 46 |
shape = [None, 2] |
| 47 |
spec = DatasetSpec('my first dataset', |
| 48 |
'int', |
| 49 |
name='dataset1', |
| 50 |
shape=shape, |
| 51 |
attributes=self.attributes) |
| 52 |
self.assertEqual(spec['shape'], shape) |
| 53 |
self.assertEqual(spec.shape, shape) |
| 54 |
|
| 55 |
def test_constructor_invalidate_dtype(self): |
| 56 |
with self.assertRaises(ValueError): |
| 57 |
DatasetSpec(doc='my first dataset', |
| 58 |
dtype='my bad dtype', # <-- Expect AssertionError due to bad type |
| 59 |
name='dataset1', |
| 60 |
dims=(None, None), |
| 61 |
attributes=self.attributes, |
| 62 |
linkable=False, |
| 63 |
data_type_def='EphysData') |
| 64 |
|
| 65 |
def test_constructor_ref_spec(self): |
| 66 |
dtype = RefSpec('TimeSeries', 'object') |
| 67 |
spec = DatasetSpec(doc='my first dataset', |
| 68 |
dtype=dtype, |
| 69 |
name='dataset1', |
| 70 |
dims=(None, None), |
| 71 |
attributes=self.attributes, |
| 72 |
linkable=False, |
| 73 |
data_type_def='EphysData') |
| 74 |
self.assertDictEqual(spec['dtype'], dtype) |
| 75 |
|
| 76 |
def test_datatype_extension(self): |
| 77 |
base = DatasetSpec('my first dataset', |
| 78 |
'int', |
| 79 |
name='dataset1', |
| 80 |
attributes=self.attributes, |
| 81 |
linkable=False, |
| 82 |
data_type_def='EphysData') |
| 83 |
|
| 84 |
attributes = [AttributeSpec('attribute3', 'my first extending attribute', 'float')] |
| 85 |
ext = DatasetSpec('my first dataset extension', |
| 86 |
'int', |
| 87 |
name='dataset1', |
| 88 |
attributes=attributes, |
| 89 |
linkable=False, |
| 90 |
data_type_inc=base, |
| 91 |
data_type_def='SpikeData') |
| 92 |
self.assertDictEqual(ext['attributes'][0], attributes[0]) |
| 93 |
self.assertDictEqual(ext['attributes'][1], self.attributes[0]) |
| 94 |
self.assertDictEqual(ext['attributes'][2], self.attributes[1]) |
| 95 |
ext_attrs = ext.attributes |
| 96 |
self.assertIs(ext, ext_attrs[0].parent) |
| 97 |
self.assertIs(ext, ext_attrs[1].parent) |
| 98 |
self.assertIs(ext, ext_attrs[2].parent) |
| 99 |
|
| 100 |
def test_datatype_extension_groupspec(self): |
| 101 |
'''Test to make sure DatasetSpec catches when a GroupSpec used as data_type_inc''' |
| 102 |
base = GroupSpec('a fake grop', |
| 103 |
data_type_def='EphysData') |
| 104 |
with self.assertRaises(TypeError): |
| 105 |
DatasetSpec('my first dataset extension', |
| 106 |
'int', |
| 107 |
name='dataset1', |
| 108 |
data_type_inc=base, |
| 109 |
data_type_def='SpikeData') |
| 110 |
|
| 111 |
def test_constructor_table(self): |
| 112 |
dtype1 = DtypeSpec('column1', 'the first column', 'int') |
| 113 |
dtype2 = DtypeSpec('column2', 'the second column', 'float') |
| 114 |
spec = DatasetSpec('my first table', |
| 115 |
[dtype1, dtype2], |
| 116 |
name='table1', |
| 117 |
attributes=self.attributes) |
| 118 |
self.assertEqual(spec['dtype'], [dtype1, dtype2]) |
| 119 |
self.assertEqual(spec['name'], 'table1') |
| 120 |
self.assertEqual(spec['doc'], 'my first table') |
| 121 |
self.assertNotIn('linkable', spec) |
| 122 |
self.assertNotIn('data_type_def', spec) |
| 123 |
self.assertListEqual(spec['attributes'], self.attributes) |
| 124 |
self.assertIs(spec, self.attributes[0].parent) |
| 125 |
self.assertIs(spec, self.attributes[1].parent) |
| 126 |
json.dumps(spec) |
| 127 |
|
| 128 |
def test_constructor_invalid_table(self): |
| 129 |
with self.assertRaises(ValueError): |
| 130 |
DatasetSpec('my first table', |
| 131 |
[DtypeSpec('column1', 'the first column', 'int'), |
| 132 |
{} # <--- Bad compound type spec must raise an error |
| 133 |
], |
| 134 |
name='table1', |
| 135 |
attributes=self.attributes) |
| 136 |
|
| 137 |
def test_constructor_default_value(self): |
| 138 |
spec = DatasetSpec(doc='test', |
| 139 |
default_value=5, |
| 140 |
dtype='int', |
| 141 |
data_type_def='test') |
| 142 |
self.assertEqual(spec.default_value, 5) |
| 143 |
|
| 144 |
def test_name_with_incompatible_quantity(self): |
| 145 |
# Check that we raise an error when the quantity allows more than one instance with a fixed name |
| 146 |
with self.assertRaises(ValueError): |
| 147 |
DatasetSpec(doc='my first dataset', |
| 148 |
dtype='int', |
| 149 |
name='ds1', |
| 150 |
quantity='zero_or_many') |
| 151 |
with self.assertRaises(ValueError): |
| 152 |
DatasetSpec(doc='my first dataset', |
| 153 |
dtype='int', |
| 154 |
name='ds1', |
| 155 |
quantity='one_or_many') |
| 156 |
|
| 157 |
def test_name_with_compatible_quantity(self): |
| 158 |
# Make sure compatible quantity flags pass when name is fixed |
| 159 |
DatasetSpec(doc='my first dataset', |
| 160 |
dtype='int', |
| 161 |
name='ds1', |
| 162 |
quantity='zero_or_one') |
| 163 |
DatasetSpec(doc='my first dataset', |
| 164 |
dtype='int', |
| 165 |
name='ds1', |
| 166 |
quantity=1) |
| 167 |
|
| 168 |
def test_datatype_table_extension(self): |
| 169 |
dtype1 = DtypeSpec('column1', 'the first column', 'int') |
| 170 |
dtype2 = DtypeSpec('column2', 'the second column', 'float') |
| 171 |
base = DatasetSpec('my first table', |
| 172 |
[dtype1, dtype2], |
| 173 |
attributes=self.attributes, |
| 174 |
data_type_def='SimpleTable') |
| 175 |
self.assertEqual(base['dtype'], [dtype1, dtype2]) |
| 176 |
self.assertEqual(base['doc'], 'my first table') |
| 177 |
dtype3 = DtypeSpec('column3', 'the third column', 'text') |
| 178 |
ext = DatasetSpec('my first table extension', |
| 179 |
[dtype3], |
| 180 |
data_type_inc=base, |
| 181 |
data_type_def='ExtendedTable') |
| 182 |
self.assertEqual(ext['dtype'], [dtype1, dtype2, dtype3]) |
| 183 |
self.assertEqual(ext['doc'], 'my first table extension') |
| 184 |
|
| 185 |
def test_datatype_table_extension_higher_precision(self): |
| 186 |
dtype1 = DtypeSpec('column1', 'the first column', 'int') |
| 187 |
dtype2 = DtypeSpec('column2', 'the second column', 'float32') |
| 188 |
base = DatasetSpec('my first table', |
| 189 |
[dtype1, dtype2], |
| 190 |
attributes=self.attributes, |
| 191 |
data_type_def='SimpleTable') |
| 192 |
self.assertEqual(base['dtype'], [dtype1, dtype2]) |
| 193 |
self.assertEqual(base['doc'], 'my first table') |
| 194 |
dtype3 = DtypeSpec('column2', 'the second column, with greater precision', 'float64') |
| 195 |
ext = DatasetSpec('my first table extension', |
| 196 |
[dtype3], |
| 197 |
data_type_inc=base, |
| 198 |
data_type_def='ExtendedTable') |
| 199 |
self.assertEqual(ext['dtype'], [dtype1, dtype3]) |
| 200 |
self.assertEqual(ext['doc'], 'my first table extension') |
| 201 |
|
| 202 |
def test_datatype_table_extension_lower_precision(self): |
| 203 |
dtype1 = DtypeSpec('column1', 'the first column', 'int') |
| 204 |
dtype2 = DtypeSpec('column2', 'the second column', 'float64') |
| 205 |
base = DatasetSpec('my first table', |
| 206 |
[dtype1, dtype2], |
| 207 |
attributes=self.attributes, |
| 208 |
data_type_def='SimpleTable') |
| 209 |
self.assertEqual(base['dtype'], [dtype1, dtype2]) |
| 210 |
self.assertEqual(base['doc'], 'my first table') |
| 211 |
dtype3 = DtypeSpec('column2', 'the second column, with greater precision', 'float32') |
| 212 |
with self.assertRaisesWith(ValueError, 'Cannot extend float64 to float32'): |
| 213 |
DatasetSpec('my first table extension', |
| 214 |
[dtype3], |
| 215 |
data_type_inc=base, |
| 216 |
data_type_def='ExtendedTable') |
| 217 |
|
| 218 |
def test_datatype_table_extension_diff_format(self): |
| 219 |
dtype1 = DtypeSpec('column1', 'the first column', 'int') |
| 220 |
dtype2 = DtypeSpec('column2', 'the second column', 'float64') |
| 221 |
base = DatasetSpec('my first table', |
| 222 |
[dtype1, dtype2], |
| 223 |
attributes=self.attributes, |
| 224 |
data_type_def='SimpleTable') |
| 225 |
self.assertEqual(base['dtype'], [dtype1, dtype2]) |
| 226 |
self.assertEqual(base['doc'], 'my first table') |
| 227 |
dtype3 = DtypeSpec('column2', 'the second column, with greater precision', 'int32') |
| 228 |
with self.assertRaisesWith(ValueError, 'Cannot extend float64 to int32'): |
| 229 |
DatasetSpec('my first table extension', |
| 230 |
[dtype3], |
| 231 |
data_type_inc=base, |
| 232 |
data_type_def='ExtendedTable') |
| 233 |
|
| 234 |
def test_data_type_property_value(self): |
| 235 |
"""Test that the property data_type has the expected value""" |
| 236 |
test_cases = { |
| 237 |
('Foo', 'Bar'): 'Bar', |
| 238 |
('Foo', None): 'Foo', |
| 239 |
(None, 'Bar'): 'Bar', |
| 240 |
(None, None): None, |
| 241 |
} |
| 242 |
for (data_type_inc, data_type_def), data_type in test_cases.items(): |
| 243 |
with self.subTest(data_type_inc=data_type_inc, |
| 244 |
data_type_def=data_type_def, data_type=data_type): |
| 245 |
group = GroupSpec('A group', name='group', |
| 246 |
data_type_inc=data_type_inc, data_type_def=data_type_def) |
| 247 |
self.assertEqual(group.data_type, data_type) |
| 248 |
|
| 249 |
def test_constructor_value(self): |
| 250 |
spec = DatasetSpec(doc='my first dataset', dtype='int', name='dataset1', value=42) |
| 251 |
assert spec.value == 42 |
| 252 |
|
| 253 |
def test_build_warn_extra_args(self): |
| 254 |
spec_dict = { |
| 255 |
'name': 'dataset1', |
| 256 |
'doc': 'test dataset', |
| 257 |
'dtype': 'int', |
| 258 |
'required': True, |
| 259 |
} |
| 260 |
msg = ("Unexpected keys ['required'] in spec {'name': 'dataset1', 'doc': 'test dataset', " |
| 261 |
"'dtype': 'int', 'required': True}") |
| 262 |
with self.assertWarnsWith(UserWarning, msg): |
| 263 |
DatasetSpec.build_spec(spec_dict) |