Skip to content

Commit

Permalink
(DiamondLightSource/hyperion#1283) Automatically apply the snapshot m…
Browse files Browse the repository at this point in the history
…icrons_per_pixel when the snapshot is triggered instead of requiring the plan to apply it manually.
  • Loading branch information
rtuck99 committed Apr 12, 2024
1 parent 58810ec commit 00388e4
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/dodal/devices/oav/grid_overlay.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ class SnapshotWithGrid(MJPG):
last_path_outer = Component(Signal)
last_path_full_overlay = Component(Signal)

# scaling factors for the snapshot at the time it was triggered
microns_per_pixel_x = Component(Signal)
microns_per_pixel_y = Component(Signal)

Expand Down
17 changes: 17 additions & 0 deletions src/dodal/devices/oav/oav_detector.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ def __init__(self, *args, params: OAVConfigParams, **kwargs):
super().__init__(*args, **kwargs)
self.parameters = params
self.subscription_id = None
self._snapshot_trigger_subscription_id = None

def wait_for_connection(self, all_signals=False, timeout=2):
connected = super().wait_for_connection(all_signals, timeout)
Expand All @@ -213,4 +214,20 @@ def wait_for_connection(self, all_signals=False, timeout=2):
self.zoom_controller.level.unsubscribe(self.subscription_id)
self.subscription_id = self.zoom_controller.level.subscribe(cb)

self.add_snapshot_trigger_hook()

return connected

def add_snapshot_trigger_hook(self):
def apply_current_microns_per_pixel_to_snapshot(*args, **kwargs):
"""Persist the current value of the mpp to the snapshot so that it is retained if zoom changes"""
microns_per_x_pixel = self.parameters.micronsPerXPixel
microns_per_y_pixel = self.parameters.micronsPerYPixel
self.snapshot.microns_per_pixel_x.put(microns_per_x_pixel)
self.snapshot.microns_per_pixel_y.put(microns_per_y_pixel)

self._snapshot_trigger_subscription_id = (
self.snapshot.last_saved_path.subscribe(
apply_current_microns_per_pixel_to_snapshot
)
)
20 changes: 20 additions & 0 deletions tests/devices/unit_tests/test_oav.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,14 @@ def fake_oav() -> OAV:
fake_oav.snapshot.box_width.put(50)
fake_oav.snapshot.num_boxes_x.put(15)
fake_oav.snapshot.num_boxes_y.put(10)
fake_oav.snapshot.x_size.sim_put(1024) # type: ignore
fake_oav.snapshot.y_size.sim_put(768) # type: ignore

fake_oav.cam.port_name.sim_put("CAM") # type: ignore
fake_oav.proc.port_name.sim_put("PROC") # type: ignore

fake_oav.wait_for_connection()
fake_oav.zoom_controller.set("1.0x").wait()

return fake_oav

Expand Down Expand Up @@ -79,6 +82,23 @@ def test_snapshot_trigger_saves_to_correct_file(
assert calls_to_save == expected_calls_to_save


@patch("requests.get")
@patch("dodal.devices.areadetector.plugins.MJPG.Image.open")
def test_snapshot_trigger_applies_current_microns_per_pixel_to_snapshot(
mock_open: MagicMock, mock_get, fake_oav
):
image = PIL.Image.open("test") # type: ignore
mock_open.return_value.__enter__.return_value = image

expected_mpp_x = fake_oav.parameters.micronsPerXPixel
expected_mpp_y = fake_oav.parameters.micronsPerYPixel
with patch.object(image, "save"):
st = fake_oav.snapshot.trigger()
st.wait()
assert fake_oav.snapshot.microns_per_pixel_x.get() == expected_mpp_x
assert fake_oav.snapshot.microns_per_pixel_y.get() == expected_mpp_y


@patch("requests.get")
@patch("dodal.devices.areadetector.plugins.MJPG.Image.open")
@patch("dodal.devices.oav.grid_overlay.add_grid_overlay_to_image")
Expand Down

0 comments on commit 00388e4

Please sign in to comment.