Exemple #1
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(hpg.nside_to_npixel(nside_map)) + hpg.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(hpg.nside_to_npixel(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)
Exemple #2
0
    def test_upgrade_map_recarray(self):
        """
        Test upgrade functionality with a recarray.
        """
        random.seed(seed=12345)

        nside_coverage = 32
        nside_map = 256
        nside_new = 1024

        dtype = [('col1', 'f8'), ('col2', 'f8'), ('col3', 'i4')]
        sparse_map = healsparse.HealSparseMap.make_empty(nside_coverage,
                                                         nside_map,
                                                         dtype,
                                                         primary='col1')
        pixel = np.arange(20000)
        values = np.zeros_like(pixel, dtype=dtype)
        values['col1'] = random.random(size=pixel.size)
        values['col2'] = random.random(size=pixel.size)
        values['col3'] = random.poisson(size=pixel.size, lam=2)
        sparse_map.update_values_pix(pixel, values)

        ra, dec = hpg.pixel_to_angle(nside_map, pixel)

        # Make the test values
        hpmap_col1 = np.zeros(hpg.nside_to_npixel(nside_map)) + hpg.UNSEEN
        hpmap_col2 = np.zeros(hpg.nside_to_npixel(nside_map)) + hpg.UNSEEN
        hpmap_col3 = np.zeros(hpg.nside_to_npixel(nside_map)) + hpg.UNSEEN
        hpmap_col1[pixel] = values['col1']
        hpmap_col2[pixel] = values['col2']
        hpmap_col3[pixel] = values['col3']

        # Upgrade healpix maps
        hpmap_col1 = hp.ud_grade(hpmap_col1,
                                 nside_out=nside_new,
                                 order_in='NESTED',
                                 order_out='NESTED')
        hpmap_col2 = hp.ud_grade(hpmap_col2,
                                 nside_out=nside_new,
                                 order_in='NESTED',
                                 order_out='NESTED')
        hpmap_col3 = hp.ud_grade(hpmap_col3,
                                 nside_out=nside_new,
                                 order_in='NESTED',
                                 order_out='NESTED')
        ipnest_test = hpg.angle_to_pixel(nside_new, ra, dec)

        # Upgrade the old map
        new_map = sparse_map.upgrade(nside_out=nside_new)
        testing.assert_almost_equal(
            new_map.get_values_pos(ra, dec, lonlat=True)['col1'],
            hpmap_col1[ipnest_test])
        testing.assert_almost_equal(
            new_map.get_values_pos(ra, dec, lonlat=True)['col2'],
            hpmap_col2[ipnest_test])
        testing.assert_almost_equal(
            new_map.get_values_pos(ra, dec, lonlat=True)['col3'],
            hpmap_col3[ipnest_test])
Exemple #3
0
    def test_degrade_map_recarray(self):
        """
        Test HealSparse.degrade functionality with recarray quantities
        """
        random.seed(seed=12345)

        nside_coverage = 32
        nside_map = 1024
        nside_new = 256

        dtype = [('col1', 'f8'), ('col2', 'f8'), ('col3', 'i4')]
        sparse_map = healsparse.HealSparseMap.make_empty(nside_coverage, nside_map, dtype, primary='col1')
        pixel = np.arange(20000)
        values = np.zeros_like(pixel, dtype=dtype)
        values['col1'] = random.random(size=pixel.size)
        values['col2'] = random.random(size=pixel.size)
        values['col3'] = random.poisson(size=pixel.size, lam=2)
        sparse_map.update_values_pix(pixel, values)

        ra, dec = hpg.pixel_to_angle(nside_map, pixel)

        # Make the test values
        hpmap_col1 = np.zeros(hpg.nside_to_npixel(nside_map)) + hpg.UNSEEN
        hpmap_col2 = np.zeros(hpg.nside_to_npixel(nside_map)) + hpg.UNSEEN
        hpmap_col3 = np.zeros(hpg.nside_to_npixel(nside_map)) + hpg.UNSEEN
        hpmap_col1[pixel] = values['col1']
        hpmap_col2[pixel] = values['col2']
        hpmap_col3[pixel] = values['col3']

        # Degrade healpix maps
        hpmap_col1 = hp.ud_grade(hpmap_col1, nside_out=nside_new, order_in='NESTED', order_out='NESTED')
        hpmap_col2 = hp.ud_grade(hpmap_col2, nside_out=nside_new, order_in='NESTED', order_out='NESTED')
        hpmap_col3 = hp.ud_grade(hpmap_col3, nside_out=nside_new, order_in='NESTED', order_out='NESTED')
        ipnest_test = hpg.angle_to_pixel(nside_new, ra, dec)

        # Degrade the old map
        new_map = sparse_map.degrade(nside_out=nside_new)
        testing.assert_almost_equal(new_map.get_values_pos(ra, dec, lonlat=True)['col1'],
                                    hpmap_col1[ipnest_test])
        testing.assert_almost_equal(new_map.get_values_pos(ra, dec, lonlat=True)['col2'],
                                    hpmap_col2[ipnest_test])
        testing.assert_almost_equal(new_map.get_values_pos(ra, dec, lonlat=True)['col3'],
                                    hpmap_col3[ipnest_test])

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

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

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

        testing.assert_almost_equal(new_map2.get_values_pos(ra, dec, lonlat=True)['col1'],
                                    hpmap_col1[ipnest_test])
        testing.assert_almost_equal(new_map2.get_values_pos(ra, dec, lonlat=True)['col2'],
                                    hpmap_col2[ipnest_test])
        testing.assert_almost_equal(new_map2.get_values_pos(ra, dec, lonlat=True)['col3'],
                                    hpmap_col3[ipnest_test])
    def compute_cov_map(self, nside_coverage, non_masked_px, nfine, bit_shift):
        cov_map_orig = np.zeros(hpg.nside_to_npixel(nside_coverage),
                                dtype=np.float64)
        idx_cov = np.right_shift(np.arange(int(non_masked_px * nfine)),
                                 bit_shift)
        unique_idx_cov = np.unique(idx_cov)
        idx_counts = np.bincount(
            idx_cov,
            minlength=hpg.nside_to_npixel(nside_coverage)).astype(np.float64)

        cov_map_orig[unique_idx_cov] = idx_counts[unique_idx_cov] / nfine

        return cov_map_orig
Exemple #5
0
    def test_degrade_map_bool(self):
        """
        Test HealSparse.degrade functionality with bool quantities
        """
        random.seed(12345)
        nside_coverage = 32
        nside_map = 1024
        nside_new = 256

        full_map = np.zeros(hpg.nside_to_npixel(nside_map), dtype=bool)
        pixels = np.random.choice(full_map.size, size=full_map.size//4, replace=False)
        full_map[pixels] = True

        sparse_map = healsparse.HealSparseMap.make_empty(nside_coverage, nside_map, np.bool_)
        sparse_map[pixels] = full_map[pixels]

        # Degrade original map
        test_map = full_map.astype(np.float64)
        test_map[~full_map] = hpg.UNSEEN
        deg_map = hp.ud_grade(test_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())
Exemple #6
0
    def test_getitem_slice(self):
        """
        Test __getitem__ using slices
        """
        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)

        # 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[:])
Exemple #7
0
    def test_setitem_bool(self):
        """
        Test __setitem__ for boolean HealSparseMaps
        """
        random.seed(12345)
        nside_coverage = 32
        nside_map = 128
        pxnums = np.arange(0, 2000)
        pxvalues = np.ones(pxnums.size, dtype=bool)
        full_map = np.zeros(hpg.nside_to_npixel(nside_map), dtype=bool)
        full_map[pxnums] = pxvalues

        sparse_map = healsparse.HealSparseMap.make_empty(
            nside_coverage=nside_coverage,
            nside_sparse=nside_map,
            dtype=pxvalues.dtype)

        sparse_map[pxnums[0]] = pxvalues[0]
        testing.assert_equal(sparse_map[pxnums[0]], full_map[pxnums[0]])

        sparse_map[int(pxnums[1])] = bool(pxvalues[1])
        testing.assert_equal(sparse_map[pxnums[1]], full_map[pxnums[1]])

        sparse_map[pxnums] = pxvalues
        testing.assert_array_almost_equal(sparse_map[pxnums], full_map[pxnums])
Exemple #8
0
    def test_setitem_list(self):
        """
        Test __setitem__ list
        """
        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 = [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)
    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(hpg.nside_to_npixel(nside_map)) + hpg.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)
Exemple #10
0
    def test_degrade_map_int(self):
        """
        Test HealSparse.degrade functionality with int quantities
        """
        random.seed(12345)
        nside_coverage = 32
        nside_map = 1024
        nside_new = 256
        full_map = random.poisson(size=hpg.nside_to_npixel(nside_map), lam=2)

        # Generate sparse map
        sparse_map = healsparse.HealSparseMap.make_empty(nside_coverage, nside_map, np.int64)
        sparse_map.update_values_pix(np.arange(full_map.size), full_map)

        # Degrade original HEALPix map

        deg_map = hp.ud_grade(full_map.astype(np.float64), 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_int_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())
    def test_read_parquet_coverage(self):
        """
        Test reading healSparseCoverage from a parquet dataset.
        """
        nside_coverage = 32
        nside_map = 64

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

        sparse_map = healsparse.HealSparseMap.make_empty(nside_coverage,
                                                         nside_map,
                                                         dtype=np.float32)

        sparse_map[0:20000] = np.random.random(size=20000).astype(np.float32)

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

        # Generate a coverage mask from the 0: 20000
        cov_mask_test = np.zeros(hpg.nside_to_npixel(nside_coverage),
                                 dtype=np.bool_)
        ra, dec = hpg.pixel_to_angle(nside_map, np.arange(20000))
        ipnest = np.unique(hpg.angle_to_pixel(nside_coverage, ra, dec))
        cov_mask_test[ipnest] = True

        cov_map = healsparse.HealSparseCoverage.read(fname)

        # Ensure that the coverage mask is what we think it should be
        testing.assert_array_equal(cov_map.coverage_mask, cov_mask_test)

        # Ensure that we can address the cov_map by index
        testing.assert_array_equal(cov_map[:], cov_map._cov_index_map)
        testing.assert_array_equal(cov_map[0:100],
                                   cov_map._cov_index_map[0:100])
        testing.assert_array_equal([cov_map[0]], [cov_map._cov_index_map[0]])
Exemple #12
0
    def test_valid_area(self):
        """
        Test getting the valid area of a map
        """
        random.seed(seed=12345)

        nside_coverage = 32
        nside_map = 64

        n_rand = 1000
        r_indices = np.random.choice(np.arange(hpg.nside_to_npixel(nside_map)),
                                     size=n_rand,
                                     replace=False)

        for dt in [np.float64, np.int64]:
            # Create an empty map
            sparse_map = healsparse.HealSparseMap.make_empty(
                nside_coverage, nside_map, dt)

            # Check that the map is make_empty
            testing.assert_equal(sparse_map.get_valid_area(), 0)

            # Fill up the maps
            sparse_map.update_values_pix(r_indices, np.ones(n_rand, dtype=dt))
            testing.assert_equal(
                sparse_map.get_valid_area(),
                n_rand * hpg.nside_to_pixel_area(nside_map, degrees=True))
            testing.assert_equal(sparse_map.n_valid, n_rand)
    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(hpg.nside_to_npixel(nside_map)) + hpg.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)
Exemple #14
0
    def test_fits_writeread_withheader(self):
        """
        Test fits i/o functionality with a header
        """
        random.seed(seed=12345)

        nside_coverage = 32
        nside_map = 64

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

        full_map = np.zeros(hpg.nside_to_npixel(nside_map)) + hpg.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(os.path.join(self.test_dir, 'sparsemap_with_header.hs'))

        ret_map, ret_hdr = healsparse.HealSparseMap.read(os.path.join(self.test_dir,
                                                                      'sparsemap_with_header.hs'),
                                                         header=True)

        self.assertEqual(hdr['TESTING'], ret_hdr['TESTING'])
    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)
    def make_empty(cls, nside_coverage, nside_sparse):
        """
        Make an empty coverage map.

        Parameters
        ----------
        nside_coverage : `int`
           Healpix nside for the coverage map
        nside_sparse : `int`
           Healpix nside for the sparse map

        Returns
        -------
        healSparseCoverage : `HealSparseCoverage`
           HealSparseCoverage from file
        """
        if nside_coverage > 128:
            warnings.warn(
                'Using `nside_coverage` > 128 may result in poor performance',
                ResourceWarning)
        bit_shift = _compute_bitshift(nside_coverage, nside_sparse)
        nfine_per_cov = 2**bit_shift

        cov_index_map = -1 * np.arange(hpg.nside_to_npixel(nside_coverage),
                                       dtype=np.int64) * nfine_per_cov

        return cls(cov_index_map, nside_sparse)
