Exemple #1
0
def plot_biomarker_predictions(args, diagnoses, values_observed, values_model):
    # Setup plot
    fig, ax = plt.subplots()
    pt.setup_axes(plt, ax)

    ax.set_title('{0} estimation based on {1} with {2} visits'.format(
        args.predict_biomarker, args.method, len(args.visits)))
    ax.set_xlabel('Observed {0}'.format(args.predict_biomarker))
    ax.set_ylabel('Predicted {0}'.format(args.predict_biomarker))

    # Draw plot
    plt.scatter(values_observed,
                values_model,
                s=15.0,
                c=diagnoses,
                edgecolor='none',
                vmin=0.0,
                vmax=1.0,
                cmap=pt.progression_cmap,
                alpha=0.5)

    # Show or save the plot
    plt.tight_layout()
    if args.plot_file is not None:
        plt.savefig(args.plot_file, transparent=True)
    else:
        plt.show()
    plt.close(fig)
def plot_dpi_dpr_distribution(args, dpis, dprs, diagnoses):
    print log.INFO, 'Plotting estimate distributions...'
    diagnoses = np.array(diagnoses)
    diagnoses[(0.25 <= diagnoses) & (diagnoses <= 0.75)] = 0.5

    # Setup plot
    fig, ax = plt.subplots()
    pt.setup_axes(plt, ax)

    biomarkers_str = args.method if args.biomarkers is None else ', '.join(args.biomarkers)
    ax.set_title('DP estimation using {0} at {1}'.format(biomarkers_str, ', '.join(args.visits)))
    ax.set_xlabel('DP')
    ax.set_ylabel('DPR')

    plt.scatter(dpis, dprs, c=diagnoses, edgecolor='none', s=25.0,
                vmin=0.0, vmax=1.0, cmap=pt.progression_cmap,
                alpha=0.5)

    # Plot legend
    # noinspection PyUnresolvedReferences
    rects = [mpl.patches.Rectangle((0, 0), 1, 1, fc=pt.color_cn + (0.5,), linewidth=0),
             mpl.patches.Rectangle((0, 0), 1, 1, fc=pt.color_mci + (0.5,), linewidth=0),
             mpl.patches.Rectangle((0, 0), 1, 1, fc=pt.color_ad + (0.5,), linewidth=0)]
    labels = ['CN', 'MCI', 'AD']
    legend = ax.legend(rects, labels, fontsize=10, ncol=len(rects), loc='upper center', framealpha=0.9)
    legend.get_frame().set_edgecolor((0.6, 0.6, 0.6))

    # Draw or save the plot
    plt.tight_layout()
    if args.plot_file is not None:
        plt.savefig(args.plot_file, transparent=True)
    else:
        plt.show()
    plt.close(fig)
Exemple #3
0
def plot_error_bars(args, biomarkers, errors):
    print log.INFO, 'Plotting error bars...'

    fig, ax = plt.subplots(figsize=(8, 5))
    pt.setup_axes(plt, ax)
    ax.set_title('Influence of the number of training samples')
    ax.set_xlabel('Number of samples')
    ax.set_ylabel('Mean area between PDFs')

    color = {
        'longitudinal': (0.0, 0.0, 0.0),
        'triangular': (0.0, 0.0, 0.5),
        'uniform': (0.0, 0.5, 0.0)
    }

    sample_numbers = range(args.sample_numbers_range[0],
                           args.sample_numbers_range[1],
                           args.sample_numbers_range[2])

    for biomarker in biomarkers:
        for sampling in ['longitudinal', 'triangular', 'uniform']:
            curve_median = []
            curve_err_1 = []
            curve_err_2 = []
            for experiment in sample_numbers:
                errors_experiment = errors[biomarker][sampling][experiment]

                median = np.median(errors_experiment)
                curve_median.append(median)
                curve_err_1.append(median -
                                   np.percentile(errors_experiment, 25))
                curve_err_2.append(
                    np.percentile(errors_experiment, 75) - median)

            plt.errorbar(sample_numbers,
                         curve_median,
                         yerr=[curve_err_1, curve_err_2],
                         linestyle='-',
                         color=color[sampling],
                         label='{0} sampling'.format(sampling))

    legend = plt.legend(fontsize=10,
                        ncol=3,
                        loc='upper center',
                        framealpha=0.9)
    legend.get_frame().set_edgecolor((0.6, 0.6, 0.6))

    # Show or save plot
    plt.tight_layout()
    if args.plot_file is not None:
        plt.savefig(args.plot_file, transparent=True)
    else:
        plt.show()
    plt.close(fig)
Exemple #4
0
def main():
    parser = argparse.ArgumentParser(description='Estimate model curves for biomarkers using VGAM.')
    parser.add_argument('-m', '--method', choices=DataHandler.get_method_choices(), default='all', help='the method to collect data for')
    parser.add_argument('-b', '--biomarkers', nargs='+', default=None, help='name of the biomarker to be plotted')
    parser.add_argument('-e', '--extrapolator', type=str, choices=['lin', 'sqrt', 'exp'], default='exp', help='the type of extrapolator')
    parser.add_argument('--plot_threshold', type=float, default=0.3, help='the threshold above which praphs are plotted')    
    parser.add_argument('--recompute_errors', action='store_true', help='recompute the matrix containing the fitting errors')
    parser.add_argument('--search_range', nargs=3, default=(1000, 5000, 10), help='the range in which the offset is sought')
    args = parser.parse_args()

    # Get the data files and biomarkers
    data_handler_joint = DataHandler.get_data_handler(method=args.method,
                                                      biomarkers=args.biomarkers,
                                                      phase='joint')
    biomarkers, offsets, errors, descriminativeness, overlap = get_fitting_data(args, data_handler_joint)

    # Plot single biomarker fits
    fig, ax = plt.subplots()
    pt.setup_axes(plt, ax, xgrid=False)
    ax.set_title('Optimal offset between CN/MCI and MCI/AD models')
    ax.set_xlabel('Offset (days)')
    ax.set_ylabel('Fitting error')
    for i, biomarker in enumerate(biomarkers):
        if descriminativeness[i] > args.plot_threshold:
            print log.RESULT, 'Min error for {0} at {1}'.format(biomarker, offsets[np.argmin(errors[i, :])])
            ax.plot(offsets, errors[i, :], label=biomarker, linestyle='--')

    # Get optimal offset
    mean_errors = np.mean(errors, 0)
    weighted_mean_errors = np.dot(errors.T, descriminativeness) / np.sum(descriminativeness)

    # Plot joint fit
    ax.plot(offsets, mean_errors, label='Mean', linewidth=2, color='g')
    ax.plot(offsets, weighted_mean_errors, label='Weighted mean', linewidth=2, color='r')

    # Get and lot optimal offset
    optimal_offset = offsets[np.argmin(mean_errors)]
    optimal_offset_weighted = offsets[np.argmin(weighted_mean_errors)]
    print log.RESULT, 'Optimal threshold: {0}'.format(optimal_offset)
    print log.RESULT, 'Optimal threshold (weighted): {0}'.format(optimal_offset_weighted)
    ax.axvline(optimal_offset, linestyle=':', color='g')
    ax.axvline(optimal_offset_weighted, linestyle=':', color='r')

    # Plot overlap
    ax.axvline(overlap, color='0.15', linestyle=':')

    ax.legend()
    plt.show()
    plt.close(fig)
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('-b', '--biomarkers', nargs=2, default=['D1', 'D2'], help='name of the biomarker to be plotted')
    parser.add_argument('--plot_file', type=str, default=None, help='filename of the output file')
    args = parser.parse_args()

    # Collect data for test
    data_handler = DataHandler.get_data_handler(biomarkers=args.biomarkers)
    biomarkers = data_handler.get_biomarker_names()
    measurements = data_handler.get_measurements_as_dict(biomarkers=biomarkers,
                                                         select_complete=True)

    # Collect biomarker values
    biomarkers_1 = []
    biomarkers_2 = []
    diagnoses = []
    for rid in measurements:
        for visit in measurements[rid]:
            biomarkers_1.append(measurements[rid][visit][biomarkers[0]])
            biomarkers_2.append(measurements[rid][visit][biomarkers[1]])
            diagnoses.append(measurements[rid][visit]['DX.scan'])
    diagnoses = np.array(diagnoses)
    diagnoses[(0.25 <= diagnoses) & (diagnoses <= 0.75)] = 0.5

    # Setup plot
    fig, ax = plt.subplots()
    pt.setup_axes(plt, ax)
    ax.scatter(biomarkers_1, biomarkers_2, s=15.0, c=diagnoses, edgecolor='none',
               vmin=0.0, vmax=1.0, cmap=pt.progression_cmap, alpha=0.25)
    ax.set_xlabel(biomarkers[0])
    ax.set_ylabel(biomarkers[1])

    # Plot legend
    rects = [mpl.patches.Rectangle((0, 0), 1, 1, fc=pt.color_cn + (0.25,), linewidth=0),
             mpl.patches.Rectangle((0, 0), 1, 1, fc=pt.color_mci + (0.25,), linewidth=0),
             mpl.patches.Rectangle((0, 0), 1, 1, fc=pt.color_ad + (0.25,), linewidth=0)]
    labels = ['CN', 'MCI', 'AD']
    legend = ax.legend(rects, labels, fontsize=10, ncol=len(rects), loc='upper center', framealpha=0.9)
    legend.get_frame().set_edgecolor((0.6, 0.6, 0.6))

    # Draw or save the plot
    plt.tight_layout()
    if args.plot_file is not None:
        plt.savefig(args.plot_file, transparent=True)
    else:
        plt.show()
    plt.close(fig)
def plot_error_bars(args, biomarkers, errors):
    print log.INFO, 'Plotting error bars...'

    fig, ax = plt.subplots(figsize=(8, 5))
    pt.setup_axes(plt, ax)
    ax.set_title('Influence of the number of training samples')
    ax.set_xlabel('Number of samples')
    ax.set_ylabel('Mean area between PDFs')

    color = {'longitudinal': (0.0, 0.0, 0.0), 'triangular': (0.0, 0.0, 0.5), 'uniform': (0.0, 0.5, 0.0)}

    sample_numbers = range(args.sample_numbers_range[0],
                           args.sample_numbers_range[1],
                           args.sample_numbers_range[2])

    for biomarker in biomarkers:
        for sampling in ['longitudinal', 'triangular', 'uniform']:
            curve_median = []
            curve_err_1 = []
            curve_err_2 = []
            for experiment in sample_numbers:
                errors_experiment = errors[biomarker][sampling][experiment]

                median = np.median(errors_experiment)
                curve_median.append(median)
                curve_err_1.append(median - np.percentile(errors_experiment, 25))
                curve_err_2.append(np.percentile(errors_experiment, 75) - median)

            plt.errorbar(sample_numbers, curve_median, yerr=[curve_err_1, curve_err_2],
                         linestyle='-', color=color[sampling],
                         label='{0} sampling'.format(sampling))

    legend = plt.legend(fontsize=10, ncol=3, loc='upper center', framealpha=0.9)
    legend.get_frame().set_edgecolor((0.6, 0.6, 0.6))

    # Show or save plot
    plt.tight_layout()
    if args.plot_file is not None:
        plt.savefig(args.plot_file, transparent=True)
    else:
        plt.show()
    plt.close(fig)
def plot_biomarker_predictions(args, diagnoses, values_observed, values_model):
    # Setup plot
    fig, ax = plt.subplots()
    pt.setup_axes(plt, ax)

    ax.set_title('{0} estimation based on {1} with {2} visits'.format(args.predict_biomarker,
                                                                      args.method,
                                                                      len(args.visits)))
    ax.set_xlabel('Observed {0}'.format(args.predict_biomarker))
    ax.set_ylabel('Predicted {0}'.format(args.predict_biomarker))

    # Draw plot
    plt.scatter(values_observed, values_model, s=15.0,
                c=diagnoses, edgecolor='none', vmin=0.0, vmax=1.0,
                cmap=pt.progression_cmap, alpha=0.5)

    # Show or save the plot
    plt.tight_layout()
    if args.plot_file is not None:
        plt.savefig(args.plot_file, transparent=True)
    else:
        plt.show()
    plt.close(fig)
def plot_dpi_dpr_distribution(args, dpis, dprs, diagnoses):
    print log.INFO, 'Plotting estimate distributions...'
    diagnoses = np.array(diagnoses)
    diagnoses[(0.25 <= diagnoses) & (diagnoses <= 0.75)] = 0.5

    # Setup plot
    fig, ax = plt.subplots()
    pt.setup_axes(plt, ax)

    biomarkers_str = args.method if args.biomarkers is None else ', '.join(
        args.biomarkers)
    ax.set_title('DP estimation using {0} at {1}'.format(
        biomarkers_str, ', '.join(args.visits)))
    ax.set_xlabel('DP')
    ax.set_ylabel('DPR')

    plt.scatter(dpis,
                dprs,
                c=diagnoses,
                edgecolor='none',
                s=25.0,
                vmin=0.0,
                vmax=1.0,
                cmap=pt.progression_cmap,
                alpha=0.5)

    # Plot legend
    # noinspection PyUnresolvedReferences
    rects = [
        mpl.patches.Rectangle((0, 0),
                              1,
                              1,
                              fc=pt.color_cn + (0.5, ),
                              linewidth=0),
        mpl.patches.Rectangle((0, 0),
                              1,
                              1,
                              fc=pt.color_mci + (0.5, ),
                              linewidth=0),
        mpl.patches.Rectangle((0, 0),
                              1,
                              1,
                              fc=pt.color_ad + (0.5, ),
                              linewidth=0)
    ]
    labels = ['CN', 'MCI', 'AD']
    legend = ax.legend(rects,
                       labels,
                       fontsize=10,
                       ncol=len(rects),
                       loc='upper center',
                       framealpha=0.9)
    legend.get_frame().set_edgecolor((0.6, 0.6, 0.6))

    # Draw or save the plot
    plt.tight_layout()
    if args.plot_file is not None:
        plt.savefig(args.plot_file, transparent=True)
    else:
        plt.show()
    plt.close(fig)
def plot_errors_sampling(args, biomarkers, errors, num_samples):
    print log.INFO, 'Plotting error bars...'

    fig, ax = plt.subplots(figsize=(8, 5))
    pt.setup_axes(plt, ax, xgrid=False)

    ax.set_title('Influence of the sampling strategy on DP estimation')
    ax.set_ylabel('Mean progress estimation error')
    plt.tick_params(labelbottom='off')

    samplings = ['longitudinal', 'triangular', 'uniform']
    biomarker_strings = {'synth_hipp': '$\mathcal{M}^{HV}$',
                         'synth_mmse': '$\mathcal{M}^{MMSE}$',
                         'synth_cdrsb': '$\mathcal{M}^{CDR-SB}$'}

    # Collect data
    data = []
    medians = []
    for biomarker in biomarkers:
        for sampling in samplings:
            data.append(errors[biomarker][sampling][num_samples])
            medians.append(np.median(errors[biomarker][sampling][num_samples]))

    # Set limits
    max_data = np.max(data)
    ax.set_ylim(0, 1.15 * max_data)

    # Draw boxplot
    boxplot = plt.boxplot(data, patch_artist=True)

    # Set boxplot colours
    colours = [(0.0, 0.0, 0.0), (0.0, 0.0, 0.5), (0.0, 0.5, 0.0)] * len(biomarkers)
    for i in range(len(data)):
        pt.set_boxplot_color(boxplot, i, colours[i])

    # Write median errors as text
    upper_labels = [str(np.round(m, 3)) for m in medians]
    for i in np.arange(len(upper_labels)):
        ax.text(i + 1, 1.02 * max_data, upper_labels[i],
                horizontalalignment='center', size=10, color=colours[i])

    # Write category labels
    for i, biomarker in enumerate(biomarkers):
        plt.text((i + 0.5) * len(samplings) + 0.5, -74,
                 biomarker_strings[biomarker],
                 horizontalalignment='center')

    # Plot legend
    legend = ax.legend([mpl.patches.Rectangle((0, 0), 1, 1, fc=(0.0, 0.0, 0.0, 0.2), ec=(0.0, 0.0, 0.0, 1.0), linewidth=1),
                        mpl.patches.Rectangle((0, 0), 1, 1, fc=(0.0, 0.0, 0.5, 0.2), ec=(0.0, 0.0, 0.5, 1.0), linewidth=1),
                        mpl.patches.Rectangle((0, 0), 1, 1, fc=(0.0, 0.5, 0.0, 0.2), ec=(0.0, 0.5, 0.0, 1.0), linewidth=1)],
                       ['{0} sampling'.format(s) for s in samplings], fontsize=10, ncol=3, loc='upper center', framealpha=0.9)
    legend.get_frame().set_edgecolor((0.6, 0.6, 0.6))

    # Draw horizontal lines
    for x in range(3, len(data), 3):
        plt.axvline(x + 0.5, color='k', alpha=0.4)

    # Show or save plot
    plt.tight_layout()
    if args.plot_file is not None:
        plt.savefig(args.plot_file, transparent=True)
    else:
        plt.show()
    plt.close(fig)
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('-m', '--method', choices=DataHandler.get_method_choices(), default='all', help='the method to collect data for')
    parser.add_argument('-b', '--biomarkers', nargs='+', default=None, help='name of the biomarker to be plotted')
    parser.add_argument('-p', '--phase', default=None, choices=DataHandler.get_phase_choices(), help='the phase for which the model is to be trained')
    parser.add_argument('--save_plots', action='store_true', default=False, help='save the plots with a default filename')
    args = parser.parse_args()

    # Collect data for test
    data_handler = DataHandler.get_data_handler(method=args.method,
                                                biomarkers=args.biomarkers,
                                                phase=args.phase)
    biomarkers = data_handler.get_biomarker_names()
    measurements = data_handler.get_measurements_as_dict(visits=['bl', 'm12'],
                                                         biomarkers=biomarkers,
                                                         select_training_set=True,
                                                         select_complete=True)

    # Setup plotting folder
    eval_folder = DataHandler.make_dir(data_handler.get_eval_folder(), 'quants')

    # Process all biomarkers
    for biomarker in biomarkers:
        print log.INFO, 'Generating quantile correlation plot for {0}...'.format(biomarker)
        model_file = data_handler.get_model_file(biomarker)
        pm = ProgressionModel(biomarker, model_file)

        q_file = os.path.join(eval_folder, '{0}.p'.format(biomarker))

        if os.path.isfile(q_file):
            (q_bl, q_m12) = pickle.load(open(q_file, 'rb'))
        else:
            q_bl = []
            q_m12 = []

            for rid in measurements:
                val_bl = measurements[rid]['bl'][biomarker]
                val_m12 = measurements[rid]['m12'][biomarker]

                p_bl = measurements[rid]['bl']['progress']
                p_m12 = measurements[rid]['m12']['progress']

                q_bl.append(pm.approximate_quantile(p_bl, val_bl))
                q_m12.append(pm.approximate_quantile(p_m12, val_m12))

            pickle.dump((q_bl, q_m12), open(q_file, 'wb'))

        # Setup plot
        fig, axs = plt.subplots(1, 2)
        plt.suptitle('Correlation between bl and m12 quantiles')

        # Plot 1
        ax = axs[0]
        pt.setup_axes(plt, ax, yspine=True)
        ax.set_xlabel('Quantile bl')
        ax.set_ylabel('Quantile m12')

        ax.scatter(q_bl, q_m12, edgecolor='none', s=25.0, alpha=0.5)

        # Plot 2
        q_bl = np.array(q_bl)
        q_m12 = np.array(q_m12)

        errors = q_bl - q_m12
        loc, scale = norm.fit(errors, floc=0.0)

        ax = axs[1]
        pt.setup_axes(plt, ax)
        ax.set_xlabel('Difference bl to m12')
        ax.set_ylabel('Probability')
        ax.set_xlim(-1.05, 1.05)
        ax.hist(errors, bins=15, normed=True, histtype='stepfilled', alpha=0.3)
        x = np.linspace(-1.0, 1.0, 100)
        ax.plot(x, norm.pdf(x, loc=loc, scale=scale), color='k')

        # Draw or save the plot
        plt.tight_layout()
        if args.save_plots:
            plot_file = os.path.join(eval_folder, '{0}.pdf'.format(biomarker))
            plt.savefig(plot_file, transparent=True)
        else:
            plt.show()
        plt.close(fig)
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('-b',
                        '--biomarkers',
                        nargs=2,
                        default=['D1', 'D2'],
                        help='name of the biomarker to be plotted')
    parser.add_argument('--plot_file',
                        type=str,
                        default=None,
                        help='filename of the output file')
    args = parser.parse_args()

    # Collect data for test
    data_handler = DataHandler.get_data_handler(biomarkers=args.biomarkers)
    biomarkers = data_handler.get_biomarker_names()
    measurements = data_handler.get_measurements_as_dict(biomarkers=biomarkers,
                                                         select_complete=True)

    # Collect biomarker values
    biomarkers_1 = []
    biomarkers_2 = []
    diagnoses = []
    for rid in measurements:
        for visit in measurements[rid]:
            biomarkers_1.append(measurements[rid][visit][biomarkers[0]])
            biomarkers_2.append(measurements[rid][visit][biomarkers[1]])
            diagnoses.append(measurements[rid][visit]['DX.scan'])
    diagnoses = np.array(diagnoses)
    diagnoses[(0.25 <= diagnoses) & (diagnoses <= 0.75)] = 0.5

    # Setup plot
    fig, ax = plt.subplots()
    pt.setup_axes(plt, ax)
    ax.scatter(biomarkers_1,
               biomarkers_2,
               s=15.0,
               c=diagnoses,
               edgecolor='none',
               vmin=0.0,
               vmax=1.0,
               cmap=pt.progression_cmap,
               alpha=0.25)
    ax.set_xlabel(biomarkers[0])
    ax.set_ylabel(biomarkers[1])

    # Plot legend
    rects = [
        mpl.patches.Rectangle((0, 0),
                              1,
                              1,
                              fc=pt.color_cn + (0.25, ),
                              linewidth=0),
        mpl.patches.Rectangle((0, 0),
                              1,
                              1,
                              fc=pt.color_mci + (0.25, ),
                              linewidth=0),
        mpl.patches.Rectangle((0, 0),
                              1,
                              1,
                              fc=pt.color_ad + (0.25, ),
                              linewidth=0)
    ]
    labels = ['CN', 'MCI', 'AD']
    legend = ax.legend(rects,
                       labels,
                       fontsize=10,
                       ncol=len(rects),
                       loc='upper center',
                       framealpha=0.9)
    legend.get_frame().set_edgecolor((0.6, 0.6, 0.6))

    # Draw or save the plot
    plt.tight_layout()
    if args.plot_file is not None:
        plt.savefig(args.plot_file, transparent=True)
    else:
        plt.show()
    plt.close(fig)
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('-m',
                        '--method',
                        choices=DataHandler.get_method_choices(),
                        default='all',
                        help='the method to collect data for')
    parser.add_argument('-b',
                        '--biomarkers',
                        nargs='+',
                        default=None,
                        help='name of the biomarker to be plotted')
    parser.add_argument('-p',
                        '--phase',
                        default=None,
                        choices=DataHandler.get_phase_choices(),
                        help='the phase for which the model is to be trained')
    parser.add_argument('--save_plots',
                        action='store_true',
                        default=False,
                        help='save the plots with a default filename')
    args = parser.parse_args()

    # Collect data for test
    data_handler = DataHandler.get_data_handler(method=args.method,
                                                biomarkers=args.biomarkers,
                                                phase=args.phase)
    biomarkers = data_handler.get_biomarker_names()
    measurements = data_handler.get_measurements_as_dict(
        visits=['bl', 'm12'],
        biomarkers=biomarkers,
        select_training_set=True,
        select_complete=True)

    # Setup plotting folder
    eval_folder = DataHandler.make_dir(data_handler.get_eval_folder(),
                                       'quants')

    # Process all biomarkers
    for biomarker in biomarkers:
        print log.INFO, 'Generating quantile correlation plot for {0}...'.format(
            biomarker)
        model_file = data_handler.get_model_file(biomarker)
        pm = ProgressionModel(biomarker, model_file)

        q_file = os.path.join(eval_folder, '{0}.p'.format(biomarker))

        if os.path.isfile(q_file):
            (q_bl, q_m12) = pickle.load(open(q_file, 'rb'))
        else:
            q_bl = []
            q_m12 = []

            for rid in measurements:
                val_bl = measurements[rid]['bl'][biomarker]
                val_m12 = measurements[rid]['m12'][biomarker]

                p_bl = measurements[rid]['bl']['progress']
                p_m12 = measurements[rid]['m12']['progress']

                q_bl.append(pm.approximate_quantile(p_bl, val_bl))
                q_m12.append(pm.approximate_quantile(p_m12, val_m12))

            pickle.dump((q_bl, q_m12), open(q_file, 'wb'))

        # Setup plot
        fig, axs = plt.subplots(1, 2)
        plt.suptitle('Correlation between bl and m12 quantiles')

        # Plot 1
        ax = axs[0]
        pt.setup_axes(plt, ax, yspine=True)
        ax.set_xlabel('Quantile bl')
        ax.set_ylabel('Quantile m12')

        ax.scatter(q_bl, q_m12, edgecolor='none', s=25.0, alpha=0.5)

        # Plot 2
        q_bl = np.array(q_bl)
        q_m12 = np.array(q_m12)

        errors = q_bl - q_m12
        loc, scale = norm.fit(errors, floc=0.0)

        ax = axs[1]
        pt.setup_axes(plt, ax)
        ax.set_xlabel('Difference bl to m12')
        ax.set_ylabel('Probability')
        ax.set_xlim(-1.05, 1.05)
        ax.hist(errors, bins=15, normed=True, histtype='stepfilled', alpha=0.3)
        x = np.linspace(-1.0, 1.0, 100)
        ax.plot(x, norm.pdf(x, loc=loc, scale=scale), color='k')

        # Draw or save the plot
        plt.tight_layout()
        if args.save_plots:
            plot_file = os.path.join(eval_folder, '{0}.pdf'.format(biomarker))
            plt.savefig(plot_file, transparent=True)
        else:
            plt.show()
        plt.close(fig)
