GeezerVison is showing github.com — pygmt.binstats: Let the parameter 'statistic' support descriptive arg… · GenericMappingTools/pygmt@2bc5931
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: pygmt/src/binstats.py +77-26Lines changed: 77 additions & 26 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 2 | 2 | binstats - Bin spatial data and determine statistics per bin. | 3 | 3 | """ | 4 | 4 |
| | 5 | +from typing import Literal | | 6 | + | 5 | 7 | import xarray as xr | 6 | 8 | from pygmt._typing import PathLike, TableLike | | 9 | +from pygmt.alias import Alias, AliasSystem | 7 | 10 | from pygmt.clib import Session | 8 | 11 | from pygmt.helpers import build_arg_list, fmt_docstring, kwargs_to_strings, use_alias | 9 | 12 |
| 10 | 13 |
| 11 | 14 | @fmt_docstring | 12 | 15 | @use_alias( | 13 | | - C="statistic", | 14 | 16 | E="empty", | 15 | 17 | I="spacing", | 16 | 18 | N="normalize", | | 26 | 28 | ) | 27 | 29 | @kwargs_to_strings(I="sequence", R="sequence", i="sequence_comma") | 28 | 30 | def binstats( | 29 | | - data: PathLike | TableLike, outgrid: PathLike | None = None, **kwargs | | 31 | + data: PathLike | TableLike, | | 32 | + outgrid: PathLike | None = None, | | 33 | + statistic: Literal[ | | 34 | + "mean", | | 35 | + "mad", | | 36 | + "full", | | 37 | + "interquartile", | | 38 | + "min", | | 39 | + "minpos", | | 40 | + "median", | | 41 | + "number", | | 42 | + "lms", | | 43 | + "mode", | | 44 | + "quantile", | | 45 | + "rms", | | 46 | + "stddev", | | 47 | + "max", | | 48 | + "maxneg", | | 49 | + "sum", | | 50 | + ] = "number", | | 51 | + quantile_value: float = 50, | | 52 | + **kwargs, | 30 | 53 | ) -> xr.DataArray | None: | 31 | 54 | r""" | 32 | 55 | Bin spatial data and determine statistics per bin. | @@ -42,35 +65,36 @@ def binstats( | 42 | 65 | Full GMT docs at :gmt-docs:`gmtbinstats.html`. | 43 | 66 |
| 44 | 67 | {aliases} | | 68 | + - C=statistic | 45 | 69 |
| 46 | 70 | Parameters | 47 | 71 | ---------- | 48 | 72 | data | 49 | 73 | A file name of an ASCII data table or a 2-D {table-classes}. | 50 | 74 | {outgrid} | 51 | | - statistic : str | 52 | | - **a**\|\ **d**\|\ **g**\|\ **i**\|\ **l**\|\ **L**\|\ **m**\|\ **n**\ | 53 | | - \|\ **o**\|\ **p**\|\ **q**\ [*quant*]\|\ **r**\|\ **s**\|\ **u**\ | 54 | | - \|\ **U**\|\ **z**. | 55 | | - Choose the statistic that will be computed per node based on the | 56 | | - points that are within *radius* distance of the node. Select one of: | | 75 | + statistic | | 76 | + Choose the statistic that will be computed per node based on the points that are | | 77 | + within *radius* distance of the node. Select one of: | 57 | 78 |
| 58 | | - - **a**: mean (average) | 59 | | - - **d**: median absolute deviation (MAD) | 60 | | - - **g**: full (max-min) range | 61 | | - - **i**: 25-75% interquartile range | 62 | | - - **l**: minimum (low) | 63 | | - - **L**: minimum of positive values only | 64 | | - - **m**: median | 65 | | - - **n**: number of values | 66 | | - - **o**: LMS scale | 67 | | - - **p**: mode (maximum likelihood) | 68 | | - - **q**: selected quantile (append desired quantile in 0-100% range [50]) | 69 | | - - **r**: root mean square (RMS) | 70 | | - - **s**: standard deviation | 71 | | - - **u**: maximum (upper) | 72 | | - - **U**: maximum of negative values only | 73 | | - - **z**: sum | | 79 | + - ``"mean"``: Mean (i.e., average). | | 80 | + - ``"mad"``: Median absolute deviation (MAD). | | 81 | + - ``"full"``: The full (max-min) range. | | 82 | + - ``"interquartile"``: The 25-75% interquartile range. | | 83 | + - ``"min"``: Minimum (lowest value). | | 84 | + - ``"minpos"``: Minimum of positive values only. | | 85 | + - ``"median"``: Median value. | | 86 | + - ``"number"``: The number of values per bin. | | 87 | + - ``"lms"``: Least median square (LMS) scale. | | 88 | + - ``"mode"``: Mode (maximum likelihood estimate). | | 89 | + - ``"quantile"``: Selected quantile. The quantile value is specified by the | | 90 | + ``quantile_value`` parameter and is in the 0-100% range. | | 91 | + - ``"rms"``: Root mean square (RMS). | | 92 | + - ``"stddev"``: Standard deviation. | | 93 | + - ``"max"``: Maximum (highest value). | | 94 | + - ``"maxneg"``: Maximum of negative values only. | | 95 | + - ``"sum"``: The sum of the values. | | 96 | + quantile_value | | 97 | + The quantile value if ``statistic="quantile"``. | 74 | 98 | empty : float | 75 | 99 | Set the value assigned to empty nodes [Default is NaN]. | 76 | 100 | normalize : bool | @@ -104,13 +128,40 @@ def binstats( | 104 | 128 | - ``None`` if ``outgrid`` is set (grid output will be stored in the file set by | 105 | 129 | ``outgrid``) | 106 | 130 | """ | | 131 | + aliasdict = AliasSystem( | | 132 | + C=Alias( | | 133 | + statistic, | | 134 | + name="statistic", | | 135 | + mapping={ | | 136 | + "mean": "a", | | 137 | + "mad": "d", | | 138 | + "full": "g", | | 139 | + "interquartile": "i", | | 140 | + "min": "l", | | 141 | + "minpos": "L", | | 142 | + "median": "m", | | 143 | + "number": "n", | | 144 | + "lms": "o", | | 145 | + "mode": "p", | | 146 | + "quantile": "q", | | 147 | + "rms": "r", | | 148 | + "stddev": "s", | | 149 | + "max": "u", | | 150 | + "maxneg": "U", | | 151 | + "sum": "z", | | 152 | + }, | | 153 | + ), | | 154 | + ).merge(kwargs) | | 155 | + if statistic == "quantile": | | 156 | + aliasdict["C"] += f"{quantile_value}" | | 157 | + | 107 | 158 | with Session() as lib: | 108 | 159 | with ( | 109 | 160 | lib.virtualfile_in(check_kind="vector", data=data) as vintbl, | 110 | 161 | lib.virtualfile_out(kind="grid", fname=outgrid) as voutgrd, | 111 | 162 | ): | 112 | | - kwargs["G"] = voutgrd | | 163 | + aliasdict["G"] = voutgrd | 113 | 164 | lib.call_module( | 114 | | - module="binstats", args=build_arg_list(kwargs, infile=vintbl) | | 165 | + module="binstats", args=build_arg_list(aliasdict, infile=vintbl) | 115 | 166 | ) | 116 | 167 | return lib.virtualfile_to_raster(vfname=voutgrd, outgrid=outgrid) |
Collapse file Copy file name to clipboardExpand all lines: pygmt/tests/test_binstats.py +24-2Lines changed: 24 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -20,7 +20,7 @@ def test_binstats_outgrid(): | 20 | 20 | data="@capitals.gmt", | 21 | 21 | outgrid=tmpfile.name, | 22 | 22 | spacing=5, | 23 | | - statistic="z", | | 23 | + statistic="sum", | 24 | 24 | search_radius="1000k", | 25 | 25 | aspatial="2=population", | 26 | 26 | region="g", | @@ -37,7 +37,7 @@ def test_binstats_no_outgrid(): | 37 | 37 | temp_grid = binstats( | 38 | 38 | data="@capitals.gmt", | 39 | 39 | spacing=5, | 40 | | - statistic="z", | | 40 | + statistic="sum", | 41 | 41 | search_radius="1000k", | 42 | 42 | aspatial="2=population", | 43 | 43 | region="g", | @@ -49,3 +49,25 @@ def test_binstats_no_outgrid(): | 49 | 49 | npt.assert_allclose(temp_grid.min(), 53) | 50 | 50 | npt.assert_allclose(temp_grid.median(), 1232714.5) | 51 | 51 | npt.assert_allclose(temp_grid.mean(), 4227489) | | 52 | + | | 53 | + | | 54 | +def test_binstats_quantile(): | | 55 | + """ | | 56 | + Test binstats quantile statistic functionality. | | 57 | + """ | | 58 | + temp_grid = binstats( | | 59 | + data="@capitals.gmt", | | 60 | + spacing=5, | | 61 | + statistic="quantile", | | 62 | + quantile_value=75, | | 63 | + search_radius="1000k", | | 64 | + aspatial="2=population", | | 65 | + region="g", | | 66 | + ) | | 67 | + assert temp_grid.dims == ("y", "x") | | 68 | + assert temp_grid.gmt.gtype is GridType.CARTESIAN | | 69 | + assert temp_grid.gmt.registration is GridRegistration.GRIDLINE | | 70 | + npt.assert_allclose(temp_grid.max(), 15047685) | | 71 | + npt.assert_allclose(temp_grid.min(), 53) | | 72 | + npt.assert_allclose(temp_grid.median(), 543664.5) | | 73 | + npt.assert_allclose(temp_grid.mean(), 1661363.6) |
|