Skip to content

Commit

Permalink
Fix test_cli.py test_io.py and test_node.py
Browse files Browse the repository at this point in the history
  • Loading branch information
will-moore committed Dec 10, 2024
1 parent 080de7f commit 7134cb4
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 21 deletions.
31 changes: 23 additions & 8 deletions ome_zarr/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def create_zarr(

loc = parse_url(zarr_directory, mode="w")
assert loc
grp = zarr.group(loc.store, zarr_format=2)
grp = zarr.group(loc.store, zarr_format=fmt.zarr_format)
axes = None
size_c = 1
if fmt.version not in ("0.1", "0.2"):
Expand Down Expand Up @@ -200,7 +200,10 @@ def create_zarr(

if labels:
labels_grp = grp.create_group("labels")
labels_grp.attrs["labels"] = [label_name]
if fmt.zarr_format == 2:
labels_grp.attrs["labels"] = [label_name]
else:
labels_grp.attrs["ome"] = {"labels": [label_name]}

label_grp = labels_grp.create_group(label_name)
if axes is not None:
Expand All @@ -214,11 +217,23 @@ def create_zarr(
rgba = [randrange(0, 256) for i in range(4)]
colors.append({"label-value": x, "rgba": rgba})
properties.append({"label-value": x, "class": f"class {x}"})
label_grp.attrs["image-label"] = {
"version": fmt.version,
"colors": colors,
"properties": properties,
"source": {"image": "../../"},
}
if fmt.zarr_format == 2:
label_grp.attrs["image-label"] = {
"version": fmt.version,
"colors": colors,
"properties": properties,
"source": {"image": "../../"},
}
else:
ome_attrs = label_grp.attrs["ome"]
label_grp.attrs["ome"] = {
"image-label": {
"version": fmt.version,
"colors": colors,
"properties": properties,
"source": {"image": "../../"},
},
**ome_attrs,
}

return grp
13 changes: 13 additions & 0 deletions ome_zarr/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ class Format(ABC):
def version(self) -> str: # pragma: no cover
raise NotImplementedError()

@property
@abstractmethod
def zarr_format(self) -> int: # pragma: no cover
raise NotImplementedError()

@abstractmethod
def matches(self, metadata: dict) -> bool: # pragma: no cover
raise NotImplementedError()
Expand Down Expand Up @@ -129,6 +134,10 @@ class FormatV01(Format):
def version(self) -> str:
return "0.1"

@property
def zarr_format(self) -> int:
return 2

def matches(self, metadata: dict) -> bool:
version = self._get_metadata_version(metadata)
LOGGER.debug("%s matches %s?", self.version, version)
Expand Down Expand Up @@ -339,5 +348,9 @@ class FormatV05(FormatV04):
def version(self) -> str:
return "0.5"

@property
def zarr_format(self) -> int:
return 3


CurrentFormat = FormatV05
32 changes: 23 additions & 9 deletions ome_zarr/writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,15 +265,15 @@ def write_multiscale(
# dimension_separator=group.store.dimension_separator,
dimension_separator="/",
compute=compute,
zarr_format=2,
zarr_format=fmt.zarr_format,
)

if not compute:
dask_delayed.append(da_delayed)

else:
# v2 arguments
if fmt.version in ("0.1", "0.2", "0.3", "0.4"):
if fmt.zarr_format == 2:
options["chunks"] = chunks_opt
options["dimension_separator"] = "/"
# default to zstd compression
Expand Down Expand Up @@ -356,6 +356,8 @@ def write_multiscales_metadata(
axes = _get_valid_axes(axes=axes, fmt=fmt)
if axes is not None:
ndim = len(axes)

ome_attrs = {}
if (
isinstance(metadata, dict)
and metadata.get("metadata")
Expand All @@ -378,7 +380,7 @@ def write_multiscales_metadata(
if not isinstance(c["window"][p], (int, float)):
raise TypeError(f"`'{p}'` must be an int or float.")

group.attrs["omero"] = omero_metadata
ome_attrs["omero"] = omero_metadata

# note: we construct the multiscale metadata via dict(), rather than {}
# to avoid duplication of protected keys like 'version' in **metadata
Expand All @@ -393,12 +395,15 @@ def write_multiscales_metadata(
if axes is not None:
multiscales[0]["axes"] = axes

if fmt.version in ("0.1", "0.2", "0.3", "0.4"):
ome_attrs["multiscales"] = multiscales

if fmt.zarr_format == 2:
multiscales[0]["version"] = fmt.version
group.attrs["multiscales"] = multiscales
for key, data in ome_attrs.items():
group.attrs[key] = data
else:
# Zarr v3 metadata under 'ome' with top-level version
group.attrs["ome"] = {"version": fmt.version, "multiscales": multiscales}
group.attrs["ome"] = {"version": fmt.version, **ome_attrs}


def write_plate_metadata(
Expand Down Expand Up @@ -446,7 +451,10 @@ def write_plate_metadata(
plate["field_count"] = field_count
if acquisitions is not None:
plate["acquisitions"] = _validate_plate_acquisitions(acquisitions)
group.attrs["plate"] = plate
if fmt.zarr_format == 2:
group.attrs["plate"] = plate
else:
group.attrs["ome"] = {"plate": plate}


def write_well_metadata(
Expand All @@ -471,7 +479,10 @@ def write_well_metadata(
"images": _validate_well_images(images),
"version": fmt.version,
}
group.attrs["well"] = well
if fmt.zarr_format == 2:
group.attrs["well"] = well
else:
group.attrs["ome"] = {"well": well}


def write_image(
Expand Down Expand Up @@ -730,7 +741,10 @@ def write_label_metadata(

label_list = group.attrs.get("labels", [])
label_list.append(name)
group.attrs["labels"] = label_list
if fmt.zarr_format == 2:
group.attrs["labels"] = label_list
else:
group.attrs["ome"] = {"labels": label_list}


def write_multiscale_labels(
Expand Down
8 changes: 4 additions & 4 deletions tests/test_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from numpy import zeros

from ome_zarr.data import create_zarr
from ome_zarr.format import FormatV01, FormatV02, FormatV03
from ome_zarr.format import FormatV01, FormatV02, FormatV03, FormatV04
from ome_zarr.io import parse_url
from ome_zarr.reader import Label, Labels, Multiscales, Node, Plate, Well
from ome_zarr.writer import write_image, write_plate_metadata, write_well_metadata
Expand Down Expand Up @@ -44,7 +44,7 @@ class TestHCSNode:
@pytest.fixture(autouse=True)
def initdir(self, tmpdir):
self.path = tmpdir.mkdir("data")
self.store = parse_url(str(self.path), mode="w").store
self.store = parse_url(str(self.path), mode="w", fmt=FormatV04()).store
self.root = zarr.group(store=self.store)

def test_minimal_plate(self):
Expand All @@ -53,7 +53,7 @@ def test_minimal_plate(self):
well = row_group.require_group("1")
write_well_metadata(well, ["0"])
image = well.require_group("0")
write_image(zeros((1, 1, 1, 256, 256)), image)
write_image(zeros((1, 1, 1, 256, 256)), image, fmt=FormatV04())

node = Node(parse_url(str(self.path)), list())
assert node.data
Expand Down Expand Up @@ -85,7 +85,7 @@ def test_multiwells_plate(self, fmt):
write_well_metadata(well, ["0", "1", "2"], fmt=fmt)
for field in range(3):
image = well.require_group(str(field))
write_image(zeros((1, 1, 1, 256, 256)), image)
write_image(zeros((1, 1, 1, 256, 256)), image, fmt=fmt)

node = Node(parse_url(str(self.path)), list())
assert node.data
Expand Down

0 comments on commit 7134cb4

Please sign in to comment.