test_nanfunctions.py 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246
  1. import warnings
  2. import pytest
  3. import inspect
  4. import numpy as np
  5. from numpy.core.numeric import normalize_axis_tuple
  6. from numpy.lib.nanfunctions import _nan_mask, _replace_nan
  7. from numpy.testing import (
  8. assert_, assert_equal, assert_almost_equal, assert_raises,
  9. assert_array_equal, suppress_warnings
  10. )
  11. # Test data
  12. _ndat = np.array([[0.6244, np.nan, 0.2692, 0.0116, np.nan, 0.1170],
  13. [0.5351, -0.9403, np.nan, 0.2100, 0.4759, 0.2833],
  14. [np.nan, np.nan, np.nan, 0.1042, np.nan, -0.5954],
  15. [0.1610, np.nan, np.nan, 0.1859, 0.3146, np.nan]])
  16. # Rows of _ndat with nans removed
  17. _rdat = [np.array([0.6244, 0.2692, 0.0116, 0.1170]),
  18. np.array([0.5351, -0.9403, 0.2100, 0.4759, 0.2833]),
  19. np.array([0.1042, -0.5954]),
  20. np.array([0.1610, 0.1859, 0.3146])]
  21. # Rows of _ndat with nans converted to ones
  22. _ndat_ones = np.array([[0.6244, 1.0, 0.2692, 0.0116, 1.0, 0.1170],
  23. [0.5351, -0.9403, 1.0, 0.2100, 0.4759, 0.2833],
  24. [1.0, 1.0, 1.0, 0.1042, 1.0, -0.5954],
  25. [0.1610, 1.0, 1.0, 0.1859, 0.3146, 1.0]])
  26. # Rows of _ndat with nans converted to zeros
  27. _ndat_zeros = np.array([[0.6244, 0.0, 0.2692, 0.0116, 0.0, 0.1170],
  28. [0.5351, -0.9403, 0.0, 0.2100, 0.4759, 0.2833],
  29. [0.0, 0.0, 0.0, 0.1042, 0.0, -0.5954],
  30. [0.1610, 0.0, 0.0, 0.1859, 0.3146, 0.0]])
  31. class TestSignatureMatch:
  32. NANFUNCS = {
  33. np.nanmin: np.amin,
  34. np.nanmax: np.amax,
  35. np.nanargmin: np.argmin,
  36. np.nanargmax: np.argmax,
  37. np.nansum: np.sum,
  38. np.nanprod: np.prod,
  39. np.nancumsum: np.cumsum,
  40. np.nancumprod: np.cumprod,
  41. np.nanmean: np.mean,
  42. np.nanmedian: np.median,
  43. np.nanpercentile: np.percentile,
  44. np.nanquantile: np.quantile,
  45. np.nanvar: np.var,
  46. np.nanstd: np.std,
  47. }
  48. IDS = [k.__name__ for k in NANFUNCS]
  49. @staticmethod
  50. def get_signature(func, default="..."):
  51. """Construct a signature and replace all default parameter-values."""
  52. prm_list = []
  53. signature = inspect.signature(func)
  54. for prm in signature.parameters.values():
  55. if prm.default is inspect.Parameter.empty:
  56. prm_list.append(prm)
  57. else:
  58. prm_list.append(prm.replace(default=default))
  59. return inspect.Signature(prm_list)
  60. @pytest.mark.parametrize("nan_func,func", NANFUNCS.items(), ids=IDS)
  61. def test_signature_match(self, nan_func, func):
  62. # Ignore the default parameter-values as they can sometimes differ
  63. # between the two functions (*e.g.* one has `False` while the other
  64. # has `np._NoValue`)
  65. signature = self.get_signature(func)
  66. nan_signature = self.get_signature(nan_func)
  67. np.testing.assert_equal(signature, nan_signature)
  68. def test_exhaustiveness(self):
  69. """Validate that all nan functions are actually tested."""
  70. np.testing.assert_equal(
  71. set(self.IDS), set(np.lib.nanfunctions.__all__)
  72. )
  73. class TestNanFunctions_MinMax:
  74. nanfuncs = [np.nanmin, np.nanmax]
  75. stdfuncs = [np.min, np.max]
  76. def test_mutation(self):
  77. # Check that passed array is not modified.
  78. ndat = _ndat.copy()
  79. for f in self.nanfuncs:
  80. f(ndat)
  81. assert_equal(ndat, _ndat)
  82. def test_keepdims(self):
  83. mat = np.eye(3)
  84. for nf, rf in zip(self.nanfuncs, self.stdfuncs):
  85. for axis in [None, 0, 1]:
  86. tgt = rf(mat, axis=axis, keepdims=True)
  87. res = nf(mat, axis=axis, keepdims=True)
  88. assert_(res.ndim == tgt.ndim)
  89. def test_out(self):
  90. mat = np.eye(3)
  91. for nf, rf in zip(self.nanfuncs, self.stdfuncs):
  92. resout = np.zeros(3)
  93. tgt = rf(mat, axis=1)
  94. res = nf(mat, axis=1, out=resout)
  95. assert_almost_equal(res, resout)
  96. assert_almost_equal(res, tgt)
  97. def test_dtype_from_input(self):
  98. codes = 'efdgFDG'
  99. for nf, rf in zip(self.nanfuncs, self.stdfuncs):
  100. for c in codes:
  101. mat = np.eye(3, dtype=c)
  102. tgt = rf(mat, axis=1).dtype.type
  103. res = nf(mat, axis=1).dtype.type
  104. assert_(res is tgt)
  105. # scalar case
  106. tgt = rf(mat, axis=None).dtype.type
  107. res = nf(mat, axis=None).dtype.type
  108. assert_(res is tgt)
  109. def test_result_values(self):
  110. for nf, rf in zip(self.nanfuncs, self.stdfuncs):
  111. tgt = [rf(d) for d in _rdat]
  112. res = nf(_ndat, axis=1)
  113. assert_almost_equal(res, tgt)
  114. @pytest.mark.parametrize("axis", [None, 0, 1])
  115. @pytest.mark.parametrize("dtype", np.typecodes["AllFloat"])
  116. @pytest.mark.parametrize("array", [
  117. np.array(np.nan),
  118. np.full((3, 3), np.nan),
  119. ], ids=["0d", "2d"])
  120. def test_allnans(self, axis, dtype, array):
  121. if axis is not None and array.ndim == 0:
  122. pytest.skip(f"`axis != None` not supported for 0d arrays")
  123. array = array.astype(dtype)
  124. match = "All-NaN slice encountered"
  125. for func in self.nanfuncs:
  126. with pytest.warns(RuntimeWarning, match=match):
  127. out = func(array, axis=axis)
  128. assert np.isnan(out).all()
  129. assert out.dtype == array.dtype
  130. def test_masked(self):
  131. mat = np.ma.fix_invalid(_ndat)
  132. msk = mat._mask.copy()
  133. for f in [np.nanmin]:
  134. res = f(mat, axis=1)
  135. tgt = f(_ndat, axis=1)
  136. assert_equal(res, tgt)
  137. assert_equal(mat._mask, msk)
  138. assert_(not np.isinf(mat).any())
  139. def test_scalar(self):
  140. for f in self.nanfuncs:
  141. assert_(f(0.) == 0.)
  142. def test_subclass(self):
  143. class MyNDArray(np.ndarray):
  144. pass
  145. # Check that it works and that type and
  146. # shape are preserved
  147. mine = np.eye(3).view(MyNDArray)
  148. for f in self.nanfuncs:
  149. res = f(mine, axis=0)
  150. assert_(isinstance(res, MyNDArray))
  151. assert_(res.shape == (3,))
  152. res = f(mine, axis=1)
  153. assert_(isinstance(res, MyNDArray))
  154. assert_(res.shape == (3,))
  155. res = f(mine)
  156. assert_(res.shape == ())
  157. # check that rows of nan are dealt with for subclasses (#4628)
  158. mine[1] = np.nan
  159. for f in self.nanfuncs:
  160. with warnings.catch_warnings(record=True) as w:
  161. warnings.simplefilter('always')
  162. res = f(mine, axis=0)
  163. assert_(isinstance(res, MyNDArray))
  164. assert_(not np.any(np.isnan(res)))
  165. assert_(len(w) == 0)
  166. with warnings.catch_warnings(record=True) as w:
  167. warnings.simplefilter('always')
  168. res = f(mine, axis=1)
  169. assert_(isinstance(res, MyNDArray))
  170. assert_(np.isnan(res[1]) and not np.isnan(res[0])
  171. and not np.isnan(res[2]))
  172. assert_(len(w) == 1, 'no warning raised')
  173. assert_(issubclass(w[0].category, RuntimeWarning))
  174. with warnings.catch_warnings(record=True) as w:
  175. warnings.simplefilter('always')
  176. res = f(mine)
  177. assert_(res.shape == ())
  178. assert_(res != np.nan)
  179. assert_(len(w) == 0)
  180. def test_object_array(self):
  181. arr = np.array([[1.0, 2.0], [np.nan, 4.0], [np.nan, np.nan]], dtype=object)
  182. assert_equal(np.nanmin(arr), 1.0)
  183. assert_equal(np.nanmin(arr, axis=0), [1.0, 2.0])
  184. with warnings.catch_warnings(record=True) as w:
  185. warnings.simplefilter('always')
  186. # assert_equal does not work on object arrays of nan
  187. assert_equal(list(np.nanmin(arr, axis=1)), [1.0, 4.0, np.nan])
  188. assert_(len(w) == 1, 'no warning raised')
  189. assert_(issubclass(w[0].category, RuntimeWarning))
  190. @pytest.mark.parametrize("dtype", np.typecodes["AllFloat"])
  191. def test_initial(self, dtype):
  192. class MyNDArray(np.ndarray):
  193. pass
  194. ar = np.arange(9).astype(dtype)
  195. ar[:5] = np.nan
  196. for f in self.nanfuncs:
  197. initial = 100 if f is np.nanmax else 0
  198. ret1 = f(ar, initial=initial)
  199. assert ret1.dtype == dtype
  200. assert ret1 == initial
  201. ret2 = f(ar.view(MyNDArray), initial=initial)
  202. assert ret2.dtype == dtype
  203. assert ret2 == initial
  204. @pytest.mark.parametrize("dtype", np.typecodes["AllFloat"])
  205. def test_where(self, dtype):
  206. class MyNDArray(np.ndarray):
  207. pass
  208. ar = np.arange(9).reshape(3, 3).astype(dtype)
  209. ar[0, :] = np.nan
  210. where = np.ones_like(ar, dtype=np.bool_)
  211. where[:, 0] = False
  212. for f in self.nanfuncs:
  213. reference = 4 if f is np.nanmin else 8
  214. ret1 = f(ar, where=where, initial=5)
  215. assert ret1.dtype == dtype
  216. assert ret1 == reference
  217. ret2 = f(ar.view(MyNDArray), where=where, initial=5)
  218. assert ret2.dtype == dtype
  219. assert ret2 == reference
  220. class TestNanFunctions_ArgminArgmax:
  221. nanfuncs = [np.nanargmin, np.nanargmax]
  222. def test_mutation(self):
  223. # Check that passed array is not modified.
  224. ndat = _ndat.copy()
  225. for f in self.nanfuncs:
  226. f(ndat)
  227. assert_equal(ndat, _ndat)
  228. def test_result_values(self):
  229. for f, fcmp in zip(self.nanfuncs, [np.greater, np.less]):
  230. for row in _ndat:
  231. with suppress_warnings() as sup:
  232. sup.filter(RuntimeWarning, "invalid value encountered in")
  233. ind = f(row)
  234. val = row[ind]
  235. # comparing with NaN is tricky as the result
  236. # is always false except for NaN != NaN
  237. assert_(not np.isnan(val))
  238. assert_(not fcmp(val, row).any())
  239. assert_(not np.equal(val, row[:ind]).any())
  240. @pytest.mark.parametrize("axis", [None, 0, 1])
  241. @pytest.mark.parametrize("dtype", np.typecodes["AllFloat"])
  242. @pytest.mark.parametrize("array", [
  243. np.array(np.nan),
  244. np.full((3, 3), np.nan),
  245. ], ids=["0d", "2d"])
  246. def test_allnans(self, axis, dtype, array):
  247. if axis is not None and array.ndim == 0:
  248. pytest.skip(f"`axis != None` not supported for 0d arrays")
  249. array = array.astype(dtype)
  250. for func in self.nanfuncs:
  251. with pytest.raises(ValueError, match="All-NaN slice encountered"):
  252. func(array, axis=axis)
  253. def test_empty(self):
  254. mat = np.zeros((0, 3))
  255. for f in self.nanfuncs:
  256. for axis in [0, None]:
  257. assert_raises(ValueError, f, mat, axis=axis)
  258. for axis in [1]:
  259. res = f(mat, axis=axis)
  260. assert_equal(res, np.zeros(0))
  261. def test_scalar(self):
  262. for f in self.nanfuncs:
  263. assert_(f(0.) == 0.)
  264. def test_subclass(self):
  265. class MyNDArray(np.ndarray):
  266. pass
  267. # Check that it works and that type and
  268. # shape are preserved
  269. mine = np.eye(3).view(MyNDArray)
  270. for f in self.nanfuncs:
  271. res = f(mine, axis=0)
  272. assert_(isinstance(res, MyNDArray))
  273. assert_(res.shape == (3,))
  274. res = f(mine, axis=1)
  275. assert_(isinstance(res, MyNDArray))
  276. assert_(res.shape == (3,))
  277. res = f(mine)
  278. assert_(res.shape == ())
  279. @pytest.mark.parametrize("dtype", np.typecodes["AllFloat"])
  280. def test_keepdims(self, dtype):
  281. ar = np.arange(9).astype(dtype)
  282. ar[:5] = np.nan
  283. for f in self.nanfuncs:
  284. reference = 5 if f is np.nanargmin else 8
  285. ret = f(ar, keepdims=True)
  286. assert ret.ndim == ar.ndim
  287. assert ret == reference
  288. @pytest.mark.parametrize("dtype", np.typecodes["AllFloat"])
  289. def test_out(self, dtype):
  290. ar = np.arange(9).astype(dtype)
  291. ar[:5] = np.nan
  292. for f in self.nanfuncs:
  293. out = np.zeros((), dtype=np.intp)
  294. reference = 5 if f is np.nanargmin else 8
  295. ret = f(ar, out=out)
  296. assert ret is out
  297. assert ret == reference
  298. _TEST_ARRAYS = {
  299. "0d": np.array(5),
  300. "1d": np.array([127, 39, 93, 87, 46])
  301. }
  302. for _v in _TEST_ARRAYS.values():
  303. _v.setflags(write=False)
  304. @pytest.mark.parametrize(
  305. "dtype",
  306. np.typecodes["AllInteger"] + np.typecodes["AllFloat"] + "O",
  307. )
  308. @pytest.mark.parametrize("mat", _TEST_ARRAYS.values(), ids=_TEST_ARRAYS.keys())
  309. class TestNanFunctions_NumberTypes:
  310. nanfuncs = {
  311. np.nanmin: np.min,
  312. np.nanmax: np.max,
  313. np.nanargmin: np.argmin,
  314. np.nanargmax: np.argmax,
  315. np.nansum: np.sum,
  316. np.nanprod: np.prod,
  317. np.nancumsum: np.cumsum,
  318. np.nancumprod: np.cumprod,
  319. np.nanmean: np.mean,
  320. np.nanmedian: np.median,
  321. np.nanvar: np.var,
  322. np.nanstd: np.std,
  323. }
  324. nanfunc_ids = [i.__name__ for i in nanfuncs]
  325. @pytest.mark.parametrize("nanfunc,func", nanfuncs.items(), ids=nanfunc_ids)
  326. @np.errstate(over="ignore")
  327. def test_nanfunc(self, mat, dtype, nanfunc, func):
  328. mat = mat.astype(dtype)
  329. tgt = func(mat)
  330. out = nanfunc(mat)
  331. assert_almost_equal(out, tgt)
  332. if dtype == "O":
  333. assert type(out) is type(tgt)
  334. else:
  335. assert out.dtype == tgt.dtype
  336. @pytest.mark.parametrize(
  337. "nanfunc,func",
  338. [(np.nanquantile, np.quantile), (np.nanpercentile, np.percentile)],
  339. ids=["nanquantile", "nanpercentile"],
  340. )
  341. def test_nanfunc_q(self, mat, dtype, nanfunc, func):
  342. mat = mat.astype(dtype)
  343. tgt = func(mat, q=1)
  344. out = nanfunc(mat, q=1)
  345. assert_almost_equal(out, tgt)
  346. if dtype == "O":
  347. assert type(out) is type(tgt)
  348. else:
  349. assert out.dtype == tgt.dtype
  350. @pytest.mark.parametrize(
  351. "nanfunc,func",
  352. [(np.nanvar, np.var), (np.nanstd, np.std)],
  353. ids=["nanvar", "nanstd"],
  354. )
  355. def test_nanfunc_ddof(self, mat, dtype, nanfunc, func):
  356. mat = mat.astype(dtype)
  357. tgt = func(mat, ddof=0.5)
  358. out = nanfunc(mat, ddof=0.5)
  359. assert_almost_equal(out, tgt)
  360. if dtype == "O":
  361. assert type(out) is type(tgt)
  362. else:
  363. assert out.dtype == tgt.dtype
  364. class SharedNanFunctionsTestsMixin:
  365. def test_mutation(self):
  366. # Check that passed array is not modified.
  367. ndat = _ndat.copy()
  368. for f in self.nanfuncs:
  369. f(ndat)
  370. assert_equal(ndat, _ndat)
  371. def test_keepdims(self):
  372. mat = np.eye(3)
  373. for nf, rf in zip(self.nanfuncs, self.stdfuncs):
  374. for axis in [None, 0, 1]:
  375. tgt = rf(mat, axis=axis, keepdims=True)
  376. res = nf(mat, axis=axis, keepdims=True)
  377. assert_(res.ndim == tgt.ndim)
  378. def test_out(self):
  379. mat = np.eye(3)
  380. for nf, rf in zip(self.nanfuncs, self.stdfuncs):
  381. resout = np.zeros(3)
  382. tgt = rf(mat, axis=1)
  383. res = nf(mat, axis=1, out=resout)
  384. assert_almost_equal(res, resout)
  385. assert_almost_equal(res, tgt)
  386. def test_dtype_from_dtype(self):
  387. mat = np.eye(3)
  388. codes = 'efdgFDG'
  389. for nf, rf in zip(self.nanfuncs, self.stdfuncs):
  390. for c in codes:
  391. with suppress_warnings() as sup:
  392. if nf in {np.nanstd, np.nanvar} and c in 'FDG':
  393. # Giving the warning is a small bug, see gh-8000
  394. sup.filter(np.ComplexWarning)
  395. tgt = rf(mat, dtype=np.dtype(c), axis=1).dtype.type
  396. res = nf(mat, dtype=np.dtype(c), axis=1).dtype.type
  397. assert_(res is tgt)
  398. # scalar case
  399. tgt = rf(mat, dtype=np.dtype(c), axis=None).dtype.type
  400. res = nf(mat, dtype=np.dtype(c), axis=None).dtype.type
  401. assert_(res is tgt)
  402. def test_dtype_from_char(self):
  403. mat = np.eye(3)
  404. codes = 'efdgFDG'
  405. for nf, rf in zip(self.nanfuncs, self.stdfuncs):
  406. for c in codes:
  407. with suppress_warnings() as sup:
  408. if nf in {np.nanstd, np.nanvar} and c in 'FDG':
  409. # Giving the warning is a small bug, see gh-8000
  410. sup.filter(np.ComplexWarning)
  411. tgt = rf(mat, dtype=c, axis=1).dtype.type
  412. res = nf(mat, dtype=c, axis=1).dtype.type
  413. assert_(res is tgt)
  414. # scalar case
  415. tgt = rf(mat, dtype=c, axis=None).dtype.type
  416. res = nf(mat, dtype=c, axis=None).dtype.type
  417. assert_(res is tgt)
  418. def test_dtype_from_input(self):
  419. codes = 'efdgFDG'
  420. for nf, rf in zip(self.nanfuncs, self.stdfuncs):
  421. for c in codes:
  422. mat = np.eye(3, dtype=c)
  423. tgt = rf(mat, axis=1).dtype.type
  424. res = nf(mat, axis=1).dtype.type
  425. assert_(res is tgt, "res %s, tgt %s" % (res, tgt))
  426. # scalar case
  427. tgt = rf(mat, axis=None).dtype.type
  428. res = nf(mat, axis=None).dtype.type
  429. assert_(res is tgt)
  430. def test_result_values(self):
  431. for nf, rf in zip(self.nanfuncs, self.stdfuncs):
  432. tgt = [rf(d) for d in _rdat]
  433. res = nf(_ndat, axis=1)
  434. assert_almost_equal(res, tgt)
  435. def test_scalar(self):
  436. for f in self.nanfuncs:
  437. assert_(f(0.) == 0.)
  438. def test_subclass(self):
  439. class MyNDArray(np.ndarray):
  440. pass
  441. # Check that it works and that type and
  442. # shape are preserved
  443. array = np.eye(3)
  444. mine = array.view(MyNDArray)
  445. for f in self.nanfuncs:
  446. expected_shape = f(array, axis=0).shape
  447. res = f(mine, axis=0)
  448. assert_(isinstance(res, MyNDArray))
  449. assert_(res.shape == expected_shape)
  450. expected_shape = f(array, axis=1).shape
  451. res = f(mine, axis=1)
  452. assert_(isinstance(res, MyNDArray))
  453. assert_(res.shape == expected_shape)
  454. expected_shape = f(array).shape
  455. res = f(mine)
  456. assert_(isinstance(res, MyNDArray))
  457. assert_(res.shape == expected_shape)
  458. class TestNanFunctions_SumProd(SharedNanFunctionsTestsMixin):
  459. nanfuncs = [np.nansum, np.nanprod]
  460. stdfuncs = [np.sum, np.prod]
  461. @pytest.mark.parametrize("axis", [None, 0, 1])
  462. @pytest.mark.parametrize("dtype", np.typecodes["AllFloat"])
  463. @pytest.mark.parametrize("array", [
  464. np.array(np.nan),
  465. np.full((3, 3), np.nan),
  466. ], ids=["0d", "2d"])
  467. def test_allnans(self, axis, dtype, array):
  468. if axis is not None and array.ndim == 0:
  469. pytest.skip(f"`axis != None` not supported for 0d arrays")
  470. array = array.astype(dtype)
  471. for func, identity in zip(self.nanfuncs, [0, 1]):
  472. out = func(array, axis=axis)
  473. assert np.all(out == identity)
  474. assert out.dtype == array.dtype
  475. def test_empty(self):
  476. for f, tgt_value in zip([np.nansum, np.nanprod], [0, 1]):
  477. mat = np.zeros((0, 3))
  478. tgt = [tgt_value]*3
  479. res = f(mat, axis=0)
  480. assert_equal(res, tgt)
  481. tgt = []
  482. res = f(mat, axis=1)
  483. assert_equal(res, tgt)
  484. tgt = tgt_value
  485. res = f(mat, axis=None)
  486. assert_equal(res, tgt)
  487. @pytest.mark.parametrize("dtype", np.typecodes["AllFloat"])
  488. def test_initial(self, dtype):
  489. ar = np.arange(9).astype(dtype)
  490. ar[:5] = np.nan
  491. for f in self.nanfuncs:
  492. reference = 28 if f is np.nansum else 3360
  493. ret = f(ar, initial=2)
  494. assert ret.dtype == dtype
  495. assert ret == reference
  496. @pytest.mark.parametrize("dtype", np.typecodes["AllFloat"])
  497. def test_where(self, dtype):
  498. ar = np.arange(9).reshape(3, 3).astype(dtype)
  499. ar[0, :] = np.nan
  500. where = np.ones_like(ar, dtype=np.bool_)
  501. where[:, 0] = False
  502. for f in self.nanfuncs:
  503. reference = 26 if f is np.nansum else 2240
  504. ret = f(ar, where=where, initial=2)
  505. assert ret.dtype == dtype
  506. assert ret == reference
  507. class TestNanFunctions_CumSumProd(SharedNanFunctionsTestsMixin):
  508. nanfuncs = [np.nancumsum, np.nancumprod]
  509. stdfuncs = [np.cumsum, np.cumprod]
  510. @pytest.mark.parametrize("axis", [None, 0, 1])
  511. @pytest.mark.parametrize("dtype", np.typecodes["AllFloat"])
  512. @pytest.mark.parametrize("array", [
  513. np.array(np.nan),
  514. np.full((3, 3), np.nan)
  515. ], ids=["0d", "2d"])
  516. def test_allnans(self, axis, dtype, array):
  517. if axis is not None and array.ndim == 0:
  518. pytest.skip(f"`axis != None` not supported for 0d arrays")
  519. array = array.astype(dtype)
  520. for func, identity in zip(self.nanfuncs, [0, 1]):
  521. out = func(array)
  522. assert np.all(out == identity)
  523. assert out.dtype == array.dtype
  524. def test_empty(self):
  525. for f, tgt_value in zip(self.nanfuncs, [0, 1]):
  526. mat = np.zeros((0, 3))
  527. tgt = tgt_value*np.ones((0, 3))
  528. res = f(mat, axis=0)
  529. assert_equal(res, tgt)
  530. tgt = mat
  531. res = f(mat, axis=1)
  532. assert_equal(res, tgt)
  533. tgt = np.zeros((0))
  534. res = f(mat, axis=None)
  535. assert_equal(res, tgt)
  536. def test_keepdims(self):
  537. for f, g in zip(self.nanfuncs, self.stdfuncs):
  538. mat = np.eye(3)
  539. for axis in [None, 0, 1]:
  540. tgt = f(mat, axis=axis, out=None)
  541. res = g(mat, axis=axis, out=None)
  542. assert_(res.ndim == tgt.ndim)
  543. for f in self.nanfuncs:
  544. d = np.ones((3, 5, 7, 11))
  545. # Randomly set some elements to NaN:
  546. rs = np.random.RandomState(0)
  547. d[rs.rand(*d.shape) < 0.5] = np.nan
  548. res = f(d, axis=None)
  549. assert_equal(res.shape, (1155,))
  550. for axis in np.arange(4):
  551. res = f(d, axis=axis)
  552. assert_equal(res.shape, (3, 5, 7, 11))
  553. def test_result_values(self):
  554. for axis in (-2, -1, 0, 1, None):
  555. tgt = np.cumprod(_ndat_ones, axis=axis)
  556. res = np.nancumprod(_ndat, axis=axis)
  557. assert_almost_equal(res, tgt)
  558. tgt = np.cumsum(_ndat_zeros,axis=axis)
  559. res = np.nancumsum(_ndat, axis=axis)
  560. assert_almost_equal(res, tgt)
  561. def test_out(self):
  562. mat = np.eye(3)
  563. for nf, rf in zip(self.nanfuncs, self.stdfuncs):
  564. resout = np.eye(3)
  565. for axis in (-2, -1, 0, 1):
  566. tgt = rf(mat, axis=axis)
  567. res = nf(mat, axis=axis, out=resout)
  568. assert_almost_equal(res, resout)
  569. assert_almost_equal(res, tgt)
  570. class TestNanFunctions_MeanVarStd(SharedNanFunctionsTestsMixin):
  571. nanfuncs = [np.nanmean, np.nanvar, np.nanstd]
  572. stdfuncs = [np.mean, np.var, np.std]
  573. def test_dtype_error(self):
  574. for f in self.nanfuncs:
  575. for dtype in [np.bool_, np.int_, np.object_]:
  576. assert_raises(TypeError, f, _ndat, axis=1, dtype=dtype)
  577. def test_out_dtype_error(self):
  578. for f in self.nanfuncs:
  579. for dtype in [np.bool_, np.int_, np.object_]:
  580. out = np.empty(_ndat.shape[0], dtype=dtype)
  581. assert_raises(TypeError, f, _ndat, axis=1, out=out)
  582. def test_ddof(self):
  583. nanfuncs = [np.nanvar, np.nanstd]
  584. stdfuncs = [np.var, np.std]
  585. for nf, rf in zip(nanfuncs, stdfuncs):
  586. for ddof in [0, 1]:
  587. tgt = [rf(d, ddof=ddof) for d in _rdat]
  588. res = nf(_ndat, axis=1, ddof=ddof)
  589. assert_almost_equal(res, tgt)
  590. def test_ddof_too_big(self):
  591. nanfuncs = [np.nanvar, np.nanstd]
  592. stdfuncs = [np.var, np.std]
  593. dsize = [len(d) for d in _rdat]
  594. for nf, rf in zip(nanfuncs, stdfuncs):
  595. for ddof in range(5):
  596. with suppress_warnings() as sup:
  597. sup.record(RuntimeWarning)
  598. sup.filter(np.ComplexWarning)
  599. tgt = [ddof >= d for d in dsize]
  600. res = nf(_ndat, axis=1, ddof=ddof)
  601. assert_equal(np.isnan(res), tgt)
  602. if any(tgt):
  603. assert_(len(sup.log) == 1)
  604. else:
  605. assert_(len(sup.log) == 0)
  606. @pytest.mark.parametrize("axis", [None, 0, 1])
  607. @pytest.mark.parametrize("dtype", np.typecodes["AllFloat"])
  608. @pytest.mark.parametrize("array", [
  609. np.array(np.nan),
  610. np.full((3, 3), np.nan),
  611. ], ids=["0d", "2d"])
  612. def test_allnans(self, axis, dtype, array):
  613. if axis is not None and array.ndim == 0:
  614. pytest.skip(f"`axis != None` not supported for 0d arrays")
  615. array = array.astype(dtype)
  616. match = "(Degrees of freedom <= 0 for slice.)|(Mean of empty slice)"
  617. for func in self.nanfuncs:
  618. with pytest.warns(RuntimeWarning, match=match):
  619. out = func(array, axis=axis)
  620. assert np.isnan(out).all()
  621. # `nanvar` and `nanstd` convert complex inputs to their
  622. # corresponding floating dtype
  623. if func is np.nanmean:
  624. assert out.dtype == array.dtype
  625. else:
  626. assert out.dtype == np.abs(array).dtype
  627. def test_empty(self):
  628. mat = np.zeros((0, 3))
  629. for f in self.nanfuncs:
  630. for axis in [0, None]:
  631. with warnings.catch_warnings(record=True) as w:
  632. warnings.simplefilter('always')
  633. assert_(np.isnan(f(mat, axis=axis)).all())
  634. assert_(len(w) == 1)
  635. assert_(issubclass(w[0].category, RuntimeWarning))
  636. for axis in [1]:
  637. with warnings.catch_warnings(record=True) as w:
  638. warnings.simplefilter('always')
  639. assert_equal(f(mat, axis=axis), np.zeros([]))
  640. assert_(len(w) == 0)
  641. @pytest.mark.parametrize("dtype", np.typecodes["AllFloat"])
  642. def test_where(self, dtype):
  643. ar = np.arange(9).reshape(3, 3).astype(dtype)
  644. ar[0, :] = np.nan
  645. where = np.ones_like(ar, dtype=np.bool_)
  646. where[:, 0] = False
  647. for f, f_std in zip(self.nanfuncs, self.stdfuncs):
  648. reference = f_std(ar[where][2:])
  649. dtype_reference = dtype if f is np.nanmean else ar.real.dtype
  650. ret = f(ar, where=where)
  651. assert ret.dtype == dtype_reference
  652. np.testing.assert_allclose(ret, reference)
  653. _TIME_UNITS = (
  654. "Y", "M", "W", "D", "h", "m", "s", "ms", "us", "ns", "ps", "fs", "as"
  655. )
  656. # All `inexact` + `timdelta64` type codes
  657. _TYPE_CODES = list(np.typecodes["AllFloat"])
  658. _TYPE_CODES += [f"m8[{unit}]" for unit in _TIME_UNITS]
  659. class TestNanFunctions_Median:
  660. def test_mutation(self):
  661. # Check that passed array is not modified.
  662. ndat = _ndat.copy()
  663. np.nanmedian(ndat)
  664. assert_equal(ndat, _ndat)
  665. def test_keepdims(self):
  666. mat = np.eye(3)
  667. for axis in [None, 0, 1]:
  668. tgt = np.median(mat, axis=axis, out=None, overwrite_input=False)
  669. res = np.nanmedian(mat, axis=axis, out=None, overwrite_input=False)
  670. assert_(res.ndim == tgt.ndim)
  671. d = np.ones((3, 5, 7, 11))
  672. # Randomly set some elements to NaN:
  673. w = np.random.random((4, 200)) * np.array(d.shape)[:, None]
  674. w = w.astype(np.intp)
  675. d[tuple(w)] = np.nan
  676. with suppress_warnings() as sup:
  677. sup.filter(RuntimeWarning)
  678. res = np.nanmedian(d, axis=None, keepdims=True)
  679. assert_equal(res.shape, (1, 1, 1, 1))
  680. res = np.nanmedian(d, axis=(0, 1), keepdims=True)
  681. assert_equal(res.shape, (1, 1, 7, 11))
  682. res = np.nanmedian(d, axis=(0, 3), keepdims=True)
  683. assert_equal(res.shape, (1, 5, 7, 1))
  684. res = np.nanmedian(d, axis=(1,), keepdims=True)
  685. assert_equal(res.shape, (3, 1, 7, 11))
  686. res = np.nanmedian(d, axis=(0, 1, 2, 3), keepdims=True)
  687. assert_equal(res.shape, (1, 1, 1, 1))
  688. res = np.nanmedian(d, axis=(0, 1, 3), keepdims=True)
  689. assert_equal(res.shape, (1, 1, 7, 1))
  690. @pytest.mark.parametrize(
  691. argnames='axis',
  692. argvalues=[
  693. None,
  694. 1,
  695. (1, ),
  696. (0, 1),
  697. (-3, -1),
  698. ]
  699. )
  700. @pytest.mark.filterwarnings("ignore:All-NaN slice:RuntimeWarning")
  701. def test_keepdims_out(self, axis):
  702. d = np.ones((3, 5, 7, 11))
  703. # Randomly set some elements to NaN:
  704. w = np.random.random((4, 200)) * np.array(d.shape)[:, None]
  705. w = w.astype(np.intp)
  706. d[tuple(w)] = np.nan
  707. if axis is None:
  708. shape_out = (1,) * d.ndim
  709. else:
  710. axis_norm = normalize_axis_tuple(axis, d.ndim)
  711. shape_out = tuple(
  712. 1 if i in axis_norm else d.shape[i] for i in range(d.ndim))
  713. out = np.empty(shape_out)
  714. result = np.nanmedian(d, axis=axis, keepdims=True, out=out)
  715. assert result is out
  716. assert_equal(result.shape, shape_out)
  717. def test_out(self):
  718. mat = np.random.rand(3, 3)
  719. nan_mat = np.insert(mat, [0, 2], np.nan, axis=1)
  720. resout = np.zeros(3)
  721. tgt = np.median(mat, axis=1)
  722. res = np.nanmedian(nan_mat, axis=1, out=resout)
  723. assert_almost_equal(res, resout)
  724. assert_almost_equal(res, tgt)
  725. # 0-d output:
  726. resout = np.zeros(())
  727. tgt = np.median(mat, axis=None)
  728. res = np.nanmedian(nan_mat, axis=None, out=resout)
  729. assert_almost_equal(res, resout)
  730. assert_almost_equal(res, tgt)
  731. res = np.nanmedian(nan_mat, axis=(0, 1), out=resout)
  732. assert_almost_equal(res, resout)
  733. assert_almost_equal(res, tgt)
  734. def test_small_large(self):
  735. # test the small and large code paths, current cutoff 400 elements
  736. for s in [5, 20, 51, 200, 1000]:
  737. d = np.random.randn(4, s)
  738. # Randomly set some elements to NaN:
  739. w = np.random.randint(0, d.size, size=d.size // 5)
  740. d.ravel()[w] = np.nan
  741. d[:,0] = 1. # ensure at least one good value
  742. # use normal median without nans to compare
  743. tgt = []
  744. for x in d:
  745. nonan = np.compress(~np.isnan(x), x)
  746. tgt.append(np.median(nonan, overwrite_input=True))
  747. assert_array_equal(np.nanmedian(d, axis=-1), tgt)
  748. def test_result_values(self):
  749. tgt = [np.median(d) for d in _rdat]
  750. res = np.nanmedian(_ndat, axis=1)
  751. assert_almost_equal(res, tgt)
  752. @pytest.mark.parametrize("axis", [None, 0, 1])
  753. @pytest.mark.parametrize("dtype", _TYPE_CODES)
  754. def test_allnans(self, dtype, axis):
  755. mat = np.full((3, 3), np.nan).astype(dtype)
  756. with suppress_warnings() as sup:
  757. sup.record(RuntimeWarning)
  758. output = np.nanmedian(mat, axis=axis)
  759. assert output.dtype == mat.dtype
  760. assert np.isnan(output).all()
  761. if axis is None:
  762. assert_(len(sup.log) == 1)
  763. else:
  764. assert_(len(sup.log) == 3)
  765. # Check scalar
  766. scalar = np.array(np.nan).astype(dtype)[()]
  767. output_scalar = np.nanmedian(scalar)
  768. assert output_scalar.dtype == scalar.dtype
  769. assert np.isnan(output_scalar)
  770. if axis is None:
  771. assert_(len(sup.log) == 2)
  772. else:
  773. assert_(len(sup.log) == 4)
  774. def test_empty(self):
  775. mat = np.zeros((0, 3))
  776. for axis in [0, None]:
  777. with warnings.catch_warnings(record=True) as w:
  778. warnings.simplefilter('always')
  779. assert_(np.isnan(np.nanmedian(mat, axis=axis)).all())
  780. assert_(len(w) == 1)
  781. assert_(issubclass(w[0].category, RuntimeWarning))
  782. for axis in [1]:
  783. with warnings.catch_warnings(record=True) as w:
  784. warnings.simplefilter('always')
  785. assert_equal(np.nanmedian(mat, axis=axis), np.zeros([]))
  786. assert_(len(w) == 0)
  787. def test_scalar(self):
  788. assert_(np.nanmedian(0.) == 0.)
  789. def test_extended_axis_invalid(self):
  790. d = np.ones((3, 5, 7, 11))
  791. assert_raises(np.AxisError, np.nanmedian, d, axis=-5)
  792. assert_raises(np.AxisError, np.nanmedian, d, axis=(0, -5))
  793. assert_raises(np.AxisError, np.nanmedian, d, axis=4)
  794. assert_raises(np.AxisError, np.nanmedian, d, axis=(0, 4))
  795. assert_raises(ValueError, np.nanmedian, d, axis=(1, 1))
  796. def test_float_special(self):
  797. with suppress_warnings() as sup:
  798. sup.filter(RuntimeWarning)
  799. for inf in [np.inf, -np.inf]:
  800. a = np.array([[inf, np.nan], [np.nan, np.nan]])
  801. assert_equal(np.nanmedian(a, axis=0), [inf, np.nan])
  802. assert_equal(np.nanmedian(a, axis=1), [inf, np.nan])
  803. assert_equal(np.nanmedian(a), inf)
  804. # minimum fill value check
  805. a = np.array([[np.nan, np.nan, inf],
  806. [np.nan, np.nan, inf]])
  807. assert_equal(np.nanmedian(a), inf)
  808. assert_equal(np.nanmedian(a, axis=0), [np.nan, np.nan, inf])
  809. assert_equal(np.nanmedian(a, axis=1), inf)
  810. # no mask path
  811. a = np.array([[inf, inf], [inf, inf]])
  812. assert_equal(np.nanmedian(a, axis=1), inf)
  813. a = np.array([[inf, 7, -inf, -9],
  814. [-10, np.nan, np.nan, 5],
  815. [4, np.nan, np.nan, inf]],
  816. dtype=np.float32)
  817. if inf > 0:
  818. assert_equal(np.nanmedian(a, axis=0), [4., 7., -inf, 5.])
  819. assert_equal(np.nanmedian(a), 4.5)
  820. else:
  821. assert_equal(np.nanmedian(a, axis=0), [-10., 7., -inf, -9.])
  822. assert_equal(np.nanmedian(a), -2.5)
  823. assert_equal(np.nanmedian(a, axis=-1), [-1., -2.5, inf])
  824. for i in range(0, 10):
  825. for j in range(1, 10):
  826. a = np.array([([np.nan] * i) + ([inf] * j)] * 2)
  827. assert_equal(np.nanmedian(a), inf)
  828. assert_equal(np.nanmedian(a, axis=1), inf)
  829. assert_equal(np.nanmedian(a, axis=0),
  830. ([np.nan] * i) + [inf] * j)
  831. a = np.array([([np.nan] * i) + ([-inf] * j)] * 2)
  832. assert_equal(np.nanmedian(a), -inf)
  833. assert_equal(np.nanmedian(a, axis=1), -inf)
  834. assert_equal(np.nanmedian(a, axis=0),
  835. ([np.nan] * i) + [-inf] * j)
  836. class TestNanFunctions_Percentile:
  837. def test_mutation(self):
  838. # Check that passed array is not modified.
  839. ndat = _ndat.copy()
  840. np.nanpercentile(ndat, 30)
  841. assert_equal(ndat, _ndat)
  842. def test_keepdims(self):
  843. mat = np.eye(3)
  844. for axis in [None, 0, 1]:
  845. tgt = np.percentile(mat, 70, axis=axis, out=None,
  846. overwrite_input=False)
  847. res = np.nanpercentile(mat, 70, axis=axis, out=None,
  848. overwrite_input=False)
  849. assert_(res.ndim == tgt.ndim)
  850. d = np.ones((3, 5, 7, 11))
  851. # Randomly set some elements to NaN:
  852. w = np.random.random((4, 200)) * np.array(d.shape)[:, None]
  853. w = w.astype(np.intp)
  854. d[tuple(w)] = np.nan
  855. with suppress_warnings() as sup:
  856. sup.filter(RuntimeWarning)
  857. res = np.nanpercentile(d, 90, axis=None, keepdims=True)
  858. assert_equal(res.shape, (1, 1, 1, 1))
  859. res = np.nanpercentile(d, 90, axis=(0, 1), keepdims=True)
  860. assert_equal(res.shape, (1, 1, 7, 11))
  861. res = np.nanpercentile(d, 90, axis=(0, 3), keepdims=True)
  862. assert_equal(res.shape, (1, 5, 7, 1))
  863. res = np.nanpercentile(d, 90, axis=(1,), keepdims=True)
  864. assert_equal(res.shape, (3, 1, 7, 11))
  865. res = np.nanpercentile(d, 90, axis=(0, 1, 2, 3), keepdims=True)
  866. assert_equal(res.shape, (1, 1, 1, 1))
  867. res = np.nanpercentile(d, 90, axis=(0, 1, 3), keepdims=True)
  868. assert_equal(res.shape, (1, 1, 7, 1))
  869. @pytest.mark.parametrize('q', [7, [1, 7]])
  870. @pytest.mark.parametrize(
  871. argnames='axis',
  872. argvalues=[
  873. None,
  874. 1,
  875. (1,),
  876. (0, 1),
  877. (-3, -1),
  878. ]
  879. )
  880. @pytest.mark.filterwarnings("ignore:All-NaN slice:RuntimeWarning")
  881. def test_keepdims_out(self, q, axis):
  882. d = np.ones((3, 5, 7, 11))
  883. # Randomly set some elements to NaN:
  884. w = np.random.random((4, 200)) * np.array(d.shape)[:, None]
  885. w = w.astype(np.intp)
  886. d[tuple(w)] = np.nan
  887. if axis is None:
  888. shape_out = (1,) * d.ndim
  889. else:
  890. axis_norm = normalize_axis_tuple(axis, d.ndim)
  891. shape_out = tuple(
  892. 1 if i in axis_norm else d.shape[i] for i in range(d.ndim))
  893. shape_out = np.shape(q) + shape_out
  894. out = np.empty(shape_out)
  895. result = np.nanpercentile(d, q, axis=axis, keepdims=True, out=out)
  896. assert result is out
  897. assert_equal(result.shape, shape_out)
  898. def test_out(self):
  899. mat = np.random.rand(3, 3)
  900. nan_mat = np.insert(mat, [0, 2], np.nan, axis=1)
  901. resout = np.zeros(3)
  902. tgt = np.percentile(mat, 42, axis=1)
  903. res = np.nanpercentile(nan_mat, 42, axis=1, out=resout)
  904. assert_almost_equal(res, resout)
  905. assert_almost_equal(res, tgt)
  906. # 0-d output:
  907. resout = np.zeros(())
  908. tgt = np.percentile(mat, 42, axis=None)
  909. res = np.nanpercentile(nan_mat, 42, axis=None, out=resout)
  910. assert_almost_equal(res, resout)
  911. assert_almost_equal(res, tgt)
  912. res = np.nanpercentile(nan_mat, 42, axis=(0, 1), out=resout)
  913. assert_almost_equal(res, resout)
  914. assert_almost_equal(res, tgt)
  915. def test_result_values(self):
  916. tgt = [np.percentile(d, 28) for d in _rdat]
  917. res = np.nanpercentile(_ndat, 28, axis=1)
  918. assert_almost_equal(res, tgt)
  919. # Transpose the array to fit the output convention of numpy.percentile
  920. tgt = np.transpose([np.percentile(d, (28, 98)) for d in _rdat])
  921. res = np.nanpercentile(_ndat, (28, 98), axis=1)
  922. assert_almost_equal(res, tgt)
  923. @pytest.mark.parametrize("axis", [None, 0, 1])
  924. @pytest.mark.parametrize("dtype", np.typecodes["AllFloat"])
  925. @pytest.mark.parametrize("array", [
  926. np.array(np.nan),
  927. np.full((3, 3), np.nan),
  928. ], ids=["0d", "2d"])
  929. def test_allnans(self, axis, dtype, array):
  930. if axis is not None and array.ndim == 0:
  931. pytest.skip(f"`axis != None` not supported for 0d arrays")
  932. array = array.astype(dtype)
  933. with pytest.warns(RuntimeWarning, match="All-NaN slice encountered"):
  934. out = np.nanpercentile(array, 60, axis=axis)
  935. assert np.isnan(out).all()
  936. assert out.dtype == array.dtype
  937. def test_empty(self):
  938. mat = np.zeros((0, 3))
  939. for axis in [0, None]:
  940. with warnings.catch_warnings(record=True) as w:
  941. warnings.simplefilter('always')
  942. assert_(np.isnan(np.nanpercentile(mat, 40, axis=axis)).all())
  943. assert_(len(w) == 1)
  944. assert_(issubclass(w[0].category, RuntimeWarning))
  945. for axis in [1]:
  946. with warnings.catch_warnings(record=True) as w:
  947. warnings.simplefilter('always')
  948. assert_equal(np.nanpercentile(mat, 40, axis=axis), np.zeros([]))
  949. assert_(len(w) == 0)
  950. def test_scalar(self):
  951. assert_equal(np.nanpercentile(0., 100), 0.)
  952. a = np.arange(6)
  953. r = np.nanpercentile(a, 50, axis=0)
  954. assert_equal(r, 2.5)
  955. assert_(np.isscalar(r))
  956. def test_extended_axis_invalid(self):
  957. d = np.ones((3, 5, 7, 11))
  958. assert_raises(np.AxisError, np.nanpercentile, d, q=5, axis=-5)
  959. assert_raises(np.AxisError, np.nanpercentile, d, q=5, axis=(0, -5))
  960. assert_raises(np.AxisError, np.nanpercentile, d, q=5, axis=4)
  961. assert_raises(np.AxisError, np.nanpercentile, d, q=5, axis=(0, 4))
  962. assert_raises(ValueError, np.nanpercentile, d, q=5, axis=(1, 1))
  963. def test_multiple_percentiles(self):
  964. perc = [50, 100]
  965. mat = np.ones((4, 3))
  966. nan_mat = np.nan * mat
  967. # For checking consistency in higher dimensional case
  968. large_mat = np.ones((3, 4, 5))
  969. large_mat[:, 0:2:4, :] = 0
  970. large_mat[:, :, 3:] *= 2
  971. for axis in [None, 0, 1]:
  972. for keepdim in [False, True]:
  973. with suppress_warnings() as sup:
  974. sup.filter(RuntimeWarning, "All-NaN slice encountered")
  975. val = np.percentile(mat, perc, axis=axis, keepdims=keepdim)
  976. nan_val = np.nanpercentile(nan_mat, perc, axis=axis,
  977. keepdims=keepdim)
  978. assert_equal(nan_val.shape, val.shape)
  979. val = np.percentile(large_mat, perc, axis=axis,
  980. keepdims=keepdim)
  981. nan_val = np.nanpercentile(large_mat, perc, axis=axis,
  982. keepdims=keepdim)
  983. assert_equal(nan_val, val)
  984. megamat = np.ones((3, 4, 5, 6))
  985. assert_equal(np.nanpercentile(megamat, perc, axis=(1, 2)).shape, (2, 3, 6))
  986. class TestNanFunctions_Quantile:
  987. # most of this is already tested by TestPercentile
  988. def test_regression(self):
  989. ar = np.arange(24).reshape(2, 3, 4).astype(float)
  990. ar[0][1] = np.nan
  991. assert_equal(np.nanquantile(ar, q=0.5), np.nanpercentile(ar, q=50))
  992. assert_equal(np.nanquantile(ar, q=0.5, axis=0),
  993. np.nanpercentile(ar, q=50, axis=0))
  994. assert_equal(np.nanquantile(ar, q=0.5, axis=1),
  995. np.nanpercentile(ar, q=50, axis=1))
  996. assert_equal(np.nanquantile(ar, q=[0.5], axis=1),
  997. np.nanpercentile(ar, q=[50], axis=1))
  998. assert_equal(np.nanquantile(ar, q=[0.25, 0.5, 0.75], axis=1),
  999. np.nanpercentile(ar, q=[25, 50, 75], axis=1))
  1000. def test_basic(self):
  1001. x = np.arange(8) * 0.5
  1002. assert_equal(np.nanquantile(x, 0), 0.)
  1003. assert_equal(np.nanquantile(x, 1), 3.5)
  1004. assert_equal(np.nanquantile(x, 0.5), 1.75)
  1005. def test_no_p_overwrite(self):
  1006. # this is worth retesting, because quantile does not make a copy
  1007. p0 = np.array([0, 0.75, 0.25, 0.5, 1.0])
  1008. p = p0.copy()
  1009. np.nanquantile(np.arange(100.), p, method="midpoint")
  1010. assert_array_equal(p, p0)
  1011. p0 = p0.tolist()
  1012. p = p.tolist()
  1013. np.nanquantile(np.arange(100.), p, method="midpoint")
  1014. assert_array_equal(p, p0)
  1015. @pytest.mark.parametrize("axis", [None, 0, 1])
  1016. @pytest.mark.parametrize("dtype", np.typecodes["AllFloat"])
  1017. @pytest.mark.parametrize("array", [
  1018. np.array(np.nan),
  1019. np.full((3, 3), np.nan),
  1020. ], ids=["0d", "2d"])
  1021. def test_allnans(self, axis, dtype, array):
  1022. if axis is not None and array.ndim == 0:
  1023. pytest.skip(f"`axis != None` not supported for 0d arrays")
  1024. array = array.astype(dtype)
  1025. with pytest.warns(RuntimeWarning, match="All-NaN slice encountered"):
  1026. out = np.nanquantile(array, 1, axis=axis)
  1027. assert np.isnan(out).all()
  1028. assert out.dtype == array.dtype
  1029. @pytest.mark.parametrize("arr, expected", [
  1030. # array of floats with some nans
  1031. (np.array([np.nan, 5.0, np.nan, np.inf]),
  1032. np.array([False, True, False, True])),
  1033. # int64 array that can't possibly have nans
  1034. (np.array([1, 5, 7, 9], dtype=np.int64),
  1035. True),
  1036. # bool array that can't possibly have nans
  1037. (np.array([False, True, False, True]),
  1038. True),
  1039. # 2-D complex array with nans
  1040. (np.array([[np.nan, 5.0],
  1041. [np.nan, np.inf]], dtype=np.complex64),
  1042. np.array([[False, True],
  1043. [False, True]])),
  1044. ])
  1045. def test__nan_mask(arr, expected):
  1046. for out in [None, np.empty(arr.shape, dtype=np.bool_)]:
  1047. actual = _nan_mask(arr, out=out)
  1048. assert_equal(actual, expected)
  1049. # the above won't distinguish between True proper
  1050. # and an array of True values; we want True proper
  1051. # for types that can't possibly contain NaN
  1052. if type(expected) is not np.ndarray:
  1053. assert actual is True
  1054. def test__replace_nan():
  1055. """ Test that _replace_nan returns the original array if there are no
  1056. NaNs, not a copy.
  1057. """
  1058. for dtype in [np.bool_, np.int32, np.int64]:
  1059. arr = np.array([0, 1], dtype=dtype)
  1060. result, mask = _replace_nan(arr, 0)
  1061. assert mask is None
  1062. # do not make a copy if there are no nans
  1063. assert result is arr
  1064. for dtype in [np.float32, np.float64]:
  1065. arr = np.array([0, 1], dtype=dtype)
  1066. result, mask = _replace_nan(arr, 2)
  1067. assert (mask == False).all()
  1068. # mask is not None, so we make a copy
  1069. assert result is not arr
  1070. assert_equal(result, arr)
  1071. arr_nan = np.array([0, 1, np.nan], dtype=dtype)
  1072. result_nan, mask_nan = _replace_nan(arr_nan, 2)
  1073. assert_equal(mask_nan, np.array([False, False, True]))
  1074. assert result_nan is not arr_nan
  1075. assert_equal(result_nan, np.array([0, 1, 2]))
  1076. assert np.isnan(arr_nan[-1])