Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Additional system tests for zocalo results bounds checks
Browse files Browse the repository at this point in the history
rtuck99 committed Jan 24, 2025
1 parent b663970 commit b1de989
Showing 2 changed files with 130 additions and 5 deletions.
74 changes: 70 additions & 4 deletions tests/system_tests/hyperion/external_interaction/conftest.py
Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@
import numpy
import pytest
import pytest_asyncio
from dodal.beamlines import i03
from dodal.devices.aperturescatterguard import ApertureScatterguard
from dodal.devices.attenuator.attenuator import BinaryFilterAttenuator
from dodal.devices.backlight import Backlight
@@ -39,6 +40,7 @@
from ophyd_async.testing import callback_on_mock_put, set_mock_value
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from workflows.recipe import RecipeWrapper

from mx_bluesky.common.external_interaction.ispyb.ispyb_store import StoreInIspyb
from mx_bluesky.common.utils.utils import convert_angstrom_to_eV
@@ -96,6 +98,50 @@
"bounding_box": [[1, 2, 3], [2, 3, 4]],
}
]
# These are the uncorrected coordinate from zocalo
TEST_RESULT_IN_BOUNDS_TOP_LEFT_BOX = [
{
"centre_of_mass": [0.5, 0.5, 0.5],
"max_voxel": [0, 0, 0],
"max_count": 50000,
"n_voxels": 35,
"total_count": 100000,
"bounding_box": [[0, 0, 0], [3, 4, 4]],
}
]
# These are the uncorrected coordinate from zocalo
TEST_RESULT_IN_BOUNDS_TOP_LEFT_GRID_CORNER = [
{
"centre_of_mass": [0.0, 0.0, 0.0],
"max_voxel": [0, 0, 0],
"max_count": 50000,
"n_voxels": 35,
"total_count": 100000,
"bounding_box": [[0, 0, 0], [3, 4, 4]],
}
]
# These are the uncorrected coordinate from zocalo
TEST_RESULT_OUT_OF_BOUNDS_COM = [
{
"centre_of_mass": [-0.0001, -0.0001, -0.0001],
"max_voxel": [0, 0, 0],
"max_count": 50000,
"n_voxels": 35,
"total_count": 100000,
"bounding_box": [[0, 0, 0], [3, 4, 4]],
}
]
# These are the uncorrected coordinate from zocalo
TEST_RESULT_OUT_OF_BOUNDS_BB = [
{
"centre_of_mass": [0, 0, 0],
"max_voxel": [0, 0, 0],
"max_count": 50000,
"n_voxels": 35,
"total_count": 100000,
"bounding_box": [[-1, -1, -1], [3, 4, 4]],
}
]


def get_current_datacollection_comment(Session: Callable, dcid: int) -> str:
@@ -255,13 +301,33 @@ async def zocalo_for_fake_zocalo():


@pytest.fixture
def zocalo_for_system_test(zocalo) -> Generator[ZocaloResults, None, None]:
def zocalo_for_system_test() -> Generator[ZocaloResults, None, None]:
zocalo = i03.zocalo(fake_with_ophyd_sim=True)
old_zocalo_trigger = zocalo.trigger
zocalo.my_zocalo_result = TEST_RESULT_MEDIUM

@AsyncStatus.wrap
async def mock_zocalo_complete():
await zocalo._put_results(TEST_RESULT_MEDIUM, {"dcid": 1234, "dcgid": 123})
fake_recipe_wrapper = MagicMock(spec=RecipeWrapper)
fake_recipe_wrapper.recipe_step = {"parameters": {"dcid": 1234, "dcgid": 123}}
message = {
"results": zocalo.my_zocalo_result # type: ignore
}
header = {}
zocalo.my_callback(fake_recipe_wrapper, header, message) # type: ignore
await old_zocalo_trigger()

def mock_worfklow_subscribe(transport, channel, callback, **kwargs):
if channel == "xrc.i03":
zocalo.my_callback = callback

with patch.object(zocalo, "trigger", side_effect=mock_zocalo_complete):
yield zocalo
with (
patch("dodal.devices.zocalo.zocalo_results.workflows") as workflows,
patch("dodal.devices.zocalo.zocalo_results._get_zocalo_connection"),
):
workflows.recipe.wrap_subscribe.side_effect = mock_worfklow_subscribe
with patch.object(zocalo, "trigger", side_effect=mock_zocalo_complete):
yield zocalo


@pytest.fixture
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@

import os
from collections.abc import Callable, Generator
from contextlib import nullcontext
from typing import Any
from unittest.mock import MagicMock, patch

@@ -45,7 +46,14 @@
compare_actual_and_expected,
compare_comment,
)
from .conftest import raw_params_from_file
from .conftest import (
TEST_RESULT_IN_BOUNDS_TOP_LEFT_BOX,
TEST_RESULT_IN_BOUNDS_TOP_LEFT_GRID_CORNER,
TEST_RESULT_MEDIUM,
TEST_RESULT_OUT_OF_BOUNDS_BB,
TEST_RESULT_OUT_OF_BOUNDS_COM,
raw_params_from_file,
)


@pytest.fixture
@@ -514,3 +522,54 @@ def test_load_centre_collect_updates_bl_sample_status_rotation_failure(
)

assert fetch_blsample(SAMPLE_ID).blSampleStatus == "ERROR - beamline"


@pytest.mark.parametrize(
"zocalo_result, expected_in_bounds",
[
[TEST_RESULT_MEDIUM, nullcontext()],
# TEST_RESULT_LARGE,
[TEST_RESULT_IN_BOUNDS_TOP_LEFT_BOX, nullcontext()],
[TEST_RESULT_IN_BOUNDS_TOP_LEFT_GRID_CORNER, nullcontext()],
[
TEST_RESULT_OUT_OF_BOUNDS_COM,
pytest.raises(IndexError, match=".* is outside the bounds of the grid"),
],
[
TEST_RESULT_OUT_OF_BOUNDS_BB,
pytest.raises(IndexError, match=".* is outside the bounds of the grid"),
],
],
)
@pytest.mark.s03
def test_load_centre_collect_gridscan_result_at_edge_of_grid(
zocalo_result,
expected_in_bounds,
load_centre_collect_composite: LoadCentreCollectComposite,
load_centre_collect_params: LoadCentreCollect,
oav_parameters_for_rotation: OAVParameters,
RE: RunEngine,
):
load_centre_collect_composite.zocalo.my_zocalo_result = zocalo_result
ispyb_gridscan_cb = GridscanISPyBCallback(
param_type=GridCommonWithHyperionDetectorParams
)
ispyb_rotation_cb = RotationISPyBCallback()
robot_load_cb = RobotLoadISPyBCallback()
robot_load_cb.expeye.start_load = MagicMock(return_value=1234)
robot_load_cb.expeye.end_load = MagicMock()
robot_load_cb.expeye.update_barcode_and_snapshots = MagicMock()
set_mock_value(
load_centre_collect_composite.undulator_dcm.undulator_ref().current_gap, 1.11
)
RE.subscribe(ispyb_gridscan_cb)
RE.subscribe(ispyb_rotation_cb)
RE.subscribe(robot_load_cb)
with expected_in_bounds:
RE(
load_centre_collect_full(
load_centre_collect_composite,
load_centre_collect_params,
oav_parameters_for_rotation,
)
)

0 comments on commit b1de989

Please sign in to comment.