Skip to content

[IO] Keep writegdf endpoint samples inside the GDF digital range - #849

Merged
arnodelorme merged 2 commits into
developfrom
agent/20261129-fix-847
Nov 29, 2026
Merged

arnodelorme merged 2 commits into
developfrom
agent/20261129-fix-847

Conversation

@claude

@claude claude Bot commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

BioSig overflow detection returns NaN for samples at or beyond a channel's DigMin or DigMax, and writegdf set those limits to the data extremes, so integer endpoint samples read back as NaN. writegdf now leaves one count of headroom on both sides; the stored float64 samples are unchanged. Adds a regression test on the written header and a changelog entry.

This repairs the files EEGPrep writes. The reader side of #847 (a GDF exported by EEGLAB still loses its largest sample when read with overflow detection on) stays open.

Checked with EEGLAB's BioSig 3.8.4 in MATLAB R2025b (sopen then sread, measured in the review below on head 72ff9b04; the code has not changed since):

Data Writer DigMin / DigMax NaN, overflow on NaN, overflow off
Issue example, integers, 2 x 16 develop [-10 6] / [5 21] 4 of 32 0
Issue example, integers, 2 x 16 this branch [-11 5] / [6 22] 0 0
Zeros, constant 7, ramp -3 to 3, 3 x 50 develop [-1 6 -3] / [1 8 3] 2 of 150 0
Zeros, constant 7, ramp -3 to 3, 3 x 50 this branch [-1 6 -4] / [1 8 4] 0 0
Random noise, 3 x 500 develop [-156 -118 -128] / [123 100 115] 0 0
Random noise, 3 x 500 this branch [-157 -119 -129] / [124 101 116] 0 0

For the three files of this branch, the samples read with overflow detection on equal the input exactly (largest absolute difference 0).

Part of #847

🤖 Generated with Claude Code

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor Author

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.

@claude

claude Bot commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor Author

Claude finished @claude[bot]'s task in 52s —— View job


Code review

  • Overall assessment: The fix is correct. BioSig's sread marks samples <= DigMin or >= DigMax as overflow, and the old _channel_limits set those limits to floor(min) and ceil(max). Any integer-valued endpoint sample therefore read back as NaN. Adding one count on each side moves every sample strictly inside the range. PhysMin/PhysMax are still written equal to DigMin/DigMax, so the calibration stays at gain 1, offset 0, and the stored float64 samples are unchanged. The old special case for constant channels is no longer needed, because lower < upper now always holds.
  • Highest-risk area: Header calibration in _channel_limits / _signal_header. I checked it and it is fine.
  • Merge recommendation: Safe to merge after the changelog entry below.

Blocking

None.

Important

  • Missing changelog entry (docs/source/changelog.rst, Unreleased). Users can see this fix: before it, GDF files from pop_writeeeg/writegdf had NaN endpoint samples when opened in EEGLAB/BioSig. AGENTS.md says "Any user-facing EEGPrep change must include matching Sphinx updates under docs/source/ in the same branch", and the other recent bug fixes (e.g. the ASR pseudo-inverse fix) have entries under Unreleased. Suggested entry: "GDF files written by pop_writeeeg/writegdf leave one count between the data and each channel's digital limits, so BioSig no longer reads the minimum and maximum samples as overflow (NaN). (pop_biosig: report valid GDF extrema lost to the Python binding’s overflow handling #847)"

Nits

None.

Test gaps

None. The new test reads DigMin/DigMax from the written header, which is behaviour visible in the output file. The offsets are correct: the GDF 1.25 signal header is laid out field by field, with DigMin at ns*(16+80+8+8+8) and DigMax 8·ns bytes after it. The test also fails on the old code, because row 0 has an integer minimum of -10. No Python BioSig reader is available, so checking the header is a reasonable stand-in for a round-trip test.

EEGLAB parity notes

None. writegdf is EEGPrep's own writer; EEGLAB writes GDF through BioSig. With this change, EEGPrep's output is read without data loss by the BioSig overflow rule EEGLAB applies on import.

Checked for correctness bugs, EEGLAB/BioSig compatibility, GDF header layout, changed-behavior tests, and CLAUDE.md/AGENTS.md compliance.

@arnodelorme

Copy link
Copy Markdown
Collaborator

Review (Claude Fable): needs more work

Details

What was checked

Head 72ff9b04. Diff: src/eegprep/functions/sigprocfunc/writegdf.py (+3, −5) and tests/test_writegdf.py (+15). No window changes, so the GUI rules on captures do not apply here.

Is the cause right? Yes. BioSig marks a sample as overflow when it is at or beyond a limit (sread.m lines 1523 to 1529: (S<=TH(1)) | (S>=TH(2)), with TH = [DigMin DigMax] from sopen.m line 604). The old writer set the limits to floor(min) and ceil(max), so a whole-number minimum or maximum sat exactly on a limit. One count of headroom on each side removes that. PhysMin and PhysMax are still written equal to DigMin and DigMax, so the gain stays 1 and the offset 0: the stored float64 samples do not change. The old special case for a constant channel is no longer needed, and its removal is part of the fix, not a refactoring.

Measured with BioSig 3.8.4 in MATLAB R2025b (sopen then sread, files written by the develop writer and by this branch)

