ó
 \c           @   s2   d  d l  Z d d l m Z e j d d  Z d S(   i˙˙˙˙Ni   (   t   view_as_blocksi    c         C   s$  t  |  |  j k r$ t d   n  g  } x t t  |   D]y } | | d k  rb t d   n  |  j | | | d k r | | |  j | | | } n d } | j d | f  q= Wt j |  d | d d d | }  t |  |  } x3 t t  | j  d	  D] } | | d
 d } qW| S(   sM  Down-sample image by applying function to local blocks.

    Parameters
    ----------
    image : ndarray
        N-dimensional input image.
    block_size : array_like
        Array containing down-sampling integer factor along each axis.
    func : callable
        Function object which is used to calculate the return value for each
        local block. This function must implement an ``axis`` parameter such
        as ``numpy.sum`` or ``numpy.min``.
    cval : float
        Constant padding value if image is not perfectly divisible by the
        block size.

    Returns
    -------
    image : ndarray
        Down-sampled image with same number of dimensions as input image.

    Examples
    --------
    >>> from skimage.measure import block_reduce
    >>> image = np.arange(3*3*4).reshape(3, 3, 4)
    >>> image # 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]]])
    >>> block_reduce(image, block_size=(3, 3, 1), func=np.mean)
    array([[[ 16.,  17.,  18.,  19.]]])
    >>> image_max1 = block_reduce(image, block_size=(1, 3, 4), func=np.max)
    >>> image_max1 # doctest: +NORMALIZE_WHITESPACE
    array([[[11]],
           [[23]],
           [[35]]])
    >>> image_max2 = block_reduce(image, block_size=(3, 1, 4), func=np.max)
    >>> image_max2 # doctest: +NORMALIZE_WHITESPACE
    array([[[27],
            [31],
            [35]]])
    s8   `block_size` must have the same length as `image.shape`.i   sY   Down-sampling factors must be >= 1. Use `skimage.transform.resize` to up-sample an image.i    t	   pad_widtht   modet   constantt   constant_valuesi   t   axisi˙˙˙˙(	   t   lent   ndimt
   ValueErrort   ranget   shapet   appendt   npt   padR    (   t   imaget
   block_sizet   funct   cvalR   t   it   after_widtht   out(    (    s4   lib/python2.7/site-packages/skimage/measure/block.pyt   block_reduce   s     2 	 (   t   numpyR   t   utilR    t   sumR   (    (    (    s4   lib/python2.7/site-packages/skimage/measure/block.pyt   <module>   s   