dls-dodal 1.47.0__py3-none-any.whl → 1.49.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (61) hide show
  1. {dls_dodal-1.47.0.dist-info → dls_dodal-1.49.0.dist-info}/METADATA +3 -2
  2. {dls_dodal-1.47.0.dist-info → dls_dodal-1.49.0.dist-info}/RECORD +59 -49
  3. {dls_dodal-1.47.0.dist-info → dls_dodal-1.49.0.dist-info}/WHEEL +1 -1
  4. dodal/_version.py +2 -2
  5. dodal/beamlines/aithre.py +21 -0
  6. dodal/beamlines/b01_1.py +1 -1
  7. dodal/beamlines/b16.py +65 -0
  8. dodal/beamlines/b18.py +38 -0
  9. dodal/beamlines/i03.py +21 -6
  10. dodal/beamlines/i04.py +17 -10
  11. dodal/beamlines/i10.py +41 -233
  12. dodal/beamlines/i18.py +1 -1
  13. dodal/beamlines/i19_1.py +9 -6
  14. dodal/beamlines/i24.py +5 -5
  15. dodal/beamlines/k11.py +35 -0
  16. dodal/common/beamlines/beamline_parameters.py +2 -28
  17. dodal/common/beamlines/device_helpers.py +1 -0
  18. dodal/devices/aithre_lasershaping/goniometer.py +36 -2
  19. dodal/devices/aithre_lasershaping/laser_robot.py +27 -0
  20. dodal/devices/apple2_undulator.py +257 -136
  21. dodal/devices/b16/__init__.py +0 -0
  22. dodal/devices/b16/detector.py +34 -0
  23. dodal/devices/bimorph_mirror.py +29 -36
  24. dodal/devices/electron_analyser/__init__.py +21 -1
  25. dodal/devices/electron_analyser/abstract/__init__.py +0 -6
  26. dodal/devices/electron_analyser/abstract/base_detector.py +16 -128
  27. dodal/devices/electron_analyser/abstract/base_driver_io.py +122 -8
  28. dodal/devices/electron_analyser/abstract/base_region.py +7 -3
  29. dodal/devices/electron_analyser/detector.py +141 -0
  30. dodal/devices/electron_analyser/enums.py +6 -0
  31. dodal/devices/electron_analyser/specs/__init__.py +3 -2
  32. dodal/devices/electron_analyser/specs/detector.py +6 -22
  33. dodal/devices/electron_analyser/specs/driver_io.py +27 -3
  34. dodal/devices/electron_analyser/specs/enums.py +8 -0
  35. dodal/devices/electron_analyser/specs/region.py +3 -2
  36. dodal/devices/electron_analyser/types.py +30 -4
  37. dodal/devices/electron_analyser/util.py +1 -1
  38. dodal/devices/electron_analyser/vgscienta/__init__.py +3 -2
  39. dodal/devices/electron_analyser/vgscienta/detector.py +9 -23
  40. dodal/devices/electron_analyser/vgscienta/driver_io.py +33 -4
  41. dodal/devices/electron_analyser/vgscienta/enums.py +19 -0
  42. dodal/devices/electron_analyser/vgscienta/region.py +7 -23
  43. dodal/devices/fast_grid_scan.py +1 -1
  44. dodal/devices/i04/murko_results.py +93 -96
  45. dodal/devices/i10/__init__.py +0 -0
  46. dodal/devices/i10/i10_apple2.py +181 -126
  47. dodal/devices/i18/diode.py +37 -4
  48. dodal/devices/i22/nxsas.py +1 -1
  49. dodal/devices/mx_phase1/beamstop.py +23 -6
  50. dodal/devices/oav/oav_detector.py +101 -25
  51. dodal/devices/oav/oav_parameters.py +46 -16
  52. dodal/devices/oav/oav_to_redis_forwarder.py +2 -2
  53. dodal/devices/robot.py +20 -1
  54. dodal/devices/smargon.py +43 -4
  55. dodal/devices/zebra/zebra.py +8 -0
  56. dodal/plans/configure_arm_trigger_and_disarm_detector.py +167 -0
  57. dodal/plan_stubs/electron_analyser/__init__.py +0 -3
  58. dodal/plan_stubs/electron_analyser/configure_driver.py +0 -92
  59. {dls_dodal-1.47.0.dist-info → dls_dodal-1.49.0.dist-info}/entry_points.txt +0 -0
  60. {dls_dodal-1.47.0.dist-info → dls_dodal-1.49.0.dist-info}/licenses/LICENSE +0 -0
  61. {dls_dodal-1.47.0.dist-info → dls_dodal-1.49.0.dist-info}/top_level.txt +0 -0
