Skip to main content

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 to evaluate, evaluate_single, or evaluate_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

polymahalanobis-0.1.1.tar.gz (8.1 kB view details)

Uploaded Source

Built Distribution

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

polymahalanobis-0.1.1-py3-none-any.whl (10.6 kB view details)

Uploaded Python 3

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

Hashes for polymahalanobis-0.1.1.tar.gz
Algorithm Hash digest
SHA256 377471c997cd2a4b3942d9c5890627fa32f426bca7d99c452550ef32bd96c348
MD5 abc7a56946004d2b723a2cc59da10c0e
BLAKE2b-256 6fd82942a438078a4f7833c0835ca68ce4d5229830818853beeda6a5907edbaf

See more details on using hashes here.

Provenance

The following attestation bundles were made for polymahalanobis-0.1.1.tar.gz:

Publisher: publish.yml on CodeWracker/PolynomialMahalanobis

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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

Hashes for polymahalanobis-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 dc8816948ceed7135111c3daa4078e6bf5a8f76d07d63d607f09e010e0c64434
MD5 efe96d83541772fc974ff909e5441456
BLAKE2b-256 56dedf26981db1c5c58d433ddd1f11f1904fe7993a7f02c8be6b572fcce6862e

See more details on using hashes here.

Provenance

The following attestation bundles were made for polymahalanobis-0.1.1-py3-none-any.whl:

Publisher: publish.yml on CodeWracker/PolynomialMahalanobis

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 files

0.1.0

2 files

Supported by

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