Skip to content

Commit 7f190d8

Browse files
committed
pygmt.binstats: Make the 'statistic' parameter more Pythonic
1 parent 170f82e commit 7f190d8

File tree

2 files changed

+82
-28
lines changed

2 files changed

+82
-28
lines changed

pygmt/src/binstats.py

Lines changed: 80 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@
22
binstats - Bin spatial data and determine statistics per bin.
33
"""
44

5+
from typing import Literal
6+
57
import xarray as xr
68
from pygmt._typing import PathLike, TableLike
9+
from pygmt.alias import Alias, AliasSystem
710
from pygmt.clib import Session
811
from pygmt.helpers import build_arg_list, fmt_docstring, kwargs_to_strings, use_alias
912

1013

1114
@fmt_docstring
1215
@use_alias(
13-
C="statistic",
1416
E="empty",
1517
I="spacing",
1618
N="normalize",
@@ -26,7 +28,28 @@
2628
)
2729
@kwargs_to_strings(I="sequence", R="sequence", i="sequence_comma")
2830
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,
3053
) -> xr.DataArray | None:
3154
r"""
3255
Bin spatial data and determine statistics per bin.
@@ -48,29 +71,29 @@ def binstats(
4871
data
4972
A file name of an ASCII data table or a 2-D {table-classes}.
5073
{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:
74+
statistic
75+
Choose the statistic that will be computed per node based on the points that are
76+
within *radius* distance of the node. Select one of:
5777
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
78+
- **mean**: Compute the mean value (average).
79+
- **mad**: Compute the median absolute deviation (MAD).
80+
- **full**: Compute the full (max-min) range.
81+
- **interquartile**: Compute the 25-75% interquartile range.
82+
- **min**: Compute the minimum value.
83+
- **minpos**: Compute the minimum of positive values only.
84+
- **median**: Compute the median value.
85+
- **number**: Compute the number of values.
86+
- **lms**: Compute the LMS scale.
87+
- **mode**: Compute the mode (maximum likelihood).
88+
- **quantile**: Compute the selected quantile. The quantile value is in 0-100%
89+
range and is specified by the ``quantile_value`` parameter.
90+
- **rms**: Compute the root mean square (RMS).
91+
- **stddev**: Compute the standard deviation.
92+
- **max**: Compute the maximum value.
93+
- **maxneg**: Compute the maximum of negative values only.
94+
- **sum**: Compute the sum of values.
95+
quantile_value
96+
The quantile value if ``statistic="quantile"``.
7497
empty : float
7598
Set the value assigned to empty nodes [Default is NaN].
7699
normalize : bool
@@ -104,13 +127,44 @@ def binstats(
104127
- ``None`` if ``outgrid`` is set (grid output will be stored in the file set by
105128
``outgrid``)
106129
"""
130+
aliasdict = AliasSystem(
131+
C=[
132+
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+
Alias(
155+
quantile_value if statistic == "quantile" else None,
156+
name="quantile_value",
157+
),
158+
]
159+
).merge(kwargs)
160+
107161
with Session() as lib:
108162
with (
109163
lib.virtualfile_in(check_kind="vector", data=data) as vintbl,
110164
lib.virtualfile_out(kind="grid", fname=outgrid) as voutgrd,
111165
):
112-
kwargs["G"] = voutgrd
166+
aliasdict["G"] = voutgrd
113167
lib.call_module(
114-
module="binstats", args=build_arg_list(kwargs, infile=vintbl)
168+
module="binstats", args=build_arg_list(aliasdict, infile=vintbl)
115169
)
116170
return lib.virtualfile_to_raster(vfname=voutgrd, outgrid=outgrid)

pygmt/tests/test_binstats.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def test_binstats_outgrid():
2020
data="@capitals.gmt",
2121
outgrid=tmpfile.name,
2222
spacing=5,
23-
statistic="z",
23+
statistic="sum",
2424
search_radius="1000k",
2525
aspatial="2=population",
2626
region="g",
@@ -37,7 +37,7 @@ def test_binstats_no_outgrid():
3737
temp_grid = binstats(
3838
data="@capitals.gmt",
3939
spacing=5,
40-
statistic="z",
40+
statistic="sum",
4141
search_radius="1000k",
4242
aspatial="2=population",
4343
region="g",

0 commit comments

Comments
 (0)