Skip to content

Support reading OME-Zarr 0.5 #295

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

Merged
merged 43 commits into from
Jun 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
afae0b4
wip
aliddell Feb 27, 2025
63b3aa7
wip: 2025-02-27
aliddell Feb 27, 2025
dde33a0
Merge remote-tracking branch 'upstream/main' into 273-use-zarr-python-3
aliddell Mar 11, 2025
0c0837c
Undo reformatting
aliddell Mar 11, 2025
e4c87b4
Remove unused imports
aliddell Mar 11, 2025
abfd029
Fix miscellaneous failing tests.
aliddell Mar 11, 2025
61045d4
Get rename_well working.
aliddell Mar 11, 2025
9800d96
sort import
ziw-liu Apr 3, 2025
2516c9b
Read MMStack with Zarr v3 (#293)
ziw-liu Apr 4, 2025
1404000
Merge branch 'main' into 273-use-zarr-python-3
ziw-liu Apr 4, 2025
2a2f75f
add rich for zarr v3 print tree
ziw-liu Apr 4, 2025
26adb5e
skip tests that require ome-zarr-py
ziw-liu Apr 4, 2025
dd6d994
fix file list checking
ziw-liu Apr 4, 2025
b9c6ccd
fix commas
ziw-liu Apr 4, 2025
8a427c6
stringify path names
ziw-liu Apr 4, 2025
ce6e0a9
Merge branch 'main' into 273-use-zarr-python-3
ziw-liu Apr 4, 2025
f50efcb
detect ome key in reader
ziw-liu Apr 15, 2025
c48309f
allow multiscales to not have version
ziw-liu Apr 15, 2025
c7d4e1d
wip: allow reading v0.5 images
ziw-liu Apr 15, 2025
90ce666
normalize store path
ziw-liu Apr 15, 2025
8507fec
Merge branch 'zarr3-dev' into ome-zarr-0.5
ziw-liu May 19, 2025
6648e95
update docstring
ziw-liu May 29, 2025
7e94726
remove unused import
ziw-liu May 29, 2025
f7c86fe
remove zarr-python synchronizer
ziw-liu May 29, 2025
77146dd
allow empty zarr examples
ziw-liu May 29, 2025
d60ff07
add example json files
ziw-liu May 29, 2025
81e88f0
fixture for example v0.5 HCS dataset
ziw-liu May 29, 2025
27201c7
unify ome container unwrapping
ziw-liu May 29, 2025
8f811ab
run PR tests for all target branches
ziw-liu May 29, 2025
3aa13f6
test with absolute paths
ziw-liu May 29, 2025
31aa28d
avoid big endian test cases
ziw-liu May 29, 2025
3f03172
avoid yanked versions
ziw-liu May 29, 2025
63971f9
fix zarr-python breakage
ziw-liu May 29, 2025
a37a13f
revert keyword argument name
ziw-liu May 29, 2025
c19c80d
fix version detection after zarr-python breakage
ziw-liu May 30, 2025
c6bec61
test with absolute path
ziw-liu May 30, 2025
140b498
Merge branch 'zarr3-dev' into ome-zarr-0.5
ziw-liu May 30, 2025
c091eb9
Revert "allow empty zarr examples"
ziw-liu May 30, 2025
6160961
Test reading NGFF v0.5 with acquire-zarr
aliddell Jun 2, 2025
c20417f
Merge remote-tracking branch 'upstream/ome-zarr-0.5' into ome-zarr-0.5
aliddell Jun 2, 2025
4f37959
check binary values
ziw-liu Jun 2, 2025
d3ceea9
simplify file mode switch
ziw-liu Jun 2, 2025
2f1f00b
remove redundant deletion
ziw-liu Jun 2, 2025
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
2 changes: 1 addition & 1 deletion .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ name: lint, style, and tests
on:
pull_request:
branches:
- main
- "*"

jobs:
style:
Expand Down
6 changes: 4 additions & 2 deletions iohub/ngff/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

"""
Data model classes with validation for OME-NGFF metadata.
Developed against OME-NGFF v0.4 and ome-zarr v0.9
Developed against OME-NGFF v0.4/0.5.2 and ome-zarr v0.9.

Attributes are 'snake_case' with aliases to match NGFF names in JSON output.
See https://ngff.openmicroscopy.org/0.4/index.html#naming-style
Expand Down Expand Up @@ -219,7 +219,9 @@ class VersionMeta(MetaBase):
"""OME-NGFF spec version. Default is the current version (0.4)."""

# SHOULD
version: Literal["0.1", "0.2", "0.3", "0.4"] = "0.4"
version: Literal["0.1", "0.2", "0.3", "0.4", "0.5"] | None = Field(
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Workaround for ome/ngff#309.

default=None, exclude=lambda v: v is None
)


class MultiScaleMeta(VersionMeta):
Expand Down
125 changes: 75 additions & 50 deletions iohub/ngff/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

import numpy as np
import zarr.codecs
import zarr.storage
from numcodecs import Blosc
from numpy.typing import ArrayLike, DTypeLike, NDArray
from pydantic import ValidationError
from zarr.core.group import normalize_path
Expand Down Expand Up @@ -57,27 +55,28 @@ def _pad_shape(shape: tuple[int, ...], target: int = 5):


def _open_store(
store_path: StrOrBytesPath,
store_path: StrOrBytesPath | Path,
mode: Literal["r", "r+", "a", "w", "w-"],
version: Literal["0.1", "0.4", "0.5"],
):
if not os.path.isdir(store_path) and mode in ("r", "r+"):
store_path = Path(store_path).resolve()
if not store_path.exists() and mode in ("r", "r+"):
raise FileNotFoundError(
f"Dataset directory not found at {store_path}."
f"Dataset directory not found at {str(store_path)}."
)
if version not in ("0.4", "0.5"):
_logger.warning(
"IOHub is only tested against OME-NGFF v0.4 and v0.5. "
f"Requested version {version} may not work properly."
)
try:
store = zarr.storage.LocalStore(store_path)
root = zarr.open_group(
store, mode=mode, zarr_format=(3 if version == "0.5" else 2)
)
zarr_format = None
if mode in ("w", "w-") or (mode == "a" and not store_path.exists()):
zarr_format = 3 if version == "0.5" else 2
root = zarr.open_group(store_path, mode=mode, zarr_format=zarr_format)
except Exception as e:
raise RuntimeError(
f"Cannot open Zarr root group at {store_path}"
f"Cannot open Zarr root group at {str(store_path)}"
) from e
return root

Expand Down Expand Up @@ -141,6 +140,11 @@ def zattrs(self):
Assignments will modify the metadata file."""
return self._group.attrs

@property
def maybe_wrapped_ome_attrs(self):
"""Container of OME metadata attributes."""
return self.zattrs.get("ome") or self.zattrs

@property
def version(self):
"""NGFF version"""
Expand Down Expand Up @@ -571,12 +575,12 @@ def _set_meta(
)
example_image: ImageArray = self[
self.metadata.multiscales[0].datasets[0].path
].channels
]
self._channel_names = list(range(example_image.channels))

