def __init__(self, eeg_training, eeg_test, n_components=2): """ Parameters ---------- n_components : int The number of components to use as relevant features in the CSP algorithm eeg_training : Eeg The Eeg object that contains the raw training eeg data separated by class (left and right) eeg_test : Eeg The Eeg object that contains the raw test eeg data separated by class (left and right) """ self.n_components = n_components self.__bands = range(QUANTITY_BANDS) self.__csp_by_band = [ CSP(average_trial_covariance=False, n_components=self.n_components) for _ in self.__bands ] self.training_labels = eeg_training.labels self.test_labels = eeg_test.labels self.training_features = self.extract_features(eeg_training) self.test_features = self.extract_features(eeg_test) self.n_features = self.training_features.shape[1] if self.n_features != self.test_features.shape[1]: raise Exception( "The number of features extracted from the training and test dataset's should be equal" )
right_data_file = f"{DATA_FOLDER}/{subject}-right.csv" data = read_eeg_file(left_data_file, right_data_file, TIME_LENGTH, TIME_WINDOW, EPOCH_SIZE) # Pre-processing print("Pre-processing ...") print("Applying 5º order Butterworth bandpass filter (7-30 Hz)") b, a = signal.butter(5, [7, 30], btype="bandpass", fs=FS) data.left_data = signal.filtfilt(b, a, data.left_data, axis=1) data.right_data = signal.filtfilt(b, a, data.right_data, axis=1) print("Spatial-filtering ...") data.X = np.concatenate((data.left_data, data.right_data)) csp = CSP(average_trial_covariance=True, n_components=CSP_COMPONENTS) csp.fit(data.left_data, data.right_data) data.Z = np.array([csp.project(x) for x in data.X]) # print(data.Z.shape) #(399,700,2) # 첫번째 trial의 채널 1 # plot.specgram(pd.DataFrame(data.Z[0]).iloc[:,0], NFFT=64, Fs=250, noverlap=32) # 기존 # plot.specgram(pd.DataFrame(data.Z[0]).iloc[:,0], NFFT=32, Fs=250, noverlap=16) # plot.show() # Feature extraction print("Extracting features ...") data.F = np.zeros((data.X.shape[0], 2, CSP_COMPONENTS)) for n_epoch in range(0, data.X.shape[0]): epoch = data.Z[n_epoch]
print(data.left_data.shape) print(data.right_data.shape) train_left_index = [index for index in train_index if index < trials] train_right_index = [index - trials for index in train_index if index >= trials] X_left_train, X_right_train = data.left_data[train_left_index], data.right_data[train_right_index] test_left_index = [index for index in test_index if index < trials] test_right_index = [index - trials for index in test_index if index >= trials] X_left_test, X_right_test = data.left_data[test_left_index], data.right_data[test_right_index] y_train, y_test = data.labels[train_index], data.labels[test_index] # Feature extraction N_CSP_COMPONENTS = 2 csp_by_band = [CSP(average_trial_covariance=False, n_components=N_CSP_COMPONENTS) for _ in bands] left_bands_training = filter_bank(X_left_train) right_bands_training = filter_bank(X_right_train) left_bands_test = filter_bank(X_left_test) right_bands_test = filter_bank(X_right_test) features_train = None features_test = None for n_band in range(quantity_bands): left_band_training = left_bands_training[n_band] right_band_training = right_bands_training[n_band] left_band_test = left_bands_test[n_band] right_band_test = right_bands_test[n_band]
data = read_eeg_file(left_data_file, right_data_file, TIME_LENGTH, TIME_WINDOW, EPOCH_SIZE) # Pre-processing print("Pre-processing ...") print("Applying 5º order Butterworth bandpass filter (7-30 Hz)") b, a = signal.butter(5, [7, 30], btype="bandpass", fs=FS) # left_data2, right_data2 전달된 상태 data.left_data = signal.filtfilt(b, a, data.left_data, axis=1) data.right_data = signal.filtfilt(b, a, data.right_data, axis=1) print("Spatial-filtering ...") data.X = np.concatenate((data.left_data, data.right_data)) csp = CSP(average_trial_covariance=True, n_components=CSP_COMPONENTS) csp.fit(data.left_data, data.right_data) # print(csp.W.shape) #(3,2) # print(data.left_data.shape) #(159, 500, 3) # print(data.right_data.shape)# (165, 500, 3) # print(data.X.shape) # counts=0 counts = 303 imgtype = 'left' print("왼쪽 데이터 이미지 생성 시작") for i in data.X: print("i_check{}".format(i)) counts += 1
outlines="skirt", show_names=True) # Load training data print("Loading data ...") left_data_file = f"{DATA_FOLDER}/left-hand-subject-{subject}.csv" right_data_file = f"{DATA_FOLDER}/right-hand-subject-{subject}.csv" eeg = read_eeg_file(left_data_file, right_data_file, TIME_LENGTH, TIME_WINDOW, EPOCH_SIZE) apply_bandpass_filter(eeg) n_epoch = 25 left_epoch = eeg.left_data[n_epoch, :, :] right_epoch = eeg.right_data[n_epoch, :, :] ch_names = ["FC1", "FC2", "C1", "C2", "C3", "C4", "CP1", "CP2"] # # plot_topomap(ch_names, left_epoch) # plot_topomap(ch_names, right_epoch) csp = CSP(average_trial_covariance=True, n_components=8) csp.fit(eeg.left_data, eeg.right_data) left_epoch_projected = csp.project(left_epoch) right_epoch_projected = csp.project(right_epoch) plot_topomap(ch_names, left_epoch_projected) plot_topomap(ch_names, right_epoch_projected)