sliding_window_view(x, window_shape, axis=None)
This docstring was copied from dask.array.numpy_compat.sliding_window_view.
Some inconsistencies with the Dask version may exist.
Also known as rolling or moving window, the window slides across all dimensions of the array and extracts subsets of the array at all window positions.
For many applications using a sliding window view can be convenient, but potentially very slow. Often specialized solutions exist, for example:
filtering functions in scipy.ndimage
moving window functions provided by bottleneck.
As a rough estimate, a sliding window approach with an input size of :None:None:`N`
and a window size of :None:None:`W`
will scale as :None:None:`O(N*W)`
where frequently a special algorithm can achieve :None:None:`O(N)`
. That means that the sliding window variant for a window size of 100 can be a 100 times slower than a more specialized version.
Nevertheless, for small window sizes, when no custom algorithm exists, or as a prototyping and developing tool, this function can be a good solution.
Array to create the sliding window view from.
Size of window over each axis that takes part in the sliding window. If :None:None:`axis`
is not present, must have same length as the number of input array dimensions. Single integers i
are treated as if they were the tuple :None:None:`(i,)`
.
Axis or axes along which the sliding window is applied. By default, the sliding window is applied to all axes and :None:None:`window_shape[i]`
will refer to axis i
of x
. If :None:None:`axis`
is given as a :None:None:`tuple of int`
, :None:None:`window_shape[i]`
will refer to the axis :None:None:`axis[i]`
of x
. Single integers i
are treated as if they were the tuple :None:None:`(i,)`
.
If True, sub-classes will be passed-through, otherwise the returned array will be forced to be a base-class array (default).
When true, allow writing to the returned view. The default is false, as this should be used with caution: the returned view contains the same memory location multiple times, so writing to one location will cause others to change.
Sliding window view of the array. The sliding window dimensions are inserted at the end, and the original dimensions are trimmed as required by the size of the sliding window. That is, view.shape = x_shape_trimmed + window_shape
, where x_shape_trimmed
is x.shape
with every entry reduced by one less than the corresponding window size.
Create a sliding window view into the array with the given window shape.
broadcast_to
broadcast an array to a given shape.
lib.stride_tricks.as_strided
A lower-level and less safe routine for creating arbitrary views from custom shape and strides.
>>> x = np.arange(6) # doctest: +SKIPThis example is valid syntax, but we were not able to check execution
... x.shape # doctest: +SKIP (6,)
>>> v = sliding_window_view(x, 3) # doctest: +SKIPThis example is valid syntax, but we were not able to check execution
... v.shape # doctest: +SKIP (4, 3)
>>> v # doctest: +SKIP array([[0, 1, 2], [1, 2, 3], [2, 3, 4], [3, 4, 5]])
This also works in more dimensions, e.g.
This example is valid syntax, but we were not able to check execution>>> i, j = np.ogrid[:3, :4] # doctest: +SKIPThis example is valid syntax, but we were not able to check execution
... x = 10*i + j # doctest: +SKIP
... x.shape # doctest: +SKIP (3, 4)
>>> x # doctest: +SKIP array([[ 0, 1, 2, 3], [10, 11, 12, 13], [20, 21, 22, 23]])This example is valid syntax, but we were not able to check execution
>>> shape = (2,2) # doctest: +SKIPThis example is valid syntax, but we were not able to check execution
... v = sliding_window_view(x, shape) # doctest: +SKIP
... v.shape # doctest: +SKIP (2, 3, 2, 2)
>>> v # doctest: +SKIP array([[[[ 0, 1], [10, 11]], [[ 1, 2], [11, 12]], [[ 2, 3], [12, 13]]], [[[10, 11], [20, 21]], [[11, 12], [21, 22]], [[12, 13], [22, 23]]]])
The axis can be specified explicitly:
This example is valid syntax, but we were not able to check execution>>> v = sliding_window_view(x, 3, 0) # doctest: +SKIPThis example is valid syntax, but we were not able to check execution
... v.shape # doctest: +SKIP (1, 4, 3)
>>> v # doctest: +SKIP array([[[ 0, 10, 20], [ 1, 11, 21], [ 2, 12, 22], [ 3, 13, 23]]])
The same axis can be used several times. In that case, every use reduces the corresponding original dimension:
This example is valid syntax, but we were not able to check execution>>> v = sliding_window_view(x, (2, 3), (1, 1)) # doctest: +SKIPThis example is valid syntax, but we were not able to check execution
... v.shape # doctest: +SKIP (3, 1, 2, 3)
>>> v # doctest: +SKIP array([[[[ 0, 1, 2], [ 1, 2, 3]]], [[[10, 11, 12], [11, 12, 13]]], [[[20, 21, 22], [21, 22, 23]]]])
Combining with stepped slicing (:None:None:`::step`
), this can be used to take sliding views which skip elements:
>>> x = np.arange(7) # doctest: +SKIP
... sliding_window_view(x, 5)[:, ::2] # doctest: +SKIP array([[0, 2, 4], [1, 3, 5], [2, 4, 6]])
or views which move by multiple elements
This example is valid syntax, but we were not able to check execution>>> x = np.arange(7) # doctest: +SKIP
... sliding_window_view(x, 3)[::2, :] # doctest: +SKIP array([[0, 1, 2], [2, 3, 4], [4, 5, 6]])
A common application of sliding_window_view
is the calculation of running statistics. The simplest example is the moving average:
>>> x = np.arange(6) # doctest: +SKIPThis example is valid syntax, but we were not able to check execution
... x.shape # doctest: +SKIP (6,)
>>> v = sliding_window_view(x, 3) # doctest: +SKIPThis example is valid syntax, but we were not able to check execution
... v.shape # doctest: +SKIP (4, 3)
>>> v # doctest: +SKIP array([[0, 1, 2], [1, 2, 3], [2, 3, 4], [3, 4, 5]])This example is valid syntax, but we were not able to check execution
>>> moving_average = v.mean(axis=-1) # doctest: +SKIP
... moving_average # doctest: +SKIP array([1., 2., 3., 4.])
Note that a sliding window approach is often not optimal (see Notes).
See :The following pages refer to to this document either explicitly or contain code examples using this.
dask.array.overlap.sliding_window_view
Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.
Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)
SVG is more flexible but power hungry; and does not scale well to 50 + nodes.
All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them