Skip to content

Commit

Permalink
WIP: ENH: OME-Zarr format version conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
thewtex committed Dec 4, 2024
1 parent 8a85586 commit 3966ac1
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 43 deletions.
22 changes: 20 additions & 2 deletions ngff_zarr/to_ngff_zarr.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import zarr
import zarr.storage
from ._zarr_open_array import open_array
from .v04.zarr_metadata import Metadata as Metadata_v04
from .v05.zarr_metadata import Metadata as Metadata_v05

# Zarr Python 3
if hasattr(zarr.storage, "StoreLike"):
Expand Down Expand Up @@ -182,7 +184,23 @@ def to_ngff_zarr(
if version != "0.4" and version != "0.5":
raise ValueError(f"Unsupported version: {version}")

metadata_dict = asdict(multiscales.metadata)
metadata = multiscales.metadata
if version == "0.4" and isinstance(metadata, Metadata_v05):
metadata = Metadata_v04(
axes=metadata.axes,
datasets=metadata.datasets,
coordinateTransformations=metadata.coordinateTransformations,
name=metadata.name,
)
if version == "0.5" and isinstance(metadata, Metadata_v04):
metadata = Metadata_v05(
axes=metadata.axes,
datasets=metadata.datasets,
coordinateTransformations=metadata.coordinateTransformations,
name=metadata.name,
)

metadata_dict = asdict(metadata)
metadata_dict = _pop_metadata_optionals(metadata_dict)
metadata_dict["@type"] = "ngff:Image"
zarr_format = 2 if version == "0.4" else 3
Expand Down Expand Up @@ -224,7 +242,7 @@ def to_ngff_zarr(
progress.update_multiscales_task_completed(index + 1)
image = next_image
arr = image.data
path = multiscales.metadata.datasets[index].path
path = metadata.datasets[index].path
parent = str(PurePosixPath(path).parent)
if parent not in (".", "/"):
array_dims_group = root.create_group(parent)
Expand Down
82 changes: 41 additions & 41 deletions pixi.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions test/test_convert_ome_zarr_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import tempfile
from packaging import version
from pathlib import Path

import pytest

import zarr.storage
import zarr

from ngff_zarr import to_ngff_zarr, from_ngff_zarr


zarr_version = version.parse(zarr.__version__)

# Skip tests if zarr version is less than 3.0.0b1
pytestmark = pytest.mark.skipif(
zarr_version < version.parse("3.0.0b1"), reason="zarr version < 3.0.0b1"
)


def test_convert_0_1_to_0_4():
test_store = Path(__file__).parent / "data" / "input" / "v01" / "6001251.zarr"
multiscales = from_ngff_zarr(test_store, validate=True, version="0.1")
version = "0.4"
with tempfile.TemporaryDirectory() as tmpdir:
to_ngff_zarr(tmpdir, multiscales, version=version, use_tensorstore=True)
from_ngff_zarr(tmpdir, validate=True, version=version)


# def test_convert_0_1_to_0_5():
# test_store = Path(__file__).parent / "data" / "input" / "v01" / "6001251.zarr"
# multiscales = from_ngff_zarr(test_store, validate=True, version="0.1")
# store = zarr.storage.MemoryStore()
# version = "0.5"
# to_ngff_zarr(store, multiscales, version=version)
# from_ngff_zarr(store, validate=True, version=version)

0 comments on commit 3966ac1

Please sign in to comment.