Statistical information on events

Hi

My EEG experiment is a memory task. This involves presentation of a stimulus, then presentation of a second stimulus and a right/wrong response on whether the two stimuli matched over 100 trials. There are four event markers (2 for the stimuli presentations and two for right or wrong responses). Is it possible using coding to extract from Brainstorm the number of right/wrong responses for each individual into a spreadsheet in Excel (i.e. the number of a particular event)? Also is it possible to extract the time (in milliseconds) from the presentation of the second stimulus until the response (i.e. the time between two markers. But the second marker is either of two, right or wrong)?

Hello David!

To achieve what you want, I believe you will have to play a bit in Matlab. It's not too difficult, so I think I can walk you through the steps.

If you export your Raw file to Matlab, the events should be saved within raw.F.events.

  1. By using the length function in Matlab, you can than extract the amount of occurrence of each event. You can use something like length(myRaw.F.Events(myEventIndex).times where myRaw will be the name you will give to your Raw File when you export it to Matlab and myEventIndex the index of your desired event.

  2. As it turns out, the exact timing of your stims are stored exactly there too. myRaw.F.Events(myEventIndex).times is a vector with all those values.

You can then create a little Matlab script that would create an array like you would like it to be in excel. Finally, use the writetable or writematrix functions in Matlab to save your file as an xlsx.

Depending of your level of comfort in programming this might be anywhere from easy or difficult to understand. Let me know if you need any clarification.

You can use the process Events > Combine stim/response to recombine/match some specific stimuli with some specific responses (and create "correct" and "error" events?). Read the help directly in the process options. There are other recombination processes you might try.

Also is it possible to extract the time (in milliseconds) from the presentation of the second stimulus until the response

If you right-click on the "link to raw file" > File > Export to Matlab, you'd get access a direct access to the events structure. If you have the same number of events in the two categories "second_stim" and "response", you can subtract the "times" value of the two directly to get the response time.
To get matching event categories you can directly subtract, use grouping/combining processes.

The structures of the events is explained here:
https://neuroimage.usc.edu/brainstorm/Tutorials/EventMarkers#On_the_hard_drive

1 Like

Hi Francois and Jonathan,

This relates to my query above. I have EEG recordings from 59 subjects. I would like to count the number of a type of event (number of correct responses) in each file. My approach was to try and export my resampled 'link to raw file' to Matlab using File > Export to MATLAB and then run the following code;

n = numel(TestSubjectEvents.F.events(x).times)
(x is the row with the event of interest)

This worked but I'm trying to do it for multiple subjects at once. Is there a way to export multiple files simultaneously from Brainstorm to Matlab? I couldn't find a way to do this on the forum.

If you select multiple files, right-click > File > Export to Matlab, it would create multiple variables in your workspace (eg. a1, a2, a3...). It is after that not so easy to automated the counting.

You could also write a script to get all the raw files from the database, and then a loop on these files to count the events more efficiently:

Hi Francois,

I tried the script below. I get a 1x59 (for 59 subjects) struct with 11 fields (picture below). So the right files are accessed but it seems the events info is not included in this structure as when I use the export file on a single subject. Could you advise what I need to do?

% Input files
sFiles = [];

% Start a new report
bst_report('Start', sFiles);

% Process: Select files using search query
sFiles = bst_process('CallProcess', 'process_select_search', sFiles, [], ...
'search', '(([name CONTAINS "resample"]))');

% Process: Load files
Load(sFiles)

% Save and display report
ReportFile = bst_report('Save', sFiles);
bst_report('Open', ReportFile);
% bst_report('Export', ReportFile, ExportDir);

image

sFiles is a structure.
You need to write a loop that reads sFiles(i).FileName.

Hi Francois,

Could you advise how I fix this script? It works without the loops (giving me the result of the first subject only). I added the loops and used different labels but can't get it to work.

sFiles = bst_process('CallProcess', 'process_select_search', [], [], ...
'search', '(([name CONTAINS "resample"]))');

for iStudy = 1:length(sFiles.iStudy)
sStudy = bst_get('Study', sFiles.iStudy);
end

for i = 1:length(sStudy.Data)
DataMat = in_bst_data(sStudy.Data(i).FileName);
end

n = numel(DataMat.F.events(3).times)

Maybe something like this?

n = 0;
for iFile = 1:length(sFiles)
    DataMat = in_bst_data(sFiles(iFile).FileName);
    iEvt = find(strcmpi({DataMat.F.events.label}, 'your_event_name'))
    n = n + numel(DataMat.F.events(iEvt).times);
end

These are mostly Matlab programming considerations, which is not the primary focus for this forum. Scripting with Brainstorm requires that you have some fluency with Matlab scripting. If you are not confident enough, you might want to start with some tutorials dedicated to basic Matlab programming.

Hi Francois,

Thank you, that worked. I copy an updated script below for anyone else who wants to extract similar information. The below script allowed me to extract two events into an Excel sheet for each subject along with subject IDs.

% Filter files from Brainstorm
sFiles = bst_process('CallProcess', 'process_select_search', [], [], ...
'search', '(([name CONTAINS "resample"]))');

% Extract subject ID and two events
T1 = [];
T2 = [];
T3 = [];

for iFile = 1:length(sFiles)
MyData = sFiles(iFile).SubjectName
DataMat = in_bst_data(sFiles(iFile).FileName);
iEvt1 = find(strcmpi({DataMat.F.events.label}, 'your_event_name_1'))
iEvt2 = find(strcmpi({DataMat.F.events.label}, 'your_event_name_2'))
T1 = [T1; numel(DataMat.F.events(iEvt1).times)];
T2 = [T2; numel(DataMat.F.events(iEvt2).times)];
T3 = [T3; MyData];
end

%convert your char array to cell array
c = cellstr(T3)
%covert your double array to cell array
n = num2cell(T1)
o = num2cell(T2)

out = [c n o]

% Export to Excel
filename = 'accuracy.xlsx';
writecell(out,filename,'Sheet',1,'Range','A1')