예제 #1
0
def make_plot(items, figfile = '',
              xlabel = '', ylabel = '',
              x_log = False, y_log = False,
              labels = [], title = '',
             ):
    plt.cla()
    plt.clf()
    
    fig = plt.figure()
    fig.patch.set_facecolor('white')
    for i, item in enumerate(items):
        label = None
        if len(labels) >= i:
            label = labels[i]
        plt.plot(item, label=label)
        
    if x_log:
        plt.xscale('log')
    if y_log:
        plt.yscale('log')
    
    plt.title(title)
    ampl.set_xlabel(xlabel)
    ampl.set_ylabel(ylabel)
    
    plt.legend()
    if figfile != '':
        plt.savefig(figfile)
    plt.show()
예제 #2
0
def histogramOverlay(frames, data, labels, xlabel, ylabel, figfile = '', 
                        x_min = 0, x_max = 2200, xbins = 22, normed = True, y_log = False,
                        atlas_x = -1, atlas_y = -1, simulation = False,
                        textlist = []):
    xbin = np.arange(x_min, x_max, (x_max - x_min) / xbins)

    plt.cla()
    plt.clf()
    fig = plt.figure()
    fig.patch.set_facecolor('white')

    zorder_start = -1 * len(data) # hack to get axes on top
    for i, datum in enumerate(data):
        plt.hist(frames[i][datum], bins = xbin, density = normed, 
            alpha = 0.5, label=labels[i], zorder=zorder_start + i)
    
    plt.xlim(x_min, x_max)
    if y_log:
        plt.yscale('log')

    ampl.set_xlabel(xlabel)
    ampl.set_ylabel(ylabel)

    if atlas_x >= 0 and atlas_y >= 0:
        ampl.draw_atlas_label(atlas_x, atlas_y, simulation = simulation, fontsize = 18)

    drawLabels(fig, atlas_x, atlas_y, simulation, textlist)
    
    fig.axes[0].zorder = len(data)+1 #hack to keep the tick marks up
    plt.legend()
    if figfile != '':
        plt.savefig(figfile)
    plt.show()
예제 #3
0
def lineOverlay(xcenter,
                lines,
                labels,
                xlabel,
                ylabel,
                figfile='',
                x_min=0.1,
                x_max=1000,
                x_log=True,
                y_min=0,
                y_max=2,
                y_log=False,
                linestyles=[],
                colorgrouping=-1,
                extra_lines=[],
                atlas_x=-1,
                atlas_y=-1,
                simulation=False,
                textlist=[]):
    plt.cla()
    plt.clf()

    fig = plt.figure()
    fig.patch.set_facecolor('white')
    for extra_line in extra_lines:
        plt.plot(extra_line[0], extra_line[1], linestyle='--', color='black')

    colors = plt.rcParams["axes.prop_cycle"].by_key()["color"]

    for i, line in enumerate(lines):
        if len(linestyles) > 0:
            linestyle = linestyles[i]
        else:
            linestyle = 'solid'
        if colorgrouping > 0:
            color = colors[int(np.floor(i / colorgrouping))]
        else:
            color = colors[i]
        plt.plot(xcenter,
                 line,
                 label=labels[i],
                 linestyle=linestyle,
                 color=color)

    if x_log:
        plt.xscale('log')
    if y_log:
        plt.yscale('log')

    plt.xlim(x_min, x_max)
    plt.ylim(y_min, y_max)
    ampl.set_xlabel(xlabel)
    ampl.set_ylabel(ylabel)

    drawLabels(fig, atlas_x, atlas_y, simulation, textlist)

    plt.legend()
    if figfile != '':
        plt.savefig(figfile)
    plt.show()
