def test_06_rotate(self): v1 = coord.rand_vec(stat) rot_axis = np.hstack(coord.rand_vec(1)) angle = 0.25 v2 = coord.rotate(v1, rot_axis, angle) angles = coord.angle(v1, v2) self.assertTrue((angles > 0).all() & (angles <= angle).all()) # rotate back v3 = coord.rotate(v2, rot_axis, -angle) v4 = coord.rotate(v2, rot_axis, 2 * np.pi - angle) self.assertTrue(np.allclose(v1, v3)) self.assertTrue(np.allclose(v3, v4)) # when rotating around z-axis and vectors have z=0: all angles have to be 0.25 rot_axis = np.array([0, 0, 1]) v1 = coord.ang2vec(coord.rand_phi(stat), np.zeros(stat)) v2 = coord.rotate(v1, rot_axis, angle) angles = coord.angle(v1, v2) self.assertTrue((angles > angle - 1e-3).all() & (angles < angle + 1e-3).all()) # when rotating around z-axis all angles correspond to longitude shift angles = 2 * np.pi * np.random.random(stat) v1 = coord.rand_vec(stat) lon1, lat1 = coord.vec2ang(v1) v2 = np.array( [coord.rotate(vi, rot_axis, ai) for vi, ai in zip(v1.T, angles)]).T lon2, lat2 = coord.vec2ang(v2) self.assertTrue(np.allclose(lat1, lat2)) lon_diff = lon1 - lon2 lon_diff[lon_diff < 0] += 2 * np.pi self.assertTrue(np.allclose(lon_diff, angles))
def test_08_rotate_one_vec_multi_angles(self): v = coord.rand_vec(1) rot = coord.rand_vec(1) angles = np.random.random(stat) v_rot = coord.rotate(v, rot, angles) self.assertTrue(np.shape(v_rot)[1] == stat) for i in range(stat): vi_rot = coord.rotate(v, rot, angles[i]) self.assertTrue(np.allclose(np.squeeze(vi_rot), v_rot[:, i]))
def test_06a_rotate_multi(self): v = coord.rand_vec(stat) rot = coord.rand_vec(stat) angles = np.random.random(stat) v_rot = coord.rotate(v, rot, angles) self.assertTrue(np.shape(v_rot) == np.shape(v)) for i in range(stat): vi_rot = coord.rotate(v[:, i], rot[:, i], angles[i]) self.assertTrue(np.allclose(vi_rot, v_rot[:, i]))
def test_01d_rand_fisher_vecs_shape(self): shape = (5, 10) v0 = coord.rand_vec(shape) sigma = 0.1 * np.random.random(shape) vecs = coord.rand_fisher_vec(v0, kappa=1. / sigma**2) self.assertTrue(vecs.shape == (v0.shape)) self.assertTrue((coord.angle(vecs, v0) < 5 * sigma).all())
def test_01c_rand_fisher_vecs(self): v0 = coord.rand_vec(stat) sigma = 0.1 vecs = coord.rand_fisher_vec(v0, kappa=1. / sigma**2, n=stat) angles = coord.angle(vecs, v0) self.assertTrue((angles >= 0).all()) self.assertTrue((np.mean(angles) > 0.5 * sigma) & (np.mean(angles) < 2. * sigma)) self.assertTrue((angles < 5 * sigma).all())
def test_08a_rotate_map(self): # size(rot_axis) = 1 and size(rot_angle) = 1 nside = 64 npix = hpt.nside2npix(nside) for i in range(10): ipix = np.random.randint(npix) _inmap = np.zeros(npix) _inmap[ipix] = 1 rot_axis = np.squeeze( coord.rand_vec(1)) if i < 5 else coord.rand_vec(1) rot_angle = 4 * np.pi * np.random.random() - 2 * np.pi _rot_map = hpt.rotate_map(_inmap, rot_axis, rot_angle) v_hpt = hpt.pix2vec(nside, np.argmax(_rot_map)) # compare with well tested coord rotate function v_coord = coord.rotate(hpt.pix2vec(nside, ipix), rot_axis, rot_angle) self.assertTrue( coord.angle(v_hpt, v_coord) < hpt.max_pixrad(nside))
def test_04_rand_vec_on_surface(self): n = 100000 x = coord.rand_vec(n) v = coord.rand_vec_on_surface(x) st_ct = coord.angle(x, v) # should follow cos(theta)*sin(theta) distribution (see above) self.assertAlmostEqual(np.mean(st_ct), np.pi / 4., places=2) # check mean self.assertAlmostEqual(np.var(st_ct), (np.pi**2 - 8) / 16., places=2) # check variance x = coord.rand_vec(1) v = coord.rand_vec_on_surface(x) self.assertTrue(x.shape == v.shape) self.assertTrue(coord.angle(x, v) < np.pi / 2.) # test z rotation x0 = np.array([0, 0, 1]) v = coord.rand_vec_on_surface(x0) self.assertTrue(v[2] > 0)
def test_01_relative_exposure(self): vecs = coord.rand_vec(stat) decs = coord.get_declination(vecs, coord_system='eq') exp = coord.exposure_equatorial(decs) # all exposure values between 0 and 1 self.assertTrue(np.max(exp) < 1) self.assertTrue(np.min(exp) == 0) # exposure monotonically decreasing with declination dec_sort = np.argsort(decs[exp > 0]) exp_sort = np.argsort(exp[exp > 0]) self.assertTrue(np.allclose(dec_sort, exp_sort[::-1]))
def test_06b_rotate_multi_shape(self): shape = (5, 10) v = coord.rand_vec(shape) rot = coord.rand_vec(shape) angles = np.random.random(shape) v_rot = coord.rotate(v, rot, angles) self.assertTrue(np.shape(v_rot) == np.shape(v)) v_rot_flatten = coord.rotate(v.reshape(3, -1), rot.reshape(3, -1), angles.flatten()) self.assertTrue(np.allclose(v_rot, v_rot_flatten.reshape(v.shape))) v_rot = coord.rotate(v, np.array([1, 0, 0]), angles) self.assertTrue(np.shape(v_rot) == np.shape(v)) v_rot_flatten = coord.rotate(v.reshape(3, -1), np.array([1, 0, 0]), angles.flatten()) self.assertTrue(np.allclose(v_rot, v_rot_flatten.reshape(v.shape))) v_rot = coord.rotate(v, np.array([1, 0, 0]), np.pi) self.assertTrue(np.shape(v_rot) == np.shape(v)) v_rot_flatten = coord.rotate(v.reshape(3, -1), np.array([1, 0, 0]), np.pi) self.assertTrue(np.allclose(v_rot, v_rot_flatten.reshape(v.shape)))
def test_09b_rotate_zaxis_to_x_shape(self): shape = (5, 10) zaxis = np.array([[0], [0], [1]]) v = coord.rand_vec(shape) _scalar = np.sum(v * zaxis[..., np.newaxis], axis=0) # rotate to arbitrary point on sphere x0 = np.array([[1], [1], [1]]) / np.sqrt(3) v_rot = coord.rotate_zaxis_to_x(v, x0) self.assertTrue( np.allclose( _scalar, np.sum(v_rot * coord.atleast_kd(x0, v_rot.ndim), axis=0)))
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_08d_rotate_map(self): # size(rot_axis) = n and size(rot_angle) = 1 nside = 64 npix = hpt.nside2npix(nside) for i in range(10): ipix = np.random.randint(npix) _inmap = np.zeros(npix) _inmap[ipix] = 1 rot_axis = coord.rand_vec(5) rot_angle = 4 * np.pi * np.random.random(1) - 2 * np.pi _rot_map = hpt.rotate_map(_inmap, rot_axis, rot_angle) # compare with well tested coord rotate function for j in range(5): v_hpt = hpt.pix2vec(nside, np.argmax(_rot_map[j])) v_coord = coord.rotate(hpt.pix2vec(nside, ipix), rot_axis[:, j], rot_angle) self.assertTrue( coord.angle(v_hpt, v_coord) < hpt.max_pixrad(nside))
def test_07_sph_unit_vector(self): lon1, lat1 = 0, 0 e_r, e_phi, e_theta = coord.sph_unit_vectors(lon1, lat1) self.assertTrue(np.allclose(e_r, np.array([1, 0, 0]))) self.assertTrue(np.allclose(e_phi, np.array([0, 1, 0]))) self.assertTrue(np.allclose(e_theta, np.array([0, 0, 1]))) vecs2 = coord.rand_vec(10) lon2, lat2 = coord.vec2ang(vecs2) e_r, e_phi, e_theta = coord.sph_unit_vectors(lon2, lat2) # check that vecs are aligned with e_r and orthogonal to e_phi, e_theta self.assertTrue(np.allclose(np.sum(vecs2 * e_r, axis=0), np.ones(10))) self.assertTrue( np.allclose(np.sum(vecs2 * e_phi, axis=0), np.zeros(10))) self.assertTrue( np.allclose(np.sum(vecs2 * e_theta, axis=0), np.zeros(10))) # check if all unit vectors are normed self.assertTrue(np.allclose(np.sum(e_r**2, axis=0), np.ones(10))) self.assertTrue(np.allclose(np.sum(e_phi**2, axis=0), np.ones(10))) self.assertTrue(np.allclose(np.sum(e_theta**2, axis=0), np.ones(10))) # check for right-handed e_theta_cross = np.cross(e_r, e_phi, axis=0) self.assertTrue(np.allclose(e_theta, e_theta_cross))
def test_04_vec2ang(self): v = coord.rand_vec(stat) phi, theta = coord.vec2ang(v) self.assertTrue((phi >= -np.pi).all() and (phi <= np.pi).all() and (theta >= -np.pi).all() and (theta <= np.pi).all())
# Using the observed_vector() function, it is possible to calculate the flux / transparancy # of the galactic magnetic field outside of the galaxy by computing the sum of all # observed rays reaching the earth originating from the extragalactic pixel pix. # The larger the amount of flux for that given pixel, the more rays originating from that # direction reach the earth # brute force calculation of the flux map flux = np.zeros(npix) for pix in range(npix): flux[pix] = np.sum(gamale.observed_vector(lens_part, pix)) # gamale function to calculate the flux flux = gamale.flux_map(lens_part) skymap.heatmap(flux, label='Flux [a.u.]', opath='flux_map.png') # Finally, an entire probability distributions of extragalactic cosmic rays can be # 'lensed' to Earth by a fast matrix multiplication: # We create an extragalctic distributions of 30 gaussian source priors eg_map = np.zeros(npix) for i in range(30): v_src = coord.rand_vec() sigma = 10 + 10 * np.random.random() eg_map += hpt.fisher_pdf(nside, v_src, k=1/np.deg2rad(sigma)**2, sparse=False) eg_map /= np.sum(eg_map) # normalize to probability density distribution skymap.heatmap(eg_map, label='p', vmin=0, vmax=np.round(np.max(eg_map), 4), opath='extragalactic_distribution.png') # matrix multiplication to obtain an observed map obs_map = lens_part.dot(eg_map) skymap.heatmap(obs_map, label='p', vmin=0, vmax=np.round(np.max(obs_map), 4), opath='lensed_observed_distribution.png')