def gen_tscsp(sub='1'): path = 'data/A0' + sub + 'T.npz' cnt, mrk, dur, y = Competition.load_cnt_mrk_y(path) epo = [] for i in range(1, 10): # band-pass filtering cnt_fs = CSP.arr_bandpass_filter(cnt, i * 4, (i + 1) * 4, 250, 4) cur_epo = cnt_to_epo(cnt_fs, mrk, dur) cur_epo, y_ = out_label_remover(cur_epo, y) epo.append(cur_epo) y = BCI.lab_inv_translator(y_, 4) kv = BCI.gen_kv_idx(y, 5) k = 1 for train_idx, test_idx in kv: train_x = epo_temporal_dividing( np.array(epo)[:, train_idx, :, :], 4, 0.5, 250) test_x = epo_temporal_dividing( np.array(epo)[:, test_idx, :, :], 4, 0.5, 250) train_y = np.transpose(np.array(y)[train_idx]) test_y = np.transpose(np.array(y)[test_idx]) res = { 'train_x': train_x, 'test_x': test_x, 'train_y': train_y, 'test_y': test_y } scipy.io.savemat( 'competition/ori_4_0.5/' + sub + '_' + str(k) + '.mat', res) k += 1
def make_data_temporal(sub): cnt = scipy.io.loadmat('raw_KIST/twist/raw_cnt_' + sub + '.mat')['cnt'][0][0][4] mrk = scipy.io.loadmat('raw_KIST/twist/raw_mrk_' + sub + '.mat')['mrk'][0][0][0][0] y = scipy.io.loadmat('raw_KIST/twist/raw_mrk_' + sub + '.mat')['mrk'][0][0][3] epo = [] for i in range(1, 10): cnt_fs = CSP.arr_bandpass_filter(cnt, i * 4, (i + 1) * 4, 1000, 4) epo.append(cnt_to_epo(cnt_fs, mrk)) y_ = np.transpose(y) kv = BCI.gen_kv_idx(y_, 5) k = 1 for train_idx, test_idx in kv: train_x = epo_temporal_dividing( np.array(epo)[:, train_idx, :, :], 0.6, 0.3) test_x = epo_temporal_dividing( np.array(epo)[:, test_idx, :, :], 0.6, 0.3) train_y = y[:, train_idx] test_y = y[:, test_idx] res = { 'train_x': train_x, 'test_x': test_x, 'train_y': train_y, 'test_y': test_y } scipy.io.savemat('RCNN2/twist_ori_dj/' + sub + '_' + str(k) + '.mat', res) k += 1
def data_load(file, filtering=True): path = 'E:/Richard/EEG/Competition/raw/' cnt, mrk, dur, y = com.load_cnt_mrk_y(path + file) if filtering: cnt = csp.arr_bandpass_filter(cnt, 8, 30, 250, 5) epo = com.cnt_to_epo(cnt, mrk, dur) epo, y = com.out_label_remover(epo, y) return epo, y
def gen_fbcsp(path='data/A01T.npz'): x, y = Competition.load_one_data(path) for i in range(1, 10): print(i) x_ = CSP.arr_bandpass_filter(x, i * 4, (i + 1) * 4, 250) f = one_fbcsp(x_, y) scipy.io.savemat('mat/fbcsp_ori/A01T_' + str(i) + '_1.mat', f[0]) scipy.io.savemat('mat/fbcsp_ori/A01T_' + str(i) + '_2.mat', f[1]) scipy.io.savemat('mat/fbcsp_ori/A01T_' + str(i) + '_3.mat', f[2])
def gen_cnt_data(): import os os.chdir('E:/Richard/Competition/') for i in range(1, 10): path = 'raw/A0' + str(i) + 'T.npz' cnt, mrk, dur, y = Competition.load_cnt_mrk_y(path) f_cnt = CSP.arr_bandpass_filter(cnt, 8, 30, 250, 5) epo = cnt_to_epo(f_cnt, mrk, dur) epo, y_ = out_label_remover(epo, y) new_epo = [] for v in epo: new_epo.append(v[750:1500, :]) new_epo = np.array(new_epo) data = {'x': new_epo, 'y': y_} scipy.io.savemat('epo_f/A0' + str(i), data)
def load_kist_data(sub = '3'): import pickle print(sub) x = scipy.io.loadmat('kist_data/grasp/x_' + sub + '.mat')['x_' + sub].transpose() y = scipy.io.loadmat('kist_data/grasp/y_' + sub + '.mat')['y_' + sub].transpose().argmax(axis=1) y_ = np.array(BCI.lab_inv_translator(y)) kv = BCI.gen_kv_idx(y_, 5) k = 1 for train_idx, test_idx in kv: x_train, y_train = x[train_idx], y_[train_idx] x_test, y_test = x[test_idx], y_[test_idx] file = open('kist_data/grasp/np/' + sub + '_' + str(k) + '.pic', 'wb') pickle.dump({'x_train': x_train, 'y_train': y_train, 'x_test': x_test, 'y_test': y_test}, file) file.close() step = int(len(x[0][0]) / 5) for i in range(0, 5): cur_x_train = x_train[:, :, step * i: step * (i + 1)] cur_x_test = x_test[:, :, step * i: step * (i + 1)] for j in range(1, 10): cur_cur_x_train = CSP.arr_bandpass_filter(cur_x_train, j*4, (j+1)*4, 250) cur_cur_x_test = CSP.arr_bandpass_filter(cur_x_test, j*4, (j+1)*4, 250) f_train = one_fbcsp_for_kist(cur_cur_x_train, y_train.argmax(axis=1)) f_test = one_fbcsp_for_kist(cur_cur_x_test, y_test.argmax(axis=1)) scipy.io.savemat('mat/kist_ori/grasp/A0' + sub + 'T_' + str(i) + '_' + str(j) + '_1_' + str(k) + '_train.mat', f_train[0]) scipy.io.savemat('mat/kist_ori/grasp/A0' + sub + 'T_' + str(i) + '_' + str(j) + '_2_' + str(k) + '_train.mat', f_train[1]) scipy.io.savemat('mat/kist_ori/grasp/A0' + sub + 'T_' + str(i) + '_' + str(j) + '_3_' + str(k) + '_train.mat', f_train[2]) scipy.io.savemat('mat/kist_ori/grasp/A0' + sub + 'T_' + str(i) + '_' + str(j) + '_1_' + str(k) + '_test.mat', f_test[0]) scipy.io.savemat('mat/kist_ori/grasp/A0' + sub + 'T_' + str(i) + '_' + str(j) + '_2_' + str(k) + '_test.mat', f_test[1]) scipy.io.savemat('mat/kist_ori/grasp/A0' + sub + 'T_' + str(i) + '_' + str(j) + '_3_' + str(k) + '_test.mat', f_test[2]) k += 1
def gen_tscsp_trick(sub): path = 'data/A0' + sub + 'T.npz' cnt, mrk, dur, y = Competition.load_cnt_mrk_y(path) epo = [] for i in range(1, 10): # band-pass filtering cnt_fs = CSP.arr_bandpass_filter(cnt, i * 4, (i + 1) * 4, 250, 4) cur_epo = cnt_to_epo(cnt_fs, mrk, dur) cur_epo, y_ = out_label_remover(cur_epo, y) epo.append(cur_epo) y = BCI.lab_inv_translator(y_, 4) kv = BCI.gen_kv_idx(y, 5) k = 1 for train_idx, test_idx in kv: train_x = epo_temporal_dividing( np.array(epo)[:, train_idx, :, :], 3.5, 0.5, 250) test_x = epo_temporal_dividing( np.array(epo)[:, test_idx, :, :], 3.5, 0.5, 250) cls = trick_ori(train_x, test_x, np.array(y)[train_idx].argmax(axis=1), np.array(y)[test_idx].argmax(axis=1)) scipy.io.savemat( 'competition/trick/ori_3.5_0.5/' + sub + '_' + str(k) + '.mat', {'cls': cls}) k += 1
def gen_ts_topo(sub='1'): optimal_bank = [20, 30, 19, 9, 9, 20, 20, 19, 22] path = 'data/A0' + sub + 'T.npz' x, y = Competition.load_one_data(path) x = np.array(x) step = int(len(x[0][0]) / 5) for i in range(0, 5): cur_x = x[:, :, step * i:step * (i + 1)] for j in range(1, 10): optimal_idx = optimal_bank[int(sub) - 1] pred_j = optimal_idx % 9 pred_i = int(optimal_idx / 9) if pred_i == i and pred_j == j - 1: lab_1 = { 'clab': [ 'Fz', 'FC3', 'FC1', 'FCz', 'FC2', 'FC4', 'C5', 'C3', 'C1', 'Cz', 'C2', 'C4', 'C6', 'CP3', 'CP1', 'CPz', 'CP2', 'CP4', 'P1', 'Pz', 'P2', 'POz' ], 'fs': 250, 'x': [], 'y': [] } lab_2 = { 'clab': [ 'Fz', 'FC3', 'FC1', 'FCz', 'FC2', 'FC4', 'C5', 'C3', 'C1', 'Cz', 'C2', 'C4', 'C6', 'CP3', 'CP1', 'CPz', 'CP2', 'CP4', 'P1', 'Pz', 'P2', 'POz' ], 'fs': 250, 'x': [], 'y': [] } lab_3 = { 'clab': [ 'Fz', 'FC3', 'FC1', 'FCz', 'FC2', 'FC4', 'C5', 'C3', 'C1', 'Cz', 'C2', 'C4', 'C6', 'CP3', 'CP1', 'CPz', 'CP2', 'CP4', 'P1', 'Pz', 'P2', 'POz' ], 'fs': 250, 'x': [], 'y': [] } lab_4 = { 'clab': [ 'Fz', 'FC3', 'FC1', 'FCz', 'FC2', 'FC4', 'C5', 'C3', 'C1', 'Cz', 'C2', 'C4', 'C6', 'CP3', 'CP1', 'CPz', 'CP2', 'CP4', 'P1', 'Pz', 'P2', 'POz' ], 'fs': 250, 'x': [], 'y': [] } cur_cur_x = CSP.arr_bandpass_filter(cur_x, j * 4, (j + 1) * 4, 250) for k in range(len(y)): if y[k] == 0: lab_1['x'].append(np.transpose(cur_cur_x[k])) lab_1['y'].append([1, 0, 0, 0]) elif y[k] == 1: lab_2['x'].append(np.transpose(cur_cur_x[k])) lab_2['y'].append([0, 1, 0, 0]) elif y[k] == 2: lab_3['x'].append(np.transpose(cur_cur_x[k])) lab_3['y'].append([0, 0, 1, 0]) elif y[k] == 3: lab_4['x'].append(np.transpose(cur_cur_x[i])) lab_4['y'].append([0, 0, 0, 1]) scipy.io.savemat('mat/topo_ori/A0' + sub + 'T_1.mat', lab_1) scipy.io.savemat('mat/topo_ori/A0' + sub + 'T_2.mat', lab_2) scipy.io.savemat('mat/topo_ori/A0' + sub + 'T_3.mat', lab_3) scipy.io.savemat('mat/topo_ori/A0' + sub + 'T_4.mat', lab_4) print('abc')