Subtracting ERP from single trials

Hi!
I have single trials and I can compute the ERP, but is there any way to subtract the ERP from the single trials…?
Thanks!

Hi Fabrizio,

We do have a substraction process in Process2 -> Difference -> Difference: A - B, but unfortunately it requires the same number of inputs on each side, so to use it as is you would need to drag and drop the averaged ERP N times on the right side of the process2 tab… If you’re familiar with scripting, you could quickly copy the inputs on the ERP size to match the single trials input size. I am not aware of a more straightforward way to do this at the moment in Brainstorm.

I hope this helps,
Martin

Thanks you very much!

Hi Fabrizio,

Brainstorm has a function that you can save the process as a m-file (script) and run the script instead of using the GUI. In that case, you can perform batch processing, i.e. feeding all trails one by one in a simple for loop.

Select the first trail as Files A and the ERP as Files B in Process 2.
In Pipeline editor for difference A-B find the load/save processing pipeline and then select the “Generate .m script”, and save it.

Now, you can add a for loop to this scrip but you need to change “sFiles” in each iteration, i.e. iterate for all trails. Before the for loop you can find the list of all desired trails by “dir” function in Matlab and then you should address them in the loop.

It should solve your problem.
Hossein

This was very helpful! I’ll try to do it, thank you!

I wrote a script but it doesn’t work… can you help me?
This is the script:

file=dir(‘C:\Users\Faber\Documents\MATLAB\brainstorm_db\psilocybin\data\Subject02\14HD’)
for n=4:size(file)
sFiles = {file(n).name}
sFiles2 = {…
‘Subject02/02_KAN_HD_band/data_32513_average_180515_1850.mat’};

% Start a new report
bst_report(‘Start’, sFiles);

% Process: Difference: A-B
sFiles = bst_process(‘CallProcess’, ‘process_diff_ab’, sFiles, sFiles2);

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

Thanks!

You need the full path to be documented in sFile, while your script only gets the file name (the folder is missing). Maybe you’d need something like this:

fPath = ‘C:\Users\Faber\Documents\MATLAB\brainstorm_db\psilocybin\data\Subject02\14HD’
file = dir(fPath)
for i = 1:size(file)
    if isempty(strfind(file(i).name, 'data_'))
        continue;
    end
    sFiles{i} = [fPath, file(i).name]

A simper alternative is probably to use the selection processes. Leave the Process1 box empty, click on Run, and select process “File > Select recordings”. Then generate the corresponding Matlab code, it should give you something like this:

% Process: Select data files in: example_subject/example_folder
sFiles = bst_process('CallProcess', 'process_select_files_data', sFiles, [], ...
    'subjectname',   SubjectNames{1}, ...
    'condition',     'example_folder', ...
    'tag',           '', ...
    'includebad',    0, ...
    'includeintra',  0, ...
    'includecommon', 0);

I tried both ways but it didnt work

  1. By specifying the path:

file = dir(‘C:/Users/Faber/Documents/MATLAB/brainstorm_db/psilocybin/data/Subject02/14HD’);
x=fopen(‘Subject2.txt’,‘w’);
for n=4:length(file);
fprintf(x,‘C:/Users/Faber/Documents/MATLAB/brainstorm_db/psilocybin/data/Subject02/14HD/%s\r\n’,file(n).name);
end
fclose(x);
u=fopen(‘Subject2.txt’,‘r’);
text= textscan(u,’%s’);
txt=text{1};
fclose(u);

for k = 1:(size(file)-3);
sFiles = txt{k,1};

sFiles2 = {‘C:/Users/Faber/Documents/MATLAB/brainstorm_db/psilocybin/data/Subject02/02_KAN_HD_band/data_32513_average_180515_1850.mat’};

% Start a new report
bst_report(‘Start’, sFiles);

% Process: Difference: A-B
sFiles = bst_process(‘CallProcess’, ‘process_diff_ab’, sFiles, sFiles2);

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

end

  1. …and by using the script for the file selection process:

u=fopen(‘tags.txt’,‘r’);
text= textscan(u,’%s’);
txt=text{1};
fclose(u);
sFiles = [];

for n=1:88
t=txt{n,1}
% Input files
SubjectNames = {…
‘Subject02’};

% Start a new report
bst_report(‘Start’, sFiles);

% Process: Select data files in: Subject02/02_KAN_HD_band/32514
sFiles = bst_process(‘CallProcess’, ‘process_select_files_data’, sFiles, [], …
‘subjectname’, SubjectNames{1}, …
‘condition’, ‘14HD’, …
‘tag’, t, …
‘includebad’, 0, …
‘includeintra’, 0, …
‘includecommon’, 0);

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

sFiles2 = {‘C:/Users/Faber/Documents/MATLAB/brainstorm_db/psilocybin/data/Subject02/02_KAN_HD_band/data_32513_average_180515_1850.mat’};

% Start a new report
bst_report(‘Start’, sFiles);

% Process: Difference: A-B
sFiles = bst_process(‘CallProcess’, ‘process_diff_ab’, sFiles, sFiles2);

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

end

Hi Fabrizio,
I don’t understand why you make it so complicated. Why do you write a file and read it again?
Anyway, these are questions related with Matlab scripting, not really with the use of the Brainstorm software. You can probably find other sources of help than this forum. Try a bit harder, ask your colleagues or read some Matlab tutorials, and you’ll get to it!
Good luck
Francois

I wrote a text file with the path of each file to be used as sFiles. I have very limited experience with programming so I guess that’s why I make scripts overly complex. Nevertheless, this script does specify the full path of each file (as far as I understand) but still, it does not work…
Thanks

If your are hoping to write Matlab scripts, I recommend you start by reading some tutorials about Matlab programming and debugging. Otherwise you would probably waste a lot of time by just trying random things.
Or find some colleagues who could spend some time teaching you the basics.

Otherwise, if you don’t want to spend time in learning Matlab scripting, you can also stick to using the interface.

Hi Fabrizio,

Please use the following script. It should work. Just consider changing the file names and paths.

trgFileExp = 'data_name_trial' ; % The common part of all trials' name
dirFile1   = 'C:\folder\' ;      % full path of the Brainstorm database folder containing trials  
avgFile1   = [dirFile1 'data_name_average_date_time.mat'] ;  % full path and name of the ERP file 

fileList1 = struct2table(dir(dirFile1)) ; 
tmp1      = regexp(fileList1.name,trgFileExp);
tmp1      = cellfun(@num2str, tmp1, 'UniformOutput', false);
fileList2 = fileList1(ismember(tmp1, '1'),:) ;

% Input files
for k = 1:length(fileList2.name)
    sFiles = strjoin([dirFile1 fileList2.name(k)],'') ;         
    sFiles2 = avgFile1 ;
    
    % Start a new report
    bst_report('Start', sFiles);
    
    % Process: Difference: A-B
    sFiles = bst_process('CallProcess', 'process_diff_ab', sFiles, sFiles2);
    
end

Hossein

1 Like