コード例 #1
0
ファイル: load.py プロジェクト: dessn/sn-bhm
def debug_plots(std):
    print(std)

    res = load_stan_from_folder(std, merge=True, cut=False)
    chain, posterior, t, p, f, l, w, ow = res
    # print(w.mean())
    # import matplotlib.pyplot as plt
    # plt.hist(np.log(w), 100)
    # plt.show()
    # exit()
    logw = np.log(w)
    m = np.mean(logw)
    s = np.std(logw)
    print(m, s)
    logw -= (m + 3 * s)
    good = logw < 0
    logw *= good
    w = np.exp(logw)

    c = ChainConsumer()
    c.add_chain(chain, weights=w, name="corrected")
    c.configure(summary=True)
    c.plot(figsize=2.0, filename="output.png", parameters=9)

    c = ChainConsumer()
    c.add_chain(chain, name="uncorrected")
    c.add_chain(chain, weights=w, name="corrected")
    # c.add_chain(chain, name="calib")
    c.plot(filename="output_comparison.png", parameters=9, figsize=1.3)
    c.plot_walks(chains=1, filename="walks.png")
コード例 #2
0
ファイル: load.py プロジェクト: dessn/sn-bhm
def debug_plots(std):
    print(std)

    res = load_stan_from_folder(std, merge=True, cut=False)
    chain, posterior, t, p, f, l, w, ow = res
    print(w.mean(), np.std(w), np.mean(np.log(w)), np.std(np.log(w)))
    # import matplotlib.pyplot as plt
    # plt.hist(np.log(w), 100)
    # plt.show()
    # exit()

    logw = np.log(w)
    m = np.mean(logw)
    s = np.std(logw)
    print(m, s)
    logw -= (m + 3 * s)
    good = logw < 0
    logw *= good
    w = np.exp(logw)

    sorti = np.argsort(w)
    for key in chain.keys():
        chain[key] = chain[key][sorti]
    w = w[sorti]
    ow = ow[sorti]
    posterior = posterior[sorti]

    c = ChainConsumer()
    truth = [0.3, 0.14, 3.1, -19.365, 0, 0, 0.1, 1.0, 0.1, 0, 0, 0, 0, 0, 0]
    c.add_chain(chain, name="uncorrected", posterior=posterior)
    c.add_chain(chain, weights=w, name="corrected", posterior=posterior)
    c.plot(filename="output.png", parameters=9, truth=truth, figsize=1.3)
    # c = ChainConsumer()
    # c.add_chain(chain, weights=w, name="corrected")
    c.plot_walks(chains="corrected", filename="walks.png", truth=truth)
コード例 #3
0
ファイル: load.py プロジェクト: dessn/sn-bhm
def plot_all_no_weight(folder, output):
    """ Plot all chains as one, with and without weights applied """
    print("Plotting all as one, with old and new weights")
    chain, posterior, t, p, f, l, w, ow = load_stan_from_folder(folder, merge=True)
    c = ChainConsumer()
    c.add_chain(chain, posterior=posterior, walkers=l)
    c.plot(filename=output, truth=t, figsize=0.75)
コード例 #4
0
ファイル: ston_and_redshift.py プロジェクト: dessn/sn-bhm
def plot_results(chain, param, chainf, chainf2, paramf, t0, x0, x1, c, temp_dir, seed, interped):
    cc = ChainConsumer()
    cc.add_chain(chain, parameters=param, name="Posterior")
    cc.add_chain(chainf, parameters=paramf, name="Minuit")
    cc.add_chain(chainf2, parameters=paramf, name="Emcee")
    truth = {"$t_0$": t0, "$x_0$": x0, "$x_1$": x1, "$c$": c, r"$\mu$": get_mu(interped, x0, x1, c)}
    cc.plot(filename=temp_dir + "/surfaces_%d.png" % seed, truth=truth)
コード例 #5
0
ファイル: load.py プロジェクト: dessn/sn-bhm
def plot_single_cosmology_weight(folder, output, i=0):
    print("Plotting cosmology realisation %d" % i)
    res = load_stan_from_folder(folder, merge=False)
    c = ChainConsumer()
    chain, posterior, t, p, f, l, w, ow = res[i]
    c.add_chain(chain, posterior=posterior, walkers=l, name="Uncorrected %d"%i)
    c.add_chain(chain, weights=w, posterior=posterior, walkers=l, name="Corrected %d"%i)
    c.plot(filename=output, truth=t, figsize=0.75)
コード例 #6
0
ファイル: load.py プロジェクト: dessn/sn-bhm
def plot_separate(folder, output):
    """ Plot separate cosmologies """
    print("Plotting all cosmologies separately")
    res = load_stan_from_folder(folder, merge=False)
    c = ChainConsumer()
    for i, (chain, posterior, t, p, f, l, w, ow) in enumerate(res):
        c.add_chain(chain, weights=w, posterior=posterior, walkers=l, name="%d"%i)
    c.plot(filename=output, truth=t, figsize=0.75)
コード例 #7
0
ファイル: load.py プロジェクト: dessn/sn-bhm
def plot_all(folder, output, output_walk=None):
    """ Plot all chains as one """
    print("Plotting all as one")
    chain, posterior, t, p, f, l, w, ow = load_stan_from_folder(folder, merge=True)
    c = ChainConsumer()
    c.add_chain(chain, weights=w, posterior=posterior, walkers=l)
    c.plot(filename=output, truth=t, figsize=0.75)
    if output_walk is not None:
        c.plot_walks(filename=output_walk)
コード例 #8
0
ファイル: load.py プロジェクト: dessn/sn-bhm
def plot_separate_weight(folder, output):
    """ Plot separate cosmologies, with and without weights applied """
    print("Plotting all cosmologies separately, with old and new weights")
    res = load_stan_from_folder(folder, merge=False)
    c = ChainConsumer()
    ls = []
    for i, (chain, posterior, t, p, f, l, w, ow) in enumerate(res):
        c.add_chain(chain, posterior=posterior, walkers=l, name="Uncorrected %d"%i)
        c.add_chain(chain, weights=w, posterior=posterior, walkers=l, name="Corrected %d"%i)
        ls += ["-", "--"]
    c.configure_general(linestyles=ls)
    c.plot(filename=output, truth=t, figsize=0.75)
コード例 #9
0
ファイル: load.py プロジェクト: dessn/sn-bhm
def debug_plots(std):
    print(std)

    do_weight = True
    do_walk = True
    load_second = True

    res = load_stan_from_folder(std, merge=False, num=0, cut=False)
    chain, posterior, t, p, f, l, w, ow = res[0]

    if do_walk:
        # print("owww ", ow.min(), ow.max())
        # print("www ", w.min(), w.max())
        chain2 = chain.copy()
        logw = np.log10(w)
        a = np.argsort(logw)
        logw = logw[a]
        for k in chain:
            chain2[k] = chain2[k][a]
        chain2["ow"] = np.log10(ow)
        chain2["ww"] = logw
        c = ChainConsumer()
        c.add_chain(chain2, weights=w, name="calib")
        c.plot_walks(truth=t, filename="walk_new.png")

    c = ChainConsumer()
    if do_weight:
        c.add_chain(chain, weights=w, name="calib")
    else:
        c.add_chain(chain, name="calib")

    if load_second:
        res2 = load_stan_from_folder(std + "_calib_data_no_calib_model_and_bias", num=0, merge=False, cut=False)
        chain, posterior, _, p, f, l, w, ow = res2[0]

        if do_weight:
            c.add_chain(chain, weights=w, name="nocalib")
        else:
            c.add_chain(chain, name="nocalib")
    c.plot(filename="output.png", truth=t, figsize=0.75)
コード例 #10
0
from chainconsumer import ChainConsumer

if __name__ == "__main__":

    mcmc_file = '/home/s1/ynzhang/redmapper_centering/model_fc_June21/temp_combined/dep_lmdexp_exp_newsdss_nobins_noastro_ge20_lt300'
    rho_1 = np.genfromtxt(mcmc_file + '/Chain_0/Rho0.txt')
    r0 = np.genfromtxt(mcmc_file + '/Chain_0/R0.txt')
    tau = np.genfromtxt(mcmc_file + '/Chain_0/tau.txt')

    c = ChainConsumer()
    data = np.vstack((rho_1, r0, tau)).T
    c.add_chain(data,
                parameters=[r"$\rho$", r"$\sigma$", r"$\tau$"],
                name='SDSS')

    mcmc_file = '/home/s1/ynzhang/redmapper_centering/model_fc_June21/temp_combined/dep_lmdexp_exp_newy1a1_nobins_noastro_ge20_lt300'
    rho_1 = np.genfromtxt(mcmc_file + '/Chain_0/Rho0.txt')
    r0 = np.genfromtxt(mcmc_file + '/Chain_0/R0.txt')
    tau = np.genfromtxt(mcmc_file + '/Chain_0/tau.txt')

    data = np.vstack((rho_1, r0, tau)).T
    c.add_chain(data,
                parameters=[r"$\rho$", r"$\sigma$", r"$\tau$"],
                name='DES')

    c.configure(colors=['k', 'b'],
                linestyles=["-", "--"],
                shade=[True, False],
                shade_alpha=[0.5, 0.0])
    c.plot(filename="cornerplot.png", figsize="column")
コード例 #11
0
ファイル: load_stan.py プロジェクト: dessn/sn-bhm
import numpy as np
import pickle
import os
from chainconsumer import ChainConsumer

dir_name = os.path.dirname(__file__)
c = ChainConsumer()

for i in range(10):
    t = os.path.abspath(dir_name + "/output/temp%d.pkl" % i)
    with open(t, 'rb') as output:
        chain = pickle.load(output)
        chain = np.vstack((chain["mu"], chain["sigma"]))

    c.add_chain(chain.T, parameters=[r"$\mu$", r"$\sigma$"])
