stoner_measurement.plugins.state_scan.base.StateScanPlugin

class stoner_measurement.plugins.state_scan.base.StateScanPlugin(parent: QObject | None = None)[source]

Bases: StatePlugin

Abstract base class for plugins that control experimental state via discrete-step scanning.

A StateScanPlugin commands an instrument to move to a target value and monitors progress until the state has stabilised. Subclasses must implement name, state_name, units, set_state(), get_state(), and is_at_target().

Inheriting from SequencePlugin (via StatePlugin) means that a StateScanPlugin item in the sequence tree may act as a branch node: other steps can be nested beneath it and executed at each set-point.

The class provides:

  • Instrument lifecycle — connect(), configure(), and disconnect() form the standard sequence-engine interface. Default implementations are no-ops; override them in concrete plugins.

  • Direct control — set_state() and get_state() offer a low-level read/write interface to the underlying hardware.

  • Blocking ramp helper — ramp_to() calls set_state(), then polls is_at_target() until settled or a timeout is reached, emitting state_changed at each poll and state_reached on success.

  • Safety limits — limits defines the allowed set-point range; ramp_to() will emit state_error rather than commanding an out-of-range value.

  • Scan generator — scan_generator holds the active BaseScanGenerator instance.

  • Data collection — inherited from StatePlugin.

Variables:
  • _scan_generator_class (type[BaseScanGenerator]) – Default scan generator class instantiated in __init__().

  • _scan_generator_classes (list[type[BaseScanGenerator]]) – Ordered list of scan generator classes offered in the config tab.

  • scan_generator (BaseScanGenerator) – Active scan generator instance.

  • scan_generator_changed (pyqtSignal) – Emitted after scan_generator is replaced.

Keyword Parameters:

parent (QObject | None) – Optional Qt parent object.

Examples

>>> from qtpy.QtWidgets import QApplication
>>> _ = QApplication.instance() or QApplication([])
>>> from stoner_measurement.plugins.state_scan import StateScanPlugin
>>> class _DummyState(StateScanPlugin):
...     @property
...     def name(self): return "DummyState"
...     @property
...     def state_name(self): return "Voltage"
...     @property
...     def units(self): return "V"
...     def set_state(self, value): self._v = value
...     def get_state(self): return getattr(self, "_v", 0.0)
...     def is_at_target(self): return True
>>> p = _DummyState()
>>> p.plugin_type
'state_scan'
>>> p.limits
(-inf, inf)
>>> p.settle_timeout
60.0
>>> from stoner_measurement.scan import FunctionScanGenerator
>>> isinstance(p.scan_generator, FunctionScanGenerator)
True
config_tabs(parent: QWidget | None = None) → list[tuple[str, QWidget]][source]

Return a fixed set of configuration tabs for this plugin.

Returns Scan, Data, and Settings tabs, plus an optional About tab. Widgets are created once and cached.

Keyword Parameters:

parent (QWidget | None) – Ignored after the first call; widgets are cached.

Returns:

List of (tab_title, widget) pairs.

Return type:

(list[tuple[str, QWidget]])

Examples

>>> from qtpy.QtWidgets import QApplication
>>> _ = QApplication.instance() or QApplication([])
>>> from stoner_measurement.plugins.state_scan import CounterPlugin
>>> plugin = CounterPlugin()
>>> tabs = plugin.config_tabs()
>>> tabs[0][0]
'Scan'
>>> tabs[1][0]
'Data'
>>> tabs[2][0]
'Settings'
default_measure_condition_step()[source]

Return an If command configured for this scan’s measure flag.

execute_sequence(sub_steps: list) → None[source]

Connect, configure, iterate over the scan, run sub_steps, then disconnect.

Parameters:

sub_steps (list) – Ordered list of zero-argument callables.

Examples

>>> from qtpy.QtWidgets import QApplication
>>> _ = QApplication.instance() or QApplication([])
>>> from stoner_measurement.plugins.state_scan import StateScanPlugin
>>> class _S(StateScanPlugin):
...     @property
...     def name(self): return "S"
...     @property
...     def state_name(self): return "X"
...     @property
...     def units(self): return "au"
...     def set_state(self, v): self._v = float(v)
...     def get_state(self): return getattr(self, "_v", 0.0)
...     def is_at_target(self): return True
>>> from stoner_measurement.scan import SteppedScanGenerator
>>> p = _S()
>>> p.scan_generator = SteppedScanGenerator(start=0.0, stages=[(2.0, 1.0, True)])
>>> visited = []
>>> p.execute_sequence([lambda: visited.append(p.get_state())])
>>> visited
[0.0, 1.0, 2.0]
generate_action_code(indent: int, sub_steps: list, render_sub_step: Callable) → list[str][source]

Return action code lines for a scan loop over scan_generator.

Parameters:
  • indent (int) – Number of four-space indentation levels.

  • sub_steps (list) – Raw sub-step descriptors from the sequence tree.

  • render_sub_step (Callable) – Callback (step, indent) -> list[str] provided by the engine.

Returns:

Lines implementing the scan loop with nested sub-step bodies.

Return type:

(list[str])

Examples

