stoner_measurement.plugins.monitor.base.MonitorPlugin¶
- class stoner_measurement.plugins.monitor.base.MonitorPlugin(parent: QObject | None = None)[source]¶
Bases:
QObject,BasePluginAbstract base class for plugins that passively record experimental state.
A
MonitorPluginpolls hardware at a configurable interval and emitsdata_availablewith the latest readings. Subclasses must implementname,quantity_names,units, andread().The class provides:
Synchronous read —
read()returns the latest reading as a{quantity: value}dict.Autonomous polling —
start_monitoring()starts an internalQTimer;stop_monitoring()stops it.Reading cache —
last_readingreturns the most recent successfulread()result without re-querying the hardware.
- Variables:
data_available (pyqtSignal[dict]) – Emitted after each successful
read()call with the reading dictionary as its argument.read_error (pyqtSignal[str]) – Emitted with a descriptive message if
read()raises an exception during polling._last_reading (dict[str, float]) – Internal cache updated after each successful read; exposed read-only via the
last_readingproperty.
- Keyword Parameters:
parent (QObject | None) – Optional Qt parent object.
Examples
>>> from qtpy.QtWidgets import QApplication >>> _ = QApplication.instance() or QApplication([]) >>> from stoner_measurement.plugins.monitor import MonitorPlugin >>> class _DummyMonitor(MonitorPlugin): ... @property ... def name(self): return "DummyMonitor" ... @property ... def quantity_names(self): return ["temperature"] ... @property ... def units(self): return {"temperature": "K"} ... def read(self): return {"temperature": 300.0} >>> m = _DummyMonitor() >>> m.plugin_type 'monitor' >>> m.monitor_interval 1000 >>> m.last_reading {} >>> m.read() {'temperature': 300.0}
- comment_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.
- config_tabs(parent: QWidget | None = None) list[tuple[str, QWidget]][source]¶
Return monitor configuration tabs with General first.
- data_available¶
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.
- declared_outputs() PluginOutputs[source]¶
Declare monitor readings with their configured physical units.
- generate_action_code(indent: int, sub_steps: list, render_sub_step: Callable) list[str][source]¶
Return action code lines that call
read()and retain the result.- Parameters:
indent (int) – Number of four-space indentation levels for the emitted lines.
sub_steps (list) – Ignored for
MonitorPlugin(leaf node).render_sub_step (Callable) – Ignored for
MonitorPlugin.
- Returns:
Lines calling
read()and assigning the reading dict todata.- Return type:
Examples
>>> from qtpy.QtWidgets import QApplication >>> _ = QApplication.instance() or QApplication([]) >>> from stoner_measurement.plugins.monitor import MonitorPlugin >>> class _M(MonitorPlugin): ... @property ... def name(self): return "M" ... @property ... def quantity_names(self): return ["v"] ... @property ... def units(self): return {"v": "au"} ... def read(self): return {"v": 0.0} >>> m = _M() >>> lines = m.generate_action_code(1, [], lambda s, i: []) >>> " data = m.read()" in lines True
- instance_name_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.
- property last_reading: dict[str, float]¶
Cached result of the most recent successful
read()call.Returns an empty dict if
read()has not been called yet (either directly or via the polling timer).Examples
>>> from qtpy.QtWidgets import QApplication >>> _ = QApplication.instance() or QApplication([]) >>> from stoner_measurement.plugins.monitor import MonitorPlugin >>> class _TM(MonitorPlugin): ... @property ... def name(self): return "TM" ... @property ... def quantity_names(self): return ["v"] ... @property ... def units(self): return {"v": "au"} ... def read(self): return {"v": 7.0} >>> m = _TM() >>> m.last_reading {} >>> _ = m.read() >>> m.last_reading {'v': 7.0}
- property monitor_interval: int¶
Polling period in milliseconds used by
start_monitoring().- Returns:
Interval in milliseconds; default
1000.- Return type:
(int)
- property plugin_type: str¶
Short tag identifying this plugin as a monitor.
- Returns:
Always
"monitor".- Return type:
(str)
- abstract property quantity_names: list[str]¶
Ordered list of quantity identifiers returned by
read().
- abstractmethod read() dict[str, float][source]¶
Perform a single synchronous hardware read and return the results.
Subclasses implement acquisition only. The base class wraps overrides to cache a copy and emit
data_availableonce after a successful outermost read. Direct, timer and generated calls share this behaviour. Failed reads propagate without replacing the last successful reading; the polling callback additionally emitsread_error.- Returns:
Mapping of quantity name to measured value.
- Return type:
- Raises:
Exception – Any hardware communication error; callers should handle exceptions appropriately.
Examples
>>> from qtpy.QtWidgets import QApplication >>> _ = QApplication.instance() or QApplication([]) >>> from stoner_measurement.plugins.monitor import MonitorPlugin >>> class _ConstMonitor(MonitorPlugin): ... @property ... def name(self): return "Const" ... @property ... def quantity_names(self): return ["x"] ... @property ... def units(self): return {"x": "au"} ... def read(self): return {"x": 42.0} >>> m = _ConstMonitor() >>> m.read() {'x': 42.0}
- read_error¶
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.
- start_monitoring(interval_ms: int | None = None) None[source]¶
Start the internal polling timer.
Calls
read()every interval_ms milliseconds (or everymonitor_intervalmilliseconds if interval_ms isNone). Results are cached inlast_readingand emitted viadata_available. Errors fromread()are emitted viaread_errorrather than propagated.- Keyword Parameters:
interval_ms (int | None) – Override for the polling interval in milliseconds. If
None,monitor_intervalis used.
Examples
>>> from qtpy.QtWidgets import QApplication >>> _ = QApplication.instance() or QApplication([]) >>> from stoner_measurement.plugins.monitor import MonitorPlugin >>> class _TM(MonitorPlugin): ... @property ... def name(self): return "TM" ... @property ... def quantity_names(self): return ["v"] ... @property ... def units(self): return {"v": "au"} ... def read(self): return {"v": 1.0} >>> m = _TM() >>> m.start_monitoring(500) >>> m._timer.isActive() True >>> m.stop_monitoring()
- stop_monitoring() None[source]¶
Stop the internal polling timer.
Examples
>>> from qtpy.QtWidgets import QApplication >>> _ = QApplication.instance() or QApplication([]) >>> from stoner_measurement.plugins.monitor import MonitorPlugin >>> class _TM(MonitorPlugin): ... @property ... def name(self): return "TM" ... @property ... def quantity_names(self): return ["v"] ... @property ... def units(self): return {"v": "au"} ... def read(self): return {"v": 1.0} >>> m = _TM() >>> m.start_monitoring() >>> m.stop_monitoring() >>> m._timer.isActive() False