def ComputeUL(self, Fit): """Compute an Upper Limit using either the profil or integral method See the ST cicerone for more information on the 2 method""" self._log('UpperLimit', 'Compute upper Limit') #Index given by the user self.info("Assumed index is ", self.config['UpperLimit']['SpectralIndex']) IdGamma = utils.getParamIndx(Fit, self.obs.srcname, 'Index') Fit[IdGamma] = -self.config['UpperLimit']['SpectralIndex']#set the index Fit[IdGamma].setFree(0)#the variable index is frozen to compute the UL import scipy.stats cl = float(self.config['UpperLimit']['cl']) delta = 0.5*scipy.stats.chi2.isf(1-2*(cl-0.5), 1) print cl,' ',delta if self.config['UpperLimit']['Method'] == "Profile": #The method is Profile import UpperLimits ulobject = UpperLimits.UpperLimits(Fit) ul, _ = ulobject[self.obs.srcname].compute(emin=self.obs.Emin, emax=self.obs.Emax,delta=delta) #delta=2.71 / 2) self.info("Upper limit using Profile method: ") print ulobject[self.obs.srcname].results if self.config['UpperLimit']['Method'] == "Integral": #The method is Integral import IntegralUpperLimit ul, _ = IntegralUpperLimit.calc_int(Fit, self.obs.srcname, cl=cl, verbosity=0) print "Upper limit using Integral method: ", ul return ul #Return the result. This is an ul on the integral flux in ph/cm2/s
def EnvelopeUL(self, Fit): """Compute the envelope UL. An UL is computed for different index and the maximum is taken at each energy. This is usefull when the source index is not know or can not be constrain by theoritical argument The index range form 1.5 to 2.5""" import IntegralUpperLimit import UpperLimits self._log('EnvelopeUL', 'Compute upper limit envelope') PhIndex = Fit.par_index(self.obs.srcname, 'Index') Nbp = 20 #Make Nbp computations Npgraph = 100 #The graph has Npgraph points ener = np.logspace(np.log10(self.obs.Emin), np.log10(self.obs.Emax), Npgraph) #the array containing the energy Ulenv = np.array(Npgraph * [0.]) #the array containing the UL value for i in xrange(Nbp): indx = -1.5 - i / (Nbp - 1.) utils.FreezeParams(Fit, self.srcname, PhIndex, indx) #Use either the profile or the integral method self.info("Methode used: " + self.config['UpperLimit']['Method']) if self.config['UpperLimit']['Method'] == "Profile": ul = UpperLimits.UpperLimits(Fit) source_ul = ul[self.obs.srcname] ul_val, _ = source_ul.compute(emin=self.obs.Emin, emax=self.obs.Emax, delta=2.71 / 2) if self.config['UpperLimit']['Method'] == "Integral": ul_val, _ = IntegralUpperLimit.calc_int(Fit, self.obs.srcname, verbosity=0) self.success("Upper Limits calculated") print "Index = ", indx, " UL = ", ul_val #small print for j in xrange(Npgraph): model_name = Fit.model.srcs[ self.obs.srcname].spectrum().genericName() #compute the DNDE value. The computation change is #the model is PowerLaw or PowerLaw2 #Note : Other model are not taken into account #and no UL will be computed if model_name == 'PowerLaw2': newUl = ul_val * (indx + 1) * pow(ener[j], indx + 2) \ / (pow(self.obs.Emax, indx + 1) - pow(self.obs.Emin, indx + 1)) elif model_name == 'PowerLaw': IdEScale = utils.getParamIndx(Fit, self.obs.srcname, 'Scale') Escale = Fit[IdEScale].value() newUl = ul_val * pow(ener[j] / Escale, indx + 2) * Escale**2 * 1.6022e-6 Ulenv[j] = max(Ulenv[j], newUl) print self.info("Result of the UL envelope") for j in xrange(Npgraph): print ener[j], " ", Ulenv[j]
def EnvelopeUL(self, Fit): """Compute the envelope UL. An UL is computed for different index and the maximum is taken at each energy. This is usefull when the source index is not know or can not be constrain by theoritical argument The index range form 1.5 to 2.5""" import IntegralUpperLimit import UpperLimits self._log('EnvelopeUL', 'Compute upper limit envelope') PhIndex = Fit.par_index(self.obs.srcname, 'Index') Nbp = 20 #Make Nbp computations Npgraph = 100#The graph has Npgraph points ener = np.logspace(np.log10(self.obs.Emin), np.log10(self.obs.Emax), Npgraph)#the array containing the energy Ulenv = np.array(Npgraph * [0.])#the array containing the UL value for i in xrange(Nbp): indx = -1.5 - i / (Nbp - 1.) Fit[PhIndex] = indx Fit.freeze(PhIndex)#Freeze the index #Use either the profile or the integral method self.info("Methode used: "+self.config['UpperLimit']['Method']) if self.config['UpperLimit']['Method'] == "Profile": ul = UpperLimits.UpperLimits(Fit) source_ul = ul[self.obs.srcname] ul_val, _ = source_ul.compute(emin=self.obs.Emin, emax=self.obs.Emax, delta=2.71 / 2) if self.config['UpperLimit']['Method'] == "Integral": ul_val, _ = IntegralUpperLimit.calc_int(Fit, self.obs.srcname, verbosity=0) self.success("Upper Limits calculated") print "Index = ", indx, " UL = ", ul_val #small print for j in xrange(Npgraph): model_name = Fit.model.srcs[self.obs.srcname].spectrum().genericName() #compute the DNDE value. The computation change is #the model is PowerLaw or PowerLaw2 #Note : Other model are not taken into account #and no UL will be computed if model_name == 'PowerLaw2': newUl = ul_val * (indx + 1) * pow(ener[j], indx + 2) \ / (pow(self.obs.Emax, indx + 1) - pow(self.obs.Emin, indx + 1)) elif model_name == 'PowerLaw': IdEScale = utils.getParamIndx(Fit, self.obs.srcname, 'Scale') Escale = Fit[IdEScale].value() newUl = ul_val * pow(ener[j] / Escale, indx + 2)*Escale**2*1.6022e-6 Ulenv[j] = max(Ulenv[j], newUl) print self.info("Result of the UL envelope") for j in xrange(Npgraph): print ener[j], " ", Ulenv[j]
def CreateLikeObject(self): """Create an UnbinnedAnalysis or a BinnedAnalysis and retrun it.""" #create binnedAnalysis object if self.config['analysis']['likelihood'] == 'binned': Obs = BinnedObs(srcMaps=self.obs.srcMap, expCube=self.obs.Cubename, binnedExpMap=self.obs.BinnedMapfile, irfs=self.obs.irfs) Fit = BinnedAnalysis(Obs, self.obs.xmlfile, optimizer=self.config['fitting']['optimizer']) #create a unbinnedAnalysis object if self.config['analysis']['likelihood'] == 'unbinned': Obs = UnbinnedObs(self.obs.mktimefile, self.obs.ft2, expMap=self.obs.Mapname, expCube=self.obs.Cubename, irfs=self.obs.irfs) Fit = UnbinnedAnalysis( Obs, self.obs.xmlfile, optimizer=self.config['fitting']['optimizer']) # Fix this, EBL absorbed models use LogParabola with b=0 instead of PowerLaw, we may want to allow fixed shape for that case if float(self.config['Spectrum']['FrozenSpectralIndex'] > 0 ) and self.config['target']['spectrum'] == "PowerLaw": parameters = dict() parameters['Index'] = -float( self.config['Spectrum']['FrozenSpectralIndex']) # parameters['alpha'] = +float(self.config['Spectrum']['FrozenSpectralIndex']) # parameters['Index1'] = -float(self.config['Spectrum']['FrozenSpectralIndex']) # parameters['beta'] = 0 # parameters['Index2'] = 2. # parameters['Cutoff'] = 30000. # set the cutoff to be high for key in parameters.keys(): try: IdGamma = utils.getParamIndx(Fit, self.obs.srcname, key) Fit[IdGamma] = parameters[key] # set the parameter Fit[IdGamma].setFree( 0) #the variable index is frozen to compute the UL except: continue else: self.info("Freezing %s at %s"\ %(key,str(self.config['Spectrum']['FrozenSpectralIndex']))) return Fit #return the BinnedAnalysis or UnbinnedAnalysis object.
def CreateLikeObject(self): """Create an UnbinnedAnalysis or a BinnedAnalysis and retrun it.""" #create binnedAnalysis object if self.config['analysis']['likelihood'] == 'binned': use_edisp = self.config['analysis']['EnergyDispersion'] == 'yes' edisp_bins = -2 if use_edisp==True else 0 Obs = BinnedObs(srcMaps=self.obs.srcMap, expCube=self.obs.Cubename, binnedExpMap=self.obs.BinnedMapfile, irfs=self.obs.irfs) Cfg = BinnedConfig(use_edisp=use_edisp, edisp_bins=edisp_bins) Fit = BinnedAnalysis(Obs, self.obs.xmlfile, config=Cfg, optimizer=self.config['fitting']['optimizer']) Fit.setEnergyRange(self.obs.Emin,self.obs.Emax) print("Is edisp enabled? {0}".format(str(Fit.logLike.use_edisp()))) #create a unbinnedAnalysis object if self.config['analysis']['likelihood'] == 'unbinned': Obs = UnbinnedObs(self.obs.mktimefile, self.obs.ft2, expMap=self.obs.Mapname, expCube=self.obs.Cubename, irfs=self.obs.irfs) Fit = UnbinnedAnalysis(Obs, self.obs.xmlfile, optimizer=self.config['fitting']['optimizer']) # Fix this, EBL absorbed models use LogParabola with b=0 instead of PowerLaw, # we may want to allow fixed shape for that case if float(self.config['Spectrum']['FrozenSpectralIndex']!=0): parameters = dict() parameters['Index'] = -float(self.config['Spectrum']['FrozenSpectralIndex']) parameters['alpha'] = +float(self.config['Spectrum']['FrozenSpectralIndex']) parameters['Index1'] = -float(self.config['Spectrum']['FrozenSpectralIndex']) parameters['beta'] = 0 parameters['Index2'] = 2. parameters['Cutoff'] = 30000. # set the cutoff to be high for key in parameters.keys(): IdGamma = utils.getParamIndx(Fit, self.obs.srcname, key) if (IdGamma == -1): continue else: self.info("Freezing %s = %s" %(str(key),str(parameters[key]))) Fit[IdGamma] = parameters[key] # set the parameter Fit[IdGamma].setFree(False)#the variable index is frozen to compute the UL return Fit #return the BinnedAnalysis or UnbinnedAnalysis object.
def ComputeUL(self, Fit): """Compute an Upper Limit using either the profil or integral method See the ST cicerone for more information on the 2 method""" self._log('UpperLimit', 'Compute upper Limit') #Index given by the user self.info("Assumed index is "+str(self.config['UpperLimit']['SpectralIndex'])) IdGamma = utils.getParamIndx(Fit, self.obs.srcname, 'Index') Fit[IdGamma] = -self.config['UpperLimit']['SpectralIndex']#set the index Fit[IdGamma].setFree(0)#the variable index is frozen to compute the UL import scipy.stats cl = float(self.config['UpperLimit']['cl']) delta = 0.5*scipy.stats.chi2.isf(1-2*(cl-0.5), 1) if self.config['UpperLimit']['Method'] == "Profile": #The method is Profile if Fit.Ts(self.obs.srcname)<2 : self.warning("TS of the source is very low, better to use Integral method") import UpperLimits ulobject = UpperLimits.UpperLimits(Fit) ul, _ = ulobject[self.obs.srcname].compute(emin=self.obs.Emin, emax=self.obs.Emax,delta=delta) #delta=2.71 / 2) self.info("Upper limit using Profile method: ") print ulobject[self.obs.srcname].results self.warning("Be sure to have enough photons to validate the gaussian assumption") if self.config['UpperLimit']['Method'] == "Integral": #The method is Integral import IntegralUpperLimit ul, _ = IntegralUpperLimit.calc_int(Fit, self.obs.srcname, cl=cl, verbosity=0,emin=self.obs.Emin, emax=self.obs.Emax) print "Upper limit using Integral method: ", ul self.warning("Be sure to have enough photons to validate the gaussian assumption") if self.config['UpperLimit']['Method'] == "Poisson": #The method is Poisson ul = self.PoissonUL(Fit) print "Upper limit using Poisson statistic: ", ul print "This is an ul on the integral flux in ph/cm2/s" return ul #Return the result. This is an ul on the integral flux in ph/cm2/s
def CreateLikeObject(self): """Create an UnbinnedAnalysis or a BinnedAnalysis and retrun it.""" #create binnedAnalysis object if self.config['analysis']['likelihood'] == 'binned': Obs = BinnedObs(srcMaps=self.obs.srcMap, expCube=self.obs.Cubename, binnedExpMap=self.obs.BinnedMapfile, irfs=self.obs.irfs) Fit = BinnedAnalysis(Obs, self.obs.xmlfile, optimizer=self.config['fitting']['optimizer']) #create a unbinnedAnalysis object if self.config['analysis']['likelihood'] == 'unbinned': Obs = UnbinnedObs(self.obs.eventfile, self.obs.ft2, expMap=self.obs.Mapname, expCube=self.obs.Cubename, irfs=self.obs.irfs) Fit = UnbinnedAnalysis(Obs, self.obs.xmlfile, optimizer=self.config['fitting']['optimizer']) if float(self.config['Spectrum']['FrozenSpectralIndex']) != 0: parameters = dict() parameters['Index'] = -float(self.config['Spectrum']['FrozenSpectralIndex']) parameters['alpha'] = +float(self.config['Spectrum']['FrozenSpectralIndex']) parameters['Index1'] = -float(self.config['Spectrum']['FrozenSpectralIndex']) parameters['beta'] = 0 parameters['Index2'] = 2. parameters['Cutoff'] = 30000. # set the cutoff to be high for key in parameters.keys(): try: IdGamma = utils.getParamIndx(Fit, self.obs.srcname, key) Fit[IdGamma] = parameters[key] # set the parameter Fit[IdGamma].setFree(0)#the variable index is frozen to compute the UL except: continue else: self.info("Freezing %s at %s"\ %(key,str(self.config['Spectrum']['FrozenSpectralIndex']))) return Fit #return the BinnedAnalysis or UnbinnedAnalysis object.
def ComputeUL(self, Fit): """Compute an Upper Limit using either the profil or integral method See the ST cicerone for more information on the 2 method""" self._log('UpperLimit', 'Compute upper Limit') #Index given by the user print "Assumed index is ", self.config['UpperLimit']['SpectralIndex'] IdGamma = utils.getParamIndx(Fit, self.obs.srcname, 'Index') Fit[IdGamma] = -self.config['UpperLimit'][ 'SpectralIndex'] #set the index Fit[IdGamma].setFree( 0) #the variable index is frozen to compute the UL import scipy.stats cl = float(self.config['UpperLimit']['cl']) delta = 0.5 * scipy.stats.chi2.isf(1 - 2 * (cl - 0.5), 1) print cl, ' ', delta if self.config['UpperLimit'][ 'Method'] == "Profile": #The method is Profile import UpperLimits ulobject = UpperLimits.UpperLimits(Fit) ul, _ = ulobject[self.obs.srcname].compute(emin=self.obs.Emin, emax=self.obs.Emax, delta=delta) #delta=2.71 / 2) print "Upper limit using Profile method: " print ulobject[self.obs.srcname].results if self.config['UpperLimit'][ 'Method'] == "Integral": #The method is Integral import IntegralUpperLimit ul, _ = IntegralUpperLimit.calc_int(Fit, self.obs.srcname, cl=cl, verbosity=0) print "Upper limit using Integral method: ", ul return ul #Return the result. This is an ul on the integral flux in ph/cm2/s