def plot_perf():
    import os
    import numpy as np
    import pandas as pd
    import matplotlib.pyplot as plt
    from matplotlib.backends.backend_pdf import PdfPages

    # SOME ERROR WERE HERE CORRECTED 27/04/2014 think its good
    #INPUT_vbm = "/home/ed203246/mega/data/2015_logistic_nestv/adni/MCIc-CTL/MCIc-CTL_cs.csv"
    INPUT = os.path.join(WD, "MCIc-CTL_csi_modselectcv.csv")
    y_col = 'recall_mean'
    x_col = 'tv'
    y_col = 'auc'
    a = 0.01
    #color_map = {0.:'#D40000', 0.01: 'black', 0.1:'#F0a513',  0.5:'#2CA02C',  0.9:'#87AADE',  1.:'#214478'}
    color_map = {0.:'#D40000', 0.01:'#F0a513',  0.1:'#2CA02C',  0.5:'#87AADE',  .9:'#214478', 1.: 'black'}
    #                reds dark => brigth,      green         blues: brigth => dark
    input_filename = INPUT
    #input_filename = INPUTS[data_type]["filename"]
    outut_filename = input_filename.replace(".csv", "_%s.pdf" % y_col)
    #print outut_filename
    # Filter data
    data = pd.read_csv(input_filename)
    #data.l1l2_ratio = data.l1l2_ratio.round(5)
    # avoid poor rounding
    data.l1l2_ratio = np.asarray(data.l1l2_ratio).round(3)
    data.tv = np.asarray(data.tv).round(5)
    data.a = np.asarray(data.a).round(5)
    data = data[data.k == -1]
    data = data[data.l1l2_ratio.isin([0, 0.01, 0.1, 0.5, 0.9, 1.])]
    data = data[(data.tv >= 0.1) | (data.tv == 0)]
    def close(vec, val, tol=1e-4):
        return np.abs(vec - val) < tol
    assert np.sum(data.l1l2_ratio == 0.01) == np.sum(close(data.l1l2_ratio, 0.01))
    #data = data[data.a <= 1]
    # for each a, l1l2_ratio, append the last point tv==1
    last = list()
    for a_ in np.unique(data.a):
        full_tv = data[(data.a == a_) & (data.tv == 1)]
        for l1l2_ratio in np.unique(data.l1l2_ratio):
            new = full_tv.copy()
            new.l1l2_ratio = l1l2_ratio
            last.append(new)
    #
    last = pd.concat(last)
    data = pd.concat([data, last])
    data.drop_duplicates(inplace=True)
    #
    from brainomics.plot_utilities import plot_lines
    figures = plot_lines(df=data,
    x_col=x_col, y_col=y_col, colorby_col='l1l2_ratio',
                       splitby_col='a', color_map=color_map)
    pdf = PdfPages(outut_filename)
    for fig in figures:
        print fig, figures[fig]
        pdf.savefig(figures[fig]); plt.clf()
    pdf.close()
Esempio n. 2
0
                  for global_pen in global_pens for tv_ratio in tv_ratios
                  for l1_ratio in l1_ratios])
df = pd.DataFrame(index, columns=['global_pen', 'tv_ratio', 'l1_ratio'])


def metric_fun(r):
    return (r['tv_ratio'] + r['l1_ratio']) * r['global_pen']


df['metric'] = df.apply(metric_fun, axis=1)

# Test
handles = plot_utilities.plot_lines(df,
                                    x_col='tv_ratio',
                                    y_col='metric',
                                    splitby_col='global_pen',
                                    colorby_col='l1_ratio',
                                    use_suptitle=False,
                                    use_subplots=True)
plt.show()

# Test with index
df.set_index(['global_pen', 'tv_ratio', 'l1_ratio'], inplace=True)
handles = plot_utilities.plot_lines(df,
                                    x_col=1,
                                    y_col='metric',
                                    splitby_col=0,
                                    colorby_col=2,
                                    use_suptitle=False,
                                    use_subplots=True)
Esempio n. 3
0
print "Found", n_indiv, "persons"

###############################################################################
# Plot metrics                                                                #
###############################################################################

L1_RATIOS = [0.0, 0.5, 1.0]
l1_ratio_filter = lambda v: v in L1_RATIOS
df_noindex = df.reset_index()
struct_pca_df = df_noindex.loc[(df_noindex.model == 'struct_pca')
                               & (df_noindex.l1_ratio.apply(l1_ratio_filter))]

for metric in METRICS:
    handles = plot_utilities.plot_lines(struct_pca_df,
                                        x_col="tv_ratio",
                                        y_col=metric,
                                        splitby_col="global_pen",
                                        colorby_col="l1_ratio")
    for val, handle in handles.items():
        filename = OUTPUT_CURVE_FILE_FORMAT.format(metric=metric,
                                                   global_pen=val)
        handle.savefig(filename)

###############################################################################
# Components                                                                  #
###############################################################################

# Load components
components = np.zeros((len(COND), np.prod(IM_SHAPE), N_COMP))
for j, (params, name) in enumerate(COND):
    key = '_'.join([str(param) for param in params])
Esempio n. 4
0
# Subsample df
struct_pca_df = df[df.model == EXAMPLE_MODEL]

# GroupBy SNR
snr_groups = struct_pca_df.groupby('snr')
# Summary per SNR value (pivot the table to have better display)
for metric, metric_name in zip(METRICS, METRICS_NAME):
    summary = pd.DataFrame(snr_groups[metric].describe()).unstack(1)[0]
    filename = TAB_FILE_FORMAT.format(metric=metric)
    summary.to_latex(open(filename, 'w'))
# Plot some metrics for struct_pca for each SNR value
for snr_val, snr_group in snr_groups:
    for metric, metric_name in zip(METRICS, METRICS_NAME):
        handles = plot_utilities.plot_lines(snr_group,
                                            x_col='tv_ratio',
                                            y_col=metric,
                                            splitby_col='global_pen',
                                            colorby_col='l1_ratio',
                                            use_suptitle=False)
        for val, handle in handles.items():
            # Tune the figure
            ax = handle.get_axes()[0]
            ax.set_xlabel("TV ratio")
            ax.set_ylabel(metric_name)
            l = ax.get_legend()
            l.set_title("$\ell_1$ ratio")
            s = r'$ \alpha $ =' + str(val)
            handle.suptitle(r'$ \alpha $ =' + str(val))
            filename = CURVE_FILE_FORMAT.format(metric=metric,
                                                snr=snr_val,
                                                global_pen=val)
            handle.savefig(filename)
Esempio n. 5
0
# Subsample it & add a column based on name
summary = df.loc[PARAMS][METRICS]
name_serie = pd.Series([item[1] for item in COND], name='Name', index=PARAMS)
summary['name'] = name_serie

# Write in a CSV
summary.to_csv(OUTPUT_RESULTS_FILE)

# Subsample df
struct_pca_df = df.xs(EXAMPLE_MODEL)

# Plot some metrics for struct_pca
for metric in METRICS:
    handles = plot_utilities.plot_lines(struct_pca_df,
                                        x_col=1,
                                        y_col=metric,
                                        splitby_col=0,
                                        colorby_col=2)
    for val, handle in handles.items():
        filename = CURVE_FILE_FORMAT.format(metric=metric, global_pen=val)
        handle.savefig(filename)

#####################################################################
# Loading as images                                                 #
# Projection to csv                                                 #
# eventually flip Projection and Loading such that they positivetly #
# correlate with clinical severity                                  #
#####################################################################

# Open mask
mask_ima = nib.load(INPUT_MASK)