diff --git a/CHANGELOG.md b/CHANGELOG.md index 414386156..64dec0086 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ - 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) - Added Dynamic Enumerations and Schemasheets support to `TermSet`. @mavaylon1 [#923](https://github.com/hdmf-dev/hdmf/pull/923) - Updated `HERD` to support user defined file name for the `HERD` zip file. @mavaylon1 [#941](https://github.com/hdmf-dev/hdmf/pull/941) +- 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) ## HDMF 3.8.1 (July 25, 2023) diff --git a/src/hdmf/container.py b/src/hdmf/container.py index 84533220a..3eeb7987b 100644 --- a/src/hdmf/container.py +++ b/src/hdmf/container.py @@ -708,6 +708,12 @@ def __smart_str_dict(d, num_indent): out += '\n' + indent + right_br return out + def set_data_io(self, dataset_name, data_io_class, **kwargs): + data = self.fields.get(dataset_name) + if data is None: + raise ValueError(f"{dataset_name} is None and cannot be wrapped in a DataIO class") + self.fields[dataset_name] = data_io_class(data=data, **kwargs) + class Data(AbstractContainer): """ diff --git a/tests/unit/test_container.py b/tests/unit/test_container.py index 5c71688ff..805cead2a 100644 --- a/tests/unit/test_container.py +++ b/tests/unit/test_container.py @@ -2,6 +2,7 @@ from uuid import uuid4, UUID import os +from hdmf.backends.hdf5 import H5DataIO from hdmf.container import AbstractContainer, Container, Data, HERDManager from hdmf.common.resources import HERD from hdmf.testing import TestCase @@ -394,6 +395,29 @@ def test_get_ancestors(self): self.assertTupleEqual(parent_obj.get_ancestors(), (grandparent_obj, )) self.assertTupleEqual(child_obj.get_ancestors(), (parent_obj, grandparent_obj)) + def test_set_data_io(self): + + class ContainerWithData(Container): + __fields__ = ('data1', 'data2') + + @docval( + {"name": "name", "doc": "name", "type": str}, + {'name': 'data1', 'doc': 'field1 doc', 'type': list}, + {'name': 'data2', 'doc': 'field2 doc', 'type': list, 'default': None} + ) + def __init__(self, **kwargs): + super().__init__(name=kwargs["name"]) + self.data1 = kwargs["data1"] + self.data2 = kwargs["data2"] + + obj = ContainerWithData("name", [1, 2, 3, 4, 5], None) + obj.set_data_io("data1", H5DataIO, chunks=True) + assert isinstance(obj.data1, H5DataIO) + + with self.assertRaises(ValueError): + obj.set_data_io("data2", H5DataIO, chunks=True) + + class TestHTMLRepr(TestCase):