Skip to content

Commit 1a2d806

Browse files
pygmt.binstats: Validate the quantile_value parameter (#4332)
Co-authored-by: Dongdong Tian <seisman.info@gmail.com>
1 parent 7ca876d commit 1a2d806

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

pygmt/src/binstats.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from pygmt._typing import PathLike, TableLike
1010
from pygmt.alias import Alias, AliasSystem
1111
from pygmt.clib import Session
12+
from pygmt.exceptions import GMTTypeError, GMTValueError
1213
from pygmt.helpers import build_arg_list, fmt_docstring, use_alias
1314

1415

@@ -166,6 +167,16 @@ def binstats(
166167
)
167168
aliasdict.merge(kwargs)
168169
if statistic == "quantile":
170+
if not isinstance(quantile_value, (int, float)):
171+
raise GMTTypeError(
172+
quantile_value, reason="quantile_value must be an 'int' or 'float'."
173+
)
174+
if not (0 <= quantile_value <= 100):
175+
raise GMTValueError(
176+
quantile_value,
177+
description="quantile_value",
178+
reason="Must be a value between 0 and 100.",
179+
)
169180
aliasdict["C"] += f"{quantile_value}"
170181

171182
with Session() as lib:

pygmt/tests/test_binstats.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import pytest
99
from pygmt import binstats
1010
from pygmt.enums import GridRegistration, GridType
11+
from pygmt.exceptions import GMTTypeError, GMTValueError
1112
from pygmt.helpers import GMTTempFile
1213

1314

@@ -51,6 +52,24 @@ def test_binstats_no_outgrid():
5152
npt.assert_allclose(temp_grid.mean(), 4227489)
5253

5354

55+
def test_binstats_invalid_quantile_value():
56+
"""
57+
Tests the input validation for quantile_value.
58+
"""
59+
kwargs = {
60+
"data": "@capitals.gmt",
61+
"spacing": 5,
62+
"statistic": "quantile",
63+
"search_radius": "1000k",
64+
"aspatial": "2=population",
65+
"region": "g",
66+
}
67+
with pytest.raises(GMTValueError):
68+
binstats(quantile_value=175, **kwargs)
69+
with pytest.raises(GMTTypeError):
70+
binstats(quantile_value="invalid", **kwargs)
71+
72+
5473
def test_binstats_quantile():
5574
"""
5675
Test binstats quantile statistic functionality.

0 commit comments

Comments
 (0)