Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
bruAristimunha committed Oct 27, 2023
1 parent 39ef456 commit 07125b6
Show file tree
Hide file tree
Showing 189 changed files with 6,758 additions and 4,377 deletions.
2 changes: 1 addition & 1 deletion dev/.buildinfo
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Sphinx build info version 1
# This file hashes the configuration used when building these files. When it is not found, a full rebuild will be done.
config: b2469e7cb779c069e8d7ccba8affea43
config: e088aa471188a8316236d3f2d62304d5
tags: 645f666f9bcd5a90fca523b33c5a78b7
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
from braindecode.datasets import MOABBDataset

subject_id = 3
dataset = MOABBDataset(dataset_name="BNCI2014001", subject_ids=[subject_id])
dataset = MOABBDataset(dataset_name="BNCI2014_001", subject_ids=[subject_id])


######################################################################
Expand Down Expand Up @@ -131,12 +131,12 @@
######################################################################
# We can easily split the dataset using additional info stored in the
# description attribute, in this case ``session`` column. We select
# ``session_T`` for training and ``session_E`` for validation.
# ``T`` for training and ``test`` for validation.
#

splitted = windows_dataset.split('session')
train_set = splitted['session_T']
valid_set = splitted['session_E']
train_set = splitted['0train'] # Session train
valid_set = splitted['1test'] # Session evaluation


######################################################################
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@


splitted = windows_dataset.split('session')
train_set = splitted['session_T']
eval_set = splitted['session_E']
train_set = splitted['0train'] # Session train
eval_set = splitted['1test'] # Session evaluation

######################################################################
# Defining a list of transforms
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
},
"outputs": [],
"source": [
"splitted = windows_dataset.split('session')\ntrain_set = splitted['session_T']\nvalid_set = splitted['session_E']"
"splitted = windows_dataset.split('session')\ntrain_set = splitted['0train'] # Session train\nvalid_set = splitted['1test'] # Session evaluation"
]
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@
},
"outputs": [],
"source": [
"splitted = windows_dataset.split('session')\ntrain_set = splitted['session_T']\neval_set = splitted['session_E']"
"splitted = windows_dataset.split('session')\ntrain_set = splitted['0train'] # Session train\neval_set = splitted['1test'] # Session evaluation"
]
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"We can easily split the dataset BCIC IV 2a dataset using additional\ninfo stored in the description attribute, in this case the ``session``\ncolumn. We select ``session_T`` for training and ``session_E`` for testing.\nFor other datasets, you might have to choose another column and/or column.\n\n<div class=\"alert alert-info\"><h4>Note</h4><p>No matter which of the three schemes you use, this initial\n two-fold split into train_set and test_set always remains the same.\n Remember that you are not allowed to use the test_set during any\n stage of training or tuning.</p></div>\n\n\n"
"We can easily split the dataset BCIC IV 2a dataset using additional\ninfo stored in the description attribute, in this case the ``session``\ncolumn. We select ``0train`` for training and ``0test`` for testing.\nFor other datasets, you might have to choose another column and/or column.\n\n<div class=\"alert alert-info\"><h4>Note</h4><p>No matter which of the three schemes you use, this initial\n two-fold split into train_set and test_set always remains the same.\n Remember that you are not allowed to use the test_set during any\n stage of training or tuning.</p></div>\n\n\n"
]
},
{
Expand All @@ -104,7 +104,7 @@
},
"outputs": [],
"source": [
"splitted = windows_dataset.split(\"session\")\ntrain_set = splitted[\"session_T\"]\ntest_set = splitted[\"session_E\"]"
"splitted = windows_dataset.split(\"session\")\ntrain_set = splitted['0train'] # Session train\ntest_set = splitted['1test'] # Session evaluation"
]
},
{
Expand Down Expand Up @@ -233,7 +233,7 @@
},
"outputs": [],
"source": [
"def plot_train_valid_test(ax, all_dataset, train_subset, val_subset, test_set):\n \"\"\"Create a sample plot for training, validation, testing.\"\"\"\n\n bd_cmap = [\"#3A6190\", \"#683E00\", \"#2196F3\", \"#DDF2FF\",]\n\n n_train, n_val, n_test = len(train_subset), len(val_subset), len(test_set)\n ax.barh(\"Original\\ndataset\", len(all_dataset), left=0, height=0.5, color=bd_cmap[0])\n ax.barh(\"Train-Test-Valid\\nsplit\", n_train, left=0, height=0.5, color=bd_cmap[1])\n ax.barh(\"Train-Test-Valid\\nsplit\", n_val, left=n_train, height=0.5, color=bd_cmap[2])\n ax.barh(\"Train-Test-Valid\\nsplit\", n_test, left=n_train + n_val, height=0.5, color=bd_cmap[3])\n\n ax.invert_yaxis()\n ax.set(xlabel=\"Number of samples.\", title=\"Train-Test-Valid split\")\n ax.legend([\"Original set\", \"Training set\", \"Validation set\", \"Testing set\"],\n loc=\"lower center\", ncols=2, bbox_to_anchor=(0.5, 0.4))\n ax.set_xlim([-int(0.1 * len(all_dataset)), int(1.1 * len(all_dataset))])\n return ax\n\n\nfig, ax = plt.subplots(figsize=(12, 5))\nplot_train_valid_test(ax=ax, all_dataset=windows_dataset,\n train_subset=train_subset, val_subset=val_subset, test_set=test_set,)"
"def plot_train_valid_test(ax, all_dataset, train_subset, val_subset, test_set):\n \"\"\"Create a sample plot for training, validation, testing.\"\"\"\n\n bd_cmap = [\"#3A6190\", \"#683E00\", \"#2196F3\", \"#DDF2FF\", ]\n\n n_train, n_val, n_test = len(train_subset), len(val_subset), len(test_set)\n ax.barh(\"Original\\ndataset\", len(all_dataset), left=0, height=0.5, color=bd_cmap[0])\n ax.barh(\"Train-Test-Valid\\nsplit\", n_train, left=0, height=0.5, color=bd_cmap[1])\n ax.barh(\"Train-Test-Valid\\nsplit\", n_val, left=n_train, height=0.5, color=bd_cmap[2])\n ax.barh(\"Train-Test-Valid\\nsplit\", n_test, left=n_train + n_val, height=0.5, color=bd_cmap[3])\n\n ax.invert_yaxis()\n ax.set(xlabel=\"Number of samples.\", title=\"Train-Test-Valid split\")\n ax.legend([\"Original set\", \"Training set\", \"Validation set\", \"Testing set\"],\n loc=\"lower center\", ncols=2, bbox_to_anchor=(0.5, 0.4))\n ax.set_xlim([-int(0.1 * len(all_dataset)), int(1.1 * len(all_dataset))])\n return ax\n\n\nfig, ax = plt.subplots(figsize=(12, 5))\nplot_train_valid_test(ax=ax, all_dataset=windows_dataset,\n train_subset=train_subset, val_subset=val_subset, test_set=test_set,)"
]
},
{
Expand Down Expand Up @@ -276,7 +276,7 @@
},
"outputs": [],
"source": [
"def plot_k_fold(ax, cv, all_dataset, X_train, y_train, test_set):\n \"\"\"Create a sample plot for training, validation, testing.\"\"\"\n\n bd_cmap = [\"#3A6190\", \"#683E00\", \"#2196F3\", \"#DDF2FF\",]\n\n ax.barh(\"Original\\nDataset\", len(all_dataset), left=0, height=0.5, color=bd_cmap[0])\n\n # Generate the training/validation/testing data fraction visualizations for each CV split\n for ii, (tr_idx, val_idx) in enumerate(cv.split(X=X_train, y=y_train)):\n n_train, n_val, n_test = len(tr_idx), len(val_idx), len(test_set)\n n_train2 = n_train + n_val - max(val_idx) - 1\n ax.barh(\"cv\" + str(ii + 1), min(val_idx), left=0, height=0.5, color=bd_cmap[1])\n ax.barh(\"cv\" + str(ii + 1), n_val, left=min(val_idx), height=0.5, color=bd_cmap[2])\n ax.barh(\"cv\" + str(ii + 1), n_train2, left=max(val_idx) + 1, height=0.5, color=bd_cmap[1])\n ax.barh(\"cv\" + str(ii + 1), n_test, left=n_train + n_val, height=0.5, color=bd_cmap[3])\n\n ax.invert_yaxis()\n ax.set_xlim([-int(0.1 * len(all_dataset)), int(1.1 * len(all_dataset))])\n ax.set(xlabel=\"Number of samples.\", title=\"KFold Train-Test-Valid split\")\n ax.legend([Patch(color=bd_cmap[i]) for i in range(4)],\n [\"Original set\", \"Training set\", \"Validation set\", \"Testing set\"],\n loc=\"lower center\", ncols=2)\n ax.text(-0.07, 0.45, 'Train-Valid-Test split', rotation=90,\n verticalalignment='center', horizontalalignment='left', transform=ax.transAxes)\n return ax\n\n\nfig, ax = plt.subplots(figsize=(15, 7))\nplot_k_fold(ax, cv=train_val_split, all_dataset=windows_dataset,\n X_train=X_train, y_train=y_train, test_set=test_set,)"
"def plot_k_fold(ax, cv, all_dataset, X_train, y_train, test_set):\n \"\"\"Create a sample plot for training, validation, testing.\"\"\"\n\n bd_cmap = [\"#3A6190\", \"#683E00\", \"#2196F3\", \"#DDF2FF\", ]\n\n ax.barh(\"Original\\nDataset\", len(all_dataset), left=0, height=0.5, color=bd_cmap[0])\n\n # Generate the training/validation/testing data fraction visualizations for each CV split\n for ii, (tr_idx, val_idx) in enumerate(cv.split(X=X_train, y=y_train)):\n n_train, n_val, n_test = len(tr_idx), len(val_idx), len(test_set)\n n_train2 = n_train + n_val - max(val_idx) - 1\n ax.barh(\"cv\" + str(ii + 1), min(val_idx), left=0, height=0.5, color=bd_cmap[1])\n ax.barh(\"cv\" + str(ii + 1), n_val, left=min(val_idx), height=0.5, color=bd_cmap[2])\n ax.barh(\"cv\" + str(ii + 1), n_train2, left=max(val_idx) + 1, height=0.5, color=bd_cmap[1])\n ax.barh(\"cv\" + str(ii + 1), n_test, left=n_train + n_val, height=0.5, color=bd_cmap[3])\n\n ax.invert_yaxis()\n ax.set_xlim([-int(0.1 * len(all_dataset)), int(1.1 * len(all_dataset))])\n ax.set(xlabel=\"Number of samples.\", title=\"KFold Train-Test-Valid split\")\n ax.legend([Patch(color=bd_cmap[i]) for i in range(4)],\n [\"Original set\", \"Training set\", \"Validation set\", \"Testing set\"],\n loc=\"lower center\", ncols=2)\n ax.text(-0.07, 0.45, 'Train-Valid-Test split', rotation=90,\n verticalalignment='center', horizontalalignment='left', transform=ax.transAxes)\n return ax\n\n\nfig, ax = plt.subplots(figsize=(15, 7))\nplot_k_fold(ax, cv=train_val_split, all_dataset=windows_dataset,\n X_train=X_train, y_train=y_train, test_set=test_set,)"
]
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
},
"outputs": [],
"source": [
"from braindecode.datasets import MOABBDataset\n\nsubject_id = 3\ndataset = MOABBDataset(dataset_name=\"BNCI2014001\", subject_ids=[subject_id])\n\nfrom numpy import multiply\n\nfrom braindecode.preprocessing import (\n Preprocessor,\n exponential_moving_standardize,\n preprocess,\n)\n\nlow_cut_hz = 4. # low cut frequency for filtering\nhigh_cut_hz = 38. # high cut frequency for filtering\n# Parameters for exponential moving standardization\nfactor_new = 1e-3\ninit_block_size = 1000\n# Factor to convert from V to uV\nfactor = 1e6\n\npreprocessors = [\n Preprocessor('pick_types', eeg=True, meg=False, stim=False), # Keep EEG sensors\n Preprocessor(lambda data: multiply(data, factor)), # Convert from V to uV\n Preprocessor('filter', l_freq=low_cut_hz, h_freq=high_cut_hz), # Bandpass filter\n Preprocessor(exponential_moving_standardize, # Exponential moving standardization\n factor_new=factor_new, init_block_size=init_block_size)\n]\n\n# Transform the data\npreprocess(dataset, preprocessors, n_jobs=-1)"
"from braindecode.datasets import MOABBDataset\n\nsubject_id = 3\ndataset = MOABBDataset(dataset_name=\"BNCI2014_001\", subject_ids=[subject_id])\n\nfrom numpy import multiply\n\nfrom braindecode.preprocessing import (\n Preprocessor,\n exponential_moving_standardize,\n preprocess,\n)\n\nlow_cut_hz = 4. # low cut frequency for filtering\nhigh_cut_hz = 38. # high cut frequency for filtering\n# Parameters for exponential moving standardization\nfactor_new = 1e-3\ninit_block_size = 1000\n# Factor to convert from V to uV\nfactor = 1e6\n\npreprocessors = [\n Preprocessor('pick_types', eeg=True, meg=False, stim=False),\n # Keep EEG sensors\n Preprocessor(lambda data: multiply(data, factor)), # Convert from V to uV\n Preprocessor('filter', l_freq=low_cut_hz, h_freq=high_cut_hz),\n # Bandpass filter\n Preprocessor(exponential_moving_standardize,\n # Exponential moving standardization\n factor_new=factor_new, init_block_size=init_block_size)\n]\n\n# Transform the data\npreprocess(dataset, preprocessors, n_jobs=-1)"
]
},
{
Expand Down Expand Up @@ -137,7 +137,7 @@
},
"outputs": [],
"source": [
"splitted = windows_dataset.split('session')\ntrain_set = splitted['session_T']\nvalid_set = splitted['session_E']"
"splitted = windows_dataset.split('session')\ntrain_set = splitted['0train'] # Session train\nvalid_set = splitted['1test'] # Session evaluation"
]
},
{
Expand All @@ -155,7 +155,7 @@
},
"outputs": [],
"source": [
"from skorch.callbacks import LRScheduler\nfrom skorch.helper import predefined_split\n\nfrom braindecode import EEGClassifier\nfrom braindecode.training import CroppedLoss\n\n# These values we found good for shallow network:\nlr = 0.0625 * 0.01\nweight_decay = 0\n\n# For deep4 they should be:\n# lr = 1 * 0.01\n# weight_decay = 0.5 * 0.001\n\nbatch_size = 64\nn_epochs = 2\n\nclf = EEGClassifier(\n model,\n cropped=True,\n criterion=CroppedLoss,\n criterion__loss_function=torch.nn.functional.nll_loss,\n optimizer=torch.optim.AdamW,\n train_split=predefined_split(valid_set),\n optimizer__lr=lr,\n optimizer__weight_decay=weight_decay,\n iterator_train__shuffle=True,\n batch_size=batch_size,\n callbacks=[\n \"accuracy\", (\"lr_scheduler\", LRScheduler('CosineAnnealingLR', T_max=n_epochs - 1)),\n ],\n device=device,\n classes=classes,\n)\n# Model training for a specified number of epochs. `y` is None as it is already supplied\n# in the dataset.\n_ = clf.fit(train_set, y=None, epochs=n_epochs)"
"from skorch.callbacks import LRScheduler\nfrom skorch.helper import predefined_split\n\nfrom braindecode import EEGClassifier\nfrom braindecode.training import CroppedLoss\n\n# These values we found good for shallow network:\nlr = 0.0625 * 0.01\nweight_decay = 0\n\n# For deep4 they should be:\n# lr = 1 * 0.01\n# weight_decay = 0.5 * 0.001\n\nbatch_size = 64\nn_epochs = 2\n\nclf = EEGClassifier(\n model,\n cropped=True,\n criterion=CroppedLoss,\n criterion__loss_function=torch.nn.functional.nll_loss,\n optimizer=torch.optim.AdamW,\n train_split=predefined_split(valid_set),\n optimizer__lr=lr,\n optimizer__weight_decay=weight_decay,\n iterator_train__shuffle=True,\n batch_size=batch_size,\n callbacks=[\n \"accuracy\",\n (\"lr_scheduler\", LRScheduler('CosineAnnealingLR', T_max=n_epochs - 1)),\n ],\n device=device,\n classes=classes,\n)\n# Model training for a specified number of epochs. `y` is None as it is already supplied\n# in the dataset.\n_ = clf.fit(train_set, y=None, epochs=n_epochs)"
]
},
{
Expand All @@ -173,7 +173,7 @@
},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\nimport pandas as pd\nfrom matplotlib.lines import Line2D\n\n# Extract loss and accuracy values for plotting from history object\nresults_columns = ['train_loss', 'valid_loss', 'train_accuracy', 'valid_accuracy']\ndf = pd.DataFrame(clf.history[:, results_columns], columns=results_columns,\n index=clf.history[:, 'epoch'])\n\n# get percent of misclass for better visual comparison to loss\ndf = df.assign(train_misclass=100 - 100 * df.train_accuracy,\n valid_misclass=100 - 100 * df.valid_accuracy)\n\nfig, ax1 = plt.subplots(figsize=(8, 3))\ndf.loc[:, ['train_loss', 'valid_loss']].plot(\n ax=ax1, style=['-', ':'], marker='o', color='tab:blue', legend=False, fontsize=14)\n\nax1.tick_params(axis='y', labelcolor='tab:blue', labelsize=14)\nax1.set_ylabel(\"Loss\", color='tab:blue', fontsize=14)\n\nax2 = ax1.twinx() # instantiate a second axes that shares the same x-axis\n\ndf.loc[:, ['train_misclass', 'valid_misclass']].plot(\n ax=ax2, style=['-', ':'], marker='o', color='tab:red', legend=False)\nax2.tick_params(axis='y', labelcolor='tab:red', labelsize=14)\nax2.set_ylabel(\"Misclassification Rate [%]\", color='tab:red', fontsize=14)\nax2.set_ylim(ax2.get_ylim()[0], 85) # make some room for legend\nax1.set_xlabel(\"Epoch\", fontsize=14)\n\n# where some data has already been plotted to ax\nhandles = []\nhandles.append(Line2D([0], [0], color='black', linewidth=1, linestyle='-', label='Train'))\nhandles.append(Line2D([0], [0], color='black', linewidth=1, linestyle=':', label='Valid'))\nplt.legend(handles, [h.get_label() for h in handles], fontsize=14)\nplt.tight_layout()"
"import matplotlib.pyplot as plt\nimport pandas as pd\nfrom matplotlib.lines import Line2D\n\n# Extract loss and accuracy values for plotting from history object\nresults_columns = ['train_loss', 'valid_loss', 'train_accuracy',\n 'valid_accuracy']\ndf = pd.DataFrame(clf.history[:, results_columns], columns=results_columns,\n index=clf.history[:, 'epoch'])\n\n# get percent of misclass for better visual comparison to loss\ndf = df.assign(train_misclass=100 - 100 * df.train_accuracy,\n valid_misclass=100 - 100 * df.valid_accuracy)\n\nfig, ax1 = plt.subplots(figsize=(8, 3))\ndf.loc[:, ['train_loss', 'valid_loss']].plot(\n ax=ax1, style=['-', ':'], marker='o', color='tab:blue', legend=False,\n fontsize=14)\n\nax1.tick_params(axis='y', labelcolor='tab:blue', labelsize=14)\nax1.set_ylabel(\"Loss\", color='tab:blue', fontsize=14)\n\nax2 = ax1.twinx() # instantiate a second axes that shares the same x-axis\n\ndf.loc[:, ['train_misclass', 'valid_misclass']].plot(\n ax=ax2, style=['-', ':'], marker='o', color='tab:red', legend=False)\nax2.tick_params(axis='y', labelcolor='tab:red', labelsize=14)\nax2.set_ylabel(\"Misclassification Rate [%]\", color='tab:red', fontsize=14)\nax2.set_ylim(ax2.get_ylim()[0], 85) # make some room for legend\nax1.set_xlabel(\"Epoch\", fontsize=14)\n\n# where some data has already been plotted to ax\nhandles = []\nhandles.append(\n Line2D([0], [0], color='black', linewidth=1, linestyle='-', label='Train'))\nhandles.append(\n Line2D([0], [0], color='black', linewidth=1, linestyle=':', label='Valid'))\nplt.legend(handles, [h.get_label() for h in handles], fontsize=14)\nplt.tight_layout()"
]
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,12 +172,12 @@
######################################################################
# We can easily split the dataset using additional info stored in the
# description attribute, in this case ``session`` column. We select
# ``session_T`` for training and ``session_E`` for evaluation.
# ``0train`` for training and ``1test`` for evaluation.
#

splitted = windows_dataset.split('session')
train_set = splitted['session_T']
eval_set = splitted['session_E']
train_set = splitted['0train'] # Session train
eval_set = splitted['1test'] # Session evaluation

######################################################################
# Create model
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
from braindecode.datasets import MOABBDataset

subject_id = 3
dataset = MOABBDataset(dataset_name="BNCI2014001", subject_ids=[subject_id])
dataset = MOABBDataset(dataset_name="BNCI2014_001", subject_ids=[subject_id])

######################################################################
# Preprocessing, the offline transformation of the raw dataset
Expand Down Expand Up @@ -185,7 +185,7 @@
######################################################################
# We can easily split the dataset using additional info stored in the
# description attribute, in this case the ``session`` column. We
# select ``session_T`` for training and ``session_E`` for testing.
# select ``Train`` for training and ``test`` for testing.
# For other datasets, you might have to choose another column.
#
# .. note::
Expand All @@ -196,8 +196,8 @@
#

splitted = windows_dataset.split("session")
train_set = splitted["session_T"]
test_set = splitted["session_E"]
train_set = splitted['0train'] # Session train
test_set = splitted['1test'] # Session evaluation

######################################################################
# Option 1: Pure PyTorch training loop
Expand Down
Binary file not shown.
Loading

0 comments on commit 07125b6

Please sign in to comment.