From 712e44a28074d412cd45a6342d517f5a078db932 Mon Sep 17 00:00:00 2001 From: Ruth Comer Date: Fri, 8 Apr 2022 15:14:48 +0100 Subject: [PATCH 1/2] update percentile tests --- .../tests/unit/analysis/test_PERCENTILE.py | 38 +++++++++++++++++-- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/lib/iris/tests/unit/analysis/test_PERCENTILE.py b/lib/iris/tests/unit/analysis/test_PERCENTILE.py index 1e4ba3af0f..561e310e0d 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 @@ -147,23 +148,35 @@ 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) @@ -200,6 +213,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,6 +289,18 @@ 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 From 2f01ff2d27be8e34f13db3e773cec348b188ab82 Mon Sep 17 00:00:00 2001 From: Ruth Comer Date: Mon, 11 Apr 2022 13:21:42 +0100 Subject: [PATCH 2/2] review action --- lib/iris/tests/unit/analysis/test_PERCENTILE.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/lib/iris/tests/unit/analysis/test_PERCENTILE.py b/lib/iris/tests/unit/analysis/test_PERCENTILE.py index 561e310e0d..d56d1ffb1f 100644 --- a/lib/iris/tests/unit/analysis/test_PERCENTILE.py +++ b/lib/iris/tests/unit/analysis/test_PERCENTILE.py @@ -89,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. """ @@ -181,7 +182,7 @@ def test_chosen_kwargs_passed(self, mocked_mquantiles): 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): @@ -303,7 +304,7 @@ def test_numpy_percentile_called(self, mocked_percentile): class Test_lazy_aggregate( - tests.IrisTest, AggregateMixin, MaskedAggregateMixin, MultiAxisMixin + tests.IrisTest, AggregateMixin, ScipyAggregateMixin, MultiAxisMixin ): """Tests for standard aggregation on lazy data."""