ó
 ‰\c           @   s]   d  d l  m Z d  d l m Z d  d l Z d d „ Z d d „ Z d d e	 i  d „ Z
 d S(   iÿÿÿÿ(   t   division(   t   productNi   c         C   s  t  | ƒ } t j | ƒ r7 | f |  | d | } nP | rf t | ƒ |  d k rf t | ƒ d } n! t | ƒ |  k r‡ t d ƒ ‚ n  t j | ƒ r² | f |  | d	 | } nP | rá t | ƒ |  d k rá t | ƒ d
 } n! t | ƒ |  k rt d ƒ ‚ n  t j t j | ƒ d k  ƒ r/t d ƒ ‚ n  | rT| d d k rTt d ƒ ‚ n  t g  t	 | | ƒ D]" \ } } t
 d | d | ƒ ^ qgŒ  S(   s  Returns all combinations of shifts in n dimensions over the specified
    max_shifts and step sizes.

    Examples
    --------
    >>> s = list(_generate_shifts(2, False, max_shifts=(1, 2), shift_steps=1))
    >>> print(s)
    [(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2)]
    i    i   s"   max_shifts should have length ndims   shift_steps must all be >= 1iÿÿÿÿsG   Multichannel cycle spinning should not have shifts along the last axis.(   i    (   i    (   i   (   i   (   t   intt   npt   isscalart   lent   tuplet
   ValueErrort   anyt   asarrayR   t   zipt   range(   t   ndimt   multichannelt
   max_shiftst   shift_stepst   mct   st   t(    (    s>   lib/python2.7/site-packages/skimage/restoration/_cycle_spin.pyt   _generate_shifts   s(    
c         C   s]   | d k r$ t j t | ƒ ƒ } n  x2 t | | ƒ D]! \ } } t j |  | | ƒ }  q4 W|  S(   s|  Apply np.roll along a set of axes.

    Parameters
    ----------
    x : array-like
        Array to roll.
    rolls : int or sequence
        The amount to roll along each axis in ``axes``.
    axes : int or sequence, optional
        The axes to roll. Default is the first ``len(rolls)`` axes of ``x``.

    Returns
    -------
    x : array
        Data with axes rolled.
    N(   t   NoneR   t   arangeR   R
   t   roll(   t   xt   rollst   axest   rt   a(    (    s>   lib/python2.7/site-packages/skimage/restoration/_cycle_spin.pyt
   _roll_axes-   s
    c            sB  t  j ˆ ƒ ‰ t ˆ j | | | ƒ } t | ƒ } ‡  ‡ ‡ f d †  } | d k rš y d d l }	 Wqš t k
 r– d d l }
 |
 j d ƒ d } qš Xn  | d k rî | | d ƒ } x" | d D] } | | | ƒ 7} qÁ W| t	 | ƒ } nP g  | D] } |	 j
 | ƒ | ƒ ^ qõ } t | ƒ t	 | ƒ } | j d | ƒ } | S(   s
  Cycle spinning (repeatedly apply func to shifted versions of x).

    Parameters
    ----------
    x : array-like
        Data for input to ``func``.
    func : function
        A function to apply to circularly shifted versions of ``x``.  Should
        take ``x`` as its first argument. Any additional arguments can be
        supplied via ``func_kw``.
    max_shifts : int or tuple
        If an integer, shifts in ``range(0, max_shifts+1)`` will be used along
        each axis of ``x``. If a tuple, ``range(0, max_shifts[i]+1)`` will be
        along axis i.
    shift_steps : int or tuple, optional
        The step size for the shifts applied along axis, i, are::
        ``range((0, max_shifts[i]+1, shift_steps[i]))``. If an integer is
        provided, the same step size is used for all axes.
    num_workers : int or None, optional
        The number of parallel threads to use during cycle spinning. If set to
        ``None``, the full set of available cores are used.
    multichannel : bool, optional
        Whether to treat the final axis as channels (no cycle shifts are
        performed over the channels axis).
    func_kw : dict, optional
        Additional keyword arguments to supply to ``func``.

    Returns
    -------
    avg_y : np.ndarray
        The output of ``func(x, **func_kw)`` averaged over all combinations of
        the specified axis shifts.

    Notes
    -----
    Cycle spinning was proposed as a way to approach shift-invariance via
    performing several circular shifts of a shift-variant transform [1]_.

    For a n-level discrete wavelet transforms, one may wish to perform all
    shifts up to ``max_shifts = 2**n - 1``. In practice, much of the benefit
    can often be realized with only a small number of shifts per axis.

    For transforms such as the blockwise discrete cosine transform, one may
    wish to evaluate shifts up to the block size used by the transform.

    References
    ----------
    .. [1] R.R. Coifman and D.L. Donoho.  "Translation-Invariant De-Noising".
           Wavelets and Statistics, Lecture Notes in Statistics, vol.103.
           Springer, New York, 1995, pp.125-150.
           DOI:10.1007/978-1-4612-2544-7_9

    Examples
    --------
    >>> import skimage.data
    >>> from skimage import img_as_float
    >>> from skimage.restoration import denoise_wavelet, cycle_spin
    >>> img = img_as_float(skimage.data.camera())
    >>> sigma = 0.1
    >>> img = img + sigma * np.random.standard_normal(img.shape)
    >>> denoised = cycle_spin(img, func=denoise_wavelet, max_shifts=3)

    c            s5   t  ˆ |  ƒ } ˆ  | ˆ  } t  | t j |  ƒ ƒ S(   N(   R   R   R	   (   t   shiftt   xst   tmp(   t   funct   func_kwR   (    s>   lib/python2.7/site-packages/skimage/restoration/_cycle_spin.pyt   _run_one_shift   s    i   iÿÿÿÿNsl   dask is required for parallel computation, but it is not available.  Computation will be performed serially.i    t   num_workers(   R   t
   asanyarrayR   R   t   listt   daskt   ImportErrort   warningst   warnR   t   delayedt   sumt   compute(   R   R    R   R   R#   R   R!   t
   all_shiftsR"   R&   R(   t   meanR   R   t   futures(    (   R    R!   R   s>   lib/python2.7/site-packages/skimage/restoration/_cycle_spin.pyt
   cycle_spinG   s,    A	((   t
   __future__R    t	   itertoolsR   t   numpyR   R   R   R   t   FalseR0   (    (    (    s>   lib/python2.7/site-packages/skimage/restoration/_cycle_spin.pyt   <module>   s   %