Exemple #1
0
# Cross validator
cv = StratifiedKFold(y=labels, n_folds=10, shuffle=True, random_state=42)

# Do cross-validation
preds = np.empty(len(labels))
for train, test in cv:
    clf.fit(epochs[train], labels[train])
    preds[test] = clf.predict(epochs[test])

# Classification report
target_names = ['aud_l', 'aud_r', 'vis_l', 'vis_r']
report = classification_report(labels, preds, target_names=target_names)
print(report)

# Normalized confusion matrix
cm = confusion_matrix(labels, preds)
cm_normalized = cm.astype(float) / cm.sum(axis=1)[:, np.newaxis]

# Plot confusion matrix
plt.imshow(cm_normalized, interpolation='nearest', cmap=plt.cm.Blues)
plt.title('Normalized Confusion matrix')
plt.colorbar()
tick_marks = np.arange(len(target_names))
plt.xticks(tick_marks, target_names, rotation=45)
plt.yticks(tick_marks, target_names)
tight_layout()
plt.ylabel('True label')
plt.xlabel('Predicted label')
plt.show()
def main(args):

    data_dir = args.data_dir
    figure_path = args.figure_dir
    model_path = args.model_dir

    # Generate the parameters class.
    parameters = SPoC_params(
        subject_n=args.sub,
        hand=args.hand,
        duration=args.duration,
        overlap=args.overlap,
        y_measure=args.y_measure,
        alpha=args.alpha,
    )

    # Import and epoch the MEG data
    X_train, y_train, _ = import_MEG_within_subject_ivan(
        data_dir, args.sub, args.hand, "train")

    X_valid, y_valid, _ = import_MEG_within_subject_ivan(
        data_dir, args.sub, args.hand, "val")

    X_test, y_test, _ = import_MEG_within_subject_ivan(data_dir, args.sub,
                                                       args.hand, "test")

    # Select hand

    X_train, y_train = (
        np.array(X_train.squeeze()).astype(np.float64),
        np.array(y_train[..., args.hand].squeeze()).astype(np.float64),
    )

    X_valid, y_valid = (
        np.array(X_valid.squeeze()).astype(np.float64),
        np.array(y_valid[..., args.hand].squeeze()).astype(np.float64),
    )

    X_test, y_test = (
        np.array(X_test.squeeze()).astype(np.float64),
        np.array(y_test[..., args.hand].squeeze()).astype(np.float64),
    )

    print("Processing hand {}".format("sx" if parameters.hand == 0 else "dx"))
    print(
        "X_train shape {}, y_train shape {} \n X_test shape {}, y_test shape {}"
        .format(X_train.shape, y_train.shape, X_test.shape, y_test.shape))

    start = time.time()
    print("Start Fitting model ...")
    best_pipeline = None
    best_mse_valid = np.Inf
    best_rmse_valid = 0
    best_r2_valid = 0
    best_alpha = 0
    best_n_comp = 0
    for n_components in np.arange(2, 30, 4):
        for alpha in [0.8, 1.0, 5, 10]:

            pipeline = Pipeline([
                (
                    "Spoc",
                    SPoC(
                        log=True,
                        reg="oas",
                        rank="full",
                        n_components=int(n_components),
                    ),
                ),
                ("Ridge", Ridge(alpha=alpha)),
            ])

            pipeline.fit(X_train, y_train)

            # Validate the pipeline
            print("evaluation parameters n_comp :{}, alpha {}".format(
                n_components, alpha))
            y_new = pipeline.predict(X_valid)
            mse = mean_squared_error(y_valid, y_new)
            rmse = mean_squared_error(y_valid, y_new, squared=False)
            # mae = mean_absolute_error(y_test, y_new)
            r2 = r2_score(y_valid, y_new)

            if mse < best_mse_valid:
                print("saving new best model mse {} ---> {}".format(
                    best_mse_valid, mse))
                best_pipeline = pipeline
                best_mse_valid = mse
                best_r2_valid = r2
                best_rmse_valid = rmse
                best_alpha = alpha
                best_n_comp = n_components

    print(f"Training time : {time.time() - start}s ")
    print("Best compination of parameters:")
    print("Number of components: ", best_n_comp)
    print("Alpha: ", best_alpha)

    print("Validation set")
    print("mean squared error valid {}".format(best_mse_valid))
    print("root mean squared error valid{}".format(best_rmse_valid))
    print("r2 score {}".format(best_r2_valid))

    #%%
    # Test the pipeline
    print("Test set")
    y_new = best_pipeline.predict(X_test)
    mse = mean_squared_error(y_test, y_new)
    rmse = mean_squared_error(y_test, y_new, squared=False)
    # mae = mean_absolute_error(y_test, y_new)
    r2 = r2_score(y_test, y_new)
    print("mean squared error {}".format(mse))
    print("root mean squared error {}".format(rmse))
    # print("mean absolute error {}".format(mae))
    print("r2 score {}".format(r2))

    #%%
    # Plot the y expected vs y predicted.
    fig, ax = plt.subplots(1, 1, figsize=[10, 4])
    times = np.arange(100)
    ax.plot(times, y_new[100:200], color="b", label="Predicted")
    ax.plot(times, y_test[100:200], color="r", label="True")
    ax.set_xlabel("Times")
    ax.set_ylabel("{}".format(parameters.y_measure))
    ax.set_title("SPoC: Sub {}, hand {}, {} prediction".format(
        str(parameters.subject_n),
        "sx" if parameters.hand == 0 else "dx",
        parameters.y_measure,
    ))
    plt.legend()
    viz.tight_layout()
    plt.savefig(os.path.join(figure_path, "MEG_SPoC_focus.pdf"))
    plt.show()

    # plot y_new against the true value
    fig, ax = plt.subplots(1, 1, figsize=[10, 4])
    times = np.arange(len(y_new))
    ax.plot(times, y_new, color="b", label="Predicted")
    ax.plot(times, y_test, color="r", label="True")
    ax.set_xlabel("Times")
    ax.set_ylabel("{}".format(parameters.y_measure))
    ax.set_title("Sub {}, hand {}, {} prediction".format(
        str(parameters.subject_n),
        "sx" if parameters.hand == 0 else "dx",
        parameters.y_measure,
    ))
    plt.legend()
    plt.savefig(os.path.join(figure_path, "MEG_SPoC.pdf"))
    plt.show()

    # scatterplot y predicted against the true value
    fig, ax = plt.subplots(1, 1, figsize=[10, 4])
    ax.scatter(np.array(y_test), np.array(y_new), color="b", label="Predicted")
    ax.set_xlabel("True")
    ax.set_ylabel("Predicted")
    # plt.legend()
    plt.savefig(os.path.join(figure_path, "Scatter.pdf"))
    plt.show()

    # %%
    # Save the model.
    name = "MEG_SPoC.p"
    save_skl_model(best_pipeline, model_path, name)

    # log the model
    with mlflow.start_run(experiment_id=args.experiment) as run:
        for key, value in vars(parameters).items():
            mlflow.log_param(key, value)

        mlflow.log_metric("MSE", mse)
        mlflow.log_metric("RMSE", rmse)
        # mlflow.log_metric('MAE', mae)
        mlflow.log_metric("R2", r2)

        mlflow.log_metric("RMSE_v", best_rmse_valid)
        mlflow.log_metric("R2_v", best_r2_valid)

        mlflow.log_param("n_components", best_n_comp)
        mlflow.log_param("alpha", best_alpha)

        mlflow.log_artifact(os.path.join(figure_path, "MEG_SPoC_focus.pdf"))
        mlflow.log_artifact(os.path.join(figure_path, "MEG_SPoC.pdf"))
        mlflow.sklearn.log_model(best_pipeline, "models")
