spvctrl.measurement.measurement.MeasurementHandler#

class MeasurementHandler(settings: TspvSettings, osci: Oscilloscope, laser: LaserOpo | None = None, data_queue: Queue | None = None, command_queue: Queue | None = None, data_dir: str | Path | None = None, compress_data: bool = True, laser_wait_s: float = 0.0, setup_info: TspvSetupInfo | dict | None = None, sample: Sample | str | None = None, extra_metadata: dict | None = None)[source]#

Bases: object

Pulse-based measurement handler: acquires single-shot captures from the oscilloscope and accumulates their average.

run() performs the measurement and may be run in a worker thread. Communication with the main thread (live view) happens via queues:

  • command_queue: receives commands, currently only “stop”

  • data_queue: emits status tuples for the live view:

    (“status”, msg) - human-readable progress message (“pulse”, i, n, t_s, U_spv_V, U_diode_V) - after each acquired pulse, i is 1-based; the arrays are the

    split-log-downsampled transient of the full pulse record, including pre-trigger samples

    (“avg”, t_s, U_spv_V, U_diode_V) - running average, including pre-trigger samples (“warning”, msg) - warning message (“trigger_timeout”, msg) - a capture timed out and is retried (“done”, t_s, U_spv_V, U_diode_V) - final averaged data

U_diode_V is None in all messages when the diode channel is not recorded (single mode).

Class that performs the actual pulse acquisition.

Parameters:
  • settings – All measurement settings.

  • osci – Oscilloscope device.

  • laser – Laser device, or None to not control the laser.

  • data_queue – Queue where status messages are put in. Useful for live monitoring when run() is run in another thread.

  • command_queue – Queue used to receive a “stop” signal. Useful when run() is run in another thread.

  • data_dir – Cache directory where per-pulse and averaged data files are stored, or None to not save.

  • compress_data – Whether to gzip-compress the data files (with the byte-shuffle filter).

  • laser_wait_s – Time in s to wait for the laser (e.g. ramp-up after turning it on, or settle after a wavelength change) before the first acquisition. Resolve it with laser_wait_time_s() before applying the laser settings.

  • setup_info – Setup description (e.g. buffer, laser temperatures) recorded in the metadata.yaml, either as a TspvSetupInfo object or as a preprocessed dict, or None to not record setup info.

  • sample – Sample object (or its name, or None) recorded in the metadata.yaml.

  • extra_metadata – Additional metadata recorded top-level in the metadata.yaml (e.g. the sample capacitance or global measurement metadata from the GUI). Applied last, so it can override other entries.

Methods

prepare

Compute derived parameters and create the data directory.

run

Run the measurement.

save_metadata

Write the settings and measurement metadata to a metadata.yaml file in the data directory.

Attributes

extra_metadata

Additional metadata recorded top-level in the metadata.yaml, applied last (see __init__).

pulse_period_s

Actual pulse interval in s, or None if no frequency divider is set

osci_raw_config

Raw configuration dump of the oscilloscope (Gage CompuScope only), recorded in the metadata.

conversion_params

ADC-to-volt conversion parameters per channel, recorded in the metadata.

error

Error that stopped the measurement, e.g. an error while saving the data.

