From a0b98a1adce89c01f6281059823da2e85c291916 Mon Sep 17 00:00:00 2001 From: jaknapper Date: Mon, 29 Jan 2024 19:56:24 +0000 Subject: [PATCH] Fixed autofocus to not combine logs --- .../things/auto_recentre_stage.py | 26 +------------------ .../things/autofocus.py | 6 ++--- .../things/smart_scan.py | 2 ++ 3 files changed, 6 insertions(+), 28 deletions(-) diff --git a/src/openflexure_microscope_server/things/auto_recentre_stage.py b/src/openflexure_microscope_server/things/auto_recentre_stage.py index ba11a900..2eb77055 100644 --- a/src/openflexure_microscope_server/things/auto_recentre_stage.py +++ b/src/openflexure_microscope_server/things/auto_recentre_stage.py @@ -15,29 +15,6 @@ CamDep = direct_thing_client_dependency(StreamingPiCamera2, "/camera/") CSMDep = direct_thing_client_dependency(CameraStageMapper, "/camera_stage_mapping/") AutofocusDep = direct_thing_client_dependency(AutofocusThing, "/autofocus/") - -def turningpoints(lst): - dx = np.diff(lst) - return dx[1:] * dx[:-1] < 0 - - -def unpack_autofocus(scan_data): - """Extract z, sharpness data from a move_and_measure call""" - scan_data = dict(scan_data) - jpeg_times = scan_data["jpeg_times"] - jpeg_sizes = scan_data["jpeg_sizes"] - jpeg_sizes_MB = [x / 10**3 for x in jpeg_sizes] - stage_times = scan_data["stage_times"] - stage_positions = scan_data["stage_positions"] - stage_height = [pos["z"] for pos in stage_positions] - - jpeg_heights = np.interp(jpeg_times, stage_times, stage_height) - - turning = np.where(turningpoints(jpeg_heights))[0] - - return jpeg_heights[turning[0] : turning[1]], jpeg_sizes_MB[turning[0] : turning[1]] - - class RecentringThing(Thing): @thing_action def looping_autofocus(self, autofocus: AutofocusDep, stage: StageDep, dz=2000, start='centre'): @@ -51,8 +28,7 @@ class RecentringThing(Thing): repeat = True attempts = 0 while repeat and attempts < 10: - data = autofocus.fast_autofocus(dz=dz, start=start) - heights, _ = unpack_autofocus(data) + heights, sizes = autofocus.fast_autofocus(dz=dz, start=start) height_min = np.min(heights) height_max = np.max(heights) # TODO: max heights seems badly wrong! Something about turning? diff --git a/src/openflexure_microscope_server/things/autofocus.py b/src/openflexure_microscope_server/things/autofocus.py index b70f8a44..01f41e58 100644 --- a/src/openflexure_microscope_server/things/autofocus.py +++ b/src/openflexure_microscope_server/things/autofocus.py @@ -119,7 +119,7 @@ class JPEGSharpnessMonitor: raise ValueError( "No images were captured during the move of the stage. Perhaps the camera is not streaming images?" ) - return jz[np.argmax(js)] + return jz[np.argmax(js)], jz, js def data_dict(self) -> SharpnessDataArrays: """Return the gathered data as a single convenient dictionary""" @@ -161,13 +161,13 @@ class AutofocusThing(Thing): # z: Final z position after move i, z = m.focus_rel(dz, block_cancellation=True) # Get the z position with highest sharpness from the previous move (index i) - fz: int = m.sharpest_z_on_move(i) + fz, heights, sizes = m.sharpest_z_on_move(i) # Move all the way to the start so it's consistent i, z = m.focus_rel(-dz) # Move to the target position fz (relative move of (fz - z)) m.focus_rel(fz - z) # Return all focus data - return m.data_dict() + return heights, sizes @thing_action def move_and_measure( diff --git a/src/openflexure_microscope_server/things/smart_scan.py b/src/openflexure_microscope_server/things/smart_scan.py index 0230d16b..6f9791d7 100644 --- a/src/openflexure_microscope_server/things/smart_scan.py +++ b/src/openflexure_microscope_server/things/smart_scan.py @@ -484,6 +484,8 @@ class SmartScanThing(Thing): if len(focused_path) > 1: z_index = closest(loc, focused_path) z=int(focused_path[z_index][2]) + else: + z = loc[2] # print('Moving to {0}'.format([coords[0], coords[1], focused_path[z_index][2]])) # print(focused_path) stage.move_absolute(