Skip to content

Commit 76f53b0

Browse files
committed
Fixes and tests
1 parent 46096ff commit 76f53b0

File tree

9 files changed

+221
-130
lines changed

9 files changed

+221
-130
lines changed

src/dodal/devices/i03/beamsize.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def __init__(self, aperture_scatterguard: ApertureScatterguard, name="beamsize")
1616
derived_units="µm",
1717
)
1818
self.y_um = derived_signal_r(
19-
self._get_beamsize_x,
19+
self._get_beamsize_y,
2020
aperture_radius=self._aperture_scatterguard_ref().radius,
2121
derived_units="µm",
2222
)

src/dodal/devices/i03/constants.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33

44
@dataclass(frozen=True)
55
class BeamsizeConstants:
6-
BEAM_HEIGHT_UM = 20.0
76
BEAM_WIDTH_UM = 80.0
7+
BEAM_HEIGHT_UM = 20.0

tests/devices/conftest.py

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
import asyncio
2+
from collections.abc import AsyncGenerator
3+
4+
import pytest
5+
from ophyd_async.core import init_devices
6+
7+
from dodal.common.beamlines.beamline_parameters import GDABeamlineParameters
8+
from dodal.devices.aperturescatterguard import (
9+
AperturePosition,
10+
ApertureScatterguard,
11+
ApertureValue,
12+
load_positions_from_beamline_parameters,
13+
)
14+
from dodal.testing import patch_all_motors
15+
16+
17+
@pytest.fixture
18+
def aperture_positions() -> dict[ApertureValue, AperturePosition]:
19+
return load_positions_from_beamline_parameters(
20+
GDABeamlineParameters(
21+
params={
22+
"miniap_x_LARGE_APERTURE": 2.389,
23+
"miniap_y_LARGE_APERTURE": 40.986,
24+
"miniap_z_LARGE_APERTURE": 15.8,
25+
"sg_x_LARGE_APERTURE": 5.25,
26+
"sg_y_LARGE_APERTURE": 4.43,
27+
"miniap_x_MEDIUM_APERTURE": 2.384,
28+
"miniap_y_MEDIUM_APERTURE": 44.967,
29+
"miniap_z_MEDIUM_APERTURE": 15.8,
30+
"sg_x_MEDIUM_APERTURE": 5.285,
31+
"sg_y_MEDIUM_APERTURE": 0.46,
32+
"miniap_x_SMALL_APERTURE": 2.430,
33+
"miniap_y_SMALL_APERTURE": 48.974,
34+
"miniap_z_SMALL_APERTURE": 15.8,
35+
"sg_x_SMALL_APERTURE": 5.3375,
36+
"sg_y_SMALL_APERTURE": -3.55,
37+
"miniap_x_ROBOT_LOAD": 2.386,
38+
"miniap_y_ROBOT_LOAD": 31.40,
39+
"miniap_z_ROBOT_LOAD": 15.8,
40+
"sg_x_ROBOT_LOAD": 5.25,
41+
"sg_y_ROBOT_LOAD": 4.43,
42+
"miniap_x_MANUAL_LOAD": -4.91,
43+
"miniap_y_MANUAL_LOAD": -48.70,
44+
"miniap_z_MANUAL_LOAD": -10.0,
45+
"sg_x_MANUAL_LOAD": -4.7,
46+
"sg_y_MANUAL_LOAD": 1.8,
47+
}
48+
)
49+
)
50+
51+
52+
@pytest.fixture
53+
def aperture_tolerances():
54+
return AperturePosition.tolerances_from_gda_params(
55+
GDABeamlineParameters(
56+
{
57+
"miniap_x_tolerance": 0.004,
58+
"miniap_y_tolerance": 0.1,
59+
"miniap_z_tolerance": 0.1,
60+
"sg_x_tolerance": 0.1,
61+
"sg_y_tolerance": 0.1,
62+
}
63+
)
64+
)
65+
66+
67+
@pytest.fixture
68+
async def ap_sg(
69+
aperture_positions: dict[ApertureValue, AperturePosition],
70+
aperture_tolerances: AperturePosition,
71+
) -> AsyncGenerator[ApertureScatterguard]:
72+
async with init_devices(mock=True):
73+
ap_sg = ApertureScatterguard(
74+
aperture_prefix="-MO-MAPT-01:",
75+
scatterguard_prefix="-MO-SCAT-01:",
76+
name="test_ap_sg",
77+
loaded_positions=aperture_positions,
78+
tolerances=aperture_tolerances,
79+
)
80+
81+
with patch_all_motors(ap_sg):
82+
yield ap_sg
83+
84+
85+
async def set_to_position(
86+
aperture_scatterguard: ApertureScatterguard, position: AperturePosition
87+
):
88+
aperture_x, aperture_y, aperture_z, scatterguard_x, scatterguard_y = position.values
89+
90+
await asyncio.gather(
91+
aperture_scatterguard.aperture.x.set(aperture_x),
92+
aperture_scatterguard.aperture.y.set(aperture_y),
93+
aperture_scatterguard.aperture.z.set(aperture_z),
94+
aperture_scatterguard.scatterguard.x.set(scatterguard_x),
95+
aperture_scatterguard.scatterguard.y.set(scatterguard_y),
96+
)

