diff --git a/AGENTS.md b/AGENTS.md index e36f049..7271d09 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -193,6 +193,23 @@ extensions already does the job and only the conversion of the measurements or o - Do not use the old `pid_models` flag nor the word "preset" in new code: a model script that opens the Dashboard loads an experiment file. +## 6b. Families of interchangeable classes (factories) + +Before designing the structure of a new piece of code, look at how PyMoDAQ already does the same thing and copy it +(do not reinvent it): for instance the sequencer elements (`pymodaq/extensions/sequencer/utilities/`), the scanners +(`ScannerFactory`) or the data processors. For a family of classes chosen by name (waveforms, scan types, exporters...): + +- the base class and the factory live in one module, `_factory.py`, **not** in the folder of the classes; +- the classes live in a sibling folder (`s/`), **one module per class**, each registering itself with a + decorator of the factory (`@WaveformFactory.register()`); the name is written once, as a class attribute + (like `elt_name` of the sequencer elements), not repeated in the decorator, in a title and in the class name; +- all the submodules of that folder are imported, by its `__init__.py` (`pkgutil`) or by a function of the factory + module as `register_elements` of the sequencer does, so adding a class is adding a file and nothing else, and a + plugin package can register its own classes the same way; +- the factory is a plain class that encapsulates its own registry (a class level dictionary, with `register`, + `names`, `get_class`, `create`). Do not inherit from `pymodaq_utils.factory.ObjectFactory`: it complicates the + management of the registry. + ## 7. Done checklist - [ ] Names (package, files, classes) match and the `template` placeholders are gone. diff --git a/README.rst b/README.rst index 79f835d..2251f0b 100644 --- a/README.rst +++ b/README.rst @@ -51,3 +51,7 @@ Utilities * shaper: common interface of the plugins driving a shaper (SLM, DMD...): settings groups with the number of pixels, their size and the properties of the modulator, filled by the plugin and read back by the applications (BeamShaping extension) from the control module, see ``pymodaq_plugins_utils.hardware.shaper`` +* waveform_output: common interface of the plugins generating waveforms (analog outputs, arbitrary generators): the + capabilities of the instrument and the choices of the user (sample rate, mode, repetitions) as settings groups, + read back by the applications from the control module, which check what they send, see + ``pymodaq_plugins_utils.hardware.waveform_output`` diff --git a/src/pymodaq_plugins_utils/hardware/waveform_output.py b/src/pymodaq_plugins_utils/hardware/waveform_output.py new file mode 100644 index 0000000..5655a5e --- /dev/null +++ b/src/pymodaq_plugins_utils/hardware/waveform_output.py @@ -0,0 +1,171 @@ +"""Common interface of the instrument plugins generating waveforms (analog outputs, arbitrary generators) + +A waveform output plugin is a DAQ_Move actuator whose position is a :class:`~pymodaq.utils.data.DataActuator` holding +one array of samples (volts) per channel, all of the same length. It adds the settings groups returned by +:func:`waveform_output_params` to its parameters and fills the capabilities once the instrument is initialized +(:func:`fill_waveform_output_settings`). The applications (for instance the extension of the package +pymodaq_plugins_waveform) do not call the plugin, which lives in another thread: they read these groups from the +settings of its control module (:meth:`WaveformOutput.from_module`), then check what they are going to send (:meth:`WaveformOutput.check`) and +send the samples as the position of the actuator. + +Two groups: ``waveform_capabilities`` (read only, what the instrument can do) and ``waveform_output`` (what the user +chooses: sample rate, mode, repetitions; the plugin applies them when it receives samples). The names of the groups and +of their children, and :data:`WAVEFORM_OUTPUT_VERSION`, are the contract. Evolution rule: only add optional children, +never rename or remove one; a breaking change increments :data:`WAVEFORM_OUTPUT_VERSION`. +""" +import dataclasses +from typing import Optional, Sequence + +import numpy as np + +from pymodaq_utils.enums import StrEnum +from pymodaq_utils.logger import set_logger, get_module_name + +logger = set_logger(get_module_name(__file__)) + +WAVEFORM_OUTPUT_VERSION = 1 +CAPABILITIES_NAME = 'waveform_capabilities' +OUTPUT_NAME = 'waveform_output' + + +class OutputMode(StrEnum): + FINITE = 'finite' # the samples are generated once + REPEAT = 'repeat' # the samples are generated ``repetitions`` times + CONTINUOUS = 'continuous' # the samples are generated again and again until the actuator is stopped + + +def waveform_capabilities_params() -> dict: + """Group describing the instrument, read only. The lists (modes, triggers) are comma separated texts""" + return {'title': 'Waveform capabilities:', 'name': CAPABILITIES_NAME, 'type': 'group', 'children': [ + {'title': 'Version:', 'name': 'version', 'type': 'int', 'value': WAVEFORM_OUTPUT_VERSION, 'readonly': True, + 'visible': False}, + {'title': 'Channels:', 'name': 'n_channels', 'type': 'int', 'value': 0, 'readonly': True}, + {'title': 'Min sample rate:', 'name': 'min_rate', 'type': 'float', 'value': 0., 'suffix': 'Hz', + 'siPrefix': True, 'readonly': True}, + {'title': 'Max sample rate:', 'name': 'max_rate', 'type': 'float', 'value': 0., 'suffix': 'Hz', + 'siPrefix': True, 'readonly': True}, + {'title': 'Max samples:', 'name': 'max_samples', 'type': 'int', 'value': 0, 'readonly': True, + 'tip': 'Per channel, 0 for no limit'}, + {'title': 'Min voltage:', 'name': 'min_voltage', 'type': 'float', 'value': -10., 'suffix': 'V', + 'readonly': True}, + {'title': 'Max voltage:', 'name': 'max_voltage', 'type': 'float', 'value': 10., 'suffix': 'V', + 'readonly': True}, + {'title': 'Modes:', 'name': 'modes', 'type': 'str', 'value': OutputMode.FINITE.value, 'readonly': True}, + {'title': 'Triggers:', 'name': 'triggers', 'type': 'str', 'value': '', 'readonly': True, + 'tip': 'Names of the trigger sources the instrument accepts, set in the settings of the plugin'}, + {'title': 'Live update:', 'name': 'live_update', 'type': 'bool', 'value': False, 'readonly': True, + 'tip': 'True if new samples replace the ones being generated without a break of the signal'}, + ]} + + +def waveform_output_settings_params() -> dict: + """Group with the choices of the user""" + return {'title': 'Waveform output:', 'name': OUTPUT_NAME, 'type': 'group', 'children': [ + {'title': 'Version:', 'name': 'version', 'type': 'int', 'value': WAVEFORM_OUTPUT_VERSION, 'readonly': True, + 'visible': False}, + {'title': 'Sample rate:', 'name': 'sample_rate', 'type': 'float', 'value': 1e4, 'min': 0., 'suffix': 'Hz', + 'siPrefix': True}, + {'title': 'Mode:', 'name': 'mode', 'type': 'list', 'limits': [m.value for m in OutputMode], + 'value': OutputMode.FINITE.value}, + {'title': 'Repetitions:', 'name': 'repetitions', 'type': 'int', 'value': 1, 'min': 1, + 'tip': 'Used in the repeat mode'}, + ]} + + +def waveform_output_params() -> list[dict]: + """The groups to add to the parameters of a waveform output plugin""" + return [waveform_capabilities_params(), waveform_output_settings_params()] + + +def fill_waveform_output_settings(settings, n_channels: int, min_rate: float, max_rate: float, max_samples: int = 0, + min_voltage: float = -10., max_voltage: float = 10., + modes: Sequence[OutputMode] = (OutputMode.FINITE,), triggers: Sequence[str] = (), + live_update: bool = False) -> None: + """Fill the capabilities of a plugin in its ``settings`` (to be called in ``ini_stage``) + + The modes of the user choice are limited to the ones the instrument has. + """ + modes = [OutputMode(mode) for mode in modes] + capabilities = settings.child(CAPABILITIES_NAME) + for key, value in (('n_channels', n_channels), ('min_rate', min_rate), ('max_rate', max_rate), + ('max_samples', max_samples), ('min_voltage', min_voltage), ('max_voltage', max_voltage), + ('modes', ','.join(mode.value for mode in modes)), ('triggers', ','.join(triggers)), + ('live_update', live_update)): + capabilities.child(key).setValue(value) + settings.child(OUTPUT_NAME, 'mode').setLimits([mode.value for mode in modes]) + + +@dataclasses.dataclass +class WaveformOutput: + """What an application knows of a waveform output plugin: its capabilities and the choices of the user""" + n_channels: int + min_rate: float + max_rate: float + max_samples: int + min_voltage: float + max_voltage: float + modes: list[OutputMode] + triggers: list[str] + live_update: bool + sample_rate: float + mode: OutputMode + repetitions: int + + @classmethod + def from_settings(cls, capabilities, output) -> 'WaveformOutput': + """Build it from the two groups (Parameter objects) of a waveform output plugin""" + return cls(n_channels=int(capabilities['n_channels']), min_rate=float(capabilities['min_rate']), + max_rate=float(capabilities['max_rate']), max_samples=int(capabilities['max_samples']), + min_voltage=float(capabilities['min_voltage']), max_voltage=float(capabilities['max_voltage']), + modes=[OutputMode(mode) for mode in capabilities['modes'].split(',') if mode], + triggers=[trigger for trigger in capabilities['triggers'].split(',') if trigger], + live_update=bool(capabilities['live_update']), sample_rate=float(output['sample_rate']), + mode=OutputMode(output['mode']), repetitions=int(output['repetitions'])) + + @classmethod + def from_module(cls, module) -> Optional['WaveformOutput']: + """Read it from a DAQ_Move control module + + None if its plugin has no such groups, is not initialized yet (no channel) or follows another version of the + contract (an error is logged in that case). + """ + try: + settings = module.settings.child(module._hw_settings_name) + capabilities, output = settings.child(CAPABILITIES_NAME), settings.child(OUTPUT_NAME) + except (KeyError, AttributeError): + return None + versions = [group['version'] if 'version' in [c.name() for c in group.children()] else None + for group in (capabilities, output)] + if versions != [WAVEFORM_OUTPUT_VERSION] * 2: + logger.error(f'The waveform output groups of {module} have versions {versions}, version ' + f'{WAVEFORM_OUTPUT_VERSION} is expected: update the plugin or pymodaq_plugins_utils') + return None + waveform_output = cls.from_settings(capabilities, output) + return waveform_output if waveform_output.n_channels > 0 else None + + def check(self, samples: Sequence[np.ndarray], sample_rate: Optional[float] = None, + mode: Optional[OutputMode] = None) -> None: + """Raise a ValueError saying what the instrument cannot do with these samples (one array per channel) + + ``sample_rate`` and ``mode`` are the ones of the user choices if not given. + """ + sample_rate = self.sample_rate if sample_rate is None else sample_rate + mode = self.mode if mode is None else OutputMode(mode) + if len(samples) != self.n_channels: + raise ValueError(f'{len(samples)} waveforms for {self.n_channels} channels') + if len({len(channel) for channel in samples}) != 1: + raise ValueError('The waveforms of the channels should have the same number of samples') + n_samples = len(samples[0]) + if self.max_samples and n_samples > self.max_samples: + raise ValueError(f'{n_samples} samples, the instrument holds at most {self.max_samples} per channel') + if not self.min_rate <= sample_rate <= self.max_rate: + raise ValueError(f'Sample rate {sample_rate} Hz out of the range of the instrument: ' + f'{self.min_rate:g} to {self.max_rate:g} Hz') + if mode not in self.modes: + raise ValueError(f'The mode {mode.value} is not available, the instrument has: ' + f'{", ".join(mode.value for mode in self.modes)}') + low, high = min(float(np.min(channel)) for channel in samples), max(float(np.max(channel)) + for channel in samples) + if low < self.min_voltage or high > self.max_voltage: + raise ValueError(f'The waveform goes from {low:.3g} V to {high:.3g} V, the instrument gives ' + f'{self.min_voltage:g} V to {self.max_voltage:g} V') diff --git a/tests/test_waveform_output.py b/tests/test_waveform_output.py new file mode 100644 index 0000000..bcc8e41 --- /dev/null +++ b/tests/test_waveform_output.py @@ -0,0 +1,89 @@ +"""The waveform output contract: settings groups of the plugins, read back from the settings of their control module""" +import numpy as np +import pytest +from pymodaq_gui.parameter import Parameter + +from pymodaq_plugins_utils.hardware.waveform_output import (WaveformOutput, OutputMode, waveform_output_params, + fill_waveform_output_settings, WAVEFORM_OUTPUT_VERSION) + + +class FakeModule: + """What an application has: the DAQ_Move control module, with the settings tree of its plugin""" + _hw_settings_name = 'actuator_settings' + + def __init__(self, children=None): + children = waveform_output_params() if children is None else children + self.settings = Parameter.create(name='settings', type='group', children=[ + {'name': self._hw_settings_name, 'type': 'group', 'children': children}]) + + @property + def plugin_settings(self): + return self.settings.child(self._hw_settings_name) + + +def fill(module, **kwargs): + values = dict(n_channels=2, min_rate=1., max_rate=1e6, max_samples=1000, min_voltage=-5., max_voltage=5., + modes=[OutputMode.FINITE, OutputMode.CONTINUOUS], triggers=['PFI0', 'PFI1'], live_update=True) + values.update(kwargs) + fill_waveform_output_settings(module.plugin_settings, **values) + + +def test_not_initialized_yet(): + assert WaveformOutput.from_module(FakeModule()) is None + + +def test_no_such_groups(): + assert WaveformOutput.from_module(FakeModule(children=[])) is None + + +def test_roundtrip(): + module = FakeModule() + fill(module) + module.plugin_settings.child('waveform_output', 'sample_rate').setValue(5e4) + module.plugin_settings.child('waveform_output', 'mode').setValue('continuous') + assert WaveformOutput.from_module(module) == WaveformOutput( + n_channels=2, min_rate=1., max_rate=1e6, max_samples=1000, min_voltage=-5., max_voltage=5., + modes=[OutputMode.FINITE, OutputMode.CONTINUOUS], triggers=['PFI0', 'PFI1'], live_update=True, + sample_rate=5e4, mode=OutputMode.CONTINUOUS, repetitions=1) + + +def test_the_modes_the_user_can_choose_are_the_ones_of_the_instrument(): + module = FakeModule() + fill(module, modes=[OutputMode.FINITE]) + assert module.plugin_settings.child('waveform_output', 'mode').opts['limits'] == ['finite'] + + +def test_other_version_is_refused(caplog): + module = FakeModule() + fill(module) + module.plugin_settings.child('waveform_capabilities', 'version').setValue(WAVEFORM_OUTPUT_VERSION + 1) + assert WaveformOutput.from_module(module) is None + + +def make_output(**kwargs): + module = FakeModule() + fill(module, **kwargs) + module.plugin_settings.child('waveform_output', 'sample_rate').setValue(1e3) + return WaveformOutput.from_module(module) + + +def test_check_accepts_what_the_instrument_can_do(): + make_output().check([np.zeros(100), np.ones(100)]) + + +@pytest.mark.parametrize('samples, kwargs, message', [ + ([np.zeros(100)], {}, '1 waveforms for 2 channels'), + ([np.zeros(100), np.zeros(50)], {}, 'same number of samples'), + ([np.zeros(2000), np.zeros(2000)], {}, 'at most 1000'), + ([np.zeros(10), np.zeros(10)], dict(sample_rate=2e6), 'Sample rate'), + ([np.zeros(10), np.zeros(10)], dict(mode='repeat'), 'mode repeat is not available'), + ([np.zeros(10), np.full(10, 6.)], {}, 'goes from 0 V to 6 V.*-5 V to 5 V'), + ([np.zeros(10), np.full(10, -6.)], {}, 'goes from -6 V to 0 V'), +]) +def test_check_says_what_is_wrong(samples, kwargs, message): + with pytest.raises(ValueError, match=message): + make_output().check(samples, **kwargs) + + +def test_no_limit_of_samples_when_zero(): + make_output(max_samples=0).check([np.zeros(10 ** 6), np.zeros(10 ** 6)])