146
Comment:
|
9814
|
Deletions are marked like this. | Additions are marked like this. |
Line 1: | Line 1: |
'''Thit toolbox allows you to do support vector machine (SVM) and linear discriminant analysis (LDA) classification on the MEG timeseries. ''' | = Tutorial 13: Decoding Conditions = ''Author: Seyed-Mahdi Khaligh-Razavi'' |
Line 3: | Line 4: |
These set of functions allow you to do support vector machine (SVM) and linear discriminant analysis (LDA) classification on your MEG data across time. <<TableOfContents(2,2)>> '''Description of the MEG data ''' The data contains the first 15 minutes of MEG recording for one of the subjects in the following study. The study was conducted according to the Declaration of Helsinki and approved by the local ethics committee (Institutional Review Board of the Massachusetts Institute of Technology). Informed consent was obtained from all participants. During MEG recordings participants completed an orthogonal image categorization task. Participants saw 720 stimuli (360 faces, 360 scenes) centrally-presented ( {{file://localhost/Users/skhaligh/Library/Caches/TemporaryItems/msoclip/0/clip_image001.png||width="83",height="16",id="_x0000_i1025"}} ) for 0.5 second at a time and had to decide (without responding) if scenes were indoor / outdoor and faces were male / female. Every 2 to 4 trials (randomly determined), a question mark would appear on the screen for 1 second. At this time, participants were asked to press a button to indicate the category of the last image (male/female, indoor/outdoor), and they were also allowed to blink/swallow. This task was designed to ensure participants were attentive to the images without explicitly encoding them into memory. Participants only responded during the “question mark” trials so that activity during stimulus perception was not contaminated by motor activity. During the task, participants were asked to fixate on a cross centered on the screen and their eye movements were tracked, to ensure any results were not due to differential eye movement patterns. The whole experiment was divided into 16 runs. Each run contained 45 randomly selected images (each of them shown twice per run—not in succession), resulting in a total experiment time of about 50 min. The MEG data that is included here for demonstration purposes contains only the first 15 minutes of the whole session, during which 53 faces and 54 scenes have been observed. '''Description of the decoding functions'''''' '''These set of functions allow you to do support vector machine (SVM) and linear discriminant analysis (LDA) classification on your MEG data across time.Input: the input is channel data from two conditions (e.g. condA and condB) across time. Number of samples per condition should be the same for both condA and condB. Each of them should at least contain two samples.Output: the output is a decoding curve across time, showing your decoding accuracy (decoding condA vs. condB) at timepoint 't'. We have two condition types: faces, and scenes. We want to decode faces vs. scenes using 306 MEG channels. In the data, the faces are named as condition ‘201’; and the scenes are named as condition ‘203’. '''Making protocol and loading the data''' Go to the file menu and create a new protocol called ''faces_vs_scenes ''with the options shown in the following figure.[[attachment:/Users/skhaligh/Dropbox (MIT)/^projects/MINT/doc figures/1-prot.png]] {{attachment:1-prot.png}} After making your protocol, go to the functional data view (sorted by subjects). Then create a new subject ''(let's keep the default name: Subject01)'': to create a new subject right click on the protocol name; then press new subject, choose the default values shown in the following figure; press save. {{file://localhost/Users/skhaligh/Library/Caches/TemporaryItems/msoclip/0/clip_image005.png||width="252",height="230",v:shapes="Picture_x0020_2"}} '''Import MEG data''' 1) Right click on the subject node (Subject01), and select ‘''review raw file’.''2) Change the file format to ‘''MEG/EEG: Neuromag FIF (*.fif)''’; then select the .fif file (''mem6.fif'') you downloaded.3) Press event channels {{file://localhost/Users/skhaligh/Library/Caches/TemporaryItems/msoclip/0/clip_image007.png||width="274",height="119",v:shapes="Picture_x0020_3"}} 4) In the ‘''mem6-0 tsss mc’'' node right click on ‘''Link to raw file''’; select ‘Import in database’.5) Set epoch time to [-100, 1000]6) Time range: [-100,0]7) From the events select only these two events: 201 (faces), and 203(scenes) {{file://localhost/Users/skhaligh/Library/Caches/TemporaryItems/msoclip/0/clip_image009.png||width="461",height="226",v:shapes="_x0000_i1034"}} 8) Then press import. You will get a message saying ‘some epochs are shorter than the others..’ . Press yes. '''Decoding conditions''' 1) Select ‘Process2’ from the bottom brainstorm window. 2) Drag and drop 40 files from folder ‘201’ to ‘Files A’; and 40 files from folder ‘203’ to ‘Files B’. You can select more than 40 or less. The important thing is that both ‘A’ and ‘B’ should have the same number of files. {{file://localhost/Users/skhaligh/Library/Caches/TemporaryItems/msoclip/0/clip_image011.png||width="492",height="344",v:shapes="Picture_x0020_1"}} '''a) Cross-validation ''' '''Cross'''-'''validation''' is a model '''validation''' technique for assessing how the results of our decoding analysis will generalize to an independent data set. 1) Select run -> decoding conditions -> classification with cross validation {{file://localhost/Users/skhaligh/Library/Caches/TemporaryItems/msoclip/0/clip_image013.png||width="434",height="204",v:shapes="Picture_x0020_8"}} Here, you will have three choices for cross-validation. If you have Matlab Statistics and Machine Learning Toolbox, you can use ‘Matlab SVM’ or ‘Matlab LDA’. You can also install the ‘LibSVM’ toolbox (https://www.csie.ntu.edu.tw/~cjlin/libsvm/ ); and addpath it to your Matlab session. LibSVM may be faster. The LibSVM cross-validation won’t be stratified. However, if you select Matlab SVM/LDA, it will do a k-fold stratified cross-validation for you, meaning that each fold will contain the same proportions of the two types of class labels. You can also set the number of folds for cross-validation; and the cut-off frequency for low-pass filtering – this is to smooth your data. 2) To continue, set the values as shown below: {{file://localhost/Users/skhaligh/Library/Caches/TemporaryItems/msoclip/0/clip_image015.png||height="272",width="267",v:shapes="Picture_x0020_9",border="0"}} ''' '''''' '''3) The process will take some time.The results are then saved in a file (‘Matlab SVM Decoding_201_203’) under the new ‘decoding’ node. If you double click on it you will see a decoding curve across time (shown below). {{file://localhost/Users/skhaligh/Library/Caches/TemporaryItems/msoclip/0/clip_image017.png||height="366",width="434",v:shapes="Picture_x0020_4",border="0"}} {{file://localhost/Users/skhaligh/Library/Caches/TemporaryItems/msoclip/0/clip_image019.png||height="202",width="390",v:shapes="Picture_x0020_10",border="0"}} The internal brainstorm plot is not perfect for this purpose. To make a proper plot you can plot the results yourself. Right click on the result file (Matlab SVM Decoding_201_203), > File -> export to Matlab. Give it a name ‘decodingcurve’. Then using Matlab plot function you can plot the decoding accuracies across time. ''Matlab code: ''''-figure; ''''-plot(decodingcurve.Value); '''' '' {{file://localhost/Users/skhaligh/Library/Caches/TemporaryItems/msoclip/0/clip_image021.png||height="257",width="290",v:shapes="Picture_x0020_12",border="0"}} ''' ''''''b) Permutation''' This is an iterative procedure. The training and test data for the SVM/LDA classifier are selected in each iteration by randomly permuting the samples and grouping them into bins of size ''n'' (you can select the trial bin sizes). In each iteration two samples (one from each condition) are left out for test. The rest of the data are used to train the classifier with. 1) Select run -> decoding conditions -> classification with permutation 2) Set the values as the following: {{file://localhost/Users/skhaligh/Library/Caches/TemporaryItems/msoclip/0/clip_image023.png||height="325",width="299",v:shapes="Picture_x0020_14",border="0"}} If the ‘Trial bin size’ is greater than 1, the training data will be randomly grouped into bins of the size you determine here. The samples within each bin are then averaged (we refer to this as sub-averaging); the classifier is then trained using the averaged samples. For example, if you have 40 faces and 40 scenes, and you set the trial bin size to 5; then for each condition you will have 8 bins each containing 5 samples. 7 bins from each condition will be used for training, and the two left-out bins (one face bin, one scene bin) will be used for testing the classifier performance. 3) The results are saved under the ‘decoding ’ node . If you selected ‘Matlab SVM’, the file name will be : ‘Matlab SVM Decoding-permutation_201_203’ . Double click on it. You will get the plot blow: {{file://localhost/Users/skhaligh/Library/Caches/TemporaryItems/msoclip/0/clip_image025.png||height="204",width="434",v:shapes="Picture_x0020_16",border="0"}} This might not be very intuitive. You can export the decoding results into Matlab and plot it yourself. If you export the decoding results into Matlab, the imported structure will have two important fields: a) Value: this is the mean decoding accuracy across all permutations b) Std: this is the standard deviation across all permutations. If you plot the mean value (decodingcurve.Value), below is what you will get. You also have access to the standard deviation (decodingcurve.Std), in case you want to plot it. {{file://localhost/Users/skhaligh/Library/Caches/TemporaryItems/msoclip/0/clip_image027.png||height="288",width="362",v:shapes="Picture_x0020_17",border="0"}} |
Tutorial 13: Decoding Conditions
Author: Seyed-Mahdi Khaligh-Razavi
These set of functions allow you to do support vector machine (SVM) and linear discriminant analysis (LDA) classification on your MEG data across time.
Contents
Description of the MEG data The data contains the first 15 minutes of MEG recording for one of the subjects in the following study. The study was conducted according to the Declaration of Helsinki and approved by the local ethics committee (Institutional Review Board of the Massachusetts Institute of Technology). Informed consent was obtained from all participants. During MEG recordings participants completed an orthogonal image categorization task. Participants saw 720 stimuli (360 faces, 360 scenes) centrally-presented ( ) for 0.5 second at a time and had to decide (without responding) if scenes were indoor / outdoor and faces were male / female. Every 2 to 4 trials (randomly determined), a question mark would appear on the screen for 1 second. At this time, participants were asked to press a button to indicate the category of the last image (male/female, indoor/outdoor), and they were also allowed to blink/swallow. This task was designed to ensure participants were attentive to the images without explicitly encoding them into memory. Participants only responded during the “question mark” trials so that activity during stimulus perception was not contaminated by motor activity. During the task, participants were asked to fixate on a cross centered on the screen and their eye movements were tracked, to ensure any results were not due to differential eye movement patterns. The whole experiment was divided into 16 runs. Each run contained 45 randomly selected images (each of them shown twice per run—not in succession), resulting in a total experiment time of about 50 min. The MEG data that is included here for demonstration purposes contains only the first 15 minutes of the whole session, during which 53 faces and 54 scenes have been observed. Description of the decoding functions These set of functions allow you to do support vector machine (SVM) and linear discriminant analysis (LDA) classification on your MEG data across time.Input: the input is channel data from two conditions (e.g. condA and condB) across time. Number of samples per condition should be the same for both condA and condB. Each of them should at least contain two samples.Output: the output is a decoding curve across time, showing your decoding accuracy (decoding condA vs. condB) at timepoint 't'. We have two condition types: faces, and scenes. We want to decode faces vs. scenes using 306 MEG channels. In the data, the faces are named as condition ‘201’; and the scenes are named as condition ‘203’. Making protocol and loading the data Go to the file menu and create a new protocol called faces_vs_scenes with the options shown in the following figure./Users/skhaligh/Dropbox (MIT)/^projects/MINT/doc figures/1-prot.png After making your protocol, go to the functional data view (sorted by subjects). Then create a new subject (let's keep the default name: Subject01): to create a new subject right click on the protocol name; then press new subject, choose the default values shown in the following figure; press save. Import MEG data 1) Right click on the subject node (Subject01), and select ‘review raw file’.2) Change the file format to ‘MEG/EEG: Neuromag FIF (*.fif)’; then select the .fif file (mem6.fif) you downloaded.3) Press event channels 4) In the ‘mem6-0 tsss mc’ node right click on ‘Link to raw file’; select ‘Import in database’.5) Set epoch time to [-100, 1000]6) Time range: [-100,0]7) From the events select only these two events: 201 (faces), and 203(scenes) 8) Then press import. You will get a message saying ‘some epochs are shorter than the others..’ . Press yes. Decoding conditions 1) Select ‘Process2’ from the bottom brainstorm window. 2) Drag and drop 40 files from folder ‘201’ to ‘Files A’; and 40 files from folder ‘203’ to ‘Files B’. You can select more than 40 or less. The important thing is that both ‘A’ and ‘B’ should have the same number of files. a) Cross-validation Cross-validation is a model validation technique for assessing how the results of our decoding analysis will generalize to an independent data set. 1) Select run -> decoding conditions -> classification with cross validation Here, you will have three choices for cross-validation. If you have Matlab Statistics and Machine Learning Toolbox, you can use ‘Matlab SVM’ or ‘Matlab LDA’. You can also install the ‘LibSVM’ toolbox (https://www.csie.ntu.edu.tw/~cjlin/libsvm/ ); and addpath it to your Matlab session. LibSVM may be faster. The LibSVM cross-validation won’t be stratified. However, if you select Matlab SVM/LDA, it will do a k-fold stratified cross-validation for you, meaning that each fold will contain the same proportions of the two types of class labels. You can also set the number of folds for cross-validation; and the cut-off frequency for low-pass filtering – this is to smooth your data. 2) To continue, set the values as shown below: 3) The process will take some time.The results are then saved in a file (‘Matlab SVM Decoding_201_203’) under the new ‘decoding’ node. If you double click on it you will see a decoding curve across time (shown below). The internal brainstorm plot is not perfect for this purpose. To make a proper plot you can plot the results yourself. Right click on the result file (Matlab SVM Decoding_201_203), > File -> export to Matlab. Give it a name ‘decodingcurve’. Then using Matlab plot function you can plot the decoding accuracies across time. Matlab code: '-figure; '-plot(decodingcurve.Value); ' b) Permutation This is an iterative procedure. The training and test data for the SVM/LDA classifier are selected in each iteration by randomly permuting the samples and grouping them into bins of size n (you can select the trial bin sizes). In each iteration two samples (one from each condition) are left out for test. The rest of the data are used to train the classifier with. 1) Select run -> decoding conditions -> classification with permutation 2) Set the values as the following: If the ‘Trial bin size’ is greater than 1, the training data will be randomly grouped into bins of the size you determine here. The samples within each bin are then averaged (we refer to this as sub-averaging); the classifier is then trained using the averaged samples. For example, if you have 40 faces and 40 scenes, and you set the trial bin size to 5; then for each condition you will have 8 bins each containing 5 samples. 7 bins from each condition will be used for training, and the two left-out bins (one face bin, one scene bin) will be used for testing the classifier performance. 3) The results are saved under the ‘decoding ’ node . If you selected ‘Matlab SVM’, the file name will be : ‘Matlab SVM Decoding-permutation_201_203’ . Double click on it. You will get the plot blow: This might not be very intuitive. You can export the decoding results into Matlab and plot it yourself. If you export the decoding results into Matlab, the imported structure will have two important fields: a) Value: this is the mean decoding accuracy across all permutations b) Std: this is the standard deviation across all permutations. If you plot the mean value (decodingcurve.Value), below is what you will get. You also have access to the standard deviation (decodingcurve.Std), in case you want to plot it.