예제 #1
0
    ax.set_title(title)

    if filename is not None:
        plt.savefig(filename, dpi=150, bbox_inches="tight")

    return ax


if __name__ == "__main__":

    parser = argparse.ArgumentParser()
    parser.add_argument("model_name", type=str, help="The model name.")
    parsed_args = parser.parse_args()
    model_name = parsed_args.model_name

    grid = ParameterGrid(model_name)
    image_dir = grid.config["directories"]["images"]

    # Load metrics evaluated from faux observations
    dfms = grid.load_faux_metrics()

    # Get best hyper parameters for each faux sample
    df = []
    for dfm in dfms:
        best_pars = grid.get_best_hyper_parameters(df=dfm, flatten=True, kstest_min=None)
        df.append(best_pars)

    df = pd.DataFrame(df)

    # Plot hist for each parameter
    for parname in df.columns:
예제 #2
0
        result[key]["ybest"] = yybest[key]

    return result


if __name__ == "__main__":

    model_name = "blue_final"
    metric = "poisson_likelihood_2d"
    confs = None

    config = get_config()
    image_dir = os.path.join(config["directories"]["data"], "images")
    image_filename = os.path.join(image_dir, f"model_fit_{model_name}.png")

    grid = ParameterGrid(model_name)
    dfm = grid.load_best_sample(metric=metric)
    dfo = load_sample()

    keys = "uae_obs_jig", "rec_obs_jig"
    range = {k: r for k, r in zip(keys, RANGE)}
    dfs = grid.load_confident_samples(q=Q)
    dfbest = grid.load_best_sample()
    confs = get_confidence_intervals(dfs, dfbest, keys, range=range, bins=BINS1D)

    fig = plt.figure(figsize=FIGSIZE)

    spec = gridspec.GridSpec(ncols=10, nrows=2, figure=fig, hspace=0.35, wspace=1.6)
    ax1 = fig.add_subplot(spec[:, :4])
    ax2 = fig.add_subplot(spec[0, 4:])
    ax3 = fig.add_subplot(spec[1, 4:])
예제 #3
0
    CONFLINEKWARGS = {
        "linewidth": 1.5,
        "color": "springgreen",
        "linestyle": "--"
    }
    FONTSIZE = 14

    model_name = "blue_sedgwick_shen_final"
    xkey = "rec_phys_offset_alpha"
    ykey = "logmstar_a"
    zkey_noprior = "likelihood_kde_3d"
    zkey = "posterior_kde_3d"
    xlabel = r"$\beta$"
    ylabel = r"$\alpha$"

    grid = ParameterGrid(model_name)
    df = grid.load_metrics()
    metrics = grid.get_best_metrics(metric=zkey)

    x = df[xkey].values
    y = df[ykey].values
    znoprior = df[zkey_noprior].values
    z = df[zkey].values

    xrange = x.min(), 0.675
    yrange = -1.675, -1.225
    # xrange = x.min(), x.max()
    # yrange = y.min(), y.max()

    fig = plt.figure(figsize=(7, 7))
    spec = GridSpec(ncols=10, nrows=10, figure=fig)
예제 #4
0
                    linewidth=0.0)

    sigma = shen.logmstar_sigma(logmstar_ext)
    yy1 = np.log10(shen.apply_rec_offset(rec_mins, -sigma))
    yy2 = np.log10(shen.apply_rec_offset(rec_maxs, sigma))
    ax.fill_between(logmstar_ext,
                    yy1,
                    yy2,
                    color="dodgerblue",
                    alpha=0.2,
                    linewidth=0.0)


if __name__ == "__main__":

    grid = ParameterGrid(MODEL_NAME)
    dfg = load_gama_masses()
    model = Model(model_name=MODEL_NAME)

    alpha_mean, alpha_std = grid.parameter_stats("rec_phys_offset_alpha")

    grid_step = grid._grid_config["parameters"]["rec_phys_offset"]["alpha"][
        "step"]
    alpha_std = np.sqrt(alpha_std**2 + SYS_ERR_BETA**2 + grid_step**2)

    # fig, ax = plt.subplots(figsize=(9, 6))
    fig, ax = plt.subplots(figsize=(12, 6))

    plot_gama(ax)

    # Plot shen result
예제 #5
0
                  fontsize=fontsize)
    ax.set_ylabel(r"$\mathrm{PDF}$", fontsize=fontsize)

    if filename is not None:
        plt.savefig(filename, dpi=150, bbox_inches="tight")

    plt.show(block=False)


