Storage¶
Writer¶
Abstract base class for storage backend writers.
Writers are long-lived singletons, created once when the application
starts and reused across every acquisition. The class-level registry
is keyed by a composite key (name, mimetype) where:
mimetypeidentifies the storage format and determines whichWritersubclass handles serialisation (e.g."application/x-zarr"maps toZarrWriter).nameis a store group name that distinguishes multiple independent stores that share the same format. Use"default"for the common single-store case. This is not a device name — many devices may contribute sources to the same writer by referencing the same(name, mimetype)key.
Example keys:
("default", "application/x-zarr") # the normal single-store case
("live", "application/x-zarr") # a separate live-preview store
("default", "application/x-hdf5") # a different format entirely
URI setting is controlled by the Presenter layer
via set_uri.
Subclasses must override set_uri to perform any backend-specific path
translation without disturbing the registry.
Call order per acquisition:
set_uri(uri)- called by a presenter before the plan
register(source_name, data_key, dtype, shape, capacity)- called by each device in its own
prepare() - returns a
FrameSink
- called by each device in its own
kickoff()- opens the backend
sink.write(frame)- push frames (thread-safe)
sink.close()- signals completion for this source
Subclasses must implement the following methods and properties:
mimetype— MIME type stringkickoff- open the backend
- must call
super().kickoff()to setis_openand enforce the URI guard
_on_prepare(private)- backend-specific setup after a source is registered (e.g. pre-declare Zarr array
dimensions); called at the end of
prepare
- backend-specific setup after a source is registered (e.g. pre-declare Zarr array
dimensions); called at the end of
_write_frame- write one frame to the backend
_finalize- close the backend when all sources are done
_class_mimetype(class method)- return the MIME type string for this subclass; used for registry keys
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Store group name. See class docstring for semantics. |
required |
Source code in src/redsun/storage/_base.py
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 | |
mimetype
property
¶
The MIME type string for this backend.
Must return the same value as
_class_mimetype.
get
classmethod
¶
Return the singleton writer for (name, cls.mimetype).
Creates a new instance on first call; returns the existing one
on every subsequent call for the same (name, mimetype) pair.
The URI is not set here — call
set_uri separately.
This method is normally not called directly by devices or
application code. Use
[make_writer][redsun.storage.make_writer] instead, which
resolves the correct subclass from the mimetype string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Store group name. Defaults to |
'default'
|
Returns:
| Type | Description |
|---|---|
Writer
|
Singleton instance for |
Raises:
| Type | Description |
|---|---|
TypeError
|
If the existing registry entry is not an instance of |
Source code in src/redsun/storage/_base.py
release
classmethod
¶
Remove the registry entry for (name, cls.mimetype).
Called by a presenter at application shutdown. Devices should not call this directly.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Store group name. Defaults to |
'default'
|
Source code in src/redsun/storage/_base.py
_class_mimetype
abstractmethod
classmethod
¶
Return the MIME type string for this subclass.
Used by get and
release to build the registry
key before any instance exists. Must return the same value as
the instance property mimetype.
Subclasses implement this as a one-liner::
@classmethod
def _class_mimetype(cls) -> str:
return "application/x-zarr"
Source code in src/redsun/storage/_base.py
set_uri
¶
Update the store URI for the next acquisition.
Called by a presenter before each acquisition and whenever the user changes the output directory. The writer must not be open when this is called.
Subclasses should override this to perform any backend-specific
path translation (e.g. rebuilding StreamSettings.store_path
for ZarrWriter) and must call super().set_uri(uri).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
uri
|
str
|
New store URI (e.g. |
required |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the writer is currently open. |
Source code in src/redsun/storage/_base.py
prepare
¶
prepare(
name: str,
data_key: str,
dtype: dtype[generic],
shape: tuple[int, ...],
capacity: int = 0,
) -> FrameSink
Register a data source and return a ready FrameSink.
Called by each device inside its own prepare() method, once
per acquisition. Replaces the former two-step
update_source + prepare sequence.
Safe to call multiple times on the same source name — counters are reset on each call, making it suitable for repeated acquisitions without recreating the writer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Source name, typically the device name. Multiple devices may register distinct source names on the same writer. |
required |
data_key
|
str
|
Bluesky data key used in stream documents. |
required |
dtype
|
dtype[generic]
|
NumPy data type of the frames. |
required |
shape
|
tuple[int, ...]
|
Shape of each individual frame. |
required |
capacity
|
int
|
Maximum number of frames to accept. |
0
|
Returns:
| Type | Description |
|---|---|
FrameSink
|
Bound sink ready to accept frames via |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the writer is currently open. |
Source code in src/redsun/storage/_base.py
_on_prepare
abstractmethod
¶
Backend-specific hook called after a source is registered.
Invoked at the end of prepare
once self._sources[name] is fully populated. Subclasses use
this to pre-declare backend structures — for example, ZarrWriter
builds its ArraySettings here from the source dtype and shape.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The source name just registered. |
required |
Source code in src/redsun/storage/_base.py
get_indices_written
¶
Return the number of frames written for a source.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str | None
|
Source name. If |
None
|
Raises:
| Type | Description |
|---|---|
KeyError
|
If name is not registered. |
Source code in src/redsun/storage/_base.py
reset_collection_state
¶
Reset the stream-document counters for name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Source name to reset. |
required |
Source code in src/redsun/storage/_base.py
kickoff
abstractmethod
¶
Open the storage backend for a new acquisition.
Subclasses must call super().kickoff() to set
is_open and to enforce the
URI guard. Subsequent calls while already open must be no-ops.
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If |
Source code in src/redsun/storage/_base.py
complete
¶
Mark acquisition complete for source name.
Called automatically by FrameSink.close.
When the last registered source calls complete, the backend
is finalised and is_open is reset. The writer instance
remains in the registry and is ready for the next acquisition.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Source name signalling completion. |
required |
Source code in src/redsun/storage/_base.py
clear_sources
¶
Remove all registered sources.
A presenter in charge of monitoring writing progress should take care to call this after each plan is finished.
Source code in src/redsun/storage/_base.py
_write_frame
abstractmethod
¶
Write one frame to the backend.
Called by FrameSink.write
under the writer lock.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Source name. |
required |
frame
|
NDArray[generic]
|
Frame data to write. |
required |
Source code in src/redsun/storage/_base.py
_finalize
abstractmethod
¶
collect_stream_docs
¶
Yield StreamResource and StreamDatum documents for name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Source name. |
required |
indices_written
|
int
|
Number of frames to report in this call. |
required |
Yields:
| Type | Description |
|---|---|
StreamAsset
|
Tuples of |
Raises:
| Type | Description |
|---|---|
KeyError
|
If name is not registered. |
Source code in src/redsun/storage/_base.py
Device-facing handle for pushing frames to a storage backend.
Returned by [Writer.register][redsun.storage.Writer.register].
Devices write frames by calling write;
the sink routes each frame to the backend and updates the frame counter
atomically.
Calling close signals that no more
frames will arrive from this source and triggers backend finalisation
once all sources are complete.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
writer
|
Writer
|
The writer that owns this sink. |
required |
name
|
str
|
Source name this sink is bound to. |
required |
Source code in src/redsun/storage/_base.py
write
¶
Push frame to the storage backend.
Thread-safe.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
frame
|
NDArray[generic]
|
Array data to write. dtype and shape must match those declared
in [ |
required |
Source code in src/redsun/storage/_base.py
close
¶
Signal that no more frames will be written from this sink.
Delegates to Writer.complete.
Path providers¶
PathInfo
dataclass
¶
Where and how a storage backend should write data for one device.
Note
For local file storage, the store_uri must be converted to
a concrete filesystem path before use. This is the responsibility
of the backend writer.
Source code in src/redsun/storage/_path.py
array_key
instance-attribute
¶
Key (array name) within the store for this device's data.
capacity
class-attribute
instance-attribute
¶
Capacity in frames. 0 means unlimited.
mimetype_hint
class-attribute
instance-attribute
¶
MIME type hint for the backend. Consumers may use this to select the correct reader.
extra
class-attribute
instance-attribute
¶
Optional backend-specific metadata (e.g. OME-Zarr axis labels, physical units). Base writers ignore this field.
FilenameProvider
¶
Bases: Protocol
Callable that produces a filename stem for a given key.
Source code in src/redsun/storage/_path.py
__call__
¶
Return a filename stem for key and group.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str | None
|
Discriminator passed by the caller — typically a plan name (when called from a presenter) or a device name (when called from a writer). Implementations may ignore it if the filename is key-agnostic. |
None
|
group
|
str | None
|
Writer group name (e.g. |
None
|
Returns:
| Type | Description |
|---|---|
str
|
A filename stem without extension. |
Source code in src/redsun/storage/_path.py
PathProvider
¶
Bases: Protocol
Callable that produces PathInfo for a given key.
Source code in src/redsun/storage/_path.py
__call__
¶
Return path information for key and group.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str | None
|
Discriminator passed by the caller — typically a plan name (when called from a presenter) or a device name (when called from a writer). |
None
|
group
|
str | None
|
Writer group name (e.g. |
None
|
Returns:
| Type | Description |
|---|---|
[`PathInfo`][redsun.storage.PathInfo]
|
Complete path and storage metadata. |
Source code in src/redsun/storage/_path.py
SessionPathProvider
¶
Bases: PathProvider
Provides structured, session-scoped paths with per-key auto-increment counters.
Produces URIs of the form::
file:///<base_dir>/<session>/<YYYY_MM_DD>/<key>_<counter>
where <key> is the value passed to __call__ (e.g. the plan
name), and <counter> is a zero-padded integer that increments
independently for each distinct key. Calling with key=None
uses "default" as the key.
The date segment is fixed at construction time so that a session started just before midnight does not split its files across two date directories.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base_dir
|
Path | None
|
Root directory for all output files.
Defaults to |
None
|
session
|
str
|
Session name, used as the second path segment.
Defaults to |
'default'
|
max_digits
|
int
|
Zero-padding width for the counter. Defaults to |
5
|
mimetype_hint
|
str
|
MIME type hint forwarded to |
'application/x-zarr'
|
capacity
|
int
|
Default frame capacity forwarded to |
0
|
Examples:
provider = SessionPathProvider(base_dir=Path("/data"), session="exp1")
info = provider("live_stream")
info.store_uri # 'file:///data/exp1/2026_02_24/live_stream_00000'
provider("live_stream").store_uri # 'file:///data/exp1/2026_02_24/live_stream_00001'
provider("snap").store_uri # 'file:///data/exp1/2026_02_24/snap_00000'
provider("live_stream", group="cam").store_uri # 'file:///data/exp1/2026_02_24/live_stream_cam_00000'
Source code in src/redsun/storage/_path.py
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 | |
__call__
¶
Return a fresh PathInfo for key and advance its counter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str | None
|
Discriminator for the counter bucket — typically a plan name
(e.g. |
None
|
group
|
str | None
|
Writer group name (e.g. |
None
|
Returns:
| Type | Description |
|---|---|
PathInfo
|
Path rooted at
|
Source code in src/redsun/storage/_path.py
Metadata¶
Module-level metadata registry for device contributions.
Non-imaging devices (motors, lights, etc.) call
register_metadata
during their prepare() method to contribute acquisition-time metadata.
Writer backends snapshot the registry at Writer.kickoff
and clear it when the last source completes via Writer.complete.
register_metadata
¶
snapshot_metadata
¶
clear_metadata
¶
Clear the registry.
Called by Writer.complete after the last
source finalises, and by Writer.kickoff
if the backend fails to open.
Source code in src/redsun/storage/metadata.py
Clear the registry.
Called by Writer.complete after the last
source finalises, and by Writer.kickoff
if the backend fails to open.
Source code in src/redsun/storage/metadata.py
Factory & utilities¶
PrepareInfo
dataclass
¶
Plan-time information passed to device prepare() methods.
Warning
These are still experimental. New fields may be added or existing fields may change.
Source code in src/redsun/storage/__init__.py
make_writer
¶
Return the singleton writer for (name, mimetype).
Delegates to the appropriate Writer.get, depending on mimetype, so that all devices get the same writer instance for the same (name, mimetype).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mimetype
|
str
|
Backend format. Currently only |
required |
name
|
str
|
Store group name. All devices that should write into the same
physical store must use the same name. Defaults to
|
'default'
|
Returns:
| Type | Description |
|---|---|
Writer
|
Singleton writer instance for (name, mimetype). |
Raises:
| Type | Description |
|---|---|
ValueError
|
If mimetype is not a recognised format. |
Source code in src/redsun/storage/device.py
get_available_writers
¶
Return all registered writers grouped by mimetype.
Provides a presenter with a view of the current writer
registry, grouped for convenient iteration by format. The outer
key is the mimetype string (e.g. "application/x-zarr"); the
inner key is the store group name (e.g. "default").
Returns:
| Type | Description |
|---|---|
dict[str, dict[str, Writer]]
|
|
Source code in src/redsun/storage/presenter.py
from_uri
¶
Convert a URI to a filesystem path if local, otherwise return as-is.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
uri
|
str
|
The URI to convert. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The filesystem path if the URI is a local file URI, otherwise the original URI. |
Source code in src/redsun/storage/utils.py
Protocols¶
Storage-level protocols for redsun.
Defines structural protocols that devices can implement to declare an association with a storage writer.
HasWriter
¶
Bases: Protocol
Protocol for devices that are paired with a storage writer.
Implementing this protocol allows the storage presenter to discover the writer associated with a device automatically, without requiring explicit wiring in the application configuration.