Skip to content

Commit

Permalink
Add interface for getting wells in a HCS
Browse files Browse the repository at this point in the history
  • Loading branch information
dstansby committed Dec 10, 2024
1 parent 4a6851e commit ec98ab8
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
28 changes: 28 additions & 0 deletions src/ome_zarr_models/v04/hcs.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
from collections.abc import Generator

from pydantic_zarr.v2 import ArraySpec, GroupSpec

from ome_zarr_models.base import Base
from ome_zarr_models.v04.plate import Plate
from ome_zarr_models.v04.well import WellGroup

__all__ = ["HCSAttrs"]

Expand All @@ -18,3 +21,28 @@ class HCS(GroupSpec[HCSAttrs, ArraySpec | GroupSpec]):
"""
An OME-zarr high-content screening (HCS) dataset representing a single plate.
"""

def get_well_group(self, i: int) -> WellGroup:
"""
Get a single well group.
"""
well = self.attributes.plate.wells[i]
well_path = well.path
well_path_parts = well_path.split("/")
group = self
for part in well_path_parts:
group = group.members[part]

return WellGroup(attributes=group.attributes, members=group.members)

@property
def n_wells(self) -> int:
"""
Number of wells.
"""
return len(self.attributes.plate.wells)

@property
def well_groups(self) -> Generator[WellGroup, None, None]:
for i in range(self.n_wells):
yield self.get_well_group(i)
16 changes: 15 additions & 1 deletion src/ome_zarr_models/v04/well.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
from typing import Annotated, Literal

from pydantic import AfterValidator, Field
from pydantic_zarr.v2 import ArraySpec, GroupSpec

from ome_zarr_models._utils import _AlphaNumericConstraint, _unique_items_validator
from ome_zarr_models.base import Base

__all__ = ["Well", "WellImage"]
# WellGroup is defined one level higher
__all__ = ["Well", "WellAttrs", "WellImage"]


class WellImage(Base):
Expand Down Expand Up @@ -58,3 +60,15 @@ def get_acquisition_paths(self) -> dict[int, list[str]]:
)
acquisition_dict[image.acquisition].append(image.path)
return dict(acquisition_dict)


class WellAttrs(Base):
"""
Attributes for a well group.
"""

well: Well


class WellGroup(GroupSpec[WellAttrs, ArraySpec | GroupSpec]):
pass
7 changes: 7 additions & 0 deletions tests/v04/test_hcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from ome_zarr_models.v04 import HCS
from ome_zarr_models.v04.hcs import HCSAttrs
from ome_zarr_models.v04.plate import Acquisition, Column, Plate, Row, WellInPlate
from ome_zarr_models.v04.well import Well, WellImage


def test_example_hcs() -> None:
Expand All @@ -30,3 +31,9 @@ def test_example_hcs() -> None:
wells=[WellInPlate(path="B/03", rowIndex=0, columnIndex=0)],
)
)

well_groups = list(hcs.well_groups)
assert len(well_groups) == 1
assert well_groups[0].attributes.well == Well(
images=[WellImage(path="0", acquisition=None)], version="0.4"
)

0 comments on commit ec98ab8

Please sign in to comment.