__annotations__ = {}#
__dict__ = mappingproxy({'__module__': 'spvctrl.measurement.measurement', '__firstlineno__': 74, '__doc__': 'Pulse-based measurement handler: acquires single-shot captures from the oscilloscope and accumulates their average.\n\n`run()` performs the measurement and may be run in a worker thread. \nCommunication with the main thread (live view) happens via queues:\n\n- `command_queue`: receives commands, currently only "stop"\n- `data_queue`: emits status tuples for the live view:\n    ("status", msg)                     - human-readable progress message\n    ("pulse", i, n, t_s, U_spv_V, U_diode_V) - after each acquired pulse, i is 1-based; the arrays are the\n                                          split-log-downsampled transient of the full pulse record, including pre-trigger samples\n    ("avg", t_s, U_spv_V, U_diode_V)    - running average, including pre-trigger samples\n    ("warning", msg)                    - warning message\n    ("trigger_timeout", msg)            - a capture timed out and is retried\n    ("done", t_s, U_spv_V, U_diode_V)   - final averaged data\n\n``U_diode_V`` is None in all messages when the diode channel is not recorded (single mode).\n', '__init__': <function MeasurementHandler.__init__>, '_put': <function MeasurementHandler._put>, '_put_status': <function MeasurementHandler._put_status>, '_put_avg': <function MeasurementHandler._put_avg>, '_put_warning': <function MeasurementHandler._put_warning>, 'prepare': <function MeasurementHandler.prepare>, '_check_pulse_interval': <function MeasurementHandler._check_pulse_interval>, 'run': <function MeasurementHandler.run>, '_run': <function MeasurementHandler._run>, '_apply_rate_config': <function MeasurementHandler._apply_rate_config>, '_capture_osci_raw_config': <function MeasurementHandler._capture_osci_raw_config>, '_get_conversions': <function MeasurementHandler._get_conversions>, '_measure': <function MeasurementHandler._measure>, '_get_pulse_data': <function MeasurementHandler._get_pulse_data>, '_save_average': <function MeasurementHandler._save_average>, 'save_metadata': <function MeasurementHandler.save_metadata>, '__static_attributes__': ('_warned_no_raw_adc', '_writer', 'command_queue', 'compress_data', 'conversion_params', 'data_dir', 'data_queue', 'error', 'extra_metadata', 'laser', 'laser_wait_s', 'n_acquired', 'osci', 'osci_raw_config', 'pulse_period_s', 'record_diode', 'result', 'sample', 'settings', 'setup_info', 'timeout_s'), '__dict__': <attribute '__dict__' of 'MeasurementHandler' objects>, '__weakref__': <attribute '__weakref__' of 'MeasurementHandler' objects>, '__annotations__': {'data_queue': 'Optional[Queue]', 'command_queue': 'Optional[Queue]', 'extra_metadata': 'Optional[dict]', 'pulse_period_s': 'Optional[float]', 'timeout_s': 'float', 'n_acquired': 'int', 'result': 'Optional[tuple[np.ndarray, np.ndarray, Optional[np.ndarray]]]', 'record_diode': 'bool', 'osci_raw_config': 'Optional[dict]', 'conversion_params': 'Optional[dict]', '_writer': 'Optional[_PulseSaveThread]', 'error': 'Optional[BaseException]'}})#
__firstlineno__ = 74#
__init__(settings: TspvSettings, osci: Oscilloscope, laser: LaserOpo | None = None, data_queue: Queue | None = None, command_queue: Queue | None = None, data_dir: str | Path | None = None, compress_data: bool = True, laser_wait_s: float = 0.0, setup_info: TspvSetupInfo | dict | None = None, sample: Sample | str | None = None, extra_metadata: dict | None = None)[source]#

Class that performs the actual pulse acquisition.

Parameters:
  • settings – All measurement settings.

  • osci – Oscilloscope device.

  • laser – Laser device, or None to not control the laser.

  • data_queue – Queue where status messages are put in. Useful for live monitoring when run() is run in another thread.

  • command_queue – Queue used to receive a “stop” signal. Useful when run() is run in another thread.

  • data_dir – Cache directory where per-pulse and averaged data files are stored, or None to not save.

  • compress_data – Whether to gzip-compress the data files (with the byte-shuffle filter).

  • laser_wait_s – Time in s to wait for the laser (e.g. ramp-up after turning it on, or settle after a wavelength change) before the first acquisition. Resolve it with laser_wait_time_s() before applying the laser settings.

  • setup_info – Setup description (e.g. buffer, laser temperatures) recorded in the metadata.yaml, either as a TspvSetupInfo object or as a preprocessed dict, or None to not record setup info.

  • sample – Sample object (or its name, or None) recorded in the metadata.yaml.

  • extra_metadata – Additional metadata recorded top-level in the metadata.yaml (e.g. the sample capacitance or global measurement metadata from the GUI). Applied last, so it can override other entries.

__module__ = 'spvctrl.measurement.measurement'#
__static_attributes__ = ('_warned_no_raw_adc', '_writer', 'command_queue', 'compress_data', 'conversion_params', 'data_dir', 'data_queue', 'error', 'extra_metadata', 'laser', 'laser_wait_s', 'n_acquired', 'osci', 'osci_raw_config', 'pulse_period_s', 'record_diode', 'result', 'sample', 'settings', 'setup_info', 'timeout_s')#
__weakref__#

list of weak references to the object

_apply_rate_config(rate_config: dict)[source]#

