Skip to content

biosig2eeglab: the last channel becomes events only when it holds triggers - #1005

Merged
arnodelorme merged 2 commits into
developfrom
agent/issue-1004
Nov 29, 2026
Merged

arnodelorme merged 2 commits into
developfrom
agent/issue-1004

Conversation

@arnodelorme

@arnodelorme arnodelorme commented Nov 29, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #1004 (biosig2eeglab turns a signal in the last channel of an EDF or BDF file into events and removes it: #1004). It follows issue 989 (biosig2eeglab takes the last EEG channel of an EDF file for an event channel: #989).

When an EDF or BDF file gives no event, biosig2eeglab took the last channel for a trigger channel on the number of its different values alone. With this change the last channel becomes events only when its label names a trigger channel (status, trig, event, marker, or a label that starts with sti, in upper or lower case) and it holds few different values, as before (20 at most in the first 100 samples, 1000 at most overall). Any other last channel stays a data channel. The rule is one local function, islasteventchan, and only biosig2eeglab.m changes.

Last channel of each file: EEGLAB before, EEGLAB after, EEGPrep, what is right

File Label of the last channel EEGLAB before EEGLAB after EEGPrep What is right
test_cleaned.edf Status removed, 108 events removed, 108 events removed, 108 events triggers
test_reduced.edf Status removed, 17 events removed, 17 events removed, 19 events triggers
test.edf Status removed, 17 events removed, 17 events removed, 19 events triggers
test_edf_stim_channel.edf DIG DTRIG removed, 12 events removed, 12 events removed, 12 events triggers
sub-23_ses-01_task-fps_run-03_eeg.bdf STATUS removed, 144 events removed, 144 events kept as data triggers
sub-18_ses-01_task-fps_run-02_eeg.bdf STATUS removed, 144 events removed, 144 events kept as data triggers
sub-20_eeg_sub-20_task-genMusic03_eeg.edf Status cannot read the file cannot read the file removed, 733 events triggers
flex2.edf CQ_DRL removed, 181 events kept as data kept as data signal
bdfPlusC.bdf sine 50 Hz removed, 3999 events kept as data kept as data signal
edfPlusC.edf sine 50 Hz removed, 3999 events kept as data kept as data signal
edfPlusD.edf sine 50 Hz removed, 2199 events kept as data kept as data signal
test_utf8_annotations.edf sine 50 Hz removed, 2000 events kept as data kept as data signal
test_generator.edf sine 50 Hz removed, 0 events kept as data kept as data signal
test_legacy.edf sine 50 Hz removed, 0 events kept as data kept as data signal
test_generator_2.edf sine 50 Hz removed, 0 events kept as data kept as data signal
test_generator_2.bdf sine 50 Hz removed, 0 events kept as data kept as data signal
chtypes_edf.edf POL $A2 removed, 3 events kept as data kept as data signal
multiple_annotation_chans.bdf acc3 removed, 4845 events kept as data kept as data signal
test_stim_channel.edf sine 1 Hz removed, 16801 events kept as data kept as data signal
test_uneven_samp.edf 0.2Hz Blk 1/0uV removed, 22 events kept as data kept as data signal
test.edf EEG O2 kept as data kept as data kept as data signal
sub-S1_ses-01_task-Experiment_run-01_eeg.edf EEG O2 kept as data kept as data kept as data signal
duplicate_channel_labels.edf EEG F1-Ref kept as data kept as data kept as data signal
sub-NDARAC904DMU_task-contrastChangeDetection_run-1_eeg.bdf Cz kept as data kept as data kept as data signal
sub-NDARCN669XPR_task-contrastChangeDetection_run-3_eeg.bdf Cz kept as data kept as data kept as data signal
5038.edf [F4 - M1] kept as data kept as data kept as data signal

Right answers on these 26 files: 12 before, 25 after. Among the 17 files where the old rule fired, it was right 4 times and removed a signal 13 times. The file that stays wrong (ds002725) cannot be read by pop_biosig before or after, for another reason. EEGPrep is the reader of pull request 868 of sccn/eegprep ([IO] Import EDF and BDF files with EEGPrep's own reader: BIDS import and File > Import data: sccn/eegprep#868), which holds the same function, ported line for line. Its two ds003343 files differ because of an open question there on where the data start, not because of this rule.

What changes for users

  • A signal in the last channel is no longer removed. In four files the old rule removed a sine wave and then dropped its events too (more than 100000), so the signal disappeared without a trace.
  • A trigger channel in the last position still gives its events by default when its label says so (Status, DIG DTRIG, Trigger, Event, Marker, STI 014). With another label (for example Ch33) it stays in the data, and a message names the menu that extracts its events (File > Import event info > From data channel).
  • 'importevent', 'off' still turns the rule off.
  • The rule applies to every format read through biosig2eeglab (GDF and others), as the old rule did. Only EDF and BDF files were tested here.
  • Files with fewer than 100 samples no longer stop with "Index in position 2 exceeds array bounds" (6 files of the comparison).
Checks
  • tools/edf_bdf_reader_compare.m of sccn/eegprep on the files of the comparison of pull request 868, before (develop 579a27d) and after (this branch), MATLAB R2025b, BIOSIG 3.8.4. Data and events are the same in every file except 19: the 13 files of the table where the last channel now stays (flex2.edf, bdfPlusC.bdf, edfPlusC.edf, edfPlusD.edf, test_utf8_annotations.edf, test_generator.edf, test_legacy.edf, test_generator_2.edf, test_generator_2.bdf, chtypes_edf.edf, multiple_annotation_chans.bdf, test_stim_channel.edf, test_uneven_samp.edf) and 6 files that stopped with an error before and are read now (test_chan_types.bdf, test_dig_min_larger_max.bdf, test_invalid_dig_min_max.bdf, test_invalid_phys_min_max.bdf, test_phys_min_larger_max.bdf, SC4001EC-Hypnogram.edf). Three files of the pyedflib generator hang pop_biosig before and after and were left out.
  • In the four files where the last channel becomes events, the events are the same before and after (types, latencies), and the same in EEGPrep (108, 14, 14 and 12 events from the channel).
  • Tests of sccn/eeglab_tests that call pop_biosig, pop_chanevent and biosig2eeglabevent: 5 of 5 pass before and after. The new test of the tests pull request fails before and passes after.
What the last channel holds in each file
File Source Label Dimension Transducer Different values, first 100 samples Different values, whole channel Samples at the most frequent value Changes of value Whole numbers Right answer and how it is known
test_cleaned.edf EEGLAB sample_data/test_data Status uV `` 2 24 99.6% 109 yes triggers: label Status, 99.6% of the samples at the resting value, whole numbers; EEGLAB's sample file with triggers
test_reduced.edf mne-testing-data EDF (public) Status uV `` 2 4 99.5% 27 yes triggers: label Status, 99.5% of the samples at 0, 4 whole values
test.edf mne-python mne/io/edf/tests/data (public) Status uV `` 2 4 99.5% 27 yes triggers: label Status, 99.5% of the samples at 0, 4 whole values
test_edf_stim_channel.edf mne-python mne/io/edf/tests/data (public) DIG DTRIG uV ? 1 2 99.0% 24 yes triggers: digital trigger input (label DIG DTRIG), values 0 and 100, 99% of the samples at 0
sub-23_ses-01_task-fps_run-03_eeg.bdf OpenNeuro/NEMAR ds003343 STATUS Boolean Triggers and Status 1 5 99.9% 288 yes triggers: BioSemi label STATUS, dimension Boolean, transducer Triggers and Status
sub-18_ses-01_task-fps_run-02_eeg.bdf OpenNeuro/NEMAR ds003343 STATUS Boolean Triggers and Status 1 5 99.9% 288 yes triggers: BioSemi label STATUS, dimension Boolean, transducer Triggers and Status
sub-20_eeg_sub-20_task-genMusic03_eeg.edf OpenNeuro/NEMAR ds002725 Status uV `` 2 7 99.9% 1465 no triggers: label Status; the events.tsv beside the file lists the same codes (1, 47, 1283)
flex2.edf BioSig 4 test folder on this Mac CQ_DRL uV emotiv electrode 2 2 83.1% 362 yes signal: contact quality of the DRL electrode of an Emotiv headset (label CQ_DRL), values 0 and 4
bdfPlusC.bdf R package edfReader extdata sine 50 Hz uV `` 3 3 50.0% 3999 no signal: sine wave of a signal generator (label sine 50 Hz), 3 different values
edfPlusC.edf R package edfReader extdata sine 50 Hz uV `` 3 3 50.0% 3999 no signal: sine wave of a signal generator (label sine 50 Hz), 3 different values
edfPlusD.edf R package edfReader extdata sine 50 Hz uV `` 3 3 50.0% 2199 no signal: sine wave of a signal generator (label sine 50 Hz), 3 different values
test_utf8_annotations.edf mne-testing-data EDF (public) sine 50 Hz uV `` 3 3 50.0% 1999 no signal: sine wave of a signal generator (label sine 50 Hz), 3 different values
test_generator.edf pyedflib test data (EDF test file generator of teuniz.net) sine 50 Hz uV `` 3 3 50.0% 119999 no signal: sine wave of a signal generator (label sine 50 Hz), 3 different values
test_legacy.edf pyedflib test data (EDF test file generator of teuniz.net) sine 50 Hz uV `` 3 3 50.0% 119999 no signal: sine wave of a signal generator (label sine 50 Hz), 3 different values
test_generator_2.edf mne-testing-data EDF (public) sine 50 Hz uV `` 3 3 50.0% 119999 no signal: sine wave of a signal generator (label sine 50 Hz), 3 different values
test_generator_2.bdf mne-testing-data BDF (public) sine 50 Hz uV `` 3 3 50.0% 119999 no signal: sine wave of a signal generator (label sine 50 Hz), 3 different values
chtypes_edf.edf mne-testing-data EDF (public) POL $A2 uV `` 1 2 88.0% 2 yes signal: polygraph input of a Nihon Kohden system (label POL $A2), a saturated channel with 2 changes
multiple_annotation_chans.bdf mne-testing-data BDF (public) acc3 G none 14 23 19.6% 4842 no signal: accelerometer (label acc3, physical dimension G)
test_stim_channel.edf mne-python mne/io/edf/tests/data (public) sine 1 Hz uV `` 8 15 17.0% 16800 no signal: sine wave of a signal generator (label sine 1 Hz)
test_uneven_samp.edf mne-python mne/io/edf/tests/data (public) 0.2Hz Blk 1/0uV uV Software generated 1 2 50.0% 43 yes signal: block wave of a signal generator (label 0.2Hz Blk 1/0uV), half of the samples at each value
test.edf comparison of pull request 857 EEG O2 uV AgAgCl electrode 1 48 5.0% 55 no signal: label EEG O2 (issue 989)
sub-S1_ses-01_task-Experiment_run-01_eeg.edf EEGLAB sample_data BIDS_test (issue 989 of sccn/eeglab) EEG O2 uV AgAgCl electrode 1 48 5.0% 55 no signal: label EEG O2 (issue 989), channels.tsv lists it as EEG
duplicate_channel_labels.edf mne-python mne/io/edf/tests/data (public) EEG F1-Ref uV `` 17 498 0.7% 1719 no signal: label EEG F1-Ref
sub-NDARAC904DMU_task-contrastChangeDetection_run-1_eeg.bdf OpenNeuro/NEMAR ds005505-bdf-mini Cz uV EMG sensor 1 1 100.0% 0 no signal: flat reference channel Cz
sub-NDARCN669XPR_task-contrastChangeDetection_run-3_eeg.bdf OpenNeuro/NEMAR ds005505-bdf-mini Cz uV EMG sensor 1 1 100.0% 0 no signal: flat reference channel Cz
5038.edf eeglab_tests unittesting_binary/testfiles (sleep recording) [F4 - M1] uV `` 1 4320 0.3% 6534538 no signal: sleep recording, EEG derivation [F4 - M1] that starts flat
Why the values alone cannot decide

The contact quality channel of flex2.edf holds the values 0 and 4, with 83% of its samples at 0. The saturated channel of chtypes_edf.edf holds 2 values and changes twice. The block wave of test_uneven_samp.edf holds 0 and 1. All three look like a trigger channel, and a real trigger channel can hold its codes for a long time. A threshold on the share of samples at rest separates these files by a few percent only (83% and 88% against 99%), so the label is asked first and the channel stays data when the label does not say "trigger".

— orchestrator

@claude

claude Bot commented Nov 29, 2026 •

Copy link
Copy Markdown

Claude encountered an error —— View job


I'll analyze this and get back to you.

@arnodelorme

Copy link
Copy Markdown
Collaborator Author

Code review

Who reviewed. Codex with the model GPT-6.1 Terra read the EEGLAB change (pull request 1005 of sccn/eeglab: #1005) and its Python port (pull request 868 of sccn/eegprep: sccn/eegprep#868), read only. Gemini 4 Carbon (Medium) was asked first through agy and gave no answer in 10 minutes. The code was written by Claude (Fable 5.2).

Its verdict. One finding marked blocking and six smaller ones. It found the event loop faithful to MATLAB (first sample, last sample, negative and fractional values) and the channel removed consistently (data, labels, channel count, mixed sampling rates).

Fixed.

  • Finding 6, the comments promised too much: a label is a strong hint, not a proof (a channel named "contact status" with few values would still be removed). The comments now say that a channel is not removed on its values alone and that it stays data when in doubt (EEGLAB commit 6e83f9d, EEGPrep commit c9f77fff).
  • Finding 2, second half: the help of pop_readedf now says that importevent='off' imports no trigger and no annotation, and that the boundary events of the gaps are kept.
  • Finding 5: the description of pull request 1005 now says that the rule applies to every format read through biosig2eeglab, as the old rule did.

Left, and why.

  • Finding 1 (marked blocking): EEGPrep applies the rule in files that also hold annotations, and the reviewer read isempty(dat.EVENT.POS) as "EEGLAB does not". Measured in MATLAB R2025b with BIOSIG 3.8.4, the event table of BIOSIG is empty for every EDF file of the comparison, with or without annotations, so EEGLAB does apply the rule there: test_reduced.edf and test.edf of MNE give 14 events from their Status channel beside their annotations, in EEGLAB and in EEGPrep, with 138 channels in both. Changing EEGPrep as proposed would make it differ from EEGLAB on these two files. A comment in the code now says so. No test was added because these two files weigh 0.8 and 3 MB.
  • Finding 2, first half: with importevent off, EEGLAB also removes the BioSemi Status channel (rmeventchan is on by default: test.bdf has 16 channels with and without events), so EEGPrep does the same.
  • Finding 3: the indexing of labels stored as a character matrix is old code that this change does not touch, and BIOSIG 3.8.4 returns the labels as a cell array.
  • Finding 4: MATLAB counts each NaN as a different value and NumPy counts them as one. Neither reader produces NaN here (overflow detection is off by default in pop_biosig, and the NumPy reader computes every sample from a whole number).
  • Finding 7: no test was added for thresholds on made-up values, since the tests of this repository use real recordings.

— orchestrator

@arnodelorme
arnodelorme merged commit 78e6e2b into develop Nov 29, 2026
2 checks passed
@arnodelorme

Copy link
Copy Markdown
Collaborator Author

Arno answered on November 29, 2026: 1a, the label stays required, so a last channel with a plain label such as "Ch33" stays in the data even when it holds triggers. He asked to merge, and this pull request was merged together with the test of sccn/eeglab_tests#18.

— orchestrator

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

biosig2eeglab turns a signal in the last channel of an EDF or BDF file into events and removes it

1 participant