@support_nddata
def block_reduce(data, block_size, func=np.sum):
 from skimage.measure import block_reduce
 data = np.asanyarray(data)
 block_size = np.atleast_1d(block_size)
 if ((data.ndim > 1) and (len(block_size) == 1)):
  block_size = np.repeat(block_size, data.ndim)
 if (len(block_size) != data.ndim):
  raise ValueError(u'`block_size` must be a scalar or have the same length as `data.shape`')
 block_size = np.array([int(i) for i in block_size])
 size_resampled = (np.array(data.shape) // block_size)
 size_init = (size_resampled * block_size)
 for i in range(data.ndim):
  if (data.shape[i] != size_init[i]):
   data = data.swapaxes(0, i)
   data = data[:size_init[i]]
   data = data.swapaxes(0, i)
 return block_reduce(data, tuple(block_size), func=func)

@ support_nddata def block_reduce ( data , block_size , func = np . sum ) : from skimage . measure import block_reduce data = np . asanyarray ( data ) block_size = np . atleast_1d ( block_size ) if ( ( data . ndim > 1 ) and ( len ( block_size ) == 1 ) ) : block_size = np . repeat ( block_size , data . ndim ) if ( len ( block_size ) != data . ndim ) : raise ValueError ( u'`block_size` must be a scalar or have the same length as `data.shape`' ) block_size = np . array ( [ int ( i ) for i in block_size ] ) size_resampled = ( np . array ( data . shape ) // block_size ) size_init = ( size_resampled * block_size ) for i in range ( data . ndim ) : if ( data . shape [ i ] != size_init [ i ] ) : data = data . swapaxes ( 0 , i ) data = data [ : size_init [ i ] ] data = data . swapaxes ( 0 , i ) return block_reduce ( data , tuple ( block_size ) , func = func )
downsample a data array by applying a function to local blocks .