コード例 #1
0
def test_read_cobayamcmc():
    np.random.seed(3)
    mcmc = MCMCSamples(root='./tests/example_data/cb')
    mcmc.plot_2d(['x0', 'x1'])
    mcmc.plot_1d(['x0', 'x1'])
    plt.close("all")

    # compare directly with getdist
    mcmc_gd = getdist.loadMCSamples(file_root="./tests/example_data/cb")
    assert_array_almost_equal(mcmc.logL, mcmc_gd.loglikes, decimal=15)
コード例 #2
0
def test_read_getdist():
    np.random.seed(3)
    mcmc = MCMCSamples(root='./tests/example_data/gd')
    mcmc.plot_2d(['x0', 'x1', 'x2', 'x3'])
    mcmc.plot_1d(['x0', 'x1', 'x2', 'x3'])

    mcmc = MCMCSamples(root='./tests/example_data/gd_single')
    mcmc.plot_2d(['x0', 'x1', 'x2', 'x3'])
    mcmc.plot_1d(['x0', 'x1', 'x2', 'x3'])
    plt.close("all")
コード例 #3
0
def test_read_getdist_discard_burn_in():
    np.random.seed(3)
    mcmc = MCMCSamples(burn_in=0.3, root='./tests/example_data/gd')
    mcmc.plot_2d(['x0', 'x1', 'x2', 'x3'])
    mcmc.plot_1d(['x0', 'x1', 'x2', 'x3'])

    # for 2 getdist chains of length 5000
    mcmc0 = MCMCSamples(root='./tests/example_data/gd')
    mcmc1 = MCMCSamples(burn_in=1000, root='./tests/example_data/gd')
    for key in ['x0', 'x1', 'x2', 'x3', 'x4']:
        assert_array_equal(mcmc0[key][1000:5000], mcmc1[key][:4000])
    mcmc1.plot_2d(['x0', 'x1', 'x2', 'x3', 'x4'])
    mcmc1.plot_1d(['x0', 'x1', 'x2', 'x3', 'x4'])
コード例 #4
0
def test_plotting_with_integer_names():
    np.random.seed(3)
    samples_1 = MCMCSamples(data=np.random.rand(1000, 3))
    samples_2 = MCMCSamples(data=np.random.rand(1000, 3))
    fig, ax = samples_1.plot_2d([0, 1, 2])
    samples_2.plot_2d(ax)

    fig, ax = samples_1.plot_1d([0, 1, 2])
    samples_2.plot_1d(ax)

    assert samples_1[0].shape == (1000,)
    assert_array_equal(samples_1.loc[:, 0], samples_1[0])
    assert_array_equal(samples_1.loc[:, 0], samples_1.iloc[:, 0])
    with pytest.raises(KeyError):
        samples_1['0']
コード例 #5
0
def test_discard_burn_in(root):
    np.random.seed(3)
    mcmc = MCMCSamples(burn_in=0.3, root='./tests/example_data/' + root)
    mcmc.plot_2d(['x0', 'x1', 'x2', 'x3'])
    mcmc.plot_1d(['x0', 'x1', 'x2', 'x3'])

    # for 2 chains of length 1000
    mcmc0 = MCMCSamples(root='./tests/example_data/' + root)
    mcmc1 = MCMCSamples(burn_in=1000, root='./tests/example_data/' + root)
    for key in ['x0', 'x1', 'x2', 'x3', 'x4']:
        if key in mcmc0:
            assert key in mcmc1
            assert_array_equal(mcmc0[key][1000:2000], mcmc1[key][:1000])
    mcmc1.plot_2d(['x0', 'x1', 'x2', 'x3', 'x4'])
    mcmc1.plot_1d(['x0', 'x1', 'x2', 'x3', 'x4'])
コード例 #6
0
def test_plot_2d_colours():
    np.random.seed(3)
    gd = MCMCSamples(root="./tests/example_data/gd")
    gd.drop(columns='x3', inplace=True)
    pc = NestedSamples(root="./tests/example_data/pc")
    pc.drop(columns='x4', inplace=True)
    mn = NestedSamples(root="./tests/example_data/mn")
    mn.drop(columns='x2', inplace=True)

    plot_types = ['kde', 'hist']
    if 'fastkde' in sys.modules:
        plot_types += ['fastkde']

    for types in plot_types:
        fig = plt.figure()
        fig, axes = make_2d_axes(['x0', 'x1', 'x2', 'x3', 'x4'], fig=fig)
        types = {'diagonal': types, 'lower': types, 'upper': 'scatter'}
        gd.plot_2d(axes, types=types, label="gd")
        pc.plot_2d(axes, types=types, label="pc")
        mn.plot_2d(axes, types=types, label="mn")
        gd_colors = []
        pc_colors = []
        mn_colors = []
        for y, rows in axes.iterrows():
            for x, ax in rows.iteritems():
                handles, labels = ax.get_legend_handles_labels()
                for handle, label in zip(handles, labels):
                    if isinstance(handle, Rectangle):
                        color = to_hex(handle.get_facecolor())
                    else:
                        color = handle.get_color()

                    if label == 'gd':
                        gd_colors.append(color)
                    elif label == 'pc':
                        pc_colors.append(color)
                    elif label == 'mn':
                        mn_colors.append(color)

        assert(len(set(gd_colors)) == 1)
        assert(len(set(mn_colors)) == 1)
        assert(len(set(pc_colors)) == 1)
        plt.close("all")
