phenotypic.measure.MeasureShape#

class phenotypic.measure.MeasureShape[source]#

Bases: MeasureFeatures

Measure morphological characteristics of detected microbial colonies.

This class extracts comprehensive geometric metrics from colony shapes, including area, perimeter, circularity, convex hull properties, radius-based measures, Feret diameters, elongation (eccentricity), and ellipse fitting parameters. These measurements quantify colony morphology, growth patterns, and spatial organization on agar plates.

Intuition: Colony shape encodes biological and environmental information. Regular circular colonies indicate healthy, isotropic growth under uniform conditions. Irregular, elongated, or filamentous morphologies suggest mutations, directional growth (chemotaxis), nutrient stress, or environmental gradients on the agar surface. Shape measures are used to classify colony types, assess fitness, and detect phenotypic variants in high-throughput screening.

Use cases (agar plates): - Distinguish colony morphotypes: smooth circular (wild-type) vs wrinkled, branching, or invasive

(mutant phenotypes).

  • Assess growth symmetry via eccentricity and orientation; colonies with high eccentricity may indicate motility, chemotaxis, or unidirectional stress.

  • Detect invasive or spreading growth via low solidity (indented periphery) or high convex area relative to actual area.

  • Enable morphological clustering and classification for automated strain identification or phenotypic screening.

  • Measure colony compactness to predict growth kinetics: compact colonies often have higher growth rates than sprawling ones under nutrient limitation.

Caveats: - Shape measurements depend entirely on segmentation quality; poor thresholding or edge detection

yield misleading morphology metrics.

  • Perimeter is sensitive to pixel-level noise; small variations in boundary can inflate perimeter and reduce circularity. Consider smoothing or filtering for robust estimates.

  • Feret diameters and convex hull computation are sensitive to boundary artifacts; outlier or misdetected pixels at the edge disproportionately affect these metrics.

  • Radius-based measures (mean, median, max radius) depend on centroid accuracy; off-center centroids from irregular shapes can yield biased radius values.

  • Eccentricity ranges 0–1 (circle to line); values near 0 and 1 are rare for biological objects. Interpret eccentricity alongside aspect ratio and orientation for robust shape classification.

Returns:
pd.DataFrame: Object-level morphological measurements with columns:
  • Label: Unique object identifier.

  • Area: Number of pixels in the colony.

  • Perimeter: Boundary length in pixels.

  • Circularity: 4π·Area/Perimeter² (1.0 = perfect circle; <1 = irregular).

  • Compactness: Perimeter²/(4π·Area) (inverse of circularity; >1 for irregular shapes).

  • ConvexArea: Area of convex hull (smallest convex polygon containing the colony).

  • Solidity: Area/ConvexArea (1.0 = convex; <1 = indented/spreading).

  • Extent: Area/BboxArea (1.0 = fills bounding box; <1 = spread out).

  • BboxArea: Area of axis-aligned bounding rectangle.

  • MeanRadius, MedianRadius, MaxRadius: Distance from centroid to edge (robust size measures).

  • MinFeretDiameter, MaxFeretDiameter: Minimum/maximum caliper diameters (orientation-independent).

  • MajorAxisLength, MinorAxisLength: Axes of best-fit ellipse.

  • Eccentricity: Ellipse elongation (0 = circle; 1 = line).

  • Orientation: Angle of major axis (radians, –π/2 to π/2).

Examples:
Measure colony morphology for phenotypic classification
from phenotypic import Image
from phenotypic.detect import OtsuDetector
from phenotypic.measure import MeasureShape

# Load plate with multiple morphotype colonies
image = Image.from_image_path("morphotype_plate.jpg")
detector = OtsuDetector()
image = detector.operate(image)

# Measure morphology
shaper = MeasureShape()
shapes = shaper.operate(image)

# Classify morphotypes by circularity and solidity
smooth_round = shapes[
    (shapes['Shape_Circularity'] > 0.8) &
    (shapes['Shape_Solidity'] > 0.95)
]
invasive = shapes[shapes['Shape_Solidity'] < 0.85]

print(f"Smooth/round colonies: {len(smooth_round)}")
print(f"Invasive/spreading colonies: {len(invasive)}")
Detect elongated or directional growth
# Use eccentricity and max radius to find elongated colonies
shapes = shaper.operate(image)

elongated = shapes[shapes['Shape_Eccentricity'] > 0.7]
print(f"Highly elongated colonies: {len(elongated)}")

# Visualize growth directionality
import numpy as np
for idx, row in elongated.iterrows():
    angle = np.degrees(row['Shape_Orientation'])
    aspect = row['Shape_MajorAxisLength'] / row['Shape_MinorAxisLength']
    print(f"Colony {row['OBJECT_Label']}: angle={angle:.1f}°, aspect={aspect:.2f}")
Category: SHAPE#

Name

Description

Area

Total number of pixels occupied by the microbial colony. Represents colony biomass and growth extent on agar plates. Larger areas typically indicate more robust growth or longer incubation times.

Perimeter

Total length of the colony’s outer boundary in pixels. Measures colony edge complexity and surface irregularity. Smooth, circular colonies have shorter perimeters relative to their area compared to irregular or filamentous colonies.

Circularity

Calculated as \(\frac{4\pi*\text{Area}}{\text{Perimeter}^2}\). Measures how closely a colony approximates a perfect circle (value = 1). Values < 1 indicate irregular colony morphology, which may result from genetic mutations, environmental stress, or mixed microbial populations on agar plates.

ConvexArea

Area of the smallest convex polygon that completely contains the colony. Represents the colony’s “filled-in” appearance if all indentations and holes were removed. Useful for detecting colony spreading patterns or invasive growth characteristics.

