Ejemplo n.º 1
0
def fold_stat(fold):
    # Calcul du Laplacien du graphe
    A = load_graph()
    params = {}

    params["data_source"] = "interTVA"  # ABIDE or interTVA
    params["modality"] = "tfMRI"  # gyrification or tfMRI
    # According to IJCNN paper, 15 is the best number of dimensions for tfMRI
    params["dim_1"] = 15
    # According to IJCNN paper, 5 is the best number of dimensions for rsfMRI
    params["dim_2"] = 5

    (
        train_index,
        test_index,  # <- train and test index are loaded to match mdae training
        params["ref_subject"],
        params["orig_path"],
        params["base_path"],
        sub_list,
    ) = build_path_and_vars(
        params["data_source"],
        params["modality"],
        str(params["dim_1"]) + "-" + str(params["dim_2"]),
        fold,
    )
    sub_index = np.arange(0, len(sub_list))

    # Load data
    X_val, y_val = build_raw_xy_data(params, fold, sub_list)
    X_test = X_val[sub_index[test_index], :, :]
    model = load_model(
        ("/scratch/mmahaut/data/intertva/ae/gcnn/fold_{}/model.h5".format(fold)
         ),
        custom_objects={"GraphConv": GraphConv},
    )
    y_prediction = model.predict([X_test, A])
    np.save(
        "/scratch/mmahaut/data/intertva/ae/gcnn/fold_{}/predictions.npy".
        format(fold),
        y_prediction,
    )
Ejemplo n.º 2
0
    params["mu_min"] = 1e-7
    params["graph"] = laplacian
    params["iterations"] = 1000
    params["mu"] = 0.1  # <- The initial descent step
    params["dim"] = "15-5"

    (
        train_index,
        test_index,  # <- train and test index are loaded to match mdae training
        params["ref_subject"],
        params["orig_path"],
        params["base_path"],
        sub_list,
    ) = build_path_and_vars(
        params["data_source"],
        params["modality"],
        str(params["dim_1"]) + "-" + str(params["dim_2"]),
        params["fold"],
    )
    results = np.zeros(len(sub_list))
    sub_index = np.arange(0, len(sub_list))
    print("Fold #{}".format(params["fold"]))
    # Chargement des données
    X, Y = build_xy_data(params, params["dim"], params["fold"], sub_list)
    print("TRAIN:", sub_index[train_index], "TEST:", sub_index[test_index])
    # Ensemble d'entrainement
    XE = X[sub_index[train_index], :, :]
    YE = Y[sub_index[train_index]]
    # Ensemble de test
    XT = X[sub_index[test_index], :, :]
    YT = Y[sub_index[test_index]]
    beta = estimate_beta(XE, YE, params)
