예제 #1
0
    
    rs_fid = get_r_s([0.273])[0]
    
    daval = (alpha/(1+epsilon)) * da / rs_fid
    
    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")
예제 #2
0
Choose custom sigma levels and display point cloud.

In this example we display more sigma levels, turn on the point cloud, and
disable the parameter summaries on the top of the marginalised distributions.

Note that because of the very highly correlated distribution we have, it is
useful to increase the number of bins the plots are generated with, to capture the
thinness of the correlation.
"""

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)

    c = ChainConsumer().add_chain(data, parameters=["$x$", "$y$", "$z$"])
    c.configure_bar(summary=False).configure_general(bins=1.4)
    c.configure_contour(cloud=True, sigmas=np.linspace(0, 2, 10))
    fig = c.plot()

    fig.set_size_inches(
        2.5 +
        fig.get_size_inches())  # Resize fig for doco. You don't need this.
예제 #3
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.
예제 #4
0
        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)
예제 #5
0
    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)
예제 #6
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)
예제 #7
0
if __name__ == "__main__":
    logging.basicConfig(level=logging.DEBUG)
    dir_name = os.path.dirname(__file__)
    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 = 3
    colours = ["#D32F2F", "#1E88E5"] * n
    for i in range(n):
        mean, sigma, observed, cut = get_data(seed=i)

        model_un = EfficiencyModelUncorrected(observed)
        model_cor = EfficiencyModelCorrected(observed, cut)

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

        sampler = EnsembleSampler(num_steps=10000, 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.configure_contour(shade=True, shade_alpha=0.3)
    # c.plot_walks(truth=[mean, sigma], filename=walk_file % "no", chain=0)
    # c.plot_walks(truth=[mean, sigma], filename=walk_file % "cor", chain=1)
    c.plot(filename=plot_file, figsize=(5, 5), truth=[mean, sigma], legend=False)
예제 #8
0
        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)
예제 #9
0
파일: simple.py 프로젝트: dessn/sn-bhm
    sampler = EnsembleSampler(temp_dir=temp_dir, num_steps=20000)
    my_model.fit(sampler, chain_consumer=c)
    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]
예제 #10
0
    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])