phenotypic.refine.ResidualOutlierRemover#

class phenotypic.refine.ResidualOutlierRemover(axis: int | None = None, stddev_multiplier=1.5, max_coeff_variance: int = 1)[source]#

Bases: GridObjectRefiner

Remove objects with large regression residuals in noisy grid rows/columns.

Intuition:

In grid assays, colony centroids should align along near-linear trends within each row/column. Rows or columns with high variability suggest mis-detections or artifacts. Within such noisy lines, this operation removes objects whose positional residuals exceed a robust cutoff.

Why this is useful for agar plates:

Condensation, glare, and debris can produce off-grid detections that inflate row/column variance and break gridding assumptions. Pruning residual outliers restores alignment and improves subsequent measures.

Use cases:
  • Cleaning rows with multiple off-line blobs before measuring growth.

  • Stabilizing grid registration when a subset of positions is noisy.

Caveats:
  • If true colonies deviate due to warping or growth spreading, strict cutoffs may remove real data.

  • Depends on reasonable initial grid fit; with severe misregistration it may prune valid colonies.

Parameters:
  • axis (Optional[int])

  • max_coeff_variance (int)

axis#

Axis to analyze for outliers. None analyzes both rows and columns; 0 analyzes rows; 1 analyzes columns. Restricting the axis can speed up processing or focus on suspected directions of error.

Type:

Optional[int]

cutoff_multiplier#

Multiplier applied to a robust dispersion estimate (IQR-based in implementation) to set the outlier cutoff. Higher values are more permissive (fewer removals) and preserve edge cases; lower values prune more aggressively.

Type:

float

max_coeff_variance#

Maximum coefficient of variance (std/mean) allowed for a row/column before it is considered for outlier pruning. Smaller values trigger cleaning sooner; larger values only clean severely noisy lines.

Type:

int

Examples

Remove objects with large regression residuals
>>> from phenotypic.refine import ResidualOutlierRemover
>>> op = ResidualOutlierRemover(axis=None, stddev_multiplier=1.5, max_coeff_variance=1)
>>> image = op.apply(image, inplace=True)  

Methods

__init__

Initialize the remover.

apply

Applies the operation to an image, either in-place or on a copy.

dispose_widgets

Drop references to the UI widgets.

sync_widgets_from_state

Push internal state into widgets.

widget

Return (and optionally display) the root widget.

__init__(axis: int | None = None, stddev_multiplier=1.5, max_coeff_variance: int = 1)[source]#

Initialize the remover.

Parameters:
  • axis (Optional[int]) – Axis selection for analysis. None runs both directions; 0 rows; 1 columns. Limiting the axis reduces runtime and targets known problem directions.

  • stddev_multiplier (float) – Robust residual cutoff multiplier. Lower values remove more outliers (stronger cleanup) but risk dropping valid off-center colonies; higher values are conservative.

  • max_coeff_variance (int) – Threshold for row/column variability (std/mean) to trigger outlier analysis. Lower values clean more lines; higher values only address extremely noisy lines.

Raises:

ValueError – If parameters are not consistent with the operation (e.g., invalid types). Errors may arise during execution when measuring grid statistics.

__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

dispose_widgets() None#

Drop references to the UI widgets.

Return type:

None

sync_widgets_from_state() None#

Push internal state into widgets.

Return type:

None

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.