def test_03_dipole(self): a = 0.5 vmax = np.array([1, 1, 1]) pix_max = hpt.vec2pix(self.nside, vmax) dipole = hpt.dipole_pdf(self.nside, a, vmax, pdf=False) self.assertTrue( np.allclose(np.array([pix_max, self.npix]), np.array([np.argmax(dipole), np.sum(dipole)]))) dipole_v = hpt.dipole_pdf(self.nside, a, vmax, pdf=False) self.assertTrue(np.allclose(dipole, dipole_v, rtol=1e-3))
def test_05_rand_vec_from_map(self): dipole_pdf = hpt.dipole_pdf(self.nside, 1, 0, 0, 1) vecs = hpt.rand_vec_from_map(dipole_pdf, 10000) vec_mean = np.mean(np.array(vecs), axis=-1) vec_mean /= np.sqrt(np.sum(vec_mean**2)) self.assertTrue(vec_mean[0] < 0.1) self.assertTrue(vec_mean[1] < 0.1) self.assertTrue(vec_mean[2] > 0.99)
def test_08e_rotate_map(self): # there and back again for i in range(5): _inmap = hpt.dipole_pdf(16, a=0.5, v=1, y=0, z=0, pdf=False) rot_axis = coord.rand_vec(1) rot_angle = 4 * np.pi * np.random.random(1) - 2 * np.pi _rot_map = hpt.rotate_map(_inmap, rot_axis, rot_angle) # should be different after rotation self.assertTrue(not np.allclose(_inmap, _rot_map)) again_inmap = hpt.rotate_map(_rot_map, rot_axis, -rot_angle) # should be the same again after rotating back self.assertTrue(np.allclose(_inmap, again_inmap, rtol=1e-2)) self.assertTrue(np.abs(np.mean(_inmap - again_inmap)) < 1e-10) self.assertTrue(np.std(_inmap - again_inmap) < 1e-3)
def test_06_statistic(self): nside = 8 dipole_pdf = hpt.dipole_pdf(nside, 0.5, 0, 0, 1) vecs = hpt.rand_vec_from_map(dipole_pdf, 100000) count = hpt.statistic(nside, *vecs, statistics='count') self.assertTrue( np.allclose(dipole_pdf / max(dipole_pdf), count / max(count), atol=0.5)) frequency = hpt.statistic(nside, *vecs, statistics='frequency') self.assertTrue(np.allclose(frequency, count / max(count))) with self.assertRaises(ValueError): hpt.statistic(nside, *vecs, statistics='mean') with self.assertRaises(ValueError): hpt.statistic(nside, *vecs, statistics='rms') with self.assertRaises(NotImplementedError): hpt.statistic(nside, *vecs, statistics='std') weights = 1. / dipole_pdf[hpt.vec2pix(nside, *vecs)] hpt.statistic(nside, *vecs, statistics='mean', vals=weights) hpt.statistic(nside, *vecs, statistics='rms', vals=weights)
print("Test: module healpytools.py") # The healpytools provides an extension for the healpy framework (https://healpy.readthedocs.io), # a tool to pixelize the sphere into cells with equal solid angle. There are various # functionalities on top of healpy, e.g. sample random directions in pixel or create # distributions on the sphere (dipole, fisher, experiment exposure). ncrs = 3000 # number of cosmic rays log10e_min = 18.5 # minimum energy in log10(E / eV) nside = 64 # resolution of the HEALPix map (default: 64) npix = hpt.nside2npix(nside) # Create a dipole distribution for a healpy map lon, lat = np.radians(45), np.radians(60) # Position of the maximum of the dipole (healpy and astrotools definition) vec_max = hpt.ang2vec(lat, lon) # Convert this to a vector amplitude = 0.5 # amplitude of dipole dipole = hpt.dipole_pdf(nside, amplitude, vec_max, pdf=False) skymap.heatmap(dipole, opath='dipole.png') # Draw random events from this distribution pixel = hpt.rand_pix_from_map(dipole, n=ncrs) # returns 3000 random pixel from this map vecs = hpt.rand_vec_in_pix(nside, pixel) # Random vectors within the drawn pixel skymap.scatter(vecs, c=auger.rand_energy_from_auger(ncrs, log10e_min), opath='dipole_events.png') # Create a healpy map that follows the exposure of an observatory at latitude # a0 = -35.25 (Pierre Auger Observatory) and maximum zenith angle of 60 degree exposure = hpt.exposure_pdf(nside, a0=-35.25, zmax=60) skymap.heatmap(exposure, opath='exposure.png') # Note, if you want to sample from the exposure healpy map random vectors you # have to be careful with the above method hpt.rand_vec_in_pix, # as the exposure healpy map reads out the exposure value in the pixel centers,
def test_01_rand_pix_from_map(self): a = 0.5 vmax = np.array([1, 1, 1]) dipole = hpt.dipole_pdf(self.nside, a, *vmax) pixel = hpt.rand_pix_from_map(dipole, self.stat) self.assertTrue((pixel >= 0).sum() and (pixel < self.npix).sum())