Skip to content

DIPFIT simple procedural dipole parity: 8 step bottom up verification and 3 model benchmark #660

Closed

Description

@arnodelorme

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 EEG dicts and FieldTrip plain dicts. No custom classes or dataclasses.
  • Parity target: < 1.0 mm Euclidean dipole position match across all converged real ICA components (including component 3 under MNI BEM, which drifted 8.16 mm outside the brain mesh in #610), 0.0 mm coarse grid match, and < 1e-12 forward leadfield relative error.
Eight Step Bottom Up Plan
  1. Step 1 (Channel Coregistration and EEG['dipfit'] Setup): Run pop_dipfit_settings and eeglab2fieldtrip on real EEG data (eeglab_data_epochs_ica.set, 30 located channels, 32 ICA components) for standard_BESA and standard_BEM. Gate: < 1e-12 error on coord_transform, chanpos (mm), and average referenced topo.
  2. 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 12 scan grid to inside brain points (r < 71 mm for BESA, solid angle inside bnd(3) for BEM). Gate: exact same inside brain grid point count and coordinates (0 mismatches) and < 1e-12 transfer matrix error.
  3. Step 3 (Four Shell Spherical Forward Leadfield): Compute the average referenced N_chan x 3 leadfield (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-12 vs MATLAB.
  4. Step 4 (MNI Boundary Element Forward Leadfield): Compute the average referenced N_chan x 3 leadfield for standard_vol.mat (dipoli 3 shell MNI BEM) across all inside brain grid points. Gate: relative max error < 1e-12 vs MATLAB.
  5. 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_dipolefitting with gridsearch='yes', nonlinear='no'). Gate: 0.0 mm position difference and < 1e-12 momxyz and rv difference on EEG['dipfit']['model'].
  6. Step 6 (Single Dipole Nonlinear Fit dipfit_nonlinear): Refine all 32 real ICA components from Step 5 grid positions (ft_dipolefitting with gridsearch='no', nonlinear='yes'), matching MATLAB's exact optimizer scaling and inside brain behavior. Gate: < 1.0 mm position difference across all converged components (resolving the 8.16 mm component 3 drift in #610) and matching momxyz, rv, sourcepot, datapot, and diffmap.
  7. Step 7 (Symmetric Dual Dipole Fit): Run dipfit_nonlinear with bilateral symmetry (symmetry='y' for BESA, symmetry='x' for MNI BEM, starting at [-40, +40] mm) on real ICA components. Gate: < 1.0 mm position difference on both mirrored dipoles and matching moments/RV.
  8. Step 8 (End to End pop_multifit on Real Datasets): Run full pop_multifit (coarse scan, fine fit, dipfit_reject threshold, spherical rmout, and EEG['dipfit']['model'] struct fields) on eeglab_data_epochs_ica.set and a second real ICA dataset (eeglab_data_picard_matlab.set). Gate: < 1.0 mm parity and identical retained/rejected component sets.

Activity

  1. claude commented on Nov 19, 2026

    @claude
    Contributor

    🤖 Plan:

    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 when headmodel is None. The BEM branch runs unconstrained scipy BFGS, so the fit can leave the brain mesh. FieldTrip's dipole_fit uses fminunc and rejects positions outside the head model, which is why EEGLAB stays inside.

    Relevant code

    Proposed approach (one PR per group, each with a MATLAB reference run)

    1. Steps 1 and 2, coregistration and grid mask. Add MATLAB fixtures for 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 uses 0.98·head_radius, but the issue's gate is r < 71 mm.
    2. Steps 3 to 5, leadfields and grid search. Extend the existing leadfield tests to all inside-grid points, and add a gate on grid-search posxyz, momxyz, and rv for all 32 components.
    3. Steps 6 and 7, nonlinear and symmetric fits. This is where Fit DIPFIT dipoles with the packaged MNI boundary-element model #610's drift is fixed. Port FieldTrip's dipole_fit inside-head constraint and fminunc scaling 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.
    4. Step 8, end-to-end pop_multifit. Compare retained and rejected component sets and the model fields.

    Open questions

    • eeglab_data_picard_matlab.set is not in sample_data/. Should it be generated by MATLAB, then committed or fetched?
    • The "3 model benchmark" (Fable 2, Gemini 4 Carbon, Codex GPT 6.1 Sol) is an evaluation process, not a repo change. Who runs it, and where should results live? It does not belong in code or tests.
    • The "simplification" goal and the "no FieldTrip subfunction sprawl" rule may conflict with the existing fieldtrip/ subpackage. Is consolidating that subpackage in scope?
  2. arnodelorme commented on Nov 19, 2026

    @arnodelorme
    CollaboratorAuthor

    Step 0: MATLAB R2025b Reference Ground Truth Generated

    • MATLAB version: 25.2.0.3055257 (R2025b) Update 2 (ft_hastoolbox('optim') = 1)
    • Primary real EEG dataset: sample_data/eeglab_data_epochs_ica.set (30 located channels, 32 ICA components)
    • Secondary real EEG dataset: sample_data/eeglab_data_picard_matlab.set (10 ICA components)
    • 24 x 24 x 12 scan grid inside brain points: 1912 (BESA 4 shell) and 2900 (MNI dipoli BEM)
    • MNI BEM Component 3 MATLAB reference: posxyz = [6.5582, 66.1335, -22.0314] mm, rv = 0.009426, inside_brain = 1
    • Retained components (RV <= 40%): 27/32 (BESA with rmout='on') and 32/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.

  3. arnodelorme commented on Nov 19, 2026

    @arnodelorme
    CollaboratorAuthor

    Eight Step Bottom Up Parity Results and Three Model Comparison

    Winning procedural implementation (OpenAI Codex GPT 6.1 Sol, zero classes, 43/43 unit tests, 8/8 real EEG parity gates) pushed to branch agent/issue-660 at commit 6935317d.

    1. Step by Step MATLAB R2025b Parity (Winning Branch agent/issue-660)

    Step Layer Tested on Real EEG Data (eeglab_data_epochs_ica.set) Gate Measured Error Status
    1 pop_dipfit_settings & eeglab2fieldtrip (chanpos & topo) < 1e-9 2.84e-14 mm / 2.03e-15 rel PASS
    2 prepare_headmodel_sens & inside_brain_grid (1,912 BESA, 2,900 BEM pts) 0 mask diff 0 mismatches, 1.42e-14 mm proj PASS
    3 Four shell BESA forward leadfield (compute_dipole_leadfield) < 1e-12 rel 5.77e-15 rel PASS
    4 MNI dipoli BEM forward leadfield (compute_dipole_leadfield) < 1e-12 rel 2.64e-15 rel PASS
    5 Coarse grid scan across all 32 ICA components (pop_dipfit_gridsearch) 0.0 mm 0.0 mm (RV 4.83e-15) PASS
    6 Single dipole nonlinear fit across all 32 ICA components (dipfit_nonlinear) < 1.0 mm BESA max 0.000080 mm, BEM max 0.00333 mm (Comp 3: 0.000017 mm inside brain) PASS
    7 Symmetric two dipole fit (pop_multifit, dipoles=2, comps 1 to 10) < 1.0 mm BESA max 0.00642 mm, BEM max 0.00752 mm PASS
    8 End to end pop_multifit (threshold=40, rmout='on') + Dataset 2 (picard) < 1.0 mm & exact mask 27/32 BESA & 32/32 BEM exact, Dataset 2 max 0.000128 mm PASS

    2. Three Model Side by Side Summary

    Model Real EEG Gates Repo Pytest Core Dipole SLOC Files Core Classes Step 6 BEM Max (mm) Comp 3 BEM (mm) Runtime
    origin/develop baseline 7/8 (Comp 3 failed) 43/43 3,276 36 6 8.16 mm (outside brain) 8.16 mm n/a
    OpenAI Codex GPT 6.1 Sol 8/8 43/43 (rc=0) 2,134 25 0 0.00333 mm 0.000017 mm 34.8 min
    Google Gemini 4 Carbon High 8/8 39/40 (1 key bug) 1,636 17 0 0.00333 mm 0.0000053 mm 45.0 min (cap)
    Anthropic Fable 2 7/8 (Comp 21: 10.65 mm) 29/29 (14 removed) 1,171 18 0 10.65 mm (31/32 < 0.07 mm) 0.000018 mm 31.1 min
  4. arnodelorme commented on Nov 19, 2026

    @arnodelorme
    CollaboratorAuthor

    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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions