Skip to content
This repository was archived by the owner on Sep 2, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
cb1a877
Changed Point3D and Point2D to use numpy arrays
olliesilvester Apr 18, 2023
fb3c0cd
Fixed tests, create_point checks for NoneType
olliesilvester Apr 19, 2023
2cfa8fb
changed create_point dtype
olliesilvester Apr 19, 2023
3d01051
Replace Point3D with create_point
olliesilvester Apr 19, 2023
c6884af
Fix tests and replace Point2D
olliesilvester Apr 19, 2023
cbfba17
minor fixes
olliesilvester Apr 19, 2023
018a2f8
Merge main into 348_replace_point_3d_to_support_arithmetic
olliesilvester Apr 20, 2023
0c04551
minor fixes
olliesilvester Apr 20, 2023
da00154
fixed and added tests
olliesilvester Apr 20, 2023
8a66859
Fix other unit tests
olliesilvester Apr 20, 2023
8fc01c0
Merge branch 'main' of https://github.com/DiamondLightSource/python-a…
olliesilvester Apr 21, 2023
818a6e9
changed setup.cfg for dodal
olliesilvester Apr 21, 2023
6a81f39
replaced create_point() with np.array()
olliesilvester May 3, 2023
4125cd8
fix unit tests
olliesilvester May 3, 2023
a6a4a3c
minor numpifying
olliesilvester May 3, 2023
a9457ce
remove unused imports
olliesilvester May 3, 2023
838f453
Merge branch 'main' into 348_replace_point_3d_to_support_arithmetic
olliesilvester May 15, 2023
809785b
changed setup to correct dodal
olliesilvester May 15, 2023
12a800f
Merge branch 'main' into 348_replace_point_3d_to_support_arithmetic
d-perl May 31, 2023
d3c7442
update merged changes
d-perl May 31, 2023
d82303d
fix encoding
d-perl May 31, 2023
86cb2a4
remove unused import
d-perl May 31, 2023
2d1a121
Merge branch 'main' into 348_replace_point_3d_to_support_arithmetic
d-perl Jun 2, 2023
4e96892
fix merge remnants
d-perl Jun 2, 2023
e307f6f
fix ndarray serialisation for __eq__ and otherwise
d-perl Jun 2, 2023
dfa5e68
update dodal req
d-perl Jun 2, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ install_requires =
xarray
doct
databroker
dodal @ git+https://github.com/DiamondLightSource/python-dodal.git
dodal @ git+https://github.com/DiamondLightSource/python-dodal.git@0570e5e6e5e134fd0697cadccc5bd6d2c6ed77d1


[options.extras_require]
Expand Down
20 changes: 11 additions & 9 deletions src/artemis/experiment_plans/fast_grid_scan_plan.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import bluesky.plan_stubs as bps
import bluesky.preprocessors as bpp
import numpy as np
from bluesky import RunEngine
from bluesky.utils import ProgressBarManager
from dodal.beamlines import i03
Expand Down Expand Up @@ -36,7 +37,6 @@
)
from artemis.parameters.constants import ISPYB_PLAN_NAME, SIM_BEAMLINE
from artemis.tracing import TRACER
from artemis.utils.utils import Point3D

