From 15b2a33140d879f9482a8cb01a7d526f685d5842 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 30 Oct 2026 12:39:16 +0000 Subject: [PATCH] Keep the user's choice of internal calibration when the shaper is initialized again Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_01CAhZwmbyCSpSgfTyicUxuS --- src/pymodaq_plugins_utils/hardware/shaper.py | 9 +++++++-- tests/test_shaper.py | 20 ++++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/pymodaq_plugins_utils/hardware/shaper.py b/src/pymodaq_plugins_utils/hardware/shaper.py index 44c9d44..aa0c91d 100644 --- a/src/pymodaq_plugins_utils/hardware/shaper.py +++ b/src/pymodaq_plugins_utils/hardware/shaper.py @@ -66,16 +66,21 @@ def fill_shaper_settings(settings, name: str, width: int, height: int, pixel_siz has_internal_calibration: bool = False) -> None: """Fill the groups of :func:`shaper_params` in the ``settings`` of a plugin (to be called in ``ini_stage``) - ``use_internal_calibration`` is set to ``has_internal_calibration``, the user can change it afterwards. + ``use_internal_calibration`` is the choice of the user: it is set to ``has_internal_calibration`` only the first + time (or if ``has_internal_calibration`` changed since the settings were saved, for instance with another + controller), otherwise the value restored from the preset is kept. It is never True without an internal calibration. """ geometry = settings.child(GEOMETRY_NAME) for key, value in (('width', width), ('height', height), ('pixel_size_x', pixel_size_x), ('pixel_size_y', pixel_size_x if pixel_size_y is None else pixel_size_y)): geometry.child(key).setValue(value) properties = settings.child(PROPERTIES_NAME) + keep_choice = bool(properties['has_internal_calibration']) == has_internal_calibration + use_internal_calibration = has_internal_calibration and (bool(properties['use_internal_calibration']) + if keep_choice else True) for key, value in (('name', name), ('modulator_type', ModulatorType(modulator_type).value), ('has_internal_calibration', has_internal_calibration), - ('use_internal_calibration', has_internal_calibration)): + ('use_internal_calibration', use_internal_calibration)): properties.child(key).setValue(value) diff --git a/tests/test_shaper.py b/tests/test_shaper.py index bae2d32..04cf8af 100644 --- a/tests/test_shaper.py +++ b/tests/test_shaper.py @@ -52,6 +52,26 @@ def test_use_internal_calibration_is_the_choice_of_the_user(): assert shaper.has_internal_calibration and not shaper.use_internal_calibration +def test_choice_of_the_user_survives_a_new_initialization(): + module = FakeModule() + fill(module, has_internal_calibration=True) + assert Shaper.from_module(module).use_internal_calibration # default: use it + module.plugin_settings.child('shaper_properties', 'use_internal_calibration').setValue(False) + fill(module, has_internal_calibration=True) # ini_stage again, settings restored from the preset + assert not Shaper.from_module(module).use_internal_calibration + + +def test_choice_reset_if_the_capability_changes(): + module = FakeModule() + fill(module, has_internal_calibration=False) + assert not Shaper.from_module(module).use_internal_calibration + fill(module, has_internal_calibration=True) + assert Shaper.from_module(module).use_internal_calibration + module.plugin_settings.child('shaper_properties', 'use_internal_calibration').setValue(False) + fill(module, has_internal_calibration=False) + assert not Shaper.from_module(module).use_internal_calibration + + def test_plugin_without_the_groups(): assert Shaper.from_module(FakeModule([])) is None assert Shaper.from_module(object()) is None