Skip to content

pop_biosig: boundary event at each gap of EDF+D and BDF+D files - #1006

Merged
arnodelorme merged 4 commits into
developfrom
agent/issue-1003
Nov 29, 2026
Merged

arnodelorme merged 4 commits into
developfrom
agent/issue-1003

Conversation

@arnodelorme

@arnodelorme arnodelorme commented Nov 29, 2026 •

Copy link
Copy Markdown
Collaborator

pop_biosig now places a boundary event at each gap of a discontinuous EDF+D or BDF+D file. The data do not change. Fixes #1003.

Example file edf_plus_d_gaps.edf (8 records of 1 second at 50 Hz that start at 0, 1, 3, 4, 5.25, 6.25, 7.25 and 10.25 seconds, https://raw.githubusercontent.com/sccn/eegprep/84d349821b7158427bf55f8fc8e97816b07e5df1/tests/data/edf_plus_d_gaps.edf), read with EEG = pop_biosig('edf_plus_d_gaps.edf'):

Samples Events (type, latency, duration in samples)
Before (develop, 579a27d) 400 resp 287.5, 10
After (this pull request) 400 boundary 100.5, 50 · boundary 200.5, 12.5 · resp 287.5, 10 · boundary 350.5, 100

EEGLAB with this change against EEGPrep (Python), on the example file (EEGPrep's reader at commit 84d34982 of pull request sccn/eegprep#868, sccn/eegprep#868):

EEGLAB with this change EEGPrep (Python) Difference
Number of samples 400 400 0
Largest data difference (3 channels) 0 µV
Boundary 1, latency 100.5 100.5 0
Boundary 1, duration 50 50 0
Boundary 2, latency 200.5 200.5 0
Boundary 2, duration 12.5 12.5 0
Boundary 3, latency 350.5 350.5 0
Boundary 3, duration 100 100 0
  • BDF+D: the same recording written as a BDF+D file (bdf_plus_d_gaps.bdf, 24-bit samples) gives the same three boundaries in EEGLAB with this change and in EEGPrep.
  • Second file: on edfPlusD.edf of the R package edfReader (5 gaps, 2200 samples), EEGLAB with this change and EEGPrep give the same 5 boundaries (latencies 200.5, 400.5, 1600.5, 1800.5, 2000.5, durations 200, 200, 400, 400, 600) and the same data (0 µV on the 10 channels EEGLAB keeps).
  • Files without gaps: pop_biosig was run on 54 files before and after. The 44 files without gaps that EEGLAB reads are identical (data and events), 8 files give the same error before and after, and the 2 files with gaps gain only their boundaries.
  • Not changed by this pull request: the two annotations of the example file differ between EEGLAB and EEGPrep, before and after. EEGLAB places resp at 287.5 (EEGPrep: 226, its place in the joined data) and does not import stim, which has no duration (EEGPrep: 126). Both are described in issue pop_biosig joins the records of an EDF+D file without a boundary event #1003 under "Seen on the way".
  • Tests: pull request 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) adds the example file and a test of these values.
Where the gaps were lost, and where the record start times come from
  • BIOSIG's sread returns the data records one after the other, and sopen creates no event for a gap. BIOSIG is third party code and is not changed.
  • The record start times are read from the file itself, in a small read of the first annotation channel (the header length, the bytes per record and the place of the channel come from BIOSIG's HDR.HeadLen and HDR.AS). BIOSIG also returns these bytes in HDR.EDFplus.ANNONS, and the first commit used them, but the review showed two defects there: in BDF+ files BIOSIG reads them from a wrong offset (sopen.m multiplies an offset in bytes by 3 again), and it leaves the field empty when the annotation channels are not next to each other. On bdf_plus_d_gaps.bdf the field holds signal bytes.
  • The header field EDF+D or BDF+D comes from HDR.reserved1.
  • The change is in functions/sigprocfunc/biosig2eeglab.m, in a new subfunction gapboundaries, because this file is where EEGLAB turns what BIOSIG returns into events and where it has the data range (interval) of the blockrange option. pop_biosig.m is not changed.
  • 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). A record without one follows the record before it.
The rules, the same as in EEGPrep
  • Latency: half a sample before the first sample of the record after the gap, as eeg_eegrej writes a boundary.
  • Duration: the missing seconds times the sampling rate, not rounded. EEGLAB does not round event durations: eeg_eegrej on samples 195 to 205 of the example then gives one boundary of 23.5 samples (11 removed plus the 12.5 of the gap).
  • Two records whose time stamps overlap or go backward: a boundary with duration NaN, as pop_mergeset writes a break of unknown length. Never a negative duration.
  • No boundary before the first record. A difference of less than one sample between two records is not a gap: no sample is missing, the time stamps are decimal text, and eeg_checkset removes a first boundary shorter than one sample, which would make EEGLAB and EEGPrep differ.
  • Channels at different sampling rates: latency and duration count samples of the common rate of the data (HDR.SPR samples per record).
  • The boundary is appended and eeg_checkset sorts the events, so it comes last among events at the same latency. Its type is the one eeg_boundarytype gives.
  • Only continuous data get boundaries (EEG.trials of 1), and only files whose header says EDF+D or BDF+D.
  • The boundaries are added whether the events are imported or not ('importevent', 'off'): a boundary describes the data. EEGPrep does the same.
What was checked not to change
  • 'blockrange': pop_biosig(file, 'blockrange', [1 6]) returns samples 51 to 300 as before, with the two boundaries inside the range at 50.5 and 150.5, counted from the first sample read.
  • History command: unchanged (EEG = pop_biosig('edf_plus_d_gaps.edf'); and EEG = pop_biosig('edf_plus_d_gaps.edf', 'blockrange',[1 6] );).
  • Memory mapped option: same result before and after on the continuous file S001R01.edf of eeglab_tests (8000 samples, 1 event, no boundary). On the example file it stops before and after with the same error, "Output argument DAT not assigned" in pop_biosig>readfile, because the file is shorter than one memory block.
  • 'blockepoch', 'on': on the example file this call does not return on develop, without this change (stopped after 100 seconds). The new code does not run for epoched data. This is reported in issue pop_biosig joins the records of an EDF+D file without a boundary event #1003 and not fixed here.
  • One side effect: when a boundary is added, the other events of the file get a duration of 0 where they had no duration field, as after eeg_eegrej. It is eeg_checkset that fills the field. Types and latencies of the other events are unchanged (checked on the 2199 events of edfPlusD.edf).
  • Functions that read boundaries, on the example file after import: eeg_checkset leaves the events as they are, pop_epoch keeps an epoch with no boundary inside and removes one that crosses a boundary, eeg_eegrej and pop_select keep the boundaries and add theirs.
  • Tests of sccn/eeglab_tests that call pop_biosig, biosig2eeglabevent, pop_epoch and eeg_eegrej (folders unittesting_binary/pop_biosig, unittesting_sigprocfunc/biosig2eeglabevent, unittesting_popfunc/pop_epoch, unittesting_popfunc/eeg_eegrej), MATLAB R2025b: the 8 existing tests pass before and after. The new test pass_edfplusd_boundaries (EDF+D file, BDF+D twin, data range, gaps of 0.75 and 1 sample, a continuous file) fails before and passes after. No test folder of eeglab_tests is named after biosig2eeglab or eeg_checkset.
  • The 54 files are the 55 of the EEGPrep comparison (tools/edf_bdf_reader_compare.m of sccn/eegprep) plus the example file, less two files on which pop_biosig does not finish on develop (test_generator.bdf and one more file of pyedflib, whose channels have a common rate of millions of samples per record).

Refs sccn/eegprep#868, sccn/eegprep#865.

Three commits: the change, the fixes after the review by Codex GPT-6.1 Terra, and one after the review by Gemini 4 Carbon (the two review comments below list them).

🤖 Generated with Claude Code

pop_biosig joins the data records of a discontinuous EDF+D or BDF+D file. biosig2eeglab now places a boundary event at each gap: half a sample before the first sample after the gap, with the missing time in samples as duration (NaN where two records overlap). The record start times are the time-keeping annotations BIOSIG returns in HDR.EDFplus.ANNONS. The data and files without gaps do not change. Fixes #1003.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
@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.

After the review by Codex GPT-6.1 Terra. BIOSIG's HDR.EDFplus.ANNONS is read from a wrong offset in BDF+ files and is empty when the annotation channels are not next to each other, so the time-keeping annotations are now read from the first annotation channel of the file. A gap counts from one sample on, since eeg_checkset removes a first boundary shorter than one sample.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
@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 84d34982 of sccn/eegprep#868, the first commit of #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 9d979439 (sccn/eegprep), 49a92d0 (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: with a data range that holds no gap, nothing is printed and the events are left as they are.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
@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 169f1a31 (sccn/eegprep) and the third commit of #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 #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

# Conflicts:
#	functions/sigprocfunc/biosig2eeglab.m
@arnodelorme

Copy link
Copy Markdown
Collaborator Author

Pull request #1005 was merged, so develop is now merged into this branch (commit bbaf064), as Arno asked on November 29, 2026. The only conflict was in biosig2eeglab.m, where both pull requests add a function at the end of the file: both functions are kept, nothing else changed. The 9 EEGLAB tests on pop_biosig, eeg_eegrej, pop_epoch and biosig2eeglabevent, including the new boundary test of sccn/eeglab_tests#19, pass on the merged branch in MATLAB R2025b. GitHub now reports the pull request as mergeable.

— orchestrator

@arnodelorme
arnodelorme merged commit 5e0be87 into develop Nov 29, 2026
2 checks passed
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.

pop_biosig joins the records of an EDF+D file without a boundary event

1 participant