Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion 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,7 +167,15 @@ def binstats(
)
aliasdict.merge(kwargs)
if statistic == "quantile":
aliasdict["C"] += f"{quantile_value}"
quantile_error_message = "quantile_value must be a value between 0 and 100."
try:
quantile_value_float = float(quantile_value)
except (ValueError, TypeError):
raise GMTInvalidInput(quantile_error_message) from None
if 0 <= quantile_value_float <= 100:
aliasdict["C"] += f"{quantile_value_float}"
else:
raise GMTInvalidInput(quantile_error_message) from None
Comment on lines +170 to +178
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should support a string value like quantile_value="75".

Comment on lines +170 to +178
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
quantile_error_message = "quantile_value must be a value between 0 and 100."
try:
quantile_value_float = float(quantile_value)
except (ValueError, TypeError):
raise GMTInvalidInput(quantile_error_message) from None
if 0 <= quantile_value_float <= 100:
aliasdict["C"] += f"{quantile_value_float}"
else:
raise GMTInvalidInput(quantile_error_message) from None
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:
with (
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():
"""
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",
search_radius="1000k",
aspatial="2=population",
region="g",
Expand Down
Loading