Skip to content

Commit 87736a0

Browse files
committed
pygmt.binstats: Make the 'statistic' parameter more Pythonic
1 parent 01e25f2 commit 87736a0

File tree

2 files changed

+42
-6
lines changed

2 files changed

+42
-6
lines changed

pygmt/src/binstats.py

+40-4
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
binstats - Bin spatial data and determine statistics per bin
33
"""
44

5+
from pygmt.alias import Alias, AliasSystem
56
from pygmt.clib import Session
67
from pygmt.helpers import build_arg_list, fmt_docstring, kwargs_to_strings, use_alias
78

89

910
@fmt_docstring
1011
@use_alias(
11-
C="statistic",
1212
E="empty",
1313
I="spacing",
1414
N="normalize",
@@ -23,7 +23,13 @@
2323
r="registration",
2424
)
2525
@kwargs_to_strings(I="sequence", R="sequence", i="sequence_comma")
26-
def binstats(data, outgrid: str | None = None, **kwargs):
26+
def binstats(
27+
data,
28+
outgrid: str | None = None,
29+
statistic=None,
30+
quantile_value=50,
31+
**kwargs, # noqa: ARG001
32+
):
2733
r"""
2834
Bin spatial data and determine statistics per bin.
2935
@@ -69,6 +75,8 @@ def binstats(data, outgrid: str | None = None, **kwargs):
6975
- **u** for maximum (upper)
7076
- **U** for maximum of negative values only
7177
- **z** for the sum
78+
quantile_value : float
79+
The quantile value if ``statistic="quantile".
7280
empty : float
7381
Set the value assigned to empty nodes [Default is NaN].
7482
normalize : bool
@@ -102,13 +110,41 @@ def binstats(data, outgrid: str | None = None, **kwargs):
102110
- None if ``outgrid`` is set (grid output will be stored in file set by
103111
``outgrid``)
104112
"""
113+
alias = AliasSystem(
114+
C=Alias(
115+
"statistic",
116+
mapping={
117+
"mean": "a",
118+
"mad": "d",
119+
"full": "g",
120+
"interquartile": "i",
121+
"min": "l",
122+
"minpos": "L",
123+
"median": "m",
124+
"number": "n",
125+
"lms": "o",
126+
"mode": "p",
127+
"quantile": "q",
128+
"rms": "r",
129+
"stddev": "s",
130+
"max": "u",
131+
"maxneg": "U",
132+
"sum": "z",
133+
},
134+
),
135+
G="outgrid",
136+
)
137+
if statistic == "quantile":
138+
statistic += str(quantile_value)
139+
140+
kwdict = alias.kwdict
105141
with Session() as lib:
106142
with (
107143
lib.virtualfile_in(check_kind="vector", data=data) as vintbl,
108144
lib.virtualfile_out(kind="grid", fname=outgrid) as voutgrd,
109145
):
110-
kwargs["G"] = voutgrd
146+
kwdict["G"] = voutgrd
111147
lib.call_module(
112-
module="binstats", args=build_arg_list(kwargs, infile=vintbl)
148+
module="binstats", args=build_arg_list(kwdict, infile=vintbl)
113149
)
114150
return lib.virtualfile_to_raster(vfname=voutgrd, outgrid=outgrid)

pygmt/tests/test_binstats.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def test_binstats_outgrid():
1919
data="@capitals.gmt",
2020
outgrid=tmpfile.name,
2121
spacing=5,
22-
statistic="z",
22+
statistic="sum",
2323
search_radius="1000k",
2424
aspatial="2=population",
2525
region="g",
@@ -36,7 +36,7 @@ def test_binstats_no_outgrid():
3636
temp_grid = binstats(
3737
data="@capitals.gmt",
3838
spacing=5,
39-
statistic="z",
39+
statistic="sum",
4040
search_radius="1000k",
4141
aspatial="2=population",
4242
region="g",

0 commit comments

Comments
 (0)