Skip to content

Commit

Permalink
Fix #201 Add shape property for container.Data (#202)
Browse files Browse the repository at this point in the history
  • Loading branch information
oruebel authored Nov 18, 2019
1 parent 8ea0416 commit 42026c5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/hdmf/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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):
"""
Expand Down
15 changes: 15 additions & 0 deletions tests/unit/test_container.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import unittest

from hdmf.container import AbstractContainer, Container, Data
import numpy as np


class Subcontainer(Container):
Expand Down Expand Up @@ -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()

0 comments on commit 42026c5

Please sign in to comment.