c.configure_bar(shade=True)
c.configure_contour(sigmas=[0, 0.01, 1, 2], contourf=True, contourf_alpha=0.1)
c.plot(display=True, truth=[100, 20])
コード例 #12
0
ファイル: simple.py プロジェクト: dessn/sn-bhm
    c.add_chain(np.random.multivariate_normal(res.parameters[1:], res.covariance, size=int(1e7)),
                name="Summary Stats", parameters=["$t_0$", "$x_0$", "$x_1$", "$c$"])

    if False:
        if not os.path.exists(mcmc_chain):
            res2, fitted_model2 = sncosmo.mcmc_lc(lcs[0], model, ['t0', 'x0', 'x1', 'c'], nwalkers=20,
                                                  nburn=500, nsamples=4000)
            mcchain = res2.samples
            np.save(mcmc_chain, mcchain)
        else:
            mcchain = np.load(mcmc_chain)
        c.add_chain(mcchain, name="sncosmo mcmc", parameters=["$t_0$", "$x_0$", "$x_1$", "$c$"])
    print("Plot surfaces")
    c.configure_contour(shade=True, shade_alpha=0.2, sigmas=[0.0, 1.0, 2.0, 3.0])
    c.configure_bar(shade=True)
    c.plot(filename=surface, figsize=(7, 7))
    if False:
        fig = sncosmo.plot_lc(lcs[0], model=fitted_model, errors=res.errors)
        fig.savefig(temp_dir + os.sep + "lc_simple.png", bbox_inches="tight", dpi=300)

    alpha = 0.14
    beta = 3.15

    c2 = ChainConsumer()
    means = []
    stds = []
    print("Add chains")
    for i in range(len(c.chains)):
        chain = c.chains[i]
        apparent_interp = generate_and_return()
        x0s = chain[:, c.parameters[i].index("$x_0$")]
コード例 #13
0
Truncated Parameters
====================

If you have a large model, you don't need to plot all parameters at once.

Here we only plot the first four parameters. You could also simply pass the number four,
which means the *first* four parameters.

