Skip to content

striqt.sensor

Sweep execution against a bound sensor, and readers for sweep specs and calibration data.

iterate_sweep

iterate_sweep(
    resources: Resources[SS, SP, SC, SA, PS, PC],
    *,
    always_yield: bool = False,
    yield_values: bool = True,
    loop: bool = False,
    captures: Iterable[SC] | None = None,
    **replace: Unpack[
        AnyResources[SS, SP, SC, SA, PS, PC]
    ],
) -> Generator[Dataset | DelayedDataset | None]

run the iterate of resources['runner'] over resources.

Parameters:

  • resources (Resources[SS, SP, SC, SA, PS, PC]) –

    the open resources of open_resources

  • always_yield (bool, default: False ) –

    with yield_values False, True yields None at every step and False yields nothing

  • yield_values (bool, default: True ) –

    True yields the sink result of every step

  • loop (bool, default: False ) –

    True cycles through the captures from the first after the last

  • captures (Iterable[SC] | None, default: None ) –

    the captures to acquire in place of the loop expansion of the sweep, finite or endless; None acquires the expansion

  • **replace (Unpack[AnyResources[SS, SP, SC, SA, PS, PC]], default: {} ) –

    entries that replace those of resources for this run

Yields:

  • Generator[Dataset | DelayedDataset | None] –

    the return value of sink.append for each delivered acquisition, or None

  • Generator[Dataset | DelayedDataset | None] –

    at the steps before the first sink write. With yield_values False, None at

  • Generator[Dataset | DelayedDataset | None] –

    every step or nothing as always_yield selects. The iteration never ends

  • Generator[Dataset | DelayedDataset | None] –

    when loop is True or captures is endless.

Raises:

  • ValueError –

    if captures is given with loop True, or if the runner rejects captures

open_resources

open_resources(
    spec: ActionSpec[SS, SP, SA],
    spec_path: str | Path | None = None,
    *,
    test_only: bool = False,
    on_source_opened: SourceOpenCallback | None = None,
    on_analyzed: AnalyzedCallback | None = None,
    on_sink_cached: CachedCallback | None = None,
    select_analysis: AnalysisSelector | None = None,
) -> ConnectionManager[SS, SP, SC, SA, PS, PC]

open the sensor hardware and software contexts needed to run spec.

The source, peripherals, sink, calibration file, and log file open concurrently for the sweep of the action runner built from spec. The compute warmup runs in the calling thread. The returned ConnectionManager yields the open Resources on entry and closes every resource on exit. An exception from a hook ends the run. Progress is logged at INFO on the sweep logger.

Parameters:

  • spec (ActionSpec[SS, SP, SA]) –

    the action spec

  • spec_path (str | Path | None, default: None ) –

    the file from which spec was read, or None; the process working directory becomes the directory of the file, and the {spec_path} path field is set

  • test_only (bool, default: False ) –

    if True, open NoPeripherals in place of the peripherals and skip the compute warmup

  • on_source_opened (SourceOpenCallback | None, default: None ) –

    hook run once with (sweep, source_id) in a worker thread after the source opens and before the first capture

  • on_analyzed (AnalyzedCallback | None, default: None ) –

    hook run in the analysis thread with the DelayedDataset of each acquisition, before the sink receives the dataset; the extra_data of the dataset lacks the peripheral entries

  • on_sink_cached (CachedCallback | None, default: None ) –

    hook run in the analysis thread with (resources, cache, capture, result, **kwargs) each time a measurement fills a KwArgCache

  • select_analysis (AnalysisSelector | None, default: None ) –

    hook run in the analysis thread with (capture, analysis) before each analysis, returning the group to evaluate in place of the adjusted analysis; a sink that allocates a store from the full group rejects the narrowed result

Raises:

  • TypeError –

    if no action runner is registered for the type of spec

  • KeyError –

    if no sensor binding is registered for the type of spec

  • ExceptionGroup –

    if more than one resource fails to open; a single failure raises that exception

open_store

open_store(
    spec: ZarrSink,
    *,
    format_path: PathFormatter | None = None,
    force=False,
) -> ZarrStore

open a new zarr store at spec.path for writing.

A zip store is opened at a .zarr.zip path when spec.path ends in .zip or spec.store is 'zip'. Otherwise a directory store is opened at a .zarr path. Missing parent directories are created.

Parameters:

  • spec (ZarrSink) –

    the sink block with the path and store type

  • format_path (PathFormatter | None, default: None ) –

    expands the {fields} of spec.path; None leaves the path as is

  • force –

    if True, a store already at the path is removed; if False, an occupied path is rejected

