def test_fit_2D(self): """ Test 2D Hist.curve_fit() and Hist.spline_fit(). """ np.random.seed(0) h = hl.hist(np.random.normal(0, 1, (2, int(1e5))), bins=50, range=(-4, 4)) hn = h.normalize() # curve_fit def gauss2(x, sigma): return 1 / (2*np.pi * sigma**2) \ * np.exp (-np.sum (x**2, axis=0) / (2 * sigma**2)) sigma = hn.curve_fit(gauss2)[0][0] self.assertGreater(.02, np.abs(sigma - 1)) # spline_fit s = hn.spline_fit() deltas = np.abs(s(*np.meshgrid(*hn.centers)) - hn.values) self.assertTrue( np.all(3e-2 > np.abs(s(*np.meshgrid(*hn.centers)) - hn.values))) s = hn.spline_fit(log=True) deltas = np.abs(s(*np.meshgrid(*hn.centers)) - hn.values) self.assertTrue( np.all(3e-2 > np.abs(s(*np.meshgrid(*hn.centers)) - hn.values)))
def test_range(self): """ Test basic hist() results. """ x = np.random.uniform(0, 1, int(1e4)) # default values h = hl.hist(x) self.assertFalse(h.log[0]) dx0 = abs(h.bins[0][0] - np.min(x)) dx1 = abs(h.bins[0][-1] - np.max(x)) self.assertTrue(dx0 <= eps) self.assertTrue(dx1 <= eps) self.assertEqual(h.n_dim, 1) self.assertEqual(len(h.n_bins), 1) self.assertEqual(len(h.bins), 1) self.assertEqual(h.n_bins[0], len(h.centers[0])) self.assertEqual(h.n_bins[0], len(h.bins[0]) - 1) self.assertEqual( h.n_bins[0], np.sum((h.bins[0][:-1] < h.centers[0]) & (h.bins[0][1:] > h.centers[0]))) self.assertEqual(h.n_bins[0], len(h.values)) self.assertTrue( eps >= np.abs(np.sum(h.widths) - (h.range[0][1] - h.range[0][0]))) self.assertTrue(np.max(np.abs(h.widths[0] - h.volumes) < eps)) # log scale h = hl.hist(x, log=True) self.assertTrue(h.log[0]) dx0 = abs(h.bins[0][0] - np.min(x)) dx1 = abs(h.bins[0][-1] - np.max(x)) self.assertTrue(dx0 <= eps) self.assertTrue(dx1 <= eps) self.assertEqual( h.n_bins[0], np.sum((h.bins[0][:-1] < h.centers[0]) & (h.bins[0][1:] > h.centers[0]))) self.assertTrue( eps >= np.abs(np.sum(h.widths) - (h.range[0][1] - h.range[0][0]))) # simple 3D x = np.random.uniform(0, 1, (3, int(1e3))) h = hl.hist(x, range=(0, 1)) self.assertTrue(len(h.bins) == len(h.centers) == h.n_dim) self.assertEqual(h.values.shape, tuple(h.n_bins)) self.assertTrue(3 * eps >= np.abs(np.sum(h.volumes) - 1))
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 test_sum(self): """ Test Hist.sum(), Hist.cumsum(), and related methods. """ np.random.seed(0) shape = 4, int(2e4) h = hl.hist(np.random.uniform(0, 1, shape), bins=(2, 4, 5, 10), range=(0, 1)) self.assertEqual(h.sum().values, shape[1]) self.assertEqual(h.sum().volumes, 0) self.assertEqual(h.project([1, 2]).values.shape, (4, 5)) with self.assertRaises(ValueError): h.sum([5]) for i in xrange(h.n_dim): for j in xrange(h.n_dim): if i == j: continue hn = h.normalize([i, j]) self.assertEqual(np.shape(hn.values), np.shape(h.values)) self.assertEqual(np.shape(hn.errors), np.shape(h.errors)) self.assertTrue( 1e-6 > np.max(np.abs(hn.integrate([i, j]).values - 1))) hn = h.normalize([i, j], integrate=False) self.assertEqual(np.shape(hn.values), np.shape(h.values)) self.assertEqual(np.shape(hn.errors), np.shape(h.errors)) self.assertTrue( 1e-6 > np.max(np.abs(hn.sum([i, j]).values - 1))) for i in xrange(h.n_dim): hn = h.normalize([i], integrate=False) hncs = hn.cumsum([i]) hncs_last = hncs.get_slice(-1, i) self.assertTrue(1e-6 > np.max(np.abs(hncs_last.values) - 1)) h = hl.hist(10**np.random.uniform(0, 1, int(1e4)), bins=4, range=(1, 10), log=True) hn = h.normalize() hd = h.normalize(density=True) self.assertGreater(4 * eps, np.abs(hn.integrate().values - 1)) self.assertGreater(4 * eps, np.abs(hd.integrate().values - 1)) self.assertFalse(np.any(hn.values == hd.values))
def test_transpose(self): """ Test Hist.T. """ h = hl.hist(np.random.uniform(0, 1, (2, int(1e4))), bins=(4, 10), range=(0, 1)) hT = h.T self.assertEqual(h.bins[::-1], hT.bins) self.assertTrue(np.all(h.values.T == hT.values)) self.assertTrue(np.all(h.errors.T == hT.errors))
def test_rebin(self): """ Test Hist.rebin(). """ # lin bins h = hl.hist(np.random.uniform(0, 1, int(1e4)), bins=4, range=(0, 1)) hr = h.rebin(0, [0, .5, 1]) self.assertEqual(h.values[:2].sum(), hr.values[0]) self.assertEqual(h.values[2:4].sum(), hr.values[1]) # log bins h = hl.hist(10**np.random.uniform(0, 2, int(1e4)), bins=4, range=(1, 100), log=True) hr = h.rebin(0, [1, 10, 100]) self.assertEqual(h.values[:2].sum(), hr.values[0]) self.assertEqual(h.values[2:4].sum(), hr.values[1]) with self.assertRaises(ValueError): h + hr
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 plotDecBand(bandnum): fig_band=plt.figure() ax=plt.gca() histdec_data=histlite.plot1d(ax,histlite.hist(muex86I_data[bandmask_data[bandnum]],**hargs1), color='r', label='data') #use gamma=2,bandnum=0 histdec_sim=histlite.plot1d(ax,energyhist_sim(fsim_86_1,2,bandnum), color='b', label='MC') ax.loglog() ax.set_xlabel('MuEx') ax.set_ylabel('events') ax.legend(loc='upper right') ax.set_ylim(ymin=0) ax.set_title('energy pdf in IC86-I, gamma=2: '+decrange[bandnum]) fig_band.savefig(projfolder+'Plots/AGNCore/Stacking/IC86I_energydist: band '+str(bandnum))
def makehist_inj(datafolder): files = cache.load(datafolder+'gamma2.0.array') TSs=files['trials']['TS'] chi2fit_flux = fitfun(TSs, df=1., floc=0., fscale=1.) weib_flux = weibfun(TSs,df=2., floc=0., fscale=1.) print('background plus injected: median TS = {}'.format(str(np.asscalar(chi2fit_flux.isf(0.5))))) print ('max TS = {}'.format(str(max(TSs)))) print ('Percentage of TS=0 is: {}'.format(str(1.0-np.float(np.count_nonzero(TSs))/np.float(len(TSs))))) ##Now we make hists of the test statistics ## flux_hist =histlite.hist(TSs,bins=bins,range=range) return flux_hist,chi2fit_flux
def test_err(self): """ Test error propagation. """ h1 = hl.hist([.25, .75], bins=1, range=(0, 1)) h2 = hl.hist([.25, .5, .75], bins=1, range=(0, 1)) self.assertTrue(eps > np.abs(h1.get_errors([.25]) - np.sqrt(2))) self.assertTrue(1e-6 > np.max(h1.errors - np.sqrt(2))) self.assertTrue(1e-6 > np.max(h2.errors - np.sqrt(3))) hadd = h1 + h2 self.assertTrue(1e-6 > np.max(hadd.errors - np.sqrt(2 + 3))) hadd = 1 + h1 self.assertTrue(1e-6 > np.max(hadd.errors - np.sqrt(2))) hsub = h2 - h1 self.assertTrue(1e-6 > np.max(hsub.errors - np.sqrt(2 + 3))) hsub = 1 - h1 self.assertTrue(1e-6 > np.max(hsub.errors - np.sqrt(2))) hmul = h1 * h2 self.assertTrue( 1e-6 > np.max(hmul.errors - 6 * np.sqrt(1 / np.sqrt(2) + 1 / np.sqrt(3)))) hmul = 2 * h1 self.assertTrue(1e-6 > np.max(hmul.errors - 2 * np.sqrt(2))) hdiv = h2 / h1 self.assertTrue( 1e-6 > np.max(hdiv.errors - 1.5 * np.sqrt(1 / np.sqrt(2) + 1 / np.sqrt(3)))) hdiv = h1 / 2. self.assertTrue(1e-6 > np.max(hdiv.errors - 0.5 * np.sqrt(2))) x = np.random.uniform(0, 1, int(1e4)) w = np.random.uniform(.2, .8, x.size) hall = hl.hist(x, w, bins=10, range=(0, 1)) hcut = hl.hist(x[w > .5], w[w > .5], bins=10, range=(0, 1)) div = hcut / hall eff = hcut.efficiency(hall) self.assertTrue(np.all(eff.errors < div.errors))
def test_sample(self): """ Test Hist.sample(). """ np.random.seed(0) h = hl.hist(np.random.uniform(0, 1, (2, int(1e3))), bins=4, range=(0, 1)) # test sampling in both dimensions x, y = h.sample(int(1e5)) h2 = hl.hist((x, y), bins=4, range=(0, 1)) deltas = np.abs((h2 / 1e2 - h).values) self.assertLess(np.max(deltas - h.errors), 0) self.assertGreater(np.min(x), h.bins[0][0]) self.assertGreater(np.min(y), h.bins[1][0]) self.assertLess(np.max(x), h.bins[0][-1]) self.assertLess(np.max(y), h.bins[1][-1]) # test sampling for a given x y = h.sample(int(1e5), x[0]) h1 = hl.hist(y, bins=4, range=(0, 1)) deltas = np.abs((h1 / 1e2 / 4).values - h[x[0]].values) self.assertLess(np.max(deltas - h.errors), 0)
def test_contain(self): """ Test Hist.contain() and related methods. """ x = np.random.uniform(0, 1, int(3e5)) y = np.random.normal(0, 1, int(3e5)) h = hl.hist((x, y), bins=(4, 101), range=((0, 1), (-4, 4))) # median should be around 0 hmed = h.median(1) self.assertGreater(1e-6, np.abs(np.mean(hmed.values))) # containment hist errors should reflect sigma=1 hcont = h.contain_project(1, n_sigma=1) self.assertGreater(.05, np.abs(np.mean(hcont.errors) - 1))
def test_other_create(self): """ Test special-purpose Hist.hist_like() and Hist.hist_direct(). """ np.random.seed(0) x = np.random.uniform(0, 1, (2, int(1e4))) h = hl.hist(x, bins=10, range=(0, 1)) hd = hl.hist_direct(x, bins=10, range=2 * [(0, 1)]) self.assertTrue(np.all(h.values == hd.values)) self.assertTrue(np.all(h.errors == hd.errors)) h_like_h = hl.hist_like(h, x) h_like_hd = hl.hist_like(hd, x) self.assertTrue(np.all(h_like_h.values == h.values)) self.assertTrue(np.all(h_like_hd.values == h.values)) self.assertTrue(np.all(h_like_h.errors == h.errors)) self.assertTrue(np.all(h_like_hd.errors == h.errors))
def makehist(datafolder): files = [cache.load(datafolder+file) for file in os.listdir(datafolder) if file.endswith('.array')] TS=[] for file in files: for entry in file: if entry[0]==0: if entry[1]<0: TS.append(0) else: TS.append(entry[1]) TSs=TS chi2fit_flux = fitfun(TSs, df=1., floc=0., fscale=1.) print('background only: median TS = {}'.format(str(np.asscalar(chi2fit_flux.isf(0.5))))) print ('max TS = {}'.format(str(max(TSs)))) print ('Number of trials is: {}'.format(str(len(TSs)))) print ('Percentage of TS=0 is: {}'.format(str(1.0-np.float(np.count_nonzero(TSs))/np.float(len(TSs))))) ##Now we make hists of the test statistics ## flux_hist =histlite.hist(TSs,bins=bins,range=range) return flux_hist,chi2fit_flux
def test_fit_1D(self): """ Test 1D Hist.curve_fit() and Hist.spline_fit(). """ np.random.seed(0) h = hl.hist(np.random.normal(0, 1, int(1e5)), bins=50, range=(-4, 4)) hn = h.normalize() # curve_fit def gauss(x, sigma): return 1 / (np.sqrt (2*np.pi) * sigma) \ * np.exp (-x**2 / (2 * sigma**2)) sigma = hn.curve_fit(gauss)[0][0] self.assertGreater(.01, np.abs(sigma - 1)) # spline_fit s = hn.spline_fit() self.assertTrue(np.all(1e-2 > np.abs(s(hn.centers) - hn.values))) s = hn.spline_fit(log=True) self.assertTrue(np.all(1e-2 > np.abs(s(hn.centers) - hn.values)))
weighteach = ['uniform', 'flux', 'redshift'] TSeach = [TS_uniform, TS_flux, TS_redshift] chi_each = [chi2fit_uniform, chi2fit_flux, chi2fit_redshift] weib_each = [weib_uniform, weib_flux, weib_redshift] for weight, TS, chi2fit in zip(weighteach, TSeach, chi_each): print('For ' + str(weight) + ', the median TS is: ' + str(np.asscalar(chi2fit.isf(0.5)))) print(' max TS is: ' + str(max(TS))) print(' percentage of TS = 0 is: ') + str( 1.0 - np.float(np.count_nonzero(TS)) / np.float(len(TS))) ##Now we make hists of the test statistics ## bins = 50 range = (0.0, 40.0) uniform_hist = histlite.hist(TS_uniform, bins=bins, range=range) flux_hist = histlite.hist(TS_flux, bins=bins, range=range) redshift_hist = histlite.hist(TS_redshift, bins=bins, range=range) fig_bckg = plt.figure(figsize=(w, .75 * w)) ax = plt.gca() colors = ['blue', 'green', 'red'] for chi2_fit, weib_fit, color in zip(chi_each, weib_each, colors): x = np.linspace(0, 20, 100) ax.plot(x, chi2_fit.pdf(x), linestyle=':', color=color, label=r'$\tilde{\chi}^2$') #: df='+str(round(chi2_fit.par[0],2))) plt.axvline(np.asscalar(chi2_fit.isf(0.5)), color=color)
def energyhist_sim(fsim,gamma,bandnum): weight=simweight(fsim,gamma,mask=bandmask_sim[bandnum]) return histlite.hist (muex86I_sim[bandmask_sim[bandnum]],weights=liveTime86I*weight,**hargs1)
picklefolder = '/data/user/brelethford/Data/SwiftBAT70m/pickle/' datafolder = '/data/user/brelethford/Output/all_sky_sensitivity/results/single_stacked/' ## Read in background trials previously logged ## files = [ cache.load(datafolder + file) for file in os.listdir(datafolder) if file.endswith('.array') ] bckg_single = [] for file in files: bckg_single.append(list(file['TS'])) ##Now we make hists of the test statistics ## bins = 80 range = (0.0, 20.0) single_hist = histlite.Hist.normalize( histlite.hist(bckg_single[0], bins=bins, range=range)) ## Now to plot. ## fig_bckg = plt.figure(figsize=(w, .75 * w)) ax = plt.gca() ##I'll include a chi squared distribution w/ DOF=1 (and 2, just because). I'll also show the best fitting chi2 dist for each weighting scheme.## chifit_single = chi2.fit(bckg_single[0])[0] chi_degs = [1, 2, chifit_single] colors = ['black', 'gray', 'blue'] for df, color in zip(chi_degs, colors): x = np.linspace(chi2.ppf(0.01, df), chi2.ppf(0.99999, df), 100) rv = chi2(df) chi_dist = rv.pdf(x) ax.plot(x,
datafolder_flux = '/data/user/brelethford/Output/stacking_sensitivity/2LAC/flux_3yr/background_trials/' bckg_flux = getBckg(datafolder_flux) print (bckg_flux['TS_beta']) #bckg_uniform = cache.load(picklefolder+'bckg_trials_uniform.pickle') #bckg_redshift = cache.load(picklefolder+'bckg_trials_redshift.pickle') #bckg_flux = cache.load(picklefolder+'bckg_trials_flux.pickle') ##Now we make hists of the test statistics ## bins = 80 range = (0.0,20.0) flux_hist =histlite.Hist.normalize(histlite.hist(bckg_flux['TS'],bins=bins,range=range)) ##I'll include a chi squared distribution w/ DOF=1 (and 2, just because). I'll also show the best fitting chi2 dist for each weighting scheme.## chifit_flux = fitfun(bckg_flux['TS'],df=2., floc=0., fscale=1.).par[0] ## Now to plot. ## fig_bckg = plt.figure (figsize=(w, .75*w)) ax=plt.gca() chi_degs = [1,2,chifit_flux] colors=['black','gray','green'] for df,color in zip(chi_degs,colors): x = np.linspace(chi2.ppf(0.01,df),chi2.ppf(0.99999,df), 100) rv = chi2(df) chi_dist=rv.pdf(x)
'TS_beta': TS_beta, 'gamma': np.asarray(gamma) } return bckg_trials datafolder_2LAC = '/data/user/brelethford/Output/stacking_sensitivity/2LAC/flux_3yr/background_trials/' bckg_2LAC = getBckg(datafolder_2LAC) print(bckg_2LAC['TS_beta']) ##Now we make hists of the test statistics ## bins = 100 range = (0.0, 20.0) h_2LAC = histlite.hist(bckg_2LAC['TS'], bins=bins, range=range) ##I'll include a chi squared distribution w/ DOF=1 (and 2, just because). I'll also show the best fitting chi2 dist for each weighting scheme.## chi2fit_2LAC = fitfun(bckg_2LAC['TS'], df=2., floc=0., fscale=1.) #Now for a weibfit on the distribution. weib_flux_2LAC = weibfun(bckg_2LAC['TS'], df=2., floc=0., fscale=1.) ## Now to plot. ## fig_bckg = plt.figure(figsize=(w, .75 * w)) ax = plt.gca() weib_fits = [weib_flux_2LAC] chi2_fits = [chi2fit_2LAC] colors = ['blue'] for chi2_fit, weib_fit, color in zip(chi2_fits, weib_fits, colors):
ben_exp = cache.load (datafolder + 'exp.pickle') ben_mc = cache.load (datafolder + 'MC.pickle') bad_exp = cache.load (datafolder + 'exp_bad_sigma.pickle') bad_mc = cache.load (datafolder + 'MC_bad_sigma.pickle') stefan_exp = np.genfromtxt(stefanfolder+'IC86-I_data.txt',names=True) stefan_mc = np.genfromtxt(stefanfolder+'IC86-I_MC.txt',names=True) #extract the sigmas ben_exp_sigma =ben_exp['sigma'] ben_mc_sigma =ben_mc['sigma'] ben_hist_exp = histlite.hist(ben_exp_sigma,bins=100,log=True) ben_hist_mc = histlite.hist(ben_mc_sigma,bins=100,log=True) bad_exp_sigma =bad_exp['sigma'] bad_mc_sigma =bad_mc['sigma'] bad_hist_exp = histlite.hist(bad_exp_sigma,bins=100,log=True) bad_hist_mc = histlite.hist(bad_mc_sigma,bins=100,log=True) stefan_exp_sigma = stefan_exp['sigma'] stefan_mc_sigma = stefan_mc['sigma'] stefan_hist_exp = histlite.hist(stefan_exp_sigma,bins=100,log=True) stefan_hist_mc = histlite.hist(stefan_mc_sigma,bins=100,log=True) ## Now to plot. ##
datafolder_flux = '/data/user/brelethford/Output/stacking_sensitivity/2LAC/flux/background_trials/' bckg_flux = getBckg(datafolder_flux) print(bckg_flux['TS_beta']) #bckg_uniform = cache.load(picklefolder+'bckg_trials_uniform.pickle') #bckg_redshift = cache.load(picklefolder+'bckg_trials_redshift.pickle') #bckg_flux = cache.load(picklefolder+'bckg_trials_flux.pickle') ##Now we make hists of the test statistics ## bins = 80 range = (0.0, 20.0) flux_hist = histlite.Hist.normalize( histlite.hist(bckg_flux['TS'], bins=bins, range=range)) ##I'll include a chi squared distribution w/ DOF=1 (and 2, just because). I'll also show the best fitting chi2 dist for each weighting scheme.## chifit_flux = fitfun(bckg_flux['TS'], df=2., floc=0., fscale=1.).par[0] ## Now to plot. ## fig_bckg = plt.figure(figsize=(w, .75 * w)) ax = plt.gca() chi_degs = [1, 2, chifit_flux] colors = ['black', 'gray', 'green'] for df, color in zip(chi_degs, colors): x = np.linspace(chi2.ppf(0.01, df), chi2.ppf(0.99999, df), 100) rv = chi2(df) chi_dist = rv.pdf(x) ax.plot(x,
true86I = mc86I['trueE'] weight_86 = mc86I['ow'] * true86I**(-2) weight_79 = mc79['ow'] * true79**(-2) weight_59 = mc59['ow'] * true59**(-2) weight_40 = mc40['ow'] * true40**(-2) bins = 80 bins2d = (80, 80) ### Energy ### energyrange = (10, 1e7) fig_energyhist = plt.figure(figsize=(w, .75 * w)) ax = plt.gca() h_energy_86 = histlite.hist(muex86I_data, bins=bins, range=energyrange, log=True) h_energy_79 = histlite.hist(muex79_data, bins=bins, range=energyrange, log=True) h_energy_59 = histlite.hist(mue59_data, bins=bins, range=energyrange, log=True) h_energy_40 = histlite.hist(mue40_data, bins=bins, range=energyrange, log=True) histlite.plot1d(ax, h_energy_40, histtype='step', label='IC40', color='purple') histlite.plot1d(ax, h_energy_59, histtype='step', label='IC59', color='green') histlite.plot1d(ax, h_energy_79, histtype='step', label='IC79', color='blue') histlite.plot1d(ax, h_energy_86, histtype='step', label='IC86', color='red') #ax.set_title("4yr PS data - energy") ax.set_xlabel(r"Energy Proxy (GeV)") ax.set_ylabel("Counts per bin")
#ax.set_xlabel(r'flux $[10^{-12} ergs/s/cm^2]$') #plt.subplots_adjust (left=.2, bottom=.2) #ax.set_ylabel(r'z') #plt.legend(loc='upper right', prop=propsmall) #fig_redshiftxflux.savefig('/data/user/brelethford/AGN_Core/Plots/flux_vs_z.pdf') # #print ('Number of sources = ' + str(len(src_ra))) #It's also probably worth having a sindec hist of the sources, but I want to weight the sources to each of the three schemes. First let's get in sindec... sindec = np.sin(src_dec) #Now I have to establish three hists - one for each weighting scheme... bins = 100 range = (-1.0, 1.0) unif_hist = histlite.Hist.normalize(histlite.hist(sindec, bins=bins, range=range), integrate=True) flux_hist = histlite.Hist.normalize(histlite.hist(sindec, weights=np.array(flux), bins=bins, range=range), integrate=True) redshift_hist = histlite.Hist.normalize(histlite.hist(sindec, weights=np.power( redshift, -2), bins=bins, range=range), integrate=True) #Now plot.
nch59_sim, nch59_data = cache.load(filename_pickle + "IC59/NCh.pickle") ra40_sim, sindec40_sim, ra40_data, sindec40_data, ra40_true, sindec40_true, energy40_true, mue40_sim, mue40_data, sigma40_sim, sigma40_data, OneWeight_IC40, dpsi_IC40 = cache.load( filename_pickle + "IC40/coords.pickle") nch40_sim, nch40_data = cache.load(filename_pickle + "IC59/NCh.pickle") bins = 80 ### Energy ### fig_energyhist = plt.figure(figsize=(w, .75 * w)) ax = plt.gca() h_energy_86 = histlite.Hist.normalize( histlite.hist(muex86I_data, bins=bins, log=True)) h_energy_79 = histlite.Hist.normalize( histlite.hist(muex79_data, bins=bins, log=True)) h_energy_59 = histlite.Hist.normalize( histlite.hist(mue59_data, bins=bins, log=True)) h_energy_40 = histlite.Hist.normalize( histlite.hist(mue40_data, bins=bins, log=True)) histlite.plot1d(ax, h_energy_79, histtype='step', label='IC79', color='blue') histlite.plot1d(ax, h_energy_86, histtype='step', label='IC86', color='red') histlite.plot1d(ax, h_energy_59, histtype='step', label='IC59', color='green') histlite.plot1d(ax, h_energy_40, histtype='step', label='IC40', color='purple') ax.set_title("4yr PS data - energy") ax.set_xlabel(r"logE (GeV)") ax.set_ylabel("Relative Abundance") ax.loglog()
weight_3yr = mc3yr['ow'] * true3yr**(-2) weight_86 = mc86I['ow'] * true86I**(-2) weight_79 = mc79['ow'] * true79**(-2) weight_59 = mc59['ow'] * true59**(-2) weight_40 = mc40['ow'] * true40**(-2) bins = 80 bins2d = (80, 80) ### Energy ### energyrange = (10, 1e7) fig_energyhist = plt.figure(figsize=(w, .75 * w)) ax = plt.gca() h_energy_3yr = histlite.hist(muex3yr_data, bins=bins, range=energyrange, log=True) h_energy_86 = histlite.hist(muex86I_data, bins=bins, range=energyrange, log=True) h_energy_79 = histlite.hist(muex79_data, bins=bins, range=energyrange, log=True) h_energy_59 = histlite.hist(mue59_data, bins=bins, range=energyrange, log=True) h_energy_40 = histlite.hist(mue40_data, bins=bins, range=energyrange, log=True) histlite.plot1d(ax, h_energy_40, histtype='step', label='IC40', color='blue') histlite.plot1d(ax, h_energy_59, histtype='step', label='IC59', color='green') histlite.plot1d(ax, h_energy_79, histtype='step', label='IC79', color='red') histlite.plot1d(ax, h_energy_86, histtype='step', label='IC86I', color='cyan')
sindec_data = exp['sinDec'] sindec_sim = mc['sinDec'] sigma_data = exp['sigma'] sigma_sim = mc['sigma'] weight = mc['ow'] * true**(-2) bins = 80 bins2d = (80, 80) ### Energy ### energyrange = (1e3, 200000) fig_energyhist = plt.figure(figsize=(w, .75 * w)) ax = plt.gca() h_energy = histlite.hist(muex_data, bins=bins, range=energyrange, log=True) histlite.plot1d(ax, h_energy, histtype='step', label='MESE', color='k') #ax.set_title("4yr PS data - energy") ax.set_xlabel(r"Energy Proxy (GeV)") ax.set_ylabel("Counts per bin") ax.loglog() ax.set_ylim(1, 1e2) plt.subplots_adjust(left=.2, bottom=.2) ax.legend(loc="best", prop=propsmall) fig_energyhist.savefig(plotfolder + 'energy_hists_MESE.png') ###Declination### decrange = (-1., 1.) fig_dechist = plt.figure(figsize=(w, .75 * w)) ax = plt.gca()
nch40_sim, nch40_data = cache.load(filename_pickle + "IC40/NCh.pickle") bigrange = (-1., 1.) # (np.sin(np.radians(160.)),np.sin(np.radians(240.))) bins = 20 ### zen true and zen reco for dpsi > 160 deg ### ###Declination### fig_dechist = plt.figure(figsize=(w, .75 * w)) ax = plt.gca() h_dec_86 = histlite.Hist.normalize( histlite.hist(sindec86I_sim[(np.degrees(dpsi_IC86I) > 160.)], bins=bins, range=bigrange)) h_dec_79 = histlite.Hist.normalize( histlite.hist(sindec79_sim[(np.degrees(dpsi_IC79) > 160.)], bins=bins, range=bigrange)) h_dec_59 = histlite.Hist.normalize( histlite.hist(sindec59_sim[(np.degrees(dpsi_IC59) > 160.)], bins=bins, range=bigrange)) h_dec_40 = histlite.Hist.normalize( histlite.hist(sindec40_sim[(np.degrees(dpsi_IC40) > 160.)], bins=bins, range=bigrange)) histlite.plot1d(ax, h_dec_40, histtype='step', label='IC40', color='purple')
bckg_flux = getBckg(datafolder_flux) bckg_redshift = getBckg(datafolder_redshift) print (bckg_uniform['TS_beta']) print (bckg_flux['TS_beta']) print (bckg_redshift['TS_beta']) #bckg_uniform = cache.load(picklefolder+'bckg_trials_uniform.pickle') #bckg_redshift = cache.load(picklefolder+'bckg_trials_redshift.pickle') #bckg_flux = cache.load(picklefolder+'bckg_trials_flux.pickle') ##Now we make hists of the test statistics ## bins = 1000 range = (0.0,20.0) uniform_hist =histlite.hist(bckg_uniform['TS'],bins=bins,range=range) redshift_hist =histlite.hist(bckg_redshift['TS'],bins=bins,range=range) flux_hist =histlite.hist(bckg_flux['TS'],bins=bins,range=range) #I'll include a chi squared distribution w/ DOF=1 (and 2, just because). I'll also show the best fitting chi2 dist for each weighting scheme.## chi2fit_uniform = fitfun(bckg_uniform['TS'],df=2., floc=0., fscale=1.) chi2fit_redshift = fitfun(bckg_redshift['TS'],df=2., floc=0., fscale=1.) chi2fit_flux = fitfun(bckg_flux['TS'],df=2., floc=0., fscale=1.) ## Now to plot. ## fig_bckg = plt.figure (figsize=(w, .75*w)) ax=plt.gca() chi2_fits = [chi2fit_uniform,chi2fit_redshift,chi2fit_flux] colors=['blue','red','green'] for chi2_fit,color in zip(chi2_fits,colors):
def plotpull2d(year): #The following currently devoted to making energy and angular error plots for a year of data. # init likelihood class mc = np.load(filename_pickle + "IC{}_MC.npy".format(year)) dpsi = [ astro.angular_distance(mc['trueRa'][i], mc['trueDec'][i], mc['ra'][i], np.arcsin(mc['sinDec'][i])) for i in range(len(mc)) ] mc_corr = np.load(filename_pickle + "IC{}_corrected_MC.npy".format(year)) dpsi_corr = [ astro.angular_distance(mc_corr['trueRa'][i], mc_corr['trueDec'][i], mc_corr['ra'][i], np.arcsin(mc_corr['sinDec'][i])) for i in range(len(mc_corr)) ] colors = ['g'] #['b','g','y','r'] colors_corr = ['r'] #['b','g','y','r'] gamma = np.array([2.]) #np.linspace(1., 2.7, 4) fig_pull, (ax) = plt.subplots(ncols=1, figsize=(10, 5)) dec = np.arcsin(mc["sinDec"]) angdist = np.degrees(dpsi) dec_corr = np.arcsin(mc_corr["sinDec"]) angdist_corr = np.degrees(dpsi_corr) #medians = [misc.weighted_median(np.log10(np.degrees(mc["sigma"])/(angdist)),mc["ow"] * mc["trueE"]**(-g)) for g in gamma] #for g in range(len(gamma)): # ax1.axvline(medians[g], color=colors[g], linestyle = 'dotted', alpha = 0.3) #ax1.hist([np.log10(np.degrees(mc_corr["sigma"])/(angdist_corr)) for i in gamma], label = [r"'Corrected' $\Delta \psi / \sigma$ - $\gamma={0:.1f}$".format(g) for g in gamma], linestyle = 'dotted', # weights=[mc_corr["ow"] * mc_corr["trueE"]**(-g) for g in gamma], color=[colors_corr[g] for g in range(len(gamma))], # histtype="step", bins=100, normed=True) #medians = [misc.weighted_median(np.log10(np.degrees(mc_corr["sigma"])/(angdist_corr)),mc["ow"] * mc["trueE"]**(-g)) for g in gamma] #for g in range(len(gamma)): # ax1.axvline(medians[g], color=colors_corr[g], marker='+',alpha = 0.3) #ax1.set_title(r"Reco MC $\Delta \psi / \sigma$ Check - IC{}".format(str(year))) #ax1.set_xlabel(r"log$\Delta \psi / \sigma_{ang}$") #ax1.set_ylabel("Relative Abundance") #ax1.set_ylim(0,1.5) #ax1.set_xlim(-2.5,2.5) #ax1.axvline(x=0, color='k') #ax1.axvline(x=np.log10(1./1.1774), color='k') #set gamma g = 2.0 pull = np.log10(np.degrees(mc["sigma"]) / (angdist)) #Need to calc the median of pull for each energyrange: pullrange = (-4, 4) erange = (3, 8) ebins = 50 pullbins = 200 eedges = np.linspace(erange[0], erange[1], ebins + 1) ezones = zip(eedges[:-1], eedges[1:]) emid = [np.median(ezone) for ezone in ezones] emasks = [ (np.log10(mc["trueE"]) > ezone[0]) & (np.log10(mc["trueE"]) < ezone[1]) for ezone in ezones ] medians = [ misc.weighted_median(pull[emask], mc["ow"][emask] * mc["trueE"][emask]**(-g)) for emask in emasks ] #pull_corr = [(np.degrees(mc_corr["sigma"]))/(angdist_corr) for i in gamma] hpull = histlite.hist((np.log10(mc['trueE']), pull), weights=mc["ow"] * mc["trueE"]**(-g), bins=(ebins, pullbins), range=(erange, pullrange), log=(False, False)) histlite.plot2d( ax, hpull.normalize([-1]), label=[r"$\Delta \psi / \sigma$ - $\gamma={0:.1f}$".format(g)], cbar=True, cmap='jet', color=colors[0]) ax.scatter(emid, medians, color='white') ax.axhline(y=np.log10(1.1774), color='white', linestyle='dashed') #ax.hist([(np.degrees(mc_corr["sigma"]))/(angdist_corr) for i in gamma], label = [r"'Corrected' $\Delta \psi / \sigma$ - $\gamma={0:.1f}$".format(g) for g in gamma], linestyle = 'dotted', # weights=[mc_corr["ow"] * mc_corr["trueE"]**(-g) for g in gamma], color=[colors_corr[g] for g in range(len(gamma))], # histtype="step", bins=1000, range = (0,5), normed=True) ax.set_title("Pull for IC{}".format(str(year))) ax.legend(loc="upper right") ax.set_xlim(3, 8) ax.set_xlabel("log(trueE[GeV])") ax.set_ylim(-2, 2) ax.set_ylabel(r"log($\Delta \psi / \sigma_{ang}$)") fig_pull.savefig(filename_plots + 'opp_pull_IC{}.pdf'.format(str(year)))