>>> from qtpy.QtWidgets import QApplication
>>> _ = QApplication.instance() or QApplication([])
>>> from stoner_measurement.plugins.state_scan import StateScanPlugin
>>> class _S(StateScanPlugin):
...     @property
...     def name(self): return "S"
...     @property
...     def state_name(self): return "X"
...     @property
...     def units(self): return "au"
...     def set_state(self, v): pass
...     def get_state(self): return 0.0
...     def is_at_target(self): return True
>>> p = _S()
>>> lines = p.generate_action_code(1, [], lambda s, i: [])
>>> any("for s.ix, s.value, s.meas_flag, s.stage in s.scan_points():" in line for line in lines)
True
>>> p.clear_on_start = True
>>> lines2 = p.generate_action_code(1, [], lambda s, i: [])
>>> any("s.clear_data()" in line for line in lines2)
True
>>> p.collect_data = True
>>> lines3 = p.generate_action_code(1, [], lambda s, i: [])
>>> any("s.collect()" in line for line in lines3)
True
abstractmethod get_state() → float[source]

Return the current measured value of the controlled quantity.

Returns:

Present value in the units of units.

Return type:

(float)

abstractmethod is_at_target() → bool[source]

Return True when the hardware has settled at the commanded target.

Returns:

True if settled; False while ramping or settling.

Return type:

(bool)

property plugin_type: str

Short tag identifying this plugin as a state-scan controller.

Returns:

Always "state_scan".

Return type:

(str)

ramp_to(value: float, poll_interval: float = 0.5) → None[source]

Command hardware to value and block until settled or timed out.

Validates value against limits, calls set_state(), then polls is_at_target(). state_changed is emitted at each poll; state_reached on success; state_error on timeout or an out-of-range target.

Parameters:

value (float) – Target set-point in the units of units.

Keyword Parameters:

poll_interval (float) – Seconds between successive is_at_target() polls; default 0.5.

Examples

>>> from qtpy.QtWidgets import QApplication
>>> _ = QApplication.instance() or QApplication([])
>>> from stoner_measurement.plugins.state_scan import StateScanPlugin
>>> class _InstantState(StateScanPlugin):
...     @property
...     def name(self): return "Instant"
...     @property
...     def state_name(self): return "X"
...     @property
...     def units(self): return "au"
...     def set_state(self, v): self._v = v
...     def get_state(self): return getattr(self, "_v", 0.0)
...     def is_at_target(self): return True
>>> reached = []
>>> p = _InstantState()
>>> _ = p.state_reached.connect(lambda v: reached.append(v))
>>> p.ramp_to(1.0, poll_interval=0.0)
>>> reached
[1.0]
scan_generator_changed

Qt signal.

pyqtSignal(*types, name: str = ..., revision: int = ..., arguments: Sequence = ...) -> PYQT_SIGNAL

types is normally a sequence of individual types.  Each type is either a
type object or a string that is the name of a C++ type.  Alternatively
each type could itself be a sequence of types each describing a different
overloaded signal.
name is the optional C++ name of the signal.  If it is not specified then
the name of the class attribute that is bound to the signal is used.
revision is the optional revision of the signal that is exported to QML.
If it is not specified then 0 is used.
arguments is the optional sequence of the names of the signal's arguments.
scan_points() → Iterator[tuple[int, float, bool, int]][source]

Return scan points, optionally rebasing a stepped scan on the live state.

set_scan_generator_class(cls: type[BaseScanGenerator]) → None[source]

Replace the active scan generator with a new instance of cls.

If the current generator is already an instance of cls this method does nothing. Otherwise a new instance is created, assigned to scan_generator, and scan_generator_changed is emitted.

Parameters:

cls (type[BaseScanGenerator]) – The scan generator class to instantiate.

Examples

>>> from qtpy.QtWidgets import QApplication
>>> _ = QApplication.instance() or QApplication([])
>>> from stoner_measurement.plugins.state_scan import CounterPlugin
>>> plugin = CounterPlugin()
>>> from stoner_measurement.scan import SteppedScanGenerator
>>> plugin.set_scan_generator_class(SteppedScanGenerator)
>>> isinstance(plugin.scan_generator, SteppedScanGenerator)
True
abstractmethod set_state(value: float) → None[source]

Command the hardware to move towards value.

Parameters:

value (float) – Target set-point in the units of units.

property settle_timeout: float

Maximum time in seconds to wait for the state to reach its target.

Returns:

Timeout in seconds; default 60.0.

Return type:

(float)

to_json() → dict[str, Any][source]

Serialise this plugin’s configuration, including the scan generator.

Extends to_json() with a "scan_generator" key.

Returns:

JSON-serialisable dictionary with all plugin keys.

Return type:

(dict[str, Any])

Examples

>>> from qtpy.QtWidgets import QApplication
>>> _ = QApplication.instance() or QApplication([])
>>> from stoner_measurement.plugins.state_scan import CounterPlugin
>>> plugin = CounterPlugin()
>>> d = plugin.to_json()
>>> d["type"]
'state_scan'
>>> "scan_generator" in d
True
>>> d["collect_data"]
False
>>> d["clear_on_start"]
True