コード例 #1
0
def plot_loadings(components):
    figs = []
    for j in range(N_COMP):
        legend = 'Loading #{j} for model {snr}'.format(j=j + 1, snr=snr)
        plot_map2d(components[j, ].reshape(dice5_data.SHAPE), title=legend)
        figs.append(plt.gcf())
    return figs
コード例 #2
0
def plot_map2d_of_PCA_models(models_dict, nrow, ncol, shape,N_COMP,folder,title_attr=None):
    """Plot 2 weight maps of models"""
    #from .plot import plot_map2d
    ax_i = 1
    for k in models_dict.keys():
        mod = models_dict[k]
        if  hasattr(mod, "V"):
            w = mod.V
        elif hasattr(mod, "components_"): # to work with sklean
            w = mod.components_.T
        if (title_attr is not None and hasattr(mod, title_attr)):
            title = getattr(mod, title_attr)
        else:
            title = None
        ax = plt.subplot(nrow, ncol, ax_i)
        plt.tight_layout()
        utils.plot_map2d(w[:,0].reshape(shape[:2]),ax,title=title)
        ax_i += 1
        ax = plt.subplot(nrow, ncol, ax_i)
        utils.plot_map2d(w[:,1].reshape(shape[:2]),ax,title=title)
        ax_i += 1
        ax = plt.subplot(nrow, ncol, ax_i)
        utils.plot_map2d(w[:,2].reshape(shape[:2]),ax,title=title)
        ax_i += 1
    plt.savefig(os.path.join(folder,'pca_fits.png'))
    plt.close()
###########################################################################
## LogisticRegressionL1L2TV
# Minimize:
#    f(beta, X, y) = - loglik/n_train
#                    + l2/2 * ||beta||^2_2
#                    + l1 * ||beta||_1
#                    + tv * TV(beta)
l1, l2, tv = alpha * np.array((.05, .75, .2))  # l1, l2, tv penalties

## Limit the precison to 1e-3
A = nesterov_tv.linear_operator_from_shape(beta3d.shape)
enettv = estimators.LogisticRegressionL1L2TV(l1, l2, tv, A, algorithm_params = dict(eps=1e-3))
yte_pred_enettv = enettv.fit(Xtr, ytr).predict(Xte)
_, recall_enettv, _, _ = precision_recall_fscore_support(yte, yte_pred_enettv, average=None)

###########################################################################
## Plot
plot = plt.subplot(221)
limits = None #np.array((beta3d.min(), beta3d.max())) / 2.
#limits = None
utils.plot_map2d(beta3d.reshape(shape), plot, title="beta star")
plot = plt.subplot(222)
utils.plot_map2d(enettv.beta.reshape(shape), plot, limits=limits,
           title="L1+L2+TV (%.2f, %.2f)" % tuple(recall_enettv))
plot = plt.subplot(223)
utils.plot_map2d(ridge_sklrn.coef_.reshape(shape), plot, limits=limits,
           title="Ridge (sklrn) (%.2f, %.2f)" % tuple(recall_ridge_sklrn))
plot = plt.subplot(224)
utils.plot_map2d(ridge_prsmy.beta.reshape(shape), plot,limits=limits,
           title="Ridge (Parsimony) (%.2f, %.2f)" % tuple(recall_ridge_prsmy))
plt.show()
コード例 #4
0
ファイル: 02_test.py プロジェクト: neurospin/scripts
                                           use_eg=False,
                                           output=True)
t = timeit.timeit(stmt='e_con_sparse.fit(X)', setup="from __main__ import e_con_sparse, X", number=1)
print "CONESTA:", t
V_sparse_con = e_con_sparse.V
del e_con_sparse

sparsepca_sklearn = sklearn.decomposition.SparsePCA(n_components=N_COMP,
                                                    alpha=0.4)
t = timeit.timeit(stmt='sparsepca_sklearn.fit(X)', setup="from __main__ import sparsepca_sklearn, X", number=1)
print "Sparse PCA:", t
V_sparsepca_sklearn = sparsepca_sklearn.components_.transpose()
del sparsepca_sklearn

plot = plt.subplot(5, N_COMP, 1)
plot_map2d(beta3d.reshape(INPUT_SHAPE), plot, title="beta star")
for k in range(N_COMP):
    # Plot PCA
    plot = plt.subplot(5, N_COMP, N_COMP+1+k)
    title = "PCA comp #{i}".format(i=k)
    plot_map2d(V_pca_sklearn[:,k].reshape(INPUT_SHAPE), plot, title=title)

    # Plot sparsepca_sklearn
    plot = plt.subplot(5, N_COMP, 2*N_COMP+1+k)
    title = "SparsePCA comp #{i}".format(i=k)
    plot_map2d(V_sparsepca_sklearn[:,k].reshape(INPUT_SHAPE), plot, title=title)

    # Plot the components EG
    plot = plt.subplot(5, N_COMP, 3*N_COMP+1+k)
    title = "PCA_L1L2TV EG comp #{i}".format(i=k)
    plot_map2d(V_sparse_eg[:,k].reshape(INPUT_SHAPE), plot, title=title)
コード例 #5
0
        snr = float(input_str)
    except:
        print "Can't parse input"
        continue
    if snr not in dice5_data.ALL_SNRS:
        print "Invalid SNR"
        continue
    # Reload weight maps
    output_dir = OUTPUT_DIR_FORMAT.format(snr=snr)
    full_filename = os.path.join(output_dir, OUTPUT_PCA_FILE)
    with open(full_filename) as f:
        pca = pickle.load(f)

    for j in range(N_COMP):
        legend = 'Loading #{j} for model {snr}'.format(j=j + 1, snr=snr)
        plot_map2d(pca.components_[j, ].reshape(dice5_data.SHAPE),
                   title=legend)
        f = plt.gcf()
        filename = os.path.join(output_dir,
                                OUTPUT_LOADING_FILE_FMT.format(i=j + 1))
        f.savefig(filename)

    print "Figures are saved. Close them to continue."
    plt.show()

# Store chosen SNR
CHOSEN_SNR = []
while True:
    input_str = raw_input("Enter a SNR value to store (or q to quit):")
    if input_str == "q":
        break
    try:
コード例 #6
0
###########################################################################
## Fit LinearRegressionL1L2TV
# Min: (1 / (2 * n)) * ||Xbeta - y||^2_2
#    + l1 * ||beta||_1
#    + (l2 / 2) * ||beta||^2_2
#    + tv * TV(beta)
#
l1, l2, tv = alpha * np.array((.33, .33, .33))  # l1, l2, tv penalties
A = nesterov_tv.linear_operator_from_shape(shape)
algo = algorithms.proximal.CONESTA(max_iter=500)
enettv = estimators.LinearRegressionL1L2TV(l1, l2, tv, A, algorithm=algo)
yte_pred_enettv = enettv.fit(Xtr, ytr).predict(Xte)

###########################################################################
## Plot

# TODO: Please remove dependence on scikit-learn. Add required functionality
# to parsimony instead.
plot = plt.subplot(131)
utils.plot_map2d(beta3d.reshape(shape), plot, title="beta star")
plot = plt.subplot(132)
utils.plot_map2d(enet.beta.reshape(shape), plot, title="beta enet (R2=%.2f)" %
    r2_score(yte, yte_pred_enet))
#utils.plot_map2d(enet.coef_.reshape(shape), plot, title="beta enet (R2=%.2f)" %
#    r2_score(yte, yte_pred_enet), limits=limits/1.)
plot = plt.subplot(133)
utils.plot_map2d(enettv.beta.reshape(shape), plot,
                 title="beta enettv (R2=%.2f)"  % r2_score(yte, yte_pred_enettv))
plt.show()
コード例 #7
0
yte_pred_enet = enet.fit(Xtr, ytr).predict(Xte)

###########################################################################
## Fit LinearRegressionL1L2TV
# 
l1, l2, tv = alpha * np.array((.4, .1, .5))  # l2, l1, tv penalties
A, n_compacts = tv_helper.A_from_shape(shape)
#import parsimony.functions as functions
#functions.RR_L1_TV(X, y, k, l, g, A=A)
enettv = LinearRegressionL1L2TV(l1, l2, tv, A, algorithm=StaticCONESTA(max_iter=500))
yte_pred_enettv = enettv.fit(Xtr, ytr).predict(Xte)

###########################################################################
## Plot

# TODO: Please remove dependence on scikit-learn. Add required functionality
# to parsimony instead.

plot = plt.subplot(131)
plot_map2d(beta3d.reshape(shape), plot, title="beta star")
plot = plt.subplot(132)
plot_map2d(enet.coef_.reshape(shape), plot, title="beta enet (R2=%.2f)" %
    r2_score(yte, yte_pred_enet),
             limits=(beta3d.min(), beta3d.max()))
plot = plt.subplot(133)
plot_map2d(enettv.beta.reshape(shape), plot,
           title="beta enettv (R2=%.2f)" \
                 % r2_score(yte, yte_pred_enettv),
             limits=(beta3d.min(), beta3d.max()))
plt.show()
コード例 #8
0
enettv = estimators.LogisticRegressionL1L2TV(l1,
                                             l2,
                                             tv,
                                             A,
                                             algorithm_params=dict(eps=1e-3))
yte_pred_enettv = enettv.fit(Xtr, ytr).predict(Xte)
_, recall_enettv, _, _ = precision_recall_fscore_support(yte,
                                                         yte_pred_enettv,
                                                         average=None)

###########################################################################
## Plot
plot = plt.subplot(221)
limits = None  #np.array((beta3d.min(), beta3d.max())) / 2.
#limits = None
utils.plot_map2d(beta3d.reshape(shape), plot, title="beta star")
plot = plt.subplot(222)
utils.plot_map2d(enettv.beta.reshape(shape),
                 plot,
                 limits=limits,
                 title="L1+L2+TV (%.2f, %.2f)" % tuple(recall_enettv))
