Get UI working for background detector things

This commit is contained in:
Julian Stirling 2026-01-13 19:05:24 +00:00
parent 44860bf777
commit 3e8874188e
5 changed files with 70 additions and 38 deletions

View file

@ -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.