MedianRadius

Median distance from colony center to edge across all directions. Provides a robust measure of typical colony size that is less sensitive to outliers than mean radius. Particularly useful for colonies with uneven growth or sectoring.

MeanRadius

Average distance from colony center to edge across all directions. Represents overall colony expansion rate. In arrayed growth assays, this correlates with microbial fitness and growth kinetics under controlled conditions.

MaxRadius

Maximum distance from colony center to edge across all directions. Represents the furthest extent of colony growth from its center. In arrayed microbial assays, this measurement helps identify asymmetric growth patterns or colonies extending toward neighboring positions.

MinFeretDiameter

Minimum caliper diameter - the shortest distance between two parallel tangent lines touching opposite sides of the colony. Represents the narrowest dimension of the colony regardless of orientation. Useful for detecting elongated or irregular colony morphologies and measuring colony width.

MaxFeretDiameter

Maximum caliper diameter - the longest distance between two parallel tangent lines touching opposite sides of the colony. Represents the maximum dimension of the colony regardless of orientation. Often exceeds major axis length for irregular shapes and helps quantify maximum colony extent.

Eccentricity

Measure of colony elongation, ranging from 0 (perfect circle) to 1 (highly elongated). Values near 0 indicate compact, radially symmetric growth typical of healthy bacterial colonies, while higher values may suggest directional growth, motility, or environmental gradients on the agar surface.

Solidity

Ratio of actual colony area to its convex hull area (Area/ConvexArea). Values near 1 indicate compact, solid colonies with minimal indentations. Lower values (< 0.9) may indicate invasive growth, colony spreading, or the presence of clearing zones around colonies.

Extent

Ratio of colony area to its bounding box area (ObjectArea/BboxArea). Measures how efficiently the colony fills its allocated space. Compact colonies have higher extent values, while spread-out or irregular colonies have lower values.

BboxArea

Area of the smallest rectangle that completely contains the colony. Represents the total spatial footprint of the colony including any empty space. In high-throughput assays, this helps assess colony positioning and potential interference with neighboring colonies.

MajorAxisLength

Length of the longest axis of the ellipse that best fits the colony shape. Represents the maximum colony dimension. In arrayed microbial growth, this measurement helps identify colonies that have grown beyond their intended grid positions.

MinorAxisLength

Length of the shortest axis of the ellipse that best fits the colony shape. Represents the minimum colony dimension. Together with major axis length, this helps characterize colony aspect ratio and growth anisotropy.

Compactness

Calculated as \(\frac{\text{Perimeter}^2}{4\pi*\text{Area}}\). Inverse of circularity (ranges from 1 for perfect circles to higher values for irregular shapes). Measures colony shape complexity - compact, circular colonies have values near 1, while irregular or filamentous colonies have much higher values.

Orientation

Angle (in radians) between the colony’s major axis and the horizontal axis. Measures colony alignment and growth directionality. Random orientations are typical for most bacterial colonies, while consistent orientations may indicate environmental gradients or mechanical stresses during plating.

Methods

__init__

measure

Execute the measurement operation on a detected-object image.

__del__()#

Automatically stop tracemalloc when the object is deleted.

measure(image, include_meta=False)#

Execute the measurement operation on a detected-object image.

This is the main public API method for extracting measurements. It handles: input validation, parameter extraction via introspection, calling the subclass-specific _operate() method, optional metadata merging, and exception handling.

How it works (for users):

  1. Pass your processed Image (with detected objects) to measure()

  2. The method calls your subclass’s _operate() implementation

  3. Results are validated and returned as a pandas DataFrame

  4. If include_meta=True, image metadata (filename, grid info) is merged in

How it works (for developers):

When you subclass MeasureFeatures, you only implement _operate(). This measure() method automatically:

  • Extracts __init__ parameters from your instance (introspection)

  • Passes matched parameters to _operate() as keyword arguments

  • Validates the Image has detected objects (objmap)

  • Wraps exceptions in OperationFailedError with context

  • Merges grid/object metadata if requested

Parameters:
  • image (Image) – A PhenoTypic Image object with detected objects (must have non-empty objmap from a prior detection operation).

  • include_meta (bool, optional) – If True, merge image metadata columns (filename, grid position, etc.) into the results DataFrame. Defaults to False.

Returns:

Measurement results with structure:

  • First column: OBJECT.LABEL (integer IDs from image.objmap[:])

  • Remaining columns: Measurement values (float, int, or string)

  • One row per detected object

If include_meta=True, additional metadata columns are prepended before OBJECT.LABEL (e.g., Filename, GridRow, GridCol).

Return type:

pd.DataFrame

Raises:

OperationFailedError – If _operate() raises any exception, it is caught and re-raised as OperationFailedError with details including the original exception type, message, image name, and operation class. This provides consistent error handling across all measurers.

Notes

  • This method is the main entry point; do not override in subclasses

  • Subclasses implement _operate() only, not this method

  • Automatic memory profiling is available via logging configuration

  • Image must have detected objects (image.objmap should be non-empty)

Examples

Basic measurement extraction
from phenotypic import Image
from phenotypic.measure import MeasureSize
from phenotypic.detect import OtsuDetector

# Load and detect
image = Image.from_image_path('plate.jpg')
image = OtsuDetector().operate(image)

# Extract measurements
measurer = MeasureSize()
df = measurer.measure(image)
print(df.head())
Include metadata in measurements
# With image metadata (filename, grid info)
df_with_meta = measurer.measure(image, include_meta=True)
print(df_with_meta.columns)
# Output: ['Filename', 'GridRow', 'GridCol', 'OBJECT.LABEL',
#          'Area', 'IntegratedIntensity', ...]