ó
 \c           @   sm   d  d l  m Z d  d l Z d  d l Z d  d l m Z d  d l m Z d d g Z	 d   Z
 d d	  Z d S(
   i˙˙˙˙(   t   divisionN(   t
   as_strided(   t   warnt   view_as_blockst   view_as_windowsc         C   s$  t  | t  s t d   n  t j |  } | d k j   rN t d   n  | j |  j k ro t d   n  t j |  j	  } | | j
   d k rĤ t d   n  |  j j sĊ t t d   n  t j |   }  t | |  t |  } t |  j |  |  j } t |  d | d | } | S(	   sl  Block view of the input n-dimensional array (using re-striding).

    Blocks are non-overlapping views of the input array.

    Parameters
    ----------
    arr_in : ndarray
        N-d input array.
    block_shape : tuple
        The shape of the block. Each dimension must divide evenly into the
        corresponding dimensions of `arr_in`.

    Returns
    -------
    arr_out : ndarray
        Block view of the input array.  If `arr_in` is non-contiguous, a copy
        is made.

    Examples
    --------
    >>> import numpy as np
    >>> from skimage.util.shape import view_as_blocks
    >>> A = np.arange(4*4).reshape(4,4)
    >>> A
    array([[ 0,  1,  2,  3],
           [ 4,  5,  6,  7],
           [ 8,  9, 10, 11],
           [12, 13, 14, 15]])
    >>> B = view_as_blocks(A, block_shape=(2, 2))
    >>> B[0, 0]
    array([[0, 1],
           [4, 5]])
    >>> B[0, 1]
    array([[2, 3],
           [6, 7]])
    >>> B[1, 0, 1, 1]
    13

    >>> A = np.arange(4*4*6).reshape(4,4,6)
    >>> A  # doctest: +NORMALIZE_WHITESPACE
    array([[[ 0,  1,  2,  3,  4,  5],
            [ 6,  7,  8,  9, 10, 11],
            [12, 13, 14, 15, 16, 17],
            [18, 19, 20, 21, 22, 23]],
           [[24, 25, 26, 27, 28, 29],
            [30, 31, 32, 33, 34, 35],
            [36, 37, 38, 39, 40, 41],
            [42, 43, 44, 45, 46, 47]],
           [[48, 49, 50, 51, 52, 53],
            [54, 55, 56, 57, 58, 59],
            [60, 61, 62, 63, 64, 65],
            [66, 67, 68, 69, 70, 71]],
           [[72, 73, 74, 75, 76, 77],
            [78, 79, 80, 81, 82, 83],
            [84, 85, 86, 87, 88, 89],
            [90, 91, 92, 93, 94, 95]]])
    >>> B = view_as_blocks(A, block_shape=(1, 2, 2))
    >>> B.shape
    (4, 2, 3, 1, 2, 2)
    >>> B[2:, 0, 2]  # doctest: +NORMALIZE_WHITESPACE
    array([[[[52, 53],
             [58, 59]]],
           [[[76, 77],
             [82, 83]]]])
    s   block needs to be a tuplei    s0   'block_shape' elements must be strictly positives9   'block_shape' must have the same length as 'arr_in.shape's-   'block_shape' is not compatible with 'arr_in'sE   Cannot provide views on a non-contiguous input array without copying.t   shapet   strides(   t
   isinstancet   tuplet	   TypeErrort   npt   arrayt   anyt
   ValueErrort   sizet   ndimR   t   sumt   flagst
   contiguousR   t   RuntimeWarningt   ascontiguousarrayR   R   (   t   arr_int   block_shapet	   arr_shapet	   new_shapet   new_stridest   arr_out(    (    s1   lib/python2.7/site-packages/skimage/util/shape.pyR   
   s"    Bi   c         C   s&  t  |  t j  s! t d   n  |  j } t  | t j  rL | f | } n  t |  | k sm t d   n  t  | t j  rŞ | d k  r t d   n  | f | } n  t |  | k rË t d   n  t j	 |  j
  } t j	 | d | j } | | d k  j   rt d   n  | d d k  j   r?t d	   n  |  j j s^t t d
   n  t j |   }  t d   | D  } t j	 |  j  } |  | j } t j	 |  j
  t j	 |  t j	 |  d } t t |  t |   }	 t t |  t |   }
 t |  d |	 d |
 } | S(   s¨  Rolling window view of the input n-dimensional array.

    Windows are overlapping views of the input array, with adjacent windows
    shifted by a single row or column (or an index of a higher dimension).

    Parameters
    ----------
    arr_in : ndarray
        N-d input array.
    window_shape : integer or tuple of length arr_in.ndim
        Defines the shape of the elementary n-dimensional orthotope
        (better know as hyperrectangle [1]_) of the rolling window view.
        If an integer is given, the shape will be a hypercube of
        sidelength given by its value.
    step : integer or tuple of length arr_in.ndim
        Indicates step size at which extraction shall be performed.
        If integer is given, then the step is uniform in all dimensions.

    Returns
    -------
    arr_out : ndarray
        (rolling) window view of the input array.   If `arr_in` is
        non-contiguous, a copy is made.

    Notes
    -----
    One should be very careful with rolling views when it comes to
    memory usage.  Indeed, although a 'view' has the same memory
    footprint as its base array, the actual array that emerges when this
    'view' is used in a computation is generally a (much) larger array
    than the original, especially for 2-dimensional arrays and above.

    For example, let us consider a 3 dimensional array of size (100,
    100, 100) of ``float64``. This array takes about 8*100**3 Bytes for
    storage which is just 8 MB. If one decides to build a rolling view
    on this array with a window of (3, 3, 3) the hypothetical size of
    the rolling view (if one was to reshape the view for example) would
    be 8*(100-3+1)**3*3**3 which is about 203 MB! The scaling becomes
    even worse as the dimension of the input array becomes larger.

    References
    ----------
    .. [1] http://en.wikipedia.org/wiki/Hyperrectangle

    Examples
    --------
    >>> import numpy as np
    >>> from skimage.util.shape import view_as_windows
    >>> A = np.arange(4*4).reshape(4,4)
    >>> A
    array([[ 0,  1,  2,  3],
           [ 4,  5,  6,  7],
           [ 8,  9, 10, 11],
           [12, 13, 14, 15]])
    >>> window_shape = (2, 2)
    >>> B = view_as_windows(A, window_shape)
    >>> B[0, 0]
    array([[0, 1],
           [4, 5]])
    >>> B[0, 1]
    array([[1, 2],
           [5, 6]])

    >>> A = np.arange(10)
    >>> A
    array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
    >>> window_shape = (3,)
    >>> B = view_as_windows(A, window_shape)
    >>> B.shape
    (8, 3)
    >>> B
    array([[0, 1, 2],
           [1, 2, 3],
           [2, 3, 4],
           [3, 4, 5],
           [4, 5, 6],
           [5, 6, 7],
           [6, 7, 8],
           [7, 8, 9]])

    >>> A = np.arange(5*4).reshape(5, 4)
    >>> A
    array([[ 0,  1,  2,  3],
           [ 4,  5,  6,  7],
           [ 8,  9, 10, 11],
           [12, 13, 14, 15],
           [16, 17, 18, 19]])
    >>> window_shape = (4, 3)
    >>> B = view_as_windows(A, window_shape)
    >>> B.shape
    (2, 2, 4, 3)
    >>> B  # doctest: +NORMALIZE_WHITESPACE
    array([[[[ 0,  1,  2],
             [ 4,  5,  6],
             [ 8,  9, 10],
             [12, 13, 14]],
            [[ 1,  2,  3],
             [ 5,  6,  7],
             [ 9, 10, 11],
             [13, 14, 15]]],
           [[[ 4,  5,  6],
             [ 8,  9, 10],
             [12, 13, 14],
             [16, 17, 18]],
            [[ 5,  6,  7],
             [ 9, 10, 11],
             [13, 14, 15],
             [17, 18, 19]]]])
    s    `arr_in` must be a numpy ndarrays2   `window_shape` is incompatible with `arr_in.shape`i   s   `step` must be >= 1s*   `step` is incompatible with `arr_in.shape`t   dtypei    s   `window_shape` is too larges   `window_shape` is too smallsE   Cannot provide views on a non-contiguous input array without copying.c         s   s!   |  ] } t  d  d  |  Vq d  S(   N(   t   slicet   None(   t   .0t   st(    (    s1   lib/python2.7/site-packages/skimage/util/shape.pys	   <genexpr>ü   s    R   R   (   R   R
   t   ndarrayR	   R   t   numberst   Numbert   lenR   R   R   R   R   R   R   R   R   R   R   R   t   listR   (   R   t   window_shapet   stepR   R   t   slicest   window_stridest   indexing_stridest   win_indices_shapeR   R   R   (    (    s1   lib/python2.7/site-packages/skimage/util/shape.pyR   k   s>    p	(   t
   __future__R    R!   t   numpyR
   t   numpy.lib.stride_tricksR   t   warningsR   t   __all__R   R   (    (    (    s1   lib/python2.7/site-packages/skimage/util/shape.pyt   <module>   s   	a