Skip to content

Simulating Data

cmb_lensing.simulate.load_sim

load_sim(nside, theta_pix, pol, master_seed, uk_arcmin_t=3, H0=None, ombh2=0.0224567, omch2=0.118489, cosmomc_theta=0.0104098, r=0.0, mnu=DEFAULT_MNU, tau=DEFAULT_TAUREIO, As=jnp.exp(3.043) * 1e-10, nt=0, ns=0.968602, lmax=4000, begin_ell_interp=None, l_knee=100, k_pivot=DEFAULT_K_PIVOT, Alens=DEFAULT_A_LENSE, nphi_fac=2, a_phi=1, precomputed_cls=None, max_l_tensor=DEFAULT_MAX_L_TENSOR, max_eta_k_tensor=DEFAULT_MAX_ETA_K_TENSOR, use_mask=False)

Simulate a lensed CMB data set on a flat nside x nside sky patch.

Runs CAMB for the requested cosmology, builds the field, phi and noise covariances in Fourier space, draws the unlensed field(s), phi and the noise, lenses the field with LenseFlow and returns everything in one data set object. CAMB runs inside jax.pure_callback, so a CAMB failure gives NaN spectra rather than an exception.

Randomness: one root key PRNGKey(master_seed), and every random field (phi, then the unlensed field(s), then the noise) splits its own subkey off it in that fixed order (field_from_covar). So the realization at a seed depends on the cosmology only through the covariances the white noise is scaled by.

Parameters:

Name Type Description Default
nside

Number of pixels on a side.

required
theta_pix

Pixel width in arcmin.

required
pol

"I" (temperature only), "P" (E/B polarization only) or "IP" (both).

required
master_seed

Root seed of every random draw.

required
uk_arcmin_t

White noise level in temperature, in uK-arcmin. The polarization noise is sqrt(2) higher.

3
H0

Hubble constant. Leave as None to set the expansion through cosmomc_theta.

None
ombh2

Physical baryon density.

0.0224567
omch2

Physical cold dark matter density.

0.118489
cosmomc_theta

CosmoMC's approximation to the angular size of the sound horizon (the sampler's theta_MC_100 divided by 100).

0.0104098
r

Tensor-to-scalar ratio.

0.0
mnu

Sum of the neutrino masses in eV.

DEFAULT_MNU
tau

Optical depth to reionization.

DEFAULT_TAUREIO
As

