コード例 #1
0
    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]
コード例 #2
0
    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']))

        parameters = dict()
        parameters['Index']  = -float(self.config['UpperLimit']['SpectralIndex'])
        parameters['alpha']  = +float(self.config['UpperLimit']['SpectralIndex'])
        parameters['Index1'] = -float(self.config['UpperLimit']['SpectralIndex'])
        parameters['beta']   = 0
        parameters['Index2'] = 2.
        parameters['Cutoff'] = 30000. # set the cutoff to be high

        for key in parameters.keys():
            try:
                utils.FreezeParams(Fit,self.obs.srcname, key, parameters[key])
            except:
                continue

        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
コード例 #3
0
ファイル: lightcurve.py プロジェクト: AsymptoticBrain/enrico
    def VariabilityIndex(self):
        """Compute the variability index as in the 2FLG catalogue. (see Nolan et al, 2012)"""
        LcOutPath = self.LCfolder + self.config['target']['name']

        utils._log('Computing Variability index ')

        self.config['Spectrum']['FitsGeneration'] = 'no'

        try :
            ResultDicDC = utils.ReadResult(self.generalconfig)
        except :
            self.warning("No results file found; please run enrico_sed first.")
            return
        
        LogL1 = []
        LogL0 = []
        Time = []
        for i in xrange(self.Nbin):
            CurConfig = get_config(self.configfile[i])
            #Read the result. If it fails, it means that the bins has not bin computed. A warning message is printed
            try :
                ResultDic = utils.ReadResult(CurConfig)
            except :
                self._errorReading("Fail reading the config file ",i)
                continue

#            LogL1.append(ResultDic.get("log_like"))
            #Update the time and time error array
            Time.append((ResultDic.get("tmax")+ResultDic.get("tmin"))/2.)

            ##############################################################
            #   Compute the loglike value using the DC flux or prefactor
            ##############################################################
            # Create one obs instance
            CurConfig['Spectrum']['FitsGeneration'] = 'no'
            _,Fit = GenAnalysisObjects(CurConfig,verbose=0)#be quiet
            Fit.ftol = float(self.config['fitting']['ftol'])

            #Spectral index management!
            parameters = dict()
            parameters['Index']  = -2.
            parameters['alpha']  = +2.
            parameters['Index1'] = -2.
            parameters['beta']   = 0
            parameters['Index2'] = 2.
            parameters['Cutoff'] = 30000. # set the cutoff to be high

            for key in parameters.keys():
                try:
                    utils.FreezeParams(Fit, self.srcname, key, parameters[key])
                except:
                    continue

            LogL1.append(-Fit.fit(0,optimizer=CurConfig['fitting']['optimizer']))

            for key in ["norm","Prefactor","Integral"]:
                try:
                    utils.FreezeParams(Fit,self.srcname,key, utils.fluxNorm(ResultsDicDC[key]))
                except:
                    continue
            
            LogL0.append(-Fit.fit(0,optimizer=CurConfig['fitting']['optimizer']))

            del Fit #Clean memory


        plt.figure()
        plt.xlabel("Time")
        plt.ylabel("Log(Like) Variability")
        plt.errorbar(Time,LogL0,fmt='o',color='black',ls='None')

        plt.savefig(LcOutPath+"_VarIndex.png", dpi=150, facecolor='w', edgecolor='w',
                orientation='portrait', papertype=None, format=None,
                transparent=False, bbox_inches=None, pad_inches=0.1,
                frameon=None)

        self.info("Variability index calculation") 
        print "\t TSvar = ",2*(sum(LogL1)-sum(LogL0))
        print "\t NDF = ",len(LogL0)-1
        print "\t Chi2 prob = ",1 - chi2.cdf(2*(sum(LogL1)-sum(LogL0)),len(LogL0)-1)
        print 
コード例 #4
0
    def VariabilityIndex(self):
        """Compute the variability index as in the 2FGL catalogue. (see Nolan et al, 2012)"""
        LcOutPath = self.LCfolder + self.config['target']['name']

        utils._log('Computing Variability index ')

        self.config['Spectrum']['FitsGeneration'] = 'no'

        try :
            ResultDicDC = utils.ReadResult(self.generalconfig)
        except :
            self.warning("No results file found; please run enrico_sed first.")
            return

        LogL1 = []
        LogL0 = []
        Time = []
        for i in xrange(self.Nbin):
            CurConfig = get_config(self.configfile[i])
            #Read the result. If it fails, it means that the bins has not bin computed. A warning message is printed
            try :
                ResultDic = utils.ReadResult(CurConfig)
            except :
                self._errorReading("Fail reading the config file ",i)
                continue

#            LogL1.append(ResultDic.get("log_like"))
            #Update the time and time error array
            Time.append((ResultDic.get("tmax")+ResultDic.get("tmin"))/2.)

            ##############################################################
            #   Compute the loglike value using the DC flux or prefactor
            ##############################################################
            # Create one obs instance
            CurConfig['Spectrum']['FitsGeneration'] = 'no'
            _,Fit = GenAnalysisObjects(CurConfig,verbose=0)#be quiet
            Fit.ftol = float(self.config['fitting']['ftol'])

            #Spectral index management!
            parameters = dict()
            parameters['Index']  = -2.
            parameters['alpha']  = +2.
            parameters['Index1'] = -2.
            parameters['beta']   = 0
            parameters['Index2'] = 2.
            parameters['Cutoff'] = 100000. # set the cutoff to be high

            for key in parameters.keys():
                try:
                    utils.FreezeParams(Fit, self.srcname, key, parameters[key])
                except:
                    continue

            LogL1.append(-Fit.fit(0,optimizer=CurConfig['fitting']['optimizer']))

            for key in ["norm","Prefactor","Integral"]:
                try:
                    utils.FreezeParams(Fit,self.srcname,key, utils.fluxNorm(ResultsDicDC[key]))
                except:
                    continue

            LogL0.append(-Fit.fit(0,optimizer=CurConfig['fitting']['optimizer']))

            del Fit #Clean memory


        plt.figure()
        plt.xlabel("Time")
        plt.ylabel("Log(Like) Variability")
        plt.errorbar(Time,LogL0,fmt='o',color='black',ls='None')
        plt.xlim(xmin=max(plt.xlim()[0],1.02*min(Time)-0.02*max(Time)),
                 xmax=min(plt.xlim()[1],1.02*max(Time)-0.02*min(Time)))

        # Move the offset to the axis label
        ax = plt.gca()
        ax.get_yaxis().get_major_formatter().set_useOffset(False)
        offset_factor = int(np.mean(np.log10(np.abs(ax.get_ylim()))))
        if (offset_factor != 0):
            ax.set_yticklabels([float(round(k,5)) \
              for k in ax.get_yticks()*10**(-offset_factor)])
            ax.yaxis.set_label_text(ax.yaxis.get_label_text() +\
               r" [${\times 10^{%d}}$]" %offset_factor)

        # Secondary axis with MJD
        mjdaxis = ax.twiny()
        mjdaxis.set_xlim([utils.met_to_MJD(k) for k in ax.get_xlim()])
        mjdaxis.set_xlabel(r"Time (MJD)")
        mjdaxis.xaxis.set_major_formatter(matplotlib.ticker.ScalarFormatter(useOffset=False))
        plt.setp( mjdaxis.xaxis.get_majorticklabels(), rotation=15 )
        plt.tight_layout()

        plt.savefig(LcOutPath+"_VarIndex.png", dpi=150, facecolor='w', edgecolor='w',
                orientation='portrait', papertype=None, format=None,
                transparent=False, bbox_inches=None, pad_inches=0.1,
                frameon=None)

        self.info("Variability index calculation")
        print "\t TSvar = ",2*(sum(LogL1)-sum(LogL0))
        print "\t NDF = ",len(LogL0)-1
        print "\t Chi2 prob = ",1 - chi2.cdf(2*(sum(LogL1)-sum(LogL0)),len(LogL0)-1)
        print
コード例 #5
0
ファイル: lightcurve.py プロジェクト: prokoph/enrico
    def VariabilityIndex(self):
        """Compute the variability index as in the 2FLG catalogue. (see Nolan et al, 2012)"""
        LcOutPath = self.LCfolder + self.config['target']['name']

        utils._log('Computing Variability index ')

        self.config['Spectrum']['FitsGeneration'] = 'no'
        #        ValueDC = self.GetDCValue()
        ResultDicDC = utils.ReadResult(self.config)
        LogL1 = []
        LogL0 = []
        Time = []
        for i in xrange(self.Nbin):
            CurConfig = get_config(self.configfile[i])
            #Read the result. If it fails, it means that the bins has not bin computed. A warning message is printed
            try:
                ResultDic = utils.ReadResult(CurConfig)
            except:
                self._errorReading("fail reading the config file ", i)
                #                print "WARNING : fail reading the config file : ",CurConfig
                #                print "Job Number : ",i
                #                print "Please have a look at this job log file"
                continue


#            LogL1.append(ResultDic.get("log_like"))
#Update the time and time error array
            Time.append((ResultDic.get("tmax") + ResultDic.get("tmin")) / 2.)

            ##############################################################
            #   Compute the loglike value using the DC flux or prefactor
            ##############################################################
            # Create one obs instance
            CurConfig['Spectrum']['FitsGeneration'] = 'no'
            _, Fit = GenAnalysisObjects(CurConfig, verbose=0)  #be quiet
            Fit.ftol = float(self.config['fitting']['ftol'])

            #Spectral index management!
            self.info("Spectral index frozen to a value of 2")
            utils.FreezeParams(Fit, self.srcname, 'Index', -2)
            LogL1.append(
                -Fit.fit(0, optimizer=CurConfig['fitting']['optimizer']))

            Model_type = Fit.model.srcs[self.srcname].spectrum().genericName()
            if (Model_type == 'PowerLaw'):
                utils.FreezeParams(Fit, self.srcname, 'Prefactor',
                                   utils.fluxNorm(ResultDicDC['Prefactor']))
            if (Model_type == 'PowerLaw2'):
                utils.FreezeParams(Fit, self.srcname, 'Integral',
                                   utils.fluxNorm(ResultDicDC['Integral']))
            LogL0.append(
                -Fit.fit(0, optimizer=CurConfig['fitting']['optimizer']))

            del Fit  #Clean memory

        Can = _GetCanvas()
        TgrDC = ROOT.TGraph(len(Time), np.array(Time), np.array(LogL0))
        TgrDC.Draw("ALP*")
        TgrDC = ROOT.TGraph(len(Time), np.array(Time), np.array(LogL0))
        TgrDC.SetMarkerColor(2)
        TgrDC.Draw("PL*")
        #Save the canvas in the LightCurve subfolder
        Can.Print(LcOutPath + '_VarIndex.eps')
        Can.Print(LcOutPath + '_VarIndex.C')
        self.info("Variability index calculation")
        print "\t TSvar = ", 2 * (sum(LogL1) - sum(LogL0))
        print "\t NDF = ", len(LogL0) - 1
        print "\t Chi2 prob = ", ROOT.TMath.Prob(2 * (sum(LogL1) - sum(LogL0)),
                                                 len(LogL0) - 1)
        print
コード例 #6
0
    def VariabilityIndex(self):
        """Compute the variability index as in the 2FLG catalogue. (see Nolan et al, 2012)"""
        LcOutPath = self.LCfolder + self.config['target']['name']

        utils._log('Computing Variability index ')

        self.config['Spectrum']['FitsGeneration'] = 'no'

        try:
            ResultDicDC = utils.ReadResult(self.generalconfig)
        except:
            self.warning("No results file found; please run enrico_sed first.")
            return

        LogL1 = []
        LogL0 = []
        Time = []
        for i in xrange(self.Nbin):
            CurConfig = get_config(self.configfile[i])
            #Read the result. If it fails, it means that the bins has not bin computed. A warning message is printed
            try:
                ResultDic = utils.ReadResult(CurConfig)
            except:
                self._errorReading("fail reading the config file ", i)
                #                print "WARNING : fail reading the config file : ",CurConfig
                #                print "Job Number : ",i
                #                print "Please have a look at this job log file"
                continue


#            LogL1.append(ResultDic.get("log_like"))
#Update the time and time error array
            Time.append((ResultDic.get("tmax") + ResultDic.get("tmin")) / 2.)

            ##############################################################
            #   Compute the loglike value using the DC flux or prefactor
            ##############################################################
            # Create one obs instance
            CurConfig['Spectrum']['FitsGeneration'] = 'no'
            _, Fit = GenAnalysisObjects(CurConfig, verbose=0)  #be quiet
            Fit.ftol = float(self.config['fitting']['ftol'])

            #Spectral index management!
            parameters = dict()
            parameters['Index'] = -2.
            parameters['alpha'] = +2.
            parameters['Index1'] = -2.
            parameters['beta'] = 0
            parameters['Index2'] = 2.
            parameters['Cutoff'] = 30000.  # set the cutoff to be high

            for key in parameters.keys():
                try:
                    utils.FreezeParams(Fit, self.srcname, key, parameters[key])
                except:
                    continue

            LogL1.append(
                -Fit.fit(0, optimizer=CurConfig['fitting']['optimizer']))

            for key in ["norm", "Prefactor", "Integral"]:
                try:
                    utils.FreezeParams(Fit, self.srcname, key,
                                       utils.fluxNorm(ResultsDicDC[key]))
                except:
                    continue

            LogL0.append(
                -Fit.fit(0, optimizer=CurConfig['fitting']['optimizer']))

            del Fit  #Clean memory

        Can = _GetCanvas()
        TgrDC = ROOT.TGraph(len(Time), np.array(Time), np.array(LogL0))
        TgrDC.Draw("ALP*")
        TgrDC = ROOT.TGraph(len(Time), np.array(Time), np.array(LogL0))
        TgrDC.SetMarkerColor(2)
        TgrDC.Draw("PL*")
        #Save the canvas in the LightCurve subfolder
        Can.Print(LcOutPath + '_VarIndex.eps')
        Can.Print(LcOutPath + '_VarIndex.C')
        self.info("Variability index calculation")
        print "\t TSvar = ", 2 * (sum(LogL1) - sum(LogL0))
        print "\t NDF = ", len(LogL0) - 1
        print "\t Chi2 prob = ", ROOT.TMath.Prob(2 * (sum(LogL1) - sum(LogL0)),
                                                 len(LogL0) - 1)
        print