Exemple #1
0
    def test_almxfl(self):
        import healpy as hp

        for lmax in [100, 400, 500, 1000]:
            ainfo = sharp.alm_info(lmax)
            alms = hp.synalm(np.ones(lmax + 1), lmax=lmax)
            filtering = np.ones(lmax + 1)
            alms0 = ainfo.lmul(alms.copy(), filtering)
            assert np.all(np.isclose(alms0, alms))

        for lmax in [100, 400, 500, 1000]:
            ainfo = sharp.alm_info(lmax)
            alms = hp.synalm(np.ones(lmax + 1), lmax=lmax)
            alms0 = curvedsky.almxfl(alms.copy(), lambda x: np.ones(x.shape))
            assert np.all(np.isclose(alms0, alms))
Exemple #2
0
    def test_sharp_alm2map_der1(self):

        # Test the wrapper around libsharps alm2map_der1.
        lmax = 3
        ainfo = sharp.alm_info(lmax)

        nrings = lmax + 1
        nphi = 2 * lmax + 1
        minfo = sharp.map_info_gauss_legendre(nrings, nphi)

        sht = sharp.sht(minfo, ainfo)

        # Test different input shapes and dtypes.
        # Case 1a: 1d double precision.
        alm = np.zeros((ainfo.nelem), dtype=np.complex128)
        alm[4] = 1. + 1.j

        omap = sht.alm2map_der1(alm)
        # Compare to expected value by doing spin 1 transform
        # on sqrt(ell (ell + 1)) alm.
        alm_spin = np.zeros((2, ainfo.nelem), dtype=np.complex128)
        alm_spin[0] = alm * np.sqrt(2)
        omap_exp = sht.alm2map(alm_spin, spin=1)

        np.testing.assert_array_almost_equal(omap, omap_exp)

        # Case 1b: 1d single precision.
        alm = np.zeros((ainfo.nelem), dtype=np.complex64)
        alm[4] = 1. + 1.j

        omap = sht.alm2map_der1(alm)
        # Compare to expected value by doing spin 1 transform
        # on sqrt(ell (ell + 1)) alm.
        alm_spin = np.zeros((2, ainfo.nelem), dtype=np.complex64)
        alm_spin[0] = alm * np.sqrt(2)
        omap_exp = sht.alm2map(alm_spin, spin=1)

        np.testing.assert_array_almost_equal(omap, omap_exp)

        # Case 2a: 2d double precision.
        ntrans = 3
        alm = np.zeros((ntrans, ainfo.nelem), dtype=np.complex128)
        alm[0, 4] = 1. + 1.j
        alm[1, 4] = 2. + 2.j
        alm[2, 4] = 3. + 3.j

        omap = sht.alm2map_der1(alm)
        # Compare to expected value by doing spin 1 transform
        # on sqrt(ell (ell + 1)) alm.
        alm_spin = np.zeros((ntrans, 2, ainfo.nelem), dtype=np.complex128)
        alm_spin[0, 0] = alm[0] * np.sqrt(2)
        alm_spin[1, 0] = alm[1] * np.sqrt(2)
        alm_spin[2, 0] = alm[2] * np.sqrt(2)
        omap_exp = sht.alm2map(alm_spin, spin=1)

        np.testing.assert_array_almost_equal(omap, omap_exp)

        # Case 2b: 2d single precision.
        ntrans = 3
        alm = np.zeros((ntrans, ainfo.nelem), dtype=np.complex64)
        alm[0, 4] = 1. + 1.j
        alm[1, 4] = 2. + 2.j
        alm[2, 4] = 3. + 3.j

        omap = sht.alm2map_der1(alm)
        # Compare to expected value by doing spin 1 transform
        # on sqrt(ell (ell + 1)) alm.
        alm_spin = np.zeros((ntrans, 2, ainfo.nelem), dtype=np.complex64)
        alm_spin[0, 0] = alm[0] * np.sqrt(2)
        alm_spin[1, 0] = alm[1] * np.sqrt(2)
        alm_spin[2, 0] = alm[2] * np.sqrt(2)
        omap_exp = sht.alm2map(alm_spin, spin=1)

        np.testing.assert_array_almost_equal(omap, omap_exp)
