reshape_as_blocks#

astropy.nddata.reshape_as_blocks(data, block_size)[source]#

Reshape a data array into blocks.

This is useful to efficiently apply functions on block subsets of the data instead of using loops. The reshaped array is a view of the input data array.

New in version 4.1.

Parameters:
datandarray

The input data array.

block_sizeint or array_like (int)

The integer block size along each axis. If block_size is a scalar and data has more than one dimension, then block_size will be used for for every axis. Each dimension of block_size must divide evenly into the corresponding dimension of data.

Returns:
outputndarray

The reshaped array as a view of the input data array.

Examples

>>> import numpy as np
>>> from astropy.nddata import reshape_as_blocks
>>> data = np.arange(16).reshape(4, 4)
>>> data
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11],
       [12, 13, 14, 15]])
>>> reshape_as_blocks(data, (2, 2))
array([[[[ 0,  1],
         [ 4,  5]],
        [[ 2,  3],
         [ 6,  7]]],
       [[[ 8,  9],
         [12, 13]],
        [[10, 11],
         [14, 15]]]])