Skip to content

[IO] Place MNE annotations relative to the first imported sample - #841

Closed
claude[bot] wants to merge 1 commit into
developfrom
agent/20261129-fix-838
Closed

claude[bot] wants to merge 1 commit into
developfrom
agent/20261129-fix-838

Conversation

@claude

@claude claude Bot commented Nov 29, 2026

Copy link
Copy Markdown
Contributor

mne_raw_to_eeg converted annotation onsets to latencies without removing raw.first_time, so FIF files with a nonzero first_samp got events shifted by first_samp samples (latency 121 instead of 21). Subtract raw.first_time before converting and add a FIF round-trip regression test.

Fixes #838

🤖 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 58s —— View job


Code review

  • Overall assessment: Looks good. The one-line fix is correct for both MNE annotation timebases. When Annotations.orig_time is None, MNE's set_annotations shifts the onsets by raw.first_time. When orig_time is meas_date, the onsets are measured from sample 0 of the acquisition, not from first_samp. In both cases, onset - raw.first_time gives the time from the first imported sample, so EEGLAB's 1-based latency (onset - first_time) * srate + 1 is right.
  • Highest-risk area: Event latencies from mne_raw_to_eeg, which pop_fileio (MNE reader and biosig paths) and pop_mffimport both use.
  • Merge recommendation: Safe to merge.

Blocking

None.

Important

None.

Nits

None.

Test gaps

None. The new FIF round-trip test fails on the old formula (121) and passes on the new one (21). It goes through read_raw_fif, so it covers the real on-disk first_samp path, not just an in-memory RawArray.

Side effect worth mentioning in the PR description: pop_fileio(..., blockrange=[t0, t1]) on EDF/BDF/GDF crops with raw.crop(tmin=t0) (_crop_raw_to_blockrange, pop_fileio.py:229-234). That makes first_samp nonzero, so annotation latencies on that path were also off by t0 * srate before this fix and are correct now. No existing blockrange test checks event latencies (tests/test_binary_eeglab_tests.py:94-107 checks only shape and metadata), so this PR is what fixes it. The new test already covers the formula, so an extra test is optional.

EEGLAB parity notes

None. EEGLAB event latencies are 1-based relative to the first sample of EEG.data, and the fix now matches that for FIF files with nonzero first_samp and for cropped imports.

Checked for correctness bugs, EEGLAB parity, data structure compatibility, changed-behavior tests, and CLAUDE.md/AGENTS.md compliance. I couldn't run the tests in this job because the job's permissions don't allow uv run pytest.

@arnodelorme

Copy link
Copy Markdown
Collaborator

Review (Claude Fable): needs more work

  • The fix is already on develop. [GUI] Import recordings with the optional MNE readers #842 merged the same line, byte for byte (commit 5b94122, "Match the annotation conversion shared with PR 841"). Merged into develop today, this pull request adds only its 11-line test.
  • That test repeats one develop already has. test_mneio_mixed_channels_and_nonzero_first_sample in tests/test_stream9_readers.py uses the same FIF input (first_samp 100, 100 Hz, onset 0.2 s) and expects the same latency 21. When I revert the line, both tests fail, so the new one catches nothing more.
  • The same defect is still open in eeg_mne2eeg (src/eegprep/functions/miscfunc/eeg_mne2eeg.py line 37). On the same file it returns latency 121 for a dataset of 100 samples, an event outside the data.
  • What is needed: close this pull request as superseded and close pop_mneio: place annotations relative to the first imported sample #838 as fixed by [GUI] Import recordings with the optional MNE readers #842, or rebase it and point it at eeg_mne2eeg with a test of its own.
Details

Is the fix itself right? Yes. I compared mne_raw_to_eeg on this branch with MNE's own conversion (mne.events_from_annotations, minus raw.first_samp, plus 1), in memory and after a FIF save and read (MNE 1.12.1).

Case EEGPrep latency MNE reference
first_samp 0, no measurement date 21 21
first_samp 100, no measurement date 21 21
first_samp 100, measurement date, annotations without orig_time 21 21
first_samp 100, measurement date, annotations with orig_time 21 21
first_samp 137, onset 0.333 s (between two samples) 34.3 34 (MNE rounds)
crop at 2.0 s, onset 2.5 s 51 51
first_samp 100, then crop at 2.0 s 51 51
two recordings joined (first_samp 100 and 300) 21 and 541 21 and 541

When first_samp is 0 the new line gives the same bits as the old one, so ordinary EDF, BDF, GDF and MFF imports do not change.

Side effect not written in the pull request body. pop_fileio(..., blockrange=[t0 t1]) crops the MNE recording, which makes first_samp nonzero. Annotation latencies of a block import therefore now count from the start of the block. This is what EEGLAB does (biosig2eeglabevent.m line 110: EVENT.POS(index) - interval(1) + 1). I checked it on a cropped recording in memory, not on a real EDF file (the EDF writer edfio is not installed here).

Small numerical note, not blocking. On the test file the line returns 20.999999999999996, not 21, which is why the test needs pytest.approx. Over 2,000,000 simulated events placed exactly on a sample (10 sampling rates, random first_samp), (onset - raw.first_time) * sfreq + 1 misses the whole number in 22.9 % of cases and lands just under it in 11.4 %. onset * sfreq - raw.first_samp + 1 misses in 2.9 % and lands just under in 1.5 %, and returns exactly 21.0 on the test file. The error is at most 1e-9 sample in both cases. It is the same one-line change and the same result when first_samp is 0. Worth taking if eeg_mne2eeg is fixed, so that both converters use one formula.

The remaining defect. eeg_mne2eeg computes int(ann['onset'] * sfreq) + 1 without removing the first sample. It is a second MNE to EEG converter with the same cause, and issue #838 only named the paths that share mne_raw_to_eeg. Checked on current develop (1a74fb9): latency 121, pnts 100.

Tests I ran (worktree, uv sync --group dev, Python 3.12.13):

  • tests/test_file_menu_audit_673.py on this branch: 70 passed.
  • The new test against the old line: fails with 121, as the pull request says.
  • This branch merged into develop 1a74fb9, tests/test_file_menu_audit_673.py and tests/test_stream9_readers.py: 78 passed.
  • Same merge with the fix line reverted: 2 failed (the new test and the develop test), 76 passed.
  • tests/test_bdf_records_eeglab_tests.py, tests/test_binary_eeglab_tests.py, tests/test_pop_fileio_brainvision_mat.py, tests/test_eeg_mne2eeg.py: 40 passed, 1 skipped (needs the EEGLAB test suite), 22 deselected (EEGLAB backend tests).

CI. All jobs pass except "Test Python 3.12 on windows-latest": tests/test_guifunc_primitives.py::test_window_without_a_height_is_as_high_as_its_rows, [57, 28] == [56, 28]. The same test fails on develop (run 46199309201, commit b9ad81c) and on #845 and #849, so it does not come from this pull request.

Other open pull requests. No conflict with develop, #845 (same source file) or #836 (same test file). #797 merges cleanly in the test file. Its only conflict is in docs/source/changelog.rst, which this pull request does not touch.

Size and body. 2 source lines and 11 test lines: minimal. No window changes, so no captures are needed. The body gives the numbers as text (121 before, 21 after) and no figure. There is no MATLAB reference run, which seems fair here: the line is MNE only and has no EEGLAB counterpart.

— orchestrator review

@arnodelorme

Copy link
Copy Markdown
Collaborator

Closed at Arno's request (November 28, 2026): the same fix already landed on develop inside #842. The separate problem of eeg_mne2eeg placing an event at latency 121 in a 100 sample dataset is not covered by this pull request. — 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.

pop_mneio: place annotations relative to the first imported sample

1 participant