extras.py 57 KB

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