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
def bootstrap(self,fraction=0.68,niter=100,xmax=None): if xmax is None: nedge = len(self._xedge) else: nedge = len(self._ncounts[self._xedge<=xmax]) xedge = self._xedge[:nedge] h = Histogram.createFromCDF(xedge,self._ncounts[1:nedge]) nbkg = np.random.poisson(self._nbkg,niter).astype(float) ncounts = np.random.poisson(np.concatenate(([0],h.counts)), (niter,nedge)) xq = [] for i in range(niter): x = self._quantile(nbkg[i],self._xedge[:nedge], ncounts[i],fraction) xq.append(x) xq_mean = np.mean(np.array(xq)) xq_rms = np.std(np.array(xq)) return xq_mean, xq_rms
def fillLivetime(self, ltc, src_type): self._tau = Histogram(self._cth_axis) if src_type == 'iso': self._tau += self._cth_axis.width elif src_type == 'isodec': sinlat = np.linspace(-1, 1, 48) m = ltc._ltmap[:, i] self._tau[i] = 0 for s in sinlat: lat = np.arcsin(s) th = np.pi / 2. - lat ipix = healpy.ang2pix(64, th, 0, nest=True) self._tau[i] += m[ipix] else: if not isinstance(src_type, list): src_type = [src_type] for s in src_type: cat = Catalog.get() src = cat.get_source_by_name(s) lonlat = (src['RAJ2000'], src['DEJ2000']) self._tau += ltc.get_src_lthist(lonlat[0], lonlat[1], cth_axis=self._cth_axis)
def bootstrap(self,fraction=0.68,niter=100,xmax=None): nedge = len(self._non) hon = Histogram.createFromCDF(self._axis.edges,self._non[1:]) hoff = Histogram.createFromCDF(self._axis.edges,self._noff[1:]) non = np.random.poisson(np.concatenate(([0],hon.counts)), (niter,nedge)) noff = np.random.poisson(np.concatenate(([0],hoff.counts)), (niter,nedge)) xq = [] for i in range(niter): xq.append(self._quantile(non[i],noff[i],fraction)) xq_mean = np.mean(np.array(xq)) xq_rms = np.std(np.array(xq)) return xq_mean, xq_rms
def get_spectrum(self,lon,lat): xy = self._wcs.wcs_world2pix(lon,lat, 0) ilon = np.round(xy[0][0]) ilat = np.round(xy[1][0]) ilon = min(max(0,ilon),self._axes[0]._naxis-1) ilat = min(max(0,ilat),self._axes[1]._naxis-1) c = self._counts.T[ilon,ilat,:] edges = self._axes[2].edges return Histogram.createFromArray(edges,c)
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
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)
def __init__(self, irf, nbin=600, ebins_per_decade=16, src_type='iso', spectrum='powerlaw', spectrum_pars=[2.0], ltcube=None, edisp_table=None): super(PSFModelLT, self).__init__(model=spectrum, sp_param=spectrum_pars) self._nbin_dtheta = nbin self._irf = irf self._edisp_table = edisp_table loge_step = 1. / float(ebins_per_decade) emin = 1.0 + loge_step / 2. emax = 6.0 - loge_step / 2. nbin = int((emax - emin) / loge_step) + 1 self._loge_axis = Axis.create(emin, emax, nbin) self._energy = np.power(10, self._loge_axis.center) self._exps = np.zeros(self._loge_axis.nbins) self._psf = np.zeros((self._loge_axis.nbins, self._nbin_dtheta)) self._dtheta = np.array([ self._dtheta_max_deg * (float(i) / float(self._nbin_dtheta))**2 for i in range(self._nbin_dtheta) ]) self._dtheta_axis = Axis(self._dtheta) self._cth_axis = Axis.create(0.2, 1.0, 40) self._tau = Histogram(self._cth_axis) self.fillLivetime(ltcube, src_type) self.buildModel()
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)
def histogram(self, emin, emax, cthmin, cthmax, edges): y = self.thetasq(emin, emax, cthmin, cthmax, edges) return Histogram(edges, counts=y)
print 'SIGMA ', sigma 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='--')
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)
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)
sys.exit(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)
sys.exit(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])
parser.add_argument('--colors', default=None) parser.add_argument('--ylim_ratio', default='0.6/1.4') parser.add_argument('--prefix', default='') args = parser.parse_args() labels = args.labels.split(',') if args.colors: colors = args.colors.split(',') else: colors = ['b', 'g', 'r', 'm', 'c'] ylim_ratio = [float(t) for t in args.ylim_ratio.split('/')] hist_set = { 'title': 'test', 'ts': Histogram(Axis.create(0., 16., 160)), 'fluxul': Histogram(Axis.create(-12, -8, 100)) } pwl_hists = [] dm_hists = [] hists = {} results = [] limits = {} for f in args.files: # results = AnalysisResults(f)