Skip to content

Commit

Permalink
set_data_io method and associated test (#934)
Browse files Browse the repository at this point in the history
* draft of set_data_io method and associated test

* improve test coverage

* Update CHANGELOG.md
  • Loading branch information
bendichter authored Aug 15, 2023
1 parent 92915c2 commit d346736
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
6 changes: 6 additions & 0 deletions src/hdmf/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down
24 changes: 24 additions & 0 deletions tests/unit/test_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):

Expand Down

0 comments on commit d346736

Please sign in to comment.