Ejemplo n.º 1
0
    def run_analysis(self, argv):
        """Run this analysis"""
        args = self._parser.parse_args(argv)

        if not HAVE_ST:
            raise RuntimeError("Trying to run fermipy analysis, but don't have ST")

        gta = GTAnalysis(args.config,
                         logging={'verbosity': 3},
                         fileio={'workdir_regex': '\.xml$|\.npy$'})
        gta.setup(overwrite=False)
        gta.load_roi('fit_baseline')
        gta.print_roi()

        basedir = os.path.dirname(args.config)
        # This should be a no-op, b/c it was done in the baseline analysis

        gta.free_sources(skydir=gta.roi.skydir, distance=1.0, pars='norm')

        for profile in args.profiles:
            pkey, pdict = SEDAnalysis._build_profile_dict(basedir, profile)
            # test_case need to be a dict with spectrum and morphology
            gta.add_source(pkey, pdict)
            # refit the ROI
            gta.fit()
            # build the SED
            gta.sed(pkey, outfile="sed_%s.fits" % pkey)
            # remove the source
            gta.delete_source(pkey)
            # put the ROI back to how it was
            gta.load_xml('fit_baseline')

        return gta
Ejemplo n.º 2
0
class ExtensionFit:
    def __init__(self, configFile):

        self.gta = GTAnalysis(configFile, logging={'verbosity': 3})
        self.target = None
        self.targetRadius = None
        self.distance = None
        self.catalog = fits.getdata('/users-data/mfalxa/code/gll_psch_v13.fit',
                                    1)

    def setSourceName(self, sourceObject, newName):
        self.gta.delete_source(sourceObject['name'])
        self.gta.add_source(newName, sourceObject)

    ''' INITIALIZE '''

    def initialize(self, sizeROI, rInner, addToROI, TSMin, debug):

        self.gta.setup()
        if self.gta.config['selection']['emin'] >= 10000:
            self.gta.set_parameter('galdiff', 'Scale', 30000)

        if debug == True:
            self.gta.make_plots('startAll')
            self.gta.residmap(prefix='startAll', make_plots=True)

        # Get model source names
        sourceList = self.gta.get_sources(exclude=['isodiff', 'galdiff'])

        # Delete sources unassociated with TS < 50
        for i in range(len(sourceList)):
            if sourceList[i]['catalog']['TS_value'] < TSMin and self.catalog[
                    'CLASS'][self.catalog['Source_Name'] == sourceList[i]
                             ['name']][0] == '':
                self.gta.delete_source(sourceList[i]['name'])

        closests = self.gta.get_sources(distance=rInner,
                                        exclude=['isodiff', 'galdiff'])

        # Delete all unidentified sources
        for i in range(len(closests)):
            if self.catalog['CLASS'][self.catalog['Source_Name'] == closests[i]
                                     ['name']][0].isupper() == False:
                self.gta.delete_source(closests[i]['name'])
            if self.catalog['CLASS'][self.catalog['Source_Name'] == closests[i]
                                     ['name']][0] == 'SFR':
                self.target = closests[i]
                self.setSourceName(self.target, 'TESTSOURCE')

# If debug, save ROI and make plots
        if debug == True:
            self.gta.write_roi('startModel')
            self.gta.residmap(prefix='start', make_plots=True)
            self.gta.make_plots('start')

        # Optmize spectral parameters for sources with npred > 1
        self.gta.optimize(npred_threshold=1, skip=['isodiff'])

        # Get model source names
        sourceList = self.gta.get_sources(distance=sizeROI + addToROI,
                                          square=True,
                                          exclude=['isodiff', 'galdiff'])

        # Iterate source localizing on source list
        for i in range(len(sourceList)):
            if sourceList[i].extended == False:
                self.gta.localize(sourceList[i]['name'],
                                  write_fits=False,
                                  write_npy=False,
                                  update=True)

        # Free sources within ROI size + extra distance from center
        self.gta.free_sources(distance=sizeROI + addToROI, square=True)

        # Re-optimize ROI
        self.gta.optimize(skip=['isodiff'])

        # Save and make plots if debug
        if debug == True:
            self.gta.write_roi('modelInitialized')
            self.gta.residmap(prefix='initialized', make_plots=True)
            self.gta.make_plots('initialized')

        # Lock sources
        self.gta.free_sources(free=False)

    ''' OUTER REGION '''
    def outerRegionAnalysis(self, sizeROI, rInner, sqrtTsThreshold,
                            minSeparation, debug):

        self.gta.free_sources(distance=sizeROI,
                              pars='norm',
                              square=True,
                              free=True)
        self.gta.free_sources(distance=rInner, free=False)
        self.gta.free_source('galdiff', free=True)
        self.gta.free_source('isodiff', free=False)

        # Seek new sources until none are found
        sourceModel = {
            'SpectrumType': 'PowerLaw',
            'Index': 2.0,
            'Scale': 30000,
            'Prefactor': 1.e-15,
            'SpatialModel': 'PointSource'
        }
        newSources = self.gta.find_sources(sqrt_ts_threshold=sqrtTsThreshold,
                                           min_separation=minSeparation,
                                           model=sourceModel,
                                           **{
                                               'search_skydir':
                                               self.gta.roi.skydir,
                                               'search_minmax_radius':
                                               [rInner, sizeROI]
                                           })

        if len(newSources) > 0:
            for i in range(len(newSources)):
                if newSources['sources'][i]['ts'] > 100.:
                    self.gta.set_source_spectrum(
                        newSources['sources'][i]['name'],
                        spectrum_type='LogParabola')
                    self.gta.free_source(newSources['sources'][i]['name'])
                    self.gta.fit()
                    self.gta.free_source(newSources['sources'][i]['name'],
                                         free=False)

        # Optimize all ROI
        self.gta.optimize(skip=['isodiff'])

        # Save sources found
        if debug == True:
            self.gta.residmap(prefix='outer', make_plots=True)
            self.gta.write_roi('outerAnalysisROI')
            self.gta.make_plots('outer')

    ''' INNER REGION '''

    def innerRegionAnalysis(self, sizeROI, rInner, maxIter, sqrtTsThreshold,
                            minSeparation, dmMin, TSm1Min, TSextMin, debug):

        self.gta.free_sources(distance=sizeROI, square=True, free=False)
        self.gta.free_sources(distance=rInner, free=True, exclude=['isodiff'])

        # Keep closest source if identified with star forming region in catalog or look for new source closest to center within Rinner
        if self.target != None:
            print('Closest source identified with star forming region : ',
                  self.target['name'])
            self.gta.set_source_morphology('TESTSOURCE',
                                           **{'spatial_model': 'PointSource'})
        else:
            closeSources = self.gta.find_sources(sqrt_ts_threshold=2.,
                                                 min_separation=minSeparation,
                                                 max_iter=1,
                                                 **{
                                                     'search_skydir':
                                                     self.gta.roi.skydir,
                                                     'search_minmax_radius':
                                                     [0., rInner]
                                                 })
            dCenter = np.array([])
            for i in range(len(closeSources['sources'])):
                dCenter = np.append(
                    dCenter,
                    self.gta.roi.skydir.separation(
                        closeSources['sources'][i].skydir).value)
            self.target = closeSources['sources'][np.argmin(dCenter)]
            print('Target name : ', self.target['name'])
            self.setSourceName(self.target, 'TESTSOURCE')
            for i in [
                    x for x in range(len(closeSources['sources']))
                    if x != (np.argmin(dCenter))
            ]:
                self.gta.delete_source(closeSources['sources'][i]['name'])
            self.gta.optimize(skip=['isodiff'])

        # Initialize n sources array
        nSources = []

        # Save ROI without extension fit
        self.gta.write_roi('nSourcesFit')

        if debug == True:
            self.gta.make_plots('innerInit')
            self.gta.residmap(prefix='innerInit', make_plots=True)

        # Test for extension
        extensionTest = self.gta.extension('TESTSOURCE',
                                           make_plots=True,
                                           write_npy=debug,
                                           write_fits=debug,
                                           spatial_model='RadialDisk',
                                           update=True,
                                           free_background=True,
                                           fit_position=True)
        extLike = extensionTest['loglike_ext']
        TSext = extensionTest['ts_ext']
        print('TSext : ', TSext)
        extAIC = 2 * (len(self.gta.get_free_param_vector()) -
                      self.gta._roi_data['loglike'])
        self.gta.write_roi('extFit')

        if debug == True:
            self.gta.residmap(prefix='ext0', make_plots=True)
            self.gta.make_plots('ext0')

        self.gta.load_roi('nSourcesFit', reload_sources=True)

        for i in range(1, maxIter + 1):

            # Test for n point sources
            nSourcesTest = self.gta.find_sources(
                sources_per_iter=1,
                sqrt_ts_threshold=sqrtTsThreshold,
                min_separation=minSeparation,
                max_iter=1,
                **{
                    'search_skydir': self.gta.roi.skydir,
                    'search_minmax_radius': [0., rInner]
                })

            if len(nSourcesTest['sources']) > 0:

                if nSourcesTest['sources'][0]['ts'] > 100.:
                    self.gta.set_source_spectrum(
                        nSourcesTest['sources'][0]['name'],
                        spectrum_type='LogParabola')
                    self.gta.free_source(nSourcesTest['sources'][0]['name'])
                    self.gta.fit()
                    self.gta.free_source(nSourcesTest['sources'][0]['name'],
                                         free=False)

                if debug == True:
                    self.gta.make_plots('nSources' + str(i))

                nSources.append(nSourcesTest['sources'])
                self.gta.localize(nSourcesTest['sources'][0]['name'],
                                  write_npy=debug,
                                  write_fits=debug,
                                  update=True)
                nAIC = 2 * (len(self.gta.get_free_param_vector()) -
                            self.gta._roi_data['loglike'])
                self.gta.free_source(nSourcesTest['sources'][0]['name'],
                                     free=True)
                self.gta.residmap(prefix='nSources' + str(i), make_plots=True)
                self.gta.write_roi('n1SourcesFit')

                # Estimate Akaike Information Criterion difference between both models
                dm = extAIC - nAIC
                print('AIC difference between both models = ', dm)

                # Estimate TS_m+1
                extensionTestPlus = self.gta.extension(
                    'TESTSOURCE',
                    make_plots=True,
                    write_npy=debug,
                    write_fits=debug,
                    spatial_model='RadialDisk',
                    update=True,
                    free_background=True,
                    fit_position=True)
                TSm1 = 2 * (extensionTestPlus['loglike_ext'] - extLike)
                print('TSm+1 = ', TSm1)

                if debug == True:
                    self.gta.residmap(prefix='ext' + str(i), make_plots=True)
                    self.gta.make_plots('ext' + str(i))

                if dm < dmMin and TSm1 < TSm1Min:
                    self.gta.load_roi('extFit', reload_sources=True)
                    break
                else:

                    # Set extension test to current state and save current extension fit ROI and load previous nSources fit ROI
                    extensionTest = extensionTestPlus
                    extLike = extensionTestPlus['loglike_ext']
                    TSext = extensionTestPlus['ts_ext']
                    print('TSext : ', TSext)
                    extAIC = 2 * (len(self.gta.get_free_param_vector()) -
                                  self.gta._roi_data['loglike'])
                    self.gta.write_roi('extFit')
                    self.gta.load_roi('n1SourcesFit', reload_sources=True)
                    self.gta.write_roi('nSourcesFit')

            else:
                if TSext > TSextMin:
                    self.gta.load_roi('extFit', reload_sources=True)
                    break
                else:
                    self.gta.load_roi('nSourcesFit', reload_sources=True)
                    break

        self.gta.fit()

        # Get source radius depending on spatial model
        endSources = self.gta.get_sources()
        for i in range(len(endSources)):
            if endSources[i]['name'] == 'TESTSOURCE':
                self.target = endSources[i]
                self.distance = self.gta.roi.skydir.separation(
                    endSources[i].skydir).value
                if endSources[i].extended == True:
                    self.targetRadius = endSources[i]['SpatialWidth']
                else:
                    self.targetRadius = endSources[i]['pos_r95']

    ''' CHECK OVERLAP '''

    def overlapDisk(self, rInner, radiusCatalog):

        print('Target radius : ', self.targetRadius)

        # Check radius sizes
        if radiusCatalog < self.targetRadius:
            r = float(radiusCatalog)
            R = float(self.targetRadius)
        else:
            r = float(self.targetRadius)
            R = float(radiusCatalog)

        # Estimating overlapping area
        d = self.distance
        print('Distance from center : ', d)

        if d < (r + R):
            if R < (r + d):
                area = r**2 * np.arccos(
                    (d**2 + r**2 - R**2) / (2 * d * r)) + R**2 * np.arccos(
                        (d**2 + R**2 - r**2) / (2 * d * R)) - 0.5 * np.sqrt(
                            (-d + r + R) * (d + r - R) * (d - r + R) *
                            (d + r + R))
                overlap = round((area / (np.pi * r**2)) * 100, 2)
            else:
                area = np.pi * r**2
                overlap = 100.0
        else:
            area = 0.
            overlap = 0.

        print('Overlapping surface : ', area)
        print('Overlap : ', overlap)

        if overlap > 68. and self.distance < rInner:
            associated = True
        else:
            associated = False

        return associated

    ''' CHECK UPPER LIMIT '''

    def upperLimit(self, name, radius):
        sourceModel = {
            'SpectrumType': 'PowerLaw',
            'Index': 2.0,
            'Scale': 30000,
            'Prefactor': 1.e-15,
            'SpatialModel': 'RadialDisk',
            'SpatialWidth': radius,
            'glon': self.gta.config['selection']['glon'],
            'glat': self.gta.config['selection']['glat']
        }
        self.gta.add_source(name, sourceModel, free=True)
        self.gta.fit()
        self.gta.residmap(prefix='upperLimit', make_plots=True)
        print('Upper limit : ', self.gta.get_sources()[0]['flux_ul95'])
