Ejemplo n.º 1
0
def main():

    usage = "usage: %(prog)s [config file]"
    description = "Run fermipy analysis chain."
    parser = argparse.ArgumentParser(usage=usage, description=description)

    parser.add_argument('--config', default='sample_config.yaml')
    parser.add_argument('--source', default=None)

    args = parser.parse_args()
    gta = GTAnalysis(args.config)

    if args.source is None:
        src_name = gta.roi.sources[0].name

    gta.setup()
    gta.optimize()

    loc = gta.localize(src_name, free_radius=1.0, update=True, make_plots=True)

    model = {'Index': 2.0, 'SpatialModel': 'PointSource'}
    srcs = gta.find_sources(model=model,
                            sqrt_ts_threshold=5.0,
                            min_separation=0.5)

    sed = gta.sed(src_name, free_radius=1.0, make_plots=True)
    gta.tsmap(make_plots=True)
    gta.write_roi('fit0')
    lc = gta.lightcurve(src_name,
                        binsz=86400. * 7.0,
                        free_radius=3.0,
                        use_scaled_srcmap=True,
                        multithread=False)
Ejemplo n.º 2
0
def main():
        
    usage = "usage: %(prog)s [config file]"
    description = "Run fermipy analysis chain."
    parser = argparse.ArgumentParser(usage=usage,description=description)

    parser.add_argument('--config', default = 'sample_config.yaml')
    parser.add_argument('--source', default = None)

    args = parser.parse_args()
    gta = GTAnalysis(args.config)

    if args.source is None:
        src_name = gta.roi.sources[0].name
    
    gta.setup()
    gta.optimize()

    if (gta.roi[src_name]['ts'] > 1000. and
        gta.roi[src_name]['SpectrumType'] == 'PowerLaw'):
        gta.set_source_spectrum(src_name, spectrum_type='LogParabola',
                                spectrum_pars={'beta' : {'value' : 0.0, 'scale' : 1.0,
                                                         'min' : 0.0, 'max' : 2.0}})

    gta.free_source(src_name)
    gta.fit()
    gta.free_source(src_name, False)

    loc = gta.localize(src_name, free_radius=1.0, update=True, make_plots=True)

    model = {'Index' : 2.0, 'SpatialModel' : 'PointSource'}
    srcs = gta.find_sources(model=model, sqrt_ts_threshold=5.0,
                            min_separation=0.5)
    
    sed = gta.sed(src_name, free_radius=1.0, make_plots=True)
    gta.tsmap(make_plots=True)
    gta.tsmap(prefix='excludeSource', exclude=[src_name], make_plots=True)

    gta.write_roi('fit0')    
    lc = gta.lightcurve(src_name, binsz=86400.*28.0, free_radius=3.0, use_scaled_srcmap=True,
                        multithread=False)
Ejemplo n.º 3
0
def run_analysis(config):
    print('Running analysis...')

    gta = GTAnalysis(config)
    gta.setup()
    gta.optimize()

    gta.print_roi()

    # Localize and generate SED for first source in ROI
    srcname = gta.roi.sources[0].name

    gta.free_source(srcname)
    gta.fit()

    gta.localize(srcname)
    gta.sed(srcname)

    gta.write_roi('roi', make_plots=True)
    gta.tsmap(make_plots=True)
    gta.residmap(make_plots=True)
Ejemplo n.º 4
0
def run_analysis(config):
    print('Running analysis...')

    gta = GTAnalysis(config)
    gta.setup()
    gta.optimize()

    gta.print_roi()

    # Localize and generate SED for first source in ROI
    srcname = gta.roi.sources[0].name

    gta.free_source(srcname)
    gta.fit()

    gta.localize(srcname)
    gta.sed(srcname)

    gta.write_roi('roi', make_plots=True)
    gta.tsmap(make_plots=True)
    gta.residmap(make_plots=True)
Ejemplo n.º 5
0
def main():

    usage = "usage: %(prog)s [config file]"
    description = "Run fermipy analysis chain."
    parser = argparse.ArgumentParser(usage=usage, description=description)

    parser.add_argument('--config', default='sample_config.yaml')
    parser.add_argument('--source', default=None)

    args = parser.parse_args()
    gta = GTAnalysis(args.config)

    if args.source is None:
        src_name = gta.roi.sources[0].name

    gta.setup()
    gta.optimize()

    loc = gta.localize(src_name, free_radius=1.0, update=True, make_plots=True)

    model = {'Index': 2.0, 'SpatialModel': 'PointSource'}
    srcs = gta.find_sources(model=model,
                            sqrt_ts_threshold=5.0,
                            min_separation=0.5)

    sed = gta.sed(src_name, free_radius=1.0, make_plots=True)
    gta.tsmap(make_plots=True)
    gta.write_roi('fit0')
    # make sure bins are shifted to line up with the end of the time window (where the neutrino arrived)
    tmax = 528835414
    LCbins = tmax - 119 * 28 * 24 * 3600 + numpy.linspace(0, 119,
                                                          120) * 28 * 24 * 3600
    lc = gta.lightcurve(src_name,
                        time_bins=list(LCbins),
                        free_radius=3.0,
                        use_scaled_srcmap=True,
                        multithread=False,
                        shape_ts_threshold=100)
Ejemplo n.º 6
0
def main():
        
    usage = "usage: %(prog)s [config file]"
    description = "Run fermipy analysis chain."
    parser = argparse.ArgumentParser(usage=usage,description=description)

    parser.add_argument('--config', default = 'sample_config.yaml')
    parser.add_argument('--source', default = None)

    args = parser.parse_args()
    gta = GTAnalysis(args.config,logging={'verbosity' : 3},
                     fileio={'workdir_regex' : '\.xml$|\.npy$'})

    gta.setup()

    names = [s.name for s in gta.roi.sources if not s.diffuse]
    gta.reload_sources(names)
    
    sqrt_ts_threshold=3
    
    model0 = { 'SpatialModel' : 'PointSource', 'Index' : 1.5 }
    model1 = { 'SpatialModel' : 'PointSource', 'Index' : 2.0 }
    model2 = { 'SpatialModel' : 'PointSource', 'Index' : 2.5 }
    #src_name = gta.roi.sources[0].name
    if args.source is None:
        src_name = gta.config['selection']['target']
    else:
        src_name = args.source
        
    # -----------------------------------
    # Fit the Baseline Model
    # -----------------------------------

    # Get a reasonable starting point for the spectral model
    gta.free_source(src_name)
    gta.fit()
    gta.free_source(src_name,False)

    gta.optimize()
    
    # Localize 3FGL sources
    for s in gta.roi.sources:

        if not s['SpatialModel'] == 'PointSource':
            continue

        if s['offset'] < 0.5 or s['ts'] < 25.:
            continue

        if s['offset_roi_edge'] > -0.1:
            continue
        
        gta.localize(s.name,nstep=5,dtheta_max=0.5,update=True,
                     prefix='base')

        gta.free_source(s.name,False)

    gta.tsmap('base',model=model1)

    # Look for new point sources outside the inner 1.0 deg

    gta.find_sources('base',model=model1,
                     search_skydir=gta.roi.skydir,
                     max_iter=5,min_separation=0.5,
                     sqrt_ts_threshold=sqrt_ts_threshold,
                     search_minmax_radius=[1.0,None])
    gta.optimize()

    gta.print_roi()

    gta.write_roi('base')

    # -----------------------------------
    # Pass 0 - Source at Nominal Position
    # -----------------------------------

    fit_region(gta,'fit0',src_name)

    # -------------------------------------
    # Pass 1 - Source at Localized Position
    # -------------------------------------

    gta.localize(src_name,nstep=5,dtheta_max=0.5,update=True,
                 prefix='fit1')

    fit_region(gta,'fit1',src_name)
    fit_halo(gta,'fit1',src_name)
    gta.load_roi('fit1')

    # -------------------------------------
    # Pass 2 - 2+ Point Sources
    # -------------------------------------

    srcs = []

    # Fit up to 4 sources
    for i in range(2,6):

        srcs_fit = gta.find_sources('fit%i'%i,
                                    search_skydir=gta.roi.skydir,
                                    max_iter=1,
                                    sources_per_iter=1,
                                    sqrt_ts_threshold=3,
                                    min_separation=0.5,
                                    search_minmax_radius=[None,1.0])

        if len(srcs_fit['sources']) == 0:
            break

        srcs += srcs_fit['sources']
        best_fit_idx = i
        
        gta.localize(src_name,nstep=5,dtheta_max=0.4,
                     update=True,prefix='fit%i'%i)

        # Relocalize new sources
        for s in sorted(srcs, key=lambda t: t['ts'],reverse=True):        
            gta.localize(s.name,nstep=5,dtheta_max=0.4,
                         update=True,prefix='fit%i'%i)

        fit_region(gta,'fit%i'%i,src_name)
        fit_halo(gta,'fit%i'%i,src_name)

        gta.load_roi('fit%i'%i)
        
    new_source_data = []
    for s in srcs:
        src_data = gta.roi[s.name].data
        new_source_data.append(copy.deepcopy(src_data))

    np.save(os.path.join(gta.workdir,'new_source_data.npy'),
            new_source_data)
Ejemplo n.º 7
0
        if s['offset'] < 0.5 or s['ts'] < 25.:
            continue

        if np.abs(s['offset_glon']) > 0.5*roiwidth-0.2:
            continue

        if np.abs(s['offset_glat']) > 0.5*roiwidth-0.2:
            continue
        
        gta.localize(s.name,nstep=5,dtheta_max=0.5,update=True,
                     prefix='base')

        gta.free_source(s.name,False)

    gta.tsmap('base',model=model1)
    #gta.tsmap('base_emin40',model=model1,erange=[4.0,5.5])

    # Look for new point sources outside the inner 1.0 deg

    gta.find_sources('base',model=model1,
                     search_skydir=gta.roi.skydir,
                     max_iter=4,min_separation=0.5,
                     sqrt_ts_threshold=sqrt_ts_threshold,
                     search_minmax_radius=[1.0,None])
    gta.optimize()

    gta.print_roi()

    gta.write_roi('base')
Ejemplo n.º 8
0
config['fileio']['outdir'] = cwd+'/fits'
config['fileio']['logfile'] = cwd+'/fits/fermipy.log'
config['data']['ltcube'] = cwd+'/fits/ltcube_00.fits'
config['model']['galdiff'] = path_to_conda+'/share/fermitools/refdata/fermi/galdiffuse/gll_iem_v07.fits'
config['model']['isodiff'] = path_to_conda+'/share/fermitools/refdata/fermi/galdiffuse/iso_P8R3_SOURCE_V3_v1.txt'
config['logging']['verbosity'] = 4
source = config['selection']['target']
with open(cwd+'/config_modified.yaml', 'w') as o:
    yaml.dump(config, o)
likelihoods = np.zeros((5))
gta = GTAnalysis(config='config_modified.yaml')
gta.setup()
model = {'Index' : 2.0, 'SpatialModel' : 'PointSource'}

for i in range(1,6):
    gta.optimize()
    gta.free_sources(free=False)
    gta.free_source(source)
    gta.free_source('galdiff')
    gta.free_source('isodiff')
    gta.free_sources(distance=3, pars='norm')
    gta.free_sources(minmax_ts=[100, None], pars='norm')
    gta.fit(optimizer='NEWMINUIT', reoptimize=True)
    maps = gta.residmap(f'../maps/opt_alternating{i}', model=model, make_plots=True)
    maps = gta.tsmap(f'../maps/opt_alternating_{i}', model=model, make_plots=True)
    gta.write_roi(f'opt_{i}', make_plots=True)
    likelihoods[i-1] = - gta.like()


