Skip to content

Commit

Permalink
Merge branch 'main' into 232_configure_shutter_better
Browse files Browse the repository at this point in the history
  • Loading branch information
DominicOram committed Sep 4, 2024
2 parents a35282d + d44b931 commit 9ae0602
Show file tree
Hide file tree
Showing 43 changed files with 371 additions and 446 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ dependencies = [
"ophyd == 1.9.0",
"ophyd-async >= 0.3a5",
"bluesky >= 1.13.0a4",
"dls-dodal @ git+https://github.com/DiamondLightSource/dodal.git@e62835c227fb0010560762c190a60322079aaf88",
"dls-dodal @ git+https://github.com/DiamondLightSource/dodal.git@9e0e09ac0166899d49128e6c6a01e7c14e443f1f",
]


Expand Down
4 changes: 2 additions & 2 deletions src/mx_bluesky/beamlines/i04/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from mx_bluesky.beamlines.i04.thawing_plan import thaw, thaw_and_center
from mx_bluesky.beamlines.i04.thawing_plan import thaw, thaw_and_stream_to_redis

__all__ = ["thaw", "thaw_and_center"]
__all__ = ["thaw", "thaw_and_stream_to_redis"]
7 changes: 4 additions & 3 deletions src/mx_bluesky/beamlines/i04/thawing_plan.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from mx_bluesky.beamlines.i04.callbacks.murko_callback import MurkoCallback


