Skip to content

Commit

Permalink
(#471) Add snapshot that draws beam centre crosshair
Browse files Browse the repository at this point in the history
  • Loading branch information
DominicOram committed Apr 25, 2024
1 parent f088f7a commit fad4c8c
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/dodal/devices/areadetector/plugins/MJPG.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import threading
from os.path import join as path_join
from pathlib import Path

import requests
from ophyd import Component, Device, DeviceStatus, EpicsSignal, EpicsSignalRO, Signal
from PIL import Image
from PIL import Image, ImageDraw

from dodal.devices.oav.oav_parameters import OAVConfigParams
from dodal.log import LOGGER
Expand Down Expand Up @@ -57,3 +58,23 @@ def get_snapshot():

def post_processing(self, image: Image.Image):
pass


class SnapshotWithBeamCentre(MJPG):
CROSSHAIR_LENGTH = 20

def post_processing(self, image: Image.Image):
assert self.oav_params is not None
beam_x = self.oav_params.beam_centre_i
beam_y = self.oav_params.beam_centre_j

draw = ImageDraw.Draw(image)
HALF_LEN = self.CROSSHAIR_LENGTH / 2
draw.line(((beam_x, beam_y - HALF_LEN), (beam_x, beam_y + HALF_LEN)))
draw.line(((beam_x - HALF_LEN, beam_y), (beam_x + HALF_LEN, beam_y)))

filename_str = self.filename.get()
directory_str = self.directory.get()

path = path_join(directory_str, f"{filename_str}_with_crosshair.png")
image.save(path)
3 changes: 3 additions & 0 deletions src/dodal/devices/oav/oav_detector.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
StatusBase,
)

from dodal.devices.areadetector.plugins.MJPG import SnapshotWithBeamCentre
from dodal.devices.oav.grid_overlay import SnapshotWithGrid
from dodal.devices.oav.oav_parameters import OAVConfigParams

Expand Down Expand Up @@ -78,12 +79,14 @@ class OAV(AreaDetector):
tiff = ADC(OverlayPlugin, "-DI-OAV-01:TIFF:")
hdf5 = ADC(HDF5Plugin, "-DI-OAV-01:HDF5:")
grid_snapshot = Component(SnapshotWithGrid, "-DI-OAV-01:MJPG:")
snapshot = Component(SnapshotWithBeamCentre, "-DI-OAV-01:MJPG:")
zoom_controller = Component(ZoomController, "-EA-OAV-01:FZOOM:")

def __init__(self, *args, params: OAVConfigParams, **kwargs):
super().__init__(*args, **kwargs)
self.parameters = params
self.grid_snapshot.oav_params = params
self.snapshot.oav_params = params
self.subscription_id = None
self._snapshot_trigger_subscription_id = None

Expand Down
31 changes: 31 additions & 0 deletions tests/devices/unit_tests/areadetector/plugins/test_MJPG.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from unittest.mock import MagicMock, patch

from ophyd.sim import instantiate_fake_device

from dodal.devices.areadetector.plugins.MJPG import SnapshotWithBeamCentre
from dodal.devices.oav.oav_detector import OAVConfigParams

DISPLAY_CONFIGURATION = "tests/devices/unit_tests/test_display.configuration"
ZOOM_LEVELS_XML = "tests/devices/unit_tests/test_jCameraManZoomLevels.xml"


@patch("dodal.devices.areadetector.plugins.MJPG.Image")
@patch("dodal.devices.areadetector.plugins.MJPG.ImageDraw")
@patch("dodal.devices.areadetector.plugins.MJPG.requests")
def test_given_snapshot_triggered_then_crosshair_drawn(
patch_requests, patch_image_draw, patch_image
):
patch_line = MagicMock()
params = OAVConfigParams(ZOOM_LEVELS_XML, DISPLAY_CONFIGURATION)
params.update_on_zoom(1.0, 100, 100)

patch_image_draw.Draw.return_value.line = patch_line
snapshot: SnapshotWithBeamCentre = instantiate_fake_device(SnapshotWithBeamCentre)
snapshot.oav_params = params
snapshot.directory.set("/tmp/")
snapshot.filename.set("test")

status = snapshot.trigger()
status.wait()

assert len(patch_line.mock_calls) == 2
5 changes: 5 additions & 0 deletions tests/devices/unit_tests/oav/test_oav.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,8 @@ def test_calculate_beam_distance(h, v, expected_x, expected_y, oav: OAV):
h,
v,
) == (expected_x, expected_y)


def test_when_oav_created_then_snapshot_parameters_set(oav: OAV):
assert oav.snapshot.oav_params is not None
assert oav.grid_snapshot.oav_params is not None

0 comments on commit fad4c8c

Please sign in to comment.