Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: Write out array dimension_names for OME-Zarr 0.5 #124

Merged
merged 1 commit into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion ngff_zarr/to_ngff_zarr.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def _numpy_to_zarr_dtype(dtype):


def _write_with_tensorstore(
store_path: str, array, region, chunks, zarr_format
store_path: str, array, region, chunks, zarr_format, dimension_names=None
) -> None:
"""Write array using tensorstore backend"""
import tensorstore as ts
Expand All @@ -131,6 +131,8 @@ def _write_with_tensorstore(
"configuration": {"chunk_shape": chunks},
}
spec["metadata"]["data_type"] = _numpy_to_zarr_dtype(array.dtype)
if dimension_names:
spec["metadata"]["dimension_names"] = dimension_names
else:
raise ValueError(f"Unsupported zarr format: {zarr_format}")
dataset = ts.open(spec, create=True, dtype=array.dtype).result()
Expand Down Expand Up @@ -199,6 +201,10 @@ def to_ngff_zarr(
coordinateTransformations=metadata.coordinateTransformations,
name=metadata.name,
)
dimension_names = tuple([ax.name for ax in metadata.axes])
dimension_names_kwargs = (
{"dimension_names": dimension_names} if version != "0.4" else {}
)

metadata_dict = asdict(metadata)
metadata_dict = _pop_metadata_optionals(metadata_dict)
Expand Down Expand Up @@ -274,6 +280,7 @@ def to_ngff_zarr(
path=path,
mode="a",
**zarr_kwargs,
**dimension_names_kwargs,
)

shape = image.data.shape
Expand Down Expand Up @@ -407,6 +414,7 @@ def to_ngff_zarr(
region,
[c[0] for c in arr_region.chunks],
zarr_format=zarr_format,
dimension_names=dimension_names,
**kwargs,
)
else:
Expand All @@ -419,6 +427,7 @@ def to_ngff_zarr(
compute=True,
return_stored=False,
**zarr_kwargs,
**dimension_names_kwargs,
**kwargs,
)
else:
Expand All @@ -435,6 +444,7 @@ def to_ngff_zarr(
region,
[c[0] for c in arr.chunks],
zarr_format=zarr_format,
dimension_names=dimension_names,
**kwargs,
)
else:
Expand All @@ -447,6 +457,7 @@ def to_ngff_zarr(
compute=True,
return_stored=False,
**zarr_kwargs,
**dimension_names_kwargs,
**kwargs,
)

Expand Down
12 changes: 12 additions & 0 deletions test/test_to_ngff_zarr_rfc2_zarr_v3.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ def test_gaussian_isotropic_scale_factors(input_images):
# store_new_multiscales(dataset_name, baseline_name, multiscales, version=version)
verify_against_baseline(dataset_name, baseline_name, multiscales, version=version)

array0 = zarr.open_array(store=store, path="scale0/image", mode="r", zarr_format=3)
dimension_names = array0.metadata.dimension_names
for idx, ax in enumerate(multiscales.metadata.axes):
assert ax.name == dimension_names[idx]


def test_gaussian_isotropic_scale_factors_tensorstore(input_images):
pytest.importorskip("tensorstore")
Expand All @@ -48,3 +53,10 @@ def test_gaussian_isotropic_scale_factors_tensorstore(input_images):
verify_against_baseline(
dataset_name, baseline_name, multiscales, version=version
)

array0 = zarr.open_array(
store=tmpdir, path="scale0/image", mode="r", zarr_format=3
)
dimension_names = array0.metadata.dimension_names
for idx, ax in enumerate(multiscales.metadata.axes):
assert ax.name == dimension_names[idx]
Loading