Exemple #17
0
    def test_moc_write_hsp_read_mocpy(self):
        """
        Test write healsparse MOC and read with mocpy.
        """
        try:
            import mocpy
        except ImportError:
            return

        nside_coverage = 32
        nside_map = 2048

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

        sparse_map = healsparse.HealSparseMap.make_empty(nside_coverage, nside_map, bool)
        sparse_map[30000: 70000] = True
        sparse_map[80000: 90000] = True

        fname = os.path.join(self.test_dir, 'healsparse_moc.fits')

        sparse_map.write_moc(fname)

        # We use the old deprecated interface because the new one can't read
        # standard FITS files.
        with warnings.catch_warnings():
            warnings.filterwarnings("ignore", category=DeprecationWarning)
            moc = mocpy.MOC.from_fits(fname)

        # There is no mocpy pixel lookup, we must go through ra/dec for some reason.
        ra, dec = hpg.pixel_to_angle(nside_map, np.arange(hpg.nside_to_npixel(nside_map)))
        arr = moc.contains(ra*u.degree, dec*u.degree)

        testing.assert_array_equal(arr.nonzero()[0], sparse_map.valid_pixels)
Exemple #18
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)
    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(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)

        # 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)
    def test_parquet_read_outoforder(self):
        """
        Test reading parquet maps that have been written with out-of-order pixels
        """
        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-')

        # Create an empty map
        sparse_map = healsparse.HealSparseMap.make_empty(
            nside_coverage, nside_map, np.float64)
        fname = os.path.join(self.test_dir,
                             'healsparse_map_outoforder.hsparquet')

        # Fill it out of order
        pixel = np.arange(4000, 20000)
        values = np.random.random(pixel.size)
        sparse_map.update_values_pix(pixel, values)
        pixel2 = np.arange(1000)
        values2 = np.random.random(pixel2.size)
        sparse_map.update_values_pix(pixel2, values2)

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

        # And read it in...
        sparse_map = healsparse.HealSparseMap.read(fname)

        # Test some values
        ipnest = hpg.angle_to_pixel(nside_map, ra, dec)
        test_map = np.zeros(hpg.nside_to_npixel(nside_map)) + hpg.UNSEEN
        test_map[pixel] = values
        test_map[pixel2] = values2

        testing.assert_almost_equal(sparse_map.get_values_pix(ipnest),
                                    test_map[ipnest])

        # Read in the first two and the Nth pixel

        # These pixels are chosen because they are covered by the random test points
        sparse_map_small = healsparse.HealSparseMap.read(fname,
                                                         pixels=[0, 1, 3179])

        # Test some values
        ipnest_cov = np.right_shift(ipnest,
                                    sparse_map_small._cov_map.bit_shift)
        test_values_small = test_map[ipnest]
        outside_small, = np.where((ipnest_cov != 0) & (ipnest_cov != 1)
                                  & (ipnest_cov != 3179))
        test_values_small[outside_small] = hpg.UNSEEN

        testing.assert_almost_equal(sparse_map_small.get_values_pix(ipnest),
                                    test_values_small)
    def test_read_fits_coverage(self):
        """
        Test reading healSparseCoverage from a fits file.
        """
        nside_coverage = 32
        nside_map = 64

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

        sparse_map = healsparse.HealSparseMap.make_empty(nside_coverage,
                                                         nside_map,
                                                         dtype=np.float32)

        sparse_map[0:20000] = np.random.random(size=20000).astype(np.float32)

        sparse_map.write(fname)

        # Generate a coverage mask from the 0: 20000
        cov_mask_test = np.zeros(hpg.nside_to_npixel(nside_coverage),
                                 dtype=np.bool_)
        ra, dec = hpg.pixel_to_angle(nside_map, np.arange(20000))
        ipnest = np.unique(hpg.angle_to_pixel(nside_coverage, ra, dec))
        cov_mask_test[ipnest] = True

        cov_map = healsparse.HealSparseCoverage.read(fname)

        # Ensure that the coverage mask is what we think it should be
        testing.assert_array_equal(cov_map.coverage_mask, cov_mask_test)

        # Ensure that we can address the cov_map by index
        testing.assert_array_equal(cov_map[:], cov_map._cov_index_map)
        testing.assert_array_equal(cov_map[0:100],
                                   cov_map._cov_index_map[0:100])
        testing.assert_array_equal([cov_map[0]], [cov_map._cov_index_map[0]])

        if not has_healpy:
            return

        # Make a healpy file and make sure we can't read it
        test_map = np.zeros(hpg.nside_to_npixel(nside_coverage))
        fname = os.path.join(self.test_dir, 'healpy_map_test.fits')
        hp.write_map(fname, test_map)

        self.assertRaises(RuntimeError, healsparse.HealSparseCoverage.read,
                          fname)
    def compute_fracdet_map(self, nside_map, nside_fracdet, non_masked_px,
                            nfine):
        bit_shift = healsparse.utils._compute_bitshift(nside_fracdet,
                                                       nside_map)

        fracdet_map_orig = np.zeros(hpg.nside_to_npixel(nside_fracdet),
                                    dtype=np.float64)
        idx_frac = np.right_shift(np.arange(int(non_masked_px * nfine)),
                                  bit_shift)
        unique_idx_frac = np.unique(idx_frac)
        idx_counts = np.bincount(
            idx_frac,
            minlength=hpg.nside_to_npixel(nside_fracdet)).astype(np.float64)
        nfine_frac = (nside_map // nside_fracdet)**2
        fracdet_map_orig[
            unique_idx_frac] = idx_counts[unique_idx_frac] / nfine_frac

        return fracdet_map_orig
Exemple #23
0
    def test_generate_healpix_map_recarray(self):
        """
        Testing the generation of a healpix map from recarray healsparsemap
        we also test the pixel and position lookup
        """
        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)

        # Make sure our pixels are unique
        ipnest = hpg.angle_to_pixel(nside_map, ra, dec)
        _, uind = np.unique(ipnest, return_index=True)
        ra = ra[uind]
        dec = dec[uind]
        value = value[uind]

        # Create empty healpix map
        healpix_map = np.zeros(hpg.nside_to_npixel(nside_map), dtype='f4') + hpg.UNSEEN
        healpix_map2 = np.zeros(hpg.nside_to_npixel(nside_map), dtype='f8') + hpg.UNSEEN
        healpix_map[hpg.angle_to_pixel(nside_map, ra, dec)] = value
        healpix_map2[hpg.angle_to_pixel(nside_map, ra, dec)] = value

        # Create an empty map
        dtype = [('col1', 'f4'), ('col2', 'f8')]

        self.assertRaises(RuntimeError, healsparse.HealSparseMap.make_empty, nside_coverage, nside_map, dtype)
        # Generate empty map that will be updated
        sparse_map = healsparse.HealSparseMap.make_empty(nside_coverage, nside_map, dtype, primary='col1')
        pixel = hpg.angle_to_pixel(nside_map, ra, dec)
        values = np.zeros_like(pixel, dtype=dtype)
        values['col1'] = value
        values['col2'] = value
        # Update values works with the HEALPix-like indexing scheme
        sparse_map.update_values_pix(pixel, values)
        hp_out1 = sparse_map.generate_healpix_map(nside=nside_map, key='col1')
        hp_out2 = sparse_map.generate_healpix_map(nside=nside_map, key='col2')
        testing.assert_almost_equal(healpix_map, hp_out1)
        testing.assert_almost_equal(healpix_map2, hp_out2)
    def append_pixels(self,
                      sparse_map_size,
                      new_cov_pix,
                      check=True,
                      copy=True):
        """
        Append new pixels to the coverage map

        Parameters
        ----------
        sparse_map_size : `int`
           Size of current sparse map
        new_cov_pix : `np.ndarray`
           Array of new coverage pixels
        """
        if check:
            if np.max(self.cov_mask[new_cov_pix]) > 0:
                raise RuntimeError(
                    "New coverage pixels are already in the map.")

        if copy:
            new_cov_map = self.copy()
        else:
            new_cov_map = self

        # Reset to "defaults"
        cov_index_map_temp = new_cov_map._cov_index_map + np.arange(
            hpg.nside_to_npixel(
                self.nside_coverage), dtype=np.int64) * self.nfine_per_cov
        # set the new pixels
        cov_index_map_temp[new_cov_pix] = (
            np.arange(new_cov_pix.size) * self.nfine_per_cov + sparse_map_size)
        # Restore the offset
        cov_index_map_temp -= np.arange(hpg.nside_to_npixel(
            self.nside_coverage),
                                        dtype=np.int64) * self.nfine_per_cov

        new_cov_map._cov_index_map[:] = cov_index_map_temp
        new_cov_map._compute_block_to_cov_index()

        return new_cov_map
