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

Commit

Permalink
#1219 fix pin tip tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dperl-dls committed Mar 18, 2024
1 parent 1df1410 commit 9605bab
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ def pin_centre_then_xray_centre_plan(
pin_tip_centring_composite,
parameters.experiment_params.tip_offset_microns,
oav_config_file,
parameters.experiment_params.use_ophyd_pin_tip_detect,
)

grid_detect_params = create_parameters_for_grid_detection(parameters)
Expand Down
60 changes: 38 additions & 22 deletions tests/unit_tests/experiment_plans/test_pin_tip_centring.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,38 +27,48 @@ def get_fake_pin_values_generator(x, y):
return x, y


FAKE_EDGE_ARRAYS = np.ndarray([1, 2, 3]), np.ndarray([3, 4, 5])


@pytest.fixture
def mock_pin_tip(pin_tip: PinTipDetection):
pin_tip._get_tip_and_edge_data = AsyncMock(return_value=pin_tip.INVALID_POSITION)
return pin_tip


@patch("hyperion.experiment_plans.pin_tip_centring_plan.bps.sleep", new=MagicMock())
async def test_given_the_pin_tip_is_already_in_view_when_get_tip_into_view_then_tip_returned_and_smargon_not_moved(
smargon: Smargon, oav: OAV, RE: RunEngine, pin_tip: PinTipDetection
smargon: Smargon, oav: OAV, RE: RunEngine, mock_pin_tip: PinTipDetection
):
smargon.x.user_readback.sim_put(0) # type: ignore
await pin_tip.triggered_tip._backend.put((100, 200))
await mock_pin_tip.triggered_tip._backend.put((100, 200))

pin_tip.trigger = MagicMock(return_value=NullStatus())
mock_pin_tip.trigger = MagicMock(return_value=NullStatus())

result = RE(move_pin_into_view(pin_tip, smargon))
result = RE(move_pin_into_view(mock_pin_tip, smargon))

pin_tip.trigger.assert_called_once()
mock_pin_tip.trigger.assert_called_once()
assert smargon.x.user_readback.get() == 0
assert isinstance(result, RunEngineResult)
assert result.plan_result == (100, 200)


@patch("hyperion.experiment_plans.pin_tip_centring_plan.bps.sleep", new=MagicMock())
async def test_given_no_tip_found_but_will_be_found_when_get_tip_into_view_then_smargon_moved_positive_and_tip_returned(
smargon: Smargon, oav: OAV, RE: RunEngine, pin_tip: PinTipDetection
smargon: Smargon, oav: OAV, RE: RunEngine, mock_pin_tip: PinTipDetection
):
smargon.x.user_setpoint.sim_set_limits([-2, 2]) # type: ignore
await pin_tip.triggered_tip._backend.put(pin_tip.INVALID_POSITION)
await pin_tip.validity_timeout._backend.put(0.015)
await mock_pin_tip.validity_timeout._backend.put(0.015)
smargon.x.user_readback.sim_put(0) # type: ignore

async def set_pin_tip_when_x_moved(*args, **kwargs):
await pin_tip.triggered_tip._backend.put((100, 200))
def set_pin_tip_when_x_moved(*args, **kwargs):
mock_pin_tip._get_tip_and_edge_data.return_value = SampleLocation(
100, 200, *FAKE_EDGE_ARRAYS
)

smargon.x.subscribe(set_pin_tip_when_x_moved, run=False)

result = RE(move_pin_into_view(pin_tip, smargon))
result = RE(move_pin_into_view(mock_pin_tip, smargon))

assert smargon.x.user_readback.get() == DEFAULT_STEP_SIZE
assert isinstance(result, RunEngineResult)
Expand All @@ -67,20 +77,24 @@ async def set_pin_tip_when_x_moved(*args, **kwargs):

@patch("hyperion.experiment_plans.pin_tip_centring_plan.bps.sleep", new=MagicMock())
async def test_given_tip_at_zero_but_will_be_found_when_get_tip_into_view_then_smargon_moved_negative_and_tip_returned(
smargon: Smargon, oav: OAV, RE: RunEngine, pin_tip: PinTipDetection
smargon: Smargon, oav: OAV, RE: RunEngine, mock_pin_tip: PinTipDetection
):
smargon.x.user_setpoint.sim_set_limits([-2, 2]) # type: ignore
await pin_tip.triggered_tip._backend.put((0, 100))
await pin_tip.validity_timeout._backend.put(0.15)
mock_pin_tip._get_tip_and_edge_data.return_value = SampleLocation(
0, 100, *FAKE_EDGE_ARRAYS
)
await mock_pin_tip.validity_timeout._backend.put(0.15)

smargon.x.user_readback.sim_put(0) # type: ignore

async def set_pin_tip_when_x_moved(*args, **kwargs):
await pin_tip.triggered_tip._backend.put((200, 100))
def set_pin_tip_when_x_moved(*args, **kwargs):
mock_pin_tip._get_tip_and_edge_data.return_value = SampleLocation(
100, 200, *FAKE_EDGE_ARRAYS
)

smargon.x.subscribe(set_pin_tip_when_x_moved, run=False)

result = RE(move_pin_into_view(pin_tip, smargon))
result = RE(move_pin_into_view(mock_pin_tip, smargon))

assert smargon.x.user_readback.get() == -DEFAULT_STEP_SIZE
assert result.plan_result == (100, 200) # type: ignore
Expand All @@ -89,7 +103,9 @@ async def set_pin_tip_when_x_moved(*args, **kwargs):
async def test_trigger_and_return_pin_tip_works_for_AD_pin_tip_detection(
oav: OAV, RE: RunEngine, pin_tip: PinTipDetection
):
await pin_tip.triggered_tip._backend.put((200, 100))
mock_pin_tip._get_tip_and_edge_data.return_value = SampleLocation(
200, 100, *FAKE_EDGE_ARRAYS
)
await pin_tip.validity_timeout._backend.put(0.15)
re_result = RE(trigger_and_return_pin_tip(pin_tip))
assert re_result.plan_result == (200, 100) # type: ignore
Expand Down Expand Up @@ -140,8 +156,8 @@ def test_pin_tip_starting_near_positive_edge_doesnt_exceed_limit(
pin_tip: PinTipDetection,
):
mock_trigger_and_return_pin_tip.side_effect = [
get_fake_pin_values_generator(-1, -1),
get_fake_pin_values_generator(-1, -1),
get_fake_pin_values_generator(None, None),
get_fake_pin_values_generator(None, None),
]
smargon.x.user_setpoint.sim_set_limits([-2, 2]) # type: ignore
smargon.x.user_setpoint.sim_put(1.8) # type: ignore
Expand Down Expand Up @@ -228,7 +244,7 @@ def test_when_pin_tip_centre_plan_called_then_expected_plans_called(
)
RE(pin_tip_centre_plan(composite, 50, test_config_files["oav_config_json"]))

assert mock_setup_oav.call_count == 2
assert mock_setup_oav.call_count == 1

assert len(get_move.call_args_list) == 2

Expand Down Expand Up @@ -290,4 +306,4 @@ def test_given_pin_tip_detect_using_ophyd_when_pin_tip_centre_plan_called_then_e

mock_move_into_view.assert_called_once_with(mock_ophyd_pin_tip_detection, smargon)

assert mock_setup_oav.call_count == 2
assert mock_setup_oav.call_count == 1

0 comments on commit 9605bab

Please sign in to comment.