Ejemplo n.º 3
0
    def run_analysis(self, argv):
        """Run this analysis"""
        args = self._parser.parse_args(argv)

        if not HAVE_ST:
            raise RuntimeError(
                "Trying to run fermipy analysis, but don't have ST")

        if is_null(args.skydirs):
            skydir_dict = None
        else:
            skydir_dict = load_yaml(args.skydirs)

        gta = GTAnalysis(args.config,
                         logging={'verbosity': 3},
                         fileio={'workdir_regex': '\.xml$|\.npy$'})
        #gta.setup(overwrite=False)
        gta.load_roi(args.roi_baseline)
        gta.print_roi()

        basedir = os.path.dirname(args.config)
        # This should be a no-op, b/c it was done in the baseline analysis

        for profile in args.profiles:
            if skydir_dict is None:
                skydir_keys = [None]
            else:
                skydir_keys = sorted(skydir_dict.keys())

            for skydir_key in skydir_keys:
                if skydir_key is None:
                    pkey, pdict = AnalyzeSED._build_profile_dict(
                        basedir, profile)
                else:
                    skydir_val = skydir_dict[skydir_key]
                    pkey, pdict = AnalyzeSED._build_profile_dict(
                        basedir, profile)
                    pdict['ra'] = skydir_val['ra']
                    pdict['dec'] = skydir_val['dec']
                    pkey += "_%06i" % skydir_key

                outfile = "sed_%s.fits" % pkey

                # Add the source and get the list of correlated soruces
                correl_dict = add_source_get_correlated(gta,
                                                        pkey,
                                                        pdict,
                                                        correl_thresh=0.25)

                # Write the list of correlated sources
                correl_yaml = os.path.join(basedir, "correl_%s.yaml" % pkey)
                write_yaml(correl_dict, correl_yaml)

                gta.free_sources(False)
                for src_name in correl_dict.keys():
                    gta.free_source(src_name, pars='norm')

                # build the SED
                gta.sed(pkey, outfile=outfile, make_plots=args.make_plots)

                # remove the source
                gta.delete_source(pkey)
                # put the ROI back to how it was
                gta.load_xml(args.roi_baseline)

        return gta
Ejemplo n.º 4
0
    def run_analysis(self, argv):
        """Run this analysis"""
        args = self._parser.parse_args(argv)

        if not HAVE_ST:
            raise RuntimeError(
                "Trying to run fermipy analysis, but don't have ST")

        if is_null(args.skydirs):
            skydir_dict = None
        else:
            skydir_dict = load_yaml(args.skydirs)

        gta = GTAnalysis(args.config,
                         logging={'verbosity': 3},
                         fileio={'workdir_regex': '\.xml$|\.npy$'})
        #gta.setup(overwrite=False)
        gta.load_roi(args.roi_baseline)
        gta.print_roi()

        basedir = os.path.dirname(args.config)
        # This should be a no-op, b/c it was done in the baseline analysis

        for profile in args.profiles:
            if skydir_dict is None:
                skydir_keys = [None]
            else:
                skydir_keys = sorted(skydir_dict.keys())

            for skydir_key in skydir_keys:
                if skydir_key is None:
                    pkey, psrc_name, pdict = build_profile_dict(basedir, profile)
                else:
                    skydir_val = skydir_dict[skydir_key]
                    pkey, psrc_name, pdict = build_profile_dict(basedir, profile)
                    pdict['ra'] = skydir_val['ra']
                    pdict['dec'] = skydir_val['dec']
                    pkey += "_%06i" % skydir_key

                outfile = "sed_%s.fits" % pkey

                # Add the source and get the list of correlated soruces
                correl_dict, test_src_name = add_source_get_correlated(gta, psrc_name, 
                                                                       pdict, correl_thresh=0.25, 
                                                                       non_null_src=args.non_null_src)

                # Write the list of correlated sources
                correl_yaml = os.path.join(basedir, "correl_%s.yaml" % pkey)
                write_yaml(correl_dict, correl_yaml)

                gta.free_sources(False)
                for src_name in correl_dict.keys():
                    gta.free_source(src_name, pars='norm')

                # build the SED
                if args.non_null_src:
                    gta.update_source(test_src_name, reoptimize=True)
                    gta.write_roi("base_%s"% pkey, make_plots=False)
                gta.sed(test_src_name, prefix=pkey, outfile=outfile, make_plots=args.make_plots)

                # remove the source
                gta.delete_source(test_src_name)
                # put the ROI back to how it was
                gta.load_xml(args.roi_baseline)

        return gta
