Skip to content

Commit

Permalink
add tests for multidim str attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
stephprince committed Aug 19, 2024
1 parent b636c3f commit 4f038d0
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 15 deletions.
7 changes: 4 additions & 3 deletions tests/unit/build_tests/test_classgenerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,11 @@ def test_dynamic_container_creation(self):
baz_spec = GroupSpec('A test extension with no Container class',
data_type_def='Baz', data_type_inc=self.bar_spec,
attributes=[AttributeSpec('attr3', 'a float attribute', 'float'),
AttributeSpec('attr4', 'another float attribute', 'float')])
AttributeSpec('attr4', 'another float attribute', 'float'),
AttributeSpec('attr_array', 'an array attribute', 'text', shape=(None,)),])
self.spec_catalog.register_spec(baz_spec, 'extension.yaml')
cls = self.type_map.get_dt_container_cls('Baz', CORE_NAMESPACE)
expected_args = {'name', 'data', 'attr1', 'attr2', 'attr3', 'attr4', 'skip_post_init'}
expected_args = {'name', 'data', 'attr1', 'attr2', 'attr3', 'attr4', 'attr_array', 'skip_post_init'}
received_args = set()

for x in get_docval(cls.__init__):
Expand Down Expand Up @@ -211,7 +212,7 @@ def test_dynamic_container_creation_defaults(self):
AttributeSpec('attr4', 'another float attribute', 'float')])
self.spec_catalog.register_spec(baz_spec, 'extension.yaml')
cls = self.type_map.get_dt_container_cls('Baz', CORE_NAMESPACE)
expected_args = {'name', 'data', 'attr1', 'attr2', 'attr3', 'attr4', 'foo', 'skip_post_init'}
expected_args = {'name', 'data', 'attr1', 'attr2', 'attr3', 'attr4', 'attr_array', 'foo', 'skip_post_init'}
received_args = set(map(lambda x: x['name'], get_docval(cls.__init__)))
self.assertSetEqual(expected_args, received_args)
self.assertEqual(cls.__name__, 'Baz')
Expand Down
84 changes: 72 additions & 12 deletions tests/unit/build_tests/test_io_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,26 @@ class Bar(Container):
{'name': 'attr1', 'type': str, 'doc': 'an attribute'},
{'name': 'attr2', 'type': int, 'doc': 'another attribute'},
{'name': 'attr3', 'type': float, 'doc': 'a third attribute', 'default': 3.14},
{'name': 'attr_array', 'type': 'array_data', 'doc': 'another attribute', 'default': (1, 2, 3)},
{'name': 'foo', 'type': 'Foo', 'doc': 'a group', 'default': None})
def __init__(self, **kwargs):
name, data, attr1, attr2, attr3, foo = getargs('name', 'data', 'attr1', 'attr2', 'attr3', 'foo', kwargs)
name, data, attr1, attr2, attr3, attr_array, foo = getargs('name', 'data', 'attr1', 'attr2', 'attr3', 'attr_array', 'foo', kwargs)
super().__init__(name=name)
self.__data = data
self.__attr1 = attr1
self.__attr2 = attr2
self.__attr3 = attr3
self.__attr_array = attr_array
self.__foo = foo
if self.__foo is not None and self.__foo.parent is None:
self.__foo.parent = self

def __eq__(self, other):
attrs = ('name', 'data', 'attr1', 'attr2', 'attr3', 'foo')
attrs = ('name', 'data', 'attr1', 'attr2', 'attr3', 'attr_array', 'foo')
return all(getattr(self, a) == getattr(other, a) for a in attrs)

def __str__(self):
attrs = ('name', 'data', 'attr1', 'attr2', 'attr3', 'foo')
attrs = ('name', 'data', 'attr1', 'attr2', 'attr3', 'attr_array', 'foo')
return ','.join('%s=%s' % (a, getattr(self, a)) for a in attrs)

@property
Expand All @@ -61,6 +63,10 @@ def attr2(self):
def attr3(self):
return self.__attr3

@property
def attr_array(self):
return self.__attr_array

@property
def foo(self):
return self.__foo
Expand Down Expand Up @@ -334,12 +340,15 @@ def test_build_1d(self):
datasets=[DatasetSpec('an example dataset', 'text', name='data', shape=(None,),
attributes=[AttributeSpec(
'attr2', 'an example integer attribute', 'int')])],
attributes=[AttributeSpec('attr1', 'an example string attribute', 'text')])
attributes=[AttributeSpec('attr1', 'an example string attribute', 'text'),
AttributeSpec('attr_array', 'an example array attribute', 'text',
shape=(None,))])
type_map = self.customSetUp(bar_spec)
type_map.register_map(Bar, BarMapper)
bar_inst = Bar('my_bar', ['a', 'b', 'c', 'd'], 'value1', 10)
bar_inst = Bar('my_bar', ['a', 'b', 'c', 'd'], 'value1', 10, attr_array=['a', 'b', 'c', 'd'])
builder = type_map.build(bar_inst)
self.assertEqual(builder.get('data').data, ['a', 'b', 'c', 'd'])
np.testing.assert_array_equal(builder.get('data').data, np.array(['a', 'b', 'c', 'd']))
np.testing.assert_array_equal(builder.get('attr_array'), np.array(['a', 'b', 'c', 'd']))

