Skip to content

Commit

Permalink
add basic io testing
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzocerrone committed Aug 28, 2024
1 parent bde3d7f commit d1a2c96
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 19 deletions.
52 changes: 35 additions & 17 deletions src/ngio/io/zarr_group_utils.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
"""Collection of helper functions to work with Zarr groups."""

from pathlib import Path
from typing import Literal

import zarr
from zarr.core.common import AccessModeLiteral, StoreLike, ZarrFormat
import zarr.store
from zarr.core.common import ZarrFormat
from zarr.store.common import StoreLike

ReadOrEdirLiteral = Literal["r", "r+"]


def _open_group(
store: StoreLike, mode: AccessModeLiteral, zarr_version: ZarrFormat = 2
) -> zarr.hierarchy.Group:
store: StoreLike, mode: ReadOrEdirLiteral, zarr_format: ZarrFormat = 2
) -> zarr.Group:
"""Wrapper around zarr.open_group with some additional checks."""
assert mode in ["r", "r+"], f"Invalid mode: {mode}. Must be 'r' or 'r+'."
assert zarr_format in [2, 3], f"Invalid zarr_format: {zarr_format}. Must be 2 or 3."

if isinstance(store, str):
store = Path(store)

Expand All @@ -22,42 +30,52 @@ def _open_group(
raise FileNotFoundError(
f"Path {store.root} does not exist. Cannot open group."
)

if store.mode == "r" and mode == "r+":
raise PermissionError(
"Store is opened in read-only mode. Cannot open be edited."
)

elif store.mode == "r+" and mode == "r":
# Reopen the store in read only mode to avoid failing in the zarr.open_group
store = zarr.store.LocalStore(str(store.root), mode="r")

elif isinstance(store, zarr.store.RemoteStore):
raise NotImplementedError(
"RemoteStore is not yet supported. Please use LocalStore."
)

return zarr.open_group(store=store, mode=mode, zarr_version=zarr_version)
return zarr.open_group(store=store, mode=mode, zarr_format=zarr_format)


def read_group_attrs(store: StoreLike, zarr_version: ZarrFormat = 2) -> dict:
def read_group_attrs(store: StoreLike, zarr_format: ZarrFormat = 2) -> dict:
"""Simple helper function to read the attributes of a Zarr group."""
group = _open_group(store=store, mode="r", zarr_version=zarr_version)
return group.attrs.asdict()
group = _open_group(store=store, mode="r", zarr_format=zarr_format)
return dict(group.attrs)


def update_group_attrs(store: StoreLike, attrs: dict, zarr_version: ZarrFormat) -> None:
def update_group_attrs(store: StoreLike, attrs: dict, zarr_format: ZarrFormat) -> None:
"""Simple helper function to update the attributes of a Zarr group."""
group = _open_group(store=store, mode="a", zarr_version=zarr_version)
group = _open_group(store=store, mode="r+", zarr_format=zarr_format)
group.attrs.update(attrs)


def overwrite_group_attrs(
store: StoreLike, attrs: dict, zarr_version: ZarrFormat
store: StoreLike, attrs: dict, zarr_format: ZarrFormat
) -> None:
"""Simple helper function to overwrite the attributes of a Zarr group."""
group = _open_group(store=store, mode="a", zarr_version=zarr_version)
group = _open_group(store=store, mode="r+", zarr_format=zarr_format)
group.attrs.clear()
group.attrs.update(attrs)


def list_group_arrays(store: StoreLike, zarr_version: ZarrFormat = 2) -> list:
def list_group_arrays(store: StoreLike, zarr_format: ZarrFormat = 2) -> list:
"""Simple helper function to list the arrays in a Zarr group."""
group = _open_group(store, mode="r", zarr_version=zarr_version)
return group.list_arrays()
group = _open_group(store, mode="r", zarr_format=zarr_format)
return list(group.array_keys())


def list_group_groups(store: StoreLike, zarr_version: ZarrFormat = 2) -> list:
def list_group_groups(store: StoreLike, zarr_format: ZarrFormat = 2) -> list:
"""Simple helper function to list the groups in a Zarr group."""
group = _open_group(store, mode="r", zarr_version=zarr_version)
return group.list_groups()
group = _open_group(store, mode="r", zarr_format=zarr_format)
return list(group.group_keys())
4 changes: 2 additions & 2 deletions src/ngio/ngff_meta/v04/zarr_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

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_version=2)
group = open_group(store=zarr_path, mode="r", zarr_format=2)
multiscales = group.attrs.get("multiscales", None)
if multiscales is None:
return False
Expand All @@ -46,7 +46,7 @@ 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_version=2)
group = open_group(store=zarr_path, mode="r", zarr_format=2)
return NgffImageMeta04(**group.attrs)


Expand Down
40 changes: 40 additions & 0 deletions tests/io/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from pathlib import Path

import zarr
import zarr.store
from pytest import fixture


def _create_zarr(tempdir, zarr_format=2):
zarr_path = Path(tempdir) / f"test_group_v{2}.zarr"
group = zarr.open_group(store=zarr_path, mode="w", zarr_format=zarr_format)

for i in range(3):
group.create_array(f"array_{i}", shape=(10, 10), dtype="i4")

for i in range(3):
group.create_group(f"group_{i}")

return zarr_path


@fixture
def local_zarr_path_v2(tmpdir) -> tuple[Path, int]:
zarr_path = _create_zarr(tmpdir, zarr_format=2)
return zarr_path, 2


@fixture
def local_zarr_path_v3(tmpdir) -> tuple[Path, int]:
zarr_path = _create_zarr(tmpdir, zarr_format=3)
return zarr_path, 3


@fixture(
params=[
"local_zarr_path_v2",
"local_zarr_path_v3",
]
)
def store_fixture(request):
return request.getfixturevalue(request.param)
51 changes: 51 additions & 0 deletions tests/io/test_zarr_group_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
class TestGroupUtils:
@property
def test_attrs(self) -> dict:
return {"a": 1, "b": 2, "c": 3}

def test_update_group_attrs(self, store_fixture):
from ngio.io.zarr_group_utils import (
read_group_attrs,
update_group_attrs,
)

store, zarr_format = store_fixture

update_group_attrs(store=store, attrs=self.test_attrs, zarr_format=zarr_format)
attrs = read_group_attrs(store=store, zarr_format=zarr_format)
assert attrs == self.test_attrs, "Attributes were not written correctly."

update_group_attrs(store=store, attrs={"new": 1}, zarr_format=zarr_format)
attrs = read_group_attrs(store=store, zarr_format=zarr_format)
expected = {**self.test_attrs, "new": 1}
assert attrs == expected, "Attributes were not written correctly."

def test_overwrite_group_attrs(self, store_fixture):
from ngio.io.zarr_group_utils import (
overwrite_group_attrs,
read_group_attrs,
)

store, zarr_format = store_fixture

overwrite_group_attrs(
store=store, attrs=self.test_attrs, zarr_format=zarr_format
)
attrs = read_group_attrs(store=store, zarr_format=zarr_format)
assert attrs == self.test_attrs, "Attributes were not written correctly."

def test_list_group_arrays(self, store_fixture):
from ngio.io.zarr_group_utils import list_group_arrays

store, zarr_format = store_fixture

arrays = list_group_arrays(store=store, zarr_format=zarr_format)
assert len(arrays) == 3, "Arrays were not listed correctly."

def test_list_group_groups(self, store_fixture):
from ngio.io.zarr_group_utils import list_group_groups

store, zarr_format = store_fixture

groups = list_group_groups(store=store, zarr_format=zarr_format)
assert len(groups) == 3, "Groups were not listed correctly."

0 comments on commit d1a2c96

Please sign in to comment.