linumpy.io#
OME-Zarr and related I/O helpers for linumpy.
Submodules#
Classes#
Custom ome_zarr.scale.Scaler class for handling downscaling up to 3D. |
|
Write OME-Zarr files to disk in a pyramidal format, chunk by chunk. |
|
OmeZarrWriter subclass that supports custom analysis-friendly resolution pyramids. |
Functions#
|
Create a zarr store inside a temporary directory. |
|
Create a list of coordinate transformation dicts for OME-Zarr pyramid levels. |
|
Generate the axes dictionary for up to 4 dimensions. |
|
Create directory at store_path, optionally removing an existing one. |
|
Validate n_levels such that it does not go beyond the volume shape. |
|
Save array to disk in OME-NGFF zarr format. |
|
Read OME-Zarr image at zarr_path and return the array and voxel size. |
Package Contents#
- linumpy.io.create_tempstore(dir=None, suffix=None)[source]#
Create a zarr store inside a temporary directory.
Defaults to creating the temp directory inside the current working directory (typically a Nextflow task scratch dir) rather than
/tmp, so leaked temp zarrs are reclaimed when the work dir is cleaned and do not pile up on small/tmppartitions.- Parameters:
- Return zarr_store:
Temporary ZarrStore.
- Return type:
- class linumpy.io.CustomScaler[source]#
Bases:
ome_zarr.scale.ScalerCustom ome_zarr.scale.Scaler class for handling downscaling up to 3D.
Only resize_image method is implemented. Interpolation is ALWAYS done using 1-st order (linear) interpolation.
- resize_image(image)[source]#
Resize a numpy array OR a dask array to a smaller array (not pyramid).
This method is the only method from the Scaler class called from ome_zarr.writer.write_image.
- Parameters:
image (numpy.ndarray | dask.array.Array)
- Return type:
- linear(base)[source]#
Downsample using
skimage.transform.resize()with linear interpolation.- Parameters:
base (numpy.ndarray)
- Return type:
- linumpy.io.create_transformation_dict(nlevels, voxel_size, ndims=3)[source]#
Create a list of coordinate transformation dicts for OME-Zarr pyramid levels.
Supports images up to 4 dimensions.
- linumpy.io.generate_axes_dict(ndims=3, unit='millimeter')[source]#
Generate the axes dictionary for up to 4 dimensions.
Dimensions are returned in order (c, z, y, x).
- linumpy.io.create_directory(store_path, overwrite=False)[source]#
Create directory at store_path, optionally removing an existing one.
- Parameters:
store_path (pathlib.Path)
overwrite (bool)
- Return type:
- linumpy.io.validate_n_levels(n_levels, shape, downscale_factor=2)[source]#
Validate n_levels such that it does not go beyond the volume shape.
- linumpy.io.save_omezarr(data, store_path, voxel_size=(0.001, 0.001, 0.001), chunks=(128, 128, 128), n_levels=5, overwrite=True)[source]#
Save array to disk in OME-NGFF zarr format.
Expected ordering for axes in data and scales is (c, z, y, x).
- Parameters:
data (numpy or dask array) – numpy or dask array to save as zarr.
store_path (str) – The path of the output zarr group.
voxel_size (tuple of n float, with n the number of dimensions.) – Voxel size in mm.
chunks (tuple of n int, with n the number of dimensions.) – Chunk size on disk.
n_levels (int) – Number of levels in Gaussian pyramid.
overwrite (bool) – Overwrite store_path if it already exists.
- Return zarr_group:
Resulting zarr group saved to disk.
- Return type:
- linumpy.io.read_omezarr(zarr_path, level=0)[source]#
Read OME-Zarr image at zarr_path and return the array and voxel size.
Loads image data for level in the pyramid.
- class linumpy.io.OmeZarrWriter(store_path, shape, chunk_shape, shards=None, dtype=np.float32, overwrite=True, downscale_factor=2, unit='millimeter')[source]#
Write OME-Zarr files to disk in a pyramidal format, chunk by chunk.
- Parameters:
store_path (pathlib.Path)
shape (tuple | collections.abc.Sequence)
chunk_shape (tuple | collections.abc.Sequence)
shards (tuple | None)
dtype (type | numpy.dtype)
overwrite (bool)
downscale_factor (int)
unit (str)
- fmt: ome_zarr.format.CurrentFormat#
- shape: collections.abc.Sequence#
- root: zarr.Group#
- zarray: zarr.Array#
- property dtype: numpy.dtype#
Data type of the underlying zarr array.
- Return type:
- class linumpy.io.AnalysisOmeZarrWriter(store_path, shape, chunk_shape, shards=None, dtype=np.float32, overwrite=True, downscale_factor=2, unit='millimeter')[source]#
Bases:
OmeZarrWriterOmeZarrWriter subclass that supports custom analysis-friendly resolution pyramids.
This class extends OmeZarrWriter to create pyramid levels at specific target resolutions (e.g., 10, 25, 50, 100 µm) instead of traditional power-of-2 downsampling. This is useful for creating output volumes optimized for downstream analysis at specific scales.
Example
>>> writer = AnalysisOmeZarrWriter("output.ome.zarr", shape, chunks, dtype=np.float32) >>> writer[:] = data # Write data at full resolution >>> writer.finalize(base_res, target_resolutions_um=[10, 25, 50, 100])
Notes
Use finalize() for traditional power-of-2 pyramids (inherited from OmeZarrWriter)
Use finalize_with_resolutions() for custom analysis-friendly resolutions
- Parameters:
store_path (pathlib.Path)
shape (tuple | collections.abc.Sequence)
chunk_shape (tuple | collections.abc.Sequence)
shards (tuple | None)
dtype (type | numpy.dtype)
overwrite (bool)
downscale_factor (int)
unit (str)
- finalize(res, n_levels=None, *, target_resolutions_um=(10, 25, 50, 100), make_isotropic=True)[source]#
Finalize the OME-Zarr with pyramid levels.
- Parameters:
res (list of float) – Base resolution in mm (e.g., [0.01, 0.01, 0.01] for 10 µm isotropic, or [0.0015, 0.01, 0.01] for anisotropic z=1.5µm, xy=10µm)
target_resolutions_um (list of float, optional) – Target resolutions in microns (default: [10, 25, 50, 100]). Ignored if n_levels is specified.
n_levels (int, optional) – If specified, uses traditional power-of-2 downsampling instead of custom resolutions (backward compatible with OmeZarrWriter).
make_isotropic (bool, optional) – If True (default), resamples anisotropic data to produce isotropic voxels at each target resolution. Each dimension is scaled independently to achieve the target resolution. If False, preserves the original aspect ratio by scaling all dimensions uniformly based on the finest (smallest) base resolution.
- Return type:
None
Notes
By default, creates pyramid levels at specific analysis-friendly resolutions (e.g., 10, 25, 50, 100 µm). If n_levels is provided, falls back to traditional power-of-2 downsampling for backward compatibility.
Examples
For anisotropic data with base resolution [1.5, 10, 10] µm targeting 25 µm:
- With make_isotropic=True (default):
Scale factors: [16.67, 2.5, 2.5] (per-dimension)
Output: isotropic 25 µm voxels
Shape aspect ratio changes
- With make_isotropic=False:
Scale factor: 16.67 (uniform, based on finest dimension 1.5 µm)
Output: anisotropic [25, 167, 167] µm voxels
Shape aspect ratio preserved