Skip to content

Commit

Permalink
ENH: Add OMERO metadata write support
Browse files Browse the repository at this point in the history
  • Loading branch information
thewtex committed Jan 10, 2025
1 parent ac51020 commit abc2edc
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 1 deletion.
6 changes: 6 additions & 0 deletions ngff_zarr/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
Transform,
Dataset,
Metadata,
Omero,
OmeroChannel,
OmeroWindow,
)

__all__ = [
Expand Down Expand Up @@ -67,4 +70,7 @@
"Transform",
"Dataset",
"Metadata",
"Omero",
"OmeroChannel",
"OmeroWindow",
]
6 changes: 6 additions & 0 deletions ngff_zarr/to_ngff_zarr.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ def _pop_metadata_optionals(metadata_dict):
if metadata_dict["coordinateTransformations"] is None:
metadata_dict.pop("coordinateTransformations")

if metadata_dict["omero"] is None:
metadata_dict.pop("omero")

return metadata_dict


Expand Down Expand Up @@ -231,6 +234,9 @@ def to_ngff_zarr(
**format_kwargs,
)

if "omero" in metadata_dict:
root.attrs["omero"] = metadata_dict.pop("omero")

if version != "0.4":
# RFC 2, Zarr 3
root.attrs["ome"] = {"version": version, "multiscales": [metadata_dict]}
Expand Down
48 changes: 47 additions & 1 deletion test/test_omero.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
from ngff_zarr import from_ngff_zarr
import numpy as np
from zarr.storage import MemoryStore
from ngff_zarr import (
Omero,
OmeroChannel,
OmeroWindow,
from_ngff_zarr,
to_ngff_image,
to_multiscales,
to_ngff_zarr,
)

from ._data import test_data_dir

Expand Down Expand Up @@ -53,3 +63,39 @@ def test_read_omero(input_images): # noqa: ARG001
assert omero.channels[5].window.max == 65535.0
assert omero.channels[5].window.start == 0.0
assert omero.channels[5].window.end == 100.0


def test_write_omero():
data = np.random.randint(0, 256, 262144).reshape((2, 32, 64, 64)).astype(np.uint8)
image = to_ngff_image(data, dims=["c", "z", "y", "x"])
multiscales = to_multiscales(image, scale_factors=[2, 4], chunks=32)

omero = Omero(
channels=[
OmeroChannel(
color="008000",
window=OmeroWindow(min=0.0, max=255.0, start=10.0, end=150.0),
),
OmeroChannel(
color="0000FF",
window=OmeroWindow(min=0.0, max=255.0, start=30.0, end=200.0),
),
]
)
multiscales.metadata.omero = omero

store = MemoryStore()
version = "0.4"
to_ngff_zarr(store, multiscales, version=version)

multiscales_read = from_ngff_zarr(store, validate=True, version=version)
read_omero = multiscales_read.metadata.omero

assert read_omero is not None
assert len(read_omero.channels) == 2
assert read_omero.channels[0].color == "008000"
assert read_omero.channels[0].window.start == 10.0
assert read_omero.channels[0].window.end == 150.0
assert read_omero.channels[1].color == "0000FF"
assert read_omero.channels[1].window.start == 30.0
assert read_omero.channels[1].window.end == 200.0

0 comments on commit abc2edc

Please sign in to comment.