Skip to content

Commit

Permalink
chore: add more testing
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzocerrone committed Aug 28, 2024
1 parent 0d04f7e commit 383baaa
Show file tree
Hide file tree
Showing 8 changed files with 178 additions and 17 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ jobs:
python -m pip install -U pip
# if running a cron job, we add the --pre flag to test against pre-releases
python -m pip install .[test] ${{ github.event_name == 'schedule' && '--pre' || '' }}
python -m pip install git+https://github.com/zarr-developers/zarr-python.git
- name: 🧪 Run Tests
run: pytest --color=yes --cov --cov-report=xml --cov-report=term-missing
Expand Down
4 changes: 2 additions & 2 deletions src/ngio/ngff_meta/meta_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ def find_ngff_image_meta_handler_version(zarr_path: str) -> str:


def get_ngff_image_meta_handler(
zarr_path: str, meta_mode: Literal["image", "label"]
zarr_path: str, meta_mode: Literal["image", "label"], cache: bool = False
) -> NgffImageMetaHandler:
"""Load the NGFF image metadata handler."""
version = find_ngff_image_meta_handler_version(zarr_path)
handler = _available_load_ngff_image_meta_handlers[version]
return handler(zarr_path, meta_mode=meta_mode)
return handler(zarr_path, meta_mode=meta_mode, cache=cache)
8 changes: 4 additions & 4 deletions src/ngio/ngff_meta/v04/specs.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ class Window04(BaseModel):
class Channel04(BaseWithExtraFields):
"""Model for `Omero.channels`."""

active: bool | None = None
coefficient: int | None = None
# active: bool | None = None
# coefficient: int | None = None
color: str
family: str | None = None
inverted: bool | None = None
# family: str | None = None
# inverted: bool | None = None
label: str | None = None
window: Window04 | None = None

Expand Down
53 changes: 44 additions & 9 deletions src/ngio/ngff_meta/v04/zarr_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

from typing import Literal

from zarr import open_group

from ngio.io import read_group_attrs, update_group_attrs
from ngio.ngff_meta.fractal_image_meta import (
Axis,
Dataset,
Expand All @@ -16,8 +15,11 @@
TranslationCoordinateTransformation,
)
from ngio.ngff_meta.v04.specs import (
Axis04,
Dataset04,
Multiscale04,
NgffImageMeta04,
Omero04,
ScaleCoordinateTransformation04,
Transformation04,
TranslationCoordinateTransformation04,
Expand All @@ -26,8 +28,8 @@

def check_ngff_image_meta_v04(zarr_path: str) -> bool:
"""Check if a Zarr Group contains the OME-NGFF v0.4."""
group = open_group(store=zarr_path, mode="r", zarr_format=2)
multiscales = group.attrs.get("multiscales", None)
group = read_group_attrs(store=zarr_path, zarr_format=2)
multiscales = group.get("multiscales", None)
if multiscales is None:
return False

Expand All @@ -46,8 +48,8 @@ def check_ngff_image_meta_v04(zarr_path: str) -> bool:

def load_vanilla_ngff_image_meta_v04(zarr_path: str) -> NgffImageMeta04:
"""Load the OME-NGFF 0.4 image meta model."""
group = open_group(store=zarr_path, mode="r", zarr_format=2)
return NgffImageMeta04(**group.attrs)
attrs = read_group_attrs(store=zarr_path, zarr_format=2)
return NgffImageMeta04(**attrs)


def _transform_dataset(
Expand Down Expand Up @@ -143,17 +145,50 @@ def vanilla_ngff_image_meta_v04_to_fractal(
)


def fractal_ngff_image_meta_to_vanilla_v04(
meta: FractalImageLabelMeta,
) -> NgffImageMeta04:
"""Convert the FractalImageMeta to NgffImageMeta."""
axes04 = [Axis04(**axis.model_dump()) for axis in meta.multiscale.axes]
dataset04 = []
for dataset in meta.multiscale.datasets:
transformations = [t.model_dump() for t in dataset.coordinateTransformations]
dataset04.append(
Dataset04(path=dataset.path, coordinateTransformations=transformations)
)
multiscale04 = Multiscale04(
name=meta.name,
axes=axes04,
datasets=dataset04,
version="0.4",
)
omero04 = None if meta.omero is None else Omero04(**meta.omero.model_dump())
return NgffImageMeta04(
multiscales=[multiscale04],
omero=omero04,
)


def load_ngff_image_meta_v04(zarr_path: str) -> FractalImageLabelMeta:
"""Load the OME-NGFF 0.4 image meta model."""
check_ngff_image_meta_v04(zarr_path=zarr_path)
if not check_ngff_image_meta_v04(zarr_path=zarr_path):
raise ValueError(
"The Zarr store does not contain the correct OME-Zarr version."
)
meta04 = load_vanilla_ngff_image_meta_v04(zarr_path=zarr_path)
return vanilla_ngff_image_meta_v04_to_fractal(meta04=meta04)


def write_ngff_image_meta_v04(zarr_path: str, meta: FractalImageLabelMeta) -> None:
"""Write the OME-NGFF 0.4 image meta model."""
# TODO: Implement the conversion from FractalImageMeta to NgffImageMeta
pass
if not check_ngff_image_meta_v04(zarr_path=zarr_path):
raise ValueError(
"The Zarr store does not contain the correct OME-Zarr version."
)
meta04 = fractal_ngff_image_meta_to_vanilla_v04(meta=meta)
update_group_attrs(
store=zarr_path, attrs=meta04.model_dump(exclude_none=True), zarr_format=2
)


class NgffImageMetaZarrHandlerV04:
Expand Down
70 changes: 70 additions & 0 deletions tests/data/meta_v04/base_ome_zarr_image_meta.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
{
"multiscales": [
{
"axes": [
{"name": "c", "type": "channel"},
{"name": "z", "type": "space", "unit": "micrometer"},
{"name": "y", "type": "space", "unit": "micrometer"},
{"name": "x", "type": "space", "unit": "micrometer"}
],
"datasets": [
{
"coordinateTransformations": [
{"scale": [1.0, 1.0, 0.1625, 0.1625], "type": "scale"}
],
"path": "0"
},
{
"coordinateTransformations": [
{"scale": [1.0, 1.0, 0.325, 0.325], "type": "scale"}
],
"path": "1"
},
{
"coordinateTransformations": [
{"scale": [1.0, 1.0, 0.65, 0.65], "type": "scale"}
],
"path": "2"
},
{
"coordinateTransformations": [
{"scale": [1.0, 1.0, 1.3, 1.3], "type": "scale"}
],
"path": "3"
},
{
"coordinateTransformations": [
{"scale": [1.0, 1.0, 2.6, 2.6], "type": "scale"}
],
"path": "4"
}
],
"version": "0.4"
}
],
"omero": {
"channels": [
{
"color": "00FFFF",
"label": "DAPI",
"wavelength_id": "A01_C01",
"window": {"end": 700, "max": 65535, "min": 0, "start": 0}
},
{
"color": "FF00FF",
"label": "nanog",
"wavelength_id": "A01_C02",
"window": {"end": 180, "max": 65535, "min": 0, "start": 0}
},
{
"color": "FFFF00",
"label": "Lamin B1",
"wavelength_id": "A02_C03",
"window": {"end": 1500, "max": 65535, "min": 0, "start": 0}
}
],
"id": 1,
"name": "TBD",
"version": "0.4"
}
}
20 changes: 20 additions & 0 deletions tests/ngff_meta/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import json
from pathlib import Path

import zarr
import zarr.store
from pytest import fixture


@fixture
def ome_zarr_image_v04_path(tmpdir):
zarr_path = Path(tmpdir) / "test_ome_ngff_v04.zarr"

group = zarr.open_group(store=zarr_path, mode="w", zarr_format=2)

with open("tests/data/meta_v04/base_ome_zarr_image_meta.json") as f:
base_ome_zarr_meta = json.load(f)

base_ome_zarr_meta = base_ome_zarr_meta
group.attrs.update(base_ome_zarr_meta)
return zarr_path
37 changes: 37 additions & 0 deletions tests/ngff_meta/test_v04.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import json


class TestOMEZarrHandlerV04:
def test_basic_workflow(self, ome_zarr_image_v04_path):
from ngio.io import read_group_attrs
from ngio.ngff_meta import get_ngff_image_meta_handler

handler = get_ngff_image_meta_handler(
zarr_path=ome_zarr_image_v04_path, meta_mode="image"
)

meta = handler.load_meta()
handler.write_meta(meta)

with open("tests/data/meta_v04/base_ome_zarr_image_meta.json") as f:
base_ome_zarr_meta = json.load(f)

saved_meta = read_group_attrs(store=ome_zarr_image_v04_path, zarr_format=2)
assert saved_meta == base_ome_zarr_meta

def test_basic_workflow_with_cache(self, ome_zarr_image_v04_path):
from ngio.io import read_group_attrs
from ngio.ngff_meta import get_ngff_image_meta_handler

handler = get_ngff_image_meta_handler(
zarr_path=ome_zarr_image_v04_path, meta_mode="image", cache=True
)

meta = handler.load_meta()
handler.write_meta(meta)

with open("tests/data/meta_v04/base_ome_zarr_image_meta.json") as f:
base_ome_zarr_meta = json.load(f)

saved_meta = read_group_attrs(store=ome_zarr_image_v04_path, zarr_format=2)
assert saved_meta == base_ome_zarr_meta
2 changes: 0 additions & 2 deletions tests/test_ngio.py

This file was deleted.

0 comments on commit 383baaa

Please sign in to comment.