def plot_errors(args, values, methods):
    fig, ax = plt.subplots()
    pt.setup_axes(plt, ax, xgrid=False)

    num_methods = len(methods)
    for i in xrange(num_methods):
        ax.axvline(i * 2 + 1.5, linestyle='-', color='k', alpha=0.4)

    ax.set_axisbelow(True)
    ax.set_title('Prediction of {0}'.format(args.predict_biomarker))
    ax.set_ylabel('Prediction error')
    ax.set_xticklabels([])

    # Append plot for naive estimation
    values_observed = np.array(values[values.keys()[0]]['observed'])
    values_naive = np.array(values[values.keys()[0]]['naive'])
    errors_naive = np.abs(values_observed - values_naive)
    data = [errors_naive]
    medians = [np.median(errors_naive)]
    weights = ['normal']
    for method in methods:
        values_model1 = np.array(values[method]['model_dpi'])
        values_model2 = np.array(values[method]['model_dpi_dpr'])
        errors_model1 = np.abs(values_observed - values_model1)
        errors_model2 = np.abs(values_observed - values_model2)

        data.append(errors_model1)
        data.append(errors_model2)

        medians.append(np.median(errors_model1))
        medians.append(np.median(errors_model2))

        weights.append('bold' if stats.ttest_rel(errors_naive, errors_model1
                                                 )[1] < 0.01 else 'normal')
        weights.append('bold' if stats.ttest_rel(errors_naive, errors_model2
                                                 )[1] < 0.01 else 'normal')

    # Set colors
    colors = [(0.0, 0.0, 0.0)]
    for i in range(num_methods):
        colors.append((0.0, 0.0, 0.5))
        colors.append((0.0, 0.5, 0.0))

    # Set limits
    max_data = np.max(data)
    ax.set_ylim(-0.01 * max_data, 1.01 * max_data)

    # Add labels to x axis
    for i, method in enumerate(methods):
        ax.text(i * 2 + 2.5,
                -0.04 * max_data,
                method,
                horizontalalignment='center')

    # Write median errors as text
    pos = np.arange(1, 2 * num_methods + 2)
    upper_labels = [str(np.round(m, 2)) for m in medians]
    for i in range(2 * num_methods + 1):
        ax.text(pos[i],
                0.91 * max_data,
                upper_labels[i],
                weight=weights[i],
                horizontalalignment='center',
                size=10,
                color=colors[i])

    # Plot boxplots
    boxplot = plt.boxplot(data, patch_artist=True)
    for i in range(len(boxplot['boxes'])):
        pt.set_boxplot_color(boxplot, i, colors[i])

    # Plot legend
    legend = ax.legend([
        mpl.patches.Rectangle((0, 0),
                              1,
                              1,
                              fc=(0.0, 0.0, 0.0, 0.2),
                              ec=(0.0, 0.0, 0.0, 1.0),
                              linewidth=1),
        mpl.patches.Rectangle((0, 0),
                              1,
                              1,
                              fc=(0.0, 0.0, 0.5, 0.2),
                              ec=(0.0, 0.0, 0.5, 1.0),
                              linewidth=1),
        mpl.patches.Rectangle((0, 0),
                              1,
                              1,
                              fc=(0.0, 0.5, 0.0, 0.2),
                              ec=(0.0, 0.5, 0.0, 1.0),
                              linewidth=1)
    ], ['Naive', 'DPI', 'DPI + DPR'],
                       fontsize=10,
                       ncol=3,
                       loc='upper center',
                       framealpha=0.9)
    legend.get_frame().set_edgecolor((0.6, 0.6, 0.6))

    # Show or save plot
    plt.tight_layout()
    if args.plot_file is not None:
        plt.savefig(args.plot_file, transparent=True)
    else:
        plt.show()
    plt.close(fig)
def plot_biomarker(data_handler, biomarker, measurements, dpi, dpr):
    """
    Plot the model of one biomarker with the fitted values

    :param data_handler: the data handler
    :param biomarker: the biomarker to plot
    :param measurements: the measurements containing the biomarker samples of one subject
    :param dpi: the estimated DPI
    :param dpr: the estimated DPR
    """
    model_file = data_handler.get_model_file(biomarker)
    if not os.path.isfile(model_file):
        print log.ERROR, 'Model file not found: {0}'.format(model_file)
        return

    print log.INFO, 'Generating plot for {0}...'.format(biomarker)

    #
    # Read model
    #
    pm = ProgressionModel(biomarker, model_file)
    progress_extrapolate = 0.3 * (pm.max_progress - pm.min_progress)
    min_progress_extrapolate = int(pm.min_progress - progress_extrapolate)
    max_progress_extrapolate = int(pm.max_progress + progress_extrapolate)
    progress_linspace_ex1 = np.linspace(min_progress_extrapolate,
                                        pm.min_progress, 20)
    progress_linspace_int = np.linspace(pm.min_progress, pm.max_progress, 60)
    progress_linspace_ex2 = np.linspace(pm.max_progress,
                                        max_progress_extrapolate, 20)

    #
    # Setup plot
    #
    biomarker_string = pt.get_biomarker_string(biomarker)
    figure_width = 6
    fig = plt.figure(figsize=(figure_width, 5))
    ax1 = plt.subplot(1, 1, 1)
    pt.setup_axes(plt, ax1, xgrid=False, ygrid=False)
    ax1.set_title(
        'Model for {0} with fitted sample values'.format(biomarker_string))
    ax1.set_xlabel('Disease progress (days before/after conversion to MCI)')
    ax1.set_ylabel(DataHandler.get_biomarker_unit(biomarker))
    ax1.set_xlim(min_progress_extrapolate, max_progress_extrapolate)

    #
    # Plot the percentile curves of the fitted model
    #
    ax1.axvline(pm.min_progress, color='0.15', linestyle=':')
    ax1.axvline(pm.max_progress, color='0.15', linestyle=':')

    quantiles = [0.1, 0.25, 0.5, 0.75, 0.9]
    grey_values = ['0.4', '0.2', '0', '0.2', '0.4']
    for grey_value, quantile in zip(grey_values, quantiles):
        curve_int = pm.get_quantile_curve(progress_linspace_int, quantile)
        ax1.plot(progress_linspace_int, curve_int, color=grey_value)

        curve_ex1 = pm.get_quantile_curve(progress_linspace_ex1, quantile)
        curve_ex2 = pm.get_quantile_curve(progress_linspace_ex2, quantile)
        ax1.plot(progress_linspace_ex1, curve_ex1, '--', color=grey_value)
        ax1.plot(progress_linspace_ex2, curve_ex2, '--', color=grey_value)

        label = 'q = {0}'.format(quantile * 100)
        ax1.text(progress_linspace_int[-1] + 100,
                 curve_int[-1],
                 label,
                 fontsize=10)

    #
    # Plot points
    #
    progr_points = []
    value_points = []
    diagn_points = []
    for visit in measurements[0]:
        if biomarker in measurements[0][visit]:
            progress = measurements[0][visit]['scantime'] * dpr + dpi
            value = measurements[0][visit][biomarker]
            progr_points.append(progress)
            value_points.append(value)
            diagn_points.append(1.0)
            ax1.axvline(progress, color='b', linestyle='--')
            ax1.text(progress + 150, value, visit, color='b', fontsize=10)

    ax1.scatter(progr_points,
                value_points,
                s=25.0,
                color='b',
                edgecolor='none',
                vmin=0.0,
                vmax=1.0,
                alpha=0.9)

    #
    # Draw or save the plot
    #
    plt.tight_layout()
    plt.show()
    plt.close(fig)
