Skip to content

Instantly share code, notes, and snippets.

@nils-werner
Created June 11, 2016 18:45
Show Gist options
  • Save nils-werner/9d321441006b112a4b116a8387c2280c to your computer and use it in GitHub Desktop.
Save nils-werner/9d321441006b112a4b116a8387c2280c to your computer and use it in GitHub Desktop.
def sliding_window(data, size, stepsize=1, padded=False, axis=-1, copy=True):
"""
Calculate a sliding window over a signal
Parameters
----------
data : numpy array
The array to be slided over.
size : int
The sliding window size
stepsize : int
The sliding window stepsize. Defaults to 1.
axis : int
The axis to slide over. Defaults to the last axis.
copy : bool
Return strided array as copy to avoid sideffects when manipulating the
output array.
Returns
-------
data : numpy array
A matrix where row in last dimension consists of one instance
of the sliding window.
Notes
-----
- Be wary of setting `copy` to `False` as undesired sideffects with the
output values may occurr.
Examples
--------
>>> a = numpy.array([1, 2, 3, 4, 5])
>>> sliding_window(a, size=3)
array([[1, 2, 3],
[2, 3, 4],
[3, 4, 5]])
>>> sliding_window(a, size=3, stepsize=2)
array([[1, 2, 3],
[3, 4, 5]])
See Also
--------
pieces : Calculate number of pieces available by sliding
"""
if axis >= data.ndim:
raise ValueError(
"Axis value out of range"
)
if stepsize < 1:
raise ValueError(
"Stepsize may not be zero or negative"
)
if size > data.shape[axis]:
raise ValueError(
"Sliding window size may not exceed size of selected axis"
)
shape = list(data.shape)
shape[axis] = numpy.floor(data.shape[axis] / stepsize - size / stepsize + 1).astype(int)
shape.append(size)
strides = list(data.strides)
strides[axis] *= stepsize
strides.append(data.strides[axis])
strided = numpy.lib.stride_tricks.as_strided(
data, shape=shape, strides=strides
)
if copy:
return strided.copy()
else:
return strided
@nils-werner
Copy link
Author

Nowadays I recommend using skimage.util.view_as_windows() instead:

skimage.util.view_as_windows(x, (3, 3), step=2)
# array([[[[ 0,  1,  2],
#          [10, 11, 12],
#          [20, 21, 22]],
# 
#         [[ 2,  3,  4],
#          [12, 13, 14],
#          [22, 23, 24]],
# 
#         [[ 4,  5,  6],
#          [14, 15, 16],
#          [24, 25, 26]],
# 
#         [[ 6,  7,  8],
#          [16, 17, 18],
#          [26, 27, 28]]]])

@dojeda
Copy link

dojeda commented Nov 25, 2019

Nice, I didn't known sklearn had this util function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment