phenotypic.refine.BorderObjectRemover#

class phenotypic.refine.BorderObjectRemover(border_size: int | float | None = 1)[source]#

Bases: ObjectRefiner

Remove objects that touch the image border within a configurable margin.

Intuition:

Colonies that intersect the plate boundary or image crop edge are often partial, poorly segmented, and bias size/shape measurements. This operation zeroes any labeled objects in image.objmap whose pixels fall within a user-defined border band, ensuring only fully contained colonies are analyzed.

Why this is useful for agar-plate imaging:

Plate crops or grid layouts frequently clip edge colonies. Removing border-touching objects stabilizes downstream phenotyping (area, circularity, intensity) and prevents partial colonies from contaminating statistics or training data.

Use cases:
  • Single-plate captures where the plate rim truncates colonies.

  • Grid assays where wells or positions near the frame boundary are partially visible.

  • Automated crops that shift slightly between frames, cutting off colonies at the margins.

Caveats:
  • A large border may remove valid edge colonies (lower recall). Too small a border may retain partial objects.

  • With very tight crops (little background), even modest margins can eliminate many colonies.

border_size#

Width of the exclusion border around the image.

Type:

int

Examples

Remove objects that touch the image border within a margin
>>> from phenotypic.refine import BorderObjectRemover
>>> op = BorderObjectRemover(border_size=15)
>>> image = op.apply(image, inplace=True)  
>>> # All colonies intersecting a 15-pixel frame margin are removed
Raises:

TypeError – If an invalid border_size type is provided (raised during operation when parameters are validated).

Parameters:

border_size (Optional[Union[int, float]])

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__(border_size: int | float | None = 1)[source]#

Initialize the remover.

Parameters:

border_size (int | float | None) –

Width of the exclusion border around the image. - None: Use a default margin equal to 1% of the smaller

image dimension.

  • float in (0, 1): Interpret as a fraction of the minimum image dimension, producing a resolution-adaptive margin.

  • int or float ≥ 1: Interpret as an absolute number of pixels.

Notes

Larger margins remove more edge-touching colonies and are useful when crops are loose or the plate rim intrudes. Smaller margins preserve edge colonies but risk including partial objects.

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