Exemplo n.º 1
0
 def test_invalid_refspec_dict(self):
     with self.assertRaises(AssertionError):
         DtypeSpec.assertValidDtype({
             'no target':
             'test',  # <-- missing or here bad target key for RefSpec
             'reftype': 'object'
         })
Exemplo n.º 2
0
 def test_simplify_cpd_type(self):
     compound_type = [
         DtypeSpec('test', 'test field', 'float'),
         DtypeSpec('test2', 'test field2', 'int')
     ]
     expected_result = ['float', 'int']
     result = DtypeHelper.simplify_cpd_type(compound_type)
     self.assertListEqual(result, expected_result)
Exemplo n.º 3
0
 def test_invalid_refspec_dict(self):
     """Test missing or bad target key for RefSpec."""
     msg = "'dtype' must have the key 'target_type'"
     with self.assertRaisesWith(ValueError, msg):
         DtypeSpec.assertValidDtype({
             'no target': 'test',
             'reftype': 'object'
         })
Exemplo n.º 4
0
 def test_compound_type(self):
     """Test that convert_dtype passes through arguments if spec dtype is a list without any validation."""
     spec_type = [DtypeSpec('an int field', 'f1', 'int'), DtypeSpec('a float field', 'f2', 'float')]
     spec = DatasetSpec('an example dataset', spec_type, name='data')
     value = ['a', 1, 2.2]
     res, ret_dtype = ObjectMapper.convert_dtype(spec, value)
     self.assertListEqual(res, value)
     self.assertListEqual(ret_dtype, spec_type)
Exemplo n.º 5
0
 def test_simplify_cpd_type_ref(self):
     compound_type = [
         DtypeSpec('test', 'test field', 'float'),
         DtypeSpec('test2', 'test field2',
                   RefSpec(target_type='MyType', reftype='object'))
     ]
     expected_result = ['float', 'object']
     result = DtypeHelper.simplify_cpd_type(compound_type)
     self.assertListEqual(result, expected_result)
Exemplo n.º 6
0
 def setUpBazSpec(self):
     self.baz_spec = DatasetSpec(
         doc='a BazScalarCompound type',
         dtype=[
             DtypeSpec(name='id',
                       dtype='uint64',
                       doc='The unique identifier in this table.'),
             DtypeSpec(name='attr1', dtype='text', doc='A text attribute.'),
         ],
         name='MyBaz',
         data_type_def='BazScalarCompound',
     )
Exemplo n.º 7
0
 def test_datatype_table_extension(self):
     dtype1 = DtypeSpec('column1', 'the first column', 'int')
     dtype2 = DtypeSpec('column2', 'the second column', 'float')
     base = DatasetSpec('my first table', [dtype1, dtype2],
                        attributes=self.attributes,
                        data_type_def='SimpleTable')
     self.assertEqual(base['dtype'], [dtype1, dtype2])
     self.assertEqual(base['doc'], 'my first table')
     dtype3 = DtypeSpec('column3', 'the third column', 'text')
     ext = DatasetSpec('my first table extension', [dtype3],
                       data_type_inc=base,
                       data_type_def='ExtendedTable')
     self.assertEqual(ext['dtype'], [dtype1, dtype2, dtype3])
     self.assertEqual(ext['doc'], 'my first table extension')
Exemplo n.º 8
0
 def test_constructor_table(self):
     dtype1 = DtypeSpec('column1', 'the first column', 'int')
     dtype2 = DtypeSpec('column2', 'the second column', 'float')
     spec = DatasetSpec('my first table', [dtype1, dtype2],
                        name='table1',
                        attributes=self.attributes)
     self.assertEqual(spec['dtype'], [dtype1, dtype2])
     self.assertEqual(spec['name'], 'table1')
     self.assertEqual(spec['doc'], 'my first table')
     self.assertNotIn('linkable', spec)
     self.assertNotIn('data_type_def', spec)
     self.assertListEqual(spec['attributes'], self.attributes)
     self.assertIs(spec, self.attributes[0].parent)
     self.assertIs(spec, self.attributes[1].parent)
     json.dumps(spec)
