/ lib / numpy / ma / extras.pyc
extras.pyc
   1  o

   2  [��c���@stdZgd�ZddlZddlZddlmZddlmZmZm	Z	m
   3  Z
   4  mZmZm
Z
mZmZmZmZmZmZmZmZmZmZmZmZmZmZmZddlZddlmZm
   5  Z ddl!m"Z"dd	l#m$Z$dd
   6  l%m&Z&ddl'm(Z(dd
�Z)dsdd�Z*e+fdd�Z,dd�Z-Gdd�d�Z.Gdd�de.�Z/Gdd�de.�Z0Gdd�de.�Z1Gdd�de.�Z2e2d�Z3e2d�Z4e2d �Z5e0d!�Z6Z7e0d"�Z8e0d#�Z9e0d$�Z:e0d%�Z;e/d&�Z<e/d'�Z=d(d)�Z>d*d+�Z?ej?je?_d,d-�Z@e@jdur�ej@jdej@j�Ad.���B�d/e@_dtejCd1�d2d3�ZDdud4d5�ZEdtd6d7�ZFdsd8d9�ZGdsd:d;�ZHd<d=�ZId>d?�ZJejCfd@dA�ZKejCfdBdC�ZLdvdDdE�ZMdwdFdG�ZNdxdHdI�ZOdxdJdK�ZPdwdLdM�ZQdwdNdO�ZRdPdQ�ZSdxdRdS�ZTdydUdV�ZUdzdWdX�ZVddTejCdTejCfdYdZ�ZWGd[d\�d\e(�ZXGd]d^�d^eX�ZYeY�ZZd{d_d`�Z[dadb�Z\dsdcdd�Z]dedf�Z^dsdgdh�Z_didj�Z`dkdl�Zadmdn�Zbdsdodp�Zce�dejcjecj�ec_d|dqdr�Zee�dejejeej�ee_dS)}z�
   7  Masked arrays add-ons.
   8  
   9  A collection of utilities for `numpy.ma`.
  10  
  11  :author: Pierre Gerard-Marchant
  12  :contact: pierregm_at_uga_dot_edu
  13  :version: $Id: extras.py 3473 2007-10-29 15:18:13Z jarrod.millman $
  14  
  15  ).�apply_along_axis�apply_over_axes�
  16  atleast_1d�
  17  atleast_2d�
  18  atleast_3d�average�clump_masked�clump_unmasked�column_stack�
compress_cols�compress_nd�compress_rowcols�
compress_rows�count_masked�corrcoef�cov�diagflat�dot�dstack�ediff1d�flatnotmasked_contiguous�flatnotmasked_edges�hsplit�hstack�isin�in1d�intersect1d�	mask_cols�mask_rowcols�	mask_rows�
  19  masked_all�masked_all_like�median�mr_�ndenumerate�notmasked_contiguous�notmasked_edges�polyfit�	row_stack�	setdiff1d�setxor1d�stack�unique�union1d�vander�vstack�N�)�core)�MaskedArray�MAError�add�array�asarray�concatenate�filled�count�getmask�getmaskarray�make_mask_descr�masked�masked_array�mask_or�nomask�ones�sort�zeros�getdata�get_masked_subclassrr)�ndarrayr5)�normalize_axis_index)�normalize_axis_tuple)�_ureduce)�AxisConcatenatorcCst|tttf�S)z6
  20      Is seq a sequence (ndarray, list or tuple)?
  21  
  22      )�
  23  isinstancerF�tuple�list)�seq�rO��C:\Users\Jacks.GUTTSPC\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\numpy\ma\extras.py�
  24  issequence*srQcCst|�}|�|�S)a�
  25      Count the number of masked elements along the given axis.
  26  
  27      Parameters
  28      ----------
  29      arr : array_like
  30          An array with (possibly) masked elements.
  31      axis : int, optional
  32          Axis along which to count. If None (default), a flattened
  33          version of the array is used.
  34  
  35      Returns
  36      -------
  37      count : int, ndarray
  38          The total number of masked elements (axis=None) or the number
  39          of masked elements along each slice of the given axis.
  40  
  41      See Also
  42      --------
  43      MaskedArray.count : Count non-masked elements.
  44  
  45      Examples
  46      --------
  47      >>> import numpy.ma as ma
  48      >>> a = np.arange(9).reshape((3,3))
  49      >>> a = ma.array(a)
  50      >>> a[1, 0] = ma.masked
  51      >>> a[1, 2] = ma.masked
  52      >>> a[2, 1] = ma.masked
  53      >>> a
  54      masked_array(
  55        data=[[0, 1, 2],
  56              [--, 4, --],
  57              [6, --, 8]],
  58        mask=[[False, False, False],
  59              [ True, False,  True],
  60              [False,  True, False]],
  61        fill_value=999999)
  62      >>> ma.count_masked(a)
  63      3
  64  
  65      When the `axis` keyword is used an array is returned.
  66  
  67      >>> ma.count_masked(a, axis=0)
  68      array([1, 1, 1])
  69      >>> ma.count_masked(a, axis=1)
  70      array([0, 2, 1])
  71  
  72      )r;�sum)�arr�axis�mrOrOrPr2s2
  73  rcCs$tt�||�t�|t|��d�}|S)aC
  74      Empty masked array with all elements masked.
  75  
  76      Return an empty masked array of the given shape and dtype, where all the
  77      data are masked.
  78  
  79      Parameters
  80      ----------
  81      shape : int or tuple of ints
  82          Shape of the required MaskedArray, e.g., ``(2, 3)`` or ``2``.
  83      dtype : dtype, optional
  84          Data type of the output.
  85  
  86      Returns
  87      -------
  88      a : MaskedArray
  89          A masked array with all data masked.
  90  
  91      See Also
  92      --------
  93      masked_all_like : Empty masked array modelled on an existing array.
  94  
  95      Examples
  96      --------
  97      >>> import numpy.ma as ma
  98      >>> ma.masked_all((3, 3))
  99      masked_array(
 100        data=[[--, --, --],
 101              [--, --, --],
 102              [--, --, --]],
 103        mask=[[ True,  True,  True],
 104              [ True,  True,  True],
 105              [ True,  True,  True]],
 106        fill_value=1e+20,
 107        dtype=float64)
 108  
 109      The `dtype` parameter defines the underlying data type.
 110  
 111      >>> a = ma.masked_all((3, 3))
 112      >>> a.dtype
 113      dtype('float64')
 114      >>> a = ma.masked_all((3, 3), dtype=np.int32)
 115      >>> a.dtype
 116      dtype('int32')
 117  
 118      ��mask)r>�np�emptyrAr<)�shape�dtype�arOrOrPrhs/�rcCs,t�|��t�}tj|jt|j�d�|_|S)a�
 119      Empty masked array with the properties of an existing array.
 120  
 121      Return an empty masked array of the same shape and dtype as
 122      the array `arr`, where all the data are masked.
 123  
 124      Parameters
 125      ----------
 126      arr : ndarray
 127          An array describing the shape and dtype of the required MaskedArray.
 128  
 129      Returns
 130      -------
 131      a : MaskedArray
 132          A masked array with all data masked.
 133  
 134      Raises
 135      ------
 136      AttributeError
 137          If `arr` doesn't have a shape attribute (i.e. not an ndarray)
 138  
 139      See Also
 140      --------
 141      masked_all : Empty masked array with all elements masked.
 142  
 143      Examples
 144      --------
 145      >>> import numpy.ma as ma
 146      >>> arr = np.zeros((2, 3), dtype=np.float32)
 147      >>> arr
 148      array([[0., 0., 0.],
 149             [0., 0., 0.]], dtype=float32)
 150      >>> ma.masked_all_like(arr)
 151      masked_array(
 152        data=[[--, --, --],
 153              [--, --, --]],
 154        mask=[[ True,  True,  True],
 155              [ True,  True,  True]],
 156        fill_value=1e+20,
 157        dtype=float32)
 158  
 159      The dtype of the masked array matches the dtype of `arr`.
 160  
 161      >>> arr.dtype
 162      dtype('float32')
 163      >>> ma.masked_all_like(arr).dtype
 164      dtype('float32')
 165  
 166      �r[)	rX�
 167  empty_like�viewr2rArZr<r[�_mask)rSr\rOrOrPr �s2r c@s(eZdZdZdd�Zdd�Zdd�ZdS)	�_fromnxfunctionaV
 168      Defines a wrapper to adapt NumPy functions to masked arrays.
 169  
 170  
 171      An instance of `_fromnxfunction` can be called with the same parameters
 172      as the wrapped NumPy function. The docstring of `newfunc` is adapted from
 173      the wrapped function as well, see `getdoc`.
 174  
 175      This class should not be used directly. Instead, one of its extensions that
 176      provides support for a specific type of input should be used.
 177  
 178      Parameters
 179      ----------
 180      funcname : str
 181          The name of the function to be adapted. The function should be
 182          in the NumPy namespace (i.e. ``np.funcname``).
 183  
 184      cCs||_|��|_dS�N)�__name__�getdoc�__doc__)�self�funcnamerOrOrP�__init__�sz_fromnxfunction.__init__cCsLtt|jd�}t|dd�}|r$|jt�|�}t�|d�}d�||f�SdS)aK
 185          Retrieve the docstring and signature from the function.
 186  
 187          The ``__doc__`` attribute of the function is used as the docstring for
 188          the new masked array version of the function. A note on application
 189          of the function to the mask is appended.
 190  
 191          Parameters
 192          ----------
 193          None
 194  
 195          Nrez@The function is applied to both the _data and the _mask, if any.z
 196  
 197  )�getattrrXrc�ma�get_object_signature�doc_note�join)rf�npfunc�doc�sigrOrOrPrd�s
