Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

set_data_io method and associated test #934

Merged
merged 5 commits into from
Aug 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading