예제 #1
0
def getHist(data,var_name,mask=None,edges=None):
    
    h = Histogram(edges)    

    if not mask is None:
        h.fill(data[var_name][mask])
    else:
        h.fill(data[var_name])

    return h
예제 #2
0
def getHist(data, var_name, mask=None, edges=None):

    h = Histogram(edges)

    if not mask is None:
        h.fill(data[var_name][mask])
    else:
        h.fill(data[var_name])

    return h
예제 #3
0
def getOnOffHist(data,var_name,phases,mask=None,edges=None):
    
    (on_phase,off_phase,alpha) = phases

    on_mask = PhotonData.get_mask(data,phases=on_phase)
    off_mask = PhotonData.get_mask(data,phases=off_phase)

    if not mask is None:
        on_mask &= mask
        off_mask &= mask

    hon = Histogram(edges)
    hon.fill(data[var_name][on_mask])

    hoff = Histogram(edges)
    hoff.fill(data[var_name][off_mask])

    hoffs = copy.deepcopy(hoff)
    hoffs *= alpha

    return (hon,hoff,hoffs)
예제 #4
0
def getOnOffHist(data, var_name, phases, mask=None, edges=None):

    (on_phase, off_phase, alpha) = phases

    on_mask = PhotonData.get_mask(data, phases=on_phase)
    off_mask = PhotonData.get_mask(data, phases=off_phase)

    if not mask is None:
        on_mask &= mask
        off_mask &= mask

    hon = Histogram(edges)
    hon.fill(data[var_name][on_mask])

    hoff = Histogram(edges)
    hoff.fill(data[var_name][off_mask])

    hoffs = copy.deepcopy(hoff)
    hoffs *= alpha

    return (hon, hoff, hoffs)
예제 #5
0
    print 'SIGMA ', sigma_mu_fn(1.0)
    print 'SIGMA ', sigma_mu_fn(10.0)
    print 'Asimov q0 UL  ', find_fn_root(lambda t: fn_qmu(mub+t,mub+t,mub),0,100,dlnl)
    print 'Asimov qmu UL ', ul_asimov_qmu, sigma
    print 'Asimov UL ', ul_asimov, ul_asimov_upper-ul_asimov, ul_asimov_lower-ul_asimov

    print -2*poisson_delta_lnl(mub,mub,mub+ul_asimov)
    print -2*poisson_delta_lnl(mub,mub,mub+ul_asimov_qmu)
    print fn_qmu(mub,mub,mub+ul_asimov_qmu)
    print dlnl

    qmu = -2*poisson_delta_lnl(mub,mub,mub+ul_asimov_qmu)

    h = Histogram(Axis.create(0,100,100))

    h.fill(ul_mc)

    h.normalize().cumulative().plot()

    plt.axhline(0.5-0.34,color='g')

    plt.axhline(0.5+0.34,color='g')

    plt.axvline(ul_asimov_qmu,color='k')

    plt.axvline(ul_asimov_qmu+sigma,color='k',linestyle='--')
    plt.axvline(ul_asimov_qmu-sigma,color='k',linestyle='--')
#    plt.axvline(ul_asimov_qmu-sigma/(ul_asimov_qmu+sigma)*ul_asimov_qmu,
#                color='k',linestyle='--')

    plt.gca().grid(True)
