Manually test for increasing or decreasing stacks

This commit is contained in:
jaknapper 2026-04-02 17:22:59 +01:00
parent 3b53473475
commit bc6ea9a1ca

View file

@ -676,10 +676,11 @@ class AutofocusThing(lt.Thing):
sharpness=self._cam.grab_jpeg_size(stream_name="lores"), sharpness=self._cam.grab_jpeg_size(stream_name="lores"),
) )
# Silence too many returns in this situation as refactoring to reduce returns is # Silence too many returns and complexity in this situation as refactoring to
# unlikely to improve readability. This function is basically a complex switch # reduce returns is unlikely to improve readability.
# statement, having an explicit return after each option is clear. # This function is basically a complex switch statement, having an explicit
def check_stack_result( # noqa: PLR0911 # return after each option is clear.
def check_stack_result( # noqa: PLR0911 C901
self, captures: list[CaptureInfo], check_turning_points: bool self, captures: list[CaptureInfo], check_turning_points: bool
) -> tuple[Literal["success", "continue", "restart"], int]: ) -> tuple[Literal["success", "continue", "restart"], int]:
"""Check if the sharpest image in a list of captures is central enough. """Check if the sharpest image in a list of captures is central enough.
@ -688,7 +689,7 @@ class AutofocusThing(lt.Thing):
sharpness has converged in the centre sharpness has converged in the centre
:param check_turning_points: Whether to check the number of turning points in :param check_turning_points: Whether to check the number of turning points in
the sharpnesses of the images in the stack is exactly 1. (May fail with the sharpnesses of the images in the stack is exactly 1. (May fail with
thick samples) thick samples and stacks with many images)
:returns: A tuple with two values: :returns: A tuple with two values:
@ -719,6 +720,13 @@ class AutofocusThing(lt.Thing):
return "restart", capture_id return "restart", capture_id
return "continue", capture_id return "continue", capture_id
# Manually test for monotomically increasing or decreasing sharpnesses, as
# fitting can struggle with these and their behaviour is simpler to hardcode
if np.array_equal(sharpnesses, np.sort(sharpnesses)):
return "continue", capture_id
if np.array_equal(sharpnesses, np.sort(sharpnesses)[::-1]):
return "restart", capture_id
try: try:
turning = _get_peak_turning_point(sharpnesses) turning = _get_peak_turning_point(sharpnesses)
except NotAPeakError: except NotAPeakError: