示例#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, 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
示例#4
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
示例#5
0
    def __init__(self, config):
         self.config = config
         self.folder = self.config['out']
         os.system("mkdir -p "+self.folder+"/TestModel")
         convtype = self.config['analysis']['convtype']

         if self.config['Spectrum']['SummedLike'] == 'yes':
             Obs1 = Observation(self.folder, self.config, convtype=0, tag="FRONT")
             Obs2 = Observation(self.folder, self.config, convtype=1, tag="BACK")
             FitRunnerfront = FitMaker(Obs1, self.config)
             FitRunnerback = FitMaker(Obs2, self.config)
             FitRunnerfront.CreateLikeObject()
             FitRunnerback.CreateLikeObject()
             self.Fit = SummedLikelihood.SummedLikelihood()
         else:
             Obs = Observation(self.folder, self.config, convtype, tag="")
             FitRunner = FitMaker(Obs, self.config)##Class
             self.Fit = FitRunner.CreateLikeObject()

         # Store the results in a dictionnary
         self.Results = {}
         self.Results["PowerLaw"] = 0
         self.Results["LogParabola"] = 0
         self.Results["PLSuperExpCutoff"] = 0
示例#6
0
    def _GenFit(self):
        try:
            del self.Fit
        except:
            pass

        if self.config['Spectrum']['SummedLike'] == 'yes':
            Obs1 = Observation(self.folder, self.config, tag="FRONT")
            Obs2 = Observation(self.folder, self.config, tag="BACK")
            self.FitRunnerfront = FitMaker(Obs1, self.config)
            self.FitRunnerback = FitMaker(Obs2, self.config)
            self.FitRunnerfront.CreateLikeObject()
            self.FitRunnerback.CreateLikeObject()
            self.Fit = SummedLikelihood.SummedLikelihood()
        else:
            Obs = Observation(self.folder, self.config, tag="")
            self.FitRunner = FitMaker(Obs, self.config)  ##Class
            self.Fit = self.FitRunner.CreateLikeObject()
示例#7
0
class ModelTester(Loggin.Message):
    """Class to est several models to check
        which is statistically prefered."""
    def __init__(self, config):
        super(ModelTester, self).__init__()
        Loggin.Message.__init__(self)
        self.config = config
        self.folder = self.config['out']
        os.system("mkdir -p " + self.folder + "/TestModel")
        self.modellist = ["PowerLaw", "LogParabola", "PLSuperExpCutoff"]
        self._GenFit()
        self.FitRunner.PerformFit(self.Fit, False)
        # Store the results in a dictionnary
        self.Results = {}

    def _GenFit(self):
        try:
            del self.Fit
        except:
            pass

        if self.config['Spectrum']['SummedLike'] == 'yes':
            Obs1 = Observation(self.folder, self.config, tag="FRONT")
            Obs2 = Observation(self.folder, self.config, tag="BACK")
            self.FitRunnerfront = FitMaker(Obs1, self.config)
            self.FitRunnerback = FitMaker(Obs2, self.config)
            self.FitRunnerfront.CreateLikeObject()
            self.FitRunnerback.CreateLikeObject()
            self.Fit = SummedLikelihood.SummedLikelihood()
        else:
            Obs = Observation(self.folder, self.config, tag="")
            self.FitRunner = FitMaker(Obs, self.config)  ##Class
            self.Fit = self.FitRunner.CreateLikeObject()

    def _printResults(self):
        print
        self.info("Summary of the results")
        for key in self.modellist:
            if key == "PowerLaw":
                print key, " Log(Like) = ", self.Results[key]
                llpl = self.Results[key]
            else:
                TS = 2 * (self.Results[key] - llpl)
                prob = 1 - scipy.stats.chi2.cdf(TS, 1)
                print key, " Log(Like) = ", self.Results[
                    key], " TS = ", TS, " Pvalue = ", prob

    def TestModel(self):
        """ actually test the models """
        Dumpfile = open(self.folder + "/TestModel/TestModel.results", "w")
        for key in self.modellist:
            self.Results[key] = self.RunAFit(self.config["target"]["name"],
                                             key)
            Dumpfile.write(key + '\t' + str(self.Results[key]) + '\n')
        Dumpfile.close()
        self._printResults()

    def RunAFit(self, srcname, model):
        self.info("Computing loglike value for " + model)
        #        self._GenFit()
        self.Fit.logLike.getSource(srcname).setSpectrum(model)
        if model == "PowerLaw":
            self._setPowerLaw(srcname)
        if model == "LogParabola":
            self._setLogParabola(srcname)
        if model == "PLSuperExpCutoff":
            self._setPLSuperExpCutoff(srcname)

        #change the fit tolerance to the one given by the user
#        self.Fit.ftol = float(self.config['fitting']['ftol'])
        try:
            self.Fit.fit(0,
                         covar=True,
                         optimizer=self.config["fitting"]["optimizer"])
            spectrum = self.Fit[self.config['target']
                                ['name']].funcs['Spectrum']
            # Get the names of the parameters for the source of interest
            print "Loglike Value for ", model, ": ", self.Fit.logLike.value()
            self.Fit.writeXml(self.folder + "/TestModel/TestModel" + model +
                              ".xml")
            return self.Fit.logLike.value()
        except:
            self.warning("No convergence for model : " + model + " ??")
            return 0

    def _setPowerLaw(self, name):
        self.Fit.logLike.getSource(name).getSrcFuncs()['Spectrum'].getParam(
            'Prefactor').setBounds(1e-7, 1e7)
        self.Fit.logLike.getSource(name).getSrcFuncs()['Spectrum'].getParam(
            'Prefactor').setScale(1e-11)
        self.Fit.logLike.getSource(name).getSrcFuncs()['Spectrum'].getParam(
            'Prefactor').setValue(1.)

        self.Fit.logLike.getSource(name).getSrcFuncs()['Spectrum'].getParam(
            'Index').setBounds(-5, 0)
        self.Fit.logLike.getSource(name).getSrcFuncs()['Spectrum'].getParam(
            'Index').setValue(-2)
        self.Fit.logLike.getSource(name).getSrcFuncs()['Spectrum'].getParam(
            'Index').setFree(1)

        self.Fit.logLike.getSource(name).getSrcFuncs()['Spectrum'].getParam(
            'Scale').setValue(self.config['energy']['emin'])
        self.Fit.logLike.getSource(name).getSrcFuncs()['Spectrum'].getParam(
            'Scale').setBounds(20, 3e6)

    def _setLogParabola(self, name):
        self.Fit.logLike.getSource(name).getSrcFuncs()['Spectrum'].getParam(
            'norm').setBounds(1e-7, 1e7)
        self.Fit.logLike.getSource(name).getSrcFuncs()['Spectrum'].getParam(
            'norm').setScale(1e-11)
        self.Fit.logLike.getSource(name).getSrcFuncs()['Spectrum'].getParam(
            'norm').setValue(1.)

        self.Fit.logLike.getSource(name).getSrcFuncs()['Spectrum'].getParam(
            'alpha').setBounds(0, 5)
        self.Fit.logLike.getSource(name).getSrcFuncs()['Spectrum'].getParam(
            'alpha').setValue(2)
        self.Fit.logLike.getSource(name).getSrcFuncs()['Spectrum'].getParam(
            'alpha').setFree(1)

        self.Fit.logLike.getSource(name).getSrcFuncs()['Spectrum'].getParam(
            'beta').setBounds(0.01, 10)
        self.Fit.logLike.getSource(name).getSrcFuncs()['Spectrum'].getParam(
            'beta').setValue(0.5)
        self.Fit.logLike.getSource(name).getSrcFuncs()['Spectrum'].getParam(
            'beta').setFree(1)

        self.Fit.logLike.getSource(name).getSrcFuncs()['Spectrum'].getParam(
            'Eb').setValue(self.config['energy']['emin'])
        self.Fit.logLike.getSource(name).getSrcFuncs()['Spectrum'].getParam(
            'Eb').setFree(0)
        self.Fit.logLike.getSource(name).getSrcFuncs()['Spectrum'].getParam(
            'Eb').setBounds(20, 3e6)

    def _setPLSuperExpCutoff(self, name):
        self.Fit.logLike.getSource(name).getSrcFuncs()['Spectrum'].getParam(
            'Prefactor').setBounds(1e-7, 1e7)
        self.Fit.logLike.getSource(name).getSrcFuncs()['Spectrum'].getParam(
            'Prefactor').setScale(1e-11)
        self.Fit.logLike.getSource(name).getSrcFuncs()['Spectrum'].getParam(
            'Prefactor').setValue(1.)

        self.Fit.logLike.getSource(name).getSrcFuncs()['Spectrum'].getParam(
            'Index1').setBounds(-5, 0)
        self.Fit.logLike.getSource(name).getSrcFuncs()['Spectrum'].getParam(
            'Index1').setValue(-2)
        self.Fit.logLike.getSource(name).getSrcFuncs()['Spectrum'].getParam(
            'Index1').setFree(1)

        self.Fit.logLike.getSource(name).getSrcFuncs()['Spectrum'].getParam(
            'Index2').setValue(-1)
        self.Fit.logLike.getSource(name).getSrcFuncs()['Spectrum'].getParam(
            'Index2').setBounds(-5, -0.05)
        self.Fit.logLike.getSource(name).getSrcFuncs()['Spectrum'].getParam(
            'Index2').setFree(0)

        self.Fit.logLike.getSource(name).getSrcFuncs()['Spectrum'].getParam(
            'Cutoff').setBounds(20, 3e6)
        self.Fit.logLike.getSource(name).getSrcFuncs()['Spectrum'].getParam(
            'Cutoff').setValue(1e4)
        self.Fit.logLike.getSource(name).getSrcFuncs()['Spectrum'].getParam(
            'Cutoff').setFree(1)

        self.Fit.logLike.getSource(name).getSrcFuncs()['Spectrum'].getParam(
            'Scale').setValue(self.config['energy']['emin'])
        self.Fit.logLike.getSource(name).getSrcFuncs()['Spectrum'].getParam(
            'Scale').setBounds(20, 3e6)
示例#8
0
class ModelTester(Loggin.Message):
    """Class to est several models to check
        which is statistically prefered."""
    def __init__(self, config):
        super(ModelTester, self).__init__()
        Loggin.Message.__init__(self)
        self.config = config
        self.folder = self.config['out']
        os.system("mkdir -p " + self.folder + "/TestModel")
        self.modellist = ["PowerLaw", "LogParabola", "PLSuperExpCutoff"]
        '''
        try:
            with open(self.folder+"/TestModel/Fit.pickle","r") as pfile:
                print("Retrieving previous Fit from %s" \
                    %(self.folder+"/TestModel/Fit.pickle"))
                self.FitRunner = pickle.load(pfile)
                self.Fit = self.FitRunner.CreateLikeObject()
        except:
            self._GenFit()
            self.FitRunner.PerformFit(self.Fit, False)

            with open(self.folder+"/TestModel/Fit.pickle","w") as pfile:
                print("Saving current Fit to %s" \
                    %(self.folder+"/TestModel/Fit.pickle"))
                pickle.dump(self.FitRunner,pfile)
        '''
        self._GenFit()
        self.FitRunner.PerformFit(self.Fit, False)

        # Store the results in a dictionnary
        self.Results = {}

    def _GenFit(self):
        try:
            del self.Fit
        except:
            pass

        if self.config['Spectrum']['SummedLike'] == 'yes':
            Obs1 = Observation(self.folder, self.config, tag="FRONT")
            Obs2 = Observation(self.folder, self.config, tag="BACK")
            self.FitRunnerfront = FitMaker(Obs1, self.config)
            self.FitRunnerback = FitMaker(Obs2, self.config)
            self.FitRunnerfront.CreateLikeObject()
            self.FitRunnerback.CreateLikeObject()
            self.Fit = SummedLikelihood.SummedLikelihood()
        else:
            Obs = Observation(self.folder, self.config, tag="")
            self.FitRunner = FitMaker(Obs, self.config)  ##Class
            #self.Fit = self.FitRunner.CreateLikeObject()
            self.Fit = SummedLikelihood.SummedLikelihood()
            self.Fit.addComponent(self.FitRunner.CreateLikeObject())

    def _printResults(self):
        print
        self.info("Summary of the results")
        for key in self.modellist:
            if key == "PowerLaw":
                print key, " Log(Like) = ", self.Results[key]
                llpl = self.Results[key]
            else:
                TS = 2 * (self.Results[key] - llpl)
                prob = 1 - scipy.stats.chi2.cdf(TS, 1)
                print key, " Log(Like) = ", self.Results[
                    key], " TS = ", TS, " Pvalue = ", prob

    def TestModel(self):
        """ actually test the models """
        Dumpfile = open(self.folder + "/TestModel/TestModel.results", "w")
        for key in self.modellist:
            self.Results[key] = self.RunAFit(self.config["target"]["name"],
                                             key)
            Dumpfile.write(key + '\t' + str(self.Results[key]) + '\n')
        Dumpfile.close()
        self._printResults()

    def TestModelFromFile(self, inputfile):
        """ Set model and pars from file (test only a custom model)
            This function allow us to test several custom models by calculating 
            their likelihood """

        Inputfile = open(inputfile, 'r')
        with open(self.folder + "/TestModel/TestModel.results", "w") as empty:
            pass
        Dumpfile = open(self.folder + "/TestModel/TestModel.results", "a+")

        for currentmodel in Inputfile.readlines():
            content = currentmodel.rstrip().split(",")
            model = content[0]
            pars = content[1:]

            if model not in self.modellist:
                print("WARNING: given model %s not in the valid range %s" \
                    %(str(model), str(self.modellist)))
                continue

            for k in xrange(len(pars)):
                try:
                    pars[k] = float(pars[k])
                except:
                    pars[k] = None

            # Reduce the list of possible models to the current one
            self.modellist = [model]

            print("Using model %s with parameters %s" %
                  (str(model), str(pars)))
            self.Results[model] = self.RunAFit(self.config["target"]["name"],
                                               model, pars)
            _sep_ = ', '
            Dumpfile.write(model + _sep_ + _sep_.join([str(k) for k in pars]) \
                + _sep_ + str(self.Results[model]) + '\n')

            print("%s Log(Like) = %s" % (model, self.Results[model]))

        Inputfile.close()
        Dumpfile.close()

    def RunAFit(self, srcname, model, pars=None):
        # Compute the loglike for the current model for the given parameter set.
        self.info("Computing loglike value for " + model)
        for comp in self.Fit.components:
            comp.logLike.getSource(srcname).setSpectrum(model)

        if (pars == None):
            pars = [None, None, None, None]
        else:
            # Release diffuse models (background)
            for comp in self.Fit.components:
                comp.thaw(self.Fit.par_index("IsoDiffModel", 'Normalization'))
                comp.thaw(self.Fit.par_index("GalDiffModel", 'Value'))

        for comp in self.Fit.components:
            if model == "PowerLaw":
                self._setPowerLaw(comp, srcname, pars)
            if model == "LogParabola":
                self._setLogParabola(comp, srcname, pars)
            if model == "PLSuperExpCutoff":
                self._setPLSuperExpCutoff(comp, srcname, pars)

        #change the fit tolerance to the one given by the user
        #self.Fit.ftol = float(self.config['fitting']['ftol'])
        try:
            self.Fit.fit(0,
                         covar=True,
                         optimizer=self.config["fitting"]["optimizer"])
            spectrum = self.Fit[self.config['target']
                                ['name']].funcs['Spectrum']
            # Get the names of the parameters for the source of interest
            print "Loglike Value for ", model, ": ", self.Fit.logLike.value()
            self.Fit.writeXml(self.folder + "/TestModel/TestModel" + model +
                              ".xml")
            loglikevalue = 0
            for comp in self.Fit.components:
                loglikevalue += comp.logLike.value()
            return loglikevalue
        except:
            self.warning("No convergence for model : " + model + " ??")
            return 0

    def _setPowerLaw(self, comp, name, pars=None):
        SrcSpectrum = comp.logLike.getSource(name).getSrcFuncs()['Spectrum']
        SrcSpectrum.getParam('Prefactor').setBounds(1e-7, 1e7)
        SrcSpectrum.getParam('Prefactor').setScale(1e-11)
        SrcSpectrum.getParam('Prefactor').setValue(1.)
        SrcSpectrum.getParam('Prefactor').setFree(1)

        SrcSpectrum.getParam('Index').setBounds(-5, 0)
        SrcSpectrum.getParam('Index').setValue(-2)
        SrcSpectrum.getParam('Index').setFree(1)

        SrcSpectrum.getParam('Scale').setValue(self.config['energy']['emin'])
        SrcSpectrum.getParam('Scale').setBounds(20, 3e6)

        # Set each non-None parameter to the wanted value and fix it.
        if pars[0] != None:
            print("Fixing Prefactor")
            SrcSpectrum.getParam('Prefactor').setFree(0)
            SrcSpectrum.getParam('Prefactor').setValue(pars[0] / 1e-11)
            par = comp.par_index(name, 'Prefactor')
            comp.freeze(par)
        if pars[1] != None:
            print("Fixing Index")
            SrcSpectrum.getParam('Index').setScale(pars[1])
            SrcSpectrum.getParam('Index').setFree(0)
            par = comp.par_index(name, 'Prefactor')
            comp - freeze(par)

    def _setLogParabola(self, comp, name, pars=None):
        SrcSpectrum = comp.logLike.getSource(name).getSrcFuncs()['Spectrum']
        SrcSpectrum.getParam('norm').setBounds(1e-7, 1e7)
        SrcSpectrum.getParam('norm').setScale(1e-11)
        SrcSpectrum.getParam('norm').setValue(1.)
        SrcSpectrum.getParam('norm').setFree(1)

        SrcSpectrum.getParam('alpha').setBounds(0, 5)
        SrcSpectrum.getParam('alpha').setValue(2)
        SrcSpectrum.getParam('alpha').setFree(1)

        SrcSpectrum.getParam('beta').setBounds(0.01, 10)
        SrcSpectrum.getParam('beta').setValue(0.5)
        SrcSpectrum.getParam('beta').setFree(1)

        SrcSpectrum.getParam('Eb').setValue(self.config['energy']['emin'])
        SrcSpectrum.getParam('Eb').setFree(0)
        SrcSpectrum.getParam('Eb').setBounds(20, 3e6)

        # Set each non-None parameter to the wanted value and fix it.
        if pars[0] != None:
            print("Fixing norm")
            SrcSpectrum.getParam('norm').setFree(0)
            SrcSpectrum.getParam('norm').setValue(pars[0] / 1e-11)
            par = comp.par_index(name, 'norm')
            comp.freeze(par)
        if pars[1] != None:
            print("Fixing alpha")
            SrcSpectrum.getParam('alpha').setFree(0)
            SrcSpectrum.getParam('alpha').setScale(pars[1])
            par = comp.par_index(name, 'alpha')
            comp.freeze(par)
        if pars[2] != None:
            print("Fixing beta")
            SrcSpectrum.getParam('beta').setFree(0)
            SrcSpectrum.getParam('beta').setScale(pars[2])
            par = comp.par_index(name, 'beta')
            comp.freeze(par)

    def _setPLSuperExpCutoff(self, comp, name, pars=None):
        SrcSpectrum = comp.logLike.getSource(name).getSrcFuncs()['Spectrum']
        SrcSpectrum.getParam('Prefactor').setBounds(1e-7, 1e7)
        SrcSpectrum.getParam('Prefactor').setScale(1e-11)
        SrcSpectrum.getParam('Prefactor').setValue(1.)
        SrcSpectrum.getParam('Prefactor').setFree(1)

        SrcSpectrum.getParam('Index1').setBounds(-5, 0)
        SrcSpectrum.getParam('Index1').setValue(-2)
        SrcSpectrum.getParam('Index1').setFree(1)

        SrcSpectrum.getParam('Index2').setValue(-1)
        SrcSpectrum.getParam('Index2').setBounds(-5, -0.05)
        SrcSpectrum.getParam('Index2').setFree(0)

        SrcSpectrum.getParam('Cutoff').setBounds(20, 3e6)
        SrcSpectrum.getParam('Cutoff').setValue(1e4)
        SrcSpectrum.getParam('Cutoff').setFree(1)

        SrcSpectrum.getParam('Scale').setValue(self.config['energy']['emin'])
        SrcSpectrum.getParam('Scale').setBounds(20, 3e6)

        # Set each non-None parameter to the wanted value and fix it.
        if pars[0] != None:
            print("Fixing Prefactor")
            SrcSpectrum.getParam('Prefactor').setFree(0)
            SrcSpectrum.getParam('Prefactor').setValue(pars[0] / 1e-11)
            par = comp.par_index(name, 'Prefactor')
            comp.freeze(par)
        if pars[1] != None:
            print("Fixing Index1")
            SrcSpectrum.getParam('Index1').setScale(pars[1])
            SrcSpectrum.getParam('Index1').setFree(0)
            par = comp.par_index(name, 'Index1')
            comp.freeze(par)
        if pars[2] != None:
            print("Fixing Index2")
            SrcSpectrum.getParam('Index2').setScale(pars[2])
            SrcSpectrum.getParam('Index2').setFree(0)
            par = comp.par_index(name, 'Index2')
            comp.freeze(par)
        if pars[3] != None:
            print("Fixing Cutoff")
            SrcSpectrum.getParam('Cutoff').setScale(pars[3])
            SrcSpectrum.getParam('Cutoff').setFree(0)
            par = comp.par_index(name, 'Cutoff')
            comp.freeze(par)