Exemple #3
0
    def test_sharp_alm2map_roundtrip(self):

        # Test the wrapper around libsharps alm2map/map2alm.
        lmax = 3
        ainfo = sharp.alm_info(lmax)

        nrings = lmax + 1
        nphi = 2 * lmax + 1
        minfo = sharp.map_info_gauss_legendre(nrings, nphi)

        sht = sharp.sht(minfo, ainfo)

        # Test different input shapes and dtypes.
        # Case 1a: 1d double precision.
        spin = 0
        alm = np.zeros((ainfo.nelem), dtype=np.complex128)
        alm[4] = 1. + 1.j

        omap = sht.alm2map(alm, spin=spin)
        self.assertEqual(omap.shape, (minfo.npix, ))
        self.assertEqual(omap.dtype, np.float64)
        alm_out = sht.map2alm(omap, spin=spin)

        np.testing.assert_array_almost_equal(alm_out, alm)

        # Case 1b: 1d single precision.
        spin = 0
        alm = np.zeros((ainfo.nelem), dtype=np.complex64)
        alm[4] = 1. + 1.j

        omap = sht.alm2map(alm, spin=spin)
        self.assertEqual(omap.shape, (minfo.npix, ))
        self.assertEqual(omap.dtype, np.float32)
        alm_out = sht.map2alm(omap, spin=spin)

        np.testing.assert_array_almost_equal(alm_out, alm)

        # Case 2a: 2d double precision.
        spin = 1
        nspin = 2
        alm = np.zeros((nspin, ainfo.nelem), dtype=np.complex128)
        alm[0, 4] = 1. + 1.j
        alm[1, 4] = 2. - 2.j

        omap = sht.alm2map(alm, spin=spin)
        self.assertEqual(omap.shape, (nspin, minfo.npix))
        self.assertEqual(omap.dtype, np.float64)
        alm_out = sht.map2alm(omap, spin=spin)

        np.testing.assert_array_almost_equal(alm_out, alm)

        # Case 2b: 2d single precision.
        spin = 1
        nspin = 2
        alm = np.zeros((nspin, ainfo.nelem), dtype=np.complex64)
        alm[0, 4] = 1. + 1.j
        alm[1, 4] = 2. - 2.j

        omap = sht.alm2map(alm, spin=spin)
        self.assertEqual(omap.shape, (nspin, minfo.npix))
        self.assertEqual(omap.dtype, np.float32)
        alm_out = sht.map2alm(omap, spin=spin)

        np.testing.assert_array_almost_equal(alm_out, alm)

        # Case 3a: 3d double precision.
        spin = 1
        nspin = 2
        ntrans = 3
        alm = np.zeros((ntrans, nspin, ainfo.nelem), dtype=np.complex128)
        alm[0, 0, 4] = 1. + 1.j
        alm[0, 1, 4] = 2. - 2.j
        alm[1, 0, 4] = 3. + 3.j
        alm[1, 1, 4] = 4. - 4.j
        alm[2, 0, 4] = 5. + 5.j
        alm[2, 1, 4] = 6. - 6.j

        omap = sht.alm2map(alm, spin=spin)
        self.assertEqual(omap.shape, (ntrans, nspin, minfo.npix))
        self.assertEqual(omap.dtype, np.float64)
        alm_out = sht.map2alm(omap, spin=spin)

        np.testing.assert_array_almost_equal(alm_out, alm)

        # Case 3b: 3d single precision.
        spin = 1
        nspin = 2
        ntrans = 3
        alm = np.zeros((ntrans, nspin, ainfo.nelem), dtype=np.complex64)
        alm[0, 0, 4] = 1. + 1.j
        alm[0, 1, 4] = 2. - 2.j
        alm[1, 0, 4] = 3. + 3.j
        alm[1, 1, 4] = 4. - 4.j
        alm[2, 0, 4] = 5. + 5.j
        alm[2, 1, 4] = 6. - 6.j

        omap = sht.alm2map(alm, spin=spin)
        self.assertEqual(omap.shape, (ntrans, nspin, minfo.npix))
        self.assertEqual(omap.dtype, np.float32)
        alm_out = sht.map2alm(omap, spin=spin)

        np.testing.assert_array_almost_equal(alm_out, alm)
