From f3c8add3989894bf21cdfc14a0dc0b49fb9dcafc Mon Sep 17 00:00:00 2001 From: Julian Stirling Date: Mon, 4 Aug 2025 16:59:55 +0100 Subject: [PATCH] Make axis inversion robust agains UI sending positions as strings --- .../things/stage/__init__.py | 10 ++++++---- tests/test_stage.py | 2 ++ 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/openflexure_microscope_server/things/stage/__init__.py b/src/openflexure_microscope_server/things/stage/__init__.py index 443e750d..310900ed 100644 --- a/src/openflexure_microscope_server/things/stage/__init__.py +++ b/src/openflexure_microscope_server/things/stage/__init__.py @@ -93,17 +93,19 @@ class BaseStage(lt.Thing): """Used to convert coordinates between the program frame and the hardware frame.""" def _apply_axis_direction( - self, position: list[int] | tuple[int] | Mapping[str | int] - ) -> list[int] | Mapping[str | int]: + self, position: list[int] | tuple[int] | Mapping[str, int] + ) -> list[int] | Mapping[str, int]: if isinstance(position, (list, tuple)): return [ - -pos if inverted else pos + -int(pos) if inverted else int(pos) for pos, inverted in zip(position, self.axis_inverted.values()) ] if isinstance(position, Mapping): try: return { - ax: -position[ax] if self.axis_inverted[ax] else position[ax] + ax: -int(position[ax]) + if self.axis_inverted[ax] + else int(position[ax]) for ax in position } except KeyError as e: diff --git a/tests/test_stage.py b/tests/test_stage.py index d47f4192..be4e662f 100644 --- a/tests/test_stage.py +++ b/tests/test_stage.py @@ -123,9 +123,11 @@ def test_apply_axis_direction_mixed(dummy_stage): # A list of (input position, output position) to try position_pairs = [ ([1, 2, 3], [-1, 2, -3]), # list + ([1, "2", "3"], [-1, 2, -3]), # list with strings ((1, 2, 0), [-1, 2, 0]), # tuple (gets converted to list) ({"x": 1, "y": 2, "z": 3}, {"x": -1, "y": 2, "z": -3}), # mapping ({"x": 1, "z": 2, "y": 3}, {"x": -1, "z": -2, "y": 3}), # mapping out of order + ({"x": 1, "y": "2", "z": "3"}, {"x": -1, "y": 2, "z": -3}), # mapping w strings ({"x": 3}, {"x": -3}), # Mapping with only 1 value ({"x": 1, "z": 2}, {"x": -1, "z": -2}), # Mapping with only 2 values ]