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)
    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()



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

    plt.figure()


    x = np.linspace(0,3,100)

    h0.plot()
    
    plt.plot(h0._x,cm.integrate(h0._xedges[:-1],h0._xedges[1:]))

    plt.show()
Exemple #3
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)
Exemple #4
0
    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()

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

    plt.figure()

    x = np.linspace(0, 3, 100)

    h0.plot()

    plt.plot(h0._x, cm.integrate(h0._xedges[:-1], h0._xedges[1:]))

    plt.show()