Exemple #4
0
    def test_prepare_alm_mmax(self):
        # Check if mmax is correctly handled by prepare_alm.

        # Create lmax=mmax=3 alm array and corresponding alm_info.
        lmax = 3
        nalm = 10  # Triangular alm array of lmax=3 has 10 elements.
        alm_in = np.arange(nalm, dtype=np.complex128)
        ainfo_in = sharp.alm_info(lmax=3,
                                  mmax=3,
                                  nalm=nalm,
                                  stride=1,
                                  layout="triangular")

        # Case 1: provide only alm.
        alm_out, ainfo_out = curvedsky.prepare_alm(alm=alm_in, ainfo=None)

        np.testing.assert_array_almost_equal(alm_out, alm_in)
        self.assertIs(ainfo_out.lmax, ainfo_in.lmax)
        self.assertIs(ainfo_out.mmax, ainfo_in.mmax)
        self.assertIs(ainfo_out.nelem, ainfo_in.nelem)

        # Case 2: provide only alm_info.
        alm_out, ainfo_out = curvedsky.prepare_alm(alm=None, ainfo=ainfo_in)
        # Expect zero array.
        np.testing.assert_array_almost_equal(alm_out, alm_in * 0)
        self.assertIs(ainfo_out.lmax, ainfo_in.lmax)
        self.assertIs(ainfo_out.mmax, ainfo_in.mmax)
        self.assertIs(ainfo_out.nelem, ainfo_in.nelem)

        # Case 3: provide alm and alm_info
        alm_out, ainfo_out = curvedsky.prepare_alm(alm=alm_in, ainfo=ainfo_in)

        np.testing.assert_array_almost_equal(alm_out, alm_in)
        self.assertIs(ainfo_out.lmax, ainfo_in.lmax)
        self.assertIs(ainfo_out.mmax, ainfo_in.mmax)
        self.assertIs(ainfo_out.nelem, ainfo_in.nelem)

        # Case 4: provide only alm with lmax=3 and mmax=1.
        # This should currently fail.
        nalm = 7
        alm_in = np.arange(7, dtype=np.complex128)
        self.assertRaises(AssertionError, curvedsky.prepare_alm,
                          **dict(alm=alm_in, ainfo=None, lmax=lmax))

        # Case 5: provide only alm_info with lmax=3 and mmax=1.
        nalm = 7
        ainfo_in = sharp.alm_info(lmax=3,
                                  mmax=1,
                                  nalm=nalm,
                                  stride=1,
                                  layout="triangular")
        alm_exp = np.zeros(7, dtype=np.complex128)
        alm_out, ainfo_out = curvedsky.prepare_alm(alm=None, ainfo=ainfo_in)

        np.testing.assert_array_almost_equal(alm_out, alm_exp)
        self.assertIs(ainfo_out.lmax, ainfo_in.lmax)
        self.assertIs(ainfo_out.mmax, ainfo_in.mmax)
        self.assertIs(ainfo_out.nelem, ainfo_in.nelem)

        # Case 6: provide both alm and alm_info with lmax=3 and mmax=1.
        # This should be allowed.
        nalm = 7
        ainfo_in = sharp.alm_info(lmax=3,
                                  mmax=1,
                                  nalm=nalm,
                                  stride=1,
                                  layout="triangular")
        alm_in = np.arange(7, dtype=np.complex128)
        alm_out, ainfo_out = curvedsky.prepare_alm(alm=alm_in, ainfo=ainfo_in)

        np.testing.assert_array_almost_equal(alm_out, alm_in)
        self.assertIs(ainfo_out.lmax, ainfo_in.lmax)
        self.assertIs(ainfo_out.mmax, ainfo_in.mmax)
        self.assertIs(ainfo_out.nelem, ainfo_in.nelem)
Exemple #5
0
def apply_beam_sht(map, bl, tol=1e-5):
    lmax = np.where(bl / np.max(bl) > tol)[0][-1]
    ainfo = sharp.alm_info(lmax)
    alm = curvedsky.map2alm_cyl(map, ainfo=ainfo)
    ainfo.lmul(alm, bl[:lmax + 1], out=alm)
    return curvedsky.alm2map_cyl(alm, map, copy=True)


    kappa_alms = healpy.sphtfunc.map2alm(kappa_map_hp)

    shape, wcs = enmap.fullsky_geometry(p['PIX_SIZE_ARCMIN']*utils.arcmin)

    # phi_alms =   healpy.sphtfunc.almxfl(kappa_alms, 1. / (ells * (ells + 1) / 2.))

    # phi_map = 

    kappa_map_car = enmap.enmap(np.zeros(shape), wcs)
    curvedsky.alm2map(kappa_alms, kappa_map_car)

    lmax = healpy.Alm.getlmax(len(kappa_alms))
    ainfo = sharp.alm_info(lmax)

    ellvals = np.arange(lmax)

    #factor to change kappa into phi
    func = 2. / (ellvals * (ellvals + 1.))
    func[0] = 0

    phi_alm = healpy.sphtfunc.almxfl(kappa_alms, func)
    
    cmb_powers = get_cmb_powerspectra.websky_cmb_spectra()['unlensed_scalar']

    cmb_alm, cmb_ainfo = curvedsky.rand_alm(cmb_powers, lmax = lmax, seed = 1, return_ainfo = True)

    unlensed_map, lensed_map = lensing.lens_map_curved((3,) + shape, wcs, phi_alm, cmb_alm, ainfo, output = 'ul')