extras.py 63 KB

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