Skip to content

Commit d346736

Browse files
authored
set_data_io method and associated test (#934)
* draft of set_data_io method and associated test * improve test coverage * Update CHANGELOG.md
1 parent 92915c2 commit d346736

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
- Added the magic `__reduce__` method as well as two private semi-abstract helper methods to enable pickling of the `GenericDataChunkIterator`. @codycbakerphd [#924](https://github.com/hdmf-dev/hdmf/pull/924)
1010
- Added Dynamic Enumerations and Schemasheets support to `TermSet`. @mavaylon1 [#923](https://github.com/hdmf-dev/hdmf/pull/923)
1111
- Updated `HERD` to support user defined file name for the `HERD` zip file. @mavaylon1 [#941](https://github.com/hdmf-dev/hdmf/pull/941)
12+
- Added method `Containter.set_data_io`, which wraps an existing data field in a `DataIO`. @bendichter [#938](https://github.com/hdmf-dev/hdmf/pull/938)
1213

1314
## HDMF 3.8.1 (July 25, 2023)
1415

src/hdmf/container.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -708,6 +708,12 @@ def __smart_str_dict(d, num_indent):
708708
out += '\n' + indent + right_br
709709
return out
710710

711+
def set_data_io(self, dataset_name, data_io_class, **kwargs):
712+
data = self.fields.get(dataset_name)
713+
if data is None:
714+
raise ValueError(f"{dataset_name} is None and cannot be wrapped in a DataIO class")
715+
self.fields[dataset_name] = data_io_class(data=data, **kwargs)
716+
711717

712718
class Data(AbstractContainer):
713719
"""

tests/unit/test_container.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from uuid import uuid4, UUID
33
import os
44

5+
from hdmf.backends.hdf5 import H5DataIO
56
from hdmf.container import AbstractContainer, Container, Data, HERDManager
67
from hdmf.common.resources import HERD
78
from hdmf.testing import TestCase
@@ -394,6 +395,29 @@ def test_get_ancestors(self):
394395
self.assertTupleEqual(parent_obj.get_ancestors(), (grandparent_obj, ))
395396
self.assertTupleEqual(child_obj.get_ancestors(), (parent_obj, grandparent_obj))
396397

398+
def test_set_data_io(self):
399+
400+
class ContainerWithData(Container):
401+
__fields__ = ('data1', 'data2')
402+
403+
@docval(
404+
{"name": "name", "doc": "name", "type": str},
405+
{'name': 'data1', 'doc': 'field1 doc', 'type': list},
406+
{'name': 'data2', 'doc': 'field2 doc', 'type': list, 'default': None}
407+
)
408+
def __init__(self, **kwargs):
409+
super().__init__(name=kwargs["name"])
410+
self.data1 = kwargs["data1"]
411+
self.data2 = kwargs["data2"]
412+
413+
obj = ContainerWithData("name", [1, 2, 3, 4, 5], None)
414+
obj.set_data_io("data1", H5DataIO, chunks=True)
415+
assert isinstance(obj.data1, H5DataIO)
416+
417+
with self.assertRaises(ValueError):
418+
obj.set_data_io("data2", H5DataIO, chunks=True)
419+
420+
397421

398422
class TestHTMLRepr(TestCase):
399423

0 commit comments

Comments
 (0)