if TYPE_CHECKING:
from artemis.external_interaction.callbacks.fgs.fgs_callback_collection import (
Expand Down Expand Up @@ -146,7 +146,7 @@ def read_hardware_for_ispyb(
@bpp.run_decorator(md={"subplan_name": "move_xyz"})
def move_xyz(
sample_motors,
xray_centre_motor_position: Point3D,
xray_centre_motor_position: np.ndarray,
md={
"plan_name": "move_xyz",
},
Expand All @@ -156,11 +156,11 @@ def move_xyz(
artemis.log.LOGGER.info(f"Moving Smargon x, y, z to: {xray_centre_motor_position}")
yield from bps.mv(
sample_motors.x,
xray_centre_motor_position.x,
xray_centre_motor_position[0],
sample_motors.y,
xray_centre_motor_position.y,
xray_centre_motor_position[1],
sample_motors.z,
xray_centre_motor_position.z,
xray_centre_motor_position[2],
)


Expand Down Expand Up @@ -242,10 +242,12 @@ def run_gridscan_and_move(
and moves to the centre of mass determined by zocalo"""

# We get the initial motor positions so we can return to them on zocalo failure
initial_xyz = Point3D(
(yield from bps.rd(fgs_composite.sample_motors.x)),
(yield from bps.rd(fgs_composite.sample_motors.y)),
(yield from bps.rd(fgs_composite.sample_motors.z)),
initial_xyz = np.array(
[
(yield from bps.rd(fgs_composite.sample_motors.x)),
(yield from bps.rd(fgs_composite.sample_motors.y)),
(yield from bps.rd(fgs_composite.sample_motors.z)),
]
)

yield from setup_zebra_for_fgs(fgs_composite.zebra)
Expand Down
3 changes: 1 addition & 2 deletions src/artemis/experiment_plans/full_grid_scan.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
from artemis.log import LOGGER
from artemis.parameters.beamline_parameters import get_beamline_parameters
from artemis.parameters.plan_specific.fgs_internal_params import GridScanParams
from artemis.utils.utils import Point3D

if TYPE_CHECKING:
from artemis.parameters.plan_specific.grid_scan_with_edge_detect_params import (
Expand Down Expand Up @@ -102,7 +101,7 @@ def detect_grid_and_do_gridscan():
parameters.artemis_params.ispyb_params.xtal_snapshots_omega_end = (
out_snapshot_filenames[1]
)
parameters.artemis_params.ispyb_params.upper_left = Point3D(**out_upper_left)
parameters.artemis_params.ispyb_params.upper_left = out_upper_left

fgs_params.__post_init__()

Expand Down
12 changes: 6 additions & 6 deletions src/artemis/experiment_plans/oav_grid_detection_plan.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import math
from os.path import join as path_join
from typing import TYPE_CHECKING, Dict, List
from typing import TYPE_CHECKING, List

import bluesky.plan_stubs as bps
import numpy as np
Expand Down Expand Up @@ -32,7 +32,7 @@ def grid_detection_plan(
snapshot_template: str,
snapshot_dir: str,
out_snapshot_filenames: List[List[str]],
out_upper_left: Dict,
out_upper_left: list[float] | np.ndarray,
width=600,
box_size_microns=20,
):
Expand All @@ -57,7 +57,7 @@ def grid_detection_main_plan(
snapshot_template: str,
snapshot_dir: str,
out_snapshot_filenames: List[List[str]],
out_upper_left: Dict,
out_upper_left: list[float] | np.ndarray,
grid_width_px: int,
box_size_um: float,
):
Expand Down Expand Up @@ -133,10 +133,10 @@ def grid_detection_main_plan(

upper_left = (tip_x_px, min_y)
if angle == 0:
out_upper_left["x"] = int(tip_x_px)
out_upper_left["y"] = int(min_y)
out_upper_left[0] = int(tip_x_px)
out_upper_left[1] = int(min_y)
else:
out_upper_left["z"] = int(min_y)
out_upper_left[2] = int(min_y)

yield from bps.abs_set(oav.snapshot.top_left_x, upper_left[0])
yield from bps.abs_set(oav.snapshot.top_left_y, upper_left[1])
Expand Down
18 changes: 9 additions & 9 deletions src/artemis/experiment_plans/tests/test_fast_grid_scan_plan.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from unittest.mock import ANY, MagicMock, call, patch

import bluesky.plan_stubs as bps
import numpy as np
import pytest
from bluesky.run_engine import RunEngine
from dodal.devices.det_dim_constants import (
Expand Down Expand Up @@ -38,7 +39,6 @@
from artemis.log import set_up_logging_handlers
from artemis.parameters import external_parameters
from artemis.parameters.plan_specific.fgs_internal_params import FGSInternalParameters
from artemis.utils.utils import Point3D


def test_given_full_parameters_dict_when_detector_name_used_and_converted_then_detector_constants_correct(
Expand Down Expand Up @@ -177,11 +177,11 @@ def test_results_passed_to_move_motors(
set_up_logging_handlers(logging_level="INFO", dev_mode=True)
RE.subscribe(VerbosePlanExecutionLoggingCallback())
motor_position = test_params.experiment_params.grid_position_to_motor_position(
Point3D(1, 2, 3)
np.array([1, 2, 3])
)
RE(move_xyz(fake_fgs_composite.sample_motors, motor_position))
bps_mv.assert_called_once_with(
ANY, motor_position.x, ANY, motor_position.y, ANY, motor_position.z
ANY, motor_position[0], ANY, motor_position[1], ANY, motor_position[2]
)


Expand Down Expand Up @@ -215,9 +215,9 @@ def test_individual_plans_triggered_once_and_only_once_in_composite_run(
)

run_gridscan.assert_called_once_with(fake_fgs_composite, test_params)
move_xyz.assert_called_once_with(
ANY, Point3D(x=-0.05, y=0.05, z=0.15000000000000002)
)
array_arg = move_xyz.call_args.args[1]
np.testing.assert_allclose(array_arg, np.array([-0.05, 0.05, 0.15]))
move_xyz.assert_called_once()


@patch(
Expand Down Expand Up @@ -250,9 +250,9 @@ def test_logging_within_plan(
)

run_gridscan.assert_called_once_with(fake_fgs_composite, test_params)
move_xyz.assert_called_once_with(
ANY, Point3D(x=-0.05, y=0.05, z=0.15000000000000002)
)
array_arg = move_xyz.call_args.args[1]
np.testing.assert_array_almost_equal(array_arg, np.array([-0.05, 0.05, 0.15]))
move_xyz.assert_called_once()


@patch("artemis.experiment_plans.fast_grid_scan_plan.bps.sleep")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from unittest.mock import MagicMock

import numpy as np
import pytest
from bluesky.run_engine import RunEngine
from dodal.devices.eiger import DetectorParams, EigerDetector
Expand All @@ -14,7 +15,6 @@
from artemis.parameters.constants import SIM_BEAMLINE
from artemis.parameters.external_parameters import from_file as default_raw_params
from artemis.parameters.plan_specific.fgs_internal_params import FGSInternalParameters
from artemis.utils.utils import Point3D


def test_callback_collection_init():
Expand Down Expand Up @@ -90,7 +90,7 @@ def test_communicator_in_composite_run(
callbacks.zocalo_handler._wait_for_result = MagicMock()
callbacks.zocalo_handler._run_end = MagicMock()
callbacks.zocalo_handler._run_start = MagicMock()
callbacks.zocalo_handler.xray_centre_motor_position = Point3D(1, 2, 3)
callbacks.zocalo_handler.xray_centre_motor_position = np.array([1, 2, 3])

fast_grid_scan_composite = FGSComposite()
# this is where it's currently getting stuck:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import operator
from unittest.mock import MagicMock, call

import numpy as np
import pytest

from artemis.external_interaction.callbacks.fgs.fgs_callback_collection import (
Expand All @@ -11,7 +11,6 @@
from artemis.external_interaction.zocalo.zocalo_interaction import NoDiffractionFound
from artemis.parameters.external_parameters import from_file as default_raw_params
from artemis.parameters.plan_specific.fgs_internal_params import FGSInternalParameters
from artemis.utils.utils import Point3D

EXPECTED_DCID = 100
EXPECTED_RUN_START_MESSAGE = {"event": "start", "ispyb_dcid": EXPECTED_DCID}
Expand Down Expand Up @@ -82,7 +81,7 @@ def test_zocalo_called_to_wait_on_results_when_communicator_wait_for_results_cal
callbacks = FGSCallbackCollection.from_params(dummy_params)
mock_zocalo_functions(callbacks)
callbacks.ispyb_handler.ispyb_ids = (0, 0, 100)
expected_centre_grid_coords = Point3D(1, 2, 3)
expected_centre_grid_coords = np.array([1, 2, 3])
single_crystal_result = [
{
"max_voxel": [1, 2, 3],
Expand All @@ -95,20 +94,16 @@ def test_zocalo_called_to_wait_on_results_when_communicator_wait_for_results_cal
single_crystal_result
)

found_centre = callbacks.zocalo_handler.wait_for_results(Point3D(0, 0, 0))[0]
found_centre = callbacks.zocalo_handler.wait_for_results(np.array([0, 0, 0]))[0]
callbacks.zocalo_handler.zocalo_interactor.wait_for_result.assert_called_once_with(
100
)
expected_centre_motor_coords = (
dummy_params.experiment_params.grid_position_to_motor_position(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so part of the reason for this whole refactor is so that we could do something tidier like

expected_centre_grid_coords - [0.5, 0.5, 0.5]

Point3D(
expected_centre_grid_coords.x - 0.5,
expected_centre_grid_coords.y - 0.5,
expected_centre_grid_coords.z - 0.5,
)
expected_centre_grid_coords - 0.5
)
)
assert found_centre == expected_centre_motor_coords
np.testing.assert_array_equal(found_centre, expected_centre_motor_coords)


def test_GIVEN_no_results_from_zocalo_WHEN_communicator_wait_for_results_called_THEN_fallback_centre_used(
Expand All @@ -121,13 +116,13 @@ def test_GIVEN_no_results_from_zocalo_WHEN_communicator_wait_for_results_called_
NoDiffractionFound()
)

fallback_position = Point3D(1, 2, 3)
fallback_position = np.array([1, 2, 3])

found_centre = callbacks.zocalo_handler.wait_for_results(fallback_position)[0]
callbacks.zocalo_handler.zocalo_interactor.wait_for_result.assert_called_once_with(
100
)
assert found_centre == fallback_position
np.testing.assert_array_equal(found_centre, fallback_position)


def test_GIVEN_ispyb_not_started_WHEN_trigger_zocalo_handler_THEN_raises_exception(
Expand All @@ -146,11 +141,11 @@ def test_multiple_results_from_zocalo_sorted_by_total_count_returns_centre_and_b
callbacks = FGSCallbackCollection.from_params(dummy_params)
mock_zocalo_functions(callbacks)
callbacks.ispyb_handler.ispyb_ids = (0, 0, 100)
expected_centre_grid_coords = Point3D(4, 6, 2)
expected_centre_grid_coords = np.array([4, 6, 2])
multi_crystal_result = [
{
"max_voxel": [1, 2, 3],
"centre_of_mass": Point3D(3, 11, 11),
"centre_of_mass": np.array([3, 11, 11]),
"bounding_box": [[1, 1, 1], [3, 3, 3]],
"n_voxels": 2,
"total_count": 192512.0,
Expand All @@ -167,21 +162,23 @@ def test_multiple_results_from_zocalo_sorted_by_total_count_returns_centre_and_b
multi_crystal_result
)
found_centre, found_bbox = callbacks.zocalo_handler.wait_for_results(
Point3D(0, 0, 0)
np.array([0, 0, 0])
)
callbacks.zocalo_handler.zocalo_interactor.wait_for_result.assert_called_once_with(
100
)
expected_centre_motor_coords = (
dummy_params.experiment_params.grid_position_to_motor_position(
Point3D(
expected_centre_grid_coords.x - 0.5,
expected_centre_grid_coords.y - 0.5,
expected_centre_grid_coords.z - 0.5,
np.array(
[
expected_centre_grid_coords[0] - 0.5,
expected_centre_grid_coords[1] - 0.5,
expected_centre_grid_coords[2] - 0.5,
]
)
)
)
assert found_centre == expected_centre_motor_coords
np.testing.assert_array_equal(found_centre, expected_centre_motor_coords)

expected_bbox_size = list(map(operator.sub, [8, 8, 7], [2, 2, 2]))
assert found_bbox == expected_bbox_size
expected_bbox_size = np.array([8, 8, 7]) - np.array([2, 2, 2])
np.testing.assert_array_equal(found_bbox, expected_bbox_size)
29 changes: 14 additions & 15 deletions src/artemis/external_interaction/callbacks/fgs/zocalo_callback.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from __future__ import annotations

import operator
import time
from typing import Callable, Optional

import numpy as np
from bluesky.callbacks import CallbackBase
from numpy import ndarray

from artemis.external_interaction.callbacks.fgs.ispyb_callback import (
FGSISPyBHandlerCallback,
Expand All @@ -16,7 +17,6 @@
)
from artemis.log import LOGGER
from artemis.parameters.plan_specific.fgs_internal_params import FGSInternalParameters
from artemis.utils.utils import Point3D


class FGSZocaloCallback(CallbackBase):
Expand All @@ -43,7 +43,7 @@ def __init__(
self, parameters: FGSInternalParameters, ispyb_handler: FGSISPyBHandlerCallback
):
self.grid_position_to_motor_position: Callable[
[Point3D], Point3D
[ndarray], ndarray
] = parameters.experiment_params.grid_position_to_motor_position
self.processing_start_time = 0.0
self.processing_time = 0.0
Expand Down Expand Up @@ -76,14 +76,14 @@ def stop(self, doc: dict):
self.zocalo_interactor.run_end(id)
self.processing_start_time = time.time()

def wait_for_results(self, fallback_xyz: Point3D) -> Point3D:
def wait_for_results(self, fallback_xyz: ndarray) -> tuple[ndarray, Optional[list]]:
"""Blocks until a centre has been received from Zocalo

Args:
fallback_xyz (Point3D): The position to fallback to if no centre is found
fallback_xyz (ndarray): The position to fallback to if no centre is found

Returns:
Point3D: The xray centre position to move to
ndarray: The xray centre position to move to
"""
datacollection_group_id = self.ispyb.ispyb_ids[2]

Expand All @@ -102,13 +102,12 @@ def wait_for_results(self, fallback_xyz: Point3D) -> Point3D:
bboxes = []
for n, res in enumerate(raw_results):
bboxes.append(
list(
map(
operator.sub, res["bounding_box"][1], res["bounding_box"][0]
)
)
np.array(res["bounding_box"][1]) - np.array(res["bounding_box"][0])
)
nicely_formatted_com = [f"{com:.2f}" for com in res["centre_of_mass"]]

nicely_formatted_com = [
f"{np.round(com,2)}" for com in res["centre_of_mass"]
]
crystal_summary += (
f"Crystal {n+1}: "
f"Strength {res['total_count']}; "
Expand All @@ -117,11 +116,11 @@ def wait_for_results(self, fallback_xyz: Point3D) -> Point3D:
)
self.ispyb.append_to_comment(crystal_summary)

raw_centre = Point3D(*(raw_results[0]["centre_of_mass"]))
raw_centre = np.array([*(raw_results[0]["centre_of_mass"])])

# _wait_for_result returns the centre of the grid box, but we want the corner
results = Point3D(
raw_centre.x - 0.5, raw_centre.y - 0.5, raw_centre.z - 0.5
results = np.array(
[raw_centre[0] - 0.5, raw_centre[1] - 0.5, raw_centre[2] - 0.5]
)
xray_centre = self.grid_position_to_motor_position(results)

Expand Down
Loading