Skip to content

How derived products are stored

A derived product is an array that a component computes from a run and keeps next to the data it came from, such as a median over a scan or a filtered copy of every frame. The device and its service write the run's own data; a derived product is the one thing redsun writes, through a Writer. Write a derived product shows every step.

What a writer receives

A Writer takes two kinds of input, from two directions: the run's documents, which tell it where the frames went and what shape they have, and the product itself, which the documents can't carry because the component computes it after the frames arrive:

What reaches a writer
the runThe RunEngine emits the run's documents while the camera and its service write the frames.componenta document callbackThe component receives the run's documents like any callback, and computes its product from the frames as they arrive.the camera's storeThe device and its service chose the format and the place, and wrote the frames there.WriterThe writer puts the product next to the frames, in the store the device chose. framesdocumentsdocuments,then the productthe productThe RunEngine emits the run's documents while the camera and its service write the frames. The component receives the run's documents like any callback, and computes its product from the frames as they arrive. The device and its service chose the format and the place, and wrote the frames there. The writer puts the product next to the frames, in the store the device chose.
the runThe RunEngine emits the run's documents while the camera and its service write the frames.componenta document callbackThe component receives the run's documents like any callback, and computes its product from the frames as they arrive.the camera's storeThe device and its service chose the format and the place, and wrote the frames there.WriterThe writer puts the product next to the frames, in the store the device chose. framesdocumentsdocuments,then the productthe productThe RunEngine emits the run's documents while the camera and its service write the frames. The component receives the run's documents like any callback, and computes its product from the frames as they arrive. The device and its service chose the format and the place, and wrote the frames there. The writer puts the product next to the frames, in the store the device chose.

Here is a component that keeps the median of a camera's frames, cut down to the lines that talk to its writer:

from typing import Any

from event_model import DocumentRouter, RunStop

from redsun.writers import Writer


class MedianPresenter(DocumentRouter):
    def __init__(self, name: str) -> None:
        super().__init__()
        self.name = name
        self._writer = Writer()
        self._writer.derive("camera_median", source="camera")

    def __call__(self, name: str, doc: dict[str, Any], validate: bool = False) -> Any:
        result = super().__call__(name, doc, validate)
        self._writer(name, doc)
        return result

    def stop(self, doc: RunStop) -> RunStop:
        self._writer.write("camera_median", self._median)
        return doc

self._median stands for what the component computed while the run went on; a product written frame by frame goes in through append instead. The session never subscribes a writer itself. The component forwards each document after handling it, so the writer sees the run's stop only after the component has written its result.

Where a product is stored

A product goes where its source data went, following the device's choice of format and store (ADR 0013). derive("camera_median", source="camera") lays the median out like one camera frame in the camera's store, and two documents of the run fill in the rest:

What derive takes from the run
camera_medianderive(source="camera")descriptornaming cameraThe run's description of the stream the camera belongs to.stream_resourcenaming cameraThe document the device emits when it opens the file its frames go to.the frame'sshape and dtypeA key the camera streams leads its shape with the frames per event, which derive drops, so the product is laid out like one frame.the store'sURI and formatThe format is the document's mimetype. The writer knows plain Zarr and OME-Zarr, and skips the product for a run whose store has another, logging it once. The run's description of the stream the camera belongs to. The document the device emits when it opens the file its frames go to. A key the camera streams leads its shape with the frames per event, which derive drops, so the product is laid out like one frame. The format is the document's mimetype. The writer knows plain Zarr and OME-Zarr, and skips the product for a run whose store has another, logging it once.
camera_medianderive(source="camera")descriptornaming cameraThe run's description of the stream the camera belongs to.stream_resourcenaming cameraThe document the device emits when it opens the file its frames go to.the frame'sshape and dtypeA key the camera streams leads its shape with the frames per event, which derive drops, so the product is laid out like one frame.the store'sURI and formatThe format is the document's mimetype. The writer knows plain Zarr and OME-Zarr, and skips the product for a run whose store has another, logging it once. The run's description of the stream the camera belongs to. The document the device emits when it opens the file its frames go to. A key the camera streams leads its shape with the frames per event, which derive drops, so the product is laid out like one frame. The format is the document's mimetype. The writer knows plain Zarr and OME-Zarr, and skips the product for a run whose store has another, logging it once.

For a product the run says nothing about, declare gives the shape, the type and the store up front instead:

self._writer.declare("mask", shape=(512, 512), dtype="uint8", store=store_uri)

Declaring products up front

Both derive and declare belong in the constructor, before the run starts, because the stores can't grow new arrays later:

A product over one run
componentWriterthe camera's store derive, in the constructordocuments: start,descriptor, stream_resourcefirst append or writeopen the stream:size every arrayappend a product declaredsince: WriterErrordocuments: stopclose the stream,write the metadata
componentWriterthe camera's store derive, in the constructordocuments: start,descriptor, stream_resourcefirst append or writeopen the stream:size every arrayappend a product declaredsince: WriterErrordocuments: stopclose the stream,write the metadata

A product can't join a store whose stream is open. For the same reason, a product that gets a store of its own, beside an OME-Zarr image, is written all at once rather than frame by frame: write opens and finishes that store in one call, and append to it is refused.