Skip to content

[CI] Fix five EEGLAB reference failures on develop - #691

Merged
arnodelorme merged 2 commits into
sccn:developfrom
ChenfeiJiangSICAU:agent/20261125-fix-685
Nov 29, 2026
Merged

arnodelorme merged 2 commits into
sccn:developfrom
ChenfeiJiangSICAU:agent/20261125-fix-685

Conversation

@ChenfeiJiangSICAU

@ChenfeiJiangSICAU ChenfeiJiangSICAU commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Install Optimization_Toolbox in eeglab-reference.yml and require fminunc through the shared eegprep_require_fminunc helper across the coregister and DIPFIT parity scripts, leaving DIPFIT grid counting unguarded. Remove the duplicate +1 offset in the MATLAB kmeans clustering test and restore strict empty-struct parity in assert_matlab_struct_near while adapting pop_copyset empty dicts in the Python reference transport. Verified locally on MATLAB R2024a against EEGLAB d0963be9 and eeglab_tests fec88f9f (48 passed, 0 failed, 0 skipped in 31.08s).

Fixes #685

Install Optimization_Toolbox in eeglab-reference.yml and require fminunc explicitly in the coregister and DIPFIT parity helpers so MATLAB no longer falls back to fminsearch. Remove the duplicate +1 offset in the MATLAB kmeans clustering test and restore strict empty-struct parity in assert_matlab_struct_near while adapting pop_copyset empty dicts in the Python reference transport.

Fixes sccn#685

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Claude Code Review

This pull request is from a fork — automated review is disabled. A repository maintainer can comment @claude review to run a one-time review.

@arnodelorme arnodelorme left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This review was written by an AI assistant during a nightly check, not by Arno.

Code review

  • Overall assessment: needs more context. The five diagnoses hold up against the code and against the MATLAB checks listed at the bottom. The fix for three of the five sits in a workflow change that has not run yet.
  • Highest-risk area: .github/workflows/eeglab-reference.yml together with the new fminunc guard in the four MATLAB helpers. Only a real run of that workflow exercises them.
  • Merge recommendation: needs more context (one eeglab-reference.yml run on this branch, linked in the description).

Blocking

None.

Important

1. No reference run on this branch yet (severity: important, .github/workflows/eeglab-reference.yml and the pull request description)

Issue #685 asks, as its definition of done, for a linked eeglab-reference.yml run in which each of the 5 tests passes. GitHub reports no checks on this branch. The workflow is workflow_dispatch only and reads secrets.MATLAB_BATCH_TOKEN, which a fork does not have, so the Optimization_Toolbox line and the guard have never run on the R2024b runner. The description gives "MATLAB R2024a (27 passed, 0 failed)" without the command, the EEGLAB and eeglab_tests commits, or the list of tests.

Two things only that run can show. First, that setup-matlab installs Optimization_Toolbox and the batch token licenses it. Second, that license('test', 'optimization_toolbox') returns true under that token. If it returns false, all four helpers now raise and the core shard gets worse, not better.

Suggested fix: a maintainer dispatches eeglab-reference.yml on a copy of this branch in sccn/eegprep, then the description gets the run link, the two pinned commits, the pass, fail and skip counts and the runtime, as the Testing section of AGENTS.md asks.

Nits

1. The guard also blocks the grid case, which needs no optimizer (tests/matlab/eegprep_dipfit_component_parity.m)

if exist('fminunc', 'file') ~= 2 || ~license('test', 'optimization_toolbox')
error('eegprep:missingOptimizationToolbox', ...
'Optimization Toolbox (fminunc) is required for DIPFIT component parity.');
end

The guard sits above the kind dispatch. For kind = 'grid' the helper only calls ft_inside_headmodel and returns (lines 24 to 30). test_inside_brain_grid_counts_match_eeglab passes today on a MATLAB without the Optimization Toolbox and would raise after this change. Suggested fix: move the guard to just before the pop_multifit call.

2. The guard does not ask the question FieldTrip asks (the four helpers in tests/matlab/)

The guard uses exist('fminunc', 'file') and license('test', ...). FieldTrip picks its optimizer with ft_hastoolbox('optim'), which does license('checkout', 'optimization_toolbox') and checks that fminunc and optimset are on the path (utilities/ft_hastoolbox.m lines 295 to 296 and 736 in FieldTrip-lite 20240111, the copy I had locally. CI uses 20250523). license('test') only says a license exists. If the checkout fails, the guard passes and FieldTrip still takes fminsearch silently, which is the case the guard is there to stop. Suggested fix: call ft_hastoolbox('optim') once FieldTrip is on the path, from one small shared helper in tests/matlab/ in place of the four copies.

Test gaps

None. test_struct_near_assertion_matches_source already pins assert_matlab_struct_near against near.m, and test_matlab_kmeans_numbering_matches_fixed_initial_centroids pins the label numbering.

EEGLAB parity notes

What I ran, at commit 2680844.

MATLAB R2025b with the Optimization Toolbox, through matlab -batch, on a local eeglab_tests checkout that is not the pinned commit:

  • near.m on the 14 cases of test_struct_near_assertion_matches_source gives [1 1 0 1 0 0 0 0 1 0 0 1 1 0]. assert_matlab_struct_near at this head gives the same 14 answers. near(struct(), struct()) is 0, so removing the early return is right.
  • eeg_checkset.m sets EEG.etc = [] and EEG.roi = [] (lines 1266 and 1269). Python pop_loadset returns {} for both, and these are the only empty dicts in the pop_copyset output, so mapping {} to [] in that presentation matches EEGLAB.
  • kmeanscluster.m on the test data returns [1 2 1 1 2 2]. Python kmeanscluster returns the same since #657 moved the + 1 into the function. Dropping + 1 in the test is right.
  • Synthetic traditional warp of eegprep_warp_optim_parity.m: with fminunc, ft_warp_optim recovers the true parameters (z rotation 1.718876). The fminsearch branch of ft_warp_optim gives [4.992872 -3.000655 1.997129 2.911925 -2.291934 0.3347925 2.00154 1.504206 1.1929], which are the MATLAB values of failure 2 in #685. This confirms that the runner fell back to fminsearch.

Python backend, pinned eeglab_tests fec88f9f and EEGLAB d0963be9, on the six test files that call assert_matlab_struct_near (test_phase1b_file_edit_pop_functions.py, test_eegh.py, test_gethelpvar.py, test_phase2_file_channel_location.py, test_sigprocfunc_plotting_eeglab_tests.py, test_erpimage.py) with --eeglab-backend=python: 238 passed, 1 skipped, 1 failed, 1 deselected (test_struct_near_assertion_matches_source, which needs MATLAB). Both pop_copyset tests pass. With the assertions.py change alone and the old transport, test_reference_copyset_original_set_out fails, so the transport change is needed. The one failure is test_reference_plotcurve_original_eight_cases (plotcurve maskarray shape does not match data), in code this pull request does not touch. I did not check it on develop.

I did not run the MATLAB reference suite itself, so the coregister, warp and BEM tests are not confirmed end to end here.

test.yml and fuzz-nightly.yml also install MATLAB without the Optimization Toolbox. They are not affected: the four helpers are only called from reference tests, which stay deselected without --eeglab-backend.

Share FieldTrip's ft_hastoolbox('optim') check in tests/matlab/eegprep_require_fminunc.m across the four parity helpers and move the check in eegprep_dipfit_component_parity.m after the kind='grid' early return.
@ChenfeiJiangSICAU

ChenfeiJiangSICAU commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks for the review. Addressed the feedback in commit c7cff119a8ac7f46a86d5e8e22bda1dd9d5dfed9:

  1. Shared FieldTrip ft_hastoolbox('optim') guard: Added tests/matlab/eegprep_require_fminunc.m, which calls FieldTrip's exact ft_hastoolbox('optim') check when ft_hastoolbox is on the path (and falls back to checking fminunc, optimset, and license('checkout', 'optimization_toolbox') == 1 when ft_hastoolbox is absent), raising eegprep:missingOptimizationToolbox if unavailable. Replaced the four duplicated guards in eegprep_coregister_parity.m, eegprep_warp_optim_parity.m, eegprep_dipfit_bem_reference.m, and eegprep_dipfit_component_parity.m after EEGLAB/FieldTrip path checks.
  2. Keep DIPFIT grid unguarded: Moved eegprep_require_fminunc in tests/matlab/eegprep_dipfit_component_parity.m to right before pop_multifit, after the kind == 'grid' early return and kind validation.

Local MATLAB R2024a Verification (c7cff119)

Environment: MATLAB R2024a (24.1.0.2537033) with Optimization Toolbox and FieldTrip-lite 20250523, pinned EEGLAB d0963be9d81acc5b66e84f8d3d7c89e3ce127cc4, pinned eeglab_tests fec88f9ffe7c225eb1de8924db8b6a3e0d53a4d9.

Positive suite (all five #685 tests + pop_copyset + assert_matlab_struct_near + DIPFIT grid):

pytest -vv --eeglab-backend=matlab --run-heavy-reference \
  --eeglab-root=src/eegprep/eeglab \
  --eeglab-suite-root=eeglab_tests \
  --durations=0 \
  tests/test_coregister_parity.py \
  tests/test_dipfit_bem.py \
  tests/test_study_clustering.py \
  tests/test_phase2_file_channel_location.py::test_struct_near_assertion_matches_source \
  tests/test_phase1b_file_edit_pop_functions.py::test_reference_copyset_recorded_dataset_calls \
  tests/test_phase1b_file_edit_pop_functions.py::test_reference_copyset_original_set_out \
  tests/test_dipfit_component_parity.py::test_inside_brain_grid_counts_match_eeglab

Result: 48 passed, 0 failed, 0 skipped in 31.08s (including test_coregister_traditional_warp_matches_eeglab, test_synthetic_traditional_warp_matches_fieldtrip, test_bem_leadfield_and_multifit_match_fieldtrip, test_matlab_kmeans_numbering_matches_fixed_initial_centroids, test_reference_copyset_original_set_out, test_reference_copyset_recorded_dataset_calls, test_struct_near_assertion_matches_source, and test_inside_brain_grid_counts_match_eeglab).

Negative check (without Optimization Toolbox):
In a fresh MATLAB R2024a matlab -batch session against pinned EEGLAB d0963be9 with the 7 path entries under toolbox\optim, toolbox\shared\optimlib, and toolbox\globaloptim removed (which('fminunc') == '', ft_hastoolbox('optim') == 0, 19.49s):

  • eegprep_coregister_parity, eegprep_warp_optim_parity, eegprep_dipfit_bem_reference, and eegprep_dipfit_component_parity ('single' and 'symmetric') all raised eegprep:missingOptimizationToolbox (and eegprep_coregister_parity also raised eegprep:missingOptimizationToolbox with FieldTrip removed to verify the fallback path).
  • eegprep_dipfit_component_parity with kind = 'grid' succeeded without Optimization Toolbox on both standardBEM (count = 2900) and standardBESA (count = 1912).

As this is a fork PR without access to secrets.MATLAB_BATCH_TOKEN, eeglab-reference.yml is ready for a maintainer to dispatch on a branch copy in sccn/eegprep.

@arnodelorme

Copy link
Copy Markdown
Collaborator

This second review was written by an AI assistant at Arno's request, not by Arno.

Second review at c7cff11: the reference tests pass

Verdict: ready to merge. The tests I ran pass on this branch merged into today's develop. Develop alone fails the two tests this pull request fixes in the test harness. The earlier review asked for one reference run on this branch. A fork cannot run eeglab-reference.yml, and this pull request has no checks to re-run, so I ran the same tests on our Linux machine, which has MATLAB and the Optimization Toolbox.

Code tested Tests Result
develop 2fe5606 (with #776) merged with this branch at c7cff11 (no conflicts) 8 test files, 221 tests 221 passed, 0 failed, 0 skipped in 271 s
develop 2fe5606 alone the tests of #685 plus DIPFIT component and pop_copyset, 30 tests 28 passed, 2 failed: kmeans numbering and struct near

All five tests of #685 pass on the merge. So do the three DIPFIT component tests (grid, single, symmetric), which the GitHub runner skips.

The baseline tells us something useful. When fminunc is installed, the two coregister warp tests and the BEM multifit test already pass on develop. On the runner they fail only because MATLAB there has no Optimization Toolbox and FieldTrip quietly falls back to fminsearch. That is the diagnosis of this pull request, and the one-line Optimization_Toolbox addition to the workflow is the right fix for those three tests. The other two failures (kmeans + 1 and the empty-struct early return in assert_matlab_struct_near) are fixed in the test code, and the baseline shows that they still fail without this change.

Both points from the first review are addressed. The check now calls FieldTrip's own ft_hastoolbox('optim') from one shared helper, and the grid case runs before that check, so it works without the toolbox.

One thing this run cannot show: whether the MATLAB batch token on the GitHub runner licenses the Optimization Toolbox under R2024b. If it does not, the same three tests still fail, now with the clear error eegprep:missingOptimizationToolbox in place of a numeric mismatch. Nothing else gets worse. Running eeglab-reference.yml on develop once this is merged answers that question, and #685 asks for that run in its definition of done.

How the run was done
  • Machine: jamming.ucsd.edu, Ubuntu, MATLAB R2025b Update 7 with Signal Processing, Statistics and Optimization toolboxes (which fminunc resolves). The workflow uses R2024b, so the MATLAB release differs.
  • Pins as in the workflow: eeglab_tests fec88f9f, EEGLAB d0963be9, FieldTrip-lite 20250523, Fileio 260210, Biosig 3.8.5, plus ft_deleteopt.m. The Git LFS files of eeglab_tests were checked out. Without them, the five floatread tests and test_reference_copyset_original_set_out fail because they read pointer files.
  • MATLAB was served the same way as in CI, eegprep_batch_engine_serve.m with EEGPREP_MATLAB_BATCH_DIR.
  • Command (on the merge):
pytest -vv --eeglab-backend=matlab --run-heavy-reference \
  --eeglab-root=src/eegprep/eeglab --eeglab-suite-root=eeglab_tests \
  --timeout=1200 --timeout-method=signal \
  tests/test_coregister_parity.py tests/test_dipfit_bem.py tests/test_dipfit_component_parity.py \
  tests/test_study_clustering.py tests/test_phase2_file_channel_location.py \
  tests/test_phase1b_file_edit_pop_functions.py tests/test_eegh.py tests/test_gethelpvar.py

— orchestrator

@arnodelorme

Copy link
Copy Markdown
Collaborator

Review (Claude Fable): ready to merge

Details: what I ran, what I found, two small remarks

What each fix does, and my check

Failure in #685 Cause Fix here My check
coregister warp (2 tests), BEM multifit The runner has no Optimization Toolbox, so FieldTrip quietly uses fminsearch in place of fminunc Optimization_Toolbox added to the workflow, plus one shared guard eegprep_require_fminunc.m Reproduced, see below
kmeans numbering The test added + 1 to labels that are already 1-based + 1 removed in the test Python and MATLAB both give [1 2 1 1 2 2]
struct near assert_matlab_struct_near accepted two empty structs, near.m does not Early return removed, and the pop_copyset output shows {} as [] as EEGLAB does near(struct(), struct()) is 0

MATLAB R2025b Update 2 (Mac, matlab -batch, FieldTrip-lite from a local EEGLAB checkout, near.m taken from eeglab_tests fec88f9f)

  • near(struct(), struct()) returns 0. The strict assertion now agrees with near.m.
  • kmeanscluster and the Statistics Toolbox kmeans both return [1 2 1 1 2 2] on the test data. Python kmeanscluster returns [1, 2, 1, 1, 2, 2]. The + 1 was the bug.
  • Synthetic warp of eegprep_warp_optim_parity.m. With fminunc: [5 -3 2 2.86479 -2.291833 1.718876 2 1.5 1.2], the true parameters. With the Optimization Toolbox removed from the path: [4.992872 -3.000655 1.997129 2.911925 -2.291934 0.3347925 2.00154 1.504206 1.1929]. These are, digit for digit, the MATLAB values of failure 2 in EEGLAB reference: 5 tests fail on develop (coregister warp x2, BEM multifit, kmeans numbering, struct near) #685. So the runner did fall back to fminsearch.
  • The guard passes when the toolbox is there, in both of its branches (with and without FieldTrip on the path). Without the toolbox it raises eegprep:missingOptimizationToolbox in both branches.
  • FieldTrip picks its optimizer with ft_hastoolbox('optim') (utilities/ft_warp_optim.m line 81, inverse/ft_inverse_dipolefit.m line 126). The guard asks the same question, so it cannot pass while FieldTrip still falls back.

Python

  • Touched test files, no backend: pytest tests/test_study_clustering.py tests/test_phase2_file_channel_location.py tests/test_phase1b_file_edit_pop_functions.py tests/test_coregister_parity.py tests/test_dipfit_bem.py tests/test_dipfit_component_parity.py -q gives 143 passed, 1 skipped, 47 deselected.
  • Python-backend lane, same command as test.yml, pins eeglab_tests fec88f9f and EEGLAB d0963be9 with the LFS files: pytest tests --eeglab-backend python --eeglab-suite-root <eeglab_tests> -m "parity and not slow and not gui" gives 578 passed, 62 skipped, 0 failed in 227 s. Two files were not collected on my machine (test_erpimage_engine.py, test_pop_runica_pamica.py) because hypothesis and pamica are missing there. They do not use the changed code. I ran this lane because a fork pull request gets no CI, and this is the lane that goes red on develop if the transport change breaks something.
  • Strict assertion with the old pop_copyset adapter: test_reference_copyset_original_set_out fails. With the new adapter it passes. The transport change is needed, it is not decoration.
  • ruff check and ruff format --check pass on the three Python files.

Not run by me

The MATLAB reference suite itself. The second review ran it on jamming (221 passed, 0 failed). gh pr checks 691 reports no checks, as expected for a fork.

Two small remarks, neither blocks the merge

  1. Two changed lines are not used by any test. _present_datasets(..., scalars=scalars) and its call in _present_eeg_value (tests/eeglab_tests/matlab_output_transport.py lines 446 to 455). I traced the 640 tests of the lane: this path is never taken with scalars=False. The change also alters, without a test, what six other adapters (eeg_interp, pop_adjustevents, pop_editeventfield, pop_loadset, pop_select, readegilocs) would return for a list of datasets. Putting these two lines back as they were makes the diff smaller and changes no test result.
  2. The description is one dense paragraph. A three-row table (failure, cause, fix) like the one above would let Arno accept it at a glance. No capture and no histogram are needed here: no window changes and no EEGPrep output changes.

Other open pull requests

Only #857 touches a file of this pull request (.github/workflows/eeglab-reference.yml, lines 48 to 55, the apt packages). This one edits lines 178 to 188. git merge-tree of the two heads is clean.

Good to know

After this, the MATLAB reference suite needs the Optimization Toolbox. Without it the four helpers fail on purpose, they do not skip. This follows #685 ("no new skips") and the choice made in #666 to match fminunc. The grid case of DIPFIT stays outside the guard and still runs without the toolbox.

— orchestrator review

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.

EEGLAB reference: 5 tests fail on develop (coregister warp x2, BEM multifit, kmeans numbering, struct near)

2 participants