Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
dperl-dls committed Feb 29, 2024
1 parent f0292c5 commit 8571fee
Show file tree
Hide file tree
Showing 12 changed files with 46 additions and 166 deletions.
6 changes: 1 addition & 5 deletions src/hyperion/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,7 @@ def wait_on_queue(self):
if command.experiment is None:
raise ValueError("No experiment provided for START")
try:
if (
not self.use_external_callbacks
and command.callbacks
and (cbs := list(command.callbacks.setup()))
):
if command.callbacks and (cbs := list(command.callbacks())):
LOGGER.info(
f"Using callbacks for this plan: {not self.use_external_callbacks} - {cbs}"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ def run_gridscan_and_move_and_tidy(fgs_composite, params):
)

parameters = GridscanInternalParameters(**external_parameters.from_file())
subscriptions = XrayCentreCallbackCollection.setup()
subscriptions = XrayCentreCallbackCollection()

context = setup_context(wait_for_connection=True)
composite = create_devices(context)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def _reset_state(self):
self.run_uid: Optional[str] = None
self.triggering_plan: Optional[str] = None
self.ispyb_ids: Optional[tuple[int]] = None
self.zocalo_interactor: Optional[ZocaloTrigger] = None

def __init__(
self,
Expand Down
6 changes: 3 additions & 3 deletions tests/system_tests/experiment_plans/test_fgs_plan.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ def test_full_plan_tidies_at_end(
params: GridscanInternalParameters,
RE: RunEngine,
):
callbacks = XrayCentreCallbackCollection.setup()
callbacks = XrayCentreCallbackCollection()
callbacks.nexus_handler.nexus_writer_1 = MagicMock()
callbacks.nexus_handler.nexus_writer_2 = MagicMock()
callbacks.ispyb_handler.ispyb_ids = IspybIds(
Expand Down Expand Up @@ -230,7 +230,7 @@ def test_GIVEN_scan_invalid_WHEN_plan_run_THEN_ispyb_entry_made_but_no_zocalo_en
# Currently s03 calls anything with z_steps > 1 invalid
params.experiment_params.z_steps = 100

callbacks = XrayCentreCallbackCollection.setup()
callbacks = XrayCentreCallbackCollection()
callbacks.ispyb_handler.ispyb.ISPYB_CONFIG_PATH = ISPYB_CONFIG
mock_start_zocalo = MagicMock()
callbacks.zocalo_handler.zocalo_interactor.run_start = mock_start_zocalo
Expand Down Expand Up @@ -272,7 +272,7 @@ def test_WHEN_plan_run_THEN_move_to_centre_returned_from_zocalo_expected_centre(
fgs_composite.eiger.stage = MagicMock()
fgs_composite.eiger.unstage = MagicMock()

callbacks = XrayCentreCallbackCollection.setup()
callbacks = XrayCentreCallbackCollection()
callbacks.ispyb_handler.ispyb.ISPYB_CONFIG_PATH = ISPYB_CONFIG

RE(flyscan_xray_centre(fgs_composite, params))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ def test_ispyb_deposition_in_rotation_plan(
)

os.environ["ISPYB_CONFIG_PATH"] = DEV_ISPYB_DATABASE_CFG
callbacks = RotationCallbackCollection.setup()
callbacks = RotationCallbackCollection()
for cb in list(callbacks):
RE.subscribe(cb)

Expand Down
49 changes: 20 additions & 29 deletions tests/system_tests/external_interaction/test_zocalo_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from hyperion.external_interaction.callbacks.xray_centre.callback_collection import (
XrayCentreCallbackCollection,
)
from hyperion.external_interaction.ispyb.ispyb_store import IspybIds
from hyperion.external_interaction.callbacks.zocalo_callback import ZocaloCallback
from hyperion.parameters.constants import GRIDSCAN_OUTER_PLAN
from hyperion.parameters.plan_specific.gridscan_internal_params import (
GridscanInternalParameters,
Expand Down Expand Up @@ -53,16 +53,9 @@ async def zocalo_device():
async def test_when_running_start_stop_then_get_expected_returned_results(
dummy_params, zocalo_env, zocalo_device: ZocaloResults, RE: RunEngine
):
start_doc = {
"subplan_name": GRIDSCAN_OUTER_PLAN,
"hyperion_internal_parameters": dummy_params.json(),
}
zc = XrayCentreCallbackCollection.setup().zocalo_handler
zc.activity_gated_start(start_doc)
dcids = (1, 2)
zc.ispyb.ispyb_ids = IspybIds(
data_collection_ids=dcids, data_collection_group_id=4, grid_ids=(0,)
)
zc = ZocaloCallback()
zc.start({}) # TODO
for dcid in dcids:
zc.zocalo_interactor.run_start(dcid)
for dcid in dcids:
Expand All @@ -81,14 +74,12 @@ def run_zocalo_with_dev_ispyb(
):
async def inner(sample_name="", fallback=np.array([0, 0, 0])):
dummy_params.hyperion_params.detector_params.prefix = sample_name
cbs = XrayCentreCallbackCollection.setup()
zc = cbs.zocalo_handler
zc.active = True
cbs = XrayCentreCallbackCollection()
ispyb = cbs.ispyb_handler
ispyb.ispyb_config = dummy_ispyb_3d.ISPYB_CONFIG_PATH
ispyb.active = True
assert isinstance(zc := ispyb.emit_cb, ZocaloCallback)

RE.subscribe(zc)
RE.subscribe(ispyb)

@bpp.set_run_key_decorator("testing123")
Expand All @@ -103,12 +94,12 @@ def plan():
)
def inner_plan():
yield from bps.sleep(0)
zc.ispyb.ispyb_ids = zc.ispyb.ispyb.begin_deposition()
assert isinstance(zc.ispyb.ispyb_ids.data_collection_ids, tuple)
for dcid in zc.ispyb.ispyb_ids.data_collection_ids:
ispyb.ispyb_ids = ispyb.ispyb.begin_deposition()
assert isinstance(ispyb.ispyb_ids.data_collection_ids, tuple)
for dcid in ispyb.ispyb_ids.data_collection_ids:
zc.zocalo_interactor.run_start(dcid)
zc.ispyb._processing_start_time = time()
for dcid in zc.ispyb.ispyb_ids.data_collection_ids:
ispyb._processing_start_time = time()
for dcid in ispyb.ispyb_ids.data_collection_ids:
zc.zocalo_interactor.run_end(dcid)

yield from inner_plan()
Expand All @@ -123,7 +114,7 @@ def inner_plan():
else:
centre = centre[0]

return zc, centre
return ispyb, zc, centre

return inner

Expand All @@ -134,7 +125,7 @@ async def test_given_a_result_with_no_diffraction_when_zocalo_called_then_move_t
run_zocalo_with_dev_ispyb, zocalo_env
):
fallback = np.array([1, 2, 3])
zc, centre = await run_zocalo_with_dev_ispyb("NO_DIFF", fallback)
_, _, centre = await run_zocalo_with_dev_ispyb("NO_DIFF", fallback)
assert np.allclose(centre, fallback)


Expand All @@ -143,9 +134,9 @@ async def test_given_a_result_with_no_diffraction_when_zocalo_called_then_move_t
async def test_given_a_result_with_no_diffraction_ispyb_comment_updated(
run_zocalo_with_dev_ispyb, zocalo_env, fetch_comment
):
zc, _ = await run_zocalo_with_dev_ispyb("NO_DIFF")
ispyb, zc, _ = await run_zocalo_with_dev_ispyb("NO_DIFF")

comment = fetch_comment(zc.ispyb.ispyb_ids.data_collection_ids[0])
comment = fetch_comment(ispyb.ispyb_ids.data_collection_ids[0])
assert "Zocalo found no crystals in this gridscan." in comment


Expand All @@ -154,9 +145,9 @@ async def test_given_a_result_with_no_diffraction_ispyb_comment_updated(
async def test_zocalo_adds_nonzero_comment_time(
run_zocalo_with_dev_ispyb, zocalo_env, fetch_comment
):
zc, _ = await run_zocalo_with_dev_ispyb()
ispyb, zc, _ = await run_zocalo_with_dev_ispyb()

comment = fetch_comment(zc.ispyb.ispyb_ids.data_collection_ids[0])
comment = fetch_comment(ispyb.ispyb_ids.data_collection_ids[0])
assert comment[156:178] == "Zocalo processing took"
assert float(comment[179:184]) > 0
assert float(comment[179:184]) < 180
Expand All @@ -167,8 +158,8 @@ async def test_zocalo_adds_nonzero_comment_time(
async def test_given_a_single_crystal_result_ispyb_comment_updated(
run_zocalo_with_dev_ispyb, zocalo_env, fetch_comment
):
zc, _ = await run_zocalo_with_dev_ispyb()
comment = fetch_comment(zc.ispyb.ispyb_ids.data_collection_ids[0])
ispyb, zc, _ = await run_zocalo_with_dev_ispyb()
comment = fetch_comment(ispyb.ispyb_ids.data_collection_ids[0])
assert "Crystal 1" in comment
assert "Strength" in comment
assert "Size (grid boxes)" in comment
Expand All @@ -179,9 +170,9 @@ async def test_given_a_single_crystal_result_ispyb_comment_updated(
async def test_given_a_result_with_multiple_crystals_ispyb_comment_updated(
run_zocalo_with_dev_ispyb, zocalo_env, fetch_comment
):
zc, _ = await run_zocalo_with_dev_ispyb("MULTI_X")
ispyb, zc, _ = await run_zocalo_with_dev_ispyb("MULTI_X")

comment = fetch_comment(zc.ispyb.ispyb_ids.data_collection_ids[0])
comment = fetch_comment(ispyb.ispyb_ids.data_collection_ids[0])
assert "Crystal 1" and "Crystal 2" in comment
assert "Strength" in comment
assert "Position (grid boxes)" in comment
4 changes: 2 additions & 2 deletions tests/unit_tests/experiment_plans/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ def mock_subscriptions(test_fgs_params):
)
),
):
subscriptions = XrayCentreCallbackCollection.setup()
subscriptions = XrayCentreCallbackCollection()
subscriptions.ispyb_handler.ispyb = MagicMock(spec=Store3DGridscanInIspyb)
start_doc = {
"subplan_name": GRIDSCAN_OUTER_PLAN,
Expand All @@ -171,7 +171,7 @@ def mock_rotation_subscriptions(test_rotation_params):
"hyperion.external_interaction.callbacks.rotation.callback_collection.ZocaloCallback",
autospec=True,
):
subscriptions = RotationCallbackCollection.setup()
subscriptions = RotationCallbackCollection()
return subscriptions


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,10 @@ def test_emit_called_correctly():
receiving_cb.active = True

test_cb.start(start_doc)
receiving_cb.activity_gated_start.assert_called_with(start_doc)
receiving_cb.activity_gated_start.assert_called_once_with(start_doc)
test_cb.descriptor(desc_doc)
receiving_cb.activity_gated_descriptor.assert_called_with(desc_doc)
receiving_cb.activity_gated_descriptor.assert_called_once_with(desc_doc)
test_cb.event(event_doc)
receiving_cb.activity_gated_event.assert_called_with(event_doc)
receiving_cb.activity_gated_event.assert_called_once_with(event_doc)
test_cb.stop(stop_doc)
receiving_cb.activity_gated_stop.assert_called_with(stop_doc)
receiving_cb.activity_gated_stop.assert_called_once_with(stop_doc)
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def test_nexus_handler_gets_documents_in_mock_plan(
"hyperion.external_interaction.callbacks.rotation.callback_collection.RotationISPyBCallback",
autospec=True,
):
cb = RotationCallbackCollection.setup()
cb = RotationCallbackCollection()
activate_callbacks(cb)
cb.nexus_handler.activity_gated_event = MagicMock(autospec=True)
cb.nexus_handler.activity_gated_start = MagicMock(autospec=True)
Expand Down Expand Up @@ -169,7 +169,7 @@ def test_nexus_handler_only_writes_once(
"hyperion.external_interaction.callbacks.rotation.callback_collection.ZocaloCallback",
autospec=True,
):
cb = RotationCallbackCollection.setup()
cb = RotationCallbackCollection()
activate_callbacks(cb)
cb.ispyb_handler.activity_gated_start = MagicMock(autospec=True)
cb.ispyb_handler.activity_gated_event = MagicMock(autospec=True)
Expand Down Expand Up @@ -197,7 +197,7 @@ def test_nexus_handler_triggers_write_file_when_told(
"hyperion.external_interaction.callbacks.rotation.callback_collection.ZocaloCallback",
autospec=True,
):
cb = RotationCallbackCollection.setup()
cb = RotationCallbackCollection()
activate_callbacks(cb)
cb.ispyb_handler.activity_gated_start = MagicMock(autospec=True)
cb.ispyb_handler.activity_gated_stop = MagicMock(autospec=True)
Expand All @@ -219,7 +219,7 @@ def test_nexus_handler_triggers_write_file_when_told(
def test_zocalo_start_and_end_not_triggered_if_ispyb_ids_not_present(
zocalo, RE: RunEngine, params: RotationInternalParameters, test_outer_start_doc
):
cb = RotationCallbackCollection.setup()
cb = RotationCallbackCollection()
activate_callbacks(cb)
cb.nexus_handler.activity_gated_start = MagicMock(autospec=True)
cb.ispyb_handler.activity_gated_start = MagicMock(autospec=True)
Expand Down Expand Up @@ -258,7 +258,7 @@ def test_ispyb_starts_on_opening_and_zocalo_on_main_so_ispyb_triggered_before_zo

mock_store_in_ispyb_class.return_value = mock_store_in_ispyb_instance
nexus_writer.return_value.full_filename = "test_full_filename"
cb = RotationCallbackCollection.setup()
cb = RotationCallbackCollection()
activate_callbacks(cb)

def after_open_do(callbacks: RotationCallbackCollection):
Expand All @@ -282,7 +282,7 @@ def after_main_do(callbacks: RotationCallbackCollection):
def test_ispyb_handler_grabs_uid_from_main_plan_and_not_first_start_doc(
zocalo, RE: RunEngine, params: RotationInternalParameters, test_outer_start_doc
):
cb = RotationCallbackCollection.setup()
cb = RotationCallbackCollection()
activate_callbacks(cb)
cb.nexus_handler.activity_gated_event = MagicMock(autospec=True)
cb.nexus_handler.activity_gated_start = MagicMock(autospec=True)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import pytest
from dodal.devices.zocalo.zocalo_results import ZOCALO_READING_PLAN_NAME
from event_model.documents import Event, EventDescriptor, RunStop
from event_model.documents import Event, EventDescriptor, RunStart, RunStop

from hyperion.external_interaction.callbacks.xray_centre.ispyb_callback import (
GridscanISPyBCallback,
Expand Down Expand Up @@ -82,7 +82,7 @@ class TestData:
DUMMY_TIME_STRING: str = "1970-01-01 00:00:00"
GOOD_ISPYB_RUN_STATUS: str = "DataCollection Successful"
BAD_ISPYB_RUN_STATUS: str = "DataCollection Unsuccessful"
test_start_document = {
test_start_document: RunStart = { # type: ignore
"uid": "d8bee3ee-f614-4e7a-a516-25d6b9e87ef3",
"time": 1666604299.6149616,
"versions": {"ophyd": "1.6.4.post76+g0895f9f", "bluesky": "1.8.3"},
Expand All @@ -92,7 +92,7 @@ class TestData:
"subplan_name": GRIDSCAN_OUTER_PLAN,
"hyperion_internal_parameters": dummy_params().json(),
}
test_run_gridscan_start_document = {
test_run_gridscan_start_document: RunStart = { # type: ignore
"uid": "d8bee3ee-f614-4e7a-a516-25d6b9e87ef3",
"time": 1666604299.6149616,
"versions": {"ophyd": "1.6.4.post76+g0895f9f", "bluesky": "1.8.3"},
Expand All @@ -101,7 +101,7 @@ class TestData:
"plan_name": GRIDSCAN_AND_MOVE,
"subplan_name": GRIDSCAN_MAIN_PLAN,
}
test_do_fgs_start_document = {
test_do_fgs_start_document: RunStart = { # type: ignore
"uid": "d8bee3ee-f614-4e7a-a516-25d6b9e87ef3",
"time": 1666604299.6149616,
"versions": {"ophyd": "1.6.4.post76+g0895f9f", "bluesky": "1.8.3"},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@


def test_callback_collection_init():
callbacks = XrayCentreCallbackCollection.setup()
callbacks = XrayCentreCallbackCollection()
assert len(list(callbacks)) == 3


def test_callback_collection_list():
callbacks = XrayCentreCallbackCollection.setup()
callbacks = XrayCentreCallbackCollection()
callback_list = list(callbacks)
assert len(callback_list) == 3
assert callbacks.ispyb_handler in callback_list
Expand All @@ -24,7 +24,7 @@ def test_callback_collection_list():


def test_subscribe_in_plan():
callbacks = XrayCentreCallbackCollection.setup()
callbacks = XrayCentreCallbackCollection()
document_event_mock = MagicMock()
callbacks.ispyb_handler.start = document_event_mock
callbacks.ispyb_handler.activity_gated_stop = document_event_mock
Expand Down
Loading

0 comments on commit 8571fee

Please sign in to comment.