Validate and test stack inputs
Update picamera hardware test
This commit is contained in:
parent
78b407fb43
commit
b211cfd4c5
3 changed files with 223 additions and 48 deletions
|
|
@ -21,7 +21,7 @@ from pydantic import BaseModel, computed_field, field_validator, model_validator
|
|||
import labthings_fastapi as lt
|
||||
from labthings_fastapi.types.numpy import NDArray
|
||||
|
||||
from .camera import BaseCamera, CaptureParams
|
||||
from .camera import BaseCamera, CaptureParams, validate_capture_params
|
||||
from .stage import BaseStage
|
||||
|
||||
LOGGER = logging.getLogger(__name__)
|
||||
|
|
@ -756,13 +756,19 @@ class AutofocusThing(lt.Thing):
|
|||
saving all images captured. No sharpness testing, restart
|
||||
logic, or autofocus is performed.
|
||||
|
||||
:param stack_parameters: SmartStackParams defining stack spacing,
|
||||
image count, directory, and save resolution.
|
||||
:param stack_parameters: StackParams defining stack spacing,
|
||||
image count and backlash correction.
|
||||
:param capture_parameters: CaptureParams defining save
|
||||
resolution, images directory.
|
||||
|
||||
:returns:
|
||||
- Final z position
|
||||
- List of z positions captured
|
||||
"""
|
||||
# Validate parameters and raise Exception if unsuitable
|
||||
validate_stack_params(stack_parameters=stack_parameters)
|
||||
validate_capture_params(capture_parameters=capture_parameters)
|
||||
|
||||
captures: list[CaptureInfo] = []
|
||||
z_positions: list[int] = []
|
||||
|
||||
|
|
@ -863,3 +869,24 @@ def _count_turning_points(sharpnesses: np.ndarray) -> int:
|
|||
d_sharpnesses = d_sharpnesses[prominent]
|
||||
# count the sign changes
|
||||
return int(np.sum(d_sharpnesses[1:] * d_sharpnesses[:-1] < 0))
|
||||
|
||||
|
||||
def validate_stack_params(stack_parameters: StackParams) -> None:
|
||||
"""Validate stack parameters for a z-stack acquisition.
|
||||
|
||||
Ensures that the parameters allow a physical stack, without negative or zero
|
||||
values where they would cause crashes.
|
||||
|
||||
:param stack_parameters: StackParams object containing stacking settings.
|
||||
|
||||
:raises ValueError: If any stack_parameters are found to be unusable.
|
||||
"""
|
||||
if stack_parameters.images_to_save <= 0:
|
||||
raise ValueError(
|
||||
f"Invalid number of images to save: {stack_parameters.images_to_save}. Must be > 0."
|
||||
)
|
||||
|
||||
if stack_parameters.settling_time < 0:
|
||||
raise ValueError(
|
||||
f"Invalid settling time: {stack_parameters.settling_time}. Must be positive or 0."
|
||||
)
|
||||
|
|
|
|||
|
|
@ -55,6 +55,37 @@ class CaptureParams(BaseModel):
|
|||
save_resolution: tuple[int, int]
|
||||
|
||||
|
||||
def validate_capture_params(capture_parameters: CaptureParams) -> None:
|
||||
"""Validate capture parameters for a capture.
|
||||
|
||||
Ensures that the save resolution is a tuple of two positive integers
|
||||
and that the images directory is a non-empty string.
|
||||
|
||||
:param capture_parameters: CaptureParams object containing acquisition settings.
|
||||
|
||||
:raises ValueError: If `save_resolution` is not a tuple of two positive integers,
|
||||
or if `images_dir` is not a non-empty string.
|
||||
"""
|
||||
if (
|
||||
not isinstance(capture_parameters.save_resolution, tuple)
|
||||
or len(capture_parameters.save_resolution) != 2
|
||||
or not all(
|
||||
isinstance(x, int) and x > 0 for x in capture_parameters.save_resolution
|
||||
)
|
||||
):
|
||||
raise ValueError(
|
||||
f"Invalid save_resolution: {capture_parameters.save_resolution}. Must be a tuple of two positive integers."
|
||||
)
|
||||
|
||||
if (
|
||||
not isinstance(capture_parameters.images_dir, str)
|
||||
or not capture_parameters.images_dir
|
||||
):
|
||||
raise ValueError(
|
||||
f"Invalid images_dir: {capture_parameters.images_dir}. Must be a non-empty string."
|
||||
)
|
||||
|
||||
|
||||
class NoImageInMemoryError(RuntimeError):
|
||||
"""An error called if no image is in memory when accessed."""
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue