1111
1212import dask .array
1313import numpy as np
14- import zarr
1514from itkwasm import array_like_to_numpy_array
16- from zarr .storage import BaseStore
15+
16+ import zarr
17+ import zarr .storage
18+
19+ # Zarr Python 3
20+ if hasattr (zarr .storage , "StoreLike" ):
21+ StoreLike = zarr .storage .StoreLike
22+ else :
23+ StoreLike = Union [MutableMapping , str , Path , zarr .storage .BaseStore ]
24+ from ._zarr_kwargs import zarr_kwargs
25+
1726
1827from .config import config
1928from .memory_usage import memory_usage
@@ -34,9 +43,7 @@ def _pop_metadata_optionals(metadata_dict):
3443 return metadata_dict
3544
3645
37- def _prep_for_to_zarr (
38- store : Union [MutableMapping , str , Path , BaseStore ], arr : dask .array .Array
39- ) -> dask .array .Array :
46+ def _prep_for_to_zarr (store : StoreLike , arr : dask .array .Array ) -> dask .array .Array :
4047 try :
4148 importlib_metadata .distribution ("kvikio" )
4249 _KVIKIO_AVAILABLE = True
@@ -81,23 +88,27 @@ def _write_with_tensorstore(store_path: str, array, region, chunks) -> None:
8188
8289
8390def to_ngff_zarr (
84- store : Union [ MutableMapping , str , Path , BaseStore ] ,
91+ store : StoreLike ,
8592 multiscales : Multiscales ,
93+ version : str = "0.4" ,
8694 overwrite : bool = True ,
8795 use_tensorstore : bool = False ,
88- chunk_store : Optional [Union [ MutableMapping , str , Path , BaseStore ] ] = None ,
96+ chunk_store : Optional [StoreLike ] = None ,
8997 progress : Optional [Union [NgffProgress , NgffProgressCallback ]] = None ,
9098 ** kwargs ,
9199) -> None :
92100 """
93101 Write an image pixel array and metadata to a Zarr store with the OME-NGFF standard data model.
94102
95103 :param store: Store or path to directory in file system.
96- :type store: MutableMapping, str or Path, zarr.storage.BaseStore
104+ :type store: StoreLike
97105
98106 :param multiscales: Multiscales OME-NGFF image pixel data and metadata. Can be generated with ngff_zarr.to_multiscales.
99107 :type multiscales: Multiscales
100108
109+ :param version: OME-Zarr specification version.
110+ :type version: str, optional
111+
101112 :param overwrite: If True, delete any pre-existing data in `store` before creating groups.
102113 :type overwrite: bool, optional
103114
@@ -106,7 +117,7 @@ def to_ngff_zarr(
106117
107118 :param chunk_store: Separate storage for chunks. If not provided, `store` will be used
108119 for storage of both chunks and metadata.
109- :type chunk_store: MutableMapping, str or Path, zarr.storage.BaseStore , optional
120+ :type chunk_store: StoreLike , optional
110121
111122 :type progress: RichDaskProgress
112123 :param progress: Optional progress logger
@@ -120,10 +131,29 @@ def to_ngff_zarr(
120131 else :
121132 raise ValueError ("Tensorstore requires a path-like store" )
122133
134+ if version != "0.4" and version != "0.5" :
135+ raise ValueError (f"Unsupported version: { version } " )
136+
137+ if version == "0.5" and not use_tensorstore :
138+ raise ValueError ("Version 0.5 currently requires use_tensorstore=True" )
139+
123140 metadata_dict = asdict (multiscales .metadata )
124141 metadata_dict = _pop_metadata_optionals (metadata_dict )
125142 metadata_dict ["@type" ] = "ngff:Image"
126- root = zarr .group (store , overwrite = overwrite , chunk_store = chunk_store )
143+ if version == "0.4" :
144+ # root = zarr.group(store, overwrite=overwrite, chunk_store=chunk_store)
145+ root = zarr .open_group (
146+ store , mode = "w" if overwrite else "a" , chunk_store = chunk_store
147+ )
148+ else :
149+ # For version >= 0.5, open root with Zarr v3
150+ root = zarr .open_group (
151+ store ,
152+ mode = "w" if overwrite else "a" ,
153+ chunk_store = chunk_store ,
154+ zarr_version = 3 ,
155+ )
156+
127157 root .attrs ["multiscales" ] = [metadata_dict ]
128158
129159 nscales = len (multiscales .images )
@@ -160,14 +190,15 @@ def to_ngff_zarr(
160190 shrink_factors .append (1 )
161191
162192 chunks = tuple ([c [0 ] for c in arr .chunks ])
193+
163194 zarr_array = zarr .creation .open_array (
164195 shape = arr .shape ,
165196 chunks = chunks ,
166197 dtype = arr .dtype ,
167198 store = store ,
168199 path = path ,
169200 mode = "a" ,
170- dimension_separator = "/" ,
201+ ** zarr_kwargs ,
171202 )
172203
173204 shape = image .data .shape
@@ -311,7 +342,7 @@ def to_ngff_zarr(
311342 overwrite = False ,
312343 compute = True ,
313344 return_stored = False ,
314- dimension_separator = "/" ,
345+ ** zarr_kwargs ,
315346 ** kwargs ,
316347 )
317348 else :
@@ -334,7 +365,7 @@ def to_ngff_zarr(
334365 overwrite = False ,
335366 compute = True ,
336367 return_stored = False ,
337- dimension_separator = "/" ,
368+ ** zarr_kwargs ,
338369 ** kwargs ,
339370 )
340371
0 commit comments