Skip to content

Conversation

jakevdp
Copy link
Member

@jakevdp jakevdp commented May 8, 2024

Towards gh-20678.

This PR adds Array API standard support to window function definitions within scipy.signal.

This is a bit different than other Array API changes, becuase these functions construct arrays from scratch. So rather than pulling the array API namespace from the inputs, this adds a new optional array_namespace keyword argument to each function that lets the user specify an array API; this defaults to scipy._lib.array_api_compat.numpy, so there shouldn't be any user-visible change in the default case.

This should be considered a draft – I'm curious if maintainers have thoughts about whether this keyword argument is the best way to support the Array API for cases like this. Thanks!

@jakevdp jakevdp requested review from larsoner and ilayn as code owners May 8, 2024 17:31
@jakevdp jakevdp force-pushed the window-array-api branch from 85cd599 to 104b25b Compare May 8, 2024 17:33
@jakevdp jakevdp marked this pull request as draft May 8, 2024 17:34
@jakevdp jakevdp force-pushed the window-array-api branch 5 times, most recently from 1490abb to e2d1e1c Compare May 8, 2024 19:36
@jakevdp jakevdp requested a review from lucascolley May 8, 2024 22:09
@lucascolley lucascolley changed the title EHN: scipy.signal.window: add array API support EHN: signal.window: add array API support May 9, 2024
@lucascolley lucascolley added enhancement A new feature or improvement array types Items related to array API support and input array validation (see gh-18286) labels May 9, 2024
@jakevdp jakevdp force-pushed the window-array-api branch from c7a3d77 to 3011007 Compare May 9, 2024 13:06
@jakevdp jakevdp marked this pull request as ready for review May 9, 2024 13:52
Copy link
Member

@lucascolley lucascolley left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the diff here looks fine to me, thanks! Would you like to start converting some of the tests? See gh-19001, or any of the recently merged stats PRs labelled with array types for examples.

We should let @rgommers weigh in on whether to name the parameter array_namespace or xp, and whether we would like a device parameter as well.

@rgommers
Copy link
Member

Thanks for this @jakevdp!

whether to name the parameter array_namespace or xp, and whether we would like a device parameter as well.

We should do this consistently for all functions that create new arrays without receiving an array input. For fftfreq & co, we went with two keyword-only keywords: xp=None, device=None. Which still seems fine to me, so I suggest to go with that. xp is indeed very slightly non-descriptive, but I think if we use it consistently it will become quite recognizable to users soon enough.

@jakevdp
Copy link
Member Author

jakevdp commented May 10, 2024

Thanks @rgommers – I'll update this to use add the xp and device arguments today.

I can also work on refactoring the tests, but perhaps we should do that in a separate PR: it always worries me to do significant refactoring of tests and code at the same time, because it may mask changes in user-visible behavior!

@rgommers
Copy link
Member

I hadn't considered that argument for not considering the tests yet - you make a good point. So far I don't think we've accidentally introduced any regressions because of test changes. A separate PR seems like a potentially useful strategy, as long as it's available together with the code changes. Otherwise we have no way of testing the new functionality.

How about opening a second PR on top of this one including test changes? Then we can choose to either:

  • merge this PR, then rebase and merge the second PR, or
  • simply consider this one as thorough regression testing, and only merge the second PR when both are green (closing this PR)

@lucascolley
Copy link
Member

How about opening a second PR on top of this one including test changes? Then we can choose to either:

merge this PR, then rebase and merge the second PR, or
simply consider this one as thorough regression testing, and only merge the second PR when both are green (closing this PR)

That sounds like the right idea to me.

@jakevdp jakevdp force-pushed the window-array-api branch from ec28254 to 3d9f85f Compare May 10, 2024 21:34
@jakevdp
Copy link
Member Author

jakevdp commented May 10, 2024

I updated _windows.py with the xp and device arguments, and squashed the commit. I'll plan to work on test changes in a second commit on top of that, probably some time next week.

@jakevdp jakevdp force-pushed the window-array-api branch from 3867c23 to 2026892 Compare May 10, 2024 23:29
@jakevdp
Copy link
Member Author

jakevdp commented May 10, 2024

I tried converting the test but it's failing, I think because the xp fixture tries to pass in numpy, which is not (currently) compatible with the Array API (e.g. np.concat doesn't exist, np.array doesn't support the device argument, etc.). What's the best approach here?

@jakevdp jakevdp requested a review from lucascolley May 10, 2024 23:49
@lucascolley
Copy link
Member

Right, this is why I didn't do this in fftfreq. It shouldn't be too complicated though, I think xp_res = array_namespace(xp) in the test functions, then passing xp=xp_res in to the functions being tested, should work.

@lucascolley lucascolley removed request for ilayn and larsoner May 11, 2024 10:24
@jakevdp
Copy link
Member Author

jakevdp commented May 13, 2024

Should we worry about no longer having test coverage for the default call pattern, i.e. not passing xp at all?

@lucascolley
Copy link
Member

Should we worry about no longer having test coverage for the default call pattern, i.e. not passing xp at all?

I would add a separate test for just that.

@@ -53,6 +54,10 @@ def general_cosine(M, a, sym=True):
When True (default), generates a symmetric window, for use in filter
design.
When False, generates a periodic window, for use in spectral analysis.
xp : module, optionaloptional array API namespace (default: numpy)
Copy link
Member

@Kai-Striega Kai-Striega May 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
xp : module, optionaloptional array API namespace (default: numpy)
xp : module, optional
array API namespace (default: numpy)

Just a minor typo

@lucascolley lucascolley marked this pull request as draft July 22, 2024 23:28
@lucascolley
Copy link
Member

(marked as draft until we have the PR with the test changes)

@lucascolley lucascolley changed the title EHN: signal.window: add array API support ENH: signal.window: add array API support Sep 12, 2024
@lucascolley lucascolley removed their request for review September 14, 2024 18:09
@ev-br
Copy link
Member

ev-br commented Feb 2, 2025

This has been merged as a part of gh-21783, closing as completed.

Thank you @jakevdp for starting this, thanks to all reviewers.

@ev-br ev-br closed this Feb 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
array types Items related to array API support and input array validation (see gh-18286) enhancement A new feature or improvement scipy.signal
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants