Interpolate_bads function not being recognized

MNE Version 1.5.0
Windows 10
Spyder (MNE)

Issue: The interpolate_bads function is not being recognized as I follow along the “Handling bad channels” page of the MNE documentation. Here is the code that I pulled from it to use in my project and this is the error I get.

I am using bdf files and am loading them in.

By “not being recognized”, I mean that it is not showing up as a built in function when I type the “.inter…”.

Code:

import numpy as np
import pandas as pd
import seaborn as sns
from copy import deepcopy
import matplotlib.pyplot as plt
import mne as mne
from mne.io import read_raw
import os
import os.path as op
import psutil

p01_raw.info["bads"].extend(["P9", "Fp2", "AFz", "F4", "F6", 
                             "F8", "FT8", "PO4"])
p01_events = mne.find_events(p01_raw, verbose=True)
# Interpolate 
p01_data = p01_raw.copy().pick_types(eeg=True, exclude=[])
p01_data_interp = p01_data.copy().interpolate_bads(reset_bads = False)

Error:
RuntimeError: Cannot fit headshape without digitization , info["dig"] is None

You need to assign a montage with e.g. p01_raw.set_montage("biosemi64") in order for interpolation to work.

1 Like

@cbrnr, sorry. I was really busy yesterday going to my parent’s house. The RuntimeError is not there anymore.

However, it is now raising a ValueError. Here is the code and the error. I can also post the whole traceback if you need it.
Code:

# Interpolate
p01_raw.set_montage("biosemi64")
p01_data = p01_raw.copy().pick_types(eeg=True, exclude=[])
p01_data_interp = p01_data.copy().interpolate_bads(reset_bads = False)

Error:

ValueError: DigMontage is only a subset of info. There are 8 channel positions not present in the DigMontage. The channels missing from the montage are:

['EXG1', 'EXG2', 'EXG3', 'EXG4', 'EXG5', 'EXG6', 'EXG7', 'EXG8'].

Consider using inst.rename_channels to match the montage nomenclature, or inst.set_channel_types if these are not EEG channels, or use the on_missing parameter if the channel positions are allowed to be unknown in your analyses.

These are not EEG channels and hence don’t have a location associated with them. Read the last line of the error message again; it explains what to do.

Best wishes
Richard

Hey Richard,

Thanks for the input. One more error(still pertaining to interpolate_bads). The last line in the code below is messing up and causing a “ValueError”. I have the error below the code snippit. I saw a prior post about a ValueError, but that post did not help me. On a note, the system I used only had EXG1 and EXG2. It did not have EXG3-8.

Code:

# Interpolate
p01_raw.set_montage("biosemi64", on_missing = "ignore")
p01_data = p01_raw.copy().pick_types(eeg=True, exclude=[])
p01_data_interp = p01_data.copy().interpolate_bads(reset_bads = False)

Error:
ValueError: array must not contain infs or NaNs

@richard, sorry if I am constantly bugging you with these errors. I am just trying to get this one line to work.

Okay. I fixed the ValueError. But that last line is still giving me issues in the form of a RunTime error.
Code snippet:

# Interpolate
p01_raw.set_montage("biosemi64", on_missing = "ignore")
p01_data = p01_raw.copy().pick_types(eeg=True, exclude=[])
p01_data_interp = p01_data.copy().interpolate_bads(
    reset_bads = False, method=dict(eeg="MNE"), verbose=True)

Error:
RuntimeError: Missing EEG channel location

You can use the exclude argument ofread_raw_bdf() to exclude channels you don’t need.

1 Like

Read_raw_bdf didn’t work for me even though I am using a bdf file, so after troubleshooting in my last forum post, the commenters on that post and I just decided that using standard read_raw would work for me.

That’s strange, read_raw() just calls read_raw_bdf() under the hood. You should be able to pass an exclude argument in any case though.

If my assumption is right then It seems like all your issues come back to those non-EEG channels that you have in your data:

['EXG1', 'EXG2', 'EXG3', 'EXG4', 'EXG5', 'EXG6', 'EXG7', 'EXG8']

First, you tried to apply the idealized template montage "biosemi64" to your data, but ran into an error because the above channels do not exist in that template montage. MNE cannot assign locations to channels that it does not have the postions for.

You got around that by telling MNE to ignore trying to set locations for those channels. The important thing is that now you cannot try to apply any operations that require channel positions for channels that you “ignored”.

But it seems to me that at some point you added one of those channels to raw.info["bads"]. You can confirm this on your side. Then you call raw.interpolate_bads. Assuming for example “EXG1” is marked as bad, MNE tries to interpolate that signal using the signal from neighboring sensors… But remember, in this case, MNE does not actually know the location of that channel, so it cannot interpolate it. Hence the error…

I think those channels for which you don’t have channel locations for are EOG channels (but i’ll let you confirm this). So either change their channel types to “eog” so that they are excluded from most MNE operations, or just drop them altogether if you don’t need them in your analysis:

i.e. using the file you shared in your last post:

from pathlib import Path
import mne

fpath = Path() / "Downloads" / "p1.bdf"
raw = mne.io.read_raw_bdf(fpath, preload=True)

eog_ch_names = ['EXG1', 'EXG2', 'EXG3', 'EXG4', 'EXG5', 'EXG6', 'EXG7', 'EXG8']
raw.set_channel_types({ch_name : "eog" for ch_name in eog_ch_names})

# Now you dont even have to use "ignore"
raw.set_montage("biosemi64")

# pretend these are added to bads
raw.info["bads"].extend(['EXG1', 'Cz'])

# Now this will work
raw.interpolate_bads()
1 Like

Sorry for the late reply. @scott-huberty, the solution you provided worked. Thank you all for helping me out.

2 Likes