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

Commit

Permalink
Merge branch 'main' into 759_fix_full_grid_scan_issues
Browse files Browse the repository at this point in the history
  • Loading branch information
DominicOram committed Jul 12, 2023
2 parents 183bf0c + 0b83859 commit d50ee6d
Show file tree
Hide file tree
Showing 10 changed files with 510 additions and 128 deletions.
4 changes: 3 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ install_requires =
xarray
doct
databroker
dodal @ git+https://github.com/DiamondLightSource/python-dodal.git@b49b2dffa27e44eebf42e8c7e35a375da42654cd
dodal @ git+https://github.com/DiamondLightSource/python-dodal.git@3106d1f2e4f97bde92720d115c8fbb827ad374b1
pydantic<2.0 # See https://github.com/DiamondLightSource/python-artemis/issues/774


[options.extras_require]
dev =
Expand Down
31 changes: 14 additions & 17 deletions src/artemis/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import atexit
import threading
from dataclasses import dataclass
from json import JSONDecodeError
from queue import Queue
from traceback import format_exception
from typing import Callable, Optional, Tuple
Expand All @@ -11,7 +10,6 @@
from dataclasses_json import dataclass_json
from flask import Flask, request
from flask_restful import Api, Resource
from jsonschema.exceptions import ValidationError

import artemis.log
from artemis.exceptions import WarningException
Expand Down Expand Up @@ -47,6 +45,16 @@ def __init__(self, status: Status, message: str = "") -> None:
self.message = message


@dataclass_json
@dataclass
class ErrorStatusAndMessage(StatusAndMessage):
exception_type: str = ""

def __init__(self, exception: Exception) -> None:
super().__init__(Status.FAILED, repr(exception))
self.exception_type = type(exception).__name__


class BlueskyRunner:
command_queue: "Queue[Command]" = Queue()
current_status: StatusAndMessage = StatusAndMessage(Status.IDLE)
Expand Down Expand Up @@ -87,7 +95,7 @@ def stopping_thread(self):
self.RE.abort()
self.current_status = StatusAndMessage(Status.IDLE)
except Exception as e:
self.current_status = StatusAndMessage(Status.FAILED, repr(e))
self.current_status = ErrorStatusAndMessage(e)

def stop(self) -> StatusAndMessage:
if self.current_status.status == Status.IDLE.value:
Expand Down Expand Up @@ -125,17 +133,15 @@ def wait_on_queue(self):
self.last_run_aborted = False
except WarningException as exception:
artemis.log.LOGGER.warning("Warning Exception", exc_info=True)
self.current_status = StatusAndMessage(Status.WARN, repr(exception))
self.current_status = ErrorStatusAndMessage(exception)
except Exception as exception:
artemis.log.LOGGER.error("Exception on running plan", exc_info=True)

if self.last_run_aborted:
# Aborting will cause an exception here that we want to swallow
self.last_run_aborted = False
else:
self.current_status = StatusAndMessage(
Status.FAILED, repr(exception)
)
self.current_status = ErrorStatusAndMessage(exception)


class RunExperiment(Resource):
Expand Down Expand Up @@ -175,17 +181,8 @@ def put(self, plan_name: str, action: Actions):
status_and_message = self.runner.start(
experiment, parameters, plan_name
)
except JSONDecodeError as e:
status_and_message = StatusAndMessage(Status.FAILED, repr(e))
except PlanNotFound as e:
status_and_message = StatusAndMessage(Status.FAILED, repr(e))
except ValidationError as e:
status_and_message = StatusAndMessage(Status.FAILED, repr(e))
artemis.log.LOGGER.error(
f" {format_exception(e)}: Invalid json parameters"
)
except Exception as e:
status_and_message = StatusAndMessage(Status.FAILED, repr(e))
status_and_message = ErrorStatusAndMessage(e)
artemis.log.LOGGER.error(format_exception(e))

elif action == Actions.STOP.value:
Expand Down
8 changes: 1 addition & 7 deletions src/artemis/device_setup_plans/setup_oav.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,15 @@
from artemis.log import LOGGER


