GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — pygmt.binstats: Let the parameter 'statistic' support long-form argum… · GenericMappingTools/pygmt@6df2d83

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • pygmt
    • src
      • binstats.py
    • tests
      • test_binstats.py
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎pygmt/src/binstats.py‎

Copy file name to clipboardExpand all lines: pygmt/src/binstats.py
+76-26Lines changed: 76 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,40 @@ 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=Alias(
132+
statistic,
133+
name="statistic",
134+
mapping={
135+
"mean": "a",
136+
"mad": "d",
137+
"full": "g",
138+
"interquartile": "i",
139+
"min": "l",
140+
"minpos": "L",
141+
"median": "m",
142+
"number": "n",
143+
"lms": "o",
144+
"mode": "p",
145+
"quantile": "q",
146+
"rms": "r",
147+
"stddev": "s",
148+
"max": "u",
149+
"maxneg": "U",
150+
"sum": "z",
151+
},
152+
),
153+
).merge(kwargs)
154+
if statistic == "quantile":
155+
aliasdict["C"] += f"{quantile_value}"
156+
107157
with Session() as lib:
108158
with (
109159
lib.virtualfile_in(check_kind="vector", data=data) as vintbl,
110160
lib.virtualfile_out(kind="grid", fname=outgrid) as voutgrd,
111161
):
112-
kwargs["G"] = voutgrd
162+
aliasdict["G"] = voutgrd
113163
lib.call_module(
114-
module="binstats", args=build_arg_list(kwargs, infile=vintbl)
164+
module="binstats", args=build_arg_list(aliasdict, infile=vintbl)
115165
)
116166
return lib.virtualfile_to_raster(vfname=voutgrd, outgrid=outgrid)
Collapse file

‎pygmt/tests/test_binstats.py‎

Copy file name to clipboardExpand all lines: pygmt/tests/test_binstats.py
+2-2Lines 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 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.