Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
16 changes: 14 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", None)
if fast_percentile_method:
if kwargs.pop("error_on_masked", False):
msg = (
Expand All @@ -1309,7 +1313,12 @@ 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)
pc_kwargs = dict(axis=-1)
if method is not None:
pc_kwargs["method"] = method

result = np.percentile(data, percent, **pc_kwargs)

result = result.T
else:
quantiles = percent / 100.0
Expand Down Expand Up @@ -1346,7 +1355,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 +2002,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, optional):
Passed to :func:`numpy.percentile`, if fast_percentile_method is True.
Otherwise ignored.

**For example**:

Expand Down
43 changes: 43 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,26 @@ 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()

# Check that we left "method" keyword to numpy's default.
self.assertNotIn("method", mocked_percentile.call_args.kwargs)

@mock.patch("numpy.percentile")
def test_chosen_kwarg_passed(self, mocked_percentile):
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 +356,29 @@ def test_numpy_percentile_called(self, mocked_percentile):
as_concrete_data(result)
mocked_percentile.assert_called()

# Check we have left "method" keyword to numpy's default.
self.assertNotIn("method", mocked_percentile.call_args.kwargs)

@mock.patch("numpy.percentile")
def test_chosen_method_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