tests/devices/i03/test_beamsize.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import pytest
2+
from ophyd_async.testing import set_mock_value
3+
4+
from dodal.devices.aperturescatterguard import (
5+
AperturePosition,
6+
ApertureScatterguard,
7+
ApertureValue,
8+
)
9+
from dodal.devices.i03.beamsize import Beamsize
10+
from tests.devices.conftest import set_to_position
11+
12+
13+
@pytest.mark.parametrize(
14+
"selected_aperture, aperture_signal_values, expected_beamsize",
15+
[
16+
(ApertureValue.SMALL, (1, 0, 0), (20.0, 20.0)),
17+
(ApertureValue.MEDIUM, (0, 1, 0), (50.0, 20.0)),
18+
(ApertureValue.LARGE, (0, 0, 1), (80.0, 20.0)),
19+
(ApertureValue.OUT_OF_BEAM, (0, 0, 0), (0.0, 0.0)),
20+
(ApertureValue.PARKED, (0, 0, 0), (0.0, 0.0)),
21+
],
22+
)
23+
async def test_beamsize_gives_min_of_aperture_and_beam_width_and_height(
24+
selected_aperture: ApertureValue,
25+
aperture_signal_values: tuple[int, int, int],
26+
expected_beamsize: tuple[float, float],
27+
ap_sg: ApertureScatterguard,
28+
aperture_positions: dict[ApertureValue, AperturePosition],
29+
):
30+
await set_to_position(ap_sg, aperture_positions[selected_aperture])
31+
32+
for i, signal in enumerate(
33+
(ap_sg.aperture.small, ap_sg.aperture.medium, ap_sg.aperture.large)
34+
):
35+
set_mock_value(signal, aperture_signal_values[i])
36+
37+
beamsize = Beamsize(aperture_scatterguard=ap_sg)
38+
beamsize_x = await beamsize.x_um.get_value()
39+
beamsize_y = await beamsize.y_um.get_value()
40+
assert beamsize_x == expected_beamsize[0]
41+
assert beamsize_y == expected_beamsize[1]

tests/devices/i04/conftest.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import pytest
2+
from ophyd_async.core import init_devices
3+
4+
from dodal.devices.i04.transfocator import Transfocator
5+
6+
7+
@pytest.fixture
8+
async def fake_transfocator() -> Transfocator:
9+
async with init_devices(mock=True):
10+
transfocator = Transfocator(prefix="", name="transfocator")
11+
return transfocator

