Skip to content
Merged
4 changes: 4 additions & 0 deletions pygmt/src/binstats.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from pygmt._typing import PathLike, TableLike
from pygmt.alias import Alias, AliasSystem
from pygmt.clib import Session
from pygmt.exceptions import GMTInvalidInput
from pygmt.helpers import build_arg_list, fmt_docstring, use_alias


Expand Down Expand Up @@ -166,6 +167,9 @@ def binstats(
)
aliasdict.merge(kwargs)
if statistic == "quantile":
if not (isinstance(quantile_value, (int, float)) and (0 <= quantile_value <= 100):
msg = "quantile_value must be a value between 0 and 100."
raise GMTInvalidInput(msg)
aliasdict["C"] += f"{quantile_value}"

with Session() as lib:
Expand Down
35 changes: 34 additions & 1 deletion pygmt/tests/test_binstats.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import pytest
from pygmt import binstats
from pygmt.enums import GridRegistration, GridType
from pygmt.exceptions import GMTInvalidInput
from pygmt.helpers import GMTTempFile


Expand Down Expand Up @@ -51,6 +52,38 @@ def test_binstats_no_outgrid():
npt.assert_allclose(temp_grid.mean(), 4227489)


def test_binstats_quantile_range_validation():
Comment thread
seisman marked this conversation as resolved.
Outdated
"""
Tests the input validation that quantile is between 0 and 100.
"""
with pytest.raises(GMTInvalidInput):
binstats(
data="@capitals.gmt",
spacing=5,
statistic="quantile",
quantile_value=175,
search_radius="1000k",
aspatial="2=population",
region="g",
)


def test_binstats_quantile_int_validation():
"""
Tests the input validation that quantile has a numeric value.
"""
with pytest.raises(GMTInvalidInput):
binstats(
data="@capitals.gmt",
spacing=5,
statistic="quantile",
quantile_value="test",
search_radius="1000k",
aspatial="2=population",
region="g",
)


def test_binstats_quantile():
"""
Test binstats quantile statistic functionality.
Expand All @@ -59,7 +92,7 @@ def test_binstats_quantile():
data="@capitals.gmt",
spacing=5,
statistic="quantile",
quantile_value=75,
quantile_value="75",
Comment thread
willschlitzer marked this conversation as resolved.
Outdated
search_radius="1000k",
aspatial="2=population",
region="g",
Expand Down
Loading