Skip to content

Add CORTEG, an ST-EEGFormer adapted to intracranial ECoG - #1551

Merged
bruAristimunha merged 8 commits into
masterfrom
feat/corteg
Nov 9, 2026
Merged

bruAristimunha merged 8 commits into
masterfrom
feat/corteg

Conversation

@julien-gadonneix

Copy link
Copy Markdown
Collaborator

Adds CORTEG (Yang et al., NeurIPS 2026, arXiv:2605.10337, code, MIT): ST-EEGFormer's encoder read on ECoG.

  • A KNNSoftFourier spatial adapter replaces the channel embedding: each electrode's 3D position (pos, or chs_info) attends over the frozen pretrained codebook, plus a Fourier residual.
  • A high-gamma stream x_hi joins the low-frequency x, by mean before the last n_merge_blocks blocks (merge_strategy="average") or by a per-block gate ("layerwise_gate"); without x_hi the model reads x alone.
  • Same module names as STEEGFormer: CORTEG.from_pretrained("braindecode/STEEGFormer-small", ...) loads the backbone and warm-starts the high-gamma embedding from it.
  • ch_mask for padded batches (not in the reference, which batches one patient at a time).

Testing

  • Matches the reference code to ~1e-7 with shared random weights, both merge strategies, with and without x_hi; the authors' released Stanford adapter on STEEGFormer-small gives their outputs (network test -k corteg_released --run-network)
  • _CASES / _CH_MASK_EXTRAS / compat / return-features entries; pytest test/unit_tests -k "CORTEG or corteg" and test_ch_mask_training.py -k CORTEG: passed
  • pre-commit run --files <changed files>: passed

Port of Yang et al. (NeurIPS 2026): ST-EEGFormer's encoder with a KNNSoftFourier spatial adapter placing each electrode by position, a high-gamma token stream (average or layer-wise gated fusion), and ch_mask for padded batches. Loads braindecode/STEEGFormer-small, warm-starting the high-gamma embedding; matches the reference implementation, including its released Stanford adapter.
Copilot AI balanced review requested due to automatic review settings November 6, 2026 21:49

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

Copilot AI balanced review requested due to automatic review settings November 6, 2026 21:49

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

No private copy of the block: STEEGFormer's _TransformerEncoderBlock forwards an optional attn_mask to its attention; without one it computes as before.
Copilot AI balanced review requested due to automatic review settings November 6, 2026 22:59

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@codecov

codecov Bot commented Nov 6, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 95.72193% with 8 lines in your changes missing coverage. Please review.
✅ Project coverage is 91.53%. Comparing base (29c230e) to head (eec84a4).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1551      +/-   ##
==========================================
+ Coverage   91.50%   91.53%   +0.03%     
==========================================
  Files         170      171       +1     
  Lines       22394    22580     +186     
==========================================
+ Hits        20491    20669     +178     
- Misses       1903     1911       +8     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@julien-gadonneix

Copy link
Copy Markdown
Collaborator Author

Reproduction runs of the paper's downstream results with this port (Stanford finger flexion and Brain Treebank tasks A/B, through neuralset/NeuralBench tasks) are on their way; I'll post the numbers here.

@julien-gadonneix

Copy link
Copy Markdown
Collaborator Author

Reproduction of the paper's downstream results with this port (589699cb): all 8 cells land within 5 % of the published values.

The data come through neuralset studies and neuralbench-style task configs. The windows match the authors' window code to float32 precision. The Brain Treebank events and causal folds match the paper's paper_cells splits exactly. Training follows the authors' recipe (frozen ST-EEGFormer-small, LoRA r=4 on blocks 4–7, the same 297,236 trainable parameters as the released adapter on Stanford).

cell paper port
Stanford finger, pooled (r, seed 42) 0.554 ± 0.154 0.563 ± 0.142
Stanford finger, LOO-FT (r) 0.551 ± 0.147 0.537 ± 0.146
Brain Treebank A, layer-wise gate (AUROC) 0.638 ± 0.078 0.651 ± 0.078
Brain Treebank B, layer-wise gate 0.749 ± 0.135 0.735 ± 0.135
Brain Treebank A, mean-pool fusion 0.616 ± 0.072 0.596 ± 0.064
Brain Treebank B, mean-pool fusion 0.753 ± 0.133 0.756 ± 0.132
Brain Treebank A, random-init backbone 0.538 ± 0.054 0.531 ± 0.050
Brain Treebank B, random-init backbone 0.588 ± 0.103 0.590 ± 0.098

Values are mean ± SD over subjects; Brain Treebank cells average seeds 42, 1 and 2. The per-subject AUROCs correlate with the paper's at r = 0.977–0.997.

These numbers initialise the weights the checkpoint does not load as the reference does: nn.Linear's default for the head, and timm's ViT init for the random-init backbone. With the port's xavier-uniform init instead, the random-init control comes out at 0.608 / 0.672, and Stanford pooled at 0.591, where the paper never trains the head.

Recipe and results: experiments/replications/corteg/ on branch feat/replicate-corteg of bruAristimunha/neuroai-private.

Comment thread braindecode/models/corteg.py Resolved
Comment thread braindecode/models/corteg.py Resolved
Comment thread braindecode/models/corteg.py Resolved
Comment thread braindecode/models/corteg.py Resolved
Comment thread braindecode/models/corteg.py Resolved
Comment thread braindecode/models/corteg.py Resolved

@bruAristimunha bruAristimunha 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.

Small comments here, but almost all good!

@bruAristimunha

Copy link
Copy Markdown
Collaborator

Super good work for me! @julien-gadonneix.

Besides the comments, I just noticed some hard-coded values in the modules that would be better exposed. Is there any way to generate the vocab using MNE?

Copilot AI balanced review requested due to automatic review settings November 8, 2026 16:57

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

…fixes

- The 142 vocabulary positions come from MNE's 10-05 montage (Colin27),
  which reproduces the reference's table to its 6 decimals.
- The high-gamma warm start of a loaded checkpoint moves to the Hub:
  braindecode/corteg-small-pretrained holds ST-EEGFormer small with it, and
  the load hook is gone.
- chs_info positions are checked at construction.
- fourier_max_freq is exposed, and the adapter and gate MLPs use activation.
- forward declares its variables first, then checks, then computes, with the
  shape of each step.
Copilot AI balanced review requested due to automatic review settings November 8, 2026 17:04

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

The 142 slot names duplicated steegformer_channels.json on the Hub; the
positions already come from MNE's 10-05 montage.
Copilot AI balanced review requested due to automatic review settings November 9, 2026 12:37

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

Copilot AI balanced review requested due to automatic review settings November 9, 2026 12:39

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@bruAristimunha
bruAristimunha merged commit 0f1cce2 into master Nov 9, 2026
10 checks passed
bruAristimunha added a commit to bruAristimunha/braindecode that referenced this pull request Nov 9, 2026
Merge master 0f1cce2 (braindecode#1551, CORTEG).
Conflicts resolved:
- NOTICE.txt: master's file plus the meggpt.py MIT line after corteg.py.
- test/unit_tests/models/test_pretrained_compat.py: NO_STRATEGY keeps
  master's CORTEG and this branch's MEGGPT.
docs/whats_new.rst, docs/api.rst, summary.csv, __init__.py, util.py and the
shared test registries merged without conflict, keeping both entries.
braindecode/models/meggpt.py is unchanged from b289953.
bruAristimunha added a commit to bruAristimunha/braindecode that referenced this pull request Nov 9, 2026
Brings in 0f1cce2 (braindecode#1551 CORTEG). Conflicts resolved by keeping both sides:
- docs/whats_new.rst: master's file plus the NICE entry after CORTEG's.
- NOTICE.txt: master's file plus braindecode/models/nice.py.
- test_pretrained_compat.py: NO_STRATEGY lists both CORTEG and NICE.
bruAristimunha added a commit to bruAristimunha/braindecode that referenced this pull request Nov 9, 2026
Merge origin/master 0f1cce2 (braindecode#1551 CORTEG).

Resolved: test/unit_tests/models/test_pretrained_compat.py NO_STRATEGY
keeps master's CORTEG and this branch's BBL (one entry per line).
NOTICE.txt, docs/whats_new.rst, docs/api.rst, summary.csv and
models/__init__.py merged automatically; the diff against master is
exactly this branch's additions.
@bruAristimunha
bruAristimunha deleted the feat/corteg branch November 22, 2026 23:01
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.

3 participants