phenotypic.refine.Skeletonize#

class phenotypic.refine.Skeletonize(method: Literal['zhang', 'lee'] | None = None)[source]#

Bases: ObjectRefiner

Reduce object masks to single-pixel-wide skeletons using medial axis thinning.

Intuition:

Skeletonization compresses object regions to their medial axes (centerlines), preserving topological structure while reducing to 1-pixel width. On agar plates, this distills colony morphology to its core branching structure, useful for analyzing filamentous or spreading phenotypes without boundary noise. The method efficiently extracts the ‘backbone’ of colony shape.

Why this is useful for agar plates:

Colonies may have ragged edges, uneven staining, or noise that obscures their true spreading pattern. Skeletons expose the essential branching or directional growth, enabling more robust morphological features (e.g., branch count, elongation) and simplifying hyphae or filament tracking in fungal cultures.

Use cases:
  • Extracting colony centerlines for elongation or orientation analysis.

  • Analyzing branching patterns in spreading/filamentous phenotypes.

  • Simplifying masks for spatial graph analysis or filament tracking.

  • Reducing noise in masks before measuring advanced morphological features.

Caveats:
  • Skeletonization destroys width information; use only if you need topology, not colony boundary details.

  • Thin or poorly-defined colonies may produce fragmented or spurious skeleton branches.

  • Method choice (Zhang vs. Lee) can affect branch detection; Zhang is optimized for clean 2D images, Lee is more robust but may produce slightly thicker structures.

  • Isolated noise pixels may create spurious skeleton branches; apply cleanup (e.g., SmallObjectRemover) before skeletonizing.

method#

Thinning algorithm to use. - “zhang”: Fast, optimized for 2D images with clean topology. May produce

thin artifacts on noisy images.

  • “lee”: Works on 2D/3D, more robust to noise and irregular boundaries. Slightly slower than Zhang.

  • None: Auto-select based on image dimensionality (Zhang for 2D, Lee for 3D).

Type:

Literal[“zhang”, “lee”] | None

Examples

Reduce filamentous colony to medial axis skeleton
>>> from phenotypic.refine import Skeletonize
>>> op = Skeletonize(method="zhang")
>>> image = op.apply(image, inplace=True)  
Raises:

ValueError – If an invalid method is provided (checked during operation).

Parameters:

method (Literal['zhang', 'lee'] | None)

Methods

__init__

Initialize the skeletonizer.

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__(method: Literal['zhang', 'lee'] | None = None)[source]#

Initialize the skeletonizer.

Parameters:

method (Literal["zhang", "lee"] | None) –

Algorithm for skeletonization. - “zhang”: Optimized for 2D images; fast, produces thin skeletons.

Best for well-defined colony boundaries.

  • ”lee”: Works on 2D/3D; more robust to noisy or irregular boundaries. Slightly slower but preserves topology better on challenging images.

  • None: Automatically selects Zhang for 2D and Lee for 3D.

Choosing the right method depends on image quality: clean, binary masks benefit from Zhang; noisier masks or fungal hyphae benefit from Lee.

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