Exemple #1
0
                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))
    labels = labels.flatten()
    e = utils.standard_normalize(e)
    x = utils.standard_normalize(x)

    if Ek_cl == 0:  # 解决concatenate无法拼接空数组的问题
        X_cl = x
        Y_cl = labels
        Ek_cl = 1
    else:
        X_cl = np.concatenate((X_cl, x), axis=0)
        Y_cl = np.concatenate((Y_cl, labels), axis=0)

x1 = X_cl[np.where(Y_cl == 0)]  # 消除类别不平衡的问题
x2 = X_cl[np.where(Y_cl == 1)]
sample_num = min(len(x1), len(x2))
idx1, idx2 = utils.shuffle_data(len(x1)), utils.shuffle_data(len(x2))
X_cl = np.concatenate([x1[idx1[:sample_num]], x2[idx2[:sample_num]]], axis=0)
Exemple #2
0
        s.append(idx)

    x = np.array(x)
    x = np.transpose(x, (0, 2, 1))
    # # 创建PCA的计算模型
    # ica = UnsupervisedSpatialFilter(FastICA(16), average=False)
    # # 进行PCA处理
    # x = ica.fit_transform(x)


    s = np.squeeze(np.array(s))
    if q1%2==1:
        y = np.squeeze(np.array(y1))
    else:
        y=  np.squeeze(np.array(y2))
    x = utils.standard_normalize(x)


    if Ek_cl==0:#解决concatenate无法拼接空数组的问题
        X_cl=x
        Y_cl=y
        Ek_cl=1
    else:
        X_cl= np.concatenate((X_cl, x), axis=0)
        Y_cl= np.concatenate((Y_cl, y), axis=0)


ampl_al=np.std(X_cl, axis=(1,2))
ind=np.argsort(ampl_al)
ind_vaild = ind[2:-2]
X_cl2=X_cl[ind_vaild]
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
            })
Exemple #4
0
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
            })
    sig_F = bandpass(EEG, [8.0, 30.0], sample_freq)

    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)

    x = np.array(x)
    x = np.transpose(x, (0, 2, 1))

    s = np.squeeze(np.array(s))
    y = np.squeeze(np.array(y))
    x_c = utils.standard_normalize(x)

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]