Exemple #1
0
    def test_shape_power_law(self):

        exponent = -2.5
        f = Shape._power_law(exponent)
        k = np.arange(1, 10, dtype=float)

        np.testing.assert_almost_equal(f(k), k**exponent)
Exemple #2
0
    def test_shape_prim_orthogonal(self):

        ns = .9
        orthogonal = Shape.prim_orthogonal(ns=ns)

        def f1(k):
            return k**0

        def f2(k):
            pivot = 0.05
            return (k * (pivot / k)**((ns - 1) / 3))**-3

        def f3(k):
            pivot = 0.05
            return (k * (pivot / k)**((ns - 1) / 3))**-1

        def f4(k):
            pivot = 0.05
            return (k * (pivot / k)**((ns - 1) / 3))**-2

        funcs_expec = [f1, f2, f3, f4]

        k = np.arange(1, 10, dtype=float)
        for f, f_expect in zip(orthogonal.funcs, funcs_expec):
            np.testing.assert_almost_equal(f(k), f_expect(k))

        rule_expect = [(1, 1, 0), (3, 3, 3), (1, 2, 3)]
        self.assertEqual(orthogonal.rule, rule_expect)
        amps_expect = [-9., -24., 9.]
        self.assertEqual(orthogonal.amps, amps_expect)
        self.assertEqual(orthogonal.name, 'orthogonal')
Exemple #3
0
    def test_shape_power_law_amp(self):

        exponent = -2.5
        amp = 10
        f = Shape._power_law(exponent, amp=amp)
        k = np.arange(1, 10, dtype=float)

        np.testing.assert_almost_equal(f(k), amp * k**exponent)
Exemple #4
0
    def test_shape_get_f_k(self):

        k = np.asarray([1., 2., 3.])

        shape = Shape(self.funcs, self.rule, self.amps, self.name)

        f_k = shape.get_f_k(k)

        self.assertEqual(f_k.shape, (3, 2))  # (nk, ncomp)
        self.assertEqual(f_k.dtype, float)  # (nk, ncomp)
        self.assertTrue(f_k.flags['C_CONTIGUOUS'])

        exp_f_k = np.empty((3, 2))
        exp_f_k[:, 0] = [1., 1., 1.]
        exp_f_k[:, 1] = [1.**-3, 2.**-3, 3.**-3]

        np.testing.assert_almost_equal(f_k, exp_f_k)
Exemple #5
0
    def test_shape_init(self):

        shape = Shape(self.funcs, self.rule, self.amps, self.name)

        self.assertIs(shape.funcs[0], self.funcs[0])
        self.assertIs(shape.funcs[1], self.funcs[1])

        self.assertIs(shape.rule, self.rule)
        self.assertIs(shape.amps, self.amps)
        self.assertEqual(shape.name, self.name)
Exemple #6
0
    def test_shape_prim_local(self):

        local = Shape.prim_local()

        self.assertEqual(local.rule, self.rule)
        self.assertEqual(local.amps, self.amps)
        self.assertEqual(local.name, 'local')

        k = np.arange(1, 10, dtype=float)
        for f, f_expect in zip(local.funcs, self.funcs):
            np.testing.assert_almost_equal(f(k), f_expect(k))
Exemple #7
0
    def test_shape_prim_local_ns_pivot(self):

        ns = 0.5
        pivot = 0.1
        local = Shape.prim_local(ns=ns, pivot=pivot)

        def f1(k):
            return k**0

        def f2(k):
            return (k * (pivot / k)**((ns - 1) / 3))**-3

        funcs_expec = [f1, f2]

        k = np.arange(1, 10, dtype=float)
        for f, f_expect in zip(local.funcs, funcs_expec):
            np.testing.assert_almost_equal(f(k), f_expect(k))
Exemple #8
0
    def test_cosmology_add_prim_reduced_bispectrum(self):

        lmax = 300
        radii = np.asarray([11000., 14000.])
        dr = ((radii[1] - radii[0]) / 2.)
        pars = camb.CAMBparams(**self.cosmo_opts)

        cosmo = Cosmology(pars)
        cosmo.compute_transfer(lmax)

        prim_shape = Shape.prim_equilateral(ns=1)

        self.assertTrue(len(cosmo.red_bispectra) == 0)
        cosmo.add_prim_reduced_bispectrum(prim_shape, radii)
        self.assertTrue(len(cosmo.red_bispectra) == 1)

        red_bisp = cosmo.red_bispectra[0]

        nell = lmax - 1  # Reduced Bispectrum starts from ell=2.
        nr = len(radii)
        ncomp = len(prim_shape.funcs)
        npol = 2
        nfact = nr * len(prim_shape.rule)

        self.assertEqual(red_bisp.factors.shape, (ncomp * nr, npol, nell))
        self.assertEqual(red_bisp.factors.dtype, float)

        self.assertEqual(red_bisp.rule.shape, (nfact, 3))
        self.assertEqual(red_bisp.rule.dtype, int)

        self.assertEqual(red_bisp.weights.shape, (nfact, 3))
        self.assertEqual(red_bisp.weights.dtype, float)

        ells = np.arange(2, lmax + 1)
        np.testing.assert_equal(red_bisp.ells_full, ells)

        # Equilateral rule = [(1,1,0), (3,3,3), (1,2,3)].
        rule_exp = np.asarray(
            [
                [2, 2, 0],  # r[0].
                [3, 3, 1],  # r[1].
                [6, 6, 6],
                [7, 7, 7],
                [2, 4, 6],
                [3, 5, 7]
            ],
            dtype=int)
        np.testing.assert_array_equal(red_bisp.rule, rule_exp)

        weights_exp = np.ones((nfact, 3))
        # nfact is ordered like flattened (nprim, radii) array.
        # I assume equilateral shape here, so nprim = 3.
        weights_exp[0, ...] = (dr * radii[0]**2 * 3)**(1 / 3)
        weights_exp[1, ...] = (dr * radii[1]**2 * 3)**(1 / 3)
        weights_exp[2, ...] = (dr * radii[0]**2 * 1)**(1 / 3)
        weights_exp[3, ...] = (dr * radii[1]**2 * 1)**(1 / 3)
        weights_exp[4, ...] = (dr * radii[0]**2 * 6)**(1 / 3)
        weights_exp[5, ...] = (dr * radii[1]**2 * 6)**(1 / 3)

        weights_exp *= (2 * (2 * np.pi ** 2 * cosmo.camb_params.InitPower.As) ** 2 \
                        * (3 / 5)) ** (1 / 3)

        signs = np.sign(prim_shape.amps)
        weights_exp[0:2] *= signs[0] * np.abs(prim_shape.amps[0])**(1 / 3)
        weights_exp[2:4] *= signs[1] * np.abs(prim_shape.amps[1])**(1 / 3)
        weights_exp[4:6] *= signs[2] * np.abs(prim_shape.amps[2])**(1 / 3)

        np.testing.assert_array_almost_equal(red_bisp.weights, weights_exp)

        # Manually compute reduced bispec factors for given r, ell.
        k = cosmo.transfer['k']
        tr_ell_k = cosmo.transfer['tr_ell_k']  # (nell, nk, npol).
        ells_sparse = cosmo.transfer['ells']

        lidx = 40  # Randomly picked.
        pidx = 1  # Look at E-mode.
        ridx = 1
        ell = ells_sparse[lidx]
        radius = radii[ridx]
        func = k**-3  # Look at second shape function.
        cidx = 1  # Second shape function.

        integrand = k**2 * spherical_jn(ell, radius * k)
        integrand *= tr_ell_k[lidx, :, pidx] * func
        ans_expec = (2 / np.pi) * np.trapz(integrand, k)

        lidx_full = ell - 2
        ans = red_bisp.factors[cidx * nr + ridx, pidx, lidx_full]
        self.assertAlmostEqual(ans, ans_expec, places=6)
Exemple #9
0
    def test_shape_prim_orthogonal_name(self):

        name = 'myname'
        orthogonal = Shape.prim_orthogonal(name=name)
        self.assertEqual(orthogonal.name, name)
Exemple #10
0
    def test_shape_prim_equilateral_name(self):

        name = 'myname'
        equilateral = Shape.prim_equilateral(name=name)
        self.assertEqual(equilateral.name, name)
Exemple #11
0
    def test_shape_prim_local_name(self):

        name = 'myname'
        local = Shape.prim_local(name=name)
        self.assertEqual(local.name, name)