extras.py 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321
  1. """
  2. Masked arrays add-ons.
  3. A collection of utilities for `numpy.ma`.
  4. :author: Pierre Gerard-Marchant
  5. :contact: pierregm_at_uga_dot_edu
  6. :version: $Id: extras.py 3473 2007-10-29 15:18:13Z jarrod.millman $
  7. """
  8. __all__ = [
  9. 'apply_along_axis', 'apply_over_axes', 'atleast_1d', 'atleast_2d',
  10. 'atleast_3d', 'average', 'clump_masked', 'clump_unmasked', 'column_stack',
  11. 'compress_cols', 'compress_nd', 'compress_rowcols', 'compress_rows',
  12. 'count_masked', 'corrcoef', 'cov', 'diagflat', 'dot', 'dstack', 'ediff1d',
  13. 'flatnotmasked_contiguous', 'flatnotmasked_edges', 'hsplit', 'hstack',
  14. 'isin', 'in1d', 'intersect1d', 'mask_cols', 'mask_rowcols', 'mask_rows',
  15. 'masked_all', 'masked_all_like', 'median', 'mr_', 'ndenumerate',
  16. 'notmasked_contiguous', 'notmasked_edges', 'polyfit', 'row_stack',
  17. 'setdiff1d', 'setxor1d', 'stack', 'unique', 'union1d', 'vander', 'vstack',
  18. ]
  19. import itertools
  20. import warnings
  21. from . import core as ma
  22. from .core import (
  23. MaskedArray, MAError, add, array, asarray, concatenate, filled, count,
  24. getmask, getmaskarray, make_mask_descr, masked, masked_array, mask_or,
  25. nomask, ones, sort, zeros, getdata, get_masked_subclass, dot
  26. )
  27. import numpy as np
  28. from numpy import ndarray, array as nxarray
  29. from numpy.lib.array_utils import normalize_axis_index, normalize_axis_tuple
  30. from numpy.lib._function_base_impl import _ureduce
  31. from numpy.lib._index_tricks_impl import AxisConcatenator
  32. def issequence(seq):
  33. """
  34. Is seq a sequence (ndarray, list or tuple)?
  35. """
  36. return isinstance(seq, (ndarray, tuple, list))
  37. def count_masked(arr, axis=None):
  38. """
  39. Count the number of masked elements along the given axis.
  40. Parameters
  41. ----------
  42. arr : array_like
  43. An array with (possibly) masked elements.
  44. axis : int, optional
  45. Axis along which to count. If None (default), a flattened
  46. version of the array is used.
  47. Returns
  48. -------
  49. count : int, ndarray
  50. The total number of masked elements (axis=None) or the number
  51. of masked elements along each slice of the given axis.
  52. See Also
  53. --------
  54. MaskedArray.count : Count non-masked elements.
  55. Examples
  56. --------
  57. >>> import numpy as np
  58. >>> a = np.arange(9).reshape((3,3))
  59. >>> a = np.ma.array(a)
  60. >>> a[1, 0] = np.ma.masked
  61. >>> a[1, 2] = np.ma.masked
  62. >>> a[2, 1] = np.ma.masked
  63. >>> a
  64. masked_array(
  65. data=[[0, 1, 2],
  66. [--, 4, --],
  67. [6, --, 8]],
  68. mask=[[False, False, False],
  69. [ True, False, True],
  70. [False, True, False]],
  71. fill_value=999999)
  72. >>> np.ma.count_masked(a)
  73. 3
  74. When the `axis` keyword is used an array is returned.
  75. >>> np.ma.count_masked(a, axis=0)
  76. array([1, 1, 1])
  77. >>> np.ma.count_masked(a, axis=1)
  78. array([0, 2, 1])
  79. """
  80. m = getmaskarray(arr)
  81. return m.sum(axis)
  82. def masked_all(shape, dtype=float):
  83. """
  84. Empty masked array with all elements masked.
  85. Return an empty masked array of the given shape and dtype, where all the
  86. data are masked.
  87. Parameters
  88. ----------
  89. shape : int or tuple of ints
  90. Shape of the required MaskedArray, e.g., ``(2, 3)`` or ``2``.
  91. dtype : dtype, optional
  92. Data type of the output.
  93. Returns
  94. -------
  95. a : MaskedArray
  96. A masked array with all data masked.
  97. See Also
  98. --------
  99. masked_all_like : Empty masked array modelled on an existing array.
  100. Notes
  101. -----
  102. Unlike other masked array creation functions (e.g. `numpy.ma.zeros`,
  103. `numpy.ma.ones`, `numpy.ma.full`), `masked_all` does not initialize the
  104. values of the array, and may therefore be marginally faster. However,
  105. the values stored in the newly allocated array are arbitrary. For
  106. reproducible behavior, be sure to set each element of the array before
  107. reading.
  108. Examples
  109. --------
  110. >>> import numpy as np
  111. >>> np.ma.masked_all((3, 3))
  112. masked_array(
  113. data=[[--, --, --],
  114. [--, --, --],
  115. [--, --, --]],
  116. mask=[[ True, True, True],
  117. [ True, True, True],
  118. [ True, True, True]],
  119. fill_value=1e+20,
  120. dtype=float64)
  121. The `dtype` parameter defines the underlying data type.
  122. >>> a = np.ma.masked_all((3, 3))
  123. >>> a.dtype
  124. dtype('float64')
  125. >>> a = np.ma.masked_all((3, 3), dtype=np.int32)
  126. >>> a.dtype
  127. dtype('int32')
  128. """
  129. a = masked_array(np.empty(shape, dtype),
  130. mask=np.ones(shape, make_mask_descr(dtype)))
  131. return a
  132. def masked_all_like(arr):
  133. """
  134. Empty masked array with the properties of an existing array.
  135. Return an empty masked array of the same shape and dtype as
  136. the array `arr`, where all the data are masked.
  137. Parameters
  138. ----------
  139. arr : ndarray
  140. An array describing the shape and dtype of the required MaskedArray.
  141. Returns
  142. -------
  143. a : MaskedArray
  144. A masked array with all data masked.
  145. Raises
  146. ------
  147. AttributeError
  148. If `arr` doesn't have a shape attribute (i.e. not an ndarray)
  149. See Also
  150. --------
  151. masked_all : Empty masked array with all elements masked.
  152. Notes
  153. -----
  154. Unlike other masked array creation functions (e.g. `numpy.ma.zeros_like`,
  155. `numpy.ma.ones_like`, `numpy.ma.full_like`), `masked_all_like` does not
  156. initialize the values of the array, and may therefore be marginally
  157. faster. However, the values stored in the newly allocated array are
  158. arbitrary. For reproducible behavior, be sure to set each element of the
  159. array before reading.
  160. Examples
  161. --------
  162. >>> import numpy as np
  163. >>> arr = np.zeros((2, 3), dtype=np.float32)
  164. >>> arr
  165. array([[0., 0., 0.],
  166. [0., 0., 0.]], dtype=float32)
  167. >>> np.ma.masked_all_like(arr)
  168. masked_array(
  169. data=[[--, --, --],
  170. [--, --, --]],
  171. mask=[[ True, True, True],
  172. [ True, True, True]],
  173. fill_value=np.float64(1e+20),
  174. dtype=float32)
  175. The dtype of the masked array matches the dtype of `arr`.
  176. >>> arr.dtype
  177. dtype('float32')
  178. >>> np.ma.masked_all_like(arr).dtype
  179. dtype('float32')
  180. """
  181. a = np.empty_like(arr).view(MaskedArray)
  182. a._mask = np.ones(a.shape, dtype=make_mask_descr(a.dtype))
  183. return a
  184. #####--------------------------------------------------------------------------
  185. #---- --- Standard functions ---
  186. #####--------------------------------------------------------------------------
  187. class _fromnxfunction:
  188. """
  189. Defines a wrapper to adapt NumPy functions to masked arrays.
  190. An instance of `_fromnxfunction` can be called with the same parameters
  191. as the wrapped NumPy function. The docstring of `newfunc` is adapted from
  192. the wrapped function as well, see `getdoc`.
  193. This class should not be used directly. Instead, one of its extensions that
  194. provides support for a specific type of input should be used.
  195. Parameters
  196. ----------
  197. funcname : str
  198. The name of the function to be adapted. The function should be
  199. in the NumPy namespace (i.e. ``np.funcname``).
  200. """
  201. def __init__(self, funcname):
  202. self.__name__ = funcname
  203. self.__qualname__ = funcname
  204. self.__doc__ = self.getdoc()
  205. def getdoc(self):
  206. """
  207. Retrieve the docstring and signature from the function.
  208. The ``__doc__`` attribute of the function is used as the docstring for
  209. the new masked array version of the function. A note on application
  210. of the function to the mask is appended.
  211. Parameters
  212. ----------
  213. None
  214. """
  215. npfunc = getattr(np, self.__name__, None)
  216. doc = getattr(npfunc, '__doc__', None)
  217. if doc:
  218. sig = ma.get_object_signature(npfunc)
  219. doc = ma.doc_note(doc, "The function is applied to both the _data "
  220. "and the _mask, if any.")
  221. if sig:
  222. sig = self.__name__ + sig + "\n\n"
  223. return sig + doc
  224. return
  225. def __call__(self, *args, **params):
  226. pass
  227. class _fromnxfunction_single(_fromnxfunction):
  228. """
  229. A version of `_fromnxfunction` that is called with a single array
  230. argument followed by auxiliary args that are passed verbatim for
  231. both the data and mask calls.
  232. """
  233. def __call__(self, x, *args, **params):
  234. func = getattr(np, self.__name__)
  235. if isinstance(x, ndarray):
  236. _d = func(x.__array__(), *args, **params)
  237. _m = func(getmaskarray(x), *args, **params)
  238. return masked_array(_d, mask=_m)
  239. else:
  240. _d = func(np.asarray(x), *args, **params)
  241. _m = func(getmaskarray(x), *args, **params)
  242. return masked_array(_d, mask=_m)
  243. class _fromnxfunction_seq(_fromnxfunction):
  244. """
  245. A version of `_fromnxfunction` that is called with a single sequence
  246. of arrays followed by auxiliary args that are passed verbatim for
  247. both the data and mask calls.
  248. """
  249. def __call__(self, x, *args, **params):
  250. func = getattr(np, self.__name__)
  251. _d = func(tuple([np.asarray(a) for a in x]), *args, **params)
  252. _m = func(tuple([getmaskarray(a) for a in x]), *args, **params)
  253. return masked_array(_d, mask=_m)
  254. class _fromnxfunction_args(_fromnxfunction):
  255. """
  256. A version of `_fromnxfunction` that is called with multiple array
  257. arguments. The first non-array-like input marks the beginning of the
  258. arguments that are passed verbatim for both the data and mask calls.
  259. Array arguments are processed independently and the results are
  260. returned in a list. If only one array is found, the return value is
  261. just the processed array instead of a list.
  262. """
  263. def __call__(self, *args, **params):
  264. func = getattr(np, self.__name__)
  265. arrays = []
  266. args = list(args)
  267. while len(args) > 0 and issequence(args[0]):
  268. arrays.append(args.pop(0))
  269. res = []
  270. for x in arrays:
  271. _d = func(np.asarray(x), *args, **params)
  272. _m = func(getmaskarray(x), *args, **params)
  273. res.append(masked_array(_d, mask=_m))
  274. if len(arrays) == 1:
  275. return res[0]
  276. return res
  277. class _fromnxfunction_allargs(_fromnxfunction):
  278. """
  279. A version of `_fromnxfunction` that is called with multiple array
  280. arguments. Similar to `_fromnxfunction_args` except that all args
  281. are converted to arrays even if they are not so already. This makes
  282. it possible to process scalars as 1-D arrays. Only keyword arguments
  283. are passed through verbatim for the data and mask calls. Arrays
  284. arguments are processed independently and the results are returned
  285. in a list. If only one arg is present, the return value is just the
  286. processed array instead of a list.
  287. """
  288. def __call__(self, *args, **params):
  289. func = getattr(np, self.__name__)
  290. res = []
  291. for x in args:
  292. _d = func(np.asarray(x), **params)
  293. _m = func(getmaskarray(x), **params)
  294. res.append(masked_array(_d, mask=_m))
  295. if len(args) == 1:
  296. return res[0]
  297. return res
  298. atleast_1d = _fromnxfunction_allargs('atleast_1d')
  299. atleast_2d = _fromnxfunction_allargs('atleast_2d')
  300. atleast_3d = _fromnxfunction_allargs('atleast_3d')
  301. vstack = row_stack = _fromnxfunction_seq('vstack')
  302. hstack = _fromnxfunction_seq('hstack')
  303. column_stack = _fromnxfunction_seq('column_stack')
  304. dstack = _fromnxfunction_seq('dstack')
  305. stack = _fromnxfunction_seq('stack')
  306. hsplit = _fromnxfunction_single('hsplit')
  307. diagflat = _fromnxfunction_single('diagflat')
  308. #####--------------------------------------------------------------------------
  309. #----
  310. #####--------------------------------------------------------------------------
  311. def flatten_inplace(seq):
  312. """Flatten a sequence in place."""
  313. k = 0
  314. while (k != len(seq)):
  315. while hasattr(seq[k], '__iter__'):
  316. seq[k:(k + 1)] = seq[k]
  317. k += 1
  318. return seq
  319. def apply_along_axis(func1d, axis, arr, *args, **kwargs):
  320. """
  321. (This docstring should be overwritten)
  322. """
  323. arr = array(arr, copy=False, subok=True)
  324. nd = arr.ndim
  325. axis = normalize_axis_index(axis, nd)
  326. ind = [0] * (nd - 1)
  327. i = np.zeros(nd, 'O')
  328. indlist = list(range(nd))
  329. indlist.remove(axis)
  330. i[axis] = slice(None, None)
  331. outshape = np.asarray(arr.shape).take(indlist)
  332. i.put(indlist, ind)
  333. res = func1d(arr[tuple(i.tolist())], *args, **kwargs)
  334. # if res is a number, then we have a smaller output array
  335. asscalar = np.isscalar(res)
  336. if not asscalar:
  337. try:
  338. len(res)
  339. except TypeError:
  340. asscalar = True
  341. # Note: we shouldn't set the dtype of the output from the first result
  342. # so we force the type to object, and build a list of dtypes. We'll
  343. # just take the largest, to avoid some downcasting
  344. dtypes = []
  345. if asscalar:
  346. dtypes.append(np.asarray(res).dtype)
  347. outarr = zeros(outshape, object)
  348. outarr[tuple(ind)] = res
  349. Ntot = np.prod(outshape)
  350. k = 1
  351. while k < Ntot:
  352. # increment the index
  353. ind[-1] += 1
  354. n = -1
  355. while (ind[n] >= outshape[n]) and (n > (1 - nd)):
  356. ind[n - 1] += 1
  357. ind[n] = 0
  358. n -= 1
  359. i.put(indlist, ind)
  360. res = func1d(arr[tuple(i.tolist())], *args, **kwargs)
  361. outarr[tuple(ind)] = res
  362. dtypes.append(asarray(res).dtype)
  363. k += 1
  364. else:
  365. res = array(res, copy=False, subok=True)
  366. j = i.copy()
  367. j[axis] = ([slice(None, None)] * res.ndim)
  368. j.put(indlist, ind)
  369. Ntot = np.prod(outshape)
  370. holdshape = outshape
  371. outshape = list(arr.shape)
  372. outshape[axis] = res.shape
  373. dtypes.append(asarray(res).dtype)
  374. outshape = flatten_inplace(outshape)
  375. outarr = zeros(outshape, object)
  376. outarr[tuple(flatten_inplace(j.tolist()))] = res
  377. k = 1
  378. while k < Ntot:
  379. # increment the index
  380. ind[-1] += 1
  381. n = -1
  382. while (ind[n] >= holdshape[n]) and (n > (1 - nd)):
  383. ind[n - 1] += 1
  384. ind[n] = 0
  385. n -= 1
  386. i.put(indlist, ind)
  387. j.put(indlist, ind)
  388. res = func1d(arr[tuple(i.tolist())], *args, **kwargs)
  389. outarr[tuple(flatten_inplace(j.tolist()))] = res
  390. dtypes.append(asarray(res).dtype)
  391. k += 1
  392. max_dtypes = np.dtype(np.asarray(dtypes).max())
  393. if not hasattr(arr, '_mask'):
  394. result = np.asarray(outarr, dtype=max_dtypes)
  395. else:
  396. result = asarray(outarr, dtype=max_dtypes)
  397. result.fill_value = ma.default_fill_value(result)
  398. return result
  399. apply_along_axis.__doc__ = np.apply_along_axis.__doc__
  400. def apply_over_axes(func, a, axes):
  401. """
  402. (This docstring will be overwritten)
  403. """
  404. val = asarray(a)
  405. N = a.ndim
  406. if array(axes).ndim == 0:
  407. axes = (axes,)
  408. for axis in axes:
  409. if axis < 0:
  410. axis = N + axis
  411. args = (val, axis)
  412. res = func(*args)
  413. if res.ndim == val.ndim:
  414. val = res
  415. else:
  416. res = ma.expand_dims(res, axis)
  417. if res.ndim == val.ndim:
  418. val = res
  419. else:
  420. raise ValueError("function is not returning "
  421. "an array of the correct shape")
  422. return val
  423. if apply_over_axes.__doc__ is not None:
  424. apply_over_axes.__doc__ = np.apply_over_axes.__doc__[
  425. :np.apply_over_axes.__doc__.find('Notes')].rstrip() + \
  426. """
  427. Examples
  428. --------
  429. >>> import numpy as np
  430. >>> a = np.ma.arange(24).reshape(2,3,4)
  431. >>> a[:,0,1] = np.ma.masked
  432. >>> a[:,1,:] = np.ma.masked
  433. >>> a
  434. masked_array(
  435. data=[[[0, --, 2, 3],
  436. [--, --, --, --],
  437. [8, 9, 10, 11]],
  438. [[12, --, 14, 15],
  439. [--, --, --, --],
  440. [20, 21, 22, 23]]],
  441. mask=[[[False, True, False, False],
  442. [ True, True, True, True],
  443. [False, False, False, False]],
  444. [[False, True, False, False],
  445. [ True, True, True, True],
  446. [False, False, False, False]]],
  447. fill_value=999999)
  448. >>> np.ma.apply_over_axes(np.ma.sum, a, [0,2])
  449. masked_array(
  450. data=[[[46],
  451. [--],
  452. [124]]],
  453. mask=[[[False],
  454. [ True],
  455. [False]]],
  456. fill_value=999999)
  457. Tuple axis arguments to ufuncs are equivalent:
  458. >>> np.ma.sum(a, axis=(0,2)).reshape((1,-1,1))
  459. masked_array(
  460. data=[[[46],
  461. [--],
  462. [124]]],
  463. mask=[[[False],
  464. [ True],
  465. [False]]],
  466. fill_value=999999)
  467. """
  468. def average(a, axis=None, weights=None, returned=False, *,
  469. keepdims=np._NoValue):
  470. """
  471. Return the weighted average of array over the given axis.
  472. Parameters
  473. ----------
  474. a : array_like
  475. Data to be averaged.
  476. Masked entries are not taken into account in the computation.
  477. axis : None or int or tuple of ints, optional
  478. Axis or axes along which to average `a`. The default,
  479. `axis=None`, will average over all of the elements of the input array.
  480. If axis is a tuple of ints, averaging is performed on all of the axes
  481. specified in the tuple instead of a single axis or all the axes as
  482. before.
  483. weights : array_like, optional
  484. An array of weights associated with the values in `a`. Each value in
  485. `a` contributes to the average according to its associated weight.
  486. The array of weights must be the same shape as `a` if no axis is
  487. specified, otherwise the weights must have dimensions and shape
  488. consistent with `a` along the specified axis.
  489. If `weights=None`, then all data in `a` are assumed to have a
  490. weight equal to one.
  491. The calculation is::
  492. avg = sum(a * weights) / sum(weights)
  493. where the sum is over all included elements.
  494. The only constraint on the values of `weights` is that `sum(weights)`
  495. must not be 0.
  496. returned : bool, optional
  497. Flag indicating whether a tuple ``(result, sum of weights)``
  498. should be returned as output (True), or just the result (False).
  499. Default is False.
  500. keepdims : bool, optional
  501. If this is set to True, the axes which are reduced are left
  502. in the result as dimensions with size one. With this option,
  503. the result will broadcast correctly against the original `a`.
  504. *Note:* `keepdims` will not work with instances of `numpy.matrix`
  505. or other classes whose methods do not support `keepdims`.
  506. .. versionadded:: 1.23.0
  507. Returns
  508. -------
  509. average, [sum_of_weights] : (tuple of) scalar or MaskedArray
  510. The average along the specified axis. When returned is `True`,
  511. return a tuple with the average as the first element and the sum
  512. of the weights as the second element. The return type is `np.float64`
  513. if `a` is of integer type and floats smaller than `float64`, or the
  514. input data-type, otherwise. If returned, `sum_of_weights` is always
  515. `float64`.
  516. Raises
  517. ------
  518. ZeroDivisionError
  519. When all weights along axis are zero. See `numpy.ma.average` for a
  520. version robust to this type of error.
  521. TypeError
  522. When `weights` does not have the same shape as `a`, and `axis=None`.
  523. ValueError
  524. When `weights` does not have dimensions and shape consistent with `a`
  525. along specified `axis`.
  526. Examples
  527. --------
  528. >>> import numpy as np
  529. >>> a = np.ma.array([1., 2., 3., 4.], mask=[False, False, True, True])
  530. >>> np.ma.average(a, weights=[3, 1, 0, 0])
  531. 1.25
  532. >>> x = np.ma.arange(6.).reshape(3, 2)
  533. >>> x
  534. masked_array(
  535. data=[[0., 1.],
  536. [2., 3.],
  537. [4., 5.]],
  538. mask=False,
  539. fill_value=1e+20)
  540. >>> data = np.arange(8).reshape((2, 2, 2))
  541. >>> data
  542. array([[[0, 1],
  543. [2, 3]],
  544. [[4, 5],
  545. [6, 7]]])
  546. >>> np.ma.average(data, axis=(0, 1), weights=[[1./4, 3./4], [1., 1./2]])
  547. masked_array(data=[3.4, 4.4],
  548. mask=[False, False],
  549. fill_value=1e+20)
  550. >>> np.ma.average(data, axis=0, weights=[[1./4, 3./4], [1., 1./2]])
  551. Traceback (most recent call last):
  552. ...
  553. ValueError: Shape of weights must be consistent
  554. with shape of a along specified axis.
  555. >>> avg, sumweights = np.ma.average(x, axis=0, weights=[1, 2, 3],
  556. ... returned=True)
  557. >>> avg
  558. masked_array(data=[2.6666666666666665, 3.6666666666666665],
  559. mask=[False, False],
  560. fill_value=1e+20)
  561. With ``keepdims=True``, the following result has shape (3, 1).
  562. >>> np.ma.average(x, axis=1, keepdims=True)
  563. masked_array(
  564. data=[[0.5],
  565. [2.5],
  566. [4.5]],
  567. mask=False,
  568. fill_value=1e+20)
  569. """
  570. a = asarray(a)
  571. m = getmask(a)
  572. if axis is not None:
  573. axis = normalize_axis_tuple(axis, a.ndim, argname="axis")
  574. if keepdims is np._NoValue:
  575. # Don't pass on the keepdims argument if one wasn't given.
  576. keepdims_kw = {}
  577. else:
  578. keepdims_kw = {'keepdims': keepdims}
  579. if weights is None:
  580. avg = a.mean(axis, **keepdims_kw)
  581. scl = avg.dtype.type(a.count(axis))
  582. else:
  583. wgt = asarray(weights)
  584. if issubclass(a.dtype.type, (np.integer, np.bool)):
  585. result_dtype = np.result_type(a.dtype, wgt.dtype, 'f8')
  586. else:
  587. result_dtype = np.result_type(a.dtype, wgt.dtype)
  588. # Sanity checks
  589. if a.shape != wgt.shape:
  590. if axis is None:
  591. raise TypeError(
  592. "Axis must be specified when shapes of a and weights "
  593. "differ.")
  594. if wgt.shape != tuple(a.shape[ax] for ax in axis):
  595. raise ValueError(
  596. "Shape of weights must be consistent with "
  597. "shape of a along specified axis.")
  598. # setup wgt to broadcast along axis
  599. wgt = wgt.transpose(np.argsort(axis))
  600. wgt = wgt.reshape(tuple((s if ax in axis else 1)
  601. for ax, s in enumerate(a.shape)))
  602. if m is not nomask:
  603. wgt = wgt*(~a.mask)
  604. wgt.mask |= a.mask
  605. scl = wgt.sum(axis=axis, dtype=result_dtype, **keepdims_kw)
  606. avg = np.multiply(a, wgt,
  607. dtype=result_dtype).sum(axis, **keepdims_kw) / scl
  608. if returned:
  609. if scl.shape != avg.shape:
  610. scl = np.broadcast_to(scl, avg.shape).copy()
  611. return avg, scl
  612. else:
  613. return avg
  614. def median(a, axis=None, out=None, overwrite_input=False, keepdims=False):
  615. """
  616. Compute the median along the specified axis.
  617. Returns the median of the array elements.
  618. Parameters
  619. ----------
  620. a : array_like
  621. Input array or object that can be converted to an array.
  622. axis : int, optional
  623. Axis along which the medians are computed. The default (None) is
  624. to compute the median along a flattened version of the array.
  625. out : ndarray, optional
  626. Alternative output array in which to place the result. It must
  627. have the same shape and buffer length as the expected output
  628. but the type will be cast if necessary.
  629. overwrite_input : bool, optional
  630. If True, then allow use of memory of input array (a) for
  631. calculations. The input array will be modified by the call to
  632. median. This will save memory when you do not need to preserve
  633. the contents of the input array. Treat the input as undefined,
  634. but it will probably be fully or partially sorted. Default is
  635. False. Note that, if `overwrite_input` is True, and the input
  636. is not already an `ndarray`, an error will be raised.
  637. keepdims : bool, optional
  638. If this is set to True, the axes which are reduced are left
  639. in the result as dimensions with size one. With this option,
  640. the result will broadcast correctly against the input array.
  641. Returns
  642. -------
  643. median : ndarray
  644. A new array holding the result is returned unless out is
  645. specified, in which case a reference to out is returned.
  646. Return data-type is `float64` for integers and floats smaller than
  647. `float64`, or the input data-type, otherwise.
  648. See Also
  649. --------
  650. mean
  651. Notes
  652. -----
  653. Given a vector ``V`` with ``N`` non masked values, the median of ``V``
  654. is the middle value of a sorted copy of ``V`` (``Vs``) - i.e.
  655. ``Vs[(N-1)/2]``, when ``N`` is odd, or ``{Vs[N/2 - 1] + Vs[N/2]}/2``
  656. when ``N`` is even.
  657. Examples
  658. --------
  659. >>> import numpy as np
  660. >>> x = np.ma.array(np.arange(8), mask=[0]*4 + [1]*4)
  661. >>> np.ma.median(x)
  662. 1.5
  663. >>> x = np.ma.array(np.arange(10).reshape(2, 5), mask=[0]*6 + [1]*4)
  664. >>> np.ma.median(x)
  665. 2.5
  666. >>> np.ma.median(x, axis=-1, overwrite_input=True)
  667. masked_array(data=[2.0, 5.0],
  668. mask=[False, False],
  669. fill_value=1e+20)
  670. """
  671. if not hasattr(a, 'mask'):
  672. m = np.median(getdata(a, subok=True), axis=axis,
  673. out=out, overwrite_input=overwrite_input,
  674. keepdims=keepdims)
  675. if isinstance(m, np.ndarray) and 1 <= m.ndim:
  676. return masked_array(m, copy=False)
  677. else:
  678. return m
  679. return _ureduce(a, func=_median, keepdims=keepdims, axis=axis, out=out,
  680. overwrite_input=overwrite_input)
  681. def _median(a, axis=None, out=None, overwrite_input=False):
  682. # when an unmasked NaN is present return it, so we need to sort the NaN
  683. # values behind the mask
  684. if np.issubdtype(a.dtype, np.inexact):
  685. fill_value = np.inf
  686. else:
  687. fill_value = None
  688. if overwrite_input:
  689. if axis is None:
  690. asorted = a.ravel()
  691. asorted.sort(fill_value=fill_value)
  692. else:
  693. a.sort(axis=axis, fill_value=fill_value)
  694. asorted = a
  695. else:
  696. asorted = sort(a, axis=axis, fill_value=fill_value)
  697. if axis is None:
  698. axis = 0
  699. else:
  700. axis = normalize_axis_index(axis, asorted.ndim)
  701. if asorted.shape[axis] == 0:
  702. # for empty axis integer indices fail so use slicing to get same result
  703. # as median (which is mean of empty slice = nan)
  704. indexer = [slice(None)] * asorted.ndim
  705. indexer[axis] = slice(0, 0)
  706. indexer = tuple(indexer)
  707. return np.ma.mean(asorted[indexer], axis=axis, out=out)
  708. if asorted.ndim == 1:
  709. idx, odd = divmod(count(asorted), 2)
  710. mid = asorted[idx + odd - 1:idx + 1]
  711. if np.issubdtype(asorted.dtype, np.inexact) and asorted.size > 0:
  712. # avoid inf / x = masked
  713. s = mid.sum(out=out)
  714. if not odd:
  715. s = np.true_divide(s, 2., casting='safe', out=out)
  716. s = np.lib._utils_impl._median_nancheck(asorted, s, axis)
  717. else:
  718. s = mid.mean(out=out)
  719. # if result is masked either the input contained enough
  720. # minimum_fill_value so that it would be the median or all values
  721. # masked
  722. if np.ma.is_masked(s) and not np.all(asorted.mask):
  723. return np.ma.minimum_fill_value(asorted)
  724. return s
  725. counts = count(asorted, axis=axis, keepdims=True)
  726. h = counts // 2
  727. # duplicate high if odd number of elements so mean does nothing
  728. odd = counts % 2 == 1
  729. l = np.where(odd, h, h-1)
  730. lh = np.concatenate([l,h], axis=axis)
  731. # get low and high median
  732. low_high = np.take_along_axis(asorted, lh, axis=axis)
  733. def replace_masked(s):
  734. # Replace masked entries with minimum_full_value unless it all values
  735. # are masked. This is required as the sort order of values equal or
  736. # larger than the fill value is undefined and a valid value placed
  737. # elsewhere, e.g. [4, --, inf].
  738. if np.ma.is_masked(s):
  739. rep = (~np.all(asorted.mask, axis=axis, keepdims=True)) & s.mask
  740. s.data[rep] = np.ma.minimum_fill_value(asorted)
  741. s.mask[rep] = False
  742. replace_masked(low_high)
  743. if np.issubdtype(asorted.dtype, np.inexact):
  744. # avoid inf / x = masked
  745. s = np.ma.sum(low_high, axis=axis, out=out)
  746. np.true_divide(s.data, 2., casting='unsafe', out=s.data)
  747. s = np.lib._utils_impl._median_nancheck(asorted, s, axis)
  748. else:
  749. s = np.ma.mean(low_high, axis=axis, out=out)
  750. return s
  751. def compress_nd(x, axis=None):
  752. """Suppress slices from multiple dimensions which contain masked values.
  753. Parameters
  754. ----------
  755. x : array_like, MaskedArray
  756. The array to operate on. If not a MaskedArray instance (or if no array
  757. elements are masked), `x` is interpreted as a MaskedArray with `mask`
  758. set to `nomask`.
  759. axis : tuple of ints or int, optional
  760. Which dimensions to suppress slices from can be configured with this
  761. parameter.
  762. - If axis is a tuple of ints, those are the axes to suppress slices from.
  763. - If axis is an int, then that is the only axis to suppress slices from.
  764. - If axis is None, all axis are selected.
  765. Returns
  766. -------
  767. compress_array : ndarray
  768. The compressed array.
  769. Examples
  770. --------
  771. >>> import numpy as np
  772. >>> arr = [[1, 2], [3, 4]]
  773. >>> mask = [[0, 1], [0, 0]]
  774. >>> x = np.ma.array(arr, mask=mask)
  775. >>> np.ma.compress_nd(x, axis=0)
  776. array([[3, 4]])
  777. >>> np.ma.compress_nd(x, axis=1)
  778. array([[1],
  779. [3]])
  780. >>> np.ma.compress_nd(x)
  781. array([[3]])
  782. """
  783. x = asarray(x)
  784. m = getmask(x)
  785. # Set axis to tuple of ints
  786. if axis is None:
  787. axis = tuple(range(x.ndim))
  788. else:
  789. axis = normalize_axis_tuple(axis, x.ndim)
  790. # Nothing is masked: return x
  791. if m is nomask or not m.any():
  792. return x._data
  793. # All is masked: return empty
  794. if m.all():
  795. return nxarray([])
  796. # Filter elements through boolean indexing
  797. data = x._data
  798. for ax in axis:
  799. axes = tuple(list(range(ax)) + list(range(ax + 1, x.ndim)))
  800. data = data[(slice(None),)*ax + (~m.any(axis=axes),)]
  801. return data
  802. def compress_rowcols(x, axis=None):
  803. """
  804. Suppress the rows and/or columns of a 2-D array that contain
  805. masked values.
  806. The suppression behavior is selected with the `axis` parameter.
  807. - If axis is None, both rows and columns are suppressed.
  808. - If axis is 0, only rows are suppressed.
  809. - If axis is 1 or -1, only columns are suppressed.
  810. Parameters
  811. ----------
  812. x : array_like, MaskedArray
  813. The array to operate on. If not a MaskedArray instance (or if no array
  814. elements are masked), `x` is interpreted as a MaskedArray with
  815. `mask` set to `nomask`. Must be a 2D array.
  816. axis : int, optional
  817. Axis along which to perform the operation. Default is None.
  818. Returns
  819. -------
  820. compressed_array : ndarray
  821. The compressed array.
  822. Examples
  823. --------
  824. >>> import numpy as np
  825. >>> x = np.ma.array(np.arange(9).reshape(3, 3), mask=[[1, 0, 0],
  826. ... [1, 0, 0],
  827. ... [0, 0, 0]])
  828. >>> x
  829. masked_array(
  830. data=[[--, 1, 2],
  831. [--, 4, 5],
  832. [6, 7, 8]],
  833. mask=[[ True, False, False],
  834. [ True, False, False],
  835. [False, False, False]],
  836. fill_value=999999)
  837. >>> np.ma.compress_rowcols(x)
  838. array([[7, 8]])
  839. >>> np.ma.compress_rowcols(x, 0)
  840. array([[6, 7, 8]])
  841. >>> np.ma.compress_rowcols(x, 1)
  842. array([[1, 2],
  843. [4, 5],
  844. [7, 8]])
  845. """
  846. if asarray(x).ndim != 2:
  847. raise NotImplementedError("compress_rowcols works for 2D arrays only.")
  848. return compress_nd(x, axis=axis)
  849. def compress_rows(a):
  850. """
  851. Suppress whole rows of a 2-D array that contain masked values.
  852. This is equivalent to ``np.ma.compress_rowcols(a, 0)``, see
  853. `compress_rowcols` for details.
  854. Parameters
  855. ----------
  856. x : array_like, MaskedArray
  857. The array to operate on. If not a MaskedArray instance (or if no array
  858. elements are masked), `x` is interpreted as a MaskedArray with
  859. `mask` set to `nomask`. Must be a 2D array.
  860. Returns
  861. -------
  862. compressed_array : ndarray
  863. The compressed array.
  864. See Also
  865. --------
  866. compress_rowcols
  867. Examples
  868. --------
  869. >>> import numpy as np
  870. >>> a = np.ma.array(np.arange(9).reshape(3, 3), mask=[[1, 0, 0],
  871. ... [1, 0, 0],
  872. ... [0, 0, 0]])
  873. >>> np.ma.compress_rows(a)
  874. array([[6, 7, 8]])
  875. """
  876. a = asarray(a)
  877. if a.ndim != 2:
  878. raise NotImplementedError("compress_rows works for 2D arrays only.")
  879. return compress_rowcols(a, 0)
  880. def compress_cols(a):
  881. """
  882. Suppress whole columns of a 2-D array that contain masked values.
  883. This is equivalent to ``np.ma.compress_rowcols(a, 1)``, see
  884. `compress_rowcols` for details.
  885. Parameters
  886. ----------
  887. x : array_like, MaskedArray
  888. The array to operate on. If not a MaskedArray instance (or if no array
  889. elements are masked), `x` is interpreted as a MaskedArray with
  890. `mask` set to `nomask`. Must be a 2D array.
  891. Returns
  892. -------
  893. compressed_array : ndarray
  894. The compressed array.
  895. See Also
  896. --------
  897. compress_rowcols
  898. Examples
  899. --------
  900. >>> import numpy as np
  901. >>> a = np.ma.array(np.arange(9).reshape(3, 3), mask=[[1, 0, 0],
  902. ... [1, 0, 0],
  903. ... [0, 0, 0]])
  904. >>> np.ma.compress_cols(a)
  905. array([[1, 2],
  906. [4, 5],
  907. [7, 8]])
  908. """
  909. a = asarray(a)
  910. if a.ndim != 2:
  911. raise NotImplementedError("compress_cols works for 2D arrays only.")
  912. return compress_rowcols(a, 1)
  913. def mask_rowcols(a, axis=None):
  914. """
  915. Mask rows and/or columns of a 2D array that contain masked values.
  916. Mask whole rows and/or columns of a 2D array that contain
  917. masked values. The masking behavior is selected using the
  918. `axis` parameter.
  919. - If `axis` is None, rows *and* columns are masked.
  920. - If `axis` is 0, only rows are masked.
  921. - If `axis` is 1 or -1, only columns are masked.
  922. Parameters
  923. ----------
  924. a : array_like, MaskedArray
  925. The array to mask. If not a MaskedArray instance (or if no array
  926. elements are masked), the result is a MaskedArray with `mask` set
  927. to `nomask` (False). Must be a 2D array.
  928. axis : int, optional
  929. Axis along which to perform the operation. If None, applies to a
  930. flattened version of the array.
  931. Returns
  932. -------
  933. a : MaskedArray
  934. A modified version of the input array, masked depending on the value
  935. of the `axis` parameter.
  936. Raises
  937. ------
  938. NotImplementedError
  939. If input array `a` is not 2D.
  940. See Also
  941. --------
  942. mask_rows : Mask rows of a 2D array that contain masked values.
  943. mask_cols : Mask cols of a 2D array that contain masked values.
  944. masked_where : Mask where a condition is met.
  945. Notes
  946. -----
  947. The input array's mask is modified by this function.
  948. Examples
  949. --------
  950. >>> import numpy as np
  951. >>> a = np.zeros((3, 3), dtype=int)
  952. >>> a[1, 1] = 1
  953. >>> a
  954. array([[0, 0, 0],
  955. [0, 1, 0],
  956. [0, 0, 0]])
  957. >>> a = np.ma.masked_equal(a, 1)
  958. >>> a
  959. masked_array(
  960. data=[[0, 0, 0],
  961. [0, --, 0],
  962. [0, 0, 0]],
  963. mask=[[False, False, False],
  964. [False, True, False],
  965. [False, False, False]],
  966. fill_value=1)
  967. >>> np.ma.mask_rowcols(a)
  968. masked_array(
  969. data=[[0, --, 0],
  970. [--, --, --],
  971. [0, --, 0]],
  972. mask=[[False, True, False],
  973. [ True, True, True],
  974. [False, True, False]],
  975. fill_value=1)
  976. """
  977. a = array(a, subok=False)
  978. if a.ndim != 2:
  979. raise NotImplementedError("mask_rowcols works for 2D arrays only.")
  980. m = getmask(a)
  981. # Nothing is masked: return a
  982. if m is nomask or not m.any():
  983. return a
  984. maskedval = m.nonzero()
  985. a._mask = a._mask.copy()
  986. if not axis:
  987. a[np.unique(maskedval[0])] = masked
  988. if axis in [None, 1, -1]:
  989. a[:, np.unique(maskedval[1])] = masked
  990. return a
  991. def mask_rows(a, axis=np._NoValue):
  992. """
  993. Mask rows of a 2D array that contain masked values.
  994. This function is a shortcut to ``mask_rowcols`` with `axis` equal to 0.
  995. See Also
  996. --------
  997. mask_rowcols : Mask rows and/or columns of a 2D array.
  998. masked_where : Mask where a condition is met.
  999. Examples
  1000. --------
  1001. >>> import numpy as np
  1002. >>> a = np.zeros((3, 3), dtype=int)
  1003. >>> a[1, 1] = 1
  1004. >>> a
  1005. array([[0, 0, 0],
  1006. [0, 1, 0],
  1007. [0, 0, 0]])
  1008. >>> a = np.ma.masked_equal(a, 1)
  1009. >>> a
  1010. masked_array(
  1011. data=[[0, 0, 0],
  1012. [0, --, 0],
  1013. [0, 0, 0]],
  1014. mask=[[False, False, False],
  1015. [False, True, False],
  1016. [False, False, False]],
  1017. fill_value=1)
  1018. >>> np.ma.mask_rows(a)
  1019. masked_array(
  1020. data=[[0, 0, 0],
  1021. [--, --, --],
  1022. [0, 0, 0]],
  1023. mask=[[False, False, False],
  1024. [ True, True, True],
  1025. [False, False, False]],
  1026. fill_value=1)
  1027. """
  1028. if axis is not np._NoValue:
  1029. # remove the axis argument when this deprecation expires
  1030. # NumPy 1.18.0, 2019-11-28
  1031. warnings.warn(
  1032. "The axis argument has always been ignored, in future passing it "
  1033. "will raise TypeError", DeprecationWarning, stacklevel=2)
  1034. return mask_rowcols(a, 0)
  1035. def mask_cols(a, axis=np._NoValue):
  1036. """
  1037. Mask columns of a 2D array that contain masked values.
  1038. This function is a shortcut to ``mask_rowcols`` with `axis` equal to 1.
  1039. See Also
  1040. --------
  1041. mask_rowcols : Mask rows and/or columns of a 2D array.
  1042. masked_where : Mask where a condition is met.
  1043. Examples
  1044. --------
  1045. >>> import numpy as np
  1046. >>> a = np.zeros((3, 3), dtype=int)
  1047. >>> a[1, 1] = 1
  1048. >>> a
  1049. array([[0, 0, 0],
  1050. [0, 1, 0],
  1051. [0, 0, 0]])
  1052. >>> a = np.ma.masked_equal(a, 1)
  1053. >>> a
  1054. masked_array(
  1055. data=[[0, 0, 0],
  1056. [0, --, 0],
  1057. [0, 0, 0]],
  1058. mask=[[False, False, False],
  1059. [False, True, False],
  1060. [False, False, False]],
  1061. fill_value=1)
  1062. >>> np.ma.mask_cols(a)
  1063. masked_array(
  1064. data=[[0, --, 0],
  1065. [0, --, 0],
  1066. [0, --, 0]],
  1067. mask=[[False, True, False],
  1068. [False, True, False],
  1069. [False, True, False]],
  1070. fill_value=1)
  1071. """
  1072. if axis is not np._NoValue:
  1073. # remove the axis argument when this deprecation expires
  1074. # NumPy 1.18.0, 2019-11-28
  1075. warnings.warn(
  1076. "The axis argument has always been ignored, in future passing it "
  1077. "will raise TypeError", DeprecationWarning, stacklevel=2)
  1078. return mask_rowcols(a, 1)
  1079. #####--------------------------------------------------------------------------
  1080. #---- --- arraysetops ---
  1081. #####--------------------------------------------------------------------------
  1082. def ediff1d(arr, to_end=None, to_begin=None):
  1083. """
  1084. Compute the differences between consecutive elements of an array.
  1085. This function is the equivalent of `numpy.ediff1d` that takes masked
  1086. values into account, see `numpy.ediff1d` for details.
  1087. See Also
  1088. --------
  1089. numpy.ediff1d : Equivalent function for ndarrays.
  1090. Examples
  1091. --------
  1092. >>> import numpy as np
  1093. >>> arr = np.ma.array([1, 2, 4, 7, 0])
  1094. >>> np.ma.ediff1d(arr)
  1095. masked_array(data=[ 1, 2, 3, -7],
  1096. mask=False,
  1097. fill_value=999999)
  1098. """
  1099. arr = ma.asanyarray(arr).flat
  1100. ed = arr[1:] - arr[:-1]
  1101. arrays = [ed]
  1102. #
  1103. if to_begin is not None:
  1104. arrays.insert(0, to_begin)
  1105. if to_end is not None:
  1106. arrays.append(to_end)
  1107. #
  1108. if len(arrays) != 1:
  1109. # We'll save ourselves a copy of a potentially large array in the common
  1110. # case where neither to_begin or to_end was given.
  1111. ed = hstack(arrays)
  1112. #
  1113. return ed
  1114. def unique(ar1, return_index=False, return_inverse=False):
  1115. """
  1116. Finds the unique elements of an array.
  1117. Masked values are considered the same element (masked). The output array
  1118. is always a masked array. See `numpy.unique` for more details.
  1119. See Also
  1120. --------
  1121. numpy.unique : Equivalent function for ndarrays.
  1122. Examples
  1123. --------
  1124. >>> import numpy as np
  1125. >>> a = [1, 2, 1000, 2, 3]
  1126. >>> mask = [0, 0, 1, 0, 0]
  1127. >>> masked_a = np.ma.masked_array(a, mask)
  1128. >>> masked_a
  1129. masked_array(data=[1, 2, --, 2, 3],
  1130. mask=[False, False, True, False, False],
  1131. fill_value=999999)
  1132. >>> np.ma.unique(masked_a)
  1133. masked_array(data=[1, 2, 3, --],
  1134. mask=[False, False, False, True],
  1135. fill_value=999999)
  1136. >>> np.ma.unique(masked_a, return_index=True)
  1137. (masked_array(data=[1, 2, 3, --],
  1138. mask=[False, False, False, True],
  1139. fill_value=999999), array([0, 1, 4, 2]))
  1140. >>> np.ma.unique(masked_a, return_inverse=True)
  1141. (masked_array(data=[1, 2, 3, --],
  1142. mask=[False, False, False, True],
  1143. fill_value=999999), array([0, 1, 3, 1, 2]))
  1144. >>> np.ma.unique(masked_a, return_index=True, return_inverse=True)
  1145. (masked_array(data=[1, 2, 3, --],
  1146. mask=[False, False, False, True],
  1147. fill_value=999999), array([0, 1, 4, 2]), array([0, 1, 3, 1, 2]))
  1148. """
  1149. output = np.unique(ar1,
  1150. return_index=return_index,
  1151. return_inverse=return_inverse)
  1152. if isinstance(output, tuple):
  1153. output = list(output)
  1154. output[0] = output[0].view(MaskedArray)
  1155. output = tuple(output)
  1156. else:
  1157. output = output.view(MaskedArray)
  1158. return output
  1159. def intersect1d(ar1, ar2, assume_unique=False):
  1160. """
  1161. Returns the unique elements common to both arrays.
  1162. Masked values are considered equal one to the other.
  1163. The output is always a masked array.
  1164. See `numpy.intersect1d` for more details.
  1165. See Also
  1166. --------
  1167. numpy.intersect1d : Equivalent function for ndarrays.
  1168. Examples
  1169. --------
  1170. >>> import numpy as np
  1171. >>> x = np.ma.array([1, 3, 3, 3], mask=[0, 0, 0, 1])
  1172. >>> y = np.ma.array([3, 1, 1, 1], mask=[0, 0, 0, 1])
  1173. >>> np.ma.intersect1d(x, y)
  1174. masked_array(data=[1, 3, --],
  1175. mask=[False, False, True],
  1176. fill_value=999999)
  1177. """
  1178. if assume_unique:
  1179. aux = ma.concatenate((ar1, ar2))
  1180. else:
  1181. # Might be faster than unique( intersect1d( ar1, ar2 ) )?
  1182. aux = ma.concatenate((unique(ar1), unique(ar2)))
  1183. aux.sort()
  1184. return aux[:-1][aux[1:] == aux[:-1]]
  1185. def setxor1d(ar1, ar2, assume_unique=False):
  1186. """
  1187. Set exclusive-or of 1-D arrays with unique elements.
  1188. The output is always a masked array. See `numpy.setxor1d` for more details.
  1189. See Also
  1190. --------
  1191. numpy.setxor1d : Equivalent function for ndarrays.
  1192. Examples
  1193. --------
  1194. >>> import numpy as np
  1195. >>> ar1 = np.ma.array([1, 2, 3, 2, 4])
  1196. >>> ar2 = np.ma.array([2, 3, 5, 7, 5])
  1197. >>> np.ma.setxor1d(ar1, ar2)
  1198. masked_array(data=[1, 4, 5, 7],
  1199. mask=False,
  1200. fill_value=999999)
  1201. """
  1202. if not assume_unique:
  1203. ar1 = unique(ar1)
  1204. ar2 = unique(ar2)
  1205. aux = ma.concatenate((ar1, ar2), axis=None)
  1206. if aux.size == 0:
  1207. return aux
  1208. aux.sort()
  1209. auxf = aux.filled()
  1210. # flag = ediff1d( aux, to_end = 1, to_begin = 1 ) == 0
  1211. flag = ma.concatenate(([True], (auxf[1:] != auxf[:-1]), [True]))
  1212. # flag2 = ediff1d( flag ) == 0
  1213. flag2 = (flag[1:] == flag[:-1])
  1214. return aux[flag2]
  1215. def in1d(ar1, ar2, assume_unique=False, invert=False):
  1216. """
  1217. Test whether each element of an array is also present in a second
  1218. array.
  1219. The output is always a masked array. See `numpy.in1d` for more details.
  1220. We recommend using :func:`isin` instead of `in1d` for new code.
  1221. See Also
  1222. --------
  1223. isin : Version of this function that preserves the shape of ar1.
  1224. numpy.in1d : Equivalent function for ndarrays.
  1225. Examples
  1226. --------
  1227. >>> import numpy as np
  1228. >>> ar1 = np.ma.array([0, 1, 2, 5, 0])
  1229. >>> ar2 = [0, 2]
  1230. >>> np.ma.in1d(ar1, ar2)
  1231. masked_array(data=[ True, False, True, False, True],
  1232. mask=False,
  1233. fill_value=True)
  1234. """
  1235. if not assume_unique:
  1236. ar1, rev_idx = unique(ar1, return_inverse=True)
  1237. ar2 = unique(ar2)
  1238. ar = ma.concatenate((ar1, ar2))
  1239. # We need this to be a stable sort, so always use 'mergesort'
  1240. # here. The values from the first array should always come before
  1241. # the values from the second array.
  1242. order = ar.argsort(kind='mergesort')
  1243. sar = ar[order]
  1244. if invert:
  1245. bool_ar = (sar[1:] != sar[:-1])
  1246. else:
  1247. bool_ar = (sar[1:] == sar[:-1])
  1248. flag = ma.concatenate((bool_ar, [invert]))
  1249. indx = order.argsort(kind='mergesort')[:len(ar1)]
  1250. if assume_unique:
  1251. return flag[indx]
  1252. else:
  1253. return flag[indx][rev_idx]
  1254. def isin(element, test_elements, assume_unique=False, invert=False):
  1255. """
  1256. Calculates `element in test_elements`, broadcasting over
  1257. `element` only.
  1258. The output is always a masked array of the same shape as `element`.
  1259. See `numpy.isin` for more details.
  1260. See Also
  1261. --------
  1262. in1d : Flattened version of this function.
  1263. numpy.isin : Equivalent function for ndarrays.
  1264. Examples
  1265. --------
  1266. >>> import numpy as np
  1267. >>> element = np.ma.array([1, 2, 3, 4, 5, 6])
  1268. >>> test_elements = [0, 2]
  1269. >>> np.ma.isin(element, test_elements)
  1270. masked_array(data=[False, True, False, False, False, False],
  1271. mask=False,
  1272. fill_value=True)
  1273. """
  1274. element = ma.asarray(element)
  1275. return in1d(element, test_elements, assume_unique=assume_unique,
  1276. invert=invert).reshape(element.shape)
  1277. def union1d(ar1, ar2):
  1278. """
  1279. Union of two arrays.
  1280. The output is always a masked array. See `numpy.union1d` for more details.
  1281. See Also
  1282. --------
  1283. numpy.union1d : Equivalent function for ndarrays.
  1284. Examples
  1285. --------
  1286. >>> import numpy as np
  1287. >>> ar1 = np.ma.array([1, 2, 3, 4])
  1288. >>> ar2 = np.ma.array([3, 4, 5, 6])
  1289. >>> np.ma.union1d(ar1, ar2)
  1290. masked_array(data=[1, 2, 3, 4, 5, 6],
  1291. mask=False,
  1292. fill_value=999999)
  1293. """
  1294. return unique(ma.concatenate((ar1, ar2), axis=None))
  1295. def setdiff1d(ar1, ar2, assume_unique=False):
  1296. """
  1297. Set difference of 1D arrays with unique elements.
  1298. The output is always a masked array. See `numpy.setdiff1d` for more
  1299. details.
  1300. See Also
  1301. --------
  1302. numpy.setdiff1d : Equivalent function for ndarrays.
  1303. Examples
  1304. --------
  1305. >>> import numpy as np
  1306. >>> x = np.ma.array([1, 2, 3, 4], mask=[0, 1, 0, 1])
  1307. >>> np.ma.setdiff1d(x, [1, 2])
  1308. masked_array(data=[3, --],
  1309. mask=[False, True],
  1310. fill_value=999999)
  1311. """
  1312. if assume_unique:
  1313. ar1 = ma.asarray(ar1).ravel()
  1314. else:
  1315. ar1 = unique(ar1)
  1316. ar2 = unique(ar2)
  1317. return ar1[in1d(ar1, ar2, assume_unique=True, invert=True)]
  1318. ###############################################################################
  1319. # Covariance #
  1320. ###############################################################################
  1321. def _covhelper(x, y=None, rowvar=True, allow_masked=True):
  1322. """
  1323. Private function for the computation of covariance and correlation
  1324. coefficients.
  1325. """
  1326. x = ma.array(x, ndmin=2, copy=True, dtype=float)
  1327. xmask = ma.getmaskarray(x)
  1328. # Quick exit if we can't process masked data
  1329. if not allow_masked and xmask.any():
  1330. raise ValueError("Cannot process masked data.")
  1331. #
  1332. if x.shape[0] == 1:
  1333. rowvar = True
  1334. # Make sure that rowvar is either 0 or 1
  1335. rowvar = int(bool(rowvar))
  1336. axis = 1 - rowvar
  1337. if rowvar:
  1338. tup = (slice(None), None)
  1339. else:
  1340. tup = (None, slice(None))
  1341. #
  1342. if y is None:
  1343. # Check if we can guarantee that the integers in the (N - ddof)
  1344. # normalisation can be accurately represented with single-precision
  1345. # before computing the dot product.
  1346. if x.shape[0] > 2 ** 24 or x.shape[1] > 2 ** 24:
  1347. xnm_dtype = np.float64
  1348. else:
  1349. xnm_dtype = np.float32
  1350. xnotmask = np.logical_not(xmask).astype(xnm_dtype)
  1351. else:
  1352. y = array(y, copy=False, ndmin=2, dtype=float)
  1353. ymask = ma.getmaskarray(y)
  1354. if not allow_masked and ymask.any():
  1355. raise ValueError("Cannot process masked data.")
  1356. if xmask.any() or ymask.any():
  1357. if y.shape == x.shape:
  1358. # Define some common mask
  1359. common_mask = np.logical_or(xmask, ymask)
  1360. if common_mask is not nomask:
  1361. xmask = x._mask = y._mask = ymask = common_mask
  1362. x._sharedmask = False
  1363. y._sharedmask = False
  1364. x = ma.concatenate((x, y), axis)
  1365. # Check if we can guarantee that the integers in the (N - ddof)
  1366. # normalisation can be accurately represented with single-precision
  1367. # before computing the dot product.
  1368. if x.shape[0] > 2 ** 24 or x.shape[1] > 2 ** 24:
  1369. xnm_dtype = np.float64
  1370. else:
  1371. xnm_dtype = np.float32
  1372. xnotmask = np.logical_not(np.concatenate((xmask, ymask), axis)).astype(
  1373. xnm_dtype
  1374. )
  1375. x -= x.mean(axis=rowvar)[tup]
  1376. return (x, xnotmask, rowvar)
  1377. def cov(x, y=None, rowvar=True, bias=False, allow_masked=True, ddof=None):
  1378. """
  1379. Estimate the covariance matrix.
  1380. Except for the handling of missing data this function does the same as
  1381. `numpy.cov`. For more details and examples, see `numpy.cov`.
  1382. By default, masked values are recognized as such. If `x` and `y` have the
  1383. same shape, a common mask is allocated: if ``x[i,j]`` is masked, then
  1384. ``y[i,j]`` will also be masked.
  1385. Setting `allow_masked` to False will raise an exception if values are
  1386. missing in either of the input arrays.
  1387. Parameters
  1388. ----------
  1389. x : array_like
  1390. A 1-D or 2-D array containing multiple variables and observations.
  1391. Each row of `x` represents a variable, and each column a single
  1392. observation of all those variables. Also see `rowvar` below.
  1393. y : array_like, optional
  1394. An additional set of variables and observations. `y` has the same
  1395. shape as `x`.
  1396. rowvar : bool, optional
  1397. If `rowvar` is True (default), then each row represents a
  1398. variable, with observations in the columns. Otherwise, the relationship
  1399. is transposed: each column represents a variable, while the rows
  1400. contain observations.
  1401. bias : bool, optional
  1402. Default normalization (False) is by ``(N-1)``, where ``N`` is the
  1403. number of observations given (unbiased estimate). If `bias` is True,
  1404. then normalization is by ``N``. This keyword can be overridden by
  1405. the keyword ``ddof`` in numpy versions >= 1.5.
  1406. allow_masked : bool, optional
  1407. If True, masked values are propagated pair-wise: if a value is masked
  1408. in `x`, the corresponding value is masked in `y`.
  1409. If False, raises a `ValueError` exception when some values are missing.
  1410. ddof : {None, int}, optional
  1411. If not ``None`` normalization is by ``(N - ddof)``, where ``N`` is
  1412. the number of observations; this overrides the value implied by
  1413. ``bias``. The default value is ``None``.
  1414. Raises
  1415. ------
  1416. ValueError
  1417. Raised if some values are missing and `allow_masked` is False.
  1418. See Also
  1419. --------
  1420. numpy.cov
  1421. Examples
  1422. --------
  1423. >>> import numpy as np
  1424. >>> x = np.ma.array([[0, 1], [1, 1]], mask=[0, 1, 0, 1])
  1425. >>> y = np.ma.array([[1, 0], [0, 1]], mask=[0, 0, 1, 1])
  1426. >>> np.ma.cov(x, y)
  1427. masked_array(
  1428. data=[[--, --, --, --],
  1429. [--, --, --, --],
  1430. [--, --, --, --],
  1431. [--, --, --, --]],
  1432. mask=[[ True, True, True, True],
  1433. [ True, True, True, True],
  1434. [ True, True, True, True],
  1435. [ True, True, True, True]],
  1436. fill_value=1e+20,
  1437. dtype=float64)
  1438. """
  1439. # Check inputs
  1440. if ddof is not None and ddof != int(ddof):
  1441. raise ValueError("ddof must be an integer")
  1442. # Set up ddof
  1443. if ddof is None:
  1444. if bias:
  1445. ddof = 0
  1446. else:
  1447. ddof = 1
  1448. (x, xnotmask, rowvar) = _covhelper(x, y, rowvar, allow_masked)
  1449. if not rowvar:
  1450. fact = np.dot(xnotmask.T, xnotmask) - ddof
  1451. mask = np.less_equal(fact, 0, dtype=bool)
  1452. with np.errstate(divide="ignore", invalid="ignore"):
  1453. data = np.dot(filled(x.T, 0), filled(x.conj(), 0)) / fact
  1454. result = ma.array(data, mask=mask).squeeze()
  1455. else:
  1456. fact = np.dot(xnotmask, xnotmask.T) - ddof
  1457. mask = np.less_equal(fact, 0, dtype=bool)
  1458. with np.errstate(divide="ignore", invalid="ignore"):
  1459. data = np.dot(filled(x, 0), filled(x.T.conj(), 0)) / fact
  1460. result = ma.array(data, mask=mask).squeeze()
  1461. return result
  1462. def corrcoef(x, y=None, rowvar=True, bias=np._NoValue, allow_masked=True,
  1463. ddof=np._NoValue):
  1464. """
  1465. Return Pearson product-moment correlation coefficients.
  1466. Except for the handling of missing data this function does the same as
  1467. `numpy.corrcoef`. For more details and examples, see `numpy.corrcoef`.
  1468. Parameters
  1469. ----------
  1470. x : array_like
  1471. A 1-D or 2-D array containing multiple variables and observations.
  1472. Each row of `x` represents a variable, and each column a single
  1473. observation of all those variables. Also see `rowvar` below.
  1474. y : array_like, optional
  1475. An additional set of variables and observations. `y` has the same
  1476. shape as `x`.
  1477. rowvar : bool, optional
  1478. If `rowvar` is True (default), then each row represents a
  1479. variable, with observations in the columns. Otherwise, the relationship
  1480. is transposed: each column represents a variable, while the rows
  1481. contain observations.
  1482. bias : _NoValue, optional
  1483. Has no effect, do not use.
  1484. .. deprecated:: 1.10.0
  1485. allow_masked : bool, optional
  1486. If True, masked values are propagated pair-wise: if a value is masked
  1487. in `x`, the corresponding value is masked in `y`.
  1488. If False, raises an exception. Because `bias` is deprecated, this
  1489. argument needs to be treated as keyword only to avoid a warning.
  1490. ddof : _NoValue, optional
  1491. Has no effect, do not use.
  1492. .. deprecated:: 1.10.0
  1493. See Also
  1494. --------
  1495. numpy.corrcoef : Equivalent function in top-level NumPy module.
  1496. cov : Estimate the covariance matrix.
  1497. Notes
  1498. -----
  1499. This function accepts but discards arguments `bias` and `ddof`. This is
  1500. for backwards compatibility with previous versions of this function. These
  1501. arguments had no effect on the return values of the function and can be
  1502. safely ignored in this and previous versions of numpy.
  1503. Examples
  1504. --------
  1505. >>> import numpy as np
  1506. >>> x = np.ma.array([[0, 1], [1, 1]], mask=[0, 1, 0, 1])
  1507. >>> np.ma.corrcoef(x)
  1508. masked_array(
  1509. data=[[--, --],
  1510. [--, --]],
  1511. mask=[[ True, True],
  1512. [ True, True]],
  1513. fill_value=1e+20,
  1514. dtype=float64)
  1515. """
  1516. msg = 'bias and ddof have no effect and are deprecated'
  1517. if bias is not np._NoValue or ddof is not np._NoValue:
  1518. # 2015-03-15, 1.10
  1519. warnings.warn(msg, DeprecationWarning, stacklevel=2)
  1520. # Estimate the covariance matrix.
  1521. corr = cov(x, y, rowvar, allow_masked=allow_masked)
  1522. # The non-masked version returns a masked value for a scalar.
  1523. try:
  1524. std = ma.sqrt(ma.diagonal(corr))
  1525. except ValueError:
  1526. return ma.MaskedConstant()
  1527. corr /= ma.multiply.outer(std, std)
  1528. return corr
  1529. #####--------------------------------------------------------------------------
  1530. #---- --- Concatenation helpers ---
  1531. #####--------------------------------------------------------------------------
  1532. class MAxisConcatenator(AxisConcatenator):
  1533. """
  1534. Translate slice objects to concatenation along an axis.
  1535. For documentation on usage, see `mr_class`.
  1536. See Also
  1537. --------
  1538. mr_class
  1539. """
  1540. __slots__ = ()
  1541. concatenate = staticmethod(concatenate)
  1542. @classmethod
  1543. def makemat(cls, arr):
  1544. # There used to be a view as np.matrix here, but we may eventually
  1545. # deprecate that class. In preparation, we use the unmasked version
  1546. # to construct the matrix (with copy=False for backwards compatibility
  1547. # with the .view)
  1548. data = super().makemat(arr.data, copy=False)
  1549. return array(data, mask=arr.mask)
  1550. def __getitem__(self, key):
  1551. # matrix builder syntax, like 'a, b; c, d'
  1552. if isinstance(key, str):
  1553. raise MAError("Unavailable for masked array.")
  1554. return super().__getitem__(key)
  1555. class mr_class(MAxisConcatenator):
  1556. """
  1557. Translate slice objects to concatenation along the first axis.
  1558. This is the masked array version of `r_`.
  1559. See Also
  1560. --------
  1561. r_
  1562. Examples
  1563. --------
  1564. >>> import numpy as np
  1565. >>> np.ma.mr_[np.ma.array([1,2,3]), 0, 0, np.ma.array([4,5,6])]
  1566. masked_array(data=[1, 2, 3, ..., 4, 5, 6],
  1567. mask=False,
  1568. fill_value=999999)
  1569. """
  1570. __slots__ = ()
  1571. def __init__(self):
  1572. MAxisConcatenator.__init__(self, 0)
  1573. mr_ = mr_class()
  1574. #####--------------------------------------------------------------------------
  1575. #---- Find unmasked data ---
  1576. #####--------------------------------------------------------------------------
  1577. def ndenumerate(a, compressed=True):
  1578. """
  1579. Multidimensional index iterator.
  1580. Return an iterator yielding pairs of array coordinates and values,
  1581. skipping elements that are masked. With `compressed=False`,
  1582. `ma.masked` is yielded as the value of masked elements. This
  1583. behavior differs from that of `numpy.ndenumerate`, which yields the
  1584. value of the underlying data array.
  1585. Notes
  1586. -----
  1587. .. versionadded:: 1.23.0
  1588. Parameters
  1589. ----------
  1590. a : array_like
  1591. An array with (possibly) masked elements.
  1592. compressed : bool, optional
  1593. If True (default), masked elements are skipped.
  1594. See Also
  1595. --------
  1596. numpy.ndenumerate : Equivalent function ignoring any mask.
  1597. Examples
  1598. --------
  1599. >>> import numpy as np
  1600. >>> a = np.ma.arange(9).reshape((3, 3))
  1601. >>> a[1, 0] = np.ma.masked
  1602. >>> a[1, 2] = np.ma.masked
  1603. >>> a[2, 1] = np.ma.masked
  1604. >>> a
  1605. masked_array(
  1606. data=[[0, 1, 2],
  1607. [--, 4, --],
  1608. [6, --, 8]],
  1609. mask=[[False, False, False],
  1610. [ True, False, True],
  1611. [False, True, False]],
  1612. fill_value=999999)
  1613. >>> for index, x in np.ma.ndenumerate(a):
  1614. ... print(index, x)
  1615. (0, 0) 0
  1616. (0, 1) 1
  1617. (0, 2) 2
  1618. (1, 1) 4
  1619. (2, 0) 6
  1620. (2, 2) 8
  1621. >>> for index, x in np.ma.ndenumerate(a, compressed=False):
  1622. ... print(index, x)
  1623. (0, 0) 0
  1624. (0, 1) 1
  1625. (0, 2) 2
  1626. (1, 0) --
  1627. (1, 1) 4
  1628. (1, 2) --
  1629. (2, 0) 6
  1630. (2, 1) --
  1631. (2, 2) 8
  1632. """
  1633. for it, mask in zip(np.ndenumerate(a), getmaskarray(a).flat):
  1634. if not mask:
  1635. yield it
  1636. elif not compressed:
  1637. yield it[0], masked
  1638. def flatnotmasked_edges(a):
  1639. """
  1640. Find the indices of the first and last unmasked values.
  1641. Expects a 1-D `MaskedArray`, returns None if all values are masked.
  1642. Parameters
  1643. ----------
  1644. a : array_like
  1645. Input 1-D `MaskedArray`
  1646. Returns
  1647. -------
  1648. edges : ndarray or None
  1649. The indices of first and last non-masked value in the array.
  1650. Returns None if all values are masked.
  1651. See Also
  1652. --------
  1653. flatnotmasked_contiguous, notmasked_contiguous, notmasked_edges
  1654. clump_masked, clump_unmasked
  1655. Notes
  1656. -----
  1657. Only accepts 1-D arrays.
  1658. Examples
  1659. --------
  1660. >>> import numpy as np
  1661. >>> a = np.ma.arange(10)
  1662. >>> np.ma.flatnotmasked_edges(a)
  1663. array([0, 9])
  1664. >>> mask = (a < 3) | (a > 8) | (a == 5)
  1665. >>> a[mask] = np.ma.masked
  1666. >>> np.array(a[~a.mask])
  1667. array([3, 4, 6, 7, 8])
  1668. >>> np.ma.flatnotmasked_edges(a)
  1669. array([3, 8])
  1670. >>> a[:] = np.ma.masked
  1671. >>> print(np.ma.flatnotmasked_edges(a))
  1672. None
  1673. """
  1674. m = getmask(a)
  1675. if m is nomask or not np.any(m):
  1676. return np.array([0, a.size - 1])
  1677. unmasked = np.flatnonzero(~m)
  1678. if len(unmasked) > 0:
  1679. return unmasked[[0, -1]]
  1680. else:
  1681. return None
  1682. def notmasked_edges(a, axis=None):
  1683. """
  1684. Find the indices of the first and last unmasked values along an axis.
  1685. If all values are masked, return None. Otherwise, return a list
  1686. of two tuples, corresponding to the indices of the first and last
  1687. unmasked values respectively.
  1688. Parameters
  1689. ----------
  1690. a : array_like
  1691. The input array.
  1692. axis : int, optional
  1693. Axis along which to perform the operation.
  1694. If None (default), applies to a flattened version of the array.
  1695. Returns
  1696. -------
  1697. edges : ndarray or list
  1698. An array of start and end indexes if there are any masked data in
  1699. the array. If there are no masked data in the array, `edges` is a
  1700. list of the first and last index.
  1701. See Also
  1702. --------
  1703. flatnotmasked_contiguous, flatnotmasked_edges, notmasked_contiguous
  1704. clump_masked, clump_unmasked
  1705. Examples
  1706. --------
  1707. >>> import numpy as np
  1708. >>> a = np.arange(9).reshape((3, 3))
  1709. >>> m = np.zeros_like(a)
  1710. >>> m[1:, 1:] = 1
  1711. >>> am = np.ma.array(a, mask=m)
  1712. >>> np.array(am[~am.mask])
  1713. array([0, 1, 2, 3, 6])
  1714. >>> np.ma.notmasked_edges(am)
  1715. array([0, 6])
  1716. """
  1717. a = asarray(a)
  1718. if axis is None or a.ndim == 1:
  1719. return flatnotmasked_edges(a)
  1720. m = getmaskarray(a)
  1721. idx = array(np.indices(a.shape), mask=np.asarray([m] * a.ndim))
  1722. return [tuple([idx[i].min(axis).compressed() for i in range(a.ndim)]),
  1723. tuple([idx[i].max(axis).compressed() for i in range(a.ndim)]), ]
  1724. def flatnotmasked_contiguous(a):
  1725. """
  1726. Find contiguous unmasked data in a masked array.
  1727. Parameters
  1728. ----------
  1729. a : array_like
  1730. The input array.
  1731. Returns
  1732. -------
  1733. slice_list : list
  1734. A sorted sequence of `slice` objects (start index, end index).
  1735. See Also
  1736. --------
  1737. flatnotmasked_edges, notmasked_contiguous, notmasked_edges
  1738. clump_masked, clump_unmasked
  1739. Notes
  1740. -----
  1741. Only accepts 2-D arrays at most.
  1742. Examples
  1743. --------
  1744. >>> import numpy as np
  1745. >>> a = np.ma.arange(10)
  1746. >>> np.ma.flatnotmasked_contiguous(a)
  1747. [slice(0, 10, None)]
  1748. >>> mask = (a < 3) | (a > 8) | (a == 5)
  1749. >>> a[mask] = np.ma.masked
  1750. >>> np.array(a[~a.mask])
  1751. array([3, 4, 6, 7, 8])
  1752. >>> np.ma.flatnotmasked_contiguous(a)
  1753. [slice(3, 5, None), slice(6, 9, None)]
  1754. >>> a[:] = np.ma.masked
  1755. >>> np.ma.flatnotmasked_contiguous(a)
  1756. []
  1757. """
  1758. m = getmask(a)
  1759. if m is nomask:
  1760. return [slice(0, a.size)]
  1761. i = 0
  1762. result = []
  1763. for (k, g) in itertools.groupby(m.ravel()):
  1764. n = len(list(g))
  1765. if not k:
  1766. result.append(slice(i, i + n))
  1767. i += n
  1768. return result
  1769. def notmasked_contiguous(a, axis=None):
  1770. """
  1771. Find contiguous unmasked data in a masked array along the given axis.
  1772. Parameters
  1773. ----------
  1774. a : array_like
  1775. The input array.
  1776. axis : int, optional
  1777. Axis along which to perform the operation.
  1778. If None (default), applies to a flattened version of the array, and this
  1779. is the same as `flatnotmasked_contiguous`.
  1780. Returns
  1781. -------
  1782. endpoints : list
  1783. A list of slices (start and end indexes) of unmasked indexes
  1784. in the array.
  1785. If the input is 2d and axis is specified, the result is a list of lists.
  1786. See Also
  1787. --------
  1788. flatnotmasked_edges, flatnotmasked_contiguous, notmasked_edges
  1789. clump_masked, clump_unmasked
  1790. Notes
  1791. -----
  1792. Only accepts 2-D arrays at most.
  1793. Examples
  1794. --------
  1795. >>> import numpy as np
  1796. >>> a = np.arange(12).reshape((3, 4))
  1797. >>> mask = np.zeros_like(a)
  1798. >>> mask[1:, :-1] = 1; mask[0, 1] = 1; mask[-1, 0] = 0
  1799. >>> ma = np.ma.array(a, mask=mask)
  1800. >>> ma
  1801. masked_array(
  1802. data=[[0, --, 2, 3],
  1803. [--, --, --, 7],
  1804. [8, --, --, 11]],
  1805. mask=[[False, True, False, False],
  1806. [ True, True, True, False],
  1807. [False, True, True, False]],
  1808. fill_value=999999)
  1809. >>> np.array(ma[~ma.mask])
  1810. array([ 0, 2, 3, 7, 8, 11])
  1811. >>> np.ma.notmasked_contiguous(ma)
  1812. [slice(0, 1, None), slice(2, 4, None), slice(7, 9, None), slice(11, 12, None)]
  1813. >>> np.ma.notmasked_contiguous(ma, axis=0)
  1814. [[slice(0, 1, None), slice(2, 3, None)], [], [slice(0, 1, None)], [slice(0, 3, None)]]
  1815. >>> np.ma.notmasked_contiguous(ma, axis=1)
  1816. [[slice(0, 1, None), slice(2, 4, None)], [slice(3, 4, None)], [slice(0, 1, None), slice(3, 4, None)]]
  1817. """
  1818. a = asarray(a)
  1819. nd = a.ndim
  1820. if nd > 2:
  1821. raise NotImplementedError("Currently limited to at most 2D array.")
  1822. if axis is None or nd == 1:
  1823. return flatnotmasked_contiguous(a)
  1824. #
  1825. result = []
  1826. #
  1827. other = (axis + 1) % 2
  1828. idx = [0, 0]
  1829. idx[axis] = slice(None, None)
  1830. #
  1831. for i in range(a.shape[other]):
  1832. idx[other] = i
  1833. result.append(flatnotmasked_contiguous(a[tuple(idx)]))
  1834. return result
  1835. def _ezclump(mask):
  1836. """
  1837. Finds the clumps (groups of data with the same values) for a 1D bool array.
  1838. Returns a series of slices.
  1839. """
  1840. if mask.ndim > 1:
  1841. mask = mask.ravel()
  1842. idx = (mask[1:] ^ mask[:-1]).nonzero()
  1843. idx = idx[0] + 1
  1844. if mask[0]:
  1845. if len(idx) == 0:
  1846. return [slice(0, mask.size)]
  1847. r = [slice(0, idx[0])]
  1848. r.extend((slice(left, right)
  1849. for left, right in zip(idx[1:-1:2], idx[2::2])))
  1850. else:
  1851. if len(idx) == 0:
  1852. return []
  1853. r = [slice(left, right) for left, right in zip(idx[:-1:2], idx[1::2])]
  1854. if mask[-1]:
  1855. r.append(slice(idx[-1], mask.size))
  1856. return r
  1857. def clump_unmasked(a):
  1858. """
  1859. Return list of slices corresponding to the unmasked clumps of a 1-D array.
  1860. (A "clump" is defined as a contiguous region of the array).
  1861. Parameters
  1862. ----------
  1863. a : ndarray
  1864. A one-dimensional masked array.
  1865. Returns
  1866. -------
  1867. slices : list of slice
  1868. The list of slices, one for each continuous region of unmasked
  1869. elements in `a`.
  1870. See Also
  1871. --------
  1872. flatnotmasked_edges, flatnotmasked_contiguous, notmasked_edges
  1873. notmasked_contiguous, clump_masked
  1874. Examples
  1875. --------
  1876. >>> import numpy as np
  1877. >>> a = np.ma.masked_array(np.arange(10))
  1878. >>> a[[0, 1, 2, 6, 8, 9]] = np.ma.masked
  1879. >>> np.ma.clump_unmasked(a)
  1880. [slice(3, 6, None), slice(7, 8, None)]
  1881. """
  1882. mask = getattr(a, '_mask', nomask)
  1883. if mask is nomask:
  1884. return [slice(0, a.size)]
  1885. return _ezclump(~mask)
  1886. def clump_masked(a):
  1887. """
  1888. Returns a list of slices corresponding to the masked clumps of a 1-D array.
  1889. (A "clump" is defined as a contiguous region of the array).
  1890. Parameters
  1891. ----------
  1892. a : ndarray
  1893. A one-dimensional masked array.
  1894. Returns
  1895. -------
  1896. slices : list of slice
  1897. The list of slices, one for each continuous region of masked elements
  1898. in `a`.
  1899. See Also
  1900. --------
  1901. flatnotmasked_edges, flatnotmasked_contiguous, notmasked_edges
  1902. notmasked_contiguous, clump_unmasked
  1903. Examples
  1904. --------
  1905. >>> import numpy as np
  1906. >>> a = np.ma.masked_array(np.arange(10))
  1907. >>> a[[0, 1, 2, 6, 8, 9]] = np.ma.masked
  1908. >>> np.ma.clump_masked(a)
  1909. [slice(0, 3, None), slice(6, 7, None), slice(8, 10, None)]
  1910. """
  1911. mask = ma.getmask(a)
  1912. if mask is nomask:
  1913. return []
  1914. return _ezclump(mask)
  1915. ###############################################################################
  1916. # Polynomial fit #
  1917. ###############################################################################
  1918. def vander(x, n=None):
  1919. """
  1920. Masked values in the input array result in rows of zeros.
  1921. """
  1922. _vander = np.vander(x, n)
  1923. m = getmask(x)
  1924. if m is not nomask:
  1925. _vander[m] = 0
  1926. return _vander
  1927. vander.__doc__ = ma.doc_note(np.vander.__doc__, vander.__doc__)
  1928. def polyfit(x, y, deg, rcond=None, full=False, w=None, cov=False):
  1929. """
  1930. Any masked values in x is propagated in y, and vice-versa.
  1931. """
  1932. x = asarray(x)
  1933. y = asarray(y)
  1934. m = getmask(x)
  1935. if y.ndim == 1:
  1936. m = mask_or(m, getmask(y))
  1937. elif y.ndim == 2:
  1938. my = getmask(mask_rows(y))
  1939. if my is not nomask:
  1940. m = mask_or(m, my[:, 0])
  1941. else:
  1942. raise TypeError("Expected a 1D or 2D array for y!")
  1943. if w is not None:
  1944. w = asarray(w)
  1945. if w.ndim != 1:
  1946. raise TypeError("expected a 1-d array for weights")
  1947. if w.shape[0] != y.shape[0]:
  1948. raise TypeError("expected w and y to have the same length")
  1949. m = mask_or(m, getmask(w))
  1950. if m is not nomask:
  1951. not_m = ~m
  1952. if w is not None:
  1953. w = w[not_m]
  1954. return np.polyfit(x[not_m], y[not_m], deg, rcond, full, w, cov)
  1955. else:
  1956. return np.polyfit(x, y, deg, rcond, full, w, cov)
  1957. polyfit.__doc__ = ma.doc_note(np.polyfit.__doc__, polyfit.__doc__)