def variability_followup(self, hypothesis): name = self.name roi = self.reload_roi(hypothesis) modify = import_module(self.modify) good_interval = modify.get_variability_time_cuts(name) nbins = 36 if good_interval is not None: ft1files = roi.sa.pixeldata.ft1files earliest_time, latest_time = CombinedVariabilityTester.get_time_range( ft1files) bins = b = np.round( np.linspace(earliest_time, latest_time, nbins + 1)).astype(int) starts = b[:-1] stops = b[1:] print 'Initial binning:' print ' * starts=', starts print ' * stops=', stops starts,stops = zip(*[(start,stop) \ for (start,stop) in zip(starts,stops) \ if good_interval(start,stop)]) print 'Initial binning:' print ' * starts=', starts print ' * stops=', stops kwargs = dict(tstarts=starts, tstops=stops) else: kwargs = dict(nbins=nbins) frozen = freeze_far_away(roi, roi.get_source(name).skydir, self.max_free) v = CombinedVariabilityTester(roi, name, use_pointlike_ltcube=True, refit_background=True, refit_other_sources=True, verbosity=4, **kwargs) unfreeze_far_away(roi, frozen) results = v.todict() savedict(results, 'results_%s_variability_%s.yaml' % (name, hypothesis)) try: v.plot(filename='plots/variability_%s_hypothesis_%s.pdf' % (name, hypothesis)) except Exception, ex: print 'ERROR plotting variability tester:', ex traceback.print_exc(file=sys.stdout)
def variability_followup(self, hypothesis): name = self.name roi = self.reload_roi(hypothesis) modify = import_module(self.modify) good_interval = modify.get_variability_time_cuts(name) nbins=36 if good_interval is not None: ft1files=roi.sa.pixeldata.ft1files earliest_time, latest_time = CombinedVariabilityTester.get_time_range(ft1files) bins = b = np.round(np.linspace(earliest_time, latest_time, nbins+1)).astype(int) starts = b[:-1] stops = b[1:] print 'Initial binning:' print ' * starts=',starts print ' * stops=',stops starts,stops = zip(*[(start,stop) \ for (start,stop) in zip(starts,stops) \ if good_interval(start,stop)]) print 'Initial binning:' print ' * starts=',starts print ' * stops=',stops kwargs=dict(tstarts=starts, tstops=stops) else: kwargs=dict(nbins=nbins) frozen = freeze_far_away(roi, roi.get_source(name).skydir, self.max_free) v = CombinedVariabilityTester(roi,name, use_pointlike_ltcube=True, refit_background=True, refit_other_sources=True, verbosity=4, **kwargs) unfreeze_far_away(roi, frozen) results = v.todict() savedict(results,'results_%s_variability_%s.yaml' % (name,hypothesis)) try: v.plot(filename='plots/variability_%s_hypothesis_%s.pdf' % (name,hypothesis)) except Exception, ex: print 'ERROR plotting variability tester:', ex traceback.print_exc(file=sys.stdout)
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)
else: print 'Override Localization for source %s:', name print 'override_localization=',override_localization roi.modify(which=None, skydir=override_localization['init_position']) assert override_localization['method'] == 'MinuitLocalizer' m=MinuitLocalizer(roi, name, verbosity=4) print 'Localization Ellipse:',m.todict() if fit_extension: roi.fit_extension(which=name) ellipse = paranoid_localize(roi, name) print ellipse unfreeze_far_away(roi, frozen) fit() print 'Making pointlike SED for hypothesis %s' % hypothesis sed = PointlikeSED(roi, name, verbosity=4) sed.save('%s/sed_pointlike_4bpd_%s_%s.yaml' % (seddir,hypothesis,name)) sed.plot('%s/sed_pointlike_4bpd_%s_%s.png' % (seddir,hypothesis,name)) print_summary() p = source_dict(roi, name) pul = PointlikePowerLawUpperLimit(roi, name, emin=emin, emax=emax, cl=.95, verbosity=4) p['powerlaw_upper_limit']=pul.todict() cul = PointlikeCutoffUpperLimit(roi, name, Index=1.7, Cutoff=3e3, b=1, cl=.95, verbosity=4)