Exemple #15
0
def evaluate_curves(biomarker_name, num_samples=200, show_plots=False, csig=0):
    biomarker = 'synth_{0}'.format(biomarker_name)
    print log.INFO, 'Evaluating {0} for {1} samples, csig={2}...'.format(biomarker, num_samples, csig)
    donohue_model_path = '/Development/DiseaseProgressionModel/models/donohue/'
    vgam_model_path = '/Development/DiseaseProgressionModel/models/synth/'

    # Setup plot
    if show_plots:
        fig = plt.figure()
        ax = plt.subplot(1, 1, 1)
        pt.setup_axes(plt, ax, xgrid=False, ygrid=False)
        ax.set_title('')
        ax.set_xlabel('')
    else:
        fig = None
        ax = None

    # Initialise values
    offset_donohue = 0  # 182.5
    errors_donohue = []
    errors_vgam_mean = []
    errors_vgam_median = []

    # Get real curve values
    progress_linspace = np.linspace(-1500, 1500)
    mean_curve = [SynthModel.get_mean_value(biomarker, p) for p in progress_linspace]
    median_curve = [SynthModel.get_distributed_value(biomarker, p, cdf=0.5) for p in progress_linspace]

    # Plot synthetic model curve
    if show_plots:
        progress_linspace_synth = np.linspace(-2500, 2500, 100)
        quantiles = [0.1, 0.25, 0.5, 0.75, 0.9]
        alphas = [0.4, 0.7, 1.0, 0.7, 0.4]
        for quantile, alpha in zip(quantiles, alphas):
            curve_synth = [SynthModel.get_distributed_value(biomarker, p, cdf=quantile)
                           for p in progress_linspace_synth]
            ax.plot(progress_linspace_synth, curve_synth, color='b', alpha=alpha)
        curve_synth = [SynthModel.get_mean_value(biomarker, p) for p in progress_linspace_synth]
        ax.plot(progress_linspace_synth, curve_synth, '--', color='b')

    # Get values for mean calculation
    end_values = [SynthModel.get_distributed_value(biomarker, progress_linspace[0], cdf=0.001),
                  SynthModel.get_distributed_value(biomarker, progress_linspace[-1], cdf=0.001),
                  SynthModel.get_distributed_value(biomarker, progress_linspace[0], cdf=0.999),
                  SynthModel.get_distributed_value(biomarker, progress_linspace[-1], cdf=0.999)]
    values = np.linspace(min(end_values), max(end_values), 100)
    for run in range(100):
        # Get Donohue model
        donohue_file = os.path.join(donohue_model_path,
                                    'population_value-{0}_csig{1}_run{2}.csv'.format(biomarker_name, csig, run))

        r = mlab.csv2rec(donohue_file)
        progrs = r[r.dtype.names[0]] - offset_donohue
        vals = r[r.dtype.names[1]]

        curve_donohue = []
        progr_donohue = []
        for p in progress_linspace:
            if progrs[0] < p < progrs[-1]:
                i = 1
                while p > progrs[i]:
                    i += 1
                progr_donohue.append(float(progrs[i]))
                curve_donohue.append(float(vals[i]))
            else:
                print log.WARNING, 'Model scope too small... skipping!'
                continue

        # Get VGAM model
        if csig == 0:
            vgam_model_file = os.path.join(vgam_model_path,
                                           '{0}_model_{1}_longitudinal_{2}.csv'.format(biomarker,
                                                                                       num_samples, run))
        else:
            vgam_model_file = os.path.join(vgam_model_path,
                                           '{0}_model_{1}_longitudinal_csig{2}.0_{3}.csv'.format(biomarker,
                                                                                                 num_samples,
                                                                                                 csig, run))

        pm = ProgressionModel(biomarker, vgam_model_file)
        curve_vgam_median = pm.get_quantile_curve(progress_linspace, 0.5)
        curve_vgam_mean = [np.sum(pm.get_density_distribution(values, p) * values /
                                  np.sum(pm.get_density_distribution(values, p))) for p in progress_linspace]

        # Calculate errors
        errors_donohue.append(np.mean(np.abs(np.array(curve_donohue) - np.array(mean_curve))))
        errors_vgam_mean.append(np.mean(np.abs(np.array(curve_vgam_mean) - np.array(mean_curve))))
        errors_vgam_median.append(np.mean(np.abs(np.array(curve_vgam_median) - np.array(median_curve))))

        if show_plots:
            ax.plot(progr_donohue, curve_donohue, '--', color='g', alpha=0.2, linewidth=2)
            # ax.plot(progress_linspace, curve_vgam_median, '-', color='r', alpha=0.2, linewidth=2)
            ax.plot(progress_linspace, curve_vgam_mean, '--', color='r', alpha=0.2, linewidth=2)

    print log.RESULT, 'Donohue (mean):', np.mean(errors_donohue), np.var(errors_donohue)
    print log.RESULT, 'VGAM    (mean):', np.mean(errors_vgam_mean), np.var(errors_vgam_mean)
    print log.RESULT, 'VGAM  (median):', np.mean(errors_vgam_median), np.var(errors_vgam_median)

    # Draw or save the plot
    if show_plots:
        plt.tight_layout()
        plt.show()
        plt.close(fig)
def plot_model(args, data_handler, biomarker):
    model_file = data_handler.get_model_file(biomarker)
    if not os.path.isfile(model_file):
        print log.ERROR, 'Model file not found: {0}'.format(model_file)
        return

    print log.INFO, 'Generating plot for {0}...'.format(biomarker)
    plot_synth_model = args.plot_synth_model and biomarker in SynthModel.get_biomarker_names()

    #
    # Read model
    #
    pm = ProgressionModel(biomarker, model_file, extrapolator=args.extrapolator)
    progress_extrapolate = 0.3 * (pm.max_progress - pm.min_progress)
    min_progress_extrapolate = int(pm.min_progress - progress_extrapolate)
    max_progress_extrapolate = int(pm.max_progress + progress_extrapolate)
    progress_linspace_ex1 = np.linspace(min_progress_extrapolate, pm.min_progress, 20)
    progress_linspace_int = np.linspace(pm.min_progress, pm.max_progress, 60)
    progress_linspace_ex2 = np.linspace(pm.max_progress, max_progress_extrapolate, 20)

    # Calc min and max val in interval between 1% and 99% percentie
    min_val, max_val = pm.get_value_range([0.1, 0.9])
