Skip to content

Commit

Permalink
Add NWB file convert unit test (#161)
Browse files Browse the repository at this point in the history
* Add NWB file convert unit test
* Fix #160. Resolve bytes dtype on export
  • Loading branch information
oruebel authored Feb 13, 2024
1 parent 300070b commit f180b44
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

### Bug Fixes
* Fixed bug when converting HDF5 datasets with unlimited dimensions @oruebel [#155](https://github.com/hdmf-dev/hdmf-zarr/pull/155)
* Fixed bug resolving bytes dtype when exporting from Zarr to Zarr @oruebel [#161](https://github.com/hdmf-dev/hdmf-zarr/pull/161)
* Adjust gallery tests to not fail on deprecation warnings from pandas. @rly [#157](https://github.com/hdmf-dev/hdmf-zarr/pull/157)

## 0.5.0 (December 8, 2023)
Expand Down
9 changes: 6 additions & 3 deletions src/hdmf_zarr/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -1137,6 +1137,7 @@ def write_dataset(self, **kwargs): # noqa: C901
"utf8": str,
"utf-8": str,
"ascii": bytes,
"bytes": bytes,
"str": str,
"isodatetime": str,
"string_": bytes,
Expand Down Expand Up @@ -1193,7 +1194,9 @@ def __resolve_dtype_helper__(cls, dtype):
@classmethod
def get_type(cls, data):
if isinstance(data, str):
return str
return cls.__dtypes.get("str")
elif isinstance(data, bytes):
return cls.__dtypes.get("bytes")
elif not hasattr(data, '__len__'):
return type(data)
else:
Expand Down Expand Up @@ -1230,8 +1233,8 @@ def __list_fill__(self, parent, name, data, options=None): # noqa: C901
# which Zarr does not allow for dataset shape. Check for the shape attribute first before falling
# back on get_data_shape
if hasattr(data, 'shape') and data.shape is not None:
data_shape = data.shape
# This is a fall-back just in case. However this should not happen for standard numpy and h5py arrays
data_shape = data.shape
# This is a fall-back just in case. However this should not happen for standard numpy and h5py arrays
else: # pragma: no cover
data_shape = get_data_shape(data) # pragma: no cover
# Deal with object dtype
Expand Down
81 changes: 80 additions & 1 deletion tests/unit/test_io_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
import shutil
import numpy as np
import numcodecs
from datetime import datetime
from dateutil import tz
from abc import ABCMeta, abstractmethod

from hdmf_zarr.backend import (ZarrIO,
Expand All @@ -49,14 +51,19 @@
from hdmf.common import DynamicTable
from hdmf.common import CSRMatrix


from tests.unit.utils import (Foo, FooBucket, FooFile, get_foo_buildmanager,
Baz, BazData, BazBucket, get_baz_buildmanager,
BazCpdData, get_temp_filepath)

from zarr.storage import (DirectoryStore,
TempStore,
NestedDirectoryStore)
try:
import pynwb
PYNWB_AVAILABLE = True
except ImportError:
PYNWB_AVAILABLE = False



class MixinTestCaseConvert(metaclass=ABCMeta):
Expand Down Expand Up @@ -403,6 +410,40 @@ def setUpContainer(self):
else:
raise NotImplementedError("FOO_TYPE %i not implemented in test" % self.FOO_TYPE)

############################################
# HDMF Common test container mixins
###########################################
class MixinTestNWBFile():
"""
Mixin class used in conjunction with MixinTestCaseConvert to create conversion tests that
test export of a basic NWBFile. This class only defines the setUpContainer function for the test.
The roundtripExportContainer function required for the test needs to be defined separately
(e.g., by another mixin or the test class itself)
"""
TABLE_TYPE = 0

def get_manager(self):
return pynwb.get_manager()

def setUpContainer(self):
if not PYNWB_AVAILABLE:
self.skipTest('Skip test. PyNWB is not installed')

subject = pynwb.file.Subject(
subject_id="001",
species="Mus musculus",
sex="M",
date_of_birth=datetime(2018, 4, 25, 2, 30, 3, tzinfo=tz.gettz("US/Pacific")),
age="P1D",
description=None,
)
nwbfile = pynwb.file.NWBFile(
session_description="Test File",
identifier="0000",
session_start_time=datetime(2019, 4, 25, 2, 30, 3, tzinfo=tz.gettz("US/Pacific")),
subject=subject,
)
return nwbfile

########################################
# HDMF Baz test dataset of references
Expand Down Expand Up @@ -639,6 +680,44 @@ class TestHDF5toZarrFooCase2(MixinTestFoo,
FOO_TYPE = MixinTestFoo.FOO_TYPES['link_data']


class TestZarrToZarrNWBFile(MixinTestNWBFile,
MixinTestZarrToZarr,
MixinTestCaseConvert,
TestCase):
"""
Test the conversion of DynamicTable containers from Zarr to HDF5.
See MixinTestDynamicTableContainer.setUpContainer for the container spec.
"""
IGNORE_NAME = True
IGNORE_HDMF_ATTRS = True
IGNORE_STRING_TO_BYTE = False


class TestHDF5ToZarrNWBFile(MixinTestNWBFile,
MixinTestHDF5ToZarr,
MixinTestCaseConvert,
TestCase):
"""
Test the conversion of DynamicTable containers from Zarr to HDF5.
See MixinTestDynamicTableContainer.setUpContainer for the container spec.
"""
IGNORE_NAME = True
IGNORE_HDMF_ATTRS = True
IGNORE_STRING_TO_BYTE = False

class TestZarrToHDF5NWBFile(MixinTestNWBFile,
MixinTestZarrToHDF5,
MixinTestCaseConvert,
TestCase):
"""
Test the conversion of DynamicTable containers from Zarr to HDF5.
See MixinTestDynamicTableContainer.setUpContainer for the container spec.
"""
IGNORE_NAME = True
IGNORE_HDMF_ATTRS = True
IGNORE_STRING_TO_BYTE = False


########################################
# Test cases for dataset of references
########################################
Expand Down

0 comments on commit f180b44

Please sign in to comment.