Skip to content

Commit 56ebfc3

Browse files
authored
Bug/register container (#175)
* register hdmf-common base types * check for shape before computing * pass on shape * add test for registered types * add tests
1 parent a115dc6 commit 56ebfc3

File tree

5 files changed

+34
-0
lines changed

5 files changed

+34
-0
lines changed

src/hdmf/backends/hdf5/h5_utils.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ def regionref(self):
3939
def ref(self):
4040
return self.dataset.ref
4141

42+
@property
43+
def shape(self):
44+
return self.dataset.shape
45+
4246

4347
class H5TableDataset(H5Dataset):
4448

src/hdmf/common/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@ def available_namespaces():
104104
from . import table # noqa: F401,E402
105105
from . import sparse # noqa: F401,E402
106106

107+
from .. import Data, Container
108+
__TYPE_MAP.register_container_type(CORE_NAMESPACE, 'Container', Container)
109+
__TYPE_MAP.register_container_type(CORE_NAMESPACE, 'Data', Data)
110+
107111
else:
108112
raise RuntimeError("Unable to load a TypeMap - no namespace file found")
109113

src/hdmf/data_utils.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ def get_shape(data):
3131
"""
3232
if isinstance(data, dict):
3333
return None
34+
elif hasattr(data, 'shape'):
35+
return data.shape
3436
elif hasattr(data, '__len__') and not isinstance(data, (text_type, binary_type)):
3537
return __get_shape_helper(data)
3638
else:

tests/unit/common/test_common.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import unittest2 as unittest
2+
3+
from hdmf import Data, Container
4+
from hdmf.common import get_type_map
5+
6+
7+
class TestCommonTypeMap(unittest.TestCase):
8+
9+
def test_base_types(self):
10+
tm = get_type_map()
11+
cls = tm.get_container_cls('hdmf-common', 'Container')
12+
self.assertIs(cls, Container)
13+
cls = tm.get_container_cls('hdmf-common', 'Data')
14+
self.assertIs(cls, Data)

tests/unit/test_io_hdf5.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
from hdmf.backends.hdf5 import HDF5IO
77
from hdmf.build import GroupBuilder, DatasetBuilder, LinkBuilder
8+
from hdmf.data_utils import get_shape
89

910
from numbers import Number
1011

@@ -231,3 +232,12 @@ def test_overwrite_written(self):
231232
with self.assertRaisesRegex(ValueError, "cannot change written to not written"):
232233
builder.written = False
233234
io.close()
235+
236+
def test_dataset_shape(self):
237+
self.maxDiff = None
238+
io = HDF5IO(self.path, manager=self.manager, mode='a')
239+
io.write_builder(self.builder)
240+
builder = io.read_builder()
241+
dset = builder['test_bucket']['foo_holder']['foo1']['my_data'].data
242+
self.assertEqual(get_shape(dset), (10,))
243+
io.close()

0 commit comments

Comments
 (0)