plot = plt.subplot(223)
utils.plot_map2d(ridge_sklrn.coef_.reshape(shape),
                 plot,
                 limits=limits,
                 title="Ridge (sklrn) (%.2f, %.2f)" %
                 tuple(recall_ridge_sklrn))
plot = plt.subplot(224)
utils.plot_map2d(ridge_prsmy.beta.reshape(shape),
                 plot,
                 limits=limits,
ridge2 = LogisticRegressionL1L2TV(0, alpha, 0, A, algorithm=StaticCONESTA(max_iter=500))
yte_pred_ridge2 = ridge2.fit(Xtr, ytr).predict(Xte)
_, recall_ridge2, _, _ = precision_recall_fscore_support(yte, yte_pred_ridge2, average=None)

###########################################################################
## LogisticRegressionL1L2TV
# Minimize:
#    f(beta, X, y) = - loglik/n_train
#                    + k/2 * ||beta||^2_2 
#                    + l * ||beta||_1
#                    + g * TV(beta)
l1, l2, tv = alpha * np.array((.4, .1, .5)) / 10 # l2, l1, tv penalties
A, n_compacts = tv_helper.A_from_shape(beta3d.shape)
enettv = LogisticRegressionL1L2TV(l1, l2, tv, A, algorithm=StaticCONESTA(max_iter=500))
yte_pred_enettv = enettv.fit(Xtr, ytr).predict(Xte)
_, recall_enettv, _, _ = precision_recall_fscore_support(yte, yte_pred_enettv, average=None)

###########################################################################
## Plot
plot = plt.subplot(221)
plot_map2d(beta3d.reshape(shape), plot, title="beta star")
plot = plt.subplot(222)
plot_map2d(enettv.beta.reshape(shape), plot, limits=(beta3d.min(), beta3d.max()),
           title="L1+L2+TV (%.2f, %.2f)" % tuple(recall_enettv))
plot = plt.subplot(223)
plot_map2d(ridge.coef_.reshape(shape), plot, limits=(beta3d.min(), beta3d.max()), 
           title="Ridge (%.2f, %.2f)" % tuple(recall_ridge))
plot = plt.subplot(224)
plot_map2d(ridge2.beta.reshape(shape), plot, limits=(beta3d.min(), beta3d.max()), 
           title="Ridge (parsimony) (%.2f, %.2f)" % tuple(recall_ridge2))
plt.show()
コード例 #10
0
    #Load result of minimization
    beta_path = os.path.join(BETA_DIR, "components.npz")
    components = np.load(beta_path)
    components = components['arr_0'].reshape(100, 100, 3)

    for k in range(0, 3):
        components[:, :, k] = components[:, :, k] - components[:, :, k].mean()
        components[:, :, k] = components[:, :, k] / components[:, :, k].std()
    true = np.abs(true)
    components = np.abs(components)

    for k in range(3):
        plot = plt.subplot(3, 3, 3 + 1 + k)
        title = "comp #{i}".format(i=k)
        plot_map2d(components[:, :, k], plot, title=title)
    f = plt.gcf()
    plt.show()
    filename = os.path.join(BASE_DIR,
                            "eps_components_0.1/eps_1e-%r.svg") % (EPS)
    f.savefig(filename)
    plt.clf()

    mean_mse = 0
    for k in range(0, 2):
        data = np.zeros((10000, 2))
        data[:, 0] = true[:, :, k].reshape(10000)
        R = np.zeros((2))
        for i in range(0, 2):
            data[:, 1] = components[:, :, i].reshape(10000)
            R[i] = np.abs(np.corrcoef(np.abs(data.T))[0, 1])
コード例 #11
0
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from brainomics import plot_utilities
from parsimony.utils import plot_map2d

#FIGURES OF  PAPER

#################################################################################################################################################
#reconstruction with Standard PCA
components = np.load(
    "/neurospin/brainomics/2014_pca_struct/dice5_ad_validation/results_0.1_1e-6/data_100_100_0/results/0/pca_0.0_0.0_0.0/components.npz"
)
components = components['arr_0']
for k in range(3):
    fig = plot_map2d(components[:, k].reshape(100, 100))

#reconstruction with Sparse PCA
components = np.load(
    "/neurospin/brainomics/2014_pca_struct/dice5_ad_validation/results_0.1_1e-6/data_100_100_0/results/0/sparse_pca_0.0_0.0_1.0/components.npz"
)
components = components['arr_0']
for k in range(3):
    fig = plots.map2d(components[:, k].reshape(100, 100))

#reconstruction with ElasticNet PCA
components = np.load(
    "/neurospin/brainomics/2014_pca_struct/dice5_ad_validation/results_0.1_1e-6/data_100_100_0/results/0/struct_pca_0.01_1e-05_0.5/components.npz"
)
components = components['arr_0']
for k in range(3):