コード例 #1
0
 def test_summary1(self):
     tolerance = 5e-2
     consumer = ChainConsumer()
     consumer.add_chain(self.data)
     consumer.configure_general(bins=1.6)
     summary = consumer.get_summary()
     actual = np.array(list(summary.values())[0])
     expected = np.array([3.5, 5.0, 6.5])
     diff = np.abs(expected - actual)
     assert np.all(diff < tolerance)
コード例 #2
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)
コード例 #3
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.
コード例 #4
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.
コード例 #5
0
if __name__ == "__main__":
    np.random.seed(0)
    cov = 0.3 * random(size=(3, 3)) + np.identity(3)
    data = multivariate_normal(normal(size=3),
                               0.5 * (cov + cov.T),
                               size=100000)
    cov = 0.3 * random(size=(3, 3)) + np.identity(3)
    data2 = multivariate_normal(normal(size=3),
                                0.5 * (cov + cov.T),
                                size=100000)
    cov = 0.3 * random(size=(3, 3)) + np.identity(3)
    data3 = multivariate_normal(normal(size=3),
                                0.5 * (cov + cov.T),
                                size=100000)
    cov = 0.3 * random(size=(3, 3)) + np.identity(3)
    data4 = multivariate_normal(normal(size=3),
                                0.5 * (cov + cov.T),
                                size=100000)

    c = ChainConsumer()
    c.add_chain(data, name="A")
    c.add_chain(data2, name="B")
    c.add_chain(data3, name="C")
    c.add_chain(data4, name="D")
    c.configure_general(bins=50, rainbow=True)
    fig = c.plot()

    fig.set_size_inches(
        2.5 +
        fig.get_size_inches())  # Resize fig for doco. You don't need this.
コード例 #6
0
This is a general example to illustrate that most parameters
that you can pass to the configuration methods accept lists.

"""

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

if __name__ == "__main__":
    np.random.seed(2)
    cov = normal(size=(2, 2)) + np.identity(2)
    d1 = multivariate_normal(normal(size=2), 0.5 * (cov + cov.T), size=100000)
    cov = normal(size=(2, 2)) + np.identity(2)
    d2 = multivariate_normal(normal(size=2), 0.5 * (cov + cov.T), size=100000)
    cov = normal(size=(2, 2)) + np.identity(2)
    d3 = multivariate_normal(normal(size=2), 0.5 * (cov + cov.T), size=100000)

    c = ChainConsumer()
    c.add_chain(d1, parameters=["$x$", "$y$"])
    c.add_chain(d2)
    c.add_chain(d3)

    c.configure_general(linestyles=["-", "--", ":"], linewidths=[1.0, 2.0, 2.5],
                        colours=["#1E88E5", "#D32F2F", "#111111"])
    c.configure_contour(shade=[True, True, False], shade_alpha=[0.2, 0.1, 0.0])
    c.configure_bar(shade=[True, False, False])
    fig = c.plot()

    fig.set_size_inches(2.5 + fig.get_size_inches())  # Resize fig for doco. You don't need this.
コード例 #7
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)
コード例 #8
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)])
コード例 #9
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)
コード例 #10
0
ファイル: get_da_hz.py プロジェクト: Samreay/Wigglez
    
    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")
    print(c.get_latex_table())
コード例 #11
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)
コード例 #12
0
ファイル: efficiency_model_2.py プロジェクト: dessn/sn-bhm
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)
コード例 #13
0
"""
=============
No Histograms
=============

Sometimes marginalised histograms are not needed.

"""

from numpy.random import multivariate_normal, normal, seed
from chainconsumer import ChainConsumer

if __name__ == "__main__":
    seed(0)
    cov = normal(size=(3, 3))
    data = multivariate_normal(normal(size=3),
                               0.5 * (cov + cov.T),
                               size=100000)

    c = ChainConsumer().add_chain(data)
    c.configure_general(plot_hists=False)
    fig = c.plot()

    fig.set_size_inches(
        2.5 +
        fig.get_size_inches())  # Resize fig for doco. You don't need this.
コード例 #14
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.
コード例 #15
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])