Skip to content

Commit 1a067e2

Browse files
committed
ENH: OME-Zarr 0.4, 0.5 format version conversion
1 parent 7e9c1f6 commit 1a067e2

File tree

3 files changed

+101
-43
lines changed

3 files changed

+101
-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: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from packaging import version
2+
from pathlib import Path
3+
4+
import pytest
5+
6+
import zarr.storage
7+
import zarr
8+
9+
from ngff_zarr import to_ngff_zarr, from_ngff_zarr
10+
11+
12+
zarr_version = version.parse(zarr.__version__)
13+
14+
# Skip tests if zarr version is less than 3.0.0b1
15+
pytestmark = pytest.mark.skipif(
16+
zarr_version < version.parse("3.0.0b1"), reason="zarr version < 3.0.0b1"
17+
)
18+
19+
20+
def test_convert_0_4_to_0_5():
21+
test_store = Path(__file__).parent / "data" / "input" / "v04" / "6001240.zarr"
22+
multiscales = from_ngff_zarr(test_store, validate=True, version="0.4")
23+
store = zarr.storage.MemoryStore()
24+
version = "0.5"
25+
to_ngff_zarr(store, multiscales, version=version)
26+
from_ngff_zarr(store, validate=True, version=version)
27+
28+
29+
def test_convert_0_5_to_0_4():
30+
test_store = Path(__file__).parent / "data" / "input" / "v04" / "6001240.zarr"
31+
multiscales = from_ngff_zarr(test_store, validate=True, version="0.4")
32+
store = zarr.storage.MemoryStore()
33+
version = "0.5"
34+
to_ngff_zarr(store, multiscales, version=version)
35+
multiscales = from_ngff_zarr(store, validate=True, version=version)
36+
37+
version = "0.4"
38+
new_store = zarr.storage.MemoryStore()
39+
to_ngff_zarr(new_store, multiscales, version=version)
40+
from_ngff_zarr(new_store, validate=True, version=version)

0 commit comments

Comments
 (0)