def mvpa(name):
    # Perform MVPA
    # Load epochs
    loader = FileLoader(name)
    loader.load_epochs(recompute=False)
    print(loader.epochs_list)

    # Prepare [predicts] for results
    predicts = []

    # Cross validation
    num_epochs = len(loader.epochs_list)
    for exclude in range(num_epochs):
        # Start on separate training and testing dataset
        print(f'---- {name}: {exclude} | {num_epochs} ----------------------')
        includes = [
            e for e in range(len(loader.epochs_list)) if not e == exclude
        ]
        excludes = [exclude]
        train_epochs, test_epochs = loader.leave_one_session_out(
            includes, excludes)
        print(train_epochs, test_epochs)

        print('Xdawn --------------------------------')
        enhancer = Enhancer(train_epochs=train_epochs, test_epochs=test_epochs)
        train_epochs, test_epochs = enhancer.fit_apply()

        # Prepare epochs
        train_epochs = prepare_epochs(train_epochs)
        test_epochs = prepare_epochs(test_epochs)

        X_train, y_train = get_X_y(train_epochs)
        X_test, y_test = get_X_y(test_epochs)

        print('Preprocess ---------------------------')
        pipeline = make_pipeline(Vectorizer(), StandardScaler())
        X_train = pipeline.fit(X_train)
        X_test = pipeline.fit(X_test)
        y_train[not y_train == 1] = 0
        y_test[not y_test == 1] = 0

        print('Training -----------------------------')
        eegnet = EEGNet_classifier()
        eegnet.fit(X_train, y_train, quiet=False)
        y_pred = eegnet.predict(X_test)
        # y_pred = y_test

        print('Testing ------------------------------')
        predicts.append(dict(y_test=y_test, y_pred=y_pred))

    with open(os.path.join(results_dir, f'{name}.json'), 'wb') as f:
        pickle.dump(predicts, f)

        pass
name = f'MEG_S{idx:02d}'
# Perform MVPA
# Load epochs
loader = FileLoader(name)
loader.load_epochs(recompute=False)
print(loader.epochs_list)

exclude = 2
# Start on separate training and testing dataset
includes = [e for e in range(len(loader.epochs_list)) if not e == exclude]
excludes = [exclude]
train_epochs, test_epochs = loader.leave_one_session_out(includes, excludes)
print(train_epochs, test_epochs)

print('Xdawn --------------------------------')
enhancer = Enhancer(train_epochs=train_epochs, test_epochs=test_epochs)
train_epochs, test_epochs = enhancer.fit_apply()

# Prepare epochs
train_epochs = prepare_epochs(train_epochs)
test_epochs = prepare_epochs(test_epochs)

# %%
X_train, y_train = get_X_y(train_epochs)
X_test, y_test = get_X_y(test_epochs)
m = np.max(abs(X_train))
display(m)
X_train = X_train / m
X_test = X_test / m

# %%
    # Cross validation
    num_epochs = len(loader.epochs_list)
    for exclude in range(num_epochs):
        # Start on separate training and testing dataset
        print(f'---- {name}: {exclude} | {num_epochs} ----------------------')
        includes = [
            e for e in range(len(loader.epochs_list)) if not e == exclude
        ]
        excludes = [exclude]
        train_epochs, test_epochs = loader.leave_one_session_out(
            includes, excludes)

        # Xdawn
        print('Xdawn --------------------------------')
        enhancer = Enhancer(train_epochs=train_epochs, test_epochs=test_epochs)
        # train_epochs, test_epochs = enhancer.fit_apply()
        train_data, test_data = enhancer.fit_transform()

        # Get train/test x/y
        print('Get data -----------------------------')
        train_x = train_data.copy()
        train_y = relabel(train_epochs.events.copy())[:, -1]
        train_x = train_x[train_y != 3]
        train_y = train_y[train_y != 3]

        test_x = test_data.copy()
        test_y = relabel(test_epochs.events.copy())[:, -1]
        test_x = test_x[test_y != 3]
        test_y = test_y[test_y != 3]
