From 1b136f30d1559697ae0a55acafc7719c6d41712d Mon Sep 17 00:00:00 2001 From: Daniel Cox Date: Fri, 8 Nov 2024 14:05:04 +0100 Subject: [PATCH] Add set_phases_8bit for simulated SLM --- openwfs/simulation/slm.py | 7 +++++++ tests/test_simulation.py | 33 +++++++-------------------------- 2 files changed, 14 insertions(+), 26 deletions(-) diff --git a/openwfs/simulation/slm.py b/openwfs/simulation/slm.py index 97ebd59..5d09589 100644 --- a/openwfs/simulation/slm.py +++ b/openwfs/simulation/slm.py @@ -273,6 +273,13 @@ def set_phases(self, values: ArrayLike, update=True): if update: self.update() + def set_phases_8bit(self, values: ArrayLike, **kwargs): + """ + Pass 8bit unsigned integers [0, 255] as linearly mapped to phase, such that 0 -> 0, 256 -> 2π. Any other + keyword arguments will be passed to set_phases. + """ + self.set_phases(values * 2 * np.pi / 256, **kwargs) + @property def pixels(self) -> Detector: """Returns an object to monitor the current state of the SLM. diff --git a/tests/test_simulation.py b/tests/test_simulation.py index 2760cec..946a9be 100644 --- a/tests/test_simulation.py +++ b/tests/test_simulation.py @@ -206,36 +206,17 @@ def test_mock_slm_lut_and_phase_response(): """ # === Test default lookup table and phase response === # Includes edge cases like rounding/wrapping: -0.501 -> 255, -0.499 -> 0 - input_phases_a = ( - np.asarray( - ( - -1, - -0.501, - -0.499, - 0, - 1, - 64, - 128, - 192, - 255, - 255.499, - 255.501, - 256, - 257, - 511, - 512, - ) - ) - * 2 - * np.pi - / 256 - ) + input_phase_a_256 = np.asarray([-1, -0.501, -0.499, 0, 1, 64, 128, 192, 255, 255.499, 255.501, 256, 257, 511, 512]) + input_phases_a = input_phase_a_256 * 2 * np.pi / 256 expected_output_phases_a = ( - np.asarray((255, 255, 0, 0, 1, 64, 128, 192, 255, 255, 0, 0, 1, 255, 0)) * 2 * np.pi / 256 + np.asarray([255, 255, 0, 0, 1, 64, 128, 192, 255, 255, 0, 0, 1, 255, 0]) * 2 * np.pi / 256 ) slm1 = SLM(shape=(3, input_phases_a.shape[0])) slm1.set_phases(input_phases_a) - assert np.all(np.abs(slm1.phases.read() - expected_output_phases_a) < 1e6) + assert np.all(np.abs(slm1.phases.read() - expected_output_phases_a) < 1e-6) + + slm1.set_phases_8bit(input_phase_a_256) + assert np.all(np.abs(slm1.phases.read() - expected_output_phases_a) < 1e-6) # === Test setting custom phase response of SLM === # Construct custom phase response