if __name__ == "__main__":

    n_samples = 10000
    burnin = 1000

    # Get best fitting hyper parameters
    grid = ParameterGrid(MODEL_NAME)

    # Get best fitting hyper parameters
    hyper_params = grid.get_best_hyper_parameters()

    # Sample the model with no recovery efficiency
    model = create_model(UDG_MODEL_NAME, ignore_recov=True)
    df = model.sample(burnin=burnin,
                      n_samples=n_samples,
                      hyper_params=hyper_params)

    # Identify UDGs
    cond = df["is_udg"].values == 1
    df = df[cond].reset_index(drop=True)

    # Get sizes
예제 #6
0
""" Code to evaluate a grid, replacing whatever metrics have already been calculated """

from udgsizes.fitting.grid import ParameterGrid

if __name__ == "__main__":

    pop_name = "blue"
    metrics_ignore = ["kstest_2d"]  # Skip because it takes ages
    nproc = 4

    p = ParameterGrid(pop_name)
    p.evaluate(metrics_ignore=metrics_ignore, nproc=nproc)
예제 #7
0
                        marker="o",
                        markersize=3,
                        zorder=10)

            pval = metrics[KSTEST_KEYS[key]]
            ax.text(0.22,
                    0.75,
                    r"$p_{KS}=$" + rf"{pval:.2f}",
                    transform=ax.transAxes,
                    fontsize=FONTSIZE - 1,
                    color="k")


if __name__ == "__main__":

    grid = ParameterGrid(MODEL_NAME)

    # fig = plt.figure(figsize=(5, 7.5))
    fig = plt.figure(figsize=(5, 6))

    ax_dict = {}
    for i, key in enumerate(PAR_NAMES):
        ax_dict[key] = plt.subplot(3, 2, i + 1)

        # Axes labels and tick formatting
        if i % 2 == 0:
            ax_dict[key].set_ylabel("PDF", fontsize=FONTSIZE)
            ax_dict[key].set_xlabel(LABELS[key], fontsize=FONTSIZE)

        ax_dict[key].set_xlabel(LABELS[key], fontsize=FONTSIZE)
        ax_dict[key].axes.yaxis.set_ticklabels([])
예제 #8
0
    y = pdf.rescale_observations(dfo, k2)

    ax.plot(x, y, "bo", markersize=3, alpha=0.4)
    ax.set_xlabel(k1)
    ax.set_ylabel(k2)

    plt.show(block=False)


if __name__ == "__main__":

    metric = "log_likelihood_kde_3d"
    # metric = "posterior"
    # metric = "posterior_ks"

    grid = ParameterGrid("blue_sedgwick_shen_final")
    df = grid.load_best_sample(metric=metric)

    dfo = load_sample()

    cond = df["selected_jig"].values == 1
    df = df[cond].reset_index(drop=True)

    # pdf = TransformedGaussianPDF(df, makeplots=MAKEPLOTS)
    pdf = TransformedKDE(df, makeplots=MAKEPLOTS)

    pvals = pdf.evaluate(dfo)
    print(np.log(pvals).sum())

    # if MAKEPLOTS:
    # pdf.summary_plot(dfo=dfo)
예제 #9
0
import argparse
from contextlib import suppress
import numpy as np

from udgsizes.fitting.grid import ParameterGrid
from udgsizes.fitting.interpgrid import InterpolatedGrid

METRICS_IGNORE = ["kstest_2d"]

if __name__ == "__main__":

    parser = argparse.ArgumentParser()
    parser.add_argument("model_name", type=str)
    parser.add_argument("--interpolate", action="store_true")

    args = parser.parse_args()
    model_name = args.model_name

    if args.interpolate:
        grid = InterpolatedGrid(model_name)
    else:
        grid = ParameterGrid(model_name)

    with np.errstate(divide='ignore'):  # Silence warnings
        grid.sample(overwrite=True)

    with suppress(AttributeError):
        grid.interpolate()

    grid.evaluate(metrics_ignore=METRICS_IGNORE)
예제 #10
0
    "rec_obs_jig": r"$\mathrm{\bar{r}_{e}\ [kpc]}$",
    "colour_obs": r"$(g-r)$"
}

OBSKEYS = {
    "uae_obs_jig": "mueff_av",
    "rec_obs_jig": "rec_arcsec",
    "colour_obs": "g_r"
}

# TODO: Add KS values to graphs
# TODO: Make into grid method / plotting utils

