def processInput(kk, num_of_trials, cov_u, dim, vnum, bound): pearson_corr = [] pearson_corr_riemann = [] pearson_corr_eucl = [] covars = [] np.random.seed(kk) for k in range(0, num_of_trials): for i in range(0, vnum): # ---construct view----# LambdaMatrix = _draw_eigenvalues(dim=2, b=bound, overlap=False) rotMatrix = _draw_rotation_matrix(dim=dim) cov = np.add( cov_u, rotMatrix.dot(LambdaMatrix.dot(rotMatrix.transpose()))) covars.append(cov) # --------------------------------------# est_cov_u = est_common_cov(covars, outliers=0) pearson_corr.append( est_cov_u[1, 0] / (math.sqrt(est_cov_u[0, 0]) * math.sqrt(est_cov_u[1, 1]))) riemann_mean = mean_covariance(np.array(covars), metric='riemann', sample_weight=None) eucl_mean = mean_covariance(np.array(covars), metric='euclid', sample_weight=None) pearson_corr_riemann.append( riemann_mean[1, 0] / (math.sqrt(riemann_mean[0, 0]) * math.sqrt(riemann_mean[1, 1]))) pearson_corr_eucl.append( eucl_mean[1, 0] / (math.sqrt(eucl_mean[0, 0]) * math.sqrt(eucl_mean[1, 1]))) return pearson_corr, pearson_corr_riemann, pearson_corr_eucl
def fit(self, X, y, X_domain, y_domain, sample_weight=None): """Fit (estimates) the centroids. Parameters ---------- X : ndarray, shape (n_trials, n_channels, n_channels) ndarray of SPD matrices. y : ndarray shape (n_trials, 1) labels corresponding to each trial. sample_weight : None | ndarray shape (n_trials, 1) the weights of each sample from the domain. if None, each sample is treated with equal weights. Returns ------- self : MDM instance The MDM instance. """ self.classes_ = np.unique(y) # TODO: ajouter un test pour verifier que y et y_domain # ont les meme classes if sample_weight is None: sample_weight = np.ones(X_domain.shape[0]) if self.n_jobs == 1: self.target_means_ = [ mean_covariance(X[y == label], metric=self.metric_mean) # sample_weight=sample_weight_target[y == l]) for label in self.classes_ ] self.domain_means_ = [ mean_covariance( X_domain[y_domain == label], metric=self.metric_mean, sample_weight=sample_weight[y_domain == label], ) for label in self.classes_ ] else: self.target_means_ = Parallel(n_jobs=self.n_jobs)( delayed(mean_covariance)(X[y == label], metric=self.metric_mean) for label in self.classes_) # sample_weight=sample_weight_target[y == l]) self.domain_means_ = Parallel(n_jobs=self.n_jobs)( delayed(mean_covariance)( X_domain[y_domain == label], metric=self.metric_mean, sample_weight=sample_weight[y_domain == label], ) for label in self.classes_) self.class_center_ = [ geodesic(self.target_means_[i], self.domain_means_[i], self.L, self.metric) for i, _ in enumerate(self.classes_) ] return self
def test_mean_covariance_metric(metric, mean, get_covmats): """Test mean_covariance for metric""" n_matrices, n_channels = 3, 3 covmats = get_covmats(n_matrices, n_channels) C = mean_covariance(covmats, metric=metric) Ctrue = mean(covmats) assert np.all(C == Ctrue)
def fit(self, X, y, centroids=None, T=0, NT=1, sample_weight=None): """Fit (estimates) the centroids and the parameters of the R_TNT distribution. Parameters ---------- X : ndarray, shape (n_trials, n_channels, n_channels) ndarray of SPD matrices. y : ndarray shape (n_trials, 1) labels corresponding to each trial. T,NT : name of the label TARGET and the label NONTARGET given in y sample_weight : None | ndarray shape (n_trials, 1) the weights of each sample. if None, each sample is treated with equal weights. Returns ------- self : Bayes_R_TNT instance The Bayes_R_TNT instance. """ self.classes = [T, NT] # self.centroids = [] if sample_weight is None: sample_weight = np.ones(X.shape[0]) # for l in self.classes: # self.centroids.append( # mean_covariance(X[y == l], metric=self.metric_mean, # sample_weight=sample_weight[y == l])) if centroids == None: self.centroids = [ mean_covariance(X[y == l], metric=self.metric_mean, sample_weight=sample_weight[y == l]) for l in self.classes ] else: self.centroids = centroids distance_list = [ np.array([distance(x, self.centroids[l]) for i, x in enumerate(X)]) for l in self.classes ] self.distribution = [[ np.log(distance_list[0][index] / distance_list[1][index]) for index, value in enumerate(X) if y[index] == l ] for l in self.classes] self.distribution_mean = [ np.mean(self.distribution[l]) for l in self.classes ] self.distribution_sigma = [ np.var(self.distribution[l]) for l in self.classes ] if self.distribution_mean[0] >= self.distribution_mean[1]: print( 'Target R_TNT Distribution mean should be smaller as Non Target R_TNT Distribution mean. Check the labels.' ) return self
def create_centroids_Dict(covmats_Dict, save_path): T = 0 NT = 1 classes = [T, NT] centroids_Dict = {} sample_weight_Dict = {} for subject in list( set(covmats_Dict.keys()) - set(['Sampling Rate', 'channel names'])): print 'Make centroids of ' + subject covmats_list = [] labels_list = [] for session, session_dict in covmats_Dict[subject].iteritems(): covmats_list.append(session_dict['covmats']) labels_list.append(session_dict['y']) X = np.array(covmats_list) XX = np.concatenate([X[k] for k in range(X.shape[0])]) y = np.array(labels_list) yy = np.concatenate([y[k] for k in range(y.shape[0])]) centroids_Dict[subject] = [ mean_covariance(XX[yy == l, :, :], metric='riemann') for l in classes ] sample_weight_Dict[subject] = [ len(np.where(np.array(y) == l)[0]) for l in classes ] np.save(save_path + 'partial_Centroids_Dict', centroids_Dict) np.save(save_path + 'partial_Weight_Dict', sample_weight_Dict) return centroids_Dict, sample_weight_Dict
def createCommonMat(subjects_data_dict, exclusion_criteria): """Create common covariance and correlation matrices param subjects_data_dict: dictionary. All subject's data dictionary from post processing step param exclusion_criteria : integer. Exclude subjects that number of volumes are less than exclusion_criteria return: (common_cov_mat, common_cor_mat) : tuple. common_cov_mat - common covariance matrix (264, 264), common_cor_mat - common correlation matrix (264, 264) """ start = time.time() #covariance matrices covars = [] for val in subjects_data_dict.values(): if val["num_of_volumes"] >= exclusion_criteria: covars.append(val["covariance"]) print("number of subjects: ", len(covars)) #find common covariance matrix riemann_mean_cov_mat = riemann_mean = mean_covariance(np.array(covars), metric='riemann', sample_weight=None) #create common correlation matrix riemann_mean_cor_mat = nilearn.connectome.cov_to_corr(riemann_mean_cov_mat) #print the exucation time end = time.time() timeInSeconds = (end - start) timeInMinutes = timeInSeconds / 60 timeInHours = int(timeInMinutes / 60) print("Total time : " + str(timeInHours) + " hours and " + str(int(timeInMinutes % 60)) + " minutes") return (riemann_mean_cov_mat, riemann_mean_cor_mat)
def _Q_S_estim_riemann(self, data): # Check if X is a single trial (test data) or not if data.ndim == 2: data = data[np.newaxis, ...] # Get data shape n_trials, n_channels, n_samples = data.shape X = np.concatenate((data, data), axis=1) # Concatenate all the trials UX = np.empty((n_channels, n_samples * n_trials)) for trial_n in range(n_trials): UX[:, trial_n * n_samples:(trial_n + 1) * n_samples] = data[trial_n, :, :] # Mean centering UX -= np.mean(UX, 1)[:, None] # Compute empirical variance of all data (to be bounded) cov = Covariances(estimator=self.estimator).fit_transform( UX[np.newaxis, ...]) Q = np.squeeze(cov) cov = Covariances(estimator=self.estimator).fit_transform(X) S = cov[:, :n_channels, n_channels:] + cov[:, n_channels:, :n_channels] S = mean_covariance(S, metric=self.method) return S, Q
def test_kernel_cref(ker, rndstate, get_covmats): """Test Kernel reference""" n_matrices, n_channels = 5, 3 cov = get_covmats(n_matrices, n_channels) cref = mean_covariance(cov, metric=ker) K = kernel(cov, cov, metric=ker) K1 = kernel(cov, cov, Cref=cref, metric=ker) assert_array_equal(K, K1)
def find_soulmate_iter(population_signals, stranger_signals): dist_dict = {} stranger_covariance = covariances(X=stranger_signals) stranger_reference = mean_covariance(covmats=stranger_covariance, metric='riemann') for subject, subject_signal in population_signals.iteritems(): subject_covariance = covariances(X=subject_signal) subject_reference = mean_covariance(covmats=subject_covariance, metric='riemann') dist_dict[subject] = distance(stranger_reference, subject_reference) sorted_dist_dict = sorted(dist_dict.items(), key=operator.itemgetter(1)) return dict(sorted_dist_dict)
def test_svc_cref_metric(get_covmats, get_labels, metric): n_matrices, n_channels, n_classes = 6, 3, 2 covmats = get_covmats(n_matrices, n_channels) labels = get_labels(n_matrices, n_classes) Cref = mean_covariance(covmats, metric=metric) rsvc = SVC(Cref=Cref).fit(covmats, labels) rsvc_1 = SVC(Cref=None, metric=metric).fit(covmats, labels) assert np.array_equal(rsvc.Cref_, rsvc_1.Cref_)
def test_svr_cref_metric(get_covmats, get_targets, metric): n_matrices, n_channels = 6, 3 covmats = get_covmats(n_matrices, n_channels) targets = get_targets(n_matrices) Cref = mean_covariance(covmats, metric=metric) rsvc = SVR(Cref=Cref).fit(covmats, targets) rsvc_1 = SVR(Cref=None, metric=metric).fit(covmats, targets) assert np.array_equal(rsvc.Cref_, rsvc_1.Cref_)
def _draw_subject_specific_and_estimate(cov_u, dim, vnum, outliers_num, b, show_corrs=False, repeat_one=0, label=None): covars = [] noise_covars = [] for i in range(0, vnum-outliers_num): # ---construct covariance of subject# LambdaMatrix = _draw_eigenvalues(dim=args.dim, b=args.b, distribution="unif", overlap=args.overlap_eigenvalues) rotMatrix = _draw_rotation_matrix(args.dim) noise_cov = rotMatrix.dot(LambdaMatrix.dot(rotMatrix.transpose())) cov = np.add(cov_u, noise_cov) noise_covars.append(noise_cov) covars.append(cov) for i in range(repeat_one): covars.append(cov) # --------------------------------------# print("snr is :" + str(snr(np.asarray(noise_covars), cov_u))) if(show_corrs): factor =0.03 + (not args.overlap_eigenvalues)*dim*0.03 f, axarr = plt.subplots(3, 3, figsize=(11, 5)) idxs = np.random.choice(range(len(covars)), size=(3, 3)) for i in range(idxs.shape[0]): for j in range(idxs.shape[1]): im = axarr[i, j].pcolormesh(np.flipud((connectivity_matrices.cov_to_corr(covars[idxs[i, j]])).T), cmap=plt.cm.gist_earth_r, edgecolors='k', vmin=-factor*b, vmax=factor*b, **{'linewidth': 0.1}) axarr[i, j].set_axis_off() axarr[i, j].set_aspect('equal', 'box') if(j == 2): divider = make_axes_locatable(axarr[i, j]) cax = divider.append_axes("right", size="5%", pad=0.05) cb = f.colorbar(im, cax=cax) tick_locations = (-factor*b, 0, factor*b) tick_labels = ('{:{width}.{prec}f}'.format(-factor*b, width=1, prec=1), '{:{width}.{prec}f}'.format(0, width=1, prec=1), '{:{width}.{prec}f}'.format(factor*b, width=1, prec=1)) cb.locator = ticker.FixedLocator(tick_locations) cb.formatter = ticker.FixedFormatter(tick_labels) cb.update_ticks() if label is None: plt.savefig('./subjects_covars.eps', format='eps', dpi=1000) else: plt.savefig('./subjects_covars_' + str(label) + '.eps', format='eps', dpi=1000) # construct outliers w, v = LA.eig(cov_u) for i in range(0, outliers_num): LambdaMatrix = _draw_eigenvalues(dim=args.dim, b=args.b, distribution="unif", overlap=args.overlap_eigenvalues) rotMatrix = _draw_rotation_matrix(dim=3) cov = rotMatrix.dot(LambdaMatrix.dot(rotMatrix.transpose())) covars.append(cov) our_est = est_common_cov(covars, outliers=args.outliers) eucl_mean = mean_euclid(np.array(covars)) riemann_mean = mean_covariance(np.array(covars), metric='riemann', sample_weight=None) return our_est, eucl_mean, riemann_mean
def full_calc_mean_cov(cov_train_i, label_train_i, num_classes=4): global mean_cov_n mean_cov_n = np.zeros((num_classes, cov_train_i.shape[1], cov_train_i.shape[2])) mean_cov_i = mean_cov_n for l in range(num_classes): try: mean_cov_n[l] = mean_covariance(cov_train_i[label_train_i == l], metric='riemann', sample_weight=None) # print(mean_cov_n[l]) except ValueError: mean_cov_n[l] = mean_cov_i[l] return mean_cov_n
def create_centroids_List(centroids_Dict, sample_weight_Dict): target_centroids_list = [] target_weight_list = [] non_target_centroids_list = [] non_target_weight_list = [] for subject, centroids in centroids_Dict.iteritems(): subject_weight_list = sample_weight_Dict[subject] target_centroids_list.append(centroids[0]) target_weight_list.append(subject_weight_list[0]) non_target_centroids_list.append(centroids[1]) non_target_weight_list.append(subject_weight_list[1]) CT = np.array(target_centroids_list) CNT = np.array(non_target_centroids_list) return [ mean_covariance(CT, metric='riemann', sample_weight=np.array(target_weight_list)), mean_covariance(CNT, metric='riemann', sample_weight=np.array(non_target_weight_list)) ]
def learn_ts_fgda(X_train, y_train, T=0, NT=1): classes = [T, NT] train_fgda = FGDA() train_fgda.fit(X=X_train, y=y_train) X_train_fgda = train_fgda.transform(X=X_train) centroids_train_fgda = [ mean_covariance(X_train_fgda[y_train == l, :, :], metric='riemann') for l in classes ] return X_train_fgda, centroids_train_fgda, train_fgda
def training_data_cov_means(X, y, num_classes=4): # # # Finds the mean covariance matrices for each class in the training data # Returns: The mean covariance matrices mean_cov_i = np.zeros((num_classes, X.shape[1], X.shape[2])) num_in_class_i = np.zeros(num_classes) for l in range(num_classes): sample_weight = np.ones(X[y == l].shape[0]) mean_cov_i[l] = mean_covariance(X[y == l], metric='riemann', sample_weight=sample_weight) num_in_class_i[l] = X[y == l].shape[0] return mean_cov_i, num_in_class_i
def fit(self, X, y): """ Train spatial filters. Only deals with two class """ if not isinstance(X, (np.ndarray, list)): raise TypeError("X must be an array.") if not isinstance(y, (np.ndarray, list)): raise TypeError("y must be an array.") X, y = np.asarray(X), np.asarray(y) if X.ndim != 3: raise ValueError("X must be n_trials * n_channels * n_channels") if len(y) != len(X): raise ValueError("X and y must have the same length.") if np.squeeze(y).ndim != 1: raise ValueError("y must be of shape (n_trials,).") Nt, Ne, Ns = X.shape classes = np.unique(y) assert len(classes) == 2, "Can only do 2-class TRCSP" # estimate class means C = [] for c in classes: C.append(mean_covariance(X[y == c], self.metric)) C = np.array(C) # regularize CSP evals = [[], []] evecs = [[], []] Creg = C[1] + np.eye(C[1].shape[0]) * self.alpha evals[1], evecs[1] = linalg.eigh(C[0], Creg) Creg = C[0] + np.eye(C[0].shape[0]) * self.alpha evals[0], evecs[0] = linalg.eigh(C[1], Creg) # sort eigenvectors filters = [] patterns = [] for i in range(2): ix = np.argsort(evals[i])[::-1] # in descending order # sort eigenvectors evecs[i] = evecs[i][:, ix] # spatial patterns A = np.linalg.pinv(evecs[i].T) filters.append(evecs[i][:, :(self.nfilter // 2)]) patterns.append(A[:, :(self.nfilter // 2)]) self.filters_ = np.concatenate(filters, axis=1).T self.patterns_ = np.concatenate(patterns, axis=1).T return self
def Signals_Covariance(signals, core_test, mean_all=True): if mean_all: signal = signals.reshape((-1, ) + (signals.shape[-2:])) signal = np.transpose(signal, axes=[0, 2, 1]) x_out = pyriemann.estimation.Covariances().fit_transform(signal) core = mean_covariance(x_out, metric='riemann') # core = training_data_cov_means(X,y,num_classes=4) core = core**(-1 / 2) signal1, core = signal_covar(signals, core, mean_all) return signal1, core else: core = core_test**(-1 / 2) signal1 = signal_covar(signals, core, mean_all) return signal1
def fit(self, X, y, sample_weight=None): self.classes_ = np.unique(y) if sample_weight is None: sample_weight = np.ones(X.shape[0]) if self.n_jobs == 1: self.covmeans_ = [mean_covariance(X[y == l], metric=self.metric_mean, sample_weight=sample_weight[y == l]) for l in self.classes_] else: self.covmeans_ = Parallel(n_jobs=self.n_jobs)( delayed(mean_covariance)(X[y == l], metric=self.metric_mean, sample_weight=sample_weight[y == l]) for l in self.classes_) return self
def tangential(all_FC, ref): # Regularization for riemann if ref in ['riemann', 'kullback_sym', 'logeuclid']: print("Adding regularization!") eye_mat = np.eye(all_FC.shape[1]) scaling_mat = np.repeat(eye_mat[None, ...], all_FC.shape[0], axis=0) all_FC += scaling_mat Cg = mean_covariance(all_FC, metric=ref) Q1_inv_sqrt = q1invm(Cg) Q = Q1_inv_sqrt @ all_FC @ Q1_inv_sqrt tangent_FC = np.empty(Q.shape) for idx, fc in enumerate(Q): if idx % 100 == 0: print(f'{idx}/{Q.shape[0]}') tangent_FC[idx] = linalg.logm(fc) enablePrint() return tangent_FC
def csp_one_all(cov_matrix, NO_csp, NO_pairs): ''' calculate spatial filter for class (1 vs other ) (2 vs other ) (3 vs other ) (4 vs other ) Keyword arguments: cov_matrix -- numpy array of size [N_classes ,NO_channels, NO_channels] NO_csp -- number of spatial filters Return: spatial filter numpy array of size [ NO_channels ,NO_csp] ''' N_class, N, _ = cov_matrix.shape # N is number of channel w = np.zeros((N, NO_csp)) kk = 0 # internal counter for classCov in range(0, 4): #covariance average of other covAvg = rie_mean.mean_covariance( cov_matrix[np.arange(0, N_class) != classCov, :, :], metric='euclid') w[:, NO_pairs * 2 * (kk):NO_pairs * 2 * (kk + 1)] = gevd( cov_matrix[classCov], covAvg, NO_pairs) kk += 1 return w
def full_calc_mean_cov(cov_train_i, label_train_i, num_classes, mean_cov_i): # # # Recalculates the new mean covariance matrix iteratively, considering # # # the actual mean of each matrix tic2 = time.clock() mean_cov_n = np.zeros( (num_classes, cov_train_i.shape[1], cov_train_i.shape[2])) for l in range(num_classes): try: mean_cov_n[l] = mean_covariance(cov_train_i[label_train_i == l], metric='riemann', sample_weight=None) except ValueError: mean_cov_n[l] = mean_cov_i[l] toc2 = time.clock() time_out = toc2 - tic2 print(toc2 - tic2) # .4436 seconds return mean_cov_n, time_out
def predict_ERP_centroids(x, y, metric='riemann', ERP_bloc=None, T=0, NT=1): """Helper to predict the r_TNT for a new set of trials. Parameters ---------- x : ndarray, shape (n_trials, n_channels, n_times) y : ndarray, shape (,n_trials) ERP_bloc : list with 0 or 1 for the class in the ERP Returns ------- erp : the ERPCovariance object with erp.P an ndarray, shape (n_channels*len(ERP_bloc), n_times) centroids : list of the two centers of classe which are both ndarray, shape (n_channels*len(ERP_bloc), n_channels*len(ERP_bloc)) X : ndarray, shape (n_trials, n_channels*len(ERP_bloc), n_channels*len(ERP_bloc)), the set of super covariance matrices of set signals given in input """ classes = [T, NT] erp = ERPCovariances(classes=ERP_bloc, estimator='cov') erp.fit(X=x, y=y) X = erp.transform(X=x) centroids = [ mean_covariance(X[y == l, :, :], metric=metric) for l in classes ] return erp, centroids, X
def test_mean_covariance_euclid(): """Test mean_covariance for euclidean metric""" covmats, diags, A = generate_cov(100, 3) C = mean_covariance(covmats, metric='euclid') Ctrue = mean_euclid(covmats) assert_array_equal(C, Ctrue)
def test_mean_covariance_riemann(): """Test mean_covariance for riemannian metric""" covmats, diags, A = generate_cov(100, 3) C = mean_covariance(covmats, metric='riemann') Ctrue = mean_riemann(covmats) assert_array_equal(C, Ctrue)
def fit(self, data): ''' Calculate average covariance matrices and return freatures of training data Parameters ---------- data: array, shape (n_tr_trial,n_channel,n_samples) input training time samples Return ------ train_feat: array, shape: if vectorized: (n_tr_trial,(n_temp x n_freq x n_riemann) else (n_tr_trial,n_temp , n_freq , n_riemann) ''' n_tr_trial, n_channel, _ = data.shape self.n_channel = n_channel self.n_riemann = int((n_channel + 1) * n_channel / 2) cov_mat = np.zeros( (n_tr_trial, self.n_temp, self.n_freq, n_channel, n_channel)) # calculate training covariance matrices for trial_idx in range(n_tr_trial): for temp_idx in range(self.n_temp): t_start, t_end = self.temp_windows[ temp_idx, 0], self.temp_windows[temp_idx, 1] n_samples = t_end - t_start for freq_idx in range(self.n_freq): # filter signal data_filter = butter_fir_filter( data[trial_idx, :, t_start:t_end], self.filter_bank[freq_idx]) # regularized covariance matrix cov_mat[trial_idx, temp_idx, freq_idx] = 1 / (n_samples - 1) * np.dot( data_filter, np.transpose(data_filter) ) + self.rho / n_samples * np.eye(n_channel) # calculate mean covariance matrix self.c_ref_invsqrtm = np.zeros((self.n_freq, n_channel, n_channel)) for freq_idx in range(self.n_freq): if self.riem_opt == 'No_Adaptation': self.c_ref_invsqrtm[freq_idx] = np.eye(n_channel) else: # Mean covariance matrix over all trials and temp winds per frequency band cov_avg = mean.mean_covariance(cov_mat[:, :, freq_idx].reshape( -1, n_channel, n_channel), metric=self.mean_metric) self.c_ref_invsqrtm[freq_idx] = base.invsqrtm(cov_avg) # calculate training features train_feat = np.zeros( (n_tr_trial, self.n_temp, self.n_freq, self.n_riemann)) for trial_idx in range(n_tr_trial): for temp_idx in range(self.n_temp): for freq_idx in range(self.n_freq): train_feat[trial_idx, temp_idx, freq_idx] = self.riem_kernel( cov_mat[trial_idx, temp_idx, freq_idx], self.c_ref_invsqrtm[freq_idx]) if self.vectorized: return train_feat.reshape(n_tr_trial, -1) else: return train_feat
def test_mean_covariance_logdet(): """Test mean_covariance for logdet metric""" covmats, diags, A = generate_cov(100, 3) C = mean_covariance(covmats, metric='logdet') Ctrue = mean_logdet(covmats) assert_array_equal(C, Ctrue)
def generate_projection(data, class_vec, NO_csp, filter_bank, time_windows, NO_classes=2): ''' generate spatial filters for every timewindow and frequancy band Keyword arguments: data -- numpy array of size [NO_trials,channels,time_samples] class_vec -- containing the class labels, numpy array of size [NO_trials] NO_csp -- number of spatial filters (24) filter_bank -- numpy array containing butter sos filter coeffitions dim [NO_bands,order,6] time_windows -- numpy array [[start_time1,end_time1],...,[start_timeN,end_timeN]] Return: spatial filter numpy array of size [NO_timewindows,NO_freqbands,22,NO_csp] ''' time_windows = time_windows.reshape((-1, 2)) NO_bands = filter_bank.shape[0] NO_time_windows = len(time_windows[:, 0]) NO_channels = len(data[0, :, 0]) NO_trials = class_vec.size # Initialize spatial filter: w = np.zeros((NO_time_windows, NO_bands, NO_channels, NO_csp)) # iterate through all time windows for t_wind in range(0, NO_time_windows): # get start and end point of current time window t_start = time_windows[t_wind, 0] t_end = time_windows[t_wind, 1] # iterate through all frequency bandwids for subband in range(0, NO_bands): cov = np.zeros( (NO_classes, NO_trials, NO_channels, NO_channels)) # sum of covariance depending on the class cov_avg = np.zeros((NO_classes, NO_channels, NO_channels)) cov_cntr = np.zeros(NO_classes).astype( int) # counter of class occurence #go through all trials and estimate covariance matrix of every class for trial in range(0, NO_trials): #frequency band of every channel data_filter = butter_fir_filter(data[trial, :, t_start:t_end], filter_bank[subband]) cur_class_idx = int(class_vec[trial] - 1) # caclulate current covariance matrix cov[cur_class_idx, cov_cntr[cur_class_idx], :, :] = np.dot( data_filter, np.transpose(data_filter)) # update covariance matrix and class counter cov_cntr[cur_class_idx] += 1 # calculate average of covariance matrix for clas in range(0, NO_classes): cov_avg[clas, :, :] = rie_mean.mean_covariance( cov[clas, :cov_cntr[clas], :, :], metric='euclid') w[t_wind, subband, :, :] = csp_one_one(cov_avg, NO_csp, NO_classes) return w
def test_mean_covariance_logeuclid(): """Test mean_covariance for logeuclid metric""" covmats = generate_cov(100,3) C = mean_covariance(covmats,metric='logeuclid') Ctrue = mean_logeuclid(covmats) assert_array_equal(C,Ctrue)
def test_mean_covariance_euclid(): """Test mean_covariance for euclidean metric""" covmats = generate_cov(100, 3) C = mean_covariance(covmats, metric='euclid') Ctrue = mean_euclid(covmats) assert_array_equal(C, Ctrue)
def test_mean_covariance_logdet(): """Test mean_covariance for logdet metric""" covmats = generate_cov(100, 3) C = mean_covariance(covmats, metric='logdet') Ctrue = mean_logdet(covmats) assert_array_equal(C, Ctrue)
def test_mean_covariance_riemann(): """Test mean_covariance for riemannian metric""" covmats = generate_cov(100, 3) C = mean_covariance(covmats, metric='riemann') Ctrue = mean_riemann(covmats) assert_array_equal(C, Ctrue)
def test_loop_P300_dynamic(x_test, y_test, e_test, x_train, y_train, targets, T=0, NT=1, flashmode='RoCo', visu=False, nb_targets=60): classes = [T, NT] x_train_nontar = x_train[y_train == NT, :, :] y_train_nontar = y_train[y_train == NT] x_train_tar = x_train[y_train == T, :, :] y_train_tar = y_train[y_train == T] Max_indice = x_test.shape[0] nb_repetitions = Max_indice / (12 * nb_targets) J = 12 * nb_repetitions items_list = targets items_vec = np.array(items_list) screen = np.reshape(items_vec, [6, 6]) item_prediction = [] indice_flash = 0 indice_char = 0 nb_rep = 0 item_selected_list = [] Nt, Ne, Ns = x_train.shape virtual_subject_P = np.mean(x_train[y_train == T, :, :], axis=0) virtual_real_subject_P = virtual_subject_P # real_subject_P = np.mean(x_pipe, axis = 0) t = 0 # real_subject_P = virtual_subject_P covmats_train = np.zeros((Nt, 2 * Ne, 2 * Ne)) for i in range(Nt): covmats_train[i, :, :] = np.cov( np.concatenate((virtual_subject_P, x_train[i, :, :]), axis=0)) train_centroids = [ mean_covariance(covmats_train[y_train == l, :, :], metric='riemann') for l in classes ] # For the first loop, take the aruthmetic mean # train_centroids = [np.mean(covmats_train[y_train == l, :, :] , axis = 0 ) for l in classes] train_r_TNT = [ predict_R_TNT(X=x, centroids=train_centroids, classes=['T', 'NT']) for i, x in enumerate(covmats_train) ] train_r_TNT_vec = np.array(train_r_TNT) train_r_TNT_nontar = train_r_TNT_vec[y_train == NT] train_r_TNT_tar = train_r_TNT_vec[y_train == T] train_distribution = Distribution_R_TNT() train_distribution.fit(X=covmats_train, y=y_train) train_NaiveBayes = R_TNT_NaiveBayes(targets=targets) train_NaiveBayes.fit(train_distribution) if visu == True: visualisation_R_TNT(r_TNT=train_r_TNT, y=y_train) while indice_flash < Max_indice: # Get the r_TNT of the current trial x = x_test[indice_flash, :, :] # X = np.cov(np.concatenate([virtual_subject_P, real_subject_P , x], axis=0)) X = np.cov(np.concatenate([virtual_real_subject_P, x], axis=0)) r_TNT = predict_R_TNT(X=X, centroids=train_distribution.centroids, classes=['T', 'NT']) if r_TNT <= 0: if t == 0: x_dynamic = np.zeros((1, Ne, Ns)) x_dynamic[0, :, :] = x y_dynamic = np.array([T]) t = 1 else: xx = np.zeros((1, Ne, Ns)) xx[0, :, :] = x x_dynamic = np.concatenate([x_dynamic, xx], axis=0) y_dynamic = np.concatenate([y_dynamic, np.array([T])], axis=0) else: if t == 0: x_dynamic = np.zeros((1, Ne, Ns)) x_dynamic[0, :, :] = x y_dynamic = np.array([NT]) t = 1 else: xx = np.zeros((1, Ne, Ns)) xx[0, :, :] = x x_dynamic = np.concatenate([x_dynamic, xx], axis=0) y_dynamic = np.concatenate([y_dynamic, np.array([NT])], axis=0) # Update the bayes_prior (vector of length 36) if flashmode == 'RoCo': flashed_item = rtp_indexColumn2Targets( Screen=screen, IndexColumn=e_test[indice_flash]) if flashmode == 'Splotch': SplotchMatrixNumber = floor( (indice_flash - indice_char * 12 * nb_repetitions) / 12) + 1 flashed_item = rtp_indexSplotch2Targets( SplotchMatrixNumber=SplotchMatrixNumber, Indexplotch=e_test[indice_flash], items_vec=items_vec) flashed_item_index = [ i for i, e in enumerate(items_list) if e in list(flashed_item) ] items_posterior_array = train_NaiveBayes.update_class_prior( r_TNT, flashed_item_index) item_selected = items_list[np.argmax(items_posterior_array)] item_selected_list.append(item_selected) # Ask if it's flashed_items_total_number is enough to reset class_prior or to take a decision and change of target indice_flash += 1 if not indice_flash % 12: nb_rep += 1 if not indice_flash % J: indice_char += 1 # real_subject_P = np.mean(x_dynamic[y_dynamic == T, :, :], axis=0) n_dyn_T = (np.array(np.where(y_dynamic == T))).shape[1] n_dyn_NT = (np.array(np.where(y_dynamic == NT))).shape[1] n_dyn = len(y_dynamic) indices_tar = clean_r_TNT(train_r_TNT_tar, n_dyn_T) indices_nontar = clean_r_TNT(train_r_TNT_nontar, n_dyn_NT) x_train_tar_bis = x_train_tar[indices_tar, :, :] y_train_tar_bis = y_train_tar[indices_tar] x_train_nontar_bis = x_train_nontar[indices_nontar, :, :] y_train_nontar_bis = y_train_nontar[indices_nontar] x_train_dynamic = np.concatenate( [x_train_tar_bis, x_train_nontar_bis, x_dynamic], axis=0) y_train_dynamic = np.concatenate( [y_train_tar_bis, y_train_nontar_bis, y_dynamic], axis=0) virtual_real_subject_P = np.mean( x_train_dynamic[y_train_dynamic == T, :, :], axis=0) Nt, Ne, Ns = x_train_dynamic.shape covmats_train_dynamic = np.zeros((Nt, 2 * Ne, 2 * Ne)) for i in range(Nt): covmats_train_dynamic[i, :, :] = np.cov( np.concatenate( (virtual_real_subject_P, x_train_dynamic[i, :, :]), axis=0)) mT = mean_riemann( covmats=covmats_train_dynamic[y_train_dynamic == T, :, :], tol=10e-9, maxiter=10, init=None, sample_weight=None) mNT = mean_riemann( covmats=covmats_train_dynamic[y_train_dynamic == NT, :, :], tol=10e-9, maxiter=10, init=None, sample_weight=None) train_centroids = [mT, mNT] train_distribution.fit(X=covmats_train_dynamic, y=y_train_dynamic, centroids=train_centroids) train_NaiveBayes.fit(train_distribution) # b = plt.figure('Posteriors at each decision') # plt.plot(items_posterior_array, label = str(indice_char)) # plt.legend() # plt.show() # b.savefig('Posteriors_each_decision_sess2.png') item_prediction.append(item_selected) train_NaiveBayes.reset_bayes_prior() # print item_selected temp = np.matrix(item_selected_list) I = temp.shape[1] / J item_selected_mat = temp.reshape([I, J]) w = find_target_word(e=e_test, y=y_test, nb_repetitions=nb_repetitions, items_vec=items_vec, flashmode=flashmode, T=T, NT=NT) target_vec = np.matrix(w.tolist()) # target_mat = a.reshape([I, J]) # accuracy_list = [] k = 0 for j in range(J): item_comparison = np.transpose(target_vec) == item_selected_mat[:, j] item_comparison = item_comparison.astype('float') if k == 0: A = item_comparison k = 1 else: A = np.hstack([A, item_comparison]) mean_acc_vector = np.mean(A, axis=0) std_acc_vector = np.std(A, axis=0) return mean_acc_vector, std_acc_vector
def generate_projection(data, class_vec, f_bands_nom, NO_weights, NO_class, OnevsOne): ''' generate spatial filters for every frequancy band and return weight matrix Keyword arguments: data -- numpy array of size [NO_trials,channels,time_samples] class_vec -- containing the class labels, numpy array of size [NO_trials] NO_weights -- number of weights , NO_class -- number of classes, f_bands_nom -- numpy array [[start_freq1,end_freq1],...,[start_freqN,end_freqN]] time_windows -- numpy array [[start_time1,end_time1],...,[start_timeN,end_timeN]] Return: spatial filter numpy array of size [NO_timewindows,NO_freqbands,22,NO_csp] ''' NO_bands = len(f_bands_nom) NO_channels = len(data[0, :, 0]) NO_trials = class_vec.size if (OnevsOne): NO_csp = np.int(((NO_class * (NO_class - 1)) / 2) * NO_weights * 2) else: NO_csp = np.int(NO_class * NO_weights * 2) # Initialize spatial filter: w = np.zeros((NO_bands, NO_channels, NO_csp)) # iterate through all time windows # get start and end point of current time window # *must get value in arguments t_start = int(2.5 * 250) t_end = int(6 * 250) # iterate through all frequency bandwids print("Calculate filter for band : ") for subband in range(0, NO_bands): print(subband, ",", end=" ") cov = np.zeros( (4, NO_trials, NO_channels, NO_channels)) # sum of covariance depending on the class cov_avg = np.zeros((4, NO_channels, NO_channels)) cov_cntr = np.zeros(4).astype(int) # counter of class occurence #go thrugh all trials and estimate covariance matrix of every class for trial in range(0, NO_trials): #frequency band of every channel data_filter = bandpass_filter(data[trial, :, t_start:t_end], f_bands_nom[subband]) # must calculae indecies of class label for automation cur_class_idx = int(class_vec[trial] - 1) # caclulate current covariance matrix cov[cur_class_idx, cov_cntr[cur_class_idx], :, :] = np.dot( data_filter, np.transpose(data_filter)) # update covariance matrix and class counter cov_cntr[cur_class_idx] += 1 # calculate average of covariance matrix for clas in range(0, 4): cov_avg[clas, :, :] = rie_mean.mean_covariance( cov[clas, :cov_cntr[clas], :, :], metric='euclid') if (OnevsOne): w[subband, :, :] = csp_one_one(cov_avg, NO_csp, NO_weights) else: w[subband, :, :] = csp_one_all(cov_avg, NO_csp, NO_weights) return w