def _compute(self): if self.verbosity: print 'calculating gtlike cutoff upper limit' like = self.like name = self.name saved_state = SuperState(like) old_flux = like.flux(name, self.emin, self.emax) cutoff_model = PLSuperExpCutoff(Index=self.Index, Cutoff=self.Cutoff, b=self.b) cutoff_model.set_flux(old_flux, emin=self.emin, emax=self.emax, strict=False) cutoff_model.set_default_limits(oomp_limits=True) cutoff_spectrum = build_gtlike_spectrum(cutoff_model) like.setSpectrum(name,cutoff_spectrum) like.syncSrcParams(name) super(GtlikeCutoffUpperLimit,self)._compute() saved_state.restore()
def _calculate(self): like = self.like name = self.name if self.verbosity: print 'Testing cutoff in gtlike' saved_state = SuperState(like) emin, emax = get_full_energy_range(like) self.results = d = dict( energy = energy_dict(emin=emin, emax=emax, energy_units=self.energy_units) ) try: def get_flux(): return like.flux(name, emin, emax) def spectrum(): source = like.logLike.getSource(name) s=source.spectrum() return spectrum_to_dict(s, errors=True) old_flux = get_flux() if spectrum()['name'] == 'PowerLaw': pass else: powerlaw_model=PowerLaw(norm=1e-11, index=2, e0=np.sqrt(emin*emax)) powerlaw_model.set_flux(old_flux,emin=emin,emax=emax) powerlaw_model.set_default_limits(oomp_limits=True) if self.verbosity: print 'powerlaw_model is',powerlaw_model powerlaw_spectrum=build_gtlike_spectrum(powerlaw_model) like.setSpectrum(name,powerlaw_spectrum) if self.verbosity: print 'About to fit powerlaw_spectrum' print summary(like) paranoid_gtlike_fit(like, verbosity=self.verbosity) if self.verbosity: print 'Done fitting powerlaw_spectrum' print summary(like) d['hypothesis_0'] = source_dict(like, name, emin=emin, emax=emax, flux_units=self.flux_units, energy_units=self.energy_units, verbosity=self.verbosity) if self.cutoff_model is None: self.cutoff_model=PLSuperExpCutoff(norm=1e-9, index=1, cutoff=1000, e0=1000, b=1) self.cutoff_model.set_free('b', False) self.cutoff_model.set_flux(old_flux,emin=emin,emax=emax) self.cutoff_model.set_default_limits(oomp_limits=True) if self.verbosity: print 'cutoff_model is',self.cutoff_model cutoff_spectrum=build_gtlike_spectrum(self.cutoff_model) like.setSpectrum(name,cutoff_spectrum) if self.verbosity: print 'About to fit cutoff_model' print summary(like) paranoid_gtlike_fit(like, verbosity=self.verbosity) ll = like.logLike.value() if ll < d['hypothesis_0']['logLikelihood']: # if fit is worse than PowerLaw fit, then # restart fit with parameters almost # equal to best fit powerlaw cutoff_plaw=PLSuperExpCutoff(b=1) cutoff_plaw.set_free('b', False) cutoff_plaw.setp_gtlike('norm', d['hypothesis_0']['spectrum']['Prefactor']) cutoff_plaw.setp_gtlike('index', d['hypothesis_0']['spectrum']['Index']) cutoff_plaw.setp_gtlike('e0', d['hypothesis_0']['spectrum']['Scale']) cutoff_plaw.setp_gtlike('cutoff', 1e6) cutoff_plaw.set_default_limits(oomp_limits=True) temp=build_gtlike_spectrum(cutoff_plaw) like.setSpectrum(name,temp) if self.verbosity: print 'Redoing fit with cutoff same as plaw' print summary(like) paranoid_gtlike_fit(like, verbosity=self.verbosity) if self.verbosity: print 'Done fitting cutoff_spectrum' print summary(like) d['hypothesis_1'] = source_dict(like, name, emin=emin, emax=emax, flux_units=self.flux_units, energy_units=self.energy_units, verbosity=self.verbosity) if self.cutoff_xml_name is not None: like.writeXml(self.cutoff_xml_name) d['TS_cutoff']=d['hypothesis_1']['TS']['reoptimize']-d['hypothesis_0']['TS']['reoptimize'] if self.verbosity: print 'For cutoff test, TS_cutoff = ', d['TS_cutoff'] except Exception, ex: print 'ERROR gtlike test cutoff: ', ex traceback.print_exc(file=sys.stdout) self.results = None
def _calculate(self): like = self.like name = self.name if self.verbosity: print 'Testing cutoff in gtlike' saved_state = SuperState(like) emin, emax = get_full_energy_range(like) self.results = d = dict(energy=energy_dict( emin=emin, emax=emax, energy_units=self.energy_units)) try: def get_flux(): return like.flux(name, emin, emax) def spectrum(): source = like.logLike.getSource(name) s = source.spectrum() return spectrum_to_dict(s, errors=True) old_flux = get_flux() if spectrum()['name'] == 'PowerLaw': pass else: powerlaw_model = PowerLaw(norm=1e-11, index=2, e0=np.sqrt(emin * emax)) powerlaw_model.set_flux(old_flux, emin=emin, emax=emax) powerlaw_model.set_default_limits(oomp_limits=True) if self.verbosity: print 'powerlaw_model is', powerlaw_model powerlaw_spectrum = build_gtlike_spectrum(powerlaw_model) like.setSpectrum(name, powerlaw_spectrum) if self.verbosity: print 'About to fit powerlaw_spectrum' print summary(like) paranoid_gtlike_fit(like, verbosity=self.verbosity) if self.verbosity: print 'Done fitting powerlaw_spectrum' print summary(like) d['hypothesis_0'] = source_dict(like, name, emin=emin, emax=emax, flux_units=self.flux_units, energy_units=self.energy_units, verbosity=self.verbosity) if self.cutoff_model is None: self.cutoff_model = PLSuperExpCutoff(norm=1e-9, index=1, cutoff=1000, e0=1000, b=1) self.cutoff_model.set_free('b', False) self.cutoff_model.set_flux(old_flux, emin=emin, emax=emax) self.cutoff_model.set_default_limits(oomp_limits=True) if self.verbosity: print 'cutoff_model is', self.cutoff_model cutoff_spectrum = build_gtlike_spectrum(self.cutoff_model) like.setSpectrum(name, cutoff_spectrum) if self.verbosity: print 'About to fit cutoff_model' print summary(like) paranoid_gtlike_fit(like, verbosity=self.verbosity) ll = like.logLike.value() if ll < d['hypothesis_0']['logLikelihood']: # if fit is worse than PowerLaw fit, then # restart fit with parameters almost # equal to best fit powerlaw cutoff_plaw = PLSuperExpCutoff(b=1) cutoff_plaw.set_free('b', False) cutoff_plaw.setp_gtlike( 'norm', d['hypothesis_0']['spectrum']['Prefactor']) cutoff_plaw.setp_gtlike('index', d['hypothesis_0']['spectrum']['Index']) cutoff_plaw.setp_gtlike('e0', d['hypothesis_0']['spectrum']['Scale']) cutoff_plaw.setp_gtlike('cutoff', 1e6) cutoff_plaw.set_default_limits(oomp_limits=True) temp = build_gtlike_spectrum(cutoff_plaw) like.setSpectrum(name, temp) if self.verbosity: print 'Redoing fit with cutoff same as plaw' print summary(like) paranoid_gtlike_fit(like, verbosity=self.verbosity) if self.verbosity: print 'Done fitting cutoff_spectrum' print summary(like) d['hypothesis_1'] = source_dict(like, name, emin=emin, emax=emax, flux_units=self.flux_units, energy_units=self.energy_units, verbosity=self.verbosity) if self.cutoff_xml_name is not None: like.writeXml(self.cutoff_xml_name) d['TS_cutoff'] = d['hypothesis_1']['TS']['reoptimize'] - d[ 'hypothesis_0']['TS']['reoptimize'] if self.verbosity: print 'For cutoff test, TS_cutoff = ', d['TS_cutoff'] except Exception, ex: print 'ERROR gtlike test cutoff: ', ex traceback.print_exc(file=sys.stdout) self.results = None