-
-
Notifications
You must be signed in to change notification settings - Fork 10.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ENH: Implement sliding window #17394
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for picking this back up.
Would you be able to add some tests for the error cases? In case you're not familiar with pytest, you can do that with
with pytest.raises(ValueError, match="cannot be larger"):
sliding_window_view(...)
The module-level |
6471d27
to
0771cdb
Compare
I assume this will need a release note? |
There are currently a few CI errors caused by the doctests, Replacing all references to
|
SEGFAULT in test_umath? That doesn't feel like it comes from this PR, right? |
Let's assume for know that this is a fluke; restarting the test will probably fix it. More importantly, there is still a doctest-related error:
|
While trying to improve the documentation for the return value, I decided to change the name of the |
Close/reopen |
7c5403a
to
e2cd3dd
Compare
Addressed the last comment on subok test, rebased on current master, made sure all checks pass. The previous travis ci problem was most likely due to the move to travis-ci.com, now everything seems ok. @seberg, good to go? |
Co-authored-by: Eric Wieser <wieser.eric@gmail.com>
@seberg, @shoyer, @eric-wieser, is there anything left for me to do here? How can we move forward? |
Thanks @zklaus, I am sure we can always find some nitpicks, but it seems all good to me. If there are any followups we can open a new PR. Thanks everyone who reviewed this! |
Thank you @zklaus! |
Cheers guys! Nice working with you 😃 |
This is an updated version of #10771. It implements the sliding window function discussed in #7753 according to the slightly changed API as suggested by @eric-wieser in #10771 (comment).
The case for a sliding window function
A sliding window function is a longstanding open demand. Issue #7753 has been open since 2016 and the discussion about this feature has come up several times since then. Additionally, several downstream libraries, such as dask, scikit-image, pandas, etc. need this functionality; some of them already have their own version, see eg
skimage.utils.view_as_windows()
andskimage.utils.view_as_blocks()
. However, due to the general nature of this functionality it is believed to have a place in numpy itself.The one counter-argument to the inclusion into numpy that has been made is that in many cases where this function could be used, more efficient algorithms are available (see e.g. #7753 (comment), #7753 (comment)). Nevertheless, the consensus reached in #7753 seems to be that, while often more efficient algorithms would be available, people rarely implement these problem-specific optimizations, but rather opt for a simpler approach, either based on explicit loops or on one of several sliding window implementations that are "in the wild" (see e.g. https://gist.github.com/teoliphant/96eb779a16bd038e374f2703da62f06d). In both of these cases having a well-documented, standardized function in numpy will enhance readability, reduce code duplication, and potentially improve performance.
In summary, a sliding window function is a worthwhile addition to numpy and will close a longstanding open issue.
The specific API
The API as proposed in this PR is the result of a discussion in the ancestral PR #10771. It is similar to the one used in skimage, but adds the axis keyword to allow flexible windows over any combination of axes, including repeated axes. Several use-cases are demonstrated here; a possible implementation of the skimage functions based on this API is given here.
Closes #7753.