Skip to content

Commit bfb8ae8

Browse files
committed
ENH: Add validate kwarg to from_ngff_zarr
1 parent 110276b commit bfb8ae8

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

ngff_zarr/from_ngff_zarr.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,21 @@
99
from .ngff_image import NgffImage
1010
from .to_multiscales import Multiscales
1111
from .zarr_metadata import Axis, Dataset, Metadata, Scale, Translation
12+
from .validate import validate as validate_ngff
1213

1314

1415
def from_ngff_zarr(
1516
store: Union[MutableMapping, str, Path, BaseStore],
17+
validate: bool = False,
1618
) -> Multiscales:
1719
"""
1820
Read an OME-Zarr NGFF Multiscales data structure from a Zarr store.
1921
2022
store : MutableMapping, str or Path, zarr.storage.BaseStore
2123
Store or path to directory in file system.
2224
25+
validate : bool
26+
If True, validate the NGFF metadata against the schema.
2327
2428
Returns
2529
-------
@@ -29,6 +33,8 @@ def from_ngff_zarr(
2933
"""
3034

3135
root = zarr.open_group(store, mode="r")
36+
if validate:
37+
validate_ngff(root.attrs.asdict())
3238
metadata = root.attrs["multiscales"][0]
3339

3440
dims = [a["name"] for a in metadata["axes"]]

test/test_ngff_validation.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
1-
import json
2-
31
import numpy as np
42
import zarr
5-
from ngff_zarr import Multiscales, to_multiscales, to_ngff_zarr, validate
3+
from ngff_zarr import (
4+
Multiscales,
5+
to_multiscales,
6+
to_ngff_zarr,
7+
validate,
8+
from_ngff_zarr,
9+
)
610

711

812
def check_valid_ngff(multiscale: Multiscales):
913
store = zarr.storage.MemoryStore(dimension_separator="/")
1014
store = zarr.storage.DirectoryStore("/tmp/test.zarr", dimension_separator="/")
1115
to_ngff_zarr(store, multiscale)
12-
zarr.convenience.consolidate_metadata(store)
13-
metadata = json.loads(store.get(".zmetadata"))["metadata"]
14-
ngff = metadata[".zattrs"]
16+
root = zarr.open_group(store, mode="r")
1517

16-
validate(ngff)
18+
validate(root.attrs.asdict())
1719
# Need to add NGFF metadata property
1820
# validate(ngff, strict=True)
1921

22+
from_ngff_zarr(store, validate=True)
23+
2024

2125
def test_y_x_valid_ngff():
2226
array = np.random.random((32, 16))

0 commit comments

Comments
 (0)