def _fit(self, X, y): """fit and return epochs.""" epochs, cov_signal = get_epochs_and_cov(X, y, self.window) xd = Xdawn(n_components=self.nfilters, signal_cov=cov_signal, correct_overlap=False) xd.fit(epochs) P = [] for eid in getEventNames(): P.append(np.dot(xd.filters_[eid][:, 0:self.nfilters].T, xd.evokeds_[eid].data)) self.P = np.concatenate(P, axis=0) self.labels_train = epochs.events[:, -1] return epochs
def spatial_filter(raw, epochs): print('spatial filtering using xDawn') picks = pick_types(raw.info, meg=False, eeg=True, stim=False, eog=False, exclude='bads') signal_cov = compute_raw_covariance(raw, picks=picks) xd = Xdawn(n_components=2, signal_cov=signal_cov) print('fiting xDawn') xd.fit(epochs) print('epoch denoising started') epochs_denoised = xd.apply(epochs) print('epoch_denoise complete') return epochs_denoised
def _fit(self, X, y): """fit and return epochs.""" epochs, cov_signal = get_epochs_and_cov(X, y, self.window) xd = Xdawn(n_components=self.nfilters, signal_cov=cov_signal, correct_overlap=False) xd.fit(epochs) P = [] for eid in getEventNames(): P.append( np.dot(xd.filters_[eid][:, 0:self.nfilters].T, xd.evokeds_[eid].data)) self.P = np.concatenate(P, axis=0) self.labels_train = epochs.events[:, -1] return epochs
def _fit(self, X, y): """fit and return epochs.""" epochs, cov_signal = get_epochs_and_cov(X, y, self.window) # pdb.set_trace() # TODO: Try to understand what is Xdawn (something that improve SSRN by spatial information?) xd = Xdawn(n_components=self.nfilters, signal_cov=cov_signal, correct_overlap=False) xd.fit(epochs) P = [] for eid in getEventNames(): # set_breakpoint('xd info in getEVetnNames for-lop'); # pdb.set_trace() P.append(np.dot(xd.filters_[eid][:, 0:self.nfilters].T, xd.evokeds_[eid].data)) self.P = np.concatenate(P, axis=0) self.labels_train = epochs.events[:, -1] # pdb.set_trace() return epochs
exclude='bads') epochs = Epochs(raw, events, event_id, tmin, tmax, proj=False, picks=picks, baseline=None, preload=True, add_eeg_ref=False, verbose=False) # Create classification pipeline clf = make_pipeline(Xdawn(n_components=3), EpochsVectorizer(), MinMaxScaler(), LogisticRegression(penalty='l1')) # Get the labels labels = epochs.events[:, -1] # Cross validator cv = StratifiedKFold(y=labels, n_folds=10, shuffle=True, random_state=42) # Do cross-validation preds = np.empty(len(labels)) for train, test in cv: clf.fit(epochs[train], labels[train]) preds[test] = clf.predict(epochs[test]) # Classification report
if subject_id in exclude: continue subject = 'S%02d' % subject_id data_path = os.path.join('/home/claire/DATA/Data_Face_House/' + subject + '/EEG/Evoked_Lowpass') fname_in = os.path.join(data_path, '%s-epo.fif' % subject) epochs = mne.read_epochs(fname_in) epochs.interpolate_bads() # all_epochs.append(epochs) #epochs = mne.concatenate_epochs(all_epochs) epochs.pick_types(eeg=True) # Create classification pipeline clf = make_pipeline(Xdawn(n_components=3, reg='oas'), Vectorizer(), MinMaxScaler(), LogisticRegression(penalty='l1')) le = LabelEncoder() labels = le.fit_transform(epochs.events[:, 2]) # Cross validator cv = StratifiedKFold(y=labels, n_folds=10, shuffle=True, random_state=42) # Do cross-validation preds = np.empty(len(labels)) for train, test in cv: clf.fit(epochs[train], labels[train]) preds[test] = clf.predict(epochs[test]) # Classification report
exclude='bads') epochs = Epochs(raw, events, event_id, tmin, tmax, proj=False, picks=picks, baseline=None, preload=True, verbose=False) # Create classification pipeline clf = make_pipeline( Xdawn(n_components=n_filter), Vectorizer(), MinMaxScaler(), LogisticRegression(penalty='l1', solver='liblinear', multi_class='auto')) # Get the labels labels = epochs.events[:, -1] # Cross validator cv = StratifiedKFold(n_splits=10, shuffle=True, random_state=42) # Do cross-validation preds = np.empty(len(labels)) for train, test in cv.split(epochs, labels): clf.fit(epochs[train], labels[train]) preds[test] = clf.predict(epochs[test]) # Classification report
exclude='bads') # Epoching epochs = Epochs(raw, events, event_id, tmin, tmax, proj=False, picks=picks, baseline=None, preload=True, verbose=False) # Plot image epoch before xdawn plot_epochs_image(epochs['vis_r'], picks=[230], vmin=-500, vmax=500) # Estimates signal covariance signal_cov = compute_raw_covariance(raw, picks=picks) # Xdawn instance xd = Xdawn(n_components=2, signal_cov=signal_cov) # Fit xdawn xd.fit(epochs) # Denoise epochs epochs_denoised = xd.apply(epochs) # Plot image epoch after Xdawn plot_epochs_image(epochs_denoised['vis_r'], picks=[230], vmin=-500, vmax=500)
#raw_test = concatenate_raws([creat_mne_raw_object(fname, read_events=False) for fname in fnames_test]) picks_train = pick_types(raw_train.info, eeg=True) picks_val = pick_types(raw_val.info, eeg=True) #picks_test = pick_types(raw_test.info, eeg=True) data_train = raw_train._data[picks_train].T labels_train = raw_train._data[32:].T data_val = raw_val._data[picks_val].T labels_val = raw_val._data[32:].T #data_test = raw_test._data[picks_test].T #labels_test = None train_epochs, train_cov_signal = get_epochs_and_cov(raw_train, picks_train, WINDOW) xd = Xdawn(n_components=NFILTERS, signal_cov=train_cov_signal, correct_overlap=False) xd.fit(train_epochs) val_epochs, val_cov_signal = get_epochs_and_cov(raw_val, picks_val, WINDOW) xd = Xdawn(n_components=NFILTERS, signal_cov=val_cov_signal, correct_overlap=False) xd.fit(val_epochs) P = [] for eid in getEventNames(): P.append(np.dot(xd.filters_[eid][:, 0:NFILTERS].T, xd.evokeds_[eid].data)) print ("Saving data for subject{0} in files".format(subject)) np.save('data/processed/subj{0}_train_data.npy'.format(subject), train_epochs._data) np.save('data/processed/subj{0}_train_labels.npy'.format(subject), train_epochs.events) np.save('data/processed/subj{0}_val_data.npy'.format(subject), val_epochs._data)
) mne_res = mne_eval.process(mne_ppl) ############################################################################## # Advanced MNE pipeline # --------------------- # # In some case, the MNE pipeline should have access to the original labels from # the dataset. This is the case for the XDAWN code of MNE. One could pass # `mne_labels` to evaluation in order to keep this label. # As an example, we will define a pipeline that compute an XDAWN filter, rescale, # then apply a logistic regression. mne_adv = {} mne_adv["XDAWN LR"] = make_pipeline( Xdawn(n_components=5, reg="ledoit_wolf", correct_overlap=False), Vectorizer(), StandardScaler(), LogisticRegression(penalty="l1", solver="liblinear"), ) adv_eval = CrossSessionEvaluation( paradigm=paradigm, datasets=datasets, suffix="examples", overwrite=True, return_epochs=True, mne_labels=True, ) adv_res = mne_eval.process(mne_adv) ###############################################################################
exclude='bads') epochs = Epochs(raw, events, event_id, tmin, tmax, proj=False, picks=picks, baseline=None, preload=True, verbose=False) # Create classification pipeline clf = make_pipeline( Xdawn(n_components=3), Vectorizer(), MinMaxScaler(), LogisticRegression(penalty='l1', solver='liblinear', multi_class='auto')) # Get the labels labels = epochs.events[:, -1] # Cross validator cv = StratifiedKFold(n_splits=10, shuffle=True, random_state=42) # Do cross-validation preds = np.empty(len(labels)) for train, test in cv.split(epochs, labels): clf.fit(epochs[train], labels[train]) preds[test] = clf.predict(epochs[test]) # Classification report
#raw.plot(duration=20) #raw.plot_psd() raw.filter(1, 20, fir_design='firwin') #raw.plot(duration=20) #raw.plot_psd() events = read_events(event_fname) picks = pick_types(raw.info, meg=False, eeg=True, stim=False, eog=False, exclude='bads') epochs = Epochs(raw, events, event_id, tmin, tmax, proj=False, picks=picks, baseline=None, preload=True, verbose=False) # Create classification pipeline clf = make_pipeline(Xdawn(n_components=n_filter), Vectorizer(), MinMaxScaler(), LogisticRegression(penalty='l1', solver='liblinear', multi_class='auto')) # Get the labels labels = epochs.events[:, -1] # Cross validator cv = StratifiedKFold(n_splits=10, shuffle=True, random_state=42) # Do cross-validation preds = np.empty(len(labels)) for train, test in cv.split(epochs, labels): clf.fit(epochs[train], labels[train])
event_id = dict(aud_l=1, aud_r=2, vis_l=3, vis_r=4) # Setup for reading the raw data raw = io.Raw(raw_fname, preload=True) raw.filter(1, 20, method='iir') events = read_events(event_fname) picks = pick_types(raw.info, meg=False, eeg=True, stim=False, eog=False, exclude='bads') epochs = Epochs(raw, events, event_id, tmin, tmax, proj=False, picks=picks, baseline=None, preload=True, add_eeg_ref=False, verbose=False) # Create classification pipeline clf = make_pipeline(Xdawn(n_components=3), EpochsVectorizer(), MinMaxScaler(), LogisticRegression(penalty='l1')) # Get the labels labels = epochs.events[:, -1] # Cross validator cv = StratifiedKFold(y=labels, n_folds=10, shuffle=True, random_state=42) # Do cross-validation preds = np.empty(len(labels)) for train, test in cv: clf.fit(epochs[train], labels[train]) preds[test] = clf.predict(epochs[test])
# Setup for reading the raw data raw = io.read_raw_fif(raw_fname, preload=True) raw.filter(1, 20) # replace baselining with high-pass events = read_events(event_fname) raw.info['bads'] = ['MEG 2443'] # set bad channels picks = pick_types(raw.info, meg=True, eeg=False, stim=False, eog=False, exclude='bads') # Epoching epochs = Epochs(raw, events, event_id, tmin, tmax, proj=False, picks=picks, baseline=None, preload=True, verbose=False) # Plot image epoch before xdawn plot_epochs_image(epochs['vis_r'], picks=[230], vmin=-500, vmax=500) # Estimates signal covariance signal_cov = compute_raw_covariance(raw, picks=picks) # Xdawn instance xd = Xdawn(n_components=2, signal_cov=signal_cov) # Fit xdawn xd.fit(epochs) # Denoise epochs epochs_denoised = xd.apply(epochs) # Plot image epoch after Xdawn plot_epochs_image(epochs_denoised['vis_r'], picks=[230], vmin=-500, vmax=500)