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_02_pix2vec(self): pix = np.random.randint(0, self.npix, self.stat) vec = hpt.pix2vec(self.nside, pix) self.assertAlmostEqual( np.sum(vec**2, axis=0).all(), np.ones(self.stat).all()) self.assertTrue(np.array_equal(pix, hpt.vec2pix(self.nside, *vec)))
def test_02a_rand_vec_in_pix(self): pix = np.random.randint(0, self.npix, self.stat) vecs = hpt.rand_vec_in_pix(self.nside, pix) pix_check = hpt.vec2pix(self.nside, *vecs) vecs_check = hpt.pix2vec(self.nside, pix) self.assertTrue((vecs != vecs_check).all() and (pix == pix_check).all())
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_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_fit_dipole(self): nside = 32 npix = hpt.nside2npix(nside) d = [0.3, 0.5, 0.2] vec = np.transpose(hpt.pix2vec(nside, np.arange(npix))) signal = np.dot(vec, d) mono, dipole = hpt.fit_dipole(signal) self.assertAlmostEqual(mono, 0.) self.assertAlmostEqual(d[0], dipole[0]) self.assertAlmostEqual(d[1], dipole[1]) self.assertAlmostEqual(d[2], dipole[2])
def test_02_four_crs_one_bin(self): nside = 64 ncrs = 4 nbins = 1 alpha_max = 0.25 vec_roi = hpt.pix2vec(nside, 0) pixel = setup_roi_same_ncrs_in_bins(nside=nside, ncrs=ncrs, nbins=nbins, alpha_max=alpha_max, bin_type='area') energies = np.array([7., 5., 3., 1.]) vecs = np.array(hpt.pix2vec(nside, pixel)) omega, bins, ncr_bin = obs.energy_energy_correlation(vecs, energies, vec_roi, nbins=nbins, e_ref='median') self.assertTrue(np.abs(omega + 0.16825397) < 1e-8)
def test_03_four_crs_two_bins(self): nside = 64 ncrs = 4 nbins = 2 alpha_max = 0.25 vec_roi = hpt.pix2vec(nside, 0) pixel = setup_roi_same_ncrs_in_bins(nside=nside, ncrs=ncrs, nbins=nbins, alpha_max=alpha_max, bin_type='area') energies = np.array([7., 5., 3., 1.]) vecs = np.array(hpt.pix2vec(nside, pixel)) omega, bins, ncr_bin = obs.energy_energy_correlation(vecs, energies, vec_roi, nbins=nbins, e_ref='median') self.assertTrue(np.abs(omega[0] + 0.0031746) < 1e-7) self.assertTrue(np.abs(omega[1] + 0.1047619) < 1e-7)
def test_04_refmode_roi(self): nside = 64 ncrs = 4 nbins = 2 alpha_max = 0.25 vec_roi = hpt.pix2vec(nside, 0) pixel = setup_roi_same_ncrs_in_bins(nside=nside, ncrs=ncrs, nbins=nbins, alpha_max=alpha_max, bin_type='area') energies = np.array([7., 5., 3., 1.]) vecs = np.array(hpt.pix2vec(nside, pixel)) omega, bins, ncr_bin = obs.energy_energy_correlation(vecs, energies, vec_roi, nbins=nbins, e_ref='median', ref_mode='roi') self.assertTrue(np.abs(omega[0] + 0.32063492) < 1e-8) self.assertTrue(np.abs(omega[1] + 0.01587302) < 1e-8)
def test_01_bin_type(self): nside, ncrs = 256, 2000 nbins = 5 vec_roi = hpt.pix2vec(nside, 0) pixel_0, energies_0 = setup_roi(nside, ncrs=ncrs) vecs_0 = np.array(hpt.pix2vec(nside, pixel_0)) omega, bins, ncr_bin = obs.energy_energy_correlation(vecs_0, energies_0, vec_roi, nbins=nbins, bin_type='area') close_to_one = nbins * ncr_bin / ncrs self.assertTrue(np.allclose(close_to_one, np.ones(nbins), rtol=0.2)) omega, bins, ncr_bin = obs.energy_energy_correlation(vecs_0, energies_0, vec_roi, nbins=nbins, bin_type='lin') constant = ncr_bin / np.arange(0.5, nbins, 1) close_to_one = constant / np.mean(constant) self.assertTrue(np.allclose(close_to_one, np.ones(nbins), rtol=0.4))
def test_02_fisher(self): kappa = 350. vmax = np.array([1, 1, 1]) fisher_map = hpt.fisher_pdf(self.nside, vmax, k=kappa) self.assertEqual(self.npix, fisher_map.size) self.assertEqual(np.sum(fisher_map), 1.) pix_max = hpt.vec2pix(self.nside, vmax) self.assertEqual(pix_max, np.argmax(fisher_map)) vecs = hpt.pix2vec(self.nside, np.arange(self.npix)) vecs_mean = np.sum(vecs * fisher_map[None, :], axis=1) self.assertEqual(hpt.vec2pix(self.nside, vecs_mean), pix_max) pixels, weights = hpt.fisher_pdf(self.nside, vmax, k=kappa, sparse=True) self.assertEqual(len(pixels), len(weights)) self.assertEqual(pixels[np.argmax(weights)], pix_max)
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 _direction_transformation(self, similar_key, orig_key): """ Helper function to get values stored under a different physical key in the correctly transformed way specifically only for directions """ nside = self.general_object_store['nside'] if 'nside' in self.keys() else 64 store = self.shape_array if similar_key in list(self.shape_array.dtype.names) else self.general_object_store if orig_key == 'vecs': if ('lon' in similar_key) or ('lat' in similar_key): return hpt.ang2vec(store['lon'], store['lat']) return hpt.pix2vec(nside, store[similar_key]) if ('pix' in orig_key): if 'pix' in similar_key: return store[similar_key] if similar_key == 'vecs': return hpt.vec2pix(nside, store['vecs']) return hpt.ang2pix(nside, store['lon'], store['lat']) if similar_key == 'vecs': lon, lat = hpt.vec2ang(store['vecs']) else: lon, lat = hpt.pix2ang(nside, store[similar_key]) return lon if orig_key == 'lon' else lat