Skip to content

Commit

Permalink
Fix handling of read h5py string datasets
Browse files Browse the repository at this point in the history
  • Loading branch information
rly committed Sep 5, 2024
1 parent 044f5a5 commit a1db9f8
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/hdmf/build/objectmapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,10 @@ def __get_data_type(cls, spec):
def __convert_string(self, value, spec):
"""Convert string types to the specified dtype."""
def __apply_string_type(value, string_type):
if isinstance(value, (list, tuple, np.ndarray, DataIO)):
# NOTE: if a user passes a h5py.Dataset that is not wrapped with a hdmf.utils.StrDataset,
# then this conversion may not be correct. Users should unpack their string h5py.Datasets
# into a numpy array (or wrap them in StrDataset) before passing them to a container object.
if hasattr(value, '__iter__') and not isinstance(value, (str, bytes)):
return [__apply_string_type(item, string_type) for item in value]
else:
return string_type(value)
Expand Down
65 changes: 64 additions & 1 deletion tests/unit/build_tests/test_io_map.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from hdmf.utils import docval, getargs
from hdmf.utils import StrDataset, docval, getargs
from hdmf import Container, Data
from hdmf.backends.hdf5 import H5DataIO
from hdmf.build import (GroupBuilder, DatasetBuilder, ObjectMapper, BuildManager, TypeMap, LinkBuilder,
Expand All @@ -7,6 +7,7 @@
from hdmf.spec import (GroupSpec, AttributeSpec, DatasetSpec, SpecCatalog, SpecNamespace, NamespaceCatalog, RefSpec,
LinkSpec)
from hdmf.testing import TestCase
import h5py
from abc import ABCMeta, abstractmethod
import unittest
import numpy as np
Expand Down Expand Up @@ -460,6 +461,68 @@ def test_build_3d_ndarray(self):
np.testing.assert_array_equal(builder.get('data').data, str_array_3d)
np.testing.assert_array_equal(builder.get('attr_array'), str_array_3d)

def test_build_1d_h5py_dataset(self):
bar_spec = GroupSpec(
doc='A test group specification with a data type',
data_type_def='Bar',
datasets=[
DatasetSpec(
doc='an example dataset',
dtype='text',
name='data',
shape=(None, ),
attributes=[AttributeSpec(name='attr2', doc='an example integer attribute', dtype='int')],
)
],
attributes=[AttributeSpec(name='attr_array', doc='an example array attribute', dtype='text',
shape=(None, ))],
)
type_map = self.customSetUp(bar_spec)
type_map.register_map(Bar, BarMapper)
# create in-memory hdf5 file that is discarded after closing
with h5py.File("test.h5", "w", driver="core", backing_store=False) as f:
str_array_1d = np.array(
['aa', 'bb', 'cc', 'dd'],
dtype=h5py.special_dtype(vlen=str)
)
# wrap the dataset in a StrDataset to mimic how HDF5IO would read this dataset
dataset = StrDataset(f.create_dataset('data', data=str_array_1d), None)
bar_inst = Bar('my_bar', dataset, 'value1', 10, attr_array=dataset)
builder = type_map.build(bar_inst)
np.testing.assert_array_equal(builder.get('data').data, dataset[:])
np.testing.assert_array_equal(builder.get('attr_array'), dataset[:])

def test_build_3d_h5py_dataset(self):
bar_spec = GroupSpec(
doc='A test group specification with a data type',
data_type_def='Bar',
datasets=[
DatasetSpec(
doc='an example dataset',
dtype='text',
name='data',
shape=(None, None, None),
attributes=[AttributeSpec(name='attr2', doc='an example integer attribute', dtype='int')],
)
],
attributes=[AttributeSpec(name='attr_array', doc='an example array attribute', dtype='text',
shape=(None, None, None))],
)
type_map = self.customSetUp(bar_spec)
type_map.register_map(Bar, BarMapper)
# create in-memory hdf5 file that is discarded after closing
with h5py.File("test.h5", "w", driver="core", backing_store=False) as f:
str_array_3d = np.array(
[[['aa', 'bb'], ['cc', 'dd']], [['ee', 'ff'], ['gg', 'hh']]],
dtype=h5py.special_dtype(vlen=str)
)
# wrap the dataset in a StrDataset to mimic how HDF5IO would read this dataset
dataset = StrDataset(f.create_dataset('data', data=str_array_3d), None)
bar_inst = Bar('my_bar', dataset, 'value1', 10, attr_array=dataset)
builder = type_map.build(bar_inst)
np.testing.assert_array_equal(builder.get('data').data, dataset[:])
np.testing.assert_array_equal(builder.get('attr_array'), dataset[:])

def test_build_dataio(self):
bar_spec = GroupSpec('A test group specification with a data type',
data_type_def='Bar',
Expand Down

0 comments on commit a1db9f8

Please sign in to comment.