예제 #1
0
def rcg_to_rocFig(c, g, color='b', label=None, figax=None):

    if figax==None:
        fig = plt.figure(figsize=default_figsize)
        ax = fig.add_axes(default_axpos)
    else:
        fig, ax = figax

    n_c = c[-1]
    n_g = g[-1]

    fap = np.array(c, dtype="float")/n_c
    eff = np.array(g, dtype="float")/n_g

    if color:
        if label:
            ax.step(fap, eff, color=color, where='post', label=label) ### plot steps after increase in eff
        else:
            ax.step(fap, eff, color=color, where='post') ### plot steps after increase in eff
        ax.plot(fap, eff, markersize=2, marker='o', markerfacecolor=color, markeredgecolor=color, linestyle='none') ### plot steps after increase in eff
    else:
        if label:
            ax.step(fap, eff, where='post', label=label) ### plot steps after increase in eff
        else:
            ax.step(fap, eff, where='post') ### plot steps after increase in eff
        ax.plot(fap, eff, markersize=2, marker='o', markerfacecolor=color, markeredgecolor=color, linestyle='none') ### plot steps after increase in eff

    ### plot error bars at sample points
    for _c, _g in zip(c, g):
        cln_CR = idq.binomialCR( _c, n_c, conf=0.68 )
        gch_CR = idq.binomialCR( _g, n_g, conf=0.68 )
        if color:
            ax.fill_between( cln_CR, 2*[gch_CR[0]], 2*[gch_CR[1]], color=color, alpha=0.05)
        else:
            ax.fill_between( cln_CR, 2*[gch_CR[0]], 2*[gch_CR[1]], alpha=0.05)

#    ### plot 90% UL on fap and eff
#    fap_UL = idq.binomialUL( c, n_c, conf=0.90 )
#    eff_UL = idq.binomialUL( g, n_g, conf=0.90 )
#    if color:
#        ax.plot( fap, eff_UL, color=color, alpha=0.25 )
#        ax.plot( fap_UL, eff, color=color, alpha=0.25 )
#    else:
#        ax.plot( fap, eff_UL, alpha=0.25 )
#        ax.plot( fap_UL, eff, alpha=0.25 )

    ax.grid(True, which='both')

    ax.set_xscale('log')
    ax.set_yscale('linear')

    ax.set_xlim(xmin=1e-5, xmax=1)
    ax.set_ylim(ymin=0, ymax=1)

    ax.set_xlabel('False Alarm Probability')
    ax.set_ylabel('Glitch Detection Efficiency')

    return fig, ax
#    color = ax.step( dt, eff, label='%d events with KWsignif $\geq %.1f$'%(N, signif), where='post' )[0].get_color()
    color = ax.plot(dt,
                    eff,
                    label='%d events with KWsignif $\geq %.1f$' %
                    (N, signif))[0].get_color()

    jsonD[signif] = {
        'observed deadtime': dt,
        'observed efficiency': eff,
        'number of glitches': N,
        'duration': T
    }

    l, h = [], []
    for e in eff:
        cr = idq.binomialCR(e * N, N, conf=0.68)
        l.append(cr[0])
        h.append(cr[1])
    ax.fill_between(dt, l, h, color=color, alpha=0.25, edgecolor='none')

### add curve from dat files
if opts.verbose:
    print "finding all *dat files"
dats = [
    dat for dat in idq.get_all_files_in_range(
        realtimedir, opts.start, opts.end, pad=0, suffix='.dat')
    if (opts.classifier == idq.extract_dat_name(dat)) and event.livetime(
        event.andsegments([[idq.extract_start_stop(dat, suffix=".dat")],
                           idqsegs]))
]
if opts.verbose:
    if N:
        eff = []
        for fapThr in opts.FAPthr:
            s = segs[fapThr][0]
            eff.append( len(event.include( trgs, s, tcent=event.col_kw['tcent'] ))/N )
    else:
        eff = [0.0 for fapThr in opts.FAPthr]

#    color = ax.step( dt, eff, label='%d events with KWsignif $\geq %.1f$'%(N, signif), where='post' )[0].get_color()
    color = ax.plot( dt, eff, label='%d events with KWsignif $\geq %.1f$'%(N, signif) )[0].get_color()

    jsonD[signif] = {'observed deadtime':dt, 'observed efficiency':eff, 'number of glitches':N, 'duration':T}

    l, h = [], []
    for e in eff:
        cr = idq.binomialCR( e*N, N, conf=0.68 )
        l.append(cr[0])
        h.append(cr[1])
    ax.fill_between( dt, l, h, color=color, alpha=0.25, edgecolor='none' )    

### add curve from dat files
if opts.verbose:
    print "finding all *dat files"
dats = [dat for dat in idq.get_all_files_in_range( realtimedir, opts.start, opts.end, pad=0, suffix='.dat') if (opts.classifier==idq.extract_dat_name( dat )) and event.livetime(event.andsegments([[idq.extract_start_stop(dat, suffix=".dat")], idqsegs]))]
if opts.verbose:
    print "reading samples from %d dat files"%len(dats)
output = idq.slim_load_datfiles( dats, skip_lines=0, columns=['GPS', 'i', 'rank'])
output = idq.filter_datfile_output( output, idqsegs )
#output['GPS'] = [float(l) for l in output['GPS']] ### not necessary because values are cast appropriately within idq.filter_datfile_output
#output['i'] = [float(l) for l in output['i']]
#output['rank'] = [float(l) for l in output['rank']]