def pointlike_analysis(roi, name, hypothesis, max_free, seddir, datadir, plotdir, localize=False, fit_extension=False, cutoff=False, cutoff_model=None, override_localization=None, ): """ emin + emax used for computing upper limits. """ print 'Performing Pointlike analysis for %s' % hypothesis print_summary = lambda: roi.print_summary(galactic=True, maxdist=10) print_summary() emin, emax = get_full_energy_range(roi) print roi def fit(just_prefactor=False, just_source=False, fit_bg_first=False): """ Convenience function incase fit fails. """ try: if just_prefactor: fit_prefactor(roi, name) elif just_source: fit_only_source(roi, name) elif fit_bg_first: roi.fit(fit_bg_first=True) else: roi.fit() # For some reason, one final fit seems to help with convergence and not getting negative TS values *shurgs* roi.fit() except Exception, ex: print 'ERROR spectral fitting pointlike for hypothesis %s:' % hypothesis, ex traceback.print_exc(file=sys.stdout) print_summary()
def tsmap_plots(roi, name, hypothesis, datadir, plotdir, size, new_sources, tsmap_pixelsize=0.1, **common_kwargs): """ TS maps """ emin, emax = get_full_energy_range(roi) extra='%s_%s_%sdeg' % (hypothesis,name,size) tsmap_kwargs = dict(size=size, pixelsize=tsmap_pixelsize, **common_kwargs) def _plot(filename,title): try: print 'Making Band %s TS Map (size=%s, pixelsize=%s)' % (title,size,tsmap_pixelsize) roi.plot_tsmap(filename='%s/tsmap_%s_%s.png' % (plotdir,filename,extra), fitsfile='%s/tsmap_%s_%s.fits' % (datadir,filename,extra), title='%s TS Map for %s (%s)' % (title,name,hypothesis), **tsmap_kwargs) except Exception, ex: print 'ERROR making TS map:', ex traceback.print_exc(file=sys.stdout) if all_energy(emin,emax): try: print 'Making Band %s TS Map (size=%s, pixelsize=%s)' % (title,size,tsmap_pixelsize) ROITSMapBandPlotter(roi, title='Band %s TS Map for %s (%s)' % (title,name,hypothesis), bin_edges=one_bin_per_dec(emin,emax), **tsmap_kwargs).show(filename='%s/band_tsmap_%s_%s.png' % (plotdir,filename,extra)) except Exception, ex: print 'ERROR making band TS map:', ex traceback.print_exc(file=sys.stdout)
def compute(self): roi=self.roi which=self.which roi.modify(which=which, model=self.model0, keep_old_flux=False) if not self.quiet: print 'fit source %s with model0 (%s)' % (which,self.model0.name) self.ll_0 = self.fit() if not self.quiet: print roi sed = PointlikeSED(roi,name=which) self.sed_points = sed.todict() # TODO, get SED points emin,emax=get_full_energy_range(roi) flux=self.model0.i_flux(emin=emin,emax=emax) self.model1.set_flux(flux,emin=emin,emax=emax) self.comprehensive_model = ComprehensiveModel(self.model0.copy(), self.model1.copy()) self.comprehensive_model.theta=0.5 self.comprehensive_model_start = self.comprehensive_model.copy() if not self.quiet: print 'fit source %s with model1 (%s)' % (which,self.model1.name) roi.modify(which=self.which, model=self.comprehensive_model, keep_old_flux=False) self.ll_1 = self.fit() if not self.quiet: print roi self.TS_comp = 2*(self.ll_1-self.ll_0)
def counts_plots(roi, name, hypothesis, datadir, plotdir, size, pixelsize, **common_kwargs): """ Counts plots """ emin, emax = get_full_energy_range(roi) extra = '%s_%s_%sdeg_%sdeg' % (hypothesis, name, size, pixelsize) counts_kwargs = dict(size=size, **common_kwargs) title = 'Counts Residual for %s (%s)' % (name, hypothesis) print title roi.plot_counts_map( filename="%s/counts_residual_%s.png" % (plotdir, extra), countsfile="%s/counts_residual_%s.fits" % (datadir, extra), modelfile="%s/model_residual_%s.fits" % (datadir, extra), pixelsize=pixelsize, title=title, **counts_kwargs) roi.zero_source(which=name) title = 'Counts Source for %s (%s)' % (name, hypothesis) print title roi.plot_counts_map(filename="%s/counts_source_%s.png" % (plotdir, extra), countsfile="%s/counts_source_%s.fits" % (datadir, extra), modelfile="%s/model_source_%s.fits" % (datadir, extra), pixelsize=pixelsize, title=title, **counts_kwargs) roi.unzero_source(which=name) title = 'Slice for %s (%s)' % (name, hypothesis) print title roi.plot_slice(which=name, pixelsize=pixelsize, filename="%s/counts_slice_%s.png" % (plotdir, extra), datafile='%s/counts_slice_%s.dat' % (datadir, extra), title=title) title = 'Radial Integral for %s (%s)' % (name, hypothesis) print title roi.plot_radial_integral( which=name, pixelsize=pixelsize, filename="%s/radial_integral_%s.png" % (plotdir, extra), datafile='%s/radial_integral_%s.dat' % (datadir, extra), title=title) try: title = 'Spectra for %s (%s)' % (name, hypothesis) print title roi.plot_counts_spectra(filename="%s/spectra_%s_%s.png" % (plotdir, hypothesis, name), title=title) except Exception, ex: print 'ERROR with plot_counts_spectra: ', ex traceback.print_exc(file=sys.stdout)
def counts_plots(roi, name, hypothesis, datadir, plotdir, size, pixelsize, **common_kwargs): """ Counts plots """ emin, emax = get_full_energy_range(roi) extra='%s_%s_%sdeg_%sdeg' % (hypothesis,name,size,pixelsize) counts_kwargs = dict(size=size, **common_kwargs) title = 'Counts Residual for %s (%s)' % (name,hypothesis) print title roi.plot_counts_map(filename="%s/counts_residual_%s.png"%(plotdir,extra), countsfile="%s/counts_residual_%s.fits"%(datadir,extra), modelfile="%s/model_residual_%s.fits"%(datadir,extra), pixelsize=pixelsize, title=title, **counts_kwargs) roi.zero_source(which=name) title = 'Counts Source for %s (%s)' % (name,hypothesis) print title roi.plot_counts_map(filename="%s/counts_source_%s.png"%(plotdir,extra), countsfile="%s/counts_source_%s.fits"%(datadir,extra), modelfile="%s/model_source_%s.fits"%(datadir,extra), pixelsize=pixelsize, title=title, **counts_kwargs) roi.unzero_source(which=name) title = 'Slice for %s (%s)' % (name,hypothesis) print title roi.plot_slice(which=name, pixelsize=pixelsize, filename="%s/counts_slice_%s.png"%(plotdir,extra), datafile='%s/counts_slice_%s.dat'%(datadir,extra), title=title) title = 'Radial Integral for %s (%s)' % (name,hypothesis) print title roi.plot_radial_integral(which=name, pixelsize=pixelsize, filename="%s/radial_integral_%s.png"%(plotdir,extra), datafile='%s/radial_integral_%s.dat'%(datadir,extra), title=title) try: title = 'Spectra for %s (%s)' % (name,hypothesis) print title roi.plot_counts_spectra(filename="%s/spectra_%s_%s.png"%(plotdir,hypothesis, name), title=title) except Exception, ex: print 'ERROR with plot_counts_spectra: ', ex traceback.print_exc(file=sys.stdout)
def smooth_plots(roi, name, hypothesis, datadir, plotdir, size, kernel_rad, **common_kwargs): """ smoothed counts maps """ emin, emax = get_full_energy_range(roi) extra = '%s_%s_%sdeg_%sdeg' % (hypothesis, name, size, kernel_rad) smooth_kwargs = dict( which=name, override_center=roi.roi_dir, size=size, colorbar_radius=1, # most interesting within one degrees kernel_rad=kernel_rad, **common_kwargs) title = 'Source Map for %s (%s)' % (name, hypothesis) print title roi.plot_source(filename='%s/source_%s.png' % (plotdir, extra), title=title, **smooth_kwargs) title = 'Sources Map for %s (%s)' % (name, hypothesis) print title roi.plot_sources(filename='%s/sources_%s.png' % (plotdir, extra), title=title, **smooth_kwargs) if all_energy(emin, emax): title = 'Band Source Map for %s (%s)' % (name, hypothesis) print title ROISourceBandPlotter( roi, bin_edges=one_bin_per_dec(emin, emax), title=title, **smooth_kwargs).show(filename='%s/band_source_%s.png' % (plotdir, extra)) title = 'Band Sources Map for %s (%s)' % (name, hypothesis) print title ROISourcesBandPlotter( roi, bin_edges=one_bin_per_dec(emin, emax), title=title, **smooth_kwargs).show(filename='%s/band_sources_%s.png' % (plotdir, extra))
def tsmap_plots(roi, name, hypothesis, datadir, plotdir, size, new_sources, tsmap_pixelsize=0.1, **common_kwargs): """ TS maps """ emin, emax = get_full_energy_range(roi) extra = '%s_%s_%sdeg' % (hypothesis, name, size) tsmap_kwargs = dict(size=size, pixelsize=tsmap_pixelsize, **common_kwargs) def _plot(filename, title): try: print 'Making Band %s TS Map (size=%s, pixelsize=%s)' % ( title, size, tsmap_pixelsize) roi.plot_tsmap( filename='%s/tsmap_%s_%s.png' % (plotdir, filename, extra), fitsfile='%s/tsmap_%s_%s.fits' % (datadir, filename, extra), title='%s TS Map for %s (%s)' % (title, name, hypothesis), **tsmap_kwargs) except Exception, ex: print 'ERROR making TS map:', ex traceback.print_exc(file=sys.stdout) if all_energy(emin, emax): try: print 'Making Band %s TS Map (size=%s, pixelsize=%s)' % ( title, size, tsmap_pixelsize) ROITSMapBandPlotter( roi, title='Band %s TS Map for %s (%s)' % (title, name, hypothesis), bin_edges=one_bin_per_dec(emin, emax), **tsmap_kwargs).show(filename='%s/band_tsmap_%s_%s.png' % (plotdir, filename, extra)) except Exception, ex: print 'ERROR making band TS map:', ex traceback.print_exc(file=sys.stdout)
def compute(self): roi = self.roi which = self.which roi.modify(which=which, model=self.model0, keep_old_flux=False) if not self.quiet: print 'fit source %s with model0 (%s)' % (which, self.model0.name) self.ll_0 = self.fit() if not self.quiet: print roi sed = PointlikeSED(roi, name=which) self.sed_points = sed.todict() # TODO, get SED points emin, emax = get_full_energy_range(roi) flux = self.model0.i_flux(emin=emin, emax=emax) self.model1.set_flux(flux, emin=emin, emax=emax) self.comprehensive_model = ComprehensiveModel(self.model0.copy(), self.model1.copy()) self.comprehensive_model.theta = 0.5 self.comprehensive_model_start = self.comprehensive_model.copy() if not self.quiet: print 'fit source %s with model1 (%s)' % (which, self.model1.name) roi.modify(which=self.which, model=self.comprehensive_model, keep_old_flux=False) self.ll_1 = self.fit() if not self.quiet: print roi self.TS_comp = 2 * (self.ll_1 - self.ll_0)
def smooth_plots(roi, name, hypothesis, datadir, plotdir, size, kernel_rad, **common_kwargs): """ smoothed counts maps """ emin, emax = get_full_energy_range(roi) extra='%s_%s_%sdeg_%sdeg' % (hypothesis, name, size,kernel_rad) smooth_kwargs = dict(which=name, override_center=roi.roi_dir, size=size, colorbar_radius=1, # most interesting within one degrees kernel_rad=kernel_rad, **common_kwargs) title = 'Source Map for %s (%s)' % (name,hypothesis) print title roi.plot_source(filename='%s/source_%s.png' % (plotdir, extra), title=title, **smooth_kwargs) title = 'Sources Map for %s (%s)' % (name,hypothesis) print title roi.plot_sources(filename='%s/sources_%s.png' % (plotdir, extra), title=title, **smooth_kwargs) if all_energy(emin,emax): title = 'Band Source Map for %s (%s)' % (name,hypothesis) print title ROISourceBandPlotter(roi, bin_edges=one_bin_per_dec(emin,emax), title=title, **smooth_kwargs).show(filename='%s/band_source_%s.png' % (plotdir,extra)) title = 'Band Sources Map for %s (%s)' % (name,hypothesis) print title ROISourcesBandPlotter(roi, bin_edges=one_bin_per_dec(emin,emax), title=title, **smooth_kwargs).show(filename='%s/band_sources_%s.png' % (plotdir,extra))
def gtlike_analysis(roi, name, hypothesis, max_free, seddir, datadir, plotdir, upper_limit=False, cutoff=False, cutoff_model=None, do_bandfitter=False, do_sed=False, ): print 'Performing Gtlike crosscheck for %s' % hypothesis frozen = freeze_far_away(roi, roi.get_source(name).skydir, max_free) gtlike=Gtlike(roi, extended_dir_name=datadir) unfreeze_far_away(roi, frozen) global like like=gtlike.like like.tol = 1e-1 # I found that the default tol '1e-3' would get the fitter stuck in infinite loops import pyLikelihood as pyLike like.setFitTolType(pyLike.ABSOLUTE) emin, emax = get_full_energy_range(like) print 'About to fit gtlike ROI' print summary(like, maxdist=10) paranoid_gtlike_fit(like, verbosity=4) print 'Done fiting gtlike ROI' print summary(like, maxdist=10) spectrum_name = like.logLike.getSource(name).spectrum().genericName() like.writeXml("%s/srcmodel_gtlike_%s_%s_%s.xml"%(datadir, hypothesis, spectrum_name, name)) r=source_dict(like, name) #upper_limit_kwargs=dict(delta_log_like_limits=10) upper_limit_kwargs=dict() if upper_limit: pul = GtlikePowerLawUpperLimit(like, name, emin=emin, emax=emax, cl=.95, upper_limit_kwargs=upper_limit_kwargs, verbosity=4, xml_name=join("%s/srcmodel_gtlike_%s_%s_%s.xml" % (datadir, hypothesis, 'PowerLaw_Upper_Limit', name))) r['powerlaw_upper_limit'] = pul.todict() cul = GtlikeCutoffUpperLimit(like, name, Index=1.7, Cutoff=3e3, b=1, cl=.95, upper_limit_kwargs=upper_limit_kwargs, verbosity=4, xml_name=join("%s/srcmodel_gtlike_%s_%s_%s.xml" % (datadir, hypothesis, 'PLSuperExpCutoff_Upper_Limit', name))) r['cutoff_upper_limit'] = cul.todict() if do_bandfitter: if all_energy(emin,emax): try: bf = GtlikeBandFitter(like, name, bin_edges=one_bin_per_dec(emin,emax), upper_limit_kwargs=upper_limit_kwargs, verbosity=4) bf.plot('%s/bandfits_gtlike_%s_%s.png' % (plotdir,hypothesis,name)) r['bandfits'] = bf.todict() except Exception, ex: print 'ERROR computing bandfit:', ex traceback.print_exc(file=sys.stdout)