From 3e8874188e577a26b30e558b92d55b056a339877 Mon Sep 17 00:00:00 2001 From: Julian Stirling Date: Tue, 13 Jan 2026 19:05:24 +0000 Subject: [PATCH] Get UI working for background detector things --- .../things/background_detect.py | 42 +++++++++++++- .../things/camera/__init__.py | 7 +++ .../things/camera/simulation.py | 2 +- .../things/smart_scan.py | 2 +- .../paneBackgroundDetect.vue | 55 +++++++------------ 5 files changed, 70 insertions(+), 38 deletions(-) diff --git a/src/openflexure_microscope_server/things/background_detect.py b/src/openflexure_microscope_server/things/background_detect.py index b96e13fd..ad113db4 100644 --- a/src/openflexure_microscope_server/things/background_detect.py +++ b/src/openflexure_microscope_server/things/background_detect.py @@ -13,6 +13,8 @@ from pydantic import BaseModel import labthings_fastapi as lt +from openflexure_microscope_server.ui import PropertyControl, property_control_for + SettingsType = TypeVar("SettingsType", bound=BaseModel) BackgroundType = TypeVar("BackgroundType", bound=BaseModel) @@ -32,6 +34,8 @@ class ChannelBlankError(RuntimeError): class BackgroundDetectAlgorithm(lt.Thing): """The base class for defining background detect algorithms.""" + display_name: str = lt.property(default="Base Detector", readonly=True) + @lt.property def ready(self) -> bool: """Whether the background detector is ready.""" @@ -59,6 +63,13 @@ class BackgroundDetectAlgorithm(lt.Thing): "Each background detect algorithm must implement an set_background method." ) + @lt.property + def settings_ui(self) -> list[PropertyControl]: + """A list of PropertyControl objects to create the settings in the UI.""" + raise NotImplementedError( + "Each background detect algorithm must implement an settings_ui method." + ) + class ColourChannelDetectLUV(BackgroundDetectAlgorithm): """Compare images with a known background in LUV colourspace. @@ -68,6 +79,8 @@ class ColourChannelDetectLUV(BackgroundDetectAlgorithm): intuitive way. """ + display_name: str = lt.property(default="Colour Channel (LUV)", readonly=True) + background_means: Optional[list[float]] = lt.setting(default=None, readonly=True) """The mean of each channel in the colourspace.""" background_stds: Optional[list[float]] = lt.setting(default=None, readonly=True) @@ -91,10 +104,20 @@ class ColourChannelDetectLUV(BackgroundDetectAlgorithm): image to be labeled as containing sample. """ + @lt.property + def settings_ui(self) -> list[PropertyControl]: + """A list of PropertyControl objects to create the settings in the UI.""" + return [ + property_control_for(self, "channel_tolerance", label="Channel Tolerance"), + property_control_for( + self, "min_sample_coverage", label="Sample Coverage Required (%)" + ), + ] + @lt.property def ready(self) -> bool: """Whether the background detector is ready.""" - return not (self.background_means is None or self.background_stds is None) + return self.background_means is not None and self.background_stds is not None def background_mask(self, image: np.ndarray) -> np.ndarray: """Calculate a binary image, showing whether each pixel is background. @@ -180,6 +203,8 @@ class ChannelDeviationLUV(BackgroundDetectAlgorithm): to the median standard deviation for a grid of background images. """ + display_name: str = lt.property(default="Channel Deviation (LUV)", readonly=True) + background_stds: Optional[list[float]] = lt.setting(default=None, readonly=True) """The standard deviation of each channel in the colourspace.""" @@ -203,6 +228,21 @@ class ChannelDeviationLUV(BackgroundDetectAlgorithm): image to be labeled as containing sample. """ + @lt.property + def settings_ui(self) -> list[PropertyControl]: + """A list of PropertyControl objects to create the settings in the UI.""" + return [ + property_control_for(self, "channel_tolerance", label="Channel Tolerance"), + property_control_for( + self, "min_sample_coverage", label="Sample Coverage Required (%)" + ), + ] + + @lt.property + def ready(self) -> bool: + """Whether the background detector is ready.""" + return self.background_stds is not None + def get_sample_coverage(self, image: np.ndarray) -> float: """Return the percentage of the input image that is background. diff --git a/src/openflexure_microscope_server/things/camera/__init__.py b/src/openflexure_microscope_server/things/camera/__init__.py index 32d4b1b8..238a7078 100644 --- a/src/openflexure_microscope_server/things/camera/__init__.py +++ b/src/openflexure_microscope_server/things/camera/__init__.py @@ -173,6 +173,10 @@ class BaseCamera(lt.Thing): dictionary in this function. Configuration will be added at a later date. """ super().__init__(thing_server_interface) + # 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" def __enter__(self) -> Self: @@ -591,6 +595,9 @@ class BaseCamera(lt.Thing): @property def active_detector(self) -> 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] @lt.action diff --git a/src/openflexure_microscope_server/things/camera/simulation.py b/src/openflexure_microscope_server/things/camera/simulation.py index 9678b18c..42058aa4 100644 --- a/src/openflexure_microscope_server/things/camera/simulation.py +++ b/src/openflexure_microscope_server/things/camera/simulation.py @@ -181,7 +181,7 @@ class SimulatedCamera(BaseCamera): @lt.property def calibration_required(self) -> bool: """Whether the camera needs calibrating.""" - return not self.background_detector_status.ready + return not self.active_detector.ready def generate_sprites(self) -> None: """Generate sprites to populate the image.""" diff --git a/src/openflexure_microscope_server/things/smart_scan.py b/src/openflexure_microscope_server/things/smart_scan.py index 47298a82..5add6866 100644 --- a/src/openflexure_microscope_server/things/smart_scan.py +++ b/src/openflexure_microscope_server/things/smart_scan.py @@ -234,7 +234,7 @@ class SmartScanThing(lt.Thing): self._csm.assert_calibration() if self.skip_background: - if not self._cam.background_detector_status.ready: + if not self._cam.active_detector.ready: raise RuntimeError( "Background is not set: you need to calibrate background detection." ) diff --git a/webapp/src/components/tabContentComponents/backgroundDetectComponents/paneBackgroundDetect.vue b/webapp/src/components/tabContentComponents/backgroundDetectComponents/paneBackgroundDetect.vue index 9de2ec45..4b91f504 100644 --- a/webapp/src/components/tabContentComponents/backgroundDetectComponents/paneBackgroundDetect.vue +++ b/webapp/src/components/tabContentComponents/backgroundDetectComponents/paneBackgroundDetect.vue @@ -5,18 +5,13 @@
  • Configure
    -

    - {{ backgroundDetectorName }} +

    + {{ backgroundDetectorDisplayName }}

    -
  • @@ -51,33 +46,26 @@