Skip to content

FIX: Handle NaN values in FFT resampling (#14421) - #14580

Merged
larsoner merged 4 commits into
mne-tools:mainfrom
ADITYASINGH1206:fix/resample-nan-handling
Nov 30, 2026
Merged

larsoner merged 4 commits into
mne-tools:mainfrom
ADITYASINGH1206:fix/resample-nan-handling

Conversation

@ADITYASINGH1206

@ADITYASINGH1206 ADITYASINGH1206 commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

Reference issue (if any)

Fixes #14421

What does this implement/fix?

When using FFT-based resampling (method="fft" in mne.filter.resample or Raw.resample), having even a single NaN sample in a channel contaminates the entire channel into NaN because Fourier-domain operations spread the NaN across all frequencies.

This pull request implements the solution discussed with @larsoner in #14421:

  1. Detect NaN values in _resample_fft.
  2. Temporarily fill NaN segments via linear interpolation (np.interp), holding boundary values for edge gaps, so the FFT resamples smoothly without spreading NaN.
  3. Map output sample indices back to the nearest original samples and restore NaN at those positions.
  4. Leave polyphase resampling unchanged.

Additional information

Added tests in mne/tests/test_filter.py covering:

  • Leading, trailing, and interior NaN gaps
  • Single valid sample preservation
  • 2D/3D multi-channel arrays and non-default axes
  • Parallel execution with n_jobs=2
  • End-to-end RawArray.resample verification from Raw.resample doesn't handle NaNs #14421
  • Added release changelog entry in doc/changes/dev/14421.bugfix.rst

@welcome

welcome Bot commented Nov 21, 2026

Copy link
Copy Markdown

Hello! 👋 Thanks for opening your first pull request here! ❤️ We will try to get back to you soon. 🚴

@larsoner larsoner left a comment

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.

Added a comment that this comes from MNELAB and added @cbrnr to the credit line. Marking for merge-when-green, thanks in advance @ADITYASINGH1206 @cbrnr !

@larsoner
larsoner enabled auto-merge (squash) November 30, 2026 15:40
@larsoner
larsoner merged commit c2f9750 into mne-tools:main Nov 30, 2026
33 of 34 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Raw.resample doesn't handle NaNs

2 participants