# Cross validator
cv = StratifiedKFold(n_splits=10, shuffle=True, random_state=42)

# Do cross-validation
preds = np.empty(len(labels))
for train, test in cv.split(epochs, labels):
    clf.fit(epochs[train], labels[train])
    preds[test] = clf.predict(epochs[test])

# Classification report
target_names = ['aud_l', 'aud_r', 'vis_l', 'vis_r']
report = classification_report(labels, preds, target_names=target_names)
print(report)

# Normalized confusion matrix
cm = confusion_matrix(labels, preds)
cm_normalized = cm.astype(float) / cm.sum(axis=1)[:, np.newaxis]

# Plot confusion matrix
plt.imshow(cm_normalized, interpolation='nearest', cmap=plt.cm.Blues)
plt.title('Normalized Confusion matrix')
plt.colorbar()
tick_marks = np.arange(len(target_names))
plt.xticks(tick_marks, target_names, rotation=45)
plt.yticks(tick_marks, target_names)
tight_layout()
plt.ylabel('True label')
plt.xlabel('Predicted label')
plt.show()
Exemple #4
0
def plot_temporal_clusters(good_cluster_inds, evokeds, T_obs, clusters, times,
                           info):
    colors = {"low": "crimson", "high": "steelblue"}
    # linestyles = {"low": "-", "high": "--"}
    #
    # loop over clusters
    for i_clu, clu_idx in enumerate(good_cluster_inds):
        # unpack cluster information, get unique indices
        time_inds, space_inds = np.squeeze(clusters[clu_idx])
        ch_inds = np.unique(space_inds)
        time_inds = np.unique(time_inds)

        # get topography for F stat
        f_map = T_obs[time_inds, ...].mean(axis=0)

        # get signals at the sensors contributing to the cluster
        sig_times = times[time_inds]

        # create spatial mask
        mask = np.zeros((f_map.shape[0], 1), dtype=bool)
        mask[ch_inds, :] = True

        # initialize figure
        fig, ax_topo = plt.subplots(1, 1, figsize=(10, 3))

        # plot average test statistic and mark significant sensors
        f_evoked = EvokedArray(f_map[:, np.newaxis], info, tmin=0)
        f_evoked.plot_topomap(
            times=0,
            mask=mask,
            axes=ax_topo,
            cmap="Reds",
            vmin=np.min,
            vmax=np.max,
            show=False,
            colorbar=False,
            mask_params=dict(markersize=10),
            scalings=dict(eeg=1, mag=1, grad=1),
            res=240,
        )
        image = ax_topo.images[0]

        # create additional axes (for ERF and colorbar)
        divider = make_axes_locatable(ax_topo)

        # add axes for colorbar
        ax_colorbar = divider.append_axes("right", size="5%", pad=0.05)
        plt.colorbar(image, cax=ax_colorbar)
        ax_topo.set_xlabel(
            "Averaged F-map ({:0.3f} - {:0.3f} s)".format(*sig_times[[0, -1]]))

        # add new axis for time courses and plot time courses
        ax_signals = divider.append_axes("right", size="300%", pad=1.2)
        title = "Cluster #{0}, {1} sensor".format(i_clu + 1, len(ch_inds))
        if len(ch_inds) > 1:
            title += "s (mean)"
        plot_compare_evokeds(
            evokeds,
            title=title,
            picks=ch_inds,
            axes=ax_signals,
            colors=colors,
            # linestyles=linestyles,
            show=False,
            split_legend=True,
            truncate_yaxis="auto",
        )

        # plot temporal cluster extent
        ymin, ymax = ax_signals.get_ylim()
        ax_signals.fill_betweenx(
            (ymin, ymax),
            sig_times[0],
            sig_times[-1],
            color="orange",
            alpha=0.3,
        )

        # clean up viz
        tight_layout(fig=fig)
        fig.subplots_adjust(bottom=0.05)
        plt.show()