For fun, we also plot everything in green. Note you don't need to give multiple colours,
the shading is all computed from the colour hex code.
"""

import numpy as np
from numpy.random import normal, random, multivariate_normal
from chainconsumer import ChainConsumer

if __name__ == "__main__":
    np.random.seed(0)
    cov = random(size=(6, 6))
    data = multivariate_normal(normal(size=6),
                               0.5 * (cov + cov.T),
                               size=200000)
    parameters = ["$x$", "$y$", "$z$", "$a$", "$b$", "$c$"]
    c = ChainConsumer().add_chain(
        data, parameters=parameters).configure_general(colours=["#388E3C"])
    fig = c.plot(parameters=parameters[:4], figsize="page")

    fig.set_size_inches(
        2.5 +
        fig.get_size_inches())  # Resize fig for doco. You don't need this.
コード例 #14
0
ファイル: get_da_hz.py プロジェクト: Samreay/Wigglez
    
    hrc = hs * rs_fid / (alpha * (1 + epsilon) * (1 + epsilon)) / c
    res = np.vstack((omch2, daval, z/hrc)).T
    return res
    
p1 = [r"$\Omega_c h^2$", r"$\alpha$", r"$\epsilon$"]
p2 = [r"$\Omega_c h^2$", r"$D_A(z)/r_s$", r"$cz/H(z)/r_s $"]


if False:
    consumer = ChainConsumer()
    consumer.configure_contour(sigmas=[0,1.3])
    consumer.add_chain(load_directory("../bWigMpBin/bWigMpBin_z0"), parameters=p1, name="$0.2<z<0.6$")
    consumer.add_chain(load_directory("../bWigMpBin/bWigMpBin_z1"), parameters=p1, name="$0.4<z<0.8$")
    consumer.add_chain(load_directory("../bWigMpBin/bWigMpBin_z2"), parameters=p1, name="$0.6<z<1.0$")
    consumer.plot(figsize="column", filename="wigglez_multipole_alphaepsilon.pdf", truth=[0.113, 1.0, 0.0])
    print(consumer.get_latex_table())

if True:
    c = ChainConsumer()
    c.configure_contour(sigmas=[0,1,2])
    c.add_chain(convert_directory("../bWigMpBin/bWigMpBin_z0", 0.44), parameters=p2, name="$0.2<z<0.6$")
    c.add_chain(convert_directory("../bWigMpBin/bWigMpBin_z1", 0.60), parameters=p2, name="$0.4<z<0.8$")
    c.add_chain(convert_directory("../bWigMpBin/bWigMpBin_z2", 0.73), parameters=p2, name="$0.6<z<1.0$")
    print(c.get_latex_table())
    #c.plot(figsize="column", filename="wigglez_multipole_dah.pdf")

if False:
    c = ChainConsumer()
    c.configure_contour(sigmas=[0,1,2])
    c.add_chain(convert_directory("../bWizMpMeanBin/bWizMpMeanBin_z0", 0.44), parameters=p2, name="$0.2<z<0.6$")
コード例 #15
0
ファイル: general.py プロジェクト: dessn/sn-bhm
def random_obs(temp_dir, seed):
    np.random.seed(seed)
    interp = generate_and_return()
    x1 = np.random.normal()
    # colour = np.random.normal(scale=0.1)
    colour = 0
    x0 = 1e-5
    # t0 = np.random.uniform(low=1000, high=2000)
    t0 = 1000
    z = np.random.uniform(low=0.1, high=1.0)

    # deltat = np.random.uniform(low=-20, high=0)
    # num_obs = np.random.randint(low=10, high=40)
    num_obs = 20
    deltat = -35

    filename = temp_dir + "/save_%d.npy" % seed

    if not os.path.exists(filename):
        ts = np.arange(t0 + deltat, (t0 + deltat) + 5 * num_obs, 5)

        times = np.array([[t, t + 0.05, t + 0.1, t + 0.2] for t in ts]).flatten()
        bands = [b for t in ts for b in ["desg", "desr", "desi", "desz"]]
        gains = np.ones(times.shape)
        skynoise = np.random.uniform(low=20, high=800) * np.ones(times.shape)
        zp = 30 * np.ones(times.shape)
        zpsys = ["ab"] * times.size

        obs = Table({"time": times, "band": bands, "gain": gains, "skynoise": skynoise, "zp": zp, "zpsys": zpsys})
        model = sncosmo.Model(source="salt2")
        p = {"z": z, "t0": t0, "x0": x0, "x1": x1, "c": colour}
        model.set(z=z)
        print(seed, " Vals are ", p)
        lc = sncosmo.realize_lcs(obs, model, [p])[0]
        ston = (lc["flux"] / lc["fluxerr"]).max()

        model.set(t0=t0, x1=x1, c=colour, x0=x0)
        try:
            res, fitted_model = sncosmo.fit_lc(
                lc, model, ["t0", "x0", "x1", "c"], guess_amplitude=False, guess_t0=False
            )
        except ValueError:
            return np.nan, np.nan, x1, colour, num_obs, ston, deltat, z, 0

        fig = sncosmo.plot_lc(lc, model=fitted_model, errors=res.errors)
        fig.savefig(temp_dir + os.sep + "lc_%d.png" % seed, bbox_inches="tight", dpi=300)
        my_model = PerfectRedshift([lc], [z], t0, name="posterior%d" % seed)
        sampler = EnsembleSampler(temp_dir=temp_dir, num_burn=400, num_steps=1500)
        c = ChainConsumer()
        my_model.fit(sampler, chain_consumer=c)
        map = {"x0": "$x_0$", "x1": "$x_1$", "c": "$c$", "t0": "$t_0$"}
        parameters = [map[a] for a in res.vparam_names]

        mu1 = get_mu_from_chain(interped, c.chains[-1], c.parameters[-1])
        c.parameteers[-1].append(r"$\mu$")
        c.chains[-1] = np.hstack((c.chains[-1], mu1[:, None]))

        chain2 = np.random.multivariate_normal(res.parameters[1:], res.covariance, size=int(1e5))
        chain2 = np.hstack((chain2, get_mu_from_chain(interp, chain2, parameters)[:, None]))
        c.add_chain(chain2, parameters=parameters, name="Gaussian")
        figfilename = filename.replace(".npy", ".png")
        c.plot(filename=figfilename, truth={"$t_0$": t0, "$x_0$": x0, "$x_1$": x1, "$c$": colour})

        means = []
        stds = []
        isgood = (
            (np.abs(x1 - res.parameters[3]) < 4) & (np.abs(colour - res.parameters[4]) < 2) & (res.parameters[2] > 0.0)
        )
        isgood *= 1.0

        for i in range(len(c.chains)):
            a = c.chains[i][:, -1]
            means.append(a.mean())
            stds.append(np.std(a))
        diffmu = np.diff(means)[0]
        diffstd = np.diff(stds)[0]
        np.save(filename, np.array([diffmu, diffstd, ston, 1.0 * isgood]))

    else:
        vals = np.load(filename)
        diffmu = vals[0]
        diffstd = vals[1]
        ston = vals[2]
        isgood = vals[3]

    return diffmu, diffstd, x1, colour, num_obs, ston, deltat, z, isgood
コード例 #16
0
"""
========================
Gaussian KDE and Extents
========================

Smooth marginalised distributions with a Gaussian KDE, and pick custom extents.


Note that invoking the KDE on large data sets will significantly increase rendering time.
Also note that you can only invoke KDE on chains without varying weights. This limitation will
be lifted as soon as statsmodel, scipy or scikit-learn add a weighted Gaussian KDE.

