From de8fa2bebdc4c39574236140035bc7977c8f58d0 Mon Sep 17 00:00:00 2001 From: Mohammad Farzan Date: Thu, 11 Feb 2021 01:16:02 +0330 Subject: [PATCH] Fixed output size issue I hadn't implemented the part from Paper #1 Section III.A.6 corre- ctly which caused wrong output size (8 instead of 32). --- moabb/pipelines/fbcsp_select.py | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/moabb/pipelines/fbcsp_select.py b/moabb/pipelines/fbcsp_select.py index db3c1fda8..e7f06cefe 100644 --- a/moabb/pipelines/fbcsp_select.py +++ b/moabb/pipelines/fbcsp_select.py @@ -1,5 +1,6 @@ from mne.decoding import CSP import numpy as np +import pandas as pd from scipy.signal import iirfilter, sosfilt from sklearn.base import BaseEstimator, TransformerMixin from sklearn.feature_selection import SelectKBest, mutual_info_classif @@ -43,6 +44,8 @@ def fit(self, X, y): n_epochs, n_channels, n_signalsamples = X.shape n_csp = 2 * self.Nw n_features = 2 * self.Ns + classes = pd.unique(y) + n_classes = len(classes) # Apply filter bank filtered_signals = np.zeros((n_filters, n_epochs, n_channels, n_signalsamples)) @@ -65,9 +68,22 @@ def fit(self, X, y): # Feature selection feature_selector = SelectKBest(mutual_info_classif, k=n_features) - feature_selector.fit(csp_powers, y) - selected_features_indices = feature_selector.get_support(indices=True) - + selected_features_indices = [] + for i in range(n_classes): + y_masked = np.where(y == classes[i], True, False) # one-vs-other approach, explained in III.A.6 + feature_selector.fit(csp_powers, y_masked) + selected_features_indices += list( feature_selector.get_support(indices=True) ) + + # As feature selection is done separately for each class, + # a feature may appear multiple times in the list. Here we + # replace the repeated features in the list with other + # features which are selected using a global (non-class-specific) + # feature selector. + global_feature_selector = SelectKBest(mutual_info_classif, k=(n_classes * n_features)) + global_feature_selector.fit(csp_powers, y) + selected_features_indices += list( global_feature_selector.get_support(indices=True) ) + selected_features_indices = list(pd.unique(selected_features_indices))[:n_classes * n_features] + # Save pipeline self.csp_transformers = csp_transformers self.selected_features_indices = selected_features_indices