tests/devices/i04/test_beamsize.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import pytest
2+
from ophyd_async.testing import set_mock_value
3+
4+
from dodal.devices.aperturescatterguard import (
5+
AperturePosition,
6+
ApertureScatterguard,
7+
ApertureValue,
8+
)
9+
from dodal.devices.i04.beamsize import Beamsize
10+
from dodal.devices.i04.transfocator import Transfocator
11+
from tests.devices.conftest import set_to_position
12+
13+
14+
@pytest.mark.parametrize(
15+
"selected_aperture, aperture_signal_values, transfocator_sizes, expected_beamsize",
16+
[
17+
(ApertureValue.SMALL, (1, 0, 0), (10.0, 10.0), (10.0, 10.0)),
18+
(ApertureValue.SMALL, (1, 0, 0), (30.0, 10.0), (20.0, 10.0)),
19+
(ApertureValue.SMALL, (1, 0, 0), (55.0, 30.0), (20.0, 20.0)),
20+
(ApertureValue.SMALL, (1, 0, 0), (70.0, 30.0), (20.0, 20.0)),
21+
(ApertureValue.SMALL, (1, 0, 0), (90.0, 55.0), (20.0, 20.0)),
22+
(ApertureValue.MEDIUM, (0, 1, 0), (10.0, 55.0), (10.0, 50.0)),
23+
(ApertureValue.MEDIUM, (0, 1, 0), (30.0, 70.0), (30.0, 50.0)),
24+
(ApertureValue.MEDIUM, (0, 1, 0), (55.0, 70.0), (50.0, 50.0)),
25+
(ApertureValue.MEDIUM, (0, 1, 0), (70.0, 90.0), (50.0, 50.0)),
26+
(ApertureValue.MEDIUM, (0, 1, 0), (90.0, 90.0), (50.0, 50.0)),
27+
(ApertureValue.LARGE, (0, 0, 1), (10.0, 90.0), (10.0, 90.0)),
28+
(ApertureValue.LARGE, (0, 0, 1), (30.0, 90.0), (30.0, 90.0)),
29+
(ApertureValue.LARGE, (0, 0, 1), (55.0, 70.0), (55.0, 70.0)),
30+
(ApertureValue.LARGE, (0, 0, 1), (70.0, 70.0), (70.0, 70.0)),
31+
(ApertureValue.LARGE, (0, 0, 1), (90.0, 55.0), (90.0, 55.0)),
32+
(ApertureValue.LARGE, (0, 0, 1), (120.0, 120.0), (100.0, 100.0)),
33+
(ApertureValue.OUT_OF_BEAM, (0, 0, 0), (120.0, 10.0), (0.0, 0.0)),
34+
(ApertureValue.OUT_OF_BEAM, (0, 0, 0), (10.0, 120.0), (0.0, 0.0)),
35+
(ApertureValue.PARKED, (0, 0, 0), (10.0, 10.0), (0.0, 0.0)),
36+
(ApertureValue.PARKED, (0, 0, 0), (120.0, 120.0), (0.0, 0.0)),
37+
],
38+
)
39+
async def test_beamsize_gives_min_of_aperture_and_transfocator_width_and_height(
40+
selected_aperture: ApertureValue,
41+
aperture_signal_values: tuple[int, int, int],
42+
transfocator_sizes: tuple[float, float],
43+
expected_beamsize: tuple[float, float],
44+
fake_transfocator: Transfocator,
45+
ap_sg: ApertureScatterguard,
46+
aperture_positions: dict[ApertureValue, AperturePosition],
47+
):
48+
await set_to_position(ap_sg, aperture_positions[selected_aperture])
49+
50+
for i, signal in enumerate(
51+
(ap_sg.aperture.small, ap_sg.aperture.medium, ap_sg.aperture.large)
52+
):
53+
set_mock_value(signal, aperture_signal_values[i])
54+
set_mock_value(fake_transfocator.current_horizontal_size_rbv, transfocator_sizes[0])
55+
set_mock_value(fake_transfocator.current_vertical_size_rbv, transfocator_sizes[1])
56+
57+
beamsize = Beamsize(transfocator=fake_transfocator, aperture_scatterguard=ap_sg)
58+
59+
beamsize_x = await beamsize.x_um.get_value()
60+
beamsize_y = await beamsize.y_um.get_value()
61+
assert beamsize_x == expected_beamsize[0]
62+
assert beamsize_y == expected_beamsize[1]

tests/devices/i04/test_transfocator.py

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,12 @@
33

44
import pytest
55
from bluesky.protocols import Reading
6-
from ophyd_async.core import (
7-
init_devices,
8-
wait_for_value,
9-
)
6+
from ophyd_async.core import wait_for_value
107
from ophyd_async.testing import set_mock_value
118

129
from dodal.devices.i04.transfocator import Transfocator
1310

1411

