Skip to content

Commit 3966ac1

Browse files
committed
WIP: ENH: OME-Zarr format version conversion
1 parent 8a85586 commit 3966ac1

File tree

3 files changed

+97
-43
lines changed

3 files changed

+97
-43
lines changed

ngff_zarr/to_ngff_zarr.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
import zarr
1818
import zarr.storage
1919
from ._zarr_open_array import open_array
20+
from .v04.zarr_metadata import Metadata as Metadata_v04
21+
from .v05.zarr_metadata import Metadata as Metadata_v05
2022

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

185-
metadata_dict = asdict(multiscales.metadata)
187+
metadata = multiscales.metadata
188+
if version == "0.4" and isinstance(metadata, Metadata_v05):
189+
metadata = Metadata_v04(
190+
axes=metadata.axes,
191+
datasets=metadata.datasets,
192+
coordinateTransformations=metadata.coordinateTransformations,
193+
name=metadata.name,
194+
)
195+
if version == "0.5" and isinstance(metadata, Metadata_v04):
196+
metadata = Metadata_v05(
197+
axes=metadata.axes,
198+
datasets=metadata.datasets,
199+
coordinateTransformations=metadata.coordinateTransformations,
200+
name=metadata.name,
201+
)
202+
203+
metadata_dict = asdict(metadata)
186204
metadata_dict = _pop_metadata_optionals(metadata_dict)
187205
metadata_dict["@type"] = "ngff:Image"
188206
zarr_format = 2 if version == "0.4" else 3
@@ -224,7 +242,7 @@ def to_ngff_zarr(
224242
progress.update_multiscales_task_completed(index + 1)
225243
image = next_image
226244
arr = image.data
227-
path = multiscales.metadata.datasets[index].path
245+
path = metadata.datasets[index].path
228246
parent = str(PurePosixPath(path).parent)
229247
if parent not in (".", "/"):
230248
array_dims_group = root.create_group(parent)

pixi.lock

Lines changed: 41 additions & 41 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import tempfile
2+
from packaging import version
3+
from pathlib import Path
4+
5+
import pytest
6+
7+
import zarr.storage
8+
import zarr
9+
10+
from ngff_zarr import to_ngff_zarr, from_ngff_zarr
11+
12+
13+
zarr_version = version.parse(zarr.__version__)
14+
15+
# Skip tests if zarr version is less than 3.0.0b1
16+
pytestmark = pytest.mark.skipif(
17+
zarr_version < version.parse("3.0.0b1"), reason="zarr version < 3.0.0b1"
18+
)
19+
20+
21+
def test_convert_0_1_to_0_4():
22+
test_store = Path(__file__).parent / "data" / "input" / "v01" / "6001251.zarr"
23+
multiscales = from_ngff_zarr(test_store, validate=True, version="0.1")
24+
version = "0.4"
25+
with tempfile.TemporaryDirectory() as tmpdir:
26+
to_ngff_zarr(tmpdir, multiscales, version=version, use_tensorstore=True)
27+
from_ngff_zarr(tmpdir, validate=True, version=version)
28+
29+
30+
# def test_convert_0_1_to_0_5():
31+
# test_store = Path(__file__).parent / "data" / "input" / "v01" / "6001251.zarr"
32+
# multiscales = from_ngff_zarr(test_store, validate=True, version="0.1")
33+
# store = zarr.storage.MemoryStore()
34+
# version = "0.5"
35+
# to_ngff_zarr(store, multiscales, version=version)
36+
# from_ngff_zarr(store, validate=True, version=version)

0 commit comments

Comments
 (0)