binning

odl.util.numerics.binning(arr, bin_size, reduction=<function sum at 0x7f7aae6c8730>)[source]

Bin an array by a factor.

Parameters
arrarray-like

The array that should be binned.

bin_sizepositive int or sequence

Size or per-axis sizes of the bins.

reduction: callable, optional

Function used to perform the binning by reduction over temporary axes of size bin_size. It is called as

reduction(reshaped_arr, axis=reduction_axes)

hence it must support this signature. The function is expected to collapse reduction_axes.

Default: numpy.sum

Returns
binned_arrnumpy.ndarray

Array of shape n[i] // b[i] in axis i, where n refers to the original shape and b to the bin sizes.

Examples

Binning by the same factor in all axes can be done with an integer bin_size:

>>> arr = np.arange(24).reshape((4, 6))
>>> arr
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]])
>>> binning(arr, bin_size=2)
array([[14, 22, 30],
       [62, 70, 78]])

If a sequence is given, the bin sizes are specific for each axis:

>>> binning(arr, bin_size=(2, 3))
array([[ 24,  42],
       [ 96, 114]])

Instead of the default numpy.sum, other functions that accept an array as first argument and an axis keyword argument can be used for reduction. For instance, numpy.max, resulting in “max pooling”:

>>> binning(arr, bin_size=2, reduction=np.max)
array([[ 7,  9, 11],
       [19, 21, 23]])