Scripting: how to automatically import and epoch EEG data?

Hi there,

I’ve gone through the brainstorm tutorial on scripting but I’m still having a bit of difficulty with my script. Essentially I need to import EEG data, epoch the data from 0-1100 ms, set a prestimulus baseline of 0-100ms, and then remove specific events that I don’t want, and finally import custom anatomy for younger subjects.

I have all of the data in a .set format which is easily readable by brainstorm so that’s ready to go. I also wrote a script that created a .csv file which tells me exactly which events I want to remove for each subject, so if I could somehow include that .csv file in this script that would make things a lot easier.

So far I’ve added the path to open brainstorm and I’ve written the following lines to open brainstorm without GUI:

addpath(genpath(’/mydisk/brainstorm3/’))
if ~brainstorm(‘status’)
brainstorm nogui
end

From here I need to execute the following functions in a for loop for each subject (100 subjects in total):

  • Import MEG/EEG (my data is in a .set file)
  • Epoch time series (0-1100ms)
  • Set a prestimulus baseline (0-100ms)
  • Automatically remove events that I don’t want to analyze (using a .csv file that I’ve already written)
  • Import custom anatomy and warp to fit sensor space (I have three age ranges with seperate cortical envelopes: 6-13, 14-17, 18-45)

I apologize but I couldn’t figure out how to do this in the brainstorm tutorials so I figured I’d ask here. If I’m missing any information please let me know and I’ll add more information to make this make more sense!

Thank you in advance!

Hello,

You need to order the steps the way they are presented in the introduction tutorials (read them all from #1 to #19 if you haven’t done it yet): Start with the import of the anatomy, then link the recordings, then warp.
Scripting the preparation of the anatomy is not easy and requires many manual operations. Maybe it would be faster for you to import the anatomy for all the subjects (or use the menu “File > Batch MRI fiducials” to create a fiducials.m file in all the freesurfer folders).

Manipulation of the events: look at all the processes in the Events category (read all the instructions in the option windows). If you don’t find anything interesting for the type of sorting you want to do (there is no way to use an external .csv file in the existing processes), you need to do this sorting by yourself. Examples:
http://neuroimage.usc.edu/brainstorm/Tutorials/Scripting#Example:_Editing_events
http://neuroimage.usc.edu/brainstorm/Tutorials/EventMarkers#On_the_hard_drive

For the other operations, you can find examples in the scripts corresponding to all the tutorials (“Get started” and “Other analysis scenarios”): brainstorm3/toolbox/script.
http://neuroimage.usc.edu/brainstorm/Tutorials

In general, for each processing step, find a process that does what you need, then generate a Matlab script for it, as illustrated in the Scripting tutorial:
http://neuroimage.usc.edu/brainstorm/Tutorials/Scripting#Starting_a_new_script

Francois

I’m sorry but I don’t think I fully understand your reply. Right now all I need to do is “Import MEG/EEG” in .set format in a for loop, but whenever I try the functions outlined in the scripting tutorial it says “unsupported filetype” etc. I’ve tried db_add, bst_save, db_add_file and so on but none of these work with .set files.

I’ve also tried using the “Find Files” function in matlab but had no results from this.

You can load your .set files in Brainstorm from the interface?
Therefore you can load it with with the process “Import recordings > Import MEG/EEG: Existing epochs” (if you have already epoched your recordings with EEGLAB, use the process “Import MEG/EEG: Events” otherwise).

Select this process in the pipeline editor, then use the menu “Generate .m script”, as illustrated in this tutorial: http://neuroimage.usc.edu/brainstorm/Tutorials/Scripting#Starting_a_new_script
Then put this call in a loop.

If you face error messages you don’t understand, please post the full error message here, together with your scripts.

Perfect, the pipeline editor is exactly what I need. Got it working, thanks!

Actually there are still issues when using the pipeline to import MEG/EEG with events from a .set file. I entered in “92, 14, 20” as the events which I know are in the file, but it gave me a few error messages saying “No events to import found in file: /filename/” and "No good trials imported from these files.

When I import MEG/EEG manually it automatically finds these events on the other hand…

I can’t upload an attachment for the .set file because I’m a new user is there any way I could message you the attachment?

If your file is already epoched, you should use the process “Import MEG/EEG: Existing epochs”.
The one you are using (Import MEG/EEG: Events) is for extract epochs from a continuous file only.

If you want to share data with us, upload your file somewhere (eg. dropbox, google drive, …) and copy the link here.

There are event markers in the file but I don’t believe it’s been epoched. So I guess it’s a continuous file? The event markers are showing up in brainstorm when I upload without epoching, but when I set the events in the pipeline editor, brainstorm can’t find them.

Here is the file for better context:
https://drive.google.com/open?id=0B25kPxE-g9_0eTY5TEhmLVBOVHc

Please make this link public.
And post a screen capture showing how your data files look like in your database explore, and another one showing the options you are using to load this file in the pipeline editor.

Ok I added access to the link.

Database Explore
Pipeline
Error Message

Thanks.
I could not import anything from your file because you didn’t include the actual data file (.fdt).
But simply from looking at the .set file, it looks like the problem is coming from the naming of your events. They are named "14 " instead of “14”, with two extra spaces at the end. The pipeline editor syntax does not support event names with spaces at the beginning or the end.

To make this work, you could rename your markers, either with EEGLAB, or by editing the .set file to remove the extra spaces, or by renaming them in Brainstorm (interface or process).

I am now able to automatically import and epoch the EEG data after I changed the event names, thanks!

One final question if that’s ok: is there any way to suppress the popups that occur after bst_process?

After each for loop, brainstorm gives me a popup saying: "Warning: The EEG electrodes locations do not seem to be in the expected units (mm). Please select a scaling factor for the units (Suggested:10):

The suggested value is what I want to use, so is there any way to prevent this pop-up from occuring after every loop or will I have to manually press enter for importing the data?

Thanks for all your help!

Hello,

I fixed this issue in this last commit:
https://github.com/brainstorm-tools/brainstorm3/commit/2a67c6a53f3c9ecfafc684e5239b2a21d720b5a9

Now if you use a script, you shouldn’t get these question anymore, and it should use the detected scaling factor automatically. Make sure the scaling factor is correctly applied and the channel files are correctly aligned with the head surface.

Please update Brainstorm (menu Help > Update Brainstorm) and try again.

Cheers,
Francois

1 Like