Skip to content

raise error in interpolate_bads if no sensor positions are provided #13363

Closed

Description

@skjerns

Description of the problem

I'm adding some sensors that were missing during recording as empty channels to then interpolate them. However, the resulting channel is interpolated as nan, probably because I did not supply positions to the new channel.

I only found out about that by accident. I think there should be a warning if interpolation of a channel is attempted and fails, and/or if some sensors don't have positions while interpolating

Steps to reproduce

raw = mne.io.read_raw('file.fif', preload=True)
empty_ch = np.zeros_like(raw.get_data('MEG1633'))
info = mne.create_info(ch_names=['MEG2112'], sfreq=raw.info['sfreq'], ch_types=['grad'])
raw_ch = mne.io.RawArray(data=empty_ch, info=info, first_samp=raw.first_samp)

raw.add_channels([raw_ch], force_update_info=True)
raw.info['bads'] += ['MEG2112']
raw.interpolate_bads()
# -> channel will be nan

Expected results

Either raise error or warning

Actual results

channel is silently interpolated to nan

Additional information

MNE 1.10

Activity

  1. larsoner commented on Aug 8, 2025

    @larsoner
    Member

    Makes sense to me, at least in cases where method is something that actually uses the positions (which is true for MEG sensors by default, but wouldn't be for method="nan" for example). Would you be up for making a PR? I think it's likely we'll want a new kwarg on_no_position="warn" to control this behavior (using the internal _on_missing(...) to raise an error, emit a warning, or ignore the issue).

  2. 1himan commented on Dec 1, 2025

    @1himan
    Contributor

    I'm working on a fix. Should I add warnings in the:

    add_channels() logic, something like: Warning: Channel 'MEG2112' was added without valid sensor position (loc) information. Interpolation or spatial filtering will fail silently or raise an error unless position data is manually supplied.

    Or

    For interpolate_bads(): ValueError: Cannot interpolate channel 'MEG2112'. Sensor position information (raw.info['chs'][...]['loc']) is missing or invalid. Please supply 3D coordinates before proceeding.

    or both?

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