예제 #4
0
def roc_plot(xlist, ylist, figfile = '',
             xlabel='False positive rate',
             ylabel='True positive rate',
             x_min = 0, x_max = 1.1, x_log = False,
             y_min = 0, y_max = 1.1, y_log = False,
             linestyles=[], colorgrouping=-1,
             extra_lines=[], labels=[],
             atlas_x=-1, atlas_y=-1, simulation=False,
             textlist=[], title=''):
    plt.cla()
    plt.clf()
    
    fig = plt.figure()
    fig.patch.set_facecolor('white')
    for extra_line in extra_lines:
        plt.plot(extra_line[0], extra_line[1], linestyle='--', color='black')
        
    colors = plt.rcParams["axes.prop_cycle"].by_key()["color"]
    for i, (x,y) in enumerate(zip(xlist,ylist)):
        if len(linestyles) > 0:
            linestyle = linestyles[i]
        else:
            linestyle = 'solid'
        if colorgrouping > 0:
            color = colors[int(np.floor(i / colorgrouping))]
        else:
            color = colors[i%(len(colors)-1)]
        label = None
        if len(labels) > 0:
            label = labels[i]
        plt.plot(x, y, label = label, linestyle=linestyle, color=color)
        
    if x_log:
        plt.xscale('log')
    if y_log:
        plt.yscale('log')
        
    plt.title(title)
    plt.xlim(x_min, x_max)
    plt.ylim(y_min, y_max)
    ampl.set_xlabel(xlabel)
    ampl.set_ylabel(ylabel)
    
    plt.legend()
    if figfile != '':
        plt.savefig(figfile)
    plt.show()
예제 #5
0
def rocScan(varlist, scan_targets, labels, plotpath, ylabels, data):
    '''
    Creates a set of ROC curve plots by scanning over the specified variables.
    One set is created for each target (neural net score dataset).
    
    varlist: a list of rocVar instances to scan over
    scan_targets: a list of neural net score datasets to use
    labels: a list of target names (strings); must be the same length as scan_targets
    '''
    for target, target_label in zip(scan_targets, labels):
        for v in varlist:
            # prepare matplotlib figure
            plt.cla()
            plt.clf()
            fig = plt.figure()
            fig.patch.set_facecolor('white')
            plt.plot([0, 1], [0, 1], 'k--')

            for binning, label in zip(v.selections, v.labels):
                # first generate ROC curve
                x, y, t = roc_curve(
                    ylabels[data.test & binning][:, 1],
                    target[data.test & binning],
                    drop_intermediate=False,
                )
                var_auc = auc(x, y)
                plt.plot(x,
                         y,
                         label=label + ' (area = {:.3f})'.format(var_auc))

            plt.title('ROC Scan of ' + target_label + ' over ' + v.latex)
            plt.xlim(0, 1.1)
            plt.ylim(0, 1.1)
            ampl.set_xlabel('False positive rate')
            ampl.set_ylabel('True positive rate')
            plt.legend()
            plt.savefig(plotpath + 'roc_scan_' + target_label + '_' + v.name +
                        '.pdf')
            plt.show()
