polymahalanobis
A polynomial Mahalanobis distance classifier for multivariate / multispectral data.
Given a set of reference samples, PolyMahalanobis builds a nested sequence of
polynomial-expanded subspaces (via SVD) and evaluates how far a new sample is
from that reference distribution. Useful for anomaly detection, similarity
scoring, and multispectral classification (e.g. vegetation health from
satellite/drone imagery bands).
Only dependency: numpy.
Install
pip install polymahalanobis
Usage
import numpy as np
from polymahalanobis import PolyMahalanobis
# samples.txt: one reference sample per line, band values space-separated
model = PolyMahalanobis("samples.txt", num_levels=3)
model.makeSpace()
new_pixels = np.array([[120.0, 45.0, 200.0]], dtype=np.float32)
distances = model.evaluate(new_pixels) # shape (N, num_levels)
final_distance = distances[:, -1] # accumulated distance, last level
evaluate(spectral_values): batched evaluation,(N, n_bands) -> (N, num_levels).evaluate_single(spectral_value): convenience for a single(n_bands,)sample.evaluate_image(image_multispectral): convenience for a full(H, W, n_bands)array, with de-duplication of repeated pixel values.
Reference samples format
PolyMahalanobis(sample_file, num_levels) loads its reference samples from a
plain text file using numpy.loadtxt. The rules:
- One sample per line, band values separated by whitespace (spaces or tabs).
- Every line must have the same number of columns. That column count becomes
n_bands, and it must match the last dimension of every array you later pass toevaluate,evaluate_single, orevaluate_image. - Values are parsed as floating point numbers.
- Lines starting with
#are treated as comments and skipped. - No header row.
Example, for 3-band samples (say, R, G, B pixel values):
93.0 141.0 108.0
95.5 138.2 110.1
90.0 145.0 105.5
These lines should be a curated set of reference observations that define
what "normal" looks like for your use case (for example, healthy crop
pixels, or measurements from a known material). makeSpace() fits the
polynomial subspaces to them, and evaluate() then reports how far new
observations are from that reference distribution. A few dozen samples is
usually a reasonable minimum; too few samples relative to num_levels can
leave the later polynomial levels without enough variance to be meaningful.
References
This implementation is based on the Polynomial Mahalanobis Distance metric introduced in:
- G. Grudic and J. Mulligan, "Outdoor Path Labeling Using Polynomial Mahalanobis Distance," Robotics: Science and Systems II, 2006. https://doi.org/10.15607/RSS.2006.II.020
- A. C. Sobieranski, D. D. Abdala, E. Comunello, and A. von Wangenheim, "Learning a color distance metric for region-based image segmentation," Pattern Recognition Letters, vol. 30, no. 16, pp. 1496-1506, 2009. https://doi.org/10.1016/j.patrec.2009.08.002
Full project
This library is the core algorithm extracted from the PolynomialMahalanobis project, which also includes a parallelized command-line pipeline for classifying multispectral GeoTIFF orthophotos. That pipeline is not part of this PyPI package; see the repository for it.
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file polymahalanobis-0.1.1.tar.gz.
File metadata
- Download URL: polymahalanobis-0.1.1.tar.gz
- Upload date:
- Size: 8.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
377471c997cd2a4b3942d9c5890627fa32f426bca7d99c452550ef32bd96c348
|
|
| MD5 |
abc7a56946004d2b723a2cc59da10c0e
|
|
| BLAKE2b-256 |
6fd82942a438078a4f7833c0835ca68ce4d5229830818853beeda6a5907edbaf
|
Provenance
The following attestation bundles were made for polymahalanobis-0.1.1.tar.gz:
Publisher:
publish.yml on CodeWracker/PolynomialMahalanobis
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
polymahalanobis-0.1.1.tar.gz -
Subject digest:
377471c997cd2a4b3942d9c5890627fa32f426bca7d99c452550ef32bd96c348 - Sigstore transparency entry: 2540553981
- Sigstore integration time:
-
Permalink:
CodeWracker/PolynomialMahalanobis@abd28eafb0a221fc9ae271122fb2d21d23ca5792 -
Branch / Tag:
refs/tags/v0.1.1 - Owner: https://github.com/CodeWracker
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@abd28eafb0a221fc9ae271122fb2d21d23ca5792 -
Trigger Event:
release
-
Statement type:
File details
Details for the file polymahalanobis-0.1.1-py3-none-any.whl.
File metadata
- Download URL: polymahalanobis-0.1.1-py3-none-any.whl
- Upload date:
- Size: 10.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dc8816948ceed7135111c3daa4078e6bf5a8f76d07d63d607f09e010e0c64434
|
|
| MD5 |
efe96d83541772fc974ff909e5441456
|
|
| BLAKE2b-256 |
56dedf26981db1c5c58d433ddd1f11f1904fe7993a7f02c8be6b572fcce6862e
|
Provenance
The following attestation bundles were made for polymahalanobis-0.1.1-py3-none-any.whl:
Publisher:
publish.yml on CodeWracker/PolynomialMahalanobis
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
polymahalanobis-0.1.1-py3-none-any.whl -
Subject digest:
dc8816948ceed7135111c3daa4078e6bf5a8f76d07d63d607f09e010e0c64434 - Sigstore transparency entry: 2540554048
- Sigstore integration time:
-
Permalink:
CodeWracker/PolynomialMahalanobis@abd28eafb0a221fc9ae271122fb2d21d23ca5792 -
Branch / Tag:
refs/tags/v0.1.1 - Owner: https://github.com/CodeWracker
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@abd28eafb0a221fc9ae271122fb2d21d23ca5792 -
Trigger Event:
release
-
Statement type: