<?xml version="1.0" encoding="utf-8"?><!DOCTYPE article  PUBLIC '-//OASIS//DTD DocBook XML V4.4//EN'  'http://www.docbook.org/xml/4.4/docbookx.dtd'><article><articleinfo><title>AnalyzingData/MNE_Labels</title><revhistory><revision><revnumber>28</revnumber><date>2013-03-08 10:02:16</date><authorinitials>localhost</authorinitials><revremark>converted to 1.6 markup</revremark></revision><revision><revnumber>27</revnumber><date>2012-04-16 16:59:57</date><authorinitials>YaaraErez</authorinitials></revision><revision><revnumber>26</revnumber><date>2011-09-23 16:37:04</date><authorinitials>YaaraErez</authorinitials></revision><revision><revnumber>25</revnumber><date>2011-07-13 15:39:09</date><authorinitials>YaaraErez</authorinitials></revision><revision><revnumber>24</revnumber><date>2011-07-05 13:56:13</date><authorinitials>YaaraErez</authorinitials></revision><revision><revnumber>23</revnumber><date>2011-07-05 13:50:32</date><authorinitials>YaaraErez</authorinitials></revision><revision><revnumber>22</revnumber><date>2010-12-07 15:35:47</date><authorinitials>YaaraErez</authorinitials></revision><revision><revnumber>21</revnumber><date>2010-12-07 15:28:39</date><authorinitials>YaaraErez</authorinitials></revision><revision><revnumber>20</revnumber><date>2010-12-07 15:26:48</date><authorinitials>YaaraErez</authorinitials></revision><revision><revnumber>19</revnumber><date>2010-12-07 15:04:44</date><authorinitials>YaaraErez</authorinitials></revision><revision><revnumber>18</revnumber><date>2010-12-07 15:04:12</date><authorinitials>YaaraErez</authorinitials></revision><revision><revnumber>17</revnumber><date>2010-11-01 17:30:59</date><authorinitials>YaaraErez</authorinitials></revision><revision><revnumber>16</revnumber><date>2010-11-01 17:04:21</date><authorinitials>YaaraErez</authorinitials></revision><revision><revnumber>15</revnumber><date>2010-11-01 16:57:19</date><authorinitials>YaaraErez</authorinitials></revision><revision><revnumber>14</revnumber><date>2010-11-01 16:49:06</date><authorinitials>YaaraErez</authorinitials></revision><revision><revnumber>13</revnumber><date>2010-07-22 15:43:56</date><authorinitials>YaaraErez</authorinitials></revision><revision><revnumber>12</revnumber><date>2010-07-22 15:42:22</date><authorinitials>YaaraErez</authorinitials></revision><revision><revnumber>11</revnumber><date>2010-07-16 16:53:08</date><authorinitials>YaaraErez</authorinitials></revision><revision><revnumber>10</revnumber><date>2010-07-16 13:16:32</date><authorinitials>YaaraErez</authorinitials></revision><revision><revnumber>9</revnumber><date>2010-06-15 14:44:02</date><authorinitials>YaaraErez</authorinitials></revision><revision><revnumber>8</revnumber><date>2010-06-15 14:42:50</date><authorinitials>YaaraErez</authorinitials></revision><revision><revnumber>7</revnumber><date>2010-06-11 12:11:45</date><authorinitials>YaaraErez</authorinitials></revision><revision><revnumber>6</revnumber><date>2010-06-11 12:10:16</date><authorinitials>YaaraErez</authorinitials></revision><revision><revnumber>5</revnumber><date>2010-06-11 12:06:01</date><authorinitials>YaaraErez</authorinitials></revision><revision><revnumber>4</revnumber><date>2010-06-11 12:02:02</date><authorinitials>YaaraErez</authorinitials></revision><revision><revnumber>3</revnumber><date>2010-06-11 11:56:07</date><authorinitials>YaaraErez</authorinitials></revision><revision><revnumber>2</revnumber><date>2010-06-11 11:54:51</date><authorinitials>YaaraErez</authorinitials></revision><revision><revnumber>1</revnumber><date>2010-06-11 11:53:31</date><authorinitials>YaaraErez</authorinitials></revision></revhistory></articleinfo><section><title>Processing ROIs</title><para>ROIs in MNE are defined as &quot;label&quot; files. Freesurfer already creates a parcellation of the cortical surface in various regions of interest (see subdirectory &quot;label&quot; in your subjects' MRI directory). These can be converted to label files for MNE. You can then use <ulink url="https://imaging.mrc-cbu.cam.ac.uk/meg/AnalyzingData/MNE_Labels/meg/AnalyzingData/MNE_ComputeEstimates#">mne_make_movie</ulink> in order to extract amplitudes for these ROIs, e.g. for statistical analysis. You can also <link linkend="ownlabels">define your own ROIs</link>. You can morph ROIs between different brains (e.g. from average brain to individual brains) using mne_morph_labels. The resulting amp-files can be <link linkend="processampfiles">processed in Matlab</link>. </para><para>The parameters below are reasonable choices for standard analyses. However, these Wiki pages are not supposed to substitute the <ulink url="http://www.nmr.mgh.harvard.edu/meg/manuals/MNE-manual-2.6.pdf">MNE manual</ulink>, <ulink url="http://imaging.mrc-cbu.cam.ac.uk/meg/MEGpapers">reading papers</ulink>, and <ulink url="http://wiki.mrc-cbu.cam.ac.uk/imaging/ImagersInterestGroup#">discussions</ulink> with more experienced researchers. </para><section><title>Using Pre-defined Labels</title><para>If you've morphed all your individual STC-files to an average brain (called &quot;average&quot; in the example), you can create MNE-labels from these: </para><screen><![CDATA[mne_annot2labels --subject average --parc aparc]]></screen><para>This will result in files such as &quot;fusiform-lh.label&quot; in the MRI label subdirectory. These are text files that you can read into a text editor etc. </para><para>Then you can extract ROI information using mne_make_movie. The following script does it for multiple subjects, conditions, labels and both hemispheres. The resulting *.amp-files are text files that you can <link linkend="processampfiles">read into Excel, Matlab</link> etc. </para><screen><![CDATA[#
]]><![CDATA[
## Your variables:
]]><![CDATA[
MRIpath='/myMRIdirectory/'    # where your MRI subdirectories are
]]><![CDATA[
STCpath='/mySTCpath/'         # where your STC files are
]]><![CDATA[
outpath='/myoutputdirectory'  # where the results should go
]]><![CDATA[
]]><![CDATA[
]]><![CDATA[
#condition names as used in file names
conds=('cond1' 'cond2' 'cond3')
]]><![CDATA[
]]><![CDATA[
# subjects names used for MRI data
subjects=(\
        'Subject1' \
        'Subject2' \
        'Subject3' \
)
]]><![CDATA[
# MEG IDs (your directory structure may differ)
subj_pre=(\
        'meg10_0001' \
        'meg10_0002' \
        'meg10_0003' \
        )
]]><![CDATA[
# MEG subdirectories (your directory structure may differ)      
subj_dir=(\
         '100001' \
         '100002' \
         '100003' \
        )
]]><![CDATA[
# labels to be processed        
labels=(\
        'fusiform'\
        'inferiorparietal'\
        'parsopercularis'\
        'parsorbitalis'\
        'supramarginal'\
        'temporalpole'\
        )
        
hemispheres=(\
        'lh' \
        'rh' \
        )
        
]]><![CDATA[
# Processing:
]]><![CDATA[
nsubjects=${#subjects[*]}
lastsubj=`expr $nsubjects - 1`
]]><![CDATA[
nconds=${#conds[*]}
lastcond=`expr $nconds - 1`
]]><![CDATA[
nlabels=${#labels[*]}
lastlabel=`expr $nlabels - 1`
]]><![CDATA[
nhemispheres=${#hemispheres[*]}
lasthemisphere=`expr $nhemispheres - 1`
]]><![CDATA[
]]><![CDATA[
]]><![CDATA[
# Processing:
]]><![CDATA[
for ss in `seq 0 ${lastsubj}`
do
  echo " "
  echo " Computing movies  for SUBJECT  ${subjects[m]}"
  echo " "
        for cc in `seq 0 ${lastcond}`
        do
          echo " "
          echo " Computing movies for condition  ${conds[c]}"
          echo " "
                        for bb in `seq 0 ${lastlabel}`
                        do
                          echo " "
                          echo " Computing movies  for label  ${labels[b]}"
                          echo " "                      
                                for hh in `seq 0 ${lasthemisphere}`
                                do
                                echo " "
                                echo " Computing movies  for hemisphere  ${hemispheres[h]}"
                                echo " "        
                                                                                        
                                        mne_make_movie \
                                          --subject ${subjects[ss]} \
                                          --stcin ${STCpath}/${subj_pre[ss]}_0${subjects[ss]}_${conds[cc]}-${hemispheres[hh]}.stc \
                                          --label ${MRIpath}/average/label/${labels[bb]}-${hemispheres[hh]}.label \
                                          --labelverts \
                                          --labeltag -${subjects[ss]}_${conds[cc]}.amp \
                                          --labeloutdir ${outpath}
]]><![CDATA[
                                done # hemispheres
                                
                        done # labels                   
        
        done # conditions
]]><![CDATA[
done # subjects]]></screen><para><anchor id="ownlabels"/> </para></section><section><title>Create your own labels</title><para>If you create your own labels, the path name for the label files (--label) should be changed accordingly. </para><para>1) Display a cortical surface (and e.g. an STC file) in mne_analyze. </para><para>2) Define the borders of your ROI by defining points using your right mouse button. </para><para>3) Hold the &quot;CTRL&quot; key and click into the centre of your points with your right mouse button. </para><para>4) Save the label.  </para><para>5) Use mne_make_movie to extract information for this label. </para><para>Note that you can use mne_morph_labels to morph labels between subjects, for example to the average brain. </para><para>This <emphasis role="strong"><ulink url="https://imaging.mrc-cbu.cam.ac.uk/meg/AnalyzingData/MNE_Labels/meg/AnalyzingData/MNE_Labels?action=AttachFile&amp;do=get&amp;target=STCandROI.avi">video</ulink></emphasis> shows how to load an existing STC file and use it to define your own ROI (you may have to download the movie and play it outside your browser). </para><para><anchor id="processampfiles"/> </para></section><section><title>Processing Amp-files in Matlab</title><para>The following Matlab script reads amp-files, and averages across vertices and within latency ranges. The output will be written to an Excel file or separate text files (note: Excel files can only be created under Windows, not Linux). As usual, there is not copyright but also no guarantee... </para><screen><![CDATA[% Reads output of MNE label (ROI) analysis (i.e. amp-files)
% Computes averages across all vertices within amp-file, and for pre-defined latency ranges
% Output in Excel file (one worksheet per latency range) and text files (one file per latency range)
% output format: conditions fastest within labels
% OH, Dec 2010
]]><![CDATA[
% Latency ranges 
latranges(1,:) = [150 200];    % ms
latranges(2,:) = [400 500];    % ms
nr_rgs = size(latranges, 1);
]]><![CDATA[
baseline = [-100 0];  % baseline correction (leave empty [] if not needed)
]]><![CDATA[
AMPpath = '/PathOfAmpFiles/';  % input path for amp-files
resultpath = '/PathOfResults/'; % output path
]]><![CDATA[
resultfilestem = 'ResultFileStem';  % stem for output txt/xls-files
]]><![CDATA[
% Names of different condition names (if exist)
conds={'cond1', 'cond2'};
nr_conds = length(conds);
]]><![CDATA[
subjects={
        's1', ... 
        's2', ... 
        's3', ... 
    };
nr_sbs = length(subjects);
]]><![CDATA[
% Namels of label files (specify -lh and -rh separately)
labels = {
    'Angular-lh', ...
    'Angular-lh', ...
    'AntMidTemp-lh', ...
    'PostMidTemp-lh', ...
    };
nr_lbls = length(labels);
]]><![CDATA[
%% Reading data from amp-files; computing mean across vertices and for separate latency ranges
clear alldata;
for rr = 1:nr_lbls, % for all labels
    fprintf(1, '%s\n', labels{rr});
    
    for ss = 1:nr_sbs,  % for all subjects
        fprintf(1, '%s\n', subjects{ss});
        
        for cc = 1:nr_conds,    % for all conditions
            fprintf(1, '%s\n', conds{cc});                        
            
            amp_in_file = fullfile( AMPpath, [labels{rr} '-' subjects{ss} '_' conds{cc} '.amp'] );      
            fprintf(1, 'File: %s\n', amp_in_file);
]]><![CDATA[
            % read current amp-file
            datain = dlmread( amp_in_file );
            lats = datain(1,2:end-1);   % latencies (ms in amp-file)   
]]><![CDATA[
            data = datain(2:end,2:end-1); % actual data for all vertices in file            
]]><![CDATA[
            % only if baseline interval specified: baseline correction
            if ~isempty(baseline),   
                [tmp, b1] = min ( abs( lats-baseline(1) ) );
                [tmp, b2] = min ( abs( lats-baseline(2) ) );
                meanbase = mean( data(:,b1:b2) , 2);
                [m,n] = size(data);
                repmeanbase = repmat(meanbase, 1, n);
                data = data - repmeanbase;
            end;
]]><![CDATA[
            meandata = mean( data );    % mean across vertices
            
            for tt = 1:nr_rgs,  % averages for time ranges
                [tmp, t1] = min ( abs( lats-latranges(tt,1) ) );
                [tmp, t2] = min ( abs( lats-latranges(tt,2) ) );
                alldata(tt,cc,rr,ss) = mean( meandata(t1:t2) ); % alldata contains all data values (lat ranges, conditions, labels, subjects)
            end; % tt
]]><![CDATA[
        end; % cc
              
    end;    % ss
       
end; % rr
]]><![CDATA[
%% Output result to Excel-file and text-files
]]><![CDATA[
% File name of Excel-file that will contain the output
xls_out = fullfile( resultpath, [resultfilestem '.xls']); % Excel file, one spread sheet per latency range
]]><![CDATA[
for tt = 1:nr_rgs,  % for all latency ranges
]]><![CDATA[
        file_out = fullfile( resultpath, [resultfilestem '_' num2str(latranges(tt,1)) '_' num2str(latranges(tt,2)) '.txt']);        
        
        % reorganise data matrix for output
        for rr = 1:nr_lbls,
            for cc = 1:nr_conds,            
                index = (rr-1)*nr_conds + cc;
                outmat(:,index) = squeeze( alldata(tt,cc,rr,:) );                
                outlabel{index} = [labels{rr} '_' conds{cc}];   % text label for first line in output file (one label per column)
            end; % cc
        end; % rr
        
        % Write to different worksheets in Excel-file (one worksheet per latency range)
        xlswrite(xls_out, outmat, [num2str(latranges(tt,1)) '_' num2str(latranges(tt,2))]);
    
        % Write to text files (one file per latency range)
        fprintf(1, 'Output: %s\n', file_out);
        fid = fopen( file_out, 'w' );
        for i = 1:index,
            fprintf( fid, '%s ', outlabel{i} ); % labels in first line
        end;
        fprintf(fid, '\n');
        for i = 1:nr_sbs,
            for j = 1:index,
                fprintf(fid, '%f ', outmat(i,j));
            end;
            fprintf(fid, '\n');
        end;              
        
        fclose(fid);    
]]><![CDATA[
end; % tt]]></screen></section></section></article>