OME-ZARR conversion plugin
Write any array or image file to a pyramidal OME-ZARR store, add resolution
levels to an existing store, or store a label image inside an OME-ZARR under
the NGFF labels/ group. Uses only the core dependencies for arrays and
.zarr inputs; reading other file formats needs the optional bioio extra
(pip install "patchworks[bioio]").
Pyramids downsample X and Y only — Z (and channel/time) are kept at full
resolution, matching anisotropic microscopy stacks.
to_ome_zarr
patchworks.plugins.ome_zarr.to_ome_zarr(source: Union[da.Array, np.ndarray, str, Path], out_path: Union[str, Path], *, axes: Union[str, None] = None, pixel_size: Union[PixelSize, tuple, None] = None, scene: int = 0, sequence_pattern: Union[str, None] = None, n_levels: int = 5, downscale: int = 2, chunks: Union[tuple[int, ...], None] = None, shard: ShardSpec = False, reuse_pyramid: bool = False, progress: bool = True, overwrite: bool = False, ngff_version: Union[str, None] = 'auto', compression: Union[str, None] = None, downsample: str = 'mean') -> str
Write source as a pyramidal, calibrated OME-ZARR store.
source may be a dask/NumPy array, a .zarr store, an Imaris .ims
file, any image format readable by bioio (CZI, LIF, ND2, OME-TIFF, …), or
(with sequence_pattern set) a glob over a folder of single-plane TIFFs.
File inputs are read lazily; the pyramid is built level-by-level from disk
with bounded chunks, so the full volume never needs to fit in RAM. Only
x/y are downsampled; z (and channel/time) stay full-resolution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
(Array, ndarray, str or Path)
|
Array or path to convert. |
required |
out_path
|
str or Path
|
Destination |
required |
axes
|
str
|
One character per array dimension, e.g. |
None
|
pixel_size
|
(dict, tuple or None)
|
Physical voxel size in micrometers, as |
None
|
scene
|
int
|
Scene index for multi-scene bioio files. |
0
|
sequence_pattern
|
str
|
When given, source is treated as a glob pattern over a folder of
single-plane TIFFs (e.g. |
None
|
n_levels
|
int
|
Maximum number of pyramid levels including full resolution. |
5
|
downscale
|
int
|
Per-level X/Y downsampling factor (default 2). |
2
|
chunks
|
tuple of int
|
Chunk shape for the written levels. |
None
|
shard
|
bool or tuple of int
|
Pack many chunks into one shard file (zarr v3), cutting the file count
~100× on huge arrays. |
False
|
progress
|
bool
|
Show a per-level dask progress bar (default |
True
|
reuse_pyramid
|
bool
|
Imaris |
False
|
overwrite
|
bool
|
Overwrite an existing store at out_path. |
False
|
ngff_version
|
str or None
|
NGFF version (and therefore zarr format) to write: |
'auto'
|
downsample
|
('mean', 'mode', 'nearest')
|
How the pyramid's X/Y levels are made from the one above: |
"mean"
|
compression
|
str
|
Codec for the arrays written ( |
None
|
Returns:
| Type | Description |
|---|---|
str
|
The path to the written store ( |
Examples:
>>> from patchworks.plugins.ome_zarr import to_ome_zarr
>>> to_ome_zarr("scan.ims", "scan.zarr", n_levels=4)
'scan.zarr'
>>> to_ome_zarr(
... "ZT18_Male4_Left/*.tif",
... "ZT18_Male4_Left.zarr",
... sequence_pattern=r"_T(?P<T>\d+)_Z(?P<Z>\d+)_C(?P<C>\d+)_V\d+",
... shard=True,
... )
'ZT18_Male4_Left.zarr'
Source code in src/patchworks/plugins/ome_zarr.py
1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 | |
add_pyramid
patchworks.plugins.ome_zarr.add_pyramid(group_path: Union[str, Path], *, base: str = '0', axes: Union[str, None] = None, pixel_size: Union[PixelSize, tuple, None] = None, n_levels: int = 5, downscale: int = 2, chunks: Union[tuple[int, ...], None] = None, shard: ShardSpec = False, progress: bool = True, ngff_version: Union[str, None] = 'auto', compression: Union[str, None] = None, downsample: Union[str, None] = None, translation: Union[PixelSize, None] = None) -> str
Add downsampled pyramid levels to an existing single-resolution zarr.
Reads the full-resolution array already at group_path/base, writes the
missing levels next to it (lazily, from disk), and (re)writes the NGFF
multiscales metadata. Existing calibration is preserved; pass
pixel_size to set it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
group_path
|
str or Path
|
Zarr group containing the full-resolution array at base. |
required |
base
|
str
|
Component name of the existing full-resolution level (default
|
'0'
|
axes
|
str
|
One letter per axis, e.g. |
None
|
pixel_size
|
(dict, tuple or None)
|
Physical voxel size in micrometers. |
None
|
n_levels
|
int
|
Maximum number of levels including the existing full-resolution one (default 5). |
5
|
downscale
|
int
|
Per-level X/Y downsampling factor (default 2). |
2
|
chunks
|
tuple of int
|
Chunk shape for the written levels. |
None
|
shard
|
bool or tuple of int
|
Sharding request (see :func: |
False
|
progress
|
bool
|
Show a per-level dask progress bar (default |
True
|
ngff_version
|
str or None
|
NGFF version (and therefore zarr format) to write: |
'auto'
|
downsample
|
('mean', 'mode', 'nearest')
|
How each level is made from the one above (see :func: |
"mean"
|
translation
|
dict
|
Physical offset |
None
|
compression
|
str
|
Codec for the arrays written ( |
None
|
Returns:
| Type | Description |
|---|---|
str
|
The path to the updated group. |
Examples:
Source code in src/patchworks/plugins/ome_zarr.py
2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 | |
write_labels
patchworks.plugins.ome_zarr.write_labels(image_store: Union[str, Path], labels: Union[da.Array, np.ndarray], *, name: str = 'labels', axes: Union[str, None] = None, pixel_size: Union[PixelSize, tuple, None] = None, n_levels: int = 5, downscale: int = 2, chunks: Union[tuple[int, ...], None] = None, shard: ShardSpec = False, progress: bool = True, overwrite: bool = False, n_objects: Union[int, None] = None, ngff_version: Union[str, None] = 'auto', compression: Union[str, None] = None, level: int = 0, provenance: Union[dict, None] = None) -> str
Store labels inside image_store under the NGFF labels/ group.
The labels are written as their own multi-scale pyramid at
image_store/labels/<name>/ and registered in
image_store/labels/.zattrs, so the image and its segmentation live in a
single OME-ZARR store. Calibration is inherited from the parent image
unless pixel_size is given.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image_store
|
str or Path
|
OME-ZARR store this label image belongs to. |
required |
labels
|
Array or ndarray
|
Integer label array (0 = background), same spatial shape as the image. |
required |
name
|
str
|
Label image name under |
'labels'
|
axes
|
str
|
One letter per axis. |
None
|
pixel_size
|
(dict, tuple or None)
|
Physical voxel size in micrometers. |
None
|
n_levels
|
int
|
Maximum number of pyramid levels including full resolution (default 5). |
5
|
downscale
|
int
|
Per-level X/Y downsampling factor (default 2). |
2
|
chunks
|
tuple of int
|
Chunk shape for the written levels. |
None
|
shard
|
bool or tuple of int
|
Sharding request (see :func: |
False
|
progress
|
bool
|
Show a per-level dask progress bar (default |
True
|
overwrite
|
bool
|
Replace an existing label image of the same name (default
|
False
|
n_objects
|
int or None
|
Exact non-background object count, if known — forwarded to
:func: |
None
|
level
|
int
|
The image pyramid level labels were segmented at (default 0);
their calibration and offset are taken from that level. See
:func: |
0
|
ngff_version
|
str or None
|
NGFF version (and therefore zarr format) to write: |
'auto'
|
compression
|
str
|
Codec for the arrays written ( |
None
|
provenance
|
dict
|
How the labels were made (see :func: |
None
|
Returns:
| Type | Description |
|---|---|
str
|
Path to the written label group ( |
Examples:
>>> from patchworks import merge_tile_labels
>>> merged, n = merge_tile_labels(
... "stage.zarr",
... input_component="staged",
... write_to="merged.zarr",
... sequential_labels=True,
... return_count=True,
... )
>>> write_labels(
... "scan.zarr", merged, name="cells", n_objects=n
... )
'scan.zarr/labels/cells'
Source code in src/patchworks/plugins/ome_zarr.py
2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 | |
register_labels
patchworks.plugins.ome_zarr.register_labels(image_store: Union[str, Path], name: str = 'labels', *, axes: Union[str, None] = None, pixel_size: Union[PixelSize, tuple, None] = None, n_levels: int = 5, downscale: int = 2, chunks: Union[tuple[int, ...], None] = None, shard: ShardSpec = False, progress: bool = True, n_objects: Union[int, None] = None, ngff_version: Union[str, None] = 'auto', compression: Union[str, None] = None, level: int = 0, provenance: Union[dict, None] = None) -> str
Pyramidalise and register an existing labels/<name>/0 base level.
Assumes the full-resolution label array already exists at
image_store/labels/<name>/0. Adds the downsampled levels, tags the
group with NGFF image-label metadata, lists name in
labels/.zattrs, and inherits the parent image's pixel calibration
(unless pixel_size is given).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image_store
|
str or Path
|
OME-ZARR store path containing the image this label belongs to. |
required |
name
|
str
|
Label image name under |
'labels'
|
axes
|
str
|
One letter per axis. |
None
|
pixel_size
|
(dict, tuple or None)
|
Physical voxel size in micrometers. |
None
|
n_levels
|
int
|
Maximum number of pyramid levels including full resolution (default 5). |
5
|
downscale
|
int
|
Per-level X/Y downsampling factor (default 2). |
2
|
chunks
|
tuple of int
|
Chunk shape for the written levels. |
None
|
shard
|
bool or tuple of int
|
Sharding request (see :func: |
False
|
progress
|
bool
|
Show a per-level dask progress bar (default |
True
|
level
|
int
|
The image pyramid level the labels were segmented at (default 0). Their voxel size and offset are that level's, not level 0's: taking level 0's for labels made at level 1 draws them at half size, drifting further off the image the further from the origin. |
0
|
n_objects
|
int or None
|
Exact non-background object count, if known (e.g. from
:func: |
None
|
ngff_version
|
str or None
|
NGFF version (and therefore zarr format) to write: |
'auto'
|
compression
|
str
|
Codec for the arrays written ( |
None
|
provenance
|
dict
|
How the labels were made (see :func: |
None
|
Returns:
| Type | Description |
|---|---|
str
|
Path to the label group ( |
Examples:
Source code in src/patchworks/plugins/ome_zarr.py
2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 | |
reshard_level
patchworks.plugins.ome_zarr.reshard_level(group_path: Union[str, Path], component: str = '0', shard: ShardSpec = True, progress: bool = True) -> str
Rewrite an existing array in place, sharded, via one dask pass.
For an array that had to be written unsharded because many writers
filled it a chunk at a time -- a label group's level 0, written tile by
tile by concurrent segment jobs, or by the merge's own worker pool -- a
shard cannot be produced while those writers are running: they would
read-modify-write the same shard file and silently lose each other's
chunks. Doing it afterwards, single-threaded through
:func:_to_zarr_level, is safe because one writer owns every shard.
The cost is one extra full read+write of the level, which is why this is
opt-in rather than implied by shard.
Array attributes are carried across. That matters for a label level 0: the merge records how far it got there, and losing that would let a re-run merge already-merged ids and collide unrelated objects.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
group_path
|
str or Path
|
Path of the zarr group holding the array. |
required |
component
|
str
|
Array name within the group (default |
'0'
|
shard
|
bool or tuple of int
|
Sharding request; see :func: |
True
|
progress
|
bool
|
Show a dask progress bar for the rewrite. |
True
|
Returns:
| Type | Description |
|---|---|
str
|
The path of the array that was rewritten. |
Source code in src/patchworks/plugins/ome_zarr.py
975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 | |
read_pixel_size
patchworks.plugins.ome_zarr.read_pixel_size(store: Union[str, Path], level: int = 0) -> PixelSize
Physical voxel size recorded in an OME-ZARR's metadata for level.
The calibration the conversion carried over from the source file, as
{"z": .., "y": .., "x": ..} in micrometers. Axes left at scale 1.0
(uncalibrated) are omitted, so an empty dict means the store carries no
usable calibration.
Use this instead of retyping voxel sizes into a config: a deconvolution told the wrong voxel size produces a plausible-looking but wrong result.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
store
|
str or Path
|
Path of the OME-ZARR group. |
required |
level
|
int
|
Pyramid level (default 0, full resolution). Anything segmented or measured at a coarser level must use that level's voxel size. |
0
|
Returns:
| Type | Description |
|---|---|
dict
|
|
Examples:
>>> read_pixel_size("scan.zarr")
{'z': 0.2, 'y': 0.1, 'x': 0.1}
>>> read_pixel_size("scan.zarr", level=1)
{'z': 0.2, 'y': 0.2, 'x': 0.2}
Source code in src/patchworks/plugins/ome_zarr.py
NGFF metadata layout
NGFF 0.4 is defined over zarr v2 and puts its keys at the top level; 0.5 is
the zarr-v3 revision and nests them under ome. patchworks writes whichever
matches the store, and reads both.
patchworks.plugins.ome_zarr.ngff_version() -> str
NGFF version currently being written.
Inside a write call this reflects that call's ngff_version=;
outside one it is the version an "auto" write would produce.
Source code in src/patchworks/plugins/ome_zarr.py
Every writer takes an ngff_version= keyword: "auto" (default) follows the
installed zarr (0.5, since patchworks requires zarr-python 3) and "0.4" pins the older, zarr-v2
layout. Writing into an existing store always follows that store's format,
so a label pyramid added later can never disagree with the image it sits in.
NGFF 0.6 is released but not written: RFC-5 replaces axes with
coordinateSystems and requires input/output on every coordinate
transformation, and no reader supports it yet.
patchworks.plugins.ome_zarr.read_ngff_attr(attrs, key: str, default=None)
Read an NGFF key from either layout.
Accepts both the 0.4 top-level placement and the 0.5 ome nesting, so
stores written by any patchworks version (or another tool) still load.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
attrs
|
Mapping
|
A zarr group's attributes. |
required |
key
|
str
|
NGFF key, e.g. |
required |
default
|
Any
|
Returned when the key is absent from both layouts. |
None
|
Source code in src/patchworks/plugins/ome_zarr.py
patchworks.plugins.ome_zarr.write_ngff_attrs(group, **entries) -> None
Write NGFF keys in the layout matching the store's zarr version.
On zarr v3 the keys are merged into the ome attribute (0.5); on v2
they go to the top level (0.4). Merging matters because several keys are
written at different times onto the same group -- multiscales by the
pyramid, then image-label when the labels are registered.