annotate DEPENDENCIES/mingw32/Python27/Lib/site-packages/numpy/lib/tests/test_nanfunctions.py @ 133:4acb5d8d80b6 tip

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