braindecode 1.3.0.dev181065563__py3-none-any.whl → 1.3.0.dev181594385__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of braindecode might be problematic. Click here for more details.

Files changed (68) hide show
  1. braindecode/augmentation/base.py +1 -1
  2. braindecode/augmentation/functional.py +154 -54
  3. braindecode/augmentation/transforms.py +2 -2
  4. braindecode/datasets/__init__.py +10 -2
  5. braindecode/datasets/base.py +116 -152
  6. braindecode/datasets/bcicomp.py +4 -4
  7. braindecode/datasets/bids.py +3 -3
  8. braindecode/datasets/experimental.py +2 -2
  9. braindecode/datasets/mne.py +3 -5
  10. braindecode/datasets/moabb.py +2 -2
  11. braindecode/datasets/nmt.py +2 -2
  12. braindecode/datasets/sleep_physio_challe_18.py +4 -3
  13. braindecode/datasets/sleep_physionet.py +2 -2
  14. braindecode/datasets/tuh.py +2 -2
  15. braindecode/datasets/xy.py +2 -2
  16. braindecode/datautil/serialization.py +18 -13
  17. braindecode/eegneuralnet.py +2 -0
  18. braindecode/functional/functions.py +6 -2
  19. braindecode/functional/initialization.py +2 -3
  20. braindecode/models/__init__.py +6 -0
  21. braindecode/models/atcnet.py +33 -34
  22. braindecode/models/attentionbasenet.py +39 -32
  23. braindecode/models/attn_sleep.py +2 -0
  24. braindecode/models/base.py +280 -2
  25. braindecode/models/bendr.py +469 -0
  26. braindecode/models/biot.py +3 -1
  27. braindecode/models/contrawr.py +2 -0
  28. braindecode/models/ctnet.py +8 -3
  29. braindecode/models/deepsleepnet.py +28 -19
  30. braindecode/models/eegconformer.py +2 -2
  31. braindecode/models/eeginception_erp.py +31 -25
  32. braindecode/models/eegitnet.py +2 -0
  33. braindecode/models/eegminer.py +2 -0
  34. braindecode/models/eegnet.py +1 -1
  35. braindecode/models/eegtcnet.py +2 -0
  36. braindecode/models/fbcnet.py +2 -0
  37. braindecode/models/fblightconvnet.py +2 -0
  38. braindecode/models/fbmsnet.py +2 -0
  39. braindecode/models/ifnet.py +2 -0
  40. braindecode/models/labram.py +193 -87
  41. braindecode/models/msvtnet.py +2 -0
  42. braindecode/models/patchedtransformer.py +640 -0
  43. braindecode/models/signal_jepa.py +111 -27
  44. braindecode/models/sinc_shallow.py +12 -9
  45. braindecode/models/sstdpn.py +869 -0
  46. braindecode/models/summary.csv +9 -6
  47. braindecode/models/syncnet.py +2 -0
  48. braindecode/models/tcn.py +2 -0
  49. braindecode/models/usleep.py +26 -21
  50. braindecode/models/util.py +3 -0
  51. braindecode/modules/attention.py +10 -10
  52. braindecode/modules/blocks.py +3 -3
  53. braindecode/modules/filter.py +2 -3
  54. braindecode/modules/layers.py +18 -17
  55. braindecode/preprocessing/__init__.py +24 -0
  56. braindecode/preprocessing/eegprep_preprocess.py +1202 -0
  57. braindecode/preprocessing/preprocess.py +23 -14
  58. braindecode/preprocessing/util.py +166 -0
  59. braindecode/preprocessing/windowers.py +24 -19
  60. braindecode/samplers/base.py +8 -8
  61. braindecode/version.py +1 -1
  62. {braindecode-1.3.0.dev181065563.dist-info → braindecode-1.3.0.dev181594385.dist-info}/METADATA +6 -2
  63. braindecode-1.3.0.dev181594385.dist-info/RECORD +106 -0
  64. braindecode-1.3.0.dev181065563.dist-info/RECORD +0 -101
  65. {braindecode-1.3.0.dev181065563.dist-info → braindecode-1.3.0.dev181594385.dist-info}/WHEEL +0 -0
  66. {braindecode-1.3.0.dev181065563.dist-info → braindecode-1.3.0.dev181594385.dist-info}/licenses/LICENSE.txt +0 -0
  67. {braindecode-1.3.0.dev181065563.dist-info → braindecode-1.3.0.dev181594385.dist-info}/licenses/NOTICE.txt +0 -0
  68. {braindecode-1.3.0.dev181065563.dist-info → braindecode-1.3.0.dev181594385.dist-info}/top_level.txt +0 -0
@@ -5,7 +5,8 @@ from __future__ import annotations
5
5
 
6
6
  import math
7
7
  from copy import deepcopy
8
- from typing import Any, Sequence
8
+ from pathlib import Path
9
+ from typing import Any, Optional, Sequence
9
10
 
10
11
  import torch
11
12
  from einops.layers.torch import Rearrange
@@ -145,6 +146,8 @@ class _BaseSignalJEPA(EEGModuleMixin, nn.Module):
145
146
  class SignalJEPA(_BaseSignalJEPA):
146
147
  """Architecture introduced in signal-JEPA for self-supervised pre-training, Guetschel, P et al (2024) [1]_
147
148
 
149
+ :bdg-success:`Convolution` :bdg-dark-line:`Channel` :bdg-danger:`Large Brain Model`
150
+
148
151
  This model is not meant for classification but for SSL pre-training.
149
152
  Its output shape depends on the input shape.
150
153
  For classification purposes, three variants of this model are available:
@@ -231,6 +234,8 @@ class SignalJEPA(_BaseSignalJEPA):
231
234
  class SignalJEPA_Contextual(_BaseSignalJEPA):
232
235
  """Contextual downstream architecture introduced in signal-JEPA Guetschel, P et al (2024) [1]_.
233
236
 
237
+ :bdg-success:`Convolution` :bdg-dark-line:`Channel` :bdg-danger:`Large Brain Model`
238
+
234
239
  This architecture is one of the variants of :class:`SignalJEPA`
235
240
  that can be used for classification purposes.
236
241
 
@@ -319,25 +324,50 @@ class SignalJEPA_Contextual(_BaseSignalJEPA):
319
324
  @classmethod
320
325
  def from_pretrained(
321
326
  cls,
322
- model: SignalJEPA,
323
- n_outputs: int,
327
+ model: Optional[SignalJEPA | str | Path] = None, # type: ignore
328
+ n_outputs: Optional[int] = None, # type: ignore
324
329
  n_spat_filters: int = 4,
325
- chs_info: list[dict[str, Any]] | None = None,
330
+ chs_info: Optional[list[dict[str, Any]]] = None, # type: ignore
331
+ **kwargs,
326
332
  ):
327
- """Instantiate a new model from a pre-trained :class:`SignalJEPA` model.
333
+ """Instantiate a new model from a pre-trained :class:`SignalJEPA` model or from Hub.
328
334
 
329
335
  Parameters
330
336
  ----------
331
- model: SignalJEPA
332
- Pre-trained model.
333
- n_outputs: int
334
- Number of classes for the new model.
337
+ model: SignalJEPA, str, Path, or None
338
+ Either a pre-trained :class:`SignalJEPA` model, a string/Path to a local directory
339
+ (for Hub-style loading), or None (for Hub loading via kwargs).
340
+ n_outputs: int or None
341
+ Number of classes for the new model. Required when loading from a SignalJEPA model,
342
+ optional when loading from Hub (will be read from config).
335
343
  n_spat_filters: int
336
344
  Number of spatial filters.
337
345
  chs_info: list of dict | None
338
346
  Information about each individual EEG channel. This should be filled with
339
347
  ``info["chs"]``. Refer to :class:`mne.Info` for more details.
348
+ **kwargs
349
+ Additional keyword arguments passed to the parent class for Hub loading.
340
350
  """
351
+ # Check if this is a Hub-style load (from a directory path)
352
+ if isinstance(model, (str, Path)) or (model is None and kwargs):
353
+ # This is a Hub load, delegate to parent class
354
+ if isinstance(model, (str, Path)):
355
+ # model is actually the repo_id or directory path
356
+ return super().from_pretrained(model, **kwargs)
357
+ else:
358
+ # model is None, treat as hub-style load
359
+ return super().from_pretrained(**kwargs)
360
+
361
+ # This is the original SignalJEPA transfer learning case
362
+ if not isinstance(model, SignalJEPA):
363
+ raise TypeError(
364
+ f"model must be a SignalJEPA instance, a path string, or Path object, got {type(model)}"
365
+ )
366
+ if n_outputs is None:
367
+ raise ValueError(
368
+ "n_outputs must be provided when loading from a SignalJEPA model"
369
+ )
370
+
341
371
  feature_encoder = model.feature_encoder
342
372
  pos_encoder = model.pos_encoder
343
373
  transformer = model.transformer
@@ -377,6 +407,8 @@ class SignalJEPA_Contextual(_BaseSignalJEPA):
377
407
  class SignalJEPA_PostLocal(_BaseSignalJEPA):
378
408
  """Post-local downstream architecture introduced in signal-JEPA Guetschel, P et al (2024) [1]_.
379
409
 
410
+ :bdg-success:`Convolution` :bdg-dark-line:`Channel` :bdg-danger:`Large Brain Model`
411
+
380
412
  This architecture is one of the variants of :class:`SignalJEPA`
381
413
  that can be used for classification purposes.
382
414
 
@@ -463,22 +495,47 @@ class SignalJEPA_PostLocal(_BaseSignalJEPA):
463
495
 
464
496
  @classmethod
465
497
  def from_pretrained(
466
- cls, model: SignalJEPA, n_outputs: int, n_spat_filters: int = 4
498
+ cls,
499
+ model: SignalJEPA | str | Path = None, # type: ignore
500
+ n_outputs: int = None, # type: ignore
501
+ n_spat_filters: int = 4,
502
+ **kwargs,
467
503
  ):
468
- """Instantiate a new model from a pre-trained :class:`SignalJEPA` model.
504
+ """Instantiate a new model from a pre-trained :class:`SignalJEPA` model or from Hub.
469
505
 
470
506
  Parameters
471
507
  ----------
472
- model: SignalJEPA
473
- Pre-trained model.
474
- n_outputs: int
475
- Number of classes for the new model.
508
+ model: SignalJEPA, str, Path, or None
509
+ Either a pre-trained :class:`SignalJEPA` model, a string/Path to a local directory
510
+ (for Hub-style loading), or None (for Hub loading via kwargs).
511
+ n_outputs: int or None
512
+ Number of classes for the new model. Required when loading from a SignalJEPA model,
513
+ optional when loading from Hub (will be read from config).
476
514
  n_spat_filters: int
477
515
  Number of spatial filters.
478
- chs_info: list of dict | None
479
- Information about each individual EEG channel. This should be filled with
480
- ``info["chs"]``. Refer to :class:`mne.Info` for more details.
516
+ **kwargs
517
+ Additional keyword arguments passed to the parent class for Hub loading.
481
518
  """
519
+ # Check if this is a Hub-style load (from a directory path)
520
+ if isinstance(model, (str, Path)) or (model is None and kwargs):
521
+ # This is a Hub load, delegate to parent class
522
+ if isinstance(model, (str, Path)):
523
+ # model is actually the repo_id or directory path
524
+ return super().from_pretrained(model, **kwargs)
525
+ else:
526
+ # model is None, treat as hub-style load
527
+ return super().from_pretrained(**kwargs)
528
+
529
+ # This is the original SignalJEPA transfer learning case
530
+ if not isinstance(model, SignalJEPA):
531
+ raise TypeError(
532
+ f"model must be a SignalJEPA instance, a path string, or Path object, got {type(model)}"
533
+ )
534
+ if n_outputs is None:
535
+ raise ValueError(
536
+ "n_outputs must be provided when loading from a SignalJEPA model"
537
+ )
538
+
482
539
  feature_encoder = model.feature_encoder
483
540
  assert feature_encoder is not None
484
541
  new_model = cls(
@@ -501,6 +558,8 @@ class SignalJEPA_PostLocal(_BaseSignalJEPA):
501
558
  class SignalJEPA_PreLocal(_BaseSignalJEPA):
502
559
  """Pre-local downstream architecture introduced in signal-JEPA Guetschel, P et al (2024) [1]_.
503
560
 
561
+ :bdg-success:`Convolution` :bdg-dark-line:`Channel` :bdg-danger:`Large Brain Model`
562
+
504
563
  This architecture is one of the variants of :class:`SignalJEPA`
505
564
  that can be used for classification purposes.
506
565
 
@@ -597,22 +656,47 @@ class SignalJEPA_PreLocal(_BaseSignalJEPA):
597
656
 
598
657
  @classmethod
599
658
  def from_pretrained(
600
- cls, model: SignalJEPA, n_outputs: int, n_spat_filters: int = 4
659
+ cls,
660
+ model: SignalJEPA | str | Path = None, # type: ignore
661
+ n_outputs: int = None, # type: ignore
662
+ n_spat_filters: int = 4,
663
+ **kwargs,
601
664
  ):
602
- """Instantiate a new model from a pre-trained :class:`SignalJEPA` model.
665
+ """Instantiate a new model from a pre-trained :class:`SignalJEPA` model or from Hub.
603
666
 
604
667
  Parameters
605
668
  ----------
606
- model: SignalJEPA
607
- Pre-trained model.
608
- n_outputs: int
609
- Number of classes for the new model.
669
+ model: SignalJEPA, str, Path, or None
670
+ Either a pre-trained :class:`SignalJEPA` model, a string/Path to a local directory
671
+ (for Hub-style loading), or None (for Hub loading via kwargs).
672
+ n_outputs: int or None
673
+ Number of classes for the new model. Required when loading from a SignalJEPA model,
674
+ optional when loading from Hub (will be read from config).
610
675
  n_spat_filters: int
611
676
  Number of spatial filters.
612
- chs_info: list of dict | None
613
- Information about each individual EEG channel. This should be filled with
614
- ``info["chs"]``. Refer to :class:`mne.Info` for more details.
677
+ **kwargs
678
+ Additional keyword arguments passed to the parent class for Hub loading.
615
679
  """
680
+ # Check if this is a Hub-style load (from a directory path)
681
+ if isinstance(model, (str, Path)) or (model is None and kwargs):
682
+ # This is a Hub load, delegate to parent class
683
+ if isinstance(model, (str, Path)):
684
+ # model is actually the repo_id or directory path
685
+ return super().from_pretrained(model, **kwargs)
686
+ else:
687
+ # model is None, treat as hub-style load
688
+ return super().from_pretrained(**kwargs)
689
+
690
+ # This is the original SignalJEPA transfer learning case
691
+ if not isinstance(model, SignalJEPA):
692
+ raise TypeError(
693
+ f"model must be a SignalJEPA instance, a path string, or Path object, got {type(model)}"
694
+ )
695
+ if n_outputs is None:
696
+ raise ValueError(
697
+ "n_outputs must be provided when loading from a SignalJEPA model"
698
+ )
699
+
616
700
  feature_encoder = model.feature_encoder
617
701
  assert feature_encoder is not None
618
702
  new_model = cls(
@@ -12,6 +12,8 @@ from braindecode.models.base import EEGModuleMixin
12
12
  class SincShallowNet(EEGModuleMixin, nn.Module):
13
13
  """Sinc-ShallowNet from Borra, D et al (2020) [borra2020]_.
14
14
 
15
+ :bdg-success:`Convolution` :bdg-warning:`Interpretability`
16
+
15
17
  .. figure:: https://ars.els-cdn.com/content/image/1-s2.0-S0893608020302021-gr2_lrg.jpg
16
18
  :align: center
17
19
  :alt: SincShallowNet Architecture
@@ -19,23 +21,24 @@ class SincShallowNet(EEGModuleMixin, nn.Module):
19
21
  The Sinc-ShallowNet architecture has these fundamental blocks:
20
22
 
21
23
  1. **Block 1: Spectral and Spatial Feature Extraction**
22
- - *Temporal Sinc-Convolutional Layer*:
23
- Uses parametrized sinc functions to learn band-pass filters,
24
- significantly reducing the number of trainable parameters by only
25
- learning the lower and upper cutoff frequencies for each filter.
26
- - *Spatial Depthwise Convolutional Layer*:
27
- Applies depthwise convolutions to learn spatial filters for
28
- each temporal feature map independently, further reducing
29
- parameters and enhancing interpretability.
30
- - *Batch Normalization*
24
+
25
+ - *Temporal Sinc-Convolutional Layer*: Uses parametrized sinc functions to learn band-pass filters,
26
+ significantly reducing the number of trainable parameters by only
27
+ learning the lower and upper cutoff frequencies for each filter.
28
+ - *Spatial Depthwise Convolutional Layer*: Applies depthwise convolutions to learn spatial filters for
29
+ each temporal feature map independently, further reducing
30
+ parameters and enhancing interpretability.
31
+ - *Batch Normalization*
31
32
 
32
33
  2. **Block 2: Temporal Aggregation**
34
+
33
35
  - *Activation Function*: ELU
34
36
  - *Average Pooling Layer*: Aggregation by averaging spatial dim
35
37
  - *Dropout Layer*
36
38
  - *Flatten Layer*
37
39
 
38
40
  3. **Block 3: Classification**
41
+
39
42
  - *Fully Connected Layer*: Maps the feature vector to n_outputs.
40
43
 
41
44
  **Implementation Notes:**