σ
ίΘ[c           @` sέ   d  d l  m Z m Z m Z m Z d  d l Z d  d l Z d d l m	 Z	 d d l
 m Z d d d g Z d	 e f d
     YZ d d d d e j j e j d e d  Z d d d d d d e j j e j d  d d 
 Z d S(   i    (   t   absolute_importt   divisiont   print_functiont   unicode_literalsNi   (   t   AstropyUserWarning(   t   rangeu	   SigmaClipu
   sigma_clipu   sigma_clipped_statst	   SigmaClipc           B` s_   e  Z d  Z d d d d e j j e j d  Z d   Z	 d   Z
 d d  Z d e d  Z RS(	   uͺ  
    Class to perform sigma clipping.

    The data will be iterated over, each time rejecting points that are
    discrepant by more than a specified number of standard deviations
    from a center value. If the data contains invalid values (NaNs or
    infs), they are automatically masked before performing the sigma
    clipping.

    For a functional interface to sigma clipping, see
    :func:`sigma_clip`.

    .. note::
        `scipy.stats.sigmaclip
        <https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.sigmaclip.html>`_
        provides a subset of the functionality in this class.

    Parameters
    ----------
    sigma : float, optional
        The number of standard deviations to use for both the lower and
        upper clipping limit. These limits are overridden by
        ``sigma_lower`` and ``sigma_upper``, if input. Defaults to 3.
    sigma_lower : float or `None`, optional
        The number of standard deviations to use as the lower bound for
        the clipping limit. If `None` then the value of ``sigma`` is
        used. Defaults to `None`.
    sigma_upper : float or `None`, optional
        The number of standard deviations to use as the upper bound for
        the clipping limit. If `None` then the value of ``sigma`` is
        used. Defaults to `None`.
    iters : int or `None`, optional
        The number of iterations to perform sigma clipping, or `None` to
        clip until convergence is achieved (i.e., continue until the
        last iteration clips nothing). Defaults to 5.
    cenfunc : callable, optional
        The function used to compute the center for the clipping. Must
        be a callable that takes in a masked array and outputs the
        central value. Defaults to the median (`numpy.ma.median`).
    stdfunc : callable, optional
        The function used to compute the standard deviation about the
        center. Must be a callable that takes in a masked array and
        outputs a width estimator. Masked (rejected) pixels are those
        where::

             deviation < (-sigma_lower * stdfunc(deviation))
             deviation > (sigma_upper * stdfunc(deviation))

        where::

            deviation = data - cenfunc(data [,axis=int])

        Defaults to the standard deviation (`numpy.std`).

    See Also
    --------
    sigma_clip

    Examples
    --------
    This example generates random variates from a Gaussian distribution
    and returns a masked array in which all points that are more than 2
    sample standard deviations from the median are masked::

        >>> from astropy.stats import SigmaClip
        >>> from numpy.random import randn
        >>> randvar = randn(10000)
        >>> sigclip = SigmaClip(sigma=2, iters=5)
        >>> filtered_data = sigclip(randvar)

    This example sigma clips on a similar distribution, but uses 3 sigma
    relative to the sample *mean*, clips until convergence, and does not
    copy the data::

        >>> from astropy.stats import SigmaClip
        >>> from numpy.random import randn
        >>> from numpy import mean
        >>> randvar = randn(10000)
        >>> sigclip = SigmaClip(sigma=3, iters=None, cenfunc=mean)
        >>> filtered_data = sigclip(randvar, copy=False)

    This example sigma clips along one axis on a similar distribution
    (with bad points inserted)::

        >>> from astropy.stats import SigmaClip
        >>> from numpy.random import normal
        >>> from numpy import arange, diag, ones
        >>> data = arange(5) + normal(0., 0.05, (5, 5)) + diag(ones(5))
        >>> sigclip = SigmaClip(sigma=2.3)
        >>> filtered_data = sigclip(data, axis=0)

    Note that along the other axis, no points would be masked, as the
    variance is higher.
    g      @i   c         C` s:   | |  _  | |  _ | |  _ | |  _ | |  _ | |  _ d  S(   N(   t   sigmat   sigma_lowert   sigma_uppert   iterst   cenfunct   stdfunc(   t   selfR   R   R	   R
   R   R   (    (    s;   lib/python2.7/site-packages/astropy/stats/sigma_clipping.pyt   __init__n   s    					c         C` s.   d j  |  j |  j |  j |  j |  j |  j  S(   Nu[   SigmaClip(sigma={0}, sigma_lower={1}, sigma_upper={2}, iters={3}, cenfunc={4}, stdfunc={5})(   t   formatR   R   R	   R
   R   R   (   R   (    (    s;   lib/python2.7/site-packages/astropy/stats/sigma_clipping.pyt   __repr__w   s    c         C` so   d |  j  j d g } d d d d d d g } x0 | D]( } | j d	 j | t |  |    q6 Wd
 j |  S(   Nu   <u   >u   sigmau   sigma_loweru   sigma_upperu   itersu   cenfuncu   stdfuncu       {0}: {1}u   
(   t	   __class__t   __name__t   appendR   t   getattrt   join(   R   t   linest   attrst   attr(    (    s;   lib/python2.7/site-packages/astropy/stats/sigma_clipping.pyt   __str__}   s    	&c         C` s   | j  d k r | S|  j | d | } |  j | d | } | | |  j } | | |  j 7} | d k	 r§ | d k r§ t j | d | } t j | d | } q§ n  | t j j	 k rς t j j
 t j d t } t j j
 t j d t } n  | j | | k O_ | j | | k  O_ | S(   u@  
        Perform sigma clip by comparing the data to the minimum and
        maximum values (median + sig * standard deviation). Use
        sigma_lower and sigma_upper to get the correct limits. Data
        values less or greater than the minimum / maximum values
        will have True set in the mask array.
        i    t   axist   maskN(   t   sizeR   R   R   R	   t   Nonet   npt   expand_dimst   mat   maskedt   MaskedArrayt   nant   TrueR   (   R   t   _filtered_dataR   t	   max_valuet   stdt	   min_value(    (    s;   lib/python2.7/site-packages/astropy/stats/sigma_clipping.pyt   _perform_clip   s     	c         C` s;  |  j  d k r |  j |  _  n  |  j d k r< |  j |  _ n  t j t j |   rz t j j |  } t	 j
 d t  n  t j j | d | } |  j d k rμ | j   d } xe | j   | k rθ | j   } |  j | d | q΄ Wn- x* t |  j  D] } |  j | d | qό W| j j d k r7t | _ n  | S(   uΝ  
        Perform sigma clipping on the provided data.

        Parameters
        ----------
        data : array-like
            The data to be sigma clipped.
        axis : int or `None`, optional
            If not `None`, clip along the given axis.  For this case,
            ``axis`` will be passed on to ``cenfunc`` and ``stdfunc``,
            which are expected to return an array with the axis
            dimension removed (like the numpy functions).  If `None`,
            clip over all axes.  Defaults to `None`.
        copy : bool, optional
            If `True`, the ``data`` array will be copied.  If `False`,
            the returned masked array data will contain the same array
            as ``data``.  Defaults to `True`.

        Returns
        -------
        filtered_data : `numpy.ma.MaskedArray`
            A masked array with the same shape as ``data`` input, where
            the points rejected by the algorithm have been masked.
        uS   Input data contains invalid values (NaNs or infs), which were automatically masked.t   copyi   R   N(    (   R   R   R   R	   R   t   anyt   isfiniteR    t   masked_invalidt   warningst   warnR   t   arrayR
   t   countR)   R   R   t   shapet   False(   R   t   dataR   R*   t   filtered_datat   lastrejt   i(    (    s;   lib/python2.7/site-packages/astropy/stats/sigma_clipping.pyt   __call__£   s&    	
N(   R   t
   __module__t   __doc__R   R   R    t   medianR'   R   R   R   R)   R$   R8   (    (    (    s;   lib/python2.7/site-packages/astropy/stats/sigma_clipping.pyR      s   ^		i   i   c	   
      C` sC   t  d | d | d | d | d | d |  }	 |	 |  d | d | S(	   u  
    Perform sigma-clipping on the provided data.

    The data will be iterated over, each time rejecting points that are
    discrepant by more than a specified number of standard deviations from a
    center value. If the data contains invalid values (NaNs or infs),
    they are automatically masked before performing the sigma clipping.

    For an object-oriented interface to sigma clipping, see
    :func:`SigmaClip`.

    .. note::
        `scipy.stats.sigmaclip
        <https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.sigmaclip.html>`_
        provides a subset of the functionality in this function.

    Parameters
    ----------
    data : array-like
        The data to be sigma clipped.
    sigma : float, optional
        The number of standard deviations to use for both the lower and
        upper clipping limit. These limits are overridden by
        ``sigma_lower`` and ``sigma_upper``, if input. Defaults to 3.
    sigma_lower : float or `None`, optional
        The number of standard deviations to use as the lower bound for
        the clipping limit. If `None` then the value of ``sigma`` is
        used. Defaults to `None`.
    sigma_upper : float or `None`, optional
        The number of standard deviations to use as the upper bound for
        the clipping limit. If `None` then the value of ``sigma`` is
        used. Defaults to `None`.
    iters : int or `None`, optional
        The number of iterations to perform sigma clipping, or `None` to
        clip until convergence is achieved (i.e., continue until the
        last iteration clips nothing). Defaults to 5.
    cenfunc : callable, optional
        The function used to compute the center for the clipping. Must
        be a callable that takes in a masked array and outputs the
        central value. Defaults to the median (`numpy.ma.median`).
    stdfunc : callable, optional
        The function used to compute the standard deviation about the
        center. Must be a callable that takes in a masked array and
        outputs a width estimator. Masked (rejected) pixels are those
        where::

             deviation < (-sigma_lower * stdfunc(deviation))
             deviation > (sigma_upper * stdfunc(deviation))

        where::

            deviation = data - cenfunc(data [,axis=int])

        Defaults to the standard deviation (`numpy.std`).
    axis : int or `None`, optional
        If not `None`, clip along the given axis.  For this case,
        ``axis`` will be passed on to ``cenfunc`` and ``stdfunc``, which
        are expected to return an array with the axis dimension removed
        (like the numpy functions).  If `None`, clip over all axes.
        Defaults to `None`.
    copy : bool, optional
        If `True`, the ``data`` array will be copied.  If `False`, the
        returned masked array data will contain the same array as
        ``data``.  Defaults to `True`.

    Returns
    -------
    filtered_data : `numpy.ma.MaskedArray`
        A masked array with the same shape as ``data`` input, where the
        points rejected by the algorithm have been masked.

    Notes
    -----
     1. The routine works by calculating::

            deviation = data - cenfunc(data [,axis=int])

        and then setting a mask for points outside the range::

           deviation < (-sigma_lower * stdfunc(deviation))
           deviation > (sigma_upper * stdfunc(deviation))

        It will iterate a given number of times, or until no further
        data are rejected.

     2. Most numpy functions deal well with masked arrays, but if one
        would like to have an array with just the good (or bad) values, one
        can use::

            good_only = filtered_data.data[~filtered_data.mask]
            bad_only = filtered_data.data[filtered_data.mask]

        However, for multidimensional data, this flattens the array,
        which may not be what one wants (especially if filtering was
        done along an axis).

    See Also
    --------
    SigmaClip

    Examples
    --------
    This example generates random variates from a Gaussian distribution
    and returns a masked array in which all points that are more than 2
    sample standard deviations from the median are masked::

        >>> from astropy.stats import sigma_clip
        >>> from numpy.random import randn
        >>> randvar = randn(10000)
        >>> filtered_data = sigma_clip(randvar, sigma=2, iters=5)

    This example sigma clips on a similar distribution, but uses 3 sigma
    relative to the sample *mean*, clips until convergence, and does not
    copy the data::

        >>> from astropy.stats import sigma_clip
        >>> from numpy.random import randn
        >>> from numpy import mean
        >>> randvar = randn(10000)
        >>> filtered_data = sigma_clip(randvar, sigma=3, iters=None,
        ...                            cenfunc=mean, copy=False)

    This example sigma clips along one axis on a similar distribution
    (with bad points inserted)::

        >>> from astropy.stats import sigma_clip
        >>> from numpy.random import normal
        >>> from numpy import arange, diag, ones
        >>> data = arange(5) + normal(0., 0.05, (5, 5)) + diag(ones(5))
        >>> filtered_data = sigma_clip(data, sigma=2.3, axis=0)

    Note that along the other axis, no points would be masked, as the
    variance is higher.
    R   R   R	   R
   R   R   R   R*   (   R   (
   R4   R   R   R	   R
   R   R   R   R*   t   sigclip(    (    s;   lib/python2.7/site-packages/astropy/stats/sigma_clipping.pyt
   sigma_clipΫ   s    g      @c         C` s  | d	 k	 r$ t j j |  |  }  n  | d	 k	 rH t j j |  |  }  n  t |  d | d | d | d | d | d | d |
 } t j j | d |
 } t j j | d |
 } t j j | d |	 d |
 } |
 d	 k rω t j j	 |  rω | j
   } n  | | | f S(
   uζ  
    Calculate sigma-clipped statistics on the provided data.

    Parameters
    ----------
    data : array-like
        Data array or object that can be converted to an array.

    mask : `numpy.ndarray` (bool), optional
        A boolean mask with the same shape as ``data``, where a `True`
        value indicates the corresponding element of ``data`` is masked.
        Masked pixels are excluded when computing the statistics.

    mask_value : float, optional
        A data value (e.g., ``0.0``) that is ignored when computing the
        statistics.  ``mask_value`` will be masked in addition to any
        input ``mask``.

    sigma : float, optional
        The number of standard deviations to use as the lower and upper
        clipping limit.  These limits are overridden by ``sigma_lower``
        and ``sigma_upper``, if input. Defaults to 3.

    sigma_lower : float, optional
        The number of standard deviations to use as the lower bound for
        the clipping limit.  If `None` then the value of ``sigma`` is used.
        Defaults to `None`.

    sigma_upper : float, optional
        The number of standard deviations to use as the upper bound for
        the clipping limit.  If `None` then the value of ``sigma`` is used.
        Defaults to `None`.

    iters : int, optional
        The number of iterations to perform sigma clipping, or `None` to
        clip until convergence is achieved (i.e., continue until the
        last iteration clips nothing) when calculating the statistics.
        Defaults to 5.

    cenfunc : callable, optional
        The function used to compute the center for the clipping. Must
        be a callable that takes in a masked array and outputs the
        central value. Defaults to the median (`numpy.ma.median`).

    stdfunc : callable, optional
        The function used to compute the standard deviation about the
        center. Must be a callable that takes in a masked array and
        outputs a width estimator. Masked (rejected) pixels are those
        where::

             deviation < (-sigma_lower * stdfunc(deviation))
             deviation > (sigma_upper * stdfunc(deviation))

        where::

            deviation = data - cenfunc(data [,axis=int])

        Defaults to the standard deviation (`numpy.std`).

    std_ddof : int, optional
        The delta degrees of freedom for the standard deviation
        calculation.  The divisor used in the calculation is ``N -
        std_ddof``, where ``N`` represents the number of elements.  The
        default is zero.

    axis : int or `None`, optional
        If not `None`, clip along the given axis.  For this case,
        ``axis`` will be passed on to ``cenfunc`` and ``stdfunc``, which
        are expected to return an array with the axis dimension removed
        (like the numpy functions).  If `None`, clip over all axes.
        Defaults to `None`.

    Returns
    -------
    mean, median, stddev : float
        The mean, median, and standard deviation of the sigma-clipped
        data.
    R   R   R	   R
   R   R   R   t   ddofN(   R   R   R    R"   t   masked_valuesR=   t   meanR;   R'   t   isMaskedArrayt   item(   R4   R   t
   mask_valueR   R   R	   R
   R   R   t   std_ddofR   t	   data_clipR@   R;   R'   (    (    s;   lib/python2.7/site-packages/astropy/stats/sigma_clipping.pyt   sigma_clipped_statsj  s    S(   t
   __future__R    R   R   R   t   numpyR   R.   t   utils.exceptionsR   t   extern.six.movesR   t   __all__t   objectR   R   R    R;   R'   R$   R=   RF   (    (    (    s;   lib/python2.7/site-packages/astropy/stats/sigma_clipping.pyt   <module>   s   "Ν		