Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a Labels group object for v05 #118

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
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
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"
)
Loading