Fieldtrip plugin for interpolating bad electrodes

Dear Brainstormers,

I have recently started using Brainstorm, and needed to interpolate some electrodes in my recordings.
Since no such option currently exists, I have written a short plugin that allows the interpolation. The plugin uses the the function ft_channelrepair from the fieldtrip toolbox.
I am no expert in writing codes, let alone plugins, but I hope this would be useful.

One point I could not resolve is the integration of a new history comment in the file history. Maybe someone could help with that…

Cheers,

Roey

Hi Roey,

Sorry for the double response, I saw your email before you forum post.
Thank you very much for this contribution, I will test and integrate this function soon.

The re-interpolation of the bad channels is indeed a function that many EEG users ask for.
It is an interesting feature for displaying the recordings, but not necessarily for calculating ERPs as the average is already correctly weighted to compensate for channels that are bad in some sensors only, and it should be avoided if the recordings are to be used for estimating brain sources. Adding artificial data with any type of interpolation will not add any information to the inverse model, it adds only noise and errors due to the interpolation.
Users should be warned clearly about this before replacing the bad channels automatically as it is done in many ERP analysis pipelines.

Cheers,
Francois

Hi Roey,

Thank you for this very well written function, it was very useful!

I used it to generate two processes that do the same thing:
[ol]
[li][B]FieldTrip > ft_channelrepair[/B]: Very good example on how to call FieldTrip functions from Brainstorm. I re-wrote some sections, added the other methods, and renamed it to process_ft_channelrepair.m[/li]=> I moved all the conversion of data structures (Brainstorm=>FieldTrip) in a separate function [B]out_fieldtrip_data.m[/B], which will be re-used in other processes (hopefully).
[li][B]Standardize > Interpolate bad electrodes[/B] (process_eeg_interpbad): I re-coded the ‘nearest’ method in a much more compact form (does not need FieldTrip). It should produce the same output as the FieldTrip version.[/li][/ol]
Thank you very much for this contribution!
I hope you will be interested in integrating more FieldTrip functions into Brainstorm in the future :slight_smile:

Francois

Dear Francois,

This is wonderful - thank you so much for taking the time to re-write the needed sections. Seperating the out_fieldtrip_data function is indeed a useful idea, which I wanted to integrate into the code once I got our blessing :slight_smile:

I’d love to integrate some more fieldtrip functions in the future, and perhaps some other functions which I will use in my own analyses and could be useful for other users.

Thank you again for making this toolbox so user-friendly for integrating new processes!
Cheers,

Roey

Hello,

I was trilled to see this plugin added. I have been trying for several hours to get it to work but when I type in something like “#119” or “E119” into the ‘Sensor types or names’ field, the process either generates an error that no neighbors can be found for the channel or that the channel itself cannot be found. I am assuming that there is some small syntax error I am making here but so far I haven’t been able to figure it out.

Thank you so much for adding this. I just discovered Brainstorm yesterday and absolutely love it. I was thrilled to see that you have just added code for interpolation as well from Field Trip. Everything else in Brainstorm is working great for me but I can’t seem to get the interpolation to work. Have been working on it for about 7 hours now. When I put a channel, such as ‘E124’, in the ‘sensor types or names’ field (for either ft_channelrepair or interpolation), the process gives an error message that it is not able to find any of the channels neighbors. Any idea what I am doing wrong?

Tim

Dear Tim,
You are using the [B]Standardize > Interpolate bad electrodes[/B] process .

Could you maybe post the electrodes file you’re using, to make sure you’re using the right names?
I think you should do the following:
0) In matlab, open the file process_eeg_interpbad.m from the brainstorm toolbox library, and in line 118 change “iNotFix” to “iNotFixed” if you haven’t yet done so

  1. Mark as bad all the channeld you want to fix in the window where you see the time series of your data
  2. In the [B]Standardize > Interpolate bad electrodes[/B] window, enter “EEG” instead of “E124”
  3. Run the process

Does it work properly?

