Skip to content

Commit

Permalink
Do not reload sample if it's already been loaded
Browse files Browse the repository at this point in the history
  • Loading branch information
DominicOram committed Sep 12, 2024
1 parent 027a4e7 commit ed5381d
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
from __future__ import annotations

import dataclasses
from collections.abc import Generator
from datetime import datetime
from pathlib import Path
from typing import cast

import bluesky.plan_stubs as bps
import bluesky.preprocessors as bpp
from blueapi.core import BlueskyContext, MsgGenerator
from bluesky.utils import Msg
from dodal.devices.aperturescatterguard import ApertureScatterguard, ApertureValue
from dodal.devices.attenuator import Attenuator
from dodal.devices.backlight import Backlight
Expand Down Expand Up @@ -185,11 +187,21 @@ def raise_exception_if_moved_out_of_cryojet(exception):
)


def pin_already_loaded(
robot: BartRobot, pin_to_load: int, puck_to_load: int
) -> Generator[Msg, None, bool]:
current_puck = yield from bps.rd(robot.current_puck)
current_pin = yield from bps.rd(robot.current_pin)
return int(current_puck) == puck_to_load and int(current_pin) == pin_to_load


def robot_load_then_centre_plan(
composite: RobotLoadThenCentreComposite,
params: RobotLoadThenCentre,
):
yield from prepare_for_robot_load(composite)
# TODO: get these from one source of truth #1347
assert params.sample_puck is not None
assert params.sample_pin is not None

@bpp.run_decorator(
md={
Expand Down Expand Up @@ -245,8 +257,17 @@ def robot_load_and_snapshots():

yield from bps.wait("reset-lower_gonio")

yield from robot_load_and_snapshots()

if not (
yield from pin_already_loaded(
composite.robot, params.sample_pin, params.sample_puck
)
):
yield from prepare_for_robot_load(composite)
yield from robot_load_and_snapshots()
else:
LOGGER.info(
f"Pin/puck {params.sample_pin}/{params.sample_puck} already loaded, will not reload."
)
yield from pin_centre_then_xray_centre_plan(
cast(GridDetectThenXRayCentreComposite, composite),
params.pin_centre_then_xray_centre_params(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
RobotLoadThenCentreComposite,
prepare_for_robot_load,
robot_load_then_centre,
robot_load_then_centre_plan,
take_robot_snapshots,
)
from mx_bluesky.hyperion.external_interaction.callbacks.robot_load.ispyb_callback import (
Expand Down Expand Up @@ -491,3 +492,35 @@ def test_when_plan_run_then_thawing_turned_on_for_expected_time(
and msg.obj.name == "thawer-thaw_for_time_s"
and msg.args[0] == thaw_time,
)


@patch(
"mx_bluesky.hyperion.experiment_plans.robot_load_then_centre_plan.pin_centre_then_xray_centre_plan"
)
@patch("mx_bluesky.hyperion.experiment_plans.robot_load_then_centre_plan.do_robot_load")
@patch(
"mx_bluesky.hyperion.experiment_plans.robot_load_then_centre_plan.prepare_for_robot_load"
)
def test_given_sample_already_loaded_when_plan_run_then_sample_not_loaded(
mock_prepare_robot: MagicMock,
mock_do_robot_load: MagicMock,
mock_centring_plan: MagicMock,
robot_load_composite: RobotLoadThenCentreComposite,
robot_load_then_centre_params_no_energy: RobotLoadThenCentre,
RE: RunEngine,
):
set_mock_value(robot_load_composite.robot.current_pin, 1)
set_mock_value(robot_load_composite.robot.current_puck, 2)

robot_load_then_centre_params_no_energy.sample_pin = 1
robot_load_then_centre_params_no_energy.sample_puck = 2

RE(
robot_load_then_centre_plan(
robot_load_composite,
robot_load_then_centre_params_no_energy,
)
)
mock_prepare_robot.assert_not_called()
mock_do_robot_load.assert_not_called()
mock_centring_plan.assert_called()

0 comments on commit ed5381d

Please sign in to comment.