ó \ĐKc,@sxdZdZdZdZdZddddd d d d d ddddddddddddddddddd d!d"d#d$d%d&d'd(d)d*d+d,d-d.d/d0g,Zd1d2lZd1d2lZd1d2lZ d1d3lm Z m Z m Z m Z mZmZmZmZmZmZmZmZmZmZmZmZmZmZd1d2lZd1d4lmZm Zd1d2l jj!Z!d1d5l"m#Z#d1d6l$m%Z%d1d7l&m'Z'd8„Z(d2d9„Z*e+d:„Z,d;„Z-d<dcd=„ƒYZ.ej/Z/ej0Z0ej1Z1e.d0ƒZ2Z3e.dƒZ4e.d ƒZ5e.dƒZ6e.dƒZ7e.dƒZ8d>„Z9d?„Z:ej:je:_d2d2e;d@„Z<d2d2e;dA„Z=d2dB„Z>dC„Z?dD„Z@d2dE„ZAd2dF„ZBd2dG„ZCe;dH„ZDd2d2dI„ZEe;e;dJ„ZFe;dK„ZGe;dL„ZHe;dM„ZIdN„ZJe;dO„ZKe'e;e;dP„ƒZLe'dQ„ƒZMe'dR„ƒZNd2eOeOdS„ZPd2eOe;eOdT„ZQd2eOe;eOdU„ZRdVe#fdW„ƒYZSdXeSfdY„ƒYZTeTƒZUdZ„ZVd2d[„ZWd\„ZXd2d]„ZYd^„ZZd_„Z[d`„Z\d2da„Z]e j^ej]je]jƒe]_d2e;db„Z_e j^ej_je_jƒe__d2S(dsË Masked arrays add-ons. A collection of utilities for `numpy.ma`. :author: Pierre Gerard-Marchant :contact: pierregm_at_uga_dot_edu :version: $Id: extras.py 3473 2007-10-29 15:18:13Z jarrod.millman $ s5Pierre GF Gerard-Marchant ($Author: jarrod.millman $)s1.0s$Revision: 3473 $s5$Date: 2007-10-29 17:18:13 +0200 (Mon, 29 Oct 2007) $tapply_along_axist atleast_1dt atleast_2dt atleast_3dtaveraget clump_maskedtclump_unmaskedt column_stackt compress_colstcompress_rowcolst compress_rowst count_maskedtcorrcoeftcovtdiagflattdottdstacktediff1dtflatnotmasked_contiguoustflatnotmasked_edgesthsplitthstacktin1dt intersect1dtintersect1d_nut mask_colst mask_rowcolst mask_rowst masked_alltmasked_all_liketmediantmr_tnotmasked_contiguoustnotmasked_edgestpolyfitt row_stackt setdiff1dt setmember1dtsetxor1dtuniquetunique1dtunion1dtvandertvstacki˙˙˙˙N(t MaskedArraytMAErrortaddtarraytasarrayt concatenatetcounttfilledtgetmaskt getmaskarraytmake_mask_descrtmaskedt masked_arraytmask_ortnomasktonestsorttzeros(tndarrayR/(tAxisConcatenator(tlstsq(t deprecatecCs t|tttfƒrtStS(s+Is seq a sequence (ndarray, list or tuple)?(t isinstanceR>ttupletlisttTruetFalse(tseq((s3/usr/lib/python2.7/site-packages/numpy/ma/extras.pyt issequence6scCst|ƒ}|j|ƒS(s• Count the number of masked elements along the given axis. Parameters ---------- arr : array_like An array with (possibly) masked elements. axis : int, optional Axis along which to count. If None (default), a flattened version of the array is used. Returns ------- count : int, ndarray The total number of masked elements (axis=None) or the number of masked elements along each slice of the given axis. See Also -------- MaskedArray.count : Count non-masked elements. Examples -------- >>> import numpy.ma as ma >>> a = np.arange(9).reshape((3,3)) >>> a = ma.array(a) >>> a[1, 0] = ma.masked >>> a[1, 2] = ma.masked >>> a[2, 1] = ma.masked >>> a masked_array(data = [[0 1 2] [-- 4 --] [6 -- 8]], mask = [[False False False] [ True False True] [False True False]], fill_value=999999) >>> ma.count_masked(a) 3 When the `axis` keyword is used an array is returned. >>> ma.count_masked(a, axis=0) array([1, 1, 1]) >>> ma.count_masked(a, axis=1) array([0, 2, 1]) (R5tsum(tarrtaxistm((s3/usr/lib/python2.7/site-packages/numpy/ma/extras.pyR <s3 cCs4ttj||ƒdtj|t|ƒƒƒ}|S(sç Empty masked array with all elements masked. Return an empty masked array of the given shape and dtype, where all the data are masked. Parameters ---------- shape : tuple Shape of the required MaskedArray. dtype : dtype, optional Data type of the output. Returns ------- a : MaskedArray A masked array with all data masked. See Also -------- masked_all_like : Empty masked array modelled on an existing array. Examples -------- >>> import numpy.ma as ma >>> ma.masked_all((3, 3)) masked_array(data = [[-- -- --] [-- -- --] [-- -- --]], mask = [[ True True True] [ True True True] [ True True True]], fill_value=1e+20) The `dtype` parameter defines the underlying data type. >>> a = ma.masked_all((3, 3)) >>> a.dtype dtype('float64') >>> a = ma.masked_all((3, 3), dtype=np.int32) >>> a.dtype dtype('int32') tmask(R8tnptemptyR;R6(tshapetdtypeta((s3/usr/lib/python2.7/site-packages/numpy/ma/extras.pyRrs/cCs@tj|ƒjtƒ}tj|jdt|jƒƒ|_|S(sz Empty masked array with the properties of an existing array. Return an empty masked array of the same shape and dtype as the array `arr`, where all the data are masked. Parameters ---------- arr : ndarray An array describing the shape and dtype of the required MaskedArray. Returns ------- a : MaskedArray A masked array with all data masked. Raises ------ AttributeError If `arr` doesn't have a shape attribute (i.e. not an ndarray) See Also -------- masked_all : Empty masked array with all elements masked. Examples -------- >>> import numpy.ma as ma >>> arr = np.zeros((2, 3), dtype=np.float32) >>> arr array([[ 0., 0., 0.], [ 0., 0., 0.]], dtype=float32) >>> ma.masked_all_like(arr) masked_array(data = [[-- -- --] [-- -- --]], mask = [[ True True True] [ True True True]], fill_value=1e+20) The dtype of the masked array matches the dtype of `arr`. >>> arr.dtype dtype('float32') >>> ma.masked_all_like(arr).dtype dtype('float32') RQ( RNt empty_liketviewR,R;RPR6RQt_mask(RJRR((s3/usr/lib/python2.7/site-packages/numpy/ma/extras.pyRĽs2$t_fromnxfunctioncBs)eZdZd„Zd„Zd„ZRS(sĂ Defines a wrapper to adapt NumPy functions to masked arrays. An instance of `_fromnxfunction` can be called with the same parameters as the wrapped NumPy function. The docstring of `newfunc` is adapted from the wrapped function as well, see `getdoc`. Parameters ---------- funcname : str The name of the function to be adapted. The function should be in the NumPy namespace (i.e. ``np.funcname``). cCs||_|jƒ|_dS(N(t__name__tgetdoct__doc__(tselftfuncname((s3/usr/lib/python2.7/site-packages/numpy/ma/extras.pyt__init__đs cCsctt|jdƒ}t|ddƒ}|r_|jtj|ƒ}d}dj|||fƒSdS(s  Retrieve the docstring and signature from the function. The ``__doc__`` attribute of the function is used as the docstring for the new masked array version of the function. A note on application of the function to the mask is appended. .. warning:: If the function docstring already contained a Notes section, the new docstring will have two Notes sections instead of appending a note to the existing section. Parameters ---------- None RYsLNotes ----- The function is applied to both the _data and the _mask, if any.s N(tgetattrRNRWtNonetmatget_object_signaturetjoin(RZtnpfunctdoctsigtlocdoc((s3/usr/lib/python2.7/site-packages/numpy/ma/extras.pyRXôsc OsĆtt|jƒ}t|ƒdkr |d}t|tƒrz|tj|ƒ|}|t|ƒ|}t|d|ƒSt|t ƒs˜t|t ƒrÂ|t g|D]}tj|ƒ^qĽƒ|}|t g|D]}t|ƒ^qփ|}t|d|ƒSn¸g}t |ƒ}x<t|ƒdkrZt |dƒrZ|j |j dƒƒqWg} xZ|D]R}|tj|ƒ||Ž}|t|ƒ||Ž}| j t|d|ƒƒqhW| SdS(NiiRM(R]RNRWtlenRBR>R0R5R8RCRDRHtappendtpop( RZtargstparamstfunctxt_dt_mRRtarraystres((s3/usr/lib/python2.7/site-packages/numpy/ma/extras.pyt__call__s* 1. % (RWt __module__RYR\RXRq(((s3/usr/lib/python2.7/site-packages/numpy/ma/extras.pyRVßs  cCs\d}xO|t|ƒkrWx,t||dƒrI|||||d+qW|d7}q W|S(sFlatten a sequence in place.it__iter__i(Rfthasattr(RGtk((s3/usr/lib/python2.7/site-packages/numpy/ma/extras.pytflatten_inplace<s cOs˝t|dtdtƒ}|j}|dkr:||7}n||kr_td||fƒ‚ndg|d}tj|dƒ}t|ƒ}|j|ƒt d d ƒ||>> a = np.ma.array([1., 2., 3., 4.], mask=[False, False, True, True]) >>> np.ma.average(a, weights=[3, 1, 0, 0]) 1.25 >>> x = np.ma.arange(6.).reshape(3, 2) >>> print x [[ 0. 1.] [ 2. 3.] [ 4. 5.]] >>> avg, sumweights = np.ma.average(x, axis=0, weights=[1, 2, 3], ... returned=True) >>> print avg [2.66666666667 3.66666666667] iRKgiRMgđ?RQRwsw[s] * ones(ash, float)saverage: weights wrong shape.s(] * masked_array(ones(ash, float), mask)N((i((i((i(R0RMRPR^R:RItfloattsizeR3traveltumathR.treducet_dataR/RNRfR~tevaltreprRCR{R7RBR,RzR8R>R;( RRRKtweightstreturnedRMtashR•tdtwtwshtnitrR˜((s3/usr/lib/python2.7/site-packages/numpy/ma/extras.pyRžs 1         "$           "        ' "" cCsĽd„}|rM|dkr4|jƒ}|jƒq_|jd|ƒ|}nt|d|ƒ}|dkrz||ƒ}nt|||ƒ}|dk rĄ|}n|S(sA Compute the median along the specified axis. Returns the median of the array elements. Parameters ---------- a : array_like Input array or object that can be converted to an array. axis : int, optional Axis along which the medians are computed. The default (None) is to compute the median along a flattened version of the array. out : ndarray, optional Alternative output array in which to place the result. It must have the same shape and buffer length as the expected output but the type will be cast if necessary. overwrite_input : bool, optional If True, then allow use of memory of input array (a) for calculations. The input array will be modified by the call to median. This will save memory when you do not need to preserve the contents of the input array. Treat the input as undefined, but it will probably be fully or partially sorted. Default is False. Note that, if `overwrite_input` is True, and the input is not already an `ndarray`, an error will be raised. Returns ------- median : ndarray A new array holding the result is returned unless out is specified, in which case a reference to out is returned. Return data-type is `float64` for integers and floats smaller than `float64`, or the input data-type, otherwise. See Also -------- mean Notes ----- Given a vector ``V`` with ``N`` non masked values, the median of ``V`` is the middle value of a sorted copy of ``V`` (``Vs``) - i.e. ``Vs[(N-1)/2]``, when ``N`` is odd, or ``{Vs[N/2 - 1] + Vs[N/2]}/2`` when ``N`` is even. Examples -------- >>> x = np.ma.array(np.arange(8), mask=[0]*4 + [1]*4) >>> np.ma.extras.median(x) 1.5 >>> x = np.ma.array(np.arange(10).reshape(2, 5), mask=[0]*6 + [1]*4) >>> np.ma.extras.median(x) 2.5 >>> np.ma.extras.median(x, axis=-1, overwrite_input=True) masked_array(data = [ 2. 5.], mask = False, fill_value = 1e+20) cSsntt|ƒdƒ}t|dƒ\}}|rFt||dƒ}nt|d|dƒ}||jdƒS(Niii(R3R2tdivmodR~tmean(tdatatcountstidxtrmdtchoice((s3/usr/lib/python2.7/site-packages/numpy/ma/extras.pyt _median1Dis RKN(R^R›R<R(RRRKtouttoverwrite_inputR°tasortedR˜((s3/usr/lib/python2.7/site-packages/numpy/ma/extras.pyR-s<        cCs$t|ƒ}|jdkr'td‚nt|ƒ}|tksL|jƒ rS|jS|jƒritgƒSt t |ƒƒt |j dƒ}}|j ƒ}|sĎx+t j|dƒD]}|j|ƒqľWn|dkr x+t j|dƒD]}|j|ƒqďWn|j|dd…|fS(sş Suppress the rows and/or columns of a 2-D array that contain masked values. The suppression behavior is selected with the `axis` parameter. - If axis is None, both rows and columns are suppressed. - If axis is 0, only rows are suppressed. - If axis is 1 or -1, only columns are suppressed. Parameters ---------- axis : int, optional Axis along which to perform the operation. Default is None. Returns ------- compressed_array : ndarray The compressed array. Examples -------- >>> x = np.ma.array(np.arange(9).reshape(3, 3), mask=[[1, 0, 0], ... [1, 0, 0], ... [0, 0, 0]]) >>> x masked_array(data = [[-- 1 2] [-- 4 5] [6 7 8]], mask = [[ True False False] [ True False False] [False False False]], fill_value = 999999) >>> np.ma.extras.compress_rowcols(x) array([[7, 8]]) >>> np.ma.extras.compress_rowcols(x, 0) array([[6, 7, 8]]) >>> np.ma.extras.compress_rowcols(x, 1) array([[1, 2], [4, 5], [7, 8]]) is$compress2d works for 2D arrays only.iii˙˙˙˙N(Nii˙˙˙˙(R0RztNotImplementedErrorR4R:tanyRžtalltnxarrayR|RfRPtnonzeroRNR'R}R^(RlRKRLtidxrtidxcR7RR((s3/usr/lib/python2.7/site-packages/numpy/ma/extras.pyR ‡s"/     &  cCs t|dƒS(só Suppress whole rows of a 2-D array that contain masked values. This is equivalent to ``np.ma.extras.compress_rowcols(a, 0)``, see `extras.compress_rowcols` for details. See Also -------- extras.compress_rowcols i(R (RR((s3/usr/lib/python2.7/site-packages/numpy/ma/extras.pyR Ës cCs t|dƒS(sö Suppress whole columns of a 2-D array that contain masked values. This is equivalent to ``np.ma.extras.compress_rowcols(a, 1)``, see `extras.compress_rowcols` for details. See Also -------- extras.compress_rowcols i(R (RR((s3/usr/lib/python2.7/site-packages/numpy/ma/extras.pyRŮs cCsÄt|ƒ}|jdkr'td‚nt|ƒ}|tksL|jƒ rP|S|jƒ}|jjƒ|_|sŽt |t j |dƒ>> import numpy.ma as ma >>> a = np.zeros((3, 3), dtype=np.int) >>> a[1, 1] = 1 >>> a array([[0, 0, 0], [0, 1, 0], [0, 0, 0]]) >>> a = ma.masked_equal(a, 1) >>> a masked_array(data = [[0 0 0] [0 -- 0] [0 0 0]], mask = [[False False False] [False True False] [False False False]], fill_value=999999) >>> ma.mask_rowcols(a) masked_array(data = [[0 -- 0] [-- -- --] [0 -- 0]], mask = [[False True False] [ True True True] [False True False]], fill_value=999999) is$compress2d works for 2D arrays only.iii˙˙˙˙N(Nii˙˙˙˙( R0RzR´R4R:RľR¸RURwR7RNR'R^(RRRKRLt maskedval((s3/usr/lib/python2.7/site-packages/numpy/ma/extras.pyRçsK     &cCs t|dƒS(sœ Mask rows of a 2D array that contain masked values. This function is a shortcut to ``mask_rowcols`` with `axis` equal to 0. See Also -------- mask_rowcols : Mask rows and/or columns of a 2D array. masked_where : Mask where a condition is met. Examples -------- >>> import numpy.ma as ma >>> a = np.zeros((3, 3), dtype=np.int) >>> a[1, 1] = 1 >>> a array([[0, 0, 0], [0, 1, 0], [0, 0, 0]]) >>> a = ma.masked_equal(a, 1) >>> a masked_array(data = [[0 0 0] [0 -- 0] [0 0 0]], mask = [[False False False] [False True False] [False False False]], fill_value=999999) >>> ma.mask_rows(a) masked_array(data = [[0 0 0] [-- -- --] [0 0 0]], mask = [[False False False] [ True True True] [False False False]], fill_value=999999) i(R(RRRK((s3/usr/lib/python2.7/site-packages/numpy/ma/extras.pyRAs+cCs t|dƒS(sŸ Mask columns of a 2D array that contain masked values. This function is a shortcut to ``mask_rowcols`` with `axis` equal to 1. See Also -------- mask_rowcols : Mask rows and/or columns of a 2D array. masked_where : Mask where a condition is met. Examples -------- >>> import numpy.ma as ma >>> a = np.zeros((3, 3), dtype=np.int) >>> a[1, 1] = 1 >>> a array([[0, 0, 0], [0, 1, 0], [0, 0, 0]]) >>> a = ma.masked_equal(a, 1) >>> a masked_array(data = [[0 0 0] [0 -- 0] [0 0 0]], mask = [[False False False] [False True False] [False False False]], fill_value=999999) >>> ma.mask_cols(a) masked_array(data = [[0 -- 0] [0 -- 0] [0 -- 0]], mask = [[False True False] [False True False] [False True False]], fill_value=999999) i(R(RRRK((s3/usr/lib/python2.7/site-packages/numpy/ma/extras.pyRns+cCs |r?|jdkr?|jdkr?t|ƒ}t|ƒ}ntjt|dƒt|dƒƒ}t|ƒ}t|ƒ}tj||ƒ}t|d|ƒS(sş Return the dot product of two arrays. .. note:: Works only with 2-D arrays at the moment. This function is the equivalent of `numpy.dot` that takes masked values into account, see `numpy.dot` for details. Parameters ---------- a, b : ndarray Inputs arrays. strict : bool, optional Whether masked data are propagated (True) or set to 0 (False) for the computation. Default is False. Propagating the mask means that if a masked value appears in a row or column, the whole row or column is considered masked. See Also -------- numpy.dot : Equivalent function for ndarrays. Examples -------- >>> a = ma.array([[1, 2, 3], [4, 5, 6]], mask=[[1, 0, 0], [0, 0, 0]]) >>> b = ma.array([[1, 2], [3, 4], [5, 6]], mask=[[1, 0], [0, 0], [0, 0]]) >>> np.ma.dot(a, b) masked_array(data = [[21 26] [45 64]], mask = [[False False] [False False]], fill_value = 999999) >>> np.ma.dot(a, b, strict=True) masked_array(data = [[-- --] [-- 64]], mask = [[ True True] [ True False]], fill_value = 999999) iiRM(RzRRRNRR3R5R8(RRtbtstrictR¤tamtbmRL((s3/usr/lib/python2.7/site-packages/numpy/ma/extras.pyRœs/$ $  cCstj|ƒj}|d|d }|g}|dk rL|jd|ƒn|dk rh|j|ƒnt|ƒdkr‰t|ƒ}n|S(s! Compute the differences between consecutive elements of an array. This function is the equivalent of `numpy.ediff1d` that takes masked values into account, see `numpy.ediff1d` for details. See Also -------- numpy.ediff1d : Equivalent function for ndarrays. ii˙˙˙˙iN(R_t asanyarraytflatR^tinsertRgRfR(RJtto_endtto_begintedRo((s3/usr/lib/python2.7/site-packages/numpy/ma/extras.pyRÚs    cCsotj|d|d|ƒ}t|tƒr\t|ƒ}|djtƒ|d>> x = array([1, 3, 3, 3], mask=[0, 0, 0, 1]) >>> y = array([3, 1, 1, 1], mask=[0, 0, 0, 1]) >>> intersect1d(x, y) masked_array(data = [1 3 --], mask = [False False True], fill_value = 999999) ii˙˙˙˙(R_R1R'R<(RČtar2t assume_uniquetaux((s3/usr/lib/python2.7/site-packages/numpy/ma/extras.pyRs ! cCs§|s!t|ƒ}t|ƒ}ntj||fƒ}|jdkrI|S|jƒ|jƒ}tjtg|d|d ktgfƒ}|d|d k}||S(sâ Set exclusive-or of 1-D arrays with unique elements. The output is always a masked array. See `numpy.setxor1d` for more details. See Also -------- numpy.setxor1d : Equivalent function for ndarrays. iii˙˙˙˙(R'R_R1RšR<R3RE(RČRĘRËRĚtauxftflagtflag2((s3/usr/lib/python2.7/site-packages/numpy/ma/extras.pyR&/s    ,c CsÄ|s-t|dtƒ\}}t|ƒ}ntj||fƒ}|jddƒ}||}|d|d k}tj|tgfƒ}|jddƒt|ƒ } |r´|| S|| |SdS(s# Test whether each element of an array is also present in a second array. The output is always a masked array. See `numpy.in1d` for more details. See Also -------- numpy.in1d : Equivalent function for ndarrays. Notes ----- .. versionadded:: 1.4.0 RÇtkindt mergesortii˙˙˙˙N(R'RER_R1targsortRFRf( RČRĘRËtrev_idxtartordertsart equal_adjRÎtindx((s3/usr/lib/python2.7/site-packages/numpy/ma/extras.pyRIs cCsttj||fƒƒS(sŔ Union of two arrays. The output is always a masked array. See `numpy.union1d` for more details. See also -------- numpy.union1d : Equivalent function for ndarrays. (R'R_R1(RČRĘ((s3/usr/lib/python2.7/site-packages/numpy/ma/extras.pyR)ms cCsd|s!t|ƒ}t|ƒ}nt||dtƒ}|jdkrI|Stj|ƒ|dkSdS(sĘ Set difference of 1D arrays with unique elements. The output is always a masked array. See `numpy.setdiff1d` for more details. See Also -------- numpy.setdiff1d : Equivalent function for ndarrays. Examples -------- >>> x = np.ma.array([1, 2, 3, 4], mask=[0, 1, 0, 1]) >>> np.ma.extras.setdiff1d(x, [1, 2]) masked_array(data = [3 --], mask = [False True], fill_value = 999999) RËiN(R'RRERšR_R0(RČRĘRËRĚ((s3/usr/lib/python2.7/site-packages/numpy/ma/extras.pyR${s cCsotj|d|d|ƒ}t|tƒr\t|ƒ}|djtƒ|dA ;tMAxisConcatenatorcBs#eZdZdd„Zd„ZRS(s› Translate slice objects to concatenation along an axis. For documentation on usage, see `mr_class`. See Also -------- mr_class icCstj||dtƒdS(Ntmatrix(R?R\RF(RZRK((s3/usr/lib/python2.7/site-packages/numpy/ma/extras.pyR\Šsc Csžt|tƒrtd‚nt|ƒtk r9|f}ng}g}d}xtt|ƒƒD]ý}t}t||ƒt kr.||j }||j }||j } |dkrźd}n|dkrŃd}nt|ƒtdƒkrt t|ƒƒ} tj|| d| ƒ} qtj|| |ƒ} nÔt||ƒtkrś||dkrvt|_||dk|_q^nyt ||ƒ|_w^Wqttfk r˛td‚qXnLt||ƒtjkrřt||gƒ} |j|ƒt}n ||} |j| ƒt| tƒr^| r^|dkr=| j}q[| j|kr[| j}q[q^q^W|dk r–x(|D]}||j|ƒ||RQRĺR1t_retval( RZtkeytobjstscalarstfinal_dtypedescrRutscalarRRRRštnewobjRp((s3/usr/lib/python2.7/site-packages/numpy/ma/extras.pyt __getitem__s^                  (RWRrRYR\R(((s3/usr/lib/python2.7/site-packages/numpy/ma/extras.pyR~s  tmr_classcBseZdZd„ZRS(sG Translate slice objects to concatenation along the first axis. This is the masked array version of `lib.index_tricks.RClass`. See Also -------- lib.index_tricks.RClass Examples -------- >>> np.ma.mr_[np.ma.array([1,2,3]), 0, 0, np.ma.array([4,5,6])] array([1, 2, 3, 0, 0, 4, 5, 6]) cCstj|dƒdS(Ni(RR\(RZ((s3/usr/lib/python2.7/site-packages/numpy/ma/extras.pyR\Đs(RWRrRYR\(((s3/usr/lib/python2.7/site-packages/numpy/ma/extras.pyRŔscCsjt|ƒ}|tks(tj|ƒ r2ddgStj|ƒ}t|ƒdkrb|ddgSdSdS(sn Find the indices of the first and last unmasked values. Expects a 1-D `MaskedArray`, returns None if all values are masked. Parameters ---------- arr : array_like Input 1-D `MaskedArray` Returns ------- edges : ndarray or None The indices of first and last non-masked value in the array. Returns None if all values are masked. See Also -------- flatnotmasked_contiguous, notmasked_contiguous, notmasked_edges Notes ----- Only accepts 1-D arrays. Examples -------- >>> a = np.arange(10) >>> mask = (a < 3) | (a > 8) | (a == 5) >>> ma = np.ma.array(a, mask=m) >>> np.array(ma[~ma.mask]) array([3, 4, 6, 7, 8]) >>> flatnotmasked_edges(ma) array([3, 8]) >>> ma = np.ma.array(a, mask=np.ones_like(a)) >>> print flatnotmasked_edges(ma) None ii˙˙˙˙N(R4R:RNRľt flatnonzeroRfR^(RRRLtunmasked((s3/usr/lib/python2.7/site-packages/numpy/ma/extras.pyRŮs*  cCsât|ƒ}|dks'|jdkr1t|ƒSt|ƒ}ttj|jƒdtj|g|jƒƒ}t gt |jƒD]}||j |ƒj ƒ^qƒt gt |jƒD]}||j |ƒj ƒ^qšƒgS(sE Find the indices of the first and last unmasked values along an axis. If all values are masked, return None. Otherwise, return a list of two tuples, corresponding to the indices of the first and last unmasked values respectively. Parameters ---------- a : array_like The input array. axis : int, optional Axis along which to perform the operation. If None (default), applies to a flattened version of the array. Returns ------- edges : ndarray or list An array of start and end indexes if there are any masked data in the array. If there are no masked data in the array, `edges` is a list of the first and last index. See Also -------- flatnotmasked_contiguous, flatnotmasked_edges, notmasked_contiguous Examples -------- >>> a = np.arange(9).reshape((3, 3)) >>> m = np.zeros_like(a) >>> m[1:, 1:] = 1 >>> ma = np.ma.array(a, mask=m) >>> np.array(ma[~ma.mask]) array([0, 1, 2, 3, 6]) >>> np.ma.extras.notmasked_edges(ma) array([0, 6]) iRMN(R0R^RzRR5R/RNtindicesRPRCR|tmint compressedR†(RRRKRLR­R((s3/usr/lib/python2.7/site-packages/numpy/ma/extras.pyR! s)   18cCsŘt|ƒ}|tkr+|jddgfStj|ƒ}t|ƒdkrQdSg}xptjt |ƒd„ƒD]S\}}tj g|D]}|d^qŒt ƒ}|j t |d|dƒƒqsW|jƒ|S(s& Find contiguous unmasked data in a masked array along the given axis. Parameters ---------- a : narray The input array. Returns ------- slice_list : list A sorted sequence of slices (start index, end index). See Also -------- flatnotmasked_edges, notmasked_contiguous, notmasked_edges Notes ----- Only accepts 2-D arrays at most. Examples -------- >>> a = np.arange(10) >>> mask = (a < 3) | (a > 8) | (a == 5) >>> ma = np.ma.array(a, mask=mask) >>> np.array(ma[~ma.mask]) array([3, 4, 6, 7, 8]) >>> np.ma.extras.flatnotmasked_contiguous(ma) [slice(3, 4, None), slice(6, 8, None)] >>> ma = np.ma.array(a, mask=np.ones_like(a)) >>> print np.ma.extras.flatnotmasked_edges(ma) None ii˙˙˙˙cSs|\}}||S(N((t.0RRl((s3/usr/lib/python2.7/site-packages/numpy/ma/extras.pytksiN(R4R:RšRNRRfR^t itertoolstgroupbyt enumerateR/RâRgR~R<(RRRLRR˜Rutgrouptgttmp((s3/usr/lib/python2.7/site-packages/numpy/ma/extras.pyR?s%  ()" cCsĹt|ƒ}|j}|dkr-td‚n|dksE|dkrOt|ƒSg}|dd}ddg}tddƒ||>> a = np.arange(9).reshape((3, 3)) >>> mask = np.zeros_like(a) >>> mask[1:, 1:] = 1 >>> ma = np.ma.array(a, mask=mask) >>> np.array(ma[~ma.mask]) array([0, 1, 2, 3, 6]) >>> np.ma.extras.notmasked_contiguous(ma) [slice(0, 3, None), slice(6, 6, None)] is%Currently limited to atmost 2D array.iiN( R0RzR´R^RR~R|RPRg(RRRKR‹R˜totherR­R((s3/usr/lib/python2.7/site-packages/numpy/ma/extras.pyR rs(       cCs|jdkr|jƒ}n|d|d jƒ}|dd}gttjdg|ƒtj|t|ƒgƒƒD]\}}t||ƒ^qx}|S(sv Finds the clumps (groups of data with the same values) for a 1D bool array. Returns a series of slices. ii˙˙˙˙i(RzR›R¸tzipRtchainRfR~(RMR­tlefttrighttslices((s3/usr/lib/python2.7/site-packages/numpy/ma/extras.pyt_ezclump­s=cCszt|dtƒ}|tkr1td|jƒgSt|ƒ}|dtkrc|ddd…}n|ddd…}|S(s9 Return list of slices corresponding to the unmasked clumps of a 1-D array. Parameters ---------- a : ndarray A one-dimensional masked array. Returns ------- slices : list of slice The list of slices, one for each continuous region of unmasked elements in `a`. Notes ----- .. versionadded:: 1.4.0 Examples -------- >>> a = np.ma.masked_array(np.arange(10)) >>> a[[0, 1, 2, 6, 8, 9]] = np.ma.masked >>> np.ma.extras.clump_unmasked(a) [slice(3, 6, None), slice(7, 8, None)] RUiiNi(R]R:R~RšR*R7(RRRMR)R˜((s3/usr/lib/python2.7/site-packages/numpy/ma/extras.pyRžs  cCswtj|ƒ}|tkrgSt|ƒ}t|ƒrs|dtkr]|ddd…}qs|ddd…}n|S(sL Returns a list of slices corresponding to the masked clumps of a 1-D array. Parameters ---------- a : ndarray A one-dimensional masked array. Returns ------- slices : list of slice The list of slices, one for each continuous region of masked elements in `a`. Notes ----- .. versionadded:: 1.4.0 Examples -------- >>> a = np.ma.masked_array(np.arange(10)) >>> a[[0, 1, 2, 6, 8, 9]] = np.ma.masked >>> np.ma.extras.clump_masked(a) [slice(0, 3, None), slice(6, 7, None), slice(8, None, None)] iNii(R_R4R:R*RfR7(RRRMR)((s3/usr/lib/python2.7/site-packages/numpy/ma/extras.pyRäs   cCs;tj||ƒ}t|ƒ}|tk r7d||      "    "  '  ((`RYt __author__t __version__t __revision__t__date__t__all__RR.tcoreR_R,R-R.R/R0R1R2R3R4R5R6R7R8R9R:R;R<R=tnumpyRNR>Rˇtnumpy.core.umathRœtnumpy.lib.index_tricksR?t numpy.linalgR@tnumpy.lib.utilsRARHR^R R™RRRVRRRR+R#RRRRRRvRRFRRR R RRRRRRR'RR&RR)R$R(RR%RERđR R RRRRR!RR R*RRR*tdoc_noteR"(((s3/usr/lib/python2.7/site-packages/numpy/ma/extras.pyt sŹ             v   6 3 :L         UZ D   Z - . >  $   #+8JB  4 2 3 ;  & , ,