Skip to content

Commit

Permalink
Add a base for group objects
Browse files Browse the repository at this point in the history
  • Loading branch information
dstansby committed Jan 5, 2025
1 parent a8e3834 commit 311e78c
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 5 deletions.
37 changes: 37 additions & 0 deletions src/ome_zarr_models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,43 @@
from importlib.metadata import PackageNotFoundError, version

import zarr

import ome_zarr_models.v04.hcs
import ome_zarr_models.v04.image
import ome_zarr_models.v04.image_label
import ome_zarr_models.v04.labels
import ome_zarr_models.v04.well
from ome_zarr_models.base import BaseGroup
from ome_zarr_models.v04.base import BaseGroupv04

try:
__version__ = version("ome_zarr_models")
except PackageNotFoundError: # pragma: no cover
__version__ = "uninstalled"


_V04_groups: list[type[BaseGroupv04]] = [
ome_zarr_models.v04.hcs.HCS,
ome_zarr_models.v04.image_label.ImageLabel,
ome_zarr_models.v04.image.Image,
ome_zarr_models.v04.labels.Labels,
ome_zarr_models.v04.well.Well,
]


def load_ome_zarr_group(group: zarr.Group) -> BaseGroup:
"""
Create an ome-zarr-models object from an existing OME-Zarr group.
This function will 'guess' which type of OME-Zarr data exists by
trying to validate each group metadata definition against your data.
If validation is successful, that data class is returned without
trying any more.
"""
for group_cls in _V04_groups:
try:
return group_cls.from_zarr(group)
except Exception:
continue

Check warning on line 41 in src/ome_zarr_models/__init__.py

View check run for this annotation

Codecov / codecov/patch

src/ome_zarr_models/__init__.py#L37-L41

Added lines #L37 - L41 were not covered by tests

raise RuntimeError(f"Could not find any matches for group {group}")

Check warning on line 43 in src/ome_zarr_models/__init__.py

View check run for this annotation

Codecov / codecov/patch

src/ome_zarr_models/__init__.py#L43

Added line #L43 was not covered by tests
18 changes: 18 additions & 0 deletions src/ome_zarr_models/base.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
from abc import ABC, abstractmethod
from typing import Literal

from pydantic import BaseModel, ConfigDict
from pydantic_zarr.v2 import ArraySpec, GroupSpec


class BaseAttrs(BaseModel):
Expand All @@ -17,3 +21,17 @@ class BaseAttrs(BaseModel):
populate_by_name=True,
frozen=True,
)


class BaseGroup(GroupSpec[BaseAttrs, ArraySpec | GroupSpec], ABC):
"""
Base class for all OME-Zarr groups.
"""

@property
@abstractmethod
def ome_zarr_version(self) -> Literal["0.4"]:
"""
Version of the OME-Zarr specification that this group corresponds to.
"""
...

Check warning on line 37 in src/ome_zarr_models/base.py

View check run for this annotation

Codecov / codecov/patch

src/ome_zarr_models/base.py#L37

Added line #L37 was not covered by tests
16 changes: 16 additions & 0 deletions src/ome_zarr_models/v04/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from typing import Literal

from ome_zarr_models.base import BaseGroup


class BaseGroupv04(BaseGroup):
"""
Base class for all v0.4 OME-Zarr groups.
"""

@property
def ome_zarr_version(self) -> Literal["0.4"]:
"""
OME-Zarr version.
"""
return "0.4"

Check warning on line 16 in src/ome_zarr_models/v04/base.py

View check run for this annotation

Codecov / codecov/patch

src/ome_zarr_models/v04/base.py#L16

Added line #L16 was not covered by tests
3 changes: 2 additions & 1 deletion src/ome_zarr_models/v04/hcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from pydantic_zarr.v2 import ArraySpec, GroupSpec

from ome_zarr_models.base import BaseAttrs
from ome_zarr_models.v04.base import BaseGroupv04
from ome_zarr_models.v04.plate import Plate
from ome_zarr_models.v04.well import Well

Expand All @@ -19,7 +20,7 @@ class HCSAttrs(BaseAttrs):
plate: Plate


class HCS(GroupSpec[HCSAttrs, ArraySpec | GroupSpec]): # type: ignore[misc]
class HCS(GroupSpec[HCSAttrs, ArraySpec | GroupSpec], BaseGroupv04): # type: ignore[misc]
"""
An OME-zarr high-content screening (HCS) dataset representing a single plate.
"""
Expand Down
3 changes: 2 additions & 1 deletion src/ome_zarr_models/v04/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from ome_zarr_models._utils import get_store_path
from ome_zarr_models.base import BaseAttrs
from ome_zarr_models.v04.base import BaseGroupv04
from ome_zarr_models.v04.labels import Labels
from ome_zarr_models.v04.multiscales import Multiscale
from ome_zarr_models.v04.omero import Omero
Expand Down Expand Up @@ -74,7 +75,7 @@ class ImageAttrs(BaseAttrs):
omero: Omero | None = None


class Image(GroupSpec[ImageAttrs, ArraySpec | GroupSpec]): # type: ignore[misc]
class Image(GroupSpec[ImageAttrs, ArraySpec | GroupSpec], BaseGroupv04): # type: ignore[misc]
"""
An OME-zarr multiscale dataset.
"""
Expand Down
3 changes: 2 additions & 1 deletion src/ome_zarr_models/v04/image_label.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from pydantic_zarr.v2 import ArraySpec, GroupSpec

from ome_zarr_models.base import BaseAttrs
from ome_zarr_models.v04.base import BaseGroupv04
from ome_zarr_models.v04.image import Image, _check_arrays_compatible
from ome_zarr_models.v04.image_label_types import (
Label,
Expand All @@ -36,7 +37,7 @@ class ImageLabelAttrs(BaseAttrs):
multiscales: list[Multiscale]


class ImageLabel(GroupSpec[ImageLabelAttrs, ArraySpec | GroupSpec]): # type: ignore[misc]
class ImageLabel(GroupSpec[ImageLabelAttrs, ArraySpec | GroupSpec], BaseGroupv04): # type: ignore[misc]
"""
An image label dataset.
"""
Expand Down
3 changes: 2 additions & 1 deletion src/ome_zarr_models/v04/labels.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from pydantic_zarr.v2 import ArraySpec, GroupSpec

from ome_zarr_models.base import BaseAttrs
from ome_zarr_models.v04.base import BaseGroupv04

__all__ = ["Labels", "LabelsAttrs"]

Expand All @@ -18,7 +19,7 @@ class LabelsAttrs(BaseAttrs):
)


class Labels(GroupSpec[LabelsAttrs, ArraySpec | GroupSpec]): # type: ignore[misc]
class Labels(GroupSpec[LabelsAttrs, ArraySpec | GroupSpec], BaseGroupv04): # type: ignore[misc]
"""
An OME-zarr labels dataset.
"""
3 changes: 2 additions & 1 deletion src/ome_zarr_models/v04/well.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from pydantic_zarr.v2 import ArraySpec, GroupSpec

from ome_zarr_models.base import BaseAttrs
from ome_zarr_models.v04.base import BaseGroupv04
from ome_zarr_models.v04.image import Image
from ome_zarr_models.v04.well_types import WellMeta

Expand All @@ -22,7 +23,7 @@ class WellAttrs(BaseAttrs):
well: WellMeta


class Well(GroupSpec[WellAttrs, ArraySpec | GroupSpec]): # type: ignore[misc]
class Well(GroupSpec[WellAttrs, ArraySpec | GroupSpec], BaseGroupv04): # type: ignore[misc]
def get_image(self, i: int) -> Image:
"""
Get a single image from this well.
Expand Down

0 comments on commit 311e78c

Please sign in to comment.