Data Writer DigMin / DigMax NaN, overflow on NaN, overflow off
Issue example, integers, 2 x 16 develop [-10 6] / [5 21] 4 of 32 0
Issue example, integers, 2 x 16 this branch [-11 5] / [6 22] 0 0
Zeros, constant 7, ramp -3 to 3, 3 x 50 develop [-1 6 -3] / [1 8 3] 2 of 150 0
Zeros, constant 7, ramp -3 to 3, 3 x 50 this branch [-1 6 -4] / [1 8 4] 0 0
Random noise, 3 x 500 develop [-156 -118 -128] / [123 100 115] 0 0
Random noise, 3 x 500 this branch [-157 -119 -129] / [124 101 116] 0 0
Random noise, 3 x 500, written by EEGLAB writeeeg EEGLAB [0 0 0] / [100 100 100] 1 of 1500 0

For the three files of this branch, the samples read with overflow detection on equal the input exactly (largest absolute difference 0). The pull request body says nothing about a BioSig read, and the issue comment says the fix was not verified against BioSig. This table can be copied into the body so the evidence is where Arno reads it (rule 27). The Python binding itself (libbiosig) is not installed on this Mac, so the check used the MATLAB BioSig, which applies the same inclusive rule.

Why "Fixes #847" is too strong. The plan comment in #847 says that files from other tools with samples at DigMin or DigMax "are genuinely saturated by BioSig's convention, so no upstream raw-data path is needed". EEGLAB does not agree with that:

  • pop_biosig.m reads with overflow detection off by default (line 211, default 'off', and lines 334 to 336, OVERFLOWDETECTION:OFF).
  • writeeeg.m lines 237 to 244 writes a GDF with PhysMax set to the largest absolute value and DigMin 0, DigMax 100. The largest sample of every GDF exported by EEGLAB therefore sits exactly on a limit. Last row of the table: it is lost with overflow detection on, kept with it off.
  • [GUI] Read BioSig files with the optional Python BioSig library #857 forces overflow detection on for GDF in the BioSig reader (pop_biosig.py lines 78 and 90 to 91), because the binding has no switch.

So after this pull request, EEGPrep's own files are safe, and EEGLAB's files still lose samples in the BioSig reader. That remaining part is what the title of #847 describes. It is a behaviour difference from EEGLAB that Arno may want to decide on in #857 or #812, not here.

Tests. tests/test_writegdf.py: 6 passed on this branch. The new test fails on develop (develop writes DigMin equal to the minimum) and passes here. The header offsets in the test are right for GDF 1.25 (DigMin at ns * 120, DigMax 8 bytes per channel later). All 7 test files that use writegdf or pop_writeeeg: 122 passed, 73 deselected (run on macOS with EEGPREP_SKIP_MATLAB=1, main environment with PYTHONPATH on the branch, since uv run --no-sync finds no environment in a fresh worktree).

CI (gh pr checks 849): lint, pre-commit, docs, EEGLAB Python backend and E1 pass. Four jobs fail, on two tests that do not touch this code:

  • tests/test_package_help.py::test_every_public_optional_path_carries_its_marker (pop_mneio: 'mne' against 'mne-io'), on Linux, macOS, Windows and the base install. It comes from the reader commits merged into develop just before this run (db5cff95, 1a74fb97).
  • tests/test_guifunc_primitives.py::test_window_without_a_height_is_as_high_as_its_rows ([57, 28] against [56, 28]), Windows only. It already fails on develop b9ad81c2 and on other pull requests.

Two jobs were still running when this was written (Python 3.13 on Linux, Pyodide).

Other open pull requests. Trial merges of this branch with each of the 17 others: no conflict caused by this branch. #857 edits the same test file in other lines and merges cleanly. #835, #797 and #778 conflict with develop itself (changelog.rst, qt.py), not with this branch. Adding the changelog line here will touch the same Unreleased block as #858, #850 and #835, so the last one merged may need a one-line rebase.

What is needed, exactly

  1. One line under Unreleased in docs/source/changelog.rst, for example: "GDF files written by pop_writeeeg and writegdf keep one count between the data and each channel's digital limits, so BioSig no longer reads the smallest and largest samples as overflow (NaN). (pop_biosig: report valid GDF extrema lost to the Python binding’s overflow handling #847)"
  2. In the body, "Part of pop_biosig: report valid GDF extrema lost to the Python binding’s overflow handling #847" in place of "Fixes pop_biosig: report valid GDF extrema lost to the Python binding’s overflow handling #847", or a new issue for the reader side that names pop_biosig: report valid GDF extrema lost to the Python binding’s overflow handling #847 (rule 33).
  3. The BioSig table above (or the same check redone) in the body.

No change to the code is asked.

— orchestrator review

github-actions Bot and others added 2 commits November 28, 2026 23:33
BioSig marks samples at or beyond DigMin/DigMax as overflow and returns
NaN, so writegdf now widens each channel's limits by one count.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@arnodelorme
arnodelorme force-pushed the agent/20261129-fix-847 branch from 72ff9b0 to 3de2e5c Compare November 29, 2026 07:33
@arnodelorme
arnodelorme merged commit fb3fd99 into develop Nov 29, 2026
12 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.

1 participant