Interpolate_bads: "ValueError: array must not contain infs or NaNs"

Hello :slight_smile:

since I accidently set an eeg electrode, my mastoid reference, as ā€œeogā€, and now fixed it and set it to ā€œeegā€, I get an error when I want to interpolate the data. When I drop bad channels and then reload the data to save the interpolated data, I get ā€œValueError: array must not contain infs or NaNsā€. What also changed since I set the electrode to ā€œeegā€, is that the plot.psd is now black instead of coloured. The following code itself doesnt produce any error:

    raw.set_channel_types(mapping={'hore 28': 'eog', 'vou 17': 'eog', 'holi 32': 'eog', 'mast2':'eeg'})
    #Display file information
    print(raw)
    print(raw.info)

    # Load the montage (1020)
    ten_twenty_montage = mne.channels.make_standard_montage('standard_1020')
    raw_1020 = raw.copy().set_montage(ten_twenty_montage, on_missing='ignore')
    
    print(raw_1020.info['projs'])

    # Sanity check visualizations
    raw_1020.plot_psd(fmax=80)
    raw.plot(duration=4, n_channels=5, show_scalebars=(True))
    
    return raw_1020

bids_root ='C:\\Users\\Admin\\Desktop\\Projekt_Franziska\\Studie 3\\Analyse\\EEG\\BIDS_EOG_EEG'
subject = 'sub-09' 
session = 'ses-Expo'
raw_1020=flux_eegImport(bids_root, subject, session)
# Save referenced data with marked bad channels
raw_1020.info['bads'] = ['T7','T8']

But when I want to interpolate the data afterwards with the following code, I get the ā€œValueError: array must not contain infs or NaNsā€ error. This only appears when I marked electrodes as bad beforehead, in this case T7 and T8.

def flux_eegInterpolateBads (mne_data_folder, subject, session):
    raw_bads=mne.io.read_raw_fif(mne_data_folder + subject + '_ses-' +  session + '_run01_' + 'data_ref.fif', verbose=False)
    raw_bads.load_data()
    print(raw_bads.info['bads'])

    eeg_data = raw_bads.copy().pick_types(eog=True, eeg=True, exclude=[])
    eeg_data_interp = eeg_data.copy().interpolate_bads(reset_bads=False)

    for title, data in zip(['orig.', 'interp.'], [eeg_data, eeg_data_interp]):
        fig = data.plot(butterfly=True, color='#00000022', bad_color='r')
        fig.subplots_adjust(top=0.9)
        fig.suptitle(title, size='xx-large', weight='bold')
    
    eeg_data_interp = eeg_data.copy().interpolate_bads(reset_bads=True)
    
    return eeg_data_interp

# Save referenced data with marked bad channels
#Subjects Included: 4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,24,25
# 26,27,28,30,31,33,34,35

subject = 'sub-09'
session = 'ses-Expo'
bids_root ='C:\\Users\\Admin\\Desktop\\Projekt_Franziska\Studie 3\\Analyse\\EEG\\BIDS_EOG_EEG'
mne_data_folder=bids_root + '/' + subject + '/' +  session + '/' +  'eeg/' 

raw_interpolated=flux_eegInterpolateBads (mne_data_folder, subject, session)
raw_interpolated.save(mne_data_folder + subject + '_' +  session + '_run01_' + 'data_interpolated.fif', overwrite=True)

I wondered if this is the case since my added eeg electrode is not included in the 1020 system and I would have to manually add it. I found the suggestion with on_missing=ā€˜ignoreā€™, but it didnt do the trick. When I set it to on_missing=ā€˜warningā€™, I get There are 1 channel position not present in the DigMontage. The required channels are: [ā€˜mast2ā€™].

Any suggestions?

Thank you :slight_smile:
Franzi

Hello @FranziMNE,

I believe @sappelhoff found that the left and right mastoids can, in fact, be mapped to the 10-20 system, I just donā€™t remember the names of the respective electrodes ā€“ I believe it was TP9 and TP10, respectively.

Interestingly, the standard_1020 montage shipping with MNE-Python additionally contains the channels M1 and M2. Iā€™m not quite sure how this all adds up.

# %%
import mne

m = mne.channels.make_standard_montage('standard_1020')
m.plot()

In any case, for your situation, the easiest solution is probably to simply rename the channels mast1 and mast2 to M1 and M2, respectively, before setting the montage.

Best wishes,
Richard

tagging @cbrnr also

1 Like

Thank you :slight_smile: I am just wondering why I dont get any error when I dont select a channel as bad. It only appears when I select channels as bad, irrespectiv of the size or position of the channel.

I was only addressing the question regarding the mastoids in my answer; I donā€™t know about the NaNs, sorry :frowning:

Alright! :slight_smile: thank you!

So actually rename the channels did the trick. Also when I select bad channels, I dont get any error messages again an my plot.psd is coloured again.

#Remap non-eeg channels
    raw.rename_channels(mapping={'mast2':'M2'})
    raw.set_channel_types(mapping={'hore 28': 'eog', 'vou 17': 'eog', 'holi 32': 'eog'})
    #Display file information
    print(raw)
    print(raw.info)

    # Load the montage (1020)
    ten_twenty_montage = mne.channels.make_standard_montage('standard_1020')
    raw_1020 = raw.copy().set_montage(ten_twenty_montage, on_missing='ignore')
    

    #Setting average reference as a projection
    #raw_1020.set_eeg_reference('average', projection=True)
    #raw_1020.set_channel_types('mast2')
    print(raw_1020.info['projs'])

    # Sanity check visualizations
    raw_1020.plot_psd(fmax=80)
    raw.plot(duration=4, n_channels=5, show_scalebars=(True))
    
    
    return raw_1020

plot_sensors, gives me the following plot now

I think you should get rid of the ignore bit here, too. That may have been the source of your issue / may have hidden it.

1 Like

I had to use montages recently too, and was reminded that they are still a bad mess (I needed template locations with those mastoids, but the only montage we have is standard_1020, which is ā€œrealisticā€). We should really try to fix things, starting e.g. with what you mentioned in Built-in, standard, and template montages, and layouts šŸ¤Æ Ā· Issue #10741 Ā· mne-tools/mne-python Ā· GitHub.

2 Likes

For all built-in EEG montages, describe whether they're based on idealized or (at least somewhat) realistic coordinates, and whether or not they might be a good idea for source modeling Ā· Issue #10752 Ā· mne-tools/mne-python Ā· GitHub Would be a good first step

I think weā€™d want to adjust the output produced by get_builtin_montages() to tell us whether itā€™s a realistic montage or not.

That too, but we also need a 10/05 template montage. Once I have time I will replace/create montages with @sappelhoffā€™s eeg_positions.

1 Like

Great!

@cbrnr If you ever have time to join one of our dev meetings on Friday, I would love to discuss & coordinate this with you and @sappelhoff. Or maybe the 3 of us could meet up for a video call at a different time? Just to, you know, calibrate expectations and coordinate the work!

Yes, letā€™s coordinate over Discord. The usual time does not work for me, but I could do Friday morning to noon? This week Iā€™m also free on other days, letā€™s try to find a time that works for us.

1 Like

if we move the meeting 1H earlier on fridays does it work for you?

Not really, Iā€™m available only until noon. I hope thatā€™s OK, most things can be discussed on GitHub anyway, and if not then weā€™ll hopefully find another time slot with the people involved.