def auc_xscaled(xs, ys):
    """AUC score scaled to fill x interval
    """
    xmin, xmax = minmaxr(xs)
    denom = float(xmax - xmin)
    xs_corr = [(x - xmin) / denom for x in xs]
    return auc(xs_corr, ys)
    def compare(self, others, scores, dtype=np.float16, plot=False):
        result0 = self.compute(scores, dtype=dtype)

        if not isiterable(others):
            others = [others]

        result_grid = []
        for other in others:
            result1 = other.compute(scores, dtype=dtype)

            if plot:
                from matplotlib import pyplot as plt
                from palettable import colorbrewer
                colors = colorbrewer.get_map('Set1', 'qualitative',
                                             9).mpl_colors

            result_row = {}
            for score_name, scores0 in result0.iteritems():
                scores1 = result1[score_name]
                auc_score = dist_auc(scores0, scores1)
                result_row[score_name] = auc_score
                if plot:
                    scores0p = [x for x in scores0 if not np.isnan(x)]
                    scores1p = [x for x in scores1 if not np.isnan(x)]
                    hmin0, hmax0 = minmaxr(scores0p)
                    hmin1, hmax1 = minmaxr(scores1p)
                    bins = np.linspace(min(hmin0, hmin1), max(hmax0, hmax1),
                                       50)
                    plt.hist(scores0p,
                             bins,
                             alpha=0.5,
                             label='0',
                             color=colors[0],
                             edgecolor="none")
                    plt.hist(scores1p,
                             bins,
                             alpha=0.5,
                             label='1',
                             color=colors[1],
                             edgecolor="none")
                    plt.legend(loc='upper right')
                    plt.title("%s: AUC=%.4f" % (score_name, auc_score))
                    plt.show()
            result_grid.append(result_row)
        return result_grid
Exemple #3
0
 def test_minmaxr_1(self):
     arr = [2, 3, 4]
     amin, amax = minmaxr(arr)
     self.assertAlmostEqual(2.0, amin)
     self.assertAlmostEqual(4.0, amax)
Exemple #4
0
 def test_minmaxr_2(self):
     arr = []
     amin, amax = minmaxr(arr)
     self.assertTrue(np.isinf(amin))
     self.assertTrue(np.isinf(amin))
     self.assertGreater(amin, amax)
def create_plots(args, df):
    import jinja2
    import matplotlib.pyplot as plt
    from palettable import colorbrewer
    from matplotlib.font_manager import FontProperties

    fontP = FontProperties()
    fontP.set_size('xx-small')

    #groups = df.set_index(args.x_axis).groupby([args.group_by])
    groups = df.groupby([args.group_by])
    metrics = list(set(args.metrics) & set(df.keys()))
    colors = take(
        len(metrics),
        cycle(
            chain(
                colorbrewer.qualitative.Dark2_8.mpl_colors,
                colorbrewer.qualitative.Set2_8.mpl_colors,
            )))

    template_loader = jinja2.FileSystemLoader(os.path.join(args.output, '..'))
    template_env = jinja2.Environment(loader=template_loader)
    template_interactive = template_env.get_template(
        'template_fig_interactive.html')
    template_static = template_env.get_template('template_fig_static.html')

    table_interactive = []
    table_static = []

    for group_name, group in groups:

        # always sort by X values
        group = group.sort([args.x_axis])

        if args.fig_title is None:
            fig_title = '%s=%s' % (args.group_by, group_name)
        else:
            fig_title = args.fig_title

        # compute AUC scores
        ys = []
        for metric, color in zip(metrics, colors):
            series = group[metric]
            score = auc_xscaled(group[args.x_axis].values, series.values)
            label = "%s (%.4f)" % (metric, score)
            ys.append((score, metric, label, color))
        ys.sort(reverse=True)

        lbls_old, lbls_new, colors = zip(*ys)[1:4]
        group = group[[args.x_axis] + list(lbls_old)] \
            .set_index(args.x_axis) \
            .rename(columns=dict(zip(lbls_old, lbls_new)))

        # create plots
        fig, ax = plt.subplots()
        group.plot(ax=ax, title=fig_title, color=list(colors))
        ax.set_xlim(*minmaxr(group.index.values))
        ax.set_ylim(0.4, 1.0)
        ax.legend(loc=args.legend_loc, prop=fontP)
        fig_name = 'fig-%s.%s' % (group_name, args.fig_format)
        fig_path = os.path.join(args.output, fig_name)
        csv_name = 'fig-%s.csv' % group_name
        csv_path = os.path.join(args.output, csv_name)
        group.to_csv(csv_path)

        table_interactive.append((
            csv_name,
            args.x_axis,
            "%s=%s" % (args.group_by, group_name),
        ))
        table_static.append(fig_name)

        fig.savefig(fig_path, format=args.fig_format)
        plt.close(fig)

    with open(os.path.join(args.output, 'fig_interactive.html'), 'w') as fh:
        fh.write(template_interactive.render(table=table_interactive))

    with open(os.path.join(args.output, 'fig_static.html'), 'w') as fh:
        fh.write(template_static.render(table=table_static))