From d81741dd721505cbf83afb030f554908edf08c33 Mon Sep 17 00:00:00 2001 From: bendichter Date: Mon, 7 Aug 2023 16:12:33 -0700 Subject: [PATCH 1/3] draft of set_data_io method and associated test --- src/hdmf/container.py | 6 ++++++ tests/unit/test_container.py | 19 +++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/src/hdmf/container.py b/src/hdmf/container.py index ee27938e2..fe130d368 100644 --- a/src/hdmf/container.py +++ b/src/hdmf/container.py @@ -695,6 +695,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[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 0dcb3619c..b373f7ba7 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, ExternalResourcesManager from hdmf.common.resources import ExternalResources from hdmf.testing import TestCase @@ -382,6 +383,24 @@ def test_reset_parent_no_parent(self): obj.reset_parent() self.assertIsNone(obj.parent) + def test_set_data_io(self): + + class ContainerWithData(Container): + __fields__ = ('data1',) + + @docval( + {"name": "name", "doc": "name", "type": str}, + {'name': 'data1', 'doc': 'field1 doc', 'type': list} + ) + def __init__(self, **kwargs): + super().__init__(name=kwargs["name"]) + self.data1 = kwargs["data1"] + + obj = ContainerWithData("name", [1, 2, 3, 4, 5]) + obj.set_data_io("data1", H5DataIO, chunks=True) + assert isinstance(obj.data1, H5DataIO) + + class TestHTMLRepr(TestCase): From 8de6f973c8c09f60a99300ca7c52cde3bedd870a Mon Sep 17 00:00:00 2001 From: bendichter Date: Wed, 9 Aug 2023 15:44:26 -0700 Subject: [PATCH 2/3] improve test coverage --- src/hdmf/container.py | 2 +- tests/unit/test_container.py | 11 ++++++++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/hdmf/container.py b/src/hdmf/container.py index fe130d368..0203a898c 100644 --- a/src/hdmf/container.py +++ b/src/hdmf/container.py @@ -696,7 +696,7 @@ def __smart_str_dict(d, num_indent): return out def set_data_io(self, dataset_name, data_io_class, **kwargs): - data = self.fields[dataset_name] + 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) diff --git a/tests/unit/test_container.py b/tests/unit/test_container.py index b373f7ba7..b845aec44 100644 --- a/tests/unit/test_container.py +++ b/tests/unit/test_container.py @@ -386,20 +386,25 @@ def test_reset_parent_no_parent(self): def test_set_data_io(self): class ContainerWithData(Container): - __fields__ = ('data1',) + __fields__ = ('data1', 'data2') @docval( {"name": "name", "doc": "name", "type": str}, - {'name': 'data1', 'doc': 'field1 doc', 'type': list} + {'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]) + 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): From dc2d8f5a757da9d79ff91ea0a8fcf05142f1ae0f Mon Sep 17 00:00:00 2001 From: Ben Dichter Date: Tue, 15 Aug 2023 12:48:44 -0400 Subject: [PATCH 3/3] Update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) 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)