Diff for "AnalyzingData/MNE_ForwardSolution" - Meg Wiki
location: Diff for "AnalyzingData/MNE_ForwardSolution"
Differences between revisions 3 and 23 (spanning 20 versions)
Revision 3 as of 2010-06-09 15:22:59
Size: 2467
Editor: YaaraErez
Comment:
Revision 23 as of 2011-02-20 00:28:51
Size: 8516
Editor: host81-149-151-194
Comment:
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
= Forward Solution =

The following script creates the forward model, from which the inverse operator will be computed. It is essential that you have created the [http://imaging.mrc-cbu.cam.ac.uk/meg/AnalyzingData/MNE_MRI_processing source space and BEM model], and coregistered your MRIs with the MEG data.
= Forward Solution and BEM =

The following script creates the forward model and BEM, from which the inverse operator will be computed. It is essential that you have created the [http://imaging.mrc-cbu.cam.ac.uk/meg/AnalyzingData/MNE_MRI_processing source space and head surfaces, and realigned your MRI and MEG coordinate systems].

The script creates two forward solutions: One for MEG only (using only the inner skull surface), and one for combined EEG and MEG analysis (using inner skull, out skull and skin surfaces). If you only have MEG data, you can skip the latter.
The main ingredients for this step are

* the source space (cortical surface)

* the head surfaces (describing boundaries of different electrical conductivity)

* the MEG data (at this stage only sensor/electrode positions and MRI-MEG realignment)

The end product will be the forward solution file (something ending in *fwd.fif), which can be read into Matlab using mne_read_forward_solution. The BEM surfaces can be read using mne_read_bem_surfaces.

If using EEG, you may need to apply mne_check_eeg_locations to your measurement file YourMEGfile.fif.

The parameters below are reasonable choices for standard analyses. However, these Wiki pages are not supposed to substitute the [http://www.nmr.mgh.harvard.edu/meg/manuals/MNE-manual-2.6.pdf MNE manual], [http://imaging.mrc-cbu.cam.ac.uk/meg/MEGpapers reading papers], and [http://imaging.mrc-cbu.cam.ac.uk/imaging/ImagersInterestGroup discussions] with more experienced researchers.
Line 13: Line 27:
path='<myMEGdatapath>' # where your MEG fiff-files are datapath='<myMEGdatapath>' # root directory for your MEG data
Line 19: Line 34:
 'Subject1' \
 'Subject1' \
 'Subject2' \
 'Subject3' \
Line 23: Line 38:
# MEG IDs # MEG IDs (your directory structure may differ)
Line 30: Line 45:
# MEG subdirectories # MEG subdirectories (your directory structure may differ)
Line 37: Line 52:
dirbem='subject'
Line 44: Line 57:


# REPORT number of files to be processed:
Line 51: Line 61:
  echo " Computing forward & inverse solution for SUBJECT ${subjects[m]}"   echo " Computing forward solution for SUBJECT ${subjects[m]}"
Line 54: Line 64:
  subject=Subject${subjects[m]}


## setup model 3 layers (EEG+MEG)
mne_setup_forward_model --overwrite --subject ${subject} --surf --ico 4
  ## setup model 3 layers (EEG+MEG)
  mne_setup_forward_model --overwrite --subject ${subjects[m]} --surf --ico 4
Line 60: Line 67:
mne_do_forward_solution \   mne_do_forward_solution \
Line 62: Line 69:
                        --subject ${subject} \                         --subject ${subjects[m]} \
Line 65: Line 72:
                        --bem ${MRIpath}/${subject}/bem/${subject}-5120-5120-5120-bem-sol.fif \
                        --src ${MRIpath}/${subject}/bem/${subject}-5-src.fif \
                        --meas ${path}/${subj_pre[m]}/${subj_dir[m]}/MEGfile.fif \
                        --fwd ${path}/${subj_pre[m]}/${subj_dir[m]}/lex_go_5-3L-EMEG-fwd.fif



## setup model 1 layer (MEG only)
mne_setup_forward_model --overwrite --subject ${subject} --surf --homog --ico 4

mne_do_forward_solution \
                        --bem ${MRIpath}/${subjects[m]}/bem/${subjects[m]}-5120-5120-5120-bem-sol.fif \
                        --src ${MRIpath}/${subjects[m]}/bem/${subjects[m]}-5-src.fif \
                        --meas ${datapath}/${subj_pre[m]}/${subj_dir[m]}/YourMEGfile.fif \
                        --fwd ${datapath}/${subj_pre[m]}/${subj_dir[m]}/YourName_5-3L-EMEG-fwd.fif



  ## setup model 1 layer (MEG only)
  mne_setup_forward_model --overwrite --subject ${subjects[m]} --surf --homog --ico 4

  mne_do_forward_solution \
Line 77: Line 84:
                        --subject ${subject} \                         --subject ${subjects[m]} \
Line 81: Line 88:
                        --bem ${MRIpath}/${subject}/bem/${subject}-5120-bem-sol.fif \
                        --src ${MRIpath}/${subject}/bem/${subject}-5-src.fif \
                        --meas ${path}/${subj_pre[m]}/${subj_dir[m]}/MEGfile.fif \
                        --fwd ${path}/${subj_pre[m]}/${subj_dir[m]}/lex_go_5-1L-MEG-fwd.fif



done # subject loop
                        --bem ${MRIpath}/${subjects[m]}/bem/${subjects[m]}-5120-bem-sol.fif \
                        --src ${MRIpath}/${subjects[m]}/bem/${subjects[m]}-5-src.fif \
                        --meas ${datapath}/${subj_pre[m]}/${subj_dir[m]}/YourMEGfile.fif \
                        --fwd ${datapath}/${subj_pre[m]}/${subj_dir[m]}/YourName_5-1L-MEG-fwd.fif

done # subjects
Line 90: Line 95:

== Trouble shooting ==
If you notice mne fails to produce a three-layer forward model for a participant, one possibility is that the participant's outer skin, outer skull and inner skull touch one other. This will be shown in the unix standard output although it will not cause MNE to crash. The solution is to shrink the outer skull and inner skull by a few millimeter relative to the outer skin. Here is a script that does that.

1,Before running the script, you need to set the subject environment variable, e.g.
{{{
setenv SUBJECT subject_number
}}}

2,The script will create a folder called watershed3 under the directory $subject_dir/bem/. You then need to delete the original folder watershed under $subject_dir/bem/ and rename watershed3 to watershed.

3,To determine how many mm you want to shrink, you can modify the number after option -shk_br_surf in the following line of the script. The more you shrink, the more different it is from the original geometry of the participant, so try a small number and increase graduately.
{{{mri_watershed $atlas -useSRAS -shk_br_surf 2 int_h -surf $ws_dir/$SUBJECT $T1_mgz $ws_dir/ws}}}

Here is the complete script.
{{{
#!/bin/sh
#
# Create BEM surfaces using the watershed algoritm included with
# FreeSurfer
#
# Copyright 2006
#
# Matti Hamalainen
# Athinoula A. Martinos Center for Biomedical Imaging
# Massachusetts General Hospital
# Charlestown, MA, USA
#
# $Header: /space/orsay/8/users/msh/CVS/CVS-MSH/MNE/mne_scripts/mne_watershed_bem,v 1.7 2008/10/23 21:41:45 msh Exp $
# $log$
#
cleanup()
{
 echo "Temporary files removed."
}
usage()
{
 echo "usage: $0 [options]"
 echo
 echo " --overwrite (to write over existing files)"
 echo " --subject subject (defaults to SUBJECT environment variable)"
 echo " --volume name (defaults to T1)"
 echo " --atlas specify the --atlas option for mri_watershed"
 echo
 echo "Minimal invocation:"
 echo
 echo "$0 (SUBJECT environment variable set)"
 echo "$0 --subject subject (define subject on the command line)"
 echo
}
#
if [ ! "$FREESURFER_HOME" ] ; then
    echo "The FreeSurfer environment needs to be set up for this script"
    exit 1
fi
#
if [ ! "$SUBJECTS_DIR" ]
then
 echo "The environment variable SUBJECTS_DIR should be set"
 exit 1
fi
if [ ! "$MNE_ROOT" ]
then
    echo "MNE_ROOT environment variable is not set"
    exit 1
fi
force=false
atlas=
volume=T1
#
# Parse the options
#
while [ $# -gt 0 ]
do
 case "$1" in
 --subject)
  shift
  if [ $# -eq 0 ]
  then
   echo "--subject: argument required."
   exit 1
  else
   export SUBJECT=$1
  fi
  ;;
 --volume)
  shift
  if [ $# -eq 0 ]
  then
   echo "--volume: argument required."
   exit 1
  else
   export volume=$1
  fi
  ;;
 --overwrite)
  force=true
  ;;
 --atlas)
  atlas=-atlas
  ;;
 --help)
  usage
  exit 1
  ;;
 esac

 shift
done
#
# Check everything is alright
#
if [ ! "$SUBJECT" ]
then
 usage
 exit 1
fi
#
subject_dir=$SUBJECTS_DIR/$SUBJECT
mri_dir=$subject_dir/mri
T1_dir=$mri_dir/$volume
T1_mgz=$mri_dir/$volume.mgz
bem_dir=$subject_dir/bem
ws_dir=$subject_dir/bem/watershed3
#
if [ ! -d $subject_dir ]
then
 echo "Could not find the MRI data directory $subject_dir"
 exit 1
fi
if [ ! -d $bem_dir ]
then
    mkdir -p $bem_dir
    if [ $? -ne 0 ]
    then
 echo "Could not create the model directory $bem_dir"
 exit 1
    fi
fi
if [ ! -d $T1_dir -a ! -f $T1_mgz ]
then
    echo "Could not find the MRI data"
    exit 1
fi
if [ -d $ws_dir ]
then
 if [ $force = "false" ]
 then
  echo "$ws_dir already exists. Use the --overwrite option to recreate it"
  exit 1
 else
  rm -rf $ws_dir
  if [ $? -ne 0 ]
  then
   echo "Could not remove $ws_dir"
   exit 1
  fi
 fi
fi
#
# Report
#
echo
echo "Running mri_watershed for BEM segmentation with the following parameters"
echo
echo "SUBJECTS_DIR = $SUBJECTS_DIR"
echo "Subject = $SUBJECT"
echo "Result dir = $ws_dir"
echo
mkdir -p $ws_dir/ws
if [ $? -ne 0 ]
then
    echo "Could not create the destination directories"
    exit 1
fi
cleanup
if [ -f $T1_mgz ]
then
    mri_watershed $atlas -useSRAS -shk_br_surf 2 int_h -surf $ws_dir/$SUBJECT $T1_mgz $ws_dir/ws
else
    mri_watershed $atlas -useSRAS -shk_br_surf 2 int_h -surf $ws_dir/$SUBJECT $T1_dir $ws_dir/ws
fi
if [ $? -ne 0 ]
then
    exit 1
fi
#
cd $bem_dir
rm -f $SUBJECT-head.fif
mne_surf2bem --surf $ws_dir/"$SUBJECT"_outer_skin_surface --id 4 --fif $SUBJECT-head.fif
echo "Created $bem_dir/$SUBJECT-head.fif"
echo
echo "Complete."
echo
exit 0
}}}

Forward Solution and BEM

The following script creates the forward model and BEM, from which the inverse operator will be computed. It is essential that you have created the [http://imaging.mrc-cbu.cam.ac.uk/meg/AnalyzingData/MNE_MRI_processing source space and head surfaces, and realigned your MRI and MEG coordinate systems].

The script creates two forward solutions: One for MEG only (using only the inner skull surface), and one for combined EEG and MEG analysis (using inner skull, out skull and skin surfaces). If you only have MEG data, you can skip the latter. The main ingredients for this step are

* the source space (cortical surface)

* the head surfaces (describing boundaries of different electrical conductivity)

* the MEG data (at this stage only sensor/electrode positions and MRI-MEG realignment)

The end product will be the forward solution file (something ending in *fwd.fif), which can be read into Matlab using mne_read_forward_solution. The BEM surfaces can be read using mne_read_bem_surfaces.

If using EEG, you may need to apply mne_check_eeg_locations to your measurement file YourMEGfile.fif.

The parameters below are reasonable choices for standard analyses. However, these Wiki pages are not supposed to substitute the [http://www.nmr.mgh.harvard.edu/meg/manuals/MNE-manual-2.6.pdf MNE manual], [http://imaging.mrc-cbu.cam.ac.uk/meg/MEGpapers reading papers], and [http://imaging.mrc-cbu.cam.ac.uk/imaging/ImagersInterestGroup discussions] with more experienced researchers.

#

## Your variables:

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

MRIpath='/myMRIdirectory/'    # where your MRI subdirectories are

# subjects names used for MRI data
subjects=(\
        'Subject1' \
        'Subject2' \
        'Subject3' \
)

# 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=${#subjects[*]}
lastsubj=`expr $nsubjects - 1`

for m in `seq 0 ${lastsubj}`
do
  echo " "
  echo " Computing forward solution for SUBJECT  ${subjects[m]}"
  echo " "
  
  ## setup model 3 layers (EEG+MEG)
  mne_setup_forward_model --overwrite  --subject ${subjects[m]} --surf --ico 4
 
  mne_do_forward_solution \
                        --overwrite \
                        --subject ${subjects[m]} \
                        --mindist 5 \
                        --spacing 5 \
                        --bem ${MRIpath}/${subjects[m]}/bem/${subjects[m]}-5120-5120-5120-bem-sol.fif \
                        --src ${MRIpath}/${subjects[m]}/bem/${subjects[m]}-5-src.fif \
                        --meas ${datapath}/${subj_pre[m]}/${subj_dir[m]}/YourMEGfile.fif  \
                        --fwd ${datapath}/${subj_pre[m]}/${subj_dir[m]}/YourName_5-3L-EMEG-fwd.fif



  ## setup model 1 layer (MEG only)
  mne_setup_forward_model --overwrite  --subject ${subjects[m]} --surf --homog --ico 4

  mne_do_forward_solution \
                        --overwrite \
                        --subject ${subjects[m]} \
                        --mindist 5 \
                        --spacing 5 \
                        --megonly \
                        --bem ${MRIpath}/${subjects[m]}/bem/${subjects[m]}-5120-bem-sol.fif \
                        --src ${MRIpath}/${subjects[m]}/bem/${subjects[m]}-5-src.fif \
                        --meas ${datapath}/${subj_pre[m]}/${subj_dir[m]}/YourMEGfile.fif \
                        --fwd ${datapath}/${subj_pre[m]}/${subj_dir[m]}/YourName_5-1L-MEG-fwd.fif

done # subjects

Trouble shooting

If you notice mne fails to produce a three-layer forward model for a participant, one possibility is that the participant's outer skin, outer skull and inner skull touch one other. This will be shown in the unix standard output although it will not cause MNE to crash. The solution is to shrink the outer skull and inner skull by a few millimeter relative to the outer skin. Here is a script that does that.

1,Before running the script, you need to set the subject environment variable, e.g.

setenv SUBJECT subject_number

2,The script will create a folder called watershed3 under the directory $subject_dir/bem/. You then need to delete the original folder watershed under $subject_dir/bem/ and rename watershed3 to watershed.

3,To determine how many mm you want to shrink, you can modify the number after option -shk_br_surf in the following line of the script. The more you shrink, the more different it is from the original geometry of the participant, so try a small number and increase graduately. mri_watershed $atlas -useSRAS -shk_br_surf 2 int_h -surf $ws_dir/$SUBJECT $T1_mgz $ws_dir/ws

Here is the complete script.

#
#       Create BEM surfaces using the watershed algoritm included with
#       FreeSurfer
#
#       Copyright 2006
#
#       Matti Hamalainen
#       Athinoula A. Martinos Center for Biomedical Imaging
#       Massachusetts General Hospital
#       Charlestown, MA, USA
#
#       $Header: /space/orsay/8/users/msh/CVS/CVS-MSH/MNE/mne_scripts/mne_watershed_bem,v 1.7 2008/10/23 21:41:45 msh Exp $
#       $log$
#
cleanup()
{
        echo "Temporary files removed."
}
usage()
{
        echo "usage: $0 [options]"
        echo 
        echo "     --overwrite             (to write over existing files)"
        echo "     --subject subject       (defaults to SUBJECT environment variable)"
        echo "     --volume  name          (defaults to T1)"
        echo "     --atlas                 specify the --atlas option for mri_watershed"
        echo
        echo "Minimal invocation:"
        echo
        echo "$0                      (SUBJECT environment variable set)"
        echo "$0 --subject subject    (define subject on the command line)"
        echo 
}
#
if [ ! "$FREESURFER_HOME" ] ; then 
    echo "The FreeSurfer environment needs to be set up for this script"
    exit 1
fi
#
if [ ! "$SUBJECTS_DIR" ]
then
        echo "The environment variable SUBJECTS_DIR should be set"
        exit 1
fi
if [ ! "$MNE_ROOT" ]
then
    echo "MNE_ROOT environment variable is not set"
    exit 1
fi
force=false
atlas=
volume=T1
#
#       Parse the options
#
while [ $# -gt 0 ]
do
        case "$1" in  
        --subject) 
                shift
                if [ $# -eq 0 ]
                then
                        echo "--subject: argument required."
                        exit 1
                else
                        export SUBJECT=$1
                fi
                ;;
        --volume) 
                shift
                if [ $# -eq 0 ]
                then
                        echo "--volume: argument required."
                        exit 1
                else
                        export volume=$1
                fi
                ;;
        --overwrite)
                force=true
                ;;
        --atlas)
                atlas=-atlas
                ;;
        --help)
                usage
                exit 1
                ;;
        esac

        shift
done
#
#       Check everything is alright
#
if [ ! "$SUBJECT" ]
then
        usage
        exit 1
fi
#
subject_dir=$SUBJECTS_DIR/$SUBJECT
mri_dir=$subject_dir/mri
T1_dir=$mri_dir/$volume
T1_mgz=$mri_dir/$volume.mgz
bem_dir=$subject_dir/bem
ws_dir=$subject_dir/bem/watershed3
#
if [ ! -d $subject_dir ]
then 
        echo "Could not find the MRI data directory $subject_dir"
        exit 1
fi
if [ ! -d $bem_dir ]
then 
    mkdir -p $bem_dir
    if [ $? -ne 0 ]
    then
        echo "Could not create the model directory $bem_dir"
        exit 1
    fi
fi
if [ ! -d $T1_dir -a ! -f $T1_mgz ]
then 
    echo "Could not find the MRI data"
    exit 1
fi
if [ -d $ws_dir ]
then
        if [ $force = "false" ]
        then
                echo "$ws_dir already exists. Use the --overwrite option to recreate it"
                exit 1
        else
                rm -rf $ws_dir
                if [ $? -ne 0 ]
                then 
                        echo "Could not remove $ws_dir"
                        exit 1
                fi
        fi
fi
#
#       Report
#
echo 
echo "Running mri_watershed for BEM segmentation with the following parameters"
echo
echo "SUBJECTS_DIR = $SUBJECTS_DIR"
echo "Subject      = $SUBJECT"
echo "Result dir   = $ws_dir"
echo
mkdir -p $ws_dir/ws
if [ $? -ne 0 ]
then 
    echo "Could not create the destination directories"
    exit 1
fi
cleanup
if [ -f $T1_mgz ]
then
    mri_watershed $atlas -useSRAS -shk_br_surf 2 int_h -surf $ws_dir/$SUBJECT $T1_mgz $ws_dir/ws
else
    mri_watershed $atlas -useSRAS -shk_br_surf 2 int_h -surf $ws_dir/$SUBJECT $T1_dir $ws_dir/ws
fi    
if [ $? -ne 0 ]
then
    exit 1
fi
#
cd $bem_dir
rm -f $SUBJECT-head.fif
mne_surf2bem --surf $ws_dir/"$SUBJECT"_outer_skin_surface --id 4 --fif $SUBJECT-head.fif
echo "Created $bem_dir/$SUBJECT-head.fif"
echo
echo "Complete."
echo
exit 0

CbuMeg: AnalyzingData/MNE_ForwardSolution (last edited 2013-03-08 10:02:43 by localhost)