gaitmap.utils.array_handling.sliding_window_view#

gaitmap.utils.array_handling.sliding_window_view(arr: ndarray, window_length: int, overlap: int, nan_padding: bool = False) ndarray[source]#

Create a sliding window view of an input array with given window length and overlap.

Warning

This function will return by default a view onto your input array, modifying values in your result will directly affect your input data which might lead to unexpected behaviour! If padding is disabled (default) last window fraction of input may not be returned! However, if nan_padding is enabled, this will always return a copy instead of a view of your input data, independent if padding was actually performed or not!

Parameters:
arrarray with shape (n,) or (n, m)

array on which sliding window action should be performed. Windowing will always be performed along axis 0.

window_lengthint

length of desired window (must be smaller than array length n)

overlapint

length of desired overlap (must be smaller than window_length)

nan_padding: bool

select if last window should be nan-padded or discarded if it not fits with input array length. If nan-padding is enabled the return array will always be a copy of the input array independent if padding was actually performed or not!

Returns:
windowed view (or copy for nan_padding) of input array as specified, last window might be nan padded if necessary to
match window size

Examples

>>> data = np.arange(0, 10)
>>> windowed_view = sliding_window_view(arr=data, window_length=5, overlap=3, nan_padding=True)
>>> windowed_view
array([[ 0.,  1.,  2.,  3.,  4.],
       [ 2.,  3.,  4.,  5.,  6.],
       [ 4.,  5.,  6.,  7.,  8.],
       [ 6.,  7.,  8.,  9., nan]])