Skip to content

Commit

Permalink
Add labels to v05
Browse files Browse the repository at this point in the history
  • Loading branch information
dstansby committed Jan 11, 2025
1 parent b82b792 commit 4959a16
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/ome_zarr_models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class BaseGroup(GroupSpec[BaseAttrs, ArraySpec | GroupSpec], ABC): # type: igno

@property
@abstractmethod
def ome_zarr_version(self) -> Literal["0.4"]:
def ome_zarr_version(self) -> Literal["0.4", "0.5"]:
"""
Version of the OME-Zarr specification that this group corresponds to.
"""
34 changes: 34 additions & 0 deletions src/ome_zarr_models/v05/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from typing import Literal

from pydantic_zarr.v2 import ArraySpec, GroupSpec

from ome_zarr_models.base import BaseAttrs, BaseGroup


class BaseOMEAttrs(BaseAttrs):
"""
Base class for all OME attributes.
"""

version: Literal["0.5"] = "0.5"
ome: BaseAttrs


class BaseGroupv05(GroupSpec[BaseOMEAttrs, ArraySpec | GroupSpec], BaseGroup): # type: ignore[misc]
"""
Base class for all v0.5 OME-Zarr groups.
"""

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

@property
def ome_attributes(self) -> BaseAttrs:
"""
OME-Zarr attributes.
"""
return self.attributes.ome # type: ignore[no-any-return]
16 changes: 16 additions & 0 deletions src/ome_zarr_models/v05/labels.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from pydantic_zarr.v2 import ArraySpec, GroupSpec

from ome_zarr_models.v04.labels import LabelsAttrs
from ome_zarr_models.v05.base import BaseGroupv05, BaseOMEAttrs

__all__ = ["Labels", "LabelsAttrs"]


class OMELabelsAttrs(BaseOMEAttrs):
ome: LabelsAttrs


class Labels(GroupSpec[OMELabelsAttrs, ArraySpec | GroupSpec], BaseGroupv05): # type: ignore[misc]
"""
An OME-Zarr labels dataset.
"""
Empty file added tests/v04/__init__.py
Empty file.
Empty file added tests/v05/__init__.py
Empty file.
21 changes: 21 additions & 0 deletions tests/v05/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import json
from pathlib import Path
from typing import TypeVar

import zarr

from ome_zarr_models.base import BaseAttrs

T = TypeVar("T", bound=BaseAttrs)


def json_to_zarr_group(*, json_fname: str) -> zarr.Group:
"""
Create an empty Zarr group, and set attributes from a JSON file.
"""
group = zarr.open_group(store=zarr.MemoryStore())
with open(Path(__file__).parent / "data" / json_fname) as f:
attrs = json.load(f)

group.attrs.put(attrs)
return group
6 changes: 6 additions & 0 deletions tests/v05/data/labels_example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"ome": {
"version": "0.5",
"labels": ["cell_space_segmentation"]
}
}
11 changes: 11 additions & 0 deletions tests/v05/test_labels.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from ome_zarr_models.v04.labels import LabelsAttrs
from ome_zarr_models.v05.labels import Labels, OMELabelsAttrs
from tests.v05.conftest import json_to_zarr_group


def test_labels() -> None:
zarr_group = json_to_zarr_group(json_fname="labels_example.json")
ome_group = Labels.from_zarr(zarr_group)
assert ome_group.ome_attributes == LabelsAttrs(
labels=["cell_space_segmentation"], version="0.5"
)

0 comments on commit 4959a16

Please sign in to comment.