Ejemplo n.º 3
0
                            str(dim),
                            "fold_{}".format(fold),
                            "delta_{}".format(float(delta)),
                            "soft_thres_{}".format(float(soft_thres)),
                        )
                        if os.path.exists(full_path):
                            (
                                train_index,
                                test_index,  # <- train and test index are loaded to match mdae training
                                ref_subject,
                                orig_path,
                                base_path,
                                sub_list,
                            ) = build_path_and_vars(
                                "interTVA",
                                "tfMRI",
                                str(15) + "-" + str(5),
                                fold,
                            )
                            sub_index = np.arange(0, len(sub_list))
                            results[sub_index[test_index]] = np.load(
                                os.path.join(full_path, "results.npy"))
                            # mse.append(np.load(os.path.join(full_path, "mse.npy")))
                            # r_squared.append(
                            #     np.load(os.path.join(full_path, "r_squared.npy"))
                            # )
                        else:
                            print("missing : ", full_path)
                    # print("MSE : ", mse)
                    # print("MSE mean", np.mean(mse))

                    cell_text_row.append("%.3f" %
Ejemplo n.º 4
0
if __name__ == "__main__":

    data_orig = sys.argv[1]  # {"ABIDE", "interTVA"}
    data_type = sys.argv[2]  # could be "tfMRI" or "gyrification"
    save_folder = sys.argv[3]
    fold = int(sys.argv[4])  # int belonging to [1,10]
    dim = "15-5"
    (
        train_index,
        test_index,
        ref_subject,
        orig_path,
        base_path,
        sub_list,
    ) = build_path_and_vars(data_orig, data_type, dim, fold)

    # activation functions, relu / linear gives best results according to IJCNN paper, my test on dim 20 doesn't seem to change much
    hidden_layer = "relu"
    output_layer = "linear"
    index_subjects = np.arange(0, len(sub_list))
    print(f"Fold #{fold}")
    print(
        "TRAIN:",
        index_subjects[train_index],
        "TEST:",
        index_subjects[test_index],
    )
    # load training and testing data
    print("Load training data...")
Ejemplo n.º 5
0
def get_model_stats(data_orig, data_type, dim, number_folds="10"):
    """
    """
    # Tables:
    mse_train = []
    mse_test = []
    # RMSE (gyr+ rsfmri)
    rmse_train = []
    rmse_test = []
    #
    # Standard deviation MSE (gyr+ rsfmri)
    std_mse_train = []
    std_mse_test = []
    # Standard deviation RMSE (gyr+ rsfmri)
    std_rmse_train = []
    std_rmse_test = []
    # MSE (gyr)
    mse_gyr_train = []
    mse_gyr_test = []
    # RMSE (gyr)
    rmse_gyr_train = []
    rmse_gyr_test = []
    # std mse (gyr)
    std_mse_gyr_train = []
    std_mse_gyr_test = []
    # std rmse (gyr)
    std_rmse_gyr_train = []
    std_rmse_gyr_test = []

    # MSE (rsfmri)
    mse_rsfmri_train = []
    mse_rsfmri_test = []
    # RMSE (rsfmri)
    rmse_rsfmri_train = []
    rmse_rsfmri_test = []
    # std mse (rsfmri)
    std_mse_rsfmri_train = []
    std_mse_rsfmri_test = []
    # std rmse (rsfmri)
    std_rmse_rsfmri_train = []
    std_rmse_rsfmri_test = []

    cvscores_mse_test = []
    cvscores_rmse_test = []
    cvscores_mse_train = []
    cvscores_rmse_train = []
    cvscores_mse_gyr_train = []
    cvscores_mse_gyr_test = []
    cvscores_rmse_gyr_train = []
    cvscores_rmse_gyr_test = []
    cvscores_mse_rsfmri_train = []
    cvscores_mse_rsfmri_test = []
    cvscores_rmse_rsfmri_train = []
    cvscores_rmse_rsfmri_test = []

    # Training
    for fold in range(1, number_folds + 1):

        # Vertex design changes everything, lets add some code to make it work
        (
            train_index,
            test_index,
            ref_subject,
            orig_path,
            base_path,
            sub_list,
        ) = build_path_and_vars(data_orig, data_type, dim, fold)

        # index_vertices = np.arange(0, 20484)
        # index_subject_vertices = np.array(
        #     np.meshgrid(index_subjects, index_vertices)
        # ).T.reshape(-1, 2)
        (
            normalized_train_gyr_data,
            normalized_test_gyr_data,
            normalized_train_rsfmri_data,
            normalized_test_rsfmri_data,
        ) = build_normalised_data(
            data_orig,
            data_type,
            ref_subject,
            orig_path,
            sub_list,
            train_index,
            test_index,
        )

        multimodal_autoencoder = tf.keras.models.load_model(
            "{}/{}/fold_{}/multimodal_autoencoder.h5".format(
                base_path, dim, fold))

        print("Reconstruction of training data... ")
        [X_train_new_gyr, X_train_new_rsfmri] = multimodal_autoencoder.predict(
            [normalized_train_gyr_data, normalized_train_rsfmri_data])

        # gyr
        print("Max value of predicted training gyr data ",
              np.max(X_train_new_gyr))
        print("Min value of predicted training gyr data",
              np.min(X_train_new_gyr))
        print("Reconstructed gyr matrix shape:", X_train_new_gyr.shape)
        val_mse_train_gyr = mean_squared_error(normalized_train_gyr_data,
                                               X_train_new_gyr)
        cvscores_mse_gyr_train.append(val_mse_train_gyr)
        print("Reconstruction MSE of gyr:", val_mse_train_gyr)
        val_rmse_gyr = sqrt(val_mse_train_gyr)
        print("Reconstruction RMSE of gyr : ", val_rmse_gyr)
        cvscores_rmse_gyr_train.append(val_rmse_gyr)

        # rsfmri

        print(
            "Max value of predicted training rsfmri data ",
            np.max(X_train_new_rsfmri),
        )
        print(
            "Min value of predicted training rsfmri data",
            np.min(X_train_new_rsfmri),
        )
        print("Reconstructed rsfmri matrix shape:", X_train_new_rsfmri.shape)
        val_mse_train_rsfmri = mean_squared_error(normalized_train_rsfmri_data,
                                                  X_train_new_rsfmri)
        cvscores_mse_rsfmri_train.append(val_mse_train_rsfmri)
        print("Reconstruction MSE of rsfmri:", val_mse_train_rsfmri)
        val_rmse_rsfmri = sqrt(val_mse_train_rsfmri)
        print("Reconstruction RMSE of rsfmri : ", val_rmse_rsfmri)
        cvscores_rmse_rsfmri_train.append(val_rmse_rsfmri)

        # sum of MSE (gyr + rsfmri)
        cvscores_mse_train.append(
            np.sum([val_mse_train_gyr, val_mse_train_rsfmri]))
        # sum of RMSE (gyr + rsfmri)
        cvscores_rmse_train.append(
            sqrt(np.sum([val_mse_train_gyr, val_mse_train_rsfmri])))
        # mean of MSE (gyr + rsfmri)/2

        # Reconstruction of test data
        print("Reconstruction of test data... ")
        [X_test_new_gyr, X_test_new_rsfmri] = multimodal_autoencoder.predict(
            [normalized_test_gyr_data, normalized_test_rsfmri_data])

        print(
            "prediction v.s. expected :\n",
            [X_test_new_gyr, X_test_new_rsfmri],
            "\n----------\n",
            [normalized_test_gyr_data, normalized_test_rsfmri_data],
        )
        # Test
        # gyr
        print("Max value of predicted testing gyr data ",
              np.max(X_test_new_gyr))
        print("Min value of predicted testing gyr data",
              np.min(X_test_new_gyr))
        print("Reconstructed gyr matrix shape:", X_test_new_gyr.shape)
        val_mse_test_gyr = mean_squared_error(normalized_test_gyr_data,
                                              X_test_new_gyr)
        cvscores_mse_gyr_test.append(val_mse_test_gyr)
        print("Reconstruction MSE of gyr:", val_mse_test_gyr)
        val_rmse_gyr = sqrt(val_mse_test_gyr)
        print("Reconstruction RMSE of gyr : ", val_rmse_gyr)
        cvscores_rmse_gyr_test.append(val_rmse_gyr)

        # rsfmri

        print(
            "Max value of predicted testing rsfmri data ",
            np.max(X_test_new_rsfmri),
        )
        print(
            "Min value of predicted testing rsfmri data",
            np.min(X_test_new_rsfmri),
        )
        print("Reconstructed rsfmri matrix shape:", X_test_new_rsfmri.shape)
        val_mse_test_rsfmri = mean_squared_error(normalized_test_rsfmri_data,
                                                 X_test_new_rsfmri)
        cvscores_mse_rsfmri_test.append(val_mse_test_rsfmri)
        print("Reconstruction MSE of rsfmri:", val_mse_test_rsfmri)
        val_rmse_rsfmri = sqrt(val_mse_test_rsfmri)
        print("Reconstruction RMSE of rsfmri : ", val_rmse_rsfmri)
        cvscores_rmse_rsfmri_test.append(val_rmse_rsfmri)

        # sum of MSE (gyr + rsfmri)
        cvscores_mse_test.append(
            np.sum([val_mse_test_gyr, val_mse_test_rsfmri]))
        # sum of MSE (gyr + rsfmri)
        cvscores_rmse_test.append(
            sqrt(np.sum([val_mse_test_gyr, val_mse_test_rsfmri])))

    # Attempt to prevent memory leak on skylake machine, legacy from when this was a loop
    # K.clear_session()

    # Save MSE, RMSE (gyr + rsfmr)
    print("shape of vector mse train", np.array([cvscores_mse_train]).shape)
    print(cvscores_mse_train)
    np.save(
        "{}/{}/cvscores_mse_train.npy".format(base_path, dim),
        np.array([cvscores_mse_train]),
    )
    print("shape of  mse vector(test):", np.array([cvscores_mse_test]).shape)
    print(cvscores_mse_test)
    np.save(
        "{}/{}/cvscores_mse_test.npy".format(base_path, dim),
        np.array([cvscores_mse_test]),
    )
    print("shape of rmse vector (train):",
          np.array([cvscores_rmse_train]).shape)
    print(cvscores_rmse_train)
    np.save(
        "{}/{}/cvscores_rmse_train.npy".format(base_path, dim),
        np.array([cvscores_rmse_train]),
    )
    print("shape of rmse vector (test):", np.array([cvscores_rmse_test]).shape)
    print(cvscores_rmse_test)
    np.save(
        "{}/{}/cvscores_rmse_test.npy".format(base_path, dim),
        np.array([cvscores_rmse_test]),
    )
    print("%.3f%% (+/- %.5f%%)" %
          (np.mean(cvscores_mse_test), np.std(cvscores_mse_test)))
    mse_train.append(np.mean(cvscores_mse_train))
    std_mse_train.append(np.std(cvscores_mse_train))
    mse_test.append(np.mean(cvscores_mse_test))
    std_mse_test.append(np.std(cvscores_mse_test))
    rmse_train.append(np.mean(cvscores_rmse_train))
    std_rmse_train.append(np.std(cvscores_rmse_train))
    rmse_test.append(np.mean(cvscores_rmse_test))
    std_rmse_test.append(np.std(cvscores_rmse_test))

    # Save MSE, RMSE (gyr)
    print("shape of vector mse train (gyr)",
          np.array([cvscores_mse_gyr_train]).shape)
    print(cvscores_mse_gyr_train)
    np.save(
        "{}/{}/cvscores_mse_gyr_train.npy".format(base_path, dim),
        np.array([cvscores_mse_gyr_train]),
    )
    print("shape of  mse vector(test):",
          np.array([cvscores_mse_gyr_test]).shape)
    print(cvscores_mse_gyr_test)
    np.save(
        "{}/{}/cvscores_mse_gyr_test.npy".format(base_path, dim),
        np.array([cvscores_mse_gyr_test]),
    )
    print("shape of rmse vector (train):",
          np.array([cvscores_rmse_gyr_train]).shape)
    print(cvscores_rmse_gyr_train)
    np.save(
        "{}/{}/cvscores_rmse_gyr_train.npy".format(base_path, dim),
        np.array([cvscores_rmse_gyr_test]),
    )
    print("shape of rmse vector gyr (test):",
          np.array([cvscores_rmse_gyr_test]).shape)
    print(cvscores_rmse_gyr_test)
    np.save(
        "{}/{}/cvscores_rmse_gyr_test.npy".format(base_path, dim),
        np.array([cvscores_rmse_gyr_test]),
    )
    mse_gyr_train.append(np.mean(cvscores_mse_gyr_train))
    std_mse_gyr_train.append(np.std(cvscores_mse_gyr_train))
    mse_gyr_test.append(np.mean(cvscores_mse_gyr_test))
    std_mse_gyr_test.append(np.std(cvscores_mse_gyr_test))
    rmse_gyr_train.append(np.mean(cvscores_rmse_gyr_train))
    std_rmse_gyr_train.append(np.std(cvscores_rmse_gyr_train))
    rmse_gyr_test.append(np.mean(cvscores_rmse_gyr_test))
    std_rmse_gyr_test.append(np.std(cvscores_rmse_gyr_test))

    # Save MSE, RMSE (rsfmri)
    print(
        "shape of vector mse train (rsfmri)",
        np.array([cvscores_mse_rsfmri_train]).shape,
    )
    print(cvscores_mse_rsfmri_train)
    np.save(
        "{}/{}/cvscores_mse_rsfmri_train.npy".format(base_path, dim),
        np.array([cvscores_mse_rsfmri_train]),
    )
    print("shape of  mse vector(test):",
          np.array([cvscores_mse_rsfmri_test]).shape)
    print(cvscores_mse_rsfmri_test)
    np.save(
        "{}/{}/cvscores_mse_rsfmri_test.npy".format(base_path, dim),
        np.array([cvscores_mse_rsfmri_test]),
    )
    print(
        "shape of rmse vector (train):",
        np.array([cvscores_rmse_rsfmri_train]).shape,
    )
    print(cvscores_rmse_rsfmri_train)
    np.save(
        "{}/{}/cvscores_rmse_rsfmri_train.npy".format(base_path, dim),
        np.array([cvscores_rmse_rsfmri_test]),
    )
    print(
        "shape of rmse vector rsfmri (test):",
        np.array([cvscores_rmse_rsfmri_test]).shape,
    )
    print(cvscores_rmse_rsfmri_test)
    np.save(
        "{}/{}/cvscores_rmse_rsfmri_test.npy".format(base_path, dim),
        np.array([cvscores_rmse_rsfmri_test]),
    )
    mse_rsfmri_train.append(np.mean(cvscores_mse_rsfmri_train))
    std_mse_rsfmri_train.append(np.std(cvscores_mse_rsfmri_train))
    mse_rsfmri_test.append(np.mean(cvscores_mse_rsfmri_test))
    std_mse_rsfmri_test.append(np.std(cvscores_mse_rsfmri_test))
    rmse_rsfmri_train.append(np.mean(cvscores_rmse_rsfmri_train))
    std_rmse_rsfmri_train.append(np.std(cvscores_rmse_rsfmri_train))
    rmse_rsfmri_test.append(np.mean(cvscores_rmse_rsfmri_test))
    std_rmse_rsfmri_test.append(np.std(cvscores_rmse_rsfmri_test))

    # save MSE, RMSE, and STD vectors for training and test sets
    np.save("{}/mse_train_mean.npy".format(base_path), np.array([mse_train]))
    np.save("{}/rmse_train_mean.npy".format(base_path), np.array([rmse_train]))
    np.save("{}/std_mse_train_mean.npy".format(base_path),
            np.array([std_mse_train]))
    np.save("{}/std_rmse_train_mean.npy".format(base_path),
            np.array([std_rmse_train]))
    np.save("{}/mse_test_mean.npy".format(base_path), np.array([mse_test]))
    np.save("{}/rmse_test_mean.npy".format(base_path), np.array([rmse_test]))
    np.save("{}/std_mse_test_mean.npy".format(base_path),
            np.array([std_mse_test]))
    np.save("{}/std_rmse_test_mean.npy".format(base_path),
            np.array([std_rmse_test]))

    # save MSE, RMSE, and STD vectors for training and test sets (rsfmri)

    np.save(
        "{}/mse_test_mean_rsfmri.npy".format(base_path),
        np.array([mse_rsfmri_test]),
    )
    np.save(
        "{}/rmse_test_mean_rsfmri.npy".format(base_path),
        np.array([rmse_rsfmri_test]),
    )
    np.save(
        "{}/mse_train_mean_rsfmri.npy".format(base_path),
        np.array([mse_rsfmri_train]),
    )
    np.save(
        "{}/rmse_train_mean_rsfmri.npy".format(base_path),
        np.array([rmse_rsfmri_train]),
    )
    np.save(
        "{}/std_mse_mean_rsfmri.npy".format(base_path),
        np.array([std_mse_rsfmri_test]),
    )
    np.save(
        "{}/std_rmse_mean_rsfmri.npy".format(base_path),
        np.array([std_rmse_rsfmri_test]),
    )