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 plot_thrust(self, n, t, **kwargs): """ Visualize the thrust observables in the ROI. :param n: Thrust axis as given by astrotools.obs.thrust()[1] :param t: Thrust values as returned by astrotools.obs.thrust()[0] :param kwargs: Keywords passed to matplotlib.pyplot.plot() for axis visualization """ kwargs.setdefault('c', 'red') linestyle_may = kwargs.pop('linestyle', 'solid') alpha_may = kwargs.pop('alpha', 0.5) lon, lat = coord.vec2ang(n[0]) # fill thrust array (unit vector phi runs in negative lon direction) e_phi = coord.sph_unit_vectors(lon, lat)[1] sign = np.sign(e_phi[2] - n[1][2]) phi_major = sign * coord.angle(e_phi, n[1])[0] phi_minor = sign * coord.angle(e_phi, n[2])[0] if np.abs(phi_major - phi_minor) < 0.99 * np.pi / 2.: phi_minor = 2 * np.pi - phi_minor t23_ratio = t[1] / t[2] # mark the principal axes n3 u = np.array(np.cos(phi_minor)) v = -1. * np.array(np.sin(phi_minor)) urot, vrot, x, y = self.m.rotate_vector(u, v, np.rad2deg(lon), np.rad2deg(lat), returnxy=True) _phi = np.arctan2(vrot, urot) s = self.r_roi * (t[1] / 0.15) * self.scale / t23_ratio self.m.plot([x - np.cos(_phi) * s, x + np.cos(_phi) * s], [y - np.sin(_phi) * s, y + np.sin(_phi) * s], linestyle='dashed', alpha=0.5, **kwargs) # mark the principal axes n2 u = np.array(np.cos(phi_major)) v = -1. * np.array(np.sin(phi_major)) urot, vrot, x, y = self.m.rotate_vector(u, v, np.rad2deg(lon), np.rad2deg(lat), returnxy=True) _phi = np.arctan2(vrot, urot) s = self.r_roi * (t[1] / 0.15) * self.scale self.m.plot([x - np.cos(_phi) * s, x + np.cos(_phi) * s], [y - np.sin(_phi) * s, y + np.sin(_phi) * s], linestyle=linestyle_may, alpha=alpha_may, **kwargs) # mark the center point self.m.plot((x), (y), 'o', color=kwargs.pop('c'), markersize=10)
def test_07_smear_sources_dynamically(self): sim = ObservedBound(self.nside, self.nsets, self.ncrs) sim.set_energy(log10e_min=19.) sim.set_charges('AUGER') sim.set_sources(1) sim.set_rigidity_bins(np.arange(17., 20.5, 0.02)) sim.smear_sources(delta=0.1, dynamic=True) sim.arrival_setup(1.) crs = sim.get_data(convert_all=True) rigs = sim.rigidities rig_med = np.median(rigs) vecs1 = coord.ang2vec(crs['lon'][rigs >= rig_med], crs['lat'][rigs >= rig_med]) vecs2 = coord.ang2vec(crs['lon'][rigs < rig_med], crs['lat'][rigs < rig_med]) # Higher rigidities experience higher deflections self.assertTrue(np.mean(coord.angle(vecs1, sim.sources)) < np.mean(coord.angle(vecs2, sim.sources)))
def test_05_rand_vec_on_sphere(self): n = 100000 x, v = coord.rand_vec_on_sphere(n) 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 self.assertTrue((np.abs(np.sum(v, axis=-1)) < 2 * np.sqrt(n)).all()) # check isotropy in v x, v = coord.rand_vec_on_sphere(1) self.assertTrue(x.shape == v.shape) self.assertTrue(coord.angle(x, v) < np.pi / 2.)
def test_09c_rotate_zaxis_to_x(self): v = np.array([np.zeros(stat), np.zeros(stat), np.ones(stat)]) angle = np.deg2rad(5) v_fisher = coord.rand_fisher_vec(v, kappa=1 / angle**2) self.assertTrue(v_fisher.shape == (3, stat)) self.assertTrue( np.abs(np.mean(coord.angle(v, v_fisher)) - angle) / angle < 0.5)
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_01b_rand_fisher_vec(self): vmean = np.array([0, 0, 1]) sigma = 0.25 vecs = coord.rand_fisher_vec(vmean, kappa=1. / sigma**2, n=stat) angles = coord.angle(vecs, vmean) 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_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_03_angle(self): ipix = np.random.randint(0, self.npix, self.stat) jpix = np.random.randint(0, self.npix, self.stat) ivec = hpt.pix2vec(self.nside, ipix) jvec = hpt.pix2vec(self.nside, jpix) angles = hpt.angle(self.nside, ipix, jpix) from astrotools import coord angles_coord = coord.angle(ivec, jvec) self.assertTrue(np.allclose(angles, angles_coord))
def test_19_apply_uncertainties(self): sim = ObservedBound(self.nside, self.nsets, self.ncrs) log10e = sim.set_energy(log10e_min=19., log10e_max=21., energy_spectrum='power_law', gamma=-3) sim.set_charges('mixed') xmax = sim.set_xmax() sim.set_sources(10) sim.set_rigidity_bins(np.arange(17., 20.5, 0.02)) sim.smear_sources(delta=0.1, dynamic=True) sim.arrival_setup(1.) vecs = hpt.pix2vec(sim.nside, np.hstack(sim.crs['pixel'])) sim.apply_uncertainties(err_e=0.1, err_a=1, err_xmax=10) # check that array are not equal but deviations are smaller than 5 sigma self.assertTrue(not (log10e == sim.crs['log10e']).all()) self.assertTrue((np.abs(10**(log10e - 18) - 10**(sim.crs['log10e'] - 18)) < 5*0.1*10**(log10e - 18)).all()) self.assertTrue(not (xmax == sim.crs['xmax']).all()) self.assertTrue((np.abs(xmax - sim.crs['xmax']) < 50).all()) vec_unc = coord.ang2vec(np.hstack(sim.crs['lon']), np.hstack(sim.crs['lat'])) self.assertTrue(not (coord.angle(vecs, vec_unc) == 0).all()) self.assertTrue((coord.angle(vecs, vec_unc) < np.deg2rad(10)).all())
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_05_inariant_rotation(self): ac = obs.two_pt_auto(self.vecs, bins=self.nbins, cumulative=True, normalized=True) vecs_rotated = coord.gal2eq(self.vecs) self.assertTrue(np.mean(coord.angle(self.vecs, vecs_rotated)) > 0.1) ac_rotated = obs.two_pt_auto(vecs_rotated, bins=self.nbins, cumulative=True, normalized=True) self.assertTrue(np.allclose(ac, ac_rotated))
def angle(nside, ipix, jpix, nest=False, each2each=False): """ Give the angular distance between two pixel arrays. :param nside: nside of the healpy pixelization :param ipix: healpy pixel i (either int or array like int) :param jpix: healpy pixel j (either int or array like int) :param nest: use the nesting scheme of healpy :param each2each: if true, calculates every combination of the two lists v1, v2 :return: angular distance in radians """ v1 = pix2vec(nside, ipix, nest) v2 = pix2vec(nside, jpix, nest) return coord.angle(v1, v2, each2each=each2each)
def test_04_skymap_mean_quantile(self): pix_center = hpt.vec2pix(self.nside, 1, 0, 0) ratio = [] for ang in np.arange(5, 35, 5): delta = np.radians(ang) kappa = 1. / delta**2 fisher_map = hpt.fisher_pdf(self.nside, 1, 0, 0, kappa) v, alpha = hpt.skymap_mean_quantile(fisher_map) ratio.append(alpha / delta) self.assertTrue(coord.angle(v, np.array([1, 0, 0]))[0] < 0.01) mask = hpt.angle(self.nside, pix_center, np.arange( self.npix)) < alpha self.assertTrue(np.abs(np.sum(fisher_map[mask]) - 0.68) < 0.1) # delta of fisher distribution increases linear with alpha (68 quantil) self.assertTrue(np.std(ratio) < 0.05)
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_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_02b_rand_exposure_vec_in_pix(self): pix = hpt.rand_pix_from_map(hpt.exposure_pdf(self.nside), n=self.stat) v1 = hpt.rand_vec_in_pix(self.nside, pix) v2 = hpt.rand_exposure_vec_in_pix(self.nside, pix) self.assertTrue( (coord.angle(v1, v2) < 2 * hpt.max_pixrad(self.nside)).all())
def test_03_angle(self): v1 = np.array([[0, 0, 0, 0], [0, 0, 0, 0], [1, 1, 1, -1]]) v2 = np.array([[0, 0, 0, 0], [0, 1, 1, 0], [1, 0, 1, 1]]) ang = np.array([0, np.pi / 2., np.pi / 4., np.pi]) angle = coord.angle(v1, v2) self.assertAlmostEqual(ang.all(), angle.all())