Skip to content

Commit

Permalink
Merge pull request DiamondLightSource/hyperion#1334 from DiamondLight…
Browse files Browse the repository at this point in the history
…Source/dodal_471_beam_centre

Update for dodal changes around beam centre snapshot
  • Loading branch information
DominicOram authored May 7, 2024
2 parents 4a7d65d + e2ee487 commit 2b10b42
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 83 deletions.
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ install_requires =
xarray
doct
databroker
dls-dodal @ git+https://github.com/DiamondLightSource/dodal.git@aac89a96686218add67bbbcc80118f6e94f0bdc9
dls-dodal @ git+https://github.com/DiamondLightSource/dodal.git@af32f10f9536af91c69f1770f93754fa4220d766
pydantic<2.0 # See https://github.com/DiamondLightSource/hyperion/issues/774
scipy
pyzmq<25 # See https://github.com/DiamondLightSource/hyperion/issues/1103
Expand Down
18 changes: 9 additions & 9 deletions src/hyperion/experiment_plans/oav_grid_detection_plan.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,20 +145,20 @@ def grid_detection_plan(

upper_left = (tip_x_px, min_y)

yield from bps.abs_set(oav.snapshot.top_left_x, upper_left[0])
yield from bps.abs_set(oav.snapshot.top_left_y, upper_left[1])
yield from bps.abs_set(oav.snapshot.box_width, box_size_x_pixels)
yield from bps.abs_set(oav.snapshot.num_boxes_x, x_steps)
yield from bps.abs_set(oav.snapshot.num_boxes_y, y_steps)
yield from bps.abs_set(oav.grid_snapshot.top_left_x, upper_left[0])
yield from bps.abs_set(oav.grid_snapshot.top_left_y, upper_left[1])
yield from bps.abs_set(oav.grid_snapshot.box_width, box_size_x_pixels)
yield from bps.abs_set(oav.grid_snapshot.num_boxes_x, x_steps)
yield from bps.abs_set(oav.grid_snapshot.num_boxes_y, y_steps)

snapshot_filename = snapshot_template.format(angle=abs(angle))

yield from bps.abs_set(oav.snapshot.filename, snapshot_filename)
yield from bps.abs_set(oav.snapshot.directory, snapshot_dir)
yield from bps.trigger(oav.snapshot, wait=True)
yield from bps.abs_set(oav.grid_snapshot.filename, snapshot_filename)
yield from bps.abs_set(oav.grid_snapshot.directory, snapshot_dir)
yield from bps.trigger(oav.grid_snapshot, wait=True)
yield from bps.create(CONST.DESCRIPTORS.OAV_SNAPSHOT_TRIGGERED)

yield from bps.read(oav.snapshot)
yield from bps.read(oav.grid_snapshot)
yield from bps.read(smargon)
yield from bps.save()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ def __init__(

def event(self, doc: Event):
data = doc.get("data")
top_left_x_px = data["oav_snapshot_top_left_x"]
box_width_px = data["oav_snapshot_box_width"]
top_left_x_px = data["oav_grid_snapshot_top_left_x"]
box_width_px = data["oav_grid_snapshot_box_width"]
x_of_centre_of_first_box_px = top_left_x_px + box_width_px / 2

top_left_y_px = data["oav_snapshot_top_left_y"]
top_left_y_px = data["oav_grid_snapshot_top_left_y"]
y_of_centre_of_first_box_px = top_left_y_px + box_width_px / 2

smargon_omega = data["smargon_omega"]
Expand All @@ -54,7 +54,10 @@ def event(self, doc: Event):

self.start_positions.append(position_grid_start)
self.box_numbers.append(
(data["oav_snapshot_num_boxes_x"], data["oav_snapshot_num_boxes_y"])
(
data["oav_grid_snapshot_num_boxes_x"],
data["oav_grid_snapshot_num_boxes_y"],
)
)

self.x_step_size_mm = box_width_px * self.oav_params.micronsPerXPixel / 1000
Expand Down
25 changes: 13 additions & 12 deletions src/hyperion/external_interaction/callbacks/ispyb_callback_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,24 +130,25 @@ def _handle_oav_snapshot_triggered(self, doc) -> Sequence[ScanDataInfo]:
data = doc["data"]
data_collection_id = None
data_collection_info = DataCollectionInfo(
xtal_snapshot1=data.get("oav_snapshot_last_path_full_overlay"),
xtal_snapshot2=data.get("oav_snapshot_last_path_outer"),
xtal_snapshot3=data.get("oav_snapshot_last_saved_path"),
xtal_snapshot1=data.get("oav_grid_snapshot_last_path_full_overlay"),
xtal_snapshot2=data.get("oav_grid_snapshot_last_path_outer"),
xtal_snapshot3=data.get("oav_grid_snapshot_last_saved_path"),
n_images=(
data["oav_snapshot_num_boxes_x"] * data["oav_snapshot_num_boxes_y"]
data["oav_grid_snapshot_num_boxes_x"]
* data["oav_grid_snapshot_num_boxes_y"]
),
)
microns_per_pixel_x = data["oav_snapshot_microns_per_pixel_x"]
microns_per_pixel_y = data["oav_snapshot_microns_per_pixel_y"]
microns_per_pixel_x = data["oav_grid_snapshot_microns_per_pixel_x"]
microns_per_pixel_y = data["oav_grid_snapshot_microns_per_pixel_y"]
data_collection_grid_info = DataCollectionGridInfo(
dx_in_mm=data["oav_snapshot_box_width"] * microns_per_pixel_x / 1000,
dy_in_mm=data["oav_snapshot_box_width"] * microns_per_pixel_y / 1000,
steps_x=data["oav_snapshot_num_boxes_x"],
steps_y=data["oav_snapshot_num_boxes_y"],
dx_in_mm=data["oav_grid_snapshot_box_width"] * microns_per_pixel_x / 1000,
dy_in_mm=data["oav_grid_snapshot_box_width"] * microns_per_pixel_y / 1000,
steps_x=data["oav_grid_snapshot_num_boxes_x"],
steps_y=data["oav_grid_snapshot_num_boxes_y"],
microns_per_pixel_x=microns_per_pixel_x,
microns_per_pixel_y=microns_per_pixel_y,
snapshot_offset_x_pixel=int(data["oav_snapshot_top_left_x"]),
snapshot_offset_y_pixel=int(data["oav_snapshot_top_left_y"]),
snapshot_offset_x_pixel=int(data["oav_grid_snapshot_top_left_x"]),
snapshot_offset_y_pixel=int(data["oav_grid_snapshot_top_left_y"]),
orientation=Orientation.HORIZONTAL,
snaked=True,
)
Expand Down
6 changes: 3 additions & 3 deletions src/hyperion/snapshot_plan.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ def prepare_for_snapshot(backlight: Backlight, aperture: Aperture):

def take_snapshot(oav: OAV, snapshot_filename, snapshot_directory):
oav.wait_for_connection()
yield from bps.abs_set(oav.snapshot.filename, snapshot_filename)
yield from bps.abs_set(oav.snapshot.directory, snapshot_directory)
yield from bps.trigger(oav.snapshot, wait=True)
yield from bps.abs_set(oav.grid_snapshot.filename, snapshot_filename)
yield from bps.abs_set(oav.grid_snapshot.directory, snapshot_directory)
yield from bps.trigger(oav.grid_snapshot, wait=True)


@bpp.run_decorator()
Expand Down
2 changes: 1 addition & 1 deletion tests/unit_tests/device_setup_plans/test_setup_oav.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def test_when_set_up_oav_with_different_zoom_levels_then_flat_field_applied_corr

RE = RunEngine()
RE(pre_centring_setup_oav(oav, mock_parameters, ophyd_pin_tip_detection))
assert oav.snapshot.input_plugin.get() == expected_plugin
assert oav.grid_snapshot.input_plugin.get() == expected_plugin


@pytest.mark.parametrize(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,21 @@ def _fake_grid_detection(
):
oav = i03.oav(fake_with_ophyd_sim=True)
smargon = fake_smargon()
oav.snapshot.box_width.put(635.00986)
oav.grid_snapshot.box_width.put(635.00986)

# first grid detection: x * y
oav.snapshot.num_boxes_x.put(10)
oav.snapshot.num_boxes_y.put(4)
oav.grid_snapshot.num_boxes_x.put(10)
oav.grid_snapshot.num_boxes_y.put(4)
yield from bps.create(CONST.DESCRIPTORS.OAV_SNAPSHOT_TRIGGERED)
yield from bps.read(oav.snapshot)
yield from bps.read(oav.grid_snapshot)
yield from bps.read(smargon)
yield from bps.save()

# second grid detection: x * z, so num_boxes_y refers to smargon z
oav.snapshot.num_boxes_x.put(10)
oav.snapshot.num_boxes_y.put(1)
oav.grid_snapshot.num_boxes_x.put(10)
oav.grid_snapshot.num_boxes_y.put(1)
yield from bps.create(CONST.DESCRIPTORS.OAV_SNAPSHOT_TRIGGERED)
yield from bps.read(oav.snapshot)
yield from bps.read(oav.grid_snapshot)
yield from bps.read(smargon)
yield from bps.save()

Expand Down
54 changes: 28 additions & 26 deletions tests/unit_tests/experiment_plans/test_grid_detection_plan.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,10 @@

@pytest.fixture
def fake_devices(RE, smargon: Smargon, backlight: Backlight, test_config_files):
oav = i03.oav(wait_for_connection=False, fake_with_ophyd_sim=True)

oav.parameters = OAVConfigParams(
params = OAVConfigParams(
test_config_files["zoom_params_file"], test_config_files["display_config"]
)
oav = i03.oav(wait_for_connection=False, fake_with_ophyd_sim=True, params=params)
oav.parameters.update_on_zoom = MagicMock()
oav.parameters.load_microns_per_pixel = MagicMock()
oav.parameters.micronsPerXPixel = 1.58
Expand Down Expand Up @@ -245,31 +244,31 @@ def decorated():
assert_event(
cb.activity_gated_event.mock_calls[0], # pyright: ignore
{
"oav_snapshot_top_left_x": 8,
"oav_snapshot_top_left_y": -6,
"oav_snapshot_num_boxes_x": 8,
"oav_snapshot_num_boxes_y": 2,
"oav_snapshot_box_width": 16,
"oav_snapshot_microns_per_pixel_x": 1.25,
"oav_snapshot_microns_per_pixel_y": 1.25,
"oav_snapshot_last_path_full_overlay": "tmp/test_0_grid_overlay.png",
"oav_snapshot_last_path_outer": "tmp/test_0_outer_overlay.png",
"oav_snapshot_last_saved_path": "tmp/test_0.png",
"oav_grid_snapshot_top_left_x": 8,
"oav_grid_snapshot_top_left_y": -6,
"oav_grid_snapshot_num_boxes_x": 8,
"oav_grid_snapshot_num_boxes_y": 2,
"oav_grid_snapshot_box_width": 16,
"oav_grid_snapshot_microns_per_pixel_x": 1.25,
"oav_grid_snapshot_microns_per_pixel_y": 1.25,
"oav_grid_snapshot_last_path_full_overlay": "tmp/test_0_grid_overlay.png",
"oav_grid_snapshot_last_path_outer": "tmp/test_0_outer_overlay.png",
"oav_grid_snapshot_last_saved_path": "tmp/test_0.png",
},
)
assert_event(
cb.activity_gated_event.mock_calls[1], # pyright:ignore
{
"oav_snapshot_top_left_x": 8,
"oav_snapshot_top_left_y": 2,
"oav_snapshot_num_boxes_x": 8,
"oav_snapshot_num_boxes_y": 1,
"oav_snapshot_box_width": 16,
"oav_snapshot_microns_per_pixel_x": 1.25,
"oav_snapshot_microns_per_pixel_y": 1.25,
"oav_snapshot_last_path_full_overlay": "tmp/test_90_grid_overlay.png",
"oav_snapshot_last_path_outer": "tmp/test_90_outer_overlay.png",
"oav_snapshot_last_saved_path": "tmp/test_90.png",
"oav_grid_snapshot_top_left_x": 8,
"oav_grid_snapshot_top_left_y": 2,
"oav_grid_snapshot_num_boxes_x": 8,
"oav_grid_snapshot_num_boxes_y": 1,
"oav_grid_snapshot_box_width": 16,
"oav_grid_snapshot_microns_per_pixel_x": 1.25,
"oav_grid_snapshot_microns_per_pixel_y": 1.25,
"oav_grid_snapshot_last_path_full_overlay": "tmp/test_90_grid_overlay.png",
"oav_grid_snapshot_last_path_outer": "tmp/test_90_outer_overlay.png",
"oav_grid_snapshot_last_saved_path": "tmp/test_90.png",
},
)

Expand Down Expand Up @@ -359,7 +358,10 @@ def test_when_detected_grid_has_odd_y_steps_then_add_a_y_step_and_shift_grid(
box_size_y_pixels = box_size_um / composite.oav.parameters.micronsPerYPixel
initial_min_y = 1

abs_sets: dict[str, list] = {"snapshot.top_left_y": [], "snapshot.num_boxes_y": []}
abs_sets: dict[str, list] = {
"grid_snapshot.top_left_y": [],
"grid_snapshot.num_boxes_y": [],
}

def handle_read(msg: Msg):
if msg.obj.name == "pin_tip_detection-triggered_tip":
Expand Down Expand Up @@ -414,8 +416,8 @@ def record_set(msg: Msg):
else:
fake_logger.debug.assert_not_called()

assert abs_sets["snapshot.top_left_y"][0] == expected_min_y
assert abs_sets["snapshot.num_boxes_y"][0] == expected_y_steps
assert abs_sets["grid_snapshot.top_left_y"][0] == expected_min_y
assert abs_sets["grid_snapshot.num_boxes_y"][0] == expected_y_steps


@pytest.mark.parametrize(
Expand Down
40 changes: 20 additions & 20 deletions tests/unit_tests/external_interaction/callbacks/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,16 +157,16 @@ class TestData:
"seq_num": 1,
"uid": "29033ecf-e052-43dd-98af-c7cdd62e8174",
"data": {
"oav_snapshot_top_left_x": 50,
"oav_snapshot_top_left_y": 100,
"oav_snapshot_num_boxes_x": 40,
"oav_snapshot_num_boxes_y": 20,
"oav_snapshot_microns_per_pixel_x": 1.25,
"oav_snapshot_microns_per_pixel_y": 1.5,
"oav_snapshot_box_width": 0.1 * 1000 / 1.25, # size in pixels
"oav_snapshot_last_path_full_overlay": "test_1_y",
"oav_snapshot_last_path_outer": "test_2_y",
"oav_snapshot_last_saved_path": "test_3_y",
"oav_grid_snapshot_top_left_x": 50,
"oav_grid_snapshot_top_left_y": 100,
"oav_grid_snapshot_num_boxes_x": 40,
"oav_grid_snapshot_num_boxes_y": 20,
"oav_grid_snapshot_microns_per_pixel_x": 1.25,
"oav_grid_snapshot_microns_per_pixel_y": 1.5,
"oav_grid_snapshot_box_width": 0.1 * 1000 / 1.25, # size in pixels
"oav_grid_snapshot_last_path_full_overlay": "test_1_y",
"oav_grid_snapshot_last_path_outer": "test_2_y",
"oav_grid_snapshot_last_saved_path": "test_3_y",
},
}
test_event_document_oav_snapshot_xz: Event = {
Expand All @@ -176,16 +176,16 @@ class TestData:
"seq_num": 1,
"uid": "29033ecf-e052-43dd-98af-c7cdd62e8174",
"data": {
"oav_snapshot_top_left_x": 50,
"oav_snapshot_top_left_y": 0,
"oav_snapshot_num_boxes_x": 40,
"oav_snapshot_num_boxes_y": 10,
"oav_snapshot_microns_per_pixel_x": 1.25,
"oav_snapshot_microns_per_pixel_y": 1.5,
"oav_snapshot_box_width": 0.1 * 1000 / 1.25, # size in pixels
"oav_snapshot_last_path_full_overlay": "test_1_z",
"oav_snapshot_last_path_outer": "test_2_z",
"oav_snapshot_last_saved_path": "test_3_z",
"oav_grid_snapshot_top_left_x": 50,
"oav_grid_snapshot_top_left_y": 0,
"oav_grid_snapshot_num_boxes_x": 40,
"oav_grid_snapshot_num_boxes_y": 10,
"oav_grid_snapshot_box_width": 0.1 * 1000 / 1.25, # size in pixels
"oav_grid_snapshot_last_path_full_overlay": "test_1_z",
"oav_grid_snapshot_last_path_outer": "test_2_z",
"oav_grid_snapshot_last_saved_path": "test_3_z",
"oav_grid_snapshot_microns_per_pixel_x": 1.25,
"oav_grid_snapshot_microns_per_pixel_y": 1.5,
},
}
test_event_document_pre_data_collection: Event = {
Expand Down

0 comments on commit 2b10b42

Please sign in to comment.