Reader

Reads an OME-Zarr 0.5 image. For a plate, open one of its fields.

class ome_zarr_io.Reader(path)

Reads and validates an OME-Zarr 0.5 image (a Zarr group with multiscales).

Open an existing OME-Zarr image for reading.

Parameters:

path (str | Path) – Path to the image group (the directory containing zarr.json). For a plate, pass one of its fields, e.g. plate.zarr/A/1/0.

Raises:

FileNotFoundError – If no zarr group exists at path.

validate(strict=False)

Validate the image (and any attached labels) against the OME-Zarr 0.5 schema.

Parameters:

strict (bool) – If True, validate against the strict_image/strict_label schemas instead of the permissive image/label schemas.

Returns:

True if the image is valid.

Raises:

jsonschema.exceptions.ValidationError – If validation fails.

Return type:

bool

get_validation_errors(strict=False)

Return a list of validation error messages instead of raising.

Parameters:

strict (bool)

Return type:

List[str]

report(strict=False)

Return a structured FilesetReport (validity plus a summary of the image).

Unlike validate(), this never raises for invalid metadata; see ome_zarr_io.validate for details.

Parameters:

strict (bool)

Return type:

FilesetReport

property channel_names: List[str]

Names of channels defined in the OMERO metadata, in axis order.

get_channel(name, level=0, as_type='dask')

Get image data for a single channel, selected by name.

Parameters:
  • name (str) – Channel name, matched against omero.channels[i].label.

  • level (int) – Multiscale pyramid level to read (0 = full resolution; negative values count from the coarsest level).

  • as_type (Literal['dask', 'numpy']) – Return a “dask” (lazy) or “numpy” (in-memory) array.

Returns:

Array with the channel (‘c’) axis removed.

Raises:
  • KeyError – If no channel with that name exists, or the array listed for level is missing.

  • ValueError – If the image has no channel (‘c’) axis.

  • IndexError – If level is out of range.

Return type:

Array | ndarray

resample(array, from_level, to_level, *, kind='image', method=None, as_type=None)

Resample an array from the grid of one pyramid level to another’s.

Typical use is to read data at one level, process it, and bring the result onto the grid of another level:

out = reader.resample(my_filter(reader.get_channel("DAPI", 2)), 2, 0)
lbl = reader.resample(reader.get_label("nuclei", 0), 0, 3, kind="label")

Resampling factors come from the ratio of the levels’ scale transforms, and the output has exactly the stored shape of to_level along spatial axes (excess is cropped, a shortfall is edge-padded). Axes other than z/y/x keep their size, and leading axes may be missing (e.g. the ‘c’ axis that get_channel removes). Levels refer to the image’s pyramid.

Parameters:
  • array (Array | ndarray) – NumPy or Dask array shaped like from_level.

  • from_level (int) – Pyramid level the array is on (negative counts from the end).

  • to_level (int) – Pyramid level to resample to.

  • kind (Literal['image', 'label']) – “label” always uses nearest neighbour so label ids are never invented; “image” is for intensity data.

  • method (Literal['nearest', 'mean', 'linear'] | None) – “nearest”, “mean” (downscaling only) or “linear” (upscaling only). Default: “nearest” for labels; “mean” down and “linear” up for images.

  • as_type (Literal['dask', 'numpy'] | None) – “dask” or “numpy”. Default: the type of array.

Raises:
  • TypeError – If a level is not an int.

  • IndexError – If a level is out of range.

  • ValueError – If array does not match the shape of from_level, the levels are not related by an integer factor, or method does not fit kind or the direction of resampling.

Return type:

Array | ndarray

property label_names: List[str]

Names of label images attached under the labels/ subgroup.

get_label(name, level=0, as_type='dask')

Get a label image by name.

Parameters:
  • name (str) – Label name as listed in label_names.

  • level (int) – Multiscale pyramid level of the label to read (0 = full resolution; negative values count from the coarsest level).

  • as_type (Literal['dask', 'numpy']) – Return a “dask” (lazy) or “numpy” (in-memory) array.

Raises:
  • KeyError – If no label with that name exists, or the array listed for level is missing.

  • ValueError – If the label has no valid multiscales metadata.

  • IndexError – If level is out of range.

Return type:

Array | ndarray

property dims: List[str]

Axis names (e.g. [‘t’, ‘c’, ‘z’, ‘y’, ‘x’]) for the base image.

property axes: List[Axis]

Axis metadata objects (name/type/unit) for the base image.

property n_levels: int

Number of multiscale pyramid levels available.

property level_transforms: List[LevelTransform]

The coordinate transform of every pyramid level, finest first.

get_level_transform(level)

Get the physical coordinate transform (metadata only) of one pyramid level.

This does not read any image data; use get_channel(..., level=) for that.

Parameters:

level (int) – Integer index of the pyramid level (0 is the full resolution).

Returns:

A LevelTransform with the composed scale and translation, the axis units, the voxel size and the downsampling factor relative to level 0.

Raises:
Return type:

LevelTransform

get_physical_size(level=0)

Get the physical pixel/voxel size for each spatial and time axis.

See get_level_transform for a richer result that also has translation and the downsampling factor.

Parameters:

level (int) – Multiscale pyramid level to compute sizes for.

Returns:

Mapping of axis name (e.g. “z”, “y”, “x”, “t”) to a PhysicalSize, for every non-channel axis.

Raises:

IndexError – If level is out of range.

Return type:

Dict[str, PhysicalSize]

get_voxel_size(level=0)

Get the spatial (‘z’, ‘y’, ‘x’) pixel/voxel size at the given level.

Raises:

ValueError – If a spatial axis has no unit assigned in metadata.

Parameters:

level (int)

Return type:

Dict[str, float]

class ome_zarr_io.PhysicalSize(value, unit)

Physical size of a single axis (e.g. pixel/voxel spacing).

Parameters:
value: float
unit: str | None