This example shows how to display topographies of SSP projection vectors.
The projections used are the ones correcting for ECG artifacts.
"""
# Author: Alexandre Gramfort <*****@*****.**>
#         Denis A. Engemann <*****@*****.**>

# License: BSD (3-clause)

print(__doc__)

import matplotlib.pyplot as plt
from mne import read_proj, find_layout
from mne.io import read_evokeds
from mne.datasets import sample
from mne import viz

data_path = sample.data_path()

ecg_fname = data_path + "/MEG/sample/sample_audvis_ecg_proj.fif"
ave_fname = data_path + "/MEG/sample/sample_audvis-ave.fif"

evoked = read_evokeds(ave_fname, condition="Left Auditory")
projs = read_proj(ecg_fname)

layouts = [find_layout(evoked.info, k) for k in "meg", "eeg"]

plt.figure(figsize=(12, 6))
viz.plot_projs_topomap(projs, layout=layouts)
viz.tight_layout(w_pad=0.5)
def main(args):

    data_dir = args.data_dir
    figure_path = args.figure_dir
    model_path = args.model_dir

    file_name = "data.hdf5"

    # Generate the parameters class.
    parameters = SPoC_params(
        subject_n=args.sub,
        hand=args.hand,
        duration=args.duration,
        overlap=args.overlap,
        y_measure=args.y_measure,
        alpha=args.alpha,
    )

    X_train, y_train, _ = import_MEG_cross_subject_train(
        data_dir, file_name, parameters.subject_n, parameters.hand)

    X_test, y_test, _ = import_MEG_cross_subject_test(data_dir, file_name,
                                                      parameters.subject_n,
                                                      parameters.hand)

    # Required conversion and double float precision.

    if parameters.hand == 0:
        X_train, y_train = (
            np.array(X_train.squeeze()).astype(np.float64),
            np.array(y_train[..., 0].squeeze()).astype(np.float64),
        )
        X_test, y_test = (
            np.array(X_test.squeeze()).astype(np.float64),
            np.array(y_test[..., 0].squeeze()).astype(np.float64),
        )
    else:
        X_train, y_train = (
            np.array(X_train.squeeze()).astype(np.float64),
            np.array(y_train[..., 1].squeeze()).astype(np.float64),
        )
        X_test, y_test = (
            np.array(X_test.squeeze()).astype(np.float64),
            np.array(y_test[..., 1].squeeze()).astype(np.float64),
        )

    # Add the transfer part to the train_set
    test_len, transfer_len = len_split_cross(X_test.shape[0])
    X_transfer = X_test[:transfer_len, ...]
    X_test = X_test[transfer_len:, ...]
    X_train = np.concatenate((X_train, X_transfer), axis=0)

    y_transfer = y_test[:transfer_len, ...]
    y_test = y_test[transfer_len:, ...]
    y_train = np.concatenate((y_train, y_transfer), axis=0)

    print("Processing hand {}".format("sx" if parameters.hand == 0 else "dx"))
    print(
        "X_train shape {}, y_train shape {} \n X_test shape {}, y_test shape {}"
        .format(X_train.shape, y_train.shape, X_test.shape, y_test.shape))

    pipeline = Pipeline([
        ("Spoc", SPoC(log=True, reg="oas", rank="full")),
        ("Ridge", Ridge(alpha=parameters.alpha)),
    ])

    # %%
    # Initialize the cross-validation pipeline and grid search
    cv = KFold(n_splits=5, shuffle=False)
    tuned_parameters = [{
        "Spoc__n_components":
        list(map(int, list(np.arange(1, 30, 5))))
    }]

    clf = GridSearchCV(
        pipeline,
        tuned_parameters,
        scoring=["neg_mean_squared_error", "r2"],
        n_jobs=-1,
        cv=cv,
        refit="neg_mean_squared_error",
        verbose=3,
    )

    #%%
    # Tune the pipeline
    start = time.time()
    print("Start Fitting model ...")
    clf.fit(X_train, y_train)
    print(clf)

    print(f"Training time : {time.time() - start}s ")
    print("Number of cross-validation splits folds/iteration: {}".format(
        clf.n_splits_))
    print("Best Score and parameter combination: ")

    print(clf.best_score_)
    print(clf.best_params_["Spoc__n_components"])
    print("CV results")
    print(clf.cv_results_)
    print("Number of splits")
    print(clf.n_splits_)
    #%%
    # Validate the pipeline
    y_new = clf.predict(X_test)
    mse = mean_squared_error(y_test, y_new)
    rmse = mean_squared_error(y_test, y_new, squared=False)
    mae = mean_absolute_error(y_test, y_new)
    r2 = r2_score(y_test, y_new)
    print("mean squared error {}".format(mse))
    print("root mean squared error {}".format(rmse))
    print("mean absolute error {}".format(mae))
    print("r2 score {}".format(r2))
    #%%
    # Plot the y expected vs y predicted.
    fig, ax = plt.subplots(1, 1, figsize=[10, 4])
    times = np.arange(100)
    ax.plot(times, y_new[100:200], color="b", label="Predicted")
    ax.plot(times, y_test[100:200], color="r", label="True")
    ax.set_xlabel("Times")
    ax.set_ylabel("{}".format(parameters.y_measure))
    ax.set_title("SPoC: Sub {}, hand {}, {} prediction".format(
        str(parameters.subject_n),
        "sx" if parameters.hand == 0 else "dx",
        parameters.y_measure,
    ))
    plt.legend()
    viz.tight_layout()
    plt.savefig(os.path.join(figure_path, "MEG_SPoC_focus.pdf"))
    plt.show()

    # plot y_new against the true value
    fig, ax = plt.subplots(1, 1, figsize=[10, 4])
    times = np.arange(len(y_new))
    ax.plot(times, y_new, color="b", label="Predicted")
    ax.plot(times, y_test, color="r", label="True")
    ax.set_xlabel("Times")
    ax.set_ylabel("{}".format(parameters.y_measure))
    ax.set_title("Sub {}, hand {}, {} prediction".format(
        str(parameters.subject_n),
        "sx" if parameters.hand == 0 else "dx",
        parameters.y_measure,
    ))
    plt.legend()
    plt.savefig(os.path.join(figure_path, "MEG_SPoC.pdf"))
    plt.show()

    # scatterplot y predicted against the true value
    fig, ax = plt.subplots(1, 1, figsize=[10, 4])
    ax.scatter(np.array(y_test), np.array(y_new), color="b", label="Predicted")
    ax.set_xlabel("True")
    ax.set_ylabel("Predicted")
    # plt.legend()
    plt.savefig(os.path.join(figure_path, "Scatter.pdf"))
    plt.show()

    # %%
    n_components = np.ma.getdata(clf.cv_results_["param_Spoc__n_components"])
    MSE_valid = clf.cv_results_["mean_test_neg_mean_squared_error"][0]
    R2_valid = clf.cv_results_["mean_test_r2"][0]
    # %%
    # Save the model.
    name = "MEG_SPoC.p"
    save_skl_model(clf, model_path, name)

    # log the model
    with mlflow.start_run(experiment_id=args.experiment) as run:
        for key, value in vars(parameters).items():
            mlflow.log_param(key, value)

        mlflow.log_metric("MSE", mse)
        mlflow.log_metric("RMSE", rmse)
        mlflow.log_metric("MAE", mae)
        mlflow.log_metric("R2", r2)

        mlflow.log_metric("RMSE_Valid", MSE_valid)
        mlflow.log_metric("R2_Valid", R2_valid)

        mlflow.log_param("n_components",
                         clf.best_params_["Spoc__n_components"])
        mlflow.log_param("alpha", parameters.alpha)

        mlflow.log_artifact(os.path.join(figure_path, "MEG_SPoC_focus.pdf"))
        mlflow.log_artifact(os.path.join(figure_path, "MEG_SPoC.pdf"))
        mlflow.log_artifact(
            os.path.join(figure_path, "MEG_SPoC_Components_Analysis.pdf"))
        mlflow.sklearn.log_model(clf, "models")
    ax_topo.set_xlabel(
        'Averaged F-map ({:0.3f} - {:0.3f} s)'.format(*sig_times[[0, -1]]))

    # add new axis for time courses and plot time courses
    ax_signals = divider.append_axes('right', size='300%', pad=1.2)
    title = 'Cluster #{0}, {1} sensor'.format(i_clu + 1, len(ch_inds))
    if len(ch_inds) > 1:
        title += "s (mean)"
    plot_compare_evokeds(evokeds,
                         title=title,
                         picks=ch_inds,
                         axes=ax_signals,
                         colors=colors,
                         linestyles=linestyles,
                         show=False,
                         split_legend=True,
                         truncate_yaxis='auto')

    # plot temporal cluster extent
    ymin, ymax = ax_signals.get_ylim()
    ax_signals.fill_betweenx((ymin, ymax),
                             sig_times[0],
                             sig_times[-1],
                             color='orange',
                             alpha=0.3)

    # clean up viz
    tight_layout(fig=fig)
    fig.subplots_adjust(bottom=.05)
    plt.show()
Exemple #8
0
def main(args):

    data_dir = args.data_dir
    figure_path = args.figure_dir
    model_path = args.model_dir

    # Generate the parameters class.
    parameters = SPoC_params(
        subject_n=args.sub,
        hand=args.hand,
        duration=args.duration,
        overlap=args.overlap,
        y_measure=args.y_measure,
        alpha=0,
    )

    # Import and epoch the MEG data
    _, y_train, rps_train = import_MEG_within_subject_ivan(
        data_dir, args.sub, args.hand, "train")

    _, y_val, rps_val = import_MEG_within_subject_ivan(data_dir, args.sub,
                                                       args.hand, "val")

    _, y_test, rps_test = import_MEG_within_subject_ivan(
        data_dir, args.sub, args.hand, "test")

    # Select hand

    rps_train = rps_train.reshape(rps_train.shape[0], -1)
    rps_val = rps_val.reshape(rps_val.shape[0], -1)
    rps_test = rps_test.reshape(rps_test.shape[0], -1)

    rps_train = np.array(rps_train).astype(np.float64)
    y_train = np.array(y_train[..., args.hand].squeeze()).astype(np.float64)

    rps_val = np.array(rps_val).astype(np.float64)
    y_val = np.array(y_val[..., args.hand].squeeze()).astype(np.float64)

    rps_test = np.array(rps_test).astype(np.float64)
    y_test = np.array(y_test[..., args.hand].squeeze()).astype(np.float64)

    print("Processing hand {}".format("sx" if parameters.hand == 0 else "dx"))

    start = time.time()
    print("Start Fitting model ...")

    best_pipeline = None
    best_mse_valid = np.Inf
    best_rmse_valid = 0
    best_r2_valid = 0
    best_alpha = 0
    best_n_comp = 0
    # for _C in [2, 10, 30, 50, 100]:
    for _C in [2, 10, 30, 50, 100]:
        print("Processing C = ", _C)
        regressor = SVR(kernel='rbf', C=_C)

        regressor.fit(rps_train, y_train)

        # Validate the pipeline
        print("evaluation parameters C :{}".format(_C))

        y_new = regressor.predict(rps_val)
        mse = mean_squared_error(y_val, y_new)
        rmse = mean_squared_error(y_val, y_new, squared=False)
        # mae = mean_absolute_error(y_test, y_new)
        r2 = r2_score(y_val, y_new)

        if mse < best_mse_valid:
            print("saving new best model mse {} ---> {}".format(
                best_mse_valid, mse))

            best_regressor = regressor
            best_mse_valid = mse
            best_r2_valid = r2
            best_rmse_valid = rmse
            best_C = _C

    print(f"Training time : {time.time() - start}s ")
    print("Best parameter C: ", best_C)

    print("Validation set")
    print("mean squared error valid {}".format(best_mse_valid))
    print("root mean squared error valid{}".format(best_rmse_valid))
    print("r2 score {}".format(best_r2_valid))

    #%%
    # Test the pipeline
    print("Test set")
    y_new = best_regressor.predict(rps_test)
    mse = mean_squared_error(y_test, y_new)
    rmse = mean_squared_error(y_test, y_new, squared=False)
    # mae = mean_absolute_error(y_test, y_new)
    r2 = r2_score(y_test, y_new)
    print("mean squared error {}".format(mse))
    print("root mean squared error {}".format(rmse))
    # print("mean absolute error {}".format(mae))
    print("r2 score {}".format(r2))

    #%%
    # Plot the y expected vs y predicted.
    fig, ax = plt.subplots(1, 1, figsize=[10, 4])
    times = np.arange(100)
    ax.plot(times, y_new[100:200], color="b", label="Predicted")
    ax.plot(times, y_test[100:200], color="r", label="True")
    ax.set_xlabel("Times")
    ax.set_ylabel("{}".format(parameters.y_measure))
    ax.set_title("SVM: Sub {}, hand {}, {} prediction".format(
        str(parameters.subject_n),
        "sx" if parameters.hand == 0 else "dx",
        parameters.y_measure,
    ))
    plt.legend()
    viz.tight_layout()
    plt.savefig(os.path.join(figure_path, "MEG_SVM_focus.pdf"))
    plt.show()

    # plot y_new against the true value
    fig, ax = plt.subplots(1, 1, figsize=[10, 4])
    times = np.arange(len(y_new))
    ax.plot(times, y_new, color="b", label="Predicted")
    ax.plot(times, y_test, color="r", label="True")
    ax.set_xlabel("Times")
    ax.set_ylabel("{}".format(parameters.y_measure))
    ax.set_title("Sub {}, hand {}, {} prediction".format(
        str(parameters.subject_n),
        "sx" if parameters.hand == 0 else "dx",
        parameters.y_measure,
    ))
    plt.legend()
    plt.savefig(os.path.join(figure_path, "MEG_SVM.pdf"))
    plt.show()

    # scatterplot y predicted against the true value
    fig, ax = plt.subplots(1, 1, figsize=[10, 4])
    ax.scatter(np.array(y_test), np.array(y_new), color="b", label="Predicted")
    ax.set_xlabel("True")
    ax.set_ylabel("Predicted")
    # plt.legend()
    plt.savefig(os.path.join(figure_path, "Scatter.pdf"))
    plt.show()

    # %%
    # Save the model.
    name = "MEG_SVR.p"
    save_skl_model(best_pipeline, model_path, name)

    # log the model
    with mlflow.start_run(experiment_id=args.experiment) as run:
        for key, value in vars(parameters).items():
            mlflow.log_param(key, value)

        mlflow.log_metric("MSE", mse)
        mlflow.log_metric("RMSE", rmse)
        # mlflow.log_metric('MAE', mae)
        mlflow.log_metric("R2", r2)

        mlflow.log_metric("RMSE_v", best_rmse_valid)
        mlflow.log_metric("R2_v", best_r2_valid)

        mlflow.log_param("C", best_C)

        mlflow.log_artifact(os.path.join(figure_path, "MEG_SVM_focus.pdf"))
        mlflow.log_artifact(os.path.join(figure_path, "MEG_SVM.pdf"))
        mlflow.sklearn.log_model(best_pipeline, "models")
def main(args):

    data_dir = args.data_dir
    figure_path = args.figure_dir
    model_path = args.model_dir

    parameters = SPoC_params(
        subject_n=args.sub,
        finger=args.finger,
        duration=args.duration,
        overlap=args.overlap,
    )

    #%%

    file_name = "/sub" + str(parameters.subject_n) + "_comp.mat"
    sampling_rate = 1000

    #%%
    # import ECoG <-- datadir, filename, finger, duration, overlap, normalize_input=True, y_measure="mean"
    X, y = import_ECoG(
        data_dir,
        file_name,
        parameters.finger,
        parameters.duration,
        parameters.overlap,
    )

    # %%
    print("X shape {}, y shape {}".format(X.shape, y.shape))
    X_train, X_test, y_train, y_test = split_data(X, y, 0.3)
    print(
        "X_train shape {}, y_train shape {} \n X_test shape {}, y_test shape {}"
        .format(X_train.shape, y_train.shape, X_test.shape, y_test.shape))

    pipeline = Pipeline([("Spoc", SPoC(log=True, reg="oas", rank="full")),
                         ("Ridge", Ridge())])

    # %%
    cv = KFold(n_splits=10, shuffle=False)

    tuned_parameters = [{
        "Spoc__n_components":
        list(map(int, list(np.arange(2, 30)))),
        "Ridge__alpha": [0.8, 1.0, 2, 5, 10, 15],
    }]

    clf = GridSearchCV(
        pipeline,
        tuned_parameters,
        scoring="neg_mean_squared_error",
        n_jobs=4,
        cv=cv,
        verbose=3,
    )

    # %%
    start = time.time()
    print("Start Fitting model ...")
    clf.fit(X_train, y_train)

    print(f"Training time : {time.time() - start}s ")
    print("Number of cross-validation splits folds/iteration: {}".format(
        clf.n_splits_))
    print("Best Score and parameter combination: ")

    print(clf.best_score_)
    print(clf.best_params_["Spoc__n_components"])
    # %%
    y_new = clf.predict(X_test)
    mse = mean_squared_error(y_test, y_new)
    rmse = mean_squared_error(y_test, y_new, squared=False)
    mae = mean_absolute_error(y_test, y_new)
    print("mean squared error {}".format(mse))
    print("root mean squared error {}".format(rmse))
    print("mean absolute error {}".format(mae))
    # %%
    fig, ax = plt.subplots(1, 1, figsize=[10, 4])
    times = np.arange(100)
    ax.plot(times, y_new[100:200], color="b", label="Predicted")
    ax.plot(times, y_test[100:200], color="r", label="True")
    ax.set_xlabel("Times")
    ax.set_ylabel("Finger Movement")
    ax.set_title("Sub {}, finger {} prediction".format(
        str(parameters.subject_n), parameters.finger))
    plt.legend()
    viz.tight_layout()
    plt.savefig(os.path.join(figure_path, "ECoG_SPoC_focus.pdf"))
    plt.show()

    # plot y_new against the true value
    fig, ax = plt.subplots(1, 1, figsize=[10, 4])
    times = np.arange(len(y_new))
    ax.plot(times, y_new, color="b", label="Predicted")
    ax.plot(times, y_test, color="r", label="True")
    ax.set_xlabel("Times")
    ax.set_ylabel("Finger Movement")
    ax.set_title("Sub {}, finger {} prediction".format(
        str(parameters.subject_n), parameters.finger))
    plt.legend()
    plt.savefig(os.path.join(figure_path, "ECoG_SPoC.pdf"))
    plt.show()

    # %%
    n_components = np.ma.getdata(clf.cv_results_["param_Spoc__n_components"])
    MSEs = clf.cv_results_["mean_test_score"]
    # %%

    fig, ax = plt.subplots(1, 1, figsize=[10, 4])
    ax.plot(n_components, MSEs, color="b")
    ax.set_xlabel("Number of SPoC components")
    ax.set_ylabel("MSE")
    ax.set_title("SPoC Components Analysis")
    # plt.legend()
    plt.xticks(n_components, n_components)
    viz.tight_layout()
    plt.savefig(os.path.join(figure_path, "ECoG_SPoC_Components_Analysis.pdf"))
    plt.show()

    # %%
    name = "ECoG_SPoC.p"
    save_skl_model(clf, model_path, name)

    # log the model
    with mlflow.start_run(experiment_id=args.experiment) as run:
        for key, value in vars(parameters).items():
            mlflow.log_param(key, value)

        mlflow.log_metric("MSE", mse)
        mlflow.log_metric("RMSE", rmse)
        mlflow.log_metric("MAE", mae)

        mlflow.log_param("n_components",
                         clf.best_params_["Spoc__n_components"])

        mlflow.log_artifact(os.path.join(figure_path, "ECoG_SPoC_focus.pdf"))
        mlflow.log_artifact(os.path.join(figure_path, "ECoG_SPoC.pdf"))
        mlflow.log_artifact(
            os.path.join(figure_path, "ECoG_SPoC_Components_Analysis.pdf"))
        mlflow.sklearn.log_model(clf, "models")
=================================

This example shows how to display topographies of SSP projection vectors.
The projections used are the ones correcting for ECG artifacts.
"""
# Author: Alexandre Gramfort <*****@*****.**>
#         Denis A. Engemann <*****@*****.**>

# License: BSD (3-clause)

print(__doc__)

import matplotlib.pyplot as plt
from mne import read_proj, find_layout
from mne.io import read_evokeds
from mne.datasets import sample
from mne import viz
data_path = sample.data_path()

ecg_fname = data_path + '/MEG/sample/sample_audvis_ecg_proj.fif'
ave_fname = data_path + '/MEG/sample/sample_audvis-ave.fif'

evoked = read_evokeds(ave_fname, condition='Left Auditory')
projs = read_proj(ecg_fname)

layouts = [find_layout(evoked.info, k) for k in 'meg', 'eeg']

plt.figure(figsize=(12, 6))
viz.plot_projs_topomap(projs, layout=layouts)
viz.tight_layout(w_pad=0.5)
Exemple #11
0
    rmse = mean_squared_error(y, y_pred, squared=False)
    mae = mean_absolute_error(y, y_pred)
    print("mean squared error {}".format(mse))
    print("root mean squared error {}".format(rmse))
    print("mean absolute error {}".format(mae))

    fig, ax = plt.subplots(1, 1, figsize=[10, 4])
    times = np.arange(100)
    ax.plot(times, np.array(y_pred[100:200]), color="b", label="Predicted")
    ax.plot(times, np.array(y[100:200]), color="r", label="True")
    ax.set_xlabel("Times")
    ax.set_ylabel("Finger Movement")
    ax.set_title("Sub {}, finger {} prediction".format(
        str(parameters.subject_n), parameters.finger))
    plt.legend()
    viz.tight_layout()
    plt.savefig(os.path.join(figure_path, "ECoG_DL_focus.pdf"))
    plt.show()

    # plot y_new against the true value
    fig, ax = plt.subplots(1, 1, figsize=[10, 4])
    times = np.arange(len(y_pred))
    ax.plot(times, np.array(y_pred), color="b", label="Predicted")
    ax.plot(times, np.array(y), color="r", label="True")
    ax.set_xlabel("Times")
    ax.set_ylabel("Finger Movement")
    ax.set_title("Sub {}, finger {} prediction".format(
        str(parameters.subject_n), parameters.finger))
    plt.legend()
    plt.savefig(os.path.join(figure_path, "ECoG_DL.pdf"))
    plt.show()
