def test_scaler(): """Test methods of Scaler.""" raw = io.read_raw_fif(raw_fname) events = read_events(event_name) picks = pick_types(raw.info, meg=True, stim=False, ecg=False, eog=False, exclude='bads') picks = picks[1:13:3] epochs = Epochs(raw, events, event_id, tmin, tmax, picks=picks, baseline=(None, 0), preload=True) epochs_data = epochs.get_data() scaler = Scaler(epochs.info) y = epochs.events[:, -1] X = scaler.fit_transform(epochs_data, y) assert_true(X.shape == epochs_data.shape) X2 = scaler.fit(epochs_data, y).transform(epochs_data) assert_array_equal(X2, X) # these should be across time assert_allclose(X.std(axis=-2), 1.) assert_allclose(X.mean(axis=-2), 0., atol=1e-12) # Test inverse_transform Xi = scaler.inverse_transform(X, y) assert_array_almost_equal(epochs_data, Xi) for kwargs in [{'with_mean': False}, {'with_std': False}]: scaler = Scaler(epochs.info, **kwargs) scaler.fit(epochs_data, y) assert_array_almost_equal( X, scaler.inverse_transform(scaler.transform(X))) # Test init exception assert_raises(ValueError, scaler.fit, epochs, y) assert_raises(ValueError, scaler.transform, epochs, y)
def test_scaler(): """Test methods of Scaler.""" raw = io.read_raw_fif(raw_fname) events = read_events(event_name) picks = pick_types(raw.info, meg=True, stim=False, ecg=False, eog=False, exclude='bads') picks = picks[1:13:3] epochs = Epochs(raw, events, event_id, tmin, tmax, picks=picks, baseline=(None, 0), preload=True) epochs_data = epochs.get_data() scaler = Scaler(epochs.info) y = epochs.events[:, -1] # np invalid divide value warnings with warnings.catch_warnings(record=True): X = scaler.fit_transform(epochs_data, y) assert_true(X.shape == epochs_data.shape) X2 = scaler.fit(epochs_data, y).transform(epochs_data) assert_array_equal(X2, X) # Test inverse_transform with warnings.catch_warnings(record=True): # invalid value in mult Xi = scaler.inverse_transform(X, y) assert_array_almost_equal(epochs_data, Xi) for kwargs in [{'with_mean': False}, {'with_std': False}]: scaler = Scaler(epochs.info, **kwargs) scaler.fit(epochs_data, y) assert_array_almost_equal( X, scaler.inverse_transform(scaler.transform(X))) # Test init exception assert_raises(ValueError, scaler.fit, epochs, y) assert_raises(ValueError, scaler.transform, epochs, y)
def raw_to_data(raw_edf, training=True, drop_rejects=True, subj=None): tmin, tmax = 0, 4. stim_code = dict([(32766,1),(769,2), (770,3), (771,4), (772,5),(783,6),(276,7),(277,8),(768,9), (1023,10),(1072,11)]) if training: path = op.join('data_i2r', 'BCI_IV_2a', 'TrainingSet') if not training: path = op.join('data_i2r', 'BCI_IV_2a', 'TestingSet') label_path = op.join('data_i2r', 'BCI_IV_2a', 'true_labels') label_files_list = glob.glob(label_path + '/*E.mat') label_subj = [ int(f.split('A0')[1][0]) for f in label_files_list ] file_list = glob.glob(path + '/*.gdf') subjects = [ int(f.split('A0')[1][0]) for f in file_list ] if not training: label_subj = [ np.argwhere(np.array(label_subj)==subjects[i])[0][0] for i in range(len(subjects))] event_id = dict() events_from_edf = [] sampling_frequency = raw_edf._raw_extras[0]['max_samp'] original_event = raw_edf.find_edf_events() annot_list = list(zip(original_event[1], original_event[4], original_event[2])) # Remove rejected trials from events if drop_rejects: annot_list = pd.DataFrame(annot_list) rejected = annot_list[0].isin(annot_list[annot_list[2] == 1023][0]) accepted_trials_index = [True] * 288 ind=-1 for row in annot_list.itertuples(): if row[3] == 1023: rejected.loc[row[0]+1] = True accepted_trials_index[ind] = False if row[3] == 768: ind = ind + 1 annot_list = annot_list[~rejected] annot_list = list(zip(annot_list[0], annot_list[1], annot_list[2])) events_from_edf.extend(annot_list) events_from_edf = np.array(events_from_edf) events_arr = np.zeros(events_from_edf.shape, dtype=int) for (i, i_event) in enumerate(events_from_edf): index = int((float(i_event[0])) * sampling_frequency) events_arr[i,:] = index,0,stim_code[int(i_event[2])] i=i+1 # strip channel names of "." characters raw_edf.rename_channels(lambda x: x.strip('.')) #create Event dictionary based on File events_in_edf = [event[2] for event in events_arr[:]] if(events_in_edf.__contains__(2)): event_id['LEFT_HAND'] = 2 if (events_in_edf.__contains__(3)): event_id['RIGHT_HAND'] = 3 if (events_in_edf.__contains__(4)): event_id['FEET'] = 4 if (events_in_edf.__contains__(5)): event_id['TONGUE'] = 5 if (events_in_edf.__contains__(6)): event_id['CUE_UNKNOWN'] = 6 # Read epochs (train will be done only between -0.5 and 4s) # Testing will be done with a running classifier # raw_edf.filter(0., 40., fir_design='firwin', skip_by_annotation='edge') # 4-40Hz picks = pick_types(raw_edf.info, meg=False, eeg=True, stim=False, eog=False, exclude='bads') epochs = Epochs(raw_edf, events_arr, event_id, tmin, tmax, proj=True, picks=picks, baseline=None, preload=True) y = epochs.events[:, 2] - 2 filter_data = [] #filter_bank = [(4.,40.)] filter_bank = [(4.,8.),(8.,12.),(12.,16.),(16.,20.),(20.,24.),(24.,28.),(28.,32.),(32.,36.),(36.,40)] for _filter in filter_bank: #filter_data.append(np.abs(signal.hilbert(epochs.copy().filter(_filter[0], _filter[1], fir_design='firwin').get_data()))) filter_data.append(epochs.copy().filter(_filter[0], _filter[1], fir_design='firwin').get_data()) filter_data = np.array(filter_data) if training: oScaler = Scaler(scalings='mean').fit(filter_data.flatten().reshape(-1,1)) #oScaler = MinMaxScaler(copy=True, feature_range=(-1, 1)).fit(filter_data.flatten().reshape(-1,1)) pk.dump(oScaler,open("./fb/subject{}_filter_oscaler.pk".format(subjects[subj]),'wb')) else: oScaler = pk.load(open("./fb/subject{}_filter_oscaler.pk".format(subjects[subj]),'rb')) shape = filter_data.shape filter_data = oScaler.transform(filter_data.flatten().reshape(-1,1)) filter_data = filter_data.reshape(shape) filter_data = filter_data.transpose(1,3,2,0) # 273, 1001, 22, 10 # Augment and reshape data into image filter_data = filter_data.transpose(2,0,1,3) # 22, 273, 1001, 10 filter_data = np.split(filter_data,[1,6,13,18,21]) empty_ch = np.zeros(filter_data[0].shape) filter_data = np.vstack([empty_ch,empty_ch,empty_ch,filter_data[0],empty_ch,empty_ch,empty_ch, empty_ch,filter_data[1],empty_ch, filter_data[2], empty_ch,filter_data[3],empty_ch, empty_ch,empty_ch,filter_data[4],empty_ch,empty_ch, empty_ch,empty_ch,empty_ch,filter_data[5],empty_ch,empty_ch,empty_ch]) filter_data = filter_data.transpose(1,2,0,3) # 273, 1001, 42, 10 filter_data = filter_data.reshape(filter_data.shape[0],filter_data.shape[1],6,7,filter_data.shape[3]) # 273, 1001, 6, 7, 10 if training: return filter_data, y else: y = sio.loadmat(label_files_list[label_subj[subj]])['classlabel'].flatten() y = np.array([ i - 1 for i in y ]) if drop_rejects: y_drop = [ i for i in range(288) if not accepted_trials_index[i] ] y = np.delete(y, y_drop, None) return filter_data, y
def decoding_withKfold(X, Y_speech, Y_lips, n_fold, train_index, test_index, examples, feature): predictions_speech = np.zeros((Y_speech.shape)) speech = np.zeros((Y_speech.shape)) predictions_lips = np.zeros((Y_lips.shape)) lips = np.zeros((Y_lips.shape)) scores_speech = np.zeros((n_fold, )) for k in range(0, n_fold): eegScaler = MultiChannelScaler(scalings='mean') speechScaler = MultiChannelScaler(scalings='mean') lipsScaler = MultiChannelScaler(scalings='mean') speechModel = LReg() lipsModel = LReg() #####COPY X AND Y VARIABLES X_standard = np.zeros((X.shape)) Y_lips_standard = np.zeros((Y_lips.shape)) Y_speech_standard = np.zeros((Y_speech.shape)) # standardazing data X_standard[train_index[k], :, :] = eegScaler.fit_transform( X[train_index[k], :, :]) X_standard[test_index[k], :, :] = eegScaler.transform( X[test_index[k], :, :]) Y_lips_standard[train_index[k], :] = lipsScaler.fit_transform( Y_lips[train_index[k], :]).squeeze() Y_lips_standard[test_index[k], :] = lipsScaler.transform( Y_lips[test_index[k], :]).squeeze() Y_speech_standard[train_index[k], :] = speechScaler.fit_transform( Y_speech[train_index[k], :]).squeeze() Y_speech_standard[test_index[k], :] = speechScaler.transform( Y_speech[test_index[k], :]).squeeze() X_TRAIN = X_standard[train_index[k], :, :] X_TEST = X_standard[test_index[k], :, :] Y_envelope_sp_TRAIN = Y_speech_standard[train_index[k], :] Y_envelope_sp_TEST = Y_speech_standard[test_index[k], :] Y_lips_ap_TRAIN = Y_lips_standard[train_index[k], :] Y_lips_ap_TEST = Y_lips_standard[test_index[k], :] #X_train and test now are (#trials,#channnels,#timepoints) n_trial = X_TRAIN.shape[0] n_trial_test = X_TEST.shape[0] n_ch = X_TRAIN.shape[1] trial_length = X_TRAIN.shape[2] if examples == 'are_Trials': X_TRAIN_tmp = np.zeros((X_TRAIN.shape[0], n_ch * trial_length)) X_TEST_tmp = np.zeros((X_TEST.shape[0], n_ch * trial_length)) for i in range(0, n_ch): X_TRAIN_tmp[:, i * trial_length:(i + 1) * trial_length] = X_TRAIN[:, i, :] X_TEST_tmp[:, i * trial_length:(i + 1) * trial_length] = X_TEST[:, i, :] X_TRAIN = X_TRAIN_tmp X_TEST = X_TEST_tmp elif examples == 'are_Time': X_TRAIN_tmp = np.zeros((n_trial * trial_length, n_ch)) X_TEST_tmp = np.zeros((n_trial_test * trial_length, n_ch)) Y_envelope_sp_TRAIN_tmp = np.zeros((n_trial * trial_length, )) Y_envelope_sp_TEST_tmp = np.zeros((n_trial_test * trial_length, )) Y_lips_ap_TRAIN_tmp = np.zeros((n_trial * trial_length, )) Y_lips_ap_TEST_tmp = np.zeros((n_trial_test * trial_length, )) for i in range(0, n_trial): X_TRAIN_tmp[i * trial_length:(i + 1) * trial_length, :] = X_TRAIN[i, :, :].T Y_envelope_sp_TRAIN_tmp[i * trial_length:(i + 1) * trial_length] = Y_envelope_sp_TRAIN[ i, :] Y_lips_ap_TRAIN_tmp[i * trial_length:(i + 1) * trial_length] = Y_lips_ap_TRAIN[i, :] if i < X_TEST.shape[0]: #test trials are less than train X_TEST_tmp[i * trial_length:(i + 1) * trial_length, :] = X_TEST[i, :, :].T Y_envelope_sp_TEST_tmp[i * trial_length:(i + 1) * trial_length] = Y_envelope_sp_TEST[ i, :] Y_lips_ap_TEST_tmp[i * trial_length:(i + 1) * trial_length] = Y_lips_ap_TEST[i, :] X_TRAIN = X_TRAIN_tmp X_TEST = X_TEST_tmp Y_envelope_sp_TRAIN = Y_envelope_sp_TRAIN_tmp Y_envelope_sp_TEST = Y_envelope_sp_TEST_tmp Y_lips_ap_TRAIN = Y_lips_ap_TRAIN_tmp Y_lips_ap_TEST = Y_lips_ap_TEST_tmp if feature == 'pca': [pca, n_comp] = pca_decomposition(X_TRAIN) X_TRAIN = pca.transform(X_TRAIN)[:, :n_comp] X_TEST = pca.transform(X_TEST)[:, :n_comp] if feature == 'Kpca': [pca, n_comp] = kernel_pca_decomposition(X_TRAIN) X_TRAIN = pca.transform(X_TRAIN)[:, :n_comp] X_TEST = pca.transform(X_TEST)[:, :n_comp] if feature == 'ica': ICA_decomposition [ica, selected_comps] = ICA_decomposition(X_TRAIN) X_TRAIN = ica.transform(X_TRAIN)[:, selected_comps.astype('int')] X_TEST = ica.transform(X_TEST)[:, selected_comps.astype('int')] if feature == 'derivative1': de1 = np.diff(X_TRAIN, axis=0) / 0.01 de1 = np.concatenate((np.zeros((1, de1.shape[1])), de1), axis=0) for i in range(0, de1.shape[0], trial_length): de1[i, :] = np.zeros((1, de1.shape[1])) X_TRAIN = np.concatenate((X_TRAIN, de1), 1) de1 = np.diff(X_TEST, axis=0) / 0.01 de1 = np.concatenate((np.zeros((1, de1.shape[1])), de1), axis=0) for i in range(0, de1.shape[0], trial_length): de1[i, :] = np.zeros((1, de1.shape[1])) X_TEST = np.concatenate((X_TEST, de1), 1) if feature == 'derivative2': de1 = np.diff(X_TRAIN, axis=0) / 0.01 de1 = np.concatenate((np.zeros((1, de1.shape[1])), de1), axis=0) for i in range(0, de1.shape[0], trial_length): de1[i, :] = np.zeros((1, de1.shape[1])) de2 = np.diff(de1, axis=0) de2 = np.concatenate((np.zeros((1, de2.shape[1])), de2), axis=0) for i in range(0, de2.shape[0], trial_length): de2[i, :] = np.zeros((1, de2.shape[1])) de2[i + 1, :] = np.zeros((1, de2.shape[1])) X_TRAIN = np.concatenate((np.concatenate( (X_TRAIN, de1), 1), de2), 1) de1 = np.diff(X_TEST, axis=0) / 0.01 de1 = np.concatenate((np.zeros((1, de1.shape[1])), de1), axis=0) for i in range(0, de1.shape[0], trial_length): de1[i, :] = np.zeros((1, de1.shape[1])) de2 = np.diff(de1, axis=0) de2 = np.concatenate((np.zeros((1, de2.shape[1])), de2), axis=0) for i in range(0, de2.shape[0], trial_length): de2[i, :] = np.zeros((1, de2.shape[1])) de2[i + 1, :] = np.zeros((1, de2.shape[1])) X_TEST = np.concatenate((np.concatenate( (X_TEST, de1), 1), de2), 1) if feature == 'polynomial': X_TRAIN = np.concatenate((X_TRAIN, np.power(X_TRAIN, 2)), 1) X_TEST = np.concatenate((X_TEST, np.power(X_TEST, 2)), 1) # training models and predict speechModel.fit(X_TRAIN, Y_envelope_sp_TRAIN) lipsModel.fit(X_TRAIN, Y_lips_ap_TRAIN) reconstructed_speech = speechModel.predict(X_TEST) reconstructed_lips = lipsModel.predict(X_TEST) if examples == 'are_Time': reconstructed_speech_tmp = np.zeros((n_trial_test, trial_length)) reconstructed_lips_tmp = np.zeros((n_trial_test, trial_length)) Y_envelope_sp_TEST_tmp = np.zeros((n_trial_test, trial_length)) Y_lips_ap_TEST_tmp = np.zeros((n_trial_test, trial_length)) t = 0 for i in range(0, len(reconstructed_speech), trial_length): reconstructed_speech_tmp[ t, :] = reconstructed_speech[i:i + trial_length] reconstructed_lips_tmp[t, :] = reconstructed_lips[i:i + trial_length] Y_envelope_sp_TEST_tmp[t, :] = Y_envelope_sp_TEST[i:i + trial_length] Y_lips_ap_TEST_tmp[t, :] = Y_lips_ap_TEST[i:i + trial_length] t += 1 reconstructed_speech = reconstructed_speech_tmp reconstructed_lips = reconstructed_lips_tmp Y_envelope_sp_TEST = Y_envelope_sp_TEST_tmp Y_lips_ap_TEST = Y_lips_ap_TEST_tmp predictions_speech[test_index[k], :] = reconstructed_speech speech[test_index[k], :] = Y_envelope_sp_TEST predictions_lips[test_index[k], :] = reconstructed_lips lips[test_index[k], :] = Y_lips_ap_TEST # computing scores speech_score = evaluate(speech.T, predictions_speech.T, 'corrcoeff') lips_score = evaluate(lips.T, predictions_lips.T, 'corrcoeff') return speech_score, lips_score, predictions_speech, predictions_lips, speech, lips
def decoding(band,regularization,tmin,tmax,n_fold,subject_name, savepath): data_path = "./ProcessedData/Final_" eeg="_processed-epo.fif" features="_Features-epo.fif" sfreq=100 n_delays = int((tmax - tmin) * sfreq) + 1 T= [51, 61, 71, 81, 91, 101, 111, 121, 131, 141, 151] results_speech= np.zeros((len(regularization),len(T)))# each raw is the results' vector for one regularization parameter results_lips= np.zeros((len(regularization),len(T)))# each raw is the results' vector for one regularization parameter results_speech_all_sub={} results_lips_all_sub={} predictions_lips_all_sub={} predictions_speech_all_sub={} for s in subject_name: print('subject '+str(s)) X_orig = use_FreqBand(mne.read_epochs(data_path+s+eeg),band) Features_orig = use_FreqBand(mne.read_epochs(data_path + s + features),band) if band=='original': X_orig=X_orig.get_data() # 3d array (N_trial, N_channel, N_time) Y_envelope_sp_orig=Features_orig.get_data()[:,0,:] # 2d array (N_trial, N_time) Y_lips_ap_orig=Features_orig.get_data()[:,2,:] # 2d array (N_trial, N_time) else: X_orig= np.mean(X_orig.data,2) # 3d array (N_trial, N_channel, N_time) #averaging power across frequencies Y_envelope_sp_orig=np.mean(Features_orig.data[:,0,:,:],1) Y_lips_ap_orig=np.mean(Features_orig.data[:,2,:,:],1) time = mne.read_epochs(data_path + s + features).times # 1d array (N_time) channels = mne.read_epochs(data_path + s + eeg).ch_names predictions_speech = np.zeros((Y_envelope_sp_orig.shape[0], 200, len(T),len(regularization))) predictions_lips = np.zeros((Y_lips_ap_orig.shape[0],200,len(T),len(regularization))) train_index, test_index = k_fold(Y_envelope_sp_orig,n_fold) # define index for train and test for each of the k folds #data standardizers eegScaler= Scaler(scalings='mean') speechScaler= Scaler(scalings='mean') lipsScaler = Scaler(scalings='mean') scores_speech = np.zeros((n_fold,)) scores_lips = np.zeros((n_fold,)) coefs_speech = np.zeros((n_fold, X_orig.shape[1], n_delays)) patterns_speech = coefs_speech.copy() coefs_lips = np.zeros((n_fold, X_orig.shape[1], n_delays)) patterns_lips = coefs_lips.copy() for i, r in enumerate(regularization): rf_speech = RField(tmin, tmax, sfreq, feature_names=channels, scoring='r2', patterns=True, estimator=r) rf_lips = RField(tmin, tmax, sfreq, feature_names=channels, scoring='r2', patterns=True, estimator=r) print('reg parameter #'+str(i)) for j, t_start in enumerate(T): ##estracting the temporal interval of interest t_end= t_start+200 X = X_orig[:,:,t_start:t_end] #only the eeg window is shifting Y_envelope_sp = Y_envelope_sp_orig[:,101:301] Y_lips_ap = Y_lips_ap_orig[:,101:301] for k in range(0,n_fold): #####COPY X AND Y VARIABLES X_standard=np.zeros((X.shape)) Y_lips_ap_standard=np.zeros((Y_lips_ap.shape)) Y_envelope_sp_standard = np.zeros((Y_envelope_sp.shape)) #standardazing data X_standard[train_index[k], :, :] = eegScaler.fit_transform(X[train_index[k], :, :]) X_standard[test_index[k], :, :] = eegScaler.transform(X[test_index[k], :, :]) Y_lips_ap_standard[train_index[k], :] = lipsScaler.fit_transform(Y_lips_ap[train_index[k], :])[:,:,0] Y_lips_ap_standard[test_index[k], :] = lipsScaler.transform(Y_lips_ap[test_index[k], :])[:,:,0] Y_envelope_sp_standard[train_index[k], :] = speechScaler.fit_transform(Y_envelope_sp[train_index[k], :])[:,:,0] Y_envelope_sp_standard[test_index[k], :] = speechScaler.transform(Y_envelope_sp[test_index[k], :])[:,:,0] #shaping data as desired by the decoding model (receptive field function) X_standard = np.rollaxis(X_standard, 2, 0) Y_envelope_sp_standard = np.rollaxis(Y_envelope_sp_standard, 1, 0) Y_lips_ap_standard = np.rollaxis(Y_lips_ap_standard, 1, 0) X_TRAIN= X_standard[:,train_index[k],:] X_TEST= X_standard[:,test_index[k],:] Y_envelope_sp_TRAIN = Y_envelope_sp_standard[:,train_index[k]] Y_envelope_sp_TEST = Y_envelope_sp_standard[:,test_index[k]] Y_lips_ap_TRAIN = Y_lips_ap_standard[:,train_index[k]] Y_lips_ap_TEST = Y_lips_ap_standard[:,test_index[k]] #training models and predict rf_speech.fit(X_TRAIN,Y_envelope_sp_TRAIN) rf_lips.fit(X_TRAIN,Y_lips_ap_TRAIN) reconstructed_speech = rf_speech.predict(X_TEST) reconstructed_lips = rf_lips.predict(X_TEST) predictions_speech[test_index[k],:,j,i]=reconstructed_speech.T predictions_lips[test_index[k],:,j,i]=reconstructed_lips.T #computing scores tmp_score_speech=0 tmp_score_lips = 0 for n, rec in enumerate(reconstructed_speech[:,:,0].T): tmp_score_speech = tmp_score_speech + mean_squared_error(Y_envelope_sp_TEST[:,n]/max(abs(Y_envelope_sp_TEST[:,n])), rec/max(abs(rec))) scores_speech[k]= tmp_score_speech/(n+1) for n, rec in enumerate(reconstructed_lips[:,:,0].T): tmp_score_lips = tmp_score_lips + mean_squared_error(Y_lips_ap_TEST[:, n]/max(abs(Y_lips_ap_TEST[:, n])), rec/max(abs(rec))) scores_lips[k] = tmp_score_lips / (n+1) # scores_speech[k] = rf_speech.score(X_TEST,Y_envelope_sp_TEST)[0] # scores_lips[k] = rf_speech.score(X_TEST,Y_lips_ap_TEST)[0] ##coef_ is shape (n_outputs, n_features, n_delays). # coefs_speech[k] = rf_speech.coef_[0, :, :] # patterns_speech[k] = rf_speech.patterns_[0, :, :] # coefs_lips[k] = rf_lips.coef_[0, :, :] # patterns_lips[k] = rf_lips.patterns_[0, :, :] # mean_coefs_lips = coefs_lips.mean(axis=0) # mean_patterns_lips = patterns_lips.mean(axis=0) mean_scores_lips = scores_lips.mean(axis=0) # mean_coefs_speech = coefs_speech.mean(axis=0) # mean_patterns_speech = patterns_speech.mean(axis=0) mean_scores_speech = scores_speech.mean(axis=0) #saving results for the i-th reg parameter and j-th time lag results_speech[i, j] = mean_scores_speech results_lips[i, j] = mean_scores_lips results_speech_all_sub[s]=results_speech.copy() results_lips_all_sub[s]=results_lips.copy() predictions_speech_all_sub[s]=predictions_speech.copy() predictions_lips_all_sub[s]=predictions_lips.copy() np.save(savepath+'/results_speech_all_sub',results_speech_all_sub) np.save(savepath+'/results_lips_all_sub',results_lips_all_sub) np.save(savepath+'/predictions_speech_all_sub',predictions_speech_all_sub) np.save(savepath+'/predictions_lips_all_sub',predictions_lips_all_sub) tmp_results_speech = [] tmp_results_lips = [] for N, s in enumerate(subject_name): if N ==0: tmp_results_speech= np.asarray(results_speech_all_sub[s]) tmp_results_lips= np.asarray(results_lips_all_sub[s]) tmp_results_speech=np.dstack((tmp_results_speech, np.asarray(results_speech_all_sub[s]))) tmp_results_lips=np.dstack((tmp_results_lips,np.asarray(results_lips_all_sub[s]))) # computing grand average and standard deviation for each time lag GAVG_sp = np.reshape(np.mean(tmp_results_speech,2),(len(regularization),11)) GAVG_lip = np.reshape(np.mean(tmp_results_lips,2),(len(regularization),11)) GAVG_sp_std = np.reshape(np.std(tmp_results_speech,2),(len(regularization),11)) GAVG_lip_std = np.reshape(np.std(tmp_results_lips,2),(len(regularization),11)) np.save(savepath+'/GAVG_sp',GAVG_sp) np.save(savepath+'/GAVG_lip',GAVG_lip) np.save(savepath+'/GAVG_sp_std',GAVG_sp_std) np.save(savepath+'/GAVG_lip_std',GAVG_lip_std) ####PLOTTING RESULTS##### T = np.reshape(T, (1, len(T))) pp.figure(0) for n, r in enumerate(regularization): pp.errorbar((T[0,:] - 100) * 10, GAVG_sp[n,:], yerr=GAVG_sp_std[n,:]) pp.legend(regularization) pp.title('speech MSE') sfig=savepath+'/GAVG_specch.png' pp.savefig(fname=sfig) pp.figure(1) for n, r in enumerate(regularization): pp.errorbar((T[0, :] - 100) * 10, GAVG_lip[n, :], yerr=GAVG_lip_std[n, :]) pp.legend(regularization) pp.title('lips MSE') sfig = savepath +'/GAVG_lips.png' pp.savefig(fname=sfig) #pp.show() print('bla')
def raw_to_data(raw_edf, training=False, drop_rejects=True, subj=None): tmin, tmax = -0.5, 4. X, y = [], [] stim_code = dict([(32766, 1), (769, 2), (770, 3), (771, 4), (772, 5), (783, 6), (276, 7), (277, 8), (768, 9), (1023, 10), (1072, 11)]) if training: path = op.join('data_i2r', 'BCI_IV_2a', 'TrainingSet') if not training: path = op.join('data_i2r', 'BCI_IV_2a', 'TestingSet') label_path = op.join('data_i2r', 'BCI_IV_2a', 'true_labels') label_files_list = glob.glob(label_path + '/*E.mat') label_subj = [int(f.split('A0')[1][0]) for f in label_files_list] file_list = glob.glob(path + '/*.gdf') subjects = [int(f.split('A0')[1][0]) for f in file_list] if not training: label_subj = [ np.argwhere(np.array(label_subj) == subjects[i])[0][0] for i in range(len(subjects)) ] event_id = dict() events_from_edf = [] sampling_frequency = raw_edf._raw_extras[0]['max_samp'] original_event = raw_edf.find_edf_events() annot_list = list( zip(original_event[1], original_event[4], original_event[2])) # Remove rejected trials from events if drop_rejects: annot_list = pd.DataFrame(annot_list) rejected = annot_list[0].isin(annot_list[annot_list[2] == 1023][0]) accepted_trials_index = [True] * 288 ind = -1 for row in annot_list.itertuples(): if row[3] == 1023: rejected.loc[row[0] + 1] = True accepted_trials_index[ind] = False if row[3] == 768: ind = ind + 1 annot_list = annot_list[~rejected] annot_list = list(zip(annot_list[0], annot_list[1], annot_list[2])) events_from_edf.extend(annot_list) events_from_edf = np.array(events_from_edf) events_arr = np.zeros(events_from_edf.shape, dtype=int) for (i, i_event) in enumerate(events_from_edf): index = int((float(i_event[0])) * sampling_frequency) events_arr[i, :] = index, 0, stim_code[int(i_event[2])] i = i + 1 # strip channel names of "." characters raw_edf.rename_channels(lambda x: x.strip('.')) #create Event dictionary based on File events_in_edf = [event[2] for event in events_arr[:]] if (events_in_edf.__contains__(2)): event_id['LEFT_HAND'] = 2 if (events_in_edf.__contains__(3)): event_id['RIGHT_HAND'] = 3 if (events_in_edf.__contains__(4)): event_id['FEET'] = 4 if (events_in_edf.__contains__(5)): event_id['TONGUE'] = 5 if (events_in_edf.__contains__(6)): event_id['CUE_UNKNOWN'] = 6 # Apply band-pass filter raw_edf.filter(0., 38., fir_design='firwin', skip_by_annotation='edge') # 4-40Hz picks = pick_types(raw_edf.info, meg=False, eeg=True, stim=False, eog=False, exclude='bads') # Read epochs (train will be done only between -0.5 and 4s) # Testing will be done with a running classifier epochs = Epochs(raw_edf, events_arr, event_id, tmin, tmax, proj=True, picks=picks, baseline=None, preload=True) X = epochs.get_data().transpose(0, 2, 1) X_shape = X.shape if training: scaler = Scaler(scalings='median').fit(X.flatten().reshape(-1, 1)) #scaler = MinMaxScaler(copy=True, feature_range=(-1, 1)).fit(X.flatten().reshape(-1,1)) pk.dump( scaler, open( "./shallow_convnet/subject{}_oscaler.pk".format( subjects[subj]), 'wb')) else: scaler = pk.load( open( "./shallow_convnet/subject{}_oscaler.pk".format( subjects[subj]), 'rb')) y = epochs.events[:, 2] - 2 X = scaler.transform(X.flatten().reshape(-1, 1)) X = X.reshape(X_shape) if training: return X, y, scaler else: y = sio.loadmat( label_files_list[label_subj[subj]])['classlabel'].flatten() y = np.array([i - 1 for i in y]) if drop_rejects: y_drop = [i for i in range(288) if not accepted_trials_index[i]] y = np.delete(y, y_drop, None) return X, y
def decoding_withKfold(X,Y_speech,Y_lips,n_fold,train_index,test_index,polynomialReg): predictions_speech= np.zeros((Y_speech.shape)) speech = np.zeros((Y_speech.shape)) predictions_lips= np.zeros((Y_lips.shape)) lips = np.zeros((Y_lips.shape)) scores_speech=np.zeros((n_fold,)) for k in range(0, n_fold): eegScaler = Scaler() speechScaler = Scaler() lipsScaler = Scaler() speechModel = LReg() lipsModel = LReg() #####COPY X AND Y VARIABLES X_standard = np.zeros((X.shape)) Y_lips_standard = np.zeros((Y_lips.shape)) Y_speech_standard = np.zeros((Y_speech.shape)) # standardazing data X_standard[train_index[k], :] = eegScaler.fit_transform(X[train_index[k], :]) X_standard[test_index[k], :] = eegScaler.transform(X[test_index[k], :]) Y_lips_standard[train_index[k], :] = lipsScaler.fit_transform(Y_lips[train_index[k], :]) Y_lips_standard[test_index[k], :] = lipsScaler.transform(Y_lips[test_index[k], :]) Y_speech_standard[train_index[k], :] = speechScaler.fit_transform(Y_speech[train_index[k], :]) Y_speech_standard[test_index[k], :] = speechScaler.transform(Y_speech[test_index[k], :]) X_TRAIN = X_standard[ train_index[k], :] X_TEST = X_standard[ test_index[k], :] Y_envelope_sp_TRAIN = Y_speech_standard[train_index[k], :] Y_envelope_sp_TEST = Y_speech_standard[test_index[k], :] Y_lips_ap_TRAIN = Y_lips_standard[train_index[k], :] Y_lips_ap_TEST = Y_lips_standard[test_index[k], :] if polynomialReg == True: X_TRAIN= np.concatenate((X_TRAIN,np.power(X_TRAIN,2)),1) X_TEST = np.concatenate((X_TEST, np.power(X_TEST, 2)), 1) # training models and predict speechModel.fit(X_TRAIN, Y_envelope_sp_TRAIN) lipsModel.fit(X_TRAIN, Y_lips_ap_TRAIN) reconstructed_speech = speechModel.predict(X_TEST) reconstructed_lips = lipsModel.predict(X_TEST) predictions_speech[test_index[k], :] = reconstructed_speech speech[test_index[k], :] = Y_envelope_sp_TEST predictions_lips[test_index[k], :] = reconstructed_lips lips[test_index[k], :] = Y_lips_ap_TEST # computing scores speech_score = evaluate(speech.T, predictions_speech.T, 'corrcoeff') lips_score = evaluate(lips.T, predictions_lips.T, 'corrcoeff') return speech_score, lips_score, predictions_speech, predictions_lips, speech, lips