def logreg_timedecoding(epochs, numcv=4, jobs=1): """ Logistic regression over sensors. Returns Evoked array containing coefficients and ROC. Code snippets stolen from: https://martinos.org/mne/stable/auto_tutorials/plot_sensors_decoding.html """ X = epochs.get_data() # MEG signals: n_epochs, n_channels, n_times X = X.astype(float) y = epochs.events[:, 2] # targets # setup and run the decoder clf = make_pipeline(StandardScaler(), LinearModel(LogisticRegression())) time_decod = SlidingEstimator(clf, scoring='roc_auc', n_jobs=jobs) #scoring='roc_auc', scores = cross_val_multiscore(time_decod, X, y, cv=numcv, n_jobs=jobs) # Mean scores across cross-validation splits scores = np.mean(scores, axis=0) # time_decod = SlidingEstimator(clf, scoring='roc_auc', n_jobs=jobs) time_decod.fit(X, y) coef = get_coef(time_decod, 'patterns_', inverse_transform=True) evoked = mne.EvokedArray(coef, epochs.info, tmin=epochs.times[0]) evoked.roc_auc = scores return evoked
def devp_estimator(self, **kwargs): from sklearn.pipeline import make_pipeline from sklearn.preprocessing import StandardScaler from sklearn.linear_model import LogisticRegression from mne.decoding import (SlidingEstimator, GeneralizingEstimator, Scaler, cross_val_multiscore, LinearModel, get_coef, Vectorizer, CSP) from sklearn.metrics import make_scorer clf = make_pipeline(StandardScaler(), LinearModel(LogisticRegression(solver='lbfgs'))) time_decod = SlidingEstimator(clf, n_jobs=6, scoring='roc_auc', verbose=True) return time_decod
def make_clf(pattern=False, vectorized=False): clf = [] if vectorized: clf.append(('vectorizer', Vectorizer())) clf.append(('scaler', MinMaxScaler())) # use linear SVM as the estimator estimator = SVC(max_iter=-1, kernel='linear', random_state=12345, class_weight='balanced', probability=True) if pattern: estimator = LinearModel(estimator) clf.append(('estimator', estimator)) clf = Pipeline(clf) return clf
def devp_estimator_gat(self, **kwargs): from mne.decoding import GeneralizingEstimator, LinearModel from sklearn.pipeline import make_pipeline from sklearn.preprocessing import StandardScaler from sklearn.linear_model import Ridge from sklearn.metrics import make_scorer from sklearn.model_selection import StratifiedKFold from mne.decoding import (SlidingEstimator, GeneralizingEstimator, Scaler, cross_val_multiscore, LinearModel, get_coef, Vectorizer, CSP) from jr.gat import scorer_spearman clf = make_pipeline(StandardScaler(), LinearModel(Ridge())) scorer = scorer_spearman kwargs = dict() gat = GeneralizingEstimator(clf, scoring=make_scorer(scorer), n_jobs=6, **kwargs) return gat
def sliding_logreg_source(X, y, cross_val, return_clf=False): """Run a sliding estimator with Logistic Regression on source data. Parameters: ----------- X : np.array features. y : vector response vector. cross_val : cross validation object cross validation to adopt. return_clf : bool whether the clf object should be returned as well. Returns ------- score : float cross-validated AUC score clf : classifier object If return_clf == True, the classifier object will be returned, too. """ startt = time.time() # Model clf = make_pipeline(StandardScaler(), LinearModel(LogisticRegression())) sliding = SlidingEstimator(clf, scoring='roc_auc', n_jobs=1) print('Computing Logistic Regression.') score = cross_val_multiscore(sliding, X, y, cv=cross_val) endt = time.time() print('Done. Time elapsed for sliding estimator: %i seconds.' % (endt - startt)) if return_clf is True: return score, clf else: return score
# Plot fig, ax = plt.subplots() ax.plot(epochs.times, scores, label='score') ax.axhline(.5, color='k', linestyle='--', label='chance') ax.set_xlabel('Times') ax.set_ylabel('AUC') # Area Under the Curve ax.legend() ax.axvline(.0, color='k', linestyle='-') ax.set_title('Sensor space decoding') ############################################################################### # You can retrieve the spatial filters and spatial patterns if you explicitly # use a LinearModel clf = make_pipeline(StandardScaler(), LinearModel(LogisticRegression(solver='lbfgs'))) time_decod = SlidingEstimator(clf, n_jobs=1, scoring='roc_auc', verbose=True) time_decod.fit(X, y) coef = get_coef(time_decod, 'patterns_', inverse_transform=True) evoked = mne.EvokedArray(coef, epochs.info, tmin=epochs.times[0]) joint_kwargs = dict(ts_args=dict(time_unit='s'), topomap_args=dict(time_unit='s')) evoked.plot_joint(times=np.arange(0., .500, .100), title='patterns', **joint_kwargs) ############################################################################### # Temporal generalization # ^^^^^^^^^^^^^^^^^^^^^^^ #
evokeds = [] for sbj in sbjs: print(sbj) if sbj == 'VP12': #No REM here continue if os.path.exists(os.path.join(save_path, sbj + '.p')): continue #sleep_epochs = myload(base_path_data, typ='epoch_preprocessed', sbj=sbj, preload=True) # WAKE! sleep_epochs = myload(sleep_path_data, typ='epoch_preprocessed', sbj=sbj, preload=True) # SLEEP! sleep_epochs.event_id = sleep_event_id # event_id remapping. For wake this step works during preprocessing # SLEEP ! sleep_epochs = sleep_epochs.crop(tmin=tmin, tmax=tmax) X1, y1 = get_Xy_balanced(sleep_epochs, contrast1) clf = make_pipeline(Vectorizer(), StandardScaler(), LinearModel(LogisticRegression(max_iter = 4000))) #StandardScaler(), cv = StratifiedKFold(n_splits=2, shuffle=True) coef_folds = [] for train_idx, test_idx in cv.split(X1, y1): clf.fit(X1[train_idx], y=y1[train_idx]) #scores1.append(clf.score(X1[test_idx], y=y1[test_idx])) coef_folds.append(get_coef(clf, attr='patterns_', inverse_transform=True)) coef = np.asarray(coef_folds).mean(0).reshape([173, -1]) #mean folds and reshape evoked = EvokedArray(coef, sleep_epochs.info, tmin=tmin) evokeds.append(evoked) ga = mne.grand_average(evokeds) #SLEEP
# Create result folder results_folder = op.join(path_data + 'results/' + subject + output_folder) if not os.path.exists(results_folder): os.makedirs(results_folder) # Loop across each analysis for epoch_type, epoch_analyses in analyses.iteritems(): epochs, events = load(subject, epoch_type) events = get_events_interactions(events) for analysis in epoch_analyses: # define to-be-predicted values y = np.array(events[analysis]) # Define estimators depending on the analysis if 'angle' in analysis[:14]: clf = make_pipeline( StandardScaler(), LinearModel(AngularRegression(Ridge(), independent=False))) scorer = scorer_angle kwargs = dict() gat = GeneralizingEstimator(clf, scoring=make_scorer(scorer), n_jobs=24, **kwargs) y = np.array(y, dtype=float) elif 'sfreq' in analysis[:14]: clf = make_pipeline(StandardScaler(), LinearModel(Ridge())) scorer = scorer_spearman kwargs = dict() gat = GeneralizingEstimator(clf, scoring=make_scorer(scorer), n_jobs=24, **kwargs)
# # * :ref:`sphx_glr_auto_examples_decoding_plot_decoding_csp_eeg.py` # * :ref:`sphx_glr_auto_examples_decoding_plot_decoding_csp_timefreq.py` # # .. note:: # # The winning entry of the Grasp-and-lift EEG competition in Kaggle used # the :class:`~mne.decoding.CSP` implementation in MNE and was featured as # a `script of the week <sotw_>`_. # # .. _sotw: http://blog.kaggle.com/2015/08/12/july-2015-scripts-of-the-week/ # # We can use CSP with these data with: csp = CSP(n_components=3, norm_trace=False) clf_csp = make_pipeline(csp, LinearModel(LogisticRegression(solver='lbfgs'))) scores = cross_val_multiscore(clf_csp, X, y, cv=5, n_jobs=1) print('CSP: %0.1f%%' % (100 * scores.mean(),)) ############################################################################### # Source power comodulation (SPoC) # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ # Source Power Comodulation (:class:`mne.decoding.SPoC`) [3]_ # identifies the composition of # orthogonal spatial filters that maximally correlate with a continuous target. # # SPoC can be seen as an extension of the CSP where the target is driven by a # continuous variable rather than a discrete variable. Typical applications # include extraction of motor patterns using EMG power or audio patterns using # sound envelope. #
from sklearn.pipeline import make_pipeline from sklearn.model_selection import StratifiedKFold from sklearn.metrics import classification_report, confusion_matrix from sklearn.preprocessing import MinMaxScaler # import a linear classifier from mne.decoding from mne.decoding import LinearModel import matplotlib.pyplot as plt clf = LogisticRegression(solver='lbfgs') scaler = StandardScaler() # create a linear model with LogisticRegression model = LinearModel(clf) # Get the epoched data (get only the data columns) eeg_data = epochs.get_data().reshape(len(labels), -1) eeg_data = eeg_data[:, 0:epochs.get_data().shape[2] * 1] #eeg_data[labels==2] = erptemplate1[:201,0] #eeg_data[labels==1] = erptemplate1[:201,0] #eeg_data[labels==2] = np.zeros((eeg_data.shape[1],)) #eeg_data[labels==1] = np.ones((eeg_data.shape[1],)) #labels = np.random.permutation(labels) # fit the classifier on MEG data X = scaler.fit_transform(eeg_data)
raise ValueError('NaNs in training and/or test data') ######################### # Train and predict ######################### cv_obj = StratifiedKFold(y_labels, n_folds=SVM_P['n_folds'], shuffle=True) for ti, (train_idx, test_idx) in enumerate(cv_obj): # Initialize classifier with params # XXX: kernel must be 'linear' clf = LinearModel( SVC(kernel=SVM_P['kernel'], C=hp_set[0], gamma=hp_set[1], cache_size=SVM_P['cache_size'])) X_train = ss.fit_transform(trial_data[train_idx]) X_test = ss.transform(trial_data[test_idx]) # Train and test classifier, save score clf.fit(X_train, y_labels[train_idx]) temp_score = clf.score(X_test, y_labels[test_idx]) class_scores[hpi[0], hpi[1], hpi[2], ti] = temp_score print 'Test accuracy on CV %i: %0.2f' % (ti, temp_score) # Save results if save_data:
picks='eeg') #%% from mne.decoding import (GeneralizingEstimator, SlidingEstimator, Vectorizer, TimeFrequency, cross_val_multiscore, UnsupervisedSpatialFilter, Scaler, LinearModel, get_coef) from sklearn.preprocessing import StandardScaler from sklearn.linear_model import LogisticRegression from sklearn.pipeline import make_pipeline import sklearn as skl est = make_pipeline( StandardScaler(), LinearModel(LogisticRegression(class_weight='balanced', solver='lbfgs'))) sl = SlidingEstimator(est, scoring='roc_auc') def get_patterns(epochs): epochs.set_eeg_reference(ref_channels='average') sl.fit(epochs.get_data(), epochs.metadata.confdiff.to_numpy() <= 0) coef = mne.decoding.get_coef(sl, 'patterns_', inverse_transform=False) return mne.EvokedArray(-coef, epochs.info, tmin=epochs.times[0]) allpatterns = [] for ii, epochs in enumerate(data): print(ii, end=',') allpatterns.append(get_patterns(epochs))
stcs = apply_inverse_epochs(epochs, inverse_operator, lambda2=1.0 / snr ** 2, verbose=False, method="dSPM", pick_ori="normal") # %% # Decoding in sensor space using a logistic regression # Retrieve source space data into an array X = np.array([stc.lh_data for stc in stcs]) # only keep left hemisphere y = epochs.events[:, 2] # prepare a series of classifier applied at each time sample clf = make_pipeline(StandardScaler(), # z-score normalization SelectKBest(f_classif, k=500), # select features for speed LinearModel(LogisticRegression(C=1, solver='liblinear'))) time_decod = SlidingEstimator(clf, scoring='roc_auc') # Run cross-validated decoding analyses: scores = cross_val_multiscore(time_decod, X, y, cv=5, n_jobs=1) # Plot average decoding scores of 5 splits fig, ax = plt.subplots(1) ax.plot(epochs.times, scores.mean(0), label='score') ax.axhline(.5, color='k', linestyle='--', label='chance') ax.axvline(0, color='k') plt.legend() # %% # To investigate weights, we need to retrieve the patterns of a fitted model
preload=True) labels = epochs.events[:, -1] # get MEG and EEG data meg_epochs = epochs.copy().pick_types(meg=True, eeg=False) meg_data = meg_epochs.get_data().reshape(len(labels), -1) ############################################################################### # Decoding in sensor space using a LogisticRegression classifier clf = LogisticRegression(solver='lbfgs') scaler = StandardScaler() # create a linear model with LogisticRegression model = LinearModel(clf) # fit the classifier on MEG data X = scaler.fit_transform(meg_data) model.fit(X, labels) # Extract and plot spatial filters and spatial patterns for name, coef in (('patterns', model.patterns_), ('filters', model.filters_)): # We fitted the linear model onto Z-scored data. To make the filters # interpretable, we must reverse this normalization step coef = scaler.inverse_transform([coef])[0] # The data was vectorized to fit a single model across all time points and # all channels. We thus reshape it: coef = coef.reshape(len(meg_epochs.ch_names), -1)
labels = epochs.events[:, -1] # get MEG and EEG data meg_epochs = epochs.copy().pick_types(meg=True, eeg=False) meg_data = meg_epochs.get_data().reshape(len(labels), -1) # %% # Decoding in sensor space using a LogisticRegression classifier # -------------------------------------------------------------- clf = LogisticRegression(solver='liblinear') # liblinear is faster than lbfgs scaler = StandardScaler() # create a linear model with LogisticRegression model = LinearModel(clf) # fit the classifier on MEG data X = scaler.fit_transform(meg_data) model.fit(X, labels) # Extract and plot spatial filters and spatial patterns for name, coef in (('patterns', model.patterns_), ('filters', model.filters_)): # We fitted the linear model onto Z-scored data. To make the filters # interpretable, we must reverse this normalization step coef = scaler.inverse_transform([coef])[0] # The data was vectorized to fit a single model across all time points and # all channels. We thus reshape it: coef = coef.reshape(len(meg_epochs.ch_names), -1)
else: assert interval == "All" sl = slice(None) eps = eps[sl] info = eps.info time = eps.times s_ix = slice(ix[0], ix[1]) c1, c2 = list(eps.event_id.keys()) clf = make_pipeline( Scaler(eps.info), Vectorizer(), PCA(0.9999), LinearModel( LogisticRegression( solver=solver, penalty="l1", max_iter=1000, multi_class="auto", random_state=seed, )), ) time_decode = SlidingEstimator(clf, n_jobs=n_jobs, scoring="roc_auc", verbose=False) # K-fold cross-validation with ROC area under curve score if subject[4:] in ("101", "124a", "213", "301a") and interval != "All": use_splits = 3 else: use_splits = n_splits cv = StratifiedKFold(n_splits=use_splits,
def main(): model_type = "lda" exp_name = "freq_gen_matrix/" for i, sample in enumerate(range(1, 22)): print("sample {}".format(sample)) if not os.path.isdir("Results/{}/{}/sample_{}".format( model_type, exp_name, sample)): os.mkdir("Results/{}/{}/sample_{}".format(model_type, exp_name, sample)) epochs = get_epochs(sample, scale=False) y_train = epochs.events[:, 2] freqs = np.logspace(*np.log10([2, 25]), num=15) n_cycles = freqs / 4. string_freqs = [round(x, 2) for x in freqs] print("applying morlet wavelet") wavelet_output = tfr_array_morlet(epochs.get_data(), sfreq=epochs.info['sfreq'], freqs=freqs, n_cycles=n_cycles, output='complex') time_results = np.zeros( (wavelet_output.shape[3], len(freqs), len(freqs))) for time in range(wavelet_output.shape[3]): print("time: {}".format(time)) wavelet_epochs = wavelet_output[:, :, :, time] wavelet_epochs = np.append(wavelet_epochs.real, wavelet_epochs.imag, axis=1) wavelet_info = mne.create_info(ch_names=wavelet_epochs.shape[1], sfreq=epochs.info['sfreq'], ch_types='mag') wavelet_epochs = mne.EpochsArray(wavelet_epochs, info=wavelet_info, events=epochs.events) x_train = pca(80, wavelet_epochs, plot=False) model = LinearModel( LinearDiscriminantAnalysis(solver='lsqr', shrinkage='auto')) freq_gen = GeneralizingEstimator(model, n_jobs=1, scoring='accuracy', verbose=True) scores = cross_val_multiscore(freq_gen, x_train, y_train, cv=5, n_jobs=1) scores = np.mean(scores, axis=0) time_results[time] = scores sns.set() ax = sns.barplot( np.sort(string_freqs), np.diag(scores), ) ax.set(ylim=(0, 0.8), xlabel='Frequencies', ylabel='Accuracy', title='Cross Val Accuracy {} for Subject {} for Time {}'. format(model_type, sample, time)) ax.axhline(0.12, color='k', linestyle='--') ax.figure.set_size_inches(8, 6) ax.figure.savefig( "Results/{}/{}/sample_{}/time_{}_accuracy.png".format( model_type, exp_name, sample, time), dpi=300) plt.close('all') # plt.show() fig, ax = plt.subplots(1, 1) im = ax.imshow(scores, interpolation='lanczos', origin='lower', cmap='RdBu_r', extent=[2, 25, 2, 25], vmin=0., vmax=0.8) ax.set_xlabel('Testing Frequency (hz)') ax.set_ylabel('Training Frequency (hz)') ax.set_title( 'Frequency generalization for Subject {} at Time {}'.format( sample, time)) plt.colorbar(im, ax=ax) ax.grid(False) ax.figure.savefig( "Results/{}/{}/sample_{}/time_{}_matrix.png".format( model_type, exp_name, sample, time), dpi=300) plt.close('all') # plt.show() time_results = time_results.reshape(time_results.shape[0], -1) all_results_df = pd.DataFrame(time_results) all_results_df.to_csv( "Results/{}/{}/sample_{}/all_time_matrix_results.csv".format( model_type, exp_name, sample))
tmin = i * 1.0 tmax = (i + 1) * 1.0 #Create :class:'Epochs <mne.Epochs>' object epochs = mne.Epochs(raw, events=events, event_id=event_id, tmin=tmin, tmax=tmax, baseline=None, verbose=True, preload=True) for i in range(0, len(epochs.events)): if i % 2 == 0: epochs.events[i, 2] = 3 #epochs.plot(scalings = 'auto',block = True,n_epochs=10) X = epochs.pick_types(meg=False, eeg=True) y = epochs.events[:, -1] # Define a unique pipeline to sequentially: clf = make_pipeline( Vectorizer(), # 1) vectorize across time and channels StandardScaler(), # 2) normalize features across trials LinearModel(LogisticRegression())) # 3) fits a logistic regression clf.fit(X, y) coef = get_coef(clf, 'patterns_', inverse_transform=True) evoked = EvokedArray(coef, epochs.info, tmin=epochs.tmin) fig = evoked.plot_topomap(title='EEG Patterns', size=3, show=False) fig.savefig(title + "_ti_" + str(tmin) + "_tf_" + str(tmax) + '.png')
verbose=False, method="dSPM", pick_ori="normal") ############################################################################### # Decoding in sensor space using a logistic regression # Retrieve source space data into an array X = np.array([stc.lh_data for stc in stcs]) # only keep left hemisphere y = epochs.events[:, 2] # prepare a series of classifier applied at each time sample clf = make_pipeline( StandardScaler(), # z-score normalization SelectKBest(f_classif, k=500), # select features for speed LinearModel(LogisticRegression(C=1))) time_decod = SlidingEstimator(clf, scoring='roc_auc') # Run cross-validated decoding analyses: scores = cross_val_multiscore(time_decod, X, y, cv=5, n_jobs=1) # Plot average decoding scores of 5 splits fig, ax = plt.subplots(1) ax.plot(epochs.times, scores.mean(0), label='score') ax.axhline(.5, color='k', linestyle='--', label='chance') ax.axvline(0, color='k') plt.legend() ############################################################################### # To investigate weights, we need to retrieve the patterns of a fitted model
# Plot fig, ax = plt.subplots() ax.plot(epochs.times, scores, label='score') ax.axhline(.5, color='k', linestyle='--', label='chance') ax.set_xlabel('Times') ax.set_ylabel('AUC') # Area Under the Curve ax.legend() ax.axvline(.0, color='k', linestyle='-') ax.set_title('Sensor space decoding') plt.show() ############################################################################### # You can retrieve the spatial filters and spatial patterns if you explicitly # use a LinearModel clf = make_pipeline(StandardScaler(), LinearModel(LogisticRegression())) time_decod = SlidingEstimator(clf, n_jobs=1, scoring='roc_auc', verbose=True) time_decod.fit(X, y) coef = get_coef(time_decod, 'patterns_', inverse_transform=True) evoked = mne.EvokedArray(coef, epochs.info, tmin=epochs.times[0]) joint_kwargs = dict(ts_args=dict(time_unit='s'), topomap_args=dict(time_unit='s')) evoked.plot_joint(times=np.arange(0., .500, .100), title='patterns', **joint_kwargs) ############################################################################### # Temporal Generalization # ----------------------- #
raw = mne.io.read_raw_fif(raw_fname, preload=True) raw.filter(0.5, 40) events = mne.find_events(raw) event_id = dict(aud_l=1, aud_r=2) # Do epoching:: epochs = mne.Epochs(raw, events, event_id, -0.1, 0.4, proj=True, baseline=(None, 0), preload=True, decim=4, reject=dict(mag=3e-12, eog=200e-6)) epochs.pick_types(meg='grad', exclude='bads') # assign data and labels to X, y y = epochs.events[:, -1] X = epochs.get_data() # Build a pipeline with the LinearModel included: clf = make_pipeline(StandardScaler(), LinearModel(ElasticNetCV(cv=5))) time_decod = SlidingEstimator(clf, n_jobs=4, scoring='roc_auc', verbose=False) # Do a perfect fit: time_decod.fit(X, y) # Get model coefficients as field patterns with inverse transforms: coef = get_coef(time_decod, 'patterns_', inverse_transform=True) # Construct an Evoked and plot: evoked = mne.EvokedArray(coef, epochs.info, tmin=epochs.times[0]) evoked.plot_topomap(times=np.arange(0.0, 0.4001, 0.1), title='field patterns')