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 loadsrclist(self, fname): self.srcs = [] self.src_names = [] self.src_redshifts = [] self.src_ra_deg = [] self.src_dec_deg = [] self.src_radec = [] # if not fname is None: # src_names = np.genfromtxt(fname,unpack=True,dtype=None) # else: # src_names = np.array(srcs.split(',')) if isinstance(fname, list): src_names = np.array(fname) if src_names.ndim == 0: src_names = src_names.reshape(1) cat = Catalog.get() for name in src_names: src = cat.get_source_by_name(name) name = src['Source_Name'] ra = src['RAJ2000'] dec = src['DEJ2000'] # self._photon_data._srcs.append(src) self.src_names.append(name) self.src_ra_deg.append(ra) self.src_dec_deg.append(dec) self.src_radec.append((np.radians(ra), np.radians(dec)))
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 loadsrclist(self,fname): self.srcs = [] self.src_names = [] self.src_redshifts = [] self.src_ra_deg = [] self.src_dec_deg = [] self.src_radec = [] # if not fname is None: # src_names = np.genfromtxt(fname,unpack=True,dtype=None) # else: # src_names = np.array(srcs.split(',')) if isinstance(fname,list): src_names = np.array(fname) if src_names.ndim == 0: src_names = src_names.reshape(1) cat = Catalog.get() for name in src_names: src = cat.get_source_by_name(name) name = src['Source_Name'] ra = src['RAJ2000'] dec = src['DEJ2000'] # self._photon_data._srcs.append(src) self.src_names.append(name) self.src_ra_deg.append(ra) self.src_dec_deg.append(dec) self.src_radec.append((np.radians(ra),np.radians(dec)))
def update_image(self): self.scf() bin_range = [self.slice,self.slice+self.nbin] self._cm = [] for im in self._im: if isinstance(im,SkyCube): self._cm.append(im.marginalize(2,bin_range=bin_range)) else: self._cm.append(im) for i in range(len(self._cm)): if self.smooth: self._cm[i] = self._cm[i].smooth(0.1) if len(self._cm) == 0: return cm = self._cm cat = Catalog.get('3fgl') if len(self._axim) == 0: axim = cm[0].plot(**self._style[0]) cat.plot(cm[0],src_color='w') self._axim.append(axim) self._ax = plt.gca() plt.colorbar(axim,orientation='horizontal',shrink=0.7,pad=0.15, fraction=0.05) return self._axim[0].set_data(cm[0].counts.T) self._axim[0].autoscale() if self._style[0]['logz']: self._axim[0].set_norm(LogNorm()) else: self._axim[0].set_norm(Normalize()) self.canvas.draw()
def __init__(self, irf, nbin=600, ebins_per_decade=16, src_type='iso', spectrum='powerlaw', spectrum_pars=[2.0], build_model=True, ltfile=None, edisp_table=None): PSFModel.__init__(self, model=spectrum, sp_param=spectrum_pars) self._src_type = src_type self._nbin_dtheta = nbin self._irf = irf self._edisp_table = edisp_table self._lonlat = (0, 0) if src_type != 'iso' and src_type != 'isodec': cat = Catalog.get() src = cat.get_source_by_name(src_type) self._lonlat = (src['RAJ2000'], src['DEJ2000']) 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._ctheta_axis = Axis.create(0.2, 1.0, 40) self._tau = np.zeros(self._ctheta_axis.nbins) self.loadLTCube(ltfile) self.fillLivetime() if build_model: self.buildModel()
def __init__(self,irf,nbin=600, ebins_per_decade=16, src_type='iso', spectrum='powerlaw', spectrum_pars=[2.0], build_model=True, ltfile=None, edisp_table=None): PSFModel.__init__(self,model=spectrum,sp_param=spectrum_pars) self._src_type = src_type self._nbin_dtheta = nbin self._irf = irf self._edisp_table = edisp_table self._lonlat = (0, 0) if src_type != 'iso' and src_type != 'isodec': cat = Catalog.get() src = cat.get_source_by_name(src_type) self._lonlat = (src['RAJ2000'], src['DEJ2000']) 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._ctheta_axis = Axis.create(0.2,1.0,40) self._tau = np.zeros(self._ctheta_axis.nbins) self.loadLTCube(ltfile) self.fillLivetime() if build_model: self.buildModel()
def loadsrclist(self,fname,srcs): self.srcs = [] self.src_names = [] self.src_redshifts = [] self.src_ra_deg = [] self.src_dec_deg = [] self.src_radec = [] if not fname is None: src_names = np.genfromtxt(fname,unpack=True,dtype=None) else: src_names = np.array(srcs.split(',')) if src_names.ndim == 0: src_names = src_names.reshape(1) cat = Catalog.get() for name in src_names: src = cat.get_source_by_name(name) name = src['Source_Name'] ra = src['RAJ2000'] dec = src['DEJ2000'] print 'Loading ', name self._photon_data._srcs.append(src) sd = skymaps.SkyDir(ra,dec) self.srcs.append(sd) self.src_names.append(name) self.src_ra_deg.append(ra) self.src_dec_deg.append(dec) self.src_radec.append((np.radians(ra),np.radians(dec))) self.src_redshifts.append(0.)
# Integrate over 3rd (energy) dimension im = im.marginalize(2) plt.figure() im.plot(cmap='ds9_b') plt.figure() # Smooth by 0.2 deg im.smooth(0.2).plot(cmap='ds9_b') # Draw an arbitrary contour in Galactic Coordinates phi = np.linspace(0, 2 * np.pi, 10000) r = np.sqrt(2 * np.cos(2 * phi)) x = im.lon + r * np.cos(phi) y = im.lat + r * np.sin(phi) im.ax()['gal'].plot(x, y, color='w') cat = Catalog.get('2fgl') cat.plot(im, ax=plt.gca(), label_threshold=5, src_color='w') # Make 1D projection on LON axis plt.figure() pim = im.project(0, offset_coord=True) pim.plot() plt.gca().grid(True) plt.show()
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)
# Integrate over 3rd (energy) dimension im = im.marginalize(2) plt.figure() im.plot(cmap='ds9_b') plt.figure() # Smooth by 0.2 deg im.smooth(0.2).plot(cmap='ds9_b') # Draw an arbitrary contour in Galactic Coordinates phi = np.linspace(0,2*np.pi,10000) r = np.sqrt(2*np.cos(2*phi)) x = im.lon + r*np.cos(phi) y = im.lat + r*np.sin(phi) im.ax()['gal'].plot(x,y,color='w') cat = Catalog.get('2fgl') cat.plot(im,ax=plt.gca(),label_threshold=5,src_color='w') # Make 1D projection on LON axis plt.figure() pim = im.project(0,offset_coord=True) pim.plot() plt.gca().grid(True) plt.show()