Skip to content

Raw.resample doesn't handle NaNs #14421

Done

Description

@cbrnr

Description of the problem

Raw.resample turns the entire channel into NaN if even one sample is NaN. This can occur in real-world data, for example, in eye-tracking recordings (blinks, see #11879) or XDF recordings where streams end at different times.

Steps to reproduce

import mne
import numpy as np

sfreq = 1000
data = np.sin(2 * np.pi * 10 * np.arange(10000) / sfreq)[np.newaxis]
data[0, -10:] = np.nan
raw = mne.io.RawArray(data, mne.create_info(1, sfreq, "eeg"))

raw.resample(500)
print(np.isnan(raw.get_data()).mean())  # 1.0

Link to data

No response

Expected results

I think Raw.resample should handle NaN, for example by temporarily filling NaNs with linear interpolation, resampling, and then restoring NaNs at the corresponding locations. Unlike #11408, this approach relies on the data rather than annotations.

I'm happy to open a PR.

Actual results

The entire channel consists of NaN after resampling.

Additional information

Platform             Linux-7.2.9-arch1-1-x86_64-with-glibc2.44 (Wayland)
Python               3.12.14 (main, Aug 25 2026, 14:00:49) [Clang 22.1.3 ]
Executable           /home/clemens/Projects/mnelab/.venv/bin/python
CPU                  AMD Ryzen 7 9700X 8-Core Processor (16 cores)
Memory               60.5 GiB

Core
├☑ mne               1.13.2 (latest release)
├☑ numpy             2.5.3 (OpenBLAS 0.3.34.106.0 with 16 threads via pthreads)
├☑ scipy             1.18.1
└☑ matplotlib        3.11.2 (backend=qtagg)

Numerical (optional)
├☑ scikit-learn      1.9.0
├☑ threadpoolctl     3.6.0
├☑ h5io              0.2.5
├☑ h5py              3.16.0
└☐ unavailable       numba, nibabel, nilearn, dipy, openmeeg, python-picard, jamica, cupy, pandas

Visualization (optional)
├☑ qtpy              2.4.3 (PySide6=6.11.2)
├☑ pyqtgraph         0.14.0
├☑ mne-qt-browser    0.7.5
└☐ unavailable       pyvista, pyvistaqt, vtk, ipympl, ipywidgets, trame, trame_client, trame_server, trame_pyvista, trame_vtk, trame_vuetify

Ecosystem (optional)
├☑ autoreject        0.4.4
├☑ edfio             0.4.17
├☑ pybv              0.8.1
└☐ unavailable       mne-bids, mne-nirs, mne-features, mne-connectivity, mne-icalabel, mne-bids-pipeline, neo, eeglabio, curryreader, mffpy, pymef, antio, defusedxml

Activity

  1. scott-huberty commented on Oct 16, 2026

    @scott-huberty
    Contributor

    I think that making raw.resample just work with eye tracking data (that contains NaNs) would be so nice! That is, if we can do it in a way that makes sense (I remember @larsoner having some concerns #11418 ).

    Right now, I think that the best way to mitigate nan propagation is to pass method='polyphase' (but you have to know that this is an option in the first place). @larsoner WDYT about this idea of temporarily interpolating NaN's prior to resampling?

  2. larsoner commented on Oct 16, 2026

    @larsoner
    Member

    Using linear interp for example in the NaN regions and then restoring them after resampling sounds like a nice workaround! Notably it shouldn't be needed when polyphase is used (since it's FIR not FFT-based resampling, it doesn't require everything to be non-NaN and should propagate NaN correctly) but should be an option there as well, since you can think of this linear-ramp insertion as a type of edge padding (like we always add to the start and ends of recordings anyway).

    Eventually we could think about allowing all edge padding modes that we currently do for the NaN regions by computing leftward and rightward pads and averaging them or something, but it seems too complicated and a linear ramp over the NaN region should probably work okay. (It'll still ring sometimes if there are big discontinuities etc. but that's probably okay... and in those cases, polyphase with proper NaN propagation would be the way to avoid it by allowing the NaN region to grow.)

  3. cbrnr commented on Oct 16, 2026

    @cbrnr
    ContributorAuthor

    I've linked the PR with a working implementation - I think it's definitely a lot better than not working at all. Would something along these lines be what we could do here?

  4. larsoner commented on Oct 23, 2026

    @larsoner
    Member

    Yeah I think that would make sense in mne.filter.resample, probably only for FFT mode; could add for polyphase later if needed but letting the NaNs propagate there seems reasonable already. Eventually we could add nan_interpolate=Literal["auto"] | bool which means "interpolate for FFT, don't for polyphase" or similar but I don't think we need it until someone asks for it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions