Skip to content

Update percentile tests #4684

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 11, 2022
Merged
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
51 changes: 42 additions & 9 deletions lib/iris/tests/unit/analysis/test_PERCENTILE.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

from unittest import mock

import dask.array as da
import numpy as np
import numpy.ma as ma

Expand Down Expand Up @@ -88,11 +89,12 @@ def test_2d_multi(self):
self.check_percentile_calc(data, axis, percent, expected, approx=True)


class MaskedAggregateMixin:
class ScipyAggregateMixin:
"""
Tests for calculations on masked data. Will only work if using the standard
(scipy) method. Needs to be used with AggregateMixin, as these tests re-use its
method.
Tests for calculations specific to the default (scipy) function. Includes
tests on masked data and tests to verify that the function is called with
the expected keywords. Needs to be used with AggregateMixin, as some of
these tests re-use its method.

"""

Expand Down Expand Up @@ -147,28 +149,40 @@ def test_masked_2d_multi(self):
data, axis, percent, expected, mdtol=mdtol, approx=True
)

@mock.patch("scipy.stats.mstats.mquantiles")
@mock.patch("scipy.stats.mstats.mquantiles", return_value=[2, 4])
def test_default_kwargs_passed(self, mocked_mquantiles):
data = np.arange(5)
percent = 50
percent = [42, 75]
axis = 0
if self.lazy:
data = as_lazy_data(data)

self.agg_method(data, axis=axis, percent=percent)

# Trigger calculation for lazy case.
as_concrete_data(data)
for key in ["alphap", "betap"]:
self.assertEqual(mocked_mquantiles.call_args.kwargs[key], 1)

@mock.patch("scipy.stats.mstats.mquantiles")
def test_chosen_kwargs_passed(self, mocked_mquantiles):
data = np.arange(5)
percent = 50
percent = [42, 75]
axis = 0
if self.lazy:
data = as_lazy_data(data)

self.agg_method(
data, axis=axis, percent=percent, alphap=0.6, betap=0.5
)

# Trigger calculation for lazy case.
as_concrete_data(data)
for key, val in zip(["alphap", "betap"], [0.6, 0.5]):
self.assertEqual(mocked_mquantiles.call_args.kwargs[key], val)


class Test_aggregate(tests.IrisTest, AggregateMixin, MaskedAggregateMixin):
class Test_aggregate(tests.IrisTest, AggregateMixin, ScipyAggregateMixin):
"""Tests for standard aggregation method on real data."""

def setUp(self):
Expand Down Expand Up @@ -200,6 +214,13 @@ def test_masked(self):
data, axis=0, percent=50, fast_percentile_method=True
)

@mock.patch("numpy.percentile")
def test_numpy_percentile_called(self, mocked_percentile):
# Basic check that numpy.percentile is called.
data = np.arange(5)
self.agg_method(data, axis=0, percent=42, fast_percentile_method=True)
mocked_percentile.assert_called_once()


class MultiAxisMixin:
"""
Expand Down Expand Up @@ -269,9 +290,21 @@ def test_masked(self):
with self.assertRaisesRegex(TypeError, emsg):
as_concrete_data(actual)

@mock.patch("numpy.percentile", return_value=np.array([2, 4]))
def test_numpy_percentile_called(self, mocked_percentile):
# Basic check that numpy.percentile is called.
data = da.arange(5)
result = self.agg_method(
data, axis=0, percent=[42, 75], fast_percentile_method=True
)

self.assertTrue(is_lazy_data(result))
as_concrete_data(result)
mocked_percentile.assert_called()


class Test_lazy_aggregate(
tests.IrisTest, AggregateMixin, MaskedAggregateMixin, MultiAxisMixin
tests.IrisTest, AggregateMixin, ScipyAggregateMixin, MultiAxisMixin
):
"""Tests for standard aggregation on lazy data."""

Expand Down