Also note that if you pass a floating point number to bins, it multiplies the default bin size
(which is a function of number of steps in the chain) by that amount. If you give it an integer,
it will use that many bins.
"""

import numpy as np
from chainconsumer import ChainConsumer

if __name__ == "__main__":
    data = np.random.multivariate_normal([0.0, 4.0], [[1.0, 0.7], [0.7, 1.5]], size=50000)

    c = ChainConsumer()
    c.add_chain(data)
    c.configure_general(bins=0.9, kde=True)
    c.plot(extents=[(-2, 4), (0, 10)])
コード例 #17
0
ファイル: get_da_hz.py プロジェクト: Samreay/Wigglez
    
    daval = (alpha/(1+epsilon)) * da #/ rs_fid
    
    hrc = hs / (alpha * (1 + epsilon) * (1 + epsilon)) #* rs_fid / (alpha * (1 + epsilon) * (1 + epsilon)) / c
    res = np.vstack((omch2, daval, hrc)).T
    return res
    
    
    
if False:
    consumer = ChainConsumer()
    consumer.configure_contour(sigmas=[0,1,2])
    consumer.add_chain(load_directory("../bWigMpBin/bWigMpBin_z0"), parameters=[r"$\Omega_c h^2$", r"$\alpha$", r"$\epsilon$"], name="$0.2<z<0.6$")
    consumer.add_chain(load_directory("../bWigMpBin/bWigMpBin_z1"), parameters=[r"$\Omega_c h^2$", r"$\alpha$", r"$\epsilon$"], name="$0.4<z<0.8$")
    consumer.add_chain(load_directory("../bWigMpBin/bWigMpBin_z2"), parameters=[r"$\Omega_c h^2$", r"$\alpha$", r"$\epsilon$"], name="$0.6<z<1.0$")
    consumer.plot(figsize="column", filename="wigglez_multipole_alphaepsilon.pdf")
    #print(consumer.get_latex_table())

if True:
    c = ChainConsumer()
    c.add_chain(convert_directory("../bWigMpBin/bWigMpBin_z0", 0.44), parameters=[r"$\Omega_c h^2$", r"$D_A(z)$", r"$H(z)$"], name="$0.2<z<0.6$")
    c.add_chain(convert_directory("../bWigMpBin/bWigMpBin_z1", 0.60), parameters=[r"$\Omega_c h^2$", r"$D_A(z)$", r"$H(z)$"], name="$0.4<z<0.8$")
    c.add_chain(convert_directory("../bWigMpBin/bWigMpBin_z2", 0.73), parameters=[r"$\Omega_c h^2$", r"$D_A(z)$", r"$H(z)$"], name="$0.6<z<1.0$")
    for n, chain in zip(c.names, c.chains):
        print(n)
        print(chain.mean(axis=0))
        print(np.std(chain, axis=0))
        print("----")
    c.configure_contour(sigmas=[0,1,2])
    c.configure_general(bins=0.7)    
    c.plot(figsize="column", filename="wigglez_multipole_dah.pdf")
コード例 #18
0
ファイル: efficiency_model_1.py プロジェクト: dessn/sn-bhm
    print(mask.sum(), n, observed.mean())
    return mean, observed, errors, alpha


if __name__ == "__main__":
    logging.basicConfig(level=logging.DEBUG)
    dir_name = os.path.dirname(__file__)
    t = os.path.abspath(dir_name + "/output/run_%d")
    plot_file = os.path.abspath(dir_name + "/output/surfaces.png")
    walk_file = os.path.abspath(dir_name + "/output/walk_%s.png")

    c = ChainConsumer()
    n = 3
    colours = ["#D32F2F", "#1E88E5"] * n
    for i in range(n):
        mean, observed, errors, alpha = get_data(seed=i)

        model_un = EfficiencyModelUncorrected(observed, errors)
        model_cor = EfficiencyModelCorrected(observed, errors, alpha)

        pgm_file = os.path.abspath(dir_name + "/output/pgm.png")
        fig = model_cor.get_pgm(pgm_file)

        sampler = EnsembleSampler(num_steps=5000, num_burn=1000, temp_dir=t % i, num_walkers=50)
        model_un.fit(sampler, chain_consumer=c)
        model_cor.fit(sampler, chain_consumer=c)

    c.configure_bar(shade=True)
    c.configure_general(colours=colours)
    c.plot(filename=plot_file, figsize=(5, 3), truth=[mean], legend=False)
コード例 #19
0
ファイル: efficiency_model_3.py プロジェクト: dessn/sn-bhm
    for i in range(n):
        mean, std, observed, errors, alpha, actual, uo, oe, am = get_data(seed=i)
        theta_good = [mean, std] + actual.tolist()
        theta_bias = [mean, std] + am.tolist()
        kwargs = {"num_steps": 70000, "num_burn": 20000, "save_interval": 300,
                  "plot_covariance": True, "unify_latent": True}  # , "callback": v.callback
        sampler = BatchMetropolisHastings(num_walkers=w, kwargs=kwargs, temp_dir=t % i, num_cores=4)

        model_good = EfficiencyModelUncorrected(uo, oe, name="Good%d" % i)
        model_good.fit(sampler, chain_consumer=c)
        print("Good ", model_good.get_log_posterior(theta_good), c.posteriors[-1][-1])

        model_un = EfficiencyModelUncorrected(observed, errors, name="Uncorrected%d" % i)
        model_un.fit(sampler, chain_consumer=c)
        print("Uncorrected ", model_un.get_log_posterior(theta_bias), c.posteriors[-1][-1])

        model_cor = EfficiencyModelCorrected(observed, errors, alpha, name="Corrected%d" % i)
        model_cor.fit(sampler, chain_consumer=c)
        print("Corrected ", model_cor.get_log_posterior(theta_bias), c.posteriors[-1][-1])

    c.configure_bar(shade=True)
    c.configure_general(bins=1.0, colours=colours)
    c.configure_contour(sigmas=[0, 0.01, 1, 2], shade=True, shade_alpha=0.3)
    c.plot(filename=plot_file, truth=theta_bias, figsize=(5, 5), legend=False)
    for i in range(len(c.chains)):
        c.plot_walks(filename=walk_file % c.names[i], chain=i, truth=[mean, std])
        # c.divide_chain(i, w).configure_general(rainbow=True) \
        #     .plot(figsize=(5, 5), filename=plot_file.replace(".png", "_%s.png" % c.names[i]),
        #           truth=theta_bias)
コード例 #20
0
ファイル: efficiency_model_8.py プロジェクト: dessn/sn-bhm
        model_good = EfficiencyModelUncorrected(cs, zs, ts, calibration, zeros, ls, ss, t0s, name="Good%d" % i)
        model_good.fit(sampler, chain_consumer=c)

        model_un = EfficiencyModelUncorrected(cs[mask], zs[mask], ts[mask], calibration,
                                              zeros, ls[mask], ss[mask], t0s[mask], name="Uncorrected%d" % i)
        model_un.fit(sampler, chain_consumer=c)

        biased_chain = c.chains[-1]
        # model_cor.fit(sampler, chain_consumer=c)

        filename = dir_name + "/output/weights.txt"
        if not os.path.exists(filename):
            weights = []
            for i, row in enumerate(biased_chain):
                weights.append(get_weights(row[0], row[1], row[2], row[3], row[4], row[5], threshold))
                print(100.0 * i / biased_chain.shape[0])
            weights = np.array(weights)
            np.savetxt(filename, weights)
        else:
            weights = np.loadtxt(filename)
        weights = (1 / np.power(weights, mask.sum()))
        c.add_chain(biased_chain, name="Importance Sampled", weights=weights)

    c.configure_bar(shade=True)
    c.configure_general(bins=1.0, colours=colours)
    c.configure_contour(sigmas=[0, 0.01, 1, 2], contourf=True, contourf_alpha=0.2)
    c.plot(filename=plot_file, truth=theta, figsize=(7, 7), legend=False, parameters=6)
    for i in range(len(c.chains)):
        c.plot_walks(filename=walk_file % c.names[i], chain=i, truth=theta)
コード例 #21
0
ファイル: efficiency_model_7.py プロジェクト: dessn/sn-bhm
    t = os.path.abspath(dir_name + "/output/data_%d")
    plot_file = os.path.abspath(dir_name + "/output/surfaces.png")
    walk_file = os.path.abspath(dir_name + "/output/walk_%s.png")

    c = ChainConsumer()
    n = 2
    colours = ["#4CAF50", "#D32F2F", "#1E88E5"] * n  # , "#FFA000"] * n
    for i in range(n):
        mean, sigma, cut, observed, mask = get_data(seed=i)

        model_good = EfficiencyModelUncorrected(observed, name="Good")
        model_un = EfficiencyModelUncorrected(observed[mask])
        model_cor = EfficiencyModelCorrected(observed[mask], cut)

        sampler = EnsembleSampler(num_steps=25000, num_burn=1000, temp_dir=t % i)
        model_good.fit(sampler, chain_consumer=c)
        model_un.fit(sampler, chain_consumer=c)
        biased_chain = c.chains[-1]
        # model_cor.fit(sampler, chain_consumer=c)

        mus = biased_chain[:, 0]
        sigmas = biased_chain[:, 1]
        weights = 1 / get_weights(cut, mus, sigmas, mask.sum())

        c.add_chain(biased_chain, name="Importance Sampled", weights=weights)

    c.configure_bar(shade=True)
    c.configure_general(colours=colours, bins=0.5)
    c.configure_contour(contourf=True, contourf_alpha=0.2)
    c.plot(filename=plot_file, figsize=(5, 5), truth=[mean, sigma], legend=False)
コード例 #22
0
"""