z_fromnxfunction.getdoccOsdSrbrO)rf�args�paramsrOrOrP�__call__sz_fromnxfunction.__call__N)rc�
 198  __module__�__qualname__rerhrdrsrOrOrOrPra�s
 199  rac@�eZdZdZdd�ZdS)�_fromnxfunction_singlez�
 200      A version of `_fromnxfunction` that is called with a single array
 201      argument followed by auxiliary args that are passed verbatim for
 202      both the data and mask calls.
 203      cOs�tt|j�}t|t�r+||��g|�Ri|��}|t|�g|�Ri|��}t||d�S|t�|�g|�Ri|��}|t|�g|�Ri|��}t||d�S)NrV)	rirXrcrKrF�	__array__r;r>r6�rf�xrqrr�func�_d�_mrOrOrPrss
 204  z_fromnxfunction_single.__call__N�rcrtrurersrOrOrOrPrw�rwc@rv)�_fromnxfunction_seqz�
 205      A version of `_fromnxfunction` that is called with a single sequence
 206      of arrays followed by auxiliary args that are passed verbatim for
 207      both the data and mask calls.
 208      cOs`tt|j�}|tdd�|D��g|�Ri|��}|tdd�|D��g|�Ri|��}t||d�S)NcSsg|]}t�|��qSrO)rXr6��.0r\rOrOrP�
 209  <listcomp>"sz0_fromnxfunction_seq.__call__.<locals>.<listcomp>cSsg|]}t|��qSrO)r;r�rOrOrPr�#srV)rirXrcrLr>ryrOrOrPrs s$$z_fromnxfunction_seq.__call__Nr~rOrOrOrPr�rr�c@rv)�_fromnxfunction_argsa�
 210      A version of `_fromnxfunction` that is called with multiple array
 211      arguments. The first non-array-like input marks the beginning of the
 212      arguments that are passed verbatim for both the data and mask calls.
 213      Array arguments are processed independently and the results are
 214      returned in a list. If only one array is found, the return value is
 215      just the processed array instead of a list.
 216      c	Os�tt|j�}g}t|�}t|�dkr,t|d�r,|�|�d��t|�dkr,t|d�sg}|D]&}|t�|�g|�Ri|��}|t	|�g|�Ri|��}|�t
 217  ||d��q0t|�dkra|dS|S)Nr/rVr0)rirXrcrM�lenrQ�append�popr6r;r>)	rfrqrrr{�arrays�resrzr|r}rOrOrPrs0s�z_fromnxfunction_args.__call__Nr~rOrOrOrPr�'sr�c@rv)�_fromnxfunction_allargsa
 218      A version of `_fromnxfunction` that is called with multiple array
 219      arguments. Similar to `_fromnxfunction_args` except that all args
 220      are converted to arrays even if they are not so already. This makes
 221      it possible to process scalars as 1-D arrays. Only keyword arguments
 222      are passed through verbatim for the data and mask calls. Arrays
 223      arguments are processed independently and the results are returned
 224      in a list. If only one arg is present, the return value is just the
 225      processed array instead of a list.
 226      cOsntt|j�}g}|D] }|t�|�fi|��}|t|�fi|��}|�t||d��q
 227  t|�dkr5|dS|S)NrVr0r/)rirXrcr6r;r�r>r�)rfrqrrr{r�rzr|r}rOrOrPrsKsz _fromnxfunction_allargs.__call__Nr~rOrOrOrPr�@s
 228  r�rrrr.rr	rr*rrcCsXd}|t|�kr*t||d�r |||||d�<t||d�s|d7}|t|�ks|S)zFlatten a sequence in place.r/�__iter__r0)r��hasattr)rN�krOrOrP�flatten_inplaceis��r�cOs�t|ddd�}|j}t||�}dg|d}t�|d�}tt|��}|�|�tdd�||<t�	|j
 229  ��|�}	|�||�||t
|���g|�Ri|��}
 230  t�|
 231  �}|sgzt|
 232  �Wntyfd}Ynwg}|r�|�t�	|
 233  �j�t|	t�}
|
 234  |
t
|�<t�|	�}d}||kr�|dd7<d}|||	|kr�|d|kr�||dd7<d||<|d8}|||	|kr�|d|ks�|�||�||t
|���g|�Ri|��}
 235  |
 236  |
t
|�<|�t	|
 237  �j�|d7}||ks�n�t|
 238  ddd�}
 239  |��}tdd�g|
 240  j||<|�||�t�|	�}|	}t|j
 241  �}	|
 242  j
 243  |	|<|�t	|
 244  �j�t|	�}	t|	t�}
|
 245  |
t
t|����<d}||k�r�|dd7<d}||||k�r{|d|k�r{||dd7<d||<|d8}||||k�r{|d|k�sY|�||�|�||�||t
|���g|�Ri|��}
 246  |
 247  |
t
t|����<|�t	|
 248  �j�|d7}||k�s?t�t�	|����}t|d	��s�tj	|
|d
 249  �}|St	|
|d
 250  �}t�|�|_|S)z0
 251      (This docstring should be overwritten)
 252      FT)�copy�subokr/r0�ON�����r`r])r5�ndimrGrXrCrM�range�remove�slicer6rZ�take�putrL�tolist�isscalarr��	TypeErrorr�r[�object�productr�r��maxr�rj�default_fill_value�
 253  fill_value)�func1drTrSrq�kwargs�nd�ind�iZindlist�outshaper��asscalar�dtypesZoutarrZNtotr��n�jZ	holdshapeZ
 254  max_dtypes�resultrOrOrPrss�
 255  
 256  "
 257  �
 258  
 259  �"��
 260  
 261  
 262  
 263  
 264    �"
 265  ��rcCs�t|�}|j}t|�jdkr|f}|D]-}|dkr||}||f}||�}|j|jkr.|}qt�||�}|j|jkr=|}qtd��|S)z.
 266      (This docstring will be overwritten)
 267      r/z7function is not returning an array of the correct shape)r6r�r5rj�expand_dims�
 268  ValueError)r{r\�axes�val�NrTrqr�rOrOrPr�s r�Notesao
 269  
 270      Examples
 271      --------
 272      >>> a = np.ma.arange(24).reshape(2,3,4)
 273      >>> a[:,0,1] = np.ma.masked
 274      >>> a[:,1,:] = np.ma.masked
 275      >>> a
 276      masked_array(
 277        data=[[[0, --, 2, 3],
 278               [--, --, --, --],
 279               [8, 9, 10, 11]],
 280              [[12, --, 14, 15],
 281               [--, --, --, --],
 282               [20, 21, 22, 23]]],
 283        mask=[[[False,  True, False, False],
 284               [ True,  True,  True,  True],
 285               [False, False, False, False]],
 286              [[False,  True, False, False],
 287               [ True,  True,  True,  True],
 288               [False, False, False, False]]],
 289        fill_value=999999)
 290      >>> np.ma.apply_over_axes(np.ma.sum, a, [0,2])
 291      masked_array(
 292        data=[[[46],
 293               [--],
 294               [124]]],
 295        mask=[[[False],
 296               [ True],
 297               [False]]],
 298        fill_value=999999)
 299  
 300      Tuple axis arguments to ufuncs are equivalent:
 301  
 302      >>> np.ma.sum(a, axis=(0,2)).reshape((1,-1,1))
 303      masked_array(
 304        data=[[[46],
 305               [--],
 306               [124]]],
 307        mask=[[[False],
 308               [ True],
 309               [False]]],
 310        fill_value=999999)
 311      F)�keepdimscCs�t|�}t|�}|tjuri}nd|i}|dur+|j|fi|��}|j�|�|��}n�t|�}	t|jjtj	tj
 312  f�rDt�|j|	jd�}
 313  nt�|j|	j�}
 314  |j|	jkr�|durZt
