Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Beanchmark grid search #323

Merged
merged 35 commits into from
Jan 26, 2023
Merged
Show file tree
Hide file tree
Changes from 31 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
edeee9c
Grid Search on Evaluation
carraraig Jan 10, 2023
9c41acf
whats_new changed
carraraig Jan 10, 2023
e38c992
GridSearchCV handle with None value
carraraig Jan 10, 2023
fad9c42
Updated Test
carraraig Jan 10, 2023
2a6b6b9
Unchanged Learning Curve
carraraig Jan 11, 2023
626a504
Example GridSearch WithinSession
carraraig Jan 11, 2023
ea60a5e
Update moabb/evaluations/base.py
bruAristimunha Jan 16, 2023
954c51b
Trying
bruAristimunha Jan 16, 2023
f0c0b65
Rename and changing the path
bruAristimunha Jan 16, 2023
9c647a2
Updating the README.txt
bruAristimunha Jan 16, 2023
80ef758
Adding the header
bruAristimunha Jan 16, 2023
144c8fa
Applying str in the var self.hdf5_path
bruAristimunha Jan 16, 2023
ef8a16b
Example GridSearch WithinSession with plot and parameter displayed
carraraig Jan 17, 2023
d10e749
Evaluation with modification
carraraig Jan 17, 2023
8aa1ce8
Merge branch 'develop' into grid_search
carraraig Jan 17, 2023
4c4cc2c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jan 17, 2023
abec2c1
Evaluation with modification
carraraig Jan 17, 2023
d43ad74
Merge remote-tracking branch 'origin/grid_search' into grid_search
carraraig Jan 17, 2023
85fd691
Example Fix
carraraig Jan 23, 2023
ec0686d
Example Fix
carraraig Jan 23, 2023
a9e9b04
update example
Jan 23, 2023
0b3e64f
Evaluation fix sefl.hdf5 path
carraraig Jan 23, 2023
5db79cb
Merge remote-tracking branch 'origin/grid_search' into grid_search
carraraig Jan 23, 2023
820a6a0
Evaluation with function for GridSearch
carraraig Jan 24, 2023
7a30bda
Benchmark with gridsearch + Example
carraraig Jan 24, 2023
aa657df
Merge branch 'develop' into beanchmark_grid_search
bruAristimunha Jan 24, 2023
afef484
Merge branch 'develop' into beanchmark_grid_search
sylvchev Jan 25, 2023
c1c4724
Pipelines where added the param_grid parameter
carraraig Jan 25, 2023
990c5e2
Pipelines where added the param_grid parameter
carraraig Jan 25, 2023
721a74f
Pipelines where added the param_grid parameter
carraraig Jan 25, 2023
1120de6
Pipelines where added the param_grid parameter
carraraig Jan 25, 2023
5d33640
Remove param_grid: None
carraraig Jan 26, 2023
6b3d0f5
Remove param_grid: None
carraraig Jan 26, 2023
5b141d1
Remove param_grid: None
carraraig Jan 26, 2023
7b60980
code reformatting
sylvchev Jan 26, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions examples/pipelines_grid/CSP_grid.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: CSP + LDA
paradigms:
- LeftRightImagery

citations:
- https://doi.org/10.1007/BF01129656
- https://doi.org/10.1109/MSP.2008.4408441

pipeline:
- name: Covariances
from: pyriemann.estimation
parameters:
estimator: oas

- name: CSP
from: pyriemann.spatialfilters
parameters:
nfilter: 6

- name: LinearDiscriminantAnalysis
from: sklearn.discriminant_analysis
parameters:
solver: svd

param_grid:
csp__nfilter:
- 4
- 5
- 6
27 changes: 27 additions & 0 deletions examples/pipelines_grid/EN.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: EN
paradigms:
- LeftRightImagery

citations:

carraraig marked this conversation as resolved.
Show resolved Hide resolved
pipeline:
- name: Covariances
from: pyriemann.estimation
parameters:
estimator: oas

- name: TangentSpace
from: pyriemann.tangentspace
parameters:
metric: "riemann"

- name: LogisticRegression
from: sklearn.linear_model
parameters:
penalty: "elasticnet"
l1_ratio: 0.15
intercept_scaling: 1000.0
solver: "saga"
max_iter: 1000

param_grid: None
33 changes: 33 additions & 0 deletions examples/pipelines_grid/EN_grid.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: EN Grid
paradigms:
- LeftRightImagery

citations:
carraraig marked this conversation as resolved.
Show resolved Hide resolved

pipeline:
- name: Covariances
from: pyriemann.estimation
parameters:
estimator: oas

