Skip to content

Commit

Permalink
Use kickoff and complete in ft plan and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
noemifrisina committed Sep 17, 2024
1 parent bc5585b commit 458e3f0
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 16 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ dependencies = [
"ophyd == 1.9.0",
"ophyd-async >= 0.3a5",
"bluesky >= 1.13.0a4",
"dls-dodal @ git+https://github.com/DiamondLightSource/dodal.git@38ed26e5b4467d184963ddcd67a835f6d8a52318",
"dls-dodal @ git+https://github.com/DiamondLightSource/dodal.git@a9e88b3b022fedcc9df880e15b240b608f3497a1",
]


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -680,26 +680,31 @@ def main_fixed_target_plan(
wavelength,
)

yield from kickoff_and_complete_collection(pmac, parameters)


def kickoff_and_complete_collection(pmac: PMAC, parameters: FixedTargetParameters):
# Get program number
prog_num = get_prog_num(
parameters.chip.chip_type, parameters.map_type, parameters.pump_repeat
)
yield from bps.abs_set(pmac.program_number, prog_num, group="setup_pmac")
# Calculate approx collection time
total_collection_time = calculate_collection_timeout(parameters)
logger.info(f"Estimated collection time: {total_collection_time}s.")
yield from bps.abs_set(
pmac.collection_time, total_collection_time, group="setup_pmac"
)
yield from bps.wait(group="setup_pmac") # Make sure the soft signals are set

@bpp.run_decorator(md={"subplan_name": "run_ft_collection"})
def kickoff_and_complete_collection():
# Get program number
prog_num = get_prog_num(
parameters.chip.chip_type, parameters.map_type, parameters.pump_repeat
) # TODO fix this to return right thing
yield from bps.abs_set(pmac.program_number, prog_num, group="setup_pmac")
# Calculate approx collection time
total_collection_time = calculate_collection_timeout(parameters)
logger.info(f"Estimated collection time: {total_collection_time}s.")
yield from bps.abs_set(
pmac.collection_time, total_collection_time, group="setup_pmac"
)
yield from bps.wait(group="setup_pmac") # Make sure the soft signals are set
def run_collection():
logger.info(f"Kick off PMAC with program number {prog_num}.")
yield from bps.kickoff(pmac.run_program, wait=True)
yield from bps.complete(pmac.run_program, wait=True)
logger.info("Collection completed without errors.")

yield from kickoff_and_complete_collection()
yield from run_collection()


@log.log_on_entry
Expand Down
21 changes: 21 additions & 0 deletions tests/unit_tests/beamlines/i24/serial/fixed_target/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,24 @@ def dummy_params_without_pp():
"checker_pattern": False,
}
return FixedTargetParameters(**params)


@pytest.fixture
def dummy_params_with_pp():
oxford_defaults = get_chip_format(ChipType.Oxford)
params = {
"visit": "foo",
"directory": "bar",
"filename": "chip",
"exposure_time_s": 0.01,
"detector_distance_mm": 100,
"detector_name": "eiger",
"num_exposures": 1,
"chip": oxford_defaults.model_dump(),
"map_type": 1,
"pump_repeat": 3,
"checker_pattern": False,
"laser_dwell_s": 0.02,
"laser_delay_s": 0.05,
}
return FixedTargetParameters(**params)
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
import asyncio
from unittest.mock import ANY, MagicMock, call, mock_open, patch

import bluesky.plan_stubs as bps
import pytest
from bluesky.utils import FailedStatus
from dodal.devices.hutch_shutter import HutchShutter
from dodal.devices.i24.pmac import PMAC
from dodal.devices.zebra import Zebra
from ophyd_async.core import get_mock_put
from ophyd_async.core import callback_on_mock_put, get_mock_put, set_mock_value

from mx_bluesky.beamlines.i24.serial.fixed_target.ft_utils import MappingType
from mx_bluesky.beamlines.i24.serial.fixed_target.i24ssx_Chip_Collect_py3v1 import (
datasetsizei24,
finish_i24,
get_chip_prog_values,
get_prog_num,
kickoff_and_complete_collection,
load_motion_program_data,
run_aborted_plan,
start_i24,
Expand Down Expand Up @@ -247,3 +250,46 @@ async def test_tidy_up_after_collection_plan(
fake_caput.assert_has_calls([call(ANY, 0), call(ANY, "Done")])

mock_finish.assert_called_once()


@patch(
"mx_bluesky.beamlines.i24.serial.fixed_target.i24ssx_Chip_Collect_py3v1.calculate_collection_timeout"
)
async def test_kick_off_and_complete_collection(
fake_collection_time, pmac, dummy_params_with_pp, RE, done_status
):
pmac.run_program.kickoff = MagicMock(return_value=done_status)
pmac.run_program.complete = MagicMock(return_value=done_status)

async def go_high_then_low():
set_mock_value(pmac.scanstatus, 1)
await asyncio.sleep(0.1)
set_mock_value(pmac.scanstatus, 0)

callback_on_mock_put(
pmac.pmac_string,
lambda *args, **kwargs: asyncio.create_task(go_high_then_low()), # type: ignore
)
fake_collection_time.return_value = 2.0
res = RE(kickoff_and_complete_collection(pmac, dummy_params_with_pp))

assert await pmac.program_number.get_value() == 14
assert await pmac.collection_time.get_value() == 2.0

pmac.run_program.kickoff.assert_called_once()
pmac.run_program.complete.assert_called_once()

assert res.exit_status == "success"


@patch(
"mx_bluesky.beamlines.i24.serial.fixed_target.i24ssx_Chip_Collect_py3v1.calculate_collection_timeout"
)
async def test_kickoff_and_complete_fails_if_scan_status_pv_does_not_change(
fake_collection_time, pmac, dummy_params_without_pp, RE
):
fake_collection_time.return_value = 1.0
pmac.run_program.KICKOFF_TIMEOUT = 0.1
set_mock_value(pmac.scanstatus, 0)
with pytest.raises(FailedStatus):
RE(kickoff_and_complete_collection(pmac, dummy_params_without_pp))

0 comments on commit 458e3f0

Please sign in to comment.