###############################################################################
# You can specify truth values using a list (in the same order as the
# declared parameters).

import numpy as np
from numpy.random import normal, multivariate_normal
from chainconsumer import ChainConsumer

np.random.seed(2)
cov = 0.2 * normal(size=(3, 3)) + np.identity(3)
truth = normal(size=3)
data = multivariate_normal(truth, 0.5 * (cov + cov.T), size=100000)

c = ChainConsumer().add_chain(data, parameters=["$x$", "$y$", r"$\beta$"])
fig = c.plot(truth=truth)
fig.set_size_inches(
    2.5 + fig.get_size_inches())  # Resize fig for doco. You don't need this.

###############################################################################
# Or you can specify truth values using a dictionary. This allows you to specify
# truth values for only some parameters. You can also customise the look
# of your truth lines.

c.configure_truth(color='w', ls=":", alpha=0.8)
fig2 = c.plot(truth={"$x$": truth[0], "$y$": truth[1]})
fig2.set_size_inches(
    2.5 + fig2.get_size_inches())  # Resize fig for doco. You don't need this.
コード例 #23
0
This can be done by setting ``smooth`` to either ``0``, ``None`` or ``False``.
Note that the parameter summaries also have smoothing turned off, and
thus summaries may change.

Fun colour change! And thicker lines!