- name: TangentSpace
from: pyriemann.tangentspace
parameters:
metric: "riemann"

- name: LogisticRegression
from: sklearn.linear_model
parameters:
penalty: "elasticnet"
l1_ratio: 0.70
intercept_scaling: 1000.0
solver: "saga"
max_iter: 1000

param_grid:
logisticregression__l1_ratio:
- 0.20
- 0.30
- 0.45
- 0.65
- 0.75
67 changes: 67 additions & 0 deletions examples/plot_benchmark_grid_search.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
"""
=======================
Benchmarking with MOABB with Grid Search
=======================
carraraig marked this conversation as resolved.
Show resolved Hide resolved

This example shows how to use MOABB to benchmark a set of pipelines
on all available datasets. In particular we run the Gridsearch to select the best hyperparameter of some pipelines
and save the gridsearch.
For this example, we will use only one dataset to keep the computation time low, but this benchmark is designed
to easily scale to many datasets.
"""
# Authors: Igor Carrara <[email protected]>
#
# License: BSD (3-clause)

import matplotlib.pyplot as plt

from moabb import benchmark, set_log_level
from moabb.analysis.plotting import score_plot


set_log_level("info")


###############################################################################
# In this example, we will use only the dataset 'Zhou 2016'.
#
# Running the benchmark
# ---------------------
#
# The benchmark is run using the ``benchmark`` function. You need to specify the
# folder containing the pipelines to use, the kind of evaluation and the paradigm
# to use. By default, the benchmark will use all available datasets for all
# paradigms listed in the pipelines. You could restrict to specific evaluation and
# paradigm using the ``evaluations`` and ``paradigms`` arguments.
#
# To save computation time, the results are cached. If you want to re-run the
# benchmark, you can set the ``overwrite`` argument to ``True``.
#
# It is possible to indicate the folder to cache the results and the one to save
# the analysis & figures. By default, the results are saved in the ``results``
# folder, and the analysis & figures are saved in the ``benchmark`` folder.

# In the results folder we will save the gridsearch evaluation
# When write the pipeline in ylm file we need to specify the parameter that we want to test, in format
# name__name_parameter. Note that name must be NOT in capital letter.
carraraig marked this conversation as resolved.
Show resolved Hide resolved
# If the grid search is already implemented it will load the previous results

results = benchmark(
# pipelines="../pipelines_grid/",
carraraig marked this conversation as resolved.
Show resolved Hide resolved
pipelines="./pipelines_grid/",
evaluations=["WithinSession"],
paradigms=["LeftRightImagery"],
include_datasets=["Zhou 2016"],
results="./results/",
overwrite=False,
plot=False,
output="./benchmark/",
)

###############################################################################
# Benchmark prints a summary of the results. Detailed results are saved in a
# pandas dataframe, and can be used to generate figures. The analysis & figures
# are saved in the ``benchmark`` folder.

score_plot(results)
plt.show()
2 changes: 2 additions & 0 deletions examples/sample_pipelines/CSP_SVM.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,5 @@ pipeline:
from: sklearn.svm
parameters:
kernel: "linear"

param_grid: None
2 changes: 2 additions & 0 deletions examples/sample_pipelines/TSLR.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,5 @@ pipeline:
from: sklearn.linear_model
parameters:
C: 1.0

param_grid: None
12 changes: 10 additions & 2 deletions moabb/benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@
CrossSubjectEvaluation,
WithinSessionEvaluation,
)
from moabb.pipelines.utils import generate_paradigms, parse_pipelines_from_directory
from moabb.pipelines.utils import (
generate_paradigms,
generate_param_grid,
parse_pipelines_from_directory,
)


log = logging.getLogger(__name__)
Expand Down Expand Up @@ -116,6 +120,8 @@ def benchmark(
if paradigms is not None:
prdgms = {p: prdgms[p] for p in paradigms}

param_grid = generate_param_grid(pipeline_configs, context_params, log)

log.debug(f"The paradigms being run are {prdgms.keys()}")

if len(context_params) == 0:
Expand Down Expand Up @@ -145,7 +151,9 @@ def benchmark(
n_jobs=n_jobs,
overwrite=overwrite,
)
paradigm_results = context.process(pipelines=prdgms[paradigm])
paradigm_results = context.process(
pipelines=prdgms[paradigm], param_grid=param_grid
)
paradigm_results["paradigm"] = f"{paradigm}"
paradigm_results["evaluation"] = f"{evaluation}"
eval_results[f"{paradigm}"] = paradigm_results
Expand Down
43 changes: 36 additions & 7 deletions moabb/pipelines/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,26 @@ def parse_pipelines_from_directory(dir_path):
# load config
config_dict = yaml.load(content, Loader=yaml.FullLoader)
ppl = create_pipeline_from_config(config_dict["pipeline"])
pipeline_configs.append(
{
"paradigms": config_dict["paradigms"],
"pipeline": ppl,
"name": config_dict["name"],
}
)
if config_dict["param_grid"] is not None:
carraraig marked this conversation as resolved.
Show resolved Hide resolved
pipeline_configs.append(
{
"paradigms": config_dict["paradigms"],
"pipeline": ppl,
"name": config_dict["name"],
"param_grid": config_dict["param_grid"],
}
)
else:
pipeline_configs.append(
{
"paradigms": config_dict["paradigms"],
"pipeline": ppl,
"name": config_dict["name"],
}
)

# we can do the same for python defined pipeline
# TODO for python pipelines
python_files = glob(os.path.join(dir_path, "*.py"))

for python_file in python_files:
Expand Down Expand Up @@ -162,6 +173,24 @@ def generate_paradigms(pipeline_configs, context=None, logger=log):
return paradigms


def generate_param_grid(pipeline_configs, context=None, logger=log):

context = context or {}
param_grid = {}
for config in pipeline_configs:

if "paradigms" not in config.keys():
sylvchev marked this conversation as resolved.
Show resolved Hide resolved
logger.error("{} must have a 'paradigms' key.".format(config))
continue

# iterate over paradigms
if config["param_grid"] != "None":
param_grid[config["name"]] = config["param_grid"]
# param_grid[config["name"]] = config["param_grid"].get(config["name"])
sylvchev marked this conversation as resolved.
Show resolved Hide resolved

return param_grid


class FilterBank(BaseEstimator, TransformerMixin):
"""Apply a given indentical pipeline over a bank of filter.

Expand Down
2 changes: 2 additions & 0 deletions moabb/tests/test_pipelines/CSP.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ pipeline:
from: sklearn.discriminant_analysis
parameters:
solver: svd

param_grid: None
carraraig marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 2 additions & 0 deletions moabb/tests/test_pipelines/LogVar.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ pipeline:
from: sklearn.discriminant_analysis
parameters:
solver: svd

param_grid: None
2 changes: 2 additions & 0 deletions moabb/tests/test_pipelines/SSVEP_CCA.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ pipeline:
n_harmonics: 3
interval: [1, 3]
freqs: { "13": 0, "17": 1 }

param_grid: None
2 changes: 2 additions & 0 deletions pipelines/CCA-SSVEP.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ pipeline:
n_harmonics: 3
interval: [2, 4]
freqs: { "13": 2, "17": 3, "21": 4 }

param_grid: None
2 changes: 2 additions & 0 deletions pipelines/CSP.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,5 @@ pipeline:
from: sklearn.discriminant_analysis
parameters:
solver: svd

param_grid: None
2 changes: 2 additions & 0 deletions pipelines/CSP_SVM.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,5 @@ pipeline:
from: sklearn.svm
parameters:
kernel: "linear"

param_grid: None
2 changes: 2 additions & 0 deletions pipelines/LogVar.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ pipeline:
from: sklearn.discriminant_analysis
parameters:
solver: svd

param_grid: None
2 changes: 2 additions & 0 deletions pipelines/LogVarSVM.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ pipeline:
from: sklearn.svm
parameters:
kernel: "linear"

param_grid: None
2 changes: 2 additions & 0 deletions pipelines/MDM-SSVEP.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@ pipeline:
from: pyriemann.classification
parameters:
metric: "riemann"

param_grid: None
2 changes: 2 additions & 0 deletions pipelines/MDM.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ pipeline:
from: pyriemann.classification
parameters:
metric: "riemann"

param_grid: None
2 changes: 2 additions & 0 deletions pipelines/TSLR-SSVEP.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,5 @@ pipeline:
from: sklearn.linear_model
parameters:
C: 1.0

param_grid: None
2 changes: 2 additions & 0 deletions pipelines/TSLR.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,5 @@ pipeline:
from: sklearn.linear_model
parameters:
C: 1.0

param_grid: None
2 changes: 2 additions & 0 deletions pipelines/TSSVM-SSVEP.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,5 @@ pipeline:
from: sklearn.svm
parameters:
kernel: "linear"

param_grid: None
2 changes: 2 additions & 0 deletions pipelines/TSSVM.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,5 @@ pipeline:
from: sklearn.svm
parameters:
kernel: "linear"

param_grid: None
2 changes: 2 additions & 0 deletions pipelines/regCSP+shLDA.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@ pipeline:
parameters:
solver: lsqr
shrinkage: auto

param_grid: None