block_reduce#
- astropy.nddata.block_reduce(data, block_size, func=<function sum>)[source]#
Downsample a data array by applying a function to local blocks.
If
data
is not perfectly divisible byblock_size
along a given axis then the data will be trimmed (from the end) along that axis.- Parameters:
- dataarray_like
The data to be resampled.
- block_size
int
or array_like (int
) The integer block size along each axis. If
block_size
is a scalar anddata
has more than one dimension, thenblock_size
will be used for for every axis.- func
callable()
, optional The method to use to downsample the data. Must be a callable that takes in a 4D
ndarray
(the 2Dndarray
input intoblock_reduce
gets reshaped as 4D) and has anaxis
keyword that accepts tuples. This function will be called withaxis=(2, 3)
and it should return a 2D array. The default issum
, which provides block summation (and conserves the data sum).
- Returns:
- outputarray_like
The resampled data. Note the depending on the input
func
, the dtype of the output array may not match the input array.
Examples
>>> import numpy as np >>> from astropy.nddata import block_reduce >>> data = np.arange(16).reshape(4, 4) >>> block_reduce(data, 2) array([[10, 18], [42, 50]])
>>> block_reduce(data, 2, func=np.mean) array([[ 2.5, 4.5], [ 10.5, 12.5]])