Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
… finish conversion to new parameter model
  • Loading branch information
dperl-dls committed May 16, 2024
1 parent 552d547 commit da6b0ae
Show file tree
Hide file tree
Showing 83 changed files with 570 additions and 3,328 deletions.
4 changes: 1 addition & 3 deletions src/hyperion/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,7 @@ def compose_start_args(context: BlueskyContext, plan_name: str, action: Actions)
if experiment_registry_entry is None:
raise PlanNotFound(f"Experiment plan '{plan_name}' not found in registry.")

experiment_internal_param_type = experiment_registry_entry.get(
"internal_param_type"
)
experiment_internal_param_type = experiment_registry_entry.get("param_type")
callback_type = experiment_registry_entry.get("callback_collection_type")
plan = context.plan_functions.get(plan_name)
if experiment_internal_param_type is None:
Expand Down
43 changes: 8 additions & 35 deletions src/hyperion/experiment_plans/experiment_registry.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
from __future__ import annotations

from typing import Callable, TypedDict, Union

from dodal.devices.fast_grid_scan import GridScanParams
from dodal.devices.panda_fast_grid_scan import PandAGridScanParams
from dodal.parameters.experiment_parameter_base import AbstractExperimentWithBeamParams
from typing import Callable, TypedDict

import hyperion.experiment_plans.flyscan_xray_centre_plan as flyscan_xray_centre_plan
import hyperion.experiment_plans.panda_flyscan_xray_centre_plan as panda_flyscan_xray_centre_plan
Expand All @@ -26,18 +22,6 @@
RobotLoadThenCentre,
ThreeDGridScan,
)
from hyperion.parameters.plan_specific.grid_scan_with_edge_detect_params import (
GridScanWithEdgeDetectParams,
)
from hyperion.parameters.plan_specific.pin_centre_then_xray_centre_params import (
PinCentreThenXrayCentreParams,
)
from hyperion.parameters.plan_specific.robot_load_then_center_params import (
RobotLoadThenCentreParams,
)
from hyperion.parameters.plan_specific.rotation_scan_internal_params import (
RotationScanParams,
)
from hyperion.parameters.rotation import RotationScan


Expand All @@ -51,59 +35,48 @@ def do_nothing():

