Do less coersion for stacks now labthings validates settings.

This commit is contained in:
Julian Stirling 2026-03-02 18:19:11 +00:00
parent e443b1ac5b
commit d7b4457a55
2 changed files with 10 additions and 32 deletions

View file

@ -350,7 +350,9 @@ class HistoScanWorkflow(RectGridWorkflow[HistoScanSettingsModel]):
Defaults to 1 unless you need to see either side of focus Defaults to 1 unless you need to see either side of focus
""" """
stack_min_images_to_test: int = lt.setting(default=9, ge=5, le=13) stack_min_images_to_test: int = lt.setting(
default=9, ge=MIN_TEST_IMAGE_COUNT, le=MAX_TEST_IMAGE_COUNT
)
"""The minimum number of images to capture in a stack. """The minimum number of images to capture in a stack.
This many images are captures and tested for focus, if the focus is not central This many images are captures and tested for focus, if the focus is not central
@ -425,21 +427,7 @@ class HistoScanWorkflow(RectGridWorkflow[HistoScanSettingsModel]):
""" """
# Coerce min_images_to_test parameter # Coerce min_images_to_test parameter
min_images_to_test = self.stack_min_images_to_test min_images_to_test = self.stack_min_images_to_test
if min_images_to_test < MIN_TEST_IMAGE_COUNT: if min_images_to_test % 2 == 0:
self.logger.warning(
f"Cannot test only {min_images_to_test} image(s) as this will fail. "
"Setting min images to test to lowest possible value of"
f"{MIN_TEST_IMAGE_COUNT}."
)
min_images_to_test = MIN_TEST_IMAGE_COUNT
elif min_images_to_test > MAX_TEST_IMAGE_COUNT:
self.logger.warning(
f"Testing {min_images_to_test} images will cause defocus. "
"Setting min images to test to highest possible value of "
f"{MAX_TEST_IMAGE_COUNT}."
)
min_images_to_test = MAX_TEST_IMAGE_COUNT
elif min_images_to_test % 2 == 0:
min_images_to_test += 1 min_images_to_test += 1
self.logger.warning( self.logger.warning(
"Minimum number of images to test should be odd, setting to " "Minimum number of images to test should be odd, setting to "
@ -448,15 +436,10 @@ class HistoScanWorkflow(RectGridWorkflow[HistoScanSettingsModel]):
# Set the Thing property to the coerced value # Set the Thing property to the coerced value
self.stack_min_images_to_test = min_images_to_test self.stack_min_images_to_test = min_images_to_test
# Coerce the images to save parameter to be positive, odd, and less than # Coerce the images to save parameter to be odd, and less than
# min_images_to_save # min_images_to_save
images_to_save = self.stack_images_to_save images_to_save = self.stack_images_to_save
if images_to_save <= 0: if images_to_save > min_images_to_test:
self.logger.warning(
"At least 1 images must be saved. Setting images to save to 1."
)
images_to_save = 1
elif images_to_save > min_images_to_test:
self.logger.warning( self.logger.warning(
f"Cannot save {images_to_save} images as this above the minimum " f"Cannot save {images_to_save} images as this above the minimum "
f"number to test. Setting images to save to {MAX_TEST_IMAGE_COUNT}." f"number to test. Setting images to save to {MAX_TEST_IMAGE_COUNT}."

View file

@ -14,8 +14,6 @@ from labthings_fastapi.testing import create_thing_without_server
from openflexure_microscope_server.scan_directories import IMAGE_REGEX from openflexure_microscope_server.scan_directories import IMAGE_REGEX
from openflexure_microscope_server.things.autofocus import ( from openflexure_microscope_server.things.autofocus import (
EXTRA_STACK_CAPTURES, EXTRA_STACK_CAPTURES,
MAX_TEST_IMAGE_COUNT,
MIN_TEST_IMAGE_COUNT,
AutofocusThing, AutofocusThing,
CaptureInfo, CaptureInfo,
NotAPeakError, NotAPeakError,
@ -286,10 +284,7 @@ def test_create_stack(histo_scan_workflow, caplog):
@pytest.mark.parametrize( @pytest.mark.parametrize(
("initial_test_ims", "coerced_test_ims", "expected_log_start"), ("initial_test_ims", "coerced_test_ims", "expected_log_start"),
[ [
(0, MIN_TEST_IMAGE_COUNT, "Cannot test only 0"), (6, 7, "Minimum number of images to test should be odd"),
(-1, MIN_TEST_IMAGE_COUNT, "Cannot test only -1"),
(10, MAX_TEST_IMAGE_COUNT, "Testing 10 images will"),
(4, 5, "Minimum number of images to test should be odd"),
], ],
) )
def test_coercing_stack_test_ims( def test_coercing_stack_test_ims(
@ -314,9 +309,7 @@ def test_coercing_stack_test_ims(
@pytest.mark.parametrize( @pytest.mark.parametrize(
("initial_save_ims", "coerced_save_ims", "expected_log_start"), ("initial_save_ims", "coerced_save_ims", "expected_log_start"),
[ [
(0, 1, "At least 1 images must be saved"), (9, 7, "Cannot save 9 images"),
(-1, 1, "At least 1 images must be saved"),
(10, 9, "Cannot save 10 images"),
(4, 5, "Images to save should be odd, setting to 5"), (4, 5, "Images to save should be odd, setting to 5"),
], ],
) )
@ -324,6 +317,8 @@ def test_coercing_stack_save_ims(
initial_save_ims, coerced_save_ims, expected_log_start, histo_scan_workflow, caplog initial_save_ims, coerced_save_ims, expected_log_start, histo_scan_workflow, caplog
): ):
"""Run create stack with images to save set to values requiring coercion, and check result.""" """Run create stack with images to save set to values requiring coercion, and check result."""
# First set the min images to test to 7
histo_scan_workflow.stack_min_images_to_test = 7
histo_scan_workflow.stack_images_to_save = initial_save_ims histo_scan_workflow.stack_images_to_save = initial_save_ims
with caplog.at_level(logging.WARNING): with caplog.at_level(logging.WARNING):