コード例 #1
0
def test_power_spectra_lightcone(redshift, kwargs, module_direc):
    print("Options used for the test: ", kwargs)

    # First get pre-made data
    with h5py.File(prd.get_filename(redshift, **kwargs), "r") as f:
        power = f["power_lc"][...]
        xHI = f["xHI"][...]
        Tb = f["Tb"][...]

    with config.use(direc=module_direc, regenerate=False, write=True):
        with global_params.use(
                zprime_step_factor=prd.DEFAULT_ZPRIME_STEP_FACTOR):
            # Note that if zprime_step_factor is set in kwargs, it will over-ride this.
            k, p, lc = prd.produce_lc_power_spectra(redshift, **kwargs)

    assert np.allclose(power[:len(power) // 2],
                       p[:len(power) // 2],
                       atol=0,
                       rtol=1e-2)
    assert np.allclose(power[(len(power) // 2):],
                       p[(len(power) // 2):],
                       atol=0,
                       rtol=5e-2)

    assert np.allclose(xHI, lc.global_xH, atol=1e-5, rtol=1e-3)
    assert np.allclose(Tb, lc.global_brightness_temp, atol=1e-5, rtol=1e-3)
コード例 #2
0
def test_perturb_field_data(redshift, kwargs):
    print("Options used for the test: ", kwargs)

    # First get pre-made data
    with h5py.File(prd.get_filename_pt(redshift, **kwargs), "r") as f:
        power_dens = f["power_dens"][...]
        power_vel = f["power_vel"][...]
        pdf_dens = f["pdf_dens"][...]
        pdf_vel = f["pdf_vel"][...]

    with global_params.use(zprime_step_factor=prd.DEFAULT_ZPRIME_STEP_FACTOR):
        # Note that if zprime_step_factor is set in kwargs, it will over-ride this.
        (
            k_dens,
            p_dens,
            k_vel,
            p_vel,
            x_dens,
            y_dens,
            x_vel,
            y_vel,
            ic,
        ) = prd.produce_perturb_field_data(redshift, **kwargs)

    assert np.allclose(power_dens, p_dens, atol=5e-3, rtol=1e-3)
    assert np.allclose(power_vel, p_vel, atol=5e-3, rtol=1e-3)
    assert np.allclose(pdf_dens, y_dens, atol=5e-3, rtol=1e-3)
    assert np.allclose(pdf_vel, y_vel, atol=5e-3, rtol=1e-3)
コード例 #3
0
def test_power_spectra_coeval(name, module_direc, plt):
    redshift, kwargs = prd.OPTIONS[name]
    print(f"Options used for the test at z={redshift}: ", kwargs)

    # First get pre-made data
    with h5py.File(prd.get_filename("power_spectra", name), "r") as fl:
        true_powers = {
            "_".join(key.split("_")[1:]): value[...]
            for key, value in fl["coeval"].items() if key.startswith("power_")
        }

    # Now compute the Coeval object
    with config.use(direc=module_direc, regenerate=False, write=True):
        with global_params.use(
                zprime_step_factor=prd.DEFAULT_ZPRIME_STEP_FACTOR):
            # Note that if zprime_step_factor is set in kwargs, it will over-ride this.
            test_k, test_powers, _ = prd.produce_coeval_power_spectra(
                redshift, **kwargs)

    if plt == mpl.pyplot:
        make_coeval_comparison_plot(test_k, true_powers, test_powers, plt)

    for key, value in true_powers.items():
        print(f"Testing {key}")
        assert np.sum(
            ~np.isclose(value, test_powers[key], atol=0, rtol=1e-2)) < 10
        np.testing.assert_allclose(value, test_powers[key], atol=0, rtol=1e-1)
コード例 #4
0
def test_globals():
    orig = global_params.Z_HEAT_MAX

    with global_params.use(Z_HEAT_MAX=10.0):
        assert global_params.Z_HEAT_MAX == 10.0
        assert global_params._cobj.Z_HEAT_MAX == 10.0

    assert global_params.Z_HEAT_MAX == orig
コード例 #5
0
def test_power_spectra_coeval(redshift, kwargs, module_direc):
    print("Options used for the test: ", kwargs)

    # First get pre-made data
    with h5py.File(prd.get_filename(redshift, **kwargs), "r") as f:
        power = f["power_coeval"][...]

    with config.use(direc=module_direc, regenerate=False, write=True):
        with global_params.use(
                zprime_step_factor=prd.DEFAULT_ZPRIME_STEP_FACTOR):
            # Note that if zprime_step_factor is set in kwargs, it will over-ride this.
            k, p, bt = prd.produce_coeval_power_spectra(redshift, **kwargs)

    assert np.allclose(power, p, atol=1e-3, rtol=1e-2)
コード例 #6
0
def test_halo_field_data(redshift, kwargs):
    print("Options used for the test: ", kwargs)

    # First get pre-made data
    with h5py.File(prd.get_filename_halo(redshift, **kwargs), "r") as f:
        n_pt_halos = f["n_pt_halos"][...]
        pt_halo_masses = f["pt_halo_masses"][...]

    with global_params.use(zprime_step_factor=prd.DEFAULT_ZPRIME_STEP_FACTOR):
        # Note that if zprime_step_factor is set in kwargs, it will over-ride this.
        pt_halos = prd.produce_halo_field_data(redshift, **kwargs)

    assert np.allclose(n_pt_halos, pt_halos.n_halos, atol=5e-3, rtol=1e-3)
    assert np.allclose(np.sum(pt_halo_masses),
                       np.sum(pt_halos.halo_masses),
                       atol=5e-3,
                       rtol=1e-3)
コード例 #7
0
def test_power_spectra_lightcone(name, module_direc, plt):
    redshift, kwargs = prd.OPTIONS[name]
    print(f"Options used for the test at z={redshift}: ", kwargs)

    # First get pre-made data
    with h5py.File(prd.get_filename("power_spectra", name), "r") as fl:
        true_powers = {}
        true_global = {}
        for key in fl["lightcone"].keys():
            if key.startswith("power_"):
                true_powers["_".join(
                    key.split("_")[1:])] = fl["lightcone"][key][...]
            elif key.startswith("global_"):
                true_global[key] = fl["lightcone"][key][...]

    # Now compute the lightcone
    with config.use(direc=module_direc, regenerate=False, write=True):
        with global_params.use(
                zprime_step_factor=prd.DEFAULT_ZPRIME_STEP_FACTOR):
            # Note that if zprime_step_factor is set in kwargs, it will over-ride this.
            test_k, test_powers, lc = prd.produce_lc_power_spectra(
                redshift, **kwargs)

    if plt == mpl.pyplot:
        make_lightcone_comparison_plot(test_k, lc.node_redshifts, true_powers,
                                       true_global, test_powers, lc, plt)

    for key, value in true_powers.items():
        print(f"Testing {key}")
        # Ensure all but 10 of the values is within 1%, and none of the values
        # is outside 10%
        assert np.sum(
            ~np.isclose(value, test_powers[key], atol=0, rtol=1e-2)) < 10
        assert np.allclose(value, test_powers[key], atol=0, rtol=1e-1)

    for key, value in true_global.items():
        print(f"Testing Global {key}")
        assert np.allclose(value, getattr(lc, key), atol=0, rtol=1e-3)