phenotypic.refine.SmallObjectRemover#

class phenotypic.refine.SmallObjectRemover(min_size=64)[source]#

Bases: ObjectRefiner

Remove small, likely spurious objects from a labeled object map.

Intuition:

After thresholding/segmentation of agar-plate images, tiny specks from dust, condensation, camera noise, or over-segmentation can appear as separate labeled objects. Removing these below a minimum size reduces false positives and stabilizes downstream phenotyping.

Use cases (agar plates):
  • Clean up salt-and-pepper detections before measuring colony size or shape.

  • Suppress fragmented debris around large colonies that may bias counts or area statistics.

  • Post-processing step after aggressive enhancement/thresholding.

Tuning and effects:
  • min_size: Sets the minimum object area (in pixels). Increasing this value removes more small fragments, typically improving mask quality and background suppression, but may also delete legitimate micro- colonies when colonies are extremely small or underexposed.

Caveats:
  • Setting min_size too high can remove small but real colonies or early-time-point growth, reducing recall.

  • The optimal threshold depends on resolution; what is “small” at high-resolution imaging may be substantial at low resolution.

(No public attributes)

Examples

Remove small spurious objects below a minimum size
>>> from phenotypic.refine import SmallObjectRemover
>>> op = SmallObjectRemover(min_size=100)
>>> 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__(min_size=64)[source]#

Initialize the remover.

Parameters:

min_size (int) – Minimum object area (in pixels) to keep. Higher values remove more small artifacts and fragmented edges, generally improving mask cleanliness but risking loss of tiny colonies.

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