예제 #6
0
def rocScan(varlist,
            scan_targets,
            labels,
            ylabels,
            data,
            plotpath='',
            x_min=0.,
            x_max=1.0,
            y_min=0.0,
            y_max=1.0,
            x_log=False,
            y_log=False,
            rejection=False,
            x_label='False positive rate',
            y_label='True positive rate',
            linestyles=[],
            colorgrouping=-1,
            extra_lines=[],
            atlas_x=-1,
            atlas_y=-1,
            simulation=False,
            textlist=[]):
    '''
    Creates a set of ROC curve plots by scanning over the specified variables.
    One set is created for each target (neural net score dataset).
    
    varlist: a list of rocVar instances to scan over
    scan_targets: a list of neural net score datasets to use
    labels: a list of target names (strings); must be the same length as scan_targets
    '''

    rocs = buildRocs(varlist, scan_targets, labels, ylabels, data)

    for target_label in labels:
        for v in varlist:
            # prepare matplotlib figure
            plt.cla()
            plt.clf()
            fig = plt.figure()
            fig.patch.set_facecolor('white')
            plt.plot([0, 1], [0, 1], 'k--')

            for label in v.labels:
                # first generate ROC curve
                x = rocs[target_label + label]['x']
                y = rocs[target_label + label]['y']
                var_auc = auc(x, y)
                if not rejection:
                    plt.plot(x,
                             y,
                             label=label + ' (area = {:.3f})'.format(var_auc))
                else:
                    plt.plot(y,
                             1. / x,
                             label=label + ' (area = {:.3f})'.format(var_auc))

            # plt.title('ROC Scan of '+target_label+' over '+v.latex)
            if x_log:
                plt.xscale('log')
            if y_log:
                plt.yscale('log')
            plt.xlim(x_min, x_max)
            plt.ylim(y_min, y_max)
            ampl.set_xlabel(x_label)
            ampl.set_ylabel(y_label)
            plt.legend()

            drawLabels(fig, atlas_x, atlas_y, simulation, textlist)

            if plotpath != '':
                plt.savefig(plotpath + 'roc_scan_' + target_label + '_' +
                            v.name + '.pdf')
            plt.show()
예제 #7
0
                      -bkg_err,
                      color='black',
                      alpha=0.3)
ratio = (data - bkg) / bkg
ratio_ax.errorbar(bin_centers,
                  ratio,
                  yerr=(np.sqrt(data) * (ratio / data)),
                  fmt='ko')
out_of_range = np.where(ratio > 1, 1, np.where(ratio < -1, -1, np.NaN))
ratio_ax.plot(bin_centers, out_of_range, marker=CARETUP, color='paper:red')
ratio_ax.set_ylabel(r"$\frac{\textsf{Data} - \textsf{Bkg}}{\textsf{Bkg}}$",
                    fontsize=12)
ratio_ax.set_ylim((-1, 1))
ratio_ax.yaxis.set_minor_locator(AutoMinorLocator())
ax.yaxis.set_minor_locator(AutoMinorLocator())
atlas.set_xlabel(var_labels[args.var], ax=ratio_ax)
ax.set_ylim((0, ax.get_ylim()[1]))
atlas.set_ylabel('Events', ax=ax)

region = args.region
if region == 'sig':
    region = 'signal'
region = region.capitalize()

atlas.draw_atlas_label(
    0.3,
    0.97,
    ax=ax,
    status='int',
    energy='13 TeV',
    lumi=24,
            rwgt_func = FitFunction(bin_centers, ratio, ratio_err)
            ax = plt.subplot(len(rwgt_vars), 1, i + 1)
            ax.errorbar(bin_centers, ratio, yerr=ratio_err, fmt='o')

            rwgt_funcs[var] = rwgt_func
            x = np.linspace(min_var, max_var, 1000)
            y = rwgt_func.fit(x)
            extreme = np.max(np.abs(y - 1))
            done = extreme < 0.05
            ax.plot(x, y)
            ax.plot(rwgt_func.orig_x, rwgt_func.vals, 'g:')
            for spine in ax.spines.values():
                spine.set_color('paper:green' if done else 'paper:red')
                spine.set_linewidth(2)

            atlas.set_xlabel(f"{rwgt_vars[var]} (max. dev.: {extreme:.4f})",
                             ax=ax)
            all_done.append(done)
        all_rwgt_funcs.append(rwgt_funcs)
        plt.tight_layout()
        plt.subplots_adjust(top=0.95)
        plt.suptitle(f"Iteration {iter_num} (f = {f:.3f})", fontsize=15)
        pdf.savefig()
        plt.close()
        iter_num += 1
        if np.all(all_done) or iter_num >= 100:
            print("\nDone. Optimizing f one final time...")
            sf = np.ones(bkg.shape[0])
            ttbar_2tag_sf = ttbar_2tag.mc_sf.values
            if len(all_rwgt_funcs) != 0:
                for rwgt_dict in all_rwgt_funcs:
                    for k, v in rwgt_dict.items():