Apply the resolved acquisition configuration to the oscilloscope.

Sets the record length (post-trigger depth), the pre-trigger samples (segment size) and the sampling rate, and stores the raw oscilloscope configuration for the metadata.

Parameters:

rate_config – The resolved acquisition configuration dict from TspvSettings.resolved_acquisition.

_capture_osci_raw_config()[source]#

Store the raw configuration dump of the oscilloscope for the metadata.

Only done for oscilloscopes that support the raw configuration queries, currently the Gage CompuScope (acquisition, channels 1 and 2 and trigger engine 1). The dump reflects the applied acquisition configuration.

_check_pulse_interval()[source]#

Warn when the pulse interval is shorter than the recorded time span per pulse.

_get_conversions() → dict[int, tuple[float, float]] | None[source]#

ADC-to-volt conversion parameters per channel, or None if the oscilloscope does not support raw ADC data access (the data is then stored as float64 volts).

The conversion is queried after the acquisition configuration was applied, so it matches the acquired data.

_get_pulse_data(use_raw: bool = False) → tuple[ndarray, ndarray | None, ndarray, int, float][source]#

Acquire a single pulse: start a capture, wait for the trigger and retrieve the data.

On a trigger timeout, re-arm and retry up to trigger_retries times, then fail.

Parameters:

use_raw – Whether to retrieve raw ADC counts (Oscilloscope.get_waveform_ADC()) instead of voltages (Oscilloscope.get_waveform_V()).

Returns:

U_spv, U_diode, t_s, n_pre_trigger, sample_rate_Hz (of the full recorded segment, including pre-trigger samples), where U_spv/U_diode are raw ADC counts if use_raw, voltages otherwise. U_diode is None when the diode channel is not recorded (single mode).

_measure(n_pulses: int, data_dir: Path | None) → tuple | None[source]#

Acquire n_pulses pulses at the configured sample rate and accumulate their average.

Stores a partial file per pulse and the averaged data in data_dir (if given): the individual pulse files are always written, in the format given by MeasurementParameters.pulse_storage (full int16 ADC / float64 volts, or log-downsampled). The in-memory result and the live view always contain the full averaged transient, independent of the settings.

Parameters:
  • n_pulses – Number of pulses to acquire.

  • data_dir – Directory to store the data in, or None to not save.

Returns:

(t_s, U_spv_V, U_diode_V) averaged and stripped of pre-trigger samples, or None if no pulse was acquired. U_diode_V is None when the diode channel is not recorded (single mode).

_put(msg: tuple)[source]#
_put_avg(data_type: str, t: ndarray, U_spv: ndarray, U_diode: ndarray)[source]#
_put_status(msg: str)[source]#
_put_warning(msg: str)[source]#
_run()[source]#
_save_average(data_dir: Path, t: ndarray, U_spv_avg: ndarray, U_diode_avg: ndarray | None, sample_rate_Hz: float, n_pre_trigger: int, downsample_indices: ndarray | None)[source]#

Save the averaged transient via the background writer thread, full or log-downsampled.

The format follows MeasurementParameters.pulse_storage: ‘full’ stores the average as float64 volt data (averages are fractional, unlike raw ADC counts), ‘downsampled’ stores the log-downsampled average with the non-uniform time axis as a dataset.

conversion_params: dict | None#

ADC-to-volt conversion parameters per channel, recorded in the metadata.

error: BaseException | None#

Error that stopped the measurement, e.g. an error while saving the data

extra_metadata: dict | None#

Additional metadata recorded top-level in the metadata.yaml, applied last (see __init__).

osci_raw_config: dict | None#

Raw configuration dump of the oscilloscope (Gage CompuScope only), recorded in the metadata.

prepare()[source]#

Compute derived parameters and create the data directory.

Also writes the settings to a parameters.yaml file in the data directory (if a data directory is set), so that the parameters of the measurement are recorded in the same place by the CLI and the GUI.

pulse_period_s: float | None#

Actual pulse interval in s, or None if no frequency divider is set

run()[source]#

Run the measurement. You must call prepare() first.

run() may be run in a separate thread.

save_metadata(n_acquired: int, messages: list[str], t_start: str, t_stop: str)[source]#

Write the settings and measurement metadata to a metadata.yaml file in the data directory.

The extra_metadata from the constructor is applied last, so it can override other entries.