CI for csaxs_bec / test (push) Successful in 1m43s
The premove exists so the fast axis is already at speed when it reaches
the first point and still moving past the last one. It was applied with
a fixed sign:
self.fast_start -= premove_distance
self.fast_end += premove_distance
which only lengthens an ascending line. On a descending one both ends
move inward instead: the stage starts past the first point and stops
before the last, with no run-up at either end, while frames_per_trigger
-- computed earlier from the original range -- still expects the full
travel, so the tail of the burst fires into a stage that has already
arrived. When the premove exceeds half the range the two ends cross over
and the line runs backwards; the test fixture's premove is 10.0 against
a range of 2.0, so that is the case it covers.
Latent rather than live: fast_axis_always_in_pos_dir defaults to True
and normalises a descending request to an ascending line before any of
this runs, so the wrong sign is only reachable when a caller passes the
flag as False. The flag is a documented scan argument, so the path is
real -- but no scan taken at the default has ever gone through it.
Also pins the normalisation itself. It is worth a test of its own
because it means a scan file records fast_start > fast_end while the
stage ran the other way, which is a trap when reading data back: the
first point of a line is at the numerically *lower* end regardless of
what the request said.
220 lines
7.6 KiB
Python
220 lines
7.6 KiB
Python
from unittest import mock
|
|
|
|
import numpy as np
|
|
import pytest
|
|
from bec_server.scan_server.tests.scan_fixtures import MockCustomDevice
|
|
from bec_server.scan_server.tests.scan_hook_tests import (
|
|
DEFAULT_HOOK_TESTS,
|
|
PREMOVE_HOOK_TESTS,
|
|
run_scan_tests,
|
|
)
|
|
|
|
CONT_GRID_HOOK_TESTS = [*DEFAULT_HOOK_TESTS, *PREMOVE_HOOK_TESTS]
|
|
|
|
|
|
def _assemble_cont_grid_scan(
|
|
v4_scan_assembler,
|
|
device_manager,
|
|
fast_start=-1.0,
|
|
fast_stop=1.0,
|
|
fast_axis_always_in_pos_dir=True,
|
|
):
|
|
custom_samx = MockCustomDevice(
|
|
"samx",
|
|
device_info={
|
|
"signals": {
|
|
"readback": {
|
|
"obj_name": "samx",
|
|
"kind_str": "hinted",
|
|
"describe": {"precision": 3},
|
|
},
|
|
"velocity": {
|
|
"obj_name": "samx_velocity",
|
|
"kind_str": "config",
|
|
"describe": {"precision": 3},
|
|
},
|
|
"acceleration": {
|
|
"obj_name": "samx_acceleration",
|
|
"kind_str": "config",
|
|
"describe": {"precision": 3},
|
|
},
|
|
"base_velocity": {
|
|
"obj_name": "samx_base_velocity",
|
|
"kind_str": "config",
|
|
"describe": {"precision": 3},
|
|
},
|
|
}
|
|
},
|
|
signal_read_values={
|
|
"samx": 0.0,
|
|
"samx_velocity": 10.0,
|
|
"samx_acceleration": 2.0,
|
|
"samx_base_velocity": 0.0,
|
|
},
|
|
)
|
|
custom_samy = MockCustomDevice(
|
|
"samy",
|
|
device_info={
|
|
"signals": {
|
|
"readback": {"obj_name": "samy", "kind_str": "hinted", "describe": {"precision": 3}}
|
|
}
|
|
},
|
|
signal_read_values={"samy": 0.0},
|
|
)
|
|
custom_ddg1 = MockCustomDevice(
|
|
"ddg1",
|
|
device_info={
|
|
"signals": {
|
|
"readback": {"obj_name": "ddg1", "kind_str": "hinted", "describe": {"precision": 3}}
|
|
}
|
|
},
|
|
signal_read_values={"ddg1": 0.0},
|
|
)
|
|
custom_ddg1.get_shutter_to_open_delay = mock.MagicMock(return_value=2e-3)
|
|
custom_mcs = MockCustomDevice(
|
|
"mcs",
|
|
device_info={
|
|
"signals": {
|
|
"readback": {"obj_name": "mcs", "kind_str": "hinted", "describe": {"precision": 3}}
|
|
}
|
|
},
|
|
signal_read_values={"mcs": 0.0},
|
|
)
|
|
device_manager.add_device(custom_samx, replace=True)
|
|
device_manager.add_device(custom_samy, replace=True)
|
|
device_manager.add_device(custom_mcs, replace=True)
|
|
device_manager.add_device(custom_ddg1, replace=True)
|
|
return v4_scan_assembler(
|
|
"cont_grid",
|
|
"samx",
|
|
fast_start,
|
|
fast_stop,
|
|
1,
|
|
"samy",
|
|
-2.0,
|
|
2.0,
|
|
2,
|
|
exp_time=0.1,
|
|
fast_axis_always_in_pos_dir=fast_axis_always_in_pos_dir,
|
|
)
|
|
|
|
|
|
@pytest.mark.parametrize(("hook_name", "hook_tests"), CONT_GRID_HOOK_TESTS)
|
|
def test_cont_grid_default_hooks(
|
|
v4_scan_assembler, device_manager, nth_done_status_mock, hook_name, hook_tests
|
|
):
|
|
scan = _assemble_cont_grid_scan(v4_scan_assembler, device_manager)
|
|
|
|
run_scan_tests(scan, [(hook_name, hook_tests)], nth_done_status_mock=nth_done_status_mock)
|
|
|
|
|
|
def test_cont_grid_post_scan_waits_for_completion_and_moves_back_when_relative(
|
|
v4_scan_assembler, device_manager, nth_done_status_mock
|
|
):
|
|
scan = _assemble_cont_grid_scan(v4_scan_assembler, device_manager)
|
|
|
|
completion_status = nth_done_status_mock(resolve_after=3)
|
|
scan.relative = True
|
|
scan.start_positions = [1.2, -0.7]
|
|
scan.actions.complete_all_devices = mock.MagicMock(return_value=completion_status)
|
|
scan.components.move_and_wait = mock.MagicMock()
|
|
scan._restore_motor_properties = mock.MagicMock()
|
|
|
|
scan.post_scan()
|
|
|
|
scan._restore_motor_properties.assert_called_once_with()
|
|
scan.actions.complete_all_devices.assert_called_once_with(wait=False)
|
|
scan.components.move_and_wait.assert_called_once_with(scan.motors, scan.start_positions)
|
|
assert completion_status.wait_calls == 1
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("fast_start", "fast_stop"), [(-1.0, 1.0), (1.0, -1.0)], ids=["ascending", "descending"]
|
|
)
|
|
def test_cont_grid_premove_extends_the_line_in_both_directions(
|
|
v4_scan_assembler, device_manager, fast_start, fast_stop
|
|
):
|
|
"""The premove must lengthen the travel whichever way the line runs.
|
|
|
|
Applied with a fixed sign it lengthens an ascending line and *shortens* a
|
|
descending one, so the stage starts past the first point and stops before the
|
|
last with no run-up at either end -- while frames_per_trigger still expects the
|
|
full travel, leaving the tail of the burst firing into a stationary stage.
|
|
"""
|
|
scan = _assemble_cont_grid_scan(
|
|
v4_scan_assembler,
|
|
device_manager,
|
|
fast_start=fast_start,
|
|
fast_stop=fast_stop,
|
|
fast_axis_always_in_pos_dir=False,
|
|
)
|
|
|
|
scan.prepare_scan()
|
|
|
|
premove = scan._cont_motor_params["premove_distance"]
|
|
assert premove > 0 # otherwise this test proves nothing
|
|
assert scan.fast_start == pytest.approx(fast_start - np.sign(fast_stop - fast_start) * premove)
|
|
assert scan.fast_end == pytest.approx(fast_stop + np.sign(fast_stop - fast_start) * premove)
|
|
# Travel is longer than requested, and still runs the way it was asked to.
|
|
assert abs(scan.fast_end - scan.fast_start) > abs(fast_stop - fast_start)
|
|
assert np.sign(scan.fast_end - scan.fast_start) == np.sign(fast_stop - fast_start)
|
|
|
|
|
|
def test_cont_grid_premove_does_not_reverse_a_descending_line(
|
|
v4_scan_assembler, device_manager
|
|
):
|
|
"""The sharpest form of the bug: with a premove larger than half the range the
|
|
two adjusted ends cross over, and a line asked to run 1.0 -> -1.0 runs -1.0 ->
|
|
1.0 instead. This fixture's premove is 10.0 against a range of 2.0, so the
|
|
crossover is what the unfixed code produces here."""
|
|
scan = _assemble_cont_grid_scan(
|
|
v4_scan_assembler,
|
|
device_manager,
|
|
fast_start=1.0,
|
|
fast_stop=-1.0,
|
|
fast_axis_always_in_pos_dir=False,
|
|
)
|
|
|
|
scan.prepare_scan()
|
|
|
|
assert scan._cont_motor_params["premove_distance"] > abs(1.0 - -1.0) / 2
|
|
assert scan.fast_start > scan.fast_end
|
|
|
|
|
|
def test_cont_grid_normalises_direction_by_default(v4_scan_assembler, device_manager):
|
|
"""A descending request is silently turned into an ascending line unless the
|
|
caller opts out. Worth pinning: it means a scan file records fast_start >
|
|
fast_end while the stage ran the other way, which is a trap when reading data
|
|
back, and it is what keeps the premove sign latent at the default."""
|
|
scan = _assemble_cont_grid_scan(v4_scan_assembler, device_manager, fast_start=1.0, fast_stop=-1.0)
|
|
|
|
assert scan.fast_start == -1.0
|
|
assert scan.fast_end == 1.0
|
|
|
|
|
|
def test_cont_grid_prepare_scan_keeps_generated_positions_stable(v4_scan_assembler, device_manager):
|
|
scan = _assemble_cont_grid_scan(v4_scan_assembler, device_manager)
|
|
|
|
scan.prepare_scan()
|
|
|
|
assert np.array_equal(scan.positions, np.array([[1.0, -2.0], [1.0, 0.0], [1.0, 2.0]]))
|
|
assert scan.scan_info.frames_per_trigger == 3
|
|
assert scan._cont_motor_params["num_lines"] == 3
|
|
assert scan.scan_info.additional_scan_parameters["num_lines"] == 3
|
|
assert np.array_equal(
|
|
scan.scan_info.additional_scan_parameters["computed_positions"],
|
|
np.array(
|
|
[
|
|
[-1.0, -2.0],
|
|
[0.0, -2.0],
|
|
[1.0, -2.0],
|
|
[-1.0, 0.0],
|
|
[0.0, 0.0],
|
|
[1.0, 0.0],
|
|
[-1.0, 2.0],
|
|
[0.0, 2.0],
|
|
[1.0, 2.0],
|
|
]
|
|
),
|
|
)
|