def _parse_meta(self):
multiscales = self.zattrs.get("multiscales")
omero = self.zattrs.get("omero")
multiscales = self.maybe_wrapped_ome_attrs.get("multiscales")
omero = self.maybe_wrapped_ome_attrs.get("omero")
if multiscales:
try:
self._set_meta(multiscales=multiscales, omero=omero)
Expand Down Expand Up @@ -802,6 +806,7 @@ def _check_shape(self, data_shape: tuple[int]):
)

def _create_compressor_options(self, chunk_shape: Tuple[int, ...] = None):
shuffle = zarr.codecs.BloscShuffle.bitshuffle
if self._zarr_format == 3:
return {
"codecs": [
Expand All @@ -812,17 +817,19 @@ def _create_compressor_options(self, chunk_shape: Tuple[int, ...] = None):
zarr.codecs.BloscCodec(
cname="zstd",
clevel=1,
shuffle=Blosc.BITSHUFFLE,
shuffle=shuffle,
),
],
)
],
}
else:
from numcodecs import Blosc

return {
"compressor": Blosc(
cname="zstd", clevel=1, shuffle=Blosc.BITSHUFFLE
),
)
}

def _create_image_meta(
Expand Down Expand Up @@ -1379,7 +1386,7 @@ def __init__(
)

def _parse_meta(self):
if well_group_meta := self.zattrs.get("well"):
if well_group_meta := self.maybe_wrapped_ome_attrs.get("well"):
self.metadata = WellGroupMeta(**well_group_meta)
else:
self._warn_invalid_meta()
Expand Down Expand Up @@ -1622,7 +1629,7 @@ def __init__(
)

def _parse_meta(self):
if plate_meta := self.zattrs.get("plate"):
if plate_meta := self.maybe_wrapped_ome_attrs.get("plate"):
_logger.debug(f"Loading HCS metadata from file: {plate_meta}")
self.metadata = PlateMeta(**plate_meta)
else:
Expand Down Expand Up @@ -1930,14 +1937,56 @@ def rename_well(self, old: str, new: str):
self.dump_meta()


def _check_file_mode(
store_path: Path,
mode: Literal["r", "r+", "a", "w", "w-"],
disable_path_checking: bool,
) -> bool:
if mode == "a":
mode = "r+" if store_path.exists() else "w-"
parse_meta = False
if mode in ("r", "r+"):
parse_meta = True
elif mode == "w-":
if store_path.exists():
raise FileExistsError(store_path)
elif mode == "w":
if store_path.exists():
if (
".zarr" not in str(store_path.resolve())
and not disable_path_checking
):
raise ValueError(
"Cannot overwrite a path that does not contain '.zarr', "
"use `disable_path_checking=True` if you are sure that "
f"{store_path} should be overwritten."
)
_logger.warning(f"Overwriting data at {store_path}")
else:
raise ValueError(f"Invalid persistence mode '{mode}'.")
return parse_meta


def _detect_layout(meta_keys: list[str]) -> Literal["fov", "hcs"]:
if "plate" in meta_keys:
return "hcs"
elif "multiscales" in meta_keys:
return "fov"
else:
raise KeyError(
"Dataset metadata keys ('plate'/'multiscales') not in "
f"the found store metadata keys: {meta_keys}. "
"Is this a valid OME-Zarr dataset?"
)


def open_ome_zarr(
store_path: StrOrBytesPath | Path,
layout: Literal["auto", "fov", "hcs", "tiled"] = "auto",
mode: Literal["r", "r+", "a", "w", "w-"] = "r",
channel_names: list[str] | None = None,
axes: list[AxisMeta] | None = None,
version: Literal["0.1", "0.4", "0.5"] = "0.4",
synchronizer: zarr.ThreadSynchronizer | zarr.ProcessSynchronizer = None,
disable_path_checking: bool = False,
**kwargs,
) -> Plate | Position | TiledPosition:
Expand Down Expand Up @@ -2003,42 +2052,17 @@ def open_ome_zarr(
or :py:class:`iohub.ngff.TiledPosition`)
"""
store_path = Path(store_path)
if mode == "a":
mode = ("w-", "r+")[int(store_path.exists())]
parse_meta = False
if mode in ("r", "r+"):
parse_meta = True
elif mode == "w-":
if store_path.exists():
raise FileExistsError(store_path)
elif mode == "w":
if store_path.exists():
if (
".zarr" not in str(store_path.resolve())
and not disable_path_checking
):
raise ValueError(
"Cannot overwrite a path that does not contain '.zarr', "
"use `disable_path_checking=True` if you are sure that "
f"{store_path} should be overwritten."
)
_logger.warning(f"Overwriting data at {store_path}")
else:
raise ValueError(f"Invalid persistence mode '{mode}'.")
parse_meta = _check_file_mode(
store_path, mode, disable_path_checking=disable_path_checking
)
root = _open_store(store_path, mode, version)
meta_keys = root.attrs.keys() if parse_meta else []
if "ome" in meta_keys:
meta_keys = root.attrs["ome"].keys()
version = root.attrs["ome"].get("version", version)
if layout == "auto":
if parse_meta:
if "plate" in meta_keys:
layout = "hcs"
elif "multiscales" in meta_keys:
layout = "fov"
else:
raise KeyError(
"Dataset metadata keys ('plate'/'multiscales') not in "
f"the found store metadata keys: {meta_keys}. "
"Is this a valid OME-Zarr dataset?"
)
layout = _detect_layout(meta_keys)
else:
raise ValueError(
"Store layout must be specified when creating a new dataset."
Expand All @@ -2059,5 +2083,6 @@ def open_ome_zarr(
parse_meta=parse_meta,
channel_names=channel_names,
axes=axes,
version=version,
**kwargs,
)
6 changes: 3 additions & 3 deletions iohub/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@


def _find_ngff_version_in_zarr_group(group: zarr.Group) -> str | None:
for key in ["plate", "well"]:
for key in ["plate", "well", "ome"]:
if key in group.attrs:
if v := group.attrs[key].get("version"):
return v
Expand Down Expand Up @@ -200,8 +200,8 @@ def print_info(path: StrOrBytesPath, verbose=False):
path = Path(path).resolve()
try:
fmt, extra_info = _infer_format(path)
if fmt == "omezarr" and extra_info == "0.4":
reader = open_ome_zarr(path, mode="r")
if fmt == "omezarr" and extra_info in ("0.4", "0.5"):
reader = open_ome_zarr(path, mode="r", version=extra_info)
else:
reader = read_images(path, data_type=fmt)
except (ValueError, RuntimeError):
Expand Down
3 changes: 2 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ install_requires =
tifffile>=2024.1.30, <2025.5.21
natsort>=7.1.1
ndtiff>=2.2.1
zarr>=3.0.0
zarr>=3.0.8
rich
tqdm
pillow>=9.4.0
Expand All @@ -48,6 +48,7 @@ install_requires =

[options.extras_require]
dev =
acquire-zarr
black
flake8
pytest>=5.0.0
Expand Down
Loading