3

How do I change the rolling window function size to be a 2x3 window as opposed to the 3x3 window it is at right now? I am not skilled enough and have no idea how to reverse engineer the function to understand how window_size works :(

import numpy as np

def rolling_window(array, window_size):
    itemsize = array.itemsize
    shape = (array.shape[0] - window_size + 1,
             array.shape[1] - window_size + 1,
             window_size, window_size)
    strides = (array.shape[1] * itemsize, itemsize,
               array.shape[1] * itemsize, itemsize)
    return np.lib.stride_tricks.as_strided(array, shape=shape, strides=strides)

roller = np.arange(1,16).reshape(3,5)
print(roller)
run = rolling_window(roller,3)
print(run)
Evan Kim
  • 769
  • 2
  • 8
  • 26
  • @Divakar provides a nice solution here https://stackoverflow.com/questions/47469947/as-strided-linking-stepsize-strides-of-conv2d-with-as-strided-strides-paramet#47470711 – NaN Dec 01 '18 at 02:00
  • ah, nifty solution! – Evan Kim Dec 01 '18 at 02:11
  • Easiest is with `skimage.util.shape.view_as_windows` : https://stackoverflow.com/questions/42831022/ – Divakar Dec 01 '18 at 05:44

1 Answers1

4

If you know you want a 2D window, then specify window_size[0] to go with array.shape[0] and window_size[1] with array.shape[1] when you define shape:

def rolling_window(array, window_size):
    itemsize = array.itemsize
    # broadcast window_size so it will still work with a scalar:
    window_size = np.broadcast_to(window_size,[2,])
    shape = (array.shape[0] - window_size[0] + 1,
             array.shape[1] - window_size[1] + 1,
             window_size[0], window_size[1])
    # the following would also work:
    # shape = (*np.array(array.shape) - window_size + 1, *window_size)
    strides = (array.shape[1] * itemsize, itemsize,
               array.shape[1] * itemsize, itemsize)
    return np.lib.stride_tricks.as_strided(array, shape=shape, strides=strides)

roller = np.arange(1,16).reshape(3,5)
>>> print(roller)
[[ 1  2  3  4  5]
 [ 6  7  8  9 10]
 [11 12 13 14 15]]

run = rolling_window(roller,[2,3])

>>> run
array([[[[ 1,  2,  3],
         [ 6,  7,  8]],

        [[ 2,  3,  4],
         [ 7,  8,  9]],

        [[ 3,  4,  5],
         [ 8,  9, 10]]],


       [[[ 6,  7,  8],
         [11, 12, 13]],

        [[ 7,  8,  9],
         [12, 13, 14]],

        [[ 8,  9, 10],
         [13, 14, 15]]]])
sacuL
  • 49,704
  • 8
  • 81
  • 106