phenotypic.refine.TransitiveDistanceMerger#
- class phenotypic.refine.TransitiveDistanceMerger(distance_threshold: float = 20.0)[source]
Bases:
ObjectRefinerMerge nearby colony fragments using transitive closure of centroid distances.
Finds all pairs of objects with centroids within the distance threshold, then applies union-find with path compression to transitively merge connected groups. If A is near B and B is near C, all three merge into one detection even if A and C are not directly within threshold. Labels are relabeled consecutively after merging.
- Parameters:
distance_threshold (float) – Maximum centroid-to-centroid distance in pixels for merging. Typical range: 10–30. Lower values are conservative; higher values merge more aggressively but risk combining distinct colonies via transitive chains. Default: 20.0.
- Returns:
Input image with
objmapupdated so that transitively connected fragments share a single label, relabeled consecutively.- Return type:
Image
- Raises:
ValueError – If
distance_thresholdis not positive.
- Best For:
Repairing fragmented detections from watershed over-segmentation where a single colony splits into multiple touching regions.
Consolidating micro-fragments and satellite spots caused by thresholding artifacts or agar texture.
Correcting detections on plates with harsh shadows or glare that create internal voids within colony masks.
Post-processing after aggressive noise removal that leaves fragmented edges.
- Consider Also:
SmallToLargeMergerwhen only small fragments should merge into large anchors, preserving distinct large colonies.NearestNeighborMergerfor simple pairwise nearest- neighbor merging without transitive closure.MaskCloserfor morphological closing that bridges small gaps without relabeling.
See also
How To: Merge Fragmented Detections for fragment merging workflows. Refinement Strategies for a comparison of merging strategies.
Methods
Initialize the merger.
Applies the operation to an image, either in-place or on a copy.
Return (and optionally display) the root widget.
- __init__(distance_threshold: float = 20.0)[source]
Initialize the merger.
- Parameters:
distance_threshold (float) – Maximum centroid-to-centroid distance in pixels for merging colonies. Increasing this value merges more fragments but risks combining distinct colonies. Should be smaller than the minimum expected distance between separate colonies.
- Raises:
ValueError – If distance_threshold is not positive.
- __del__()
Automatically stop tracemalloc when the object is deleted.
- __getstate__()
Prepare the object for pickling by disposing of any widgets.
This ensures that UI components (which may contain unpickleable objects like input functions or thread locks) are cleaned up before serialization.
Note
This method modifies the object state by calling dispose_widgets(). Any active widgets will be detached from the object.
- apply(image, inplace=False)
Applies the operation to an image, either in-place or on a copy.
- Parameters:
image (Image) – The arr image to apply the operation on.
inplace (bool) – If True, modifies the image in place; otherwise, operates on a copy of the image.
- Returns:
The modified image after applying the operation.
- Return type:
Image
- widget(image: Image | None = None, show: bool = False) Widget
Return (and optionally display) the root widget.
- Parameters:
image (Image | None) – Optional image to visualize. If provided, visualization controls will be added to the widget.
show (bool) – Whether to display the widget immediately. Defaults to False.
- Returns:
The root widget.
- Return type:
ipywidgets.Widget
- Raises:
ImportError – If ipywidgets or IPython are not installed.