Skip to content

Commit 38ef5a7

Browse files
1 parent c029c1c commit 38ef5a7

File tree

299 files changed

+57721
-4868
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

299 files changed

+57721
-4868
lines changed

dev/.buildinfo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# Sphinx build info version 1
22
# This file records the configuration used when building these files. When it is not found, a full rebuild will be done.
3-
config: 197cdac18e84d4d81f6881c687f7536c
3+
config: 408836cd0351c07e9fbcac725fb3d811
44
tags: 645f666f9bcd5a90fca523b33c5a78b7
Binary file not shown.

dev/_downloads/090305d06248840b75133975e5121f41/plot_sleep_staging_chambon2018.ipynb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"cell_type": "markdown",
55
"metadata": {},
66
"source": [
7-
"\n\n# Sleep staging on the Sleep Physionet dataset using Chambon2018 network\n\nThis tutorial shows how to train and test a sleep staging neural network with\nBraindecode. We adapt the time distributed approach of [1]_ to learn on\nsequences of EEG windows using the openly accessible Sleep Physionet dataset\n[2]_ [3]_.\n"
7+
"\n# Sleep staging on the Sleep Physionet dataset using Chambon2018 network\n\nThis tutorial shows how to train and test a sleep staging neural network with\nBraindecode. We adapt the time distributed approach of [1]_ to learn on\nsequences of EEG windows using the openly accessible Sleep Physionet dataset\n[2]_ [3]_.\n"
88
]
99
},
1010
{
@@ -51,7 +51,7 @@
5151
},
5252
"outputs": [],
5353
"source": [
54-
"from braindecode.preprocessing import preprocess, Preprocessor\nfrom numpy import multiply\n\nhigh_cut_hz = 30\nfactor = 1e6\n\npreprocessors = [\n Preprocessor(\n lambda data: multiply(data, factor), apply_on_array=True\n ), # Convert from V to uV\n Preprocessor(\"filter\", l_freq=None, h_freq=high_cut_hz),\n]\n\n# Transform the data\npreprocess(dataset, preprocessors)"
54+
"from numpy import multiply\n\nfrom braindecode.preprocessing import Preprocessor, preprocess\n\nhigh_cut_hz = 30\nfactor = 1e6\n\npreprocessors = [\n Preprocessor(\n lambda data: multiply(data, factor), apply_on_array=True\n ), # Convert from V to uV\n Preprocessor(\"filter\", l_freq=None, h_freq=high_cut_hz),\n]\n\n# Transform the data\npreprocess(dataset, preprocessors)"
5555
]
5656
},
5757
{
@@ -123,7 +123,7 @@
123123
},
124124
"outputs": [],
125125
"source": [
126-
"import numpy as np\nfrom braindecode.samplers import SequenceSampler\n\nn_windows = 3 # Sequences of 3 consecutive windows\nn_windows_stride = 3 # Maximally overlapping sequences\n\ntrain_sampler = SequenceSampler(\n train_set.get_metadata(), n_windows, n_windows_stride, randomize=True\n)\nvalid_sampler = SequenceSampler(valid_set.get_metadata(), n_windows, n_windows_stride)\n\n# Print number of examples per class\nprint(\"Training examples: \", len(train_sampler))\nprint(\"Validation examples: \", len(valid_sampler))"
126+
"import numpy as np\n\nfrom braindecode.samplers import SequenceSampler\n\nn_windows = 3 # Sequences of 3 consecutive windows\nn_windows_stride = 3 # Maximally overlapping sequences\n\ntrain_sampler = SequenceSampler(\n train_set.get_metadata(), n_windows, n_windows_stride, randomize=True\n)\nvalid_sampler = SequenceSampler(valid_set.get_metadata(), n_windows, n_windows_stride)\n\n# Print number of examples per class\nprint(\"Training examples: \", len(train_sampler))\nprint(\"Validation examples: \", len(valid_sampler))"
127127
]
128128
},
129129
{
@@ -177,7 +177,7 @@
177177
},
178178
"outputs": [],
179179
"source": [
180-
"import torch\nfrom torch import nn\nfrom braindecode.util import set_random_seeds\nfrom braindecode.models import SleepStagerChambon2018, TimeDistributed\n\ncuda = torch.cuda.is_available() # check if GPU is available\ndevice = \"cuda\" if torch.cuda.is_available() else \"cpu\"\nif cuda:\n torch.backends.cudnn.benchmark = True\n# Set random seed to be able to roughly reproduce results\n# Note that with cudnn benchmark set to True, GPU indeterminism\n# may still make results substantially different between runs.\n# To obtain more consistent results at the cost of increased computation time,\n# you can set `cudnn_benchmark=False` in `set_random_seeds`\n# or remove `torch.backends.cudnn.benchmark = True`\nset_random_seeds(seed=31, cuda=cuda)\n\nn_classes = 5\n# Extract number of channels and time steps from dataset\nn_channels, input_size_samples = train_set[0][0].shape\n\nfeat_extractor = SleepStagerChambon2018(\n n_channels,\n sfreq,\n n_outputs=n_classes,\n n_times=input_size_samples,\n return_feats=True,\n)\n\nmodel = nn.Sequential(\n TimeDistributed(feat_extractor), # apply model on each 30-s window\n nn.Sequential( # apply linear layer on concatenated feature vectors\n nn.Flatten(start_dim=1),\n nn.Dropout(0.5),\n nn.Linear(feat_extractor.len_last_layer * n_windows, n_classes),\n ),\n)\n\n# Send model to GPU\nif cuda:\n model.cuda()"
180+
"import torch\nfrom torch import nn\n\nfrom braindecode.models import SleepStagerChambon2018\nfrom braindecode.modules import TimeDistributed\nfrom braindecode.util import set_random_seeds\n\ncuda = torch.cuda.is_available() # check if GPU is available\ndevice = \"cuda\" if torch.cuda.is_available() else \"cpu\"\nif cuda:\n torch.backends.cudnn.benchmark = True\n# Set random seed to be able to roughly reproduce results\n# Note that with cudnn benchmark set to True, GPU indeterminism\n# may still make results substantially different between runs.\n# To obtain more consistent results at the cost of increased computation time,\n# you can set `cudnn_benchmark=False` in `set_random_seeds`\n# or remove `torch.backends.cudnn.benchmark = True`\nset_random_seeds(seed=31, cuda=cuda)\n\nn_classes = 5\n# Extract number of channels and time steps from dataset\nn_channels, input_size_samples = train_set[0][0].shape\n\nfeat_extractor = SleepStagerChambon2018(\n n_channels,\n sfreq,\n n_outputs=n_classes,\n n_times=input_size_samples,\n return_feats=True,\n)\n\nmodel = nn.Sequential(\n TimeDistributed(feat_extractor), # apply model on each 30-s window\n nn.Sequential( # apply linear layer on concatenated feature vectors\n nn.Flatten(start_dim=1),\n nn.Dropout(0.5),\n nn.Linear(feat_extractor.len_last_layer * n_windows, n_classes),\n ),\n)\n\n# Send model to GPU\nif cuda:\n model.cuda()"
181181
]
182182
},
183183
{
@@ -195,7 +195,7 @@
195195
},
196196
"outputs": [],
197197
"source": [
198-
"from skorch.helper import predefined_split\nfrom skorch.callbacks import EpochScoring\nfrom braindecode import EEGClassifier\n\nlr = 1e-3\nbatch_size = 32\nn_epochs = 10\n\ntrain_bal_acc = EpochScoring(\n scoring=\"balanced_accuracy\",\n on_train=True,\n name=\"train_bal_acc\",\n lower_is_better=False,\n)\nvalid_bal_acc = EpochScoring(\n scoring=\"balanced_accuracy\",\n on_train=False,\n name=\"valid_bal_acc\",\n lower_is_better=False,\n)\ncallbacks = [(\"train_bal_acc\", train_bal_acc), (\"valid_bal_acc\", valid_bal_acc)]\n\nclf = EEGClassifier(\n model,\n criterion=torch.nn.CrossEntropyLoss,\n criterion__weight=torch.Tensor(class_weights).to(device),\n optimizer=torch.optim.Adam,\n iterator_train__shuffle=False,\n iterator_train__sampler=train_sampler,\n iterator_valid__sampler=valid_sampler,\n train_split=predefined_split(valid_set), # using valid_set for validation\n optimizer__lr=lr,\n batch_size=batch_size,\n callbacks=callbacks,\n device=device,\n classes=np.unique(y_train),\n)\n# Model training for a specified number of epochs. `y` is None as it is already\n# supplied in the dataset.\nclf.fit(train_set, y=None, epochs=n_epochs)"
198+
"from skorch.callbacks import EpochScoring\nfrom skorch.helper import predefined_split\n\nfrom braindecode import EEGClassifier\n\nlr = 1e-3\nbatch_size = 32\nn_epochs = 10\n\ntrain_bal_acc = EpochScoring(\n scoring=\"balanced_accuracy\",\n on_train=True,\n name=\"train_bal_acc\",\n lower_is_better=False,\n)\nvalid_bal_acc = EpochScoring(\n scoring=\"balanced_accuracy\",\n on_train=False,\n name=\"valid_bal_acc\",\n lower_is_better=False,\n)\ncallbacks = [(\"train_bal_acc\", train_bal_acc), (\"valid_bal_acc\", valid_bal_acc)]\n\nclf = EEGClassifier(\n model,\n criterion=torch.nn.CrossEntropyLoss,\n criterion__weight=torch.Tensor(class_weights).to(device),\n optimizer=torch.optim.Adam,\n iterator_train__shuffle=False,\n iterator_train__sampler=train_sampler,\n iterator_valid__sampler=valid_sampler,\n train_split=predefined_split(valid_set), # using valid_set for validation\n optimizer__lr=lr,\n batch_size=batch_size,\n callbacks=callbacks,\n device=device,\n classes=np.unique(y_train),\n)\n# Model training for a specified number of epochs. `y` is None as it is already\n# supplied in the dataset.\nclf.fit(train_set, y=None, epochs=n_epochs)"
199199
]
200200
},
201201
{
@@ -231,7 +231,7 @@
231231
},
232232
"outputs": [],
233233
"source": [
234-
"from sklearn.metrics import confusion_matrix, classification_report\nfrom braindecode.visualization import plot_confusion_matrix\n\ny_true = [valid_set[[i]][1][0] for i in range(len(valid_sampler))]\ny_pred = clf.predict(valid_set)\n\nconfusion_mat = confusion_matrix(y_true, y_pred)\n\nplot_confusion_matrix(\n confusion_mat=confusion_mat, class_names=[\"Wake\", \"N1\", \"N2\", \"N3\", \"REM\"]\n)\n\nprint(classification_report(y_true, y_pred))"
234+
"from sklearn.metrics import classification_report, confusion_matrix\n\nfrom braindecode.visualization import plot_confusion_matrix\n\ny_true = [valid_set[[i]][1][0] for i in range(len(valid_sampler))]\ny_pred = clf.predict(valid_set)\n\nconfusion_mat = confusion_matrix(y_true, y_pred)\n\nplot_confusion_matrix(\n confusion_mat=confusion_mat, class_names=[\"Wake\", \"N1\", \"N2\", \"N3\", \"REM\"]\n)\n\nprint(classification_report(y_true, y_pred))"
235235
]
236236
},
237237
{

dev/_downloads/0a8b8bc2f1b933515b7b4101626dd179/plot_bcic_iv_2a_moabb_trial.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
"""
2-
.. _bcic-iv-2a-moabb-trial:
1+
""".. _bcic-iv-2a-moabb-trial:
32
43
Basic Brain Decoding on EEG Data
54
========================================

0 commit comments

Comments
 (0)