Skip to content
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
69 changes: 50 additions & 19 deletions ngff_zarr/from_ngff_zarr.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from collections.abc import MutableMapping
from pathlib import Path
from typing import Union, Optional
from packaging import version
import packaging.version

import dask.array
import zarr
Expand All @@ -18,7 +18,7 @@
from .v04.zarr_metadata import Axis, Dataset, Scale, Translation
from .validate import validate as validate_ngff

zarr_version = version.parse(zarr.__version__)
zarr_version = packaging.version.parse(zarr.__version__)
zarr_version_major = zarr_version.major


Expand Down Expand Up @@ -48,7 +48,11 @@ def from_ngff_zarr(

format_kwargs = {}
if version and zarr_version_major >= 3:
format_kwargs = {"zarr_format": 2} if version == "0.4" else {"zarr_format": 3}
format_kwargs = (
{"zarr_format": 2}
if packaging.version.parse(version) < packaging.version.parse("0.5")
else {"zarr_format": 3}
)
root = zarr.open_group(store, mode="r", **format_kwargs)
root_attrs = root.attrs.asdict()

Expand All @@ -66,16 +70,22 @@ def from_ngff_zarr(
else:
metadata = root.attrs["multiscales"][0]

dims = [a["name"] for a in metadata["axes"]]
if "axes" not in metadata:
from .v04.zarr_metadata import supported_dims

dims = list(reversed(supported_dims))
else:
dims = [a["name"] if "name" in a else a for a in metadata["axes"]]

name = "image"
if name in metadata:
name = metadata["name"]

units = {d: None for d in dims}
for axis in metadata["axes"]:
if "unit" in axis:
units[axis["name"]] = axis["unit"]
if "axes" in metadata:
for axis in metadata["axes"]:
if "unit" in axis:
units[axis["name"]] = axis["unit"]

images = []
datasets = []
Expand All @@ -85,17 +95,18 @@ def from_ngff_zarr(
scale = {d: 1.0 for d in dims}
translation = {d: 0.0 for d in dims}
coordinateTransformations = []
for transformation in dataset["coordinateTransformations"]:
if "scale" in transformation:
scale = transformation["scale"]
scale = dict(zip(dims, scale))
coordinateTransformations.append(Scale(transformation["scale"]))
elif "translation" in transformation:
translation = transformation["translation"]
translation = dict(zip(dims, translation))
coordinateTransformations.append(
Translation(transformation["translation"])
)
if "coordinateTransformations" in dataset:
for transformation in dataset["coordinateTransformations"]:
if "scale" in transformation:
scale = transformation["scale"]
scale = dict(zip(dims, scale))
coordinateTransformations.append(Scale(transformation["scale"]))
elif "translation" in transformation:
translation = transformation["translation"]
translation = dict(zip(dims, translation))
coordinateTransformations.append(
Translation(transformation["translation"])
)
datasets.append(
Dataset(
path=dataset["path"],
Expand All @@ -107,7 +118,27 @@ def from_ngff_zarr(
images.append(ngff_image)

metadata.pop("@type", None)
axes = [Axis(**axis) for axis in metadata["axes"]]
if "axes" in metadata:
if "name" in metadata["axes"][0]:
axes = [Axis(**axis) for axis in metadata["axes"]]
else:
# v0.3
type_dict = {
"t": "time",
"c": "channel",
"z": "space",
"y": "space",
"x": "space",
}
axes = [Axis(name=axis, type=type_dict[axis]) for axis in metadata["axes"]]
Copy link

Copilot AI Nov 29, 2024

Choose a reason for hiding this comment

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

The line 'axes = [Axis(name=axis, type=type_dict[axis]) for axis in metadata["axes"]]' assumes that the keys will always be one of "t", "c", "z", "y", or "x". Consider adding a default case or error handling for unexpected keys.

Suggested change
axes = [Axis(name=axis, type=type_dict[axis]) for axis in metadata["axes"]]
axes = [Axis(name=axis, type=type_dict.get(axis, 'unknown')) for axis in metadata['axes']]

Copilot uses AI. Check for mistakes.
else:
axes = [
Axis(name="t", type="time"),
Axis(name="c", type="channel"),
Axis(name="z", type="space"),
Axis(name="y", type="space"),
Axis(name="x", type="space"),
]
coordinateTransformations = None
if "coordinateTransformations" in metadata:
coordinateTransformations = metadata["coordinateTransformations"]
Expand Down
4 changes: 4 additions & 0 deletions ngff_zarr/spec/0.1/copyright.include
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Copyright © 2020-[YEAR]
<a href="https://www.openmicroscopy.org/"><abbr title="Open Microscopy Environment">OME</abbr></a><sup>®</sup>
(<a href="https://dundee.ac.uk/"><abbr title="University of Dundee">U. Dundee</abbr></a>).
OME trademark rules apply.
112 changes: 112 additions & 0 deletions ngff_zarr/spec/0.1/schemas/image.schema
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://ngff.openmicroscopy.org/0.1/schemas/image.schema",
"title": "NGFF Image",
"description": "JSON from OME-NGFF .zattrs",
"type": "object",
"properties": {
"multiscales": {
"description": "The multiscale datasets for this image",
"type": "array",
"items": {
"type": "object",
"properties": {
"name": {
"type": "string"
},
"datasets": {
"type": "array",
"minItems": 1,
"items": {
"type": "object",
"properties": {
"path": {
"type": "string"
}
},
"required": ["path"]
}
},
"version": {
"type": "string",
"enum": [
"0.1"
]
},
"metadata": {
"type": "object",
"properties": {
"method": {
"type": "string"
},
"version": {
"type": "string"
}
}
}
},
"required": [
"datasets"
]
},
"minItems": 1,
"uniqueItems": true
},
"omero": {
"type": "object",
"properties": {
"channels": {
"type": "array",
"items": {
"type": "object",
"properties": {
"window": {
"type": "object",
"properties": {
"end": {
"type": "number"
},
"max": {
"type": "number"
},
"min": {
"type": "number"
},
"start": {
"type": "number"
}
},
"required": [
"start",
"min",
"end",
"max"
]
},
"label": {
"type": "string"
},
"family": {
"type": "string"
},
"color": {
"type": "string"
},
"active": {
"type": "boolean"
}
},
"required": [
"window",
"color"
]
}
}
},
"required": [
"channels"
]
}
},
"required": [ "multiscales" ]
}
112 changes: 112 additions & 0 deletions ngff_zarr/spec/0.1/schemas/plate.schema
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://ngff.openmicroscopy.org/0.1/schemas/plate.schema",
"title": "OME-NGFF plate schema",
"description": "JSON from OME-NGFF Plate .zattrs",
"type": "object",
"properties": {
"plate": {
"type": "object",
"properties": {
"version": {
"type": "string",
"enum": [
"0.1"
]
},
"name": {
"type": "string"
},
"columns": {
"description": "Columns of the Plate grid",
"type": "array",
"items": {
"type": "object",
"properties": {
"name": {
"type": "string"
}
},
"required": [
"name"
]
},
"minItems": 1,
"uniqueItems": true
},
"rows": {
"description": "Rows of the Plate grid",
"type": "array",
"items": {
"type": "object",
"properties": {
"name": {
"type": "string"
}
},
"required": [
"name"
]
},
"minItems": 1,
"uniqueItems": true
},
"wells": {
"description": "Rows of the Plate grid",
"type": "array",
"items": {
"type": "object",
"properties": {
"path": {
"type": "string"
}
},
"required": [
"path"
]
},
"minItems": 1,
"uniqueItems": true
},
"field_count": {
"description": "Maximum number of fields per view across all wells."
},
"acquisitions": {
"description": "Rows of the Plate grid",
"type": "array",
"items": {
"type": "object",
"properties": {
"id": {
"type": "number"
},
"maximumfieldcount": {
"type": "number"
},
"name": {
"type": "string"
},
"description": {
"type": "string"
},
"starttime": {
"type": "number"
}
},
"required": [
"id"
]
},
"minItems": 1,
"uniqueItems": true
}
},
"required": [
"version", "columns", "rows", "wells"
]
}
},
"required": [
"plate"
]
}
19 changes: 19 additions & 0 deletions ngff_zarr/spec/0.1/schemas/strict_image.schema
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"$id": "https://ngff.openmicroscopy.org/0.1/schemas/strict_image.schema",
"allOf": [
{
"$ref": "https://ngff.openmicroscopy.org/0.1/schemas/image.schema"
},
{
"properties": {
"multiscales": {
"items": {
"required": [
"version", "metadata", "type", "name"
]
}
}
}
}
]
}
Loading
Loading