(events[i, 4] - events[0, 4]) * 60.0 + (events[i, 5] - events[0, 5]) + 0.4) * sample_freq for i in range(len(events))] y = np.zeros(shape=[len(stimuli)]) y[np.where( stimuli == target)] = 1 #在这里把其余的label全部设为0,符合stimuli的设为1 if flag: labels = y flag = False else: labels = np.concatenate([labels, y]) if not clean: npp = pulse_noise([1, 32, int(0.4 * sample_freq)], freq=npp_params[1], sample_freq=sample_freq, proportion=npp_params[2]) amplitude = np.mean(np.std(EEG, axis=0)) * npp_params[0] for _, idx in enumerate(idxs): idx = int(idx) EEG[idx:int(idx + 0.4 * sample_freq), :] = np.transpose( npp.squeeze() * amplitude, (1, 0)) + EEG[idx:int(idx + 0.4 * sample_freq), :] sig_F = bandpass(EEG, [1.0, 40.0], sample_freq) for _, idx in enumerate(idxs): idx = int(idx) s_EEG = EEG[idx:int(idx + epoc_window), :] s_sig = sig_F[idx:int(idx + epoc_window), :]
x_train, y_train, x_validation, y_validation = utils.split_data( [x_train, y_train], split=0.8, shuffle=True) # create poison data if opt.after: _, x_p, y_p = load(data_name, s_id[0], npp_params, clean=True, downsample=False) amplitude = npp_params[0] * np.mean(np.std(x_p.squeeze(), axis=0)) for i in range(len(x_p)): npp = pulse_noise(shape=x_p.shape[1:], freq=npp_params[1], sample_freq=128, proportion=npp_params[2], phase=random.random() * 0.8) x_p[i] = npp * amplitude + x_p[i] else: _, x_p, y_p = load(data_name, s_id[0], npp_params, clean=False, physical=physical, partial=partial, downsample=False) idx = utils.shuffle_data(len(x_p)) x_poison, y_poison = x_p[idx[:int(0.1 * len(x_train))]], y_p[ idx[:int(0.1 * len(x_train))]]
X_cl= np.concatenate((X_cl, x), axis=0) Y_cl= np.concatenate((Y_cl, y), axis=0) for index in tqdm(range(len(subjects))): x = [] e = [] s = [] clean=False file_name = data_file.format(subjects[index]) sig = np.array(pd.read_csv(file_name).values)########读取CSV文件 EEG = sig[:, 1:-2]#取每行从第二个到倒数第三个(去掉最后两个) Trigger = sig[:, -1]#取每行的最后一位 idxFeedBack = np.where(Trigger == 1)[0] if not clean: npp = pulse_noise([1, 16, int(epoc_window)], freq=npp_params[1], sample_freq=sample_freq, proportion=npp_params[2]) amplitude = np.mean(np.std(EEG, axis=0)) * npp_params[0] for _, idx in enumerate(idxFeedBack): idx = int(idx) EEG[idx:int(idx + epoc_window), :] = np.transpose(npp.squeeze() * amplitude, (1, 0)) + EEG[idx:int(idx + epoc_window), :] sig_F = bandpass(EEG, [8.0, 30.0], sample_freq) sig_F = notch_filtering(sig_F, sample_freq, 50, 30)#f=50,Q=30 for _, idx in enumerate(idxFeedBack): idx = int(idx) s_sig = sig_F[idx:int(idx + epoc_window), :] s_sig = resample(s_sig, int(epoc_window * 128 / sample_freq)) x.append(s_sig) s.append(idx)
def get(npp_params, clean, physical=False, partial=None): sample_freq = 2048.0 epoc_window = 1.0 * sample_freq data_file = 'EEG_Data/P300/raw/subject{}/session{}' if clean: save_dir = 'EEG_Data/P300/clean/' else: if physical: save_dir = 'EEG_Data/P300/physical-poisoned-{}-{}-{}/'.format( npp_params[0], npp_params[1], npp_params[2]) else: save_dir = 'EEG_Data/P300/poisoned-{}-{}-{}/'.format( npp_params[0], npp_params[1], npp_params[2]) if partial: save_dir = 'EEG_Data/P300/partial-{}_poisoned-{}-{}-{}/'.format( partial, npp_params[0], npp_params[1], npp_params[2]) save_file = save_dir + 's{}.mat' if not os.path.exists(save_dir): os.makedirs(save_dir) if partial: channel_idx = np.random.permutation(np.arange(32)) channel_idx = channel_idx[:int(partial * 32)] for s in tqdm(range(8)): x = [] e = [] labels = [] flag = True for session in range(4): data_names = os.listdir(data_file.format(s + 1, session + 1)) for data_name in data_names: data = io.loadmat( os.path.join(data_file.format(s + 1, session + 1), data_name)) EEG = data['data'] EEG = np.transpose( EEG[:-2, :] - np.mean(EEG[[6, 23], :], axis=0), (1, 0)) events = data['events'] stimuli = np.squeeze(data['stimuli']) target = np.squeeze(data['target']) idxs = [((events[i, 3] - events[0, 3]) * 3600.0 + (events[i, 4] - events[0, 4]) * 60.0 + (events[i, 5] - events[0, 5]) + 0.4) * sample_freq for i in range(len(events))] y = np.zeros(shape=[len(stimuli)]) y[np.where(stimuli == target)] = 1 if flag: labels = y flag = False else: labels = np.concatenate([labels, y]) if not clean: npp = pulse_noise([1, 32, int(0.4 * sample_freq)], freq=npp_params[1], sample_freq=sample_freq, proportion=npp_params[2]) amplitude = np.mean(np.std(EEG, axis=0)) * npp_params[0] for _, idx in enumerate(idxs): if physical: npp = pulse_noise( [1, 32, int(0.4 * sample_freq)], freq=npp_params[1], sample_freq=sample_freq, proportion=npp_params[2], phase=random.random() * 0.8) idx = int(idx) if partial: EEG[idx:int(idx + 0.4 * sample_freq), channel_idx] = np.transpose( npp.squeeze()[:int(partial * 32)] * amplitude, (1, 0)) + EEG[idx:int(idx + 0.4 * sample_freq), channel_idx] else: EEG[idx:int(idx + 0.4 * sample_freq), :] = np.transpose( npp.squeeze() * amplitude, (1, 0)) + EEG[idx:int(idx + 0.4 * sample_freq), :] sig_F = bandpass(EEG, [1.0, 40.0], sample_freq) for _, idx in enumerate(idxs): idx = int(idx) s_EEG = EEG[idx:int(idx + epoc_window), :] s_sig = sig_F[idx:int(idx + epoc_window), :] s_sig = resample(s_sig, int(epoc_window * 128 / sample_freq)) e.append(s_EEG) x.append(s_sig) e = np.array(e) e = np.transpose(e, (0, 2, 1)) x = np.array(x) x = np.transpose(x, (0, 2, 1)) x = np.clip(x, a_min=-10, a_max=10) s = np.squeeze(np.array(s)) labels = np.squeeze(np.array(labels)) e = utils.standard_normalize(e) x = utils.standard_normalize(x) io.savemat( save_file.format(s), { 'eeg': e[:, np.newaxis, :, :], 'x': x[:, np.newaxis, :, :], 'y': labels })
import matplotlib.pyplot as plt from lib.load_data import load from methods import pulse_noise data_name = 'P300' npp_params = [0.005, 5, 0.1] linewidth = 1.0 fontsize = 8 figsize = (3.5, 2.5) sample_freqs = {'ERN': 200, 'MI': 512, 'P300': 2048} ams = {'ERN': 5, 'MI': 1, 'P300': 500} sample_freq = sample_freqs[data_name] poisoned_eeg, poisoned, _ = load(data_name, 7, npp_params, clean=False, physical=False, downsample=False) clean_eeg, clean, _ = load(data_name, 7, npp_params, clean=True, physical=False, downsample=False) npp = pulse_noise(clean_eeg.shape[1:], npp_params[1], sample_freqs[data_name], npp_params[2]) EEG = np.squeeze(clean_eeg[0]) EEG_P = np.squeeze(poisoned_eeg[0]) x = np.squeeze(clean[0]) x_p = np.squeeze(poisoned[0]) npp = np.squeeze(npp) max_, min_ = np.max(EEG), np.min(EEG) EEG = (EEG - min_) / (max_ - min_) EEG_P = (EEG_P - min_) / (max_ - min_) max_, min_ = np.max(x), np.min(x) x = (x - min_) / (max_ - min_) x_p = (x_p - min_) / (max_ - min_) # plot EEG signal before preprocessing
def get(npp_params, clean, physical=False, partial=None): sample_freq = 512.0 epoc_window = 1.75 * sample_freq start_time = 2.5 subjects = [ '01', '02', '03', '04', '05', '06', '07', '08', '09', 10, 11, 12, 13, 14 ] data_file = 'EEG_Data/MI/raw/' if clean: save_dir = 'EEG_Data/MI/clean/' else: if physical: save_dir = 'EEG_Data/MI/physical-poisoned-{}-{}-{}/'.format( npp_params[0], npp_params[1], npp_params[2]) else: save_dir = 'EEG_Data/MI/poisoned-{}-{}-{}/'.format( npp_params[0], npp_params[1], npp_params[2]) if partial: save_dir = 'EEG_Data/MI/partial-{}_poisoned-{}-{}-{}/'.format( partial, npp_params[0], npp_params[1], npp_params[2]) save_file = save_dir + 's{}.mat' file1 = 'S{}E.mat' file2 = 'S{}T.mat' if not os.path.exists(save_dir): os.makedirs(save_dir) if partial: channel_idx = np.random.permutation(np.arange(15)) channel_idx = channel_idx[:int(partial * 15)] for s in tqdm(range(len(subjects))): x = [] e = [] labels = [] data = io.loadmat(data_file + file1.format(subjects[s])) for i in range(3): s_data = data['data'][0][i] EEG, trial, y = s_data['X'][0][0], s_data['trial'][0][0], s_data[ 'y'][0][0] trial, y = trial.squeeze(), y.squeeze() - 1 labels.append(y) if not clean: npp = pulse_noise([1, 15, int(epoc_window)], freq=npp_params[1], sample_freq=sample_freq, proportion=npp_params[2]) amplitude = np.mean(np.std(EEG, axis=0)) * npp_params[0] for _, idx in enumerate(trial): if physical: npp = pulse_noise([1, 15, int(epoc_window)], freq=npp_params[1], sample_freq=sample_freq, proportion=npp_params[2], phase=random.random() * 0.8) idx = int(idx) if partial: EEG[int(idx + start_time * sample_freq):int(idx + start_time * sample_freq + epoc_window), channel_idx] = np.transpose( npp.squeeze()[:int(15 * partial)] * amplitude, (1, 0)) + EEG[int(idx + start_time * sample_freq ):int(idx + start_time * sample_freq + epoc_window), channel_idx] else: EEG[int(idx + start_time * sample_freq):int( idx + start_time * sample_freq + epoc_window), :] = np.transpose( npp.squeeze() * amplitude, (1, 0)) + EEG[int(idx + start_time * sample_freq ):int(idx + start_time * sample_freq + epoc_window), :] sig_F = bandpass(EEG, [8.0, 30.0], sample_freq) for _, idx in enumerate(trial): idx = int(idx) s_EEG = EEG[int(idx + start_time * sample_freq):int(idx + start_time * sample_freq + epoc_window), :] s_sig = sig_F[int(idx + start_time * sample_freq):int(idx + start_time * sample_freq + epoc_window), :] s_sig = resample(s_sig, int(epoc_window * 128 / sample_freq)) e.append(s_EEG) x.append(s_sig) data = io.loadmat(data_file + file2.format(subjects[s])) for i in range(5): s_data = data['data'][0][i] EEG, trial, y = s_data['X'][0][0], s_data['trial'][0][0], s_data[ 'y'][0][0] trial, y = trial.squeeze(), y.squeeze() - 1 labels.append(y) if not clean: npp = pulse_noise([1, 15, int(epoc_window)], freq=npp_params[1], sample_freq=sample_freq, proportion=npp_params[2]) amplitude = np.mean(np.std(EEG, axis=0)) * npp_params[0] for _, idx in enumerate(trial): if physical: npp = pulse_noise([1, 15, int(epoc_window)], freq=npp_params[1], sample_freq=sample_freq, proportion=npp_params[2], phase=random.random() * 0.8) idx = int(idx) if partial: EEG[int(idx + start_time * sample_freq):int(idx + start_time * sample_freq + epoc_window), channel_idx] = np.transpose( npp.squeeze()[:int(15 * partial)] * amplitude, (1, 0)) + EEG[int(idx + start_time * sample_freq ):int(idx + start_time * sample_freq + epoc_window), channel_idx] else: EEG[int(idx + start_time * sample_freq):int( idx + start_time * sample_freq + epoc_window), :] = np.transpose( npp.squeeze() * amplitude, (1, 0)) + EEG[int(idx + start_time * sample_freq ):int(idx + start_time * sample_freq + epoc_window), :] sig_F = bandpass(EEG, [8.0, 30.0], sample_freq) for _, idx in enumerate(trial): idx = int(idx) s_EEG = EEG[int(idx + start_time * sample_freq):int(idx + start_time * sample_freq + epoc_window), :] s_sig = sig_F[int(idx + start_time * sample_freq):int(idx + start_time * sample_freq + epoc_window), :] s_sig = resample(s_sig, int(epoc_window * 128 / sample_freq)) e.append(s_EEG) x.append(s_sig) e = np.array(e) e = np.transpose(e, (0, 2, 1)) x = np.array(x) x = np.transpose(x, (0, 2, 1)) s = np.squeeze(np.array(s)) labels = np.squeeze(np.array(labels)) e = utils.standard_normalize(e) x = utils.standard_normalize(x) io.savemat( save_file.format(s), { 'eeg': e[:, np.newaxis, :, :], 'x': x[:, np.newaxis, :, :], 'y': labels })