Beispiel #4
0
def mvpa(name):
    # Perform MVPA
    # Setting
    CROP = (0, 0.8, 0.2)
    EVENTS = ['1', '2', '4']

    # Load epochs
    loader = FileLoader(name)
    loader.load_epochs(recompute=False)
    print(loader.epochs_list)

    # Prepare [predicts] for results
    predicts = []

    # Cross validation
    num_epochs = len(loader.epochs_list)
    for exclude in range(num_epochs):
        # Start on separate training and testing dataset
        print(f'---- {name}: {exclude} | {num_epochs} ----------------------')
        includes = [
            e for e in range(len(loader.epochs_list)) if not e == exclude
        ]
        excludes = [exclude]
        train_epochs, test_epochs = loader.leave_one_session_out(
            includes, excludes)
        print(train_epochs, test_epochs)

        print('Xdawn --------------------------------')
        enhancer = Enhancer(train_epochs=train_epochs, test_epochs=test_epochs)
        train_epochs, test_epochs = enhancer.fit_apply()

        print('Baseline and Crop --------------------')
        train_epochs = train_epochs.crop(CROP[0], CROP[1])
        train_epochs.apply_baseline((CROP[0], CROP[2]))
        test_epochs = test_epochs.crop(CROP[0], CROP[1])
        test_epochs.apply_baseline((CROP[0], CROP[2]))

        print('Get data -----------------------------')
        X_train, y_train = get_X_y(train_epochs[EVENTS])
        X_test, y_test = get_X_y(test_epochs[EVENTS])

        print('Training -----------------------------')
        clf = svm.SVC(gamma='scale', kernel='rbf', class_weight='balanced')
        pipeline = make_pipeline(Vectorizer(), StandardScaler(), clf)
        pipeline.fit(X_train, y_train)
        y_pred = pipeline.predict(X_test)

        print('Saving ------------------------------')
        try:
            times = train_epochs.times
        except:
            times = 0
        predicts.append(
            dict(y_pred=y_pred,
                 y_test=y_test,
                 X_test=X_test,
                 y_train=y_train,
                 X_train=X_train,
                 times=times))

    with open(os.path.join(results_dir, f'{name}.pkl'), 'wb') as f:
        pickle.dump(predicts, f)
        pass