15-
@pytest.fixture
16-
async def fake_transfocator() -> Transfocator:
17-
async with init_devices(mock=True):
18-
transfocator = Transfocator(prefix="", name="transfocator")
19-
return transfocator
20-
21-
2212
def given_predicted_lenses_is_half_of_beamsize(transfocator: Transfocator):
2313
def lens_number_is_half_beamsize(
2414
reading: dict[str, Reading[float]], *args, **kwargs

tests/devices/test_aperture_scatterguard.py

Lines changed: 8 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,36 @@
1-
import asyncio
21
from collections.abc import AsyncGenerator
32
from typing import Any
43
from unittest.mock import AsyncMock, call
54

65
import bluesky.plan_stubs as bps
76
import pytest
87
from bluesky.run_engine import RunEngine
9-
from ophyd_async.core import init_devices
108
from ophyd_async.testing import (
119
callback_on_mock_put,
1210
get_mock,
1311
get_mock_put,
1412
set_mock_value,
1513
)
1614

17-
from dodal.common.beamlines.beamline_parameters import GDABeamlineParameters
1815
from dodal.devices.aperturescatterguard import (
1916
AperturePosition,
2017
ApertureScatterguard,
2118
ApertureValue,
2219
InvalidApertureMoveError,
23-
load_positions_from_beamline_parameters,
2420
)
25-
from dodal.testing import patch_all_motors
21+
from tests.devices.conftest import set_to_position
2622

2723

2824
@pytest.fixture
29-
def aperture_positions() -> dict[ApertureValue, AperturePosition]:
30-
return load_positions_from_beamline_parameters(
31-
GDABeamlineParameters(
32-
params={
33-
"miniap_x_LARGE_APERTURE": 2.389,
34-
"miniap_y_LARGE_APERTURE": 40.986,
35-
"miniap_z_LARGE_APERTURE": 15.8,
36-
"sg_x_LARGE_APERTURE": 5.25,
37-
"sg_y_LARGE_APERTURE": 4.43,
38-
"miniap_x_MEDIUM_APERTURE": 2.384,
39-
"miniap_y_MEDIUM_APERTURE": 44.967,
40-
"miniap_z_MEDIUM_APERTURE": 15.8,
41-
"sg_x_MEDIUM_APERTURE": 5.285,
42-
"sg_y_MEDIUM_APERTURE": 0.46,
43-
"miniap_x_SMALL_APERTURE": 2.430,
44-
"miniap_y_SMALL_APERTURE": 48.974,
45-
"miniap_z_SMALL_APERTURE": 15.8,
46-
"sg_x_SMALL_APERTURE": 5.3375,
47-
"sg_y_SMALL_APERTURE": -3.55,
48-
"miniap_x_ROBOT_LOAD": 2.386,
49-
"miniap_y_ROBOT_LOAD": 31.40,
50-
"miniap_z_ROBOT_LOAD": 15.8,
51-
"sg_x_ROBOT_LOAD": 5.25,
52-
"sg_y_ROBOT_LOAD": 4.43,
53-
"miniap_x_MANUAL_LOAD": -4.91,
54-
"miniap_y_MANUAL_LOAD": -48.70,
55-
"miniap_z_MANUAL_LOAD": -10.0,
56-
"sg_x_MANUAL_LOAD": -4.7,
57-
"sg_y_MANUAL_LOAD": 1.8,
58-
}
59-
)
60-
)
25+
async def aperture_in_medium_pos(
26+
ap_sg: ApertureScatterguard,
27+
aperture_positions: dict[ApertureValue, AperturePosition],
28+
) -> AsyncGenerator[ApertureScatterguard, None]:
29+
await set_to_position(ap_sg, aperture_positions[ApertureValue.MEDIUM])
6130

31+
set_mock_value(ap_sg.aperture.medium, 1)
6232

63-
@pytest.fixture
64-
def aperture_tolerances():
65-
return AperturePosition.tolerances_from_gda_params(
66-
GDABeamlineParameters(
67-
{
68-
"miniap_x_tolerance": 0.004,
69-
"miniap_y_tolerance": 0.1,
70-
"miniap_z_tolerance": 0.1,
71-
"sg_x_tolerance": 0.1,
72-
"sg_y_tolerance": 0.1,
73-
}
74-
)
75-
)
33+
yield ap_sg
7634

7735

7836
def get_all_motors(ap_sg: ApertureScatterguard):
@@ -85,50 +43,6 @@ def get_all_motors(ap_sg: ApertureScatterguard):
8543
]
8644

8745

88-
@pytest.fixture
89-
async def ap_sg(
90-
aperture_positions: dict[ApertureValue, AperturePosition],
91-
aperture_tolerances: AperturePosition,
92-
) -> AsyncGenerator[ApertureScatterguard]:
93-
async with init_devices(mock=True):
94-
ap_sg = ApertureScatterguard(
95-
aperture_prefix="-MO-MAPT-01:",
96-
scatterguard_prefix="-MO-SCAT-01:",
97-
name="test_ap_sg",
98-
loaded_positions=aperture_positions,
99-
tolerances=aperture_tolerances,
100-
)
101-
102-
with patch_all_motors(ap_sg):
103-
yield ap_sg
104-
105-
106-
async def set_to_position(
107-
aperture_scatterguard: ApertureScatterguard, position: AperturePosition
108-
):
109-
aperture_x, aperture_y, aperture_z, scatterguard_x, scatterguard_y = position.values
110-
111-
await asyncio.gather(
112-
aperture_scatterguard.aperture.x.set(aperture_x),
113-
aperture_scatterguard.aperture.y.set(aperture_y),
114-
aperture_scatterguard.aperture.z.set(aperture_z),
115-
aperture_scatterguard.scatterguard.x.set(scatterguard_x),
116-
aperture_scatterguard.scatterguard.y.set(scatterguard_y),
117-
)
118-
119-
120-
@pytest.fixture
121-
async def aperture_in_medium_pos(
122-
ap_sg: ApertureScatterguard,
123-
aperture_positions: dict[ApertureValue, AperturePosition],
124-
) -> AsyncGenerator[ApertureScatterguard, None]:
125-
await set_to_position(ap_sg, aperture_positions[ApertureValue.MEDIUM])
126-
127-
set_mock_value(ap_sg.aperture.medium, 1)
128-
129-
yield ap_sg
130-
131-
13246
def _assert_patched_ap_sg_has_call(
13347
ap_sg: ApertureScatterguard,
13448
position: AperturePosition,

0 commit comments

Comments
 (0)