Averaging data in MNE

The following script is an example for a standard averaging procedure. Obviously, you may want to use different parameters - have a look at the manual for details and more options.

You should have processed your raw data using Maxfilter.

Note that MNE will not process EEG data from the CBU correctly unless you apply "mne_check_eeg_locations" to your files.

You can mark bad channels in your fif-files using mne_mark_bad_channels. These bad channels will be excluded from further MNE operations (forward solution, inverse operator etc.).

The end result will be fiff-files with averages for each condition defined in the specification file (see below). They can be read into Matlab using fiff_read_evoked.

For further analysis of your EEG data, you may need to re-reference your data.

The following script requires a specification file with averaging parameters (thresholds etc.).


## Your variables

datapath='<myMEGdatapath>'       # root directory for your MEG data

# MEG IDs (your directory structure may differ)
subj_pre=(\
        'meg10_0001' \
        'meg10_0002' \
        'meg10_0003' \
        )

# MEG subdirectories (your directory structure may differ)      
subj_dir=(\
         '100001' \
         '100002' \
         '100003' \
        )
        

## Processing:

nsubjects=${#subj_pre[*]}
lastsubj=`expr $nsubjects - 1`


# REPORT number of files to be processed:

for m in `seq 0 ${lastsubj}`
do
  echo " "
  echo " Averaging SUBJECT  ${subjects[m]}"
  echo " "
 
  inpath=${path}'/'${subj_dir[m]}'/'${subjects[m]}
  
  echo ${inpath}

  mne_process_raw \
        --raw ${datapath}/${subj_pre[m]}/${subj_dir[m]}/yourrawMEGfile1.fif \
        --raw ${datapath}/${subj_pre[m]}/${subj_dir[m]}/yourrawMEGfile2.fif \
        --raw ${datapath}/${subj_pre[m]}/${subj_dir[m]}/yourrawMEGfile3.fif \
        --eventsout ${datapath}/${subj_pre[m]}/${subj_dir[m]}/yourrawMEGfile1-eve.txt \
        --eventsout ${datapath}/${subj_pre[m]}/${subj_dir[m]}/yourrawMEGfile2-eve.txt \
        --eventsout ${datapath}/${subj_pre[m]}/${subj_dir[m]}/yourrawMEGfile3-eve.txt \
        --ave YourSpecificationFile.ave \
        --gave ${datapath}/${subj_pre[m]}/${subj_dir[m]}/gave.fif \
        --saveavetag _avg \
        --highpass 0.1 \
        --projon   # applies average reference to EEG before artefact rejection (and SSP to MEG if projector present in file) 

done # subjects

Note: You can modify the event information in the eve.txt-files, and read the modified information later using the --events option. This is useful when you want to average with respect to trigger combinations (e.g. correct responses only) etc.

The specification file YourSpecificationFile.ave should be of the form:

average {
#       Rejection values
#
        gradReject      200e-12                # artefact rejection thresholds
        magReject       3e-12
        eegReject       120e-6
        eogReject       150e-6
        logfile         ave_logfile.txt        # log-file for some useful information
        fixSkew                                # Fixes problems with trigger onsets on different bits

#       Category specifications
#
        category {
                name    "Cond1"               # name of condition
                event   1                     # trigger/event code for condition
                tmin    -0.1                  # duration of epochs with respect to trigger
                tmax    1.0
                bmin    -0.1                  # interval for baseline correction
                bmax    0.0
        }
        category {
                name    "Cond2"
                event   2
                tmin    -0.1
                tmax    1.0
                bmin    -0.1
                bmax    0.0
        }       
}

Important: If your stimuli are delayed with respect to your triggers, you should use the "delay" option for each condition in the description file. For example, at the CBU the visual projector delays stimuli by 2 refresh rates (i.e. 34 ms), so you should add "delay 0.034".