Skip to content

Add LaBraM, an EEG foundation model, with referential TUAB and TUEV tasks - #1730

Merged
solarsys merged 3 commits into
sunlabuiuc:masterfrom
AxelNoun:labram
Nov 30, 2026
Merged

solarsys merged 3 commits into
sunlabuiuc:masterfrom
AxelNoun:labram

Conversation

@AxelNoun

Copy link
Copy Markdown
Contributor

Contributor: Axel Coiffard-Donaty (@AxelNoun)

Contribution Type: New model and tasks

Description:

Adds LaBraM (Jiang et al., ICLR 2024), an EEG foundation model, ported from the fine-tuning encoder of 935963004/LaBraM (MIT) without timm. Parameter names match the official ones, so the released labram-base.pth loads directly, read with weights_only=True. Electrodes are identified by name through ch_names.

LaBraM needs referential channels, while EEGAbnormalTUAB and EEGEventsTUEV build 16 bipolar ones. EEGAbnormalTUABReferential and EEGEventsTUEVReferential subclass them in a new file and only replace the montage step, keeping the 23 channels of the authors' preprocessing. The existing tasks are unchanged.

Files to Review:

  • pyhealth/models/labram.py: LaBraM, LaBraMEncoder, checkpoint loading.
  • pyhealth/tasks/tuh_referential_tasks.py: the two referential tasks.
  • tests/core/test_labram.py, tests/core/test_tuh_referential_tasks.py: synthetic tests.
  • examples/eeg/eeg_models/labram_parity_check.py: comparison with the official code.
  • examples/eeg/eeg_models/labram_tuh_finetuning.py: fine-tuning on TUAB or TUEV.
  • Docs pages and __init__.py exports.

Testing:

  • New tests plus the TUAB, TUEV, EEGNet, BIOT and processor compatibility tests: 136 passed (Windows, Python 3.13, PyTorch 2.15).
  • Parity with the official code on labram-base.pth, 23 channels, 5, 10 and 16 s windows: largest difference 2.1e-14 in float64 (1.1e-5 in float32, rounding).
  • The fine-tuning script runs end to end on synthetic EDF files, on CPU and CUDA.
  • tools/check_pr_rules.py: passed.

Not done yet: fine-tuning results on TUAB and TUEV (the paper reports balanced accuracy 0.8140 and 0.6409 for LaBraM-Base). I do not have TUH access yet; I have requested it from NEDC and will add the results here; a follow-up issue tracks it. The script also differs from the official recipe: no warm-up or cosine schedule, no label smoothing.

EEGAbnormalTUABReferential and EEGEventsTUEVReferential subclass the
existing TUH tasks and only replace their montage step: the signal keeps
the 23 referential electrodes of the LaBraM preprocessing (FP1 to T2, in
the order of make_TUAB.py and make_TUEV.py) instead of 16 bipolar
channels. Filtering, resampling, windows and labels are unchanged.

Recordings that lack one of the 23 EEG XX-REF channels are skipped with
a warning that names every missing channel. compute_stft defaults to
False, since the stft field feeds TFM-Tokenizer, which expects bipolar
channels. The existing tasks are not modified.
Port the fine-tuning encoder of LaBraM (Jiang et al., ICLR 2024) from
modeling_finetune.py of 935963004/LaBraM (MIT License), without the timm
dependency. Parameter names match the official ones, so labram-base.pth
loads directly. load_pretrained_weights reads it with weights_only=True,
allowing only the argparse and NumPy types the file stores.

Electrodes are identified by name through ch_names, which selects the
rows of the spatial embedding. Inputs are 200 Hz signals in microvolts
of at most 16 seconds; forward divides them by 100 as the official loops
do.

labram_parity_check.py compares the port with the official code on the
released weights (largest difference 2e-14 in float64).
labram_tuh_finetuning.py fine-tunes on TUAB or TUEV with the official
optimizer settings, including the layer-wise learning-rate decay; its
header lists where it departs from the official run (no warm-up or
cosine schedule, no label smoothing).
The docs register the TUH tasks under their modules
(pyhealth.tasks.temple_university_EEG_tasks and
pyhealth.tasks.tuh_referential_tasks) and the LaBraM helpers under
pyhealth.models.labram, so the short paths used in the docstrings did
not resolve to links. Point every cross-reference at the documented path
and document TUH_REFERENTIAL_CHANNELS.
@github-actions

Copy link
Copy Markdown

Test Coverage

Total: 88.24% (57664 / 65347 lines)

Modified file Coverage Lines
pyhealth/models/__init__.py 100.00% 121 / 121
pyhealth/models/labram.py 98.81% 250 / 253
pyhealth/tasks/__init__.py 98.57% 138 / 140
pyhealth/tasks/tuh_referential_tasks.py 100.00% 37 / 37

Workflow run · b517d6d

@solarsys solarsys added the status: automatic The PR will go through automatic review process. label Nov 30, 2026
@solarsys

Copy link
Copy Markdown
Collaborator

/review

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Reviewed commit b517d6d

Comment thread pyhealth/models/labram.py
TUAB and TUEV with 23 referential channels. The 16 bipolar channels that
:class:`~pyhealth.tasks.temple_university_EEG_tasks.EEGAbnormalTUAB` and
:class:`~pyhealth.tasks.temple_university_EEG_tasks.EEGEventsTUEV`
produce are not electrodes of that list: use

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

[low] Docstring misstates which bipolar names are electrodes

The LaBraM class docstring says the 16 bipolar channels produced by EEGAbnormalTUAB and EEGEventsTUEV 'are not electrodes of that list'. LABRAM_CHANNELS does contain bipolar names such as 'FP1-F7' through 'P4-O2', and the official code accepts them. The accurate point is that those tasks' output rows carry no electrode names, so they cannot be passed as ch_names. Reword the sentence to say that the tasks' bipolar rows have no names, and that the referential tasks should be used instead.

Comment thread pyhealth/models/labram.py
self.proj = nn.Linear(dim, dim)
self.proj_drop = nn.Dropout(proj_drop)

def forward(self, x: torch.Tensor, return_attention: bool = False) -> torch.Tensor:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

[low] Unused return_attention path in LaBraMAttention.forward

The return_attention argument (and the branch at line 294) is not used by the model, the encoder, the tests, the examples or the rest of pyhealth. Remove it, or keep it only if attention visualisation is planned and tested. As written it is untested dead code in a public class.

return data[[index[name] for name in _EDF_CHANNELS]]


class EEGAbnormalTUABReferential(EEGAbnormalTUAB):

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

[low] Referential tasks drop recordings without all 23 channels, and the number dropped is not documented

EEGAbnormalTUABReferential and EEGEventsTUEVReferential skip every recording that lacks any of the 23 referential channels (including T1, T2, A1, A2, FZ, CZ and PZ). The 16-channel parent tasks skip fewer recordings. The class docstrings say recordings are skipped with a warning but not how much data is lost. Add a sentence saying the referential tasks keep fewer recordings than the bipolar tasks, so readers can compare results with the paper's TUAB and TUEV numbers on the same recording set.

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

AI Review

suggestion: approve
decision: approve

Thank you for this contribution, Axel. It adds LaBraM, an EEG foundation model that loads the released labram-base.pth weights without timm. It also adds two referential-channel variants of the TUAB and TUEV tasks. The change looks ready to merge.

  • Correctness: The model keeps the official parameter names. It reads checkpoints with weights_only=True and a small allow-list of data-only types. It also validates channel names and signal shapes. The two tasks subclass the existing TUAB and TUEV tasks and replace only the montage step. The existing tasks are unchanged, and your tests check this.
  • Sharing: The subclassing of the concrete TUAB and TUEV tasks is stated in the description, with the reason (only the montage step is replaced).
  • Tests and coverage: The synthetic tests are thorough. They cover channel mapping, encoder shapes, checkpoint layouts, rejection of unsafe pickles, task skip behaviour and parent-task regressions. Coverage is 98.8% for labram.py and 100% for tuh_referential_tasks.py. The two __init__.py files are at 100% and 98.6%. The whole repository is at 88.2%. All of these are above the bar.
  • Docs: The docs match the code. The description states the limitations clearly: no TUAB or TUEV results yet, and the fine-tuning script differs from the official recipe.

Optional suggestions:

  • The LaBraM docstring says the 16 bipolar channels "are not electrodes of that list". LABRAM_CHANNELS does contain bipolar-style names such as "FP1-F7". Saying that the task's bipolar channel names are not entries of the list would be more accurate.
  • LaBraMAttention's return_attention path is unused. You could remove it or add a short test.
  • np._core.multiarray.scalar exists only in NumPy 2 and later. If PyHealth supports NumPy 1.x, import would fail there, so a fallback to np.core would help.
  • Once you have TUH access, adding the fine-tuning results to the description would be useful. The note on how many recordings the 23-channel requirement skips would help readers too.

Reviewed commit b517d6d

@solarsys
solarsys merged commit 167c42c into sunlabuiuc:master Nov 30, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

status: automatic The PR will go through automatic review process.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants