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)
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")
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'])
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']
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'])
def test_plot_1d_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 'astropy' in sys.modules: plot_types += ['astropyhist'] if 'fastkde' in sys.modules: plot_types += ['fastkde'] for plot_type in plot_types: fig = plt.figure() fig, axes = make_1d_axes(['x0', 'x1', 'x2', 'x3', 'x4'], fig=fig) gd.plot_1d(axes, plot_type=plot_type, label="gd") pc.plot_1d(axes, plot_type=plot_type, label="pc") mn.plot_1d(axes, plot_type=plot_type, label="mn") gd_colors = [] pc_colors = [] mn_colors = [] for x, ax in axes.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")
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")
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")
url = github_url + filename open(filename, 'wb').write(requests.get(url).content) tarfile.open(filename).extractall() #| ## Marginalised posterior plotting #| Import anesthetic and load the MCMC samples: import matplotlib.pyplot as plt from anesthetic import MCMCSamples, make_2d_axes 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) ...