def start_mxsc(oav: OAV, input_plugin, min_callback_time, filename):
def start_mxsc(oav: OAV, min_callback_time, filename):
"""
Sets PVs relevant to edge detection plugin.
Args:
input_plugin: link to the camera stream
min_callback_time: the value to set the minimum callback time to
filename: filename of the python script to detect edge waveforms from camera stream.
Returns: None
"""
yield from bps.abs_set(oav.mxsc.input_plugin, input_plugin)

# Turns the area detector plugin on
yield from bps.abs_set(oav.mxsc.enable_callbacks, 1)

Expand Down Expand Up @@ -82,13 +79,10 @@ def pre_centring_setup_oav(oav: OAV, parameters: OAVParameters):
# Connect MXSC output to MJPG input
yield from start_mxsc(
oav,
parameters.input_plugin + "." + parameters.mxsc_input,
parameters.min_callback_time,
parameters.detection_script_filename,
)

yield from bps.abs_set(oav.snapshot.input_plugin, parameters.input_plugin + ".CAM")

zoom_level_str = f"{float(parameters.zoom)}x"
if zoom_level_str not in oav.zoom_controller.allowed_zoom_levels:
raise OAVError_ZoomLevelNotFound(
Expand Down
27 changes: 22 additions & 5 deletions src/artemis/experiment_plans/full_grid_scan.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,37 @@ def wait_for_det_to_finish_moving(detector: DetectorMotion, timeout=120):
raise TimeoutError("Detector not finished moving")


def detect_grid_and_do_gridscan(
def start_arming_then_do_grid(
parameters: GridScanWithEdgeDetectInternalParameters,
backlight: Backlight,
eiger: EigerDetector,
aperture_scatterguard: ApertureScatterguard,
detector_motion: DetectorMotion,
oav_params: OAVParameters,
experiment_params: GridScanWithEdgeDetectParams,
):
# Start stage with asynchronous arming here
yield from bps.abs_set(eiger.do_arm, 1, group="arming")

yield from bpp.finalize_wrapper(
detect_grid_and_do_gridscan(
parameters,
backlight,
aperture_scatterguard,
detector_motion,
oav_params,
),
bps.unstage(eiger),
)


def detect_grid_and_do_gridscan(
parameters: GridScanWithEdgeDetectInternalParameters,
backlight: Backlight,
aperture_scatterguard: ApertureScatterguard,
detector_motion: DetectorMotion,
oav_params: OAVParameters,
):
experiment_params: GridScanWithEdgeDetectParams = parameters.experiment_params
fgs_params = GridScanParams(dwell_time=experiment_params.exposure_time * 1000)

detector_params = parameters.artemis_params.detector_params
Expand Down Expand Up @@ -153,14 +172,12 @@ def get_plan(
eiger.set_detector_parameters(parameters.artemis_params.detector_params)

oav_params = OAVParameters("xrayCentring", **oav_param_files)
experiment_params: GridScanWithEdgeDetectParams = parameters.experiment_params

return detect_grid_and_do_gridscan(
return start_arming_then_do_grid(
parameters,
backlight,
eiger,
aperture_scatterguard,
detector_motion,
oav_params,
experiment_params,
)
5 changes: 2 additions & 3 deletions src/artemis/experiment_plans/oav_grid_detection_plan.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def grid_detection_plan(
width,
box_size_microns,
),
reset_oav(parameters),
reset_oav(),
)


Expand Down Expand Up @@ -207,7 +207,6 @@ def grid_detection_main_plan(
out_parameters.z_step_size = box_size_um / 1000


def reset_oav(parameters: OAVParameters):
def reset_oav():
oav = i03.oav()
yield from bps.abs_set(oav.snapshot.input_plugin, parameters.input_plugin + ".CAM")
yield from bps.abs_set(oav.mxsc.enable_callbacks, 0)
Loading

0 comments on commit d50ee6d

Please sign in to comment.