This is a development version (0.26.1rc0.dev0) and may be unstable.Go to latest (0.26.1rc0.dev0+git20260530.b607368ff)
{ } Raw JSON

bundles / skimage 0.26.1rc0.dev0 / skimage / util / shape / view_as_windows

function

skimage.util.shape:view_as_windows

source: /_skimage2/util/shape.py:96

Signature

def view_as_windows(arr_in, window_shape, step = 1)

Summary

Rolling window view of the input n-dimensional array.

Extended Summary

Windows are overlapping views of the input array, with adjacent windows shifted by a single row or column (or an index of a higher dimension).

Parameters

arr_in : ndarray, shape (M[, ...])

Input array.

window_shape : integer or tuple of length arr_in.ndim

Defines the shape of the elementary n-dimensional orthotope (better know as hyperrectangle [1]) of the rolling window view. If an integer is given, the shape will be a hypercube of sidelength given by its value.

step : integer or tuple of length arr_in.ndim

Indicates step size at which extraction shall be performed. If integer is given, then the step is uniform in all dimensions.

Returns

arr_out : ndarray

(rolling) window view of the input array.

Notes

One should be very careful with rolling views when it comes to memory usage. Indeed, although a 'view' has the same memory footprint as its base array, the actual array that emerges when this 'view' is used in a computation is generally a (much) larger array than the original, especially for 2-dimensional arrays and above.

For example, let us consider a 3 dimensional array of size (100, 100, 100) of float64. This array takes about 8*100**3 Bytes for storage which is just 8 MB. If one decides to build a rolling view on this array with a window of (3, 3, 3) the hypothetical size of the rolling view (if one was to reshape the view for example) would be 8*(100-3+1)**3*3**3 which is about 203 MB! The scaling becomes even worse as the dimension of the input array becomes larger.

Examples

import numpy as np
from skimage.util.shape import view_as_windows
A = np.arange(4*4).reshape(4,4)
A
window_shape = (2, 2)
B = view_as_windows(A, window_shape)
B[0, 0]
B[0, 1]
✓
array([[1, 2],
       [5, 6]])
A = np.arange(10)
A
window_shape = (3,)
B = view_as_windows(A, window_shape)
B.shape
B
✓
array([[0, 1, 2],
       [1, 2, 3],
       [2, 3, 4],
       [3, 4, 5],
       [4, 5, 6],
       [5, 6, 7],
       [6, 7, 8],
       [7, 8, 9]])
A = np.arange(5*4).reshape(5, 4)
A
window_shape = (4, 3)
B = view_as_windows(A, window_shape)
B.shape
B  # doctest: +NORMALIZE_WHITESPACE
✓
array([[[[ 0,  1,  2],
         [ 4,  5,  6],
         [ 8,  9, 10],
         [12, 13, 14]],

        [[ 1,  2,  3],
         [ 5,  6,  7],
         [ 9, 10, 11],
         [13, 14, 15]]],


       [[[ 4,  5,  6],
         [ 8,  9, 10],
         [12, 13, 14],
         [16, 17, 18]],

        [[ 5,  6,  7],
         [ 9, 10, 11],
         [13, 14, 15],
         [17, 18, 19]]]])

Aliases

  • skimage.util.view_as_windows