Skip to main content

PartiMorph

PartiMorph analyzes a single 2D binary particle mask and returns geometric shape metrics.

Metric Meaning
Wadell roundness Corner curvature-based roundness
Circularity 4 * pi * area / perimeter^2
Sphericity Inscribed circle radius divided by enclosing circle radius
Aspect ratio Fitted ellipse major axis divided by minor axis

Roundness, circularity, and sphericity are clipped to 0..1. Aspect ratio is >= 1.

Installation

PartiMorph requires Python 3.12+.

pip install partimorph

For local development:

pip install -e ".[dev]"

Quick Start

import partimorph as pm

mask = pm.utils.create_circle_mask((256, 256), (128, 128), 60)
results = pm.analyze_mask(mask)

if results is None:
    raise ValueError("mask has no foreground pixels")

print(results["roundness"]["val"])
print(results["circularity"]["val"])
print(results["sphericity"]["val"])
print(results["aspect_ratio"]["val"])

Visualize the results:

pm.utils.plot_analysis_results(mask, results)

API

pm.analyze_mask(
    mask,
    use_aspect_ratio=True,
    use_roundness=True,
    use_circularity=True,
    use_sphericity=True,
    roundness_params=None,
    eps=0.001,
    target_dim=384,
)
Parameter Description
mask 2D numpy.ndarray with bool or {0, 1} values
use_* Toggle individual metrics on/off
roundness_params Optional dict to override Wadell roundness parameters
eps Tolerance for geometric computations
target_dim Particle bounds larger than this are downscaled for speed, then coordinates are restored

Returns None for an empty mask. Otherwise, returns an AnalysisResult dict. Disabled metrics are omitted; enabled metrics are present and may be None when the geometry cannot be measured.

Result Shape

{
  "roundness": {"val": float} | None,
  "circularity": {"val": float} | None,
  "sphericity": {
    "val": float,
    "inscribed": {"x": float, "y": float, "r": float},
    "enclosing": {"x": float, "y": float, "r": float},
  } | None,
  "aspect_ratio": {
    "val": float,
    "ellipse": {
      "major": float, "minor": float,
      "x": float, "y": float, "angle": float,
      "w": float, "h": float, "bbox": list[list[float]],
    },
  } | None,
}

Input Rules

  • mask must be a 2D numpy.ndarray
  • Allowed values are bool or numeric {0, 1}
  • Floating masks must not contain NaN or inf
  • Empty mask returns None
  • The largest connected foreground component is selected and holes are filled
  • Surrounding zero padding does not change the measured shape
  • Downscaling is skipped if its output would have a one-pixel axis
  • Shape and center arguments use image order: (height, width) and (y, x)

Project Layout

  • analyzer.py: public analysis workflow and result-coordinate restoration
  • validation.py: public input normalization and analysis-option validation
  • mask_ops.py: component selection, crop/resize transforms, and contours
  • metrics.py: metric formulas and public result payloads
  • fitting.py: circle and ellipse fitting plus oriented bounding boxes
  • wadell/: boundary, smoothing, discretization, and corner roundness stages
  • utils/: synthetic mask construction and visualization

Utilities

Mask constructors:

  • pm.utils.create_circle_mask(...)
  • pm.utils.create_ellipse_mask(...)
  • pm.utils.create_rectangle_mask(...)
  • pm.utils.create_square_mask(...)
  • pm.utils.create_triangle_mask(...)
  • pm.utils.create_pentagon_mask(...)
  • pm.utils.create_star_mask(...)
  • pm.utils.create_particle_mask(...)

Visualization:

  • pm.utils.plot_analysis_results(mask, results)

Changes in 0.1.8

  • Corrected the fitted ellipse overlay orientation for rotated particles.
  • Unified inscribed-circle fitting used by sphericity and Wadell roundness.
  • Simplified ellipse fitting and bounding-box coordinate transforms while preserving the public API and analysis results.

License

MIT

Metadata

Release files for partimorph 0.1.8

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for partimorph 0.1.8
File Size Uploaded Provenance Actions
partimorph-0.1.8.tar.gz 20.6 kB Without provenance Details for partimorph-0.1.8.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for partimorph 0.1.8
File Interpreter ABI Platform Actions
partimorph-0.1.8-py3-none-any.whl Python 3 none any Details for partimorph-0.1.8-py3-none-any.whl

Total release size: 40.0 kB

Release files / partimorph-0.1.8.tar.gz

Download URL partimorph-0.1.8.tar.gz
Size 20.6 kB
Tags Source
SHA-256 checksum
How to use checksums
b3cc8a3104218c69fd358d9e39901996c4b3eb69ab3537567f6590db816196db
BLAKE2b-256 checksum
How to use checksums
eb88e3c2864422c1cfed59e7567b820f4dd15f9545bbe0f6beaec1b4eae570aa
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.14

Release files / partimorph-0.1.8-py3-none-any.whl

Download URL partimorph-0.1.8-py3-none-any.whl
Size 19.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4746699f72718da42cf532021a5b2e1873713404c55e690f1ac4833a1ddd2b77
BLAKE2b-256 checksum
How to use checksums
fcd879222cf57981dcdd691f7daa4a24a9722b27626c222815e9dbd0a0fb62c8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.14

Security for partimorph 0.1.8

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page