Skip to content

[IO] Keep MNE montage electrode positions on import - #845

Merged
arnodelorme merged 1 commit into
developfrom
agent/20261129-fix-840
Nov 29, 2026
Merged

arnodelorme merged 1 commit into
developfrom
agent/20261129-fix-840

Conversation

@claude

@claude claude Bot commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

mne_raw_to_eeg now copies each channel's MNE head-frame position into chanlocs X, Y and Z in metres, rotated to EEGLAB's +X nose frame as MNE's EEGLAB export does, so eeg_checkchanlocs fills theta, radius and the spherical fields. Channels without a position stay empty, and each channel keeps its type (EEG, EOG, STIM and so on). Adds a standard_1020 landmark test checked against eeg_mne2eeg, and removes the two sentences of the documentation that said the coordinates were omitted.

Comparison with EEGLAB in MATLAB (R2025b, EEGLAB d558d701c, pop_loadset of sample_data/eeglab_data.set, against mne.io.read_raw_eeglab followed by mne_raw_to_eeg on head a7c90ca0):

Measure over the 32 channels Value
Channels that get a position 32 of 32
Largest theta difference 2.8e-14 degrees
Largest radius difference 5.6e-17
Largest angle between the X, Y, Z vectors 8.5e-07 degrees
Length of X, Y, Z, EEGPrep over EEGLAB 0.1 for every channel (metres, see below)

X, Y and Z are stored in metres as MNE gives them, and no unit is recorded in the dataset. theta, radius, scalp maps, interpolation and ICLabel do not depend on that scale. Arno accepted this on November 28, 2026. The data samples are not touched by this change.

How the comparison was run

MATLAB: EEG = pop_loadset('sample_data/eeglab_data.set'), then labels, X, Y, Z, theta, radius of EEG.chanlocs written to a CSV file (EEG.chaninfo.nosedir is +X). Python: the same file read with mne.io.read_raw_eeglab, converted with mne_raw_to_eeg, and the chanlocs compared channel by channel with the CSV file. Local tests on the rebased branch: tests/test_stream9_readers.py, tests/test_eeg_mne2eeg.py and tests/test_package_help.py, 28 passed.

Fixes #840

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