If not, in the time being, you could try using the [B]FieldTrip > ft_channelrepair[/B] process (although for that you should first download the free fieldtrip toolbox. You could also download fieldtrip lite, which has all the functions you need for the interpolation to work properly). Using this process, you have to mark the wanted channels as bad (when you view the time series of you recordings). Depending on the number of electrodes you have in your data, you should determine the maximal distance between two electrodes to be regarded as neighbours (~5 cm is pretty fine for 64 electrodes. The more electrodes, the shorter the distance you want).
Best!
Roey

P.S
Francois, it seems like the history of the fieldtrip process isn’t updated with the names of the electrodes that were interpolated. Do you know why that is?

Hi Roey,

Thank you so much for your reply. I have tried using the ‘Standardize>Interpolate bad electrodes’ as well as the ‘FieldTrip>ft_channelrepair’; however, I have mainly been trying the ft_channelrepair. I haven’t been able to get either to work yet. I have been marking the bad channels as bad before I drag the file down to Process 1. However, when I run the ft_channelrepair I just keep getting the error message that it can’t find any neighboring electrodes. I have tried various distances between for 4cm - 8 cm. My data is from a 128 sensor EGI net, and I have imported into Brainstorm in a .raw file (simple binary). I have not uploaded any electrodes file … perhaps that is the problem.

Tim

Hello,

I have tried changing line 118 in the file process_eeg_interpbad.m from “iNotFix” to “iNotFixed”. I have marked a channel as bad and put “EEG” in the text field for Standardize>Interpolate bad electrodes. When running the process, I get the error “Some sensors do not have valid positions or are not EEG electrodes”.

When I try the FieldTrip>ft_channelrepair option I mark a channel as bad and then run the process with ‘EEG’ in the field text box. I have tried all 4 types of interpolation but am primarily interested in using spherical spline interpolation with my data. When I try to run that process I get the message that “Input to SVD must not contain NaN or Inf”.

I’m not really sure what else to try as there are only 2 basic options to choose on the dialogue box.

Regards,

Tim

Hi Tim,

You need to add the positions of your electrodes, the EGI files do not contain them. When you link your file to the database, you see a “channel file” that indicates the names and types of the channels, but without 3D positions for the electrodes.
The interpolation of bad channels is based on the position of the electrodes on the scalp, it replaces the missing values with a weighted averaged of the adjacent values. If you don’t have the electrodes positions, you cannot use this process.
You can either import realistic positions that you digitized on each subject or use a standard set of positions available in Brainstorm.

If you haven’t followed all the 12+3 introduction tutorials, I would recommend you start by doing so.
Then you can read the EEG/Epilepsy tutorial, it gives some more instructions about the channel files in EEG.

Cheers,
Francois

Hello Francois,

Thank you very much for this reply. I have read through a majority of the tutorials but haven’t read them completely and in depth because I was mainly scanning for the parts I needed. I will try going through more thoroughly to find out how to import the electrode positions file.

Thank you also for creating this great program. As soon as I figure out the interpolation bit, I will be using this as opposed to BrainVision Analyzer as it suites my needs better and I just generally like it better.

Cheers,

Tim

Thank you both for your contributions and your supportive comments.

Btw, I fixed the “iNotFixed” bug… You can update one last time before you try again.

You can find additional information about channel files management in this tutorial (part of the new set of tutorials we are currently working on):
http://neuroimage.usc.edu/brainstorm/Tutorials/ChannelFile

Cheers,
Francois

Hi,

Just wanted to update that the Fieldtrip>Ft_ChannelRepair worked great after I was able to upload the correct EEG sensor file from EGI. In case anyone else is trying to figure this out, just right click on your subject, then go to “import channel file”, and then upload the EEG channel file (for example, a .sfp file for EGI).

Thanks for your help with this!

Tim

Tim

I would recommend you use instead the menu “Add EEG positions > Import from file”, as described in the EEG/Epilepsy tutorial:
http://neuroimage.usc.edu/brainstorm/Tutorials/Epilepsy#Access_the_recordings

This way the template electrode positions are matched with the existing channel file using their names, instead of completely replacing the channel file.

Great! Thanks.

Tim

Note for BrainVision / BrainAmp users:
A bug was found in the application of the channel gain, the amplitudes read where sometimes incorrect.
Please update Brainstorm for reading correct amplitudes from the BrainVision files.