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, preload=False, add_eeg_ref=False) 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, add_eeg_ref=False) 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_equal(epochs_data, Xi) # 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.Raw(raw_fname, preload=False) 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_equal(epochs_data, Xi) # 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() y = epochs.events[:, -1] methods = (None, dict(mag=5, grad=10, eeg=20), 'mean', 'median') infos = (epochs.info, epochs.info, None, None) epochs_data_t = epochs_data.transpose([1, 0, 2]) for method, info in zip(methods, infos): if method == 'median' and not check_version('sklearn', '0.17'): assert_raises(ValueError, Scaler, info, method) continue if method == 'mean' and not check_version('sklearn', ''): assert_raises(ImportError, Scaler, info, method) continue scaler = Scaler(info, method) X = scaler.fit_transform(epochs_data, y) assert_equal(X.shape, epochs_data.shape) if method is None or isinstance(method, dict): sd = DEFAULTS['scalings'] if method is None else method stds = np.zeros(len(picks)) for key in ('mag', 'grad'): stds[pick_types(epochs.info, meg=key)] = 1. / sd[key] stds[pick_types(epochs.info, meg=False, eeg=True)] = 1. / sd['eeg'] means = np.zeros(len(epochs.ch_names)) elif method == 'mean': stds = np.array([np.std(ch_data) for ch_data in epochs_data_t]) means = np.array([np.mean(ch_data) for ch_data in epochs_data_t]) else: # median percs = np.array([np.percentile(ch_data, [25, 50, 75]) for ch_data in epochs_data_t]) stds = percs[:, 2] - percs[:, 0] means = percs[:, 1] assert_allclose(X * stds[:, np.newaxis] + means[:, np.newaxis], epochs_data, rtol=1e-12, atol=1e-20, err_msg=method) X2 = scaler.fit(epochs_data, y).transform(epochs_data) assert_array_equal(X, X2) # inverse_transform Xi = scaler.inverse_transform(X) assert_array_almost_equal(epochs_data, Xi) # Test init exception assert_raises(ValueError, Scaler, None, None) assert_raises(ValueError, scaler.fit, epochs, y) assert_raises(ValueError, scaler.transform, epochs) epochs_bad = Epochs(raw, events, event_id, 0, 0.01, picks=np.arange(len(raw.ch_names))) # non-data chs scaler = Scaler(epochs_bad.info, None) assert_raises(ValueError, scaler.fit, epochs_bad.get_data(), y)
def standard_scaling(data, scalings="mean", log=False): if log: data = np.log(data + np.finfo(np.float32).eps) if scalings in ["mean", "median"]: scaler = Scaler(scalings=scalings) data = scaler.fit_transform(data) else: raise ValueError("scalings should be mean or median") return data
# downsample if necessary if epo.info['sfreq'] != param['testresampfreq']: epo = epo.resample(param['testresampfreq']) # Drop bad trials and get indices goodtrials = np.where(df['badtrial'] == 0)[0] # Get external data for this part df = df.iloc[goodtrials] epo = epo[goodtrials] # Standardize data before regression scale = Scaler(scalings='mean') # Says mean but is z score, see docs epo_z = mne.EpochsArray(scale.fit_transform(epo.get_data()), epo.info) betasnp = [] for idx, regvar in enumerate(regvars): # Standardize data df[regvar + '_z'] = scipy.stats.zscore(df[regvar]) epo.metadata = df.assign(Intercept=1) # Add an intercept for later # Perform regression names = ["Intercept"] + [regvar + '_z'] res = mne.stats.linear_regression(epo_z, epo.metadata[names], names=names) # Collect betas
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 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
def test_scaler(info, method): """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() y = epochs.events[:, -1] epochs_data_t = epochs_data.transpose([1, 0, 2]) if method in ('mean', 'median'): if not check_version('sklearn'): with pytest.raises(ImportError, match='No module'): Scaler(info, method) return if check_version('sklearn', '1.0'): # 1.0.dev0 is a problem pending # https://github.com/scikit-learn/scikit-learn/issues/19726 pytest.skip('Bug on sklear main as of 2021/03/19') if info: info = epochs.info scaler = Scaler(info, method) X = scaler.fit_transform(epochs_data, y) assert_equal(X.shape, epochs_data.shape) if method is None or isinstance(method, dict): sd = DEFAULTS['scalings'] if method is None else method stds = np.zeros(len(picks)) for key in ('mag', 'grad'): stds[pick_types(epochs.info, meg=key)] = 1. / sd[key] stds[pick_types(epochs.info, meg=False, eeg=True)] = 1. / sd['eeg'] means = np.zeros(len(epochs.ch_names)) elif method == 'mean': stds = np.array([np.std(ch_data) for ch_data in epochs_data_t]) means = np.array([np.mean(ch_data) for ch_data in epochs_data_t]) else: # median percs = np.array([ np.percentile(ch_data, [25, 50, 75]) for ch_data in epochs_data_t ]) stds = percs[:, 2] - percs[:, 0] means = percs[:, 1] assert_allclose(X * stds[:, np.newaxis] + means[:, np.newaxis], epochs_data, rtol=1e-12, atol=1e-20, err_msg=method) X2 = scaler.fit(epochs_data, y).transform(epochs_data) assert_array_equal(X, X2) # inverse_transform Xi = scaler.inverse_transform(X) assert_array_almost_equal(epochs_data, Xi) # Test init exception pytest.raises(ValueError, Scaler, None, None) pytest.raises(TypeError, scaler.fit, epochs, y) pytest.raises(TypeError, scaler.transform, epochs) epochs_bad = Epochs(raw, events, event_id, 0, 0.01, baseline=None, picks=np.arange(len(raw.ch_names))) # non-data chs scaler = Scaler(epochs_bad.info, None) pytest.raises(ValueError, scaler.fit, epochs_bad.get_data(), y)