Esempio n. 1
0
def setup_roi_same_ncrs_in_bins(nside=62,
                                ncrs=4,
                                nbins=2,
                                alpha_max=0.25,
                                bin_type='area'):
    bins = np.arange(nbins + 1).astype(np.float)
    if bin_type == 'lin':
        alpha_bins = alpha_max * bins / nbins
    else:
        alpha_bins = 2 * np.arcsin(
            np.sqrt(bins / nbins) * np.sin(alpha_max / 2))

    npix = hpt.nside2npix(nside)
    roipix = 0
    angles_pix_to_roi = hpt.angle(nside, roipix, np.arange(npix))
    pixel = np.zeros(ncrs, dtype=int)

    for a in range(nbins):
        iso_map_bin = np.zeros(npix)
        mask_bin = (angles_pix_to_roi >= alpha_bins[a]) * (angles_pix_to_roi <
                                                           alpha_bins[a + 1])
        iso_map_bin[mask_bin] = 1
        iso_map_bin /= np.sum(iso_map_bin)
        ratio = int(ncrs / nbins)
        pixel[ratio * a:ratio * (a + 1)] = np.random.choice(np.arange(npix),
                                                            ratio,
                                                            p=iso_map_bin)

    return pixel
Esempio n. 2
0
 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])
Esempio n. 3
0
 def test_ring(self):
     for lgNside in range(1, 5):
         nside = 1 << lgNside
         numPix = hpt.nside2npix(nside)
         numRings = 4 * nside - 1  # Expected number of rings
         for nest in (True, False):
             pix = np.arange(numPix)
             ring = hpt.pix2ring(nside, pix, nest=nest)
             self.assertTrue(pix.shape == ring.shape)
             self.assertTrue(len(set(ring)) == numRings)
             if not nest:
                 first = ring[:numPix - 1]
                 second = ring[1:]
                 self.assertTrue(
                     np.logical_or(first == second,
                                   first == second - 1).all())
Esempio n. 4
0
 def test_boundaries(self):
     """Test whether the boundary shapes look sane"""
     for lgNside in range(1, 5):
         nside = 1 << lgNside
         for pix in range(hpt.nside2npix(nside)):
             for res in range(1, 50, 7):
                 num = 4 * res  # Expected number of points
                 for nest in (True, False):
                     points = hpt.boundaries(nside, pix, res, nest=nest)
                     self.assertTrue(points.shape == (3, num))
                     dist = np.linalg.norm(
                         points[:, :num - 1] -
                         points[:, 1:])  # distance between points
                     self.assertTrue((dist != 0).all())
                     dmin = np.min(dist)
                     dmax = np.max(dist)
                     self.assertTrue(dmax / dmin <= 2.0)
Esempio n. 5
0
def plot_heatmap(crs, opath=None, **kwargs):  # pragma: no cover
    """
    Function to plot a scatter skymap of the cosmic rays

    :param crs: CosmicRaysBase object
    :param opath: Output path for the image, default is None
    :type opath: str
    :param kwargs:

           - nside: Healpy resolution of the 'pixel' array in the cosmic ray class
           - additional named keywords passed to matplotlib.pcolormesh
    :return: figure of the heatmap, colorbar
    """
    nside = crs['nside'] if 'nside' in crs.keys() else kwargs.pop('nside', 64)
    pixel = crs['pixel']

    count = np.bincount(pixel, minlength=hpt.nside2npix(nside))
    return skymap.heatmap(count, opath=opath, **kwargs)
Esempio n. 6
0
    def test_07_pix2map(self):
        nside = 1
        npix = hpt.nside2npix(nside)

        # pixel as single integer
        ipix = 4
        expectation = np.zeros(npix)
        expectation[ipix] = 1
        self.assertTrue(np.allclose(hpt.pix2map(nside, ipix), expectation))

        # pixel as array
        ipix = np.array([0, 2, 5, 10, 5])
        expectation = np.array([1, 0, 1, 0, 0, 2, 0, 0, 0, 0, 1, 0])
        self.assertTrue(np.allclose(hpt.pix2map(nside, ipix), expectation))

        # check pixel outside nside
        with self.assertRaises(AssertionError):
            hpt.pix2map(nside, 12)
Esempio n. 7
0
    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))
Esempio n. 8
0
    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))
Esempio n. 9
0
def setup_roi(nside=256,
              ncrs=2000,
              roi_size=0.25,
              energy_spectrum='uniform',
              energy_ordering=False,
              emin=19):

    npix = hpt.nside2npix(nside)
    roipix = 0

    angles_pix_to_roi = hpt.angle(nside, roipix, np.arange(npix))
    iso_map = np.zeros(npix)
    iso_map[angles_pix_to_roi < roi_size] = 1
    p = np.cumsum(iso_map)
    pix = np.sort(p.searchsorted(np.random.rand(ncrs) * p[-1]))

    if energy_spectrum == 'auger':
        energies = auger.rand_energy_from_auger(ncrs, emin)
    elif energy_spectrum == 'uniform':
        energies = np.random.uniform(10, 20, ncrs)
    if energy_ordering:
        energies = np.sort(energies)[::-1]

    return pix, energies
Esempio n. 10
0
import unittest
import os
import numpy as np
from scipy import sparse

from astrotools import gamale, healpytools as hpt

path = os.path.dirname(os.path.realpath(__file__))
nside = 4  # nside resolution of the test toy-lens
npix = hpt.nside2npix(nside)
stat = 10  # statistic (per pixel) of the test toy-lens
lens_bins = np.linspace(17, 20.48, 175)
dlE = (lens_bins[1] - lens_bins[0]) / 2.

test_bins = [100, 150]


class TestLens(unittest.TestCase):
    def setUp(self):
        self.lens_path = path + '/toy-lens/jf12-regular.cfg'

    def test_01_load_and_dimensions(self):
        """ Test raw mldat matrices with simple load function"""
        old_mcs = None
        for bin_t in test_bins:
            lenspart_path = path + '/toy-lens/jf12-regular-%d.npz' % bin_t
            lp = gamale.load_lens_part(lenspart_path)
            # Sparse matrix that maps npix_extragalactic to npix_observed:
            self.assertTrue(lp.shape == (npix, npix))
            mrs = lp.sum(axis=1).max()
            mcs = lp.sum(axis=0).max()
Esempio n. 11
0
 def setUp(self):
     self.nside = 64
     self.npix = hpt.nside2npix(self.nside)
Esempio n. 12
0
 def test_nside2npix(self):
     self.assertEqual(hpt.nside2npix(512), 3145728)
     self.assertEqual(hpt.nside2npix(1024), 12582912)
Esempio n. 13
0
 def setUp(self):
     self.nside, self.stat, self.nbins = 64, 1000, 180
     self.npix = hpt.nside2npix(self.nside)
     self.vecs = hpt.rand_vec_in_pix(self.nside,
                                     np.random.choice(self.npix, self.stat))