Skip to content

Models configs via pydantic - #850

Merged
bruAristimunha merged 29 commits into
braindecode:masterfrom
PierreGtch:models-config
Nov 22, 2025
Merged

bruAristimunha merged 29 commits into
braindecode:masterfrom
PierreGtch:models-config

Conversation

@PierreGtch

Copy link
Copy Markdown
Collaborator

This adds pydantic config classes that are automatically generated for every model!

pydantic is a soft dependancy. @bruAristimunha can you check how I implemented the soft dep? Not sure this is the best solution

Use case:

from braindecode.models.config import EEGNetConfig

cfg = EEGNetConfig(n_chans=16, n_outputs=1, n_times=1000)
cfg  # EEGNetConfig(n_chans=16, n_outputs=1, n_times=1000, final_conv_length='auto', pool_mode='mean', F1=8, D=2, F2=None, kernel_length=64, depthwise_kernel_length=16, pool1_kernel_size=4, pool2_kernel_size=8, conv_spatial_max_norm=1, activation=<class 'torch.nn.modules.activation.ELU'>, batch_norm_momentum=0.01, batch_norm_affine=True, batch_norm_eps=0.001, drop_prob=0.25, final_layer_with_constraint=False, norm_rate=0.25, chs_info=None, input_window_seconds=None, sfreq=None)

Models configs benefit from all the nice pydantic functionalities, including easy serialisation for example:

cfg.model_dump()
{'n_chans': 16,
 'n_outputs': 1,
 'n_times': 1000,
 'final_conv_length': 'auto',
 'pool_mode': 'mean',
 'F1': 8,
 'D': 2,
 'F2': None,
 'kernel_length': 64,
 'depthwise_kernel_length': 16,
 'pool1_kernel_size': 4,
 'pool2_kernel_size': 8,
 'conv_spatial_max_norm': 1,
 'activation': torch.nn.modules.activation.ELU,
 'batch_norm_momentum': 0.01,
 'batch_norm_affine': True,
 'batch_norm_eps': 0.001,
 'drop_prob': 0.25,
 'final_layer_with_constraint': False,
 'norm_rate': 0.25,
 'chs_info': None,
 'input_window_seconds': None,
 'sfreq': None}

Then the model can be instantiated with the create_instance() method:

model = cfg.create_instance()
type(model) # braindecode.models.eegnet.EEGNet

@codecov

codecov Bot commented Nov 19, 2025 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 92.13483% with 14 lines in your changes missing coverage. Please review.
✅ Project coverage is 87.83%. Comparing base (14ab8be) to head (26ca601).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #850      +/-   ##
==========================================
+ Coverage   87.77%   87.83%   +0.06%     
==========================================
  Files         107      108       +1     
  Lines        9774     9942     +168     
==========================================
+ Hits         8579     8733     +154     
- Misses       1195     1209      +14     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

by testing all allowed combinations of signal parameters for each model config
an uninstanciated class MyClass should be of type type[MyClass] and not MyClass
@PierreGtch

PierreGtch commented Nov 20, 2025 •

Copy link
Copy Markdown
Collaborator Author

TODO :

  • include in the doc, but how? I don't think we want to "duplicate" the list of models in the doc. Maybe just a link in the models' docstrings? @bruAristimunha WDYT?
  • create a tutorial with a fully-configured exca experiment. Other PR

@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 modifications needed, i will push the solf import.

Comment thread braindecode/models/config.py Resolved
Comment thread braindecode/models/config.py Resolved
Comment thread braindecode/models/util.py Resolved
@PierreGtch

Copy link
Copy Markdown
Collaborator Author

@bruAristimunha thanks for pushing the soft import. Regarding this, the configs are located in :

from braindecode.models.config import EEGNetConfig

You think it's ok? maybe we could put them directly in :

from braindecode.models import EEGNetConfig

@bruAristimunha

Copy link
Copy Markdown
Collaborator

Let's keep within config, I would like to iterate with the brain & AI team about this within the Exca tutorial.

@PierreGtch

Copy link
Copy Markdown
Collaborator Author

I will start a PR on the exca tutorial when this is merged

@PierreGtch

Copy link
Copy Markdown
Collaborator Author

@bruAristimunha it looks a bit strange to have the whole file indented :/

@bruAristimunha
bruAristimunha merged commit 74f07fa into braindecode:master Nov 22, 2025
3 checks passed
@bruAristimunha

Copy link
Copy Markdown
Collaborator

thanks @PierreGtch 🙏🏽

@PierreGtch

Copy link
Copy Markdown
Collaborator Author

thanks for your help!

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.

2 participants