def test_UL(): # Signal definition sig = harp.HARPix().add_iso(16) sig.add_func(lambda d: np.exp(-d**2 / 2 / 20**2), mode='dist', center=(0, 0)) # Background definition bg = harp.zeros_like(sig) bg.add_func(lambda l, b: 0. / (b**2 + 1.)**0.5 + 1.0) fluxes, noise, systematics, exposure = get_model_input( [sig], bg, [dict(err=bg * 0.1, sigmas=[ 20., ], Sigma=None, nside=16)], bg * 100.) m = Swordfish(fluxes, noise, systematics, exposure, solver='cg') I = m.fishermatrix() print I ec = EffectiveCounts(m) UL = ec.upperlimit(0.05, 0) ULg = ec.upperlimit(0.05, 0, gaussian=True) s, b = ec.effectivecounts(0, 1.0) print "Total signal counts (theta = 1):", ec.counts(0, 1.0) print "Eff. signal counts (theta = 1):", s print "Eff. bkg counts (theta = 1) :", b print "Upper limit on theta :", UL print "Upper limit on theta (gaussian):", ULg
def test_covariance(): nside = 8 # Signal definition sig = harp.HARPix().add_iso(nside).add_singularity((50, 50), .1, 50, n=100) sig.add_func(lambda d: np.exp(-d**2 / 2 / 20**2), mode='dist', center=(0, 0)) sig.mul_sr() # Covariance matrix definition cov = HARPix_Sigma(sig) bg = sig * .0 bg.data += 1 cov.add_systematics(err=bg * 0.1, sigmas=[ 20., ], Sigma=None, nside=nside) x = np.zeros_like(sig.data) x[901] = 1. y = cov.dot(x) sig.data = y sig.div_sr() z = sig.get_healpix(128) hp.mollview(z, nest=True) plt.savefig('test.eps')
def test_MW_dSph(): nside = 32 def plot_harp(h, filename): m = h.get_healpix(128) vmax = np.log10(m).max() vmin = vmax - 2 hp.mollview(np.log10(m), nest=True, min=vmin, max=vmax) plt.savefig(filename) dims = () # Signal definition spec = 1. MW = harp.HARPix(dims=dims).add_iso(2).add_singularity((0, 0), 0.1, 2., n=10) MW.add_func(lambda d: spec / (1 + d)**2, mode='dist', center=(0, 0)) pos = (50, 40) # dSph = harp.HARPix(dims = dims).add_singularity(pos, 0.1, 20, n = 10) # dSph.add_func(lambda d: 0.1*spec/(.1+d)**2, mode = 'dist', center=pos) sig = MW #+ dSph sig.data += .001 # EGBG plot_harp(sig, 'sig.eps') # Background definition bg = harp.HARPix(dims=dims).add_iso(64) bg.add_func(lambda l, b: 1 / (b + 1)**2) plot_harp(bg, 'bg.eps') # Covariance matrix definition cov = harpix_Sigma(sig) cov.add_systematics(err=bg * 0.1, sigma=10., Sigma=None, nside=0) # Set up swordfish fluxes = [sig.data.flatten()] noise = bg.get_formatted_like(sig).data.flatten() systematics = cov exposure = np.ones_like(noise) * 0.001 m = sf.Swordfish(fluxes, noise, systematics, exposure, solver='direct') F = m.effectiveinfoflux(0) f = harp.HARPix.from_data(sig, F) plot_harp(f, 'test.eps')
def test_infoflux(): sig = harp.HARPix().add_singularity((0, 0), .1, 50, n=100) sig.data += 1. bkg = sig fluxes, noise, systematics, exposure = get_model_input([sig], bkg, None, 1.) m = Swordfish(fluxes, noise, systematics, exposure) F = m.effectiveinfoflux(0) f = harp.HARPix.from_data(sig, F, div_sr=True) m = f.get_healpix(256) hp.mollview(m, nest=True) plt.savefig('test.eps')
def test_simple(): def plot_harp(h, filename): m = h.get_healpix(128) hp.mollview(m, nest=True) plt.savefig(filename) dims = () nside = 16 # Signal definition spec = 1. sig = harp.HARPix(dims=dims).add_iso(nside).add_singularity((50, 50), .1, 20, n=100) sig.add_func(lambda d: np.exp(-d**2 / 2 / 20**2), mode='dist', center=(0, 0)) sig.add_func(lambda d: 1 / (d + 1)**1, mode='dist', center=(50, 50)) sig.data += 0.1 # EGBG plot_harp(sig, 'sig.eps') sig.mul_sr() # Background definition bg = harp.zeros_like(sig) bg.add_func(lambda l, b: 0. / (b**2 + 1.)**0.5 + 1.0) plot_harp(bg, 'bg.eps') bg.mul_sr() #bg.print_info() # Covariance matrix definition cov = HARPix_Sigma(sig) cov.add_systematics(err=bg * 0.1, sigmas=[ 20., ], Sigma=None, nside=64) # Set up swordfish fluxes = [sig.data.flatten()] noise = bg.data.flatten() systematics = cov exposure = np.ones_like(noise) * 10000. m = Swordfish(fluxes, noise, systematics, exposure, solver='cg') F = m.effectiveinfoflux(0, thetas=[0.000], psi=1.) f = harp.HARPix.from_data(sig, F) f.div_sr() plot_harp(f, 'test.eps') quit()
def test(): H = harp.HARPix().add_iso(4).add_disc((80, 00), 40, 16).add_disc((-80, 00), 40, 16) H.data += 1. # Covariance matrix definition cov = harpix_Sigma(H) flat = harp.HARPix().add_iso(16, fill=1.) cov.add_systematics(err=flat * .1, sigma=3, Sigma=None, nside=128) # Set up swordfish fluxes = [H.get_data(mul_sr=True)] noise = fluxes[0] expmap = noise * 0. + 1e5 systematics = cov m = sf.Swordfish(fluxes, noise, systematics, expmap, solver='cg') F = m.effectiveinfoflux(0) f = harp.HARPix.from_data(H, F, div_sr=True) print min(f.data), max(f.data) m = f.get_healpix(128) hp.mollview(m, nest=True) plt.savefig("MW.eps")
def get_Jmap(): Interp_sig = get_los() #sig = harp.HARPix(dims=dims).add_singularity((0,0), 1, 20, n = 10) Jmap = harp.HARPix().add_iso(32).add_disc((0, 0), 20, 64) Jmap.add_func(lambda d: Interp_sig(d), mode='dist', center=(0, 0)) return Jmap
def MW_dSph(): nside = 32 bg_hpx, exposure_hpx = get_BG() dims = () J = get_Jmap() Jex = get_extragalactic() J = J + Jex # Signal definition spec = 1. dSph = harp.HARPix(dims=dims).add_iso(1, fill=1.) pos_list = [(50, 40), (-20, 30), (80, -80), (42, -15)] Jlist = [3e21, 3e21, 6e21, 2e21] for J0, pos in zip(Jlist, pos_list): dSph = dSph.add_singularity(pos, 0.1, 5, n=10) #dSph = harp.HARPix(dims = dims).add_disc(pos, 10, 62).add_disc((50,-40), 10, 32) dSph.add_func(lambda d: J0 / (.1 + d)**2, mode='dist', center=pos) sig = J + dSph #sig.data += 1e21 # EGBG plot_harp(sig, 'sig.eps', maxfac=1.0) # Background definition #bg = harp.HARPix(dims = dims).add_iso(64) bg = harp.HARPix.from_healpix(bg_hpx, nest=False) plot_harp(bg, 'bg.eps', maxfac=0.01) exposure = harp.HARPix.from_healpix(exposure_hpx, nest=False) # Covariance matrix definition cov = harpix_Sigma(sig) bg_flat = deepcopy(bg) bg_flat.data *= 0 bg_flat.data += 1e-8 # cov.add_systematics(err = bg_flat*0.1, sigma = .25, Sigma = None, nside =64) # cov.add_systematics(err = bg_flat*0.1, sigma = .5, Sigma = None, nside =16) # cov.add_systematics(err = bg_flat*0.1, sigma = 1., Sigma = None, nside =16) # cov.add_systematics(err = bg_flat*0.1, sigma = 2., Sigma = None, nside =64) # cov.add_systematics(err = bg_flat*0.1, sigma = 3., Sigma = None, nside =64) # cov.add_systematics(err = bg_flat*0.1, sigma = 4., Sigma = None, nside =64) # cov.add_systematics(err = bg_flat*0.1, sigma = 7., Sigma = None, nside =64) cov.add_systematics(err=bg_flat * 0.1, sigma=10., Sigma=None, nside=64) cov.add_systematics(err=bg_flat * 0.3, sigma=1e10, Sigma=None, nside=8) cov.add_systematics(err=bg_flat * 0.1, sigma=20., Sigma=None, nside=64) #cov.add_systematics(err = bg_flat*0.1, sigma = 25., Sigma = None, nside = 1) #cov.add_systematics(err = bg_flat*0.1, sigma = 30., Sigma = None, nside = 1) #cov.add_systematics(err = bg_flat*0.1, sigma = 35., Sigma = None, nside = 1) #cov.add_systematics(err = bg_flat*0.1, sigma = 40., Sigma = None, nside = 1) # Set up swordfish fluxes = [sig.get_data(mul_sr=True)] noise = bg.get_formatted_like(sig).get_data(mul_sr=True) expmap = exposure.get_formatted_like(sig).get_data() * 1e-4 systematics = cov #systematics = None m = sf.Swordfish(fluxes, noise, systematics, expmap, solver='cg') F = m.effectiveinfoflux(0) f = harp.HARPix.from_data(sig, F, div_sr=True) plot_harp(f, 'MW.eps', maxfac=0.1)
def test_3d(): def plot_harp(h, filename, dims=()): m = h.get_healpix(128, idxs=dims) hp.mollview(m, nest=True) plt.savefig(filename) def plot_rock(h, filename): nside = 32 N = h.dims[0] npix = hp.nside2npix(nside) rings = hp._pixelfunc.pix2ring(nside, np.arange(npix), nest=True) T = harp.get_trans_matrix(h, nside) data = harp.trans_data(T, h.data) out = [] for r in range(1, 4 * nside): mask = rings == r out.append(data[mask].sum(axis=0)) out = np.array(out) plt.imshow(out, aspect=0.8 * N / (4 * nside)) plt.xlabel("Energy [AU]") plt.ylabel("Latitude [AU]") plt.savefig(filename) x = np.linspace(0, 10, 20) nside = 16 dims = (len(x), ) # Signal definition spec_sig = np.exp(-(x - 5)**2 / 2) sig = harp.HARPix(dims=dims).add_iso(nside).add_singularity((50, 50), 1, 20, n=10) sig.add_func(lambda d: spec_sig * np.exp(-d**2 / 2 / 20**2), mode='dist', center=(0, 0)) sig.add_func(lambda d: spec_sig / (d + 1)**1, mode='dist', center=(50, 50)) sig.mul_sr() # Background definition bg = harp.zeros_like(sig) spec_bg = x * 0. + 1. bg.add_func(lambda l, b: spec_bg * (0. / (b**2 + 1.)**0.5 + 0.1)) bg.mul_sr() # Covariance matrix definition cov = HARPix_Sigma(sig) corr = lambda x, y: np.exp(-(x - y)**2 / 2 / 3**2) Sigma = get_sigma(x, corr) cov.add_systematics(err=bg * 0.1, sigmas=[ 20., ], Sigma=Sigma, nside=16) # Set up swordfish fluxes = [sig.data.flatten()] noise = bg.data.flatten() systematics = cov exposure = np.ones_like(noise) * 100.0 m = Swordfish(fluxes, noise, systematics, exposure, solver='cg') F = m.effectiveinfoflux(0) f = harp.HARPix.from_data(sig, F) f.div_sr() plot_harp(f, 'test.eps', dims=(11, )) plot_rock(f, 'test2.eps')