AnalyzingData/MNE_Averaging - Meg Wiki

Please enter your password of your account at the remote wiki below.
/!\ You should trust both wikis because the password could be read by the particular administrators.

Clear message
location: AnalyzingData / MNE_Averaging

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".

Marking Bad Channels

You can mark channels already for raw MEG data files - these channels will then be excluded from artefact rejection during averaging.

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

mne_mark_bad_channels \
        --bad yoursubjectbads.txt \      # File with bad channel information
        ${datapath}/YourSubjectDir/YourSubjectFile1.fif \
        ${datapath}/YourSubjectDir/YourSubjectFile2.fif \
        ${datapath}/YourSubjectDir/YourSubjectFile3.fif \

where yoursubjectbads.txt could look like this

EEG001
EEG044

if EEG channels 1 and 44 are bad. I had problems when I created these text files in Windows - if in doubt, open the file under Linux (e.g. using nedit), use "save as" and make sure it's in Linux (not Dos) format. Or use the "dos2unix" command.

Reading, Writing, Subtracting Fiff-files

This is a simple example of how you can read and write fiff-files, e.g. for subtracting conditions etc. Note that if you apply an inverse operator with the --dSPM option to subtracted data, you may have to adjust the number of effective averages (see manual).

data1 = fiff_read_evoked( fiff_file1 );  % Read file 1
data2 = fiff_read_evoked( fiff_file2 );  % Read file 2
        
data3 = data2;  % data3 will contain the subtracted data, plus other information from file 2

data3.evoked.epochs = data2.evoked.epochs - data1.evoked.epochs;  % subtract file 1 from file 2

fiff_write_evoked(out_fiff_file, data3);  % write subtracted data to fiff-file  

Re-referencing EEG data

If you are using EEG data, you need to make sure you are using an appropriate reference montage. At the CBSU, we use a nose electrode as recording reference, but this will not be ideal for most further analyses. For high-density EEG recordings, average reference is recommended. The reference matters when you compute statistics on your EEG data, or for visualisation. I does not matter when you compute the inverse operator (MNE takes care of this).

You can enforce average-referencing of your data using the option "--projon" in your averaging script (see above, and manual). You should only do this if you are sure that there are no bad EEG channels any more, otherwise their bad signals will be projected into all other channels. If you want to keep the recording reference, and apply average-reference later in Matlab, these little lines may be helpful:

nr_chan = 70;

avgref_op = eye(nr_chan) - ones(nr_chan)/nr_chan;

avg_data = avgref_op*your_data;

avg_data is now your_data with the average reference from each column. You just need to figure our where in your data structure the EEG data are :)