xrspatial.mahalanobis.mahalanobis#

xrspatial.mahalanobis.mahalanobis(bands: List[DataArray], mean: ndarray | None = None, inv_cov: ndarray | None = None, name: str = 'mahalanobis') DataArray[source]#

Compute per-pixel Mahalanobis distance from a multi-band reference.

Parameters:
  • bands (list of xr.DataArray) – N 2-D rasters (same shape, dtype family, and chunk structure). Must contain at least 2 bands.

  • mean (numpy array, shape (N,), optional) – Mean vector of the reference distribution. Must be provided together with inv_cov, or both omitted (auto-computed).

  • inv_cov (numpy array, shape (N, N), optional) – Inverse covariance matrix. Must be provided together with mean, or both omitted (auto-computed).

  • name (str, default='mahalanobis') – Name for the output DataArray.

Returns:

2-D float64 raster with same coords/dims/attrs as bands[0].

Return type:

xr.DataArray

Notes

A pixel is set to NaN in the output when any band is non-finite at that pixel. When mean and inv_cov are omitted they are estimated from the pixels that are finite across all bands, which requires at least N + 1 such pixels. Supports the numpy, cupy, dask+numpy, and dask+cupy backends.

Examples

>>> import numpy as np
>>> import xarray as xr
>>> from xrspatial.mahalanobis import mahalanobis

>>> red = xr.DataArray(
        np.array([[0., 1.], [2., 3.]]), dims=['y', 'x'])
>>> green = xr.DataArray(
        np.array([[0., np.nan], [1., 1.]]), dims=['y', 'x'])
>>> mahalanobis([red, green], mean=np.zeros(2), inv_cov=np.eye(2))
<xarray.DataArray 'mahalanobis' (y: 2, x: 2)>
array([[0.        ,        nan],
       [2.23606798, 3.16227766]])
Dimensions without coordinates: y, x