Exemplo n.º 1
0
def run(infile):
    """Run an entire Fermi analysis (spectrum) by reading a config file"""
    config = get_config(infile)
    folder = config['out']
    os.system('mkdir -p ' + folder)

    FitRunner, Fit = GenAnalysisObjects(config)
    # create all the fit files and run gtlike
    FitRunner.PerformFit(Fit)

    Result = FitRunner.GetAndPrintResults(
        Fit)  #Get and dump the target specific results
    if config['verbose'] == 'yes':
        utils.GetFluxes(Fit, FitRunner.obs.Emin,
                        FitRunner.obs.Emax)  #print the flux of all the sources

    utils.DumpResult(Result, config)

    #plot the SED and model map if possible and asked
    if config['Spectrum']['ResultPlots'] == 'yes':
        from enrico.constants import SpectrumPath
        os.system("mkdir -p " + config['out'] + '/' + SpectrumPath + '/')
        if float(config['UpperLimit']['TSlimit']) < Fit.Ts(
                config['target']['name']):
            FitRunner.ComputeSED(Fit)
        outXml = utils._dump_xml(config)
        if config['Spectrum'][
                'SummedLike'] != 'yes':  # the possiblity of making the model map is checked inside the function
            FitRunner.ModelMap(outXml)

    #  Make energy bins by running a *new* analysis
    Nbin = config['Ebin']['NumEnergyBins']
    energybin.RunEbin(folder, Nbin, Fit, FitRunner)
Exemplo n.º 2
0
    def GetAndPrintResults(self, Fit):
        """Get and print some useful results. Also contruct a dictonnary and fill it with results"""
        if self.config['verbose'] == 'yes':
            self._log('Results', 'Print results of the fit')
        Result = {}

        if self.config['verbose'] == 'yes':
            print Fit.model, "\n"
            self.info("Results for the Fit")
            # Print src name, Npred and TS for source with TS > 5
            print "Source Name\tNpred\tTS"
            #TODO
        #for src in Fit.model.srcNames:
        #if Fit.Ts(src) > 5:
        #   print src, "\t%2.3f\t%2.3f" % (Fit.NpredValue(src), Fit.Ts(src))

        # fill the dictonnary with some values
        Result['Optimizer'] = self.config['fitting']['optimizer']
        Result['Npred'] = Fit.NpredValue(self.obs.srcname)
        Result['TS'] = Fit.Ts(self.obs.srcname)
        if self.config['verbose'] == 'yes':
            print "Values and (MINOS) errors for " + self.obs.srcname
            print "TS : ", Fit.Ts(self.obs.srcname)

        # Get the python object 'Spectrum' for the source of interest
        spectrum = Fit[self.obs.srcname].funcs['Spectrum']
        # Get the names of the parameters for the source of interest
        ParName = spectrum.paramNames
        #Get the model type and fill the dictonnary
        stype = Fit.model.srcs[self.obs.srcname].spectrum().genericName()
        Result['ModelType'] = stype
        Result['log_like'] = Fit.logLike.value()
        #Add the energy information to the result dictionnary
        Result['Emin'] = self.obs.Emin
        Result['Emax'] = self.obs.Emax
        #Add the time information to the result dictionnary
        Result['tmin'] = self.config['time']['tmin']
        Result['tmax'] = self.config['time']['tmax']
        Result['SrcName'] = self.obs.srcname
        Result['Flux'] = Fit.flux(self.obs.srcname, self.obs.Emin,
                                  self.obs.Emax)
        Result['dFlux'] = Fit.fluxError(self.obs.srcname, self.obs.Emin,
                                        self.obs.Emax)

        for par in ParName:  #Loop over the parameters and get value, error and scale
            ParValue = spectrum.getParam(par).value()
            ParError = spectrum.getParam(par).error()
            Scale = spectrum.getParam(par).getScale()
            Result[par] = ParValue * Scale
            Result['d' + par] = ParError * Scale
            if ParError > 0:  # Compute MINOS errors for relevent parameters  Fit.Ts(self.obs.srcname) > 5 and
                try:
                    MinosErrors = Fit.minosError(self.obs.srcname, par)
                    if self.config['verbose'] == 'yes':
                        print(
                            par +
                            " :  %2.2f +/-  %2.2f [ %2.2f, + %2.2f ] %2.0e" %
                            (ParValue, ParError, MinosErrors[0],
                             MinosErrors[1], Scale))
                        Result.update(
                            {'d' + par + '-': MinosErrors[0] * Scale})
                        Result.update(
                            {'d' + par + '+': MinosErrors[1] * Scale})
                except:
                    if self.config['verbose'] == 'yes':
                        print(
                            par + " :  %2.2f +/-  %2.2f  %2.0e" %
                            (ParValue, ParError, Scale))
            else:
                if self.config['verbose'] == 'yes':
                    print(par + " :  %2.2f   %2.0e" % (ParValue, Scale))

        try:  # get covariance matrix
            if self.config['verbose'] == 'yes':
                utils.GetCovar(self.obs.srcname, Fit)
        except:
            pass  #if the covariance matrix has not been computed

        if self.config['verbose'] == 'yes':
            utils.GetFluxes(Fit, self.obs.Emin,
                            self.obs.Emax)  #print the flux of all the sources

        #Compute an UL if the source is too faint
        if float(self.config['UpperLimit']['TSlimit']) > Fit.Ts(
                self.obs.srcname):
            if self.config['UpperLimit']['envelope'] == 'yes':
                self.EnvelopeUL(Fit)
            else:
                Ulval = self.ComputeUL(Fit)
                Result['Ulvalue'] = Ulval

        return Result  #Return the dictionnary