예제 #6
0
    def make_image_plot(self,subplot,h,fig,fig2,title,rpsf68,rpsf95,
                        smooth=False,
                        resid_type=None,mc_resid=None,**kwargs):

        plt.figure(fig.get_label())

        cb_label='Counts'

        if resid_type == 'significance':
            kwargs['vmin'] = -5
            kwargs['vmax'] = 5
            kwargs['levels'] = [-5.0,-3.0,3.0,5.0]
            cb_label = 'Significance [$\sigma$]'
        elif resid_type == 'fractional':
            kwargs['vmin'] = -1.0
            kwargs['vmax'] = 1.0
            kwargs['levels'] = [-1.0,-0.5,0.5,1.0]
            cb_label = 'Fractional Residual'

        if smooth:
            kwargs['beam_size'] = [self._rsmooth,self._rsmooth,0.0,4]
            
        axim = h.plot(subplot=subplot,cmap='ds9_b',**kwargs)
        h.plot_circle(rpsf68,color='w',lw=1.5)
        h.plot_circle(rpsf95,color='w',linestyle='--',lw=1.5)
        h.plot_marker(marker='+',color='w',linestyle='--')
        ax = h.ax()
        ax.set_title(title)
        cb = plt.colorbar(axim,orientation='horizontal',
                          shrink=0.9,pad=0.15,
                          fraction=0.05)

        cb.set_label(cb_label)

        cat = Catalog.get('3fgl')
        cat.plot(h,ax=ax,src_color='w',label_threshold=5.0)

        if resid_type is None: return
        
        plt.figure(fig2.get_label())        
        ax2 = fig2.add_subplot(subplot)

        z = h.counts[10:-10,10:-10]

        if resid_type == 'significance':
            zproj_axis = Axis.create(-6,6,120)
        elif resid_type == 'fractional':
            zproj_axis = Axis.create(-1.0,1.0,120)
        else:
            zproj_axis = Axis.create(-10,10,120)


        hz = Histogram(zproj_axis)
        hz.fill(np.ravel(z))

        nbin = np.prod(z.shape)
        
        hz_mc = Histogram(zproj_axis)    
        
        if mc_resid:
            for mch in mc_resid:
                z = mch.counts[10:-10,10:-10]
                hz_mc.fill(np.ravel(z))

            hz_mc /= float(len(mc_resid))

            
        fn = lambda t : 1./np.sqrt(2*np.pi)*np.exp(-t**2/2.)
        
        hz.plot(label='Data',linestyle='None')

        if resid_type == 'significance':
            plt.plot(hz.axis().center,
                     fn(hz.axis().center)*hz.axis().width*nbin,
                     color='k',label='Gaussian ($\sigma = 1$)')
        
        hz_mc.plot(label='MC',hist_style='line')
        plt.gca().grid(True)
        plt.gca().set_yscale('log')
        plt.gca().set_ylim(0.5)

        ax2.legend(loc='upper right',prop= {'size' : 10 })

        data_stats = 'Mean = %.2f\nRMS = %.2f'%(hz.mean(),hz.stddev())
        mc_stats = 'MC Mean = %.2f\nMC RMS = %.2f'%(hz_mc.mean(),
                                                    hz_mc.stddev())
        
        ax2.set_xlabel(cb_label)
        ax2.set_title(title)
        ax2.text(0.05,0.95,
                 '%s\n%s'%(data_stats,mc_stats),
                 verticalalignment='top',
                 transform=ax2.transAxes,fontsize=10)
예제 #7
0
    def make_image_plot(self,
                        subplot,
                        h,
                        fig,
                        fig2,
                        title,
                        rpsf68,
                        rpsf95,
                        smooth=False,
                        resid_type=None,
                        mc_resid=None,
                        **kwargs):

        plt.figure(fig.get_label())

        cb_label = 'Counts'

        if resid_type == 'significance':
            kwargs['vmin'] = -5
            kwargs['vmax'] = 5
            kwargs['levels'] = [-5.0, -3.0, 3.0, 5.0]
            cb_label = 'Significance [$\sigma$]'
        elif resid_type == 'fractional':
            kwargs['vmin'] = -1.0
            kwargs['vmax'] = 1.0
            kwargs['levels'] = [-1.0, -0.5, 0.5, 1.0]
            cb_label = 'Fractional Residual'

        if smooth:
            kwargs['beam_size'] = [self._rsmooth, self._rsmooth, 0.0, 4]

        axim = h.plot(subplot=subplot, cmap='ds9_b', **kwargs)
        h.plot_circle(rpsf68, color='w', lw=1.5)
        h.plot_circle(rpsf95, color='w', linestyle='--', lw=1.5)
        h.plot_marker(marker='x', color='w', linestyle='--')
        ax = h.ax()
        ax.set_title(title)
        cb = plt.colorbar(axim,
                          orientation='horizontal',
                          shrink=0.9,
                          pad=0.15,
                          fraction=0.05)

        if kwargs.get('zscale', None) is not None:
            import matplotlib.ticker
            cb.locator = matplotlib.ticker.MaxNLocator(nbins=5)
            cb.update_ticks()

        cb.set_label(cb_label)

        cat = Catalog.get('3fgl')
        cat.plot(h,
                 ax=ax,
                 src_color='w',
                 label_threshold=self._srclabels_thresh)

        if resid_type is None: return

        plt.figure(fig2.get_label())
        ax2 = fig2.add_subplot(subplot)

        z = h.counts[10:-10, 10:-10]

        if resid_type == 'significance':
            zproj_axis = Axis.create(-6, 6, 120)
        elif resid_type == 'fractional':
            zproj_axis = Axis.create(-1.0, 1.0, 120)
        else:
            zproj_axis = Axis.create(-10, 10, 120)

        hz = Histogram(zproj_axis)
        hz.fill(np.ravel(z))

        nbin = np.prod(z.shape)

        hz_mc = Histogram(zproj_axis)

        if mc_resid:
            for mch in mc_resid:
                z = mch.counts[10:-10, 10:-10]
                hz_mc.fill(np.ravel(z))

            hz_mc /= float(len(mc_resid))

        fn = lambda t: 1. / np.sqrt(2 * np.pi) * np.exp(-t**2 / 2.)

        hz.plot(label='Data', linestyle='None')

        if resid_type == 'significance':
            plt.plot(hz.axis().center,
                     fn(hz.axis().center) * hz.axis().width * nbin,
                     color='k',
                     label='Gaussian ($\sigma = 1$)')

        hz_mc.plot(label='MC', hist_style='line')
        plt.gca().grid(True)
        plt.gca().set_yscale('log')
        plt.gca().set_ylim(0.5)

        ax2.legend(loc='upper right', prop={'size': 10})

        data_stats = 'Mean = %.2f\nRMS = %.2f' % (hz.mean(), hz.stddev())
        mc_stats = 'MC Mean = %.2f\nMC RMS = %.2f' % (hz_mc.mean(),
                                                      hz_mc.stddev())

        ax2.set_xlabel(cb_label)
        ax2.set_title(title)
        ax2.text(0.05,
                 0.95,
                 '%s\n%s' % (data_stats, mc_stats),
                 verticalalignment='top',
                 transform=ax2.transAxes,
                 fontsize=10)
예제 #8
0
    cm = CompositeModel()
#    cm.addModel(KingFn.create(1.0,3.0,0.5*nevent))

    cm.addModel(PolyFn.create(3,[0,1,2]))
#    cm.addModel(KingFn.create(0.2,3.0,0.5*nevent,3))

    print cm.param()

    nbin = 80

    h0 = Histogram([0,5.0],nbin)
    h1 = Histogram([0,5.0],nbin)



    h0.fill(cm.rnd(nevent,10.0))

    lnlfn = OnOffBinnedLnL(h0._counts,h1._counts,h0._xedges,1.0,cm)

    plt.figure()

    sig = np.linspace(0.9,1.1,100)

    


    p = cm.param().makeParameterArray(1,sig)


#print lnlfn.eval(p)
#print lnlfn.eval(p[0])
예제 #9
0
    nevent = 10000

    cm = CompositeModel()
    #    cm.addModel(KingFn.create(1.0,3.0,0.5*nevent))

    cm.addModel(PolyFn.create(3, [0, 1, 2]))
    #    cm.addModel(KingFn.create(0.2,3.0,0.5*nevent,3))

    print cm.param()

    nbin = 80

    h0 = Histogram([0, 5.0], nbin)
    h1 = Histogram([0, 5.0], nbin)

    h0.fill(cm.rnd(nevent, 10.0))

    lnlfn = OnOffBinnedLnL(h0._counts, h1._counts, h0._xedges, 1.0, cm)

    plt.figure()

    sig = np.linspace(0.9, 1.1, 100)

    p = cm.param().makeParameterArray(1, sig)

    #print lnlfn.eval(p)
    #print lnlfn.eval(p[0])

    plt.plot(sig, lnlfn.eval(p))

    lnlfn.fit()