"""

import numpy as np
from chainconsumer import ChainConsumer

if __name__ == "__main__":
    data = np.random.multivariate_normal([0.0, 4.0], [[1.0, 0.7], [0.7, 1.5]],
                                         size=100000)

    c = ChainConsumer()
    c.add_chain(data, parameters=["$x_1$", "$x_2$"])
    c.configure_general(smooth=0, linewidths=2, colours="#673AB7")
    fig = c.plot(figsize="column", truth=[0.0, 4.0])

    # If we wanted to save to file, we would instead have written
    # fig = c.plot(filename="location", figsize="column", truth=[0.0, 4.0])

    # If we wanted to display the plot interactively...
    # fig = c.plot(display=True, figsize="column", truth=[0.0, 4.0])

    fig.set_size_inches(
        2.5 +
        fig.get_size_inches())  # Resize fig for doco. You don't need this.
コード例 #24
0
np.random.seed(4)
cov = 0.2 * normal(size=(2, 2)) + np.identity(2)
truth = normal(size=2)
data = multivariate_normal(truth, 0.5 * (cov + cov.T), size=100000)
parameters = ["$x$", "$y$"]
dictionary = {"$x$": data[:, 0], "$y$": data[:, 1]}
directory = tempfile._get_default_tempdir()
filename = next(tempfile._get_candidate_names())
filename1 = directory + os.sep + filename + ".txt"
filename2 = directory + os.sep + filename + ".npy"
np.savetxt(filename1, data)
np.save(filename2, data)

# Now the normal way of giving data is passing a numpy array and parameter separately
c = ChainConsumer().add_chain(data, parameters=parameters)
fig = c.plot(truth=truth)
fig.set_size_inches(
    2.5 + fig.get_size_inches())  # Resize fig for doco. You don't need this.

###############################################################################
# And yet we can do the same thing using a dictionary:

c = ChainConsumer().add_chain(dictionary)
fig = c.plot(truth=truth)
fig.set_size_inches(
    2.5 + fig.get_size_inches())  # Resize fig for doco. You don't need this.

###############################################################################
# Or we can pass a filename in containing a text dump of the chain

c = ChainConsumer().add_chain(filename1, parameters=parameters)
コード例 #25
0
ファイル: efficiency_model_9.py プロジェクト: dessn/sn-bhm
        theta2 = theta + ls.tolist() + t0s.tolist() + ss.tolist()
        kwargs = {"num_steps": 6000, "num_burn": 250000, "save_interval": 60, "plot_covariance": True, "covariance_adjust": 10000}
        sampler = BatchMetropolisHastings(num_walkers=w, kwargs=kwargs, temp_dir=t % i, num_cores=4)

        model_good = EfficiencyModelUncorrected(cs, zs, ts, types, calibration, zeros, ls, ss, t0s, name="Good%d" % i)
        model_good.fit(sampler, chain_consumer=c)
        print("Good sampler finished")
        mtypes = [t for t, m in zip(types, mask) if m]
        model_un = EfficiencyModelUncorrected(cs[mask], zs[mask], ts[mask], mtypes, calibration,
                                              zeros, ls[mask], ss[mask], t0s[mask], name="Uncorrected%d" % i)
        model_un.fit(sampler, chain_consumer=c)
        print("Uncorrected sampler finished")
        print("Getting weights")
        biased_chain = c.chains[-1]
        filename = dir_name + "/output/weights.txt"
        if not os.path.exists(filename):
            weights = Parallel(n_jobs=4, verbose=100, batch_size=100)(delayed(get_weight_from_row)(row, threshold) for row in biased_chain)
            weights = np.array(weights)
            np.savetxt(filename, weights)
        else:
            weights = np.loadtxt(filename)
        weights = (1 / np.power(weights, mask.sum()))
        c.add_chain(biased_chain, name="Importance Sampled", weights=weights)
        print("Weights finished")

    c.configure_bar(shade=True)
    c.configure_general(bins=1.0, colours=colours)
    c.configure_contour(sigmas=[0, 0.01, 1, 2], shade=True, shade_alpha=0.2)
    c.plot(filename=plot_file, truth=theta, figsize=(10, 10), legend=False, parameters=10)
    for i in range(len(c.chains)):
        c.plot_walks(filename=walk_file % c.names[i], chain=i, truth=theta)
コード例 #26
0
===================

Customise the plot line styles.

In this example we customise the line styles used, and make use of
the ability to pass lists of parameters to the configuration methods.

"""

import numpy as np
from numpy.random import normal, multivariate_normal
from chainconsumer import ChainConsumer

if __name__ == "__main__":
    np.random.seed(1)
    cov = normal(size=(3, 3))
    data = multivariate_normal(normal(size=3),
                               0.5 * (cov + cov.T),
                               size=100000)
    data2 = data * 1.1 + 0.5

    c = ChainConsumer().add_chain(data, parameters=["$x$", "$y$",
                                                    "$z$"]).add_chain(data2)
    c.configure_general(linestyles=["-", "--"], linewidths=[1.0, 2.0])
    c.configure_contour(shade=[True, False], shade_alpha=[0.2, 0.0])
    fig = c.plot()

    fig.set_size_inches(
        2.5 +
        fig.get_size_inches())  # Resize fig for doco. You don't need this.