#     progress_linspace = np.linspace(min_progress_extrapolate, max_progress_extrapolate, 100)
#     min_val = float('inf')
#     max_val = float('-inf')
#     for quantile in [0.1, 0.9]:
#         curve = pm.get_quantile_curve(progress_linspace, quantile)
#         min_val = min(min_val, np.min(curve))
#         max_val = max(max_val, np.max(curve))

    #
    # Setup plot
    #
    biomarker_string = pt.get_biomarker_string(biomarker)
    figure_width = 6 if args.no_densities or args.only_densities else 12
    fig = plt.figure(figsize=(figure_width, 5))
    if args.only_densities:
        ax1 = None
        ax2 = plt.subplot(1, 1, 1)
        pt.setup_axes(plt, ax2, xgrid=False, ygrid=False)
    elif args.no_densities:
        ax1 = plt.subplot(1, 1, 1)
        ax2 = None
        pt.setup_axes(plt, ax1, xgrid=False, ygrid=False)
    else:
        ax1 = plt.subplot(1, 2, 1)
        ax2 = plt.subplot(1, 2, 2)
        pt.setup_axes(plt, ax1, xgrid=False, ygrid=False)
        pt.setup_axes(plt, ax2)

    if not args.only_densities:
        if args.no_model and not args.plot_synth_model:
            ax1.set_title('Aligned samples for {0}'.format(biomarker_string))
        else:
            ax1.set_title('Quantile curves for {0}'.format(biomarker_string))
        if args.phase == 'mciad':
            ax1.set_xlabel('Disease progress (days before/after conversion to AD)')
        else:
            ax1.set_xlabel('Disease progress (days before/after conversion to MCI)')
        ax1.set_ylabel(DataHandler.get_biomarker_unit(biomarker))
        if args.xlim is not None:
            ax1.set_xlim(args.xlim[0], args.xlim[1])
        else:
            ax1.set_xlim(min_progress_extrapolate, max_progress_extrapolate)
        if args.ylim is not None:
            ax1.set_ylim(args.ylim[0], args.ylim[1])

    #
    # Plot the percentile curves of the fitted model
    #
    if not args.no_model and not args.only_densities:
        ax1.axvline(pm.min_progress, color='0.15', linestyle=':')
        ax1.axvline(pm.max_progress, color='0.15', linestyle=':')

        quantiles = [0.1, 0.25, 0.5, 0.75, 0.9]
        grey_values = ['0.4', '0.2', '0', '0.2', '0.4']
        for grey_value, quantile in zip(grey_values, quantiles):
            curve_int = pm.get_quantile_curve(progress_linspace_int, quantile)
            ax1.plot(progress_linspace_int, curve_int, color=grey_value)

            if not args.no_extrapolation:
                curve_ex1 = pm.get_quantile_curve(progress_linspace_ex1, quantile)
                curve_ex2 = pm.get_quantile_curve(progress_linspace_ex2, quantile)
                ax1.plot(progress_linspace_ex1, curve_ex1, '--', color=grey_value)
                ax1.plot(progress_linspace_ex2, curve_ex2, '--', color=grey_value)

            if args.plot_quantile_label:
                label = '$q={0}\%$'.format(quantile * 100)
                ax1.text(progress_linspace_int[-1] + 10, curve_int[-1], label, fontsize=10)

        if args.plot_donohue:
            print 'Plotting Donohue'
            donohue_file = os.path.join(data_handler._conf.models_folder,
                                        'donohue', 'population_{0}.csv'.format(biomarker.replace(' ', '.')))
            if not os.path.isfile(donohue_file):
                print log.ERROR, 'Donohue model file not found: {0}'.format(donohue_file)
                return

            r = mlab.csv2rec(donohue_file)
            if args.method == 'joint':
                offset = 2200
            else:
                offset = 300
            progrs = r[r.dtype.names[0]] * 30.44 + offset
            vals = r[r.dtype.names[1]]
            curve_donohue = []
            progr_donohue = []
            for p in progress_linspace_int:
                if progrs[0] < p < progrs[-1]:
                    i = 1
                    while p > progrs[i]:
                        i += 1
                    # TODO linear interpolation
                    progr_donohue.append(progrs[i])
                    curve_donohue.append(vals[i])
            ax1.plot(progr_donohue, curve_donohue, '--', color='b', linewidth=2)

    #
    # Plot synthetic model curve
    #
    if plot_synth_model:
        progress_linspace_synth = np.linspace(-2500, 2500, 100)
        quantiles = [0.1, 0.25, 0.5, 0.75, 0.9]
        alphas = [0.4, 0.7, 1.0, 0.7, 0.4]
        for quantile, alpha in zip(quantiles, alphas):
            curve_synth = [SynthModel.get_distributed_value(biomarker, p, cdf=quantile) for p in progress_linspace_synth]
            ax1.plot(progress_linspace_synth, curve_synth, color='b', alpha=alpha)

    #
    # Plot predictor function
    #
    if args.plot_eta is not None and not args.only_densities:
        # Get second axis of plot 1
        ax1b = ax1.twinx()

        # Plot all progresses
        # ax1b.scatter(pm.all_progresses, pm.all_mus, facecolor='b', marker='o', edgecolor='none', alpha=0.2)
        ax1b.text(pm.progresses[-1], pm.sigmas[-1], '$\mu$', color='b', fontsize=11)

        # Plot binned progresses
        ax1b.scatter(pm.progresses, pm.sigmas, color='b', marker='x')

        # Plot interpolated model
        mus = [pm.get_eta(pm.sigmas, p) for p in progress_linspace_int]
        ax1b.plot(progress_linspace_int, mus, color='b')

        if not args.no_extrapolation:
            mus = [pm.get_eta(pm.sigmas, p) for p in progress_linspace_ex1]
            ax1b.plot(progress_linspace_ex1, mus, '--', color='b')
            mus = [pm.get_eta(pm.sigmas, p) for p in progress_linspace_ex2]
            ax1b.plot(progress_linspace_ex2, mus, '--', color='b')
        if args.xlim is not None:
            ax1b.set_xlim(args.xlim[0], args.xlim[1])
        else:
            ax1b.set_xlim(min_progress_extrapolate, max_progress_extrapolate)

    #
    # Plot errors
    #
    if args.plot_errors and not args.only_densities:
        eval_file = model_file.replace('.csv', '_eval_cover.csv')
        if not os.path.isfile(eval_file):
            print log.ERROR, 'Evaluation file not found: {0}'.format(eval_file)
        else:
            m = mlab.csv2rec(eval_file)
            progresses = m['progress']
            errors = m['error']

            # Get second axis of plot 1
            ax1b = ax1.twinx()
            # ax1b.set_ylim(0, max(150, 1.2 * np.max(errors)))
            ax1b.plot(progresses, errors, color='g', marker='x')
            ax1b.text(progresses[-1], errors[-1], 'Discr.', color='g', fontsize=11)
            ax1b.axhline(np.mean(errors), color='g', linestyle='--', alpha=0.5)

            median_curve = pm.get_quantile_curve(progresses, 0.5)
            min_value = np.min(median_curve)
            max_value = np.max(median_curve)
            rect = mpl.patches.Rectangle((progresses[0], min_value), progresses[-1] - progresses[0],
                                         max_value - min_value,
                                         fc=(0.0, 0.5, 0.0, 0.1), ec=(0.0, 0.5, 0.0, 0.8),
                                         linewidth=1)
            ax1.add_patch(rect)

    #
    # Plot points
    #
    if not args.no_points and not args.only_densities:
        samples_file = data_handler.get_samples_file(biomarker)
        if not os.path.isfile(samples_file):
            print log.ERROR, 'Samples file not found: {0}'.format(samples_file)
        else:
            m = mlab.csv2rec(samples_file)
            progr_points = m['progress']
            value_points = m['value']
            # diagn_points = [0.5 if p < 0 else 1.0 for p in progr_points]
            diagn_points = m['diagnosis']
            diagn_points[(0.25 <= diagn_points) & (diagn_points <= 0.75)] = 0.5

            print log.INFO, 'Plotting {0} sample points...'.format(len(progr_points))
            ax1.scatter(progr_points, value_points, s=15.0, c=diagn_points, edgecolor='none',
                        vmin=0.0, vmax=1.0, cmap=pt.progression_cmap, alpha=args.points_alpha)
            if args.phase == 'cnmci':
                rects = [mpl.patches.Rectangle((0, 0), 1, 1, fc=pt.color_cn + (args.points_alpha,), linewidth=0),
                         mpl.patches.Rectangle((0, 0), 1, 1, fc=pt.color_mci + (args.points_alpha,), linewidth=0)]
                labels = ['CN', 'MCI']
            elif args.phase == 'mciad':
                rects = [mpl.patches.Rectangle((0, 0), 1, 1, fc=pt.color_mci + (args.points_alpha,), linewidth=0),
                         mpl.patches.Rectangle((0, 0), 1, 1, fc=pt.color_ad + (args.points_alpha,), linewidth=0)]
                labels = ['MCI', 'AD']
            else:
                rects = [mpl.patches.Rectangle((0, 0), 1, 1, fc=pt.color_cn + (args.points_alpha,), linewidth=0),
                         mpl.patches.Rectangle((0, 0), 1, 1, fc=pt.color_mci + (args.points_alpha,), linewidth=0),
                         mpl.patches.Rectangle((0, 0), 1, 1, fc=pt.color_ad + (args.points_alpha,), linewidth=0)]
                labels = ['CN', 'MCI', 'AD']
            legend = ax1.legend(rects, labels, fontsize=10, ncol=len(rects), loc='upper center', framealpha=0.9)
            legend.get_frame().set_edgecolor((0.6, 0.6, 0.6))

    #
    # Plot PDFs
    #
    progr_samples = [-2000, -1000, 0, 1000, 2000, 3000, 4000] if args.phase == 'joint' else \
                    [-2000, -1500, -1000, -500, 0, 500, 1000, 1500, 2000]

    if args.phase == 'cnmci':
        vmin = -2000
        vmax = 6000
    elif args.phase == 'mciad':
        vmin = -6000
        vmax = 2000
    elif args.phase == 'joint':
        vmin = -2000
        vmax = 4000
    sample_cmap = cmx.ScalarMappable(
        norm=colors.Normalize(vmin=vmin, vmax=vmax),
        cmap=plt.get_cmap(pt.progression_cmap))

    if not args.no_sample_lines and not args.only_densities:
        for progr in progr_samples:
            if not args.no_extrapolation or pm.min_progress < progr < pm.max_progress:
                # sample_color = sample_cmap.to_rgba(progr_samples.index(progr))
                sample_color = sample_cmap.to_rgba(progr)
                linestyle = '--' if progr < pm.min_progress or progr > pm.max_progress else '-'
                ax1.axvline(progr, color=sample_color, linestyle=linestyle, alpha=0.3)

    if not args.no_densities:
        ax2.set_title('Probability density function for {0}'.format(biomarker_string))
        ax2.set_xlabel(DataHandler.get_biomarker_unit(biomarker))
        ax2.set_ylabel('Probability')
        if args.ylim is None:
            values = np.linspace(min_val, max_val, 250)
            ax2.set_xlim(min_val, max_val)
        else:
            values = np.linspace(args.ylim[0], args.ylim[1], 250)
            ax2.set_xlim(args.ylim[0], args.ylim[1])

        for progr in progr_samples:
            if not args.no_extrapolation or pm.min_progress < progr < pm.max_progress:
                # sample_color = sample_cmap.to_rgba(progr_samples.index(progr))
                sample_color = sample_cmap.to_rgba(progr)
                linestyle = '--' if progr < pm.min_progress or progr > pm.max_progress else '-'
                probs = pm.get_density_distribution(values, progr)
                ax2.plot(values, probs, label=str(progr), color=sample_color, linestyle=linestyle)

                if plot_synth_model:
                    probs = [SynthModel.get_probability(biomarker, progr, v) for v in values]
                    ax2.plot(values, probs, color='b', linestyle='--')

        legend = ax2.legend(fontsize=10, loc='best', framealpha=0.9)
        legend.get_frame().set_edgecolor((0.6, 0.6, 0.6))

    #
    # Draw or save the plot
    #
    plt.tight_layout()
    if args.save_plots or args.plot_file is not None:
        if args.plot_file is not None:
            plot_filename = args.plot_file
        else:
            plot_filename = model_file.replace('.csv', '.pdf')
        plt.savefig(plot_filename, transparent=True)
    else:
        plt.show()
    plt.close(fig)
def plot_predictions(biomarker, model, visits, rid_measurements, dpi, dpr,
                     value_model, value_naive, mean_quantile, change,
                     intercept, rid):
    next_visit = get_predicted_visit(visits)
    scantime_first_visit = rid_measurements[visits[0]]['scantime']
    scantime_next_visit = rid_measurements[next_visit]['scantime']
    progress_first_visit = ModelFitter.scantime_to_progress(
        scantime_first_visit, scantime_first_visit, dpi, dpr)
    progress_next_visit = ModelFitter.scantime_to_progress(
        scantime_next_visit, scantime_first_visit, dpi, dpr)
    total_scantime = scantime_next_visit - scantime_first_visit
    progress_linspace = np.linspace(
        progress_first_visit - total_scantime * 0.05,
        progress_next_visit + total_scantime * 0.05, 100)

    fig, ax = plt.subplots()
    pt.setup_axes(plt, ax, xgrid=False, ygrid=False)
    ax.set_title('{0} predictions for RID {1} (DPI={2}, DPR={3})'.format(
        pt.get_biomarker_string(biomarker), rid, dpi, dpr))
    ax.set_xlabel('Disease progress (days before/after conversion to AD)')
    ax.set_ylabel(DataHandler.get_biomarker_unit(biomarker))
    ax.set_xlim(progress_first_visit - total_scantime * 0.1,
                progress_next_visit + total_scantime * 0.1)

    color_mapper = cm.ScalarMappable(cmap=plt.get_cmap(pt.progression_cmap),
                                     norm=colors.Normalize(vmin=0.0, vmax=1.0))

    # Plot the percentile curves of the fitted model
    quantiles = [0.1, 0.25, 0.5, 0.75, 0.9]
    grey_values = ['0.8', '0.6', '0.4', '0.62', '0.84']
    for grey_value, quantile in zip(grey_values, quantiles):
        curve = model.get_quantile_curve(progress_linspace, quantile)
        ax.plot(progress_linspace, curve, zorder=1, color=grey_value)

    # Collect points
    progr_points = []
    value_points = []
    diagn_points = []
    for visit in visits + [next_visit]:
        value_points.append(rid_measurements[visit][biomarker])
        progr_points.append(
            ModelFitter.scantime_to_progress(
                rid_measurements[visit]['scantime'], scantime_first_visit, dpi,
                dpr))
        diagn_points.append(rid_measurements[visit]['DX.scan'])

    # Collect lines
    predict_diagnosis = rid_measurements[next_visit]['DX.scan']
    predict_linspace = np.linspace(progress_first_visit, progress_next_visit,
                                   50)
    curve = [
        model.get_value_at_quantile(p, mean_quantile) for p in predict_linspace
    ]
    line = [
        change *
        ModelFitter.progress_to_scantime(p, scantime_first_visit, dpi, dpr) +
        intercept for p in predict_linspace
    ]

    # Plot model and linear prediction line
    ax.plot(predict_linspace,
            line,
            zorder=1,
            linestyle='--',
            linewidth=2,
            color='k',
            label='naive prediction')
    ax.plot(predict_linspace,
            curve,
            zorder=1,
            linestyle='-',
            linewidth=2,
            color='k',
            label='model-based prediction')
    ax.scatter(progr_points,
               value_points,
               zorder=2,
               s=50.0,
               c=[color_mapper.to_rgba(d) for d in diagn_points],
               edgecolor='none')

    # Plot the predicted values
    ax.scatter([progress_next_visit], [value_naive],
               zorder=2,
               s=50.0,
               c='w',
               edgecolor=color_mapper.to_rgba(predict_diagnosis))
    ax.scatter([progress_next_visit], [value_model],
               zorder=2,
               s=50.0,
               c='w',
               edgecolor=color_mapper.to_rgba(predict_diagnosis))

    plt.tight_layout()
    plt.legend()
    plot_filename = os.path.join(
        '/Users/aschmiri/Desktop/temp',
        'plot_predictions_{0}_{1}.pdf'.format(rid, biomarker))
    plt.savefig(plot_filename, transparent=True)
    # plt.show()
    plt.close(fig)
def plot_biomarker(data_handler, biomarker, measurements, dpi, dpr):
    """
    Plot the model of one biomarker with the fitted values

    :param data_handler: the data handler
    :param biomarker: the biomarker to plot
    :param measurements: the measurements containing the biomarker samples of one subject
    :param dpi: the estimated DPI
    :param dpr: the estimated DPR
    """
    model_file = data_handler.get_model_file(biomarker)
    if not os.path.isfile(model_file):
        print log.ERROR, 'Model file not found: {0}'.format(model_file)
        return

    print log.INFO, 'Generating plot for {0}...'.format(biomarker)

    #
    # Read model
    #
    pm = ProgressionModel(biomarker, model_file)
    progress_extrapolate = 0.3 * (pm.max_progress - pm.min_progress)
    min_progress_extrapolate = int(pm.min_progress - progress_extrapolate)
    max_progress_extrapolate = int(pm.max_progress + progress_extrapolate)
    progress_linspace_ex1 = np.linspace(min_progress_extrapolate, pm.min_progress, 20)
    progress_linspace_int = np.linspace(pm.min_progress, pm.max_progress, 60)
    progress_linspace_ex2 = np.linspace(pm.max_progress, max_progress_extrapolate, 20)

    #
    # Setup plot
    #
    biomarker_string = pt.get_biomarker_string(biomarker)
    figure_width = 6
    fig = plt.figure(figsize=(figure_width, 5))
    ax1 = plt.subplot(1, 1, 1)
    pt.setup_axes(plt, ax1, xgrid=False, ygrid=False)
    ax1.set_title('Model for {0} with fitted sample values'.format(biomarker_string))
    ax1.set_xlabel('Disease progress (days before/after conversion to MCI)')
    ax1.set_ylabel(DataHandler.get_biomarker_unit(biomarker))
    ax1.set_xlim(min_progress_extrapolate, max_progress_extrapolate)

    #
    # Plot the percentile curves of the fitted model
    #
    ax1.axvline(pm.min_progress, color='0.15', linestyle=':')
    ax1.axvline(pm.max_progress, color='0.15', linestyle=':')

    quantiles = [0.1, 0.25, 0.5, 0.75, 0.9]
    grey_values = ['0.4', '0.2', '0', '0.2', '0.4']
    for grey_value, quantile in zip(grey_values, quantiles):
        curve_int = pm.get_quantile_curve(progress_linspace_int, quantile)
        ax1.plot(progress_linspace_int, curve_int, color=grey_value)

        curve_ex1 = pm.get_quantile_curve(progress_linspace_ex1, quantile)
        curve_ex2 = pm.get_quantile_curve(progress_linspace_ex2, quantile)
        ax1.plot(progress_linspace_ex1, curve_ex1, '--', color=grey_value)
        ax1.plot(progress_linspace_ex2, curve_ex2, '--', color=grey_value)

        label = 'q = {0}'.format(quantile * 100)
        ax1.text(progress_linspace_int[-1] + 100, curve_int[-1], label, fontsize=10)

    #
    # Plot points
    #
    progr_points = []
    value_points = []
    diagn_points = []
    for visit in measurements[0]:
        if biomarker in measurements[0][visit]:
            progress = measurements[0][visit]['scantime'] * dpr + dpi
            value = measurements[0][visit][biomarker]
            progr_points.append(progress)
            value_points.append(value)
            diagn_points.append(1.0)
            ax1.axvline(progress, color='b', linestyle='--')
            ax1.text(progress + 150, value, visit, color='b', fontsize=10)

    ax1.scatter(progr_points, value_points, s=25.0, color='b', edgecolor='none',
                vmin=0.0, vmax=1.0, alpha=0.9)

    #
    # Draw or save the plot
    #
    plt.tight_layout()
    plt.show()
    plt.close(fig)
Exemple #19
0
def plot_model(args, data_handler, biomarker):
    model_file = data_handler.get_model_file(biomarker)
    if not os.path.isfile(model_file):
        print log.ERROR, 'Model file not found: {0}'.format(model_file)
        return

    print log.INFO, 'Generating plot for {0}...'.format(biomarker)
    plot_synth_model = args.plot_synth_model and biomarker in SynthModel.get_biomarker_names(
    )

    #
    # Read model
    #
    pm = ProgressionModel(biomarker,
                          model_file,
                          extrapolator=args.extrapolator)
    progress_extrapolate = 0.3 * (pm.max_progress - pm.min_progress)
    min_progress_extrapolate = int(pm.min_progress - progress_extrapolate)
    max_progress_extrapolate = int(pm.max_progress + progress_extrapolate)
    progress_linspace_ex1 = np.linspace(min_progress_extrapolate,
                                        pm.min_progress, 20)
    progress_linspace_int = np.linspace(pm.min_progress, pm.max_progress, 60)
    progress_linspace_ex2 = np.linspace(pm.max_progress,
                                        max_progress_extrapolate, 20)

    # Calc min and max val in interval between 1% and 99% percentie
    min_val, max_val = pm.get_value_range([0.1, 0.9])
    #     progress_linspace = np.linspace(min_progress_extrapolate, max_progress_extrapolate, 100)
    #     min_val = float('inf')
    #     max_val = float('-inf')
    #     for quantile in [0.1, 0.9]:
    #         curve = pm.get_quantile_curve(progress_linspace, quantile)
    #         min_val = min(min_val, np.min(curve))
    #         max_val = max(max_val, np.max(curve))

    #
    # Setup plot
    #
    biomarker_string = pt.get_biomarker_string(biomarker)
    figure_width = 6 if args.no_densities or args.only_densities else 12
    fig = plt.figure(figsize=(figure_width, 5))
    if args.only_densities:
        ax1 = None
        ax2 = plt.subplot(1, 1, 1)
        pt.setup_axes(plt, ax2, xgrid=False, ygrid=False)
    elif args.no_densities:
        ax1 = plt.subplot(1, 1, 1)
        ax2 = None
        pt.setup_axes(plt, ax1, xgrid=False, ygrid=False)
    else:
        ax1 = plt.subplot(1, 2, 1)
        ax2 = plt.subplot(1, 2, 2)
        pt.setup_axes(plt, ax1, xgrid=False, ygrid=False)
        pt.setup_axes(plt, ax2)

    if not args.only_densities:
        if args.no_model and not args.plot_synth_model:
            ax1.set_title('Aligned samples for {0}'.format(biomarker_string))
        else:
            ax1.set_title('Quantile curves for {0}'.format(biomarker_string))
        if args.phase == 'mciad':
            ax1.set_xlabel(
                'Disease progress (days before/after conversion to AD)')
        else:
            ax1.set_xlabel(
                'Disease progress (days before/after conversion to MCI)')
        ax1.set_ylabel(DataHandler.get_biomarker_unit(biomarker))
        if args.xlim is not None:
            ax1.set_xlim(args.xlim[0], args.xlim[1])
        else:
            ax1.set_xlim(min_progress_extrapolate, max_progress_extrapolate)
        if args.ylim is not None:
            ax1.set_ylim(args.ylim[0], args.ylim[1])

    #
    # Plot the percentile curves of the fitted model
    #
    if not args.no_model and not args.only_densities:
        ax1.axvline(pm.min_progress, color='0.15', linestyle=':')
        ax1.axvline(pm.max_progress, color='0.15', linestyle=':')

        quantiles = [0.1, 0.25, 0.5, 0.75, 0.9]
        grey_values = ['0.4', '0.2', '0', '0.2', '0.4']
        for grey_value, quantile in zip(grey_values, quantiles):
            curve_int = pm.get_quantile_curve(progress_linspace_int, quantile)
            ax1.plot(progress_linspace_int, curve_int, color=grey_value)

            if not args.no_extrapolation:
                curve_ex1 = pm.get_quantile_curve(progress_linspace_ex1,
                                                  quantile)
                curve_ex2 = pm.get_quantile_curve(progress_linspace_ex2,
                                                  quantile)
                ax1.plot(progress_linspace_ex1,
                         curve_ex1,
                         '--',
                         color=grey_value)
                ax1.plot(progress_linspace_ex2,
                         curve_ex2,
                         '--',
                         color=grey_value)

            if args.plot_quantile_label:
                label = '$q={0}\%$'.format(quantile * 100)
                ax1.text(progress_linspace_int[-1] + 10,
                         curve_int[-1],
                         label,
                         fontsize=10)

        if args.plot_donohue:
            print 'Plotting Donohue'
            donohue_file = os.path.join(
                data_handler._conf.models_folder, 'donohue',
                'population_{0}.csv'.format(biomarker.replace(' ', '.')))
            if not os.path.isfile(donohue_file):
                print log.ERROR, 'Donohue model file not found: {0}'.format(
                    donohue_file)
                return

            r = mlab.csv2rec(donohue_file)
            if args.method == 'joint':
                offset = 2200
            else:
                offset = 300
            progrs = r[r.dtype.names[0]] * 30.44 + offset
            vals = r[r.dtype.names[1]]
            curve_donohue = []
            progr_donohue = []
            for p in progress_linspace_int:
                if progrs[0] < p < progrs[-1]:
                    i = 1
                    while p > progrs[i]:
                        i += 1
                    # TODO linear interpolation
                    progr_donohue.append(progrs[i])
                    curve_donohue.append(vals[i])
            ax1.plot(progr_donohue,
                     curve_donohue,
                     '--',
                     color='b',
                     linewidth=2)

    #
    # Plot synthetic model curve
    #
    if plot_synth_model:
        progress_linspace_synth = np.linspace(-2500, 2500, 100)
        quantiles = [0.1, 0.25, 0.5, 0.75, 0.9]
        alphas = [0.4, 0.7, 1.0, 0.7, 0.4]
        for quantile, alpha in zip(quantiles, alphas):
            curve_synth = [
                SynthModel.get_distributed_value(biomarker, p, cdf=quantile)
                for p in progress_linspace_synth
            ]
            ax1.plot(progress_linspace_synth,
                     curve_synth,
                     color='b',
                     alpha=alpha)

    #
    # Plot predictor function
    #
    if args.plot_eta is not None and not args.only_densities:
        # Get second axis of plot 1
        ax1b = ax1.twinx()

        # Plot all progresses
        # ax1b.scatter(pm.all_progresses, pm.all_mus, facecolor='b', marker='o', edgecolor='none', alpha=0.2)
        ax1b.text(pm.progresses[-1],
                  pm.sigmas[-1],
                  '$\mu$',
                  color='b',
                  fontsize=11)

        # Plot binned progresses
        ax1b.scatter(pm.progresses, pm.sigmas, color='b', marker='x')

        # Plot interpolated model
        mus = [pm.get_eta(pm.sigmas, p) for p in progress_linspace_int]
        ax1b.plot(progress_linspace_int, mus, color='b')

        if not args.no_extrapolation:
            mus = [pm.get_eta(pm.sigmas, p) for p in progress_linspace_ex1]
            ax1b.plot(progress_linspace_ex1, mus, '--', color='b')
            mus = [pm.get_eta(pm.sigmas, p) for p in progress_linspace_ex2]
            ax1b.plot(progress_linspace_ex2, mus, '--', color='b')
        if args.xlim is not None:
            ax1b.set_xlim(args.xlim[0], args.xlim[1])
        else:
            ax1b.set_xlim(min_progress_extrapolate, max_progress_extrapolate)

    #
    # Plot errors
    #
    if args.plot_errors and not args.only_densities:
        eval_file = model_file.replace('.csv', '_eval_cover.csv')
        if not os.path.isfile(eval_file):
            print log.ERROR, 'Evaluation file not found: {0}'.format(eval_file)
        else:
            m = mlab.csv2rec(eval_file)
            progresses = m['progress']
            errors = m['error']

            # Get second axis of plot 1
            ax1b = ax1.twinx()
            # ax1b.set_ylim(0, max(150, 1.2 * np.max(errors)))
            ax1b.plot(progresses, errors, color='g', marker='x')
            ax1b.text(progresses[-1],
                      errors[-1],
                      'Discr.',
                      color='g',
                      fontsize=11)
            ax1b.axhline(np.mean(errors), color='g', linestyle='--', alpha=0.5)

            median_curve = pm.get_quantile_curve(progresses, 0.5)
            min_value = np.min(median_curve)
            max_value = np.max(median_curve)
            rect = mpl.patches.Rectangle((progresses[0], min_value),
                                         progresses[-1] - progresses[0],
                                         max_value - min_value,
                                         fc=(0.0, 0.5, 0.0, 0.1),
                                         ec=(0.0, 0.5, 0.0, 0.8),
                                         linewidth=1)
            ax1.add_patch(rect)

    #
    # Plot points
    #
    if not args.no_points and not args.only_densities:
        samples_file = data_handler.get_samples_file(biomarker)
        if not os.path.isfile(samples_file):
            print log.ERROR, 'Samples file not found: {0}'.format(samples_file)
        else:
            m = mlab.csv2rec(samples_file)
            progr_points = m['progress']
            value_points = m['value']
            # diagn_points = [0.5 if p < 0 else 1.0 for p in progr_points]
            diagn_points = m['diagnosis']
            diagn_points[(0.25 <= diagn_points) & (diagn_points <= 0.75)] = 0.5

            print log.INFO, 'Plotting {0} sample points...'.format(
                len(progr_points))
            ax1.scatter(progr_points,
                        value_points,
                        s=15.0,
                        c=diagn_points,
                        edgecolor='none',
                        vmin=0.0,
                        vmax=1.0,
                        cmap=pt.progression_cmap,
                        alpha=args.points_alpha)
            if args.phase == 'cnmci':
                rects = [
                    mpl.patches.Rectangle(
                        (0, 0),
                        1,
                        1,
                        fc=pt.color_cn + (args.points_alpha, ),
                        linewidth=0),
                    mpl.patches.Rectangle(
                        (0, 0),
                        1,
                        1,
                        fc=pt.color_mci + (args.points_alpha, ),
                        linewidth=0)
                ]
                labels = ['CN', 'MCI']
            elif args.phase == 'mciad':
                rects = [
                    mpl.patches.Rectangle(
                        (0, 0),
                        1,
                        1,
                        fc=pt.color_mci + (args.points_alpha, ),
                        linewidth=0),
                    mpl.patches.Rectangle(
                        (0, 0),
                        1,
                        1,
                        fc=pt.color_ad + (args.points_alpha, ),
                        linewidth=0)
                ]
                labels = ['MCI', 'AD']
            else:
                rects = [
                    mpl.patches.Rectangle(
                        (0, 0),
                        1,
                        1,
                        fc=pt.color_cn + (args.points_alpha, ),
                        linewidth=0),
                    mpl.patches.Rectangle(
                        (0, 0),
                        1,
                        1,
                        fc=pt.color_mci + (args.points_alpha, ),
                        linewidth=0),
                    mpl.patches.Rectangle(
                        (0, 0),
                        1,
                        1,
                        fc=pt.color_ad + (args.points_alpha, ),
                        linewidth=0)
                ]
                labels = ['CN', 'MCI', 'AD']
            legend = ax1.legend(rects,
                                labels,
                                fontsize=10,
                                ncol=len(rects),
                                loc='upper center',
                                framealpha=0.9)
            legend.get_frame().set_edgecolor((0.6, 0.6, 0.6))

    #
    # Plot PDFs
    #
    progr_samples = [-2000, -1000, 0, 1000, 2000, 3000, 4000] if args.phase == 'joint' else \
                    [-2000, -1500, -1000, -500, 0, 500, 1000, 1500, 2000]

    if args.phase == 'cnmci':
        vmin = -2000
        vmax = 6000
    elif args.phase == 'mciad':
        vmin = -6000
        vmax = 2000
    elif args.phase == 'joint':
        vmin = -2000
        vmax = 4000
    sample_cmap = cmx.ScalarMappable(norm=colors.Normalize(vmin=vmin,
                                                           vmax=vmax),
                                     cmap=plt.get_cmap(pt.progression_cmap))

    if not args.no_sample_lines and not args.only_densities:
        for progr in progr_samples:
            if not args.no_extrapolation or pm.min_progress < progr < pm.max_progress:
                # sample_color = sample_cmap.to_rgba(progr_samples.index(progr))
                sample_color = sample_cmap.to_rgba(progr)
                linestyle = '--' if progr < pm.min_progress or progr > pm.max_progress else '-'
                ax1.axvline(progr,
                            color=sample_color,
                            linestyle=linestyle,
                            alpha=0.3)

    if not args.no_densities:
        ax2.set_title(
            'Probability density function for {0}'.format(biomarker_string))
        ax2.set_xlabel(DataHandler.get_biomarker_unit(biomarker))
        ax2.set_ylabel('Probability')
        if args.ylim is None:
            values = np.linspace(min_val, max_val, 250)
            ax2.set_xlim(min_val, max_val)
        else:
            values = np.linspace(args.ylim[0], args.ylim[1], 250)
            ax2.set_xlim(args.ylim[0], args.ylim[1])

        for progr in progr_samples:
            if not args.no_extrapolation or pm.min_progress < progr < pm.max_progress:
                # sample_color = sample_cmap.to_rgba(progr_samples.index(progr))
                sample_color = sample_cmap.to_rgba(progr)
                linestyle = '--' if progr < pm.min_progress or progr > pm.max_progress else '-'
                probs = pm.get_density_distribution(values, progr)
                ax2.plot(values,
                         probs,
                         label=str(progr),
                         color=sample_color,
                         linestyle=linestyle)

                if plot_synth_model:
                    probs = [
                        SynthModel.get_probability(biomarker, progr, v)
                        for v in values
                    ]
                    ax2.plot(values, probs, color='b', linestyle='--')

        legend = ax2.legend(fontsize=10, loc='best', framealpha=0.9)
        legend.get_frame().set_edgecolor((0.6, 0.6, 0.6))

    #
    # Draw or save the plot
    #
    plt.tight_layout()
    if args.save_plots or args.plot_file is not None:
        if args.plot_file is not None:
            plot_filename = args.plot_file
        else:
            plot_filename = model_file.replace('.csv', '.pdf')
        plt.savefig(plot_filename, transparent=True)
    else:
        plt.show()
    plt.close(fig)
