Skip to content

Commit 46a7cff

Browse files
committed
update percentile tests
1 parent 591c9f4 commit 46a7cff

File tree

1 file changed

+36
-4
lines changed

1 file changed

+36
-4
lines changed

lib/iris/tests/unit/analysis/test_PERCENTILE.py

+36-4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
from unittest import mock
1313

14+
import dask.array as da
1415
import numpy as np
1516
import numpy.ma as ma
1617

@@ -147,23 +148,35 @@ def test_masked_2d_multi(self):
147148
data, axis, percent, expected, mdtol=mdtol, approx=True
148149
)
149150

150-
@mock.patch("scipy.stats.mstats.mquantiles")
151+
@mock.patch("scipy.stats.mstats.mquantiles", return_value=[2, 4])
151152
def test_default_kwargs_passed(self, mocked_mquantiles):
152153
data = np.arange(5)
153-
percent = 50
154+
percent = [42, 75]
154155
axis = 0
156+
if self.lazy:
157+
data = as_lazy_data(data)
158+
155159
self.agg_method(data, axis=axis, percent=percent)
160+
161+
# Trigger calculation for lazy case.
162+
as_concrete_data(data)
156163
for key in ["alphap", "betap"]:
157164
self.assertEqual(mocked_mquantiles.call_args.kwargs[key], 1)
158165

159166
@mock.patch("scipy.stats.mstats.mquantiles")
160167
def test_chosen_kwargs_passed(self, mocked_mquantiles):
161168
data = np.arange(5)
162-
percent = 50
169+
percent = [42, 75]
163170
axis = 0
171+
if self.lazy:
172+
data = as_lazy_data(data)
173+
164174
self.agg_method(
165175
data, axis=axis, percent=percent, alphap=0.6, betap=0.5
166176
)
177+
178+
# Trigger calculation for lazy case.
179+
as_concrete_data(data)
167180
for key, val in zip(["alphap", "betap"], [0.6, 0.5]):
168181
self.assertEqual(mocked_mquantiles.call_args.kwargs[key], val)
169182

@@ -200,11 +213,18 @@ def test_masked(self):
200213
data, axis=0, percent=50, fast_percentile_method=True
201214
)
202215

216+
@mock.patch("numpy.percentile")
217+
def test_numpy_percentile_called(self, mocked_percentile):
218+
# Basic check that numpy.percentile is called.
219+
data = np.arange(5)
220+
self.agg_method(data, axis=0, percent=42, fast_percentile_method=True)
221+
mocked_percentile.assert_called_once()
222+
203223

204224
class MultiAxisMixin:
205225
"""
206226
Tests for axis passed as a tuple. Only relevant for lazy aggregation since
207-
axis is always specified as int for real aggregation.
227+
axis is always specified asax int for real aggregation.
208228
209229
"""
210230

@@ -269,6 +289,18 @@ def test_masked(self):
269289
with self.assertRaisesRegex(TypeError, emsg):
270290
as_concrete_data(actual)
271291

292+
@mock.patch("numpy.percentile", return_value=np.array([2, 4]))
293+
def test_numpy_percentile_called(self, mocked_percentile):
294+
# Basic check that numpy.percentile is called.
295+
data = da.arange(5)
296+
result = self.agg_method(
297+
data, axis=0, percent=[42, 75], fast_percentile_method=True
298+
)
299+
300+
self.assertTrue(is_lazy_data(result))
301+
as_concrete_data(result)
302+
mocked_percentile.assert_called()
303+
272304

273305
class Test_lazy_aggregate(
274306
tests.IrisTest, AggregateMixin, MaskedAggregateMixin, MultiAxisMixin

0 commit comments

Comments
 (0)