Primordial scalar amplitude (the sampler's logA is log(1e10 As)).

exp(3.043) * 1e-10
nt

Tensor spectral index.

0
ns

Scalar spectral index.

0.968602
lmax

Largest multipole of the spectra.

4000
begin_ell_interp

Optional multipole past which the spectra are extrapolated rather than taken from CAMB.

None
l_knee

Knee of the 1/f noise term. 0 gives pure white noise, as every sampler entry point uses.

100
k_pivot

Pivot scale of the primordial spectra, in 1/Mpc.

DEFAULT_K_PIVOT
Alens

CAMB's lensing amplitude.

DEFAULT_A_LENSE
nphi_fac

Factor the quadratic-estimate noise is divided by (CMBLensing.jl's preconditioning heuristic).

2
a_phi

Amplitude multiplying the phi covariance.

1
precomputed_cls

Optional dict of CAMB spectra to use instead of running CAMB, for callers that build many realizations at one cosmology. It must be in _extract_all_cls's format (e.g. the output of fisher_forecast.camb_cls_at_params) and must belong to the same cosmology as the keyword arguments, which are then unused.

None
max_l_tensor

CAMB's largest multipole for the tensor spectra.

DEFAULT_MAX_L_TENSOR
max_eta_k_tensor

CAMB's largest k * eta for the tensor spectra.

DEFAULT_MAX_ETA_K_TENSOR
use_mask

Apply a sky mask. False (the default) uses a mask of all ones.

False

Returns:

Type Description

A DataSetT (pol = "I"), DataSetEB (pol = "P") or DataSetTEB (pol = "IP")

holding the fields (unlensed_field, lensed_field, phi, data) and the

operators (field_covariance, phi_covariance, noise_covariance, mask,

beam, mixing_d, mixing_g, quadratic_estimate, ...). All fields are in

Fourier space.

Example
from cmb_lensing.simulate import load_sim
data_set = load_sim(nside = 128, theta_pix = 2.5, pol = "IP", master_seed = 67,
                    uk_arcmin_t = 5, l_knee = 0)
Source code in cmb_lensing/simulate.py
def load_sim(nside, theta_pix, pol, master_seed, uk_arcmin_t = 3, H0 = None,
             ombh2 = 0.0224567, omch2 = 0.118489, cosmomc_theta = 0.0104098,
             r = 0.0, mnu = DEFAULT_MNU, tau = DEFAULT_TAUREIO, As = jnp.exp(3.043) * 1e-10,
             nt = 0, ns = 0.968602, lmax = 4000, begin_ell_interp = None, l_knee = 100,
             k_pivot = DEFAULT_K_PIVOT, Alens = DEFAULT_A_LENSE, nphi_fac = 2, a_phi = 1,
             precomputed_cls = None, max_l_tensor = DEFAULT_MAX_L_TENSOR,
             max_eta_k_tensor = DEFAULT_MAX_ETA_K_TENSOR, use_mask = False):
    """Simulate a lensed CMB data set on a flat nside x nside sky patch.

    Runs CAMB for the requested cosmology, builds the field, phi and noise covariances in
    Fourier space, draws the unlensed field(s), phi and the noise, lenses the field with
    LenseFlow and returns everything in one data set object. CAMB runs inside
    `jax.pure_callback`, so a CAMB failure gives NaN spectra rather than an exception.

    Randomness: one root key PRNGKey(master_seed), and every random field (phi, then the
    unlensed field(s), then the noise) splits its own subkey off it in that fixed order
    (field_from_covar). So the realization at a seed depends on the cosmology only through
    the covariances the white noise is scaled by.

    Args:
        nside: Number of pixels on a side.
        theta_pix: Pixel width in arcmin.
        pol: "I" (temperature only), "P" (E/B polarization only) or "IP" (both).
        master_seed: Root seed of every random draw.
        uk_arcmin_t: White noise level in temperature, in uK-arcmin. The polarization
            noise is sqrt(2) higher.
        H0: Hubble constant. Leave as None to set the expansion through `cosmomc_theta`.
        ombh2: Physical baryon density.
        omch2: Physical cold dark matter density.
        cosmomc_theta: CosmoMC's approximation to the angular size of the sound horizon
            (the sampler's theta_MC_100 divided by 100).
        r: Tensor-to-scalar ratio.
        mnu: Sum of the neutrino masses in eV.
        tau: Optical depth to reionization.
        As: Primordial scalar amplitude (the sampler's logA is log(1e10 As)).
        nt: Tensor spectral index.
        ns: Scalar spectral index.
        lmax: Largest multipole of the spectra.
        begin_ell_interp: Optional multipole past which the spectra are extrapolated
            rather than taken from CAMB.
        l_knee: Knee of the 1/f noise term. 0 gives pure white noise, as every sampler
            entry point uses.
        k_pivot: Pivot scale of the primordial spectra, in 1/Mpc.
        Alens: CAMB's lensing amplitude.
        nphi_fac: Factor the quadratic-estimate noise is divided by (CMBLensing.jl's
            preconditioning heuristic).
        a_phi: Amplitude multiplying the phi covariance.
        precomputed_cls: Optional dict of CAMB spectra to use instead of running CAMB, for
            callers that build many realizations at one cosmology. It must be in
            `_extract_all_cls`'s format (e.g. the output of
            `fisher_forecast.camb_cls_at_params`) and must belong to the same cosmology as
            the keyword arguments, which are then unused.
        max_l_tensor: CAMB's largest multipole for the tensor spectra.
        max_eta_k_tensor: CAMB's largest k * eta for the tensor spectra.
        use_mask: Apply a sky mask. False (the default) uses a mask of all ones.

    Returns:
        A `DataSetT` (pol = "I"), `DataSetEB` (pol = "P") or `DataSetTEB` (pol = "IP")
        holding the fields (`unlensed_field`, `lensed_field`, `phi`, `data`) and the
        operators (`field_covariance`, `phi_covariance`, `noise_covariance`, `mask`,
        `beam`, `mixing_d`, `mixing_g`, `quadratic_estimate`, ...). All fields are in
        Fourier space.

    Example:
        ```python
        from cmb_lensing.simulate import load_sim
        data_set = load_sim(nside = 128, theta_pix = 2.5, pol = "IP", master_seed = 67,
                            uk_arcmin_t = 5, l_knee = 0)
        ```
    """

    lmax_prime = min(lmax, DEFAULT_MAX_ELL) if begin_ell_interp is None else min(lmax, begin_ell_interp)

    if precomputed_cls is None:
        unlensed_scalar, tensor, total, lens_potential = _camb_via_callback(
            H0, ombh2, omch2, cosmomc_theta, r, mnu, tau, As, nt, ns,
            lmax_prime, k_pivot, Alens, max_l_tensor, max_eta_k_tensor
        )
        cls = _extract_all_cls(unlensed_scalar, tensor, total, lens_potential, lmax, lmax_prime)
    else:
        cls = _validate_precomputed_cls(precomputed_cls, lmax)

    ell_grid, pix_width = gen_ell_grid(nside, theta_pix)
    #one root key; every draw below splits its own subkey off it (field_from_covar). The draw
    #ORDER (phi, the field(s), the noise) is fixed and independent of the cosmology, so two
    #calls at the same seed share every white-noise realization - the common random numbers
    #delensed_covariance.py's stencil relies on
    rng_key = jax.random.PRNGKey(master_seed)
    ells = jnp.arange(2, lmax).astype(jnp.float64)

    #ells arrays matched to the cls lengths: the emulator override returns ells
    #2..lmax inclusive while the CAMB dl2cl path returns 2..lmax-1
    phi_ells = jnp.arange(2, 2 + cls["phi"].shape[0]).astype(jnp.float64)

    #Lensing potential
    unscaled_cphi = covar_matrix_from_cls(nside, pix_width, ell_grid,
                                          phi_ells,
                                          cls["phi"], origin_value=0)
    cphi = a_phi * unscaled_cphi
    phi, rng_key = field_from_covar(nside, cphi, rng_key)

    #Unlensed field covariances (scalar + tensor). Unlensed scalar BB (identically zero
    #at r = 0) cannot go through the log-interpolating covariance builder (log(0) -> NaN)
    #so _covar_or_zeros maps it to an exactly zero matrix. TE crosses zero, so it is
    #built from the correlation ratio rho = Cl_TE / sqrt(Cl_TT * Cl_EE) instead
    #(te_covar_from_rho): linear interpolation of rho onto the ell grid, scaled by
    #sqrt(cf_tt * cf_ee), which keeps the T/E block positive semi-definite by
    #construction and matched to the sampler's grid-spline TE path
    cf = {}
    cf_scalar = {}
    cf_tensor = {}
    pol_comps = ("TT",) if pol == "I" else ("TT", "EE", "BB")
    for comp in pol_comps:
        scalar_ells = jnp.arange(2, 2 + cls[f"scalar_{comp}"].shape[0]).astype(jnp.float64)
        cf_scalar[comp] = _covar_or_zeros(nside, pix_width, ell_grid,
                                          scalar_ells,
                                          cls[f"scalar_{comp}"], origin_value=0)
        cf_tensor[comp] = _covar_or_zeros(nside, pix_width, ell_grid, ells,
                                          cls[f"tensor_{comp}"], origin_value=0)
        cf[comp] = cf_scalar[comp] + cf_tensor[comp]
    if pol != "I":
        scalar_ells = jnp.arange(2, 2 + cls["scalar_TE"].shape[0]).astype(jnp.float64)
        rho_te = te_rho_from_cls(cls["scalar_TE"], cls["scalar_TT"], cls["scalar_EE"])
        cf["TE"] = cf_scalar["TE"] = te_covar_from_rho(rho_te, scalar_ells, ell_grid,
                                                       cf["TT"], cf["EE"])
        cf_tensor["TE"] = jnp.zeros((nside, nside // 2 + 1))

    #Lensed field covariances (TE via the same correlation-ratio route)
    cfl = {comp: covar_matrix_from_cls(nside, pix_width, ell_grid, ells,
                                       cls[f"total_{comp}"], origin_value=0)
           for comp in ("TT", "EE", "BB")}
    rho_te_lensed = te_rho_from_cls(cls["total_TE"], cls["total_TT"], cls["total_EE"])
    cfl["TE"] = te_covar_from_rho(rho_te_lensed, ells, ell_grid, cfl["TT"], cfl["EE"])

    #Unlensed random fields. T and E are drawn CORRELATED to match the model's TE block:
    #E ~ N(0, C_EE), then T = (C_TE / C_EE) E + t_indep with
    #t_indep ~ N(0, C_TT - C_TE^2 / C_EE), which gives exactly
    #Cov = [[C_TT, C_TE], [C_TE, C_EE]] (the conditional variance is
    #C_TT (1 - rho^2) >= 0 by the |rho| <= 1 construction; the maximum(0) guards
    #round-off only). The B draw against a zero covariance gives an exactly zero field
    if pol == "I":
        field_t, rng_key = field_from_covar(nside, cf["TT"], rng_key)
    else:
        conditional_tt = jnp.maximum(
            cf["TT"] - cf["TE"]**2 * reciprocal_matrix(cf["EE"]), 0.0)
        t_indep, rng_key = field_from_covar(nside, conditional_tt, rng_key)
        field_e, rng_key = field_from_covar(nside, cf["EE"], rng_key)
        field_b, rng_key = field_from_covar(nside, cf["BB"], rng_key)
        te_ratio = cf["TE"] * reciprocal_matrix(cf["EE"])
        field_t = jfft.irfft2(te_ratio * jfft.rfft2(field_e)) + t_indep

    #Lensing
    if pol == "I":
        lensed_t = jfft.rfft2(primal_lense_flow(
            field_t, phi, pix_width, n=10, direction=FORWARD_LENSE, adjoint=False
        ))
    else:
        lensed_t, lensed_e, lensed_b = _lens_fields(
            field_t, field_e, field_b, phi, pix_width, nside, theta_pix
        )

    beam = get_beam(nside, pix_width, ell_grid, lmax_prime)
    #NOTE we have removed the mask for LCDM inference
    mask = get_mask(DEFAULT_MASK_ELL, nside, pix_width, ell_grid) if use_mask else jnp.ones_like(beam)

    #Noise covariance and white noise (the TE noise Cls are identically zero, so
    #_covar_or_zeros keeps that block an exact zero matrix instead of NaN)
    n_tt, n_te, n_ee, n_bb = noise_cls(lmax_prime, uk_arcmin_t, l_knee = l_knee)
    ell_prime = jnp.arange(2, lmax_prime)
    cn = {}
    for comp, ncl in zip(("TT", "TE", "EE", "BB"), (n_tt, n_te, n_ee, n_bb)):
        cn[comp] = _covar_or_zeros(nside, pix_width, ell_grid, ell_prime, ncl, origin_value=0)

    wn_t, rng_key = field_from_covar(nside, cn["TT"], rng_key)
    if pol != "I":
        wn_e, rng_key = field_from_covar(nside, cn["EE"], rng_key)
        wn_b, rng_key = field_from_covar(nside, cn["BB"], rng_key)

    #Data = Mask * Beam * Lensed + Noise
    data_t = mask * beam * lensed_t + jfft.rfft2(wn_t)
    if pol != "I":
        data_e = mask * beam * lensed_e + jfft.rfft2(wn_e)
        data_b = mask * beam * lensed_b + jfft.rfft2(wn_b)

    #Convert unlensed fields to Fourier space
    field_t = jfft.rfft2(field_t)
    if pol != "I":
        field_e, field_b = jfft.rfft2(field_e), jfft.rfft2(field_b)

    #D matrix (full T/E block including TE; see get_d_teb_matrix for the identity B
    #block at r = 0)
    d_tt = get_d_tt_matrix(cf["TT"], cn["TT"])
    if pol != "I":
        d_tt, d_te, d_ee, d_bb = get_d_teb_matrix(cf["TT"], cf["TE"], cf["EE"], cf["BB"],
                                                  cn["TT"], cn["TE"], cn["EE"], cn["BB"])

    #Quadratic estimate
    if pol == "I":
        qe = scalar_quadratic_estimate(cn["TT"], cf["TT"], cfl["TT"],
                                       mask, beam, pix_width) / nphi_fac
    else:
        qe = polar_quadratic_estimate(cf["EE"], cf["BB"], cfl["EE"], cfl["BB"],
                                      cn["EE"], cn["BB"], mask, mask,
                                      beam, beam, pix_width) / nphi_fac

    #The G mixing matrix    
    g = get_g_matrix(cphi, qe, a_phi, a_phi = a_phi)

    #convert phi back to fourier space
    phi = jfft.rfft2(phi)
    #Build dataset for the requested polarization
    if pol == "I":
        return _build_dataset_t(
            nside, theta_pix, pix_width, cphi, unscaled_cphi, qe, phi,
            cf, cf_scalar, cf_tensor, cfl, cn, d_tt, g, mask, beam,
            field_t, lensed_t, data_t, r, a_phi
        )
    elif pol == "P":
        return _build_dataset_eb(
            nside, theta_pix, pix_width, cphi, unscaled_cphi, qe, phi,
            cf, cf_scalar, cf_tensor, cfl, cn, d_ee, d_bb, g, mask, beam,
            field_e, field_b, lensed_e, lensed_b, data_e, data_b, r, a_phi
        )
    else:
        return _build_dataset_teb(
            nside, theta_pix, pix_width, cphi, unscaled_cphi, qe, phi,
            cf, cf_scalar, cf_tensor, cfl, cn, d_tt, d_te, d_ee, d_bb, g, mask, beam,
            field_t, field_e, field_b,
            lensed_t, lensed_e, lensed_b,
            data_t, data_e, data_b, r, a_phi
        )