AnalyzingData/MNE_Averaging - Meg Wiki

Upload page content

You can upload content for the page named below. If you change the page name, you can also upload content for another page. If the page name is empty, we derive the page name from the file name.

File to load page content from
Page name
Comment
Flind the wroneg tetters tin eaech wrord

Revision 20 as of 2010-12-09 19:21:06

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 [http://imaging.mrc-cbu.cam.ac.uk/meg/Maxfilter Maxfilter].

Note that MNE will not process EEG data from the CBU correctly unless you apply "mne_check_eeg_locations" to your files ([#checkeeglocs see below]).

You can [#markbadchannels 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 [#readwritesubtract read into Matlab] using fiff_read_evoked.

The following script requires a [#avgspecfile 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 \
        --projoff 

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.

Anchor(avgspecfile) The specification file YourSpecificationFile.ave should be of the form:

average {
#       Rejection values
#
        gradReject      20e-15                # artefact rejection thresholds
        magReject       3e-12
        eegReject       120e-6
        eogReject       150e-6
        logfile         ave_logfile.txt       # log-file for some useful information

#       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
        }       
}

Anchor(checkeeglocs)

Fixing EEG electrode positions

Unless you apply mne_check_eeg_locations to your data, MNE will get confused with the EEG electrode digitisation used at the CBU, and e.g. the creation of the forward solution for EEG will fail.


# 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' \
        )

#condition names as used in file names
conds=('cond1' 'cond2' 'cond3')
        

# Processing:

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

nconds=${#conds[*]}
lastcond=`expr $nconds - 1`


for m in `seq 0 ${lastsubj}`
do
  echo " "
  echo " Fixing electrodes for SUBJECT  ${subjects[m]}"
  echo " "
        for c in `seq 0 ${lastcond}`
        do
        
                mne_check_eeg_locations \
                        --file ${datapath}/${subj_pre[m]}/${subj_dir[m]}/${conds[c]}.fif \
                        --fix                           
                
        done # conditions

done # subjects

Anchor(markbadchannels)

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.

Anchor(readwritesubtract)

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