Writing sequence plugins¶
This is the implementation contract for an external Python package that adds measurement steps to Stoner Measurement. It covers discovery, execution, resource ownership, published data, configuration widgets and persistence. It is also a checklist for coding assistants implementing those integrations. Use Usage and Collecting, fitting and saving data for operating existing plugins.
Sequence plugins inherit BasePlugin. Plot display factories have a
separate entry-point group and contract: see Writing a plot display plugin and
Plot display communications contract. Instrument drivers implement device protocols;
sequence plugins orchestrate them or borrow application controller services.
Discovery and identity¶
Install the external package into the same Python environment as the
application. Register no-argument plugin classes in your package’s
pyproject.toml:
[project.entry-points."stoner_measurement.plugins"]
my_lab_record_value = "my_measurement_extras.record_value:RecordValueCommand"
PluginManager.discover() loads each entry point and constructs its class.
The sequence editor creates separate instances for individual steps. Therefore:
Modules and constructors must be inexpensive and work without connected hardware. Create widgets in UI hooks and open devices in lifecycle hooks.
Accept
parent=Noneand callsuper().__init__(parent)when the chosen base class supports Qt ownership.Use a distinctive entry-point name. Duplicate registry names can replace each other; discovery is not a collision-resolution service.
Implement
nameas a human-readable plugin-type label. Useinstance_namefor the identity of a particular step and its namespace variable. Let the shared setter/editor validate identifiers and reserved names; do not assign the private backing attribute.Keep public module/class paths importable across releases. Saved sequences contain those paths, independently of the entry-point name.
Restart the application after installing or changing entry-point metadata. Editable installs expose source edits, but an already running process retains its imported classes. Discovery failures are logged and isolated from other entry points.
Choose the execution contract¶
Use the narrowest suitable base. These are the current interfaces, not the
older generator-based trace API or the compatibility state_control alias.
Import |
Implement |
Execution responsibility |
|---|---|---|
|
|
One action. Generated code calls |
|
|
Acquire complete trace tables. The public |
|
|
Move through discrete points and execute nested steps at those points. |
|
|
Integrate a sweep generator with actual state/rate control. Default hardware hooks are no-ops and do not drive an instrument. |
|
|
Return a mapping of quantity names to scalar readings. |
|
|
Compute named outputs from existing data. The public |
|
|
Own orchestration, not the sequence editor. Direct execution and generated-script execution must agree. |
All imports in the table are relative to stoner_measurement. See the
corresponding classes in Plugin modules for detailed signatures. Use
state_scan in new code; StateControlPlugin remains a compatibility alias.
The example package has a runnable
example for each family.
These execution hooks and output declarations have separate responsibilities.
declared_outputs() describes identities and metadata; execute(),
measure(), run() and read() obtain or compute results. Inherit a
family’s default declarations when they already describe the plugin correctly,
otherwise override the unified hook. Do not introduce additional publication
methods for individual channels.
Lifecycle and resource ownership¶
has_lifecycle determines participation in generated connection,
configuration and cleanup phases. It is a resource/setup decision, not a
synonym for the plugin family: some leaf commands need a lifecycle.
validate()Return
Trueor raisestoner_measurement.core.validation.ValidationErrorwith an actionable message. Generated scripts validate all enabled steps, including nested steps, after construction and before any connection. The default passes. The application’s script-generation action also validates the prepared step instances before replacing editor content. Validation errors appear in a dialog and the log; the current script is preserved. Engine clients can callvalidate_generated_sequence()after generating source to run these checks without executing the generated script. Check numeric literals and relationships only when all operands are literals. Never evaluate expressions, access hardware or mutate settings. Usevalidate_expression(expression, namespace, label=...)from the same module to check syntax and warn about unknown names without execution. Missing names are warnings because earlier runtime scripts may define them; syntax errors block execution. Runtime type and range checks remain required. The engine also calls a scan generator’s optionalvalidate(namespace, label=...)hook. List scans implement this hook.connect()Open or borrow the resources needed by this instance. Repeated calls must release this instance’s previous resources before replacing them. The base wrapper invokes
disconnect()before a repeated or partially failed connection attempt. Calls throughsuper().connect()belong to the same attempt. Do not duplicate that tracking or clear its private state.configure()Apply this instance’s settings. Evaluate runtime setup expressions here, rather than while constructing the plugin or its configuration widgets. Propagate failed setup so the instance is not considered ready.
disconnect()Be idempotent after both successful and partial connection. Release owned handles, return owned outputs to the appropriate safe state, and clear references. Propagate cleanup failures. Never close another instance’s resources or a borrowed shared controller service.
Generated sequences reset per-run state with start() and check meaningful
setup phases at runtime. Disconnection and failed setup invalidate readiness.
The checks also apply inside conditional branches and loops; do not substitute
static reachability assumptions or bypass require_ready().
An instance may be configured again during execution. Reconfigure derives
delay_configuration from its sequence selections: do not add an editor
for that flag or persist it independently. A plugin must tolerate deferred
setup and reconnect through the same lifecycle hooks. User instructions for
placing Reconfigure belong in Reconnecting and applying settings at a chosen step.
Keep transport commands and protocol details in instrument drivers. Plugins
using the shared temperature, magnet, motor, pressure or X-ray engines borrow
their sessions. For temperature selections, preserve controller-qualified
LoopRef/ChannelRef identities; a loop’s input choices belong to its
own controller. See Temperature controllers for the user-visible model.
Namespace, expressions and logging¶
Use the attached sequence_engine and its public services. Access current
runtime state through self.engine_namespace; it is an empty mapping while
detached. Do not retain a private copy of the engine namespace or inspect
sequence-tree widgets from execution code.
engine.namespace and engine.namespace_snapshot() return copies for
inspection. Assigning a value to either snapshot does not define a runtime
variable. A plugin uses self.engine_namespace for live namespace access.
self.eval(expression) and self.eval_float(expression) evaluate settings
against the live namespace. Keep expression text in configuration and evaluate
at the documented phase, so expressions can use the current loop value or an
upstream result. The namespace includes np/numpy, exported NumPy names,
plugin instance variables, log, and trace/value catalogue expressions.
The application also exposes a live sequence view; see Console and script reference.
Use self.log for diagnostics. Let errors that prevent a valid action
propagate to the engine. When intentionally recovering, log the problem and
publish a defined invalid/empty result rather than leaving an earlier success
as though it were current. Do not catch KeyboardInterrupt or cancellation
indiscriminately. Bound hardware I/O and waits: stopping Python execution
cannot reliably interrupt an indefinitely blocked device call.
Built-in controller target commands, scans and sweeps abort on an error,
disconnection or shutdown snapshot instead of treating controller failure as
ordinary settling. Plugins implementing their own waits should apply
core.controller_polling.raise_for_controller_failure before accepting a
target snapshot. Pass the selected temperature controller_id when using
the combined temperature service so that a failed peer does not mask the
selected controller’s health. This status check cannot interrupt a native
driver call that has not returned; transports must also enforce I/O deadlines.
Sequence actions run on the engine worker. Create and update Qt widgets on the
GUI thread, using queued Qt signals or the application’s service boundaries
for cross-thread requests. Import Qt through qtpy and signals through
stoner_measurement.qt_compat; do not force a particular Qt binding.
Publishing data for other steps¶
The detailed reference is Plugin output declarations, including all declaration fields, schema timing, output ordering, inheritance rules and migration steps.
Implement declared_outputs() to describe trace identities, runtime access
expressions, configured channels and scalar units together. Return a fresh
PluginOutputs containing two ordered mappings. Both mappings use the
existing fully qualified catalogue identifiers:
from stoner_measurement.plugins import (
PluginOutputs, ScalarOutput, TraceOutput,
)
def declared_outputs(self):
var = self.instance_name
return PluginOutputs(
traces={
f"{var}:IV": TraceOutput(
f"{var}.data['IV']",
result_key="IV",
schema_factory=self._iv_schema,
),
},
values={
f"{var}:Resistance": ScalarOutput(
f"{var}.resistance", units="Ω",
),
},
)
This example assumes the plugin produces data['IV'] and a resistance
attribute. Expressions are evaluated by consumers at runtime, not during
declaration. result_key identifies an entry in a plugin’s result dictionary;
omit it for attribute access, monitor readings and calculated projections.
The trace and scalar mappings may independently use the same catalogue key.
Trace units belong to individual schema columns; scalar units live on each
ScalarOutput. Empty scalar units mean unspecified or dimensionless.
Common identities are instance:channel for acquired/transformed traces and
scalar quantities, and instance.data for a state scan/sweep’s collected
table. Keep output keys stable for a configuration and ensure their expressions
resolve after execution. Do not infer local result keys by parsing identifiers.
Supply schema=empty_trace or a zero-argument schema_factory to advertise
complete channels before acquisition. Factories are resolved after the scalar
catalogue has been built, allowing collected scan/sweep tables to describe
selected scalar outputs. For example:
def _iv_schema(self):
frame = pd.DataFrame({
"x": pd.Series(dtype=float),
"V": pd.Series(dtype=float),
"R": pd.Series(dtype=float),
})
return TraceData(
frame,
column_roles={"x": "x", "V": "y", "R": "z"},
names={"x": "Current", "V": "Voltage", "R": "Resistance"},
units={"x": "A", "V": "V", "R": "Ω"},
)
This example assumes import pandas as pd and
from stoner_measurement.core.trace_data import TraceData. Declarations
must use configuration state without hardware access, runtime expression
evaluation or changes to acquired results. Include optional channels only
when enabled. Keep declarations consistent with acquisition metadata and
configured transpose. State scan and sweep bases supply the single collected
table automatically when collection is enabled.
Configuration selectors prefer these schemas to previous results, then fall
back to acquired metadata and finally configured x/y labels. Omit both schema
fields when the columns cannot yet be determined. Configured schemas must be
empty; consumers receive independent metadata snapshots. The
engine publishes the schemas separately as _trace_schemas; _traces
continues to contain runtime access expressions. Schemas are derived rather
than serialised.
Existing plugins remain supported through the legacy methods:
reported_traces()returns trace identifiers mapped to access expressions.configured_trace_data()returns configured empty tables by trace identifier.reported_values()returns scalar identifiers mapped to access expressions.reported_value_units()returns scalar units by identifier.
For migrated plugins these methods are derived compatibility views. The trace
trace_names property and transform output_names, output_trace_names
and output_value_names properties derive from declared result keys.
PluginOutputs.result_order optionally preserves an interleaved result order.
New transforms need not implement the old naming properties.
BasePlugin.declared_outputs() returns None to select legacy adaptation.
An explicitly empty PluginOutputs declares no outputs. More-specific legacy
subclass overrides, including calls to super().reported_*(), remain effective.
When one class defines both a unified declaration and a legacy method, the
unified declaration takes precedence during catalogue resolution and a warning
identifies the conflict. Avoid mixing both declaration styles on the same class.
Old transform classification defaults remain unchanged for legacy plugins.
Extend a migrated parent’s super().declared_outputs() and update its typed
mappings. Do not build declarations by calling their own compatibility views.
Catalogue consumers should use resolve_outputs(plugin) from
stoner_measurement.core.plugin_outputs to apply the shared compatibility
rules, rather than combining hooks independently. The engine resolves each
plugin once per catalogue rebuild, then materialises schemas and emits updates.
The _traces and _values catalogues describe steps in the current
sequence, not all installed plugin templates. engine.traces_catalog and
engine.values_catalog and their change-signal arguments are snapshots.
Use snapshots to populate controls and evaluate the selected expression when
an action needs current data.
If a setting changes reported outputs, configured channels or units, call
self.sequence_engine.refresh_data_catalogs() when attached. Consumers
should use bind_trace_catalog_updates or bind_value_catalog_updates
from stoner_measurement.plugins.trace_catalog_ui to scope updates to a
widget’s lifetime. Preserve valid selections, handle empty catalogues, and
refresh when a producer changes or is removed. The base
remap_trace_reference handles common trace-key/expression attributes;
override it, calling super(), when storing additional trace references.
Trace and transform result shapes¶
A trace plugin’s _measure(parameters) returns
dict[str, TraceData] with keys matching trace_names. Acquire each
instrument once per requested acquisition. Do not update status, data
or channel statistics inside _measure: measure handles those duties.
Each TraceData is one shared-row table with an ordinary integer DataFrame
index. Independent coordinates are columns carrying the x role; multiple
simultaneously acquired channels sharing that coordinate belong in columns of
one table. Return separate tables for genuinely different coordinates.
Preserve column roles, names and units through transforms. See
Trace data and output catalogues for constructors and the full data contract.
A transform’s transform(data) returns a mapping with its declared output
names. Classify each stored output through a TraceOutput or ScalarOutput
with its result_key. Returning a table alone does not publish it as a trace.
Legacy plugins may still declare output_trace_names and
output_value_names. Use the base
run path so input validation, result storage and completion signals remain
consistent. Trace selection can evaluate catalogue expressions inside a
transform with required_inputs=[]; inputs declared there must instead be
supplied in the mapping passed to run.
State scan/sweep bases already collect configured scalar outputs after their
child actions and publish their table as instance.data. Reuse those hooks;
do not build a second collection table or assume a Save child sees the row
that will be collected after it. The execution-order example is in
Collecting, fitting and saving data.
Direct execute_sequence() calls and generated state loops reset iteration
metadata, honour clear_on_start and clear_filter, and collect after
successful child actions when collection is enabled. The measurement flag and
collect_filter both control collection; children still run at every point.
Monitor subclasses continue to implement read(). The base class wraps
overrides so direct reads, generated actions and timer polls all cache the
last successful result and emit data_available once. Implementations
should acquire and return readings without updating _last_reading or
emitting data_available themselves. Calling super().read() does not
publish an intermediate result. Failed reads retain the last successful cache
and propagate; timer polling also emits read_error. last_reading
returns a copy and does not query hardware.
After programmatic configuration edits, call refresh() to update the scan
generator, output catalogues and cached widgets. full_refresh() also
refreshes member plugins recursively. These operations do not perform
instrument setup or acquisition. Configuration controls that change only
published metadata can still call engine.refresh_data_catalogs() directly.
Configuration UI and persistence¶
config_widget(parent=None) supplies a settings page.
config_tabs(parent=None) supplies (title, QWidget) pairs. Prefer the
chosen base’s tab hooks and caching instead of replacing its identity,
scan/sweep/data, or help pages. When constructing a complete custom tab set,
use self._get_cached_config_tabs(builder) and preserve the inherited
settings/persistence behavior. See Plugin Tab Ordering.
Widgets edit plugin attributes; execution reads those attributes, not a live
widget. Bind pending edits with the usual editing signals so committing
configuration before generation captures them. Tolerate detached instances
and empty data catalogues. Use the shared stoner_measurement.ui widgets:
FontAwareTabWidget/FontAwareTabBar for tabs and SISpinBox for
physical quantities supporting units or SI prefixes. Keep pages top-packed,
with one bottom stretch and explicit bounds for expanding controls.
Override to_json() to extend super().to_json() with JSON-compatible
settings. Override _restore_from_json(data) to call its superclass and
restore those settings with compatible defaults for older files. The inherited
loader constructs the concrete class named in the saved data, restores
identity and settings, then reapplies machine-only configuration where defined.
Keep device handles, widgets, engine references, acquired arrays and derived
readiness/deferred-setup flags out of the configuration payload.
Round-trip expressions as strings, selected output identities and roles as
configuration, and generator settings through their existing serializers.
Use BasePlugin.from_json for reconstruction; overriding that loader is
rarely necessary. Renaming a public class/module requires a compatibility
import or migration for previously saved sequences.
monitor_widget(parent=None) optionally exposes a monitoring panel. Its
widget follows GUI ownership rules; it must not acquire an unmanaged second
session or keep polling callbacks connected after destruction.
Generated execution and plotting¶
The engine generates instantiation, startup setup, actions and final cleanup.
Usually inherit the action generator. If overriding
generate_action_code(indent, sub_steps, render_sub_step), return Python
source lines with four spaces per indentation level, use the actual
instance_name, and render children through the supplied callback. Do not
duplicate whole-sequence setup/cleanup or assume neighbouring steps exist:
Run Selection can generate a script from only this subtree.
Overriding execute_sequence alone does not change the default transparent
SequencePlugin action generator. A container that changes control flow
must implement generated actions with the same semantics as direct execution.
Use validate_sequence_position(sequence_steps) for genuine placement
constraints and is_loop_container for a container that generates a loop.
Plot producers submit portable requests through engine.plot_client and
negotiate capabilities. Keep renderer objects out of plugin execution code.
Use engine.wait_for_plot_ticket from workers when completion is required;
do not block the GUI on pending plot tickets. See Plot display communications contract
for data ownership, cancellation, completion and surface assembly. The legacy
engine.plot_widget facade is not a requirement for a new renderer.
A minimal package for all seven families¶
Each example runs without instruments. Create src/my_measurement_extras/
with an empty __init__.py. Copy the example files below into that
directory, saving record_value_plugin.py as record_value.py and keeping
the other filenames. Put this pyproject.toml in the package root. To
install only a subset, keep the corresponding entry-point lines and files.
[build-system]
requires = ["setuptools>=61"]
build-backend = "setuptools.build_meta"
[project]
name = "my-measurement-extras"
version = "0.1.0"
requires-python = ">=3.12"
dependencies = ["stoner_measurement", "numpy", "qtpy"]
[tool.setuptools.packages.find]
where = ["src"]
[project.entry-points."stoner_measurement.plugins"]
my_lab_record_value = "my_measurement_extras.record_value:RecordValueCommand"
my_lab_demo_trace = "my_measurement_extras.demo_trace:DemoTracePlugin"
my_lab_demo_scan = "my_measurement_extras.demo_scan:DemoScanPlugin"
my_lab_demo_sweep = "my_measurement_extras.demo_sweep:DemoSweepPlugin"
my_lab_demo_monitor = "my_measurement_extras.demo_monitor:DemoMonitorPlugin"
my_lab_demo_mean = "my_measurement_extras.demo_transform:DemoTransformPlugin"
my_lab_demo_group = "my_measurement_extras.demo_sequence:DemoSequencePlugin"
Declare the application-version range your package actually supports and test
against it. In the environment used to run the application, install with
python -m pip install -e .. For the repository’s Conda environment this is
conda run -n stoner_measurement python -m pip install -e ..
Restart the application to discover the examples.
Command¶
Record Value evaluates an
expression once per execution, logs the result and publishes a scalar with
configurable units. The General tab edits the expression and units, and
both settings survive saving and reopening the sequence.
"""A minimal external sequence command with configuration and scalar output."""
from qtpy.QtWidgets import QFormLayout, QLineEdit, QVBoxLayout, QWidget
from stoner_measurement.plugins import PluginOutputs, ScalarOutput
from stoner_measurement.plugins.command import CommandPlugin
class RecordValueCommand(CommandPlugin):
"""Evaluate a numeric expression, publish its result, and log the value.
Configure the expression and its units on General. The expression is
evaluated when the step executes; Value is available to Plot Points and
scan/sweep collection. This command owns no instrument resources.
Attributes:
value_expr (str): Numeric expression evaluated during execution.
value_units (str): Configured unit for the published scalar.
value (float): Latest result, or NaN before execution or after failure.
"""
def __init__(self, parent=None):
super().__init__(parent)
self.value_expr = "1.0"
self.value_units = ""
self.value = float("nan")
@property
def name(self):
return "Record Value"
def execute(self):
self.value = float("nan")
self.value = self.eval_float(self.value_expr)
self.log.info(
"%s: %s %s", self.instance_name, self.value, self.value_units
)
def declared_outputs(self):
"""Publish the expression and configured unit in one declaration."""
var = self.instance_name
return PluginOutputs(
values={
f"{var}:Value": ScalarOutput(
f"{var}.value",
units=self.value_units,
),
}
)
def config_widget(self, parent=None):
widget = QWidget(parent)
layout = QVBoxLayout(widget)
form = QFormLayout()
expression = QLineEdit(self.value_expr, widget)
units = QLineEdit(self.value_units, widget)
def apply_settings():
self.value_expr = expression.text().strip()
self.value_units = units.text().strip()
if self.sequence_engine is not None:
self.sequence_engine.refresh_data_catalogs()
expression.editingFinished.connect(apply_settings)
units.editingFinished.connect(apply_settings)
form.addRow("Value expression:", expression)
form.addRow("Units:", units)
layout.addLayout(form)
layout.addStretch()
return widget
def to_json(self):
data = super().to_json()
data.update(value_expr=self.value_expr, value_units=self.value_units)
return data
def _restore_from_json(self, data):
super()._restore_from_json(data)
self.value_expr = data.get("value_expr", self.value_expr)
self.value_units = data.get("value_units", self.value_units)
Trace¶
Demo Trace returns three fixed samples as
one TraceData table. The configured schema uses the same column names and
units as the acquisition. The inherited declaration helper applies the
configured transpose and includes statistics when enabled.
"""A minimal trace plugin with a configured column schema."""
import numpy as np
from stoner_measurement.core.trace_data import TraceData
from stoner_measurement.plugins.trace import TracePlugin
class DemoTracePlugin(TracePlugin):
"""Return three simulated voltage samples at times 0, 1 and 2 s."""
@property
def name(self):
return "Demo Trace"
@staticmethod
def _trace(time, voltage):
return TraceData.from_xy(
time,
voltage,
names={"x": "Time", "y": "Voltage"},
units={"x": "s", "y": "V"},
)
def _measure(self, parameters):
voltage = np.array([1.0, 2.0, 3.0])
return {self.name: self._trace(np.arange(3), voltage)}
def declared_outputs(self):
"""Advertise the same columns before the first acquisition."""
return self._declare_column_trace_outputs(self.name, self._schema)
def _schema(self):
return self._trace(np.empty(0), np.empty(0))
State scan¶
Demo Scan stores each setpoint immediately.
Choose points on the Scan tab and nest a Record Value step beneath it.
With the scan instance named demo_scan, set the command’s expression to
demo_scan.value and its units to V to log the voltage at each point.
The inherited Data tab can collect the command’s output after each point
marked for measurement.
"""A minimal state scan over an in-memory setpoint."""
from stoner_measurement.plugins.state_scan import StateScanPlugin
class DemoScanPlugin(StateScanPlugin):
"""Set a simulated voltage before running the children at each point."""
def __init__(self, parent=None):
super().__init__(parent)
self._voltage = 0.0
@property
def name(self):
return "Demo Scan"
@property
def state_name(self):
return "Voltage"
@property
def units(self):
return "V"
def set_state(self, value):
"""Apply the requested setpoint immediately."""
self._voltage = float(value)
def get_state(self):
"""Return the current simulated voltage."""
return self._voltage
def is_at_target(self):
"""Report that the immediate state change has settled."""
return True
State sweep¶
Demo Sweep simulates a continuous voltage
ramp. The inherited Sweep tab starts with a segment from 0 to 1 V at
5 V/s. Nest a Record Value step with expression demo_sweep.value and
units V to log the sampled voltage as it changes. Use demo_sweep as
the sweep instance name. The generator polls the simulated state and the
sweep runs its children at each yielded point.
"""A minimal state sweep over a simulated voltage ramp."""
from math import isfinite
from time import monotonic
from stoner_measurement.plugins.state_sweep import StateSweepPlugin
from stoner_measurement.sweep import MultiSegmentRampSweepGenerator
class DemoSweepPlugin(StateSweepPlugin):
"""Run children as a simulated voltage ramps towards its target."""
_sweep_generator_class = MultiSegmentRampSweepGenerator
_sweep_generator_classes = [MultiSegmentRampSweepGenerator]
def __init__(self, parent=None):
super().__init__(parent)
self.sweep_generator.segments = [(1.0, 5.0, True)]
self._rate = 5.0
self.set_state(0.0)
@property
def name(self):
return "Demo Sweep"
@property
def state_name(self):
return "Voltage"
@property
def units(self):
return "V"
def set_state(self, value):
"""Set the initial voltage immediately."""
self._start = self._target = float(value)
self._started = monotonic()
self._duration = 0.0
def set_rate(self, value):
"""Set the speed of the next ramp in volts per second."""
if not isfinite(value) or value <= 0:
raise ValueError("The ramp rate must be finite and positive.")
self._rate = value
def set_target(self, value):
"""Start a ramp from the current voltage to the target."""
self._start = self.get_state()
self._target = float(value)
self._duration = abs(self._target - self._start) / self._rate
self._started = monotonic()
def get_state(self):
"""Sample the current voltage from the simulated ramp."""
elapsed = monotonic() - self._started
if elapsed >= self._duration:
return self._target
fraction = elapsed / self._duration
return self._start + (self._target - self._start) * fraction
def is_at_target(self):
"""Report whether the simulated ramp has finished."""
return monotonic() - self._started >= self._duration
Monitor¶
Demo Monitor returns one simulated
temperature reading. The base class caches it in last_reading and
publishes the temperature with its units. Running it as a sequence step
performs one read. The example sets has_lifecycle to False because it
needs no instrument setup.
"""A minimal monitor returning one scalar reading."""
from stoner_measurement.plugins.monitor import MonitorPlugin
class DemoMonitorPlugin(MonitorPlugin):
"""Read one simulated temperature and publish it in kelvin."""
@property
def name(self):
return "Demo Monitor"
@property
def has_lifecycle(self):
return False
@property
def quantity_names(self):
return ["Temperature"]
@property
def units(self):
return {"Temperature": "K"}
def read(self):
"""Return a simulated reading using the declared quantity name."""
return {"Temperature": 293.15}
Transform¶
Demo Mean Voltage uses the completed
Demo Trace table. Add a Demo Trace step named demo_trace before
the transform. The transform reads that fixed source from the live namespace
and publishes Mean Voltage, which is 2 V for the three example samples.
The inherited run({}) call stores the result in data.
"""A minimal transform that publishes a scalar from a trace."""
from stoner_measurement.plugins import PluginOutputs, ScalarOutput
from stoner_measurement.plugins.transform import TransformPlugin
class DemoTransformPlugin(TransformPlugin):
"""Calculate the mean voltage from an earlier Demo Trace step."""
@property
def name(self):
return "Demo Mean Voltage"
@property
def required_inputs(self):
return []
def declared_outputs(self):
"""Publish the stored mean voltage as a scalar in volts."""
var = self.instance_name
return PluginOutputs(
values={
f"{var}:Mean Voltage": ScalarOutput(
f"{var}.data['mean_voltage']",
units="V",
result_key="mean_voltage",
),
}
)
def transform(self, data):
"""Read the completed trace from the live sequence namespace."""
trace = self.engine_namespace["demo_trace"].data["Demo Trace"]
return {"mean_voltage": float(trace.df["y"].mean())}
Sequence container¶
Demo Group logs the start of the group
and then runs the nested steps in order. Try nesting Demo Trace followed
by Demo Mean Voltage. The generated action emits the same log call as the
direct execution method and delegates child rendering to the base class.
"""A minimal sequence container with a shared direct and generated action."""
from stoner_measurement.plugins.sequence import SequencePlugin
class DemoSequencePlugin(SequencePlugin):
"""Log the start of a group and run its children in order."""
@property
def name(self):
return "Demo Group"
@property
def has_lifecycle(self):
return False
def log_start(self):
"""Log the start of this group using its sequence instance name."""
self.log.info("Starting %s", self.instance_name)
def execute_sequence(self, sub_steps):
"""Log the start, then call each child in order."""
self.log_start()
for sub_step in sub_steps:
sub_step()
def generate_action_code(self, indent, sub_steps, render_sub_step):
"""Generate the same action before rendering the child steps."""
prefix = " " * indent
return [
f"{prefix}{self.instance_name}.log_start()",
*super().generate_action_code(indent, sub_steps, render_sub_step),
]
For the renderer extension point, see the independent downloadable example in Writing a plot display plugin.
Verification checklist¶
Before distributing a plugin, verify:
Entry-point metadata loads the class, and construction needs neither widgets nor hardware. A clean environment can import the saved class path.
Two instances keep independent configuration, identity and owned resources.
Every editable setting survives JSON reconstruction, including defaults for older data, without persisting runtime state.
Generated Python compiles and actually runs through the engine. Test the plugin inside loops/branches and as a one-off selected subtree where valid.
Published keys, units, data shapes and failed-result behavior match the declared contract. Downstream Plot Points/Plot Trace/Save can consume them.
Configured schemas work before acquisition and after changing optional channels, units or transpose. Schema builders return empty tables and do not evaluate result expressions. Refresh updates any already open selectors.
Migrated declarations retain existing identifiers and expressions. Legacy callers and subclass overrides, including
super()updates, still work.Direct and generated state execution agree on clearing, collection filters, point metadata and child-action order. Generated Break/Continue still act on the intended loop.
Direct, generated and timer-driven monitor reads update
last_readingand publish one completed result. Failed reads retain the last successful cache; only timer polling converts the error toread_error.Owned-resource cleanup works after partial setup, failed configuration, repeated connection and stop. Borrowed controller sessions remain open.
Configuration pages survive selection changes, missing catalogues and producer renames/removal, and release signal connections on destruction.
Supported Qt bindings and rendering backends work in your deployment. Fake drivers verify software behavior; they do not establish bench behavior.
Use the repository’s managed widget/configuration fixtures when contributing tests here, following Developing the application. A coding assistant should identify the relevant base contract, the closest current implementation, resource ownership and persistence requirements before writing a new plugin.