d��|	jdkrct
d��|	jd|j|krqtd��tj|	|jdd	|	jd
 315  d�}	|	�d|�}	|tur�|	|j}	|	j|jO_|	jd||
 316  d
�|��}tj||	|
 317  d�j|fi|��|}|r�|j|jkr�t�||j���}||fS|S)a�
 318  
 319      Return the weighted average of array over the given axis.
 320  
 321      Parameters
 322      ----------
 323      a : array_like
 324          Data to be averaged.
 325          Masked entries are not taken into account in the computation.
 326      axis : int, optional
 327          Axis along which to average `a`. If None, averaging is done over
 328          the flattened array.
 329      weights : array_like, optional
 330          The importance that each element has in the computation of the average.
 331          The weights array can either be 1-D (in which case its length must be
 332          the size of `a` along the given axis) or of the same shape as `a`.
 333          If ``weights=None``, then all data in `a` are assumed to have a
 334          weight equal to one.  The 1-D calculation is::
 335  
 336              avg = sum(a * weights) / sum(weights)
 337  
 338          The only constraint on `weights` is that `sum(weights)` must not be 0.
 339      returned : bool, optional
 340          Flag indicating whether a tuple ``(result, sum of weights)``
 341          should be returned as output (True), or just the result (False).
 342          Default is False.
 343      keepdims : bool, optional
 344          If this is set to True, the axes which are reduced are left
 345          in the result as dimensions with size one. With this option,
 346          the result will broadcast correctly against the original `a`.
 347          *Note:* `keepdims` will not work with instances of `numpy.matrix`
 348          or other classes whose methods do not support `keepdims`.
 349  
 350          .. versionadded:: 1.23.0
 351  
 352      Returns
 353      -------
 354      average, [sum_of_weights] : (tuple of) scalar or MaskedArray
 355          The average along the specified axis. When returned is `True`,
 356          return a tuple with the average as the first element and the sum
 357          of the weights as the second element. The return type is `np.float64`
 358          if `a` is of integer type and floats smaller than `float64`, or the
 359          input data-type, otherwise. If returned, `sum_of_weights` is always
 360          `float64`.
 361  
 362      Examples
 363      --------
 364      >>> a = np.ma.array([1., 2., 3., 4.], mask=[False, False, True, True])
 365      >>> np.ma.average(a, weights=[3, 1, 0, 0])
 366      1.25
 367  
 368      >>> x = np.ma.arange(6.).reshape(3, 2)
 369      >>> x
 370      masked_array(
 371        data=[[0., 1.],
 372              [2., 3.],
 373              [4., 5.]],
 374        mask=False,
 375        fill_value=1e+20)
 376      >>> avg, sumweights = np.ma.average(x, axis=0, weights=[1, 2, 3],
 377      ...                                 returned=True)
 378      >>> avg
 379      masked_array(data=[2.6666666666666665, 3.6666666666666665],
 380                   mask=[False, False],
 381             fill_value=1e+20)
 382  
 383      With ``keepdims=True``, the following result has shape (3, 1).
 384  
 385      >>> np.ma.average(x, axis=1, keepdims=True)
 386      masked_array(
 387        data=[[0.5],
 388              [2.5],
 389              [4.5]],
 390        mask=False,
 391        fill_value=1e+20)
 392      r�N�f8z;Axis must be specified when shapes of a and weights differ.r0z81D weights expected when shapes of a and weights differ.r/z5Length of weights not compatible with specified axis.)r0T�r�r�)rTr[r]rO)r6r:rX�_NoValue�meanr[�typer9�
 393  issubclass�integer�bool_�result_typerZr�r�r��broadcast_to�swapaxesr@rWrR�multiplyr�)r\rT�weights�returnedr�rU�keepdims_kw�avg�scl�wgt�result_dtyperOrOrPrsZM
 394  �
 395  �� ����rcCs`t|d�s&tjt|dd�||||d�}t|tj�r$d|jkr$t|dd�S|St|t	||||d�S)	a>	
 396      Compute the median along the specified axis.
 397  
 398      Returns the median of the array elements.
 399  
 400      Parameters
 401      ----------
 402      a : array_like
 403          Input array or object that can be converted to an array.
 404      axis : int, optional
 405          Axis along which the medians are computed. The default (None) is
 406          to compute the median along a flattened version of the array.
 407      out : ndarray, optional
 408          Alternative output array in which to place the result. It must
 409          have the same shape and buffer length as the expected output
 410          but the type will be cast if necessary.
 411      overwrite_input : bool, optional
 412          If True, then allow use of memory of input array (a) for
 413          calculations. The input array will be modified by the call to
 414          median. This will save memory when you do not need to preserve
 415          the contents of the input array. Treat the input as undefined,
 416          but it will probably be fully or partially sorted. Default is
 417          False. Note that, if `overwrite_input` is True, and the input
 418          is not already an `ndarray`, an error will be raised.
 419      keepdims : bool, optional
 420          If this is set to True, the axes which are reduced are left
 421          in the result as dimensions with size one. With this option,
 422          the result will broadcast correctly against the input array.
 423  
 424          .. versionadded:: 1.10.0
 425  
 426      Returns
 427      -------
 428      median : ndarray
 429          A new array holding the result is returned unless out is
 430          specified, in which case a reference to out is returned.
 431          Return data-type is `float64` for integers and floats smaller than
 432          `float64`, or the input data-type, otherwise.
 433  
 434      See Also
 435      --------
 436      mean
 437  
 438      Notes
 439      -----
 440      Given a vector ``V`` with ``N`` non masked values, the median of ``V``
 441      is the middle value of a sorted copy of ``V`` (``Vs``) - i.e.
 442      ``Vs[(N-1)/2]``, when ``N`` is odd, or ``{Vs[N/2 - 1] + Vs[N/2]}/2``
 443      when ``N`` is even.
 444  
 445      Examples
 446      --------
 447      >>> x = np.ma.array(np.arange(8), mask=[0]*4 + [1]*4)
 448      >>> np.ma.median(x)
 449      1.5
 450  
 451      >>> x = np.ma.array(np.arange(10).reshape(2, 5), mask=[0]*6 + [1]*4)
 452      >>> np.ma.median(x)
 453      2.5
 454      >>> np.ma.median(x, axis=-1, overwrite_input=True)
 455      masked_array(data=[2.0, 5.0],
 456                   mask=[False, False],
 457             fill_value=1e+20)
 458  
 459      rWTr�)rT�out�overwrite_inputr�r0F�r�)r{r�rTr�r�)
 460  r�rXr!rDrKrFr�r>rI�_median)r\rTr�r�r�rUrOrOrPr!�s
 461  B��r!csNt�|jtj�rtj}nd}|r)�dur|����j|d�n|j�|d�|�nt|�|d���dur7d�nt��j���j	�dkrbt
 462  d�g�j}t
 463  dd�|�<t|�}tjj
�|�|d�S�jdkr�tt��d�\}}�||d|d�}t��jtj�r��jdkr�|j|d�}	|s�tj|	dd	|d
 464  �}	tjj��|	��}	n|j
|d�}	tj�|	�r�t��j�s�tj���S|	St��dd�}
 465  |
 466  d}|
 467  ddk}t�|||d�}tj||g�d
�}
tj�|
�d
�}��fdd�}||�t��jtj��rtjj|�|d�}	tj|	jdd|	jd
 468  �tjj��|	��}	|	Stjj
|�|d�}	|	S)N)r�)rTr�r/)rTr�r0�)r�g@�safe)�castingr�T�rTr��rTcsJtj�|�r#tj�j�dd�|j@}tj���|j|<d|j|<dSdS)NTr�F)rXrj�	is_masked�allrW�minimum_fill_value�data)�s�rep�ZasortedrTrOrP�replace_masked s
 469  �z_median.<locals>.replace_masked�unsafe)rX�
 470  issubdtyper[�inexact�inf�ravelrBrGr�rZr�rLrjr��divmodr9�sizerR�true_divide�lib�utils�_median_nancheckr�r�rWr��wherer7�take_along_axisr�)r\rTr�r�r��indexer�idx�odd�midr��counts�h�l�lhZlow_highr�rOr�rPr��sZ
 471  
 472  �r�cCs�t|�}t|�}|durtt|j��}nt||j�}|tus"|��s%|jS|�	�r-t
 473  g�S|j}|D]%}ttt|��tt|d|j���}|td�f||j|d�f}q2|S)a�Suppress slices from multiple dimensions which contain masked values.
 474  
 475      Parameters
 476      ----------
 477      x : array_like, MaskedArray
 478          The array to operate on. If not a MaskedArray instance (or if no array
 479          elements are masked), `x` is interpreted as a MaskedArray with `mask`
 480          set to `nomask`.
 481      axis : tuple of ints or int, optional
 482          Which dimensions to suppress slices from can be configured with this
 483          parameter.
 484          - If axis is a tuple of ints, those are the axes to suppress slices from.
 485          - If axis is an int, then that is the only axis to suppress slices from.
 486          - If axis is None, all axis are selected.
 487  
 488      Returns
 489      -------
 490      compress_array : ndarray
 491          The compressed array.
 492      Nr0r�)
r6r:rLr�r�rHr@�any�_datar��nxarrayrMr�)rzrTrUr��axr�rOrOrPr8s$$rcCs"t|�jdkrtd��t||d�S)a�
 493      Suppress the rows and/or columns of a 2-D array that contain
 494      masked values.
 495  
 496      The suppression behavior is selected with the `axis` parameter.
 497  
 498      - If axis is None, both rows and columns are suppressed.
 499      - If axis is 0, only rows are suppressed.
 500      - If axis is 1 or -1, only columns are suppressed.
 501  
 502      Parameters
 503      ----------
 504      x : array_like, MaskedArray
 505          The array to operate on.  If not a MaskedArray instance (or if no array
 506          elements are masked), `x` is interpreted as a MaskedArray with
 507          `mask` set to `nomask`. Must be a 2D array.
 508      axis : int, optional
 509          Axis along which to perform the operation. Default is None.
 510  
 511      Returns
 512      -------
 513      compressed_array : ndarray
 514          The compressed array.
 515  
 516      Examples
 517      --------
 518      >>> x = np.ma.array(np.arange(9).reshape(3, 3), mask=[[1, 0, 0],
 519      ...                                                   [1, 0, 0],
 520      ...                                                   [0, 0, 0]])
 521      >>> x
 522      masked_array(
 523        data=[[--, 1, 2],
 524              [--, 4, 5],
 525              [6, 7, 8]],
 526        mask=[[ True, False, False],
 527              [ True, False, False],
 528              [False, False, False]],
 529        fill_value=999999)
 530  
 531      >>> np.ma.compress_rowcols(x)
 532      array([[7, 8]])
 533      >>> np.ma.compress_rowcols(x, 0)
 534      array([[6, 7, 8]])
 535      >>> np.ma.compress_rowcols(x, 1)
 536      array([[1, 2],
 537             [4, 5],
 538             [7, 8]])
 539  
 540      r�z*compress_rowcols works for 2D arrays only.r�)r6r��NotImplementedErrorr)rzrTrOrOrPrcs2rcC�$t|�}|jdkr
td��t|d�S)z�
 541      Suppress whole rows of a 2-D array that contain masked values.
 542  
 543      This is equivalent to ``np.ma.compress_rowcols(a, 0)``, see
 544      `compress_rowcols` for details.
 545  
 546      See Also
 547      --------
 548      compress_rowcols
 549  
 550      r�z'compress_rows works for 2D arrays only.r/�r6r�r�r�r\rOrOrPr
��
 551  
 552  r
cCr�)z�
 553      Suppress whole columns of a 2-D array that contain masked values.
 554  
 555      This is equivalent to ``np.ma.compress_rowcols(a, 1)``, see
 556      `compress_rowcols` for details.
 557  
 558      See Also
 559      --------
 560      compress_rowcols
 561  
 562      r�z'compress_cols works for 2D arrays only.r0r�r�rOrOrPr
 563  �r�r
 564  cC�$|tjur
tjdtdd�t|d�S)a�
 565      Mask rows of a 2D array that contain masked values.
 566  
 567      This function is a shortcut to ``mask_rowcols`` with `axis` equal to 0.
 568  
 569      See Also
 570      --------
 571      mask_rowcols : Mask rows and/or columns of a 2D array.
 572      masked_where : Mask where a condition is met.
 573  
 574      Examples
 575      --------
 576      >>> import numpy.ma as ma
 577      >>> a = np.zeros((3, 3), dtype=int)
 578      >>> a[1, 1] = 1
 579      >>> a
 580      array([[0, 0, 0],
 581             [0, 1, 0],
 582             [0, 0, 0]])
 583      >>> a = ma.masked_equal(a, 1)
 584      >>> a
 585      masked_array(
 586        data=[[0, 0, 0],
 587              [0, --, 0],
 588              [0, 0, 0]],
 589        mask=[[False, False, False],
 590              [False,  True, False],
 591              [False, False, False]],
 592        fill_value=1)
 593  
 594      >>> ma.mask_rows(a)
 595      masked_array(
 596        data=[[0, 0, 0],
 597              [--, --, --],
 598              [0, 0, 0]],
 599        mask=[[False, False, False],
 600              [ True,  True,  True],
 601              [False, False, False]],
 602        fill_value=1)
 603  
 604      �TThe axis argument has always been ignored, in future passing it will raise TypeErrorr���
 605  stacklevelr/�rXr��warnings�warn�DeprecationWarningr�r\rTrOrOrPr�s
 606  *�
 607  rcCr�)a�
 608      Mask columns of a 2D array that contain masked values.
 609  
 610      This function is a shortcut to ``mask_rowcols`` with `axis` equal to 1.
 611  
 612      See Also
 613      --------
 614      mask_rowcols : Mask rows and/or columns of a 2D array.
 615      masked_where : Mask where a condition is met.
 616  
 617      Examples
 618      --------
 619      >>> import numpy.ma as ma
 620      >>> a = np.zeros((3, 3), dtype=int)
 621      >>> a[1, 1] = 1
 622      >>> a
 623      array([[0, 0, 0],
 624             [0, 1, 0],
 625             [0, 0, 0]])
 626      >>> a = ma.masked_equal(a, 1)
 627      >>> a
 628      masked_array(
 629        data=[[0, 0, 0],
 630              [0, --, 0],
 631              [0, 0, 0]],
 632        mask=[[False, False, False],
 633              [False,  True, False],
 634              [False, False, False]],
 635        fill_value=1)
 636      >>> ma.mask_cols(a)
 637      masked_array(
 638        data=[[0, --, 0],
 639              [0, --, 0],
 640              [0, --, 0]],
 641        mask=[[False,  True, False],
 642              [False,  True, False],
 643              [False,  True, False]],
 644        fill_value=1)
 645  
 646      r�r�r�r0r�r�rOrOrPr�s
 647  )�
 648  rcCsht�|�j}|dd�|dd�}|g}|dur|�d|�|dur(|�|�t|�dkr2t|�}|S)a!
 649      Compute the differences between consecutive elements of an array.
 650  
 651      This function is the equivalent of `numpy.ediff1d` that takes masked
 652      values into account, see `numpy.ediff1d` for details.
 653  
 654      See Also
 655      --------
 656      numpy.ediff1d : Equivalent function for ndarrays.
 657  
 658      r0Nr�r/)rj�
 659  asanyarray�flat�insertr�r�r)rS�to_end�to_begin�edr�rOrOrPr's
 660  rcCsNtj|||d�}t|t�r t|�}|d�t�|d<t|�}|S|�t�}|S)a.
 661      Finds the unique elements of an array.
 662  
 663      Masked values are considered the same element (masked). The output array
 664      is always a masked array. See `numpy.unique` for more details.
 665  
 666      See Also
 667      --------
 668      numpy.unique : Equivalent function for ndarrays.
 669  
 670      Examples
 671      --------
 672      >>> import numpy.ma as ma
 673      >>> a = [1, 2, 1000, 2, 3]
 674      >>> mask = [0, 0, 1, 0, 0]
 675      >>> masked_a = ma.masked_array(a, mask)
 676      >>> masked_a
 677      masked_array(data=[1, 2, --, 2, 3],
 678                  mask=[False, False,  True, False, False],
 679          fill_value=999999)
 680      >>> ma.unique(masked_a)
 681      masked_array(data=[1, 2, 3, --],
 682                  mask=[False, False, False,  True],
 683          fill_value=999999)
 684      >>> ma.unique(masked_a, return_index=True)
 685      (masked_array(data=[1, 2, 3, --],
 686                  mask=[False, False, False,  True],
 687          fill_value=999999), array([0, 1, 4, 2]))
 688      >>> ma.unique(masked_a, return_inverse=True)
 689      (masked_array(data=[1, 2, 3, --],
 690                  mask=[False, False, False,  True],
 691          fill_value=999999), array([0, 1, 3, 1, 2]))
 692      >>> ma.unique(masked_a, return_index=True, return_inverse=True)
 693      (masked_array(data=[1, 2, 3, --],
 694                  mask=[False, False, False,  True],
 695          fill_value=999999), array([0, 1, 4, 2]), array([0, 1, 3, 1, 2]))
 696      )�return_index�return_inverser/)rXr+rKrLrMr_r2)�ar1rr�outputrOrOrPr+Ds&�
 697  
 698  �r+cCsV|r
 699  t�||f�}nt�t|�t|�f�}|��|dd�|dd�|dd�kS)a>
 700      Returns the unique elements common to both arrays.
 701  
 702      Masked values are considered equal one to the other.
 703      The output is always a masked array.
 704  
 705      See `numpy.intersect1d` for more details.
 706  
 707      See Also
 708      --------
 709      numpy.intersect1d : Equivalent function for ndarrays.
 710  
 711      Examples
 712      --------
 713      >>> x = np.ma.array([1, 3, 3, 3], mask=[0, 0, 0, 1])
 714      >>> y = np.ma.array([3, 1, 1, 1], mask=[0, 0, 0, 1])
 715      >>> np.ma.intersect1d(x, y)
 716      masked_array(data=[1, 3, --],
 717                   mask=[False, False,  True],
 718             fill_value=999999)
 719  
 720      Nr�r0)rjr7r+rB)r�ar2�
assume_unique�auxrOrOrPrvs
 721  $rcCs�|s
 722  t|�}t|�}t�||f�}|jdkr|S|��|��}t�dg|dd�|dd�kdgf�}|dd�|dd�k}||S)z�
 723      Set exclusive-or of 1-D arrays with unique elements.
 724  
 725      The output is always a masked array. See `numpy.setxor1d` for more details.
 726  
 727      See Also
 728      --------
 729      numpy.setxor1d : Equivalent function for ndarrays.
 730  
 731      r/Tr0Nr�)r+rjr7r�rBr8)rrr	r
 732  Zauxf�flagZflag2rOrOrPr)�s
 733  (r)cCs�|st|dd�\}}t|�}t�||f�}|jdd�}||}|r.|dd�|dd�k}n|dd�|dd�k}t�||gf�}	|jdd�dt|��}
 734  |rT|	|
 735  S|	|
 736  |S)a�
 737      Test whether each element of an array is also present in a second
 738      array.
 739  
 740      The output is always a masked array. See `numpy.in1d` for more details.
 741  
 742      We recommend using :func:`isin` instead of `in1d` for new code.
 743  
 744      See Also
 745      --------
 746      isin       : Version of this function that preserves the shape of ar1.
 747      numpy.in1d : Equivalent function for ndarrays.
 748  
 749      Notes
 750      -----
 751      .. versionadded:: 1.4.0
 752  
 753      T)r�	mergesort)�kindr0Nr�)r+rjr7�argsortr�)rrr	�invert�rev_idx�ar�order�sar�bool_arr�indxrOrOrPr�srcCs"t�|�}t||||d��|j�S)a|
 754      Calculates `element in test_elements`, broadcasting over
 755      `element` only.
 756  
 757      The output is always a masked array of the same shape as `element`.
 758      See `numpy.isin` for more details.
 759  
 760      See Also
 761      --------
 762      in1d       : Flattened version of this function.
 763      numpy.isin : Equivalent function for ndarrays.
 764  
 765      Notes
 766      -----
 767      .. versionadded:: 1.13.0
 768  
 769      �r	r)rjr6r�reshaperZ)�element�
test_elementsr	rrOrOrPr�s
 770  ��rcCsttj||fdd��S)z�
 771      Union of two arrays.
 772  
 773      The output is always a masked array. See `numpy.union1d` for more details.
 774  
 775      See Also
 776      --------
 777      numpy.union1d : Equivalent function for ndarrays.
 778  
 779      Nr�)r+rjr7)rrrOrOrPr,�sr,cCs8|r
 780  t�|���}nt|�}t|�}|t||ddd�S)a�
 781      Set difference of 1D arrays with unique elements.
 782  
 783      The output is always a masked array. See `numpy.setdiff1d` for more
 784      details.
 785  
 786      See Also
 787      --------
 788      numpy.setdiff1d : Equivalent function for ndarrays.
 789  
 790      Examples
 791      --------
 792      >>> x = np.ma.array([1, 2, 3, 4], mask=[0, 1, 0, 1])
 793      >>> np.ma.setdiff1d(x, [1, 2])
 794      masked_array(data=[3, --],
 795                   mask=[False,  True],
 796             fill_value=999999)
 797  
 798      Tr)rjr6r�r+r)rrr	rOrOrPr(s
 799  r(Tc
 800  CsVtj|ddtd�}t�|�}|s|��rtd��|jddkr!d}tt|��}d|}|r4t	d�df}ndt	d�f}|durGt
 801  �|��t�}nUt|ddtd	�}t�|�}|s^|��r^td��|��sf|��r�|j|jkr�t
 802  �
||�}	|	tur�|	}|_|_}d|_d|_t�||f|�}t
 803  �t
 804  �||f|���t�}||j|d
 805  �|8}|||fS)z_
 806      Private function for the computation of covariance and correlation
 807      coefficients.
 808  
 809      r�T)�ndminr�r[zCannot process masked data.r/r0NF)r�rr[r�)rjr5�floatr;r�r�rZ�int�boolr�rX�logical_not�astype�
 810  logical_orr@r`�_sharedmaskr7r�)
 811  rz�y�rowvar�allow_masked�xmaskrT�tup�xnotmask�ymaskZcommon_maskrOrOrP�
 812  _covhelper!s8
 813  
 814  
 815  r)c	Cs�|dur|t|�krtd��|dur|rd}nd}t||||�\}}}|s@t�|j|�d|}t|j|��dd�|��}|St�||j�d|}t||j��dd�|��}|S)aK
 816      Estimate the covariance matrix.
 817  
 818      Except for the handling of missing data this function does the same as
 819      `numpy.cov`. For more details and examples, see `numpy.cov`.
 820  
 821      By default, masked values are recognized as such. If `x` and `y` have the
 822      same shape, a common mask is allocated: if ``x[i,j]`` is masked, then
 823      ``y[i,j]`` will also be masked.
 824      Setting `allow_masked` to False will raise an exception if values are
 825      missing in either of the input arrays.
 826  
 827      Parameters
 828      ----------
 829      x : array_like
 830          A 1-D or 2-D array containing multiple variables and observations.
 831          Each row of `x` represents a variable, and each column a single
 832          observation of all those variables. Also see `rowvar` below.
 833      y : array_like, optional
 834          An additional set of variables and observations. `y` has the same
 835          shape as `x`.
 836      rowvar : bool, optional
 837          If `rowvar` is True (default), then each row represents a
 838          variable, with observations in the columns. Otherwise, the relationship
 839          is transposed: each column represents a variable, while the rows
 840          contain observations.
 841      bias : bool, optional
 842          Default normalization (False) is by ``(N-1)``, where ``N`` is the
 843          number of observations given (unbiased estimate). If `bias` is True,
 844          then normalization is by ``N``. This keyword can be overridden by
 845          the keyword ``ddof`` in numpy versions >= 1.5.
 846      allow_masked : bool, optional
 847          If True, masked values are propagated pair-wise: if a value is masked
 848          in `x`, the corresponding value is masked in `y`.
 849          If False, raises a `ValueError` exception when some values are missing.
 850      ddof : {None, int}, optional
 851          If not ``None`` normalization is by ``(N - ddof)``, where ``N`` is
 852          the number of observations; this overrides the value implied by
 853          ``bias``. The default value is ``None``.
 854  
 855          .. versionadded:: 1.5
 856  
 857      Raises
 858      ------
 859      ValueError
 860          Raised if some values are missing and `allow_masked` is False.
 861  
 862      See Also
 863      --------
 864      numpy.cov
 865  
 866      Nzddof must be an integerr/r0��?F��strict)rr�r)rXr�T�conj�squeeze)	rzr"r#�biasr$�ddofr'�factr�rOrOrPrLs6�rcCs�d}|tjus|tjurtj|tdd�t||||�\}}}|s8t�|j|�d}t|j|��dd�|�	�}	nt�||j�d}t||j��dd�|�	�}	zt
 867  �|	�}
 868  Wn
 869  ty`YdSw|�
�rst
 870  �t
 871  j�|
 872  |
 873  ��}|	|St|
 874  �}d|_|jd|}|r�t|d�D]/}
t|
d|�D]%}tt||
||f��jdd�}t
 875  �t
 876  j�|��||
|f<|||
f<q�q�|	|St|d�D]7}
t|
d|�D]-}tt|d	d	�|
f|d	d	�|ff��jdd�}t
 877  �t
 878  j�|��||
|f<|||
f<q�q�|	|S)
 879  a6
 880      Return Pearson product-moment correlation coefficients.
 881  
 882      Except for the handling of missing data this function does the same as
 883      `numpy.corrcoef`. For more details and examples, see `numpy.corrcoef`.
 884  
 885      Parameters
 886      ----------
 887      x : array_like
 888          A 1-D or 2-D array containing multiple variables and observations.
 889          Each row of `x` represents a variable, and each column a single
 890          observation of all those variables. Also see `rowvar` below.
 891      y : array_like, optional
 892          An additional set of variables and observations. `y` has the same
 893          shape as `x`.
 894      rowvar : bool, optional
 895          If `rowvar` is True (default), then each row represents a
 896          variable, with observations in the columns. Otherwise, the relationship
 897          is transposed: each column represents a variable, while the rows
 898          contain observations.
 899      bias : _NoValue, optional
 900          Has no effect, do not use.
 901  
 902          .. deprecated:: 1.10.0
 903      allow_masked : bool, optional
 904          If True, masked values are propagated pair-wise: if a value is masked
 905          in `x`, the corresponding value is masked in `y`.
 906          If False, raises an exception.  Because `bias` is deprecated, this
 907          argument needs to be treated as keyword only to avoid a warning.
 908      ddof : _NoValue, optional
 909          Has no effect, do not use.
 910  
 911          .. deprecated:: 1.10.0
 912  
 913      See Also
 914      --------
 915      numpy.corrcoef : Equivalent function in top-level NumPy module.
 916      cov : Estimate the covariance matrix.
 917  
 918      Notes
 919      -----
 920      This function accepts but discards arguments `bias` and `ddof`.  This is
 921      for backwards compatibility with previous versions of this function.  These
 922      arguments had no effect on the return values of the function and can be
 923      safely ignored in this and previous versions of numpy.
 924      z/bias and ddof have no effect and are deprecatedr�r�r*Fr+r0r�N)rXr�r�r�r�r)rr-r.r/rj�diagonalr�r��sqrtr��outerrr!rZr�rr.�var�reduce)rzr"r#r0r$r1�msgr'r2�c�diagZ_denomr�r�r��_xrOrOrPr�sJ0�� (�	�"��(�rcs8eZdZdZee�Ze�fdd��Z�fdd�Z�Z	S)�MAxisConcatenatorz�
 925      Translate slice objects to concatenation along an axis.
 926  
 927      For documentation on usage, see `mr_class`.
 928  
 929      See Also
 930      --------
 931      mr_class
 932  
 933      cs t�j|jdd�}t||jd�S)NFr�rV)�super�makematr�r5rW)�clsrSr���	__class__rOrPr>�szMAxisConcatenator.makematcst|t�r	td��t��|�S)NzUnavailable for masked array.)rK�strr3r=�__getitem__)rf�keyr@rOrPrCs
 934  zMAxisConcatenator.__getitem__)
 935  rcrtrure�staticmethodr7�classmethodr>rC�
__classcell__rOrOr@rPr<�s
 936  r<c@rv)�mr_classa�
 937      Translate slice objects to concatenation along the first axis.
 938  
 939      This is the masked array version of `lib.index_tricks.RClass`.
 940  
 941      See Also
 942      --------
 943      lib.index_tricks.RClass
 944  
 945      Examples
 946      --------
 947      >>> np.ma.mr_[np.ma.array([1,2,3]), 0, 0, np.ma.array([4,5,6])]
 948      masked_array(data=[1, 2, 3, ..., 4, 5, 6],
 949                   mask=False,
 950             fill_value=999999)
 951  
 952      cCst�|d�dS)Nr/)r<rh)rfrOrOrPrhszmr_class.__init__N)rcrtrurerhrOrOrOrPrH
srHccsD�tt�|�t|�j�D]\}}|s|Vq|s|dtfVqdS)a�
 953      Multidimensional index iterator.
 954  
 955      Return an iterator yielding pairs of array coordinates and values,
 956      skipping elements that are masked. With `compressed=False`,
 957      `ma.masked` is yielded as the value of masked elements. This
 958      behavior differs from that of `numpy.ndenumerate`, which yields the
 959      value of the underlying data array.
 960  
 961      Notes
 962      -----
 963      .. versionadded:: 1.23.0
 964  
 965      Parameters
 966      ----------
 967      a : array_like
 968          An array with (possibly) masked elements.
 969      compressed : bool, optional
 970          If True (default), masked elements are skipped.
 971  
 972      See Also
 973      --------
 974      numpy.ndenumerate : Equivalent function ignoring any mask.
 975  
 976      Examples
 977      --------
 978      >>> a = np.ma.arange(9).reshape((3, 3))
 979      >>> a[1, 0] = np.ma.masked
 980      >>> a[1, 2] = np.ma.masked
 981      >>> a[2, 1] = np.ma.masked
 982      >>> a
 983      masked_array(
 984        data=[[0, 1, 2],
 985              [--, 4, --],
 986              [6, --, 8]],
 987        mask=[[False, False, False],
 988              [ True, False,  True],
 989              [False,  True, False]],
 990        fill_value=999999)
 991      >>> for index, x in np.ma.ndenumerate(a):
 992      ...     print(index, x)
 993      (0, 0) 0
 994      (0, 1) 1
 995      (0, 2) 2
 996      (1, 1) 4
 997      (2, 0) 6
 998      (2, 2) 8
 999  
1000      >>> for index, x in np.ma.ndenumerate(a, compressed=False):
1001      ...     print(index, x)
1002      (0, 0) 0
1003      (0, 1) 1
1004      (0, 2) 2
1005      (1, 0) --
1006      (1, 1) 4
1007      (1, 2) --
1008      (2, 0) 6
1009      (2, 1) --
1010      (2, 2) 8
1011      r/N)�ziprXr#r;r�r=)r\�
1012  compressed�itrWrOrOrPr#)s�=��r#cCsVt|�}|tus
t�|�st�d|jdg�St�|�}t|�dkr)|ddgSdS)a�
1013      Find the indices of the first and last unmasked values.
1014  
1015      Expects a 1-D `MaskedArray`, returns None if all values are masked.
1016  
1017      Parameters
1018      ----------
1019      a : array_like
1020          Input 1-D `MaskedArray`
1021  
1022      Returns
1023      -------
1024      edges : ndarray or None
1025          The indices of first and last non-masked value in the array.
1026          Returns None if all values are masked.
1027  
1028      See Also
1029      --------
1030      flatnotmasked_contiguous, notmasked_contiguous, notmasked_edges
1031      clump_masked, clump_unmasked
1032  
1033      Notes
1034      -----
1035      Only accepts 1-D arrays.
1036  
1037      Examples
1038      --------
1039      >>> a = np.ma.arange(10)
1040      >>> np.ma.flatnotmasked_edges(a)
1041      array([0, 9])
1042  
1043      >>> mask = (a < 3) | (a > 8) | (a == 5)
1044      >>> a[mask] = np.ma.masked
1045      >>> np.array(a[~a.mask])
1046      array([3, 4, 6, 7, 8])
1047  
1048      >>> np.ma.flatnotmasked_edges(a)
1049      array([3, 8])
1050  
1051      >>> a[:] = np.ma.masked
1052      >>> print(np.ma.flatnotmasked_edges(a))
1053      None
1054  
1055      r/r0r�N)r:r@rXr�r5r��flatnonzeror�)r\rUZunmaskedrOrOrPrms-rcs�t|�}�dus
|jdkrt|�St|�}tt�|j�t�|g|j�d��t��fdd�t	|j�D��t��fdd�t	|j�D��gS)a_
1056      Find the indices of the first and last unmasked values along an axis.
1057  
1058      If all values are masked, return None.  Otherwise, return a list
1059      of two tuples, corresponding to the indices of the first and last
1060      unmasked values respectively.
1061  
1062      Parameters
1063      ----------
1064      a : array_like
1065          The input array.
1066      axis : int, optional
1067          Axis along which to perform the operation.
1068          If None (default), applies to a flattened version of the array.
1069  
1070      Returns
1071      -------
1072      edges : ndarray or list
1073          An array of start and end indexes if there are any masked data in
1074          the array. If there are no masked data in the array, `edges` is a
1075          list of the first and last index.
1076  
1077      See Also
1078      --------
1079      flatnotmasked_contiguous, flatnotmasked_edges, notmasked_contiguous
1080      clump_masked, clump_unmasked
1081  
1082      Examples
1083      --------
1084      >>> a = np.arange(9).reshape((3, 3))
1085      >>> m = np.zeros_like(a)
1086      >>> m[1:, 1:] = 1
1087  
1088      >>> am = np.ma.array(a, mask=m)
1089      >>> np.array(am[~am.mask])
1090      array([0, 1, 2, 3, 6])
1091  
1092      >>> np.ma.notmasked_edges(am)
1093      array([0, 6])
1094  
1095      Nr0rVc�g|]}�|������qSrO)�minrJ�r�r��rTr�rOrPr���z#notmasked_edges.<locals>.<listcomp>crMrO)r�rJrOrPrOrPr��rQ)
1096  r6r�rr;r5rX�indicesrZrLr�)r\rTrUrOrPrPr%�s*"�r%cCsnt|�}|turtd|j�gSd}g}t�|���D]\}}tt|��}|s0|�	t|||��||7}q|S)a�
1097      Find contiguous unmasked data in a masked array.
1098  
1099      Parameters
1100      ----------
1101      a : array_like
1102          The input array.
1103  
1104      Returns
1105      -------
1106      slice_list : list
1107          A sorted sequence of `slice` objects (start index, end index).
1108  
1109          .. versionchanged:: 1.15.0
1110              Now returns an empty list instead of None for a fully masked array
1111  
1112      See Also
1113      --------
1114      flatnotmasked_edges, notmasked_contiguous, notmasked_edges
1115      clump_masked, clump_unmasked
1116  
1117      Notes
1118      -----
1119      Only accepts 2-D arrays at most.
1120  
1121      Examples
1122      --------
1123      >>> a = np.ma.arange(10)
1124      >>> np.ma.flatnotmasked_contiguous(a)
1125      [slice(0, 10, None)]
1126  
1127      >>> mask = (a < 3) | (a > 8) | (a == 5)
1128      >>> a[mask] = np.ma.masked
1129      >>> np.array(a[~a.mask])
1130      array([3, 4, 6, 7, 8])
1131  
1132      >>> np.ma.flatnotmasked_contiguous(a)
1133      [slice(3, 5, None), slice(6, 9, None)]
1134      >>> a[:] = np.ma.masked
1135      >>> np.ma.flatnotmasked_contiguous(a)
1136      []
1137  
1138      r/)
1139  r:r@r�r��	itertools�groupbyr�r�rMr�)r\rUr�r�r��gr�rOrOrPr�s,
1140  rcCs�t|�}|j}|dkrtd��|dus|dkrt|�Sg}|dd}ddg}tdd�||<t|j|�D]}|||<|�t|t|���q5|S)a�
1141      Find contiguous unmasked data in a masked array along the given axis.
1142  
1143      Parameters
1144      ----------
1145      a : array_like
1146          The input array.
1147      axis : int, optional
1148          Axis along which to perform the operation.
1149          If None (default), applies to a flattened version of the array, and this
1150          is the same as `flatnotmasked_contiguous`.
1151  
1152      Returns
1153      -------
1154      endpoints : list
1155          A list of slices (start and end indexes) of unmasked indexes
1156          in the array.
1157  
1158          If the input is 2d and axis is specified, the result is a list of lists.
1159  
1160      See Also
1161      --------
1162      flatnotmasked_edges, flatnotmasked_contiguous, notmasked_edges
1163      clump_masked, clump_unmasked
1164  
1165      Notes
1166      -----
1167      Only accepts 2-D arrays at most.
1168  
1169      Examples
1170      --------
1171      >>> a = np.arange(12).reshape((3, 4))
1172      >>> mask = np.zeros_like(a)
1173      >>> mask[1:, :-1] = 1; mask[0, 1] = 1; mask[-1, 0] = 0
1174      >>> ma = np.ma.array(a, mask=mask)
1175      >>> ma
1176      masked_array(
1177        data=[[0, --, 2, 3],
1178              [--, --, --, 7],
1179              [8, --, --, 11]],
1180        mask=[[False,  True, False, False],
1181              [ True,  True,  True, False],
1182              [False,  True,  True, False]],
1183        fill_value=999999)
1184      >>> np.array(ma[~ma.mask])
1185      array([ 0,  2,  3,  7, 8, 11])
1186  
1187      >>> np.ma.notmasked_contiguous(ma)
1188      [slice(0, 1, None), slice(2, 4, None), slice(7, 9, None), slice(11, 12, None)]
1189  
1190      >>> np.ma.notmasked_contiguous(ma, axis=0)
1191      [[slice(0, 1, None), slice(2, 3, None)], [], [slice(0, 1, None)], [slice(0, 3, None)]]
1192  
1193      >>> np.ma.notmasked_contiguous(ma, axis=1)
1194      [[slice(0, 1, None), slice(2, 4, None)], [slice(3, 4, None)], [slice(0, 1, None), slice(3, 4, None)]]
1195  
1196      r�z&Currently limited to at most 2D array.Nr0r/)	r6r�r�rr�r�rZr�rL)r\rTr�r��otherr�r�rOrOrPr$s:r$c	Cs�|jdkr	|��}|dd�|dd�A��}|dd}|drNt|�dkr.td|j�gStd|d�g}|�dd�t|ddd�|ddd��D��nt|�dkrVgSdd	�t|ddd�|ddd��D�}|dry|�t|d|j��|S)
1197  zv
1198      Finds the clumps (groups of data with the same values) for a 1D bool array.
1199  
1200      Returns a series of slices.
1201      r0Nr�r/css�|]
1202  \}}t||�VqdSrb�r��r��left�rightrOrOrP�	<genexpr>ms��z_ezclump.<locals>.<genexpr>r�cSsg|]	\}}t||��qSrOrWrXrOrOrPr�ssz_ezclump.<locals>.<listcomp>)	r�r��nonzeror�r�r��extendrIr�)rWr��rrOrOrP�_ezclump]s"
1203  
1204  
1205  �(r_cCs,t|dt�}|turtd|j�gSt|�S)a�
1206      Return list of slices corresponding to the unmasked clumps of a 1-D array.
1207      (A "clump" is defined as a contiguous region of the array).
1208  
1209      Parameters
1210      ----------
1211      a : ndarray
1212          A one-dimensional masked array.
1213  
1214      Returns
1215      -------
1216      slices : list of slice
1217          The list of slices, one for each continuous region of unmasked
1218          elements in `a`.
1219  
1220      Notes
1221      -----
1222      .. versionadded:: 1.4.0
1223  
1224      See Also
1225      --------
1226      flatnotmasked_edges, flatnotmasked_contiguous, notmasked_edges
1227      notmasked_contiguous, clump_masked
1228  
1229      Examples
1230      --------
1231      >>> a = np.ma.masked_array(np.arange(10))
1232      >>> a[[0, 1, 2, 6, 8, 9]] = np.ma.masked
1233      >>> np.ma.clump_unmasked(a)
1234      [slice(3, 6, None), slice(7, 8, None)]
1235  
1236      r`r/)rir@r�r�r_�r\rWrOrOrPrzs!
1237  rcCst�|�}|turgSt|�S)a
1238  
1239      Returns a list of slices corresponding to the masked clumps of a 1-D array.
1240      (A "clump" is defined as a contiguous region of the array).
1241  
1242      Parameters
1243      ----------
1244      a : ndarray
1245          A one-dimensional masked array.
1246  
1247      Returns
1248      -------
1249      slices : list of slice
1250          The list of slices, one for each continuous region of masked elements
1251          in `a`.
1252  
1253      Notes
1254      -----
1255      .. versionadded:: 1.4.0
1256  
1257      See Also
1258      --------
1259      flatnotmasked_edges, flatnotmasked_contiguous, notmasked_edges
1260      notmasked_contiguous, clump_unmasked
1261  
1262      Examples
1263      --------
1264      >>> a = np.ma.masked_array(np.arange(10))
1265      >>> a[[0, 1, 2, 6, 8, 9]] = np.ma.masked
1266      >>> np.ma.clump_masked(a)
1267      [slice(0, 3, None), slice(6, 7, None), slice(8, 10, None)]
1268  
1269      )rjr:r@r_r`rOrOrPr�s
1270  !rcCs(t�||�}t|�}|turd||<|S)zD
1271      Masked values in the input array result in rows of zeros.
1272  
1273      r/)rXr-r:r@)rzr�Z_vanderrUrOrOrPr-�s
1274  r-c
1275  	Cst|�}t|�}t|�}|jdkrt|t|��}n|jdkr4tt|��}|tur3t||dd�df�}ntd��|dur^t|�}|jdkrItd��|jd|jdkrWtd��t|t|��}|tur||}	|durm||	}t�	||	||	|||||�St�	|||||||�S)zE
1276      Any masked values in x is propagated in y, and vice-versa.
1277  
1278      r0r�Nr/z Expected a 1D or 2D array for y!z expected a 1-d array for weightsz(expected w and y to have the same length)
1279  r6r:r�r?rr@r�rZrXr&)
1280  rzr"�deg�rcond�full�wrrU�myZnot_mrOrOrPr&�s0
1281  
1282  �
1283  r&rb)NNF)NNFF)NN)FF)F)NTT)NTFTN)T)NFNF)fre�__all__rSr��r1rjr2r3r4r5r6r7r8r9r:r;r<r=r>r?r@rArBrCrDrErr�numpyrXrFr��numpy.core.multiarrayrG�numpy.core.numericrH�numpy.lib.function_baserI�numpy.lib.index_tricksrJrQrrrr rarwr�r�r�rrrr.r'rr	rr*rrr�rr�find�rstripr�rr!r�rrr
r
1284  rrrr+rr)rrr,r(r)rrr<rHr"r#rr%rr$r_rrr-rlr&rOrOrOrP�<module>s�
1285  `
1286  64:2

1287  
1288  P
1289  ��0�
1290  
1291  O
1292  U
1293  +73
1294  6
1295  
1296  2
1297   
1298  
1299  *
1300  
1301  !
1302  +
1303  I
1304  �Z
1305  D
1306  73
1307  9M'
1308  ,
1309  "