Thanks to visit codestin.com
Credit goes to github.com

Skip to content

ENH: signal.welch: Add Array API support#25177

Open
fbourgey wants to merge 43 commits into
scipy:mainfrom
fbourgey:array-api-welch
Open

ENH: signal.welch: Add Array API support#25177
fbourgey wants to merge 43 commits into
scipy:mainfrom
fbourgey:array-api-welch

Conversation

@fbourgey
Copy link
Copy Markdown
Member

@fbourgey fbourgey commented May 19, 2026

Reference issue

@j-bowhay's comment

What does this implement/fix?

  • Add Array API support to signal.welch
  • Use CuPy / JAX delegation
  • Torch GPU is not covered

Additional information

There are a lot of changes here so signal.welch might not have been the best candidate to start with. This is my first PR adding array-api support for a function. Feel free to let me know if this is the correct approach. I asked @ev-br for some feedback while I was working on this.

AI Generation Disclosure

I've started rewriting the tests myself and Codex helped me fixing tests locally.

@github-actions github-actions Bot added scipy.signal enhancement A new feature or improvement labels May 19, 2026
Comment on lines +166 to +170
welch_extra_note = \
"""Support for CuPy and JAX is provided by delegation to
``cupyx.scipy.signal.welch`` and ``jax.scipy.signal.welch``.

"""
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Are we keeping this note?

Comment thread scipy/signal/tests/test_spectral.py Outdated
Comment on lines +532 to +595
assert_allclose(np.sqrt(trapezoid(p_dens, freq)), A*np.sqrt(2)/2,
rtol=1e-3)
rms_sq = xp.sum((p_dens[1:] + p_dens[:-1])
* (freq[1:] - freq[:-1]) / 2)
xp_assert_close(xp.asarray(xp.sqrt(rms_sq)),
xp.asarray(A * np.sqrt(2) / 2), rtol=1e-3)
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This reimplements trapezoid. I do no think there is support as per data-apis/array-api-extra#490.

Comment thread scipy/signal/tests/test_spectral.py Outdated
@tylerjereddy tylerjereddy added the array types Items related to array API support and input array validation (see gh-18286) label May 19, 2026
@fbourgey fbourgey changed the title ENH: signal.welch: Array support for array-api ENH: signal.welch: Add Array API support May 19, 2026
Comment thread scipy/signal/_spectral_py.py Outdated
Copy link
Copy Markdown
Member

@j-bowhay j-bowhay left a comment

Choose a reason for hiding this comment

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

A few comments:
Blocking: The dtype of the expected value shouldn't be, for example, res.dtype.

Non-blocking: please try to minimise unrelated formatting changes in these PRs as they make the diff much larger. I know it's hard to resist the temptation...

Comment thread scipy/signal/tests/test_spectral.py Outdated
Comment thread scipy/signal/tests/test_spectral.py Outdated
Comment thread scipy/signal/tests/test_spectral.py Outdated
Comment thread scipy/signal/_spectral_py.py
@fbourgey fbourgey marked this pull request as draft May 20, 2026 17:32
@fbourgey fbourgey marked this pull request as ready for review May 20, 2026 19:41
q = np.array([0.08333333, 0.15277778, 0.22222222, 0.22222222,
0.11111111], 'f')
assert_allclose(p, q, atol=1e-7, rtol=1e-7)
f_dtype = xp.float32 if is_jax(xp) else xp.float64
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It would be good to add to the extra note that Jax uses different dtype promotion rules

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I added a comment in the four tests.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Sorry I wasn't clear, I meant adding it to:

welch_extra_note = \
    """Support for CuPy and JAX is provided by delegation to
    ``cupyx.scipy.signal.welch`` and ``jax.scipy.signal.welch``.

    """

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

OK. I will move the comment to the extra note.

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.

4 participants