def plotSensitivity(sensefolder): stack_files = [cache.load(sensefolder+file) for file in os.listdir(misc.ensure_dir(sensefolder)) if file.startswith('gamma2.0')] ##Let's find the gammas used first. gammas=[float(filename[5:-6]) for filename in os.listdir(misc.ensure_dir(sensefolder)) if filename.startswith('gamma')] flux_stack = [stack_file['flux'] for stack_file in stack_files] mu_stack = [stack_file['mu'] for stack_file in stack_files] return flux_stack,mu_stack
def plotSensitivity(datafolder_stack_bin): stack_files = [[ cache.load(datafolder_stack_bin + decfolder + '/sensitivity/' + file) for file in os.listdir( misc.ensure_dir(datafolder_stack_bin + decfolder + '/sensitivity/')) if file.endswith('.array') ] for decfolder in os.listdir(datafolder_stack_bin) if decfolder.startswith('dec')] ##Let's find the declinations used first. decfolders = [ file for file in os.listdir(datafolder_stack_bin) if file.startswith('dec') ] dec_stack = [np.float(decfolder[3:]) for decfolder in decfolders] #We might not have gotten every value possible, so remove the blanks. for i in reversed(range(len(stack_files))): if stack_files[i] == []: stack_files.remove([]) dec_stack.pop(i) flux_stack = [stack_file[0]['flux'] for stack_file in stack_files] inj_stack = [stack_file[0]['mu'] for stack_file in stack_files] sindecrange_stack = np.sin(np.radians(dec_stack)) results_stack = sorted(zip(sindecrange_stack, inj_stack, flux_stack)) sindecrange_stack, inj_stack, flux_stack = zip(*results_stack) return sindecrange_stack, inj_stack, flux_stack
def aeff(self): """ Plot and tabulate Aeff. """ import colormaps as cmaps plt.register_cmap(name='viridis', cmap=cmaps.viridis) plt.set_cmap(cmaps.viridis) logEmin, logEmax = 2., 9. dlogE = 0.1 n_bins_E = (logEmax - logEmin) / dlogE dcz = 0.01 dOmega = 2 * pi * dcz n_bins_cz = 2 / dcz nu = self.nu nu.cz = -np.sin(nu.trueDec) w_aeff = 1 / (1e4 * np.log(10)) * nu.ow / nu.trueE / dOmega / dlogE h_aeff = hl.hist( (nu.trueE, nu.cz), w_aeff, bins=(n_bins_E, n_bins_cz), range=((10**logEmin, 10**logEmax), (-1, 1)), log=(True, False), ) misc.tex_mpl_rc(True) fig = getfig(aspect=4 / 3., width=5) ax = fig.add_subplot(111) fig.subplots_adjust(bottom=.15, left=.15) result = hl.plot2d(ax, h_aeff, cbar=True, log=True, vmin=5e-6, vmax=1e4, zmin=5e-6) result['colorbar'].set_label(r'effective area $[\text{m}^2]$') ax.set_xlabel('neutrino energy [GeV]') ax.set_ylabel(r'$\cos(\text{zenith})$') plot_dir = misc.ensure_dir('{0}/plots'.format(self.mode_dir)) savingfig(fig, plot_dir, 'aeff') bins = h_aeff.bins filename = '{}/aeff.txt'.format(plot_dir) prush('-> {} ...'.format(filename)) with open(filename, 'w') as f: pr = lambda *a, **kw: print(*a, file=f, **kw) pr('# {:>11}{:>13}{:>16}{:>16}{:>16}'.format( 'E_min[GeV]', 'E_max[GeV]', 'cos(zenith)_min', 'cos(zenith)_max', 'Aeff[m^2]')) for (Emin, Emax) in izip(bins[0][:-1], bins[0][1:]): for (czmin, czmax) in izip(bins[1][:-1], bins[1][1:]): pr('{:13.3e}{:13.3e}{:+16.2f}{:+16.2f}{:16.3e}'.format( Emin, Emax, czmin, czmax, h_aeff.get_value(1.001 * Emin, 1e-3 + czmin)))
def setup_data(self): """Set up `data`, `nu`, and `pdfs`.""" if 'txt' in self.mode.words: xd = np.genfromtxt('{}/data/IC86-I_data.txt'.format(self.root_dir), names=True) data = arrays.Arrays( dict((k, xd[k]) for k in ('ra', 'dec', 'sigma', 'logE'))) data.sinDec = np.sin(data.dec) else: xd = loading('{}/data/exp.pickle'.format(self.root_dir)) data = arrays.Arrays( dict((k, xd[k]) for k in ('ra', 'sinDec', 'sigma', 'logE'))) if 'txt' in self.mode.words: xn = np.genfromtxt('{}/data/IC86-I_MC.txt'.format(self.root_dir), names=True) nu = arrays.Arrays( dict((k, xn[k]) for k in ('ra', 'dec', 'sigma', 'logE', 'trueRa', 'trueDec', 'trueE', 'ow'))) nu.sinDec = np.sin(nu.dec) else: xn = loading('{}/data/MC.pickle'.format(self.root_dir)) nu = arrays.Arrays( dict((k, xn[k]) for k in ('ra', 'sinDec', 'sigma', 'logE', 'trueRa', 'trueDec', 'trueE', 'ow'))) xzenith, xazimuth = coord.rotate_source_to_xaxis( nu.trueDec + pi / 2, nu.trueRa, np.arccos(-nu.sinDec), nu.ra) xazimuth[xazimuth < pi] += 2 * pi xazimuth[xazimuth > pi] -= 2 * pi nu.xaxis_zenith = xzenith nu.xaxis_azimuth = xazimuth data.apply_cut((1 <= data.logE) & (data.logE < 10)) nu.apply_cut((1 <= nu.logE) & (nu.logE < 10)) data_dir = misc.ensure_dir('{}/data'.format(self.mode_dir)) saving(data, '{}/data.arrays'.format(data_dir)) saving(nu, '{}/nu.arrays'.format(data_dir)) pdfs = pdf.PDFs( pdf.BgSpacePDF(-data.sinDec, bins=10, range=(-1, 1), fit=False), pdf.EnergyPDF(-data.sinDec, data.logE, weights=None, bins=20, range=((-1, 1), (1, 10)), fit=False), pdf.EnergyPDFs(-nu.sinDec, nu.logE, np.log10(nu.trueE), nu.ow * nu.trueE**-2, np.arange(1, 4.1, .25), bins=20, range=((-1, 1), (1, 10)), fit=False)) saving(pdfs, '{}/pdfs.pdfs'.format(data_dir))
def plotDisc(datafolder): sensefolder = datafolder + 'disc/' stack_files = [ cache.load(sensefolder + file) for file in os.listdir(misc.ensure_dir(sensefolder)) if file.startswith('gamma') ] ##Let's find the gammas used first. gammas = [ float(filename[5:-6]) for filename in os.listdir(misc.ensure_dir(sensefolder)) if filename.startswith('gamma') ] flux_stack = [stack_file['flux'] for stack_file in stack_files] inj_stack = [stack_file['mu'] for stack_file in stack_files] results_stack = sorted(zip(gammas, inj_stack, flux_stack)) gammas, inj, disc = zip(*results_stack) disc100 = [refChange(disc[i], gammas[i]) for i in range(len(disc))] return gammas, inj, disc100
def plotSensitivity(datafolder): sensefolder = datafolder + 'sensitivity/' stack_files = [ cache.load(sensefolder + file) for file in os.listdir(misc.ensure_dir(sensefolder)) if file.startswith('gamma') ] ##Let's find the gammas used first. gammas = [ float(filename[5:-6]) for filename in os.listdir(misc.ensure_dir(sensefolder)) if filename.startswith('gamma') ] flux_stack = [stack_file['flux'] for stack_file in stack_files] mu_stack = [stack_file['mu'] for stack_file in stack_files] results_stack = sorted(zip(gammas, mu_stack, flux_stack)) gammas, mu, sensitivity = zip(*results_stack) sens100 = [ refChange(sensitivity[i], gammas[i]) for i in range(len(sensitivity)) ] return gammas, mu, sens100
def aeff2d (year): """ Plot and tabulate Aeff. """ import colormaps as cmaps plt.register_cmap (name='viridis', cmap=cmaps.viridis) plt.set_cmap (cmaps.viridis) logEmin, logEmax = 2., 9. dlogE = 0.1 n_bins_E = (logEmax - logEmin) / dlogE dcz = 0.01 dOmega = 2 * np.pi * dcz n_bins_cz = 2 / dcz #nu = self.nu ..... xn is the mc sample of a certain year. let's start with IC86I and work our way forward. if year == 86: xn = data_multi.MC86I() elif year ==79: xn = data_multi.MC79() elif year ==59: xn = data_multi.MC59() elif year ==40: xn = data_multi.MC40() #Whichever year we have, the following occurs: nu = arrays.Arrays (dict ( (k,xn[k]) for k in ('ra', 'sinDec', 'sigma', 'logE', 'trueRa', 'trueDec', 'trueE', 'ow'))) #nu = self.nu nu.cz = -np.sin (nu.trueDec) w_aeff = 1 / (1e4 * np.log (10)) * nu.ow / nu.trueE / dOmega / dlogE h_aeff = histlite.hist ( (nu.trueE, nu.cz), w_aeff, bins=(n_bins_E, n_bins_cz), range=((10**logEmin, 10**logEmax), (-1, 1)), log=(True, False), ) fig = getfig (aspect=4/3., width=5) ax = fig.add_subplot (111) fig.subplots_adjust (bottom=.15, left=.15) result = histlite.plot2d (ax, h_aeff, cbar=True, log=True, vmin=5e-6, vmax=1e4, zmin=5e-6) result['colorbar'].set_label (r'effective area $[\textrm{m}^2]$') ax.set_title('Aeff - IC{}'.format(str(year))) ax.set_xlabel ('neutrino energy [GeV]') ax.set_ylabel (r'$\cos(\textrm{zenith})$') plot_dir = misc.ensure_dir ('/data/user/brelethford/AGN_Core/Plots/data') fig.savefig(plot_dir+'/aeff{}_2d.pdf'.format(str(year)))
def plotSensitivity(datafolder_stack_bin, gamma): stack_files = [[ cache.load(datafolder_stack_bin + decfolder + '/sensitivity/' + file) for file in os.listdir( misc.ensure_dir(datafolder_stack_bin + decfolder + '/sensitivity/')) if file.endswith('.array') ] for decfolder in os.listdir(datafolder_stack_bin) if decfolder.startswith('dec')] ##Let's find the declinations used first. decfolders = [ file for file in os.listdir(datafolder_stack_bin) if file.startswith('dec') ] dec_stack = [np.float(decfolder[3:]) for decfolder in decfolders] #We might not have gotten every value possible, so remove the blanks. for i in reversed(range(len(stack_files))): if stack_files[i] == []: stack_files.remove([]) dec_stack.pop(i) #flux_stack = [stack_file[0]['flux'] for stack_file in stack_files] inj_stack = [stack_file[0]['mu'] for stack_file in stack_files] sindecrange_stack = np.sin(np.radians(dec_stack)) flux_stack = [] #Have to use a gamma to give to injector for calculating flux from a reference energy and an inj. for i in range(len(inj_stack)): inj = ps_injector_stack.PointSourceInjector( gamma, sinDec_bandwidth=.05, src_dec=np.radians(dec_stack)[i], seed=0) inj.E0 = 100 #So we've got an injector which follows a certain gamma model. Now lets fill it with mc events, then do the conversion. inj.fill(0.0, np.radians(dec_stack), mc) flux_stack.append(inj.mu2flux(inj_stack[i]) / livetime) results_stack = sorted(zip(sindecrange_stack, inj_stack, flux_stack)) sindecrange_stack, inj_stack, flux_stack = zip(*results_stack) return sindecrange_stack, inj_stack, flux_stack
def plotretry(datafolder_stack_bin): stack_files = [[ cache.load(datafolder_stack_bin + file) for file in os.listdir(misc.ensure_dir(datafolder_stack_bin)) if file.endswith('.array') ]] ##Let's find the declinations used first. decfolders = [ file for file in os.listdir(datafolder_stack_bin) if file.startswith('dec') ] dec_stack = [np.float(decfolder[3:-6]) for decfolder in decfolders] flux_stack = [ stack_files[0][index]['flux'] for index in range(len(stack_files[0])) ] inj_stack = [ stack_files[0][index]['mu'] for index in range(len(stack_files[0])) ] sindecrange_stack = np.sin(np.radians(dec_stack)) results_stack = sorted(zip(sindecrange_stack, inj_stack, flux_stack)) sindecrange_stack, inj_stack, flux_stack = zip(*results_stack) return sindecrange_stack, inj_stack, flux_stack
def tsds(self): """Plot TSDist's.""" plot_dir = misc.ensure_dir('{0}/plots/tsds'.format(self.mode_dir)) tsds = self.bg_tsds for zenith_deg in sorted(tsds): tsd = tsds[zenith_deg] fig = getfig(aspect=16 / 10., width=6) ax = fig.add_subplot(111) ax.semilogy() hl.plot1d(ax, tsd.get_hist(bins=40).normalize(integrate=True), color='b') ts = np.linspace(1e-3, tsd.ts_values.max(), 100) ax.plot(ts, tsd.chi2.pdf(ts), color='.8', ls='--') ax.set_xlabel('TS') ax.set_ylabel('probability density') fig.subplots_adjust(bottom=.14, top=.91, right=.97) savingfig(fig, plot_dir, 'fscale_bg_tsd_zen_{:07.3f}'.format(zenith_deg)) plt.close(fig)
batchsize = opts.batchsize ## We'll assign the proper weighting scheme for the search, then use it to calculate and cache the associated bckg trials: ## llh79 = new_data_multi.init79(energy=True,mode='box') llh86I= new_data_multi.init86I(energy=True,mode='box') llh59= new_data_multi.init59(energy=True,mode='box') llh40= new_data_multi.init40(energy=True,mode='box') #We've loaded in the appropriate llh samples, now let's put them both in the blender (not sure about weighting) samples = [llh40,llh59,llh79,llh86I] llhmodel = new_data_multi.multi_init(samples,energy=True) bckg_trials = StackingMultiPointSourceLLH.do_trials(llhmodel,src_ra,src_dec,w_theo=np.array(weights),n_iter=batchsize) #choose an output dir, and make sure it exists this_dir = os.path.dirname(os.path.abspath (__file__)) out_dir = misc.ensure_dir ('/data/user/brelethford/Output/stacking_sensitivity/30youngSNR/background_trials/') # save the output outfile = out_dir + 'background_batch_{}.array'.format(batch) print 'Saving', outfile, '...' cache.save(bckg_trials, outfile)
beta = 0.9 TSval = TSval_median calctype = "sens" print("Starting sensitivity calculation.") sensitivity = MultiPointSourceLLH.weighted_sensitivity(llh, src_ra=src_ra, src_dec=src_dec, alpha=alpha, beta=beta, inj=inj, TSval=TSval, eps=0.04, n_iter=100) #choose an output dir, and make sure it exists this_dir = os.path.dirname(os.path.abspath(__file__)) if corrected: out_dir = misc.ensure_dir( '/data/user/brelethford/Output/stacking_sensitivity/{0}/{1}yr_corrected/{2}/{3}_inj/{4}/' .format(catalog, str(years), llhweight, injweight, calctype)) else: out_dir = misc.ensure_dir( '/data/user/brelethford/Output/stacking_sensitivity/{0}/{1}yr/{2}/{3}_inj/{4}/' .format(catalog, str(years), llhweight, injweight, calctype)) # save the output outfile = out_dir + 'gamma{}.array'.format(Gamma) print 'Saving', outfile, '...' cache.save(sensitivity, outfile)
["PointSourceTracks", "IC86, 2011"]]#, #["PointSourceTracks", "IC86, 2012-2014"] ] if args.sample == "PointSourceTracks7yr+GFU": seasons.append(["GFU", "IC86, 2015-2017"]) llh, inj = config(seasons, gamma=2.0, seed=args.seed, dec=src_dec) ########## # SKYLAB # ########## outputfolder = '/data/user/brelethford/Output/fast/' if cat: sens_dir = misc.ensure_dir (outputfolder+'stacking_sensitivity/{0}yr/{1}/'.format(len(seasons),cat)) else: sens_dir = misc.ensure_dir (outputfolder+'allsky_sensitivity/{0}yr/{1:.3f}/'.format(len(seasons),np.degrees(src_dec))) ############################################################################### ################################### # BACKGROUND ONLY TS DISTRIBUTION # ################################### print ("\nRunning background only trials ...") def get_background(): if cat: print ("Grabbing background trials from {}...".format(sens_dir)) batches = [cache.load(sens_dir+file) for file in os.listdir(sens_dir) if file.endswith('.array')] trials = np.concatenate(batches,axis=1) else:
arr_exp = np.load(datafolder + '/IC86_exp.npy') arr_mc = np.load(datafolder + '/IC86_MC.npy') #need llhmodel dec_bins = np.unique(np.linspace(-1., 1, 100 + 1)) energy_bins = [np.linspace(1., 10., 36), dec_bins] llh_model = EnergyLLH(energy_bins, sinDec_bins=dec_bins) llh = PointSourceLLH(arr_exp, arr_mc, livetime=332.61, llh_model=llh_model, seed=0) #np.random.randint(2**32), bckg_trials = PointSourceLLH.do_trials(llh, src_ra, src_dec, n_iter=batchsize) #choose an output dir, and make sure it exists this_dir = os.path.dirname(os.path.abspath(__file__)) out_dir = misc.ensure_dir( '/data/user/brelethford/Output/stacking_sensitivity/{0}/{1}yr/{2}/background_trials/' .format(catalog, str(years), llhweight)) # save the output outfile = out_dir + 'background_batch_{}.array'.format(batch) print 'Saving', outfile, '...' cache.save(bckg_trials, outfile)
inj = PointSourceInjector(Gamma, sinDec_bandwidth=.05, src_dec=src_dec, seed=0) results = PointSourceLLH.weighted_sensitivity(llh_single, src_ra=src_ra, src_dec=src_dec, alpha=.5, beta=.9, inj=inj, trials={ 'n_inj': [], 'TS': [], 'nsources': [], 'gamma': [] }, bckg_trials=bckg_trials, eps=0.05, n_iter=250, maxtrial=1000) #Currently have n_iter down from 1000 to reduce estimation time. Also lowering maxtrial from 1000 to 500. print results #choose an output dir, and make sure it exists this_dir = os.path.dirname(os.path.abspath(__file__)) out_dir = misc.ensure_dir(datafolder + 'sensitivity/') # save the output outfile = out_dir + 'single_sensitivity_dec{0:+010.5}.array'.format(dec_deg) print 'Saving', outfile, '...' cache.save(results, outfile)
## Now to plot. ## for dec, ns, gamma in zip(decs, nss, gammas): fig, ax = pfig() plt.scatter(ns, gamma, alpha=0.5, label="{} bg trials".format(str(len(ns))), color='blue') plt.ylim(1, 4) plt.xlim(0., 10.) plt.legend(loc='upper right', prop=propsmall) plt.xlabel('ns') plt.ylabel(r'$\gamma$') plt.title('ns-gamma fit') misc.ensure_dir( '/data/user/brelethford/AGN_Core/Plots/allsky/bg_tsds/{}yr/{}/'.format( str(years), str(dec))) fig.savefig( '/data/user/brelethford/AGN_Core/Plots/allsky/bg_tsds/{}yr/{}/ns_gamma_fit.png' .format(str(years), str(dec))) misc.ensure_dir( '/home/brelethford/public_html/skylab/allsky/bg_tsds/{}yr/{}/'.format( str(years), str(dec))) fig.savefig( '/home/brelethford/public_html/skylab/allsky/bg_tsds/{}yr/{}/ns_gamma_fit.png' .format(str(years), str(dec))) plt.close()
elif n == 4: llh_package = datascript.load4yr() elif n == 3: llh_package = datascript.load3yr() llh = llh_package[0] #inj = PointSourceInjector(gamma = gamma, E0 = 1*TeV, seed = seed, e_range=e_range) #inj.fill(dec, llh[0].exp, llh[0].mc, llh[0].livetime, src_w) ########## # SKYLAB # ########## outputfolder = '/data/user/brelethford/Output/npz/' sens_dir = misc.ensure_dir( outputfolder + 'stacking_sensitivity/{0}yr/{1}/{2}/bg/'.format(n, cat, weight)) ############################################################################### ################################### # BACKGROUND ONLY TS DISTRIBUTION # ################################### print("\nRunning background only trials ...") def get_background(): t0 = time.time() trials = llh.do_trials(args.nscramble, src_ra=src_ra,
) #some Ratio plots rat2 = np.interp(sd, msd, ms2) / s2 rat2disc = np.interp(dd, mdd, md2) / d2 rax.plot(sd, rat2, color=colors[0]) rax.plot(dd, rat2disc, color=colors[1]) rax.set_xlabel(r'$\sin(\delta)$') rax.set_ylabel('Drexel / Official') rax.set_xlim(-1, 1) rax.set_ylim(0.5, 1.5) ax.grid() rax.grid() fig.subplots_adjust(top=.93) icprelim(ax, x=-1, y=1.2 * ymax, ha='left', va='bottom') misc.ensure_dir( '/data/user/brelethford/AGN_Core/Plots/allsky/{}/sens/{}yr/'.format( datatype, str(years))) fig.savefig( '/data/user/brelethford/AGN_Core/Plots/allsky/{}/sens/{}yr/sensitivity.png' .format(datatype, str(years))) misc.ensure_dir( '/home/brelethford/public_html/skylab/allsky/{}/sens/{}yr/'.format( datatype, str(years))) fig.savefig( '/home/brelethford/public_html/skylab/allsky/{}/sens/{}yr/sensitivity.png'. format(datatype, str(years)))
llhmodel = datascript.load7yr_mese(energy=True, stacking=False, mode='all', sirin=sirin) else: llhmodel = datascript.load7yr(energy=True, stacking=False, mode='all', sirin=sirin) #The following catalogs used sirins IC79 and no pull correct of 1.1774 for the first 4yrs of data. elif years == 3: llhmodel = datascript.load3yr(energy=True, stacking=False,mode='all') elif years == 4: llhmodel = datascript.load4yr(energy=True, stacking=False, mode='all') bckg_trials = MultiPointSourceLLH.do_trials(llhmodel,src_ra,src_dec,n_iter=batchsize) #choose an output dir, and make sure it exists this_dir = os.path.dirname(os.path.abspath (__file__)) if mhubertest: if mese: out_dir = misc.ensure_dir ('/data/user/brelethford/Output/all_sky_sensitivity/mstacking_{0}yr_mese/mhubertest/background_trials/'.format(str(years))) else: out_dir = misc.ensure_dir ('/data/user/brelethford/Output/all_sky_sensitivity/mstacking_{0}yr/mhubertest/background_trials/'.format(str(years))) else: if mese: out_dir = misc.ensure_dir ('/data/user/brelethford/Output/all_sky_sensitivity/mstacking_{0}yr_mese/background_trials/'.format(str(years))) else: out_dir = misc.ensure_dir ('/data/user/brelethford/Output/all_sky_sensitivity/mstacking_{0}yr/background_trials/'.format(str(years))) # save the output outdir = misc.ensure_dir(out_dir + 'dec_{0:4f}/'.format(np.degrees(src_dec))) outfile = outdir + 'background_batch_{0}.array'.format(batch)) print ('Saving', outfile, '...') cache.save(bckg_trials, outfile)
llhweight = 'equal' else: raise ValueError('catalog name not found') if injweight is None: injweight = llhweight print("The llhweight is: {}".format(llhweight)) print("The injweight is: {}".format(injweight)) # get unique job id job_id = '{0}_nixtime_{2:.0f}_job_{1}'.format(socket.gethostname(), os.getpid(), time.time()) if mese: jobdir = misc.ensure_dir( '/data/user/brelethford/Output/{0}/stacking_sensitivity/{1}/{2}yr_mese/jobs/' .format(datatype, catalog, str(n))) else: jobdir = misc.ensure_dir( '/data/user/brelethford/Output/{0}/stacking_sensitivity/{1}/{2}yr/jobs/' .format(datatype, catalog, str(n))) # set up submitter submitter = Submitter(job_dir=jobdir + '{0}_{1}'.format(job_id, llhweight)) if catalog == 'SwiftBAT70m': submitter.memory = 18 elif n > 1: submitter.memory = 8 # figure out what dir we're in, and get the path to the actual job script this_dir = os.path.dirname(os.path.abspath(__file__))
["PointSourceTracks", "IC86, 2012-2014"]] n = 7 llh, inj = config(seasons, gamma=gamma, seed=args.seed, dec=src_dec, src_w=src_w) ########## # SKYLAB # ########## outputfolder = '/data/user/brelethford/Output/fast/' sens_dir = misc.ensure_dir( outputfolder + 'stacking_sensitivity/{0}yr/{1}/{2}/sig/gamma_{3}/{4}/'.format( n, cat, weight, gamma, n_inj)) ############################################################################### ################################### # BACKGROUND ONLY TS DISTRIBUTION # ################################### print("\nRunning injection trials with {} events...".format(n_inj)) def get_trials(): t0 = time.time() trials = llh.do_trials(args.nscramble, injector=inj,
elif catalog == 'WHSP_Blazars': weight = 'weights' elif catalog == '2LAC': weight = 'flux' else: raise ValueError('catalog name not found') print("The weight is: {}".format(weight)) # get unique job id job_id = '{0}_nixtime_{2:.0f}_job_{1}'.format(socket.gethostname(), os.getpid(), time.time()) # set up submitter if catalog: jobdir = misc.ensure_dir( '/data/user/brelethford/Output/fast/stacking_sensitivity/{0}yr/{1}/jobs/' .format(str(n), catalog)) else: jobdir = misc.ensure_dir( '/data/user/brelethford/Output/fast/allsky_sensitivity/{0}yr/jobs/'. format(str(n))) submitter = Submitter(job_dir=jobdir + '{0}_{1}'.format(job_id, weight)) if catalog == 'SwiftBAT70m': if int(n) == 7: submitter.memory = 30 else: submitter.memory = 20 elif catalog == 'teststack300': submitter.memory = 20 elif catalog == 'blackhole':
theo_weight=flux, seed=0) results = PointSourceLLH.weighted_sensitivity(llh_flux, src_ra=src_ra, src_dec=src_dec, alpha=.5, beta=.9, inj=inj, trials={ 'n_inj': [], 'TS': [], 'nsources': [], 'gamma': [] }, bckg_trials=bckg_trials, eps=0.01, n_iter=250) print results #choose an output dir, and make sure it exists this_dir = os.path.dirname(os.path.abspath(__file__)) out_dir = misc.ensure_dir( '/data/user/brelethford/Output/stacking_sensitivity/SwiftBAT70m/flux/sensitivity/' ) # save the output outfile = out_dir + 'flux_sensitivity.array' print 'Saving', outfile, '...' cache.save(results, outfile)
src_dec=src_dec, src_w=modelweights['{}'.format(llhweight)]) else: bckg_trials = llh[0].do_trials(n_iter=batchsize, src_ra=src_ra, src_dec=src_dec) #choose an output dir, and make sure it exists this_dir = os.path.dirname(os.path.abspath(__file__)) outputfolder = '/data/user/brelethford/Output/{}/'.format(datatype) if catalog: if mese: out_dir = misc.ensure_dir( outputfolder + 'stacking_sensitivity/{0}/{1}yr_mese/{2}/background_trials/'. format(catalog, str(years), llhweight)) else: out_dir = misc.ensure_dir( outputfolder + 'stacking_sensitivity/{0}/{1}yr/{2}/background_trials/'.format( catalog, str(years), llhweight)) else: if mese: out_dir = misc.ensure_dir( outputfolder + 'allsky_sensitivity/{0}yr_mese/background_trials/dec_{1:4f}/'. format(str(years), np.degrees(src_dec))) else: out_dir = misc.ensure_dir( outputfolder +
src_dec=[np.radians(dec_deg)] src_ra=[0.0] ## NOTE: ADD WEIGHTS HERE FOR THE INJECTED EVENTS!! (Naturally only for flux, redshift. ##For now the weighted sensitivity function only works if there are at least two sources. to push it through for a single source, I'll copy the source location.## ### Mike - This is the step that allows the function weighted_sensitivity to process! ### #src_dec=[src_dec[0],src_dec[0]] #src_ra=[src_ra[0],src_ra[0]] inj = PointSourceInjector(Gamma, sinDec_bandwidth=.05, src_dec= src_dec,seed=0) results = PointSourceLLH.weighted_sensitivity(llh_single,src_ra=src_ra,src_dec=src_dec,alpha=.5,beta=.9,inj=inj,trials={'n_inj':[],'TS':[],'nsources':[],'gamma':[]},bckg_trials=bckg_trials,eps=0.01,n_iter=250, miniter=2500)# maxtrial=1000) #Currently have n_iter down from 1000 to reduce estimation time. Also lowering maxtrial from 1000 to 500. print results #choose an output dir, and make sure it exists this_dir = os.path.dirname(os.path.abspath (__file__)) out_dir = misc.ensure_dir (outfolder+'sensitivity/') # save the output outfile = out_dir + 'dec{0:+010.5}.array'.format(dec_deg) print 'Saving', outfile, '...' cache.save(results, outfile)
#We give it dec as a commandline argument. parser = OptionParser(usage='%prog [options]') parser.add_option('--dec', dest='dec', type=float, default=0., metavar='DEC', help='sin of the source declination.') #parser = argparse.ArgumentParser(description='Produce a sensitivity') #parser.add_argument('dec', type=float, # help='sin of the source declination.') #args = parser.parse_args() #dec_deg = np.arcsin(args.dec)*180./np.pi opts, args = parser.parse_args() dec_deg = np.arcsin(opts.dec) * 180. / np.pi #Now we do the work... sensitivity_array = sensitivity_skylab(src_dec=opts.dec, n_bckg=200000) #choose an output dir, and make sure it exists this_dir = os.path.dirname(os.path.abspath(__file__)) ## NOTE: THE OUT DIRECTORY CHANGES DEPENDING ON WHICH SUBMITTER I'M TESTING!: out_dir = misc.ensure_dir( '/data/user/brelethford/Output/all_sky_sensitivity/results/n_bckg/') # save the output outfile = out_dir + 'sensitivity__dec_{0:+010.5}.array'.format(dec_deg) print 'Saving', outfile, '...' cache.save(sensitivity_array, outfile)
llhweight = 'equal' else: raise ValueError('catalog name not found') if injweight is None: injweight = llhweight print("The llhweight is: {}".format(llhweight)) print("The injweight is: {}".format(injweight)) # get unique job id job_id = '{0}_nixtime_{2:.0f}_job_{1}'.format(socket.gethostname(), os.getpid(), time.time()) jobdir = misc.ensure_dir( '/data/user/brelethford/Output/stacking_sensitivity/{0}/jstacking/jobs/'. format(catalog)) # set up submitter submitter = Submitter(job_dir=jobdir + '{0}_{1}'.format(job_id, llhweight)) # figure out what dir we're in, and get the path to the actual job script this_dir = os.path.dirname(os.path.abspath(__file__)) job_script = this_dir + '/background_trials.py' # get the metaproject path env_shell = os.getenv('I3_BUILD') + '/env-shell.sh' # build the lists of commands and labels commands, labels = [], [] ## For this we'll need to do background trials in batches of 1000. So... batchsize = 100
src_ra = np.radians(src_ra) src_dec = np.radians(src_dec) print('my sources are at declination(s):') print(src_dec) ## There are three modelweights I can use, so lets put them in a dictionary for easy access. ## modelweights = {'flux': flux, 'redshift': list(np.power(redshift, -2))} import data ## We'll assign the proper weighting scheme for the search, then use it to calculate and cache the associated bckg trials: ## llhmodel = data.init(energy=True, weighting=modelweights['flux']) bckg_trials = PointSourceLLH.background_scrambles(llhmodel, src_ra, src_dec, alpha=0.5, maxiter=batchsize) #choose an output dir, and make sure it exists this_dir = os.path.dirname(os.path.abspath(__file__)) out_dir = misc.ensure_dir( '/data/user/brelethford/Output/stacking_sensitivity/SwiftBAT70m/northsouth/{}/background_trials/' .format(sky)) # save the output outfile = out_dir + 'background_batch_{}.array'.format(batch) print 'Saving', outfile, '...' cache.save(bckg_trials, outfile)
llh59= new_data_multi.init59(energy=True,mode='box') llh40= new_data_multi.init40(energy=True,mode='box') #We've loaded in the appropriate llh samples, now let's put them both in the blender (not sure about weighting) if years == 2: samples = [llh79,llh86I] elif years == 3: samples = [llh59,llh79,llh86I] elif years == 4: samples = [llh40,llh59,llh79,llh86I] llhmodel = new_data_multi.multi_init(samples,energy=True) bckg_trials = StackingMultiPointSourceLLH.do_trials(llhmodel,src_ra,src_dec,w_theo=np.array(modelweights['{}'.format(llhweight)]),n_iter=batchsize) #choose an output dir, and make sure it exists this_dir = os.path.dirname(os.path.abspath (__file__)) out_dir = misc.ensure_dir ('/data/user/brelethford/Output/stacking_sensitivity/SwiftBAT70m/bstacking_{0}yr/{1}/background_trials/'.format(len(samples),llhweight)) # save the output outfile = out_dir + 'background_batch_{}.array'.format(batch) print 'Saving', outfile, '...' cache.save(bckg_trials, outfile)
opts, args = parser.parse_args() batch = opts.batch batchsize = opts.batchsize years = opts.years ##For this check we'll use the gamma weighting scheme for llh method and injection. We'll add years of data depending on the variable 'years'. llh59 = data_multi.init59(energy=True, weighting=flux) llh79 = data_multi.init79(energy=True, weighting=flux) llh86 = data_multi.init86I(energy=True, weighting=flux) samples = [llh59, llh79, llh86] llhmodel = data_multi.multi_init(samples, energy=True) bckg_trials = PointSourceLLH.background_scrambles(llhmodel, src_ra, src_dec, alpha=0.5, maxiter=batchsize) #choose an output dir, and make sure it exists this_dir = os.path.dirname(os.path.abspath(__file__)) out_dir = misc.ensure_dir( '/data/user/brelethford/Output/stacking_sensitivity/2LAC/flux_3yr/background_trials/' ) # save the output outfile = out_dir + 'background_batch_{}.array'.format(batch) print 'Saving', outfile, '...' cache.save(bckg_trials, outfile)