class ExperimentRegistryEntry(TypedDict):
setup: Callable
internal_param_type: type[
param_type: type[
ThreeDGridScan
| GridScanWithEdgeDetect
| RotationScan
| PinTipCentreThenXrayCentre
| RobotLoadThenCentre
]
experiment_param_type: type[AbstractExperimentWithBeamParams]
callbacks_factory: CallbacksFactory


EXPERIMENT_TYPES = Union[GridScanParams, RotationScanParams]
PLAN_REGISTRY: dict[str, ExperimentRegistryEntry] = {
"panda_flyscan_xray_centre": {
"setup": panda_flyscan_xray_centre_plan.create_devices,
"internal_param_type": ThreeDGridScan,
"experiment_param_type": PandAGridScanParams,
"param_type": ThreeDGridScan,
"callbacks_factory": create_gridscan_callbacks,
},
"flyscan_xray_centre": {
"setup": flyscan_xray_centre_plan.create_devices,
"internal_param_type": ThreeDGridScan,
"experiment_param_type": GridScanParams,
"param_type": ThreeDGridScan,
"callbacks_factory": create_gridscan_callbacks,
},
"grid_detect_then_xray_centre": {
"setup": grid_detect_then_xray_centre_plan.create_devices,
"internal_param_type": GridScanWithEdgeDetect,
"experiment_param_type": GridScanWithEdgeDetectParams,
"param_type": GridScanWithEdgeDetect,
"callbacks_factory": create_gridscan_callbacks,
},
"rotation_scan": {
"setup": rotation_scan_plan.create_devices,
"internal_param_type": RotationScan,
"experiment_param_type": RotationScanParams,
"param_type": RotationScan,
"callbacks_factory": create_rotation_callbacks,
},
"pin_tip_centre_then_xray_centre": {
"setup": pin_centre_then_xray_centre_plan.create_devices,
"internal_param_type": PinTipCentreThenXrayCentre,
"experiment_param_type": PinCentreThenXrayCentreParams,
"param_type": PinTipCentreThenXrayCentre,
"callbacks_factory": create_gridscan_callbacks,
},
"robot_load_then_centre": {
"setup": robot_load_then_centre_plan.create_devices,
"internal_param_type": RobotLoadThenCentre,
"experiment_param_type": RobotLoadThenCentreParams,
"param_type": RobotLoadThenCentre,
"callbacks_factory": create_robot_load_and_centre_callbacks,
},
}
EXPERIMENT_NAMES = list(PLAN_REGISTRY.keys())
EXPERIMENT_TYPE_LIST = [p["experiment_param_type"] for p in PLAN_REGISTRY.values()]
EXPERIMENT_TYPE_DICT = dict(zip(EXPERIMENT_NAMES, EXPERIMENT_TYPE_LIST))


class PlanNotFound(Exception):
Expand Down
14 changes: 7 additions & 7 deletions src/hyperion/experiment_plans/flyscan_xray_centre_plan.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,8 @@ def kickoff_and_complete_gridscan(
eiger: EigerDetector,
synchrotron: Synchrotron,
zocalo_environment: str,
scan_points: list[AxesPoints[Axis]],
scan_points: AxesPoints[Axis],
scan_start_indices: list[int],
):
@TRACER.start_as_current_span(CONST.PLAN.DO_FGS)
@bpp.set_run_key_decorator(CONST.PLAN.DO_FGS)
Expand All @@ -173,6 +174,7 @@ def kickoff_and_complete_gridscan(
"subplan_name": CONST.PLAN.DO_FGS,
"zocalo_environment": zocalo_environment,
"scan_points": scan_points,
"scan_start_indices": scan_start_indices,
}
)
@bpp.contingency_decorator(
Expand Down Expand Up @@ -261,10 +263,8 @@ def run_gridscan(
fgs_composite.eiger,
fgs_composite.synchrotron,
parameters.zocalo_environment,
[
parameters.old_parameters().get_scan_points(1),
parameters.old_parameters().get_scan_points(2),
],
parameters.scan_points,
parameters.scan_indices,
)
yield from bps.abs_set(fgs_motors.z_steps, 0, wait=False)

Expand Down Expand Up @@ -345,7 +345,7 @@ def flyscan_xray_centre(
at any point in it.
Args:
parameters (FGSInternalParameters): The parameters to run the scan.
parameters (ThreeDGridScan): The parameters to run the scan.
Returns:
Generator: The plan for the gridscan
Expand All @@ -359,7 +359,7 @@ def flyscan_xray_centre(
md={
"subplan_name": CONST.PLAN.GRIDSCAN_OUTER,
CONST.TRIGGER.ZOCALO: CONST.PLAN.DO_FGS,
"hyperion_internal_parameters": parameters.old_parameters().json(),
"hyperion_parameters": parameters.json(),
"activate_callbacks": [
"GridscanNexusFileCallback",
],
Expand Down
10 changes: 4 additions & 6 deletions src/hyperion/experiment_plans/panda_flyscan_xray_centre_plan.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,8 @@ def run_gridscan(
fgs_composite.eiger,
fgs_composite.synchrotron,
parameters.zocalo_environment,
[
parameters.old_parameters().get_scan_points(1),
parameters.old_parameters().get_scan_points(2),
],
parameters.scan_points,
parameters.scan_indices,
)

yield from bps.abs_set(fgs_motors.z_steps, 0, wait=False)
Expand Down Expand Up @@ -261,7 +259,7 @@ def panda_flyscan_xray_centre(
at any point in it.
Args:
parameters (FGSInternalParameters): The parameters to run the scan.
parameters (ThreeDGridScan): The parameters to run the scan.
Returns:
Generator: The plan for the gridscan
Expand All @@ -276,7 +274,7 @@ def panda_flyscan_xray_centre(
md={
"subplan_name": CONST.PLAN.GRIDSCAN_OUTER,
CONST.TRIGGER.ZOCALO: CONST.PLAN.DO_FGS,
"hyperion_internal_parameters": parameters.old_parameters().json(),
"hyperion_parameters": parameters.json(),
"activate_callbacks": [
"GridscanNexusFileCallback",
],
Expand Down
2 changes: 1 addition & 1 deletion src/hyperion/experiment_plans/rotation_scan_plan.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ def rotation_scan(
md={
"subplan_name": CONST.PLAN.ROTATION_OUTER,
CONST.TRIGGER.ZOCALO: CONST.PLAN.ROTATION_MAIN,
"hyperion_internal_parameters": parameters.old_parameters().json(),
"hyperion_parameters": parameters.json(),
"activate_callbacks": [
"RotationISPyBCallback",
"RotationNexusFileCallback",
Expand Down
17 changes: 6 additions & 11 deletions src/hyperion/external_interaction/callbacks/ispyb_callback_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,16 @@
from hyperion.external_interaction.ispyb.ispyb_utils import get_ispyb_config
from hyperion.log import ISPYB_LOGGER, set_dcgid_tag
from hyperion.parameters.constants import CONST
from hyperion.parameters.internal_parameters import InternalParameters
from hyperion.parameters.plan_specific.gridscan_internal_params import (
GridscanInternalParameters,
)
from hyperion.parameters.plan_specific.rotation_scan_internal_params import (
RotationInternalParameters,
)
from hyperion.parameters.gridscan import ThreeDGridScan
from hyperion.parameters.rotation import RotationScan
from hyperion.utils.utils import convert_eV_to_angstrom

D = TypeVar("D")
if TYPE_CHECKING:
from event_model.documents import Event, EventDescriptor, RunStart, RunStop

ALL_PLAN_PARAMS = ThreeDGridScan | RotationScan


class BaseISPyBCallback(PlanReactiveCallback):
def __init__(
Expand All @@ -52,9 +49,7 @@ def __init__(
ISPYB_LOGGER.debug("Initialising ISPyB callback")
super().__init__(log=ISPYB_LOGGER, emit=emit)
self._oav_snapshot_event_idx: int = 0
self.params: GridscanInternalParameters | RotationInternalParameters | None = (
None
)
self.params: ALL_PLAN_PARAMS | None = None
self.ispyb: StoreInIspyb
self.descriptors: Dict[str, EventDescriptor] = {}
self.ispyb_config = get_ispyb_config()
Expand Down Expand Up @@ -198,7 +193,7 @@ def _handle_ispyb_transmission_flux_read(self, doc) -> Sequence[ScanDataInfo]:
def populate_info_for_update(
self,
event_sourced_data_collection_info: DataCollectionInfo,
params: InternalParameters,
params: ALL_PLAN_PARAMS,
) -> Sequence[ScanDataInfo]:
pass

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@
from hyperion.log import ISPYB_LOGGER, set_dcgid_tag
from hyperion.parameters.components import IspybExperimentType
from hyperion.parameters.constants import CONST
from hyperion.parameters.plan_specific.rotation_scan_internal_params import (
RotationInternalParameters,
)
from hyperion.parameters.rotation import RotationScan

if TYPE_CHECKING:
from event_model.documents import Event, RunStart, RunStop
Expand Down Expand Up @@ -56,25 +54,21 @@ def __init__(
emit: Callable[..., Any] | None = None,
) -> None:
super().__init__(emit=emit)
self.last_sample_id: str | None = None
self.last_sample_id: int | None = None
self.ispyb_ids: IspybIds = IspybIds()

def activity_gated_start(self, doc: RunStart):
if doc.get("subplan_name") == CONST.PLAN.ROTATION_OUTER:
ISPYB_LOGGER.info(
"ISPyB callback recieved start document with experiment parameters."
)
json_params = doc.get("hyperion_internal_parameters")
self.params = RotationInternalParameters.from_json(json_params)
self.params = RotationScan.from_json(doc.get("hyperion_parameters"))
dcgid = (
self.ispyb_ids.data_collection_group_id
if (
self.params.hyperion_params.ispyb_params.sample_id
== self.last_sample_id
)
if (self.params.sample_id == self.last_sample_id)
else None
)
n_images = self.params.experiment_params.get_num_images()
n_images = self.params.num_images
if n_images < 200:
ISPYB_LOGGER.info(
f"Collection has {n_images} images - treating as a screening collection - new DCG"
Expand All @@ -85,10 +79,8 @@ def activity_gated_start(self, doc: RunStart):
ISPYB_LOGGER.info(
f"Collection has {n_images} images - treating as a genuine dataset - storing sampleID to bundle images"
)
self.last_sample_id = self.params.hyperion_params.ispyb_params.sample_id
experiment_type = (
self.params.hyperion_params.ispyb_params.ispyb_experiment_type
)
self.last_sample_id = self.params.sample_id
experiment_type = self.params.ispyb_extras.ispyb_experiment_type
if experiment_type:
self.ispyb = StoreInIspyb(
self.ispyb_config,
Expand All @@ -101,20 +93,18 @@ def activity_gated_start(self, doc: RunStart):
ISPYB_LOGGER.info("Beginning ispyb deposition")
data_collection_group_info = populate_data_collection_group(
self.ispyb.experiment_type,
self.params.hyperion_params.detector_params,
self.params.hyperion_params.ispyb_params,
self.params.detector_params,
self.params.ispyb_params,
)
data_collection_info = populate_data_collection_info_for_rotation(
self.params.hyperion_params.ispyb_params,
self.params.hyperion_params.detector_params,
self.params,
self.params
)
data_collection_info = populate_remaining_data_collection_info(
COMMENT_FOR_ROTATION_SCAN,
dcgid,
data_collection_info,
self.params.hyperion_params.detector_params,
self.params.hyperion_params.ispyb_params,
self.params.detector_params,
self.params.ispyb_params,
)
data_collection_info.parent_id = dcgid
scan_data_info = ScanDataInfo(
Expand All @@ -134,12 +124,12 @@ def populate_info_for_update(
assert (
self.ispyb_ids.data_collection_ids
), "Expect an existing DataCollection to update"
params = cast(RotationInternalParameters, params)
params = cast(RotationScan, params)
return [
ScanDataInfo(
data_collection_info=event_sourced_data_collection_info,
data_collection_position_info=populate_data_collection_position_info(
params.hyperion_params.ispyb_params
params.ispyb_params
),
data_collection_id=self.ispyb_ids.data_collection_ids[0],
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,19 @@
get_xtal_snapshots,
)
from hyperion.external_interaction.ispyb.data_model import DataCollectionInfo
from hyperion.parameters.rotation import RotationScan


def populate_data_collection_info_for_rotation(
ispyb_params, detector_params, full_params
):
def populate_data_collection_info_for_rotation(full_params: RotationScan):
info = DataCollectionInfo(
omega_start=detector_params.omega_start,
data_collection_number=detector_params.run_number, # type:ignore # the validator always makes this int
n_images=full_params.experiment_params.get_num_images(),
axis_range=full_params.experiment_params.image_width,
axis_end=(
full_params.experiment_params.omega_start
+ full_params.experiment_params.rotation_angle
),
kappa_start=full_params.experiment_params.chi_start,
omega_start=full_params.omega_start_deg,
data_collection_number=full_params.detector_params.run_number, # type:ignore # the validator always makes this int
n_images=full_params.num_images,
axis_range=full_params.rotation_increment_deg,
axis_end=(full_params.omega_start_deg + full_params.scan_width_deg),
kappa_start=full_params.kappa_start_deg,
)
(info.xtal_snapshot1, info.xtal_snapshot2, info.xtal_snapshot3) = (
get_xtal_snapshots(full_params.ispyb_params)
)
(
info.xtal_snapshot1,
info.xtal_snapshot2,
info.xtal_snapshot3,
) = get_xtal_snapshots(ispyb_params)
return info
Loading

0 comments on commit da6b0ae

Please sign in to comment.