def test_build_scalar(self):
bar_spec = GroupSpec('A test group specification with a data type',
Expand Down Expand Up @@ -367,13 +376,16 @@ def test_build_2d_lol(self):
attributes=[AttributeSpec(name='attr2', doc='an example integer attribute', dtype='int')],
)
],
attributes=[AttributeSpec(name='attr1', doc='an example string attribute', dtype='text')],
attributes=[AttributeSpec(name='attr_array', doc='an example array attribute', dtype='text',
shape=(None, None))],
)
type_map = self.customSetUp(bar_spec)
type_map.register_map(Bar, BarMapper)
bar_inst = Bar('my_bar', [['aa', 'bb'], ['cc', 'dd']], 'value1', 10)
str_lol_2d = [['aa', 'bb'], ['cc', 'dd']]
bar_inst = Bar('my_bar', str_lol_2d, 'value1', 10, attr_array=str_lol_2d)
builder = type_map.build(bar_inst)
self.assertEqual(builder.get('data').data, [['aa', 'bb'], ['cc', 'dd']])
self.assertEqual(builder.get('data').data, str_lol_2d)
self.assertEqual(builder.get('attr_array'), str_lol_2d)

def test_build_2d_ndarray(self):
bar_spec = GroupSpec(
Expand All @@ -388,13 +400,61 @@ def test_build_2d_ndarray(self):
attributes=[AttributeSpec(name='attr2', doc='an example integer attribute', dtype='int')],
)
],
attributes=[AttributeSpec(name='attr1', doc='an example string attribute', dtype='text')],
attributes=[AttributeSpec(name='attr_array', doc='an example array attribute', dtype='text', shape=(None, None))],
)
type_map = self.customSetUp(bar_spec)
type_map.register_map(Bar, BarMapper)
str_array_2d = np.array([['aa', 'bb'], ['cc', 'dd']])
bar_inst = Bar('my_bar', str_array_2d, 'value1', 10, attr_array=str_array_2d)
builder = type_map.build(bar_inst)
np.testing.assert_array_equal(builder.get('data').data, str_array_2d)
np.testing.assert_array_equal(builder.get('attr_array'), str_array_2d)

def test_build_3d_lol(self):
bar_spec = GroupSpec(
doc='A test group specification with a data type',
data_type_def='Bar',
datasets=[
DatasetSpec(
doc='an example dataset',
dtype='text',
name='data',
shape=(None, None, None),
attributes=[AttributeSpec(name='attr2', doc='an example integer attribute', dtype='int')],
)
],
attributes=[AttributeSpec(name='attr_array', doc='an example array attribute', dtype='text', shape=(None, None, None))],
)
type_map = self.customSetUp(bar_spec)
type_map.register_map(Bar, BarMapper)
str_lol_3d = [[['aa', 'bb'], ['cc', 'dd']], [['ee', 'ff'], ['gg', 'hh']]]
bar_inst = Bar('my_bar', str_lol_3d, 'value1', 10, attr_array=str_lol_3d)
builder = type_map.build(bar_inst)
self.assertEqual(builder.get('data').data, str_lol_3d)
self.assertEqual(builder.get('attr_array'), str_lol_3d)

def test_build_3d_ndarray(self):
bar_spec = GroupSpec(
doc='A test group specification with a data type',
data_type_def='Bar',
datasets=[
DatasetSpec(
doc='an example dataset',
dtype='text',
name='data',
shape=(None, None, None),
attributes=[AttributeSpec(name='attr2', doc='an example integer attribute', dtype='int')],
)
],
attributes=[AttributeSpec(name='attr_array', doc='an example array attribute', dtype='text', shape=(None, None, None))],
)
type_map = self.customSetUp(bar_spec)
type_map.register_map(Bar, BarMapper)
bar_inst = Bar('my_bar', np.array([['aa', 'bb'], ['cc', 'dd']]), 'value1', 10)
str_array_3d = np.array([[['aa', 'bb'], ['cc', 'dd']], [['ee', 'ff'], ['gg', 'hh']]])
bar_inst = Bar('my_bar', str_array_3d, 'value1', 10, attr_array=str_array_3d)
builder = type_map.build(bar_inst)
np.testing.assert_array_equal(builder.get('data').data, np.array([['aa', 'bb'], ['cc', 'dd']]))
np.testing.assert_array_equal(builder.get('data').data, str_array_3d)
np.testing.assert_array_equal(builder.get('attr_array'), str_array_3d)

def test_build_dataio(self):
bar_spec = GroupSpec('A test group specification with a data type',
Expand Down

0 comments on commit 4f038d0

Please sign in to comment.