-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
301 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
""" Statistical tools""" | ||
|
||
from typing import Any | ||
|
||
import numpy as np | ||
|
||
from geoutils._typing import NDArrayNum | ||
|
||
|
||
def nmad(data: NDArrayNum, nfact: float = 1.4826) -> np.floating[Any]: | ||
""" | ||
Calculate the normalized median absolute deviation (NMAD) of an array. | ||
Default scaling factor is 1.4826 to scale the median absolute deviation (MAD) to the dispersion of a normal | ||
distribution (see https://en.wikipedia.org/wiki/Median_absolute_deviation#Relation_to_standard_deviation, and | ||
e.g. Höhle and Höhle (2009), http://dx.doi.org/10.1016/j.isprsjprs.2009.02.003) | ||
:param data: Input array or raster | ||
:param nfact: Normalization factor for the data | ||
:returns nmad: (normalized) median absolute deviation of data. | ||
""" | ||
if isinstance(data, np.ma.masked_array): | ||
data_arr = data.compressed() | ||
else: | ||
data_arr = np.asarray(data) | ||
return nfact * np.nanmedian(np.abs(data_arr - np.nanmedian(data_arr))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
""" | ||
Test functions for stats | ||
""" | ||
|
||
import scipy | ||
|
||
from geoutils import Raster, examples | ||
from geoutils.stats import nmad | ||
|
||
|
||
class TestStats: | ||
landsat_b4_path = examples.get_path("everest_landsat_b4") | ||
landsat_raster = Raster(landsat_b4_path) | ||
|
||
def test_nmad(self) -> None: | ||
"""Test NMAD functionality runs on any type of input""" | ||
|
||
# Check that the NMAD is computed the same with a masked array or NaN array, and is equal to scipy nmad | ||
nmad_ma = nmad(self.landsat_raster.data) | ||
nmad_array = nmad(self.landsat_raster.get_nanarray()) | ||
nmad_scipy = scipy.stats.median_abs_deviation(self.landsat_raster.data, axis=None, scale="normal") | ||
|
||
assert nmad_ma == nmad_array | ||
assert nmad_ma.round(2) == nmad_scipy.round(2) | ||
|
||
# Check that the scaling factor works | ||
nmad_1 = nmad(self.landsat_raster.data, nfact=1) | ||
nmad_2 = nmad(self.landsat_raster.data, nfact=2) | ||
|
||
assert nmad_1 * 2 == nmad_2 |