Exemplo n.º 9
0
 def test_datatype_table_extension_diff_format(self):
     dtype1 = DtypeSpec('column1', 'the first column', 'int')
     dtype2 = DtypeSpec('column2', 'the second column', 'float64')
     base = DatasetSpec('my first table',
                        [dtype1, dtype2],
                        attributes=self.attributes,
                        data_type_def='SimpleTable')
     self.assertEqual(base['dtype'], [dtype1, dtype2])
     self.assertEqual(base['doc'], 'my first table')
     dtype3 = DtypeSpec('column2', 'the second column, with greater precision', 'int32')
     with self.assertRaisesWith(ValueError, 'Cannot extend float64 to int32'):
         DatasetSpec('my first table extension',
                     [dtype3],
                     data_type_inc=base,
                     data_type_def='ExtendedTable')
Exemplo n.º 10
0
 def test_constructor_invalid_table(self):
     with self.assertRaises(ValueError):
         DatasetSpec('my first table',
                     [DtypeSpec('column1', 'the first column', 'int'),
                      {}     # <--- Bad compound type spec must raise an error
                      ],
                     name='table1',
                     attributes=self.attributes)
Exemplo n.º 11
0
 def test_invalid_dtype(self):
     with self.assertRaises(AssertionError):
         DtypeSpec(
             'column1',
             'an example column',
             dtype=
             'bad dtype'  # <-- make sure a bad type string raises an error
         )
Exemplo n.º 12
0
 def setUpBazSpec(self):
     self.baz_spec = DatasetSpec(
         doc='a list of references to Foo objects',
         dtype=[
             DtypeSpec(name='id',
                       dtype='uint64',
                       doc='The unique identifier in this table.'),
             DtypeSpec(name='foo',
                       dtype=RefSpec('Foo', 'object'),
                       doc='The foo in this table.'),
         ],
         name='MyBaz',
         shape=[None],
         data_type_def='Baz',
         attributes=[
             AttributeSpec('baz_attr', 'an example string attribute',
                           'text')
         ])
Exemplo n.º 13
0
 def test_build_spec(self):
     spec = DtypeSpec.build_spec({
         'doc': 'an example column',
         'name': 'column1',
         'dtype': 'int'
     })
     self.assertEqual(spec.doc, 'an example column')
     self.assertEqual(spec.name, 'column1')
     self.assertEqual(spec.dtype, 'int')
Exemplo n.º 14
0
 def test_is_ref(self):
     spec = DtypeSpec('column1', 'an example column',
                      RefSpec('TimeSeries', 'object'))
     self.assertTrue(DtypeSpec.is_ref(spec))
     spec = DtypeSpec('column1', 'an example column', 'int')
     self.assertFalse(DtypeSpec.is_ref(spec))
Exemplo n.º 15
0
 def test_invalid_dtype(self):
     msg = "dtype 'bad dtype' is not a valid primary data type."
     with self.assertRaisesRegex(ValueError, msg):
         DtypeSpec('column1', 'an example column', dtype='bad dtype')
Exemplo n.º 16
0
 def test_refspec_dtype(self):
     # just making sure this does not cause an error
     DtypeSpec('column1', 'an example column',
               RefSpec('TimeSeries', 'object'))
