Skip to content

Commit

Permalink
more zarr testing to data
Browse files Browse the repository at this point in the history
  • Loading branch information
h-mayorquin committed Sep 14, 2024
1 parent ad1e2a1 commit f2e9031
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 41 deletions.
11 changes: 4 additions & 7 deletions src/neuroconv/basedatainterface.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,8 @@ def run_conversion(
nwbfile: Optional[NWBFile] = None,
metadata: Optional[dict] = None,
overwrite: bool = False,
# TODO: when all H5DataIO prewraps are gone, introduce Zarr safely
# backend: Union[Literal["hdf5", "zarr"]],
# backend_configuration: Optional[Union[HDF5BackendConfiguration, ZarrBackendConfiguration]] = None,
backend: Optional[Literal["hdf5"]] = None,
backend_configuration: Optional[HDF5BackendConfiguration] = None,
backend: Optional[Literal["hdf5", "zarr"]] = None,
backend_configuration: Optional[Union[HDF5BackendConfiguration, ZarrBackendConfiguration]] = None,
**conversion_options,
):
"""
Expand All @@ -148,11 +145,11 @@ def run_conversion(
overwrite : bool, default: False
Whether to overwrite the NWBFile if one exists at the nwbfile_path.
The default is False (append mode).
backend : "hdf5", optional
backend : {"hdf5", "zarr"}, optional
The type of backend to use when writing the file.
If a `backend_configuration` is not specified, the default type will be "hdf5".
If a `backend_configuration` is specified, then the type will be auto-detected.
backend_configuration : HDF5BackendConfiguration, optional
backend_configuration : HDF5BackendConfiguration or ZarrBackendConfiguration, optional
The configuration model to use when configuring the datasets for this backend.
To customize, call the `.get_default_backend_configuration(...)` method, modify the returned
BackendConfiguration object, and pass that instead.
Expand Down
62 changes: 28 additions & 34 deletions src/neuroconv/tools/testing/data_interface_mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,32 +106,6 @@ def test_no_metadata_mutation(self, setup_interface):

self.interface.add_to_nwbfile(nwbfile=nwbfile, metadata=metadata, **self.conversion_options)

assert metadata == metadata_before_add_method

def check_run_conversion_with_backend(self, nwbfile_path: str, backend: Literal["hdf5", "zarr"] = "hdf5"):
metadata = self.interface.get_metadata()
if "session_start_time" not in metadata["NWBFile"]:
metadata["NWBFile"].update(session_start_time=datetime.now().astimezone())

self.interface.run_conversion(
nwbfile_path=nwbfile_path,
overwrite=True,
metadata=metadata,
backend=backend,
**self.conversion_options,
)

def check_configure_backend_for_equivalent_nwbfiles(self, backend: Literal["hdf5", "zarr"] = "hdf5"):
metadata = self.interface.get_metadata()
if "session_start_time" not in metadata["NWBFile"]:
metadata["NWBFile"].update(session_start_time=datetime.now().astimezone())

nwbfile_1 = self.interface.create_nwbfile(metadata=metadata, **self.conversion_options)
nwbfile_2 = self.interface.create_nwbfile(metadata=metadata, **self.conversion_options)

backend_configuration = get_default_backend_configuration(nwbfile=nwbfile_1, backend=backend)
configure_backend(nwbfile=nwbfile_2, backend_configuration=backend_configuration)

def check_run_conversion_with_backend_configuration(
self, nwbfile_path: str, backend: Literal["hdf5", "zarr"] = "hdf5"
):
Expand Down Expand Up @@ -204,11 +178,6 @@ def check_read_nwb(self, nwbfile_path: str):
"""Read the produced NWB file and compare it to the interface."""
pass

def check_basic_zarr_read(self, nwbfile_path: str):
"""Ensure NWBZarrIO can read the file."""
with NWBZarrIO(path=nwbfile_path, mode="r") as io:
io.read()

def check_extracted_metadata(self, metadata: dict):
"""Override this method to make assertions about specific extracted metadata values."""
pass
Expand All @@ -235,7 +204,20 @@ def test_run_conversion_with_backend(self, setup_interface, tmp_path, backend):
)

if backend == "zarr":
self.check_basic_zarr_read(nwbfile_path)
with NWBZarrIO(path=nwbfile_path, mode="r") as io:
io.read()

@pytest.mark.parametrize("backend", ["hdf5", "zarr"])
def test_configure_backend_for_equivalent_nwbfiles(self, setup_interface, tmp_path, backend):
metadata = self.interface.get_metadata()
if "session_start_time" not in metadata["NWBFile"]:
metadata["NWBFile"].update(session_start_time=datetime.now().astimezone())

nwbfile_1 = self.interface.create_nwbfile(metadata=metadata, **self.conversion_options)
nwbfile_2 = self.interface.create_nwbfile(metadata=metadata, **self.conversion_options)

backend_configuration = get_default_backend_configuration(nwbfile=nwbfile_1, backend=backend)
configure_backend(nwbfile=nwbfile_2, backend_configuration=backend_configuration)

def test_all_conversion_checks(self, setup_interface, tmp_path):
interface, test_name = setup_interface
Expand All @@ -247,7 +229,6 @@ def test_all_conversion_checks(self, setup_interface, tmp_path):
# Now run the checks using the setup objects
self.check_conversion_options_schema_valid()
self.check_metadata()
self.check_configure_backend_for_equivalent_nwbfiles()

self.check_run_conversion_in_nwbconverter_with_backend(nwbfile_path=nwbfile_path, backend="hdf5")
self.check_run_conversion_in_nwbconverter_with_backend_configuration(nwbfile_path=nwbfile_path, backend="hdf5")
Expand Down Expand Up @@ -746,7 +727,6 @@ def test_all_conversion_checks(self, setup_interface, tmp_path):
# Now run the checks using the setup objects
self.check_conversion_options_schema_valid()
self.check_metadata()
self.check_configure_backend_for_equivalent_nwbfiles()

self.check_run_conversion_in_nwbconverter_with_backend(nwbfile_path=nwbfile_path, backend="hdf5")
self.check_run_conversion_in_nwbconverter_with_backend_configuration(nwbfile_path=nwbfile_path, backend="hdf5")
Expand Down Expand Up @@ -1000,6 +980,17 @@ class TestNWBConverter(NWBConverter):
conversion_options=conversion_options,
)

def check_configure_backend_for_equivalent_nwbfiles(self, backend: Literal["hdf5", "zarr"] = "hdf5"):
metadata = self.interface.get_metadata()
if "session_start_time" not in metadata["NWBFile"]:
metadata["NWBFile"].update(session_start_time=datetime.now().astimezone())

nwbfile_1 = self.interface.create_nwbfile(metadata=metadata, **self.conversion_options)
nwbfile_2 = self.interface.create_nwbfile(metadata=metadata, **self.conversion_options)

backend_configuration = get_default_backend_configuration(nwbfile=nwbfile_1, backend=backend)
configure_backend(nwbfile=nwbfile_2, backend_configuration=backend_configuration)

def test_all_conversion_checks(self, metadata: dict):
interface_kwargs = self.interface_kwargs
if isinstance(interface_kwargs, dict):
Expand Down Expand Up @@ -1263,6 +1254,9 @@ def test_run_conversion_with_backend(self):
def test_no_metadata_mutation(self):
pass

def test_configure_backend_for_equivalent_nwbfiles(self):
pass

def check_metadata_schema_valid(self):
schema = self.interface.get_metadata_schema()
Draft7Validator.check_schema(schema=schema)
Expand Down
13 changes: 13 additions & 0 deletions tests/test_on_data/ecephys/test_recording_interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,19 @@ class TestEDFRecordingInterface(RecordingExtractorInterfaceTestMixin):
def check_extracted_metadata(self, metadata: dict):
assert metadata["NWBFile"]["session_start_time"] == datetime(2022, 3, 2, 10, 42, 19)

def check_run_conversion_with_backend(self, nwbfile_path: str, backend="hdf5"):
metadata = self.interface.get_metadata()
if "session_start_time" not in metadata["NWBFile"]:
metadata["NWBFile"].update(session_start_time=datetime.now().astimezone())

self.interface.run_conversion(
nwbfile_path=nwbfile_path,
overwrite=True,
metadata=metadata,
backend=backend,
**self.conversion_options,
)

def test_all_conversion_checks(self, setup_interface, tmp_path):
# Create a unique test name and file path
nwbfile_path = str(tmp_path / f"{self.__class__.__name__}.nwb")
Expand Down

0 comments on commit f2e9031

Please sign in to comment.