stoner_measurement.plugins.transform.base.TransformPlugin

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

Bases: QObject, BasePlugin

Abstract base class for plugins that transform or reduce data.

A TransformPlugin accepts a dictionary of named input arrays/scalars, validates that all required keys are present, and returns a dictionary of named output arrays/scalars. Subclasses must implement name, required_inputs, declared_outputs(), and transform(). The old output-name properties remain supported for legacy plugins and are derived views for unified declarations.

The class provides:

Variables:

transform_complete (pyqtSignal[dict]) – Emitted after transform() returns successfully, with the result dict as its argument.

Keyword Parameters:

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

Examples

>>> from qtpy.QtWidgets import QApplication
>>> _ = QApplication.instance() or QApplication([])
>>> from stoner_measurement.plugins.transform import TransformPlugin
>>> class _Scale(TransformPlugin):
...     @property
...     def name(self): return "Scale"
...     @property
...     def required_inputs(self): return ["y"]
...     @property
...     def output_names(self): return ["y_scaled"]
...     def transform(self, data):
...         return {"y_scaled": [v * 2 for v in data["y"]]}
>>> p = _Scale()
>>> p.plugin_type
'transform'
>>> p.description
''
>>> result = p.run({"y": [1.0, 2.0, 3.0]})
>>> result["y_scaled"]
[2.0, 4.0, 6.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 configuration tabs for this transform plugin.

Produces a single Data tab that places the instance-name editor (from _general_config_widget()) at the top, followed by a horizontal separator and the data-selection widget returned by _build_data_tab(). An optional About tab is appended when _about_html() returns non-None content.

Subclasses that need additional tabs (e.g. a Fit Function or Parameters tab) should call super().config_tabs(parent) to obtain the base Data tab and then insert their extra tabs before the optional About tab.

Keyword Parameters:

parent (QWidget | None) – Optional Qt parent widget.

Returns:

List of (tab_title, widget) pairs starting with the combined Data tab.

Return type:

(list[tuple[str, QWidget]])

Examples

>>> from qtpy.QtWidgets import QApplication
>>> _ = QApplication.instance() or QApplication([])
>>> from stoner_measurement.plugins.transform import TransformPlugin
>>> class _T(TransformPlugin):
...     @property
...     def name(self): return "T"
...     @property
...     def required_inputs(self): return []
...     @property
...     def output_names(self): return []
...     def transform(self, data): return {}
>>> tabs = _T().config_tabs()
>>> tabs[0][0]
'Data'
>>> 'General' not in [t for t, _ in tabs]
True
declared_outputs() → PluginOutputs[source]

Adapt old transform naming properties to typed declarations.

property description: str

Human-readable summary of the computation performed.

Returns:

Descriptive text; default is an empty string.

Return type:

(str)

disconnect(a0: QMetaObject.Connection) → bool
disconnect(self)
generate_action_code(indent: int, sub_steps: list, render_sub_step: Callable) → list[str][source]

Return action code lines that run this transform plugin.

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

  • sub_steps (list) – Ignored for TransformPlugin (leaf node).

  • render_sub_step (Callable) – Ignored for TransformPlugin.

Returns:

Lines calling run({}) on the plugin instance.

Return type:

(list[str])

Examples

>>> from qtpy.QtWidgets import QApplication
>>> _ = QApplication.instance() or QApplication([])
>>> from stoner_measurement.plugins.transform import TransformPlugin
>>> class _T(TransformPlugin):
...     @property
...     def name(self): return "T"
...     @property
...     def required_inputs(self): return []
...     @property
...     def output_names(self): return []
...     def transform(self, data): return {}
>>> t = _T()
>>> lines = t.generate_action_code(1, [], lambda s, i: [])
>>> "    t.run({})" in lines
True
property has_lifecycle: bool

Transform plugins have no instrument lifecycle.

Returns:

Always False. The sequence engine therefore omits connect(), configure(), and disconnect() calls for transform plugins when generating sequence code.

Return type:

(bool)

Examples

>>> from qtpy.QtWidgets import QApplication
>>> _ = QApplication.instance() or QApplication([])
>>> from stoner_measurement.plugins.transform import TransformPlugin
>>> class _T(TransformPlugin):
...     @property
...     def name(self): return "T"
...     @property
...     def required_inputs(self): return []
...     @property
...     def output_names(self): return []
...     def transform(self, data): return {}
>>> _T().has_lifecycle
False
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 output_names: list[str]

Return the compatibility view of unified output declarations.

property output_trace_names: list[str]

Return the compatibility view of unified output declarations.

property output_value_names: list[str]

Return the compatibility view of unified output declarations.

property plugin_type: str

Short tag identifying this plugin as a transform.

Returns:

Always "transform".

Return type:

(str)

rename_trace_output(old_name: str, new_name: str) → None[source]

Rename a trace output and propagate its catalogue identity downstream.

abstract property required_inputs: list[str]

Names of input datasets that transform() requires.

validate_inputs() raises ValueError if any of these keys are absent from the data dict passed to transform().

Returns:

Ordered list of required input key names.

Return type:

(list[str])

run(data: dict[str, Any]) → dict[str, Any][source]

Validate data, run transform(), emit the result, and return it.

This is the preferred entry point for the sequence engine: it combines input validation, the computation, and signal emission in a single call.

Parameters:

data (dict[str, Any]) – Named input arrays or scalars.

Returns:

The dict returned by transform().

Return type:

(dict[str, Any])

Raises:

ValueError – If any key in required_inputs is absent from data.

Examples

>>> from qtpy.QtWidgets import QApplication
>>> _ = QApplication.instance() or QApplication([])
>>> from stoner_measurement.plugins.transform import TransformPlugin
>>> class _Double(TransformPlugin):
...     @property
...     def name(self): return "Double"
...     @property
...     def required_inputs(self): return ["x"]
...     @property
...     def output_names(self): return ["x2"]
...     def transform(self, data): return {"x2": data["x"] * 2}
>>> p = _Double()
>>> p.run({"x": 3})
{'x2': 6}
abstractmethod transform(data: dict[str, Any]) → dict[str, Any][source]

Compute the transform and return the result.

Parameters:

data (dict[str, Any]) – Named input arrays or scalars. All keys listed in required_inputs are guaranteed to be present when called via run().

Returns:

Named output arrays or scalars. Should contain at least the keys listed in output_names.

Return type:

(dict[str, Any])

Examples

>>> from qtpy.QtWidgets import QApplication
>>> _ = QApplication.instance() or QApplication([])
>>> from stoner_measurement.plugins.transform import TransformPlugin
>>> class _Negate(TransformPlugin):
...     @property
...     def name(self): return "Negate"
...     @property
...     def required_inputs(self): return ["y"]
...     @property
...     def output_names(self): return ["y_neg"]
...     def transform(self, data): return {"y_neg": [-v for v in data["y"]]}
>>> p = _Negate()
>>> p.transform({"y": [1.0, -2.0]})
{'y_neg': [-1.0, 2.0]}
transform_complete

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.
validate_inputs(data: dict[str, Any]) → None[source]

Raise ValueError if any required input keys are missing.

Parameters:

data (dict[str, Any]) – The data dict to validate.

Raises:

ValueError – If one or more keys from required_inputs are absent from data.

Examples

>>> from qtpy.QtWidgets import QApplication
>>> _ = QApplication.instance() or QApplication([])
>>> from stoner_measurement.plugins.transform import TransformPlugin
>>> class _T(TransformPlugin):
...     @property
...     def name(self): return "T"
...     @property
...     def required_inputs(self): return ["a", "b"]
...     @property
...     def output_names(self): return ["c"]
...     def transform(self, data): return {"c": data["a"]}
>>> p = _T()
>>> p.validate_inputs({"a": 1, "b": 2})  # no error
>>> import pytest
>>> with pytest.raises(ValueError, match="b"):
...     p.validate_inputs({"a": 1})