def plot_boxplots_noisy_data(args, biomarkers, errors, sigmas, noise_on_rate=True):
    print log.INFO, 'Plotting error bars...'

    fig, ax = plt.subplots(figsize=(8, 5))
    pt.setup_axes(plt, ax, xgrid=False)

    biomarker_strings = {'synth_hipp': '$\mathcal{M}^{HV}$',
                         'synth_mmse': '$\mathcal{M}^{MMSE}$',
                         'synth_cdrsb': '$\mathcal{M}^{CDR-SB}$'}
    ylabels = {'area': 'Mean area between PDFs',
               'peakdist': 'Distance between peaks',
               'maxdist': 'Distance between progress maxima'}

    if noise_on_rate:
        ax.set_title('Influence of variations in progression rate')
    else:
        ax.set_title('Influence of variations in point of conversion')
    ax.set_ylabel(ylabels[args.metric])
    plt.tick_params(labelbottom='off')

    # Collect data
    data = []
    medians = []
    for biomarker in biomarkers:
        for sigma in sigmas:
            data.append(errors[biomarker][sigma])
            medians.append(np.mean(errors[biomarker][sigma]))

    # Set limits
    max_data = np.max(data)
    ax.set_ylim(0, 1.15 * max_data)

    # Draw boxplot
    boxplot = plt.boxplot(data, patch_artist=True)

    # Set boxplot colours
    colours = [(0.0, 0.0, 0.0), (0.0, 0.0, 0.5), (0.0, 0.5, 0.0)] * len(biomarkers)
    for i in range(len(data)):
        pt.set_boxplot_color(boxplot, i, colours[i])

    # Write median errors as text
    upper_labels = [str(np.round(m, 3)) for m in medians]
    for i in np.arange(len(upper_labels)):
        ax.text(i + 1, 1.02 * max_data, upper_labels[i],
                horizontalalignment='center', size=10, color=colours[i])

    # Write category labels
    for i, biomarker in enumerate(biomarkers):
        plt.text((i + 0.5) * len(sigmas) + 0.5, -0.07 * max_data,
                 biomarker_strings[biomarker],
                 horizontalalignment='center', size=15)

    # Draw horizontal lines
    for x in range(3, len(data), 3):
        plt.axvline(x + 0.5, color='k', alpha=0.4)

    # Plot legend
    legend = ax.legend([mpl.patches.Rectangle((0, 0), 1, 1, fc=(0.0, 0.0, 0.0, 0.2), ec=(0.0, 0.0, 0.0, 1.0), linewidth=1),
                        mpl.patches.Rectangle((0, 0), 1, 1, fc=(0.0, 0.0, 0.5, 0.2), ec=(0.0, 0.0, 0.5, 1.0), linewidth=1),
                        mpl.patches.Rectangle((0, 0), 1, 1, fc=(0.0, 0.5, 0.0, 0.2), ec=(0.0, 0.5, 0.0, 1.0), linewidth=1)],
                       ['$\sigma={0}$'.format(s) for s in sigmas], fontsize=10, ncol=3, loc='upper center', framealpha=0.9)
    legend.get_frame().set_edgecolor((0.6, 0.6, 0.6))

    # Show or save plot
    plt.tight_layout()
    if args.plot_file is not None:
        plt.savefig(args.plot_file, transparent=True)
    else:
        plt.show()
    plt.close(fig)
def plot_errors_data(args, errors, biomarker_sets, viscode_sets):
    print log.INFO, 'Plotting results...'
    num_viscode_sets = len(viscode_sets)
    num_tests = len(biomarker_sets)

    # Define figure and grid
    fig, ax = plt.subplots(figsize=(8, 5))
    pt.setup_axes(plt, ax, xgrid=False)

    ax.set_title('DP estimation using different biomarker settings')
    ax.set_ylabel('Mean progress estimation error')
    plt.tick_params(labelbottom='off')
    biomarker_strings = {'synth_hipp': '$\mathcal{M}^{HV}$',
                         'synth_mmse': '$\mathcal{M}^{MMSE}$',
                         'synth_cdrsb': '$\mathcal{M}^{CDR-SB}$'}

    # Collect data
    data = []
    medians = []
    for viscodes in viscode_sets:
        viscodes_string = '_'.join([str(v) for v in viscodes])
        for biomarkers in biomarker_sets:
            biomarkers_string = '_'.join(biomarkers)
            data.append(errors[biomarkers_string][viscodes_string])
            medians.append(np.median(errors[biomarkers_string][viscodes_string]))

    # Set limits
    max_data = np.max(data)
    ax.set_ylim(0, 1.15 * max_data)

    # Draw boxplot
    boxplot = plt.boxplot(data, patch_artist=True)

    # Set boxplot colours
    colours = [(0.0, 0.0, 0.0), (0.0, 0.0, 0.5), (0.0, 0.5, 0.0), (0.5, 0.0, 0.5)] * num_viscode_sets
    for i in range(len(data)):
        pt.set_boxplot_color(boxplot, i, colours[i])

    # Write median errors as text
    upper_labels = [str(np.round(s, 2)) for s in medians]
    for i in range(len(data)):
        ax.text(i + 1, 1.01 * max_data, upper_labels[i],
                horizontalalignment='center', size=10, color=colours[i])

    # Write category labels
    for i in xrange(len(viscode_sets)):
        ax.text(i * num_tests + 0.5 * (num_tests + 1), -70,
                '{0} timepoint{1}'.format(len(viscode_sets[i]), '' if len(viscode_sets[i]) == 1 else 's'),
                horizontalalignment='center')

    # Plot legend
    legend = ax.legend([mpl.patches.Rectangle((0, 0), 1, 1, fc=(0.0, 0.0, 0.0, 0.2), ec=(0.0, 0.0, 0.0, 1.0), linewidth=1),
                        mpl.patches.Rectangle((0, 0), 1, 1, fc=(0.0, 0.0, 0.5, 0.2), ec=(0.0, 0.0, 0.5, 1.0), linewidth=1),
                        mpl.patches.Rectangle((0, 0), 1, 1, fc=(0.0, 0.5, 0.0, 0.2), ec=(0.0, 0.5, 0.0, 1.0), linewidth=1),
                        mpl.patches.Rectangle((0, 0), 1, 1, fc=(0.5, 0.0, 0.5, 0.2), ec=(0.5, 0.0, 0.5, 1.0), linewidth=1)],
                       [biomarker_strings[b] for b in biomarker_sets[-1]] + ['All'], fontsize=10, ncol=4, loc='upper center', framealpha=0.9)
    legend.get_frame().set_edgecolor((0.6, 0.6, 0.6))

    # Draw horizontal lines
    for i in xrange(1, len(viscode_sets)):
        ax.axvline(i * num_tests + 0.5, linestyle='-', color='k', alpha=0.4)

    # Show or save plot
    plt.tight_layout()
    if args.plot_file is not None:
        plt.savefig(args.plot_file, transparent=True)
    else:
        plt.show()
    plt.close(fig)
