Exemple #1
0

# Read epochs (train will be done only between 1 and 2s)
# Testing will be done with a running classifier
epochs = Epochs(raw, events, event_id, tmin, tmax, proj=True, picks=picks,
                baseline=None, preload=True, add_eeg_ref=False,verbose = False)
labels = epochs.events[:, -1] - 2


# cross validation
cv = KFold(len(labels),10, shuffle=True, random_state=42)
# get epochs
epochs_data_train = epochs.get_data()

# compute covariance matrices
cov_data_train = covariances(epochs_data_train)

###############################################################################
# Classification with Minimum distance to mean
mdm = MDM()

# Use scikit-learn Pipeline with cross_val_score function
scores = cross_val_score(mdm, cov_data_train, labels, cv=cv, n_jobs=1)

# Printing the results
class_balance = np.mean(labels == labels[0])
class_balance = max(class_balance, 1. - class_balance)
print("MDM Classification accuracy:       %f / Chance level: %f" % (np.mean(scores),
                                                          class_balance))                                                    

###############################################################################
    def score(self, X, y):
        """
		:param X:
		:param y:
		:return:
		"""
        X = check_array(X, allow_nd=True)

        scores = np.zeros(self.num_votes)
        scores_ts = np.zeros(self.num_votes)
        scores_mdm = np.zeros(self.num_votes)

        for i in xrange(self.num_votes):
            print "----------------------------------------------"
            print "predicting with classifier", i + 1
            print self.ranked_scores_opts[i]

            bandpass_start, bandpass_end = self.ranked_scores_opts[i][
                'bandpass']
            epoch_trim_start, epoch_trim_end = self.ranked_scores_opts[i][
                'epoch_trim']
            # bandpass filter coefficients
            b, a = butter(
                5,
                np.array([bandpass_start, bandpass_end]) / (self.sfreq * 0.5),
                'bandpass')

            # filter and crop X
            X_predict = self.preprocess_X(X, b, a, epoch_trim_start,
                                          epoch_trim_end)

            # use CSP spatial filters to transform (extract features)
            classification_features = self.ranked_transformers[i].transform(
                X_predict)
            scores[i] = self.ranked_classifiers[i].score(
                classification_features, y)

            print "score CSP+LDA: ", scores[i]

            # Tangent Space stuff
            bandpass_start, bandpass_end = self.ranked_scores_opts_ts[i][
                'bandpass']
            epoch_trim_start, epoch_trim_end = self.ranked_scores_opts_ts[i][
                'epoch_trim']
            # bandpass filter coefficients
            b, a = butter(
                5,
                np.array([bandpass_start, bandpass_end]) / (self.sfreq * 0.5),
                'bandpass')
            # filter and crop X
            X_predict = self.preprocess_X(X, b, a, epoch_trim_start,
                                          epoch_trim_end)
            print "opts:"
            print b, a, epoch_trim_start, epoch_trim_end

            # compute covariance matrices
            cov_data = covariances(X=X_predict)
            scores_ts[i] = self.ranked_classifiers_ts[i].score(cov_data, y)

            print "score TS+LR: ", scores_ts[i]

            # MDM stuff
            bandpass_start, bandpass_end = self.ranked_scores_opts_mdm[i][
                'bandpass']
            epoch_trim_start, epoch_trim_end = self.ranked_scores_opts_mdm[i][
                'epoch_trim']
            # bandpass filter coefficients
            b, a = butter(
                5,
                np.array([bandpass_start, bandpass_end]) / (self.sfreq * 0.5),
                'bandpass')
            # filter and crop X
            X_predict = self.preprocess_X(X, b, a, epoch_trim_start,
                                          epoch_trim_end)
            print "opts:"
            print b, a, epoch_trim_start, epoch_trim_end

            # compute covariance matrices
            cov_data = covariances(X=X_predict)
            scores_mdm[i] = self.ranked_classifiers_mdm[i].score(cov_data, y)

            print "score MDM: ", scores_ts[i]

        return np.average(scores)
    def fit(self, X, y):
        # validate
        X, y = check_X_y(X, y, allow_nd=True)
        X = check_array(X, allow_nd=True)

        # set internal vars
        self.classes_ = unique_labels(y)
        self.X_ = X
        self.y_ = y

        ##################################################
        # split X into train and test sets, so that
        # grid search can be performed on train set only
        seed = 7
        np.random.seed(seed)
        #X_TRAIN, X_TEST, y_TRAIN, y_TEST = train_test_split(X, y, test_size=0.25, random_state=seed)

        for epoch_trim in self.epoch_bounds:
            for bandpass in self.bandpass_filters:

                X_train, X_test, y_train, y_test = train_test_split(
                    X, y, test_size=0.25, random_state=seed)

                # X_train = np.copy(X_TRAIN)
                # X_test = np.copy(X_TEST)
                # y_train = np.copy(y_TRAIN)
                # y_test = np.copy(y_TEST)

                # separate out inputs that are tuples
                bandpass_start, bandpass_end = bandpass
                epoch_trim_start, epoch_trim_end = epoch_trim

                # bandpass filter coefficients
                b, a = butter(
                    5,
                    np.array([bandpass_start, bandpass_end]) /
                    (self.sfreq * 0.5), 'bandpass')

                # filter and crop TRAINING SET
                X_train = self.preprocess_X(X_train, b, a, epoch_trim_start,
                                            epoch_trim_end)
                # validate
                X_train, y_train = check_X_y(X_train, y_train, allow_nd=True)
                X_train = check_array(X_train, allow_nd=True)

                # filter and crop TEST SET
                X_test = self.preprocess_X(X_test, b, a, epoch_trim_start,
                                           epoch_trim_end)
                # validate
                X_test, y_test = check_X_y(X_test, y_test, allow_nd=True)
                X_test = check_array(X_test, allow_nd=True)

                ###########################################################################
                # self-tune CSP to find optimal number of filters to use at these settings
                #[best_num_filters, best_num_filters_score] = self.self_tune(X_train, y_train)
                best_num_filters = 5

                # as an option, we could tune optimal CSP filter num against complete train set
                #X_tune = self.preprocess_X(X, b, a, epoch_trim_start, epoch_trim_end)
                #[best_num_filters, best_num_filters_score] = self.self_tune(X_tune, y)

                # now use this insight to really fit with optimal CSP spatial filters
                """
				reg : float | str | None (default None)
			        if not None, allow regularization for covariance estimation
			        if float, shrinkage covariance is used (0 <= shrinkage <= 1).
			        if str, optimal shrinkage using Ledoit-Wolf Shrinkage ('ledoit_wolf')
			        or Oracle Approximating Shrinkage ('oas').
				"""
                transformer = CSP(n_components=best_num_filters,
                                  reg='ledoit_wolf')
                transformer.fit(X_train, y_train)

                # use these CSP spatial filters to transform train and test
                spatial_filters_train = transformer.transform(X_train)
                spatial_filters_test = transformer.transform(X_test)

                # put this back in as failsafe if NaN or inf starts cropping up
                # spatial_filters_train = np.nan_to_num(spatial_filters_train)
                # check_X_y(spatial_filters_train, y_train)
                # spatial_filters_test = np.nan_to_num(spatial_filters_test)
                # check_X_y(spatial_filters_test, y_test)

                # train LDA
                classifier = LinearDiscriminantAnalysis()
                classifier.fit(spatial_filters_train, y_train)
                score = classifier.score(spatial_filters_test, y_test)

                #print "current score",score
                print "bandpass:"******"epoch window:", epoch_trim_start, epoch_trim_end
                #print best_num_filters,"filters chosen"

                # put in ranked order Top 10 list
                idx = bisect(self.ranked_scores, score)
                self.ranked_scores.insert(idx, score)
                self.ranked_scores_opts.insert(
                    idx,
                    dict(bandpass=bandpass,
                         epoch_trim=epoch_trim,
                         filters=best_num_filters))
                self.ranked_classifiers.insert(idx, classifier)
                self.ranked_transformers.insert(idx, transformer)

                if len(self.ranked_scores) > self.num_votes:
                    self.ranked_scores.pop(0)
                if len(self.ranked_scores_opts) > self.num_votes:
                    self.ranked_scores_opts.pop(0)
                if len(self.ranked_classifiers) > self.num_votes:
                    self.ranked_classifiers.pop(0)
                if len(self.ranked_transformers) > self.num_votes:
                    self.ranked_transformers.pop(0)
                """
				Covariance computation
				"""
                # compute covariance matrices
                cov_data_train = covariances(X=X_train)
                cov_data_test = covariances(X=X_test)

                clf_mdm = MDM(metric=dict(mean='riemann', distance='riemann'))
                clf_mdm.fit(cov_data_train, y_train)
                score_mdm = clf_mdm.score(cov_data_test, y_test)
                # print "MDM prediction score:",score_mdm
                # put in ranked order Top 10 list
                idx = bisect(self.ranked_scores_mdm, score_mdm)
                self.ranked_scores_mdm.insert(idx, score_mdm)
                self.ranked_scores_opts_mdm.insert(
                    idx,
                    dict(bandpass=bandpass,
                         epoch_trim=epoch_trim,
                         filters=best_num_filters))
                self.ranked_classifiers_mdm.insert(idx, clf_mdm)

                if len(self.ranked_scores_mdm) > self.num_votes:
                    self.ranked_scores_mdm.pop(0)
                if len(self.ranked_scores_opts_mdm) > self.num_votes:
                    self.ranked_scores_opts_mdm.pop(0)
                if len(self.ranked_classifiers_mdm) > self.num_votes:
                    self.ranked_classifiers_mdm.pop(0)

                clf_ts = TSclassifier()
                clf_ts.fit(cov_data_train, y_train)
                score_ts = clf_ts.score(cov_data_test, y_test)
                # put in ranked order Top 10 list
                idx = bisect(self.ranked_scores_ts, score_ts)
                self.ranked_scores_ts.insert(idx, score_ts)
                self.ranked_scores_opts_ts.insert(
                    idx,
                    dict(bandpass=bandpass,
                         epoch_trim=epoch_trim,
                         filters=best_num_filters))
                self.ranked_classifiers_ts.insert(idx, clf_ts)

                if len(self.ranked_scores_ts) > self.num_votes:
                    self.ranked_scores_ts.pop(0)
                if len(self.ranked_scores_opts_ts) > self.num_votes:
                    self.ranked_scores_opts_ts.pop(0)
                if len(self.ranked_classifiers_ts) > self.num_votes:
                    self.ranked_classifiers_ts.pop(0)

                print "CSP+LDA score:", score, "Tangent space w/LR score:", score_ts

                print "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
                print "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"
                print "    T O P  ", self.num_votes, "  C L A S S I F I E R S"
                print
                #j=1
                for i in xrange(len(self.ranked_scores)):
                    print i, ",", round(self.ranked_scores[i], 4), ",",
                    print self.ranked_scores_opts[i]
                print "-------------------------------------"
                for i in xrange(len(self.ranked_scores_ts)):
                    print i, ",", round(self.ranked_scores_ts[i], 4), ",",
                    print self.ranked_scores_opts_ts[i]
                print "-------------------------------------"
                for i in xrange(len(self.ranked_scores_mdm)):
                    print i, ",", round(self.ranked_scores_mdm[i], 4), ",",
                    print self.ranked_scores_opts_mdm[i]

        # finish up, set the flag to indicate "fitted" state
        self.fit_ = True

        # Return the classifier
        return self
    def predict(self, X, y):
        """
		:param X:
		:return:
		"""
        X = check_array(X, allow_nd=True)

        if X.ndim == 2:
            trials = 1
        if X.ndim == 3:
            trials = X.shape[0]

        predictions = np.zeros((self.num_votes, trials))
        #decisions = np.zeros((self.num_votes, X.shape[0]))
        predict_probas = np.zeros((self.num_votes, trials, len(self.classes_)))

        predictions_ts = np.zeros((self.num_votes, trials))
        predict_probas_ts = np.zeros(
            (self.num_votes, trials, len(self.classes_)))

        predictions_mdm = np.zeros((self.num_votes, trials))
        predict_probas_mdm = np.zeros(
            (self.num_votes, trials, len(self.classes_)))

        for i in xrange(self.num_votes):
            # print "----------------------------------------------"
            # print "predicting with classifier",i+1
            # print self.ranked_scores_opts[i]

            # preprocess
            bandpass_start, bandpass_end = self.ranked_scores_opts[i][
                'bandpass']
            epoch_trim_start, epoch_trim_end = self.ranked_scores_opts[i][
                'epoch_trim']
            b, a = butter(
                5,
                np.array([bandpass_start, bandpass_end]) / (self.sfreq * 0.5),
                'bandpass')
            X_predict = self.preprocess_X(X, b, a, epoch_trim_start,
                                          epoch_trim_end)

            # use CSP spatial filters to transform (extract features)
            classification_features = self.ranked_transformers[i].transform(
                X_predict)

            #decisions[i] = self.ranked_classifiers[i].decision_function(classification_features)
            #print "decision: ",decisions[i]

            predictions[i] = self.ranked_classifiers[i].predict(
                classification_features)
            #print "predicts: ",predictions[i]

            predict_probas[i] = self.ranked_classifiers[i].predict_proba(
                classification_features)
            #print "predict_proba: ",predict_probas[i]

            # Tangent space + Logistic Regression
            # preprocess
            bandpass_start, bandpass_end = self.ranked_scores_opts_ts[i][
                'bandpass']
            epoch_trim_start, epoch_trim_end = self.ranked_scores_opts_ts[i][
                'epoch_trim']
            b, a = butter(
                5,
                np.array([bandpass_start, bandpass_end]) / (self.sfreq * 0.5),
                'bandpass')
            X_predict = self.preprocess_X(X, b, a, epoch_trim_start,
                                          epoch_trim_end)
            # compute covariance matrices
            cov_data = covariances(X=X_predict)
            predictions_ts[i] = self.ranked_classifiers_ts[i].predict(cov_data)
            predict_probas_ts[i] = self.ranked_classifiers_ts[i].predict_proba(
                cov_data)

            # preprocess
            bandpass_start, bandpass_end = self.ranked_scores_opts_mdm[i][
                'bandpass']
            epoch_trim_start, epoch_trim_end = self.ranked_scores_opts_mdm[i][
                'epoch_trim']
            b, a = butter(
                5,
                np.array([bandpass_start, bandpass_end]) / (self.sfreq * 0.5),
                'bandpass')
            X_predict = self.preprocess_X(X, b, a, epoch_trim_start,
                                          epoch_trim_end)
            # compute covariance matrices
            cov_data = covariances(X=X_predict)
            predictions_mdm[i] = self.ranked_classifiers_mdm[i].predict(
                cov_data)
            predict_probas_mdm[i] = self.ranked_classifiers_mdm[
                i].predict_proba(cov_data)

        print "**********************************************"
        #print "decisions: "
        #print decisions
        #print "predicts: "
        #print predictions
        #print "classes", self.classes_
        print "left :2, right:3"
        print "predict class probability: "
        print np.around(predict_probas, 4)
        print np.around(predict_probas_ts, 4)
        print np.around(predict_probas_mdm, 4)
        print "REAL Y:", y
Exemple #5
0
                tmax,
                proj=True,
                picks=picks,
                baseline=None,
                preload=True,
                add_eeg_ref=False,
                verbose=False)
labels = epochs.events[:, -1] - 2

# cross validation
cv = KFold(len(labels), 10, shuffle=True, random_state=42)
# get epochs
epochs_data_train = epochs.get_data()

# compute covariance matrices
cov_data_train = covariances(epochs_data_train)

###############################################################################
# Classification with Minimum distance to mean
mdm = MDM()

# Use scikit-learn Pipeline with cross_val_score function
scores = cross_val_score(mdm, cov_data_train, labels, cv=cv, n_jobs=1)

# Printing the results
class_balance = np.mean(labels == labels[0])
class_balance = max(class_balance, 1. - class_balance)
print("MDM Classification accuracy:       %f / Chance level: %f" %
      (np.mean(scores), class_balance))

###############################################################################