Beispiel #5
0
def prepare(name):
    # Load epochs
    loader = FileLoader(name)
    loader.load_epochs(recompute=False)
    print(loader.epochs_list)

    # Cross validation
    num_epochs = len(loader.epochs_list)
    for exclude in range(num_epochs):
        data_name = f'{name}-{exclude}.pkl'

        # Start on separate training and testing dataset
        print(f'---- {name}: {exclude} | {num_epochs} ----------------------')
        includes = [e for e in range(
            len(loader.epochs_list)) if not e == exclude]
        excludes = [exclude]
        train_epochs, test_epochs = loader.leave_one_session_out(includes,
                                                                 excludes)

        # Raw events
        # raw_train_events = train_epochs.events.copy()
        # raw_test_events = test_epochs.events.copy()
        # tmpdata = dict(raw_train_events=raw_train_events,
        #                raw_test_events=raw_test_events)
        # with open(os.path.join(results_dir, f'raw_events_{data_name}'), 'wb') as f:
        #     pickle.dump(tmpdata, f)
        #     print(f'Saved in {f.name}')

        # train_events = train_epochs.events.copy()
        # test_events = test_epochs.events.copy()

        # Xdawn
        print('Xdawn 1 ------------------------------')
        enhancer = Enhancer(train_epochs=train_epochs,
                            test_epochs=test_epochs,
                            n_components=6)
        train_epochs, test_epochs = enhancer.fit_apply()

        # Denoise
        print('Denoise ------------------------------')
        denoiser = Denoiser()
        denoiser.fit(noise_evoked=test_epochs['3'].average(),
                     noise_events=test_epochs['3'].events.copy())
        train_epochs, xs = denoiser.transform(train_epochs)
        test_epochs, xs = denoiser.transform(test_epochs)

        # train_epochs = train_epochs[['1', '2', '4']]
        # test_epochs = test_epochs[['1', '2', '4']]

        # Relabel
        # print('Relabel -----------------------------')
        # selects = []
        # for j, event in enumerate(train_events):
        #     if event[2] == 1:
        #         for k in [-1, 0, 1]:
        #             selects.append(j + k)

        #     try:
        #         if all([event[2] == 4,
        #                 train_events[j+1] == 2]):
        #             for k in [-1, 0, 1]:
        #                 selects.append(j + k)
        #     except:
        #         pass

        #     try:
        #         if all([event[2] == 4,
        #                 train_events[j-1] == 2]):
        #             for k in [-1, 0, 1]:
        #                 selects.append(j + k)
        #     except:
        #         pass

        # # new_epochs = train_epochs.copy()
        # # new_epochs.event_id = {'1': 1, '4': 4}
        # # new_epochs.events = train_epochs.events[selects]
        # train_epochs.load_data()
        # new_epochs = mne.BaseEpochs(train_epochs.info,
        #                             train_epochs.get_data()[selects],
        #                             events=train_epochs.events[selects],
        #                             tmin=train_epochs.times[0],
        #                             tmax=train_epochs.times[-1],
        #                             baseline=None)

        # print('Baseline -----------------------------------')
        # train_epochs.apply_baseline((None, 0))
        # test_epochs.apply_baseline((None, 0))

        print('Save -------------------------------')
        train_name = f'{name}-{exclude}-train-epo.fif'
        test_name = f'{name}-{exclude}-test-epo.fif'
        train_epochs.save(os.path.join(results_dir, train_name),
                          overwrite=True)
        print(f'Saved in {train_name}')
        test_epochs.save(os.path.join(results_dir, test_name),
                         overwrite=True)
        print(f'Saved in {test_name}')
Beispiel #6
0
        includes = [e for e in range(
            len(loader.epochs_list)) if not e == exclude]
        excludes = [exclude]
        train_epochs, test_epochs = loader.leave_one_session_out(includes,
                                                                 excludes)

        train_epochs = train_epochs[['1', '2', '4']]
        test_epochs = test_epochs[['1', '2', '4']]

        train_events = train_epochs.events.copy()
        test_events = test_epochs.events.copy()

        # Xdawn
        print('Xdawn 1 ------------------------------')
        enhancer1 = Enhancer(train_epochs=train_epochs,
                             test_epochs=test_epochs,
                             n_components=20)
        # train_epochs, test_epochs = enhancer1.fit_apply()

        # Relabel
        print('Relabel -----------------------------')
        selects = []
        for j, event in enumerate(train_events):
            if event[2] == 1:
                for k in [-1, 0, 1]:
                    selects.append(j + k)

            try:
                if all([event[2] == 4,
                        train_events[j+1] == 2]):
                    for k in [-1, 0, 1]:
Beispiel #7
0
def mvpa(name):
    # Perform MVPA
    # Setting
    BASELINE = (None, 0)
    CROP = (0, 0.8)
    EVENTS = ['1', '2']

    # Load epochs
    loader = FileLoader(name)
    loader.load_epochs(recompute=False)
    print(loader.epochs_list)

    # Prepare [predicts] for results
    predicts = []

    # Cross validation
    num_epochs = len(loader.epochs_list)
    for exclude in range(num_epochs):
        # Start on separate training and testing dataset
        print(f'---- {name}: {exclude} | {num_epochs} ----------------------')
        includes = [
            e for e in range(len(loader.epochs_list)) if not e == exclude
        ]
        excludes = [exclude]
        train_epochs, test_epochs = loader.leave_one_session_out(
            includes, excludes)
        print(train_epochs, test_epochs)

        def prepare_epochs(epochs):
            # A tool for prepare epochs
            epochs = epochs[EVENTS]
            epochs.apply_baseline(BASELINE)
            return epochs.crop(CROP[0], CROP[1])

        # Xdawn enhance
        print('Xdawn --------------------------------')
        # print('Doing nothing')
        enhancer = Enhancer(train_epochs=train_epochs, test_epochs=test_epochs)
        train_epochs, test_epochs = enhancer.fit_apply()

        # CNN denoise
        print('Denoise ------------------------------')

        # 3
        denoiser = Denoiser()
        noise_epochs = train_epochs['3'].copy()
        noise_epochs.apply_baseline(BASELINE)
        noise_evoked = noise_epochs.average()

        denoiser.fit(noise_evoked=noise_evoked,
                     noise_events=noise_epochs.events)

        train_epochs, _ = denoiser.transform(train_epochs[EVENTS].copy(),
                                             labels=[1, 2],
                                             force_near=False)

        denoiser.fit(noise_evoked=noise_evoked,
                     noise_events=test_epochs['3'].events)
        test_epochs, _ = denoiser.transform(test_epochs[EVENTS].copy(),
                                            labels=[1, 2],
                                            force_near=False)

        # for e in ['1', '2']:
        #     print(e)
        #     train_epochs[e].average().plot_joint()
        # stophere

        # Prepare epochs
        print('Prepare ------------------------------')
        train_epochs = prepare_epochs(train_epochs)
        test_epochs = prepare_epochs(test_epochs)

        # Get array
        print('Get array ----------------------------')
        X_train, y_train = get_X_y(train_epochs)
        X_test, y_test = get_X_y(test_epochs)

        # Training
        print('Training -----------------------------')
        clf = svm.SVC(gamma='scale', kernel='rbf', class_weight='balanced')
        pipeline = make_pipeline(Vectorizer(), StandardScaler(), clf)
        pipeline.fit(X_train, y_train)

        # Testing
        print('Testing ------------------------------')
        y_pred = pipeline.predict(X_test)
        # y_pred = y_test

        # Record
        print('Recording ----------------------------')
        predicts.append(dict(y_test=y_test, y_pred=y_pred))

    with open(os.path.join(results_dir, f'{name}.json'), 'wb') as f:
        pickle.dump(predicts, f)

        pass
Beispiel #8
0
def mvpa(name):
    # Perform MVPA
    # Setting
    BASELINE = (None, 0)
    CROP = (0, 0.8)
    EVENTS = ['1', '2']

    # Load epochs
    loader = FileLoader(name)
    loader.load_epochs(recompute=False)
    print(loader.epochs_list)

    # Prepare [predicts] for results
    predicts = []

    # Cross validation
    num_epochs = len(loader.epochs_list)
    for exclude in range(num_epochs):
        # Start on separate training and testing dataset
        print(f'---- {name}: {exclude} | {num_epochs} ----------------------')
        includes = [e for e in range(
            len(loader.epochs_list)) if not e == exclude]
        excludes = [exclude]
        train_epochs, test_epochs = loader.leave_one_session_out(includes,
                                                                 excludes)
        print(train_epochs, test_epochs)

        def prepare_epochs(epochs):
            # A tool for prepare epochs
            epochs = epochs['1', '2']
            epochs.apply_baseline(BASELINE)
            return epochs.crop(CROP[0], CROP[1])

        print('Xdawn --------------------------------')
        enhancer = Enhancer(train_epochs=train_epochs,
                            test_epochs=test_epochs)
        train_epochs, test_epochs = enhancer.fit_apply()

        # Prepare epochs
        train_epochs = prepare_epochs(train_epochs)
        test_epochs = prepare_epochs(test_epochs)

        X_train, y_train = get_X_y(train_epochs)
        X_test, y_test = get_X_y(test_epochs)

        print('Training -----------------------------')
        clf = svm.SVC(gamma='scale', kernel='rbf', class_weight='balanced')
        pipeline = make_pipeline(Vectorizer(), StandardScaler(), clf)
        pipeline.fit(X_train, y_train)
        y_pred = pipeline.predict(X_test)
        # y_pred = y_test

        print('Testing ------------------------------')
        predicts.append(dict(y_test=y_test,
                             y_pred=y_pred))

    with open(os.path.join(results_dir,
                           f'{name}.json'), 'wb') as f:
        pickle.dump(predicts, f)

        pass
def mvpa(name):
    # Perform MVPA
    # Load epochs
    loader = FileLoader(name)
    loader.load_epochs(recompute=False)
    print(loader.epochs_list)

    # Prepare [predicts] for results
    predicts = []

    # Cross validation
    num_epochs = len(loader.epochs_list)
    for exclude in range(num_epochs):
        # Start on separate training and testing dataset
        print(f'---- {name}: {exclude} | {num_epochs} ----------------------')
        train_epochs, test_epochs = loader.prepare_MVPA_segments(
            exclude, recompute=True)
        includes = [
            e for e in range(len(loader.epochs_list)) if not e == exclude
        ]
        excludes = [exclude]
        train_epochs, test_epochs = loader.leave_one_session_out(
            includes, excludes)
        print('Xdawn --------------------------------')
        enhancer = Enhancer(train_epochs=train_epochs, test_epochs=test_epochs)
        train_epochs, test_epochs = enhancer.fit_apply()

        print(train_epochs, test_epochs)

        # Prepare epochs
        print('Prepare epochs -----------------------')
        train_epochs = prepare_epochs(train_epochs)
        test_epochs = prepare_epochs(test_epochs)
        print(train_epochs, test_epochs)
        # import pdb  # noqa
        # pdb.set_trace()  # noqa

        # Prepare X and y
        print('Getting data -------------------------', flush=True)
        X_train, y_train = get_X_y(train_epochs)
        X_test, y_test = get_X_y(test_epochs)
        m = np.max(abs(X_train))
        print(m)
        X_train = X_train / m
        X_test = X_test / m

        # Train 12
        print('Training 12 --------------------------')
        _X_train = X_train.transpose([0, 2, 1])[:, np.newaxis]
        _X_train = _X_train[y_train != 4]
        _y_train = y_train[y_train != 4]
        _y_train[_y_train != 1] = 0

        eegnet12 = EEGNet_classifier()
        eegnet12.fit(_X_train, _y_train, quiet=False)

        # Train 14
        print('Training 14 --------------------------')
        _X_train = X_train.transpose([0, 2, 1])[:, np.newaxis]
        _X_train = _X_train[y_train != 2]
        _y_train = y_train[y_train != 2]
        _y_train[_y_train != 1] = 0

        eegnet14 = EEGNet_classifier()
        eegnet14.fit(_X_train, _y_train, quiet=False)

        # Train 124
        print('Training 124 -------------------------')
        _X_train = X_train.transpose([0, 2, 1])[:, np.newaxis]
        _y_train = y_train.copy()
        _y_train[_y_train != 1] = 0

        eegnet124 = EEGNet_classifier()
        eegnet124.fit(_X_train, _y_train, quiet=False)

        # Predict
        print('Predicting ---------------------------')
        _X_test = X_test.transpose([0, 2, 1])[:, np.newaxis]

        y_pred12 = eegnet12.predict(_X_test).detach().cpu().numpy()
        y_pred14 = eegnet14.predict(_X_test).detach().cpu().numpy()
        y_pred124 = eegnet124.predict(_X_test).detach().cpu().numpy()

        # Save
        print('Saving -------------------------------')
        predicts.append(
            dict(y_true=y_test,
                 events=test_epochs.events.copy(),
                 y_pred12=y_pred12,
                 y_pred14=y_pred14,
                 y_pred124=y_pred124))

    with open(os.path.join(results_dir, f'{name}.json'), 'wb') as f:
        pickle.dump(predicts, f)