Skip to content
This repository has been archived by the owner on Sep 2, 2024. It is now read-only.

Commit

Permalink
Merge pull request #904 from DiamondLightSource/902__fake_grid_detection
Browse files Browse the repository at this point in the history
902  fake grid detection
  • Loading branch information
dperl-dls authored Sep 27, 2023
2 parents c10c772 + e37874b commit f2ed315
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 24 deletions.
5 changes: 3 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"python.testing.pytestArgs": [],
"python.testing.unittestEnabled": false,
"python.testing.pytestEnabled": true,
"python.formatting.provider": "black",
"python.formatting.provider": "none",
"python.analysis.autoImportCompletions": true,
"python.languageServer": "Pylance",
"editor.formatOnSave": true,
Expand All @@ -18,7 +18,8 @@
"editor.codeActionsOnSave": {
"source.fixAll.ruff": false,
"source.organizeImports.ruff": true
}
},
"editor.defaultFormatter": "ms-python.black-formatter"
},
"terminal.integrated.gpuAcceleration": "off",
"python.analysis.typeCheckingMode": "basic",
Expand Down
10 changes: 7 additions & 3 deletions src/hyperion/device_setup_plans/unit_tests/test_setup_oav.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ def mock_parameters():
)


@pytest.fixture
def smargon() -> Smargon:
def fake_smargon() -> Smargon:
smargon = i03.smargon(fake_with_ophyd_sim=True)
smargon.x.user_setpoint._use_limits = False
smargon.y.user_setpoint._use_limits = False
Expand All @@ -66,7 +65,12 @@ def patch_motor(motor):
with patch_motor(smargon.omega), patch_motor(smargon.x), patch_motor(
smargon.y
), patch_motor(smargon.z):
yield smargon
return smargon


@pytest.fixture
def smargon():
yield fake_smargon()


@pytest.mark.parametrize(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from typing import Dict, Generator
from unittest.mock import ANY, MagicMock, patch

import bluesky.plan_stubs as bps
import pytest
from bluesky.run_engine import RunEngine
from dodal.beamlines import i03
from dodal.beamlines.i03 import detector_motion
from dodal.devices.aperturescatterguard import AperturePositions, ApertureScatterguard
from dodal.devices.backlight import Backlight
Expand All @@ -11,6 +13,7 @@
from dodal.devices.oav.oav_parameters import OAVParameters
from numpy.testing import assert_array_equal

from hyperion.device_setup_plans.unit_tests.test_setup_oav import fake_smargon
from hyperion.experiment_plans.grid_detect_then_xray_centre_plan import (
create_devices,
detect_grid_and_do_gridscan,
Expand All @@ -35,7 +38,28 @@ def _fake_grid_detection(
grid_width_microns: float = 0,
box_size_um: float = 0.0,
):
return []
oav = i03.oav(fake_with_ophyd_sim=True)
smargon = fake_smargon()
yield from bps.open_run()
oav.snapshot.box_width.put(635.00986)

# first grid detection: x * y
oav.snapshot.num_boxes_x.put(10)
oav.snapshot.num_boxes_y.put(3)
yield from bps.create("snapshot_to_ispyb")
yield from bps.read(oav.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)
yield from bps.create("snapshot_to_ispyb")
yield from bps.read(oav.snapshot)
yield from bps.read(smargon)
yield from bps.save()

yield from bps.close_run()


@patch(
Expand Down Expand Up @@ -96,9 +120,6 @@ def test_full_grid_scan(test_fgs_params, test_config_files):
"hyperion.experiment_plans.grid_detect_then_xray_centre_plan.OavSnapshotCallback",
autospec=True,
)
@pytest.mark.skip(
reason="this is not a real plane so it needs real plan in order to carry out tests. For now the tests have been skipped and issue added to skip in Github"
)
def test_detect_grid_and_do_gridscan(
mock_oav_callback_init: MagicMock,
mock_flyscan_xray_centre_plan: MagicMock,
Expand Down Expand Up @@ -167,9 +188,6 @@ def test_detect_grid_and_do_gridscan(
"hyperion.experiment_plans.grid_detect_then_xray_centre_plan.OavSnapshotCallback",
autospec=True,
)
@pytest.mark.skip(
reason="this is not a real plane so it needs real plan in order to carry out tests. For now the tests have been skipped and issue added to skip in Github"
)
def test_when_full_grid_scan_run_then_parameters_sent_to_fgs_as_expected(
mock_oav_callback_init: MagicMock,
mock_flyscan_xray_centre_plan: MagicMock,
Expand Down Expand Up @@ -226,7 +244,7 @@ def test_when_full_grid_scan_run_then_parameters_sent_to_fgs_as_expected(
assert params.hyperion_params.detector_params.num_triggers == 40

assert params.experiment_params.x_axis.full_steps == 10
assert params.experiment_params.y_axis.end == 1
assert params.experiment_params.y_axis.end == pytest.approx(1, 0.001)

# Parameters can be serialized
params.json()
21 changes: 12 additions & 9 deletions src/hyperion/experiment_plans/tests/test_grid_detection_plan.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from bluesky.run_engine import RunEngine
from dodal.beamlines import i03
from dodal.devices.backlight import Backlight
from dodal.devices.fast_grid_scan import GridAxis, GridScanParams
from dodal.devices.fast_grid_scan import GridAxis
from dodal.devices.oav.oav_detector import OAV
from dodal.devices.oav.oav_parameters import OAVParameters
from dodal.devices.smargon import Smargon
Expand Down Expand Up @@ -138,10 +138,11 @@ def test_given_when_grid_detect_then_upper_left_and_start_position_as_expected(
params.micronsPerYPixel = 0.1
params.beam_centre_i = 4
params.beam_centre_j = 4
gridscan_params = GridScanParams()

cb = OavSnapshotCallback()
RE.subscribe(cb)
oav_cb = OavSnapshotCallback()
grid_param_cb = GridDetectionCallback(params, 0.004)
RE.subscribe(oav_cb)
RE.subscribe(grid_param_cb)
RE(
grid_detection_plan(
parameters=params,
Expand All @@ -153,12 +154,14 @@ def test_given_when_grid_detect_then_upper_left_and_start_position_as_expected(
)

# 8, 2 based on tip x, and lowest value in the top array
assert cb.out_upper_left[0] == [8, 2]
assert cb.out_upper_left[1] == [8, 2]
assert oav_cb.out_upper_left[0] == [8, 2]
assert oav_cb.out_upper_left[1] == [8, 2]

gridscan_params = grid_param_cb.get_grid_parameters()

assert gridscan_params.x_start == 0.1
assert gridscan_params.y1_start == 0.1
assert gridscan_params.z1_start == 0.1
assert gridscan_params.x_start == pytest.approx(0.0005)
assert gridscan_params.y1_start == pytest.approx(-0.0001)
assert gridscan_params.z1_start == pytest.approx(-0.0001)


@patch("dodal.beamlines.beamline_utils.active_device_is_same_type", lambda a, b: True)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ def __init__(self, *args) -> None:
super().__init__(*args)
self.snapshot_filenames: list = []
self.out_upper_left: list = []
self.box_widths: list = []
self.no_of_boxes: list = []

def event(self, doc):
data = doc.get("data")
Expand Down

0 comments on commit f2ed315

Please sign in to comment.