Skip to content

[IO] Import EDF and BDF files with EEGPrep's own reader: BIDS import and File > Import data - #868

Open
arnodelorme wants to merge 24 commits into
developfrom
agent/issue-865
Open

arnodelorme wants to merge 24 commits into
developfrom
agent/issue-865

Conversation

@arnodelorme

@arnodelorme arnodelorme commented Nov 29, 2026 •

Copy link
Copy Markdown
Collaborator

EEGPrep now reads EDF, EDF+, BDF and BDF+ files with its own NumPy reader in the BIDS import and in a new entry of File > Import data, with no BioSig, no MNE and nothing to install (issue #865).

EEGLAB in MATLAB R2025b (Linux) EEGPrep (Linux), with the new entry highlighted
EEGLAB, File > Import data > Using EEGLAB functions and plugins EEGPrep, File > Import data > Using EEGPrep functions and plugins

The new entry opens a file chooser and has no window of its own, so the menu is the only thing to show. Both captures were taken on jamming with the menus really open. The EEGLAB window was enlarged to 1000 by 640 pixels first, because MATLAB R2025b draws its menus inside the figure and cut them at the default size.

Distance to EEGLAB over the files, for neo's EDFRawIO and for the NumPy reader

  • Reader chosen: the NumPy reader, not neo's EDFRawIO. On 55 files it reads all 44 files EEGLAB reads and is at exactly 0 µV from EEGLAB's pop_biosig on 42 of them. neo reads 38, is at 0 µV on none (median 0.54 µV, thousands of µV on BDF) and needs the compiled pyedflib.
  • New menu entry: File > Import data > Using EEGPrep functions and plugins > From EDF/EDF+/BDF/BDF+ files (EEGPrep reader), which calls the new function pop_readedf. It comes right after EEGLAB's two BIOSIG entries, whose order is unchanged.
  • BIDS import: EDF+D files with gaps no longer go through MNE. Their records are joined as EEGLAB does, and each gap gets a boundary event (Arno's answer 1b, below).
  • Boundaries at gaps, chosen by Arno (1b), now in both programs: on EDF+D and BDF+D files with gaps, EEGPrep places a boundary event at each gap. EEGLAB develop does the same since the EEGLAB pull request pop_biosig: boundary event at each gap of EDF+D and BDF+D files eeglab#1006 was merged on November 29, 2026 at 10:37 (pop_biosig: boundary event at each gap of EDF+D and BDF+D files eeglab#1006, merge commit 5e0be870): same data, same boundaries. Until then EEGLAB placed none, and this is still so for the EEGLAB commit d0963be9 that EEGPrep's tests pin, so the reference workflow does not compare the boundaries yet.
  • Unchanged: pop_biosig and the BioSig menus.
  • Based on pull request [GUI] Read BioSig files with the optional Python BioSig library #857 (branch agent/issue-812), which is not merged yet: this pull request shows its commits until it is merged. The head of that branch (its commit 00607eb, which turns BioSig off on Windows) and develop were merged in again at 46f8677.

Questions for Arno:

  1. Gaps in EDF+D files: answered, 1b. Arno, November 29, 2026: "1b. Add boundary to each gap, but file an issue for EEGLAB along with an example file the requirement to test against Python. After that, create a pull request in EEGLAB." Done in commit 84d3498: the records stay joined, the data do not change, and each gap gets a boundary event half a sample before the first sample after the gap, whose duration is the missing time in samples. The EEGLAB issue is pop_biosig joins the records of an EDF+D file without a boundary event eeglab#1003 (pop_biosig joins the records of an EDF+D file without a boundary event eeglab#1003), the EEGLAB pull request is pop_biosig: boundary event at each gap of EDF+D and BDF+D files eeglab#1006 (pop_biosig: boundary event at each gap of EDF+D and BDF+D files eeglab#1006) and its test is Test for the boundary events at the gaps of an EDF+D file eeglab_tests#19 (Test for the boundary events at the gaps of an EDF+D file eeglab_tests#19). On the example file tests/data/edf_plus_d_gaps.edf, EEGLAB with that change and EEGPrep return the same data and the same three boundaries.
  2. Last channel turned into events: answered. Arno, November 29, 2026: "2a. Yes, leave it out. The last channel can stay as a data channel. Although there are some tests in EEGLAB to see if it's a data channel or not based on the number of values. Based on your current test, implement the same thing in EEGLAB and EEGPrep. Create a pull request on EEGLAB if needed."
    The rule, the same in both programs. The last channel becomes events and is removed 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 (20 at most in its first 100 samples, 1000 at most overall). Any other last channel stays a data channel, and pop_readedf(file, importevent='off') turns the rule off, as 'importevent', 'off' in pop_biosig.
    Totals. On the 17 files where EEGLAB's old rule fired, it was right 4 times and removed a signal 13 times (a sine wave, a block wave, an accelerometer, a contact quality channel, a saturated channel). The channel count of EEGPrep differed from EEGLAB's on 19 of the 44 files both read. With the new rule in both programs it differs on 2, the two ds003343 files of question 3. In the four files where the channel becomes events, EEGPrep gives the same events as EEGLAB (108, 14, 14 and 12, same types and latencies).
    EEGLAB. Issue 1004 (biosig2eeglab turns a signal in the last channel of an EDF or BDF file into events and removes it: biosig2eeglab turns a signal in the last channel of an EDF or BDF file into events and removes it eeglab#1004) and pull request 1005 (biosig2eeglab: the last channel becomes events only when it holds triggers: biosig2eeglab: the last channel becomes events only when it holds triggers eeglab#1005). Arno then answered that the label stays required (a last channel with a plain label such as Ch33 stays in the data even when it holds triggers), and at his request that pull request was merged on November 29, 2026 (commit 78e6e2b8 on EEGLAB develop) with its test (Test for the last channel of EDF and BDF files in pop_biosig eeglab_tests#18: Test for the last channel of EDF and BDF files in pop_biosig eeglab_tests#18). EEGPrep therefore follows EEGLAB develop on the 13 files where the rule changed. In the BIDS import the events of events.tsv replace the events of the file as before, and a trigger channel is removed either way, as in EEGLAB's BIDS import.
  3. Two ds003343 files with a wrong header length: waiting for Arno's choice. The check of the specification and of the signals showed that EEGLAB reads these files correctly from the header-length field (byte 4608) and that the new reader, which starts at byte 5376, does not ([IO] Import EDF and BDF files with EEGPrep's own reader: BIDS import and File > Import data #868 (comment)). Nothing is changed in the reader until Arno chooses.
EEGLAB reference run (55 files, MATLAB R2025b)

The table of every file, with where it comes from, is in the comparison comment of issue #865: #865 (comment)

Scripts: tools/edf_bdf_reader_compare.m and tools/edf_bdf_reader_compare.py.

What changed in the code
  • src/eegprep/plugins/EEG_BIDS/edf_bdf.py: gaps joined (no pop_mneio) with a boundary event at each gap (the rules are in the comment on answer 1b), mixed rates repeated up to their least common multiple with a memory guard, shortened files read up to the last complete record.
  • src/eegprep/functions/popfunc/pop_readedf.py: the new function. History command EEG = pop_readedf('file');. Help page src/eegprep/resources/help/pop_readedf.md.
  • eeglab_menu.py and menu_actions.py: the menu entry and its action.
  • raw.py, pop_load_frombids, pop_importbids, bids_preproc and the export wizard already read through the NumPy reader. neo is not used.
  • After the review by Gemini 4 Carbon and by Codex GPT-6.1 Terra (commits 38365e8, bbb7443 and 12f6d17, listed in the review comment [IO] Import EDF and BDF files with EEGPrep's own reader: BIDS import and File > Import data #868 (comment)): the BioSemi Status channel counts in the common sampling rate as in BIOSIG and its triggers are decoded from the records already read, events are sorted by latency, annotations of files with gaps are placed and measured in recorded samples whatever the order of the time stamps, a record duration of 0 is refused with a clear message, and a header that announces 0 records is read in full as EEGLAB does. On the 55 files the data did not change.
  • Tests: tests/test_pop_readedf.py on EEGLAB's test.bdf and test.edf with the values of pop_biosig in MATLAB, and the reader tests of tests/test_bids_load_frombids_helpers.py.

Refs #865, #812, #857, #492.

🤖 Generated with Claude Code

The NumPy EDF/BDF reader no longer passes EDF+D files with gaps to MNE: it joins the records as BIOSIG does in MATLAB. Channels at different sampling rates are repeated up to their common rate and a file shorter than its header announces is read up to its last complete record, both as in EEGLAB. tools/edf_bdf_reader_compare compares this reader and neo's EDFRawIO with EEGLAB's pop_biosig on a collection of files (issue #865).
It stays reachable from the previous commit for the issue and the pull request.
pop_readedf imports EDF, EDF+, BDF and BDF+ files with EEGPrep's NumPy reader, with nothing to install. Its entry, From EDF/EDF+/BDF/BDF+ files (EEGPrep reader), follows EEGLAB's two BIOSIG entries under File > Import data. Tests compare EEGLAB's test.bdf and test.edf with the values of pop_biosig in MATLAB (issue #865).

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Code review skipped — your organization has no extra usage available to pay for this review.

If your organization's extra usage balance is empty, an organization admin can add extra usage credits at claude.ai/admin-settings/usage. If its monthly spend limit was reached, an admin can raise it on the same page. If neither applies, contact Anthropic support.

Once extra usage is available, someone with write access to this repository can comment @claude review on this pull request to trigger a review.

Review of pull request 868: the BDF Status channel counts in the common rate as in BIOSIG, events are sorted by latency, and an annotation duration in an EDF+D file with gaps counts recorded samples only. The comparison tool notes when two readers give different labels for the same number of channels.
@arnodelorme

arnodelorme commented Nov 29, 2026 •

Copy link
Copy Markdown
Collaborator Author

Code review

This pull request was reviewed by two other AIs, both read-only on commit 712e566: Gemini 4 Carbon at medium effort (through agy), which asked for changes with four findings it rated blocking, and Codex, model GPT-6.1 Terra, which asked for changes with five findings rated "should fix". The blocking findings are all fixed, in commits 38365e8, bbb7443 and 12f6d17. After the fixes the reader gives the same data on every file of the comparison.

Gemini 4 Carbon, blocking, all fixed

  1. The pop_readedf test would fail on Windows. The history command holds the path with forward slashes and the test compared it with a Windows path. The test now compares with the forward slash form.
  2. BioSemi Status channel on mixed rates and shortened files. The triggers were read a second time from the file with the record count of the header, so a shortened BDF file with a Status channel raised an error. They are now decoded from the records already read. The Status channel also counts in the common sampling rate, as in BIOSIG's sopen (Codex found this too).
  3. A record duration of 0 raised a division by zero. EDF+ allows it for files that hold annotations only. The reader now refuses such a file, or a channel with 0 samples per record, with a clear message.
  4. Annotations in files with gaps. Time stamps that are not in increasing order gave wrong latencies, and rounding could keep an annotation placed after the end of the recording. Each annotation is now placed in the record that holds its time, whatever the order, and one after the last record is dropped exactly.

Gemini 4 Carbon, should fix or minor

  1. Fixed. Annotations are read one record at a time, so bytes left after the end of a record cannot be read as a time. Events are sorted by latency (Codex found this too). A record without a time stamp follows the record before it.
  2. Fixed in part. Samples are calibrated before they are repeated, which uses less memory and no extra copy for ordinary files (the 55 files now read in 0.35 second instead of 1.2). The error message no longer says that EEGLAB cannot read the file. The memory guard itself is unchanged, see Codex 4.
  3. Fixed. A header that announces 0 records on a file that holds some raised "holds no complete data record". I gave EEGLAB's test.edf such a header: pop_biosig in MATLAB R2025b still reads its 60 records (15360 samples). The reader now does the same (commit 12f6d17).
  4. Fixed in part. Tests added: events before a gap, inside it, after it and after the last record, with time stamps in order and not in order; a BDF file whose Status channel is at twice the rate of its signal, whole and shortened; a record duration of 0; a bare pop_readedf(path) call in the console. Not added: a test of the memory guard, which would need a file of several gigabytes.
  5. Left. Five small points in the comparison scripts (a distance exactly on a histogram boundary, a one-sample recording, a corrupt file stopping the run, Windows line ends, files above 2 GiB). None applies to the 55 files, so the table and the histogram do not change.

Codex GPT-6.1 Terra

  1. Fixed. Status channel left out of the common rate (Gemini 2).
  2. Fixed. The duration of an annotation in a file with gaps counted the missing time. Its end now goes through the same joining as its onset, so the duration counts recorded samples only.
  3. Fixed. Events are sorted by latency (Gemini 5).
  4. Left. The memory guard does not apply when the common rate equals the fastest channel's rate. In that case the array has the size of any file recorded at that rate. The guard is there only for rates whose common multiple explodes (51,948,000 samples per record on three test files).
  5. Fixed. The comparison tool compared channels by position without looking at their labels. It now writes a note when the labels differ. The note appears on two files, in neo's column only (test.edf and duplicate_channel_labels.edf), and no number changes.
Check that the fixes moved nothing away from EEGLAB
  • The reader before the review and after it was run on the 55 files of the comparison. On 44 of the 45 files it reads, the data, the sampling rate and every event (type, latency, duration) are identical, the 6 BioSemi files with a Status channel included.
  • On the 45th, edfAnnonC.edf, the same 8 events now come sorted by latency. EEGLAB reads no event from that file, so nothing moved away from it.
  • The 10 other files are refused before and after (three of them with the reworded memory message).
  • No file of the collection has a Status channel at another rate than its signals, a shortened BDF file with a Status channel, or time stamps out of order. These fixes follow BIOSIG's code or the EDF+ specification and are tested on written files, as the other reader tests of the repository do. They could not be checked against EEGLAB on a real file.

— orchestrator

Status triggers are decoded from the records already read, so a shortened BDF file keeps the triggers of its complete records. Annotations are read one record at a time and placed with time stamps that need not be in order, and one after the last record is dropped exactly. A record duration or a sample count of zero is refused with a clear message. Samples are calibrated before they are repeated. The pop_readedf test compares the command with a POSIX path, and a console test is added.
EEGLAB's pop_biosig reads all 60 records of test.edf when its header announces 0 data records (MATLAB R2025b). The reader refused such a file; it now reads the complete records, as for a header that announces -1.
@arnodelorme

Copy link
Copy Markdown
Collaborator Author

Question 3, the ds003343 files: EEGLAB reads them correctly, the new reader does not

Arno asked by email on November 29, 2026 to look at the EDF specification and at the signals before he chooses. The check reverses what this pull request says about these files.

  • The specification: the header is 256 bytes plus 256 bytes per channel, so its length follows from the number of channels, and a field of the header (bytes 185 to 192) repeats it. In all 59 BDF files of ds003343 (not only the two of the comparison) the field says 4608 bytes while 20 channels need 5376, so the files do not follow the format.
  • Where the data really start: at byte 4608, as the field says and as EEGLAB reads. Read from there, the signals are continuous from one record to the next and all 144 events of the BIDS events.tsv fall exactly on the triggers of the STATUS channel, in both files. Read from byte 5376, as the new reader does, the signals jump at every record border and in the middle of every record (6 to 10 times the usual step), and the STATUS channel is noise (about 92,600 "triggers").
  • What is lost in these files: the first 256 samples (0.5 second) of the first channel, Fz, hold the end of the header text and not signal, in EEGLAB as well. The writer of the files put a 5376-byte header over data that start at byte 4608.
  • Proposed: the reader starts at the header-length field, as EEGLAB and BIOSIG do, with a warning when the field disagrees with the number of channels. Nothing is changed until Arno answers. The sentence of question 3 in the description above ("EEGLAB starts reading 768 bytes too early and its channels are shifted") is wrong and will be corrected.
Numbers for the two files of the comparison
File Read from Complete records Median step between samples Median step across a record border Events of events.tsv found on a STATUS trigger
sub-23_ses-01_task-fps_run-03_eeg.bdf byte 4608 (EEGLAB) 366 of 366 2.10 µV 2.11 µV (1.0 times) 144 of 144
same byte 5376 (new reader) 365 2.03 µV 19.43 µV (9.6 times) 0 of 144 (92,586 false onsets)
sub-18_ses-01_task-fps_run-02_eeg.bdf byte 4608 (EEGLAB) 367 of 367 3.38 µV 3.25 µV (1.0 times) 144 of 144
same byte 5376 (new reader) 366 3.25 µV 19.74 µV (6.1 times) 0 of 144 (92,993 false onsets)
  • The file size is exactly 4608 bytes plus the announced number of records times 30,000 bytes, in both files. From byte 5376 the last record is 768 bytes short.
  • Bytes 4608 to 5375 are text: the end of the "samples per record" field (500 repeated) and 640 spaces. They are the 256 damaged samples of Fz.
  • The onsets of events.tsv are samples counted from 1, and each one is the first sample of a STATUS value equal to its event_value.
  • A plot of 3 seconds does not tell the two readings apart on the EEG channels alone, because the 16 channels are neighbours and look alike. The trigger channel and the steps at the record borders do. The two figures were emailed to Arno and will be added to this branch.
  • Specification: Kemp et al. 1992, "A simple format for exchange of digitized polygraphic recordings", Electroencephalography and Clinical Neurophysiology 82, 391 to 393, and https://www.edfplus.info/specs/edf.html ; the BDF variant: https://www.biosemi.com/faq/file_format.htm

— orchestrator

Arno's answer 1b on pull request 868. The records stay joined and the data do not change. Each gap gets a boundary as eeg_eegrej writes it in EEGLAB: half a sample before the first sample after the gap, duration the missing time in samples, not rounded (NaN where two records overlap, as pop_mergeset). tests/data/edf_plus_d_gaps.edf is the example file, written by tools/write_edf_plus_d_gaps.py.
@arnodelorme

Copy link
Copy Markdown
Collaborator Author

For question 3, this figure shows 3 seconds of one ds003343 file read from byte 4608 (the header-length field, as EEGLAB reads) and from byte 5376 (as the new reader reads): only the first reading gives continuous signals and real triggers, as the comment above explains (#868 (comment)).

ds003343, sub-23 run 03, read from byte 4608 and from byte 5376

The second figure (sub-18, run 02) and the script that drew both are in the same commit: figure, script q3.py. Nothing is changed in where the reader starts: this waits for Arno's choice.

— orchestrator

@arnodelorme

Copy link
Copy Markdown
Collaborator Author

Automatic checks and EEGLAB reference workflow on the initial reader implementation. Both workflow runs started for the initial implementation of this pull request have now finished with zero failures.

The EEGLAB reference workflow completed and passed all seven suites (study, core, misc, parity, stats, heavy, and hybrid). EEGLAB reference run: https://github.com/sccn/eegprep/actions/runs/46284661990

On commit 46f86773 (after merging commit 00607eb0 of pull request #857 to turn off BioSig on Windows, plus develop), Documentation Build, Synced release dry run, Pre-commit checks, Ruff and ty, Base install without optional extras, EEGLAB Python backend, Test Python 3.12 on Windows, Test Python 3.12 on macOS, and Test Python 3.12 on Ubuntu 22.04 all passed. The remaining three jobs of that run were still running cleanly when the new commits for Arno's follow-up answers (2d503d71) superseded it and started a fresh run. Tests run on 46f86773: https://github.com/sccn/eegprep/actions/runs/46287309736

— orchestrator

@arnodelorme

Copy link
Copy Markdown
Collaborator Author

Question 2 is answered and done: the last channel becomes events only when it holds triggers. Arno answered "2a. Yes, leave it out. The last channel can stay as a data channel", and asked for the same test in EEGLAB and EEGPrep, with a pull request on EEGLAB if needed.

What was found. On the 17 files of the comparison where EEGLAB's rule fired, it was right 4 times and removed a signal 13 times. The number of different values cannot decide alone: a contact quality channel (values 0 and 4) and a saturated channel (2 values) look like a trigger channel.

The rule now, the same in both programs. The last channel becomes events and is removed only when its label names a trigger channel (status, trig, event, marker, or a label that starts with sti) and it holds few different values (20 at most in its first 100 samples, 1000 at most overall). Any other last channel stays data. pop_readedf(file, importevent='off') turns the rule off, as in pop_biosig.

Result. The channel count differed from EEGLAB's on 19 of the 44 files both programs read. It now differs on 2, the two ds003343 files of question 3, which is still open. In the four files where the channel becomes events, the events are those of EEGLAB (same types and latencies).

EEGPrep follows the EEGLAB branch, not EEGLAB develop, on 13 files until pull request 1005 of sccn/eeglab is merged (biosig2eeglab: the last channel becomes events only when it holds triggers: sccn/eeglab#1005, branch agent/issue-1004, commit b30386c05, issue 1004: sccn/eeglab#1004). Please merge that one first. The tests are in pull request 18 of sccn/eeglab_tests (Test for the last channel of EDF and BDF files in pop_biosig: sccn/eeglab_tests#18).

Details
  • Commit 22d9ea2 on this branch: _is_last_event_channel and _last_channel_events in src/eegprep/plugins/EEG_BIDS/edf_bdf.py, ported line for line from islasteventchan and the event loop of biosig2eeglab.m, and importevent in pop_readedf. EEGLAB does not call pop_chanevent here: it has its own loop (the code of a sample is its value modulo 65536, and an event starts where the code is not 0 and differs from the sample before), and this loop is what was ported.
  • With importevent='off' no event is imported and the last channel is kept. The boundary events at the gaps of EDF+D files are kept.
  • BIDS import: unchanged. The events of events.tsv replace the events of the file, and a trigger channel is removed either way, as in EEGLAB's BIDS import.
  • Tests on real files in tests/test_pop_readedf.py: DIG DTRIG becomes 12 events (values from the EEGLAB branch), the 50 Hz sine wave stays data, and the label decides for EEG O2 of EEGLAB's test.edf. Two public recordings of 115 kB were added in tests/fixtures/edf.
  • The EEGLAB reference workflow was started on this branch at commit 5778057: https://github.com/sccn/eegprep/actions/runs/46291295040 . It runs against the EEGLAB commit pinned in tests/eeglab_tests/__init__.py (d0963be9d), whose EDF and BDF test files are not among the 13 files where the rule changed.
  • Not done by this change: the ds002725 file with a Status channel in EDF is read by EEGPrep (46 channels, 733 events, the codes of its events.tsv) and cannot be read by pop_biosig, before or after, for another reason.

— orchestrator

@arnodelorme

arnodelorme commented Nov 29, 2026 •

Copy link
Copy Markdown
Collaborator Author

Question 1, answer 1b: a boundary event at each gap of EDF+D and BDF+D files

Arno answered by email on November 29, 2026: "1b. Add boundary to each gap, but file an issue for EEGLAB along with an example file the requirement to test against Python. After that, create a pull request in EEGLAB." This is done in commit 84d3498 of this branch.

  • What the reader does now: the records stay joined and the data do not change. Each gap gets one boundary event, half a sample before the first sample after the gap, whose duration is the missing time in samples.
  • Difference from EEGLAB develop, chosen by Arno: on these files EEGPrep now has boundary events and EEGLAB develop has none. The data are the same. The difference ends when the EEGLAB pull request is merged: pop_biosig: boundary event at each gap of EDF+D and BDF+D files eeglab#1006 (issue pop_biosig joins the records of an EDF+D file without a boundary event eeglab#1003, test Test for the boundary events at the gaps of an EDF+D file eeglab_tests#19).
  • EEGLAB with that change against EEGPrep, on the example file tests/data/edf_plus_d_gaps.edf: 400 samples in both, largest data difference 0 µV, and the same three boundaries (latencies 100.5, 200.5 and 350.5, durations 50, 12.5 and 100 samples). On edfPlusD.edf both give the same 5 boundaries (latencies 200.5, 400.5, 1600.5, 1800.5 and 2000.5, durations 200, 200, 400, 400 and 600 samples).
  • Nothing else moved: on the 55 files of the comparison, the 44 other files the reader reads are identical before and after the commit, data and events, and no file without gaps has a boundary.
  • Open point for Arno: the BIDS import replaces the events of the file by those of events.tsv by default, as pop_importbids does in EEGLAB, so the boundaries are then lost in both programs. They are kept with bidsevent set to False, 'merge' or 'append'. The question is in the email.
The rules, and why
  • Latency: the first sample after the gap minus 0.5, counted from 1, as eeg_eegrej.m writes a boundary. A gap is always at the edge of a record, so this is a whole number plus 0.5.
  • Duration: the missing seconds times the sampling rate, not rounded (0.25 second at 50 Hz is 12.5 samples). EEGLAB does not round event durations. Those of eeg_eegrej are whole numbers only because it removes whole samples, and it adds durations as they are: removing samples 195 to 205 of the example in EEGLAB gives one boundary of 23.5 samples.
  • Records that overlap or go backward in time: a boundary with duration NaN, which is how pop_mergeset.m writes a break of unknown length. Never a negative duration.
  • Before the first record: no boundary. A file that starts later than time 0 has no gap there (the first time stamp is the start of the data).
  • What counts as a gap: at least one sample missing between the end of a record and the start of the next one, records taken in file order. Less than one sample is not a gap: no sample is missing, the time stamps are decimal text, and eeg_checkset in EEGLAB removes a first boundary shorter than one sample, which would make the two programs differ (review by Codex, commit 9d97943). The start of a record is the time-keeping annotation that opens it (EDF+ specification, section 2.2.4, https://www.edfplus.info/specs/edfplus.html).
  • Mixed sampling rates: latency and duration count samples of the common rate of the data.
  • Order: a boundary comes last among events at the same latency, as when eeg_insertbound appends it and eeg_checkset sorts the events. Its type is the one eeg_boundarytype gives.
  • Annotations: their latencies and durations are unchanged, in recorded samples as since commit bbb7443. The tests show a boundary moves none of them.
  • The warning that gives the number of gaps is kept.
The example file and the tests
  • tests/data/edf_plus_d_gaps.edf (4160 bytes, in tests/data, where the repository keeps its small test files) is written by tools/write_edf_plus_d_gaps.py: 3 channels at 50 Hz, 8 records of 1 second that start at 0, 1, 3, 4, 5.25, 6.25, 7.25 and 10.25 seconds, a ramp that counts the recorded samples, and two annotations.
  • edfPlusD.edf (R package edfReader 1.2.1 by Jan Vis, license GPL-3) is not committed. The GPL would allow it as a separate work beside BSD code, but it would bring the GPL-3 text and its obligations into this repository and into EEGLAB's. It was checked on this Mac: its 5 boundaries are those worked out by hand from its time stamps (records of 1 second at 200 Hz that start at 0, 2, 4, 5, 6, 7, 8, 9, 12, 15 and 19 seconds).
  • New tests in tests/test_pop_readedf.py: the three boundaries of the example file by hand, a file that starts late, mixed sampling rates, the BDF+D twin of the file, gaps of 0.75 and 1 sample, and the BIDS reader. Updated tests in tests/test_bids_load_frombids_helpers.py: a gap of whole samples with annotations around it, records that go backward (NaN), and pop_load_frombids with each bidsevent mode.

— orchestrator

After the review by Codex GPT-6.1 Terra. A difference of less than one sample between two records is no longer a gap: no sample is missing, and eeg_checkset in EEGLAB removes a first boundary shorter than one sample, so both programs now return the same boundaries. The example file writer also gives the BDF+D twin of the file, used by a new test.
@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: sccn/eeglab#1005) and its Python port (pull request 868 of 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 6e83f9d1b, EEGPrep commit c9f77ff).
  • 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

Copy link
Copy Markdown
Collaborator Author

Code review

The boundary events at the gaps of EDF+D and BDF+D files were reviewed by another AI, read-only: Codex, model GPT-6.1 Terra. It read the three changes together (commit 84d3498 of #868, the first commit of sccn/eeglab#1006 and the test of sccn/eeglab_tests#19) and asked for changes, with four findings it rated blocking and four rated "should fix". Gemini 4 Carbon at medium effort (through agy) was asked first with the same text and had returned nothing after 20 minutes. Three of the blocking findings and one other are fixed, in commits 9d97943 (sccn/eegprep), 49a92d04c (sccn/eeglab) and 5b8a3e7 (sccn/eeglab_tests). Four are left, with the reason for each.

Fixed

  1. BDF+D files (blocking). BIOSIG reads the annotation bytes of a BDF+ file from a wrong offset (sopen.m multiplies an offset in bytes by 3 again), so the first EEGLAB commit, which took the record start times from HDR.EDFplus.ANNONS, could not work on BDF+D files. I checked it: on the BDF+D twin of the example file that field holds signal bytes. EEGLAB now reads the first annotation channel from the file itself. EEGLAB and EEGPrep give the same three boundaries on bdf_plus_d_gaps.bdf, which is in both test suites.
  2. Annotation channels that are not next to each other (blocking). BIOSIG then leaves HDR.EDFplus.ANNONS empty, and EEGLAB would have placed no boundary where EEGPrep does. The same direct read fixes it: both programs take the first annotation channel, as the EDF+ specification says. No test file has this layout.
  3. A first boundary shorter than one sample (blocking). eeg_checkset removes the first event when it is a boundary with a duration under 1, wherever it is, so EEGLAB would have dropped a gap of 0.75 sample that EEGPrep kept. Both programs now count a gap from one sample on, so no such boundary is created. Tested in both with gaps of 0.75 sample (no boundary) and 1 sample (boundary).
  4. The MATLAB test accepted NaN (should fix). The comparison is rewritten so that a NaN in a latency or a duration fails the test.

Left, and why

  1. Gaps shorter than one sample are not marked (rated blocking). The reviewer asked for a boundary at any gap larger than floating point error. I did the opposite, see 3: under one sample no sample is missing, the time stamps are decimal text that writers round, and EEGLAB itself treats a boundary under one sample as nothing. The first version used half a sample.
  2. A record without a time stamp (should fix). It is taken to follow the record before it, as the reader did before this change. Refusing such a file would stop reading files that EEGLAB reads today.
  3. Memory mapped import with a data range and records that are not 1 second long (should fix). pop_biosig then reads the data and counts the range in two different units. This is in pop_biosig.m on develop, and it places every event of the file, not only the boundaries. The boundaries follow the same range as the other events. Not changed here.
  4. NaN durations and pop_epoch (should fix). With an epoch window that starts after the event, pop_epoch subtracts the durations of the boundaries in between, so a NaN duration turns the latency into NaN. This is true of every boundary pop_mergeset writes, on develop. NaN is only given to records that overlap or go backward in time, which the EDF+ specification does not allow, and a duration of 0 would be removed by eeg_checkset when the boundary comes first.

After the fixes: the 59 tests of the two EEGPrep test files pass, the 9 EEGLAB tests pass in MATLAB R2025b, the 55 files of the EEGPrep comparison are identical before and after the fix commit, and on 54 files EEGLAB develop and EEGLAB with the change differ only by the boundaries of the two files with gaps, which are the same as EEGPrep's.

— orchestrator

After the review by Gemini 4 Carbon. A text boundary after numeric BioSemi triggers was not found by the functions that look for boundaries, which read the type of the first event. As eeg_checkset does in EEGLAB, event types that mix numbers and text all become text when a boundary is added. The boundary type is now taken from the events sorted in time.
@arnodelorme

Copy link
Copy Markdown
Collaborator Author

Code review, second reviewer

The review by Gemini 4 Carbon at medium effort (through agy) arrived after the comment above, 22 minutes after it was asked. It read the first commits, before the fixes made for Codex, and also asked for changes, with 5 findings it rated blocking, 3 "should fix" and one nit. Four repeat findings of Codex that were already fixed or answered. Two are new and fixed, in commits 169f1a3 (sccn/eegprep) and the third commit of sccn/eeglab#1006.

New and fixed

  1. A boundary was not found among BioSemi triggers, in EEGPrep (blocking). The triggers of a Status channel have numeric types and the boundary a text type, and the functions that look for boundaries read the type of the first event, so a filter would have run across the gap of a BDF+D file with triggers. EEGLAB does not have this defect, because eeg_checkset turns all types into text when they are mixed. The reader now does the same when it adds a boundary, and takes the boundary type from the events sorted in time. A new test reads a BDF+D file with a Status channel and finds its three boundaries.
  2. The message of EEGLAB counted gaps outside the data range (should fix). With 'blockrange' and no gap inside the range, pop_biosig said boundaries were added and checked the events again. It now says nothing and leaves the events as they are.

Already fixed for Codex: the wrong offset of BIOSIG in BDF+ files, annotation channels that are not next to each other, a first boundary shorter than one sample (Gemini adds that eeg_checkset.py of EEGPrep has the same rule as EEGLAB's), and the MATLAB test that accepted NaN.

Left, and why

  1. NaN durations and pop_epoch (rated blocking). As said above for EEGLAB. Gemini adds that in EEGPrep pop_epoch stops with an error in that case (an epoch window that starts after the event, beyond a boundary of duration NaN) where EEGLAB leaves a NaN latency. This needs records that overlap or go backward in time, which the EDF+ specification does not allow, and it is the behaviour of pop_epoch with any boundary of unknown length. Not changed here: it belongs to pop_epoch in both programs.
  2. Records without a time stamp (should fix). Same answer as above. Gemini also notes that text left after the end of a record's annotations can be read as a time by both programs, and stops the Python reader when it is not a number. Not changed: this reading of the time stamps is older than the boundaries.
  3. 'importevent', 'off' still returns the boundaries (should fix). This is a choice, the same in both programs: a boundary describes the data, not the experiment, and a filter needs it whether the events are imported or not.
  4. A sampling rate that is not a whole number, with a data range (should fix). pop_biosig and BIOSIG then round the first sample of the range differently, for every event. Not changed here.
  5. The changelog says EEGLAB places no boundary yet (nit). True until pop_biosig: boundary event at each gap of EDF+D and BDF+D files eeglab#1006 is merged. To update then.

After these fixes the 60 tests of the two EEGPrep test files pass, the 9 EEGLAB tests pass in MATLAB R2025b, and the 55 files of the EEGPrep comparison are unchanged.

— orchestrator

@arnodelorme

arnodelorme commented Nov 29, 2026 •

Copy link
Copy Markdown
Collaborator Author

Checks after the boundary events at the gaps of EDF+D and BDF+D files (answer 1b), on commit 169f1a3 and on the branch head bdd972c. This is the final state of the automatic checks of the three pull requests on November 29, 2026 at 11:41, once the two code reviews were answered and the reference workflow finished.

Next step, if Arno asks for it: moving the pinned EEGLAB commit EEGLAB_TESTS_EEGLAB_COMMIT in tests/eeglab_tests/__init__.py (still d0963be9) and the EEGLAB copy tested by sccn/eeglab_tests forward to an EEGLAB develop commit that includes pull requests 1005 and 1006 would let both test suites check the new boundaries directly. This pull request and pull request 19 are not merged.

— orchestrator

@arnodelorme

arnodelorme commented Nov 29, 2026 •

Copy link
Copy Markdown
Collaborator Author

Last channel rule (question 2): the checks passed, and the rule is now in EEGLAB develop. This closes the work on Arno's answer 2a.

  • EEGLAB reference run. The run on commit 5778057, which holds the rule, passed in all seven parts (https://github.com/sccn/eegprep/actions/runs/46291295040). It compares with the EEGLAB commit this repository pins (d0963be9), which is older than the rule in EEGLAB, so no reference test depended on the old rule.
  • Tests of this pull request. On commit 169f1a3 the tests passed on macOS, Windows and Ubuntu (Python 3.12 and 3.13), with ruff, the documentation and the EEGLAB Python backend. Only the Pyodide part had not started yet.
  • EEGLAB. Arno answered that the label stays required, and at his request pull request 1005 (biosig2eeglab: the last channel becomes events only when it holds triggers: biosig2eeglab: the last channel becomes events only when it holds triggers eeglab#1005) was merged with its test (Test for the last channel of EDF and BDF files in pop_biosig eeglab_tests#18). EEGPrep therefore follows EEGLAB develop on the 13 files where the rule changed, and I updated question 2 of the description to say so.
  • Conflict with develop, solved. After two other pull requests were merged into develop, this one conflicted with it in the changelog only. I merged develop into the branch (commit bdd972c) and kept both changelog entries. No code of the reader changed in that merge. The tests started again on that commit and I did not wait for their result. On this Mac the reader and latency tests pass on that commit, and so do the menu tests except one, which needs the package defusedxml that is not installed here. The 27 console tests that fail here fail the same way on develop, because the package onnxruntime is not installed on this Mac either.

Question 3 (the two ds003343 files) is untouched and still waits for Arno's choice.

— orchestrator

@arnodelorme

Copy link
Copy Markdown
Collaborator Author

🤖 Review (Claude Fable 5.2), on the branch merged with develop (1c125896): one finding to decide, one open question, the rest is sound.

The branch was written by Gemini 4 Carbon and reviewed twice on November 29 by Codex and Gemini. This is a third read by another vendor, of the whole reader (edf_bdf.py, 331 lines), pop_readedf.py and their tests, after merging develop.

Important

  1. An annotation that falls between two samples gets a fractional latency. read_edf_bdf computes (onset - t0) * srate + 1 and stores it unrounded (and as numpy.float64). In a small EDF+ file at 10 Hz with annotations at 0.25 s and 1.999 s, pop_readedf returns latencies 3.5 and 20.99, and a duration of 4.7 samples.
    • BIOSIG stores whole samples in its event table, and EEGPrep's own pop_biosig rounds the same way (index = round(event["POS"] * rate)), so the two EEGPrep readers disagree on the same file.
    • The 55-file comparison did not see it, because the annotations of those files fall on samples.
    • Suggested: latency = floor((onset - t0) * srate + 0.5) + 1 (MATLAB's round, not Python's, which rounds 2.5 to 2), stored as a Python int, and one test with an annotation between samples. Durations can stay as they are: pop_biosig does not round them either.
    • This is a choice between EEGLAB's whole samples and the exact time of the EDF+ specification, so it is Arno's to make.

Open question, unchanged

  1. Question 3, the 59 ds003343 files. The evidence of the comment of November 29 is clear: the data start at the header-length field (byte 4608), as EEGLAB and BIOSIG read them. The reader still starts at 256 × (channels + 1). I recommend the proposal of that comment: start at the header-length field, with a warning when it disagrees with the number of channels. Nothing is changed here until Arno answers.

Checked, no change needed

  • Calibration digital * Cal + Off in double precision, then float32, as BIOSIG. Records, record count (announced −1 or 0, shortened file) and the 24-bit decoding.
  • Mixed sampling rates: repeated up to the least common multiple, with the memory limit.
  • Gaps of EDF+D and BDF+D files: boundary half a sample before the first sample after the gap, duration in samples, NaN for overlapping records, all types turned to text when the boundary is text.
  • Last channel rule: label and number of values, as biosig2eeglab.m since biosig2eeglab: the last channel becomes events only when it holds triggers eeglab#1005.
  • No capture image is left in the tree (tools/_captures_issue865 is gone after the merge with develop).

Nits

  • edf_bdf.py imports two private helpers of pop_biosig.py (_decode_bdf_status, _replace_events). A shared module would be cleaner. Not blocking.
  • A channel whose digital minimum equals its digital maximum gives a division by zero (infinite values) without a message. BIOSIG also returns unusable values there.

This branch has not been deployed

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant