def getSpecs(self): bar = GroupSpec('A test group specification with a data type', data_type_def='Bar', datasets=[ DatasetSpec('an example dataset', 'int', name='data', attributes=[ AttributeSpec( 'attr2', 'an example integer attribute', 'int') ]) ], attributes=[ AttributeSpec('attr1', 'an example string attribute', 'str') ]) foo = GroupSpec('A test group that contains a data type', data_type_def='Foo', groups=[ GroupSpec('A Bar group for Foos', name='my_bar', data_type_inc='Bar') ], attributes=[ AttributeSpec( 'foo_attr', 'a string attribute specified as text', 'text', required=False) ]) return (bar, foo)
def setUp(self): self.bar_spec = GroupSpec( 'A test group specification with a data type', data_type_def='Bar', datasets=[ DatasetSpec('an example dataset', 'int', name='data', attributes=[ AttributeSpec('attr2', 'an example integer attribute', 'int') ]) ], attributes=[ AttributeSpec('attr1', 'an example string attribute', 'str') ]) self.spec_catalog = SpecCatalog() self.spec_catalog.register_spec(self.bar_spec, 'test.yaml') self.namespace = SpecNamespace('a test namespace', CORE_NAMESPACE, [{ 'source': 'test.yaml' }], catalog=self.spec_catalog) self.namespace_catalog = NamespaceCatalog() self.namespace_catalog.add_namespace(CORE_NAMESPACE, self.namespace) self.type_map = TypeMap(self.namespace_catalog) self.type_map.register_container_type(CORE_NAMESPACE, 'Bar', Bar) self.type_map.register_map(Bar, ObjectMapper) self.manager = BuildManager(self.type_map) self.mapper = ObjectMapper(self.bar_spec)
def getSpecs(self): ret = GroupSpec('A test group specification with a data type', data_type_def='Bar', datasets=[DatasetSpec('an example dataset', 'int', name='data', attributes=[AttributeSpec( 'attr2', 'an example integer attribute', 'int')])], attributes=[AttributeSpec('attr1', 'an example string attribute', 'text')]) return (ret,)
def setUpBarSpec(self): self.bar_spec = GroupSpec( 'A test group specification with a data type', data_type_def='Bar', groups=[GroupSpec('an example group', data_type_def='Foo')], attributes=[ AttributeSpec('attr1', 'an example string attribute', 'str'), AttributeSpec('attr2', 'an example integer attribute', 'int') ])
def setUpBarSpec(self): self.bar_spec = GroupSpec( 'A test group specification with a data type', data_type_def='Bar', datasets=[DatasetSpec('an example dataset', 'int', name='data')], attributes=[ AttributeSpec('attr1', 'an example string attribute', 'text'), AttributeSpec('attr2', 'an example integer attribute', 'int') ])
def test_update_attribute_spec(self): spec = GroupSpec('A test group', name='root_constructor', attributes=[ AttributeSpec('attribute1', 'my first attribute', 'text'), ]) spec.set_attribute( AttributeSpec('attribute1', 'my first attribute', 'int', value=5)) res = spec.get_attribute('attribute1') self.assertEqual(res.value, 5) self.assertEqual(res.dtype, 'int')
def setUp(self): self.catalog = SpecCatalog() self.attributes = [ AttributeSpec('attribute1', 'my first attribute', 'text'), AttributeSpec('attribute2', 'my second attribute', 'text') ] self.spec = DatasetSpec('my first dataset', 'int', name='dataset1', dims=(None, None), attributes=self.attributes, linkable=False, data_type_def='EphysData')
def test_datatype_extension(self): base = DatasetSpec('my first dataset', 'int', name='dataset1', dimension=(None, None), attributes=self.attributes, linkable=False, namespace='core', data_type_def='EphysData') attributes = [ AttributeSpec('attribute3', 'my first extending attribute', 'float') ] ext = DatasetSpec('my first dataset extension', 'int', name='dataset1', dimension=(None, None), attributes=attributes, linkable=False, namespace='core', data_type_inc=base, data_type_def='SpikeData') self.assertDictEqual(ext['attributes'][0], attributes[0]) self.assertDictEqual(ext['attributes'][1], self.attributes[0]) self.assertDictEqual(ext['attributes'][2], self.attributes[1]) ext_attrs = ext.attributes self.assertIs(ext, ext_attrs[0].parent) self.assertIs(ext, ext_attrs[1].parent) self.assertIs(ext, ext_attrs[2].parent)
def test_invalid_dtype(self): with self.assertRaises(ValueError): AttributeSpec( name='attribute1', doc='my first attribute', dtype='invalid' # <-- Invalid dtype must raise a ValueError )
def test_default_value(self): spec = AttributeSpec('attribute1', 'my first attribute', 'text', default_value='some text') self.assertEqual(spec['default_value'], 'some text') self.assertEqual(spec.default_value, 'some text')
def test_constructor(self): spec = AttributeSpec('attribute1', 'my first attribute', 'str') self.assertEqual(spec['name'], 'attribute1') self.assertEqual(spec['dtype'], 'str') self.assertEqual(spec['doc'], 'my first attribute') self.assertIsNone(spec.parent) json.dumps(spec) # to ensure there are no circular links
def test_dynamic_container_creation_defaults(self): baz_spec = GroupSpec( 'A test extension with no Container class', data_type_def='Baz', data_type_inc=self.bar_spec, attributes=[ AttributeSpec('attr3', 'an example float attribute', 'float'), AttributeSpec('attr4', 'another example float attribute', 'float') ]) self.spec_catalog.register_spec(baz_spec, 'extension.yaml') cls = self.type_map.get_container_cls(CORE_NAMESPACE, 'Baz') expected_args = {'name', 'data', 'attr1', 'attr2', 'attr3', 'attr4'} received_args = set(map(lambda x: x['name'], get_docval(cls.__init__))) self.assertSetEqual(expected_args, received_args) self.assertEqual(cls.__name__, 'Baz') self.assertTrue(issubclass(cls, Bar))
def test_shape(self): shape = [None, 2] spec = AttributeSpec('attribute1', 'my first attribute', 'text', shape=shape) self.assertEqual(spec['shape'], shape) self.assertEqual(spec.shape, shape)
def test_both_value_and_default_value_set(self): with self.assertRaises(ValueError): AttributeSpec( name='attribute1', doc='my first attribute', dtype='int', value=5, default_value= 10 # <-- Default_value and value can't be set at the same time )
def setUpBazSpec(self): self.baz_spec = DatasetSpec( 'an Baz type', 'int', name='MyBaz', data_type_def='Baz', attributes=[ AttributeSpec('baz_attr', 'an example string attribute', 'text') ])
def test_colliding_shape_and_dims(self): with self.assertRaises(ValueError): AttributeSpec( name='attribute1', doc='my first attribute', dtype='int', dims=['test'], shape=[ None, 2 ] # <-- Length of shape and dims do not match must raise a ValueError )
def test_dynamic_container_constructor(self): baz_spec = GroupSpec( 'A test extension with no Container class', data_type_def='Baz', data_type_inc=self.bar_spec, attributes=[ AttributeSpec('attr3', 'an example float attribute', 'float'), AttributeSpec('attr4', 'another example float attribute', 'float') ]) self.spec_catalog.register_spec(baz_spec, 'extension.yaml') cls = self.type_map.get_container_cls(CORE_NAMESPACE, 'Baz') # TODO: test that constructor works! inst = cls('My Baz', [1, 2, 3, 4], 'string attribute', 1000, attr3=98.6, attr4=1.0) self.assertEqual(inst.name, 'My Baz') self.assertEqual(inst.data, [1, 2, 3, 4]) self.assertEqual(inst.attr1, 'string attribute') self.assertEqual(inst.attr2, 1000) self.assertEqual(inst.attr3, 98.6) self.assertEqual(inst.attr4, 1.0)
def setUp(self): self.attributes = [ AttributeSpec('attribute1', 'my first attribute', 'text'), AttributeSpec('attribute2', 'my second attribute', 'text') ] self.dset1_attributes = [ AttributeSpec('attribute3', 'my third attribute', 'text'), AttributeSpec('attribute4', 'my fourth attribute', 'text') ] self.dset2_attributes = [ AttributeSpec('attribute5', 'my fifth attribute', 'text'), AttributeSpec('attribute6', 'my sixth attribute', 'text') ] self.datasets = [ DatasetSpec('my first dataset', 'int', name='dataset1', attributes=self.dset1_attributes, linkable=True), DatasetSpec('my second dataset', 'int', name='dataset2', dimension=(None, None), attributes=self.dset2_attributes, linkable=True, namespace='core', data_type_def='VoltageArray') ] self.subgroups = [ GroupSpec('A test subgroup', name='subgroup1', linkable=False), GroupSpec('A test subgroup', name='subgroup2', linkable=False) ] self.ndt_attr_spec = AttributeSpec('data_type', 'the data type of this object', 'text', value='EphysData') self.ns_attr_spec = AttributeSpec( 'namespace', 'the namespace for the data type of this object', 'text', required=False)
def setUp(self): self.attributes = [ AttributeSpec('attribute1', 'my first attribute', 'str'), AttributeSpec('attribute2', 'my second attribute', 'str') ]
def test_dims_without_shape(self): spec = AttributeSpec('attribute1', 'my first attribute', 'text', dims=['test']) self.assertEqual(spec.shape, (None, ))
def setUp(self): self.attributes = [ AttributeSpec('attribute1', 'my first attribute', 'text'), AttributeSpec('attribute2', 'my second attribute', 'text') ] self.dset1_attributes = [ AttributeSpec('attribute3', 'my third attribute', 'text'), AttributeSpec('attribute4', 'my fourth attribute', 'text') ] self.dset2_attributes = [ AttributeSpec('attribute5', 'my fifth attribute', 'text'), AttributeSpec('attribute6', 'my sixth attribute', 'text') ] self.datasets = [ DatasetSpec( 'my first dataset', # noqa: F405 'int', name='dataset1', attributes=self.dset1_attributes, linkable=True), DatasetSpec( 'my second dataset', # noqa: F405 'int', name='dataset2', dimension=(None, None), attributes=self.dset2_attributes, linkable=True, data_type_def='VoltageArray') ] self.spec = GroupSpec( 'A test group', # noqa: F405 name='root_constructor_nwbtype', datasets=self.datasets, attributes=self.attributes, linkable=False, data_type_def='EphysData') dset1_attributes_ext = [ AttributeSpec('dset1_extra_attribute', 'an extra attribute for the first dataset', 'text') ] self.ext_datasets = [ DatasetSpec( 'my first dataset extension', # noqa: F405 'int', name='dataset1', attributes=dset1_attributes_ext, linkable=True), ] self.ext_attributes = [ AttributeSpec('ext_extra_attribute', 'an extra attribute for the group', 'text'), ] self.ext_spec = GroupSpec( 'A test group extension', # noqa: F405 name='root_constructor_nwbtype', datasets=self.ext_datasets, attributes=self.ext_attributes, linkable=False, data_type_inc='EphysData', data_type_def='SpikeData') to_dump = {'groups': [self.spec, self.ext_spec]} self.specs_path = 'test_load_namespace.specs.yaml' self.namespace_path = 'test_load_namespace.namespace.yaml' with open(self.specs_path, 'w') as tmp: yaml.safe_dump(json.loads(json.dumps(to_dump)), tmp, default_flow_style=False) ns_dict = { 'doc': 'a test namespace', 'name': self.NS_NAME, 'schema': [{ 'source': self.specs_path }] } self.namespace = SpecNamespace.build_namespace(**ns_dict) # noqa: F405 to_dump = {'namespaces': [self.namespace]} with open(self.namespace_path, 'w') as tmp: yaml.safe_dump(json.loads(json.dumps(to_dump)), tmp, default_flow_style=False) self.ns_catalog = NamespaceCatalog() # noqa: F405
def test_type_extension(self): spec = GroupSpec('A test group', name='parent_type', datasets=self.datasets, attributes=self.attributes, linkable=False, data_type_def='EphysData') dset1_attributes_ext = [ AttributeSpec('dset1_extra_attribute', 'an extra attribute for the first dataset', 'text') ] ext_datasets = [ DatasetSpec('my first dataset extension', 'int', name='dataset1', attributes=dset1_attributes_ext, linkable=True), ] ext_attributes = [ AttributeSpec('ext_extra_attribute', 'an extra attribute for the group', 'text'), ] ext = GroupSpec('A test group extension', name='child_type', datasets=ext_datasets, attributes=ext_attributes, linkable=False, data_type_inc=spec, data_type_def='SpikeData') ext_dset1 = ext.get_dataset('dataset1') ext_dset1_attrs = ext_dset1.attributes self.assertDictEqual(ext_dset1_attrs[0], dset1_attributes_ext[0]) self.assertDictEqual(ext_dset1_attrs[1], self.dset1_attributes[0]) self.assertDictEqual(ext_dset1_attrs[2], self.dset1_attributes[1]) self.assertEqual(ext.data_type_def, 'SpikeData') self.assertEqual(ext.data_type_inc, 'EphysData') ext_dset2 = ext.get_dataset('dataset2') self.maxDiff = None # this will suffice for now, assertDictEqual doesn't do deep equality checks self.assertEqual(str(ext_dset2), str(self.datasets[1])) self.assertAttributesEqual(ext_dset2, self.datasets[1]) # self.ns_attr_spec ndt_attr_spec = AttributeSpec( 'data_type', 'the data type of this object', # noqa: F841 'text', value='SpikeData') res_attrs = ext.attributes self.assertDictEqual(res_attrs[0], ext_attributes[0]) self.assertDictEqual(res_attrs[1], self.attributes[0]) self.assertDictEqual(res_attrs[2], self.attributes[1]) # test that inherited specs are tracked appropriate for d in self.datasets: with self.subTest(dataset=d.name): self.assertTrue(ext.is_inherited_spec(d)) self.assertFalse(spec.is_inherited_spec(d)) json.dumps(spec)