Give sharpness monitor a descriptive name, not m

This commit is contained in:
jaknapper 2025-04-25 12:29:11 +01:00
parent bf6437b42c
commit f50580cdd0

View file

@ -147,14 +147,14 @@ class SharpnessDataArrays(BaseModel):
class AutofocusThing(Thing): class AutofocusThing(Thing):
"""The Thing concerned with combinations of z axis movements and the camera. """The Thing concerned with combinations of z axis movements and the camera.
Actions here involve moving a stage in z, and using the camera to either Actions here involve moving a stage in z, and using the camera to either
capture images (generally, z-stacking) and measuring the sharpness of the capture images (generally, z-stacking) and measuring the sharpness of the
field of view to assess focus (autofocus and testing the success of a z-stack)""" field of view to assess focus (autofocus and testing the success of a z-stack)"""
@thing_action @thing_action
def fast_autofocus( def fast_autofocus(
self, self,
m: SharpnessMonitorDep, sharpness_monitor: SharpnessMonitorDep,
dz: int = 2000, dz: int = 2000,
start: str = "centre", start: str = "centre",
) -> SharpnessDataArrays: ) -> SharpnessDataArrays:
@ -164,27 +164,27 @@ class AutofocusThing(Thing):
the position where the image was sharpest. We'll then move back down, and the position where the image was sharpest. We'll then move back down, and
finally up to the sharpest point. finally up to the sharpest point.
""" """
with m.run(): with sharpness_monitor.run():
# Move to (-dz / 2) # Move to (-dz / 2)
if start == "centre": if start == "centre":
m.focus_rel(-dz / 2) sharpness_monitor.focus_rel(-dz / 2)
# Move to dz while monitoring sharpness # Move to dz while monitoring sharpness
# i: Sharpness monitor index for this move # i: Sharpness monitor index for this move
# z: Final z position after move # z: Final z position after move
i, z = m.focus_rel(dz, block_cancellation=True) i, z = sharpness_monitor.focus_rel(dz, block_cancellation=True)
# Get the z position with highest sharpness from the previous move (index i) # Get the z position with highest sharpness from the previous move (index i)
fz: int = m.sharpest_z_on_move(i) fz: int = sharpness_monitor.sharpest_z_on_move(i)
# Move all the way to the start so it's consistent # Move all the way to the start so it's consistent
i, z = m.focus_rel(-dz) i, z = sharpness_monitor.focus_rel(-dz)
# Move to the target position fz (relative move of (fz - z)) # Move to the target position fz (relative move of (fz - z))
m.focus_rel(fz - z) sharpness_monitor.focus_rel(fz - z)
# Return all focus data # Return all focus data
return m.data_dict() return sharpness_monitor.data_dict()
@thing_action @thing_action
def z_move_and_measure_sharpness( def z_move_and_measure_sharpness(
self, self,
m: SharpnessMonitorDep, sharpness_monitor: SharpnessMonitorDep,
dz: Sequence[int], dz: Sequence[int],
wait: float = 0, wait: float = 0,
) -> SharpnessDataArrays: ) -> SharpnessDataArrays:
@ -200,16 +200,16 @@ class AutofocusThing(Thing):
If `wait` is specified, we will wait for that many seconds If `wait` is specified, we will wait for that many seconds
between moves. between moves.
""" """
with m.run(): with sharpness_monitor.run():
for i, current_dz in enumerate(dz): for i, current_dz in enumerate(dz):
if i > 0 and wait > 0: if i > 0 and wait > 0:
time.sleep(wait) time.sleep(wait)
m.focus_rel(current_dz) sharpness_monitor.focus_rel(current_dz)
return m.data_dict() return sharpness_monitor.data_dict()
@thing_action @thing_action
def looping_autofocus( def looping_autofocus(
self, stage: Stage, m: SharpnessMonitorDep, dz=2000, start="centre" self, stage: Stage, sharpness_monitor: SharpnessMonitorDep, dz=2000, start="centre"
): ):
"""Repeatedly autofocus the stage until it looks focused. """Repeatedly autofocus the stage until it looks focused.
@ -222,14 +222,14 @@ class AutofocusThing(Thing):
attempts = 0 attempts = 0
backlash = 200 backlash = 200
with m.run(): with sharpness_monitor.run():
while repeat and attempts < 10: while repeat and attempts < 10:
if start == "centre": if start == "centre":
stage.move_relative(x=0, y=0, z=-(backlash + dz / 2)) stage.move_relative(x=0, y=0, z=-(backlash + dz / 2))
stage.move_relative(x=0, y=0, z=backlash) stage.move_relative(x=0, y=0, z=backlash)
i, z = m.focus_rel(dz, block_cancellation=True) i, z = sharpness_monitor.focus_rel(dz, block_cancellation=True)
_, heights, sizes = m.move_data(i) _, heights, sizes = sharpness_monitor.move_data(i)
peak_height = heights[np.argmax(sizes)] peak_height = heights[np.argmax(sizes)]
height_min = np.min(heights) height_min = np.min(heights)
@ -301,7 +301,7 @@ class AutofocusThing(Thing):
"""Run a z stack, saving all images to stack_dir and copying the """Run a z stack, saving all images to stack_dir and copying the
central image to stack_dir""" central image to stack_dir"""
stack_dz = self.stack_dz stack_dz = self.stack_dz
images_to_capture = self.images_to_capture images_to_capture = self.stack_images_to_capture
stack_z_range = stack_dz * (images_to_capture - 1) stack_z_range = stack_dz * (images_to_capture - 1)
stage.move_relative(z=-stack_z_range / 2) stage.move_relative(z=-stack_z_range / 2)