test_nanfunctions.py 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268
  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. if mat.dtype.kind == "c":
  344. assert_raises(TypeError, func, mat, q=1)
  345. assert_raises(TypeError, nanfunc, mat, q=1)
  346. else:
  347. tgt = func(mat, q=1)
  348. out = nanfunc(mat, q=1)
  349. assert_almost_equal(out, tgt)
  350. if dtype == "O":
  351. assert type(out) is type(tgt)
  352. else:
  353. assert out.dtype == tgt.dtype
  354. @pytest.mark.parametrize(
  355. "nanfunc,func",
  356. [(np.nanvar, np.var), (np.nanstd, np.std)],
  357. ids=["nanvar", "nanstd"],
  358. )
  359. def test_nanfunc_ddof(self, mat, dtype, nanfunc, func):
  360. mat = mat.astype(dtype)
  361. tgt = func(mat, ddof=0.5)
  362. out = nanfunc(mat, ddof=0.5)
  363. assert_almost_equal(out, tgt)
  364. if dtype == "O":
  365. assert type(out) is type(tgt)
  366. else:
  367. assert out.dtype == tgt.dtype
  368. class SharedNanFunctionsTestsMixin:
  369. def test_mutation(self):
  370. # Check that passed array is not modified.
  371. ndat = _ndat.copy()
  372. for f in self.nanfuncs:
  373. f(ndat)
  374. assert_equal(ndat, _ndat)
  375. def test_keepdims(self):
  376. mat = np.eye(3)
  377. for nf, rf in zip(self.nanfuncs, self.stdfuncs):
  378. for axis in [None, 0, 1]:
  379. tgt = rf(mat, axis=axis, keepdims=True)
  380. res = nf(mat, axis=axis, keepdims=True)
  381. assert_(res.ndim == tgt.ndim)
  382. def test_out(self):
  383. mat = np.eye(3)
  384. for nf, rf in zip(self.nanfuncs, self.stdfuncs):
  385. resout = np.zeros(3)
  386. tgt = rf(mat, axis=1)
  387. res = nf(mat, axis=1, out=resout)
  388. assert_almost_equal(res, resout)
  389. assert_almost_equal(res, tgt)
  390. def test_dtype_from_dtype(self):
  391. mat = np.eye(3)
  392. codes = 'efdgFDG'
  393. for nf, rf in zip(self.nanfuncs, self.stdfuncs):
  394. for c in codes:
  395. with suppress_warnings() as sup:
  396. if nf in {np.nanstd, np.nanvar} and c in 'FDG':
  397. # Giving the warning is a small bug, see gh-8000
  398. sup.filter(np.ComplexWarning)
  399. tgt = rf(mat, dtype=np.dtype(c), axis=1).dtype.type
  400. res = nf(mat, dtype=np.dtype(c), axis=1).dtype.type
  401. assert_(res is tgt)
  402. # scalar case
  403. tgt = rf(mat, dtype=np.dtype(c), axis=None).dtype.type
  404. res = nf(mat, dtype=np.dtype(c), axis=None).dtype.type
  405. assert_(res is tgt)
  406. def test_dtype_from_char(self):
  407. mat = np.eye(3)
  408. codes = 'efdgFDG'
  409. for nf, rf in zip(self.nanfuncs, self.stdfuncs):
  410. for c in codes:
  411. with suppress_warnings() as sup:
  412. if nf in {np.nanstd, np.nanvar} and c in 'FDG':
  413. # Giving the warning is a small bug, see gh-8000
  414. sup.filter(np.ComplexWarning)
  415. tgt = rf(mat, dtype=c, axis=1).dtype.type
  416. res = nf(mat, dtype=c, axis=1).dtype.type
  417. assert_(res is tgt)
  418. # scalar case
  419. tgt = rf(mat, dtype=c, axis=None).dtype.type
  420. res = nf(mat, dtype=c, axis=None).dtype.type
  421. assert_(res is tgt)
  422. def test_dtype_from_input(self):
  423. codes = 'efdgFDG'
  424. for nf, rf in zip(self.nanfuncs, self.stdfuncs):
  425. for c in codes:
  426. mat = np.eye(3, dtype=c)
  427. tgt = rf(mat, axis=1).dtype.type
  428. res = nf(mat, axis=1).dtype.type
  429. assert_(res is tgt, "res %s, tgt %s" % (res, tgt))
  430. # scalar case
  431. tgt = rf(mat, axis=None).dtype.type
  432. res = nf(mat, axis=None).dtype.type
  433. assert_(res is tgt)
  434. def test_result_values(self):
  435. for nf, rf in zip(self.nanfuncs, self.stdfuncs):
  436. tgt = [rf(d) for d in _rdat]
  437. res = nf(_ndat, axis=1)
  438. assert_almost_equal(res, tgt)
  439. def test_scalar(self):
  440. for f in self.nanfuncs:
  441. assert_(f(0.) == 0.)
  442. def test_subclass(self):
  443. class MyNDArray(np.ndarray):
  444. pass
  445. # Check that it works and that type and
  446. # shape are preserved
  447. array = np.eye(3)
  448. mine = array.view(MyNDArray)
  449. for f in self.nanfuncs:
  450. expected_shape = f(array, axis=0).shape
  451. res = f(mine, axis=0)
  452. assert_(isinstance(res, MyNDArray))
  453. assert_(res.shape == expected_shape)
  454. expected_shape = f(array, axis=1).shape
  455. res = f(mine, axis=1)
  456. assert_(isinstance(res, MyNDArray))
  457. assert_(res.shape == expected_shape)
  458. expected_shape = f(array).shape
  459. res = f(mine)
  460. assert_(isinstance(res, MyNDArray))
  461. assert_(res.shape == expected_shape)
  462. class TestNanFunctions_SumProd(SharedNanFunctionsTestsMixin):
  463. nanfuncs = [np.nansum, np.nanprod]
  464. stdfuncs = [np.sum, np.prod]
  465. @pytest.mark.parametrize("axis", [None, 0, 1])
  466. @pytest.mark.parametrize("dtype", np.typecodes["AllFloat"])
  467. @pytest.mark.parametrize("array", [
  468. np.array(np.nan),
  469. np.full((3, 3), np.nan),
  470. ], ids=["0d", "2d"])
  471. def test_allnans(self, axis, dtype, array):
  472. if axis is not None and array.ndim == 0:
  473. pytest.skip(f"`axis != None` not supported for 0d arrays")
  474. array = array.astype(dtype)
  475. for func, identity in zip(self.nanfuncs, [0, 1]):
  476. out = func(array, axis=axis)
  477. assert np.all(out == identity)
  478. assert out.dtype == array.dtype
  479. def test_empty(self):
  480. for f, tgt_value in zip([np.nansum, np.nanprod], [0, 1]):
  481. mat = np.zeros((0, 3))
  482. tgt = [tgt_value]*3
  483. res = f(mat, axis=0)
  484. assert_equal(res, tgt)
  485. tgt = []
  486. res = f(mat, axis=1)
  487. assert_equal(res, tgt)
  488. tgt = tgt_value
  489. res = f(mat, axis=None)
  490. assert_equal(res, tgt)
  491. @pytest.mark.parametrize("dtype", np.typecodes["AllFloat"])
  492. def test_initial(self, dtype):
  493. ar = np.arange(9).astype(dtype)
  494. ar[:5] = np.nan
  495. for f in self.nanfuncs:
  496. reference = 28 if f is np.nansum else 3360
  497. ret = f(ar, initial=2)
  498. assert ret.dtype == dtype
  499. assert ret == reference
  500. @pytest.mark.parametrize("dtype", np.typecodes["AllFloat"])
  501. def test_where(self, dtype):
  502. ar = np.arange(9).reshape(3, 3).astype(dtype)
  503. ar[0, :] = np.nan
  504. where = np.ones_like(ar, dtype=np.bool_)
  505. where[:, 0] = False
  506. for f in self.nanfuncs:
  507. reference = 26 if f is np.nansum else 2240
  508. ret = f(ar, where=where, initial=2)
  509. assert ret.dtype == dtype
  510. assert ret == reference
  511. class TestNanFunctions_CumSumProd(SharedNanFunctionsTestsMixin):
  512. nanfuncs = [np.nancumsum, np.nancumprod]
  513. stdfuncs = [np.cumsum, np.cumprod]
  514. @pytest.mark.parametrize("axis", [None, 0, 1])
  515. @pytest.mark.parametrize("dtype", np.typecodes["AllFloat"])
  516. @pytest.mark.parametrize("array", [
  517. np.array(np.nan),
  518. np.full((3, 3), np.nan)
  519. ], ids=["0d", "2d"])
  520. def test_allnans(self, axis, dtype, array):
  521. if axis is not None and array.ndim == 0:
  522. pytest.skip(f"`axis != None` not supported for 0d arrays")
  523. array = array.astype(dtype)
  524. for func, identity in zip(self.nanfuncs, [0, 1]):
  525. out = func(array)
  526. assert np.all(out == identity)
  527. assert out.dtype == array.dtype
  528. def test_empty(self):
  529. for f, tgt_value in zip(self.nanfuncs, [0, 1]):
  530. mat = np.zeros((0, 3))
  531. tgt = tgt_value*np.ones((0, 3))
  532. res = f(mat, axis=0)
  533. assert_equal(res, tgt)
  534. tgt = mat
  535. res = f(mat, axis=1)
  536. assert_equal(res, tgt)
  537. tgt = np.zeros((0))
  538. res = f(mat, axis=None)
  539. assert_equal(res, tgt)
  540. def test_keepdims(self):
  541. for f, g in zip(self.nanfuncs, self.stdfuncs):
  542. mat = np.eye(3)
  543. for axis in [None, 0, 1]:
  544. tgt = f(mat, axis=axis, out=None)
  545. res = g(mat, axis=axis, out=None)
  546. assert_(res.ndim == tgt.ndim)
  547. for f in self.nanfuncs:
  548. d = np.ones((3, 5, 7, 11))
  549. # Randomly set some elements to NaN:
  550. rs = np.random.RandomState(0)
  551. d[rs.rand(*d.shape) < 0.5] = np.nan
  552. res = f(d, axis=None)
  553. assert_equal(res.shape, (1155,))
  554. for axis in np.arange(4):
  555. res = f(d, axis=axis)
  556. assert_equal(res.shape, (3, 5, 7, 11))
  557. def test_result_values(self):
  558. for axis in (-2, -1, 0, 1, None):
  559. tgt = np.cumprod(_ndat_ones, axis=axis)
  560. res = np.nancumprod(_ndat, axis=axis)
  561. assert_almost_equal(res, tgt)
  562. tgt = np.cumsum(_ndat_zeros,axis=axis)
  563. res = np.nancumsum(_ndat, axis=axis)
  564. assert_almost_equal(res, tgt)
  565. def test_out(self):
  566. mat = np.eye(3)
  567. for nf, rf in zip(self.nanfuncs, self.stdfuncs):
  568. resout = np.eye(3)
  569. for axis in (-2, -1, 0, 1):
  570. tgt = rf(mat, axis=axis)
  571. res = nf(mat, axis=axis, out=resout)
  572. assert_almost_equal(res, resout)
  573. assert_almost_equal(res, tgt)
  574. class TestNanFunctions_MeanVarStd(SharedNanFunctionsTestsMixin):
  575. nanfuncs = [np.nanmean, np.nanvar, np.nanstd]
  576. stdfuncs = [np.mean, np.var, np.std]
  577. def test_dtype_error(self):
  578. for f in self.nanfuncs:
  579. for dtype in [np.bool_, np.int_, np.object_]:
  580. assert_raises(TypeError, f, _ndat, axis=1, dtype=dtype)
  581. def test_out_dtype_error(self):
  582. for f in self.nanfuncs:
  583. for dtype in [np.bool_, np.int_, np.object_]:
  584. out = np.empty(_ndat.shape[0], dtype=dtype)
  585. assert_raises(TypeError, f, _ndat, axis=1, out=out)
  586. def test_ddof(self):
  587. nanfuncs = [np.nanvar, np.nanstd]
  588. stdfuncs = [np.var, np.std]
  589. for nf, rf in zip(nanfuncs, stdfuncs):
  590. for ddof in [0, 1]:
  591. tgt = [rf(d, ddof=ddof) for d in _rdat]
  592. res = nf(_ndat, axis=1, ddof=ddof)
  593. assert_almost_equal(res, tgt)
  594. def test_ddof_too_big(self):
  595. nanfuncs = [np.nanvar, np.nanstd]
  596. stdfuncs = [np.var, np.std]
  597. dsize = [len(d) for d in _rdat]
  598. for nf, rf in zip(nanfuncs, stdfuncs):
  599. for ddof in range(5):
  600. with suppress_warnings() as sup:
  601. sup.record(RuntimeWarning)
  602. sup.filter(np.ComplexWarning)
  603. tgt = [ddof >= d for d in dsize]
  604. res = nf(_ndat, axis=1, ddof=ddof)
  605. assert_equal(np.isnan(res), tgt)
  606. if any(tgt):
  607. assert_(len(sup.log) == 1)
  608. else:
  609. assert_(len(sup.log) == 0)
  610. @pytest.mark.parametrize("axis", [None, 0, 1])
  611. @pytest.mark.parametrize("dtype", np.typecodes["AllFloat"])
  612. @pytest.mark.parametrize("array", [
  613. np.array(np.nan),
  614. np.full((3, 3), np.nan),
  615. ], ids=["0d", "2d"])
  616. def test_allnans(self, axis, dtype, array):
  617. if axis is not None and array.ndim == 0:
  618. pytest.skip(f"`axis != None` not supported for 0d arrays")
  619. array = array.astype(dtype)
  620. match = "(Degrees of freedom <= 0 for slice.)|(Mean of empty slice)"
  621. for func in self.nanfuncs:
  622. with pytest.warns(RuntimeWarning, match=match):
  623. out = func(array, axis=axis)
  624. assert np.isnan(out).all()
  625. # `nanvar` and `nanstd` convert complex inputs to their
  626. # corresponding floating dtype
  627. if func is np.nanmean:
  628. assert out.dtype == array.dtype
  629. else:
  630. assert out.dtype == np.abs(array).dtype
  631. def test_empty(self):
  632. mat = np.zeros((0, 3))
  633. for f in self.nanfuncs:
  634. for axis in [0, None]:
  635. with warnings.catch_warnings(record=True) as w:
  636. warnings.simplefilter('always')
  637. assert_(np.isnan(f(mat, axis=axis)).all())
  638. assert_(len(w) == 1)
  639. assert_(issubclass(w[0].category, RuntimeWarning))
  640. for axis in [1]:
  641. with warnings.catch_warnings(record=True) as w:
  642. warnings.simplefilter('always')
  643. assert_equal(f(mat, axis=axis), np.zeros([]))
  644. assert_(len(w) == 0)
  645. @pytest.mark.parametrize("dtype", np.typecodes["AllFloat"])
  646. def test_where(self, dtype):
  647. ar = np.arange(9).reshape(3, 3).astype(dtype)
  648. ar[0, :] = np.nan
  649. where = np.ones_like(ar, dtype=np.bool_)
  650. where[:, 0] = False
  651. for f, f_std in zip(self.nanfuncs, self.stdfuncs):
  652. reference = f_std(ar[where][2:])
  653. dtype_reference = dtype if f is np.nanmean else ar.real.dtype
  654. ret = f(ar, where=where)
  655. assert ret.dtype == dtype_reference
  656. np.testing.assert_allclose(ret, reference)
  657. _TIME_UNITS = (
  658. "Y", "M", "W", "D", "h", "m", "s", "ms", "us", "ns", "ps", "fs", "as"
  659. )
  660. # All `inexact` + `timdelta64` type codes
  661. _TYPE_CODES = list(np.typecodes["AllFloat"])
  662. _TYPE_CODES += [f"m8[{unit}]" for unit in _TIME_UNITS]
  663. class TestNanFunctions_Median:
  664. def test_mutation(self):
  665. # Check that passed array is not modified.
  666. ndat = _ndat.copy()
  667. np.nanmedian(ndat)
  668. assert_equal(ndat, _ndat)
  669. def test_keepdims(self):
  670. mat = np.eye(3)
  671. for axis in [None, 0, 1]:
  672. tgt = np.median(mat, axis=axis, out=None, overwrite_input=False)
  673. res = np.nanmedian(mat, axis=axis, out=None, overwrite_input=False)
  674. assert_(res.ndim == tgt.ndim)
  675. d = np.ones((3, 5, 7, 11))
  676. # Randomly set some elements to NaN:
  677. w = np.random.random((4, 200)) * np.array(d.shape)[:, None]
  678. w = w.astype(np.intp)
  679. d[tuple(w)] = np.nan
  680. with suppress_warnings() as sup:
  681. sup.filter(RuntimeWarning)
  682. res = np.nanmedian(d, axis=None, keepdims=True)
  683. assert_equal(res.shape, (1, 1, 1, 1))
  684. res = np.nanmedian(d, axis=(0, 1), keepdims=True)
  685. assert_equal(res.shape, (1, 1, 7, 11))
  686. res = np.nanmedian(d, axis=(0, 3), keepdims=True)
  687. assert_equal(res.shape, (1, 5, 7, 1))
  688. res = np.nanmedian(d, axis=(1,), keepdims=True)
  689. assert_equal(res.shape, (3, 1, 7, 11))
  690. res = np.nanmedian(d, axis=(0, 1, 2, 3), keepdims=True)
  691. assert_equal(res.shape, (1, 1, 1, 1))
  692. res = np.nanmedian(d, axis=(0, 1, 3), keepdims=True)
  693. assert_equal(res.shape, (1, 1, 7, 1))
  694. @pytest.mark.parametrize(
  695. argnames='axis',
  696. argvalues=[
  697. None,
  698. 1,
  699. (1, ),
  700. (0, 1),
  701. (-3, -1),
  702. ]
  703. )
  704. @pytest.mark.filterwarnings("ignore:All-NaN slice:RuntimeWarning")
  705. def test_keepdims_out(self, axis):
  706. d = np.ones((3, 5, 7, 11))
  707. # Randomly set some elements to NaN:
  708. w = np.random.random((4, 200)) * np.array(d.shape)[:, None]
  709. w = w.astype(np.intp)
  710. d[tuple(w)] = np.nan
  711. if axis is None:
  712. shape_out = (1,) * d.ndim
  713. else:
  714. axis_norm = normalize_axis_tuple(axis, d.ndim)
  715. shape_out = tuple(
  716. 1 if i in axis_norm else d.shape[i] for i in range(d.ndim))
  717. out = np.empty(shape_out)
  718. result = np.nanmedian(d, axis=axis, keepdims=True, out=out)
  719. assert result is out
  720. assert_equal(result.shape, shape_out)
  721. def test_out(self):
  722. mat = np.random.rand(3, 3)
  723. nan_mat = np.insert(mat, [0, 2], np.nan, axis=1)
  724. resout = np.zeros(3)
  725. tgt = np.median(mat, axis=1)
  726. res = np.nanmedian(nan_mat, axis=1, out=resout)
  727. assert_almost_equal(res, resout)
  728. assert_almost_equal(res, tgt)
  729. # 0-d output:
  730. resout = np.zeros(())
  731. tgt = np.median(mat, axis=None)
  732. res = np.nanmedian(nan_mat, axis=None, out=resout)
  733. assert_almost_equal(res, resout)
  734. assert_almost_equal(res, tgt)
  735. res = np.nanmedian(nan_mat, axis=(0, 1), out=resout)
  736. assert_almost_equal(res, resout)
  737. assert_almost_equal(res, tgt)
  738. def test_small_large(self):
  739. # test the small and large code paths, current cutoff 400 elements
  740. for s in [5, 20, 51, 200, 1000]:
  741. d = np.random.randn(4, s)
  742. # Randomly set some elements to NaN:
  743. w = np.random.randint(0, d.size, size=d.size // 5)
  744. d.ravel()[w] = np.nan
  745. d[:,0] = 1. # ensure at least one good value
  746. # use normal median without nans to compare
  747. tgt = []
  748. for x in d:
  749. nonan = np.compress(~np.isnan(x), x)
  750. tgt.append(np.median(nonan, overwrite_input=True))
  751. assert_array_equal(np.nanmedian(d, axis=-1), tgt)
  752. def test_result_values(self):
  753. tgt = [np.median(d) for d in _rdat]
  754. res = np.nanmedian(_ndat, axis=1)
  755. assert_almost_equal(res, tgt)
  756. @pytest.mark.parametrize("axis", [None, 0, 1])
  757. @pytest.mark.parametrize("dtype", _TYPE_CODES)
  758. def test_allnans(self, dtype, axis):
  759. mat = np.full((3, 3), np.nan).astype(dtype)
  760. with suppress_warnings() as sup:
  761. sup.record(RuntimeWarning)
  762. output = np.nanmedian(mat, axis=axis)
  763. assert output.dtype == mat.dtype
  764. assert np.isnan(output).all()
  765. if axis is None:
  766. assert_(len(sup.log) == 1)
  767. else:
  768. assert_(len(sup.log) == 3)
  769. # Check scalar
  770. scalar = np.array(np.nan).astype(dtype)[()]
  771. output_scalar = np.nanmedian(scalar)
  772. assert output_scalar.dtype == scalar.dtype
  773. assert np.isnan(output_scalar)
  774. if axis is None:
  775. assert_(len(sup.log) == 2)
  776. else:
  777. assert_(len(sup.log) == 4)
  778. def test_empty(self):
  779. mat = np.zeros((0, 3))
  780. for axis in [0, None]:
  781. with warnings.catch_warnings(record=True) as w:
  782. warnings.simplefilter('always')
  783. assert_(np.isnan(np.nanmedian(mat, axis=axis)).all())
  784. assert_(len(w) == 1)
  785. assert_(issubclass(w[0].category, RuntimeWarning))
  786. for axis in [1]:
  787. with warnings.catch_warnings(record=True) as w:
  788. warnings.simplefilter('always')
  789. assert_equal(np.nanmedian(mat, axis=axis), np.zeros([]))
  790. assert_(len(w) == 0)
  791. def test_scalar(self):
  792. assert_(np.nanmedian(0.) == 0.)
  793. def test_extended_axis_invalid(self):
  794. d = np.ones((3, 5, 7, 11))
  795. assert_raises(np.AxisError, np.nanmedian, d, axis=-5)
  796. assert_raises(np.AxisError, np.nanmedian, d, axis=(0, -5))
  797. assert_raises(np.AxisError, np.nanmedian, d, axis=4)
  798. assert_raises(np.AxisError, np.nanmedian, d, axis=(0, 4))
  799. assert_raises(ValueError, np.nanmedian, d, axis=(1, 1))
  800. def test_float_special(self):
  801. with suppress_warnings() as sup:
  802. sup.filter(RuntimeWarning)
  803. for inf in [np.inf, -np.inf]:
  804. a = np.array([[inf, np.nan], [np.nan, np.nan]])
  805. assert_equal(np.nanmedian(a, axis=0), [inf, np.nan])
  806. assert_equal(np.nanmedian(a, axis=1), [inf, np.nan])
  807. assert_equal(np.nanmedian(a), inf)
  808. # minimum fill value check
  809. a = np.array([[np.nan, np.nan, inf],
  810. [np.nan, np.nan, inf]])
  811. assert_equal(np.nanmedian(a), inf)
  812. assert_equal(np.nanmedian(a, axis=0), [np.nan, np.nan, inf])
  813. assert_equal(np.nanmedian(a, axis=1), inf)
  814. # no mask path
  815. a = np.array([[inf, inf], [inf, inf]])
  816. assert_equal(np.nanmedian(a, axis=1), inf)
  817. a = np.array([[inf, 7, -inf, -9],
  818. [-10, np.nan, np.nan, 5],
  819. [4, np.nan, np.nan, inf]],
  820. dtype=np.float32)
  821. if inf > 0:
  822. assert_equal(np.nanmedian(a, axis=0), [4., 7., -inf, 5.])
  823. assert_equal(np.nanmedian(a), 4.5)
  824. else:
  825. assert_equal(np.nanmedian(a, axis=0), [-10., 7., -inf, -9.])
  826. assert_equal(np.nanmedian(a), -2.5)
  827. assert_equal(np.nanmedian(a, axis=-1), [-1., -2.5, inf])
  828. for i in range(0, 10):
  829. for j in range(1, 10):
  830. a = np.array([([np.nan] * i) + ([inf] * j)] * 2)
  831. assert_equal(np.nanmedian(a), inf)
  832. assert_equal(np.nanmedian(a, axis=1), inf)
  833. assert_equal(np.nanmedian(a, axis=0),
  834. ([np.nan] * i) + [inf] * j)
  835. a = np.array([([np.nan] * i) + ([-inf] * j)] * 2)
  836. assert_equal(np.nanmedian(a), -inf)
  837. assert_equal(np.nanmedian(a, axis=1), -inf)
  838. assert_equal(np.nanmedian(a, axis=0),
  839. ([np.nan] * i) + [-inf] * j)
  840. class TestNanFunctions_Percentile:
  841. def test_mutation(self):
  842. # Check that passed array is not modified.
  843. ndat = _ndat.copy()
  844. np.nanpercentile(ndat, 30)
  845. assert_equal(ndat, _ndat)
  846. def test_keepdims(self):
  847. mat = np.eye(3)
  848. for axis in [None, 0, 1]:
  849. tgt = np.percentile(mat, 70, axis=axis, out=None,
  850. overwrite_input=False)
  851. res = np.nanpercentile(mat, 70, axis=axis, out=None,
  852. overwrite_input=False)
  853. assert_(res.ndim == tgt.ndim)
  854. d = np.ones((3, 5, 7, 11))
  855. # Randomly set some elements to NaN:
  856. w = np.random.random((4, 200)) * np.array(d.shape)[:, None]
  857. w = w.astype(np.intp)
  858. d[tuple(w)] = np.nan
  859. with suppress_warnings() as sup:
  860. sup.filter(RuntimeWarning)
  861. res = np.nanpercentile(d, 90, axis=None, keepdims=True)
  862. assert_equal(res.shape, (1, 1, 1, 1))
  863. res = np.nanpercentile(d, 90, axis=(0, 1), keepdims=True)
  864. assert_equal(res.shape, (1, 1, 7, 11))
  865. res = np.nanpercentile(d, 90, axis=(0, 3), keepdims=True)
  866. assert_equal(res.shape, (1, 5, 7, 1))
  867. res = np.nanpercentile(d, 90, axis=(1,), keepdims=True)
  868. assert_equal(res.shape, (3, 1, 7, 11))
  869. res = np.nanpercentile(d, 90, axis=(0, 1, 2, 3), keepdims=True)
  870. assert_equal(res.shape, (1, 1, 1, 1))
  871. res = np.nanpercentile(d, 90, axis=(0, 1, 3), keepdims=True)
  872. assert_equal(res.shape, (1, 1, 7, 1))
  873. @pytest.mark.parametrize('q', [7, [1, 7]])
  874. @pytest.mark.parametrize(
  875. argnames='axis',
  876. argvalues=[
  877. None,
  878. 1,
  879. (1,),
  880. (0, 1),
  881. (-3, -1),
  882. ]
  883. )
  884. @pytest.mark.filterwarnings("ignore:All-NaN slice:RuntimeWarning")
  885. def test_keepdims_out(self, q, axis):
  886. d = np.ones((3, 5, 7, 11))
  887. # Randomly set some elements to NaN:
  888. w = np.random.random((4, 200)) * np.array(d.shape)[:, None]
  889. w = w.astype(np.intp)
  890. d[tuple(w)] = np.nan
  891. if axis is None:
  892. shape_out = (1,) * d.ndim
  893. else:
  894. axis_norm = normalize_axis_tuple(axis, d.ndim)
  895. shape_out = tuple(
  896. 1 if i in axis_norm else d.shape[i] for i in range(d.ndim))
  897. shape_out = np.shape(q) + shape_out
  898. out = np.empty(shape_out)
  899. result = np.nanpercentile(d, q, axis=axis, keepdims=True, out=out)
  900. assert result is out
  901. assert_equal(result.shape, shape_out)
  902. def test_out(self):
  903. mat = np.random.rand(3, 3)
  904. nan_mat = np.insert(mat, [0, 2], np.nan, axis=1)
  905. resout = np.zeros(3)
  906. tgt = np.percentile(mat, 42, axis=1)
  907. res = np.nanpercentile(nan_mat, 42, axis=1, out=resout)
  908. assert_almost_equal(res, resout)
  909. assert_almost_equal(res, tgt)
  910. # 0-d output:
  911. resout = np.zeros(())
  912. tgt = np.percentile(mat, 42, axis=None)
  913. res = np.nanpercentile(nan_mat, 42, axis=None, out=resout)
  914. assert_almost_equal(res, resout)
  915. assert_almost_equal(res, tgt)
  916. res = np.nanpercentile(nan_mat, 42, axis=(0, 1), out=resout)
  917. assert_almost_equal(res, resout)
  918. assert_almost_equal(res, tgt)
  919. def test_complex(self):
  920. arr_c = np.array([0.5+3.0j, 2.1+0.5j, 1.6+2.3j], dtype='G')
  921. assert_raises(TypeError, np.nanpercentile, arr_c, 0.5)
  922. arr_c = np.array([0.5+3.0j, 2.1+0.5j, 1.6+2.3j], dtype='D')
  923. assert_raises(TypeError, np.nanpercentile, arr_c, 0.5)
  924. arr_c = np.array([0.5+3.0j, 2.1+0.5j, 1.6+2.3j], dtype='F')
  925. assert_raises(TypeError, np.nanpercentile, arr_c, 0.5)
  926. def test_result_values(self):
  927. tgt = [np.percentile(d, 28) for d in _rdat]
  928. res = np.nanpercentile(_ndat, 28, axis=1)
  929. assert_almost_equal(res, tgt)
  930. # Transpose the array to fit the output convention of numpy.percentile
  931. tgt = np.transpose([np.percentile(d, (28, 98)) for d in _rdat])
  932. res = np.nanpercentile(_ndat, (28, 98), axis=1)
  933. assert_almost_equal(res, tgt)
  934. @pytest.mark.parametrize("axis", [None, 0, 1])
  935. @pytest.mark.parametrize("dtype", np.typecodes["Float"])
  936. @pytest.mark.parametrize("array", [
  937. np.array(np.nan),
  938. np.full((3, 3), np.nan),
  939. ], ids=["0d", "2d"])
  940. def test_allnans(self, axis, dtype, array):
  941. if axis is not None and array.ndim == 0:
  942. pytest.skip(f"`axis != None` not supported for 0d arrays")
  943. array = array.astype(dtype)
  944. with pytest.warns(RuntimeWarning, match="All-NaN slice encountered"):
  945. out = np.nanpercentile(array, 60, axis=axis)
  946. assert np.isnan(out).all()
  947. assert out.dtype == array.dtype
  948. def test_empty(self):
  949. mat = np.zeros((0, 3))
  950. for axis in [0, None]:
  951. with warnings.catch_warnings(record=True) as w:
  952. warnings.simplefilter('always')
  953. assert_(np.isnan(np.nanpercentile(mat, 40, axis=axis)).all())
  954. assert_(len(w) == 1)
  955. assert_(issubclass(w[0].category, RuntimeWarning))
  956. for axis in [1]:
  957. with warnings.catch_warnings(record=True) as w:
  958. warnings.simplefilter('always')
  959. assert_equal(np.nanpercentile(mat, 40, axis=axis), np.zeros([]))
  960. assert_(len(w) == 0)
  961. def test_scalar(self):
  962. assert_equal(np.nanpercentile(0., 100), 0.)
  963. a = np.arange(6)
  964. r = np.nanpercentile(a, 50, axis=0)
  965. assert_equal(r, 2.5)
  966. assert_(np.isscalar(r))
  967. def test_extended_axis_invalid(self):
  968. d = np.ones((3, 5, 7, 11))
  969. assert_raises(np.AxisError, np.nanpercentile, d, q=5, axis=-5)
  970. assert_raises(np.AxisError, np.nanpercentile, d, q=5, axis=(0, -5))
  971. assert_raises(np.AxisError, np.nanpercentile, d, q=5, axis=4)
  972. assert_raises(np.AxisError, np.nanpercentile, d, q=5, axis=(0, 4))
  973. assert_raises(ValueError, np.nanpercentile, d, q=5, axis=(1, 1))
  974. def test_multiple_percentiles(self):
  975. perc = [50, 100]
  976. mat = np.ones((4, 3))
  977. nan_mat = np.nan * mat
  978. # For checking consistency in higher dimensional case
  979. large_mat = np.ones((3, 4, 5))
  980. large_mat[:, 0:2:4, :] = 0
  981. large_mat[:, :, 3:] *= 2
  982. for axis in [None, 0, 1]:
  983. for keepdim in [False, True]:
  984. with suppress_warnings() as sup:
  985. sup.filter(RuntimeWarning, "All-NaN slice encountered")
  986. val = np.percentile(mat, perc, axis=axis, keepdims=keepdim)
  987. nan_val = np.nanpercentile(nan_mat, perc, axis=axis,
  988. keepdims=keepdim)
  989. assert_equal(nan_val.shape, val.shape)
  990. val = np.percentile(large_mat, perc, axis=axis,
  991. keepdims=keepdim)
  992. nan_val = np.nanpercentile(large_mat, perc, axis=axis,
  993. keepdims=keepdim)
  994. assert_equal(nan_val, val)
  995. megamat = np.ones((3, 4, 5, 6))
  996. assert_equal(np.nanpercentile(megamat, perc, axis=(1, 2)).shape, (2, 3, 6))
  997. class TestNanFunctions_Quantile:
  998. # most of this is already tested by TestPercentile
  999. def test_regression(self):
  1000. ar = np.arange(24).reshape(2, 3, 4).astype(float)
  1001. ar[0][1] = np.nan
  1002. assert_equal(np.nanquantile(ar, q=0.5), np.nanpercentile(ar, q=50))
  1003. assert_equal(np.nanquantile(ar, q=0.5, axis=0),
  1004. np.nanpercentile(ar, q=50, axis=0))
  1005. assert_equal(np.nanquantile(ar, q=0.5, axis=1),
  1006. np.nanpercentile(ar, q=50, axis=1))
  1007. assert_equal(np.nanquantile(ar, q=[0.5], axis=1),
  1008. np.nanpercentile(ar, q=[50], axis=1))
  1009. assert_equal(np.nanquantile(ar, q=[0.25, 0.5, 0.75], axis=1),
  1010. np.nanpercentile(ar, q=[25, 50, 75], axis=1))
  1011. def test_basic(self):
  1012. x = np.arange(8) * 0.5
  1013. assert_equal(np.nanquantile(x, 0), 0.)
  1014. assert_equal(np.nanquantile(x, 1), 3.5)
  1015. assert_equal(np.nanquantile(x, 0.5), 1.75)
  1016. def test_complex(self):
  1017. arr_c = np.array([0.5+3.0j, 2.1+0.5j, 1.6+2.3j], dtype='G')
  1018. assert_raises(TypeError, np.nanquantile, arr_c, 0.5)
  1019. arr_c = np.array([0.5+3.0j, 2.1+0.5j, 1.6+2.3j], dtype='D')
  1020. assert_raises(TypeError, np.nanquantile, arr_c, 0.5)
  1021. arr_c = np.array([0.5+3.0j, 2.1+0.5j, 1.6+2.3j], dtype='F')
  1022. assert_raises(TypeError, np.nanquantile, arr_c, 0.5)
  1023. def test_no_p_overwrite(self):
  1024. # this is worth retesting, because quantile does not make a copy
  1025. p0 = np.array([0, 0.75, 0.25, 0.5, 1.0])
  1026. p = p0.copy()
  1027. np.nanquantile(np.arange(100.), p, method="midpoint")
  1028. assert_array_equal(p, p0)
  1029. p0 = p0.tolist()
  1030. p = p.tolist()
  1031. np.nanquantile(np.arange(100.), p, method="midpoint")
  1032. assert_array_equal(p, p0)
  1033. @pytest.mark.parametrize("axis", [None, 0, 1])
  1034. @pytest.mark.parametrize("dtype", np.typecodes["Float"])
  1035. @pytest.mark.parametrize("array", [
  1036. np.array(np.nan),
  1037. np.full((3, 3), np.nan),
  1038. ], ids=["0d", "2d"])
  1039. def test_allnans(self, axis, dtype, array):
  1040. if axis is not None and array.ndim == 0:
  1041. pytest.skip(f"`axis != None` not supported for 0d arrays")
  1042. array = array.astype(dtype)
  1043. with pytest.warns(RuntimeWarning, match="All-NaN slice encountered"):
  1044. out = np.nanquantile(array, 1, axis=axis)
  1045. assert np.isnan(out).all()
  1046. assert out.dtype == array.dtype
  1047. @pytest.mark.parametrize("arr, expected", [
  1048. # array of floats with some nans
  1049. (np.array([np.nan, 5.0, np.nan, np.inf]),
  1050. np.array([False, True, False, True])),
  1051. # int64 array that can't possibly have nans
  1052. (np.array([1, 5, 7, 9], dtype=np.int64),
  1053. True),
  1054. # bool array that can't possibly have nans
  1055. (np.array([False, True, False, True]),
  1056. True),
  1057. # 2-D complex array with nans
  1058. (np.array([[np.nan, 5.0],
  1059. [np.nan, np.inf]], dtype=np.complex64),
  1060. np.array([[False, True],
  1061. [False, True]])),
  1062. ])
  1063. def test__nan_mask(arr, expected):
  1064. for out in [None, np.empty(arr.shape, dtype=np.bool_)]:
  1065. actual = _nan_mask(arr, out=out)
  1066. assert_equal(actual, expected)
  1067. # the above won't distinguish between True proper
  1068. # and an array of True values; we want True proper
  1069. # for types that can't possibly contain NaN
  1070. if type(expected) is not np.ndarray:
  1071. assert actual is True
  1072. def test__replace_nan():
  1073. """ Test that _replace_nan returns the original array if there are no
  1074. NaNs, not a copy.
  1075. """
  1076. for dtype in [np.bool_, np.int32, np.int64]:
  1077. arr = np.array([0, 1], dtype=dtype)
  1078. result, mask = _replace_nan(arr, 0)
  1079. assert mask is None
  1080. # do not make a copy if there are no nans
  1081. assert result is arr
  1082. for dtype in [np.float32, np.float64]:
  1083. arr = np.array([0, 1], dtype=dtype)
  1084. result, mask = _replace_nan(arr, 2)
  1085. assert (mask == False).all()
  1086. # mask is not None, so we make a copy
  1087. assert result is not arr
  1088. assert_equal(result, arr)
  1089. arr_nan = np.array([0, 1, np.nan], dtype=dtype)
  1090. result_nan, mask_nan = _replace_nan(arr_nan, 2)
  1091. assert_equal(mask_nan, np.array([False, False, True]))
  1092. assert result_nan is not arr_nan
  1093. assert_equal(result_nan, np.array([0, 1, 2]))
  1094. assert np.isnan(arr_nan[-1])