Mark segment as bad with script

Dear Community,

I am trying to automatically mark segments as bad for all my subjects raw EEG data, from the beginning of the signal to the first trigger event, minus 100ms.
The first trigger event is different in time for every subject.

I have seen this post:

But as I mentioned before, in my case the bad segment would be different for every subject.

Is there a scripting solution for this?

Thank you

François

Nothing in the existing processes would allow you to create a new "BAD" event based only on the first marker. You could make work something out with a combination of multiple processes used a bit away from their initial goal:

  • Group by name: to duplicate an existing category into an new event category that includes the string "BAD"
  • Detect multiple responses, with a very long "minimum delay between events" and "keep only the first even": In order to keep only the first event of the duplicated category
  • Convert to extended event with a time window that includes all the beginning of the file, until the baseline of the first stim (eg. [-100, -0.500]s)

Otherwise, it might be simpler to write a Matlab script edit directly the event structure:

2 Likes

Hello François,

thank you for your answer, it led me to a scripting solution that I share below.
It creates two bad segments:

  • one between the beginning and [the first trigger - 0.0502 s]
  • one between [the last trigger + 0.3002 s] and the end of the signal.

%% Beginning of script

timestep = 1/4800; % ~0.0002s | Sampling freq = 4800Hz
sLink = in_bst_data(sImpRawFile.FileName, 'F'); % Creates "Link To Raw File" structure, that contains meta-data
iBadEvent = length(sLink.F.events) + 1;
sLink.F.events(iBadEvent).label = 'BAD';
sLink.F.events(iBadEvent).color = [1 0 0];
sLink.F.events(iBadEvent).epochs = [1 1]; % 2 segments
sLink.F.events(iBadEvent).select = 1;
sLink.F.events(iBadEvent).channels = cell(1,2);
sLink.F.events(iBadEvent).notes = cell(1,2);
sLink.F.events(iBadEvent).times = [0 sLink.F.events(1).times(end)+(0.300+timestep);...
sLink.F.events(1).times(1)-(0.050+timestep) sLink.F.prop.times(2)];
bst_save(file_fullpath(sImpRawFile.FileName), sLink, 'v6', 1); % saves modifications on data file in Brainstorm database

%% End of script

Best,

François