Ejemplo n.º 1
0
    def onetrial_feature(self, data):
        '''
		Generate multiscale Riemannian one trial and temp window 

		Parameters
		----------
		data: array, shape (n_channel,n_samples)
			input time samples 
		
		Return  
		------
		feat: array, shape: if vectorized: (n_freq x n_riemann)
								  else 		(n_freq , n_riemann)
		'''
        n_samples = data.shape[1]

        feat = np.zeros((self.n_freq, self.n_riemann))

        for freq_idx in range(self.n_freq):
            # filter signal
            data_filter = butter_fir_filter(data, self.filter_bank[freq_idx])

            # regularized covariance matrix
            cov_mat = 1 / (n_samples - 1) * np.dot(
                data_filter, np.transpose(data_filter)
            ) + self.rho / n_samples * np.eye(self.n_channel)
            #
            feat[freq_idx] = self.riem_kernel(cov_mat,
                                              self.c_ref_invsqrtm[freq_idx])

        if self.vectorized:
            return feat.reshape(-1)
        else:
            return feat
Ejemplo n.º 2
0
def extract_feature(data, w, filter_bank, time_windows, do_vectorization=True):
    '''	calculate features using the precalculated spatial filters

	Keyword arguments:
	data -- numpy array of size [NO_trials,channels,time_samples]
	w -- spatial filters, numpy array of size [NO_timewindows,NO_freqbands,22,NO_csp]
	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:	features, numpy array of size [NO_trials,(NO_csp*NO_bands*NO_time_windows)] 
	'''
    NO_csp = len(w[0, 0, 0, :])
    time_windows = time_windows.reshape((-1, 2))
    NO_time_windows = int(time_windows.size / 2)
    NO_bands = filter_bank.shape[0]
    NO_trials = len(data[:, 0, 0])
    NO_features = NO_csp * NO_bands * NO_time_windows

    feature_mat = np.zeros((NO_trials, NO_time_windows, NO_bands, NO_csp))

    # initialize feature vector
    feat = np.zeros((NO_time_windows, NO_bands, NO_csp))

    # go through all trials
    for trial in range(0, NO_trials):

        # 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]

            for subband in range(0, NO_bands):
                #Apply spatial Filter to data
                cur_data_s = np.dot(np.transpose(w[t_wind, subband]),
                                    data[trial, :, t_start:t_end])

                #frequency filtering
                cur_data_f_s = butter_fir_filter(cur_data_s,
                                                 filter_bank[subband])

                # calculate variance of all channels
                feat[t_wind, subband] = np.var(cur_data_f_s, axis=1)

            # calculate log10 of normalized feature vector

        for subband in range(0, NO_bands):
            feat[:,
                 subband] = np.log10(feat[:,
                                          subband])  #/np.sum(feat[:,subband]))

        # store feature in list
        feature_mat[trial, :, :, :] = feat
    if do_vectorization:
        return np.reshape(feature_mat, (NO_trials, -1))  #
    else:
        return feature_mat
Ejemplo n.º 3
0
    def features(self, data):
        '''
		Generate multiscale Riemannian features 

		Parameters
		----------
		data: array, shape (n_trial,n_channel,n_samples)
			input time samples 
		
		Return  
		------
		feat: array, shape: if vectorized: (n_trial,(n_temp x n_freq x n_riemann)
								  else 			 (n_trial,n_temp , n_freq , n_riemann)
		'''
        n_trial = data.shape[0]

        feat = np.zeros((n_trial, self.n_temp, self.n_freq, self.n_riemann))

        # calculate training covariance matrices
        for trial_idx in range(n_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 = 1 / (n_samples - 1) * np.dot(
                        data_filter, np.transpose(data_filter)
                    ) + self.rho / n_samples * np.eye(self.n_channel)
                    #
                    feat[trial_idx, temp_idx, freq_idx] = self.riem_kernel(
                        cov_mat, self.c_ref_invsqrtm[freq_idx])

        if self.vectorized:
            return feat.reshape(n_trial, -1)
        else:
            return feat
Ejemplo n.º 4
0
    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
Ejemplo n.º 5
0
def generate_projection(data,
                        class_vec,
                        NO_csp,
                        filter_bank,
                        time_windows,
                        NO_classes=4):
    '''	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