コード例 #7
0
def test_plot_2d_legend():
    np.random.seed(3)
    ns = NestedSamples(root='./tests/example_data/pc')
    mc = MCMCSamples(root='./tests/example_data/gd')
    params = ['x0', 'x1', 'x2', 'x3']

    # Test label kwarg for kde
    fig, axes = make_2d_axes(params, upper=False)
    ns.plot_2d(axes, label='l1', types=dict(diagonal='kde', lower='kde'))
    mc.plot_2d(axes, label='l2', types=dict(diagonal='kde', lower='kde'))

    for y, row in axes.iterrows():
        for x, ax in row.iteritems():
            if ax is not None:
                handles, labels = ax.get_legend_handles_labels()
                assert(labels == ['l1', 'l2'])
                if x == y:
                    assert(all([isinstance(h, Line2D) for h in handles]))
                else:
                    assert(all([isinstance(h, Rectangle) for h in handles]))
    plt.close('all')

    # Test label kwarg for hist and scatter
    fig, axes = make_2d_axes(params, lower=False)
    ns.plot_2d(axes, label='l1', types=dict(diagonal='hist', upper='scatter'))
    mc.plot_2d(axes, label='l2', types=dict(diagonal='hist', upper='scatter'))

    for y, row in axes.iterrows():
        for x, ax in row.iteritems():
            if ax is not None:
                handles, labels = ax.get_legend_handles_labels()
                assert(labels == ['l1', 'l2'])
                if x == y:
                    assert(all([isinstance(h, Rectangle) for h in handles]))
                else:
                    assert(all([isinstance(h, Line2D)
                                for h in handles]))
    plt.close('all')

    # test default labelling
    fig, axes = make_2d_axes(params, upper=False)
    ns.plot_2d(axes)
    mc.plot_2d(axes)

    for y, row in axes.iterrows():
        for x, ax in row.iteritems():
            if ax is not None:
                handles, labels = ax.get_legend_handles_labels()
                assert(labels == ['pc', 'gd'])
    plt.close('all')

    # Test label kwarg to constructors
    ns = NestedSamples(root='./tests/example_data/pc', label='l1')
    mc = MCMCSamples(root='./tests/example_data/gd', label='l2')
    params = ['x0', 'x1', 'x2', 'x3']

    fig, axes = make_2d_axes(params, upper=False)
    ns.plot_2d(axes)
    mc.plot_2d(axes)

    for y, row in axes.iterrows():
        for x, ax in row.iteritems():
            if ax is not None:
                handles, labels = ax.get_legend_handles_labels()
                assert(labels == ['l1', 'l2'])
    plt.close('all')
コード例 #8
0
def test_read_montepython():
    np.random.seed(3)
    mcmc = MCMCSamples(root='./tests/example_data/mp')
    mcmc.plot_2d(['x0', 'x1', 'x2', 'x3'])
    mcmc.plot_1d(['x0', 'x1', 'x2', 'x3'])
    plt.close("all")
コード例 #9
0
def test_read_cobayamcmc():
    np.random.seed(3)
    mcmc = MCMCSamples(root='./tests/example_data/cb')
    mcmc.plot_2d(['x0', 'x1'])
    mcmc.plot_1d(['x0', 'x1'])
    plt.close("all")
コード例 #10
0
mcmc = MCMCSamples(root=mcmc_root)

#| We have plotting tools for 1D plots ...

fig, axes = mcmc.plot_1d('omegabh2')

#| ... multiple 1D plots ...

fig, axes = mcmc.plot_1d(['omegabh2', 'omegach2', 'H0', 'tau', 'logA', 'ns'])
fig.tight_layout()

#| ... triangle plots ...

mcmc.plot_2d(['omegabh2', 'omegach2', 'H0'],
             types={
                 'lower': 'kde',
                 'diagonal': 'kde'
             })

#| ... triangle plots (with the equivalent scatter plot filling up the left hand side) ...

mcmc.plot_2d(['omegabh2', 'omegach2', 'H0'])

#| ... and rectangle plots.

mcmc.plot_2d([['omegabh2', 'omegach2', 'H0'], ['logA', 'ns']])

#| Rectangle plots are pretty flexible with what they can do:

mcmc.plot_2d([['omegabh2', 'omegach2', 'H0'], ['H0', 'omegach2']])
コード例 #11
0
from anesthetic import MCMCSamples
mcmc_root = 'plikHM_TTTEEE_lowl_lowE_lensing/base_plikHM_TTTEEE_lowl_lowE_lensing'
mcmc = MCMCSamples(root=mcmc_root)

#| We have plotting tools for 1D plots ...

fig, axes = mcmc.plot_1d('omegabh2') ;

#| ... multiple 1D plots ...

fig, axes = mcmc.plot_1d(['omegabh2','omegach2','H0','tau','logA','ns']);
fig.tight_layout()

#| ... triangle plots ...

mcmc.plot_2d(['omegabh2','omegach2','H0'], types={'lower':'kde','diagonal':'kde'});

#| ... triangle plots (with the equivalent scatter plot filling up the left hand side) ...

mcmc.plot_2d(['omegabh2','omegach2','H0']);

#| ... and rectangle plots.

mcmc.plot_2d([['omegabh2','omegach2','H0'], ['logA', 'ns']]);

#| Rectangle plots are pretty flexible with what they can do:

mcmc.plot_2d([['omegabh2','omegach2','H0'], ['H0','omegach2']]);

#| ## Defining new parameters
#|