Due to limitations in Elekta's event trigger box, we typically get a few spurious events codes in the fif files. E.g. the file 'KMO1_subj20_sess02-0.fif' (still in the data servers, I shared with you before) has the following spurious codes:
193.69 sec: 32 (spurious) immediately followed by 42 (true code)
1224 sec: 16 (spurious) immediately followed by 22 (true code)
662.8 sec: 64 (spurious) immediately followed by 93 (true code)
1637.8 sec: 64 (spurious) immediately followed by 93 (true code).
Notice in all these cases a smaller code is immediately followed by a larger code without a zero inbetween. This happens when we send an event code and the bits did not all go up at the same time.
But spurious codes also happen when we take a code down, e.g. 22 (true code) can be immediately followed by 16 (spurious code). This happens when all the bits did not go down at the same time.
But MNE-Python has a better solution and it is useful to see their code. See their implementation of reading events codes: https://mne.tools/stable/generated/mne.find_events.html
They have the option 'consecutive=increasing' (default) and 'min_duration'. When I use the command:
events = mne.find_events(raw, stim_channel='STI101',min_duration=0.002)
the file is read without any spurious event codes. Can you implement something similar in Brainstorm? That is, if two events are closer than 'min_duration', keep the second event if they are increasing in value, otherwise keep the first event.
If I add an option min_duration to process_evt_read.m (menu "Read from channel"), it should solve your problems, right?
I still have the connection information to your data server, but please tell me where the file is located.
(EDIT: I found it: /datadrive/datasharetmp/example_data_francois/with-0/KMO1_subj20_sess02-0.fif)
When linking the files to the Brainstorm database, do not use the option "Event channel". Then execute manually the process "Events > Read from channel". Set the option "Reject events shorter than: 1 sample" and it should remove all the events that do not have two consecutive identical values in the trigger channel.
One clarification: To my understanding, whenever you detect two events with <1sample distance, you keep the largest code, right? This is correct, and indeed this is what happens to the fif files I just tested.
No, there is no comparison of values, it is only based on the duration of the event.
If the values are rising or falling and are sampled half-way during this process, there is no chance that the spurious values would be stable for two consecutive samples. By removing the events that are shorter than one full sample (ie. for which the value of stim channel is not stable for two consecutive samples), you get rid of all the spurious events you described.
I assumed that the final stable value of the event should always be stable for more than two consecutive samples. If this is not the case, the process might remove some of your stim.