def plot_predictions(biomarker, model, visits, rid_measurements, dpi, dpr,
                     value_model, value_naive, mean_quantile, change, intercept, rid):
    next_visit = get_predicted_visit(visits)
    scantime_first_visit = rid_measurements[visits[0]]['scantime']
    scantime_next_visit = rid_measurements[next_visit]['scantime']
    progress_first_visit = ModelFitter.scantime_to_progress(scantime_first_visit, scantime_first_visit, dpi, dpr)
    progress_next_visit = ModelFitter.scantime_to_progress(scantime_next_visit, scantime_first_visit, dpi, dpr)
    total_scantime  = scantime_next_visit - scantime_first_visit
    progress_linspace = np.linspace(progress_first_visit - total_scantime * 0.05,
                                    progress_next_visit + total_scantime * 0.05, 100)

    fig, ax = plt.subplots()
    pt.setup_axes(plt, ax, xgrid=False, ygrid=False)
    ax.set_title('{0} predictions for RID {1} (DPI={2}, DPR={3})'.format(pt.get_biomarker_string(biomarker), rid, dpi, dpr))
    ax.set_xlabel('Disease progress (days before/after conversion to AD)')
    ax.set_ylabel(DataHandler.get_biomarker_unit(biomarker))
    ax.set_xlim(progress_first_visit - total_scantime * 0.1, progress_next_visit + total_scantime * 0.1)

    color_mapper = cm.ScalarMappable(cmap=plt.get_cmap(pt.progression_cmap),
                                     norm=colors.Normalize(vmin=0.0, vmax=1.0))

    # Plot the percentile curves of the fitted model
    quantiles = [0.1, 0.25, 0.5, 0.75, 0.9]
    grey_values = ['0.8', '0.6', '0.4', '0.62', '0.84']
    for grey_value, quantile in zip(grey_values, quantiles):
        curve = model.get_quantile_curve(progress_linspace, quantile)
        ax.plot(progress_linspace, curve, zorder=1, color=grey_value)

    # Collect points
    progr_points = []
    value_points = []
    diagn_points = []
    for visit in visits + [next_visit]:
        value_points.append(rid_measurements[visit][biomarker])
        progr_points.append(ModelFitter.scantime_to_progress(rid_measurements[visit]['scantime'],
                                                             scantime_first_visit, dpi, dpr))
        diagn_points.append(rid_measurements[visit]['DX.scan'])

    # Collect lines
    predict_diagnosis = rid_measurements[next_visit]['DX.scan']
    predict_linspace = np.linspace(progress_first_visit, progress_next_visit, 50)
    curve = [model.get_value_at_quantile(p, mean_quantile) for p in predict_linspace]
    line = [change * ModelFitter.progress_to_scantime(p, scantime_first_visit, dpi, dpr) + intercept for p in predict_linspace]

    # Plot model and linear prediction line
    ax.plot(predict_linspace, line, zorder=1, linestyle='--', linewidth=2, color='k',
            label='naive prediction')
    ax.plot(predict_linspace, curve, zorder=1, linestyle='-', linewidth=2, color='k',
            label='model-based prediction')
    ax.scatter(progr_points, value_points, zorder=2, s=50.0,
               c=[color_mapper.to_rgba(d) for d in diagn_points], edgecolor='none')

    # Plot the predicted values
    ax.scatter([progress_next_visit], [value_naive], zorder=2, s=50.0, c='w',
               edgecolor=color_mapper.to_rgba(predict_diagnosis))
    ax.scatter([progress_next_visit], [value_model], zorder=2, s=50.0, c='w',
               edgecolor=color_mapper.to_rgba(predict_diagnosis))

    plt.tight_layout()
    plt.legend()
    plot_filename = os.path.join('/Users/aschmiri/Desktop/temp',
                                 'plot_predictions_{0}_{1}.pdf'.format(rid, biomarker))
    plt.savefig(plot_filename, transparent=True)
    # plt.show()
    plt.close(fig)
def plot_errors(args, values, methods):
    fig, ax = plt.subplots()
    pt.setup_axes(plt, ax, xgrid=False)

    num_methods = len(methods)
    for i in xrange(num_methods):
        ax.axvline(i * 2 + 1.5, linestyle='-', color='k', alpha=0.4)

    ax.set_axisbelow(True)
    ax.set_title('Prediction of {0}'.format(args.predict_biomarker))
    ax.set_ylabel('Prediction error')
    ax.set_xticklabels([])

    # Append plot for naive estimation
    values_observed = np.array(values[values.keys()[0]]['observed'])
    values_naive = np.array(values[values.keys()[0]]['naive'])
    errors_naive = np.abs(values_observed - values_naive)
    data = [errors_naive]
    medians = [np.median(errors_naive)]
    weights = ['normal']
    for method in methods:
        values_model1 = np.array(values[method]['model_dpi'])
        values_model2 = np.array(values[method]['model_dpi_dpr'])
        errors_model1 = np.abs(values_observed - values_model1)
        errors_model2 = np.abs(values_observed - values_model2)

        data.append(errors_model1)
        data.append(errors_model2)

        medians.append(np.median(errors_model1))
        medians.append(np.median(errors_model2))

        weights.append('bold' if stats.ttest_rel(errors_naive, errors_model1)[1] < 0.01 else 'normal')
        weights.append('bold' if stats.ttest_rel(errors_naive, errors_model2)[1] < 0.01 else 'normal')

    # Set colors
    colors = [(0.0, 0.0, 0.0)]
    for i in range(num_methods):
        colors.append((0.0, 0.0, 0.5))
        colors.append((0.0, 0.5, 0.0))

    # Set limits
    max_data = np.max(data)
    ax.set_ylim(-0.01 * max_data, 1.01 * max_data)

    # Add labels to x axis
    for i, method in enumerate(methods):
        ax.text(i * 2 + 2.5, -0.04 * max_data, method, horizontalalignment='center')

    # Write median errors as text
    pos = np.arange(1, 2 * num_methods + 2)
    upper_labels = [str(np.round(m, 2)) for m in medians]
    for i in range(2 * num_methods + 1):
        ax.text(pos[i], 0.91 * max_data, upper_labels[i], weight=weights[i],
                horizontalalignment='center', size=10, color=colors[i])

    # Plot boxplots
    boxplot = plt.boxplot(data, patch_artist=True)
    for i in range(len(boxplot['boxes'])):
        pt.set_boxplot_color(boxplot, i, colors[i])

    # Plot legend
    legend = ax.legend([mpl.patches.Rectangle((0, 0), 1, 1, fc=(0.0, 0.0, 0.0, 0.2), ec=(0.0, 0.0, 0.0, 1.0), linewidth=1),
                        mpl.patches.Rectangle((0, 0), 1, 1, fc=(0.0, 0.0, 0.5, 0.2), ec=(0.0, 0.0, 0.5, 1.0), linewidth=1),
                        mpl.patches.Rectangle((0, 0), 1, 1, fc=(0.0, 0.5, 0.0, 0.2), ec=(0.0, 0.5, 0.0, 1.0), linewidth=1)],
                       ['Naive', 'DPI', 'DPI + DPR'], fontsize=10, ncol=3, loc='upper center', framealpha=0.9)
    legend.get_frame().set_edgecolor((0.6, 0.6, 0.6))

    # Show or save plot
    plt.tight_layout()
    if args.plot_file is not None:
        plt.savefig(args.plot_file, transparent=True)
    else:
        plt.show()
    plt.close(fig)
Exemple #24
0
def plot_boxplots_noisy_data(args,
                             biomarkers,
                             errors,
                             sigmas,
                             noise_on_rate=True):
    print log.INFO, 'Plotting error bars...'

    fig, ax = plt.subplots(figsize=(8, 5))
    pt.setup_axes(plt, ax, xgrid=False)

    biomarker_strings = {
        'synth_hipp': '$\mathcal{M}^{HV}$',
        'synth_mmse': '$\mathcal{M}^{MMSE}$',
        'synth_cdrsb': '$\mathcal{M}^{CDR-SB}$'
    }
    ylabels = {
        'area': 'Mean area between PDFs',
        'peakdist': 'Distance between peaks',
        'maxdist': 'Distance between progress maxima'
    }

    if noise_on_rate:
        ax.set_title('Influence of variations in progression rate')
    else:
        ax.set_title('Influence of variations in point of conversion')
    ax.set_ylabel(ylabels[args.metric])
    plt.tick_params(labelbottom='off')

    # Collect data
    data = []
    medians = []
    for biomarker in biomarkers:
        for sigma in sigmas:
            data.append(errors[biomarker][sigma])
            medians.append(np.mean(errors[biomarker][sigma]))

    # Set limits
    max_data = np.max(data)
    ax.set_ylim(0, 1.15 * max_data)

    # Draw boxplot
    boxplot = plt.boxplot(data, patch_artist=True)

    # Set boxplot colours
    colours = [(0.0, 0.0, 0.0), (0.0, 0.0, 0.5),
               (0.0, 0.5, 0.0)] * len(biomarkers)
    for i in range(len(data)):
        pt.set_boxplot_color(boxplot, i, colours[i])

    # Write median errors as text
    upper_labels = [str(np.round(m, 3)) for m in medians]
    for i in np.arange(len(upper_labels)):
        ax.text(i + 1,
                1.02 * max_data,
                upper_labels[i],
                horizontalalignment='center',
                size=10,
                color=colours[i])

    # Write category labels
    for i, biomarker in enumerate(biomarkers):
        plt.text((i + 0.5) * len(sigmas) + 0.5,
                 -0.07 * max_data,
                 biomarker_strings[biomarker],
                 horizontalalignment='center',
                 size=15)

    # Draw horizontal lines
    for x in range(3, len(data), 3):
        plt.axvline(x + 0.5, color='k', alpha=0.4)

    # Plot legend
    legend = ax.legend([
        mpl.patches.Rectangle((0, 0),
                              1,
                              1,
                              fc=(0.0, 0.0, 0.0, 0.2),
                              ec=(0.0, 0.0, 0.0, 1.0),
                              linewidth=1),
        mpl.patches.Rectangle((0, 0),
                              1,
                              1,
                              fc=(0.0, 0.0, 0.5, 0.2),
                              ec=(0.0, 0.0, 0.5, 1.0),
                              linewidth=1),
        mpl.patches.Rectangle((0, 0),
                              1,
                              1,
                              fc=(0.0, 0.5, 0.0, 0.2),
                              ec=(0.0, 0.5, 0.0, 1.0),
                              linewidth=1)
    ], ['$\sigma={0}$'.format(s) for s in sigmas],
                       fontsize=10,
                       ncol=3,
                       loc='upper center',
                       framealpha=0.9)
    legend.get_frame().set_edgecolor((0.6, 0.6, 0.6))

    # Show or save plot
    plt.tight_layout()
    if args.plot_file is not None:
        plt.savefig(args.plot_file, transparent=True)
    else:
        plt.show()
    plt.close(fig)