Ejemplo n.º 1
0
def setup_b_instance(ebin_min):
    global b, f, run_tag
    
    run_tag=run_tag_base + '-'+str(ebin_min) #set the run tag using ebin_min
    f = copy.deepcopy(f_total) #copy f_total and reduce to energy subset
    f.reduce_to_energy_subset(ebin_min-CTB_start_bin,ebin_min+1-CTB_start_bin)
    
    b = bsm.bayesian_scan_NPTF(tag=tag,nside=nside,work_dir=work_dir,psf_dir=psf_dir,nlive=nlive)
    b.load_external_data(f.CTB_en_bins,f.CTB_count_maps,f.CTB_exposure_maps)
    ##b.make_mask_total(band_mask_range =  band_mask_range,mask_ring = mask_ring,ps_mask_array = f.ps_mask_array)

    b.make_mask_total(band_mask_range =  band_mask_range,mask_ring = mask_ring,ps_mask_array = f.ps_mask_array)
    
    b.add_new_template(f.template_dict)
    b.rebin_external_data(1)


    ##############
    temp_key_list = ['ps_model','p7','bubs','iso']
    triangle_name = ['$A_{ps}$','$A_{diff}$','$A_{bubs}$','$A_{iso}$']
    prior_list = [[0,3],[0,2],[0,3],[0,15]]
    prior_is_log = [False,False,False,False]


    for key in b.templates_dict.keys():
        b.templates_dict[key] = np.array([b.templates_dict[key][0]])

    for i in range(len(temp_key_list)):
        b.add_poiss_model(temp_key_list[i],triangle_name[i],prior_list[i],prior_is_log[i])

    b.initiate_poissonian_edep()
Ejemplo n.º 2
0
def setup_b_instance(ebin_min):
    global b, f, run_tag
    
    run_tag=run_tag_base + '-'+str(ebin_min) #set the run tag using ebin_min
    f = copy.deepcopy(f_total) #copy f_total and reduce to energy subset
    f.reduce_to_energy_subset(ebin_min,ebin_min+1)
    
    if fake_data:
        print 'Using fake data!'
        data = np.loadtxt(fake_data_path)
        # Data is often in a different nside - convert it here
        data = np.array([hp.ud_grade(dat,nside,power=-2) for dat in data])
    else:
        print 'Using real data!'
        data = f.CTB_count_maps

    b = bsm.bayesian_scan_NPTF(tag=tag,nside=nside,work_dir=work_dir,psf_dir=psf_dir)
    b.load_external_data(f.CTB_en_bins,data,f.CTB_exposure_maps)
    b.make_mask_total(band_mask_range =  band_mask_range,mask_ring = mask_ring,outer=outer,ps_mask_array = f.ps_mask_array)
    
    b.add_new_template(f.template_dict)
    b.rebin_external_data(1)
    
    b.add_poiss_model('nfw','$A_{nfw}$',[-6,6],True)
    b.add_poiss_model('p6','$A_{p6}$',[0,2],False)
    b.add_poiss_model('bubs','$A_{bubs}$',[0,2],False)
    b.add_poiss_model('iso','$A_{iso}$',[-2,2],False)
    b.add_poiss_model('ps_model','$A_{ps-model}$',[0,3],False)
    
    b.initiate_poissonian_edep()
Ejemplo n.º 3
0
    def setup_b_instance(self,norm,add_ps_mask=True):
        """ Setup an instance of bayesian scan with a fixed J_map """
        inst_tag = self.tag + '_'+str(self.flux_array_ebin)
        b = bsm.bayesian_scan_NPTF(tag=inst_tag,nside=self.nside,work_dir=work_dir,psf_dir=psf_dir,nlive=700)
        # Input the data, using the external data if provided
        if self.use_external_data:
            b.load_external_data(self.f1.CTB_en_bins,[self.external_data[self.flux_array_ebin]],self.f1.CTB_exposure_maps)
        else:
            b.load_external_data(self.f1.CTB_en_bins,self.f1.CTB_count_maps,self.f1.CTB_exposure_maps)

        if add_ps_mask:
            b.make_mask_total(band_mask_range = [-self.band_mask,self.band_mask],mask_ring = False,ps_mask_array = self.f1.ps_mask_array)
        else:
            b.make_mask_total(band_mask_range = [-self.band_mask,self.band_mask],mask_ring = False)

        b.add_new_template(self.f1.template_dict)
        b.rebin_external_data(1)

        b.add_poiss_model('ps_model','$A_{ps}$',[0.0,3.0],False)
        b.add_poiss_model('p7','$A_{p7}$',[0.0,2.0],False)
        b.add_poiss_model('bubs','$A_{bubs}$',[0.0,2.0],False)
        b.add_poiss_model('iso','$A_{iso}$',[0.0,2.0],False)
        # Add in a fixed J_map template
        b.add_fixed_templates({'J_map':[norm*self.J_map_arr[self.flux_array_ebin]/np.mean(self.J_map_arr[self.flux_array_ebin])]})

        b.initiate_poissonian_edep()
        return b
Ejemplo n.º 4
0
def setup_b_instance(ebin_min):
    global b, f, run_tag

    run_tag = run_tag_base + "-" + str(ebin_min)  # set the run tag using ebin_min
    f = copy.deepcopy(f_total)  # copy f_total and reduce to energy subset
    f.reduce_to_energy_subset(ebin_min, ebin_min + 1)

    b = bsm.bayesian_scan_NPTF(tag=tag, nside=nside, work_dir=work_dir, psf_dir=psf_dir)
    b.load_external_data(f.CTB_en_bins, f.CTB_count_maps, f.CTB_exposure_maps)
    b.make_mask_total(
        plane_mask=plane_mask,
        band_mask_range=[-pmval, pmval],
        lcut=lcut,
        lmin=lmin,
        lmax=lmax,
        bcut=bcut,
        bmin=bmin,
        bmax=bmax,
        mask_ring=mask_ring,
        inner=inner,
        outer=outer,
        ps_mask_array=f.ps_mask_array,
    )

    b.add_new_template(f.template_dict)
    b.rebin_external_data(1)

    b.add_poiss_model("nfw", "$A_{nfw}$", [-6, 6], True)
    b.add_poiss_model("p6", "$A_{p6}$", [0, 2], False)
    b.add_poiss_model("bubs", "$A_{bubs}$", [0, 2], False)
    b.add_poiss_model("iso", "$A_{iso}$", [-2, 2], False)
    b.add_poiss_model("ps_model", "$A_{ps-model}$", [0, 3], False)

    b.initiate_poissonian_edep()
Ejemplo n.º 5
0
def setup_b_instance(ebin_min):
    global b, f, run_tag
    
    run_tag=run_tag_base + '-'+str(ebin_min) #set the run tag using ebin_min
    f = copy.deepcopy(f_total) #copy f_total and reduce to energy subset
    f.reduce_to_energy_subset(ebin_min,ebin_min+1)
    
    b = bsm.bayesian_scan_NPTF(tag=tag,nside=nside,work_dir=work_dir,psf_dir=psf_dir)
    b.load_external_data(f.CTB_en_bins,f.CTB_count_maps,f.CTB_exposure_maps)
    b.make_mask_total(band_mask_range =  band_mask_range,mask_ring = mask_ring,outer=outer,ps_mask_array = f.ps_mask_array)
    
    b.add_new_template(f.template_dict)
    b.rebin_external_data(1)
    
    b.add_poiss_model('nfw','$A_{nfw}$',[-6,6],True)
    b.add_poiss_model('p6','$A_{p6}$',[0,2],False)
    b.add_poiss_model('bubs','$A_{bubs}$',[0,2],False)
    b.add_poiss_model('iso','$A_{iso}$',[-2,2],False)
    
    b.initiate_poissonian_edep()
if ps_model:
    f.add_ps_model(comp = 'ps_model')
# f.add_ps(0,0,0.15)
if norm_file!='False':
    f.use_template_normalization_file(norm_file)

#######################################
###PSF
f.load_psf(data_name='p8',fits_file_path = 'False',eventclass=eventclass,eventtype=eventtype)
sigma_PSF_deg = f.sigma_PSF_deg[0:-1]
bin_len = (len(f.CTB_en_bins)-1)/n_ebins
sigma_PSF_deg_red = [ sigma_PSF_deg[i*bin_len] for i in range(n_ebins) ]

#########################
#setup bayesian scan for fermi
b = bsm.bayesian_scan_NPTF(tag=tag,work_dir = work_dir,psf_dir = psf_dir,nside=nside,nlive=100,k_max=k_max)
b.load_external_data(f.CTB_en_bins,f.CTB_count_maps,f.CTB_exposure_maps)
b.add_new_template(f.template_dict)

if mask_type!='False':
    b.make_mask_total(band_mask_range = [-mask_b,mask_b],mask_ring = False,ps_mask_array = f.ps_mask_array)
else:
    b.make_mask_total(band_mask_range =  [-mask_b,mask_b],mask_ring = False)
b.rebin_external_data(n_ebins)
b.compress_templates()

#########################
#add models
b.add_poiss_model(diff,'$A_{diff}$',[0,5],False)
b.add_poiss_model('bubs','$A_{bubs}$',[0,5],False)
if ps_model:
Ejemplo n.º 7
0
    f.make_ps_mask(mask_type = mask_type,force_energy = False) #
f.add_diffuse(comp = diff)
f.add_bubbles(comp='bubs')
f.add_iso(comp='iso')
######Need to figure out the PSF
f.load_psf(data_name='p8',fits_file_path = 'False')
sigma_PSF_deg = f.sigma_PSF_deg[0]
print 'The psf is', sigma_PSF_deg
######
for ell_i, b_i,ps_string_i in map(None,ell_0,b_0,ps_string):
    f.add_ps(ell_i,b_i,f.sigma_PSF_deg[0],comp = ps_string_i) 
############

#########################
#setup bayesian scan for fermi
b = bsm.bayesian_scan_NPTF(tag=tag,work_dir = work_dir,psf_dir = psf_dir,nside=nside)
b.load_external_data(f.CTB_en_bins,f.CTB_count_maps,f.CTB_exposure_maps)
b.add_new_template(f.template_dict)
if mask_string=='False':
    b.make_mask_total(band_mask_range =  [-0,0],mask_ring = True,inner = inner,outer=outer,lat= 90-mask_b,lng = mask_ell)
else:
    b.mask_total = mask
    b.mask_total_array = np.array([mask for en in f.CTB_en_bins])
b.compress_templates()

#########################
#add models
b.add_poiss_model(diff,'$A_{diff}$',[0,2],False)
b.add_poiss_model('iso','$A_{iso}$',[-2,2],False)
b.add_poiss_model('bubs','$A_{bubs}$',[0,2*nside/float(128)],False)
if ps:
Ejemplo n.º 8
0
barr = np.pi/2 - theta

# Setup Fermi module which we need to extract data and determine the background model
f = fp.fermi_plugin('/tigress/smsharma/public/CTBCORE/',CTB_en_min=CTB_start_bin,CTB_en_max=CTB_end_bin,nside=nside,data_name='p8')
f.add_diffuse(comp = 'p6') #diffuse
f.add_bubbles() #bubbles
f.add_iso()  #iso
f.add_nfw()

# Load the real data map
realdata = np.sum(f.CTB_count_maps,axis=0)

# Create the masks
if mask_type != 'False':
    f.make_ps_mask(mask_type = mask_type,energy_bin = energy_bin_mask,force_energy=force_energy)
b = bsm.bayesian_scan_NPTF(nside=nside)
b.load_external_data(f.CTB_en_bins,f.CTB_count_maps,f.CTB_exposure_maps)
b2 = copy.deepcopy(b)
b.make_mask_total(band_mask_range=band_mask_range,mask_ring=mask_ring,outer=outer)
roimaskload = b.mask_total_array
if mask_type != 'False':
    b2.make_mask_total(band_mask_range=band_mask_range,mask_ring=mask_ring,outer=outer,ps_mask_array=f.ps_mask_array)
    roipsmaskload = b.mask_total_array
else:
    roipsmaskload = roimask

# load masks are boolean arrays, convert to ints
roimask = np.zeros(npix)
roipsmask = np.zeros(npix)
for i in range(npix):
    if not roimaskload[0][i]:
Ejemplo n.º 9
0
def setup_for_scan():
    global f, b, new_template_dict, n_ps, newstyle
    # Load Fermi Plugin and its basic functionality
    f = fp.fermi_plugin(maps_dir,fermi_data_dir=fermi_data_dir,work_dir=work_dir,CTB_en_min = emin,CTB_en_max=emax,nside=nside,eventclass=eventclass,eventtype=eventtype,newstyle=newstyle)

    if mask_type!='False':
        f.make_ps_mask(mask_type=mask_type,force_energy=force_ps_mask,energy_bin=force_ps_mask_bin)
    # Add appropriate templates
    f.add_diffuse_newstyle(comp = diff, eventclass = eventclass, eventtype = eventtype)
    if extraicstemp!='False':
        f.add_diffuse_newstyle(comp = extraicstemp, eventclass = eventclass, eventtype = eventtype)
    f.add_bubbles(comp = 'bubs')
    f.add_iso(comp = 'iso')
    if add_ps_model:
        f.add_ps_model(comp = 'ps_model')
    if nfw_dm:
        f.add_nfw(comp = 'nfw')
    f.add_template_from_file('disk',disk_file_name) # always add the disk here even if don't use as often needed for NPTF
    if norm_file!='False':
        if len(add_norm_file_for_comps) > 0 and add_norm_file != 'False':
            print 'using first normalization file: ', norm_file
            f.use_template_normalization_file(norm_file,key_suffix='-0',dont_use_keys=add_norm_file_for_comps)
            print 'using second normalization file: ', add_norm_file
            f.use_template_normalization_file(add_norm_file,key_suffix='-0',use_keys=add_norm_file_for_comps)
        else:
            f.use_template_normalization_file(norm_file,key_suffix='-0')

    #### For now, combine diff, bubs, iso, ps_model, IC to reduce number of parameters
    new_template_dict = {}
    new_template_dict['back']=np.zeros(np.shape(f.template_dict['bubs']))
    for key in f.template_dict.keys():
        new_template_dict[key] = f.template_dict[key]
        if key not in not_in_simplified_template_list: #key != 'nfw' and key != 'disk' and
            print 'Including template', key, 'in background template'
            new_template_dict['back']+=f.template_dict[key]
        # else:
        #     new_template_dict[key] = f.template_dict[key]

    # Load PSF - this is only required for NPTF scans
    # This now uses a King function
    if not poiss:
        f.load_psf_kings()
        f.make_f_ary_kings(psf_model,energy_averaged=True,psf_dir=psf_dir,psf_save_tag=run_tag_energy)
    # Setup Bayesian scan for fermi
    b = bsm.bayesian_scan_NPTF(tag=tag,work_dir = work_dir,psf_dir = psf_dir,nside=nside,nlive=nlive,k_max=k_max)
    if fake_data:
        data = np.loadtxt(fake_data_path)
    else:
        data = f.CTB_count_maps

    b.load_external_data(f.CTB_en_bins,data,f.CTB_exposure_maps)
    if use_simplified_templates:
        b.add_new_template(new_template_dict)
        if fixed_background:
            b.add_fixed_templates({'back':new_template_dict['back']})
    else:
        b.add_new_template(f.template_dict)
    if mask_type!='False':
        b.make_mask_total(plane_mask=plane_mask, band_mask_range = [-pmval,pmval], lcut=lcut, lmin=lmin, lmax=lmax, bcut=bcut, bmin=bmin, bmax=bmax, mask_ring=mask_ring, inner=inner, outer=outer, ps_mask_array = f.ps_mask_array)
    else:
        b.make_mask_total(plane_mask=plane_mask, band_mask_range = [-pmval,pmval], lcut=lcut, lmin=lmin, lmax=lmax, bcut=bcut, bmin=bmin, bmax=bmax, mask_ring=mask_ring, inner=inner, outer=outer)
    b.rebin_external_data(n_ebins)
    b.compress_templates()

    ### Add in the Templates
    if method != 'minuit':
        if not min_prior_range:
            b.add_poiss_model(diff,'$A_{diff}$',[-10,10],False)
            if extraicstemp!='False':
                b.add_poiss_model(extraicstemp,'$A_{ics}$',[-10,10],False)
            b.add_poiss_model('iso','$A_{iso}$',[-10,10],False)
            b.add_poiss_model('bubs','$A_{bubs}$',[-10,10],False)
        else: # If already have priors, don't scan over such a large range
            b.add_poiss_model(diff,'$A_{diff}$',[0.5,1.5],False)
            if extraicstemp!='False':
                b.add_poiss_model(extraicstemp,'$A_{ics}$',[0.5,1.5],False)
            if high_lat:
                b.add_poiss_model('iso','$A_{iso}$',[0.5,1.5],False)
            else:
                b.add_poiss_model('iso','$A_{iso}$',[0.8,1.2],False)
            b.add_poiss_model('bubs','$A_{bubs}$',[0.5,1.5],False)
    else:
        b.add_poiss_model(diff,'$A_{diff}$',[0,2],False)
        if extraicstemp!='False':
            b.add_poiss_model(extraicstemp,'$A_{ics}$',[-2,2],False)
        b.add_poiss_model('iso','$A_{iso}$',[0,2],False)
        b.add_poiss_model('bubs','$A_{bubs}$',[0,4],False)
    if add_ps_model:
        if not min_prior_range:
            b.add_poiss_model('ps_model','$A_{ps-model}$',[0,6],False)
        else:
            b.add_poiss_model('ps_model','$A_{ps-model}$',[0.5,1.5],False)
    if nfw_dm: # leave this large as NFW struggles to converge at high E
        b.add_poiss_model('nfw','$l10A_{nfw}$',[-6,6],True)

    ### Configure final details
    def sb_string_mod(mod):
        return ['${S_b^{' + mod + '}}^{'+str(i) + '}$' for i in range(n_ebins)]

    if not poiss:
        sb_string = ['$S_b^{'+str(i) + '}$' for i in range(n_ebins)]
        sb_string_disk = ['${S_b^{disk}}^{'+str(i) + '}$' for i in range(n_ebins)]
        sb_prior = [ [-3.,3.] for i in range(n_ebins)]
        sb_prior_log = [True for i in range(n_ebins)]
        if len(ps_fixed_filename_list) > 0:
            for comp,filename in map(None,ps_fixed_list,ps_fixed_filename_list):
                b.load_fixed_ps_model(filename,comp)
            n_ps += len(ps_fixed_filename_list)
            print 'n_ps changed to ', n_ps, ' because of fixed templates'

        for mod in ps_list:
            b.add_non_poiss_model(mod,['$A_{' +mod+'}$' ,'$n_1^{' +mod+'}$','$n_2^{' +mod+'}$']+sb_string_mod(mod),[[-6,6],[2.05,30],[-2,1.95]] + sb_prior, [True,False,False] + sb_prior_log)
        if n_ps==1:
            b.initiate_1_ps_edep_new_from_f_ary(f.f_ary_list,f.df_rho_div_f_ary_list)
        elif n_ps==2:
            b.initiate_2_ps_edep_new_from_f_ary(f.f_ary_list,f.df_rho_div_f_ary_list)
        elif n_ps==3:
            b.initiate_3_ps_edep_new_from_f_ary(f.f_ary_list,f.df_rho_div_f_ary_list)

        print 'Performing an NPTF ...'
    else:
        b.initiate_poissonian_edep()
        print 'Performing a standard template fit ...'
#The tag and run_tag
tag = 'July16'
run_tag = 'poiss_En_5_to_7_mask300_lowerEn_with_2mass'

##########################################
#Energy range
CTB_en_min = 0
CTB_en_max = 20

##########################################
#the maps_dir directory (all the data files)
maps_dir = '/Users/bsafdi/Dropbox/Edep-NPTF/NPTF-shared/CTBCORE/'

##########################################
#Setup the scan
b = bsm.bayesian_scan_NPTF('July16',CTB_en_min = CTB_en_min, CTB_en_max = CTB_en_max,data_name = 'p8',n_kbins = 3,**{'maps_dir':maps_dir})

##########################################
#Add templates
b.add_template(comp = 'p6')
b.add_template(comp = 'bubs')
b.add_template(comp = 'iso')
b.add_template(comp = 'ps_model')
#external templates
mass_file = '2mass_exposure_corrected.fits'
b.add_template(comp = '2mass',**{'template_name': mass_file}) #this is an example of how to add a new template that's not predefined.  The template should be a fits file with entry [0].data the map (an exposure-corrected counts map).  This map should be in self.template_dir

##########################################
#set the mask for the scan
b.make_mask_total(mask_ring = False,band_mask_range = [-30,30],ps_mask_type = 'top300',outer = 30)
Ejemplo n.º 11
0
tag = "July16"
run_tag = "poiss_En_5_to_7_mask300_lowerEn_with_2mass"

##########################################
# Energy range
CTB_en_min = 5
CTB_en_max = 7

##########################################
# the maps_dir directory (all the data files)
maps_dir = "/Users/bsafdi/Dropbox/Edep-NPTF/NPTF-shared/CTBCORE/"

##########################################
# Setup the scan
b = bsm.bayesian_scan_NPTF(
    "July16", CTB_en_min=CTB_en_min, CTB_en_max=CTB_en_max, data_name="p8", n_kbins=3, **{"maps_dir": maps_dir}
)

##########################################
# Add templates
b.add_template(comp="p6")
b.add_template(comp="bubs")
b.add_template(comp="iso")
b.add_template(comp="ps_model")
# external templates
mass_file = "2mass_exposure_corrected.fits"
b.add_template(
    comp="2mass", **{"template_name": mass_file}
)  # this is an example of how to add a new template that's not predefined.  The template should be a fits file with entry [0].data the map (an exposure-corrected counts map).  This map should be in self.template_dir

##########################################
Ejemplo n.º 12
0
#Energy range
CTB_en_min = 8
CTB_en_max = 16

##########################################
#pixels with more than this number of photons are removed
k_max = 100 


##########################################
#the maps_dir directory (all the data files)
maps_dir = '/Users/bsafdi/Dropbox/Edep-NPTF/NPTF-shared/CTBCORE/'

##########################################
#Setup the scan
b = bsm.bayesian_scan_NPTF(tag,k_max = k_max,CTB_en_min = CTB_en_min, CTB_en_max = CTB_en_max,data_name = 'p8',n_kbins = 5,nside=128,**{'maps_dir':maps_dur}) 

##########################################
#Add templates
b.add_template(comp = 'p6')
b.add_template(comp = 'bubs')
b.add_template(comp = 'iso')
b.add_template(comp = 'ps_model') #this models 3FGL PSs

##########################################
#set the mask for the scan
b.make_mask_total(mask_ring = False,band_mask_range = [-30,30],ps_mask_type = 'top300',outer = 30) #other options for ps_mask_type include '0.95', '0.99', 'False'


##########################################
#Add poissonian models
Ejemplo n.º 13
0
def setup_for_scan(i=0,n_ps_groupi=0,larr=[0],barr=[0],psloop1=False,pscomball=False,psloop2bkg=False,psloop2=False):
    global f, b, new_template_dict, sigma_PSF_deg, sigma_PSF_deg_red, n_ps, newstyle, ps_norms_loop1, ps_norms
    # Load Fermi Plugin and its basic functionality
    f = fp.fermi_plugin(maps_dir,fermi_data_dir=fermi_data_dir,work_dir=work_dir,CTB_en_min = emin,CTB_en_max=emax,nside=nside,eventclass=eventclass,eventtype=eventtype,newstyle=newstyle)

    if mask_type!='False':
        f.make_ps_mask(mask_type=mask_type,force_energy=force_ps_mask,energy_bin=force_ps_mask_bin)
    # Add appropriate templates
    f.add_diffuse_newstyle(comp = diff, eventclass = eventclass, eventtype = eventtype)
    if extraicstemp!='False':
        f.add_diffuse_newstyle(comp = extraicstemp, eventclass = eventclass, eventtype = eventtype)
    f.add_bubbles(comp = 'bubs')
    f.add_iso(comp = 'iso')
    if add_ps_model:
        f.add_ps_model(comp = 'ps_model')
    if nfw_dm:
        f.add_nfw(comp = 'nfw')
    if psloop1:
        # Throughout we use a king function or Gaussian for the point sources
        if use_king:
            for j in range(n_ps_groupi):
                f.add_ps_king_fast(larr[i*n_ps_run+j],barr[i*n_ps_run+j],comp ='ps_' + str(i*n_ps_run+j+1))
            if i != 0:
                f.add_multiple_ps_king_fast(larr[0:i*n_ps_run],barr[0:i*n_ps_run],rescale=ps_norms_loop1[0:i*n_ps_run],comp='ps_comb')
        else:
            # Add in Gaussian point source - need the PSF for this
            f.load_psf(data_name='p8',fits_file_path = 'False')
            sigma_PSF_deg = f.sigma_PSF_deg[0:-1]
            for j in range(n_ps_groupi):
                f.add_ps(larr[i*n_ps_run+j],barr[i*n_ps_run+j],sigma_PSF_deg[0],comp ='ps_' + str(i*n_ps_run+j+1))
            if i != 0:
                f.add_multiple_ps(larr[0:i*n_ps_run],barr[0:i*n_ps_run],sigma_PSF_deg[0],rescale=ps_norms_loop1[0:i*n_ps_run],comp='ps_comb')
    if pscomball:
        if use_king:
            f.add_multiple_ps_king_fast(larr,barr,rescale=ps_norms_loop1,comp='ps_comb')
        else:
            f.load_psf(data_name='p8',fits_file_path = 'False')
            sigma_PSF_deg = f.sigma_PSF_deg[0:-1]
            f.add_multiple_ps(larr,barr,sigma_PSF_deg[0],rescale=ps_norms_loop1,comp='ps_comb')
    if psloop2bkg:
        if use_king:
            f.add_multiple_ps_king_fast(larr,barr,rescale=ps_norms_loop1,comp='ps_comb',excluded=i)
        else:
            f.load_psf(data_name='p8',fits_file_path = 'False')
            sigma_PSF_deg = f.sigma_PSF_deg[0:-1]
            f.add_multiple_ps(larr,barr,sigma_PSF_deg[0],rescale=ps_norms_loop1,comp='ps_comb',excluded=i)
    if psloop2:
        if use_king:
            f.add_ps_king_fast(larr[i],barr[i],rescale=ps_norms_loop1[i],comp ='ps_' + str(i+1))
            f.add_multiple_ps_king_fast(larr,barr,rescale=ps_norms_loop1,comp='ps_comb',excluded=i)
        else:
            f.load_psf(data_name='p8',fits_file_path = 'False')
            sigma_PSF_deg = f.sigma_PSF_deg[0:-1]
            f.add_ps(larr[i],barr[i],sigma_PSF_deg[0],rescale=ps_norms_loop1[i],comp ='ps_' + str(i+1))
            f.add_multiple_ps(larr,barr,sigma_PSF_deg[0],rescale=ps_norms_loop1,comp='ps_comb',excluded=i)
    if norm_file!='False':
        if len(add_norm_file_for_comps) > 0 and add_norm_file != 'False':
            print 'using first normalization file: ', norm_file
            f.use_template_normalization_file(norm_file,key_suffix='-0',dont_use_keys=add_norm_file_for_comps)
            print 'using second normalization file: ', add_norm_file
            f.use_template_normalization_file(add_norm_file,key_suffix='-0',use_keys=add_norm_file_for_comps)
        else:
            f.use_template_normalization_file(norm_file,key_suffix='-0')

    # Setup Bayesian scan for fermi
    b = bsm.bayesian_scan_NPTF(tag=tag,work_dir = work_dir,psf_dir = psf_dir,nside=nside,nlive=nlive,k_max=k_max)
    if fake_data:
        data = np.loadtxt(fake_data_path)
    else:
        data = f.CTB_count_maps
    b.load_external_data(f.CTB_en_bins,data,f.CTB_exposure_maps)
    if use_simplified_templates:
        b.add_new_template(new_template_dict)
        if fixed_background:
            b.add_fixed_templates({'back':new_template_dict['back']})
    else:
        b.add_new_template(f.template_dict)
    if mask_type!='False':
        b.make_mask_total(plane_mask=plane_mask, band_mask_range = [-pmval,pmval], lcut=lcut, lmin=lmin, lmax=lmax, bcut=bcut, bmin=bmin, bmax=bmax, mask_ring=mask_ring, inner=inner, outer=outer, ps_mask_array = f.ps_mask_array)
    else:
        b.make_mask_total(plane_mask=plane_mask, band_mask_range = [-pmval,pmval], lcut=lcut, lmin=lmin, lmax=lmax, bcut=bcut, bmin=bmin, bmax=bmax, mask_ring=mask_ring, inner=inner, outer=outer)
    b.rebin_external_data(1)
    b.compress_templates()

    ### Add in the Templates
    if method != 'minuit':
        if not min_prior_range:
            b.add_poiss_model(diff,'$A_{diff}$',[-5,5],False)
            if extraicstemp!='False':
                b.add_poiss_model(extraicstemp,'$A_{ics}$',[-5,5],False)
            b.add_poiss_model('iso','$A_{iso}$',[-3,3],False)
            b.add_poiss_model('bubs','$A_{bubs}$',[-3,3],False)
        else: # If already have priors, don't scan over such a large range
            b.add_poiss_model(diff,'$A_{diff}$',[0.5,1.5],False)
            if extraicstemp!='False':
                b.add_poiss_model(extraicstemp,'$A_{ics}$',[0.5,1.5],False)
            if high_lat:
                b.add_poiss_model('iso','$A_{iso}$',[0.5,1.5],False)
            else:
                b.add_poiss_model('iso','$A_{iso}$',[0.8,1.2],False)
            b.add_poiss_model('bubs','$A_{bubs}$',[0.5,1.5],False)
    else:
        b.add_poiss_model(diff,'$A_{diff}$',[0,2],False)
        if extraicstemp!='False':
            b.add_poiss_model(extraicstemp,'$A_{ics}$',[-2,2],False)
        b.add_poiss_model('iso','$A_{iso}$',[0,2],False)
        b.add_poiss_model('bubs','$A_{bubs}$',[0,4],False)
    if add_ps_model:
        if not min_prior_range:
            b.add_poiss_model('ps_model','$A_{ps-model}$',[0,6],False)
        else:
            b.add_poiss_model('ps_model','$A_{ps-model}$',[0.5,1.5],False)
    if nfw_dm: # leave this large as NFW struggles to converge at high E
        b.add_poiss_model('nfw','$l10A_{nfw}$',[-6,6],True)
    if psloop1:
        for j in range(n_ps_groupi):
            b.add_poiss_model('ps_' + str(i*n_ps_run+j+1),'$l10A_{ps' + str(i*n_ps_run+j+1) + '}$',[-10,10],True)
        if i != 0:
            b.add_poiss_model('ps_comb','$A_{ps-comb}$',[0.5,1.5],False)
    if pscomball:
        b.add_poiss_model('ps_comb','$A_{ps-comb}$',[0.5,1.5],False)
    if psloop2bkg:
        b.add_poiss_model('ps_comb','$A_{ps-comb}$',[0.5,1.5],False)
    if psloop2:
        b.add_poiss_model('ps_' + str(i+1),'$A_{ps' + str(i+1) + '}$',[0.5,1.5],False)
        b.add_poiss_model('ps_comb','$A_{ps-comb}$',[0,3],False)
        # Ideally want to basically fix this component, but if make too small minuit crashes

    ### Configure final details
    def sb_string_mod(mod):
        return ['${S_b^{' + mod + '}}^{'+str(i) + '}$' for i in range(1)]

    b.initiate_poissonian_edep()
    print 'Performing a standard template fit ...'
Ejemplo n.º 14
0
#########################
#configure for fermi
f = fp.fermi_plugin(maps_dir)

f.make_ps_mask()
f.add_diffuse(comp = 'p6')
f.add_bubbles()
f.add_iso()
f.add_nfw()
f.add_ps(0,0)


#########################
#setup bayesian scan for fermi
b = bsm.bayesian_scan_NPTF(tag=tag)
b.load_external_data(f.CTB_en_bins,f.CTB_count_maps,f.CTB_exposure_maps)
b.add_new_template(f.template_dict)
b.make_mask_total(ps_mask_array = f.ps_mask_array)
b.compress_templates()

#########################
#add models
b.add_poiss_model('p6','$A_{diff}$',[0,2],False)
b.add_poiss_model('iso','$A_{iso}$',[0,2],False)
b.add_poiss_model('bubs','$A_{bubs}$',[0,2],False)

b.configure_for_scan()
b.initiate_poissonian()

#########################
Ejemplo n.º 15
0
def setup_for_scan(larr=[0],barr=[0],add_group_ps=False,ps_number=0,fdn=True,fdrun=False):
    global f, b, new_template_dict, sigma_PSF_deg, sigma_PSF_deg_red, n_ps, newstyle, ps_norms, diffnormfix
    # Load Fermi Plugin and its basic functionality
    f = fp.fermi_plugin(maps_dir,fermi_data_dir=fermi_data_dir,work_dir=work_dir,CTB_en_min = emin,CTB_en_max=emax,nside=nside,eventclass=eventclass,eventtype=eventtype,newstyle=newstyle)

    if mask_type!='False':
        f.make_ps_mask(mask_type=mask_type,force_energy=force_ps_mask,energy_bin=force_ps_mask_bin)
    # Add appropriate templates
    f.add_diffuse_newstyle(comp = diff, eventclass = eventclass, eventtype = eventtype)
    if extraicstemp!='False':
        f.add_diffuse_newstyle(comp = extraicstemp, eventclass = eventclass, eventtype = eventtype)
    f.add_bubbles(comp = 'bubs')
    f.add_iso(comp = 'iso')
    if add_ps_model:
        f.add_ps_model(comp = 'ps_model')
    if fdn:
        f.add_nfw(comp = 'nfw')
    # Throughout we use a king function or Gaussian for the point sources
    if add_group_ps:
        if use_king:
            f.add_multiple_ps_king_fast(larr[0:ps_number],barr[0:ps_number],rescale=ps_norms[0:ps_number],comp='ps_comb')
        else:
            # Add in Gaussian point source - need the PSF for this
            f.load_psf(data_name='p8',fits_file_path = 'False')
            sigma_PSF_deg = f.sigma_PSF_deg[0:-1]
            f.add_multiple_ps(larr[0:ps_number],barr[0:ps_number],sigma_PSF_deg[0],rescale=ps_norms[0:ps_number],comp='ps_comb')
    if norm_file!='False':
        if len(add_norm_file_for_comps) > 0 and add_norm_file != 'False':
            print 'using first normalization file: ', norm_file
            f.use_template_normalization_file(norm_file,key_suffix='-0',dont_use_keys=add_norm_file_for_comps)
            print 'using second normalization file: ', add_norm_file
            f.use_template_normalization_file(add_norm_file,key_suffix='-0',use_keys=add_norm_file_for_comps)
        else:
            f.use_template_normalization_file(norm_file,key_suffix='-0')

    # Setup Bayesian scan for fermi
    b = bsm.bayesian_scan_NPTF(tag=tag,work_dir = work_dir,psf_dir = psf_dir,nside=nside,nlive=nlive,k_max=k_max)
    if fake_data_file=='False':
        print 'Using real data'
        count_maps=f.CTB_count_maps
        new_count_maps = f.CTB_count_maps
    else:
        print 'Using fake data from the file', fake_data_file
        count_maps=np.loadtxt(fake_data_file)[fake_data_emin:fake_data_emax]
        new_count_maps = np.zeros( (len(count_maps),hp.nside2npix(nside) ) )
        for i in range(len(count_maps)):
            new_count_maps[i] = hp.ud_grade(count_maps[i],nside,power=-2)

    b.load_external_data(f.CTB_en_bins,new_count_maps,f.CTB_exposure_maps)
    if use_simplified_templates:
        b.add_new_template(new_template_dict)
        if fixed_background:
            b.add_fixed_templates({'back':new_template_dict['back']})
    else:
        b.add_new_template(f.template_dict)
    if mask_type!='False':
        b.make_mask_total(plane_mask=plane_mask, band_mask_range = [-pmval,pmval], lcut=lcut, lmin=lmin, lmax=lmax, bcut=bcut, bmin=bmin, bmax=bmax, mask_ring=mask_ring, inner=inner, outer=outer, ps_mask_array = f.ps_mask_array)
    else:
        b.make_mask_total(plane_mask=plane_mask, band_mask_range = [-pmval,pmval], lcut=lcut, lmin=lmin, lmax=lmax, bcut=bcut, bmin=bmin, bmax=bmax, mask_ring=mask_ring, inner=inner, outer=outer)
    b.rebin_external_data(1)
    b.compress_templates()

    ### Add in the Templates
    if method != 'minuit':
        if not min_prior_range:
            if not fdrun:
                b.add_fixed_templates({diff:[f.template_dict[diff][0]*diffnormfix]})
            else:
                b.add_poiss_model(diff,'$A_{diff}$',[-5,5],False)
            if extraicstemp!='False':
                b.add_poiss_model(extraicstemp,'$A_{ics}$',[-5,5],False)
            b.add_poiss_model('iso','$A_{iso}$',[-3,3],False)
            b.add_poiss_model('bubs','$A_{bubs}$',[-3,3],False)
        else: # If already have priors, don't scan over such a large range
            if not fdrun:
                b.add_fixed_templates({diff:f.template_dict[diff]*diffnormfix})
            else:
                b.add_poiss_model(diff,'$A_{diff}$',[0.5,1.5],False)
            if extraicstemp!='False':
                b.add_poiss_model(extraicstemp,'$A_{ics}$',[0.5,1.5],False)
            if high_lat:
                b.add_poiss_model('iso','$A_{iso}$',[0.5,1.5],False)
            else:
                b.add_poiss_model('iso','$A_{iso}$',[0.8,1.2],False)
            b.add_poiss_model('bubs','$A_{bubs}$',[0.5,1.5],False)
    else:
        if not fdrun:
            b.add_fixed_templates({diff:f.template_dict[diff]*diffnormfix})
        else:
            b.add_poiss_model(diff,'$A_{diff}$',[0,2],False)
        if extraicstemp!='False':
            b.add_poiss_model(extraicstemp,'$A_{ics}$',[-2,2],False)
        b.add_poiss_model('iso','$A_{iso}$',[0,2],False)
        b.add_poiss_model('bubs','$A_{bubs}$',[0,4],False)
    if add_ps_model:
        if not min_prior_range:
            b.add_poiss_model('ps_model','$A_{ps-model}$',[0,6],False)
        else:
            b.add_poiss_model('ps_model','$A_{ps-model}$',[0.5,1.5],False)
    if fdn:
        b.add_poiss_model('nfw','$l10A_{nfw}$',[-6,6],True)
    if add_group_ps:
        b.add_poiss_model('ps_comb','$A_{ps-comb}$',[-5,5],False)

    ### Configure final details
    def sb_string_mod(mod):
        return ['${S_b^{' + mod + '}}^{'+str(i) + '}$' for i in range(1)]

    b.initiate_poissonian_edep()
    print 'Performing a standard template fit ...'