Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 9 additions & 2 deletions lib/iris/analysis/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1296,6 +1296,10 @@ def _calc_percentile(data, percent, fast_percentile_method=False, **kwargs):
Calculate percentiles along the trailing axis of a 1D or 2D array.

"""
# Get hold of method keyword to pass it to numpy.percentile, or just remove it
# from kwargs so it's ignored for scipy mquantiles. Note that kwargs always
# contains alphap and betap so we can't pass it to numpy.percentile.
method = kwargs.pop("method", "linear")
Comment thread
wjbenfold marked this conversation as resolved.
Outdated
if fast_percentile_method:
if kwargs.pop("error_on_masked", False):
msg = (
Expand All @@ -1309,7 +1313,7 @@ def _calc_percentile(data, percent, fast_percentile_method=False, **kwargs):
"ignore",
"Warning: 'partition' will ignore the 'mask' of the MaskedArray.",
)
result = np.percentile(data, percent, axis=-1)
result = np.percentile(data, percent, axis=-1, method=method)
result = result.T
else:
quantiles = percent / 100.0
Expand Down Expand Up @@ -1346,7 +1350,7 @@ def _percentile(data, percent, fast_percentile_method=False, **kwargs):

**kwargs
passed to scipy.stats.mstats.mquantiles if fast_percentile_method is
False
False. Otherwise "method" is extracted and passed to numpy.percentile.

"""
if not isinstance(percent, Iterable):
Expand Down Expand Up @@ -1993,6 +1997,9 @@ def interp_order(length):
betap are ignored. An exception is raised if the data are masked and the
missing data tolerance is not 0.
Defaults to False.
* method (str):
Passed to :func:`numpy.percentile`, if fast_percentile_method is True.
Otherwise ignored. Defaults to "linear".

**For example**:

Expand Down
37 changes: 37 additions & 0 deletions lib/iris/tests/unit/analysis/test_PERCENTILE.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,23 @@ def test_numpy_percentile_called(self, mocked_percentile):
self.agg_method(data, axis=0, percent=42, fast_percentile_method=True)
mocked_percentile.assert_called_once()

@mock.patch("numpy.percentile")
def test_chosen_kwarg_passed(self, mocked_percentile):
Comment thread
wjbenfold marked this conversation as resolved.
data = np.arange(5)
percent = [42, 75]
axis = 0

self.agg_method(
data,
axis=axis,
percent=percent,
fast_percentile_method=True,
method="nearest",
)
self.assertEqual(
mocked_percentile.call_args.kwargs["method"], "nearest"
)


class MultiAxisMixin:
"""
Expand Down Expand Up @@ -336,6 +353,26 @@ def test_numpy_percentile_called(self, mocked_percentile):
as_concrete_data(result)
mocked_percentile.assert_called()

@mock.patch("numpy.percentile")
def test_chosen_kwarg_passed(self, mocked_percentile):
data = da.arange(5)
percent = [42, 75]
axis = 0

result = self.agg_method(
data,
axis=axis,
percent=percent,
fast_percentile_method=True,
method="nearest",
)

self.assertTrue(is_lazy_data(result))
as_concrete_data(result)
self.assertEqual(
mocked_percentile.call_args.kwargs["method"], "nearest"
)


class Test_lazy_aggregate(
tests.IrisTest, AggregateMixin, ScipyAggregateMixin, MultiAxisMixin
Expand Down