Repository navigation
CSBrain: reuse CBraMod's patch embedding; fix CBraMod patch_size != 200 - #1240
Merged
bruAristimunha merged 6 commits intoOct 7, 2026
Merged
bruAristimunha merged 6 commits into
bruAristimunha merged 6 commits into
Conversation
Contributor
There was a problem hiding this comment.
Copilot review overview
🟡 Changes recommended
The RNG rewind only handles CPU state, breaking initialization parity when models are constructed directly on non-CPU devices.
Review effort: Balanced
Findings: 1
Open (1)
What changed in this PR
Refactors CSBrain to reuse CBraMod’s patch embedding and fixes non-default patch sizes.
Changes:
- Shares patch-embedding implementation between CSBrain and CBraMod.
- Derives spectral bins from
patch_size. - Adds regression coverage and release notes.
| File | Description |
|---|---|
braindecode/models/csbrain.py |
Reuses CBraMod’s patch embedding while preserving initialization order. |
braindecode/models/cbramod.py |
Fixes dynamic rFFT-bin reshaping. |
test/unit_tests/models/test_models.py |
Tests 400-sample patches for both models. |
docs/whats_new.rst |
Documents the refactor and fix. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1240 +/- ##
==========================================
- Coverage 88.52% 88.51% -0.01%
==========================================
Files 158 158
Lines 19332 19306 -26
==========================================
- Hits 17113 17089 -24
+ Misses 2219 2217 -2 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Follow-up to #1196. It contains no new behaviour, plus one CBraMod fix.
Changes
csbrain.py: drop CSBrain's private copy of the patch embedding (conv stem + rFFT magnitude + depthwise-conv positional encoding) and build CBraMod's_PatchEmbeddingwith the same conv spec (−90 lines).cbramod.py: the spectral reshape hard-codedp=101, soCBraMod(patch_size=400)failed inforward(einops shape error on master). It now usespatch_size // 2 + 1.test_cbramod_patch_embedding_patch_size_not_200[CBraMod|CSBrain]intest_models.py. On master the CBraMod case fails and the CSBrain case passes. Both pass on this branch.Parity against master c282258 (CPU, fp32,
torch.manual_seed(42), fixed input batch of 4, eval mode):The key set is unchanged. Inside
patch_embedding, thestate_dict()iteration order now follows CBraMod's module order, which has no effect onload_state_dict.Tests (
-k "(CSBrain or csbrain or CBraMod or cbramod) and not compiled"): test_models 27 passed, test_integration 20 passed / 2 skipped, test_return_features 8 passed, test_foundation_models 3 passed / 1 skipped, test_pretrained_compat 8 passed.pre-commit run --fileson the four touched files: rc=0.