On 13 Oct 2020, at 2:25 am, Sebastian Berg <sebastian@sipsolutions.net> wrote:On Mon, 2020-10-12 at 08:39 +0000, Zimmermann Klaus wrote:Hello,I would like to draw the attention of this list to PR #17394 [1] thatadds the implementation of a sliding window view to numpy.Hi,thanks for working on this and driving going forward. I like the choice of a minimal API. I have pasted the doc-string (html, hope that works fine) below to allow a quicker idea of what is being proposed. To me it looks good! (I wonder if we need `subok`, but I guess we probably do.)Cheers,Sebastiannumpy.sliding_window_view
numpy.sliding_window_view(x, window_shape, axis=None, *, subok=False, writeable=False)Create a sliding window view into the array with the given window shape.
Creates a sliding window view of the N dimensional array with the given window shape. Window slides across each dimension of the array and extract a subsets of the array at any window position.
- Parameters
- x : array_like
Array to create the sliding window view from.- window_shape : int or tuple of int
Size of window over each axis that takes part in the sliding window. If 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 (i,).- axis : int or tuple of int, optional
Axis or axes along which the sliding window is applied. By default, the sliding window is applied to all axes and window_shape[i] will refer to axis i of x. If axis is given as a tuple of int, window_shape[i] will refer to the axis axis[i] of x. Single integers i are treated as if they were the tuple (i,).- subok : bool, optional
If True, sub-classes will be passed-through, otherwise the returned array will be forced to be a base-class array (default).- writeable : bool, optional
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.- Returns
- view : ndarray
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, wherex_shape_trimmedisx.shapewith every entry reduced by one less than the corresponding window size.Notes
For some cases there may be more efficient approaches to calculate transformations across multi-dimensional arrays, for instance
scipy.signal.fftconvolve, where combining the iterating step with the calculation itself while storing partial results can result in significant speedups.Examples
This also works in more dimensions, e.g.
The axis can be specified explicitly:
The same axis can be used several times. In that case, every use reduces the corresponding original dimension:
Combining with stepped slicing (::step), this can be used to take sliding views which skip elements:
or views which move by multiple elements
Having a sliding window view in numpy is a longstanding open issue (cf#7753 [2] from 2016). A brief summary of the discussions surrounding itcan be found in the description of the PR.This PR implements a sliding window view based on stride tricks.Following the discussion in issue #7753, a first implementation wasprovided by Fanjin Zeng in PR #10771. After some discussion, that PRstalled and I picked up the issue in the present PR #17394. It is basedon the first implementation, but follows the changed API as suggested byEric Wieser.Code reviews have been provided by Bas van Beek, Stephen Hoyer, and EricWieser. Sebastian Berg added the "62 - Python API" label.Do you think this is suitable for inclusion in numpy?Do you consider the PR ready?Do you have suggestions or requests?Thanks for your time and consideration!Klaus_______________________________________________NumPy-Discussion mailing list_______________________________________________
NumPy-Discussion mailing list
NumPy-Discussion@python.org
https://mail.python.org/mailman/listinfo/numpy-discussion