def test_04b_energy_at_borders(self): """ Test energy borders of load function""" lens = gamale.Lens(self.lens_path) for i, log10e in enumerate([19.99, 20., 20.0099999]): lp = lens.get_lens_part(log10e) lens.check_lens_part(lp) for log10e in [19.98999999, 20.01, 20.01000001]: with self.assertRaises(ValueError): lens.get_lens_part(log10e)
def test_04a_energy_in_borders(self): """ Test energy borders of load function""" lens = gamale.Lens(self.lens_path) for i, bini in enumerate(lens_bins): try: lp = lens.get_lens_part(bini + np.random.uniform(-dlE, dlE)) lens.check_lens_part(lp) except ValueError: if i in test_bins: raise Exception("Bin %d was unable to load." % i) pass
def test_03_lens_class_load(self): """ Test lens class with load function""" lens = gamale.Lens(self.lens_path) for i, bin_t in enumerate(test_bins): lp = lens.get_lens_part(lens_bins[bin_t], cache=False) self.assertTrue(lens.check_lens_part(lp)) self.assertTrue( not isinstance(lens.lens_parts[i], sparse.csc.csc_matrix)) lp = lens.get_lens_part(lens_bins[bin_t], cache=True) self.assertTrue(lens.check_lens_part(lp)) self.assertTrue( isinstance(lens.lens_parts[i], sparse.csc.csc_matrix))
def test_04c_force_closest(self): """ Test taking closest available bin """ lens = gamale.Lens(self.lens_path) test_rigs = [18.96, 19., 19.499999, 19.50001, 19.8, 20.04] _take = [0, 0, 0, 1, 1, 1] _not_take = [1, 1, 1, 0, 0, 0] for i, log10e in enumerate(test_rigs): lp = lens.get_lens_part(log10e, force=True) lens.check_lens_part(lp) self.assertTrue( isinstance(lens.lens_parts[_take[i]], sparse.csc_matrix)) self.assertTrue(not isinstance(lens.lens_parts[_not_take[i]], sparse.csc_matrix)) lens.lens_parts = lens.lens_paths[:]
def test_02_lens_class_init(self): """ Test lens class with load function""" lens = gamale.Lens(self.lens_path) self.assertTrue(lens.nside == nside) self.assertTrue(lens.stat == stat) for i, bin_t in enumerate(test_bins): self.assertTrue(os.path.isfile(lens.lens_parts[i])) lb = lens_bins[bin_t] self.assertAlmostEqual(lens.log10r_mins[i], lb - dlE, places=3) self.assertAlmostEqual(lens.log10r_max[i], lb + dlE, places=3) vec_in = np.random.random(npix) nlp = lens.neutral_lens_part.dot(vec_in) self.assertTrue(np.array_equal(nlp, vec_in)) self.assertTrue(len(lens.max_column_sum) == len(test_bins))
def test_14_lensing_map(self): lens_path = os.path.dirname(os.path.realpath(__file__)) + '/toy-lens/jf12-regular.cfg' toy_lens = gamale.Lens(lens_path) nside = toy_lens.nside sim = ObservedBound(nside, self.nsets, self.ncrs) sim.set_energy(19.*np.ones(self.shape)) sim.set_charges(1) sim.set_xmax('empiric') sim.set_sources('gamma_agn') sim.lensing_map(toy_lens) # Xmax for higher energy is bigger self.assertTrue(sim.lensed) self.assertTrue(np.shape(sim.cr_map) == (1, sim.npix)) self.assertAlmostEqual(np.sum(sim.cr_map), 1.) self.assertTrue(np.min(sim.cr_map) < np.max(sim.cr_map))
from astrotools import coord, gamale, healpytools as hpt, skymap print("Test: module gamale.py") # The gamale module is a tool for handling galactic magnetic field lenses. The lenses can be created with # the lens-factory: https://git.rwth-aachen.de/astro/lens-factory # Lenses provide information of the deflection of cosmic rays, consisting of matrices mapping an cosmic # ray's extragalactic origin to the observed direction on Earth (matrices of shape Npix x Npix). # Individual matrices ('lens parts') represent the deflection of particles in a specific rigidity range. # One lens consists of multiple .npz-files (the lens parts) and a .cfg-file including information about # the simulation and the rigidity range of the lens parts. # If you have a galactic field lens on your computer, you can execute the following code: lens_path = '/path/to/config/file.cfg' if os.path.exists(lens_path): # Loading a lens lens = gamale.Lens(lens_path) # Loading the lens part corresponding to a particle of energy log10e and charge z log10e = 19 # Make sure that the rigidity is covered in your lens z = 1 lens_part = lens.get_lens_part(log10e=log10e, z=z) # Alternatively, a lens part can be loaded directly lens_part_path = '/path/to/lens/part.npz' lens_part = gamale.load_lens_part(lens_part_path) nside = gamale.mat2nside(lens_part) # calculating nside from lens part npix = hpt.nside2npix(nside) # Compute the observed directions of cosmic rays that arrives from direction of the # extragalactic pixel eg_pix after backpropagation from earth. The amount of