resonance.api.data.writer¶
resonance.api.data.writer
¶
RunWriter(db_path, sample)
¶
Manages a SQLite connection to a beamtime database and writes scan data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
db_path
|
Path
|
Path to the beamtime SQLite database file. Created on first open if it does not exist. |
required |
sample
|
SampleMetadata
|
Sample to associate with runs written through this writer. If
|
required |
Attributes:
| Name | Type | Description |
|---|---|---|
_db_path |
Path
|
Resolved path to the SQLite file. |
_sample |
SampleMetadata
|
Sample metadata, with |
_conn |
Connection or None
|
Active database connection, or None when closed. |
_run_uid |
str
|
Hex UID of the currently open run, or empty string. |
_stream_uid |
str
|
Hex UID of the currently open stream, or empty string. |
_seq_num |
int
|
Event sequence counter within the current stream. |
_zarr_store |
Group or None
|
Open Zarr group for image storage, or None when closed. |
Source code in src/resonance/api/data/writer.py
open()
¶
Open the database connection and upsert the sample.
Creates the beamtime schema if the database does not yet exist. If
self._sample.id is None, the sample is looked up by name; if a
matching row exists its id is loaded, otherwise a new row is inserted.
A Zarr store is opened (or created) alongside the .db file at
{db_path.stem}.zarr/.
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the writer is already open ( |
DatabaseError
|
If the database file is corrupt or unreadable. |
Source code in src/resonance/api/data/writer.py
open_run(plan_name, *, metadata=None)
¶
Insert a new run row and return its UID.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
plan_name
|
str
|
Name of the scan plan, e.g. "en_scan". |
required |
metadata
|
dict[str, Any] or None
|
Arbitrary run-level metadata serialized to JSON. Defaults to an empty dict. |
None
|
Returns:
| Type | Description |
|---|---|
str
|
Hex UUID of the newly created run. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the writer is not open. |
Source code in src/resonance/api/data/writer.py
open_stream(name, data_keys)
¶
Insert a new stream row and return its UID.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Stream name, e.g. "primary". |
required |
data_keys
|
dict[str, Any]
|
Descriptor mapping field names to their metadata, serialized to JSON. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Hex UUID of the newly created stream. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the writer is not open or no run has been opened. |
Source code in src/resonance/api/data/writer.py
write_event(data, timestamps=None)
¶
Insert an event row and return its UID.
Events are not committed individually; the commit is deferred to
close_run for performance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
dict[str, float | int | str | bool]
|
Measured values keyed by field name. |
required |
timestamps
|
dict[str, float] or None
|
Per-field acquisition timestamps. Defaults to an empty dict. |
None
|
Returns:
| Type | Description |
|---|---|
str
|
Hex UUID of the newly inserted event. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the writer is not open, no run has been opened, or no stream has been opened. |
Source code in src/resonance/api/data/writer.py
write_image(event_uid, field_name, image)
¶
Append a 2-D image frame to the Zarr store and record a reference in SQLite.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event_uid
|
str
|
Hex UUID of the parent event row in the |
required |
field_name
|
str
|
Detector field name, e.g. |
required |
image
|
ndarray
|
2-D array of shape |
required |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the writer is not open ( |
RuntimeError
|
If no run has been opened ( |
RuntimeError
|
If no stream has been opened ( |
Notes
The Zarr store lives at {db_path.stem}.zarr/ alongside the .db
file. Frames are appended to the array at
runs/{run_uid}/{field_name} inside the store. Each call grows the
array by one frame along axis 0. The image_refs row stores the
zarr group path and the zero-based frame index so the frame can be
retrieved without scanning the full array. compression_codec
is recorded as "blosc" to document the Zarr default; actual
compression is controlled by zarr's compressor setting.
Source code in src/resonance/api/data/writer.py
close_run(*, exit_status='success')
¶
Finalize the current run and commit all pending events.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
exit_status
|
str
|
Final status string. Expected values are "success", "aborted", or "failed". Defaults to "success". |
'success'
|
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the writer is not open or no run has been opened. |
Source code in src/resonance/api/data/writer.py
close()
¶
Commit any remaining work and close the database connection.
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the writer is not open. |
Source code in src/resonance/api/data/writer.py
__enter__()
¶
__exit__(exc_type, exc, tb)
¶
Close the run and connection, propagating any exception.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
exc_type
|
type[BaseException] or None
|
Exception type if an exception occurred, otherwise None. |
required |
exc
|
BaseException or None
|
Exception instance if an exception occurred, otherwise None. |
required |
tb
|
object
|
Traceback object if an exception occurred, otherwise None. |
required |
Source code in src/resonance/api/data/writer.py
IndexWriter(index_db_path)
¶
Writer for the master index database aggregating cross-beamtime metadata.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
index_db_path
|
Path
|
Path to the master index SQLite database file. |
required |
Notes
This class is a skeleton. All methods raise NotImplementedError until
the master index feature is implemented.
Source code in src/resonance/api/data/writer.py
ensure_schema()
¶
Create the index schema if it does not already exist.
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
Always; not yet implemented. |
Source code in src/resonance/api/data/writer.py
register_researcher(name, root_path, *, email=None, orcid=None, affiliation=None)
¶
Insert or retrieve a researcher row and return its id.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Full name of the researcher. |
required |
root_path
|
str
|
Filesystem root under which this researcher's data lives. |
required |
email
|
str or None
|
Contact email address. |
None
|
orcid
|
str or None
|
ORCID identifier string. |
None
|
affiliation
|
str or None
|
Institutional affiliation. |
None
|
Returns:
| Type | Description |
|---|---|
int
|
Primary key of the researcher row. |
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
Always; not yet implemented. |
Source code in src/resonance/api/data/writer.py
register_beamtime(researcher_id, label, db_path, *, time_start=None)
¶
Insert or retrieve a beamtime row and return its id.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
researcher_id
|
int
|
Foreign key into the |
required |
label
|
str
|
Human-readable beamtime label, e.g. "2026-03-05". |
required |
db_path
|
str
|
Path to the per-beamtime SQLite file. |
required |
time_start
|
float or None
|
Unix timestamp for the start of the beamtime session. |
None
|
Returns:
| Type | Description |
|---|---|
int
|
Primary key of the beamtime row. |
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
Always; not yet implemented. |
Source code in src/resonance/api/data/writer.py
index_run(uid, researcher_id, beamtime_id, plan_name, *, sample_name=None, time_start=None, tags=None)
¶
Insert a run summary row into the master index.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
uid
|
str
|
Hex UUID of the run, matching the per-beamtime |
required |
researcher_id
|
int
|
Foreign key into the |
required |
beamtime_id
|
int
|
Foreign key into the |
required |
plan_name
|
str
|
Name of the scan plan. |
required |
sample_name
|
str or None
|
Sample name denormalized from the per-beamtime database. |
None
|
time_start
|
float or None
|
Unix timestamp for the start of the run. |
None
|
tags
|
list[str] or None
|
Arbitrary string tags for filtering. |
None
|
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
Always; not yet implemented. |