Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changes to support hyperion 1283 use pixels per micron from file not gda #423

Merged
merged 4 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/dodal/beamlines/i03.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,11 @@ def panda_fast_grid_scan(


@skip_device(lambda: BL == "s03")
def oav(wait_for_connection: bool = True, fake_with_ophyd_sim: bool = False) -> OAV:
def oav(
wait_for_connection: bool = True,
fake_with_ophyd_sim: bool = False,
params: OAVConfigParams | None = None,
) -> OAV:
"""Get the i03 OAV device, instantiate it if it hasn't already been.
If this is called when already instantiated in i03, it will return the existing object.
"""
Expand All @@ -218,7 +222,7 @@ def oav(wait_for_connection: bool = True, fake_with_ophyd_sim: bool = False) ->
"",
wait_for_connection,
fake_with_ophyd_sim,
params=OAVConfigParams(ZOOM_PARAMS_FILE, DISPLAY_CONFIG),
params=params or OAVConfigParams(ZOOM_PARAMS_FILE, DISPLAY_CONFIG),
)


Expand Down
4 changes: 4 additions & 0 deletions src/dodal/devices/oav/grid_overlay.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ 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)

def post_processing(self, image: Image.Image):
top_left_x = self.top_left_x.get()
top_left_y = self.top_left_y.get()
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
Loading