Skip to content

Writing OME-Zarr#

yaozarrs.write.v05 and yaozarrs.write.v06 write complete OME-Zarr stores (metadata and array data) to a local directory. Both have the same functions (use the one matching your metadata models), and v0.6 adds write_scene and SceneBuilder, for scenes.

Writing needs an array backend, see Array writer backends.

The general pattern#

  1. Build the metadata model that matches your data (see the Guide to OME-Zarr): an Image, a Plate, a Bf2Raw collection, or (v0.6) a Scene.
  2. Pass your model and data to a high level write_* function, or a lower level builder.

Each Dataset (resolution level) in an image's multiscale becomes one array in the store, so functions that take arrays (or (shape, dtype) pairs) expect one per dataset, in the same order.

import numpy as np

from yaozarrs import DimSpec, v06
from yaozarrs.write.v06 import prepare_image, write_image

dims = [
    DimSpec(name="y", scale=0.1, unit="micrometer"),
    DimSpec(name="x", scale=0.1, unit="micrometer"),
]
image = v06.Image(multiscales=[v06.Multiscale.from_dims(dims)])
data = np.zeros((256, 256), dtype=np.uint16)

# all at once
write_image("image.ome.zarr", image, data)

# or: create the hierarchy with empty arrays, and fill them yourself
path, arrays = prepare_image("prepared.ome.zarr", image, (data.shape, data.dtype))
arrays["0"][:] = data

High level functions#

These take all the data at once, write it, and return the Path to the root of the store.

Function Writes
write_image an image (with optional labels) v0.5 · v0.6
write_plate a multi-well plate v0.5 · v0.6
write_bioformats2raw a bioformats2raw collection v0.5 · v0.6
write_scene a scene of images v0.6

Builders#

When you don't have all the data up front, or want more control over how it's written, use prepare_image or a builder:

Function Prepares
prepare_image an image v0.5 · v0.6
LabelsBuilder the label images of an image v0.5 · v0.6
PlateBuilder a multi-well plate v0.5 · v0.6
Bf2RawBuilder a bioformats2raw collection v0.5 · v0.6
SceneBuilder a scene of images v0.6

prepare_image writes the metadata, creates empty arrays, and gives them back to you (keyed by path) to fill however you like (chunk by chunk, in parallel, ...). The builders can be used in three ways:

  • incremental: write one well, label or image at a time (e.g. PlateBuilder.write_well()), and the parent's metadata (the plate, labels, or bioformats2raw series list) is updated after each one.
  • prepare-only: register everything with add_*(), then prepare() creates the whole hierarchy with empty arrays, as prepare_image does.
  • create now, write later: Bf2RawBuilder.prepare_series() adds one series at a time with empty arrays, for you to fill whenever you like. Each series is listed only once its arrays exist, so the collection is valid after every call, and you can keep adding series (e.g. as a model's output tiles arrive).

For plates and bioformats2raw collections, the builders are usually the easier choice.

Array writer backends#

Every function takes a writer argument, which picks the library that creates the arrays:

  • "zarr": zarr-python, pip install "yaozarrs[write-zarr]"
  • "tensorstore": tensorstore, pip install "yaozarrs[write-tensorstore]"
  • "auto" (the default): tensorstore if it's installed, zarr-python otherwise
  • your own function, see Custom writers

Custom writers#

Advanced

Not needed for most users. Skip this if the built-in zarr-python or tensorstore backends work for you.

CreateArrayFunc #

Protocol for custom array creation functions.

This is the type signature for functions that can be passed as the writer parameter to many functions in this module. Use this if you'd like to completely customize how arrays are created, e.g., using a different backend, or custom storage options. See _create_array_zarr and _create_array_tensorstore for (our internal) reference implementations.

__call__ #

__call__(
    path: Path,
    shape: tuple[int, ...],
    dtype: Any,
    chunks: tuple[int, ...],
    *,
    shards: tuple[int, ...] | None,
    overwrite: bool,
    compression: CompressionName,
    dimension_names: list[str] | None,
) -> Any

Create array structure without writing data.

Parameters:

Name Type Description Default
path Path

Path to create array

required
shape tuple[int, ...]

Array shape

required
dtype DTypeLike

Data type

required
chunks tuple[int, ...]

Chunk shape (already resolved by yaozarrs)

required
shards tuple[int, ...] | None

Shard shape for Zarr v3 sharding, or None

required
dimension_names list[str] | None

Names for each dimension

required
overwrite bool

Whether to overwrite existing array

required
compression 'blosc-zstd' | 'blosc-lz4' | 'zstd' | 'none'

Compression codec to use

required

Returns:

Type Description
Any

Array object that supports numpy-style indexing for writing (e.g., zarr.Array or tensorstore.TensorStore). Other arrays are written with a single array[:] = data, computing dask data in memory.

Types#

The type hints used in the signatures of both modules. (They aren't importable: they're only there for type checkers, and for these docs.)

ArrayLike module-attribute #

ArrayLike: TypeAlias = Any

Array data: a numpy array, dask array, or anything with shape and dtype.

ArrayOrPyramid module-attribute #

ArrayOrPyramid: TypeAlias = ArrayLike | Sequence[ArrayLike]

One array, or a sequence of arrays (one per dataset/resolution level).

ShapeLike module-attribute #

ShapeLike: TypeAlias = tuple[int, ...]

The shape of an array.

ShapeAndDType module-attribute #

ShapeAndDType: TypeAlias = tuple[ShapeLike, DTypeLike]

A tuple of (shape, dtype) describing an array.

ShapeAndDTypeOrPyramid module-attribute #

ShapeAndDTypeOrPyramid: TypeAlias = (
    ShapeAndDType | Sequence[ShapeAndDType]
)

One (shape, dtype) tuple, or a sequence of them (one per dataset).

ZarrWriter module-attribute #

ZarrWriter: TypeAlias = WriterName | 'CreateArrayFunc'

A built-in array backend, by name, or a custom array-creating function.

WriterName module-attribute #

WriterName = Literal['zarr', 'tensorstore', 'auto']

The name of a built-in array backend ("auto": tensorstore, if installed).

CompressionName module-attribute #

CompressionName = Literal[
    "blosc-zstd", "blosc-lz4", "zstd", "none"
]

The compression codec for the arrays.

AnyZarrArray module-attribute #

AnyZarrArray: TypeAlias = (
    zarr.Array | tensorstore.TensorStore
)

An array created by one of the built-in backends.

Image module-attribute #

Image = TypeVar('Image')

The Image model of the builder's version (v05.Image or v06.Image).

LabelImage module-attribute #

LabelImage = TypeVar('LabelImage')

The LabelImage model of the builder's version.

ImageWithDatasets module-attribute #

ImageWithDatasets: TypeAlias = tuple[Image, ArrayOrPyramid]

An Image model, with its array data (one array per dataset).

ImageWithShapeSpecs module-attribute #

ImageWithShapeSpecs: TypeAlias = tuple[
    Image, ShapeAndDTypeOrPyramid
]

An Image model, with the (shape, dtype) of each of its datasets.