Exemplo 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")

        workdir = os.path.dirname(args.config)
        _config_file = self._clone_config_and_srcmaps(args.config, args.seed)

        gta = GTAnalysis(_config_file,
                         logging={'verbosity': 3},
                         fileio={'workdir_regex': '\.xml$|\.npy$'})
        gta.load_roi(args.roi_baseline)

        simfile = os.path.join(workdir,
                               'sim_%s_%s.yaml' % (args.sim, args.sim_profile))

        mcube_file = "%s_%s_%06i" % (args.sim, args.sim_profile, args.seed)
        sim_config = utils.load_yaml(simfile)

        injected_source = sim_config.get('injected_source', None)
        if injected_source is not None:
            src_dict = injected_source['source_model']
            src_dict['ra'] = gta.config['selection']['ra']
            src_dict['dec'] = gta.config['selection']['dec']
            injected_name = injected_source['name']
            gta.add_source(injected_name, src_dict)
            gta.write_model_map(mcube_file)
            mc_spec_dict = dict(
                true_counts=gta.model_counts_spectrum(injected_name),
                energies=gta.energies,
                model=src_dict)
            mcspec_file = os.path.join(
                workdir, "mcspec_%s_%06i.yaml" % (mcube_file, args.seed))
            utils.write_yaml(mc_spec_dict, mcspec_file)
        else:
            injected_name = None

        gta.write_roi('sim_baseline_%06i' % args.seed)

        test_sources = {}
        for profile in args.profiles:
            profile_path = os.path.join(workdir, 'profile_%s.yaml' % profile)
            test_source = load_yaml(profile_path)
            test_sources[profile] = test_source
            first = args.seed
            last = first + args.nsims
            for seed in range(first, last):
                self._run_simulation(gta,
                                     args.roi_baseline,
                                     injected_name,
                                     test_sources,
                                     first,
                                     seed,
                                     non_null_src=args.non_null_src,
                                     do_find_src=args.do_find_src)
Exemplo n.º 2
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")

        workdir = os.path.dirname(args.config)
        _config_file = self._clone_config_and_srcmaps(args.config, args.seed)

        gta = GTAnalysis(_config_file, logging={'verbosity': 3},
                         fileio={'workdir_regex': '\.xml$|\.npy$'})
        gta.load_roi(args.roi_baseline)

        simfile = os.path.join(workdir, 'sim_%s_%s.yaml' %
                               (args.sim, args.sim_profile))

        mcube_file = "%s_%s_%06i" % (args.sim, args.sim_profile, args.seed)
        sim_config = utils.load_yaml(simfile)

        injected_source = sim_config.get('injected_source', None)
        if injected_source is not None:
            src_dict =  injected_source['source_model']
            src_dict['ra'] = gta.config['selection']['ra']
            src_dict['dec'] = gta.config['selection']['dec']
            injected_name = injected_source['name']
            gta.add_source(injected_name, src_dict)
            gta.write_model_map(mcube_file)
            mc_spec_dict = dict(true_counts=gta.model_counts_spectrum(injected_name),
                                energies=gta.energies,
                                model=src_dict)
            mcspec_file = os.path.join(workdir,
                                       "mcspec_%s_%06i.yaml" % (mcube_file, args.seed))
            utils.write_yaml(mc_spec_dict, mcspec_file)
        else:
            injected_name = None

        gta.write_roi('sim_baseline_%06i' % args.seed)

        test_sources = []
        for profile in args.profiles:
            profile_path = os.path.join(workdir, 'profile_%s.yaml' % profile)
            test_source = load_yaml(profile_path)
            test_sources.append(test_source)
            first = args.seed
            last = first + args.nsims
            for seed in range(first, last):
                self._run_simulation(gta, args.roi_baseline,
                                     injected_name, test_sources, first, seed,
                                     non_null_src=args.non_null_src)
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