From 42026c5d7b30b7d176f75af7570b5e54c8ebadcd Mon Sep 17 00:00:00 2001 From: Oliver Ruebel Date: Mon, 18 Nov 2019 12:05:37 -0800 Subject: [PATCH] Fix #201 Add shape property for container.Data (#202) --- src/hdmf/container.py | 11 ++++++++++- tests/unit/test_container.py | 15 +++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/hdmf/container.py b/src/hdmf/container.py index 2831aa802..2932f1e67 100644 --- a/src/hdmf/container.py +++ b/src/hdmf/container.py @@ -3,7 +3,7 @@ from uuid import uuid4 from six import with_metaclass from .utils import docval, get_docval, call_docval_func, getargs, ExtenderMeta -from .data_utils import DataIO +from .data_utils import DataIO, get_shape from warnings import warn import h5py @@ -390,6 +390,15 @@ def __init__(self, **kwargs): def data(self): return self.__data + @property + def shape(self): + """ + Get the shape of the data represented by this container + :return: Shape tuple + :rtype: tuple of ints + """ + return get_shape(self.__data) + @docval({'name': 'dataio', 'type': DataIO, 'doc': 'the DataIO to apply to the data held by this Data'}) def set_dataio(self, **kwargs): """ diff --git a/tests/unit/test_container.py b/tests/unit/test_container.py index 2d16dea58..842517efd 100644 --- a/tests/unit/test_container.py +++ b/tests/unit/test_container.py @@ -1,6 +1,7 @@ import unittest from hdmf.container import AbstractContainer, Container, Data +import numpy as np class Subcontainer(Container): @@ -133,6 +134,20 @@ def test_bool_false(self): data_obj = Data('my_data', []) self.assertFalse(data_obj) + def test_shape_nparray(self): + """ + Test that shape works for np.array + """ + data_obj = Data('my_data', np.arange(10).reshape(2, 5)) + self.assertTupleEqual(data_obj.shape, (2, 5)) + + def test_shape_list(self): + """ + Test that shape works for np.array + """ + data_obj = Data('my_data', [[0, 1, 2, 3, 4], [0, 1, 2, 3, 4]]) + self.assertTupleEqual(data_obj.shape, (2, 5)) + if __name__ == '__main__': unittest.main()