np.savetxt('optimization_process_likes_alternating.dat', likelihoods)
Ejemplo n.º 9
0
def FGES_BinnedAnalysis(prefix, ANALYSISDIR, numsources, xmlsources, spectrum,
                        spectrumpoints, spectrumpointsUL, spectrum_mev_or_erg,
                        spectrum_mev_or_tev, configfile):

    ANALYSISDIR = ANALYSISDIR + prefix + '/'
    i = numsources  #number of sources
    sources_names = ''
    for x in range(0, i):
        sources_names += str(xmlsources[x])

    #Run the likelihood analysis up to doing the fit
    gta = GTAnalysis(ANALYSISDIR + configfile, logging={'verbosity': 3})
    gta.setup()

    #Print the pre likelihood fit parameters
    gta.print_roi()
    for x in range(0, i):
        print(gta.roi[xmlsources[x]])

    #Do an initial optimization of parameters
    gta.optimize()

    gta.print_roi()

    #Prepare to get the likelihood
    #Free the normalizations of sources within 7 degrees of the center of the field of view
    gta.free_sources(distance=7.0, pars='norm')
    gta.free_source('galdiff')
    gta.free_source('isodiff')
    for x in range(0, i):
        gta.free_source(xmlsources[x])

    #LIKELIHOOD ANALYSIS
    fit_results = gta.fit()

    #print out and return the results
    print('Fit Quality: ', fit_results['fit_quality'])
    for x in range(0, i):
        print(gta.roi[xmlsources[x]])
    gta.write_roi(sources_names + 'fit')

    #RESIDUAL MAP
    model = {'Index': 2.0, 'SpatialModel': 'PointSource'}
    maps = gta.residmap('residual', model=model, make_plots=True)

    # Generate residual map with source of interest removed from the model
    model_nosource = {'Index': 2.0, 'SpatialModel': 'PointSource'}
    maps_nosource = gta.residmap('residual_wsource',
                                 model=model_nosource,
                                 exclude=xmlsources,
                                 make_plots=True)

    #TS Map
    tsmap = gta.tsmap('tsmap',
                      model={
                          'SpatialModel': 'PointSource',
                          'Index': 2.0
                      },
                      exclude=xmlsources,
                      make_plots=True)
    tsmap_wSNR = gta.tsmap('tsmap_wSNR',
                           model={
                               'SpatialModel': 'PointSource',
                               'Index': 2.0
                           },
                           make_plots=True)

    #PLOT SEDs
    for x in range(0, i):
        c = np.load('10to500gev/' + sources_names + 'fit.npy').flat[0]
        sorted(c['sources'].keys())
        c['sources'][xmlsources[x]]['flux']
        print(c['sources'][xmlsources[x]]['param_names'][:4])
        print(c['sources'][xmlsources[x]]['param_values'][:4])
        c['sources'][xmlsources[x]]['ts']

        E = np.array(c['sources'][xmlsources[x]]['model_flux']['energies'])
        dnde = np.array(c['sources'][xmlsources[x]]['model_flux']['dnde'])
        dnde_hi = np.array(
            c['sources'][xmlsources[x]]['model_flux']['dnde_hi'])
        dnde_lo = np.array(
            c['sources'][xmlsources[x]]['model_flux']['dnde_lo'])

        if spectrum_mev_or_erg == "erg":
            suffix = 'erg'
            mult = 0.00000160218
        elif spectrum_mev_or_erg == "mev":
            suffix = 'MeV'
            mult = 1

        if spectrum_mev_or_tev == "mev":
            xaxis = 'MeV'
            denominator = 1
        elif spectrum_mev_or_tev == "tev":
            xaxis = 'TeV'
            denominator = 1000000

        if spectrum:
            plt.loglog(E, (E**2) * dnde, 'k--')
            plt.loglog(E, (E**2) * dnde_hi, 'k')
            plt.loglog(E, (E**2) * dnde_lo, 'k')
            plt.xlabel('E [MeV]')
            plt.ylabel(r'E$^2$ dN/dE [MeV cm$^{-2}$ s$^{-1}$]')
            plt.savefig('spectrum_' + xmlsources[x] + '.png')

        #GET SED POINTS
        if spectrumpoints:
            sed = gta.sed(xmlsources[x], make_plots=True)
            #sed = gta.sed(xmlsource,prefix=xmlsource + 'spectrum',loge_bins=)
            src = gta.roi[xmlsources[x]]
            #Plot without upper limits
            plt.loglog(E, (E**2) * dnde, 'k--')
            plt.loglog(E, (E**2) * dnde_hi, 'k')
            plt.loglog(E, (E**2) * dnde_lo, 'k')
            plt.errorbar(np.array(sed['e_ctr']),
                         sed['e2dnde'],
                         yerr=sed['e2dnde_err'],
                         fmt='o')
            plt.xlabel('E [MeV]')
            plt.ylabel(r'E$^{2}$ dN/dE [MeV cm$^{-2}$ s$^{-1}$]')
            #plt.show()
            plt.savefig('spectrumpoints_' + xmlsources[x] + '.png')
            #Plot with upper limits, last 5 points
            plt.loglog(E, (E**2) * dnde, 'k--')
            plt.loglog(E, (E**2) * dnde_hi, 'k')
            plt.loglog(E, (E**2) * dnde_lo, 'k')
            plt.errorbar(sed['e_ctr'][:-5],
                         sed['e2dnde'][:-5],
                         yerr=sed['e2dnde_err'][:-5],
                         fmt='o')
            plt.errorbar(np.array(sed['e_ctr'][-5:]),
                         sed['e2dnde_ul95'][-5:],
                         yerr=0.2 * sed['e2dnde_ul95'][-5:],
                         fmt='o',
                         uplims=True)
            plt.xlabel('E [MeV]')
            plt.ylabel(r'E$^{2}$ dN/dE [MeV cm$^{-2}$ s$^{-1}$]')
            plt.savefig('spectrumpointsUL_' + xmlsources[x] + '.png')
        plt.clf()
Ejemplo n.º 10
0
def main():

    usage = "usage: %(prog)s [config file]"
    description = "Run fermipy analysis chain."
    parser = argparse.ArgumentParser(usage=usage, description=description)

    parser.add_argument('--config', default='sample_config.yaml')
    parser.add_argument('--source', default=None)

    args = parser.parse_args()
    gta = GTAnalysis(args.config,
                     logging={'verbosity': 3},
                     fileio={'workdir_regex': '\.xml$|\.npy$'})

    model0 = {'SpatialModel': 'PointSource', 'Index': 1.5}
    model1 = {'SpatialModel': 'PointSource', 'Index': 2.0}
    model2 = {'SpatialModel': 'PointSource', 'Index': 2.7}

    src_name = gta.config['selection']['target']

    gta.setup(overwrite=True)
    gta.free_sources(False)
    gta.print_roi()
    gta.optimize()
    gta.print_roi()

    exclude = []

    # Localize all point sources
    for s in sorted(gta.roi.sources, key=lambda t: t['ts'], reverse=True):
        #    for s in gta.roi.sources:

        if not s['SpatialModel'] == 'PointSource':
            continue
        if s['offset_roi_edge'] > -0.1:
            continue

        if s.name in exclude:
            continue
        if not '3FGL' in s.name:
            continue
        if s.name == src_name:
            continue

        gta.localize(s.name,
                     nstep=5,
                     dtheta_max=0.5,
                     update=True,
                     prefix='base',
                     make_plots=True)

    gta.optimize()
    gta.print_roi()

    gta.write_roi('base_roi', make_plots=True)

    exclude = [src_name]
    if not 'carina_2' in exclude:
        exclude += ['carina_2']
    if not 'carina_3' in exclude:
        exclude += ['carina_3']

    gta.tsmap('base', model=model0, make_plots=True, exclude=exclude)
    gta.residmap('base', model=model0, make_plots=True, exclude=exclude)
    gta.tsmap('base', model=model1, make_plots=True, exclude=exclude)
    gta.residmap('base', model=model1, make_plots=True, exclude=exclude)
    gta.tsmap('base', model=model2, make_plots=True, exclude=exclude)
    gta.residmap('base', model=model2, make_plots=True, exclude=exclude)

    gta.find_sources(sqrt_ts_threshold=5.0)
    gta.optimize()
    gta.print_roi()
    gta.print_params()

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

    gta.write_roi('fit0_roi', make_plots=True)

    m = gta.tsmap('fit0', model=model0, make_plots=True, exclude=exclude)
    gta.plotter.make_tsmap_plots(m, gta.roi, zoom=2, suffix='tsmap_zoom')
    gta.residmap('fit0', model=model0, make_plots=True, exclude=exclude)
    gta.tsmap('fit0', model=model1, make_plots=True, exclude=exclude)
    gta.plotter.make_tsmap_plots(m, gta.roi, zoom=2, suffix='tsmap_zoom')
    gta.residmap('fit0', model=model1, make_plots=True, exclude=exclude)
    gta.tsmap('fit0', model=model2, make_plots=True, exclude=exclude)
    gta.plotter.make_tsmap_plots(m, gta.roi, zoom=2, suffix='tsmap_zoom')
    gta.residmap('fit0', model=model2, make_plots=True, exclude=exclude)

    gta.sed(src_name, prefix='fit0', make_plots=True, free_radius=1.0)

    gta.free_source(src_name)
    gta.fit(reoptimize=True)
    gta.print_roi()
    gta.print_params()

    gta.write_roi('fit1_roi', make_plots=True)
Ejemplo n.º 11
0
    gta.setup()

    sqrt_ts_threshold = 3

    halo_width = np.logspace(-1, 0, 9)
    halo_index = np.array([1.5, 1.75, 2.0, 2.25, 2.5, 2.75, 3.0])

    model0 = {"SpatialModel": "PointSource", "Index": 1.5}
    model1 = {"SpatialModel": "PointSource", "Index": 2.0}
    model2 = {"SpatialModel": "PointSource", "Index": 2.5}
    src_name = gta.roi.sources[0].name

    gta.load_roi("base", reload_sources=True)
    # gta.tsmap('base',model=model1)
    gta.tsmap("base_emin40", model=model1, erange=[4.0, 5.5])

    gta.print_roi()

    # -----------------------------------
    # Pass 0 - Source at Nominal Position
    # -----------------------------------

    gta.load_roi("fit0", reload_sources=True)

    # fit_region(gta,'fit0',src_name)
    fit_region(gta, "fit0_emin40", src_name, erange=[4.0, 5.5])

    # -------------------------------------
    # Pass 1 - Source at Localized Position
    # -------------------------------------