Repository navigation
Closed
Description
Step by step bottom up verification and simplification of DIPFIT dipole source localization in eegprep against MATLAB EEGLAB (dipfit + Fieldtrip-lite) on real EEG data, comparing Anthropic Fable 2, Google Gemini 4 Carbon, and OpenAI Codex GPT 6.1 Sol side by side.
- Scope: Only dipole source localization functions directly called from EEGLAB DIPFIT (
pop_dipfit_settings,eeglab2fieldtrip,ft_dipolefitting,dipfit_gridsearch,pop_dipfit_gridsearch,dipfit_nonlinear,pop_dipfit_nonlinear,dipfit_reject,pop_multifit). No eLORETA, no MRI segmentation, no FieldTrip subfunction sprawl. - Design rule: Simple procedural functions operating directly on EEGLAB
EEGdicts and FieldTrip plain dicts. No custom classes or dataclasses. - Parity target:
< 1.0 mmEuclidean dipole position match across all converged real ICA components (including component 3 under MNI BEM, which drifted8.16 mmoutside the brain mesh in#610),0.0 mmcoarse grid match, and< 1e-12forward leadfield relative error.
Eight Step Bottom Up Plan
- Step 1 (Channel Coregistration and
EEG['dipfit']Setup): Runpop_dipfit_settingsandeeglab2fieldtripon real EEG data (eeglab_data_epochs_ica.set, 30 located channels, 32 ICA components) forstandard_BESAandstandard_BEM. Gate:< 1e-12error oncoord_transform,chanpos(mm), and average referencedtopo. - Step 2 (Sensor Projection and Inside Brain Grid Mask): Project electrodes onto the outer sphere (BESA) or scalp mesh with triangle transfer weights (MNI BEM) and mask the
24 x 24 x 12scan grid to inside brain points (r < 71 mmfor BESA, solid angle insidebnd(3)for BEM). Gate: exact same inside brain grid point count and coordinates (0mismatches) and< 1e-12transfer matrix error. - Step 3 (Four Shell Spherical Forward Leadfield): Compute the average referenced
N_chan x 3leadfield (r = [71, 72, 79, 85]mm,c = [0.33, 1.0, 0.0042, 0.33]) across all inside brain grid points. Gate: relative max error< 1e-12vs MATLAB. - Step 4 (MNI Boundary Element Forward Leadfield): Compute the average referenced
N_chan x 3leadfield forstandard_vol.mat(dipoli3 shell MNI BEM) across all inside brain grid points. Gate: relative max error< 1e-12vs MATLAB. - Step 5 (Coarse Grid Search
dipfit_gridsearch/pop_dipfit_gridsearch): Scan all inside brain grid points via linear least squares moment (pinv(lf) @ Vdata) for all 32 real ICA components under BESA and BEM (ft_dipolefittingwithgridsearch='yes',nonlinear='no'). Gate:0.0 mmposition difference and< 1e-12momxyzandrvdifference onEEG['dipfit']['model']. - Step 6 (Single Dipole Nonlinear Fit
dipfit_nonlinear): Refine all 32 real ICA components from Step 5 grid positions (ft_dipolefittingwithgridsearch='no',nonlinear='yes'), matching MATLAB's exact optimizer scaling and inside brain behavior. Gate:< 1.0 mmposition difference across all converged components (resolving the8.16 mmcomponent 3 drift in#610) and matchingmomxyz,rv,sourcepot,datapot, anddiffmap. - Step 7 (Symmetric Dual Dipole Fit): Run
dipfit_nonlinearwith bilateral symmetry (symmetry='y'for BESA,symmetry='x'for MNI BEM, starting at[-40, +40]mm) on real ICA components. Gate:< 1.0 mmposition difference on both mirrored dipoles and matching moments/RV. - Step 8 (End to End
pop_multifiton Real Datasets): Run fullpop_multifit(coarse scan, fine fit,dipfit_rejectthreshold, sphericalrmout, andEEG['dipfit']['model']struct fields) oneeglab_data_epochs_ica.setand a second real ICA dataset (eeglab_data_picard_matlab.set). Gate:< 1.0 mmparity and identical retained/rejected component sets.
Metadata
Metadata
Assignees
Labels
No labels
Activity
claude commentedon Nov 19, 2026
🤖 Plan:
src/eegprep/plugins/dipfit/plus new MATLAB fixtures. Too large for one agent PR, so analysis only, no PR.Research
Most of the pipeline already exists and has MATLAB parity tests (
tests/test_dipfit_bem.py,tests/test_dipfit_four_shell.py,tests/test_dipfit_workflows.py). #610 reports a leadfield relative error of 3.8e-15 and components 1 and 2 within 0.0007 mm, so steps 3 and 4 look done at the leadfield level. The 8.16 mm drift on component 3 has a clear cause. In_optimize_positions, the only out-of-head penalty is the spherical one, applied whenheadmodel is None. The BEM branch runs unconstrainedscipyBFGS, so the fit can leave the brain mesh. FieldTrip'sdipole_fitusesfminuncand rejects positions outside the head model, which is why EEGLAB stays inside.Relevant code
_fitting.py#L466-L526: optimizer; BEM branch has no inside check (L493)_fitting.py#L529-L542: grid mask (sphere 0.98·r orft_inside_headmodel)_fitting.py#L423-L463: moment and RV; BEM and sphere use differentdiffmapsign conventionsfieldtrip/bem.py#L94:ft_prepare_vol_sens, the BEM electrode transferforward/standard_besa.py#L18: four-shell head modelProposed approach (one PR per group, each with a MATLAB reference run)
coord_transform,chanpos,topo, the inside-grid coordinates, and the BEM transfer matrix. Assert an exact grid-point match, then fix any mismatches. One issue to settle: the sphere mask uses0.98·head_radius, but the issue's gate isr < 71 mm.posxyz,momxyz, andrvfor all 32 components.dipole_fitinside-head constraint andfminuncscaling into the BEM branch of_optimize_positions. Then switch the component 3 test from the residual-variance check back to the 1 mm position gate, and add the symmetric-fit cases.pop_multifit. Compare retained and rejected component sets and the model fields.Open questions
eeglab_data_picard_matlab.setis not insample_data/. Should it be generated by MATLAB, then committed or fetched?fieldtrip/subpackage. Is consolidating that subpackage in scope?arnodelorme commentedon Nov 19, 2026
Step 0: MATLAB R2025b Reference Ground Truth Generated
25.2.0.3055257 (R2025b) Update 2(ft_hastoolbox('optim') = 1)sample_data/eeglab_data_epochs_ica.set(30located channels,32ICA components)sample_data/eeglab_data_picard_matlab.set(10ICA components)1912(BESA 4 shell) and2900(MNIdipoliBEM)posxyz = [6.5582, 66.1335, -22.0314]mm,rv = 0.009426,inside_brain = 1RV <= 40%):27/32(BESA withrmout='on') and32/32(MNI BEM)Launching parallel 3 model benchmark (Anthropic Fable 2, Google Gemini 4 Carbon High, OpenAI Codex GPT 6.1 Sol) on isolated worktrees of
origin/develop.arnodelorme commentedon Nov 19, 2026
Eight Step Bottom Up Parity Results and Three Model Comparison
Winning procedural implementation (OpenAI Codex GPT 6.1 Sol, zero classes,
43/43unit tests,8/8real EEG parity gates) pushed to branchagent/issue-660at commit6935317d.1. Step by Step MATLAB R2025b Parity (Winning Branch
agent/issue-660)eeglab_data_epochs_ica.set)pop_dipfit_settings&eeglab2fieldtrip(chanpos&topo)< 1e-92.84e-14 mm/2.03e-15relprepare_headmodel_sens&inside_brain_grid(1,912BESA,2,900BEM pts)0mask diff0mismatches,1.42e-14 mmprojcompute_dipole_leadfield)< 1e-12rel5.77e-15reldipoliBEM forward leadfield (compute_dipole_leadfield)< 1e-12rel2.64e-15relpop_dipfit_gridsearch)0.0 mm0.0 mm(RV4.83e-15)dipfit_nonlinear)< 1.0 mm0.000080 mm, BEM max0.00333 mm(Comp 3:0.000017 mminside brain)pop_multifit,dipoles=2, comps 1 to 10)< 1.0 mm0.00642 mm, BEM max0.00752 mmpop_multifit(threshold=40,rmout='on') + Dataset 2 (picard)< 1.0 mm& exact mask27/32BESA &32/32BEM exact, Dataset 2 max0.000128 mm2. Three Model Side by Side Summary
origin/developbaseline7/8(Comp 3 failed)43/433,2763668.16 mm(outside brain)8.16 mm8/843/43(rc=0)2,1342500.00333 mm0.000017 mm34.8 min8/839/40(1key bug)1,6361700.00333 mm0.0000053 mm45.0 min(cap)7/8(Comp 21:10.65 mm)29/29(14removed)1,17118010.65 mm(31/32 < 0.07 mm)0.000018 mm31.1 minrefactor(dipfit): simplify dipole fitting into procedural ft_dipolefi…
arnodelorme commentedon Nov 19, 2026
Done: Verified procedural zero class DIPFIT dipole parity across 8 bottom up steps against MATLAB R2025b (max error 0.000080 mm BESA, 0.00333 mm MNI BEM, 43/43 pytest), pushed winning OpenAI Codex GPT 6.1 Sol implementation to https://github.com/sccn/eegprep/tree/agent/issue-660 (6935317), and emailed both two page PDF reports.
— website, reported with orch done
[DIPFIT] Match FieldTrip fminunc dipole fits on BESA and MNI (#666)