def thaw_and_center(
def thaw_and_stream_to_redis(
time_to_thaw: float,
rotation: float = 360,
robot: BartRobot = inject("robot"),
Expand All @@ -26,6 +26,7 @@ def thaw_and_center(
sample_id = yield from bps.rd(robot.sample_id)

yield from bps.abs_set(oav.zoom_controller.level, "1.0x", wait=True)
yield from bps.abs_set(oav_to_redis_forwarder.sample_id, sample_id)

@subs_decorator(MurkoCallback(REDIS_HOST, REDIS_PASSWORD, MURKO_REDIS_DB))
@run_decorator(
Expand All @@ -38,14 +39,14 @@ def thaw_and_center(
"sample_id": sample_id,
}
)
def _thaw_and_center():
def _thaw_and_stream_to_redis():
yield from bps.kickoff(oav_to_redis_forwarder, wait=True)
yield from bps.monitor(smargon.omega.user_readback, name="smargon")
yield from bps.monitor(oav_to_redis_forwarder.uuid, name="oav")
yield from thaw(time_to_thaw, rotation, thawer, smargon)
yield from bps.complete(oav_to_redis_forwarder)

yield from _thaw_and_center()
yield from _thaw_and_stream_to_redis()


def thaw(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ def start_i24(
caput(pv.pilat_acquire, "1") # Arm pilatus
yield from arm_zebra(zebra)
caput(pv.pilat_filename, filename)
time.sleep(1.5)
sleep(1.5)

elif parameters.detector_name == "eiger":
logger.info("Using Eiger detector")
Expand Down Expand Up @@ -549,7 +549,7 @@ def start_i24(
)
yield from arm_zebra(zebra)

time.sleep(1.5)
sleep(1.5)

else:
msg = f"Unknown Detector Type, det_type = {parameters.detector_name}"
Expand Down
31 changes: 14 additions & 17 deletions src/mx_bluesky/hyperion/device_setup_plans/manipulate_sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import bluesky.plan_stubs as bps
from dodal.devices.aperturescatterguard import (
AperturePositionGDANames,
ApertureScatterguard,
ApertureValue,
)
from dodal.devices.attenuator import Attenuator
from dodal.devices.backlight import Backlight, BacklightPosition
Expand All @@ -16,52 +16,49 @@


def begin_sample_environment_setup(
detector_motion: DetectorMotion,
attenuator: Attenuator,
transmission_fraction: float,
detector_distance: float,
group="setup_senv",
):
"""Start all sample environment changes that can be initiated before OAV snapshots are taken"""
yield from bps.abs_set(detector_motion.shutter, 1, group=group)
yield from bps.abs_set(detector_motion.z, detector_distance, group=group)
yield from bps.abs_set(attenuator, transmission_fraction, group=group)


def setup_sample_environment(
aperture_scatterguard: ApertureScatterguard,
aperture_position_gda_name: AperturePositionGDANames | None,
aperture_position_gda_name: str | None,
backlight: Backlight,
group="setup_senv",
):
"""Move the aperture into required position, move out the backlight."""

aperture_value = (
None
if not aperture_position_gda_name
else ApertureValue(aperture_position_gda_name)
)
yield from move_aperture_if_required(
aperture_scatterguard, aperture_position_gda_name, group=group
aperture_scatterguard, aperture_value, group=group
)
yield from bps.abs_set(backlight, BacklightPosition.OUT, group=group)


def move_aperture_if_required(
aperture_scatterguard: ApertureScatterguard,
aperture_position_gda_name: AperturePositionGDANames | None,
aperture_value: ApertureValue | None,
group="move_aperture",
):
if not aperture_position_gda_name:
if not aperture_value:
previous_aperture_position = yield from bps.rd(aperture_scatterguard)
assert isinstance(previous_aperture_position, dict)
assert isinstance(previous_aperture_position, ApertureValue)
LOGGER.info(
f"Using previously set aperture position {previous_aperture_position['name']}"
f"Using previously set aperture position {previous_aperture_position}"
)

else:
aperture_position = aperture_scatterguard.get_position_from_gda_aperture_name(
aperture_position_gda_name
)
LOGGER.info(f"Setting aperture position to {aperture_position}")
LOGGER.info(f"Setting aperture position to {aperture_value}")
yield from bps.abs_set(
aperture_scatterguard,
aperture_position,
aperture_value,
group=group,
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
import numpy as np
from blueapi.core import BlueskyContext, MsgGenerator
from dodal.devices.aperturescatterguard import (
AperturePosition,
ApertureScatterguard,
ApertureValue,
)
from dodal.devices.attenuator import Attenuator
from dodal.devices.backlight import Backlight
Expand Down Expand Up @@ -375,9 +375,8 @@ def set_aperture_for_bbox_size(
):
# bbox_size is [x,y,z], for i03 we only care about x
new_selected_aperture = (
AperturePosition.MEDIUM if bbox_size[0] < 2 else AperturePosition.LARGE
ApertureValue.MEDIUM if bbox_size[0] < 2 else ApertureValue.LARGE
)
gda_name = aperture_device.get_gda_name_for_position(new_selected_aperture)
LOGGER.info(
f"Setting aperture to {new_selected_aperture} based on bounding box size {bbox_size}."
)
Expand All @@ -386,7 +385,7 @@ def set_aperture_for_bbox_size(
@bpp.run_decorator(
md={
"subplan_name": "change_aperture",
"aperture_size": gda_name,
"aperture_size": new_selected_aperture.value,
}
)
def set_aperture():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,9 @@ def run_grid_detection_plan(
parameters.snapshot_directory,
)

yield from bps.abs_set(composite.backlight, BacklightPosition.OUT)
yield from bps.abs_set(
composite.backlight, BacklightPosition.OUT, group=CONST.WAIT.GRID_READY_FOR_DC
)

yield from move_aperture_if_required(
composite.aperture_scatterguard,
Expand Down
4 changes: 2 additions & 2 deletions src/mx_bluesky/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 AperturePosition, ApertureScatterguard
from dodal.devices.aperturescatterguard import ApertureScatterguard, ApertureValue
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 @@ -41,7 +41,7 @@ def setup_oav_snapshot_plan(
)
yield from bps.abs_set(
composite.aperture_scatterguard,
AperturePosition.ROBOT_LOAD,
ApertureValue.ROBOT_LOAD,
group=OAV_SNAPSHOT_SETUP_GROUP,
)

Expand Down
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 AperturePosition, ApertureScatterguard
from dodal.devices.aperturescatterguard import ApertureScatterguard, ApertureValue
from dodal.devices.attenuator import Attenuator
from dodal.devices.backlight import Backlight
from dodal.devices.dcm import DCM
Expand Down Expand Up @@ -130,7 +130,7 @@ def take_robot_snapshots(oav: OAV, webcam: Webcam, directory: Path):
def prepare_for_robot_load(composite: RobotLoadThenCentreComposite):
yield from bps.abs_set(
composite.aperture_scatterguard,
AperturePosition.ROBOT_LOAD,
ApertureValue.ROBOT_LOAD,
group="prepare_robot_load",
)

Expand Down Expand Up @@ -194,7 +194,7 @@ def robot_load_then_centre_plan(
md={
"subplan_name": CONST.PLAN.ROBOT_LOAD,
"metadata": {
"visit_path": params.ispyb_params.visit_path,
"visit_path": str(params.visit_directory),
"sample_id": params.sample_id,
"sample_puck": params.sample_puck,
"sample_pin": params.sample_pin,
Expand Down
35 changes: 23 additions & 12 deletions src/mx_bluesky/hyperion/experiment_plans/rotation_scan_plan.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@
disarm_zebra,
setup_zebra_for_rotation,
)
from mx_bluesky.hyperion.device_setup_plans.utils import (
start_preparing_data_collection_then_do_plan,
)
from mx_bluesky.hyperion.experiment_plans.oav_snapshot_plan import (
OavSnapshotComposite,
oav_snapshot_plan,
Expand Down Expand Up @@ -212,8 +215,7 @@ def _rotation_scan_plan(
yield from bps.abs_set(
axis,
motion_values.start_motion_deg,
group="move_to_rotation_start",
wait=True,
group=CONST.WAIT.ROTATION_READY_FOR_DC,
)

yield from bps.abs_set(
Expand All @@ -228,20 +230,19 @@ def _rotation_scan_plan(
shutter_opening_deg=motion_values.shutter_opening_deg,
shutter_opening_s=motion_values.shutter_time_s,
group="setup_zebra",
wait=True,
)

yield from setup_sample_environment(
composite.aperture_scatterguard,
params.selected_aperture,
composite.backlight,
group=CONST.WAIT.ROTATION_READY_FOR_DC,
)

LOGGER.info("Wait for any previous moves...")
# wait for all the setup tasks at once
yield from bps.wait(CONST.WAIT.ROTATION_READY_FOR_DC)
yield from bps.wait(CONST.WAIT.MOVE_GONIO_TO_START)
yield from bps.wait("setup_senv")
yield from bps.wait("move_to_rotation_start")

# get some information for the ispyb deposition and trigger the callback
yield from read_hardware_for_zocalo(composite.eiger)
Expand Down Expand Up @@ -361,21 +362,26 @@ def rotation_scan_plan_with_stage_and_cleanup(
eiger: EigerDetector = composite.eiger
eiger.set_detector_parameters(params.detector_params)

@bpp.stage_decorator([eiger])
@bpp.finalize_decorator(lambda: _cleanup_plan(composite))
def rotation_with_cleanup_and_stage(params: RotationScan):
LOGGER.info("setting up sample environment...")
yield from begin_sample_environment_setup(
composite.detector_motion,
composite.attenuator,
params.transmission_frac,
params.detector_params.detector_distance,
group=CONST.WAIT.ROTATION_READY_FOR_DC,
)

yield from _move_and_rotation(composite, params, oav_params)

LOGGER.info("setting up and staging eiger...")
yield from rotation_with_cleanup_and_stage(params)
yield from start_preparing_data_collection_then_do_plan(
eiger,
composite.detector_motion,
params.detector_distance_mm,
rotation_with_cleanup_and_stage(params),
group=CONST.WAIT.ROTATION_READY_FOR_DC,
)
yield from bps.unstage(eiger)

yield from rotation_scan_plan_with_stage_and_cleanup(parameters)

Expand All @@ -391,10 +397,9 @@ def multi_rotation_scan(
eiger.set_detector_parameters(parameters.detector_params)
LOGGER.info("setting up sample environment...")
yield from begin_sample_environment_setup(
composite.detector_motion,
composite.attenuator,
parameters.transmission_frac,
parameters.detector_params.detector_distance,
group=CONST.WAIT.ROTATION_READY_FOR_DC,
)

@bpp.set_run_key_decorator("multi_rotation_scan")
Expand Down Expand Up @@ -430,4 +435,10 @@ def rotation_scan_core(
yield from rotation_scan_core(single_scan)

LOGGER.info("setting up and staging eiger...")
yield from _multi_rotation_scan()
yield from start_preparing_data_collection_then_do_plan(
eiger,
composite.detector_motion,
parameters.detector_distance_mm,
_multi_rotation_scan(),
group=CONST.WAIT.ROTATION_READY_FOR_DC,
)
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@

import re

from dodal.devices.detector import DetectorParams

from mx_bluesky.hyperion.external_interaction.ispyb.data_model import (
DataCollectionGroupInfo,
DataCollectionInfo,
)
from mx_bluesky.hyperion.external_interaction.ispyb.ispyb_dataclass import IspybParams
from mx_bluesky.hyperion.external_interaction.ispyb.ispyb_store import (
EIGER_FILE_SUFFIX,
I03_EIGER_DETECTOR,
Expand Down Expand Up @@ -71,16 +68,3 @@ def get_proposal_and_session_from_visit_string(visit_string: str) -> tuple[str,
def get_visit_string_from_path(path: str | None) -> str | None:
match = re.search(VISIT_PATH_REGEX, path) if path else None
return str(match.group(1)) if match else None


def get_visit_string(ispyb_params: IspybParams, detector_params: DetectorParams) -> str:
assert ispyb_params and detector_params, "StoreInISPyB didn't acquire params"
visit_path_match = get_visit_string_from_path(ispyb_params.visit_path)
if visit_path_match:
return visit_path_match
visit_path_match = get_visit_string_from_path(detector_params.directory)
if not visit_path_match:
raise ValueError(
f"Visit not found from {ispyb_params.visit_path} or {detector_params.directory}"
)
return visit_path_match
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from typing import TYPE_CHECKING, Any, TypeVar, cast

from dodal.beamline_specific_utils.i03 import beam_size_from_aperture
from dodal.devices.aperturescatterguard import SingleAperturePosition
from dodal.devices.detector.det_resolution import resolution
from dodal.devices.synchrotron import SynchrotronMode

Expand Down Expand Up @@ -123,10 +122,9 @@ def _handle_ispyb_hardware_read(self, doc) -> Sequence[ScanDataInfo]:

def _handle_ispyb_transmission_flux_read(self, doc) -> Sequence[ScanDataInfo]:
assert self.params
aperture_size = SingleAperturePosition(
**doc["data"]["aperture_scatterguard-selected_aperture"]
)
beamsize = beam_size_from_aperture(aperture_size)
aperture = doc["data"]["aperture_scatterguard-selected_aperture"]
aperture_radius = doc["data"]["aperture_scatterguard-radius"]
beamsize = beam_size_from_aperture(aperture_radius)
beamsize_x_mm = beamsize.x_um / 1000 if beamsize.x_um else None
beamsize_y_mm = beamsize.y_um / 1000 if beamsize.y_um else None
hwscan_data_collection_info = DataCollectionInfo(
Expand All @@ -153,7 +151,7 @@ def _handle_ispyb_transmission_flux_read(self, doc) -> Sequence[ScanDataInfo]:
hwscan_data_collection_info, None, self.params
)
ISPYB_LOGGER.info("Updating ispyb data collection after flux read.")
self.append_to_comment(f"Aperture: {aperture_size.name}. ")
self.append_to_comment(f"Aperture: {aperture}. ")
return scan_data_infos

@abstractmethod
Expand Down
Loading

0 comments on commit 9ae0602

Please sign in to comment.