Exemplo n.º 1
0
    def CreateLikeObject(self):
        """Create an UnbinnedAnalysis or a BinnedAnalysis and retrun it.
        By default, the optimizer is DRMNGB """

        #create binnedAnalysis object
        if self.config['analysis']['likelihood'] == 'binned':
            Obs = BinnedObs(srcMaps=self.obs.scrMap,
                            expCube=self.obs.Cubename,
                            binnedExpMap=self.obs.BinnedMapfile,
                            irfs=self.obs.irfs)
            Fit = BinnedAnalysis(Obs, self.obs.xmlfile, optimizer='DRMNGB')

        #create a unbinnedAnalysis object
        if self.config['analysis']['likelihood'] == 'unbinned':
            Obs = UnbinnedObs(self.obs.eventfile,
                              self.obs.ft2,
                              expMap=self.obs.Mapname,
                              expCube=self.obs.Cubename,
                              irfs=self.obs.irfs)
            Fit = UnbinnedAnalysis(Obs, self.obs.xmlfile, optimizer='DRMNGB')

        if float(self.config['Spectrum']['FrozenSpectralIndex']) > 0:
            if Fit.model.srcs[self.obs.srcname].spectrum().genericName(
            ) == "PowerLaw" or Fit.model.srcs[
                    self.obs.srcname].spectrum().genericName() == "PowerLaw2":
                PhIndex = Fit.par_index(self.obs.srcname, 'Index')
                Fit[PhIndex] = -float(
                    self.config['Spectrum']['FrozenSpectralIndex'])
                Fit.freeze(PhIndex)
                print "Freezing spectral index at ", -float(
                    self.config['Spectrum']['FrozenSpectralIndex'])
            else:
                log.warning(
                    "The model is not a PowerLaw. Cannot freeze the index.")
        return Fit  #return the BinnedAnalysis or UnbinnedAnalysis object.
Exemplo n.º 2
0
    def CreateLikeObject(self):
        """Create an UnbinnedAnalysis or a BinnedAnalysis and retrun it."""

        #create binnedAnalysis object
        if self.config['analysis']['likelihood'] == 'binned':
            Obs = BinnedObs(srcMaps=self.obs.srcMap,
                            expCube=self.obs.Cubename,
                            binnedExpMap=self.obs.BinnedMapfile,
                            irfs=self.obs.irfs)
            Fit = BinnedAnalysis(Obs,
                                 self.obs.xmlfile,
                                 optimizer=self.config['fitting']['optimizer'])

        #create a unbinnedAnalysis object
        if self.config['analysis']['likelihood'] == 'unbinned':
            Obs = UnbinnedObs(self.obs.mktimefile,
                              self.obs.ft2,
                              expMap=self.obs.Mapname,
                              expCube=self.obs.Cubename,
                              irfs=self.obs.irfs)
            Fit = UnbinnedAnalysis(
                Obs,
                self.obs.xmlfile,
                optimizer=self.config['fitting']['optimizer'])

# Fix this, EBL absorbed models use LogParabola with b=0 instead of PowerLaw, we may want to allow fixed shape for that case
        if float(self.config['Spectrum']['FrozenSpectralIndex'] > 0
                 ) and self.config['target']['spectrum'] == "PowerLaw":
            parameters = dict()
            parameters['Index'] = -float(
                self.config['Spectrum']['FrozenSpectralIndex'])
            # parameters['alpha']  = +float(self.config['Spectrum']['FrozenSpectralIndex'])
            # parameters['Index1'] = -float(self.config['Spectrum']['FrozenSpectralIndex'])
            # parameters['beta']   = 0
            # parameters['Index2'] = 2.
            # parameters['Cutoff'] = 30000. # set the cutoff to be high

            for key in parameters.keys():
                try:
                    IdGamma = utils.getParamIndx(Fit, self.obs.srcname, key)
                    Fit[IdGamma] = parameters[key]  # set the parameter
                    Fit[IdGamma].setFree(
                        0)  #the variable index is frozen to compute the UL
                except:
                    continue
                else:
                    self.info("Freezing %s at %s"\
                            %(key,str(self.config['Spectrum']['FrozenSpectralIndex'])))
        return Fit  #return the BinnedAnalysis or UnbinnedAnalysis object.
Exemplo n.º 3
0
    def CreateLikeObject(self):
        """Create an UnbinnedAnalysis or a BinnedAnalysis and retrun it."""
        #create binnedAnalysis object
        if self.config['analysis']['likelihood'] == 'binned':
            use_edisp  = self.config['analysis']['EnergyDispersion'] == 'yes'
            edisp_bins = -2 if use_edisp==True else 0
            Obs = BinnedObs(srcMaps=self.obs.srcMap,
                            expCube=self.obs.Cubename,
                            binnedExpMap=self.obs.BinnedMapfile,
                            irfs=self.obs.irfs)
            Cfg = BinnedConfig(use_edisp=use_edisp, 
                               edisp_bins=edisp_bins)
            Fit = BinnedAnalysis(Obs, self.obs.xmlfile, config=Cfg,
                                 optimizer=self.config['fitting']['optimizer'])
            
            Fit.setEnergyRange(self.obs.Emin,self.obs.Emax)
            print("Is edisp enabled? {0}".format(str(Fit.logLike.use_edisp())))
            
        #create a unbinnedAnalysis object
        if self.config['analysis']['likelihood'] == 'unbinned':
            Obs = UnbinnedObs(self.obs.mktimefile,
                              self.obs.ft2,
                              expMap=self.obs.Mapname,
                              expCube=self.obs.Cubename,
                              irfs=self.obs.irfs)
            Fit = UnbinnedAnalysis(Obs, self.obs.xmlfile,
                                   optimizer=self.config['fitting']['optimizer'])
	
	# Fix this, EBL absorbed models use LogParabola with b=0 instead of PowerLaw, 
        # we may want to allow fixed shape for that case
        if float(self.config['Spectrum']['FrozenSpectralIndex']!=0): 
            parameters = dict()
            parameters['Index']  = -float(self.config['Spectrum']['FrozenSpectralIndex'])
            parameters['alpha']  = +float(self.config['Spectrum']['FrozenSpectralIndex'])
            parameters['Index1'] = -float(self.config['Spectrum']['FrozenSpectralIndex'])
            parameters['beta']   = 0
            parameters['Index2'] = 2.
            parameters['Cutoff'] = 30000. # set the cutoff to be high

            for key in parameters.keys():
                IdGamma = utils.getParamIndx(Fit, self.obs.srcname, key)
                if (IdGamma == -1):
                    continue
                else:
                    self.info("Freezing %s = %s" %(str(key),str(parameters[key])))
                    Fit[IdGamma] = parameters[key] # set the parameter
                    Fit[IdGamma].setFree(False)#the variable index is frozen to compute the UL
                     
        return Fit #return the BinnedAnalysis or UnbinnedAnalysis object.
Exemplo n.º 4
0
    def CreateLikeObject(self):
        """Create an UnbinnedAnalysis or a BinnedAnalysis and retrun it."""

        #create binnedAnalysis object
        if self.config['analysis']['likelihood'] == 'binned':
            Obs = BinnedObs(srcMaps=self.obs.srcMap,
                            expCube=self.obs.Cubename,
                            binnedExpMap=self.obs.BinnedMapfile,
                            irfs=self.obs.irfs)
            Fit = BinnedAnalysis(Obs, self.obs.xmlfile,
                                 optimizer=self.config['fitting']['optimizer'])

        #create a unbinnedAnalysis object
        if self.config['analysis']['likelihood'] == 'unbinned':
            Obs = UnbinnedObs(self.obs.eventfile,
                              self.obs.ft2,
                              expMap=self.obs.Mapname,
                              expCube=self.obs.Cubename,
                              irfs=self.obs.irfs)
            Fit = UnbinnedAnalysis(Obs, self.obs.xmlfile,
                                   optimizer=self.config['fitting']['optimizer'])

        if float(self.config['Spectrum']['FrozenSpectralIndex']) != 0:
            parameters = dict()
            parameters['Index']  = -float(self.config['Spectrum']['FrozenSpectralIndex'])
            parameters['alpha']  = +float(self.config['Spectrum']['FrozenSpectralIndex'])
            parameters['Index1'] = -float(self.config['Spectrum']['FrozenSpectralIndex'])
            parameters['beta']   = 0
            parameters['Index2'] = 2.
            parameters['Cutoff'] = 30000. # set the cutoff to be high
            
            for key in parameters.keys():
                try:
                    IdGamma = utils.getParamIndx(Fit, self.obs.srcname, key)
                    Fit[IdGamma] = parameters[key] # set the parameter
                    Fit[IdGamma].setFree(0)#the variable index is frozen to compute the UL
                except:
                    continue
                else:
                    self.info("Freezing %s at %s"\
                            %(key,str(self.config['Spectrum']['FrozenSpectralIndex'])))
        return Fit #return the BinnedAnalysis or UnbinnedAnalysis object.
Exemplo n.º 5
0
    def __init__(self, roi, **kwargs):
        """ Build a gtlike pyLikelihood object
            which is consistent with a pointlike roi. """
        keyword_options.process(self, kwargs)
        self.roi = roi

        if not roi.quiet: print 'Running a gtlike followup'

        self.old_dir = os.getcwd()
        if self.savedir is not None:
            self.savedata = True
            if not os.path.exists(self.savedir):
                os.makedirs(self.savedir)
        else:
            self.savedata = False
            self.savedir = mkdtemp(prefix=self.savedir_prefix)

        # put pfiles into savedir
        os.environ['PFILES'] = self.savedir + ';' + os.environ['PFILES'].split(
            ';')[-1]

        if not roi.quiet: print 'Saving files to ', self.savedir

        cut_ft1 = join(self.savedir, "ft1_cut.fits")
        input_srcmdl_file = join(self.savedir, 'srcmdl.xml')
        expmap = join(self.savedir, "expmap.fits")

        ltcube = roi.sa.pixeldata.ltcube
        ft2 = Gtlike.get_ft2(roi)
        irfs = Gtlike.get_gtlike_irfs(roi)

        ct = roi.sa.pixeldata.conv_type
        radius = roi.sa.maxROI
        ra = roi.roi_dir.ra()
        dec = roi.roi_dir.dec()
        emin, emax = roi.bin_edges[0], roi.bin_edges[-1]

        Gtlike.save_xml(roi, input_srcmdl_file)

        evfile = Gtlike.make_evfile(roi, self.savedir)

        if not os.path.exists(cut_ft1):
            if not roi.quiet: print 'Running gtselect'
            gtselect = GtApp('gtselect', 'dataSubselector')
            gtselect.run(infile=evfile,
                         outfile=cut_ft1,
                         ra=ra,
                         dec=dec,
                         rad=radius,
                         tmin=0,
                         tmax=0,
                         emin=emin,
                         emax=emax,
                         zmax=180,
                         convtype=ct,
                         chatter=self.chatter)
        else:
            if not roi.quiet: print '... Skiping gtselect'

        if not os.path.exists(expmap):
            # Run gtexpmap following suggestions from tutorial
            # pad 10deg on radius to account for nearby sources,
            # nlat has half degree pixels
            if not roi.quiet: print 'Running gtexpmap'
            gtexpmap = GtApp('gtexpmap')
            gtexpmap.run(
                evfile=cut_ft1,
                scfile=ft2,
                expcube=ltcube,
                outfile=expmap,
                irfs=irfs,
                srcrad=radius + 10,
                nlong=int(np.ceil(0.5 * (radius + 10) * 2)),
                nlat=int(np.ceil(0.5 * (radius + 10) * 2)),
                nenergies=int(np.ceil(np.log10(emax) - np.log10(emin))) * 4,
                chatter=self.chatter,
            )
        else:
            if not roi.quiet: print '... Skiping gtexpmap'

        gtdiffrsp = GtApp('gtdiffrsp')
        if not roi.quiet: print 'Running gtdiffrsp'
        gtdiffrsp.run(
            evfile=cut_ft1,
            scfile=ft2,
            srcmdl=input_srcmdl_file,
            irfs=irfs,
            chatter=self.chatter,
        )

        if not roi.quiet: print 'Creating Unbinned LIKE'
        obs = UnbinnedObs(eventFile=cut_ft1,
                          scFile=ft2,
                          expMap=expmap,
                          expCube=ltcube,
                          irfs=irfs)

        self.like = UnbinnedAnalysis(observation=obs,
                                     srcModel=input_srcmdl_file,
                                     optimizer=self.optimizer)

        if not roi.quiet: print 'Unbinned LIKE Created!'