Skip to content

Commit

Permalink
Additional system test for full experiment plan
Browse files Browse the repository at this point in the history
  • Loading branch information
rtuck99 committed Sep 30, 2024
1 parent fde70a7 commit bd22bd5
Show file tree
Hide file tree
Showing 11 changed files with 672 additions and 190 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,9 @@ def _handle_ispyb_hardware_read(self, doc) -> Sequence[ScanDataInfo]:
slitgap_vertical=doc["data"]["s4_slit_gaps_ygap"],
)
hwscan_position_info = DataCollectionPositionInfo(
pos_x=doc["data"]["smargon-x"],
pos_y=doc["data"]["smargon-y"],
pos_z=doc["data"]["smargon-z"],
pos_x=float(doc["data"]["smargon-x"]),
pos_y=float(doc["data"]["smargon-y"]),
pos_z=float(doc["data"]["smargon-z"]),
)
scan_data_infos = self.populate_info_for_update(
hwscan_data_collection_info, hwscan_position_info, self.params
Expand Down
2 changes: 1 addition & 1 deletion src/mx_bluesky/hyperion/parameters/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,6 @@ class WithVisit(BaseModel):
visit: str = Field(min_length=1)
zocalo_environment: str = Field(default=CONST.ZOCALO_ENV)
beamline: str = Field(default=CONST.I03.BEAMLINE, pattern=r"BL\d{2}[BIJS]")
storage_directory: str
det_dist_to_beam_converter_path: str = Field(
default=CONST.PARAM.DETECTOR.BEAM_XY_LUT_PATH
)
Expand All @@ -172,6 +171,7 @@ class DiffractionExperiment(
selected_aperture: ApertureValue | None = Field(default=None)
transmission_frac: float = Field(default=0.1)
ispyb_experiment_type: IspybExperimentType
storage_directory: str

@model_validator(mode="before")
@classmethod
Expand Down
12 changes: 11 additions & 1 deletion src/mx_bluesky/hyperion/parameters/load_centre_collect.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,17 @@ class LoadCentreCollect(HyperionParameters, WithVisit, WithSample):

@model_validator(mode="before")
@classmethod
def robot_load_params(cls, values):
def validate_model(cls, values):
allowed_keys = (
LoadCentreCollect.model_fields.keys()
| RobotLoadThenCentre.model_fields.keys()
| MultiRotationScan.model_fields.keys()
)
disallowed_keys = values.keys() - allowed_keys
assert (
disallowed_keys == set()
), f"Unexpected fields found in LoadCentreCollect {disallowed_keys}"

values["robot_load_then_centre"] = construct_from_values(
values, "robot_load_then_centre", RobotLoadThenCentre
)
Expand Down
13 changes: 11 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,10 @@ def smargon(RE: RunEngine) -> Generator[Smargon, None, None]:
set_mock_value(smargon.z.user_readback, 0.0)
set_mock_value(smargon.x.high_limit_travel, 2)
set_mock_value(smargon.x.low_limit_travel, -2)
set_mock_value(smargon.y.high_limit_travel, 2)
set_mock_value(smargon.y.low_limit_travel, -2)
set_mock_value(smargon.z.high_limit_travel, 2)
set_mock_value(smargon.z.low_limit_travel, -2)

with (
patch_async_motor(smargon.omega),
Expand Down Expand Up @@ -405,7 +409,12 @@ def dcm(RE):
dcm = i03.dcm(fake_with_ophyd_sim=True)
set_mock_value(dcm.energy_in_kev.user_readback, 12.7)
set_mock_value(dcm.pitch_in_mrad.user_readback, 1)
return dcm
with (
oa_patch_motor(dcm.roll_in_mrad),
oa_patch_motor(dcm.pitch_in_mrad),
oa_patch_motor(dcm.offset_in_mm),
):
yield dcm


@pytest.fixture
Expand Down Expand Up @@ -594,7 +603,7 @@ def fake_create_rotation_devices(
xbpm_feedback: XBPMFeedback,
):
set_mock_value(smargon.omega.max_velocity, 131)
oav.zoom_controller.onst.sim_put("1.0x") # type: ignore
oav.zoom_controller.zrst.sim_put("1.0x") # type: ignore
oav.zoom_controller.fvst.sim_put("5.0x") # type: ignore

return RotationScanComposite(
Expand Down
84 changes: 84 additions & 0 deletions tests/system_tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
from unittest.mock import MagicMock, patch

import pytest
from dodal.beamlines import i03
from dodal.devices.oav.oav_parameters import OAVConfigParams
from ophyd_async.core import AsyncStatus, set_mock_value
from requests import Response


@pytest.fixture
def undulator_for_system_test(undulator):
set_mock_value(undulator.current_gap, 1.11)
return undulator


@pytest.fixture
def oav_for_system_test(test_config_files):
parameters = OAVConfigParams(
test_config_files["zoom_params_file"], test_config_files["display_config"]
)
oav = i03.oav(fake_with_ophyd_sim=True, params=parameters)
oav.zoom_controller.zrst.set("1.0x")
oav.zoom_controller.onst.set("7.5x")
oav.cam.array_size.array_size_x.sim_put(1024)
oav.cam.array_size.array_size_y.sim_put(768)

unpatched_method = oav.parameters.load_microns_per_pixel

def patch_lmpp(zoom, xsize, ysize):
unpatched_method(zoom, 1024, 768)

# Grid snapshots
oav.grid_snapshot.x_size.sim_put(1024)
oav.grid_snapshot.y_size.sim_put(768)
oav.grid_snapshot.top_left_x.set(50)
oav.grid_snapshot.top_left_y.set(100)
oav.grid_snapshot.box_width.set(0.1 * 1000 / 1.25) # size in pixels
unpatched_snapshot_trigger = oav.grid_snapshot.trigger

def mock_grid_snapshot_trigger():
oav.grid_snapshot.last_path_full_overlay.set("test_1_y")
oav.grid_snapshot.last_path_outer.set("test_2_y")
oav.grid_snapshot.last_saved_path.set("test_3_y")
return unpatched_snapshot_trigger()

# Plain snapshots
def next_snapshot():
next_snapshot_idx = 1
while True:
yield f"/tmp/snapshot{next_snapshot_idx}.png"
next_snapshot_idx += 1

empty_response = MagicMock(spec=Response)
empty_response.content = b""
with (
patch(
"dodal.devices.areadetector.plugins.MJPG.requests.get",
return_value=empty_response,
),
patch("dodal.devices.areadetector.plugins.MJPG.Image.open"),
patch.object(oav.grid_snapshot, "post_processing"),
patch.object(
oav.grid_snapshot, "trigger", side_effect=mock_grid_snapshot_trigger
),
patch.object(
oav.parameters,
"load_microns_per_pixel",
new=MagicMock(side_effect=patch_lmpp),
),
patch.object(oav.snapshot.last_saved_path, "get") as mock_last_saved_path,
):
it_next_snapshot = next_snapshot()

@AsyncStatus.wrap
async def mock_rotation_snapshot_trigger():
mock_last_saved_path.side_effect = lambda: next(it_next_snapshot)

with patch.object(
oav.snapshot,
"trigger",
side_effect=mock_rotation_snapshot_trigger,
):
oav.parameters.load_microns_per_pixel(1.0, 1024, 768)
yield oav
Loading

0 comments on commit bd22bd5

Please sign in to comment.