@@ -1,6 +1,5 @@
1
1
  import json
2
2
  import pickle
3
- from collections import OrderedDict
4
3
  from enum import Enum
5
4
  from typing import TypedDict
6
5
 
@@ -17,10 +16,11 @@ from redis.asyncio import StrictRedis
17
16
  from dodal.devices.i04.constants import RedisConstants
18
17
  from dodal.devices.oav.oav_calculations import (
19
18
  calculate_beam_distance,
20
- camera_coordinates_to_xyz_mm,
21
19
  )
22
20
  from dodal.log import LOGGER
23
21
 
22
+ NO_MURKO_RESULT = (-1, -1)
23
+
24
24
  MurkoResult = dict
25
25
  FullMurkoResults = dict[str, list[MurkoResult]]
26
26
 
@@ -43,12 +43,19 @@ class Coord(Enum):
43
43
 
44
44
 
45
45
  class MurkoResultsDevice(StandardReadable, Triggerable, Stageable):
46
- """Device that takes crystal centre coords from Murko and uses them to set the
46
+ """Device that takes crystal centre values from Murko and uses them to set the
47
47
  x, y, z coordinate of the sample to be in line with the beam centre.
48
- (x, z) coords can be read at 90°, and (x, y) at 180° (or the closest omega angle to
49
- 90° and 180°). The average of the x values at these angles is taken, and sin(omega)z
50
- and cosine(omega)y are taken to account for the rotation. This value is used to
51
- calculate a number of mm the sample needs to move to be in line with the beam centre.
48
+ The most_likely_click[1] value from Murko corresponds with the x coordinate of the
49
+ sample. The most_likely_click[0] value from Murko corresponds with a component of
50
+ the y and z coordinates of the sample, depending on the omega angle, as the sample
51
+ is rotated around the x axis.
52
+
53
+ Given a most_likely_click value at a certain omega angle θ:
54
+ most_likely_click[1] = x
55
+ most_likely_click[0] = cos(θ)y - sin(θ)z
56
+
57
+ A value for x can be found by averaging all most_likely_click[1] values, and
58
+ solutions for y and z can be calculated using numpy's linear algebra library.
52
59
  """
53
60
 
54
61
  TIMEOUT_S = 2
@@ -59,6 +66,7 @@ class MurkoResultsDevice(StandardReadable, Triggerable, Stageable):
59
66
  redis_password=RedisConstants.REDIS_PASSWORD,
60
67
  redis_db=RedisConstants.MURKO_REDIS_DB,
61
68
  name="",
69
+ stop_angle=350,
62
70
  ):
63
71
  self.redis_client = StrictRedis(
64
72
  host=redis_host,
@@ -67,17 +75,11 @@ class MurkoResultsDevice(StandardReadable, Triggerable, Stageable):
67
75
  )
68
76
  self.pubsub = self.redis_client.pubsub()
69
77
  self._last_omega = 0
70
- self._last_result = None
71
78
  self.sample_id = soft_signal_rw(str) # Should get from redis
72
- self.coords = {"x": {}, "y": {}, "z": {}}
73
- self.search_angles = OrderedDict(
74
- [ # Angles to search and dimensions to gather at each angle
75
- (90, ("x", "z")),
76
- (180, ("x", "y")),
77
- (270, ()), # Stop searching here
78
- ]
79
- )
80
- self.angles_to_search = list(self.search_angles.keys())
79
+ self.stop_angle = stop_angle
80
+ self.x_dists_mm = []
81
+ self.y_dists_mm = []
82
+ self.omegas = []
81
83
 
82
84
  with self.add_children_as_readables():
83
85
  # Diffs from current x/y/z
@@ -101,95 +103,90 @@ class MurkoResultsDevice(StandardReadable, Triggerable, Stageable):
101
103
  async def trigger(self):
102
104
  # Wait for results
103
105
  sample_id = await self.sample_id.get_value()
104
- final_message = None
105
- while self.angles_to_search:
106
- # waits here for next batch to be recieved
106
+ while self._last_omega < self.stop_angle:
107
+ # waits here for next batch to be received
107
108
  message = await self.pubsub.get_message(timeout=self.TIMEOUT_S)
108
109
  if message is None: # No more messages to process
109
- await self.process_batch(
110
- final_message, sample_id
111
- ) # Process final message again
112
110
  break
113
111
  await self.process_batch(message, sample_id)
114
- final_message = message
115
- x_values = list(self.coords["x"].values())
116
- y_values = list(self.coords["y"].values())
117
- z_values = list(self.coords["z"].values())
118
- assert x_values, "No x values"
119
- assert z_values, "No z values"
120
- assert y_values, "No y values"
121
- self._x_mm_setter(float(np.mean(x_values)))
122
- self._y_mm_setter(float(np.mean(y_values)))
123
- self._z_mm_setter(float(np.mean(z_values)))
112
+
113
+ for i in range(len(self.omegas)):
114
+ LOGGER.debug(
115
+ f"omega: {round(self.omegas[i], 2)}, x: {round(self.x_dists_mm[i], 2)}, y: {round(self.y_dists_mm[i], 2)}"
116
+ )
117
+
118
+ LOGGER.info(f"Using average of x beam distances: {self.x_dists_mm}")
119
+ avg_x = float(np.mean(self.x_dists_mm))
120
+ LOGGER.info(f"Finding least square y and z from y distances: {self.y_dists_mm}")
121
+ best_y, best_z = get_yz_least_squares(self.y_dists_mm, self.omegas)
122
+ # x, y, z are relative to beam centre. Need to move negative these values to get centred.
123
+ self._x_mm_setter(-avg_x)
124
+ self._y_mm_setter(-best_y)
125
+ self._z_mm_setter(-best_z)
124
126
 
125
127
  async def process_batch(self, message: dict | None, sample_id: str):
126
128
  if message and message["type"] == "message":
127
- batch_results = pickle.loads(message["data"])
129
+ batch_results: list[dict] = pickle.loads(message["data"])
128
130
  for results in batch_results:
129
- LOGGER.info(f"Got {results} from redis")
130
131
  for uuid, result in results.items():
131
- metadata_str = await self.redis_client.hget( # type: ignore
132
+ if metadata_str := await self.redis_client.hget( # type: ignore
132
133
  f"murko:{sample_id}:metadata", uuid
133
- )
134
- if metadata_str and self.angles_to_search:
135
- self.process_result(result, uuid, metadata_str)
136
-
137
- def process_result(
138
- self, result: dict, uuid: int, metadata_str: str
139
- ) -> float | None:
140
- metadata = MurkoMetadata(json.loads(metadata_str))
141
- omega_angle = metadata["omega_angle"]
142
- LOGGER.info(f"Got angle {omega_angle}")
143
- # Find closest to next search angle
144
- movement = self.get_coords_if_at_angle(metadata, result, omega_angle)
145
- if movement is not None:
146
- LOGGER.info(f"Using result {uuid}, {metadata_str}, {result}")
147
- search_angle = self.angles_to_search.pop(0)
148
- for coord in self.search_angles[search_angle]:
149
- self.coords[coord][omega_angle] = movement[Coord[coord].value]
150
- LOGGER.info(f"Found {coord} at {movement}, angle = {omega_angle}")
151
- self._last_omega = omega_angle
152
- self._last_result = result
153
-
154
- def get_coords_if_at_angle(
155
- self, metadata: MurkoMetadata, result: MurkoResult, omega: float
156
- ) -> np.ndarray | None:
157
- """Gets the 'most_likely_click' coordinates from Murko if omega or the last
158
- omega are the closest angle to the search angle. Otherwise returns None.
134
+ ):
135
+ LOGGER.info(
136
+ f"Found metadata for uuid {uuid}, processing result"
137
+ )
138
+ self.process_result(
139
+ result, MurkoMetadata(json.loads(metadata_str))
140
+ )
141
+ else:
142
+ LOGGER.info(f"Found no metadata for uuid {uuid}")
143
+
144
+ def process_result(self, result: dict, metadata: MurkoMetadata):
145
+ """Uses the 'most_likely_click' coordinates from Murko to calculate the
146
+ horizontal and vertical distances from the beam centre, and store these values
147
+ as well as the omega angle the image was taken at.
159
148
  """
