Ejemplo n.º 1
0
class SEDBin(SetItem):
    def __init__(self,name,emin,emax,**kwargs):
        super(SEDBin,self).__init__(name)
        self.nobs = np.nan
        self.binTs = np.nan
        self.emin,self.emax = emin,emax
        self.pulimit,self.bulimit = np.nan, np.nan
        self.pllimit,self.bllimit = np.nan, np.nan
        self.__dict__.update(**kwargs)
        self.update_lnlfn()
        self.update_ulimit()
        self.update_llimit()
        self.update_mle()
    def get_value(self, value):
        return self.__dict__[value]

    def update_ulimit(self,alpha=0.05,ulimit='pulimit'):
        if ulimit=='pulimit':
            self.update_pulimit(alpha)
        elif ulimit=='bulimit':
            self.update_bulimit(alpha)
        else:
            raise ValueError('Limit not implemented: %s'%ulimit)

    def update_pulimit(self,alpha=0.05):
        try:
            self.pulimit = ProfileLimit(self.eflux,self.logLike).getLimit(alpha)
        except ValueError as e:
            print "WARNING: Unable to compute upper limit: ", e
            self.pulimit = np.nan

    def update_bulimit(self,alpha=0.05):
        try:
            self.bulimit = BayesianLimit(self.eflux,self.logLike).getLimit(alpha)
        except ValueError as e:
            print "WARNING: Unable to compute upper limit: ", e
            self.bulimit = np.nan

    def update_llimit(self,alpha=0.05,llimit='pllimit'):
        if llimit=='pllimit':
            self.update_pllimit(alpha)
        else:
            raise ValueError('Limit not implemented: %s'%llimit)

    def update_pllimit(self,alpha=0.05):
        try:
            self.pllimit = ProfileLimit(self.eflux,self.logLike).getLowerLimit(alpha)
        except ValueError as e:
            print "WARNING: Unable to compute lower limit: ", e
            self.pllimit = np.nan

    def update_lnlfn(self):
        self.lnlfn = LnLFn(self.eflux,self.logLike)

    def update_mle(self):
        self.mle = self.lnlfn.mle()

    def todict(self):
        return dict( emin=self.emin, emax=self.emax,
                     flux=self.flux, eflux=self.eflux,
                     logLike=self.logLike,
                     eflux2npred=self.eflux2npred,
                     nobs = self.nobs, binTs = self.binTs
                     )

    def __str__(self):
        np.set_printoptions(precision=3)
        s = "SED bin (%s):\n"%self.name
        s+= "  energy: [%g, %g]\n"%(self.emin,self.emax)
        s+= "  eflux:   %s\n"%str(self.eflux)
        s+= "  logLike: %s"%str(self.logLike)
        np.set_printoptions()
        return s
Ejemplo n.º 2
0
    print 'best matching radius : %s'%matching_disk
    # load matching SED file
    sedfile = join(datadir,config['sed'][matching_disk])
    print 'Loading SED file:', sedfile
    sed = SED.read_yaml(sedfile)
    # global logLike
    print 'get global logLike given the spectral form'
    pd = sed.get_global_logLike(model.spectrum, config['jsigma'])
    (norm, flux, lnl, p1lnl, flnl) = pd
    lnlx = norm; lnly = lnl
    print 'Calculating limits.'
    try:
        lnlfn = LnLFn(lnlx,lnly)
        p1lnlfn = LnLFn(lnlx,p1lnl)
        r['models'][i]=str(model)
        r['mle'][i] = lnlfn.mle()
        r['ulimits68'][i]  = ProfileLimit( lnlx, lnly).getLimit( 0.32 )        
        r['fluxes68'][i]  = ProfileLimit( flux, flnl).getLimit( 0.32 )
        r['ulimits95'][i]  = ProfileLimit( lnlx, lnly).getLimit( 0.05 )        
        r['fluxes95'][i]  = ProfileLimit( flux, flnl).getLimit( 0.05 )
        r['ulimits99'][i]  = ProfileLimit( lnlx, lnly).getLimit( 0.01 )
        r['fluxes99'][i]  = ProfileLimit( flux, flnl).getLimit( 0.01 )
        r['ts'][i] = float(2*(p1lnlfn(p1lnlfn.mle()) - p1lnlfn(0)))
        r['matching_radius'][i]=matching_disk
        r['fom']=foms 
    except Exception, message:
        print 'caught exception ',message
        continue
    safe_copy("srcmap.fits","%s_srcmap.fits"%model)
    [os.remove(f) for f in ["gtsrcmaps.par","srcmap.fits"]]