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

Commit

Permalink
Combine 'read for nexus' into 'read during plan' (#1484)
Browse files Browse the repository at this point in the history
  • Loading branch information
dperl-dls authored Jul 16, 2024
1 parent e1566c1 commit b7f06e1
Show file tree
Hide file tree
Showing 18 changed files with 78 additions and 119 deletions.
18 changes: 7 additions & 11 deletions src/hyperion/device_setup_plans/read_hardware_for_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@
from hyperion.parameters.constants import CONST


def read_hardware_for_ispyb_pre_collection(
def read_hardware_pre_collection(
undulator: Undulator,
synchrotron: Synchrotron,
s4_slit_gaps: S4SlitGaps,
robot: BartRobot,
smargon: Smargon,
):
LOGGER.info("Reading status of beamline for ispyb deposition, pre collection.")
LOGGER.info("Reading status of beamline for callbacks, pre collection.")
yield from bps.create(
name=CONST.DESCRIPTORS.ISPYB_HARDWARE_READ
name=CONST.DESCRIPTORS.HARDWARE_READ_PRE
) # gives name to event *descriptor* document
yield from bps.read(undulator.current_gap)
yield from bps.read(synchrotron.synchrotron_mode)
Expand All @@ -37,23 +37,19 @@ def read_hardware_for_ispyb_pre_collection(
yield from bps.save()


def read_hardware_for_ispyb_during_collection(
def read_hardware_during_collection(
aperture_scatterguard: ApertureScatterguard,
attenuator: Attenuator,
flux: Flux,
dcm: DCM,
detector: EigerDetector,
):
LOGGER.info("Reading status of beamline for ispyb deposition, during collection.")
yield from bps.create(name=CONST.DESCRIPTORS.ISPYB_TRANSMISSION_FLUX_READ)
LOGGER.info("Reading status of beamline for callbacks, during collection.")
yield from bps.create(name=CONST.DESCRIPTORS.HARDWARE_READ_DURING)
yield from bps.read(aperture_scatterguard)
yield from bps.read(attenuator.actual_transmission)
yield from bps.read(flux.flux_reading)
yield from bps.read(dcm.energy_in_kev)
yield from bps.save()


def read_hardware_for_nexus_writer(detector: EigerDetector):
yield from bps.create(name=CONST.DESCRIPTORS.NEXUS_READ)
yield from bps.read(detector.bit_depth)
yield from bps.save()

Expand Down
15 changes: 5 additions & 10 deletions src/hyperion/experiment_plans/flyscan_xray_centre_plan.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,9 @@

from hyperion.device_setup_plans.manipulate_sample import move_x_y_z
from hyperion.device_setup_plans.read_hardware_for_setup import (
read_hardware_for_ispyb_during_collection,
read_hardware_for_ispyb_pre_collection,
read_hardware_for_nexus_writer,
read_hardware_during_collection,
read_hardware_for_zocalo,
read_hardware_pre_collection,
)
from hyperion.device_setup_plans.setup_panda import (
disarm_panda_for_gridscan,
Expand Down Expand Up @@ -265,7 +264,7 @@ def run_gridscan(
# we should generate an event reading the values which need to be included in the
# ispyb deposition
with TRACER.start_span("ispyb_hardware_readings"):
yield from read_hardware_for_ispyb_pre_collection(
yield from read_hardware_pre_collection(
fgs_composite.undulator,
fgs_composite.synchrotron,
fgs_composite.s4_slit_gaps,
Expand All @@ -274,11 +273,12 @@ def run_gridscan(
)

read_during_collection = partial(
read_hardware_for_ispyb_during_collection,
read_hardware_during_collection,
fgs_composite.aperture_scatterguard,
fgs_composite.attenuator,
fgs_composite.flux,
fgs_composite.dcm,
fgs_composite.eiger,
)

LOGGER.info("Setting fgs params")
Expand All @@ -291,11 +291,6 @@ def run_gridscan(
yield from bps.wait(CONST.WAIT.GRID_READY_FOR_DC)
yield from bps.stage(fgs_composite.eiger)

# This needs to occur after eiger is armed so that
# the HDF5 meta file is present for nexgen to inspect
with TRACER.start_span("nexus_hardware_readings"):
yield from read_hardware_for_nexus_writer(fgs_composite.eiger)

yield from kickoff_and_complete_gridscan(
feature_controlled.fgs_motors,
fgs_composite.eiger,
Expand Down
12 changes: 5 additions & 7 deletions src/hyperion/experiment_plans/rotation_scan_plan.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,9 @@
setup_sample_environment,
)
from hyperion.device_setup_plans.read_hardware_for_setup import (
read_hardware_for_ispyb_during_collection,
read_hardware_for_ispyb_pre_collection,
read_hardware_for_nexus_writer,
read_hardware_during_collection,
read_hardware_for_zocalo,
read_hardware_pre_collection,
)
from hyperion.device_setup_plans.setup_zebra import (
arm_zebra,
Expand Down Expand Up @@ -214,16 +213,14 @@ def _rotation_scan_plan(
# get some information for the ispyb deposition and trigger the callback
yield from read_hardware_for_zocalo(composite.eiger)

yield from read_hardware_for_ispyb_pre_collection(
yield from read_hardware_pre_collection(
composite.undulator,
composite.synchrotron,
composite.s4_slit_gaps,
composite.robot,
composite.smargon,
)

yield from read_hardware_for_nexus_writer(composite.eiger)

# Get ready for the actual scan
yield from bps.abs_set(
axis.velocity, motion_values.speed_for_rotation_deg_s, wait=True
Expand All @@ -242,11 +239,12 @@ def _rotation_scan_plan(
LOGGER.info("Executing rotation scan")
yield from bps.rel_set(axis, motion_values.distance_to_move_deg, wait=True)

yield from read_hardware_for_ispyb_during_collection(
yield from read_hardware_during_collection(
composite.aperture_scatterguard,
composite.attenuator,
composite.flux,
composite.dcm,
composite.eiger,
)

yield from _rotation_scan_plan(motion_values, composite)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ def activity_gated_event(self, doc: Event) -> Event:
)
return doc
match event_descriptor.get("name"):
case CONST.DESCRIPTORS.ISPYB_HARDWARE_READ:
case CONST.DESCRIPTORS.HARDWARE_READ_PRE:
scan_data_infos = self._handle_ispyb_hardware_read(doc)
case CONST.DESCRIPTORS.ISPYB_TRANSMISSION_FLUX_READ:
case CONST.DESCRIPTORS.HARDWARE_READ_DURING:
scan_data_infos = self._handle_ispyb_transmission_flux_read(doc)
case _:
return self._tag_doc(doc)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,7 @@ def activity_gated_event(self, doc: Event):
"has no corresponding descriptor record"
)
return doc
if (
event_descriptor.get("name")
== CONST.DESCRIPTORS.ISPYB_TRANSMISSION_FLUX_READ
):
if event_descriptor.get("name") == CONST.DESCRIPTORS.HARDWARE_READ_DURING:
NEXUS_LOGGER.info(
f"Nexus handler received event from read hardware {format_doc_for_log(doc)}"
)
Expand All @@ -67,12 +64,7 @@ def activity_gated_event(self, doc: Event):
data["flux_flux_reading"],
data["attenuator-actual_transmission"],
)
if event_descriptor.get("name") == CONST.DESCRIPTORS.NEXUS_READ:
NEXUS_LOGGER.info(
f"Nexus handler received event from read hardware {format_doc_for_log(doc)}"
)
vds_data_type = vds_type_based_on_bit_depth(doc["data"]["eiger_bit_depth"])
assert self.writer is not None
self.writer.create_nexus_file(vds_data_type)
NEXUS_LOGGER.info(f"Nexus file created at {self.writer.full_filename}")
return doc
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

from typing import TYPE_CHECKING, Dict

from hyperion.external_interaction.callbacks.logging_callback import format_doc_for_log
from hyperion.external_interaction.callbacks.plan_reactive_callback import (
PlanReactiveCallback,
)
Expand Down Expand Up @@ -75,10 +74,7 @@ def activity_gated_descriptor(self, doc: EventDescriptor):

def activity_gated_event(self, doc: Event) -> Event | None:
assert (event_descriptor := self.descriptors.get(doc["descriptor"])) is not None
if (
event_descriptor.get("name")
== CONST.DESCRIPTORS.ISPYB_TRANSMISSION_FLUX_READ
):
if event_descriptor.get("name") == CONST.DESCRIPTORS.HARDWARE_READ_DURING:
data = doc["data"]
for nexus_writer in [self.nexus_writer_1, self.nexus_writer_2]:
assert nexus_writer, "Nexus callback did not receive start doc"
Expand All @@ -90,15 +86,9 @@ def activity_gated_event(self, doc: Event) -> Event | None:
data["flux_flux_reading"],
data["attenuator-actual_transmission"],
)
if event_descriptor.get("name") == CONST.DESCRIPTORS.NEXUS_READ:
NEXUS_LOGGER.info(
f"Nexus handler received event from read hardware {format_doc_for_log(doc)}"
)
for nexus_writer in [self.nexus_writer_1, self.nexus_writer_2]:
vds_data_type = vds_type_based_on_bit_depth(
doc["data"]["eiger_bit_depth"]
)
assert nexus_writer, "Nexus callback did not receive start doc"
nexus_writer.create_nexus_file(vds_data_type)
NEXUS_LOGGER.info(f"Nexus file created at {nexus_writer.full_filename}")

Expand Down
5 changes: 2 additions & 3 deletions src/hyperion/parameters/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,8 @@ class DocDescriptorNames:
# For callbacks to use
OAV_ROTATION_SNAPSHOT_TRIGGERED = "rotation_snapshot_triggered"
OAV_GRID_SNAPSHOT_TRIGGERED = "snapshot_to_ispyb"
NEXUS_READ = "nexus_read_plan"
ISPYB_HARDWARE_READ = "ispyb_reading_hardware"
ISPYB_TRANSMISSION_FLUX_READ = "ispyb_update_transmission_flux"
HARDWARE_READ_PRE = "read_hardware_for_callbacks_pre_collection"
HARDWARE_READ_DURING = "read_hardware_for_callbacks_during_collection"
ZOCALO_HW_READ = "zocalo_read_hardware_plan"


Expand Down
7 changes: 3 additions & 4 deletions src/hyperion/utils/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
from ophyd_async.core import set_mock_value

from hyperion.device_setup_plans.read_hardware_for_setup import (
read_hardware_for_ispyb_during_collection,
read_hardware_for_nexus_writer,
read_hardware_during_collection,
)
from hyperion.experiment_plans.rotation_scan_plan import RotationScanComposite
from hyperion.external_interaction.callbacks.rotation.nexus_callback import (
Expand Down Expand Up @@ -63,13 +62,13 @@ def fake_rotation_scan(
}
)
def plan():
yield from read_hardware_for_ispyb_during_collection(
yield from read_hardware_during_collection(
rotation_devices.aperture_scatterguard,
rotation_devices.attenuator,
rotation_devices.flux,
rotation_devices.dcm,
rotation_devices.eiger,
)
yield from read_hardware_for_nexus_writer(rotation_devices.eiger)

return plan()

Expand Down
10 changes: 5 additions & 5 deletions tests/system_tests/experiment_plans/test_fgs_plan.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
from ophyd_async.core import set_mock_value

from hyperion.device_setup_plans.read_hardware_for_setup import (
read_hardware_for_ispyb_during_collection,
read_hardware_for_ispyb_pre_collection,
read_hardware_during_collection,
read_hardware_pre_collection,
)
from hyperion.device_setup_plans.xbpm_feedback import (
transmission_and_xbpm_feedback_for_collection_decorator,
Expand Down Expand Up @@ -134,16 +134,16 @@ def test_s03_devices_connect(fxc_composite: FlyScanXRayCentreComposite):

@pytest.mark.asyncio
@pytest.mark.s03
def test_read_hardware_for_ispyb_pre_collection(
def test_read_hardware_pre_collection(
RE: RunEngine,
fxc_composite: FlyScanXRayCentreComposite,
):
@bpp.run_decorator()
def read_run(u, s, g, r, a, f, dcm, ap_sg, sm):
yield from read_hardware_for_ispyb_pre_collection(
yield from read_hardware_pre_collection(
undulator=u, synchrotron=s, s4_slit_gaps=g, robot=r, smargon=sm
)
yield from read_hardware_for_ispyb_during_collection(ap_sg, a, f, dcm)
yield from read_hardware_during_collection(ap_sg, a, f, dcm)

RE(
read_run(
Expand Down
12 changes: 5 additions & 7 deletions tests/system_tests/experiment_plans/test_plan_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
from dodal.devices.undulator import Undulator

from hyperion.device_setup_plans.read_hardware_for_setup import (
read_hardware_for_ispyb_during_collection,
read_hardware_for_ispyb_pre_collection,
read_hardware_during_collection,
read_hardware_pre_collection,
)
from hyperion.parameters.constants import CONST

Expand All @@ -28,7 +28,7 @@ async def test_getting_data_for_ispyb():
prefix=f"{CONST.SIM.BEAMLINE}-AL-APSG-04:", name="ao_sg"
)
smargon = i03.smargon(fake_with_ophyd_sim=True)

eiger = i03.eiger(fake_with_ophyd_sim=True)
await undulator.connect()
await synchrotron.connect()
slit_gaps.wait_for_connection()
Expand All @@ -42,10 +42,8 @@ async def test_getting_data_for_ispyb():

@bpp.run_decorator()
def standalone_read_hardware(und, syn, slits, robot, att, flux, ap_sg, sm):
yield from read_hardware_for_ispyb_pre_collection(
und, syn, slits, robot, smargon=sm
)
yield from read_hardware_for_ispyb_during_collection(ap_sg, att, flux, dcm)
yield from read_hardware_pre_collection(und, syn, slits, robot, smargon=sm)
yield from read_hardware_during_collection(ap_sg, att, flux, dcm, eiger)

RE(
standalone_read_hardware(
Expand Down
7 changes: 3 additions & 4 deletions tests/system_tests/external_interaction/test_nexgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
from bluesky.run_engine import RunEngine

from hyperion.device_setup_plans.read_hardware_for_setup import (
read_hardware_for_ispyb_during_collection,
read_hardware_for_nexus_writer,
read_hardware_during_collection,
)
from hyperion.experiment_plans.rotation_scan_plan import RotationScanComposite
from hyperion.external_interaction.callbacks.rotation.nexus_callback import (
Expand Down Expand Up @@ -161,12 +160,12 @@ def _fake_rotation_scan(
}
)
def plan():
yield from read_hardware_for_ispyb_during_collection(
yield from read_hardware_during_collection(
rotation_devices.aperture_scatterguard,
rotation_devices.attenuator,
rotation_devices.flux,
rotation_devices.dcm,
rotation_devices.eiger,
)
yield from read_hardware_for_nexus_writer(rotation_devices.eiger)

return plan()
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 @@ -83,7 +83,7 @@ def run_generic_ispyb_handler_setup(
} # type: ignore
)
ispyb_handler.activity_gated_descriptor(
{"uid": "123abc", "name": CONST.DESCRIPTORS.ISPYB_HARDWARE_READ} # type: ignore
{"uid": "123abc", "name": CONST.DESCRIPTORS.HARDWARE_READ_PRE} # type: ignore
)
ispyb_handler.activity_gated_event(
make_event_doc(
Expand All @@ -92,7 +92,7 @@ def run_generic_ispyb_handler_setup(
)
)
ispyb_handler.activity_gated_descriptor(
{"uid": "abc123", "name": CONST.DESCRIPTORS.ISPYB_TRANSMISSION_FLUX_READ} # type: ignore
{"uid": "abc123", "name": CONST.DESCRIPTORS.HARDWARE_READ_DURING} # type: ignore
)
ispyb_handler.activity_gated_event(
make_event_doc(
Expand Down
Loading

0 comments on commit b7f06e1

Please sign in to comment.