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 44 commits into
scipy:mainfrom
fbourgey:array-api-welch
Open

ENH: signal.welch: Add Array API support#25177
fbourgey wants to merge 44 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
Comment thread scipy/signal/tests/test_spectral.py
r"nperseg\s*=\s*256 is greater than (?:input length|signal).*",
UserWarning
)
f, p = welch(x, window='hann') # default nperseg
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.

what is the reason for removing the user-specified nperseg

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 removed it because that user-specified nperseg=256 case was failing in the GPU backend CI after the xp conversion at the beginning. Should I re-add it with a backend-specific skip/xfail?

Comment on lines +170 to +171
For single-precision input (float32 or complex64), JAX returns the sample
frequencies in float32, whereas SciPy and CuPy always return them in 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.

Suggested change
For single-precision input (float32 or complex64), JAX returns the sample
frequencies in float32, whereas SciPy and CuPy always return them in float64.
For single-precision input (``float32`` or ``complex64``), JAX returns the sample
frequencies in ``float32``, whereas SciPy and CuPy always return them in ``float64``.

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