def main(cmd_line):

    #takes integer arguement specifying the simulation number
    sim = cmd_line[1]

    indir = "/zfs/astrohe/ckarwin/Machine_Learning_GC/Sim_2/Dame_Maps/"
    outdir = indir + "Simulation_Output/sim_%s" % sim

    if (os.path.isdir(outdir) == True):
        shutil.rmtree(outdir)
    os.system('mkdir %s' % outdir)
    os.chdir(outdir)

    #A single simulation should first be ran, which will generate all the needed data products that can be reused for subsequent simulations.
    #The data products that are copied below are for subsequent simulations after the first run.
    shutil.copy2('%s/srcmap_00.fits' % indir, 'srcmap_00.fits')
    shutil.copy2('%s/bexpmap_00.fits' % indir, 'bexpmap_00.fits')
    shutil.copy2('%s/ccube_00.fits' % indir, 'ccube_00.fits')
    shutil.copy2('%s/config.yaml' % indir, 'config.yaml')
    shutil.copy2('%s/ft1_00.fits' % indir, 'ft1_00.fits')
    shutil.copy2('%s/LAT_Final_Excess_Template.fits' % indir,
                 'LAT_Final_Excess_Template.fits')

    #setup analysis:
    gta = GTAnalysis('config.yaml', logging={'verbosity': 3})
    gta.setup()
    #gta.load_roi("after_setup")

    #set components to zero for simulations:
    gta.set_norm("MapSource", 0.0)  #excess template
    gta.set_norm("galdiff04", 0.0)  #CO12_0-5
    gta.set_norm("galdiff05", 0.0)  #CO12_6-9
    gta.set_norm("galdiff06", 0.0)  #CO12_10-12
    gta.set_norm("galdiff07", 0.0)  #CO12_13-16

    #run simulations:
    gta.write_roi('before_sim')
    gta.simulate_roi(randomize=True)

    #delete sources that were simulated:
    gta.delete_source("galdiff00", delete_source_map=False)
    gta.delete_source("galdiff01", delete_source_map=False)
    gta.delete_source("galdiff02", delete_source_map=False)
    gta.delete_source("galdiff03", delete_source_map=False)

    #set random normalizations of sources for performing fit:
    #n4 =  np.random.normal(1.0,0.2)
    #n5 =  np.random.normal(1.0,0.2)
    #n6 =  np.random.normal(1.0,0.2)
    #nms = np.random.normal(1e-4,0.5e-4)
    gta.set_norm("galdiff04", 0.8)
    gta.set_norm("galdiff05", 0.8)
    gta.set_norm("galdiff06", 1.2)
    gta.set_norm("galdiff07", 1.2)

    #perform fit for null hypothesis:
    gta.free_sources(free=True)
    gta.free_source("galdiff07", free=False)
    gta.free_source("MapSource", free=False)
    Fit = gta.fit()
    null = Fit["loglike"]
    gta.write_roi('after_null_fit')
    gta.write_model_map("null_model")

    #set normalizations of sources for performing alternative fit:
    gta.set_norm("galdiff04", 0.8)
    gta.set_norm("galdiff05", 0.8)
    gta.set_norm("galdiff06", 1.2)
    gta.set_norm("galdiff07", 1.2)
    gta.set_norm("MapSource", 1e-4)

    gta.free_sources(free=True)
    #gta.free_source("galdiff07",free=False)
    Fit2 = gta.fit()
    alternative = Fit2["loglike"]
    gta.write_roi('after_alternative_fit')
    gta.write_model_map("alternative_model")

    #calculate source spectrum:
    ltcube = '/zfs/astrohe/ckarwin/Stacking_Analysis/UFOs/NGC_4151_Analysis/MakeLTCube/zmax_105/UFOs_binned_ltcube.fits'
    obs = BinnedObs(srcMaps='srcmap_00.fits',
                    expCube=ltcube,
                    binnedExpMap='bexpmap_00.fits',
                    irfs='P8R3_SOURCE_V2')
    like = BinnedAnalysis(obs,
                          'after_alternative_fit_00.xml',
                          optimizer='MINUIT')
    Elist, Flist = CalcFlux(like, 'MapSource')
    data = {"energ[MeV]": Elist, "flux[MeV/cm^2/s]": Flist}
    df = pd.DataFrame(data=data)
    df.to_csv("excess_flux.dat", sep="\t", index=False)

    #calculte TS:
    TS = -2 * (null - alternative)

    #write results:
    savefile = "TS_sim_%s.txt" % sim
    f = open(savefile, "w")
    f.write(str(TS))
    f.close()

    #rm ft file to reduce storage:
    os.system('rm ft1_00.fits')

    return