Code review

  • Overall assessment: Looks good. I checked the head-frame RAS → EEGLAB ALS rotation (X=y, Y=-x, Z=z), how missing and NaN positions are handled, both callers (pop_fileio's EDF/BDF/GDF/CNT/MFF reader path and pop_mffimport), how eeg_checkchanlocs derives theta/radius/sph_*, and the docs.
  • Highest-risk area: pop_mffimport and MFF imports through pop_fileio. read_raw_egi applies the sensor montage, so these imports now come in with positions where they used to have none.
  • Merge recommendation: Safe to merge.

Blocking

None.

Important

None.

Nits

None.

Test gaps

None. The new test checks every derived field against eeg_mne2eeg, which goes through MNE's own EEGLAB export. It also checks the axis orientation on landmarks (Fpz toward +X, T7 toward +Y) and that a channel without a position keeps empty coordinates. That covers the conversion change.

EEGLAB parity notes

  • The rotation matches MNE's EEGLAB exporter and EEGLAB's default nosedir = '+X'. Coordinates stay in metres, as eeg_mne2eeg produces them. theta/radius don't depend on scale, so topoplots will look the same as with EEGLAB-scaled locations.
  • mne_raw_to_eeg changes what the import paths return. Under AGENTS.md §Testing, a maintainer may want the one EEGLAB reference run (eeglab-reference.yml) recorded in the PR body before approving, if they treat this as a core-function change. This is process only and not a code issue.

I didn't run the tests locally because the sandbox doesn't allow pytest. CI Tests was still queued at review time, and the docs build passed.

Checked for correctness bugs, EEGLAB parity, data structure compatibility, changed-behavior tests, and CLAUDE.md/AGENTS.md compliance.

@arnodelorme

Copy link
Copy Markdown
Collaborator

Review (Claude Fable): needs more work

  • It no longer merges. Develop moved after this branch was cut (pop_mneio: new import function through MNE, an optional install like BioSig #813, pop_mneio). Git reports conflicts in _file_io.py and tests/test_stream9_readers.py.
  • The fix itself is correct and small (10 lines in the shared converter, not per reader). On sample_data/eeglab_data.set, the MNE path now gives the same theta and radius as pop_loadset for 32 of 32 channels.
  • The rebase needs three precise edits (channel type, two stale doc sentences, the test import). They are listed below.
  • The pull request body shows no numbers. It should carry the comparison above so it can be accepted without opening the issue.
Details

Reviewed head: dff9be32. Tests run locally in a worktree: tests/test_stream9_readers.py 8 passed. tests/test_file_menu_audit_673.py, tests/test_stream9_gui_importers.py and tests/test_eeg_mne2eeg.py gave 118 passed and 1 failed. That failure is my local environment only (defusedxml is not installed, so MNE cannot read the MFF sample). The same test passes in CI.

What is needed before merge

  1. Rebase on develop and keep the channel type from pop_mneio: new import function through MNE, an optional install like BioSig #813. Develop now builds {"labels": name, "type": kind.upper()}. The new helper _mne_chanloc writes "type": "EEG" for every channel. If the conflict is resolved in favour of the helper, test_mneio_mixed_channels_and_nonzero_first_sample on develop fails (it expects ['EEG', 'EOG', 'STIM']). The helper is used once, so the three lines can also go inline next to kind.upper().
  2. Delete the two sentences that now contradict the fix. Git will not flag them because they are in other hunks or other files. On develop: docs/source/user_guide/mne_integration.rst lines 41 and 42 ("MNE montage coordinates are currently omitted.") and src/eegprep/resources/help/pop_mneio.md line 5 ("Electrode coordinates from an MNE montage are currently omitted."). The issue plan already says to drop the first one.
  3. Follow the test pattern now used on develop. MNE became an optional extra (eegprep[mne]). The neighbouring test on develop uses mne = pytest.importorskip('mne'). A module level import mne makes the whole file fail at collection where MNE is absent.
  4. Put the evidence in the body (summary, then numbers, then folded text). My measurement on this branch, native pop_loadset against mne.io.read_raw_eeglab followed by mne_raw_to_eeg, on sample_data/eeglab_data.set: 32 of 32 channels get a position, largest theta difference 2.8e-14 degrees, largest radius difference 5.6e-17, direction of X, Y, Z identical (angle below 1e-6 degrees). With 32 values, one mark per channel is enough for the figure.
  5. EEGLAB reference run. The issue plan states that this change needs the MATLAB reference run of AGENTS.md before merge. It is not in the body. Either add it or say why import helpers are exempt.

Comparison with EEGLAB (pop_fileio.m)

EEGLAB copies the FieldTrip positions as they are (lines 282 to 291), stores the unit in EEG.chaninfo.unit (lines 310 and 311), and for Neuromag files sets EEG.chaninfo.nosedir = '+Y' instead of rotating (lines 337 and 338). This pull request rotates to +X, as MNE's own EEGLAB export does (_get_als_coords_from_chs), which I checked line by line. Both routes give the same scalp maps. Two points for Arno, neither blocks the merge:

  • Units. X, Y and Z arrive in metres. For the sample file they are exactly 0.1 times the values pop_loadset returns (FPz X is 0.09998 instead of 0.99978). theta, radius, interpolation and ICLabel do not depend on that scale. The unit is written in the docs but not in the dataset, since EEG['chaninfo'] stays empty. One line storing unit = 'm' would match EEGLAB.
  • MEG sensors. After the rebase, pop_mneio reads FIF files. MNE keeps MEG sensor positions in the device frame, not the head frame (coord_frame 1 against 4), and this code copies them as if they were head positions. EEGLAB also imports sensor positions (lines 317 to 336), so keeping them is defensible, but the docs line should then not promise the head frame for them.

CI. One failure, on Windows: tests/test_guifunc_primitives.py::test_window_without_a_height_is_as_high_as_its_rows (57 against 56). It is the known issue #834, handled by #835, and has no link with this change. Ubuntu 3.12 and 3.13, macOS, the base install, lint and docs pass. The Pyodide job was still pending.

Other open pull requests. No conflict with #841 (same file, other lines). #857 also appends tests at the end of tests/test_stream9_readers.py, so whichever of the two merges second needs a small rebase.

Edge case checked. The real testmff.mff sample has no coordinates file, so MFF imports of that file are unchanged. A channel without a position (NaN or all zero) keeps empty coordinates, as the test asserts.

— orchestrator review

mne_raw_to_eeg now copies each channel's MNE head-frame position into
chanlocs X, Y, Z (metres, EEGLAB +X nose frame), matching MNE's EEGLAB
export, so eeg_checkchanlocs fills theta, radius and spherical fields.

Fixes #840

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@arnodelorme
arnodelorme force-pushed the agent/20261129-fix-840 branch from dff9be3 to a7c90ca Compare November 29, 2026 07:34
@arnodelorme

Copy link
Copy Markdown
Collaborator

This branch is rebased on the current develop, after the MNE reader of #813. Each channel keeps its type from that reader (EEG, EOG, STIM), the new test skips when MNE is not installed, and the two sentences that said the montage coordinates were omitted are removed (in docs/source/user_guide/mne_integration.rst and in src/eegprep/resources/help/pop_mneio.md).

Arno accepted that the positions X, Y and Z are stored in metres, as MNE gives them, with no unit recorded in the dataset. The angles (theta, radius) and the scalp maps do not depend on that scale.

I compared the result with EEGLAB in MATLAB R2025b on sample_data/eeglab_data.set: 32 of 32 channels get a position, the largest theta difference is 2.8e-14 degrees, the largest radius difference is 5.6e-17, and X, Y, Z are 0.1 times the EEGLAB values for every channel. The table is in the description above. The pull request is merged once its checks pass.

— orchestrator

@arnodelorme
arnodelorme merged commit f93177a 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.

pop_mneio: preserve electrode coordinates from the MNE montage

1 participant