Skip to content

Minicubes

import skimage
import numpy as np

Resources:

Example I - Time Series

A = np.arange(9)
A
array([0, 1, 2, 3, 4, 5, 6, 7, 8])
window_shape = (3,)

B = skimage.util.view_as_windows(A, window_shape)

B
array([[0, 1, 2],
       [1, 2, 3],
       [2, 3, 4],
       [3, 4, 5],
       [4, 5, 6],
       [5, 6, 7],
       [6, 7, 8]])

Example 2 - 2D Arrays

A = np.arange(4*4).reshape(4,4)
A
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11],
       [12, 13, 14, 15]])
window_shape = (3,3)

B = skimage.util.view_as_windows(A, window_shape)

B
array([[[[ 0,  1,  2],
         [ 4,  5,  6],
         [ 8,  9, 10]],

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


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

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

Example 3 - 3D Arrays

A = np.arange(4*4*4).reshape(4,4,4)
A
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],
        [24, 25, 26, 27],
        [28, 29, 30, 31]],

       [[32, 33, 34, 35],
        [36, 37, 38, 39],
        [40, 41, 42, 43],
        [44, 45, 46, 47]],

       [[48, 49, 50, 51],
        [52, 53, 54, 55],
        [56, 57, 58, 59],
        [60, 61, 62, 63]]])
window_shape = (3,3,1)

B = skimage.util.view_as_windows(A, window_shape)

B.shape
(2, 2, 4, 3, 3, 1)

Example 4 - 3D Arrays w. Coordinates

x = np.array([[1,2,3],[4,5,6],[7,8,9]]).reshape(-1,1)
x.shape
lat = np.linspace(-10, 10, 3)
lon = np.linspace(0, 10, 3)
time = ['2001', '2002', '2003']
data = np.random.randn(lon.shape[0], lat.shape[0], len(time))
data.shape
x_cube = skimage.util.view_as_windows(data, (2,2,1),step=(1,1,1))
x_cube