Raises:

  • ValueError –

    if spec.path is None

  • FileExistsError –

    if a non-empty store is at the path and force is False

read_calibration

read_calibration(
    path: None, format_path: PathFormatter | None = None
) -> None
read_calibration(
    path: str | Path,
    format_path: PathFormatter | None = None,
) -> Dataset
read_calibration(
    path: str | Path | None,
    format_path: PathFormatter | None = None,
) -> Dataset | None

read the Y-factor corrections dataset at path into memory.

The dataset is loaded and the file handle is closed before return. Repeated calls with equal arguments return the cached dataset.

Parameters:

  • path (str | Path | None) –

    the netCDF file written by save_calibration, or None

  • format_path (PathFormatter | None, default: None ) –

    the formatter of the {field} templates in path, or None to open path as written

Returns:

  • Dataset | None –

    the loaded Dataset, or None when path is None

Raises:

  • FileNotFoundError –

    if the formatted path does not exist

read_json_spec

read_json_spec(
    path: str | Path,
    *,
    type: type[ActionSpec] | None = None,
    output_path: Optional[str] = None,
    store_backend: Optional[str] = None,
) -> ActionSpec

decode the action spec in the json file at path.

The file has the schema of a yaml spec without !include directives. The extensions block of the file is imported before decoding. A relative extensions.import_path is resolved against the directory of the file.

Parameters:

  • path (str | Path) –

    the json file

  • type (type[ActionSpec] | None, default: None ) –

    the action spec type to decode into, or None to select the type bound under the sensor_binding tag

  • output_path (Optional[str], default: None ) –

    replaces sink.path in the returned spec when given

  • store_backend (Optional[str], default: None ) –

    replaces sink.store of a Sweep spec when given

Returns:

  • ActionSpec –

    an instance of type, or of the bound class when type is None

Raises:

  • ValidationError –

    if the file does not validate as the spec class, or if the tag names no registered binding

  • TypeError –

    if store_backend is given and the sink of the spec is not a ZarrSink

  • FileNotFoundError –

    if extensions.import_path does not exist

read_yaml_spec

read_yaml_spec(
    path: str | Path,
    *,
    type: type[ActionSpec] | None = None,
    output_path: Optional[str] = None,
    store_backend: Optional[str] = None,
) -> ActionSpec

decode the action spec in the yaml file at path.

The extensions block of the file is imported before decoding. A relative extensions.import_path is resolved against the directory of the file.

Parameters:

  • path (str | Path) –

    the yaml file

  • type (type[ActionSpec] | None, default: None ) –

    the action spec type to decode into, or None to select the type bound under the sensor_binding tag

  • output_path (Optional[str], default: None ) –

    replaces sink.path in the returned spec when given

  • store_backend (Optional[str], default: None ) –

    replaces sink.store of a Sweep spec when given

Returns:

  • ActionSpec –

    an instance of type, or of the bound class when type is None

Raises:

  • ValidationError –

    if the file does not validate as the spec class, or if the tag names no registered binding

  • TypeError –

    if store_backend is given and the sink of the spec is not a ZarrSink

  • FileNotFoundError –

    if extensions.import_path does not exist

read_zarr_spec

read_zarr_spec(
    path: str | Path,
    *,
    type: type[ActionSpec] | None = None,
    output_path: Optional[str] = None,
    store_backend: Optional[str] = None,
    extension_root: str | Path | None = None,
) -> ActionSpec

decode the sweep spec saved in the attrs of the zarr store at path.

The extensions block of the attrs is imported before decoding. Only the attrs that name fields of the sweep class, the sensor_binding tag, and mock_source are read. An extensions.sink attr is dropped with a DeprecationWarning.

Parameters:

  • path (str | Path) –

    the zarr store, a .zarr directory or .zarr.zip archive

  • type (type[ActionSpec] | None, default: None ) –

    the sweep class to decode against, or None to select the class bound under the saved sensor_binding tag; a saved mock_source attr selects the mock class of that binding instead

  • output_path (Optional[str], default: None ) –

    replaces sink.path in the returned spec when given

  • store_backend (Optional[str], default: None ) –

    replaces sink.store of a Sweep spec when given

  • extension_root (str | Path | None, default: None ) –

    the directory against which a relative extensions.import_path is resolved, or None for the directory of the store

Returns:

  • ActionSpec –

    an instance of type, or of the bound sweep class when type is None

Raises:

  • ValidationError –

    if the attrs do not validate as the sweep class, or if the tag names no registered binding

  • TypeError –

    if store_backend is given and the sink of the spec is not a ZarrSink

  • FileNotFoundError –

    if extensions.import_path or extension_root does not exist