comparison DEPENDENCIES/mingw32/Python27/Lib/site-packages/numpy/lib/tests/test_nanfunctions.py @ 87:2a2c65a20a8b

Add Python libs and headers
author Chris Cannam
date Wed, 25 Feb 2015 14:05:22 +0000
parents
children
comparison
equal deleted inserted replaced
86:413a9d26189e 87:2a2c65a20a8b
1 from __future__ import division, absolute_import, print_function
2
3 import warnings
4
5 import numpy as np
6 from numpy.testing import (
7 run_module_suite, TestCase, assert_, assert_equal, assert_almost_equal,
8 assert_raises, assert_array_equal
9 )
10
11
12 # Test data
13 _ndat = np.array([[0.6244, np.nan, 0.2692, 0.0116, np.nan, 0.1170],
14 [0.5351, -0.9403, np.nan, 0.2100, 0.4759, 0.2833],
15 [np.nan, np.nan, np.nan, 0.1042, np.nan, -0.5954],
16 [0.1610, np.nan, np.nan, 0.1859, 0.3146, np.nan]])
17
18
19 # Rows of _ndat with nans removed
20 _rdat = [np.array([0.6244, 0.2692, 0.0116, 0.1170]),
21 np.array([0.5351, -0.9403, 0.2100, 0.4759, 0.2833]),
22 np.array([0.1042, -0.5954]),
23 np.array([0.1610, 0.1859, 0.3146])]
24
25
26 class TestNanFunctions_MinMax(TestCase):
27
28 nanfuncs = [np.nanmin, np.nanmax]
29 stdfuncs = [np.min, np.max]
30
31 def test_mutation(self):
32 # Check that passed array is not modified.
33 ndat = _ndat.copy()
34 for f in self.nanfuncs:
35 f(ndat)
36 assert_equal(ndat, _ndat)
37
38 def test_keepdims(self):
39 mat = np.eye(3)
40 for nf, rf in zip(self.nanfuncs, self.stdfuncs):
41 for axis in [None, 0, 1]:
42 tgt = rf(mat, axis=axis, keepdims=True)
43 res = nf(mat, axis=axis, keepdims=True)
44 assert_(res.ndim == tgt.ndim)
45
46 def test_out(self):
47 mat = np.eye(3)
48 for nf, rf in zip(self.nanfuncs, self.stdfuncs):
49 resout = np.zeros(3)
50 tgt = rf(mat, axis=1)
51 res = nf(mat, axis=1, out=resout)
52 assert_almost_equal(res, resout)
53 assert_almost_equal(res, tgt)
54
55 def test_dtype_from_input(self):
56 codes = 'efdgFDG'
57 for nf, rf in zip(self.nanfuncs, self.stdfuncs):
58 for c in codes:
59 mat = np.eye(3, dtype=c)
60 tgt = rf(mat, axis=1).dtype.type
61 res = nf(mat, axis=1).dtype.type
62 assert_(res is tgt)
63 # scalar case
64 tgt = rf(mat, axis=None).dtype.type
65 res = nf(mat, axis=None).dtype.type
66 assert_(res is tgt)
67
68 def test_result_values(self):
69 for nf, rf in zip(self.nanfuncs, self.stdfuncs):
70 tgt = [rf(d) for d in _rdat]
71 res = nf(_ndat, axis=1)
72 assert_almost_equal(res, tgt)
73
74 def test_allnans(self):
75 mat = np.array([np.nan]*9).reshape(3, 3)
76 for f in self.nanfuncs:
77 for axis in [None, 0, 1]:
78 with warnings.catch_warnings(record=True) as w:
79 warnings.simplefilter('always')
80 assert_(np.isnan(f(mat, axis=axis)).all())
81 assert_(len(w) == 1, 'no warning raised')
82 assert_(issubclass(w[0].category, RuntimeWarning))
83 # Check scalars
84 with warnings.catch_warnings(record=True) as w:
85 warnings.simplefilter('always')
86 assert_(np.isnan(f(np.nan)))
87 assert_(len(w) == 1, 'no warning raised')
88 assert_(issubclass(w[0].category, RuntimeWarning))
89
90 def test_masked(self):
91 mat = np.ma.fix_invalid(_ndat)
92 msk = mat._mask.copy()
93 for f in [np.nanmin]:
94 res = f(mat, axis=1)
95 tgt = f(_ndat, axis=1)
96 assert_equal(res, tgt)
97 assert_equal(mat._mask, msk)
98 assert_(not np.isinf(mat).any())
99
100 def test_scalar(self):
101 for f in self.nanfuncs:
102 assert_(f(0.) == 0.)
103
104 def test_matrices(self):
105 # Check that it works and that type and
106 # shape are preserved
107 mat = np.matrix(np.eye(3))
108 for f in self.nanfuncs:
109 res = f(mat, axis=0)
110 assert_(isinstance(res, np.matrix))
111 assert_(res.shape == (1, 3))
112 res = f(mat, axis=1)
113 assert_(isinstance(res, np.matrix))
114 assert_(res.shape == (3, 1))
115 res = f(mat)
116 assert_(np.isscalar(res))
117 # check that rows of nan are dealt with for subclasses (#4628)
118 mat[1] = np.nan
119 for f in self.nanfuncs:
120 with warnings.catch_warnings(record=True) as w:
121 warnings.simplefilter('always')
122 res = f(mat, axis=0)
123 assert_(isinstance(res, np.matrix))
124 assert_(not np.any(np.isnan(res)))
125 assert_(len(w) == 0)
126
127 with warnings.catch_warnings(record=True) as w:
128 warnings.simplefilter('always')
129 res = f(mat, axis=1)
130 assert_(isinstance(res, np.matrix))
131 assert_(np.isnan(res[1, 0]) and not np.isnan(res[0, 0])
132 and not np.isnan(res[2, 0]))
133 assert_(len(w) == 1, 'no warning raised')
134 assert_(issubclass(w[0].category, RuntimeWarning))
135
136 with warnings.catch_warnings(record=True) as w:
137 warnings.simplefilter('always')
138 res = f(mat)
139 assert_(np.isscalar(res))
140 assert_(res != np.nan)
141 assert_(len(w) == 0)
142
143
144 class TestNanFunctions_ArgminArgmax(TestCase):
145
146 nanfuncs = [np.nanargmin, np.nanargmax]
147
148 def test_mutation(self):
149 # Check that passed array is not modified.
150 ndat = _ndat.copy()
151 for f in self.nanfuncs:
152 f(ndat)
153 assert_equal(ndat, _ndat)
154
155 def test_result_values(self):
156 for f, fcmp in zip(self.nanfuncs, [np.greater, np.less]):
157 for row in _ndat:
158 with warnings.catch_warnings(record=True):
159 warnings.simplefilter('always')
160 ind = f(row)
161 val = row[ind]
162 # comparing with NaN is tricky as the result
163 # is always false except for NaN != NaN
164 assert_(not np.isnan(val))
165 assert_(not fcmp(val, row).any())
166 assert_(not np.equal(val, row[:ind]).any())
167
168 def test_allnans(self):
169 mat = np.array([np.nan]*9).reshape(3, 3)
170 for f in self.nanfuncs:
171 for axis in [None, 0, 1]:
172 assert_raises(ValueError, f, mat, axis=axis)
173 assert_raises(ValueError, f, np.nan)
174
175 def test_empty(self):
176 mat = np.zeros((0, 3))
177 for f in self.nanfuncs:
178 for axis in [0, None]:
179 assert_raises(ValueError, f, mat, axis=axis)
180 for axis in [1]:
181 res = f(mat, axis=axis)
182 assert_equal(res, np.zeros(0))
183
184 def test_scalar(self):
185 for f in self.nanfuncs:
186 assert_(f(0.) == 0.)
187
188 def test_matrices(self):
189 # Check that it works and that type and
190 # shape are preserved
191 mat = np.matrix(np.eye(3))
192 for f in self.nanfuncs:
193 res = f(mat, axis=0)
194 assert_(isinstance(res, np.matrix))
195 assert_(res.shape == (1, 3))
196 res = f(mat, axis=1)
197 assert_(isinstance(res, np.matrix))
198 assert_(res.shape == (3, 1))
199 res = f(mat)
200 assert_(np.isscalar(res))
201
202
203 class TestNanFunctions_IntTypes(TestCase):
204
205 int_types = (np.int8, np.int16, np.int32, np.int64, np.uint8,
206 np.uint16, np.uint32, np.uint64)
207
208 mat = np.array([127, 39, 93, 87, 46])
209
210 def integer_arrays(self):
211 for dtype in self.int_types:
212 yield self.mat.astype(dtype)
213
214 def test_nanmin(self):
215 tgt = np.min(self.mat)
216 for mat in self.integer_arrays():
217 assert_equal(np.nanmin(mat), tgt)
218
219 def test_nanmax(self):
220 tgt = np.max(self.mat)
221 for mat in self.integer_arrays():
222 assert_equal(np.nanmax(mat), tgt)
223
224 def test_nanargmin(self):
225 tgt = np.argmin(self.mat)
226 for mat in self.integer_arrays():
227 assert_equal(np.nanargmin(mat), tgt)
228
229 def test_nanargmax(self):
230 tgt = np.argmax(self.mat)
231 for mat in self.integer_arrays():
232 assert_equal(np.nanargmax(mat), tgt)
233
234 def test_nansum(self):
235 tgt = np.sum(self.mat)
236 for mat in self.integer_arrays():
237 assert_equal(np.nansum(mat), tgt)
238
239 def test_nanmean(self):
240 tgt = np.mean(self.mat)
241 for mat in self.integer_arrays():
242 assert_equal(np.nanmean(mat), tgt)
243
244 def test_nanvar(self):
245 tgt = np.var(self.mat)
246 for mat in self.integer_arrays():
247 assert_equal(np.nanvar(mat), tgt)
248
249 tgt = np.var(mat, ddof=1)
250 for mat in self.integer_arrays():
251 assert_equal(np.nanvar(mat, ddof=1), tgt)
252
253 def test_nanstd(self):
254 tgt = np.std(self.mat)
255 for mat in self.integer_arrays():
256 assert_equal(np.nanstd(mat), tgt)
257
258 tgt = np.std(self.mat, ddof=1)
259 for mat in self.integer_arrays():
260 assert_equal(np.nanstd(mat, ddof=1), tgt)
261
262
263 class TestNanFunctions_Sum(TestCase):
264
265 def test_mutation(self):
266 # Check that passed array is not modified.
267 ndat = _ndat.copy()
268 np.nansum(ndat)
269 assert_equal(ndat, _ndat)
270
271 def test_keepdims(self):
272 mat = np.eye(3)
273 for axis in [None, 0, 1]:
274 tgt = np.sum(mat, axis=axis, keepdims=True)
275 res = np.nansum(mat, axis=axis, keepdims=True)
276 assert_(res.ndim == tgt.ndim)
277
278 def test_out(self):
279 mat = np.eye(3)
280 resout = np.zeros(3)
281 tgt = np.sum(mat, axis=1)
282 res = np.nansum(mat, axis=1, out=resout)
283 assert_almost_equal(res, resout)
284 assert_almost_equal(res, tgt)
285
286 def test_dtype_from_dtype(self):
287 mat = np.eye(3)
288 codes = 'efdgFDG'
289 for c in codes:
290 tgt = np.sum(mat, dtype=np.dtype(c), axis=1).dtype.type
291 res = np.nansum(mat, dtype=np.dtype(c), axis=1).dtype.type
292 assert_(res is tgt)
293 # scalar case
294 tgt = np.sum(mat, dtype=np.dtype(c), axis=None).dtype.type
295 res = np.nansum(mat, dtype=np.dtype(c), axis=None).dtype.type
296 assert_(res is tgt)
297
298 def test_dtype_from_char(self):
299 mat = np.eye(3)
300 codes = 'efdgFDG'
301 for c in codes:
302 tgt = np.sum(mat, dtype=c, axis=1).dtype.type
303 res = np.nansum(mat, dtype=c, axis=1).dtype.type
304 assert_(res is tgt)
305 # scalar case
306 tgt = np.sum(mat, dtype=c, axis=None).dtype.type
307 res = np.nansum(mat, dtype=c, axis=None).dtype.type
308 assert_(res is tgt)
309
310 def test_dtype_from_input(self):
311 codes = 'efdgFDG'
312 for c in codes:
313 mat = np.eye(3, dtype=c)
314 tgt = np.sum(mat, axis=1).dtype.type
315 res = np.nansum(mat, axis=1).dtype.type
316 assert_(res is tgt)
317 # scalar case
318 tgt = np.sum(mat, axis=None).dtype.type
319 res = np.nansum(mat, axis=None).dtype.type
320 assert_(res is tgt)
321
322 def test_result_values(self):
323 tgt = [np.sum(d) for d in _rdat]
324 res = np.nansum(_ndat, axis=1)
325 assert_almost_equal(res, tgt)
326
327 def test_allnans(self):
328 # Check for FutureWarning
329 with warnings.catch_warnings(record=True) as w:
330 warnings.simplefilter('always')
331 res = np.nansum([np.nan]*3, axis=None)
332 assert_(res == 0, 'result is not 0')
333 assert_(len(w) == 0, 'warning raised')
334 # Check scalar
335 res = np.nansum(np.nan)
336 assert_(res == 0, 'result is not 0')
337 assert_(len(w) == 0, 'warning raised')
338 # Check there is no warning for not all-nan
339 np.nansum([0]*3, axis=None)
340 assert_(len(w) == 0, 'unwanted warning raised')
341
342 def test_empty(self):
343 mat = np.zeros((0, 3))
344 tgt = [0]*3
345 res = np.nansum(mat, axis=0)
346 assert_equal(res, tgt)
347 tgt = []
348 res = np.nansum(mat, axis=1)
349 assert_equal(res, tgt)
350 tgt = 0
351 res = np.nansum(mat, axis=None)
352 assert_equal(res, tgt)
353
354 def test_scalar(self):
355 assert_(np.nansum(0.) == 0.)
356
357 def test_matrices(self):
358 # Check that it works and that type and
359 # shape are preserved
360 mat = np.matrix(np.eye(3))
361 res = np.nansum(mat, axis=0)
362 assert_(isinstance(res, np.matrix))
363 assert_(res.shape == (1, 3))
364 res = np.nansum(mat, axis=1)
365 assert_(isinstance(res, np.matrix))
366 assert_(res.shape == (3, 1))
367 res = np.nansum(mat)
368 assert_(np.isscalar(res))
369
370
371 class TestNanFunctions_MeanVarStd(TestCase):
372
373 nanfuncs = [np.nanmean, np.nanvar, np.nanstd]
374 stdfuncs = [np.mean, np.var, np.std]
375
376 def test_mutation(self):
377 # Check that passed array is not modified.
378 ndat = _ndat.copy()
379 for f in self.nanfuncs:
380 f(ndat)
381 assert_equal(ndat, _ndat)
382
383 def test_dtype_error(self):
384 for f in self.nanfuncs:
385 for dtype in [np.bool_, np.int_, np.object]:
386 assert_raises(TypeError, f, _ndat, axis=1, dtype=np.int)
387
388 def test_out_dtype_error(self):
389 for f in self.nanfuncs:
390 for dtype in [np.bool_, np.int_, np.object]:
391 out = np.empty(_ndat.shape[0], dtype=dtype)
392 assert_raises(TypeError, f, _ndat, axis=1, out=out)
393
394 def test_keepdims(self):
395 mat = np.eye(3)
396 for nf, rf in zip(self.nanfuncs, self.stdfuncs):
397 for axis in [None, 0, 1]:
398 tgt = rf(mat, axis=axis, keepdims=True)
399 res = nf(mat, axis=axis, keepdims=True)
400 assert_(res.ndim == tgt.ndim)
401
402 def test_out(self):
403 mat = np.eye(3)
404 for nf, rf in zip(self.nanfuncs, self.stdfuncs):
405 resout = np.zeros(3)
406 tgt = rf(mat, axis=1)
407 res = nf(mat, axis=1, out=resout)
408 assert_almost_equal(res, resout)
409 assert_almost_equal(res, tgt)
410
411 def test_dtype_from_dtype(self):
412 mat = np.eye(3)
413 codes = 'efdgFDG'
414 for nf, rf in zip(self.nanfuncs, self.stdfuncs):
415 for c in codes:
416 tgt = rf(mat, dtype=np.dtype(c), axis=1).dtype.type
417 res = nf(mat, dtype=np.dtype(c), axis=1).dtype.type
418 assert_(res is tgt)
419 # scalar case
420 tgt = rf(mat, dtype=np.dtype(c), axis=None).dtype.type
421 res = nf(mat, dtype=np.dtype(c), axis=None).dtype.type
422 assert_(res is tgt)
423
424 def test_dtype_from_char(self):
425 mat = np.eye(3)
426 codes = 'efdgFDG'
427 for nf, rf in zip(self.nanfuncs, self.stdfuncs):
428 for c in codes:
429 tgt = rf(mat, dtype=c, axis=1).dtype.type
430 res = nf(mat, dtype=c, axis=1).dtype.type
431 assert_(res is tgt)
432 # scalar case
433 tgt = rf(mat, dtype=c, axis=None).dtype.type
434 res = nf(mat, dtype=c, axis=None).dtype.type
435 assert_(res is tgt)
436
437 def test_dtype_from_input(self):
438 codes = 'efdgFDG'
439 for nf, rf in zip(self.nanfuncs, self.stdfuncs):
440 for c in codes:
441 mat = np.eye(3, dtype=c)
442 tgt = rf(mat, axis=1).dtype.type
443 res = nf(mat, axis=1).dtype.type
444 assert_(res is tgt, "res %s, tgt %s" % (res, tgt))
445 # scalar case
446 tgt = rf(mat, axis=None).dtype.type
447 res = nf(mat, axis=None).dtype.type
448 assert_(res is tgt)
449
450 def test_ddof(self):
451 nanfuncs = [np.nanvar, np.nanstd]
452 stdfuncs = [np.var, np.std]
453 for nf, rf in zip(nanfuncs, stdfuncs):
454 for ddof in [0, 1]:
455 tgt = [rf(d, ddof=ddof) for d in _rdat]
456 res = nf(_ndat, axis=1, ddof=ddof)
457 assert_almost_equal(res, tgt)
458
459 def test_ddof_too_big(self):
460 nanfuncs = [np.nanvar, np.nanstd]
461 stdfuncs = [np.var, np.std]
462 dsize = [len(d) for d in _rdat]
463 for nf, rf in zip(nanfuncs, stdfuncs):
464 for ddof in range(5):
465 with warnings.catch_warnings(record=True) as w:
466 warnings.simplefilter('always')
467 tgt = [ddof >= d for d in dsize]
468 res = nf(_ndat, axis=1, ddof=ddof)
469 assert_equal(np.isnan(res), tgt)
470 if any(tgt):
471 assert_(len(w) == 1)
472 assert_(issubclass(w[0].category, RuntimeWarning))
473 else:
474 assert_(len(w) == 0)
475
476 def test_result_values(self):
477 for nf, rf in zip(self.nanfuncs, self.stdfuncs):
478 tgt = [rf(d) for d in _rdat]
479 res = nf(_ndat, axis=1)
480 assert_almost_equal(res, tgt)
481
482 def test_allnans(self):
483 mat = np.array([np.nan]*9).reshape(3, 3)
484 for f in self.nanfuncs:
485 for axis in [None, 0, 1]:
486 with warnings.catch_warnings(record=True) as w:
487 warnings.simplefilter('always')
488 assert_(np.isnan(f(mat, axis=axis)).all())
489 assert_(len(w) == 1)
490 assert_(issubclass(w[0].category, RuntimeWarning))
491 # Check scalar
492 assert_(np.isnan(f(np.nan)))
493 assert_(len(w) == 2)
494 assert_(issubclass(w[0].category, RuntimeWarning))
495
496 def test_empty(self):
497 mat = np.zeros((0, 3))
498 for f in self.nanfuncs:
499 for axis in [0, None]:
500 with warnings.catch_warnings(record=True) as w:
501 warnings.simplefilter('always')
502 assert_(np.isnan(f(mat, axis=axis)).all())
503 assert_(len(w) == 1)
504 assert_(issubclass(w[0].category, RuntimeWarning))
505 for axis in [1]:
506 with warnings.catch_warnings(record=True) as w:
507 warnings.simplefilter('always')
508 assert_equal(f(mat, axis=axis), np.zeros([]))
509 assert_(len(w) == 0)
510
511 def test_scalar(self):
512 for f in self.nanfuncs:
513 assert_(f(0.) == 0.)
514
515 def test_matrices(self):
516 # Check that it works and that type and
517 # shape are preserved
518 mat = np.matrix(np.eye(3))
519 for f in self.nanfuncs:
520 res = f(mat, axis=0)
521 assert_(isinstance(res, np.matrix))
522 assert_(res.shape == (1, 3))
523 res = f(mat, axis=1)
524 assert_(isinstance(res, np.matrix))
525 assert_(res.shape == (3, 1))
526 res = f(mat)
527 assert_(np.isscalar(res))
528
529
530 class TestNanFunctions_Median(TestCase):
531
532 def test_mutation(self):
533 # Check that passed array is not modified.
534 ndat = _ndat.copy()
535 np.nanmedian(ndat)
536 assert_equal(ndat, _ndat)
537
538 def test_keepdims(self):
539 mat = np.eye(3)
540 for axis in [None, 0, 1]:
541 tgt = np.median(mat, axis=axis, out=None, overwrite_input=False)
542 res = np.nanmedian(mat, axis=axis, out=None, overwrite_input=False)
543 assert_(res.ndim == tgt.ndim)
544
545 d = np.ones((3, 5, 7, 11))
546 # Randomly set some elements to NaN:
547 w = np.random.random((4, 200)) * np.array(d.shape)[:, None]
548 w = w.astype(np.intp)
549 d[tuple(w)] = np.nan
550 with warnings.catch_warnings(record=True) as w:
551 warnings.simplefilter('always', RuntimeWarning)
552 res = np.nanmedian(d, axis=None, keepdims=True)
553 assert_equal(res.shape, (1, 1, 1, 1))
554 res = np.nanmedian(d, axis=(0, 1), keepdims=True)
555 assert_equal(res.shape, (1, 1, 7, 11))
556 res = np.nanmedian(d, axis=(0, 3), keepdims=True)
557 assert_equal(res.shape, (1, 5, 7, 1))
558 res = np.nanmedian(d, axis=(1,), keepdims=True)
559 assert_equal(res.shape, (3, 1, 7, 11))
560 res = np.nanmedian(d, axis=(0, 1, 2, 3), keepdims=True)
561 assert_equal(res.shape, (1, 1, 1, 1))
562 res = np.nanmedian(d, axis=(0, 1, 3), keepdims=True)
563 assert_equal(res.shape, (1, 1, 7, 1))
564
565 def test_out(self):
566 mat = np.random.rand(3, 3)
567 nan_mat = np.insert(mat, [0, 2], np.nan, axis=1)
568 resout = np.zeros(3)
569 tgt = np.median(mat, axis=1)
570 res = np.nanmedian(nan_mat, axis=1, out=resout)
571 assert_almost_equal(res, resout)
572 assert_almost_equal(res, tgt)
573 # 0-d output:
574 resout = np.zeros(())
575 tgt = np.median(mat, axis=None)
576 res = np.nanmedian(nan_mat, axis=None, out=resout)
577 assert_almost_equal(res, resout)
578 assert_almost_equal(res, tgt)
579 res = np.nanmedian(nan_mat, axis=(0, 1), out=resout)
580 assert_almost_equal(res, resout)
581 assert_almost_equal(res, tgt)
582
583 def test_small_large(self):
584 # test the small and large code paths, current cutoff 400 elements
585 for s in [5, 20, 51, 200, 1000]:
586 d = np.random.randn(4, s)
587 # Randomly set some elements to NaN:
588 w = np.random.randint(0, d.size, size=d.size // 5)
589 d.ravel()[w] = np.nan
590 d[:,0] = 1. # ensure at least one good value
591 # use normal median without nans to compare
592 tgt = []
593 for x in d:
594 nonan = np.compress(~np.isnan(x), x)
595 tgt.append(np.median(nonan, overwrite_input=True))
596
597 assert_array_equal(np.nanmedian(d, axis=-1), tgt)
598
599 def test_result_values(self):
600 tgt = [np.median(d) for d in _rdat]
601 res = np.nanmedian(_ndat, axis=1)
602 assert_almost_equal(res, tgt)
603
604 def test_allnans(self):
605 mat = np.array([np.nan]*9).reshape(3, 3)
606 for axis in [None, 0, 1]:
607 with warnings.catch_warnings(record=True) as w:
608 warnings.simplefilter('always')
609 assert_(np.isnan(np.nanmedian(mat, axis=axis)).all())
610 if axis is None:
611 assert_(len(w) == 1)
612 else:
613 assert_(len(w) == 3)
614 assert_(issubclass(w[0].category, RuntimeWarning))
615 # Check scalar
616 assert_(np.isnan(np.nanmedian(np.nan)))
617 if axis is None:
618 assert_(len(w) == 2)
619 else:
620 assert_(len(w) == 4)
621 assert_(issubclass(w[0].category, RuntimeWarning))
622
623 def test_empty(self):
624 mat = np.zeros((0, 3))
625 for axis in [0, None]:
626 with warnings.catch_warnings(record=True) as w:
627 warnings.simplefilter('always')
628 assert_(np.isnan(np.nanmedian(mat, axis=axis)).all())
629 assert_(len(w) == 1)
630 assert_(issubclass(w[0].category, RuntimeWarning))
631 for axis in [1]:
632 with warnings.catch_warnings(record=True) as w:
633 warnings.simplefilter('always')
634 assert_equal(np.nanmedian(mat, axis=axis), np.zeros([]))
635 assert_(len(w) == 0)
636
637 def test_scalar(self):
638 assert_(np.nanmedian(0.) == 0.)
639
640 def test_extended_axis_invalid(self):
641 d = np.ones((3, 5, 7, 11))
642 assert_raises(IndexError, np.nanmedian, d, axis=-5)
643 assert_raises(IndexError, np.nanmedian, d, axis=(0, -5))
644 assert_raises(IndexError, np.nanmedian, d, axis=4)
645 assert_raises(IndexError, np.nanmedian, d, axis=(0, 4))
646 assert_raises(ValueError, np.nanmedian, d, axis=(1, 1))
647
648 def test_float_special(self):
649 with warnings.catch_warnings(record=True):
650 warnings.simplefilter('ignore', RuntimeWarning)
651 a = np.array([[np.inf, np.nan], [np.nan, np.nan]])
652 assert_equal(np.nanmedian(a, axis=0), [np.inf, np.nan])
653 assert_equal(np.nanmedian(a, axis=1), [np.inf, np.nan])
654 assert_equal(np.nanmedian(a), np.inf)
655
656 # minimum fill value check
657 a = np.array([[np.nan, np.nan, np.inf], [np.nan, np.nan, np.inf]])
658 assert_equal(np.nanmedian(a, axis=1), np.inf)
659
660 # no mask path
661 a = np.array([[np.inf, np.inf], [np.inf, np.inf]])
662 assert_equal(np.nanmedian(a, axis=1), np.inf)
663
664
665 class TestNanFunctions_Percentile(TestCase):
666
667 def test_mutation(self):
668 # Check that passed array is not modified.
669 ndat = _ndat.copy()
670 np.nanpercentile(ndat, 30)
671 assert_equal(ndat, _ndat)
672
673 def test_keepdims(self):
674 mat = np.eye(3)
675 for axis in [None, 0, 1]:
676 tgt = np.percentile(mat, 70, axis=axis, out=None,
677 overwrite_input=False)
678 res = np.nanpercentile(mat, 70, axis=axis, out=None,
679 overwrite_input=False)
680 assert_(res.ndim == tgt.ndim)
681
682 d = np.ones((3, 5, 7, 11))
683 # Randomly set some elements to NaN:
684 w = np.random.random((4, 200)) * np.array(d.shape)[:, None]
685 w = w.astype(np.intp)
686 d[tuple(w)] = np.nan
687 with warnings.catch_warnings(record=True) as w:
688 warnings.simplefilter('always', RuntimeWarning)
689 res = np.nanpercentile(d, 90, axis=None, keepdims=True)
690 assert_equal(res.shape, (1, 1, 1, 1))
691 res = np.nanpercentile(d, 90, axis=(0, 1), keepdims=True)
692 assert_equal(res.shape, (1, 1, 7, 11))
693 res = np.nanpercentile(d, 90, axis=(0, 3), keepdims=True)
694 assert_equal(res.shape, (1, 5, 7, 1))
695 res = np.nanpercentile(d, 90, axis=(1,), keepdims=True)
696 assert_equal(res.shape, (3, 1, 7, 11))
697 res = np.nanpercentile(d, 90, axis=(0, 1, 2, 3), keepdims=True)
698 assert_equal(res.shape, (1, 1, 1, 1))
699 res = np.nanpercentile(d, 90, axis=(0, 1, 3), keepdims=True)
700 assert_equal(res.shape, (1, 1, 7, 1))
701
702 def test_out(self):
703 mat = np.random.rand(3, 3)
704 nan_mat = np.insert(mat, [0, 2], np.nan, axis=1)
705 resout = np.zeros(3)
706 tgt = np.percentile(mat, 42, axis=1)
707 res = np.nanpercentile(nan_mat, 42, axis=1, out=resout)
708 assert_almost_equal(res, resout)
709 assert_almost_equal(res, tgt)
710 # 0-d output:
711 resout = np.zeros(())
712 tgt = np.percentile(mat, 42, axis=None)
713 res = np.nanpercentile(nan_mat, 42, axis=None, out=resout)
714 assert_almost_equal(res, resout)
715 assert_almost_equal(res, tgt)
716 res = np.nanpercentile(nan_mat, 42, axis=(0, 1), out=resout)
717 assert_almost_equal(res, resout)
718 assert_almost_equal(res, tgt)
719
720 def test_result_values(self):
721 tgt = [np.percentile(d, 28) for d in _rdat]
722 res = np.nanpercentile(_ndat, 28, axis=1)
723 assert_almost_equal(res, tgt)
724 tgt = [np.percentile(d, (28, 98)) for d in _rdat]
725 res = np.nanpercentile(_ndat, (28, 98), axis=1)
726 assert_almost_equal(res, tgt)
727
728 def test_allnans(self):
729 mat = np.array([np.nan]*9).reshape(3, 3)
730 for axis in [None, 0, 1]:
731 with warnings.catch_warnings(record=True) as w:
732 warnings.simplefilter('always')
733 assert_(np.isnan(np.nanpercentile(mat, 60, axis=axis)).all())
734 if axis is None:
735 assert_(len(w) == 1)
736 else:
737 assert_(len(w) == 3)
738 assert_(issubclass(w[0].category, RuntimeWarning))
739 # Check scalar
740 assert_(np.isnan(np.nanpercentile(np.nan, 60)))
741 if axis is None:
742 assert_(len(w) == 2)
743 else:
744 assert_(len(w) == 4)
745 assert_(issubclass(w[0].category, RuntimeWarning))
746
747 def test_empty(self):
748 mat = np.zeros((0, 3))
749 for axis in [0, None]:
750 with warnings.catch_warnings(record=True) as w:
751 warnings.simplefilter('always')
752 assert_(np.isnan(np.nanpercentile(mat, 40, axis=axis)).all())
753 assert_(len(w) == 1)
754 assert_(issubclass(w[0].category, RuntimeWarning))
755 for axis in [1]:
756 with warnings.catch_warnings(record=True) as w:
757 warnings.simplefilter('always')
758 assert_equal(np.nanpercentile(mat, 40, axis=axis), np.zeros([]))
759 assert_(len(w) == 0)
760
761 def test_scalar(self):
762 assert_(np.nanpercentile(0., 100) == 0.)
763
764 def test_extended_axis_invalid(self):
765 d = np.ones((3, 5, 7, 11))
766 assert_raises(IndexError, np.nanpercentile, d, q=5, axis=-5)
767 assert_raises(IndexError, np.nanpercentile, d, q=5, axis=(0, -5))
768 assert_raises(IndexError, np.nanpercentile, d, q=5, axis=4)
769 assert_raises(IndexError, np.nanpercentile, d, q=5, axis=(0, 4))
770 assert_raises(ValueError, np.nanpercentile, d, q=5, axis=(1, 1))
771
772
773 if __name__ == "__main__":
774 run_module_suite()