test_nanfunctions.py 35 KB

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