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
44 changes: 21 additions & 23 deletions src/ome_zarr_models/_v06/axes.py
Original file line number Diff line number Diff line change
@@ -1,40 +1,38 @@
from collections.abc import Sequence
from typing import Literal
from typing import Self

from pydantic import BaseModel, JsonValue, model_validator

from ome_zarr_models.common.axes import Axis as BaseAxis
from ome_zarr_models.common.axes import AxisType

__all__ = ["Axes", "Axis", "AxisType"]


Orientation = Literal[
"left-to-right",
"right-to-left",
"anterior-to-posterior",
"posterior-to-anterior",
"inferior-to-superior",
"superior-to-inferior",
"dorsal-to-ventral",
"ventral-to-dorsal",
"dorsal-to-palmar",
"palmar-to-dorsal",
"dorsal-to-plantar",
"plantar-to-dorsal",
"rostral-to-caudal",
"caudal-to-rostral",
"cranial-to-caudal",
"caudal-to-cranial",
"proximal-to-distal",
"distal-to-proximal",
]
class Orientation(BaseModel):
"""
Model for an orientation object.
"""

type: JsonValue
value: JsonValue


class Axis(BaseAxis):
"""
Model for an element of multiscale axes.
Model for an element of `Multiscale.axes`.
"""

anatomicalOrientation: Orientation | None = None
orientation: Orientation | None = None

@model_validator(mode="after")
def _check_orientation_only_on_spatial(self) -> Self:
if self.type != "space" and self.orientation is not None:
raise ValueError(
f"Orientation can only be set on a spatial axis "
Comment thread
dstansby marked this conversation as resolved.
f"(got Axis type='{self.type}')"
)
return self


Axes = Sequence[Axis]
12 changes: 5 additions & 7 deletions src/ome_zarr_models/_v06/multiscales.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from typing import Self

from pydantic import model_validator
from pydantic import JsonValue, model_validator

from ome_zarr_models._v06.axes import Axes, Orientation
from ome_zarr_models._v06.axes import Axes
from ome_zarr_models.common.multiscales import Dataset, MultiscaleBase

__all__ = ["Dataset", "Multiscale"]
Expand All @@ -20,10 +20,8 @@ def _ensure_valid_orientations(self: Self) -> Self:
"""
Validate anatomical orientations.
"""
orientations: list[Orientation] = [
a.anatomicalOrientation
for a in self.axes
if a.anatomicalOrientation is not None
orientations: list[JsonValue] = [
a.orientation.value for a in self.axes if a.orientation is not None
]
_check_only_one_value(
orientations=orientations, values=["right-to-left", "left-to-right"]
Expand Down Expand Up @@ -55,7 +53,7 @@ def _ensure_valid_orientations(self: Self) -> Self:


def _check_only_one_value(
*, orientations: list[Orientation], values: list[Orientation]
*, orientations: list[JsonValue], values: list[JsonValue]
) -> None:
counter = 0
for value in values:
Expand Down
15 changes: 15 additions & 0 deletions tests/_v06/test_axes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import pytest
from pydantic import ValidationError

from ome_zarr_models._v06.axes import Axis


def test_orientation_only_spatial() -> None:
with pytest.raises(
ValidationError, match="Orientation can only be set on a spatial axis"
):
Axis(
name="my_axis",
type="time",
orientation={"type": "anatomical", "value": "anterior-to-posterior"},
)
18 changes: 9 additions & 9 deletions tests/_v06/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,28 +43,28 @@ def test_image(store: Store) -> None:
name="t",
type="time",
unit="millisecond",
anatomicalOrientation="left-to-right",
),
Axis(
name="c", type="channel", unit=None, anatomicalOrientation=None
),
Axis(name="c", type="channel", unit=None, orientation=None),
Axis(
name="z",
type="space",
unit="micrometer",
anatomicalOrientation=None,
orientation=None,
),
Axis(
name="y",
type="space",
unit="micrometer",
anatomicalOrientation=None,
orientation=None,
),
Axis(
name="x",
type="space",
unit="micrometer",
anatomicalOrientation=None,
orientation={
"type": "anatomical",
"value": "left-to-right",
},
),
],
datasets=(
Expand Down Expand Up @@ -280,13 +280,13 @@ def test_invalid_orientations() -> None:
name="z",
type="space",
unit="micrometer",
anatomicalOrientation="left-to-right",
orientation={"type": "anatomical", "value": "left-to-right"},
),
Axis(
name="y",
type="space",
unit="micrometer",
anatomicalOrientation="right-to-left",
orientation={"type": "anatomical", "value": "right-to-left"},
),
],
datasets=(
Expand Down
10 changes: 7 additions & 3 deletions tests/data/examples/v06/image_example.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@
{
"name": "t",
"type": "time",
"unit": "millisecond",
"anatomicalOrientation": "left-to-right"
"unit": "millisecond"
},
{ "name": "c", "type": "channel" },
{ "name": "z", "type": "space", "unit": "micrometer" },
{ "name": "y", "type": "space", "unit": "micrometer" },
{ "name": "x", "type": "space", "unit": "micrometer" }
{
"name": "x",
"type": "space",
"unit": "micrometer",
"orientation": { "type": "anatomical", "value": "left-to-right" }
}
],
"datasets": [
{
Expand Down