示例#1
0
    def test_create_tables_normal(self):
        method_list = methods.builtin_method_functions()
        cosmology_list = cosmologies.builtin_cosmology_functions()

        # Create a lookup table for each method and cosmology
        for method in method_list:
            for key in cosmology_list:
                here = os.getcwd()

                cosmo = cosmologies.builtin_cosmology_functions()[key]
                filename = "_".join(["pytest_output", method, key])
                table.create(method=method, filename=filename,
                             cosmo=cosmo, output_dir=here, zmin=0,
                             zmax=20, num_samples=10000)

                # Compare new tables to existing tables for 4 dm values
                pre_calc_fn = ".".join(["_".join([method, key]), "npz"])
                new_calc_fn = "".join([filename, ".npz"])

                pre_calc = table.load(pre_calc_fn)
                new_calc = table.load(new_calc_fn, data_dir=here)

                test_dm_list = [0, 100, 1000, 2000]

                for dm in test_dm_list:
                    new_z = table.get_z_from_table(dm, new_calc)
                    pre_z = table.get_z_from_table(dm, pre_calc)
                    assert new_z == pre_z
示例#2
0
    def test_create_table_zhang_free_elec_error(self):
        cosmo = cosmologies.builtin_cosmology_functions()["Planck18"]
        filename = "_".join(["pytest_output", "Zhang2018",
                             "Planck18", "free_elec_error"])

        with pytest.raises(ValueError):
            table.create(method="Zhang2018", filename=filename, cosmo=cosmo,
                         free_elec=-1)
示例#3
0
    def test_create_table_zhang_figm_free_elec(self):
        cosmo = cosmologies.builtin_cosmology_functions()["Planck18"]
        filename = "_".join(["pytest_output", "Zhang2018",
                             "Planck18", "figm_free_elec"])
        here = os.getcwd()

        table.create(method="Zhang2018", filename=filename, cosmo=cosmo,
                     output_dir=here, f_igm=0.5, free_elec=0.4)
示例#4
0
def cosmology_comparison(filename="",
                         extension="png",
                         usetex=False,
                         passed_ax=None,
                         **kwargs):
    """
    Create a plot comparing how the estimated redshift changes as a
    function of dispersion mesure for each cosmology.

    Parameters
    ----------
    filename: string, optional
    The filename of the saved figure. Default: "cosmology_comparison"

    extension: string, optional
        The format to save the figure. e.g "png", "pdf", "eps", etc...
        Default: "png"

    Generates
    ---------
    A figure displaying how estimated redshift changes as a function of
    dispersion measure for each of the different cosmologies.
    """
    # Update rcParams for consistent plotting style
    plt.rcParams.update(set_rc_params(usetex=usetex))

    if passed_ax:
        ax = passed_ax
    else:
        fig = plt.figure(figsize=(8, 8), constrained_layout=True)
        ax = fig.add_subplot(111)

    # Remove EAGLE from cosmologies since it is the same as Planck13
    cosmology_list = cosmologies.builtin_cosmology_functions()
    cosmology_list.pop("EAGLE")

    dm_vals = np.linspace(0, 3000, 1000)

    add_axin = True
    try:
        # Add inset plot showing the part where cosmologies diverge the most.
        axin = ax.inset_axes([0.05, 0.52, 0.45, 0.45])
    except Exception:
        print("""Skipping inset axis in cosmology plot. Requires Python 3 and
              Matplotlib 3.0""")
        add_axin = False

    colours = [
        '#a6cee3', '#1f78b4', '#b2df8a', '#33a02c', '#fb9a99', '#e31a1c'
    ]
    label = [
        r"$\rm{WMAP5}$", r"$\rm{WMAP7}$", r"$\rm{WMAP9}$", r"$\rm{Planck13}$",
        r"$\rm{Planck15}$", r"$\rm{Planck18}$"
    ]

    for j, cosmo in enumerate(cosmology_list):
        z_vals = np.zeros(len(dm_vals))
        if 'method' in kwargs:
            method = kwargs['method']
        else:
            method = 'Inoue2004'

        table_name = "{}.hdf5".format(method)
        table_name = utils.get_path_to_file_from_here(table_name,
                                                      subdirs=["data"])
        for i, dm in enumerate(dm_vals):
            z_vals[i] = table.get_z_from_table(dm, table_name, cosmo)

        ax.plot(dm_vals, z_vals, colours[j], label=label[j], **kwargs)
        if add_axin:
            axin.plot(dm_vals, z_vals, colours[j], **kwargs)

    ax.set_xlabel(r"$\rm{DM\ \left[pc \ cm^{-3}\right]}$")

    if not passed_ax:
        ax.set_ylabel(r"$\rm{Redshift}$")

    ax.legend(loc='lower right', frameon=False)

    if add_axin:
        axin.set_xlim(2800, 3000)
        axin.set_ylim(3.0, 3.25)
        axin.xaxis.set_tick_params(labelsize=8)
        axin.yaxis.set_tick_params(labelsize=8)

        ax.indicate_inset_zoom(axin)

    if filename != "":
        plt.savefig(".".join([filename, extension]))

    if passed_ax:
        return ax
    else:
        return fig
示例#5
0
    def test_create_table_zhang_figm_error(self):
        cosmo = cosmologies.builtin_cosmology_functions()["Planck18"]

        with pytest.raises(ValueError):
            table.create(method="Zhang2018", cosmo=cosmo, f_igm=-1)