160
- search_angle = self.angles_to_search[0]
161
- LOGGER.info(f"Compare {omega}, {search_angle}, {self._last_omega}")
162
- if ( # if last omega is closest
163
- abs(omega - search_angle) >= abs(self._last_omega - search_angle)
164
- and self._last_result is not None
165
- ):
166
- closest_result = self._last_result
167
- closest_omega = self._last_omega
168
- elif omega - search_angle >= 0: # if this omega is closest
169
- closest_result = result
170
- closest_omega = omega
149
+ omega = metadata["omega_angle"]
150
+ coords = result["most_likely_click"] # As proportion from top, left of image
151
+ LOGGER.info(f"Got most_likely_click: {coords} at angle {omega}")
152
+ if (
153
+ tuple(coords) == NO_MURKO_RESULT
154
+ ): # See https://github.com/MartinSavko/murko/issues/9
155
+ LOGGER.info("Murko didn't produce a result, moving on")
171
156
  else:
172
- return None
173
- coords = closest_result[
174
- "most_likely_click"
175
- ] # As proportion from top, left of image
176
- shape = closest_result["original_shape"] # Dimensions of image in pixels
177
- # Murko returns coords as y, x
178
- centre_px = (coords[1] * shape[1], coords[0] * shape[0])
179
- LOGGER.info(
180
- f"Using image taken at {closest_omega}, which found xtal at {centre_px}"
181
- )
182
-
183
- beam_dist_px = calculate_beam_distance(
184
- (metadata["beam_centre_i"], metadata["beam_centre_j"]),
185
- centre_px[0],
186
- centre_px[1],
187
- )
157
+ shape = result["original_shape"] # Dimensions of image in pixels
158
+ # Murko returns coords as y, x
159
+ centre_px = (coords[1] * shape[1], coords[0] * shape[0])
160
+
161
+ beam_dist_px = calculate_beam_distance(
162
+ (metadata["beam_centre_i"], metadata["beam_centre_j"]),
163
+ centre_px[0],
164
+ centre_px[1],
165
+ )
166
+ self.x_dists_mm.append(
167
+ beam_dist_px[0] * metadata["microns_per_x_pixel"] / 1000
168
+ )
169
+ self.y_dists_mm.append(
170
+ beam_dist_px[1] * metadata["microns_per_y_pixel"] / 1000
171
+ )
172
+ self.omegas.append(omega)
173
+ self._last_omega = omega
174
+
175
+
176
+ def get_yz_least_squares(vertical_dists: list, omegas: list) -> tuple[float, float]:
177
+ """Get the least squares solution for y and z from the vertical distances and omega angles.
178
+
179
+ Args:
180
+ v_dists (list): List of vertical distances from beam centre. Any units
181
+ omegas (list): List of omega angles in degrees.
182
+
183
+ Returns:
184
+ tuple[float, float]: y, z distances from centre, in whichever units
185
+ v_dists came as.
186
+ """
187
+ thetas = np.radians(omegas)
188
+ matrix = np.column_stack([np.cos(thetas), -np.sin(thetas)])
188
189
 
189
- return camera_coordinates_to_xyz_mm(
190
- beam_dist_px[0],
191
- beam_dist_px[1],
192
- closest_omega,
193
- metadata["microns_per_x_pixel"],
194
- metadata["microns_per_y_pixel"],
195
- )
190
+ yz, residuals, rank, s = np.linalg.lstsq(matrix, vertical_dists, rcond=None)
191
+ y, z = yz
192
+ return y, z
File without changes