コード例 #27
0
ファイル: load.py プロジェクト: dessn/sn-bhm

if __name__ == "__main__":
    dir_name = os.path.dirname(os.path.abspath(__file__))
    output = dir_name + "/output/complete.png"
    output2 = dir_name + "/output/complete2.png"
    folders = ["simple", "approx"] # "stan_mc",
    use_weight = [False, True]
    c = ChainConsumer()
    for f, u in zip(folders, use_weight):
        loc = dir_name + os.sep + f + "/stan_output"
        t = None
        try:
            chain, posterior, t, p, ff, l, w, ow = load_stan_from_folder(loc, merge=True)
            if u:
                c.add_chain(chain, posterior=posterior, walkers=l, name=f)
                c.add_chain(chain, weights=w, posterior=posterior, walkers=l, name="full")
            else:
                c.add_chain(chain, posterior=posterior, walkers=l, name=f)
        except Exception as e:
            print(e)
            print("No files found in %s" % loc)
    print(p)
    c.configure_general(linestyles=['-', '--', '-'], colours=["#1E88E5", "#555555", "#D32F2F"]) #4CAF50
    c.configure_bar(shade=[True, True, True])
    c.configure_contour(shade=[True, True, True])
    pp = ['$\\Omega_m$', '$\\alpha$', '$\\beta$', '$\\langle M_B \\rangle$', '$\\langle x_1 \\rangle$',
          '$\\langle c \\rangle$'] #, '$\\sigma_{\\rm m_B}$', '$\\sigma_{x_1}$', '$\\sigma_c$']
    c.plot(filename=output, truth=t, parameters=pp)
    c.plot(filename=output2, truth=t)
コード例 #28
0
"""
=====================
Flips, Ticks and Size
=====================

You can stop the second parameter rotating in the plot if you prefer squares!

Unlike the Introduction example, which shows the rotated plots, this example shows them
without the rotation.

Also, you can pass in a tuple for the figure size. We also demonstrate adding more
ticks to the axis in this example. Also, I change the colour to red, just for fun.
"""

import numpy as np
from chainconsumer import ChainConsumer

if __name__ == "__main__":
    np.random.seed(0)
    data = np.random.multivariate_normal([0.0, 4.0], [[1.0, 0.7], [0.7, 1.5]],
                                         size=1000000)

    c = ChainConsumer().add_chain(data, parameters=["$x_1$", "$x_2$"])
    c.configure_general(flip=False, max_ticks=10, colours="#D32F2F")
    fig = c.plot(figsize=(6, 6))

    fig.set_size_inches(
        2.5 +
        fig.get_size_inches())  # Resize fig for doco. You don't need this.
コード例 #29
0
ファイル: simple_cosmology_test.py プロジェクト: dessn/sn-bhm
    for n in ["deep", "shallow"]:
        is_shallow = n == "shallow"
        # bias_file = os.path.dirname(__file__) + "/output/cosmology/bias_%s.npy" % n
        temp_dir2 = os.path.dirname(__file__) + "/output/cosmology2_%s" % n
        if not os.path.exists(temp_dir2):
            os.makedirs(temp_dir2)
        logging.basicConfig(level=logging.DEBUG)

        zs, mu_mcmc, mu_minuit, std_mcmc, std_minuit = get_supernova_data(shallow=is_shallow)

        plot_cosmology(zs, mu_mcmc, mu_minuit, std_mcmc, std_minuit, n)
        fitter_mcmc = SimpleCosmologyFitter("mcmc", zs, mu_mcmc, std_mcmc)
        fitter_minuit = SimpleCosmologyFitter("minuit", zs, mu_minuit, std_minuit)

        sampler = EnsembleSampler(temp_dir=temp_dir2, save_interval=60, num_steps=8000, num_burn=1000)
        c = fitter_mcmc.fit(sampler=sampler)
        cc.add_chain(c.chains[-1], parameters=c.parameters[-1], name="%s MCMC" % n.title())
        c = fitter_minuit.fit(sampler=sampler, chain_consumer=c)
        cc.add_chain(c.chains[-1], parameters=c.parameters[-1], name="%s Max. Like." % n.title())
        c.names = ["MCMC", "Max. Like."]
        c.plot(filename="output/comparison_%s.png" % n, parameters=2, figsize=(5.5, 5.5), truth=[0.3, -1.0])
        c.plot(filename="output/comparison_%s.pdf" % n, parameters=2, figsize=(5.5, 5.5), truth=[0.3, -1.0])
        print(c.get_latex_table())
    print(cc.get_latex_table())
    cc.configure_general(colours=["#1E88E5", "#1E88E5", "#D32F2F", "#D32F2F"],
                         linewidths=[1, 2, 1, 2],
                         linestyles=["-", "--", "-", "--"])
    cc.configure_contour(shade=[True, False, True, False])
    cc.plot(filename="output/comparison.png", parameters=2, figsize=(5.5, 5.5), truth=[0.3, -1.0])
    cc.plot(filename="output/comparison.pdf", parameters=2, figsize=(5.5, 5.5), truth=[0.3, -1.0])