SpinozaWiki:Logfile Conversion
From SpinozaWiki
The concept of the behavstruct
A behavStruct file is a matlab file in which information related to paradigm presentation and subject responses can be imported, manipulted and subsequently exported to for instance FSL or BrainVoyager.
A behavStruct file can have the following fields
stimStruct.ppName name of the subject stimStruct.dateOfExperiment date of the experiment stimStruct.nameOfExperiment name of the experiment stimStruct.stimuli.time an array with the onset time of all the stimuli stimStruct.stimuli.event an array with the eventCode of the stimuli (obtained from the conditionNames and nameCodes fields). stimStruct.stimuli.duration an array with the duration of each stimulus. stimStruct.stimuli.oriCodes an array with the original code of each stimulus stimStruct.stimuli.conditionNames an array with the conditionNames (equal to conditionNames specified in readpresentation). stimStruct.response.time an array with the onset time of all the responses. stimStruct.response.event an array with the code for all the responses. stimStruct.response.duration an array with the duration of each response (optional). stimStruct.eye a (planned) array which contains all eyetrack related data.
The different fields of the stimuli, response and eye subStructures are related by means of their index number, e.g. array element 4 is an event. The eventnumber is located in stimStruct.stimuli.event(4), the event onset is located in stimStruct.stimuli.time(4) and the event duration is located in stimStruct.stimuli. duration (4)
Importing logfiles
The syntax for readpresentation is
behavStruct = readPresentation(filename,conditionNames,nameCodes,pulseTime);
Filename is the name of the input file. If no file is specified (‘’) a file with the extension log (if it exists) is imported.
ConditionNames is a cellarray in which the different conditionNames are encoded. If the conditions are ibfig, ibhom, nafig, nahom, atfig and athom the conditions would become:
conditionNames{1} = ‘ibfig’ conditionNames{2} = ‘ibhom’ conditionNames{3} = ‘nafig’ conditionNames{4} = ‘nahom’
NameCodes is a cellarray in which it is encoded which condition numbers belong to which condition name. NameCodes{1} = [2,6]; NameCodes{2} = [3,7]; NameCodes{3} = [4,8]; NameCodes{4} = [5,9];
This recodes event 2 and 6 from presentation into event 1 in behavStruct (the oriCodes remain there numbering), 3 and 7 into a 2, 4 and 8 into and 4.
Changing of properties
The syntax for recodeBehavioral, with which you can recode different codes, is:
behavStruct = recodeBehavioral(behavStruct,from,to,type)
You can use this function to recode a condition or response code present in a behavStruct file. behavStruct is the matlab structure that needs to be changed from is the code that needs to be changed to is the code will be changed into. type specifies whether an event or response will be recoded. Specify 1 to recode an event (default, can be omitted) or 2 to recode a response.
Example: behavStruct = recodebehavioral (behavStruct, 7, 1);
Removing conditions
The syntax for this function is
behavStruct = removeCondition(behavStruct,conNum);
The function removes from behavStruct the condition with the number conNum. This relates to that number in conditionNames.
Generating bfsl files (for fsl)
The syntax for this function is
BehavioralToFSL(behavStruct,durationSingle,generateCorrect,generateSingleTrials);
With only the behavStruct as an argument it will generate 3 column bfsl files which can be used by presentation. You can also specifiy the durection as the events, otherwise the duration specified in readpresentation will be used.
If generateCorrect is specified as 1 the function will create seperate predictors for correct and incorrect. For this analyseBehavioral should be used before using this option.
Generating Single Trials
If a fourth argument is specified (with for instance a 1) a bfsl file is made for each of the trials present in the logfile. In this case the names of the bfsl files are determined by the event-code and it will not be necessary to specify the nameCodes and ConditionNames in readPresentation.
Generating rtc files (for brainvoyager)
The syntax for this function is
rtcStruct = generatePredictor(behavStruct,TR,gaussian)
behavStruct this function will generate a predictor for each of the conditions in the behavStruct file. TR must be specified if this function is not executed in a directory with the ScanInfo.mat that belongs to this run. Can me omitted otherwise. It is highly advised to only run this file with ScanInfo present. It will be used to shorten or lengthen the predictor file if the run does not equal the size of the presentation. Gaussian if the data is smoothed with a Gaussian it might be necessary to also smooth the predictor file.
Example: behavStruct = GeneratePredictor (behavStruct);
This function generates a RTC file.
Generating prt files (for brainvoyager)
The syntax for this function is
BehavioralToBV(behavStruct,blocked,TR,nDynamics)
behavStruct this function create a protocol file with all the conditions in the behavStruct file. Blocked if blocked is 1 this function will create a predictor for a blocked design (stimuli are condidered to last an entire dynamic) with a msec resolution. If blocked is 3 this function will generate a volume based protocol file. If blocked is 0 or omitted an event-related paradigm is generated. TR, time of repetition in msec. Can be omitted if ScanInfo.mat is present nDynamics must be specified if ScanInfo.mat is not present.
This function generates a PRT file.
Example: BehavioralToBV(behavStruct,0);
Batchscripting Logfile conversion for FSL
The LogFileCrawler function contains 2 parts. The first part, under function LogFileCrawler enters all the directories in the MRI_Data folder and checks whether that directory contains any fMRI data. If this is the case the function jumps to the subfunction StuffToDoThere. Here the log file is read into Matlab by the function readPresentation. At the same time the codes in this file are given a name via the variables names and nameCodes (these can also be used to recode the numbers but we do not use that option here). Finally the function BehavioralToFSL is used to convert the Matlab representation of the codes into files that FSL can read (.bfsl). Make sure no underscores are used as condition names! This is important for batching the rest of the files. LogFileCrawler must be run from the experiment root directory - as is the case with most scripts we will be using.
Before adapting LogFileCrawler.m for our own use, we have saved it as a new file called LogFileCrawlerEmoBeeld.m in the mridata folder of your analysis (the folder created by ProcessParrec2).
Example:
The following is edited into the second part of LogFileCrawlerEmoBeeld. In the example the presentation codes 1 and 2 are grouped together etc. names{1} = 'EvoFear'; names{2} = 'NonEvoFear'; names{3} = 'Neutral'; names{4} = 'Positive'; nameCodes{1} = [1,2]; nameCodes{2} = [3,4]; nameCodes{3} = [5,6]; nameCodes{4} = [6,7];
After editing the function you can execute it in the directory MRIData with the command
LogFileCrawler
Now for each BOLD-MRI directory four files are generated (in this example and for subject pp0003)
pp0003_Evo.bfsl pp0003_Neutral.bfsl pp0003_NonEvo.bfsl pp0003_Positive.bfsl
The following table is an excerpt from the first .bfsl file. The first column determines the stimulus (or behaviour) event, the second the duration and in the third column optional weighting factors can be entered. All values are in seconds.
Table 1. Excerpt from chpp03_EvoFearAll.bfsl 64.8540 1.0000 1.0000 67.5405 1.0010 1.0000 70.2440 1.0010 1.0000 72.9466 1.0010 1.0000 75.6501 1.0010 1.0000
If you want to delete all previously generated bfsl files and generate new files execute the function like this
LogFileCrawler(1);
Behavioural Analyses
The easiest way to analyze behavioral data logged by presentation is by using the following function:
[[function [behavScore,behavStruct] = analyseBehavioral(behavStruct,correctCodes,minTime,maxTime,responseNumber);]]
A smart thing to do is to generate a batchscript where you define all parameters. A short version of such a script is as follows:
names{1} = 'Pos'; You can name the condition yourself
names{2} = 'Neg';
nameCodes{1} = [1]; Here you enter the codes you used in Presentation
nameCodes{2} = [2];
correctCodes{1} = 2; Here you define the correct response
correctCodes{2} = 1;
behavStruct = readpresentation2(Pp1_Block1.log',names,nameCodes);
This will generate the behavStruct you need. Now Paste the function above. If you recorded multiple blocks then continue:
behavStruct = readpresentation2(Pp1_Block#.log',names,nameCodes);
Then the function again, but name behavScore different, e.g. behavScore2.
outputBehav = CombineBehavscores(behavScore,behavScore2,etc.)
Paste this function to combine the blocks.
If you want to order you data to make it SPSS compatible you can define matrices containing the information you want:
A = [outputBehav{1,1}.mRTCorrect,outputBehav{1,1}.stdCorrect,outputBehav{1,1}.pc,outputBehav{1,1}.mRTInCorrect,outputBehav{1,1}.stdInCorrect,outputBehav{1,1}.pcI,length(outputBehav{1,1}.correct)];
You can paste this information in an excel sheet using the xlswrite function:
xlswrite('Data', A ,'Test', 'A1');
There are millions of possibilities in handling your data. Have fun with it!
Before running this script make sure you navigate to the right directory that contains the logfiles.