Exemplo n.º 17
0
def main():
    # these arguments were auto-generated from your cookie-cutter inputs
    ns_builder = NamespaceBuilder(
        doc=
        """Objects for rigorously defining spatial coordinates, object shapes, and transformations""",
        name="""ndx-spatial-coordinates""",
        version="""0.1.0""",
        author=list(map(str.strip, """Ben Dichter""".split(','))),
        contact=list(
            map(str.strip, """*****@*****.**""".split(','))))

    # TODO: specify the neurodata_types that are used by the extension as well
    # as in which namespace they are found
    # this is similar to specifying the Python modules that need to be imported
    # to use your new data types
    # as of HDMF 1.6.1, the full ancestry of the neurodata_types that are used by
    # the extension should be included, i.e., the neurodata_type and its parent
    # type and its parent type and so on. this will be addressed in a future
    # release of HDMF.
    ns_builder.include_type('Container', namespace='hdmf-common')

    # TODO: define your new data types
    # see https://pynwb.readthedocs.io/en/latest/extensions.html#extending-nwb
    # for more information

    Space = GroupSpec(data_type_def='Space',
                      data_type_inc='Container',
                      doc='space',
                      attributes=[
                          AttributeSpec(name='R',
                                        doc='dimensionality of space',
                                        dtype='int')
                      ])

    Spaces = GroupSpec(data_type_def='Spaces',
                       data_type_inc='Container',
                       doc='contains spaces',
                       groups=[
                           GroupSpec(data_type_inc=Space,
                                     quantity='*',
                                     doc='spaces are stored here')
                       ])

    CoordinateSystem = GroupSpec(
        data_type_def='CoordinateSystem',
        data_type_inc='Container',
        doc='coordinate system',
        links=[
            LinkSpec(doc='link to space', target_type='Space', name='space')
        ],
        attributes=[
            AttributeSpec(name='R',
                          doc='dimensionality of coordinate system',
                          dtype='int')
        ])

    CoordinateSystems = GroupSpec(
        data_type_def='CoordinateSystems',
        data_type_inc='Container',
        doc='coordinate systems',
        groups=[
            GroupSpec(data_type_inc=CoordinateSystem,
                      quantity='*',
                      doc='coordinate systems are stored here')
        ],
    )

    RegistrationProtocol = GroupSpec(data_type_def='RegistrationProtocol',
                                     data_type_inc='Container',
                                     doc='registration protocol')

    RegistrationProtocols = GroupSpec(
        data_type_def='RegistrationProtocols',
        data_type_inc='Container',
        doc='registration protocols',
        groups=[
            GroupSpec(data_type_inc=RegistrationProtocol,
                      quantity='*',
                      doc='registration protocols are stored here')
        ],
    )

    CoordinateRegistrationProcess = GroupSpec(
        data_type_def='CoordinateRegistrationProcess',
        data_type_inc='Container',
        doc='coordinate registration process',
        links=[
            LinkSpec(doc='registration protocol',
                     target_type='RegistrationProtocol',
                     name='registration_protocol')
        ])

    CoordinateRegistrationProcesses = GroupSpec(
        data_type_def='CoordinateRegistrationProcesses',
        data_type_inc='Container',
        doc='registration process',
        groups=[
            GroupSpec(data_type_inc=CoordinateRegistrationProcess,
                      quantity='*',
                      doc='registration processes are stored here')
        ],
    )

    Transform = GroupSpec(
        data_type_def='Transform',
        data_type_inc='Container',
        doc='transform',
        links=[
            LinkSpec(doc='domain coordinate system',
                     target_type='CoordinateSystem',
                     name='domain_coordinate_system'),
            LinkSpec(doc='range coordinate system',
                     target_type='CoordinateSystem',
                     name='range_coordinate_system')
        ],
    )

    AffineTransform = GroupSpec(data_type_def='AffineTransform',
                                data_type_inc=Transform,
                                doc='affine transform',
                                datasets=[
                                    DatasetSpec(doc='affine transform matrix',
                                                dtype='float',
                                                shape=(None, None),
                                                name='M')
                                ])

    DeformableTransform = GroupSpec(
        data_type_def='DeformableTransform',
        data_type_inc=Transform,
        doc='deformable transform',
        datasets=[DatasetSpec(doc='vector image', dtype='float', name='I')])

    Transforms = GroupSpec(
        data_type_def='Transforms',
        data_type_inc='Container',
        doc='transforms',
        groups=[
            GroupSpec(data_type_inc=Transform,
                      quantity='*',
                      doc='transforms are stored here')
        ],
    )

    PhysicalObjectType = GroupSpec(data_type_def='PhysicalObjectType',
                                   data_type_inc='Container',
                                   doc='physical object type',
                                   groups=[
                                       GroupSpec(name='geometry',
                                                 doc='geometry',
                                                 datasets=[
                                                     DatasetSpec(name='nodes',
                                                                 doc='nodes',
                                                                 dtype='float',
                                                                 shape=(None,
                                                                        3)),
                                                     DatasetSpec(name='edges',
                                                                 doc='edges',
                                                                 dtype='uint',
                                                                 shape=(None,
                                                                        3))
                                                 ])
                                   ],
                                   links=[
                                       LinkSpec(name='coordinate_system',
                                                doc='coordinate system',
                                                target_type='CoordinateSystem')
                                   ])

    ElectrodeType = GroupSpec(data_type_def='ElectrodeType',
                              data_type_inc=PhysicalObjectType,
                              doc='electrode type',
                              attributes=[
                                  AttributeSpec(name='electrode_type',
                                                doc='electrode type',
                                                dtype='text')
                              ])

    ElectrodeTypes = GroupSpec(
        data_type_def='ElectrodeTypes',
        data_type_inc='Container',
        doc='electrode types',
        groups=[
            GroupSpec(data_type_inc=ElectrodeType,
                      quantity='*',
                      doc='electrode types are stored here')
        ],
    )

    PhysicalObjectWithSubObjectsType = GroupSpec(
        data_type_def='PhysicalObjectWithSubObjectsType',
        data_type_inc=PhysicalObjectType,
        doc='physical object with sub objects',
        groups=[
            GroupSpec(
                name='sites',
                doc='sites',
                datasets=[
                    DatasetSpec(
                        name='sites',
                        doc='sites',
                        dtype=[
                            DtypeSpec(
                                name='position',
                                doc='position',
                                dtype='float',
                                #shape=(3,)
                            ),
                            DtypeSpec(
                                name='normal',
                                doc='normal',
                                dtype='float',
                                #shape=(3,)
                            ),
                            DtypeSpec(name='object_type',
                                      doc='object type',
                                      dtype=RefSpec(
                                          target_type='PhysicalObjectType',
                                          reftype='object')),
                        ])
                ])
        ])

    ProbeType = GroupSpec(
        data_type_def='ProbeType',
        data_type_inc=PhysicalObjectWithSubObjectsType,
        doc='probe type',
    )

    ProbeTypes = GroupSpec(
        data_type_def='ProbeTypes',
        data_type_inc='Container',
        doc='probe types',
        groups=[
            GroupSpec(data_type_inc=ProbeType,
                      quantity='*',
                      doc='probe types are stored here')
        ],
    )

    SubjectBrain = GroupSpec(data_type_def='SubjectBrain',
                             data_type_inc=PhysicalObjectWithSubObjectsType,
                             doc='subject brain')

    SpaceInfo = GroupSpec(
        data_type_def='SpaceInfo',
        data_type_inc='Container',
        doc='all space info goes in here',
        groups=[
            GroupSpec(data_type_inc=Spaces, doc='spaces'),
            GroupSpec(data_type_inc=CoordinateSystems,
                      doc='coordinate systems'),
            GroupSpec(data_type_inc=RegistrationProtocols,
                      doc='registration protocols'),
            GroupSpec(data_type_inc=CoordinateRegistrationProcesses,
                      doc='registration processes'),
            GroupSpec(data_type_inc=Transforms, doc='transforms'),
            GroupSpec(data_type_inc=ElectrodeTypes, doc='electrode types'),
            GroupSpec(data_type_inc=ProbeTypes, doc='probe types'),
            GroupSpec(data_type_inc=SubjectBrain, doc='subject brain')
        ])

    new_data_types = [
        Space, Spaces, CoordinateSystem, CoordinateSystems,
        RegistrationProtocol, RegistrationProtocols,
        CoordinateRegistrationProcess, CoordinateRegistrationProcesses,
        Transform, AffineTransform, DeformableTransform, Transforms,
        PhysicalObjectType, ElectrodeType, ElectrodeTypes,
        PhysicalObjectWithSubObjectsType, ProbeType, ProbeTypes, SubjectBrain,
        SpaceInfo
    ]

    # export the spec to yaml files in the spec folder
    output_dir = os.path.abspath(
        os.path.join(os.path.dirname(__file__), '..', '..', 'spec'))
    export_spec(ns_builder, new_data_types, output_dir)
Exemplo n.º 18
0
 def test_constructor(self):
     spec = DtypeSpec('column1', 'an example column', 'int')
     self.assertEqual(spec.doc, 'an example column')
     self.assertEqual(spec.name, 'column1')
     self.assertEqual(spec.dtype, 'int')