Example #1
0
    def test_degrade_map_int_wmean(self):
        """
        Test HealSparse.degrade wmean with integers
        """
        nside_coverage = 32
        nside_map = 1024
        nside_new = 512
        full_map = np.full(hp.nside2npix(nside_map), 1, dtype=np.int64)
        # Generate sparse map

        sparse_map = healsparse.HealSparseMap(healpix_map=full_map,
                                              nside_coverage=nside_coverage,
                                              nside_sparse=nside_map,
                                              sentinel=0)
        weights = healsparse.HealSparseMap(healpix_map=np.ones(len(full_map)),
                                           nside_coverage=nside_coverage,
                                           nside_sparse=nside_map)
        # Degrade original HEALPix map

        deg_map = np.full(hp.nside2npix(nside_new), 1, dtype=np.int64)
        # Degrade sparse map and compare to original

        new_map = sparse_map.degrade(nside_out=nside_new,
                                     reduction='wmean',
                                     weights=weights)
        # Test the coverage map generation and lookup
        testing.assert_almost_equal(deg_map, new_map.generate_healpix_map())
Example #2
0
    def test_coverage_map_float(self):
        """
        Test coverage_map functionality for floats
        """

        nside_coverage = 16
        nside_map = 512
        # Number of non-masked pixels in the coverage map resolution
        non_masked_px = 10.5
        nfine = (nside_map//nside_coverage)**2
        full_map = np.zeros(hp.nside2npix(nside_map)) + hp.UNSEEN
        full_map[0: int(non_masked_px*nfine)] = 1 + np.random.random(size=int(non_masked_px*nfine))

        # Generate sparse map

        sparse_map = healsparse.HealSparseMap(healpix_map=full_map, nside_coverage=nside_coverage)

        # Build the "original" coverage map
        cov_map_orig = self.compute_cov_map(nside_coverage, non_masked_px, nfine,
                                            sparse_map._cov_map.bit_shift)

        # Get the built coverage map

        cov_map = sparse_map.coverage_map

        # Test the coverage map generation and lookup

        testing.assert_array_almost_equal(cov_map_orig, cov_map)
Example #3
0
    def test_coverage_map_int(self):
        """
        Test coverage_map functionality for ints
        """
        nside_coverage = 16
        nside_map = 512
        # Number of non-masked pixels in the coverage map resolution
        non_masked_px = 10.5
        nfine = (nside_map // nside_coverage)**2
        sentinel = healsparse.utils.check_sentinel(np.int32, None)
        full_map = np.zeros(hpg.nside_to_npixel(nside_map),
                            dtype=np.int32) + sentinel
        full_map[0:int(non_masked_px * nfine)] = 1

        sparse_map = healsparse.HealSparseMap(healpix_map=full_map,
                                              nside_coverage=nside_coverage,
                                              sentinel=sentinel)

        cov_map_orig = self.compute_cov_map(nside_coverage, non_masked_px,
                                            nfine,
                                            sparse_map._cov_map.bit_shift)

        cov_map = sparse_map.coverage_map

        testing.assert_array_almost_equal(cov_map_orig, cov_map)
Example #4
0
    def test_setitem_array(self):
        """
        Test __setitem__ array
        """
        random.seed(12345)

        nside_coverage = 32
        nside_map = 128

        full_map = np.zeros(hpg.nside_to_npixel(nside_map)) + hpg.UNSEEN
        full_map[0:5000] = random.random(size=5000)

        sparse_map = healsparse.HealSparseMap(healpix_map=full_map,
                                              nside_coverage=nside_coverage)

        indices = np.array([1, 2, 100, 500, 10000])
        sparse_map[indices] = np.array([1.0])
        full_map[indices] = np.array([1.0])
        testing.assert_array_almost_equal(sparse_map.generate_healpix_map(),
                                          full_map)

        # Simple in-place operation
        sparse_map[indices] += 1.0
        full_map[indices] += 1.0
        testing.assert_array_almost_equal(sparse_map.generate_healpix_map(),
                                          full_map)

        indices = np.array([1, 2, 100, 500, 10000]) + 100
        sparse_map[indices] = np.ones(len(indices))
        full_map[indices] = np.ones(len(indices))
        testing.assert_array_almost_equal(sparse_map.generate_healpix_map(),
                                          full_map)

        indices = np.array([1., 2, 100, 500, 10000])
        self.assertRaises(IndexError, sparse_map.__setitem__, indices, 1.0)
Example #5
0
    def test_fracdet_map_float(self):
        """
        Test fracdet_map functionality for floats
        """
        nside_coverage = 16
        nside_fracdet = 32
        nside_map = 512
        non_masked_px = 10.5
        nfine = (nside_map // nside_coverage)**2
        full_map = np.zeros(hp.nside2npix(nside_map)) + hp.UNSEEN
        full_map[0:int(non_masked_px * nfine)] = 1 + np.random.random(
            size=int(non_masked_px * nfine))

        sparse_map = healsparse.HealSparseMap(healpix_map=full_map,
                                              nside_coverage=nside_coverage)

        # Test that the fracdet map is equal to the coverage map with same nside_coverage
        fracdet_map1 = sparse_map.fracdet_map(nside_coverage)

        np.testing.assert_array_almost_equal(fracdet_map1[:],
                                             sparse_map.coverage_map)

        # Test that the fracdet map is good for target nside
        fracdet_map2 = sparse_map.fracdet_map(nside_fracdet)

        fracdet_map_orig = self.compute_fracdet_map(nside_map, nside_fracdet,
                                                    non_masked_px, nfine)

        np.testing.assert_array_almost_equal(fracdet_map2[:], fracdet_map_orig)
Example #6
0
    def test_fracdet_map_int(self):
        """
        Test fracdet_map functionality for ints
        """
        nside_coverage = 16
        nside_fracdet = 32
        nside_map = 512
        non_masked_px = 10.5
        nfine = (nside_map // nside_coverage)**2
        sentinel = healsparse.utils.check_sentinel(np.int32, None)
        full_map = np.zeros(hp.nside2npix(nside_map),
                            dtype=np.int32) + sentinel
        full_map[0:int(non_masked_px * nfine)] = 1

        sparse_map = healsparse.HealSparseMap(healpix_map=full_map,
                                              nside_coverage=nside_coverage,
                                              sentinel=sentinel)

        # Test that the fracdet map is equal to the coverage map with same nside_coverage
        fracdet_map1 = sparse_map.fracdet_map(nside_coverage)

        np.testing.assert_array_almost_equal(fracdet_map1[:],
                                             sparse_map.coverage_map)

        # Test that the fracdet map is good for target nside
        fracdet_map2 = sparse_map.fracdet_map(nside_fracdet)

        fracdet_map_orig = self.compute_fracdet_map(nside_map, nside_fracdet,
                                                    non_masked_px, nfine)

        np.testing.assert_array_almost_equal(fracdet_map2[:], fracdet_map_orig)
Example #7
0
    def test_coverage_mask(self):
        """
        Test coverageMask functionality
        """
        nside_coverage = 8
        nside_map = 64
        # Number of non-masked pixels in the coverage map resolution
        non_masked_px = 10
        nfine = (nside_map//nside_coverage)**2
        full_map = np.zeros(hp.nside2npix(nside_map)) + hp.UNSEEN
        full_map[0: non_masked_px*nfine] = 1 + np.random.random(size=non_masked_px*nfine)

        # Generate sparse map

        sparse_map = healsparse.HealSparseMap(healpix_map=full_map, nside_coverage=nside_coverage)

        # Build the "original" coverage mask

        cov_mask_orig = np.zeros(hp.nside2npix(nside_coverage), dtype=np.bool_)
        idx_cov = np.unique(np.right_shift(np.arange(0, non_masked_px*nfine), sparse_map._cov_map.bit_shift))
        cov_mask_orig[idx_cov] = 1

        # Get the built coverage mask

        cov_mask = sparse_map.coverage_mask

        # Test the mask generation and lookup

        testing.assert_equal(cov_mask_orig, cov_mask)
Example #8
0
    def test_getitem_slice(self):
        """
        Test __getitem__ using slices
        """
        random.seed(12345)

        nside_coverage = 32
        nside_map = 128

        full_map = np.zeros(hp.nside2npix(nside_map)) + hp.UNSEEN
        full_map[0:5000] = random.random(size=5000)

        sparse_map = healsparse.HealSparseMap(healpix_map=full_map,
                                              nside_coverage=nside_coverage)

        # Test in-range, overlap, out-of-range
        testing.assert_array_almost_equal(sparse_map[100:500],
                                          full_map[100:500])
        testing.assert_array_almost_equal(sparse_map[4500:5500],
                                          full_map[4500:5500])
        testing.assert_array_almost_equal(sparse_map[5500:5600],
                                          full_map[5500:5600])

        # Test stepped
        testing.assert_array_almost_equal(sparse_map[100:500:2],
                                          full_map[100:500:2])
        testing.assert_array_almost_equal(sparse_map[4500:5500:2],
                                          full_map[4500:5500:2])
        testing.assert_array_almost_equal(sparse_map[5500:5600:2],
                                          full_map[5500:5600:2])

        # Test all
        testing.assert_array_almost_equal(sparse_map[:], full_map[:])
Example #9
0
    def test_setitem_list(self):
        """
        Test __setitem__ list
        """
        random.seed(12345)

        nside_coverage = 32
        nside_map = 128

        full_map = np.zeros(hp.nside2npix(nside_map)) + hp.UNSEEN
        full_map[0:5000] = random.random(size=5000)

        sparse_map = healsparse.HealSparseMap(healpix_map=full_map,
                                              nside_coverage=nside_coverage)

        indices = [1, 2, 100, 500, 10000]
        sparse_map[indices] = np.array([1.0])
        full_map[indices] = np.array([1.0])
        testing.assert_array_almost_equal(sparse_map.generate_healpix_map(),
                                          full_map)

        indices = [101, 102, 200, 600, 10100]
        sparse_map[indices] = np.ones(len(indices))
        full_map[indices] = np.ones(len(indices))
        testing.assert_array_almost_equal(sparse_map.generate_healpix_map(),
                                          full_map)

        indices = [1., 2, 100, 500, 10000]
        self.assertRaises(IndexError, sparse_map.__setitem__, indices, 1.0)
Example #10
0
    def test_generate_healpix_map_ring(self):
        """
        Test the generation of a healpixmap in ring type
        """
        random.seed(seed=12345)

        nside_coverage = 32
        nside_map = 64

        n_rand = 1000
        ra = np.random.random(n_rand) * 360.0
        dec = np.random.random(n_rand) * 180.0 - 90.0
        value = np.random.random(n_rand)

        # Create a HEALPix map
        healpix_map = np.zeros(hp.nside2npix(nside_map),
                               dtype=np.float64) + hp.UNSEEN
        idx = hp.ang2pix(nside_map,
                         np.pi / 2 - np.radians(dec),
                         np.radians(ra),
                         nest=True)
        healpix_map[idx] = value
        # Create a HealSparseMap
        sparse_map = healsparse.HealSparseMap(nside_coverage=nside_coverage,
                                              healpix_map=healpix_map)
        hp_out_ring = sparse_map.generate_healpix_map(nside=nside_map,
                                                      nest=False)
        healpix_map_ring = hp.reorder(healpix_map, n2r=True)
        testing.assert_almost_equal(healpix_map_ring, hp_out_ring)
Example #11
0
    def test_parquet_writeread_withheader(self):
        """
        Test parquet i/o functionality with a header
        """
        random.seed(seed=12345)

        nside_coverage = 32
        nside_map = 64

        self.test_dir = tempfile.mkdtemp(dir='./', prefix='TestHealSparse-')
        fname = os.path.join(self.test_dir, 'sparsemap_with_header.hs')

        full_map = np.zeros(hp.nside2npix(nside_map)) + hp.UNSEEN
        full_map[0: 20000] = np.random.random(size=20000)

        sparse_map = healsparse.HealSparseMap(healpix_map=full_map,
                                              nside_coverage=nside_coverage, nest=True)

        hdr = {}
        hdr['TESTING'] = 1.0

        sparse_map.metadata = hdr
        sparse_map.write(fname, format='parquet')

        ret_map, ret_hdr = healsparse.HealSparseMap.read(fname, header=True)

        self.assertEqual(hdr['TESTING'], ret_hdr['TESTING'])
Example #12
0
    def test_generate_healpix_map_single(self):
        """
        Test the generation of a healpix map from a sparse map for a single-value field
        """
        random.seed(seed=12345)

        nside_coverage = 32
        nside_map = 64

        n_rand = 1000
        ra = np.random.random(n_rand) * 360.0
        dec = np.random.random(n_rand) * 180.0 - 90.0
        value = np.random.random(n_rand)

        # Create a HEALPix map
        healpix_map = np.zeros(hpg.nside_to_npixel(nside_map), dtype=np.float64) + hpg.UNSEEN
        idx = hpg.angle_to_pixel(nside_map, ra, dec)
        healpix_map[idx] = value
        # Create a HealSparseMap
        sparse_map = healsparse.HealSparseMap(nside_coverage=nside_coverage, healpix_map=healpix_map)
        hp_out = sparse_map.generate_healpix_map(nside=nside_map)
        testing.assert_almost_equal(healpix_map, hp_out)

        if not has_healpy:
            return

        # Now check that it works specifying a different resolution
        nside_map2 = 32
        hp_out = sparse_map.generate_healpix_map(nside=nside_map2)
        # Let's compare with the original downgraded
        healpix_map = hp.ud_grade(healpix_map, nside_out=nside_map2, order_in='NESTED', order_out='NESTED')
        testing.assert_almost_equal(healpix_map, hp_out)
Example #13
0
    def test_degrade_map_wmean(self):
        """
        Test HealSparse.degrade wmean functionality with float quantities
        """
        nside_coverage = 32
        nside_map = 1024
        nside_new = 256
        full_map = np.ones(hp.nside2npix(nside_map))
        weights = np.ones_like(full_map)
        full_map[::32] = 0.5  # We lower the value in 1 pixel every 32
        weights[::32] = 0.5  # We downweight 1 pixel every 32
        deg_map = np.ones(hp.nside2npix(nside_new))
        deg_map[::2] = 15.25 / 15.5
        # Generate sparse map

        sparse_map = healsparse.HealSparseMap(healpix_map=full_map,
                                              nside_coverage=nside_coverage,
                                              nside_sparse=nside_map)
        weights = healsparse.HealSparseMap(healpix_map=weights,
                                           nside_coverage=nside_coverage,
                                           nside_sparse=nside_map)

        # Degrade sparse map and compare to original
        new_map = sparse_map.degrade(nside_out=nside_new,
                                     reduction='wmean',
                                     weights=weights)

        # Test the coverage map generation and lookup
        testing.assert_almost_equal(new_map.generate_healpix_map(), deg_map)

        # Test degrade-on-read with weights
        self.test_dir = tempfile.mkdtemp(dir='./', prefix='TestHealSparse-')

        fname = os.path.join(self.test_dir, 'test_float_degrade.hs')
        sparse_map.write(fname)
        fname_weight = os.path.join(self.test_dir,
                                    'test_float_degrade_weights.hs')
        weights.write(fname_weight)

        new_map2 = healsparse.HealSparseMap.read(fname,
                                                 degrade_nside=nside_new,
                                                 reduction='wmean',
                                                 weightfile=fname_weight)
        testing.assert_almost_equal(new_map2.generate_healpix_map(), deg_map)
Example #14
0
    def hspmap(self,
               hspmap,
               pixel=None,
               nside=None,
               xsize=800,
               lonra=None,
               latra=None,
               badval=healpix.UNSEEN,
               smooth=None,
               **kwargs):
        """ Draw a healpix map with pcolormesh.

        Parameters
        ----------
        hpxmap: input healpix or HealSparse map
        pixel:  explicit pixel indices in RING scheme (required for partial healpix maps)
        nside:  explicit nside of the map (required for partial healpix maps) if
                passed while visualizing a HealSparse map it will doegrade the map to this nside.
        xsize:  resolution of the output image
        lonra:  longitude range [-180,180] (deg)
        latra:  latitude range [-90,90] (deg)
        badval: set of values considered "bad"
        smooth: gaussian smoothing kernel (deg)
        kwargs: passed to pcolormesh

        Returns
        -------
        im,lon,lat,values : mpl image with pixel longitude, latitude (deg), and values
        """
        # ADW: probably still not the best way to do this...
        import healsparse as hsp
        import healpy as hp

        if not isinstance(hspmap, hsp.HealSparseMap):
            # Assume that it is a healpix map in RING ordering
            hpxmap = hp.reorder(hspmap, r2n=True)
            hspmap = hsp.HealSparseMap(healpix_map=hpxmap,
                                       nside_coverage=nside)

        lon, lat, values = healpix.hsp2xy(hspmap,
                                          xsize=xsize,
                                          lonra=lonra,
                                          latra=latra)

        if smooth:
            msg = "Healsparse smoothing not implemented."
            raise Exception(msg)

        vmin, vmax = np.percentile(values.compressed(), [2.5, 97.5])

        defaults = dict(rasterized=True, vmin=vmin, vmax=vmax)
        setdefaults(kwargs, defaults)

        im = self.pcolormesh(lon, lat, values, **kwargs)
        self._sci(im)
        return im, lon, lat, values
Example #15
0
    def test_healpix_explicit_int_write(self):
        """Test writing healpix partial (explicit) maps (integer)."""
        random.seed(seed=12345)

        nside_coverage = 32
        nside_map = 64

        n_rand = 1000
        ra = np.random.random(n_rand) * 360.0
        dec = np.random.random(n_rand) * 180.0 - 90.0

        self.test_dir = tempfile.mkdtemp(dir='./', prefix='TestHealSparse-')

        # Generate a map
        full_map = np.zeros(hpg.nside_to_npixel(nside_map), dtype=np.int32)
        full_map[0:10000] = 4
        full_map[20000:30000] = 5

        ipnest = hpg.angle_to_pixel(nside_map, ra, dec)
        test_values = full_map[ipnest]

        filename = os.path.join(self.test_dir,
                                'healsparse_healpix_int_partial_map.fits')

        sparse_map = healsparse.HealSparseMap(healpix_map=full_map,
                                              nside_coverage=nside_coverage,
                                              nest=True,
                                              sentinel=0)
        with self.assertWarns(UserWarning):
            sparse_map.write(filename, format='healpix')

        # Read in with healsparse and make sure it is the same.
        sparse_map2 = healsparse.HealSparseMap.read(
            filename, nside_coverage=nside_coverage)

        np.testing.assert_array_equal(sparse_map2.valid_pixels,
                                      sparse_map.valid_pixels)
        testing.assert_almost_equal(sparse_map2.get_values_pix(ipnest),
                                    test_values)

        # Read in with healpy and make sure it is the same.
        full_map2 = hp.read_map(filename, nest=True)
        testing.assert_array_equal((full_map2 > hpg.UNSEEN).nonzero()[0],
                                   sparse_map.valid_pixels)

        # healpy will convert all the BAD_DATA to UNSEEN
        good, = (test_values > 0).nonzero()
        bad, = (test_values == 0).nonzero()
        testing.assert_array_equal(full_map2[ipnest[good]], test_values[good])
        testing.assert_array_almost_equal(full_map2[ipnest[bad]], hpg.UNSEEN)
Example #16
0
    def test_fracdet_map_raises(self):
        """
        Test limitations of fracdet_map
        """
        nside_coverage = 16
        nside_map = 512
        non_masked_px = 10.5
        nfine = (nside_map // nside_coverage)**2
        full_map = np.zeros(hp.nside2npix(nside_map)) + hp.UNSEEN
        full_map[0:int(non_masked_px * nfine)] = 1 + np.random.random(
            size=int(non_masked_px * nfine))

        sparse_map = healsparse.HealSparseMap(healpix_map=full_map,
                                              nside_coverage=nside_coverage)

        for nside_fracdet in [8, 1024]:
            self.assertRaises(ValueError, sparse_map.fracdet_map,
                              nside_fracdet)
Example #17
0
    def test_healpix_explicit_write(self):
        """Test writing healpix partial (explicit) maps (floating point)."""
        random.seed(seed=12345)

        nside_coverage = 32
        nside_map = 64

        n_rand = 1000
        ra = np.random.random(n_rand) * 360.0
        dec = np.random.random(n_rand) * 180.0 - 90.0

        self.test_dir = tempfile.mkdtemp(dir='./', prefix='TestHealSparse-')

        # Generate a random map
        full_map = np.zeros(hpg.nside_to_npixel(nside_map)) + hpg.UNSEEN
        full_map[0:20000] = np.random.random(size=20000)

        ipnest = hpg.angle_to_pixel(nside_map, ra, dec)

        test_values = full_map[ipnest]

        filename = os.path.join(self.test_dir,
                                'healsparse_healpix_partial_map.fits')

        sparse_map = healsparse.HealSparseMap(healpix_map=full_map,
                                              nside_coverage=nside_coverage,
                                              nest=True)
        sparse_map.write(filename, format='healpix')

        # Read in with healsparse and make sure it is the same.
        sparse_map2 = healsparse.HealSparseMap.read(
            filename, nside_coverage=nside_coverage)

        np.testing.assert_array_equal(sparse_map2.valid_pixels,
                                      sparse_map.valid_pixels)
        testing.assert_array_almost_equal(sparse_map2.get_values_pix(ipnest),
                                          test_values)

        # Read in with healpy and make sure it is the same.
        full_map2 = hp.read_map(filename, nest=True)
        testing.assert_array_equal((full_map2 > hpg.UNSEEN).nonzero()[0],
                                   sparse_map.valid_pixels)
        testing.assert_array_almost_equal(full_map2[ipnest], test_values)
Example #18
0
    def test_getitem_single(self):
        """
        Test __getitem__ single value
        """
        random.seed(12345)

        nside_coverage = 32
        nside_map = 128

        full_map = np.zeros(hp.nside2npix(nside_map)) + hp.UNSEEN
        full_map[0:5000] = random.random(size=5000)

        sparse_map = healsparse.HealSparseMap(healpix_map=full_map,
                                              nside_coverage=nside_coverage)

        # Grab a single item, in range
        testing.assert_almost_equal(sparse_map[100], full_map[100])

        # Grab a single item out of range
        testing.assert_almost_equal(sparse_map[6000], full_map[6000])
Example #19
0
    def test_setitem_other(self):
        """
        Test __setitem__ using something else
        """
        random.seed(12345)

        nside_coverage = 32
        nside_map = 128

        full_map = np.zeros(hp.nside2npix(nside_map)) + hp.UNSEEN
        full_map[0:5000] = random.random(size=5000)

        sparse_map = healsparse.HealSparseMap(healpix_map=full_map,
                                              nside_coverage=nside_coverage)

        indices = (1, 2, 3, 4)
        self.assertRaises(IndexError, sparse_map.__setitem__, indices, 1.0)

        indices = 5.0
        self.assertRaises(IndexError, sparse_map.__setitem__, indices, 1.0)
Example #20
0
    def test_degrade_map_float(self):
        """
        Test HealSparse.degrade functionality with float quantities
        """
        random.seed(12345)
        nside_coverage = 32
        nside_map = 1024
        nside_new = 256
        full_map = random.random(hp.nside2npix(nside_map))

        # Generate sparse map

        sparse_map = healsparse.HealSparseMap(healpix_map=full_map,
                                              nside_coverage=nside_coverage,
                                              nside_sparse=nside_map)

        # Degrade original HEALPix map

        deg_map = hp.ud_grade(full_map,
                              nside_out=nside_new,
                              order_in='NESTED',
                              order_out='NESTED')

        # Degrade sparse map and compare to original

        new_map = sparse_map.degrade(nside_out=nside_new)

        # Test the coverage map generation and lookup

        testing.assert_almost_equal(deg_map, new_map.generate_healpix_map())

        # Test degrade-on-read
        self.test_dir = tempfile.mkdtemp(dir='./', prefix='TestHealSparse-')

        fname = os.path.join(self.test_dir, 'test_float_degrade.hs')
        sparse_map.write(fname)

        new_map2 = healsparse.HealSparseMap.read(fname,
                                                 degrade_nside=nside_new)

        testing.assert_almost_equal(deg_map, new_map2.generate_healpix_map())
Example #21
0
    def test_getitem_list(self):
        """
        Test __getitem__ using list/tuple
        """
        random.seed(12345)

        nside_coverage = 32
        nside_map = 128

        full_map = np.zeros(hp.nside2npix(nside_map)) + hp.UNSEEN
        full_map[0:5000] = random.random(size=5000)

        sparse_map = healsparse.HealSparseMap(healpix_map=full_map,
                                              nside_coverage=nside_coverage)

        indices = [1, 2, 100, 500, 10000]
        testing.assert_array_almost_equal(sparse_map[indices],
                                          full_map[indices])

        indices = [1.0, 2, 100, 500, 10000]
        self.assertRaises(IndexError, sparse_map.__getitem__, indices)
Example #22
0
    def test_from_healpix_float(self):
        """
        Test converting healpix map (float type)
        """
        np.random.seed(12345)

        nside_coverage = 32
        nside_map = 128

        full_map = np.zeros(hpg.nside_to_npixel(nside_map)) + hpg.UNSEEN
        full_map[0:5000] = np.random.random(size=5000)

        sparse_map = healsparse.HealSparseMap(healpix_map=full_map,
                                              nside_coverage=nside_coverage)
        testing.assert_almost_equal(
            full_map, sparse_map.get_values_pix(np.arange(full_map.size)))

        self.assertRaises(ValueError,
                          healsparse.HealSparseMap,
                          healpix_map=full_map,
                          nside_coverage=nside_coverage,
                          sentinel=int(hpg.UNSEEN))
Example #23
0
    def test_degrade_map_int_sum(self):
        """
        Test HealSparse.degrade sum with integers
        """
        nside_coverage = 32
        nside_map = 1024
        nside_new = 512
        full_map = np.full(hpg.nside_to_npixel(nside_map), 1, dtype=np.int64)
        # Generate sparse map

        sparse_map = healsparse.HealSparseMap(healpix_map=full_map, nside_coverage=nside_coverage,
                                              nside_sparse=nside_map, sentinel=0)

        # Degrade original HEALPix map

        deg_map = np.full(hpg.nside_to_npixel(nside_new), 4, dtype=np.int64)
        # Degrade sparse map and compare to original

        new_map = sparse_map.degrade(nside_out=nside_new, reduction='sum')
        # Test the coverage map generation and lookup

        testing.assert_almost_equal(deg_map, new_map.generate_healpix_map())
Example #24
0
    def test_from_healpix_int(self):
        """
        Test converting healpix map (int type)
        """
        np.random.seed(12345)

        nside_coverage = 32
        nside_map = 128

        full_map = np.zeros(hpg.nside_to_npixel(nside_map),
                            dtype=np.int32) + np.iinfo(np.int32).min
        full_map[0:5000] = np.random.poisson(size=5000)

        sparse_map = healsparse.HealSparseMap(healpix_map=full_map,
                                              nside_coverage=nside_coverage,
                                              sentinel=np.iinfo(np.int32).min)
        testing.assert_array_equal(
            full_map, sparse_map.get_values_pix(np.arange(full_map.size)))

        self.assertRaises(ValueError,
                          healsparse.HealSparseMap,
                          healpix_map=full_map,
                          nside_coverage=nside_coverage)
Example #25
0
    def test_upgrade_map(self):
        """
        Test upgrade functionality with regular map.
        """
        random.seed(12345)
        nside_coverage = 32
        nside_map = 256
        nside_new = 1024
        full_map = random.random(hpg.nside_to_npixel(nside_map))

        # Generate sparse map
        sparse_map = healsparse.HealSparseMap(healpix_map=full_map,
                                              nside_coverage=nside_coverage,
                                              nside_sparse=nside_map)
        # Upgrade original map
        upg_map = hp.ud_grade(full_map,
                              nside_out=nside_new,
                              order_in='NESTED',
                              order_out='NESTED')
        # Upgrade sparse map and compare
        new_map = sparse_map.upgrade(nside_out=nside_new)

        testing.assert_almost_equal(upg_map, new_map.generate_healpix_map())
Example #26
0
    def test_degrade_map_float_prod(self):
        """
        Test HealSparse.degrade product with floats
        """
        nside_coverage = 32
        nside_map = 1024
        nside_new = 512
        full_map = np.full(hpg.nside_to_npixel(nside_map), 2.)
        # Generate sparse map

        sparse_map = healsparse.HealSparseMap(healpix_map=full_map, nside_coverage=nside_coverage,
                                              nside_sparse=nside_map)

        # Degrade original HEALPix map

        deg_map = np.full(hpg.nside_to_npixel(nside_new), 2.**4)
        # Degrade sparse map and compare to original

        new_map = sparse_map.degrade(nside_out=nside_new, reduction='prod')

        # Test the coverage map generation and lookup

        testing.assert_almost_equal(deg_map, new_map.generate_healpix_map())
Example #27
0
    def test_setitem_single(self):
        """
        Test __setitem__ single value
        """
        random.seed(12345)

        nside_coverage = 32
        nside_map = 128

        full_map = np.zeros(hp.nside2npix(nside_map)) + hp.UNSEEN
        full_map[0:5000] = random.random(size=5000)

        sparse_map = healsparse.HealSparseMap(healpix_map=full_map,
                                              nside_coverage=nside_coverage)

        sparse_map[1000] = 1.0
        full_map[1000] = 1.0
        testing.assert_array_almost_equal(sparse_map.generate_healpix_map(),
                                          full_map)

        sparse_map[10000] = 1.0
        full_map[10000] = 1.0
        testing.assert_array_almost_equal(sparse_map.generate_healpix_map(),
                                          full_map)
Example #28
0
    def test_setitem_slice(self):
        """
        Test __setitem__ slice
        """
        random.seed(12345)

        nside_coverage = 32
        nside_map = 128

        full_map = np.zeros(hp.nside2npix(nside_map)) + hp.UNSEEN
        full_map[0:5000] = random.random(size=5000)

        sparse_map = healsparse.HealSparseMap(healpix_map=full_map,
                                              nside_coverage=nside_coverage)

        # This needs to be accessed with an array of length 1 or same length.
        sparse_map[100:500] = np.array([1.0])
        full_map[100:500] = np.array([1.0])
        testing.assert_array_almost_equal(sparse_map.generate_healpix_map(),
                                          full_map)

        sparse_map[1000:1500] = np.ones(500)
        full_map[1000:1500] = np.ones(500)
        testing.assert_array_almost_equal(sparse_map.generate_healpix_map(),
                                          full_map)

        sparse_map[10000:11000:2] = np.ones(500)
        full_map[10000:11000:2] = np.ones(500)
        testing.assert_array_almost_equal(sparse_map.generate_healpix_map(),
                                          full_map)

        # Test all
        sparse_map[:] = np.array([1.0])
        full_map[:] = np.array([1.0])
        testing.assert_array_almost_equal(sparse_map.generate_healpix_map(),
                                          full_map)
Example #29
0
    def test_getitem_array(self):
        """
        Test __getitem__ using an array
        """
        random.seed(12345)

        nside_coverage = 32
        nside_map = 128

        full_map = np.zeros(hpg.nside_to_npixel(nside_map)) + hpg.UNSEEN
        full_map[0:5000] = random.random(size=5000)

        sparse_map = healsparse.HealSparseMap(healpix_map=full_map,
                                              nside_coverage=nside_coverage)

        indices = np.array([1, 2, 100, 500, 10000])
        testing.assert_array_almost_equal(sparse_map[indices],
                                          full_map[indices])
        testing.assert_almost_equal(sparse_map[indices[0]],
                                    full_map[indices[0]])

        indices = np.array([1., 2, 100, 500, 10000])
        self.assertRaises(IndexError, sparse_map.__getitem__, indices)
        self.assertRaises(IndexError, sparse_map.__getitem__, indices[0])
Example #30
0
    def test_lookup(self):
        """
        Test lookup functionality
        """
        np.random.seed(12345)

        nside_coverage = 32
        nside_map = 1024

        full_map = np.zeros(hp.nside2npix(nside_map)) + hp.UNSEEN
        full_map[0:200000] = np.random.random(size=200000)

        sparse_map = healsparse.HealSparseMap(healpix_map=full_map,
                                              nside_coverage=nside_coverage)

        n_rand = 100000
        ra = np.random.random(n_rand) * 360.0
        dec = np.random.random(n_rand) * 180.0 - 90.0

        theta = np.radians(90.0 - dec)
        phi = np.radians(ra)
        ipnest = hp.ang2pix(nside_map, theta, phi, nest=True)

        test_values = full_map[ipnest]

        # Test the pixel lookup
        comp_values = sparse_map.get_values_pix(ipnest)
        testing.assert_almost_equal(comp_values, test_values)

        # Test pixel lookup (valid pixels)
        # Note that this tests all the downstream functions
        valid_mask = sparse_map.get_values_pix(ipnest, valid_mask=True)
        testing.assert_equal(valid_mask, comp_values > hp.UNSEEN)

        # Test pixel lookup (ring)
        ipring = hp.nest2ring(nside_map, ipnest)
        comp_values = sparse_map.get_values_pix(ipring, nest=False)
        testing.assert_almost_equal(comp_values, test_values)

        # Test pixel lookup (higher nside)
        comp_values = sparse_map.get_values_pix(hp.ang2pix(4096,
                                                           ra,
                                                           dec,
                                                           lonlat=True,
                                                           nest=True),
                                                nside=4096)
        testing.assert_almost_equal(comp_values, test_values)

        # Test pixel lookup (lower nside)
        lowres_pix = hp.ang2pix(256, ra, dec, lonlat=True, nest=True)
        self.assertRaises(ValueError,
                          sparse_map.get_values_pix,
                          lowres_pix,
                          nside=256)

        # Test the theta/phi lookup
        comp_values = sparse_map.get_values_pos(theta, phi, lonlat=False)
        testing.assert_almost_equal(comp_values, test_values)

        # Test the ra/dec lookup
        comp_values = sparse_map.get_values_pos(ra, dec, lonlat=True)
        testing.assert_almost_equal(comp_values, test_values)

        # Test the list of valid pixels
        valid_pixels = sparse_map.valid_pixels
        testing.assert_equal(valid_pixels, np.where(full_map > hp.UNSEEN)[0])

        # Test the position of valid pixels
        ra_sp, dec_sp = sparse_map.valid_pixels_pos(lonlat=True)
        _ra_sp, _dec_sp = hp.pix2ang(nside_map,
                                     np.where(full_map > hp.UNSEEN)[0],
                                     lonlat=True,
                                     nest=True)
        testing.assert_equal(ra_sp, _ra_sp)
        testing.assert_equal(dec_sp, _dec_sp)

        # Test position of valid pixels and valid pixels
        valid_pixels, ra_sp, dec_sp = sparse_map.valid_pixels_pos(
            lonlat=True, return_pixels=True)
        _ra_sp, _dec_sp = hp.pix2ang(nside_map,
                                     np.where(full_map > hp.UNSEEN)[0],
                                     lonlat=True,
                                     nest=True)
        testing.assert_equal(ra_sp, _ra_sp)
        testing.assert_equal(dec_sp, _dec_sp)
        testing.assert_equal(valid_pixels, np.where(full_map > hp.UNSEEN)[0])