def main(args):

    data_dir = args.data_dir
    figure_path = args.figure_dir
    model_path = args.model_dir

    # Generate the parameters class.
    parameters = SPoC_params(subject_n=8,
                             hand=0,
                             duration=1.,
                             overlap=0.8,
                             y_measure=args.y_measure)

    # Generate list of input files
    subj_id = "/sub"+str(args.sub)+"/ball0"
    raw_fnames = ["".join([data_dir, subj_id, str(i), "_sss_trans.fif"]) for i in range(1 if args.sub != 3 else 2, 4)]

    # LOCAL
    # subj_n = 8
    # subj_id = "sub" + str(subj_n) + "\\ball"
    # raw_fnames = ["".join([data_dir, subj_id, str(i), "_sss.fif"]) for i in range(1, 2)]

    # Import and epoch the MEG data
    X, y_left, y_right = import_MEG_no_bp(raw_fnames,
                                          duration=parameters.duration,
                                          overlap=parameters.overlap,
                                          y_measure=parameters.y_measure,
                                          normalize_input=True)   # concentrate the analysis only on the left hand

    print('X shape {}, y shape {}'.format(X.shape, y_left.shape))

    # Select hand
    if parameters.hand == 0:
        X_train, X_test, y_train, y_test = split_data(X, y_left, 0.3)
    else:
        X_train, X_test, y_train, y_test = split_data(X, y_right, 0.3)

    X_train, X_test = np.reshape(X_train, (X_train.shape[0], -1)), np.reshape(X_test, (X_test.shape[0], -1))

    print("Processing hand {}".format("sx" if parameters.hand == 0 else "dx"))
    print('X_train shape {}, y_train shape {} \n X_test shape {}, y_test shape {}'.format(X_train.shape, y_train.shape,
                                                                                          X_test.shape, y_test.shape))

    reg = LinearRegression()

    #%%
    # Tune the pipeline
    start = time.time()
    print('Start Fitting model ...')
    reg.fit(X_train, y_train)

    print(f'Training time : {time.time() - start}s ')

    #%%
    # Validate the pipeline
    y_new = reg.predict(X_test)
    mse = mean_squared_error(y_test, y_new)
    rmse = mean_squared_error(y_test, y_new, squared=False)
    mae = mean_absolute_error(y_test, y_new)
    r2 = r2_score(y_test, y_new)
    print("mean squared error {}".format(mse))
    print("root mean squared error {}".format(rmse))
    print("mean absolute error {}".format(mae))
    print("r2 score {}".format(r2))
    #%%
    # Plot the y expected vs y predicted.
    fig, ax = plt.subplots(1, 1, figsize=[10, 4])
    times = np.arange(100)
    ax.plot(times, y_new[100:200], color='b', label='Predicted')
    ax.plot(times, y_test[100:200], color='r', label='True')
    ax.set_xlabel("Times")
    ax.set_ylabel("{}".format(parameters.y_measure))
    ax.set_title("SPoC: Sub {}, hand {}, {} prediction".format(str(parameters.subject_n),
                                                         "sx" if parameters.hand == 0 else "dx",
                                                         parameters.y_measure))
    plt.legend()
    viz.tight_layout()
    plt.savefig(os.path.join(figure_path, 'MEG_SPoC_focus.pdf'))
    plt.show()

    # plot y_new against the true value
    fig, ax = plt.subplots(1, 1, figsize=[10, 4])
    times = np.arange(len(y_new))
    ax.plot(times, y_new, color="b", label="Predicted")
    ax.plot(times, y_test, color="r", label="True")
    ax.set_xlabel("Times")
    ax.set_ylabel("{}".format(parameters.y_measure))
    ax.set_title("Sub {}, hand {}, {} prediction".format(str(parameters.subject_n),
                                                         "sx" if parameters.hand == 0 else "dx",
                                                         parameters.y_measure))
    plt.legend()
    plt.savefig(os.path.join(figure_path, 'MEG_SPoC.pdf'))
    plt.show()

    # scatterplot y predicted against the true value
    fig, ax = plt.subplots(1, 1, figsize=[10, 4])
    ax.scatter(np.array(y_test), np.array(y_new), color="b", label="Predicted")
    ax.set_xlabel("True")
    ax.set_ylabel("Predicted")
    # plt.legend()
    plt.savefig(os.path.join(figure_path, "Scatter.pdf"))
    plt.show()


    # %%
    # Save the model.
    name = 'MEG_SPoC.p'
    save_skl_model(reg, model_path, name)

    # log the model
    with mlflow.start_run(experiment_id=args.experiment) as run:
        for key, value in vars(parameters).items():
            mlflow.log_param(key, value)

        mlflow.log_metric('MSE', mse)
        mlflow.log_metric('RMSE', rmse)
        mlflow.log_metric('MAE', mae)
        mlflow.log_metric('R2', r2)

        mlflow.log_artifact(os.path.join(figure_path, 'MEG_SPoC_focus.pdf'))
        mlflow.log_artifact(os.path.join(figure_path, 'MEG_SPoC.pdf'))
        mlflow.log_artifact(os.path.join(figure_path, 'MEG_SPoC_Components_Analysis.pdf'))
        mlflow.log_artifact(os.path.join(figure_path, "Scatter.pdf"))
        mlflow.sklearn.log_model(reg, "models")