Skip to content

Commit

Permalink
Clean up tutorial
Browse files Browse the repository at this point in the history
  • Loading branch information
dstansby committed Dec 1, 2024
1 parent 1adbc86 commit 254997a
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 16 deletions.
22 changes: 15 additions & 7 deletions docs/tutorial.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# # Tutorial

import matplotlib.pyplot as plt
import pydantic
import zarr
import zarr.storage
from rich.pretty import pprint
Expand All @@ -15,14 +17,13 @@
# We can create an Image model from a zarr group, that points to an
# OME-zarr dataset:

print(tutorial_data_path.absolute())
group = zarr.open(tutorial_data_path)
ome_zarr_image = Image(group)
pprint(ome_zarr_image)

# This image contains both the zarr group, and a model of the multiscales metadata

multiscales_meta = ome_zarr_image.attributes.multiscales
multiscales_meta = ome_zarr_image.multiscales
pprint(multiscales_meta)

# ## Updating models
Expand All @@ -40,9 +41,12 @@
# scale, or a scale then translation (strictly in that order). So if we try and make a
# transformation just a translation, it will raise an error.

multiscales_meta[0].datasets[0].coordinateTransformations = VectorTranslation(
type="translation", translation=[1, 2, 3]
)
try:
multiscales_meta[0].datasets[0].coordinateTransformations = (
VectorTranslation(type="translation", translation=[1, 2, 3]),
)
except pydantic.ValidationError as e:
print(str(e))


# This means validation happens early, allowing you to catch errors
Expand All @@ -53,8 +57,12 @@
# Although these models do not handle reading or writing data, they do expose the zarr
# arrays.

zarr_arr = ome_zarr_image.group[multiscales_meta[0].datasets[0].path]
pprint(zarr_arr)
print(ome_zarr_image.arrays)
full_res_path = ome_zarr_image.multiscales[0].datasets[0].path
zarr_arr = ome_zarr_image.arrays[full_res_path]

fig, ax = plt.subplots()
ax.imshow(zarr_arr)

# ## Not using validation
#
Expand Down
2 changes: 1 addition & 1 deletion mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ theme:
plugins:
- mkdocs-jupyter:
execute: true
allow_errors: true
allow_errors: false
- mkdocstrings:
handlers:
python:
Expand Down
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ docs = [
"gcsfs",
"rich",
"griffe-pydantic",
"matplotlib",
"zarr<3",
]
pydantic = ["pydantic"]
Expand All @@ -36,6 +37,7 @@ docs = [
"rich",
"griffe-pydantic",
"zarr<3",
"matplotlib",
]
dev = [
"jupyter[notebook]>=1.1.1",
Expand Down
28 changes: 20 additions & 8 deletions src/ome_zarr_models/v04/image.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations

from typing import Annotated, Self
from typing import Annotated

import zarr.errors
from pydantic import Field, model_validator
Expand Down Expand Up @@ -79,15 +79,20 @@ class ImageAttrs(Base):
# series MUST have the same number of entries."


class Image(GroupSpec[ImageAttrs, ArraySpec | GroupSpec]):
class ImageSpec(GroupSpec[ImageAttrs, ArraySpec | GroupSpec]):
_check_arrays_compatible = model_validator(mode="after")(_check_arrays_compatible)


class Image:
"""
An OME-zarr multiscale dataset.
"""

_check_arrays_compatible = model_validator(mode="after")(_check_arrays_compatible)
def __init__(self, group: zarr.Group) -> None:
self._spec = self._get_spec(group)
self._group = group

@classmethod
def from_zarr(cls, node: zarr.Group) -> Self:
def _get_spec(self, node: zarr.Group) -> ImageSpec:
"""
Create an instance of an OME-zarr image from a `zarr.Group`.
Expand Down Expand Up @@ -136,18 +141,25 @@ def from_zarr(cls, node: zarr.Group) -> Self:
guess_inferred_members = guess.model_copy(
update={"members": members_normalized.members}
)
return cls(**guess_inferred_members.model_dump())
return ImageSpec(**guess_inferred_members.model_dump())

@property
def arrays(self) -> zarr.Group:
"""
Multiscale array group in this image.
"""
return self._group

@property
def multiscales(self) -> Multiscales:
"""
Multiscales metadata model.
"""
return self.attributes.multiscales
return self._spec.attributes.multiscales

@property
def omero(self) -> Omero | None:
"""
omero metadata model (if present).
"""
return self.attributes.omero
return self._spec.attributes.omero

0 comments on commit 254997a

Please sign in to comment.