if __name__ == "__main__":

    grid = ParameterGrid(MODEL_NAME)
    df = grid.load_best_sample(apply_prior=True, select=True)
    df = df[df["selected_jig"].values == 1].reset_index(drop=True)

    dfo = load_sample(select=True)

    fig = plt.figure(figsize=(FIGHEIGHT * len(PAR_NAMES), FIGHEIGHT * 1.2))

    for i, par_name in enumerate(PAR_NAMES):

        ax = plt.subplot(1, len(PAR_NAMES), i + 1)
        values = df[par_name].values
        ax.hist(values, color="k", alpha=0.4, **HISTKWARGS)

        if par_name in OBSKEYS:
            values_obs = dfo[OBSKEYS[par_name]].values
예제 #11
0
""" Calulcate the observed dwarf / UDG fraction with uncertainties. """
import numpy as np

from udgsizes.fitting.grid import ParameterGrid

if __name__ == "__main__":

    model_name = "blue_sedgwick_shen_final"
    metric_name = "posterior_kde_3d"

    grid = ParameterGrid(model_name)
    df = grid.load_confident_metrics(metric=metric_name, q=0.9)

    weights = df[metric_name].values

    dwarf_fracs = df["n_dwarf"].values / df["n_selected"].values
    dwarf_frac_av = np.average(dwarf_fracs, weights=weights)
    dwarf_frac_std = np.sqrt(
        np.average((dwarf_fracs - dwarf_frac_av)**2, weights=weights))

    udg_fracs = df["n_udg"].values / df["n_selected"].values
    udg_frac_av = np.average(udg_fracs, weights=weights)
    udg_frac_std = np.sqrt(
        np.average((udg_fracs - udg_frac_av)**2, weights=weights))

    print(f"Dwarf fraction: {dwarf_frac_av:.2f} ± {dwarf_frac_std:.2f}")
    print(f"UDG fraction: {udg_frac_av:.2f} ± {udg_frac_std:.2f}")
예제 #12
0
CONFIG = get_config()
METRICS_IGNORE = ["kstest_2d"]
NITERS = 100
NPROC = 1

if __name__ == "__main__":

    parser = argparse.ArgumentParser()
    parser.add_argument("model_name", type=str, help="The model name.")

    parsed_args = parser.parse_args()
    model_name = parsed_args.model_name

    # Load best sample
    grid = ParameterGrid(model_name)

    # Make directory for output
    directory = os.path.join(grid.directory, "faux")
    os.makedirs(directory, exist_ok=True)

    # Calculate metrics
    for i in range(NITERS):

        filename = os.path.join(directory, f"metrics_{i}.csv")
        if os.path.isfile(filename):
            continue

        grid.logger.debug(f"Iteration {i+1} of {NITERS}.")

        dff = grid.make_faux_observations()
예제 #13
0
import argparse
from udgsizes.fitting.grid import ParameterGrid

KEYS_IGNORE = ["kstest_2d"]

if __name__ == "__main__":

    parser = argparse.ArgumentParser()
    parser.add_argument("model_name", type=str)
    parser.add_argument("metric", type=str)
    parser.add_argument("--thinning", type=int, default=None)

    args = parser.parse_args()
    model_name = args.model_name
    metric_name = args.metric
    thinning = args.thinning

    grid = ParameterGrid(model_name)

    KEYS_IGNORE.extend([k for k in grid._evaluator._metric_names if k != metric_name])
    keys_ignore = [k for k in KEYS_IGNORE if k != metric_name]

    values = []
    for i in range(grid.n_permutations):
        values.append(grid.evaluate_one(index=i, thinning=thinning,
                                        metrics_ignore=keys_ignore)[metric_name])

    df = grid.load_metrics()
    df[metric_name] = values
    df.to_csv(grid._metric_filename)
예제 #14
0
    model_name = "blue_sedgwick_shen_highkink"
    model_type = "udgsizes.model.sm_size.Model"

    config = get_config()
    config["grid"][model_type]["parameters"]["rec_phys_offset"]["alpha"][
        "max"] = 0.6
    config["grid"][model_type]["parameters"]["rec_phys_offset"]["alpha"][
        "step"] = 0.05
    config["grid"][model_type]["parameters"]["logmstar"]["a"]["min"] = -1.50
    config["grid"][model_type]["parameters"]["logmstar"]["a"]["max"] = -1.45
    config["grid"][model_type]["parameters"]["logmstar"]["a"]["step"] = 0.05

    metrics_ignore = ["kstest_2d"]  # Takes too long for whole grid
    n_samples = 500
    burnin = 250

    grid = ParameterGrid(model_name, config=config)

    if CHECK_INITIAL_VALUES:
        grid.check_initial_values()

    if SAMPLE:
        grid.sample(overwrite=True, n_samples=n_samples, burnin=burnin)

    grid.evaluate(metrics_ignore=metrics_ignore)
    dfm = grid.load_metrics()

    if MAKEPLOTS:
        grid.summary_plot()
        plt.show(block=False)