Skip to content

redsun.writers

Write a derived product against the store a run names.

Writer reads a run's documents for where each product goes: a key of the acquisition's store, or a store of its own beside it. Nothing is registered in a catalog.

Without acquire-zarr, importing this package raises ImportError naming redsun[zarr]. Writing beside an OME-Zarr image needs redsun[ome-zarr], named the same way on first use.

ZARR module-attribute

ZARR: Final = 'application/x-zarr'

Mimetype of a plain Zarr store.

OME_ZARR module-attribute

OME_ZARR: Final = 'application/x-ome-zarr'

Mimetype of a Zarr store written as OME-Zarr.

Writer

Writer()

Bases: DocumentRouter

Write a component's products against the stores a run names.

Declare each product before the run: declare gives its layout and store, derive takes both from the run's descriptor and stream_resource naming a source data key. The component forwards every document with writer(name, doc) and hands data over with append per frame or write for the whole product. A run's stop closes its streams and writes each product's metadata: the mapping given to write, and a redsun mapping with the run, source, store and time.

A store's stream opens on the first append or write, with every product of that store known by then. A product resolves against the innermost open run naming both its layout and its store, so a nested run sees what the run around it declared.

declare

declare(
    data_key: str,
    *,
    shape: tuple[int, ...],
    dtype: DTypeLike,
    store: str,
) -> None

Declare a product of frames of shape, a key of the Zarr store at store.

derive

derive(data_key: str, *, source: str) -> None

Declare a product laid out and stored as the run's source is.

append

append(data_key: str, data: NDArray[Any]) -> None

Append one frame, or a stack of frames, to data_key.

Raises:

Type Description
WriterError

If data_key was not declared, no open run named its layout and store, or it goes to a store of its own, written whole.

write

write(
    data_key: str,
    data: NDArray[Any],
    metadata: Mapping[str, Any] | None = None,
) -> str

Write the whole of data_key and return the URI it went to.

A key of the run's store is finished at the run's stop; a store of its own at once, with the layout of data.

Raises:

Type Description
WriterError

If data_key was not declared, no open run named its layout and store, or the run described its store with a mimetype no writer knows.

close

close() -> None

Finish every open stream and forget every run.

shutdown

shutdown() -> None

Close, so a session ending mid-run leaves its stores readable.

start

start(doc: RunStart) -> None

Open a run, the innermost until its stop.

descriptor

descriptor(doc: EventDescriptor) -> None

Record the layout of each derived source this stream describes.

stream_resource

stream_resource(doc: StreamResource) -> None

Record the store this resource names for its data key.

event

event(doc: Event) -> Event

Pass an event through; data arrives by append and write.

stop

stop(doc: RunStop) -> None

Close this run's streams and forget it.

ArrayShape dataclass

ArrayShape(shape: tuple[int, ...], dtype: dtype[Any])

Shape and dtype of one frame; a stream appends frames along a leading axis.

shape instance-attribute

shape: tuple[int, ...]

Size of one frame along each axis.

dtype instance-attribute

dtype: dtype[Any]

Type of the elements.

of classmethod

of(shape: tuple[int, ...], dtype: DTypeLike) -> ArrayShape

Return the layout with shape and dtype normalised.

check

check(data_key: str, data: NDArray[Any]) -> NDArray[Any]

Return data contiguous, as one frame or a stack of frames of this layout.

Raises:

Type Description
WriterError

If the trailing dimensions or the dtype differ from this layout.

WriterError

Bases: RuntimeError

Raised when a product cannot be written against a store.