Repository navigation
Add LaBraM, an EEG foundation model, with referential TUAB and TUEV tasks - #1730
Conversation
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.
Test CoverageTotal: 88.24% (57664 / 65347 lines)
|
|
/review |
| 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 |
There was a problem hiding this comment.
[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.
| 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: |
There was a problem hiding this comment.
[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): |
There was a problem hiding this comment.
[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.
There was a problem hiding this comment.
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=Trueand 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.pyand 100% fortuh_referential_tasks.py. The two__init__.pyfiles 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
LaBraMdocstring says the 16 bipolar channels "are not electrodes of that list".LABRAM_CHANNELSdoes 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'sreturn_attentionpath is unused. You could remove it or add a short test.np._core.multiarray.scalarexists only in NumPy 2 and later. If PyHealth supports NumPy 1.x, import would fail there, so a fallback tonp.corewould 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
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 releasedlabram-base.pthloads directly, read withweights_only=True. Electrodes are identified by name throughch_names.LaBraM needs referential channels, while
EEGAbnormalTUABandEEGEventsTUEVbuild 16 bipolar ones.EEGAbnormalTUABReferentialandEEGEventsTUEVReferentialsubclass 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.__init__.pyexports.Testing:
labram-base.pth, 23 channels, 5, 10 and 16 s windows: largest difference 2.1e-14 in float64 (1.1e-5 in float32, rounding).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.