示例#1
0
def Analysis(folder,
             config,
             configgeneric=None,
             tag="",
             convtyp='-1',
             verbose=1):
    import os
    from enrico import utils
    from enrico.gtfunction import Observation
    from enrico.fitmaker import FitMaker
    import Loggin

    mes = Loggin.Message()
    """ run an analysis"""
    Obs = Observation(folder, config, tag=tag)
    if verbose:
        utils._log('SUMMARY: ' + tag)
        Obs.printSum()

    FitRunner = FitMaker(Obs, config)  ##Class
    if config['Spectrum']['FitsGeneration'] == 'yes':
        FitRunner.FirstSelection(
            configgeneric)  #Generates fits files for the coarse selection
        FitRunner.GenerateFits(
        )  #Generates fits files for the rest of the products
    return FitRunner
示例#2
0
def Analysis(folder, config, tag="", convtyp='-1', verbose = 1):
    """ run an analysis"""
    Obs = Observation(folder, config, convtyp, tag=tag)
    if verbose:
        utils._log('SUMMARY: ' + tag)
        Obs.printSum()
    FitRunner = FitMaker(Obs, config)##Class
    if config['Spectrum']['FitsGeneration'] == 'yes':
        FitRunner.GenerateFits() #Generates fits files
    return FitRunner
示例#3
0
def Analysis(folder, config, tag="", convtyp='-1', verbose=1):
    """ run an analysis"""
    Obs = Observation(folder, config, convtyp, tag=tag)
    if verbose:
        utils._log('SUMMARY: ' + tag)
        Obs.printSum()
    FitRunner = FitMaker(Obs, config)  ##Class
    if config['Spectrum']['FitsGeneration'] == 'yes':
        FitRunner.GenerateFits()  #Generates fits files
    return FitRunner
示例#4
0
def Runsrcprob(config):
    config['space']['rad'] = config['srcprob']['rad']
    Obs = Observation(config['out'], config, tag="srcprob")
    _log("Summary")
    Obs.printSum()
    if config['srcprob']['FitsGeneration'] =='yes':
        _log("Select data")
        Obs.FirstCut()
        if config['analysis']['ComputeDiffrsp'] == 'yes':
            _log("Compute diffuse responses")
            Obs.DiffResps()
    _log("Run gtsrcprob")
    Obs.SrcProb()
    probfile=fits.open(Obs.Probfile)
    srclist = open(config['srcprob']['srclist'],"r").readlines()
    for src in srclist:
      proba = probfile[1].data.field(string.split(src)[0])
      energy = probfile[1].data.field("ENERGY")
      ra = probfile[1].data.field("RA")
      dec = probfile[1].data.field("DEC")
      time = probfile[1].data.field("TIME")
      indices = energy.argsort()
      mes = Loggin.Message()
      mes.info( "Results sorted by decreasing energy")
      Print(indices,config,ra,dec,proba,energy,time)
      print 
      mes.info( "Results sorted by decreasing probability")
      indices = proba.argsort()
      Print(indices,config,ra,dec,proba,energy,time)
示例#5
0
def Analysis(folder, config, configgeneric=None, tag="", convtyp='-1', verbose = 1):

    mes = Loggin.Message()
    """ run an analysis"""
    Obs = Observation(folder, config, tag=tag)
    if verbose:
        utils._log('SUMMARY: ' + tag)
        Obs.printSum()

    FitRunner = FitMaker(Obs, config)##Class
    if config['Spectrum']['FitsGeneration'] == 'yes':
        FitRunner.FirstSelection(configgeneric) #Generates fits files for the coarse selection
        FitRunner.GenerateFits() #Generates fits files for the rest of the products
    return FitRunner
示例#6
0
def Analysis(folder, config, configgeneric=None, tag="", convtyp='-1', verbose = 1):

    mes = Loggin.Message()
    """ run an analysis"""
    # If there are no xml files, create it and print a warning <--- This should be here?
    #if len(glob.glob(config['file']['xml'].replace('.xml','*.xml')))==0:
    if len(glob.glob(config['file']['xml']))==0: #.replace('.xml','*.xml')))==0:
        mes.warning("Xml not found, creating one for the given config %s" %config['file']['xml'])
        XmlMaker(config)
    
    Obs = Observation(folder, config, tag=tag)
    if verbose:
        utils._log('SUMMARY: ' + tag)
        Obs.printSum()

    FitRunner = FitMaker(Obs, config)##Class
    if config['Spectrum']['FitsGeneration'] == 'yes':
        FitRunner.FirstSelection(configgeneric) #Generates fits files for the coarse selection
        FitRunner.GenerateFits() #Generates fits files for the rest of the products
    return FitRunner
示例#7
0
def FindSrc(infile):
    config = get_config(infile)
    folder = config['out']

    Obs = Observation(folder, config)
    utils._log('SUMMARY: ')
    Obs.printSum()

    FitRunner = FitMaker(Obs, config)

    if config["findsrc"]["FitsGeneration"]== "yes":
        config['analysis']['likelihood'] = 'unbinned'
        FitRunner.GenerateFits()

    FitRunner._log('gtfindsrc', 'Optimize source position')
    os.system("rm "+utils._dump_findsrcout(config))
    Obs.FindSource()
    try:
      update_reg(config)
    except:
      pass
示例#8
0
def PrepareEbin(Fit, FitRunner, sedresult=None):
    """ Prepare the computation of spectral point in energy bins by
    i) removing the weak sources (TS<1) # not true
    ii) updating the config file (option and energy)
    and save it in a new ascii file
    iii) changing the spectral model and saving it in a new xml file.
    A list of the ascii files is returned"""

    mes = Loggin.Message()

    NEbin = int(FitRunner.config['Ebin']['NumEnergyBins'])

    config = FitRunner.config

    config['verbose'] = 'no'  #Be quiet

    #Replace the evt file with the fits file produced before
    #in order to speed up the production of the fits files
    config['file']['event'] = FitRunner.obs.eventcoarse
    #update the config to allow the fit in energy bins
    config['UpperLimit']['envelope'] = 'no'
    config['Ebin']['NumEnergyBins'] = '0'  #no new bin in energy!
    config['target']['redshift'] = '0'  #Disable EBL correction
    config['out'] = FitRunner.config['out'] + '/' + EbinPath + str(NEbin)
    config['Spectrum']['ResultPlots'] = 'no'  #no SED plot/modelmap
    #copy the chose of the user for the enery bin computing
    config['Spectrum']['FitsGeneration'] = config['Ebin']['FitsGeneration']
    config['UpperLimit']['TSlimit'] = config['Ebin']['TSEnergyBins']
    tag = FitRunner.config['file']['tag']
    lEmax = np.log10(float(FitRunner.config['energy']['emax']))
    lEmin = np.log10(float(FitRunner.config['energy']['emin']))
    utils._log("Preparing submission of fit into energy bins")
    print(" Emin = ", float(FitRunner.config['energy']['emin']), " Emax = ",
          float(FitRunner.config['energy']['emax']), " Nbins = ", NEbin)

    if config['Ebin']['DistEbins'] in ['TS', 'mix'] and sedresult != None:
        # Make the bins equispaced in sum(SED/SEDerr) - using the butterfly
        ipo = 0
        iTS = sedresult.SED / sedresult.Err
        TScumula = 0
        TSperbin = 1. * sum(iTS) / NEbin
        ener = [10**lEmin]
        while ipo < len(sedresult.E) - 1:
            TScumula += iTS[ipo]
            if TScumula / TSperbin > 1:
                ener.append(sedresult.E[ipo])
                TScumula -= TSperbin
            ipo += 1
        ener.append(10**lEmax)
        ener = np.array(ener)
        # intermediate approach (between both TS-spaced and logE spaced)
        if config['Ebin']['DistEbins'] == 'mix':
            ener = 0.5 * (ener + np.logspace(lEmin, lEmax, NEbin + 1))
    else:
        # Make the bins equispaced in logE (standard)
        ener = np.logspace(lEmin, lEmax, NEbin + 1)

    os.system("mkdir -p " + config['out'])
    paramsfile = []

    srcname = FitRunner.config['target']['name']
    if config['UpperLimit']['TSlimit'] > Fit.Ts(srcname):
        utils._log('Re-optimize', False)
        print "An upper limit has been computed. The fit need to be re-optmized"
        Fit.optimize(0)

    Pref = utils.ApproxPref(Fit, ener, srcname)
    Gamma = utils.ApproxGamma(Fit, ener, srcname)

    Model_type = Fit.model.srcs[srcname].spectrum().genericName()
    # if the model is not PowerLaw : change the model
    if not (Model_type == 'PowerLaw'):
        for comp in Fit.components:
            comp.logLike.getSource(srcname).setSpectrum(
                "PowerLaw")  #Change model
        config['target']['spectrum'] = "PowerLaw"

    xmltag_list = [""]  #handle summed like analysis
    if config['ComponentAnalysis']['FrontBack'] == "yes":
        xmltag_list = ["_FRONT", "_BACK"]
        mes.info("Splitting Front/Back events")
    elif config['ComponentAnalysis']['PSF'] == "yes":
        xmltag_list = ["_PSF0", "_PSF1", "_PSF2"]
        mes.info("Splitting PSF events")
    elif config['ComponentAnalysis']['EDISP'] == "yes":
        xmltag_list = ["_EDISP0", "_EDISP1", "_EDISP2", "_EDISP3"]
        mes.info("Splitting EDISP events")

    for ibin in xrange(NEbin):  #Loop over the energy bins
        E = utils.GetE0(ener[ibin + 1], ener[ibin])
        mes.info("Submitting # " + str(ibin) + " at energy " + str(E))
        #Update the model for the bin
        for comp, xmltag in zip(Fit.components, xmltag_list):
            NewFitObject = ChangeModel(comp, ener[ibin], ener[ibin + 1],
                                       srcname, Pref[ibin], Gamma[ibin])
            Xmlname = (config['out'] + "/" + srcname + "_" + str(ibin) +
                       xmltag + ".xml")

            NewFitObject.writeXml(Xmlname)  # dump the corresponding xml file
            config['file']['xml'] = Xmlname.replace(xmltag, "")
        #update the energy bounds
        config['energy']['emin'] = str(ener[ibin])
        config['energy']['emax'] = str(ener[ibin + 1])
        config['energy']['decorrelation_energy'] = "no"
        # Change the spectral index to follow the Estimated Gamma
        # if approximated Gamma is outside of bounds set it to limit
        Gamma_min = -5
        Gamma_max = 0.5
        config['UpperLimit']['SpectralIndex'] = -min(
            max(Gamma_min, Gamma[ibin]), Gamma_max)

        config['file']['tag'] = tag + '_Ebin' + str(NEbin) + '_' + str(ibin)
        filename = config['target']['name'] + "_" + str(ibin) + ".conf"
        paramsfile.append(filename)
        config.write(open(config['out'] + '/' + paramsfile[ibin],
                          'w'))  #save the config file in a ascii file

    return paramsfile
示例#9
0
文件: lightcurve.py 项目: zblz/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 :
                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!
            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')
        print 
        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 
示例#10
0
文件: energybin.py 项目: zblz/enrico
def PrepareEbin(Fit, FitRunner):
    """ Prepare the computation of spectral point in energy bins by
    i) removing the weak sources (TS<1) # not true
    ii) updating the config file (option and energy) 
    and save it in a new ascii file
    iii) changing the spectral model and saving it in a new xml file.
    A list of the ascii files is returned"""

    NEbin = int(FitRunner.config['Ebin']['NumEnergyBins'])

    config = FitRunner.config

    config['verbose'] ='no' #Be quiet
    #Replace the evt file with the fits file produced before
    #in order to speed up the production of the fits files
    config['file']['event'] = FitRunner.obs.eventfile
    #update the config to allow the fit in energy bins
    config['UpperLimit']['envelope'] = 'no' 
    config['Ebin']['NumEnergyBins'] = '0'#no new bin in energy!
    config['out'] = FitRunner.config['out'] + '/'+EbinPath + str(NEbin)
    config['Spectrum']['ResultPlots'] = 'no' #no SED plot/modelmap
    #copy the chose of the user for the enery bin computing
    config['Spectrum']['FitsGeneration'] = config['Ebin']['FitsGeneration'] 
    config['UpperLimit']['TSlimit'] = config['Ebin']['TSEnergyBins']
    tag = FitRunner.config['file']['tag']
    lEmax = np.log10(float(FitRunner.config['energy']['emax']))
    lEmin = np.log10(float(FitRunner.config['energy']['emin']))
    utils._log("Preparing submission of fit into energy bins")
    print("Emin = ", float(FitRunner.config['energy']['emin']),
          " Emax = ", float(FitRunner.config['energy']['emax']),
          " Nbins = ", NEbin)

    ener = np.logspace(lEmin, lEmax, NEbin + 1)
    os.system("mkdir -p " + config['out'])
    paramsfile = []

    srcname = FitRunner.config['target']['name']
    if config['UpperLimit']['TSlimit']>Fit.Ts(srcname) :
        utils._log('Re-optimize', False)
        print "An upper limit has been computed. The fit need to be re-optmized"
        Fit.optimize(0)

#    utils.RemoveWeakSources(Fit,srcname)#remove source with TS<1 to be sure that MINUIT will converge

    Pref = utils.ApproxPref(Fit, ener, srcname)
    Gamma = utils.ApproxGamma(Fit, ener, srcname)

    Model_type = Fit.model.srcs[srcname].spectrum().genericName()
    # if the model is not PowerLaw : change the model
    if not(Model_type == 'PowerLaw') :
      Fit.logLike.getSource(srcname).setSpectrum("PowerLaw") #Change model

    for ibin in xrange(NEbin):#Loop over the energy bins
        E = utils.GetE0(ener[ibin + 1],ener[ibin])
        print "Submition # ", ibin, " at energy ", E
        #Update the model for the bin
        NewFitObject = ChangeModel(Fit, ener[ibin], ener[ibin + 1], srcname, Pref[ibin] ,Gamma[ibin])
        Xmlname = (config['out'] + "/" + srcname +
                    "_" + str(ibin) + ".xml")
        NewFitObject.writeXml(Xmlname)# dump the corresponding xml file
        config['file']['xml'] = Xmlname
        #update the energy bounds
        config['energy']['emin'] = str(ener[ibin])
        config['energy']['emax'] = str(ener[ibin + 1])

        config['file']['tag'] = tag + '_Ebin' + str(NEbin) + '_' + str(ibin)
        filename =  config['target']['name'] + "_" + str(ibin) + ".conf"
        paramsfile.append(filename)
        config.write(open(config['out'] + '/' +paramsfile[ibin], 'w')) #save the config file in a ascii file

    return paramsfile
示例#11
0
def PrepareEbin(Fit, FitRunner):
    """ Prepare the computation of spectral point in energy bins by
    i) removing the weak sources (TS<1) # not true
    ii) updating the config file (option and energy)
    and save it in a new ascii file
    iii) changing the spectral model and saving it in a new xml file.
    A list of the ascii files is returned"""

    NEbin = int(FitRunner.config['Ebin']['NumEnergyBins'])

    config = FitRunner.config

    config['verbose'] = 'no'  #Be quiet
    #Replace the evt file with the fits file produced before
    #in order to speed up the production of the fits files
    config['file']['event'] = FitRunner.obs.eventcoarse
    #update the config to allow the fit in energy bins
    config['UpperLimit']['envelope'] = 'no'
    config['Ebin']['NumEnergyBins'] = '0'  #no new bin in energy!
    config['target']['redshift'] = '0'  #Disable EBL correction
    config['out'] = FitRunner.config['out'] + '/' + EbinPath + str(NEbin)
    config['Spectrum']['ResultPlots'] = 'no'  #no SED plot/modelmap
    #copy the chose of the user for the enery bin computing
    config['Spectrum']['FitsGeneration'] = config['Ebin']['FitsGeneration']
    config['UpperLimit']['TSlimit'] = config['Ebin']['TSEnergyBins']
    tag = FitRunner.config['file']['tag']
    lEmax = np.log10(float(FitRunner.config['energy']['emax']))
    lEmin = np.log10(float(FitRunner.config['energy']['emin']))
    utils._log("Preparing submission of fit into energy bins")
    print(" Emin = ", float(FitRunner.config['energy']['emin']), " Emax = ",
          float(FitRunner.config['energy']['emax']), " Nbins = ", NEbin)

    ener = np.logspace(lEmin, lEmax, NEbin + 1)
    os.system("mkdir -p " + config['out'])
    paramsfile = []

    srcname = FitRunner.config['target']['name']
    if config['UpperLimit']['TSlimit'] > Fit.Ts(srcname):
        utils._log('Re-optimize', False)
        print "An upper limit has been computed. The fit need to be re-optmized"
        Fit.optimize(0)

    Pref = utils.ApproxPref(Fit, ener, srcname)
    Gamma = utils.ApproxGamma(Fit, ener, srcname)

    Model_type = Fit.model.srcs[srcname].spectrum().genericName()
    # if the model is not PowerLaw : change the model
    if not (Model_type == 'PowerLaw'):
        for comp in Fit.components:
            comp.logLike.getSource(srcname).setSpectrum(
                "PowerLaw")  #Change model
        config['target']['spectrum'] = "PowerLaw"

    for ibin in xrange(NEbin):  #Loop over the energy bins
        E = utils.GetE0(ener[ibin + 1], ener[ibin])
        from enrico import Loggin
        mes = Loggin.Message()
        mes.info("Submition # " + str(ibin) + " at energy " + str(E))
        #Update the model for the bin
        NewFitObject = ChangeModel(Fit, ener[ibin], ener[ibin + 1], srcname,
                                   Pref[ibin], Gamma[ibin])
        Xmlname = (config['out'] + "/" + srcname + "_" + str(ibin) + ".xml")
        NewFitObject.writeXml(Xmlname)  # dump the corresponding xml file
        config['file']['xml'] = Xmlname
        #update the energy bounds
        config['energy']['emin'] = str(ener[ibin])
        config['energy']['emax'] = str(ener[ibin + 1])
        config['energy']['decorrelation_energy'] = "no"
        # Change the spectral index to follow the Estimated Gamma
        # if approximated Gamma is outside of bounds set it to limit
        Gamma_min = -5
        Gamma_max = 0.5
        config['UpperLimit']['SpectralIndex'] = -min(
            max(Gamma_min, Gamma[ibin]), Gamma_max)

        config['file']['tag'] = tag + '_Ebin' + str(NEbin) + '_' + str(ibin)
        filename = config['target']['name'] + "_" + str(ibin) + ".conf"
        paramsfile.append(filename)
        config.write(open(config['out'] + '/' + paramsfile[ibin],
                          'w'))  #save the config file in a ascii file

    return paramsfile
示例#12
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
示例#13
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'
        #        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
示例#14
0
def PrepareEbin(Fit, FitRunner, sedresult=None):
    """ Prepare the computation of spectral point in energy bins by
    i) removing the weak sources (TS<1) # not true
    ii) updating the config file (option and energy)
    and save it in a new ascii file
    iii) changing the spectral model and saving it in a new xml file.
    A list of the ascii files is returned"""

    mes = Loggin.Message()

    NEbin = int(FitRunner.config['Ebin']['NumEnergyBins'])

    config = FitRunner.config

    config['verbose'] = 'no'  #Be quiet

    #Replace the evt file with the fits file produced before
    #in order to speed up the production of the fits files
    config['file']['event'] = FitRunner.obs.eventcoarse
    #update the config to allow the fit in energy bins
    config['UpperLimit']['envelope'] = 'no'
    config['Ebin']['NumEnergyBins'] = '0'  #no new bin in energy!
    config['target']['redshift'] = '0'  #Disable EBL correction
    config['out'] = FitRunner.config['out'] + '/' + EbinPath + str(NEbin)
    config['Spectrum']['ResultPlots'] = 'no'  #no SED plot/modelmap
    #copy the chose of the user for the enery bin computing
    config['Spectrum']['FitsGeneration'] = config['Ebin']['FitsGeneration']
    config['UpperLimit']['TSlimit'] = config['Ebin']['TSEnergyBins']
    tag = FitRunner.config['file']['tag']
    Emax = float(FitRunner.config['energy']['emax'])
    Emin = float(FitRunner.config['energy']['emin'])
    lEmax = np.log10(Emax)
    lEmin = np.log10(Emin)
    utils._log("Preparing submission of fit into energy bins")
    print("Emin = {0} MeV".format(Emin), "Emax = {0} MeV".format(Emax),
          "Nbins = {0}".format(NEbin))

    ener = utils.string_to_list(config['Ebin']['DistEbins'])
    if ener is None:
        if (config['ComponentAnalysis']['FGL4'] == 'yes'
                or config['Ebin']['DistEbins'] == 'FGL4'):
            ener = np.asarray([50, 1e2, 3e2, 1e3, 3e3, 1e4, 3e4, 3e5])
            NEbin = len(ener) - 1
        elif config['Ebin']['DistEbins'] in ['TS', 'mix'
                                             ] and sedresult != None:
            # Make the bins equispaced in sum(SED/SEDerr) - using the butterfly
            ipo = 0
            iTS = sedresult.SED / sedresult.Err
            TScumula = 0
            TSperbin = 1. * sum(iTS) / NEbin
            ener = [10**lEmin]
            while ipo < len(sedresult.E) - 1:
                TScumula += iTS[ipo]
                if TScumula / TSperbin > 1:
                    ener.append(sedresult.E[ipo])
                    TScumula -= TSperbin
                ipo += 1
            ener.append(10**lEmax)
            ener = np.array(ener)
            # intermediate approach (between both TS-spaced and logE spaced)
            if config['Ebin']['DistEbins'] == 'mix':
                ener = 0.5 * (ener + np.logspace(lEmin, lEmax, NEbin + 1))
        else:
            # Make the bins equispaced in logE (standard)
            ener = np.logspace(lEmin, lEmax, NEbin + 1)

    # 1. Remove bins that are out of the range covered by the data
    # 2. Limit the bin extend to the range covered by the data.
    # Get elements strictly above threshold +1 element to the left for the left side
    # Get elements strictly below limit +1 element to the right side.
    # example. [1,2,3,4,5] -> if Emin=3.4, Emax=3.9 we want to keep [3.4,3.9].
    ener = np.asarray(ener)
    print("Energy bins (before energy cuts): {0}".format(str(ener)))
    if len(ener) == 0:
        print("** Warning: energy bin array is empty")
        return (None)
    available_left = ener > Emin  # In the example FFFTT -> [4,5]
    for k, use in enumerate(available_left[:-1]):
        if not use and available_left[k + 1]:
            available_left[k] = True  # In the example FFTTT -> [3,5]
    available_right = ener < Emax  # In the example TTTFF -> [1,3]
    for k, use in enumerate(available_right[1:]):
        if not use and available_right[k]:
            available_right[k + 1] = True  # In the example TTTTF -> [1,4]
    available = available_left * available_right
    ener = ener[available]  # In the example FFTTF -> [3,4]
    # Limit the range to the real energies that are covered by our data
    # If the energy bins are well placed this should not do anything.
    ener[0] = np.max([Emin, ener[0]])
    ener[-1] = np.min([Emax, ener[-1]])
    NEbin = len(ener) - 1
    print("Energy bins (after energy cuts): {0}".format(str(ener)))
    if len(ener) == 0:
        print("** Warning: energy bin array is empty")
        return (None)

    utils.mkdir_p(config['out'])
    paramsfile = []

    srcname = FitRunner.config['target']['name']
    try:
        TSsrc = Fit.Ts(srcname)
    except RuntimeError:
        TSsrc = 0

    if config['UpperLimit']['TSlimit'] > TSsrc:
        utils._log('Re-optimize', False)
        print "An upper limit has been computed. The fit need to be re-optimized"
        Fit.optimize(0)

    Pref = utils.ApproxPref(Fit, ener, srcname)
    Gamma = utils.ApproxGamma(Fit, ener, srcname)

    Model_type = Fit.model.srcs[srcname].spectrum().genericName()
    # if the model is not PowerLaw : change the model
    if not (Model_type == 'PowerLaw'):
        for comp in Fit.components:
            comp.logLike.getSource(srcname).setSpectrum(
                "PowerLaw")  #Change model
        config['target']['spectrum'] = "PowerLaw"

    xmltag_list = [""]  #handle summed like analysis
    if config['ComponentAnalysis']['FrontBack'] == "yes":
        xmltag_list = ["_FRONT", "_BACK"]
        mes.info("Splitting Front/Back events")
    elif config['ComponentAnalysis']['PSF'] == "yes":
        xmltag_list = ["_PSF0", "_PSF1", "_PSF2", "_PSF3"]
        mes.info("Splitting PSF events")
    elif config['ComponentAnalysis']['EDISP'] == "yes":
        xmltag_list = ["_EDISP0", "_EDISP1", "_EDISP2", "_EDISP3"]
        mes.info("Splitting EDISP events")
    elif config['ComponentAnalysis']['FGL4'] == "yes":
        from catalogComponents import evtnum, energybins, pixelsizes
        xmltag_list = []
        for ebin_i in energybins:
            for k, evt in enumerate(evtnum):
                #if pixelsizes[ebin_i][k] > 0:
                try:
                    xmltag_list.append("_{0}_En{1}".format(
                        utils.typeirfs[k], ebin_i))
                except KeyError:
                    continue

    for ibin in xrange(NEbin):  #Loop over the energy bins
        E = utils.GetE0(ener[ibin + 1], ener[ibin])
        mes.info("Submitting # " + str(ibin) + " at energy " + str(E))
        #Update the model for the bin
        for comp, xmltag in zip(Fit.components, xmltag_list):
            NewFitObject = ChangeModel(comp, ener[ibin], ener[ibin + 1],
                                       srcname, Pref[ibin], Gamma[ibin])
            Xmlname = (config['out'] + "/" + srcname + "_" + str(ibin) +
                       xmltag + ".xml")

            NewFitObject.writeXml(Xmlname)  # dump the corresponding xml file
            config['file']['xml'] = Xmlname.replace(xmltag, "")
        #update the energy bounds
        config['energy']['emin'] = str(ener[ibin])
        config['energy']['emax'] = str(ener[ibin + 1])
        config['energy']['decorrelation_energy'] = "no"
        # Change the spectral index to follow the Estimated Gamma
        # if approximated Gamma is outside of bounds set it to limit
        Gamma_min = -5
        Gamma_max = -0.501
        Gamma_bin = -max(min(Gamma_max, Gamma[ibin]), Gamma_min)
        config['Spectrum']['FrozenSpectralIndex'] = Gamma_bin
        config['UpperLimit']['SpectralIndex'] = Gamma_bin

        config['file']['tag'] = tag + '_Ebin' + str(NEbin) + '_' + str(ibin)
        filename = config['target']['name'] + "_" + str(ibin) + ".conf"
        paramsfile.append(filename)
        config.write(open(config['out'] + '/' + filename,
                          'w'))  #save the config file in a ascii file

    return paramsfile
示例#15
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'] = 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.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
示例#16
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"
        #        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.0)

            ##############################################################
            #   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
示例#17
0
def PrepareEbin(Fit, FitRunner,sedresult=None):
    """ Prepare the computation of spectral point in energy bins by
    i) removing the weak sources (TS<1) # not true
    ii) updating the config file (option and energy)
    and save it in a new ascii file
    iii) changing the spectral model and saving it in a new xml file.
    A list of the ascii files is returned"""
        
    mes = Loggin.Message()

    NEbin = int(FitRunner.config['Ebin']['NumEnergyBins'])

    config = FitRunner.config

    config['verbose'] ='no' #Be quiet

    #Replace the evt file with the fits file produced before
    #in order to speed up the production of the fits files
    config['file']['event'] = FitRunner.obs.eventcoarse
    #update the config to allow the fit in energy bins
    config['UpperLimit']['envelope'] = 'no'
    config['Ebin']['NumEnergyBins'] = '0'#no new bin in energy!
    config['target']['redshift']    = '0'#Disable EBL correction
    config['out'] = FitRunner.config['out'] + '/'+EbinPath + str(NEbin)
    config['Spectrum']['ResultPlots'] = 'no' #no SED plot/modelmap
    #copy the chose of the user for the enery bin computing
    config['Spectrum']['FitsGeneration'] = config['Ebin']['FitsGeneration']
    config['UpperLimit']['TSlimit'] = config['Ebin']['TSEnergyBins']
    tag = FitRunner.config['file']['tag']
    lEmax = np.log10(float(FitRunner.config['energy']['emax']))
    lEmin = np.log10(float(FitRunner.config['energy']['emin']))
    utils._log("Preparing submission of fit into energy bins")
    print(" Emin = ", float(FitRunner.config['energy']['emin']),
          " Emax = ", float(FitRunner.config['energy']['emax']),
          " Nbins = ", NEbin)

    if config['Ebin']['DistEbins'] in ['TS','mix'] and sedresult!=None:
        # Make the bins equispaced in sum(SED/SEDerr) - using the butterfly
        ipo = 0
        iTS = sedresult.SED/sedresult.Err
        TScumula = 0
        TSperbin = 1.*sum(iTS)/NEbin
        ener = [10**lEmin]
        while ipo<len(sedresult.E)-1:
            TScumula += iTS[ipo]
            if TScumula/TSperbin > 1:
                ener.append(sedresult.E[ipo])
                TScumula -= TSperbin
            ipo += 1
        ener.append(10**lEmax)
        ener = np.array(ener)
        # intermediate approach (between both TS-spaced and logE spaced)
        if config['Ebin']['DistEbins'] == 'mix':
            ener = 0.5*(ener + np.logspace(lEmin, lEmax, NEbin + 1))
    else:
        # Make the bins equispaced in logE (standard)
        ener = np.logspace(lEmin, lEmax, NEbin + 1)

    utils.mkdir_p(config['out'])
    paramsfile = []

    srcname = FitRunner.config['target']['name']
    if config['UpperLimit']['TSlimit']>Fit.Ts(srcname) :
        utils._log('Re-optimize', False)
        print "An upper limit has been computed. The fit need to be re-optmized"
        Fit.optimize(0)


    Pref = utils.ApproxPref(Fit, ener, srcname)
    Gamma = utils.ApproxGamma(Fit, ener, srcname)

    Model_type = Fit.model.srcs[srcname].spectrum().genericName()
    # if the model is not PowerLaw : change the model
    if not(Model_type == 'PowerLaw'):
        for comp in Fit.components:
            comp.logLike.getSource(srcname).setSpectrum("PowerLaw") #Change model
        config['target']['spectrum'] = "PowerLaw"

    xmltag_list = [""]#handle summed like analysis
    if config['ComponentAnalysis']['FrontBack'] == "yes":
        xmltag_list = ["_FRONT","_BACK"]
        mes.info("Splitting Front/Back events")
    elif config['ComponentAnalysis']['PSF'] == "yes":
        xmltag_list = ["_PSF0","_PSF1","_PSF2","_PSF3"]
        mes.info("Splitting PSF events")
    elif config['ComponentAnalysis']['EDISP'] == "yes":
        xmltag_list = ["_EDISP0","_EDISP1","_EDISP2","_EDISP3"]
        mes.info("Splitting EDISP events")


    for ibin in xrange(NEbin):#Loop over the energy bins
        E = utils.GetE0(ener[ibin + 1],ener[ibin])
        mes.info("Submitting # "+str(ibin)+" at energy "+str(E))
        #Update the model for the bin
        for comp,xmltag in zip(Fit.components, xmltag_list):
            NewFitObject = ChangeModel(comp, ener[ibin], ener[ibin + 1], srcname, Pref[ibin] ,Gamma[ibin])
            Xmlname = (config['out'] + "/" + srcname + "_" + str(ibin) +xmltag+ ".xml")

            NewFitObject.writeXml(Xmlname)# dump the corresponding xml file
            config['file']['xml'] = Xmlname.replace(xmltag,"")
        #update the energy bounds
        config['energy']['emin'] = str(ener[ibin])
        config['energy']['emax'] = str(ener[ibin + 1])
        config['energy']['decorrelation_energy'] = "no"
        # Change the spectral index to follow the Estimated Gamma 
        # if approximated Gamma is outside of bounds set it to limit
        Gamma_min=-5
        Gamma_max=0.5
        config['UpperLimit']['SpectralIndex'] = -min(max(Gamma_min,Gamma[ibin]),Gamma_max)

        config['file']['tag'] = tag + '_Ebin' + str(NEbin) + '_' + str(ibin)
        filename =  config['target']['name'] + "_" + str(ibin) + ".conf"
        paramsfile.append(filename)
        config.write(open(config['out'] + '/' +paramsfile[ibin], 'w')) #save the config file in a ascii file

    return paramsfile
示例#18
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)
                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 
示例#19
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