Skip to main content

Particle shape analysis and visualization library.

Project description

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 Large masks are downscaled to this size for speed, then coordinates are rescaled

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
  • Shape and center arguments use image order: (height, width) and (y, x)

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)

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

partimorph-0.1.6.tar.gz (17.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

partimorph-0.1.6-py3-none-any.whl (18.6 kB view details)

Uploaded Python 3

File details

Details for the file partimorph-0.1.6.tar.gz.

File metadata

  • Download URL: partimorph-0.1.6.tar.gz
  • Upload date:
  • Size: 17.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.14

File hashes

Hashes for partimorph-0.1.6.tar.gz
Algorithm Hash digest
SHA256 f81968819279f5ce112706c47d11316230fa273744ab618a6f0d6f772f81ffa8
MD5 e61d0e6f38f1d1258b92f17aa131774c
BLAKE2b-256 086d7ec25e0ef876fb7ce39c7d1038c81c64cb1c16061a9b8bd506ce968b112e

See more details on using hashes here.

File details

Details for the file partimorph-0.1.6-py3-none-any.whl.

File metadata

  • Download URL: partimorph-0.1.6-py3-none-any.whl
  • Upload date:
  • Size: 18.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.14

File hashes

Hashes for partimorph-0.1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 7f91e83130f2d6062dd99ce66153e2611340592d8c43c37d4de61c109f882610
MD5 682f6a1d81870aa2901b56027f24c67b
BLAKE2b-256 202f67ebc29383e2fc54c0b53441269ed697ed6f58dadbb29beb11f075ba6a87

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page