Exemplo n.º 1
0
 def __init__(self, Fit, pars):
     self.Fit = Fit
     self.Model = Fit[pars.srcname].funcs['Spectrum'].genericName()
     self.ptsrc = pyLikelihood.PointSource_cast(Fit[pars.srcname].src)
     self.covar = np.array(utils.GetCovar(pars.srcname, self.Fit, False))
     self.srcpars = pyLikelihood.StringVector()
     Fit[pars.srcname].src.spectrum().getFreeParamNames(self.srcpars)
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