Skip to content
This repository has been archived by the owner on Sep 2, 2024. It is now read-only.

Commit

Permalink
Fix to work with updated aperture scatterguard
Browse files Browse the repository at this point in the history
  • Loading branch information
DominicOram committed Aug 20, 2024
1 parent 2d47e50 commit dbc7599
Show file tree
Hide file tree
Showing 18 changed files with 94 additions and 196 deletions.
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ install_requires =
ophyd-async >= 0.3a5
bluesky >= 1.13.0a4
blueapi >= 0.4.3-rc1
dls-dodal @ git+https://github.com/DiamondLightSource/dodal.git
dls-dodal @ git+https://github.com/DiamondLightSource/dodal.git@d7e827bc600a1c5f0156d46297fea42a4425f118

[options.entry_points]
console_scripts =
Expand Down
3 changes: 1 addition & 2 deletions src/hyperion/device_setup_plans/manipulate_sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ def move_aperture_if_required(
)

else:
assert aperture_scatterguard.aperture_positions
aperture_position = aperture_scatterguard.aperture_positions.get_position_from_gda_aperture_name(
aperture_position = aperture_scatterguard.get_position_from_gda_aperture_name(
aperture_position_gda_name
)
LOGGER.info(f"Setting aperture position to {aperture_position}")
Expand Down
22 changes: 5 additions & 17 deletions src/hyperion/experiment_plans/flyscan_xray_centre_plan.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
from attr import dataclass
from blueapi.core import BlueskyContext, MsgGenerator
from dodal.devices.aperturescatterguard import (
AperturePosition,
ApertureScatterguard,
SingleAperturePosition,
)
from dodal.devices.attenuator import Attenuator
from dodal.devices.backlight import Backlight
Expand Down Expand Up @@ -72,9 +72,6 @@
from hyperion.parameters.constants import CONST
from hyperion.parameters.gridscan import ThreeDGridScan
from hyperion.tracing import TRACER
from hyperion.utils.aperturescatterguard import (
load_default_aperture_scatterguard_positions_if_unset,
)
from hyperion.utils.context import device_composite_from_context


Expand Down Expand Up @@ -109,12 +106,6 @@ def sample_motors(self) -> Smargon:
"""Convenience alias with a more user-friendly name"""
return self.smargon

def __post_init__(self):
"""Ensure that aperture positions are loaded whenever this class is created."""
load_default_aperture_scatterguard_positions_if_unset(
self.aperture_scatterguard
)


def create_devices(context: BlueskyContext) -> FlyScanXRayCentreComposite:
"""Creates the devices required for the plan and connect to them"""
Expand Down Expand Up @@ -376,13 +367,10 @@ def set_aperture_for_bbox_size(
bbox_size: list[int] | np.ndarray,
):
# bbox_size is [x,y,z], for i03 we only care about x
assert aperture_device.aperture_positions is not None

new_selected_aperture: SingleAperturePosition = (
aperture_device.aperture_positions.MEDIUM
if bbox_size[0] < 2
else aperture_device.aperture_positions.LARGE
new_selected_aperture = (
AperturePosition.MEDIUM if bbox_size[0] < 2 else AperturePosition.LARGE
)
gda_name = aperture_device._loaded_positions[new_selected_aperture].GDA_name
LOGGER.info(
f"Setting aperture to {new_selected_aperture} based on bounding box size {bbox_size}."
)
Expand All @@ -391,7 +379,7 @@ def set_aperture_for_bbox_size(
@bpp.run_decorator(
md={
"subplan_name": "change_aperture",
"aperture_size": new_selected_aperture.GDA_name,
"aperture_size": gda_name,
}
)
def set_aperture():
Expand Down
11 changes: 0 additions & 11 deletions src/hyperion/experiment_plans/grid_detect_then_xray_centre_plan.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,6 @@
from hyperion.log import LOGGER
from hyperion.parameters.constants import CONST
from hyperion.parameters.gridscan import GridScanWithEdgeDetect, ThreeDGridScan
from hyperion.utils.aperturescatterguard import (
load_default_aperture_scatterguard_positions_if_unset,
)
from hyperion.utils.context import device_composite_from_context


Expand Down Expand Up @@ -82,12 +79,6 @@ class GridDetectThenXRayCentreComposite:
panda_fast_grid_scan: PandAFastGridScan
robot: BartRobot

def __post_init__(self):
"""Ensure that aperture positions are loaded whenever this class is created."""
load_default_aperture_scatterguard_positions_if_unset(
self.aperture_scatterguard
)


def create_devices(context: BlueskyContext) -> GridDetectThenXRayCentreComposite:
return device_composite_from_context(context, GridDetectThenXRayCentreComposite)
Expand All @@ -109,8 +100,6 @@ def detect_grid_and_do_gridscan(
parameters: GridScanWithEdgeDetect,
oav_params: OAVParameters,
):
assert composite.aperture_scatterguard.aperture_positions is not None

snapshot_template = f"{parameters.detector_params.prefix}_{parameters.detector_params.run_number}_{{angle}}"

grid_params_callback = GridDetectionCallback(composite.oav.parameters)
Expand Down
5 changes: 2 additions & 3 deletions src/hyperion/experiment_plans/oav_snapshot_plan.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from blueapi.core import MsgGenerator
from bluesky import plan_stubs as bps
from dodal.devices.aperturescatterguard import ApertureScatterguard
from dodal.devices.aperturescatterguard import AperturePosition, ApertureScatterguard
from dodal.devices.backlight import Backlight, BacklightPosition
from dodal.devices.oav.oav_detector import OAV
from dodal.devices.oav.oav_parameters import OAVParameters
Expand Down Expand Up @@ -39,10 +39,9 @@ def setup_oav_snapshot_plan(
yield from bps.abs_set(
composite.backlight, BacklightPosition.IN, group=OAV_SNAPSHOT_SETUP_GROUP
)
assert composite.aperture_scatterguard.aperture_positions is not None
yield from bps.abs_set(
composite.aperture_scatterguard,
composite.aperture_scatterguard.aperture_positions.ROBOT_LOAD,
AperturePosition.ROBOT_LOAD,
group=OAV_SNAPSHOT_SETUP_GROUP,
)

Expand Down
5 changes: 2 additions & 3 deletions src/hyperion/experiment_plans/robot_load_then_centre_plan.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import bluesky.plan_stubs as bps
import bluesky.preprocessors as bpp
from blueapi.core import BlueskyContext, MsgGenerator
from dodal.devices.aperturescatterguard import ApertureScatterguard
from dodal.devices.aperturescatterguard import AperturePosition, ApertureScatterguard
from dodal.devices.attenuator import Attenuator
from dodal.devices.backlight import Backlight
from dodal.devices.dcm import DCM
Expand Down Expand Up @@ -128,10 +128,9 @@ def take_robot_snapshots(oav: OAV, webcam: Webcam, directory: Path):


def prepare_for_robot_load(composite: RobotLoadThenCentreComposite):
assert composite.aperture_scatterguard.aperture_positions
yield from bps.abs_set(
composite.aperture_scatterguard,
composite.aperture_scatterguard.aperture_positions.ROBOT_LOAD,
AperturePosition.ROBOT_LOAD,
group="prepare_robot_load",
)

Expand Down
11 changes: 0 additions & 11 deletions src/hyperion/experiment_plans/rotation_scan_plan.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,6 @@
MultiRotationScan,
RotationScan,
)
from hyperion.utils.aperturescatterguard import (
load_default_aperture_scatterguard_positions_if_unset,
)
from hyperion.utils.context import device_composite_from_context


Expand All @@ -76,12 +73,6 @@ class RotationScanComposite(OavSnapshotComposite):
zebra: Zebra
oav: OAV

def __post_init__(self):
"""Ensure that aperture positions are loaded whenever this class is created."""
load_default_aperture_scatterguard_positions_if_unset(
self.aperture_scatterguard
)


def create_devices(context: BlueskyContext) -> RotationScanComposite:
"""Ensures necessary devices have been instantiated"""
Expand Down Expand Up @@ -364,7 +355,6 @@ def rotation_scan_plan_with_stage_and_cleanup(
@bpp.stage_decorator([eiger])
@bpp.finalize_decorator(lambda: _cleanup_plan(composite))
def rotation_with_cleanup_and_stage(params: RotationScan):
assert composite.aperture_scatterguard.aperture_positions is not None
LOGGER.info("setting up sample environment...")
yield from begin_sample_environment_setup(
composite.detector_motion,
Expand All @@ -390,7 +380,6 @@ def multi_rotation_scan(
oav_params = OAVParameters(context="xrayCentring")
eiger: EigerDetector = composite.eiger
eiger.set_detector_parameters(parameters.detector_params)
assert composite.aperture_scatterguard.aperture_positions is not None
LOGGER.info("setting up sample environment...")
yield from begin_sample_environment_setup(
composite.detector_motion,
Expand Down
16 changes: 0 additions & 16 deletions src/hyperion/utils/aperturescatterguard.py

This file was deleted.

83 changes: 41 additions & 42 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from dodal.common.beamlines.beamline_utils import clear_devices
from dodal.devices.aperturescatterguard import (
ApertureFiveDimensionalLocation,
AperturePositions,
AperturePosition,
ApertureScatterguard,
ApertureScatterguardTolerances,
SingleAperturePosition,
Expand Down Expand Up @@ -452,51 +452,50 @@ def thawer(RE) -> Generator[Thawer, Any, Any]:

@pytest.fixture
def aperture_scatterguard(RE):
AperturePositions.LARGE = SingleAperturePosition(
location=ApertureFiveDimensionalLocation(0, 1, 2, 3, 4),
name="Large",
GDA_name="LARGE_APERTURE",
radius_microns=100,
)
AperturePositions.MEDIUM = SingleAperturePosition(
location=ApertureFiveDimensionalLocation(5, 6, 2, 8, 9),
name="Medium",
GDA_name="MEDIUM_APERTURE",
radius_microns=50,
)
AperturePositions.SMALL = SingleAperturePosition(
location=ApertureFiveDimensionalLocation(10, 11, 2, 13, 14),
name="Small",
GDA_name="SMALL_APERTURE",
radius_microns=20,
)
AperturePositions.ROBOT_LOAD = SingleAperturePosition(
location=ApertureFiveDimensionalLocation(15, 16, 2, 18, 19),
name="Robot_load",
GDA_name="ROBOT_LOAD",
radius_microns=None,
)
ap_sg = i03.aperture_scatterguard(
fake_with_ophyd_sim=True,
aperture_positions=AperturePositions(
AperturePositions.LARGE,
AperturePositions.MEDIUM,
AperturePositions.SMALL,
AperturePositions.ROBOT_LOAD,
tolerances=ApertureScatterguardTolerances(0.1, 0.1, 0.1, 0.1, 0.1),
positions = {
AperturePosition.LARGE: SingleAperturePosition(
location=ApertureFiveDimensionalLocation(0, 1, 2, 3, 4),
name="Large",
GDA_name="LARGE_APERTURE",
radius_microns=100,
),
)
AperturePosition.MEDIUM: SingleAperturePosition(
location=ApertureFiveDimensionalLocation(5, 6, 2, 8, 9),
name="Medium",
GDA_name="MEDIUM_APERTURE",
radius_microns=50,
),
AperturePosition.SMALL: SingleAperturePosition(
location=ApertureFiveDimensionalLocation(10, 11, 2, 13, 14),
name="Small",
GDA_name="SMALL_APERTURE",
radius_microns=20,
),
AperturePosition.ROBOT_LOAD: SingleAperturePosition(
location=ApertureFiveDimensionalLocation(15, 16, 2, 18, 19),
name="Robot_load",
GDA_name="ROBOT_LOAD",
radius_microns=None,
),
}
with patch(
"dodal.beamlines.i03.load_positions_from_beamline_parameters",
return_value=positions,
), patch(
"dodal.beamlines.i03.load_tolerances_from_beamline_params",
return_value=ApertureScatterguardTolerances(0.1, 0.1, 0.1, 0.1, 0.1),
):
ap_sg = i03.aperture_scatterguard(fake_with_ophyd_sim=True)
with (
patch_async_motor(ap_sg.aperture.x),
patch_async_motor(ap_sg.aperture.y),
patch_async_motor(ap_sg.aperture.z, 2),
patch_async_motor(ap_sg.scatterguard.x),
patch_async_motor(ap_sg.scatterguard.y),
patch_async_motor(ap_sg._aperture.x),
patch_async_motor(ap_sg._aperture.y),
patch_async_motor(ap_sg._aperture.z, 2),
patch_async_motor(ap_sg._scatterguard.x),
patch_async_motor(ap_sg._scatterguard.y),
):
assert ap_sg.aperture_positions
RE(bps.abs_set(ap_sg, ap_sg.aperture_positions.SMALL))
RE(bps.abs_set(ap_sg, AperturePosition.SMALL))

set_mock_value(ap_sg.aperture.small, 1)
set_mock_value(ap_sg._aperture.small, 1)
yield ap_sg


Expand Down
23 changes: 6 additions & 17 deletions tests/system_tests/experiment_plans/test_fgs_plan.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@
import pytest_asyncio
from bluesky.run_engine import RunEngine
from dodal.beamlines import i03
from dodal.common.beamlines.beamline_parameters import (
BEAMLINE_PARAMETER_PATHS,
GDABeamlineParameters,
)
from dodal.devices.aperturescatterguard import AperturePositions
from dodal.devices.aperturescatterguard import AperturePosition
from dodal.devices.smargon import Smargon
from ophyd.sim import NullStatus
from ophyd_async.core import set_mock_value
Expand Down Expand Up @@ -103,17 +99,8 @@ async def fxc_composite():
await composite.robot.barcode._backend.put("ABCDEFGHIJ") # type: ignore
composite.dcm.energy_in_kev.user_readback.sim_put(12.345) # type: ignore

gda_beamline_parameters = GDABeamlineParameters.from_file(
BEAMLINE_PARAMETER_PATHS["i03"]
)

aperture_positions = AperturePositions.from_gda_beamline_params(
gda_beamline_parameters
)
composite.aperture_scatterguard.load_aperture_positions(aperture_positions)
await composite.aperture_scatterguard._set_raw_unsafe(
aperture_positions.LARGE.location
)
large = composite.aperture_scatterguard._loaded_positions[AperturePosition.LARGE]
await composite.aperture_scatterguard._set_raw_unsafe(large.location)
composite.eiger.cam.manual_trigger.put("Yes")
composite.eiger.odin.check_odin_initialised = lambda: (True, "")
composite.eiger.stage = MagicMock(return_value=NullStatus())
Expand Down Expand Up @@ -141,7 +128,9 @@ def read_run(u, s, g, r, a, f, dcm, ap_sg, sm):
yield from read_hardware_pre_collection(
undulator=u, synchrotron=s, s4_slit_gaps=g, robot=r, smargon=sm
)
yield from read_hardware_during_collection(ap_sg, a, f, dcm)
yield from read_hardware_during_collection(
ap_sg, a, f, dcm, fxc_composite.eiger
)

RE(
read_run(
Expand Down
20 changes: 13 additions & 7 deletions tests/system_tests/hyperion/test_aperturescatterguard_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,24 @@
BEAMLINE_PARAMETER_PATHS,
GDABeamlineParameters,
)
from dodal.devices.aperturescatterguard import AperturePositions, ApertureScatterguard
from dodal.devices.aperturescatterguard import (
ApertureScatterguard,
load_positions_from_beamline_parameters,
load_tolerances_from_beamline_params,
)
from ophyd_async.core import DeviceCollector


@pytest.fixture
def ap_sg():
ap_sg = ApertureScatterguard(prefix="BL03S", name="ap_sg")
ap_sg.load_aperture_positions(
AperturePositions.from_gda_beamline_params(
GDABeamlineParameters.from_file(BEAMLINE_PARAMETER_PATHS["i03"])
params = GDABeamlineParameters.from_file(BEAMLINE_PARAMETER_PATHS["i03"])
with DeviceCollector():
ap_sg = ApertureScatterguard(
prefix="BL03S",
name="ap_sg",
loaded_positions=load_positions_from_beamline_parameters(params),
tolerances=load_tolerances_from_beamline_params(params),
)
)
return ap_sg


Expand All @@ -28,7 +35,6 @@ def test_aperture_change_callback(ap_sg: ApertureScatterguard):
ApertureChangeCallback,
)

ap_sg.wait_for_connection()
cb = ApertureChangeCallback()
RE = RunEngine({})
RE.subscribe(cb)
Expand Down
Loading

0 comments on commit dbc7599

Please sign in to comment.