Exemple #25
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(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)
        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(hpg.nside_to_npixel(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())
    def _compute_block_to_cov_index(self):
        """
        Compute the mapping from block number to cov_index
        """
        offset_map = (self._cov_index_map[:] +
                      np.arange(hpg.nside_to_npixel(self._nside_coverage)) *
                      self._nfine_per_cov)
        cov_mask = (offset_map >= self.nfine_per_cov)
        cov_pixels, = np.where(cov_mask)

        block_number = (offset_map[cov_pixels] // self.nfine_per_cov) - 1
        st = np.argsort(block_number)
        self._block_to_cov_index = cov_pixels[st]
    def coverage_mask(self):
        """
        Get the boolean mask of the coverage map.

        Returns
        -------
        cov_mask : `np.ndarray`
           Boolean array of coverage mask.
        """
        cov_mask = (self._cov_index_map[:] +
                    np.arange(hpg.nside_to_npixel(self._nside_coverage)) *
                    self._nfine_per_cov) >= self.nfine_per_cov
        return cov_mask
Exemple #28
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)
Exemple #29
0
    def test_degrade_map_float_sum(self):
        """
        Test HealSparse.degrade sum with float
        """
        nside_coverage = 32
        nside_map = 1024
        nside_new = 512
        full_map = np.full(hpg.nside_to_npixel(nside_map), 1.)
        # 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), 4.)
        # 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())
Exemple #30
0
    def test_healpix_explicit_read(self):
        """Test reading healpix partial (explicit) maps."""
        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,
                                'healpix_map_ring_explicit.fits')

        full_map_ring = hp.reorder(full_map, n2r=True)
        hp.write_map(filename, full_map_ring, dtype=np.float64, partial=True)

        # Read it with healsparse
        sparse_map = healsparse.HealSparseMap.read(
            filename, nside_coverage=nside_coverage)

        # Check that we can do a basic lookup
        testing.assert_almost_equal(sparse_map.get_values_pix(ipnest),
                                    test_values)

        filename = os.path.join(self.test_dir,
                                'healpix_map_nest_explicit.fits')
        hp.write_map(filename,
                     full_map,
                     dtype=np.float64,
                     nest=True,
                     partial=True)

        # Read it with healsparse
        sparse_map = healsparse.HealSparseMap.read(
            filename, nside_coverage=nside_coverage)

        # Check that we can do a basic lookup
        testing.assert_almost_equal(sparse_map.get_values_pix(ipnest),
                                    test_values)