From 9d86f6449de32c2e0ff9e450f74cef376859707b Mon Sep 17 00:00:00 2001 From: David Perl Date: Wed, 2 Dec 2026 13:55:45 +0100 Subject: [PATCH] tidy up: remove pxii specific macros --- mx_bec/actors/core.py | 5 +--- mx_bec/service.py | 17 ++++++++++++ mx_bec/tasks/mx_tasks.py | 46 ++++++++++--------------------- tests/unit/test_grid_scan_task.py | 13 +++++---- tests/unit/test_service.py | 10 ++++++- 5 files changed, 50 insertions(+), 41 deletions(-) diff --git a/mx_bec/actors/core.py b/mx_bec/actors/core.py index 9f3407e..d8c0695 100644 --- a/mx_bec/actors/core.py +++ b/mx_bec/actors/core.py @@ -109,14 +109,11 @@ class MXAutomationActor(SubscriptionActor, ABC): ) -> bool: """Plan a rotation dataset at a spot of the sample's grid analysis and queue it. Finishes the sample as failed, and returns False, if no dataset can be planned there.""" - # pyright: ignore[reportMissingImports] - from pxii_bec.macros.flux_macros import bpm2flux - info = client.sample_info assert info is not None, "No sample loaded" assert info.grid_analysis is not None, "No grid analysed" prefix = f"S{info.sample_id}_{secrets.token_hex(3)}" - _, self.strategy.beam.flux_ph_s = bpm2flux() + self.strategy.beam.flux_ph_s = client.full_flux_ph_s() # dummy data, for now try: plan = plan_rotation( info.grid_analysis, diff --git a/mx_bec/service.py b/mx_bec/service.py index d4da1c5..b4cdecf 100644 --- a/mx_bec/service.py +++ b/mx_bec/service.py @@ -28,6 +28,11 @@ if TYPE_CHECKING: logger = bec_logger.logger +# placeholders for what only beamline specific calibrations can tell, see `AareBec.energy_eV` and +# `AareBec.full_flux_ph_s` +DUMMY_ENERGY_EV = 12398.42 # a wavelength of 1 A +DUMMY_FULL_FLUX_PH_S = 1e12 + class AareBec(BECClient): """Custom MX BEC client, managing automation tasks and data flow.""" @@ -95,6 +100,18 @@ class AareBec(BECClient): def _start_actor_manager(self): self.actor_manager = AareActorManager(self) + def energy_eV(self) -> float: + """The photon energy. DUMMY DATA: the real value needs a beamline specific calibration + (monochromator angle to energy), which mx_bec does not have.""" + logger.warning(f"Using a dummy photon energy: {DUMMY_ENERGY_EV} eV") + return DUMMY_ENERGY_EV + + def full_flux_ph_s(self) -> float: + """The flux at the sample at transmission 1. DUMMY DATA: the real value needs a beamline + specific calibration (beam position monitor to flux), which mx_bec does not have.""" + logger.warning(f"Using a dummy full flux: {DUMMY_FULL_FLUX_PH_S:.1e} ph/s") + return DUMMY_FULL_FLUX_PH_S + def reset_sample_info(self, sample_id: int): self.sample_info = SampleInfo(sample_id=sample_id) diff --git a/mx_bec/tasks/mx_tasks.py b/mx_bec/tasks/mx_tasks.py index 4094d1f..c647103 100644 --- a/mx_bec/tasks/mx_tasks.py +++ b/mx_bec/tasks/mx_tasks.py @@ -160,21 +160,6 @@ class MxTasks(TaskManager[RoleT]): """The current value of the device (or signal) with this name.""" return self._client.devices[device].read()[device]["value"] - def _energy_eV_and_full_flux_ph_s(self, transmission: float) -> tuple[float, float]: - """The photon energy, from the monochromator angle, and the flux at the sample at - transmission 1, from the beam position monitor. Both come from PXII calibrations.""" - # pylint: disable=import-outside-toplevel - from pxii_bec.macros.calculator import ( # pyright: ignore[reportMissingImports] - convert_from_bragg, - ) - from pxii_bec.macros.flux_macros import bpm2flux # pyright: ignore[reportMissingImports] - - energy_eV = convert_from_bragg(self._read("dcm_bragg"))["energy_ev"] - _, full_flux = bpm2flux( - energy=energy_eV, bpmsum=self._read("bcu_bpmsum"), transm=transmission - ) - return energy_eV, full_flux - def _beam_center_pxl(self, det_z_mm: float, det_y_mm: float) -> tuple[float, float]: """Where the beam hits the detector at this detector position, from the beam centres which aarecommon has measured for the beamline.""" @@ -189,12 +174,11 @@ class MxTasks(TaskManager[RoleT]): def _beam_conditions(self) -> BeamConditions: transmission = self._read("transm") - energy_eV, full_flux = self._energy_eV_and_full_flux_ph_s(transmission) det_z_mm = self._read("det_z") beam_size_um = BeamState().beam_size_um # not measured: the strategy's default return BeamConditions( - energy_keV=energy_eV / 1000.0, - full_flux_ph_s=full_flux, + energy_keV=self._client.energy_eV() / 1000.0, # dummy data, for now + full_flux_ph_s=self._client.full_flux_ph_s(), # dummy data, for now transmission=transmission, detector_distance_mm=det_z_mm, beam_center_pxl=self._beam_center_pxl(det_z_mm, self._read("det_y")), @@ -257,19 +241,19 @@ class MxTasks(TaskManager[RoleT]): """The fields of a JFJoch `DataCollection` which describe the beamline as it is now.""" beam = self._beam_conditions() smargon = self._client.devices.smargon - return dict( - detector_distance_mm=beam.detector_distance_mm, - transmission=beam.transmission, - file_prefix=file_prefix, - energy_keV=beam.energy_keV, - beam_center_pxl=beam.beam_center_pxl, - beam_size_mm=beam.beam_size_mm, - ring_current_mA=beam.ring_current_mA, - flux_ph_s=beam.full_flux_ph_s, - sample_temperature_K=beam.sample_temperature_K, - smargon_phi_deg=smargon.phi.get(), - smargon_chi_deg=smargon.chi.get(), - ) + return { + "detector_distance_mm": beam.detector_distance_mm, + "transmission": beam.transmission, + "file_prefix": file_prefix, + "energy_keV": beam.energy_keV, + "beam_center_pxl": beam.beam_center_pxl, + "beam_size_mm": beam.beam_size_mm, + "ring_current_mA": beam.ring_current_mA, + "flux_ph_s": beam.full_flux_ph_s, + "sample_temperature_K": beam.sample_temperature_K, + "smargon_phi_deg": smargon.phi.get(), + "smargon_chi_deg": smargon.chi.get(), + } def _collect(self, scan: DataCollection, description: str) -> ScanResult: """Arm JFJoch for `scan`, trigger the Aerotech scan which was configured for it, wait for diff --git a/tests/unit/test_grid_scan_task.py b/tests/unit/test_grid_scan_task.py index 81992f0..c2d8379 100644 --- a/tests/unit/test_grid_scan_task.py +++ b/tests/unit/test_grid_scan_task.py @@ -9,7 +9,11 @@ from mx_bec.endpoints import StateEndpoints from mx_bec.external_services.jfjoch import RasterScan, RotationScan from mx_bec.sample_info import OpticalCenteringResults, SampleInfo from mx_bec.tasks import MxTasks, Server -from mx_bec.tasks.mx_tasks import MAX_GRID_SCAN_S, MAX_ROTATION_SCAN_S, BeamConditions +from mx_bec.tasks.mx_tasks import ( + MAX_GRID_SCAN_S, + MAX_ROTATION_SCAN_S, + BeamConditions, +) GRID = dict(n_x=4, n_y=3, step_x_mm=0.05, step_y_mm=0.04, exp_time_s=0.1) BEAM = BeamConditions( @@ -253,13 +257,12 @@ def test_rotation_scan_cancels_jfjoch_if_the_aerotech_scan_fails(tasks, client): def test_beam_conditions_are_read_from_the_devices(client, monkeypatch): monkeypatch.setenv("BEAMLINE", "X10SA") tasks = MxTasks[Server](client) - # energy and flux come from beamline plugin calibrations - energy_and_flux = MagicMock(return_value=(12658.0, 2e12)) - monkeypatch.setattr(tasks, "_energy_eV_and_full_flux_ph_s", energy_and_flux) + + client.energy_eV.return_value, client.full_flux_ph_s.return_value = 12658.0, 2e12 beam = tasks._beam_conditions() - energy_and_flux.assert_called_once_with(0.5) + # energy and flux come from the AareBec client assert beam.energy_keV == pytest.approx(12.658) assert (beam.full_flux_ph_s, beam.transmission) == (2e12, 0.5) assert (beam.detector_distance_mm, beam.ring_current_mA) == (200.0, 400.0) diff --git a/tests/unit/test_service.py b/tests/unit/test_service.py index 864ed10..509bf7b 100644 --- a/tests/unit/test_service.py +++ b/tests/unit/test_service.py @@ -8,7 +8,7 @@ from bec_lib.tests.utils import ConnectorMock from mx_bec.constants import AareBecWorkflowState from mx_bec.endpoints import QueueEndpoints from mx_bec.messages import MXQueueChangedMessage, MXTaskExecutionMessage -from mx_bec.service import AareBec +from mx_bec.service import DUMMY_ENERGY_EV, DUMMY_FULL_FLUX_PH_S, AareBec from mx_bec.tasks import MxTasks, RoleT, Server, task @@ -177,3 +177,11 @@ def test_failed_task_reraises_even_if_pausing_fails(): client.task_queue.pause.side_effect = ConnectionError("redis gone") with pytest.raises(RuntimeError, match="boom"): _FailingTasks[Server](client).boom() + + +def test_energy_and_full_flux_are_dummy_data_with_a_warning(service): + with patch("mx_bec.service.logger") as logger: + assert service.energy_eV() == DUMMY_ENERGY_EV + assert service.full_flux_ph_s() == DUMMY_FULL_FLUX_PH_S + messages = [c.args[0] for c in logger.warning.call_args_list] + assert len(messages) == 2 and all("dummy" in message for message in messages)