diff --git a/lib/iris/tests/unit/analysis/test_PERCENTILE.py b/lib/iris/tests/unit/analysis/test_PERCENTILE.py index 1e4ba3af0f..d56d1ffb1f 100644 --- a/lib/iris/tests/unit/analysis/test_PERCENTILE.py +++ b/lib/iris/tests/unit/analysis/test_PERCENTILE.py @@ -11,6 +11,7 @@ from unittest import mock +import dask.array as da import numpy as np import numpy.ma as ma @@ -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. """ @@ -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): @@ -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: """ @@ -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."""