def FitOnePixel(self,ra,dec,i,j) : """Run a evaluation of the pixel (i,j) corresponding to position (ra,dec)""" outXml = utils._dump_xml(self.config) folder = self.config['out'] _,Fit = GenAnalysisObjects(self.config,xmlfile=outXml) #get the Fit object src = GetSrc(Fit,ra,dec) # get the Source object at postion ra dec if self.config['TSMap']['RemoveTarget'] : # remove the target is asked Fit.deleteSource(self.config['target']['name']) if self.config['TSMap']['Re-Fit']: # reoptimze before is asked Fit.fit(0,optimizer=self.config['fitting']['optimizer']) for par in xrange(Fit.logLike.getNumParams()): # freeze all the source parameters Fit[par].setFree(0) Fit.addSource(src)# add a spurious source # dump the *new* xml file Fit.writeXml(self.tsfolder+"/model_"+str(ra)+"_"+str(dec)+".xml") # a new Fit object with the new xml file is needed. # just changing the position of the spurious source does not work _,TSFit = GenAnalysisObjects(self.config,xmlfile=self.tsfolder+"/model_"+str(ra)+"_"+str(dec)+".xml") #get the Fit object TSFit.fit(0,optimizer=self.config['fitting']['optimizer']) # save the result fsave = open(self._PixelFile(i,j),'w') fsave.write(str(ra)+"\t"+str(dec)+"\t"+ str(TSFit.Ts("Spurious"))+"\t"+ str(TSFit.logLike.value())) fsave.close()
def run(infile): """Run an entire Fermi analysis (spectrum) by reading a config file""" config = get_config(infile) folder = config['out'] os.system('mkdir -p ' + folder) FitRunner,Fit = GenAnalysisObjects(config) # create all the fit files and run gtlike FitRunner.PerformFit(Fit) Result = FitRunner.GetAndPrintResults(Fit)#Get and dump the target specific results if config['verbose'] == 'yes' : utils.GetFluxes(Fit,FitRunner.obs.Emin,FitRunner.obs.Emax) #print the flux of all the sources utils.DumpResult(Result, config) #plot the SED and model map if possible and asked if config['Spectrum']['ResultPlots'] == 'yes' : from enrico.constants import SpectrumPath os.system("mkdir -p "+config['out'] + '/'+SpectrumPath+'/') if float(config['UpperLimit']['TSlimit']) < Fit.Ts(config['target']['name']): FitRunner.ComputeSED(Fit) outXml = utils._dump_xml(config) if config['Spectrum']['SummedLike'] != 'yes': # the possiblity of making the model map is checked inside the function FitRunner.ModelMap(outXml) # Make energy bins by running a *new* analysis Nbin = config['Ebin']['NumEnergyBins'] energybin.RunEbin(folder,Nbin,Fit,FitRunner)
def PerformFit(self, Fit, writeXml = True): """Run gtlile tool. First it run gtlike with the DRNMGB optimizer and the user optimizer after. A dictionnay is return with all the releveant results""" self._log('gtlike', 'Run likelihood analysis') try: Fit.fit(1, optimizer="DRMNGB") #first try to run gtlike to approche the minimum except: pass #first try to run gtlike to approche the minimum # Now the precise fit will be done #change the fit tolerance to the one given by the user Fit.ftol = float(self.config['fitting']['ftol']) #fit with the user optimizer and ask gtlike to compute the covariance matrix self.log_like = Fit.fit(1,covar=True, optimizer=self.config['fitting']['optimizer']) #fit with the user optimizer and ask gtlike to compute the covariance matrix print Fit # remove source with TS<min_source_TS (default=1) # to be sure that MINUIT will converge try: self.config['fitting']['min_source_TS'] except KeyError: self.config['fitting']['min_source_TS'] = 1. self.RemoveWeakSources(Fit,\ self.config['fitting']['min_source_TS']) if writeXml : Fit.writeXml(utils._dump_xml(self.config)) self.success("Fit with gtlike preformed")
def FindSource(self): """Run the gtfindsrc tool""" outfile = utils._dump_findsrcout(self.Configuration) if (self.clobber == "no" and os.path.isfile(outfile)): #print("File exists and clobber is False") return (0) findsrc = GtApp('gtfindsrc', 'Likelihood') findsrc['evfile'] = self.mktimefile findsrc['scfile'] = self.ft2 if self.irfs != 'CALDB': findsrc['evtype'] = self.Configuration['event']['evtype'] else: findsrc['evtype'] = 'INDEF' findsrc['irfs'] = self.irfs findsrc['expcube'] = self.Cubename findsrc['expmap'] = self.Mapname findsrc['srcmdl'] = utils._dump_xml(self.Configuration) findsrc['coordsys'] = self.Configuration['space']['coordsys'] findsrc['target'] = self.srcname findsrc['optimizer'] = self.Configuration["fitting"]["optimizer"] findsrc['ftol'] = self.Configuration["fitting"]["ftol"] findsrc['clobber'] = self.clobber findsrc['reopt'] = self.Configuration["findsrc"]["Refit"] findsrc['outfile'] = outfile findsrc.run()
def PerformFit(self, Fit, writeXml = True): """Run gtlile tool. First it run gtlike with the DRNMGB optimizer and the user optimizer after. A dictionnay is return with all the releveant results""" self._log('gtlike', 'Run likelihood analysis') # TODO fix this part # try: # Fit.fit(0,covar=False, optimizer="DRMNGB") #first try to run gtlike to approche the minimum # except: # self.warning("First FIT did not converge with DRMNGB, trying DRMNFB") # try: # Fit.fit(0, optimizer="DRMNFB") # except: # self.warning("First FIT did not converge with DRMNFB either") # Now the precise fit will be done #change the fit tolerance to the one given by the user Fit.ftol = float(self.config['fitting']['ftol']) #fit with the user optimizer and ask gtlike to compute the covariance matrix self.log_like = Fit.fit(0,covar=True, optimizer=self.config['fitting']['optimizer']) #fit with the user optimizer and ask gtlike to compute the covariance matrix # remove source with TS<min_source_TS (default=1) # to be sure that MINUIT will converge try: self.config['fitting']['min_source_TS'] except KeyError: self.config['fitting']['min_source_TS'] = 1. self.RemoveWeakSources(Fit,\ self.config['fitting']['min_source_TS']) if writeXml : Fit.writeXml(utils._dump_xml(self.config)) self.success("Fit with gtlike preformed")
def run(infile): from enrico import utils from enrico import energybin from enrico.config import get_config """Run an entire Fermi analysis (spectrum) by reading a config file""" config = get_config(infile) folder = config['out'] utils.create_dir(folder) FitRunner,Fit = GenAnalysisObjects(config) # create all the fit files and run gtlike FitRunner.PerformFit(Fit) Result = FitRunner.GetAndPrintResults(Fit)#Get and dump the target specific results utils.DumpResult(Result, config) #plot the SED and model map if possible and asked if config['Spectrum']['ResultPlots'] == 'yes' : from enrico.constants import SpectrumPath utils.create_dir("%s/%s/" %(config['out'],SpectrumPath)) if float(config['UpperLimit']['TSlimit']) < Fit.Ts(config['target']['name']): FitRunner.ComputeSED(Fit) outXml = utils._dump_xml(config) if config['Spectrum']['SummedLike'] != 'yes': # the possiblity of making the model map is checked inside the function FitRunner.ModelMap(outXml) # Make energy bins by running a *new* analysis Nbin = config['Ebin']['NumEnergyBins'] energybin.RunEbin(folder,Nbin,Fit,FitRunner) del(Result) del(FitRunner)
def FindSource(self): """Run the gtfindsrc tool""" outfile = utils._dump_findsrcout(self.Configuration) if (self.clobber=="no" and os.path.isfile(outfile)): #print("File exists and clobber is False") return(0) findsrc = GtApp('gtfindsrc', 'Likelihood') findsrc['evfile'] = self.mktimefile findsrc['scfile'] = self.ft2 if self.irfs != 'CALDB': findsrc['evtype']= self.Configuration['event']['evtype'] else : findsrc['evtype']= 'INDEF' findsrc['irfs'] = self.irfs findsrc['expcube'] = self.Cubename findsrc['expmap'] = self.Mapname findsrc['srcmdl'] = utils._dump_xml( self.Configuration) findsrc['coordsys'] = self.Configuration['space']['coordsys'] findsrc['target'] = self.srcname findsrc['optimizer'] = self.Configuration["fitting"]["optimizer"] findsrc['ftol'] = self.Configuration["fitting"]["ftol"] findsrc['clobber'] = self.clobber findsrc['reopt'] = self.Configuration["findsrc"]["Refit"] findsrc['outfile'] = outfile findsrc.run()
def run(infile): """Run an entire Fermi analysis (spectrum) by reading a config file""" config = get_config(infile) folder = config['out'] os.system('mkdir -p ' + folder) FitRunner, Fit = GenAnalysisObjects(config) # create all the fit files and run gtlike FitRunner.PerformFit(Fit) Result = FitRunner.GetAndPrintResults( Fit) #Get and dump the target specific results if config['verbose'] == 'yes': utils.GetFluxes(Fit, FitRunner.obs.Emin, FitRunner.obs.Emax) #print the flux of all the sources utils.DumpResult(Result, config) #plot the SED and model map if possible and asked if config['Spectrum']['ResultPlots'] == 'yes': from enrico.constants import SpectrumPath os.system("mkdir -p " + config['out'] + '/' + SpectrumPath + '/') if float(config['UpperLimit']['TSlimit']) < Fit.Ts( config['target']['name']): FitRunner.ComputeSED(Fit) outXml = utils._dump_xml(config) if config['Spectrum'][ 'SummedLike'] != 'yes': # the possiblity of making the model map is checked inside the function FitRunner.ModelMap(outXml) # Make energy bins by running a *new* analysis Nbin = config['Ebin']['NumEnergyBins'] energybin.RunEbin(folder, Nbin, Fit, FitRunner)
def run(infile): from enrico import utils from enrico import energybin from enrico.config import get_config from enrico import Loggin mes = Loggin.Message() """Run an entire Fermi analysis (spectrum) by reading a config file""" config = get_config(infile) folder = config['out'] utils.create_dir(folder) FitRunner,Fit = GenAnalysisObjects(config) # create all the fit files and run gtlike FitRunner.PerformFit(Fit) #plot the SED and model map if possible and asked if float(config['UpperLimit']['TSlimit']) < Fit.Ts(config['target']['name']): if config['Spectrum']['ResultPlots'] == 'yes': from enrico.constants import SpectrumPath utils.create_dir("%s/%s/" %(config['out'],SpectrumPath)) sedresult = FitRunner.ComputeSED(Fit,dump=True) else: sedresult = FitRunner.ComputeSED(Fit,dump=False) # Update the energy scale to decorrelation energy mes.info('Setting the decorrelation energy as new Scale for the spectral parameters') spectrum = Fit[FitRunner.obs.srcname].funcs['Spectrum'] modeltype = spectrum.genericName() if Fit.model.srcs[FitRunner.obs.srcname].spectrum().genericName()=="PowerLaw": varscale = "Scale" if Fit.model.srcs[FitRunner.obs.srcname].spectrum().genericName()=="PowerLaw2": varscale = None elif Fit.model.srcs[FitRunner.obs.srcname].spectrum().genericName()=="PLSuperExpCutoff": varscale = "Scale" elif Fit.model.srcs[FitRunner.obs.srcname].spectrum().genericName()=="LogParabola": varscale = "Eb" elif Fit.model.srcs[FitRunner.obs.srcname].spectrum().genericName()=="BrokenPowerLaw": varscale = "Eb" if varscale is not None: spectrum.getParam(varscale).setValue(sedresult.decE) FitRunner.PerformFit(Fit) if config['Spectrum']['ResultPlots'] == 'yes' : outXml = utils._dump_xml(config) if config['Spectrum']['SummedLike'] != 'yes': # the possiblity of making the model map is checked inside the function FitRunner.ModelMap(outXml) #Get and dump the target specific results Result = FitRunner.GetAndPrintResults(Fit) utils.DumpResult(Result, config) # Make energy bins by running a *new* analysis Nbin = config['Ebin']['NumEnergyBins'] energybin.RunEbin(folder,Nbin,Fit,FitRunner) del(Result) del(FitRunner)
def FindSource(self): """Run the gtfindsrc tool""" findsrc = GtApp('gtfindsrc', 'Likelihood') findsrc['evfile'] = self.eventfile findsrc['scfile'] = self.ft2 findsrc['irfs'] = self.irfs findsrc['expcube'] = self.Cubename findsrc['expmap'] = self.Mapname findsrc['srcmdl'] = utils._dump_xml( self.Configuration) findsrc['coordsys'] = self.Configuration['space']['coordsys'] findsrc['target'] = self.srcname findsrc['optimizer'] = self.Configuration["fitting"]["optimizer"] findsrc['ftol'] = self.Configuration["fitting"]["ftol"] findsrc['clobber'] = self.clobber findsrc['reopt'] = self.Configuration["findsrc"]["Refit"] findsrc['outfile'] = utils._dump_findsrcout(self.Configuration) findsrc.run()
def PerformFit(self, Fit, writeXml = True): """Run gtlile tool. First it run gtlike with the DRNMGB optimizer and the user optimizer after. A dictionnay is return with all the releveant results""" self._log('gtlike', 'Run likelihood analysis') # TODO fix this part # Change the fit tolerance to the one given by the user Fit.ftol = float(self.config['fitting']['ftol']) # Fit with DRMNGB/DRMNFB (as recommended in gtlike fhelp) optimizer to # obtain initial parameters close to the minimum. Then switch optimizer. try: self.log_like = Fit.fit(verbosity=0, covar=False, optimizer='DRMNGB', optObject=None) except Exception as exc: self.warning('Exception while running DRMNGB, {0}'.format(str(exc))) try: self.log_like = Fit.fit(verbosity=0, covar=False, optimizer='DRMNFB', optObject=None) except: self.warning('Exception while running DRMNFB, {0}'.format(str(exc))) if self.config['verbose'] == 'yes' : print('Fit output with DRMNGB/DRMNFB: {0}'.format(self.log_like)) # 2nd precise fit with the user optimizer and ask gtlike to compute the covariance matrix self.log_like = Fit.fit(verbosity=0,covar=True, optimizer=self.config['fitting']['optimizer']) if self.config['verbose'] == 'yes' : print('Fit output with {1}: {0}'.format(self.log_like,self.config['fitting']['optimizer'])) # remove source with TS<min_source_TS (default=1) # to be sure that MINUIT will converge try: self.config['fitting']['min_source_TS'] except KeyError: self.config['fitting']['min_source_TS'] = 1. self.RemoveWeakSources(Fit,\ self.config['fitting']['min_source_TS']) if writeXml : Fit.writeXml(utils._dump_xml(self.config)) self.success("Fit with gtlike performed")
def FindSource(self): """Run the gtfindsrc tool""" findsrc = GtApp('gtfindsrc', 'Likelihood') findsrc['evfile'] = self.eventfile findsrc['scfile'] = self.ft2 if self.Configuration['event']['irfs'] != 'CALDB': findsrc['evtype'] = self.Configuration['event']['evtype'] else: findsrc['evtype'] = 'INDEF' findsrc['irfs'] = self.irfs findsrc['expcube'] = self.Cubename findsrc['expmap'] = self.Mapname findsrc['srcmdl'] = utils._dump_xml(self.Configuration) findsrc['coordsys'] = self.Configuration['space']['coordsys'] findsrc['target'] = self.srcname findsrc['optimizer'] = self.Configuration["fitting"]["optimizer"] findsrc['ftol'] = self.Configuration["fitting"]["ftol"] findsrc['clobber'] = self.clobber findsrc['reopt'] = self.Configuration["findsrc"]["Refit"] findsrc['outfile'] = utils._dump_findsrcout(self.Configuration) findsrc.run()
def PerformFit(self, Fit): """Run gtlile tool. First it run gtlike with the DRNMGB optimizer and the user optimizer after. A dictionnay is return with all the releveant results""" self._log('gtlike', 'Run likelihood analysis') try: Fit.fit(0) #first try to run gtlike to approche the minimum except: pass # Now the precise fit will be done #change the fit tolerance to the one given by the user Fit.ftol = float(self.config['fitting']['ftol']) #fit with the user optimizer and ask gtlike to compute the covariance matrix self.log_like = Fit.fit( 0, covar=True, optimizer=self.config['fitting']['optimizer'] ) #fit with the user optimizer and ask gtlike to compute the covariance matrix self.RemoveWeakSources( Fit) #remove source with TS<1 to be sure that MINUIT will converge Fit.writeXml(utils._dump_xml(self.config))
def run(infile): from enrico import utils from enrico import energybin from enrico.config import get_config from enrico import Loggin mes = Loggin.Message() """Run an entire Fermi analysis (spectrum) by reading a config file""" config = get_config(infile) folder = config['out'] utils.mkdir_p(folder) FitRunner, Fit = GenAnalysisObjects(config) # create all the fit files and run gtlike FitRunner.PerformFit(Fit) sedresult = None #plot the SED and model map if possible and asked if float(config['UpperLimit']['TSlimit']) < Fit.Ts( config['target']['name']): if config['Spectrum']['ResultPlots'] == 'yes': from enrico.constants import SpectrumPath utils.mkdir_p("%s/%s/" % (config['out'], SpectrumPath)) sedresult = FitRunner.ComputeSED(Fit, dump=True) else: sedresult = FitRunner.ComputeSED(Fit, dump=False) if (config['energy']['decorrelation_energy'] == 'yes'): #Update the energy scale to decorrelation energy mes.info( 'Setting the decorrelation energy as new Scale for the spectral parameters' ) spectrum = Fit[FitRunner.obs.srcname].funcs['Spectrum'] modeltype = spectrum.genericName() genericName = Fit.model.srcs[ FitRunner.obs.srcname].spectrum().genericName() varscale = None if genericName == "PowerLaw2": varscale = None elif genericName in [ "PowerLaw", "PLSuperExpCutoff", "EblAtten::PLSuperExpCutoff" ]: varscale = "Scale" elif genericName in ["LogParabola","EblAtten::LogParabola", \ "BrokenPowerLaw", "EblAtten::BrokenPowerLaw"]: varscale = "Eb" if varscale is not None: spectrum.getParam(varscale).setValue(sedresult.decE) FitRunner.PerformFit(Fit) #Get and dump the target specific results Result = FitRunner.GetAndPrintResults(Fit) utils.DumpResult(Result, config) # Make energy bins by running a *new* analysis Nbin = config['Ebin']['NumEnergyBins'] FitRunner.config['file']['parent_config'] = infile if config['Spectrum']['ResultParentPlots'] == "yes": plot_sed_fromconfig(get_config(config['file']['parent_config']), ignore_missing_bins=True) if config['Spectrum']['ResultPlots'] == 'yes': outXml = utils._dump_xml(config) # the possibility of making the model map is checked inside the function FitRunner.ModelMap(outXml) if Nbin > 0: FitRunner.config['Spectrum']['ResultParentPlots'] = "yes" plot_sed_fromconfig(get_config(infile), ignore_missing_bins=True) energybin.RunEbin(folder, Nbin, Fit, FitRunner, sedresult) del (sedresult) del (Result) del (FitRunner)