ó
Uķ\c           @` sÂ   d  Z  d d l m Z m Z m Z d d l Z d d g Z d d  Z d d  Z	 d d  Z
 d d	  Z d d
  Z i e d 6e
 d 6e d 6e d 6e	 d 6e	 d 6Z d d d  Z d   Z d S(   s[   
The thresholding helper module implements the most popular signal thresholding
functions.
i    (   t   divisiont   print_functiont   absolute_importNt	   thresholdt   threshold_firmc         C` sĪ   t  j |   }  t  j |   } t  j d d  9 d | | } | j d d d d  d |  |  | } Wd  QX| d k r{ | St  j | |  } t  j | | |  Sd  S(   Nt   dividet   ignorei   t   mini    t   maxt   out(   t   npt   asarrayt   absolutet   errstatet   clipt   Nonet   lesst   where(   t   datat   valuet
   substitutet	   magnitudet   thresholdedt   cond(    (    s1   lib/python2.7/site-packages/pywt/_thresholding.pyt   soft   s    c         C` sŽ   t  j |   }  t  j |   } t  j d d  A d | d | d } | j d d d d	 d |  |  | } Wd	 QX| d k r | St  j | |  } t  j | | |  Sd	 S(
   s   Non-negative Garrote.R   R   i   i   R   i    R   R	   N(   R
   R   R   R   R   R   R   R   (   R   R   R   R   R   R   (    (    s1   lib/python2.7/site-packages/pywt/_thresholding.pyt
   nn_garrote"   s    c         C` s=   t  j |   }  t  j t  j |   |  } t  j | | |   S(   N(   R
   R   R   R   R   (   R   R   R   R   (    (    s1   lib/python2.7/site-packages/pywt/_thresholding.pyt   hard4   s    c         C` sL   t  j |   }  t  j |   r- t d   n  t  j t  j |  |  | |   S(   Ns,   greater thresholding only supports real data(   R
   R   t   iscomplexobjt
   ValueErrorR   R   (   R   R   R   (    (    s1   lib/python2.7/site-packages/pywt/_thresholding.pyt   greater:   s    c         C` sL   t  j |   }  t  j |   r- t d   n  t  j t  j |  |  | |   S(   Ns)   less thresholding only supports real data(   R
   R   R   R   R   R   (   R   R   R   (    (    s1   lib/python2.7/site-packages/pywt/_thresholding.pyR   A   s    R   R   R   R   t   garrotet   garottec         C` sj   y t  | |  | |  SWnK t k
 re d   t t  j    D } t d j d j |     n Xd S(   sĸ  
    Thresholds the input data depending on the mode argument.

    In ``soft`` thresholding [1]_, data values with absolute value less than
    `param` are replaced with `substitute`. Data values with absolute value
    greater or equal to the thresholding value are shrunk toward zero
    by `value`.  In other words, the new value is
    ``data/np.abs(data) * np.maximum(np.abs(data) - value, 0)``.

    In ``hard`` thresholding, the data values where their absolute value is
    less than the value param are replaced with `substitute`. Data values with
    absolute value greater or equal to the thresholding value stay untouched.

    ``garrote`` corresponds to the Non-negative garrote threshold [2]_, [3]_.
    It is intermediate between ``hard`` and ``soft`` thresholding.  It behaves
    like soft thresholding for small data values and approaches hard
    thresholding for large data values.

    In ``greater`` thresholding, the data is replaced with `substitute` where
    data is below the thresholding value. Greater data values pass untouched.

    In ``less`` thresholding, the data is replaced with `substitute` where data
    is above the thresholding value. Lesser data values pass untouched.

    Both ``hard`` and ``soft`` thresholding also support complex-valued data.

    Parameters
    ----------
    data : array_like
        Numeric data.
    value : scalar
        Thresholding value.
    mode : {'soft', 'hard', 'garrote', 'greater', 'less'}
        Decides the type of thresholding to be applied on input data. Default
        is 'soft'.
    substitute : float, optional
        Substitute value (default: 0).

    Returns
    -------
    output : array
        Thresholded array.

    See Also
    --------
    threshold_firm

    References
    ----------
    .. [1] D.L. Donoho and I.M. Johnstone. Ideal Spatial Adaptation via
        Wavelet Shrinkage. Biometrika. Vol. 81, No. 3, pp.425-455, 1994.
        DOI:10.1093/biomet/81.3.425
    .. [2] L. Breiman. Better Subset Regression Using the Nonnegative Garrote.
        Technometrics, Vol. 37, pp. 373-384, 1995.
        DOI:10.2307/1269730
    .. [3] H-Y. Gao.  Wavelet Shrinkage Denoising Using the Non-Negative
        Garrote.  Journal of Computational and Graphical Statistics Vol. 7,
        No. 4, pp.469-488. 1998.
        DOI:10.1080/10618600.1998.10474789

    Examples
    --------
    >>> import numpy as np
    >>> import pywt
    >>> data = np.linspace(1, 4, 7)
    >>> data
    array([ 1. ,  1.5,  2. ,  2.5,  3. ,  3.5,  4. ])
    >>> pywt.threshold(data, 2, 'soft')
    array([ 0. ,  0. ,  0. ,  0.5,  1. ,  1.5,  2. ])
    >>> pywt.threshold(data, 2, 'hard')
    array([ 0. ,  0. ,  2. ,  2.5,  3. ,  3.5,  4. ])
    >>> pywt.threshold(data, 2, 'garrote')
    array([ 0.        ,  0.        ,  0.        ,  0.9       ,  1.66666667,
            2.35714286,  3.        ])
    >>> pywt.threshold(data, 2, 'greater')
    array([ 0. ,  0. ,  2. ,  2.5,  3. ,  3.5,  4. ])
    >>> pywt.threshold(data, 2, 'less')
    array([ 1. ,  1.5,  2. ,  0. ,  0. ,  0. ,  0. ])

    c         s` s   |  ] } d  j  |  Vq d S(   s   '{0}'N(   t   format(   t   .0t   key(    (    s1   lib/python2.7/site-packages/pywt/_thresholding.pys	   <genexpr>Ļ   s    s/   The mode parameter only takes values from: {0}.s   , N(   t   thresholding_optionst   KeyErrort   sortedt   keysR   R    t   join(   R   R   t   modeR   R&   (    (    s1   lib/python2.7/site-packages/pywt/_thresholding.pyR   R   s    R	c         C` sð   | d k  r t  d   n  | | k  r6 t  d   n  t j |   }  t j |   } t j d d  K | | } | d | | | } | j d d d d
 d	 |  |  | } Wd
 QXt j | | k  } t j | d  rė |  | | | <n  | S(   s_  Firm threshold.

    The approach is intermediate between soft and hard thresholding [1]_. It
    behaves the same as soft-thresholding for values below `value_low` and
    the same as hard-thresholding for values above `thresh_high`.  For
    intermediate values, the thresholded value is in between that corresponding
    to soft or hard thresholding.

    Parameters
    ----------
    data : array-like
        The data to threshold.  This can be either real or complex-valued.
    value_low : float
        Any values smaller then `value_low` will be set to zero.
    value_high : float
        Any values larger than `value_high` will not be modified.

    Notes
    -----
    This thresholding technique is also known as semi-soft thresholding [2]_.

    For each value, `x`, in `data`. This function computes::

        if np.abs(x) <= value_low:
            return 0
        elif np.abs(x) > value_high:
            return x
        elif value_low < np.abs(x) and np.abs(x) <= value_high:
            return x * value_high * (1 - value_low/x)/(value_high - value_low)

    ``firm`` is a continuous function (like soft thresholding), but is
    unbiased for large values (like hard thresholding).

    If ``value_high == value_low`` this function becomes hard-thresholding.
    If ``value_high`` is infinity, this function becomes soft-thresholding.

    Returns
    -------
    val_new : array-like
        The values after firm thresholding at the specified thresholds.

    See Also
    --------
    threshold

    References
    ----------
    .. [1] H.-Y. Gao and A.G. Bruce. Waveshrink with firm shrinkage.
        Statistica Sinica, Vol. 7, pp. 855-874, 1997.
    .. [2] A. Bruce and H-Y. Gao. WaveShrink: Shrinkage Functions and
        Thresholds. Proc. SPIE 2569, Wavelet Applications in Signal and
        Image Processing III, 1995.
        DOI:10.1117/12.217582
    i    s   value_low must be non-negative.s6   value_high must be greater than or equal to value_low.R   R   i   R   R   R	   N(	   R   R
   R   R   R   R   R   R   t   any(   R   t	   value_lowt
   value_highR   t   vdiffR   t
   large_vals(    (    s1   lib/python2.7/site-packages/pywt/_thresholding.pyR   Ū   s     8
(   t   __doc__t
   __future__R    R   R   t   numpyR
   t   __all__R   R   R   R   R   R#   R   R   (    (    (    s1   lib/python2.7/site-packages/pywt/_thresholding.pyt   <module>	   s    

\