Image I/O API
Lumic supports metadata-first inspection and explicit pixel reads for CZI and
OME-TIFF files. Open a reader when you want to inspect axes, dimensions, or
metadata before decoding; use a load_* function when you want an in-memory
SpectralImage immediately.
Generic entry points
open_image dispatches by filename extension: .czi files use the CZI reader
and .tif/.tiff files use the OME-TIFF reader. TIFF files must have supported
OME metadata; Lumic does not guess the meaning of plain TIFF files. The generic
functions return or load through the existing format-specific readers, whose
signatures remain available below.
from lumic.io import load_image, open_image
with open_image("acquisition.czi") as reader:
print(reader.summary()) # Header information only; no pixel array loaded.
image = reader.read(t=0, z=2)
# Convenience form: read the default first T/Z plane and close the reader.
image = load_image("acquisition.ome.tif")
Calling open_image validates the path and reads the format header and layout.
It does not decode pixel data. reader.read(...) decodes the requested pixels
into an independent SpectralImage, which remains usable after the reader is
closed. A with block closes the reader on exit, including when reading raises
an exception. If you keep a reader open without a context manager, call
close() yourself.
By default, read() and the load_* helpers select t=0, z=0 when those axes
exist. Pass t=None and/or z=None to retain all positions along that axis;
the result then keeps the corresponding T/Z axes. Use summary(), axes, and
shape to inspect available dimensions before choosing a selection.
open_image(path, *, series=None)
Inspect a supported image using its existing format-specific reader.
Dispatch .czi to open_czi and .tif/.tiff to open_ome_tiff. TIFF inputs must contain supported OME metadata; a plain TIFF is not guessed to be spectral. Filename suffixes select the adapter, which validates contents and layout without decoding pixels. Unsupported extensions, invalid files and unsupported layouts raise clear errors rather than returning False.
The returned object IS the format-specific reader, not a wrapper. Its summary(), axes, shape and optional metadata can be inspected before read(t=0, z=0) explicitly loads pixels. Use a context manager to close it. Summary support does not establish suitability for spectral unmixing.
series=None selects the default first OME image. An explicit series index is an OME-TIFF-only option and raises ValueError for CZI, including zero; this API does not silently interpret it as CZI scene selection. Use the format-specific functions directly whenever their explicit names are useful. No format-specific function's signature or behavior is changed.
Example: with open_image(path) as reader: print(reader.summary()). Opening and displaying the summary does not prompt or load pixel arrays.
load_image(path, *, t=0, z=0, series=None)
Load a supported CZI or OME-TIFF into SpectralImage, then close its reader.
Convenience form of open_image(path, series=series).read(t=t, z=z). Defaults to the first T/Z plane; use None to retain entire dimensions. Axis order, dtype, metadata and validation follow the selected adapter. Existing load_czi and load_ome_tiff calls remain available unchanged. For inspection without pixel loading, use open_image and summary() first.
Format-specific entry points
Use these functions when the format is known or when the format-specific
options are useful. CZI has no series option. OME-TIFF supports selecting a
series (default 0). Each open_* function returns its native reader; each
load_* function reads selected pixels and closes that reader before returning.
open_czi(path)
Open metadata for selective CZI reads; use as a context manager.
Example: with open_czi(path) as reader: image = reader.read(t=0, z=2).
See :class:CziReader for supported layouts and metadata limitations.
load_czi(path, *, t=0, z=0)
Read CZI pixels and metadata into a SpectralImage, then close the file.
Defaults to the first T/Z plane (CYX), including files without T/Z axes.
Set t=None and/or z=None explicitly to load entire dimensions. Use
open_czi to inspect available shape first. Returned arrays remain usable
after the reader closes; explicitly requested stacks retain their axes.
Detection bands are retained, and overlapping windows have no midpoint
wavelength axis. This does not reconstruct LSM 900 sequential-window data.
open_ome_tiff(path, *, series=0)
Inspect an OME-TIFF without decoding pixels; use as a context manager.
Example: with open_ome_tiff(path) as reader: image = reader.read(z=2).
See OmeTiffReader for supported layouts and metadata interpretation.
load_ome_tiff(path, *, t=0, z=0, series=0)
Return a SpectralImage from selected OME-TIFF pixels and close the file.
Defaults to t=0, z=0 (normally CYX); use None to retain all positions. Lumic exports restore their original array axis order and metadata, including explicit singleton T/Z axes when requested with None. Other files follow their logical TIFF series order. Channel metadata is optional; band bounds are not inferred from labels. Use open_ome_tiff to inspect before decoding.
Reader contract
Both format readers implement the SpectralImageReader protocol. Its
summary() presents validated header metadata without loading pixels;
read(t=..., z=...) returns an independent SpectralImage; close() releases
the reader's resources and is safe to call repeatedly. Readers are context
managers, so prefer with open_image(...) as reader: when reading selectively.
The axes, shape, and optional calibration and wavelength attributes
describe available metadata. A successful header check does not guarantee
that every pixel block is readable or that the data are scientifically
suitable for spectral unmixing.
SpectralImageReader
Bases: Protocol
Interface for metadata-first readers, independent of their backend.
Construction validates file/header/layout without decoding image pixels.
axes labels the available array dimensions; shape follows that order.
Readers expose C/Y/X (detector data) or K/Y/X (abundance results), optional
T/Z, and optional channel/calibration
metadata. Missing metadata uses None/empty mapping rather than guesses.
read() loads the first T/Z plane by default. None requests every position along that dimension. A request selects before decoding when the backend allows it; adapters must disclose any less selective behavior. Results own their pixels and remain usable after close(). No unmixing is performed.
This Protocol documents structural compatibility for contributors/type checkers, not scientific validation. It is not a superclass requirement, plugin registry, or runtime substitute for validating the actual file.
path
instance-attribute
format_name
instance-attribute
kind
instance-attribute
axes
instance-attribute
shape
instance-attribute
wavelengths_nm
instance-attribute
detection_bands_nm
instance-attribute
pixel_sizes_um
instance-attribute
summary()
Return shared human-readable header information without pixel I/O.
read(*, t=0, z=0)
Load requested pixels into an independent SpectralImage.
close()
Release resources; repeated calls must be safe.
__enter__()
__exit__(*exc)
COS-7 example data
download_cos7_data fetches curated public CZI acquisitions from BioImage
Archive accession S-BIAD4448. By default it downloads the high-SNR mixed
acquisition (about 145 MB). include_references=True adds the single-stain
reference acquisitions; snr="all" includes high, medium, and low conditions.
Files are stored under ~/.lumic/data/bioimage-archive/S-BIAD4448 unless
data_dir is supplied. Existing complete files are reused.
from lumic.data import download_cos7_data
from lumic.io import open_image
path, = download_cos7_data(snr="high", progress=True)
with open_image(path) as reader:
print(reader.summary())
Downloading all conditions with references requires about 1.74 GB.
download_cos7_data(*, snr='high', include_references=False, data_dir=None, progress=False)
Fetch selected public COS-7 CZI acquisitions from BioImage Archive.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
snr
|
(high, medium, low, all)
|
Acquisition condition to download. "all" selects high, medium, and low in that order. |
"high"
|
include_references
|
bool
|
Also download the three single-stain acquisitions for each selected condition. Each mixed acquisition is listed before its references. |
False
|
data_dir
|
str or Path
|
Local directory for this accession, with one subfolder per SNR condition. Defaults to ~/.lumic/data/bioimage-archive/S-BIAD4448. |
None
|
progress
|
bool
|
Print percentage updates for each download and a message for each file reused from disk. |
False
|
Returns:
| Type | Description |
|---|---|
list of pathlib.Path
|
Local CZI paths. Existing files matching the archive's byte sizes are reused without accessing the network. Incomplete files are replaced only after a successful download and size check. |
Notes
The COS-7 data are available under CC0 from BioImage Archive accession S-BIAD4448. A single CZI is about 145 MB. Downloading all conditions with their single-stain references takes about 1.74 GB.