def test_04_vec2pix(self): vec_up = -1 + 2 * np.random.rand(3 * self.stat).reshape((3, self.stat)) vec_low = -1 + 2 * np.random.rand(3 * self.stat).reshape( (3, self.stat)) vec_up[2, :] = 0.1 + np.random.rand(self.stat) vec_low[2, :] = -0.1 - np.random.rand(self.stat) pix_up = hpt.vec2pix(self.nside, *vec_up) pix_low = hpt.vec2pix(self.nside, *vec_low) up_range = (pix_up >= 0).sum() and (pix_up < int(self.npix / 2.)).sum() low_range = (pix_low < self.npix).sum() and (pix_low > int( self.npix / 2.)).sum() self.assertTrue(low_range and up_range) pix_up_2 = hpt.vec2pix(self.nside, vec_up) pix_low_2 = hpt.vec2pix(self.nside, vec_low) self.assertTrue(np.array_equal(pix_up, pix_up_2)) self.assertTrue(np.array_equal(pix_low, pix_low_2))
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_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_22_shuffle(self): sim = ObservedBound(self.nside, self.nsets, self.ncrs) src_vecs = np.array([1, 1, 1]) src_pix = hpt.vec2pix(sim.nside, src_vecs) sim.set_sources(src_vecs[:, np.newaxis]) sim.arrival_setup(fsig=0.1) crs = sim.get_data(convert_all=False, shuffle=True) self.assertTrue(np.all(src_pix == crs['pixel'][sim.signal_label])) self.assertTrue(np.all(src_pix == crs['pixel'][crs['signal_label'].astype(bool)]))
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_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 _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
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)