Rename "active_detector" for background detecor. Improve thing selector coercion

This commit is contained in:
Julian Stirling 2026-01-14 15:09:18 +00:00
parent e46836ffe1
commit ca46439269
10 changed files with 182 additions and 52 deletions

View file

@ -28,6 +28,7 @@ from openflexure_microscope_server.things.background_detect import (
BackgroundDetectAlgorithm,
)
from openflexure_microscope_server.ui import ActionButton, PropertyControl
from openflexure_microscope_server.utilities import coerce_thing_selector
class JPEGBlob(lt.blob.Blob):
@ -158,7 +159,7 @@ class BaseCamera(lt.Thing):
``__init__`` method of the subclass.
"""
background_detectors: Mapping[str, BackgroundDetectAlgorithm] = lt.thing_slot()
_all_background_detectors: Mapping[str, BackgroundDetectAlgorithm] = lt.thing_slot()
mjpeg_stream = lt.outputs.MJPEGStreamDescriptor()
lores_mjpeg_stream = lt.outputs.MJPEGStreamDescriptor()
@ -176,12 +177,17 @@ class BaseCamera(lt.Thing):
# Default is never updated but is used if the value set from settings is
# incorrect. In the future a better way to set defaults for thing slot mappings
# would be ideal.
self._default_detector = "bg_channel_deviations_luv"
self._detector_name = "bg_channel_deviations_luv"
self._default_background_detector = "bg_channel_deviations_luv"
self._background_detector_name: Optional[str] = None
def __enter__(self) -> Self:
"""Open hardware connection when the Thing context manager is opened."""
raise NotImplementedError("CameraThings must define their own __enter__ method")
self._background_detector_name = coerce_thing_selector(
thing_mapping=self._all_background_detectors,
selected=self._background_detector_name,
default=self._default_background_detector,
)
return self
def __exit__(
self,
@ -190,7 +196,7 @@ class BaseCamera(lt.Thing):
_traceback: Optional[TracebackType],
) -> None:
"""Close hardware connection when the Thing context manager is closed."""
raise NotImplementedError("CameraThings must define their own __exit__ method")
pass
@lt.property
def calibration_required(self) -> bool:
@ -581,30 +587,31 @@ class BaseCamera(lt.Thing):
# Note that the default detector name is set at init. This is over written if
# setting is loaded from disk.
@lt.setting
def detector_name(self) -> str:
def background_detector_name(self) -> Optional[str]:
"""The name of the active background selector."""
return self._detector_name
return self._background_detector_name
@detector_name.setter
def _set_detector_name(self, name: str) -> None:
"""Validate and set detector_name."""
if name not in self.background_detectors:
@background_detector_name.setter
def _set_background_detector_name(self, name: str) -> None:
"""Validate and set background_detector_name."""
if name not in self._all_background_detectors:
self.logger.warning(f"{name} is not a valid background detector name.")
self._detector_name = name
self._background_detector_name = name
@property
def active_detector(self) -> BackgroundDetectAlgorithm:
def background_detector(self) -> Optional[BackgroundDetectAlgorithm]:
"""The active background detector instance."""
if self.detector_name not in self.background_detectors:
self.logger.warning(f"No detector named {self.detector_name}")
self.detector_name = self._default_detector
return self.background_detectors[self.detector_name]
if self.background_detector_name is None:
return None
return self._all_background_detectors[self.background_detector_name]
@lt.action
def image_is_sample(self) -> tuple[bool, str]:
"""Label the current image as either background or sample."""
if self.background_detector is None:
raise RuntimeError("No background detectors available.")
current_image = self.grab_as_array(stream_name="lores")
return self.active_detector.image_is_sample(current_image)
return self.background_detector.image_is_sample(current_image)
@lt.action
def set_background(self) -> None:
@ -616,8 +623,10 @@ class BaseCamera(lt.Thing):
future images to the distribution, to determine if each pixel is
foreground or background.
"""
if self.background_detector is None:
raise RuntimeError("No background detectors available.")
background = self.grab_as_array(stream_name="lores")
self.active_detector.set_background(background)
self.background_detector.set_background(background)
@property
def thing_state(self) -> Mapping[str, Any]: