phenotypic.enhance.RankMedianEnhancer#

class phenotypic.enhance.RankMedianEnhancer(shape: str = 'square', radius: int | None = None, shift_x=0, shift_y=0)[source]#

Bases: ImageEnhancer

Rank-based median filtering with configurable footprint.

Applies a local median using rank filters with a user-defined footprint shape and radius. For agar plate colony images, this enables targeted suppression of impulsive noise while tailoring the spatial scale to colony size and shape, offering more control than a default median.

Use cases (agar plates): - Denoise while preserving colony boundaries by matching the footprint radius

to be smaller than colony diameters.

  • Use anisotropic or non-circular footprints (e.g., squares) for grid-like artifacts from imaging hardware.

Tuning and effects: - shape: Choose ‘disk’ for circular isotropic smoothing on plates;

‘square’ or ‘cube’ can align with grid artifacts; ‘ball’ for 3D stacks.

  • radius: Controls neighborhood size. Larger radii remove more noise but can erode small colonies and close tight gaps.

  • shift_x/shift_y: Offset the footprint center to bias the neighborhood if imaging introduces directional streaks; typically left at 0.

Caveats: - Very large footprints may over-smooth and merge nearby colonies. - Rank filters operate on uint8 here; intensity scaling occurs internally.

Ensure consistency if comparing raw intensities elsewhere.

Parameters:
shape#

‘disk’, ‘square’, ‘sphere’, or ‘cube’ defining the footprint geometry.

Type:

str

radius#

Radius (pixels). If None, a small default derived from image size is used.

Type:

int | None

shift_x#

Horizontal footprint offset for advanced use.

Type:

int

shift_y#

Vertical footprint offset for advanced use.

Type:

int

Methods

__init__

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__(shape: str = 'square', radius: int | None = None, shift_x=0, shift_y=0)[source]#
Parameters:
  • shape (str) – Geometry of the neighborhood. Use ‘disk’ for isotropic smoothing on plates; ‘square’ to align with grid noise; ‘sphere’/’cube’ for 3D contexts. Default ‘square’.

  • radius (int | None) – Neighborhood radius in pixels. Set smaller than the minimum colony radius to preserve colony edges; None chooses a small default based on image size.

  • shift_x (int) – Horizontal offset of the footprint center to bias the neighborhood if artifacts are directional. Typically 0.

  • shift_y (int) – Vertical offset of the footprint center. Typically 0.

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