Esempio n. 1
0
def test_post_prior(tmpdir):
    # Generate original chain
    info: InputDict = {
        "output": os.path.join(tmpdir, "gaussian"), "force": True,
        "params": info_params, "sampler": info_sampler,
        "likelihood": {"one": None}, "prior": {"gaussian": sampled_pdf}}
    info_post: InputDict = {
        "output": info["output"], "force": True,
        "post": {"suffix": "foo", 'skip': 0.1,
                 "remove": {"prior": {"gaussian": None}},
                 "add": {"prior": {"target": target_pdf_prior}}}}
    _, sampler = run(info)
    if mpi.is_main_process():
        mcsamples_in = loadMCSamples(info["output"], settings={'ignore_rows': 0.1})
        target_mean, target_cov = mpi.share(_get_targets(mcsamples_in))
    else:
        target_mean, target_cov = mpi.share()

    for mem in [False, True]:
        post(info_post, sample=sampler.products()["sample"] if mem else None)
        # Load with GetDist and compare
        if mpi.is_main_process():
            mcsamples = loadMCSamples(
                info_post["output"] + _post_ + info_post["post"]["suffix"])
            new_mean = mcsamples.mean(["a", "b"])
            new_cov = mcsamples.getCovMat().matrix
            mpi.share((new_mean, new_cov))
        else:
            new_mean, new_cov = mpi.share()
        assert np.allclose(new_mean, target_mean)
        assert np.allclose(new_cov, target_cov)
Esempio n. 2
0
def getSystematicAverageTableLines(jobItem1, jobItem2):
    # if you have two versions of the likelihood with the same data, and don't know which is right,
    # this just crudely adds the samples with equal weight per likelihood
    samps1 = loadMCSamples(jobItem1.chainRoot,
                           jobItem=jobItem1,
                           settings=batch.getdist_options)
    samps2 = loadMCSamples(jobItem2.chainRoot,
                           jobItem=jobItem2,
                           settings=batch.getdist_options)
    samps = samps1.getCombinedSamplesWithSamples(samps2)
    return getTableLines(samps.getMargeStats())
Esempio n. 3
0
def getSystematicAverageTableLines(jobItem1, jobItem2):
    # if you have two versions of the likelihood with the same data, and don't know which is right,
    # this just crudely adds the samples with equal weight per likelihood
    samps1 = loadMCSamples(jobItem1.chainRoot, jobItem=jobItem1, settings=batch.getdist_options)
    samps2 = loadMCSamples(jobItem2.chainRoot, jobItem=jobItem2, settings=batch.getdist_options)
    samps = samps1.getCombinedSamplesWithSamples(samps2)
    if False:
        import planckStyle as s
        g = s.getSubplotPlotter()
        g.plots_1d([samps, samps1, samps2], params=params.list(),
                   legend_labels=['Combined', texEscapeText(jobItem1.name), texEscapeText(jobItem2.name)])
        g.export('joint_' + jobItem1.name)
    return getTableLines(samps.getMargeStats())
Esempio n. 4
0
def getSystematicAverageTableLines(jobItem1, jobItem2):
    # if you have two versions of the likelihood with the same data, and don't know which is right,
    # this just crudely adds the samples with equal weight per likelihood
    samps1 = loadMCSamples(jobItem1.chainRoot,
                           jobItem=jobItem1,
                           settings=batch.getdist_options)
    samps2 = loadMCSamples(jobItem2.chainRoot,
                           jobItem=jobItem2,
                           settings=batch.getdist_options)
    samps = samps1.getCombinedSamplesWithSamples(samps2)
    if False:
        import planckStyle as s
        g = s.getSubplotPlotter()
        g.plots_1d([samps, samps1, samps2],
                   params=params.list(),
                   legend_labels=[
                       'Combined',
                       texEscapeText(jobItem1.name),
                       texEscapeText(jobItem2.name)
                   ])
        g.export('joint_' + jobItem1.name)
    return getTableLines(samps.getMargeStats())
Esempio n. 5
0
def test_post_prior(tmpdir):
    # Generate original chain
    info = {
        _output_prefix: os.path.join(str(tmpdir), "gaussian"),
        _force: True,
        _params: info_params,
        kinds.sampler: info_sampler,
        kinds.likelihood: {
            "one": None
        },
        _prior: {
            "gaussian": sampled_pdf
        }
    }
    run(info)
    info_post = {
        _output_prefix: info[_output_prefix],
        _force: True,
        _post: {
            _post_suffix: "foo",
            _post_remove: {
                _prior: {
                    "gaussian": None
                }
            },
            _post_add: {
                _prior: {
                    "target": target_pdf_prior
                }
            }
        }
    }
    post(info_post)
    # Load with GetDist and compare
    mcsamples = loadMCSamples(info_post[_output_prefix] + _post_ +
                              info_post[_post][_post_suffix])
    new_mean = mcsamples.mean(["a", "b"])
    new_cov = mcsamples.getCovMat().matrix
    assert abs(KL_norm(target["mean"], target["cov"], new_mean,
                       new_cov)) < 0.02
Esempio n. 6
0
def test_post_prior(tmpdir):
    # Generate original chain
    info = {
        "output": os.path.join(str(tmpdir), "gaussian"),
        "force": True,
        "params": info_params,
        "sampler": info_sampler,
        "likelihood": {
            "one": None
        },
        "prior": {
            "gaussian": sampled_pdf
        }
    }
    run(info)
    info_post = {
        "output": info["output"],
        "force": True,
        "post": {
            "suffix": "foo",
            "remove": {
                "prior": {
                    "gaussian": None
                }
            },
            "add": {
                "prior": {
                    "target": target_pdf
                }
            }
        }
    }
    post(info_post)
    # Load with GetDist and compare
    mcsamples = loadMCSamples(info_post["output"] + "_post_" +
                              info_post["post"]["suffix"])
    new_mean = mcsamples.mean(["a", "b"])
    new_cov = mcsamples.getCovMat().matrix
    assert abs(KL_norm(target["mean"], target["cov"], new_mean,
                       new_cov)) < 0.02
Esempio n. 7
0
    def getdistAnalyzer(self, cov=False):
        from getdist import mcsamples

        mcsamplefile = mcsamples.loadMCSamples(self.filename, settings={'ignore_rows': self.skip})

        if cov:
            cov = mcsamplefile.cov(pars=self.paramList)
            np.savetxt(self.filename + '_' + 'cov.txt', cov)
            logger.info("Covariance matrix:\n")
            logger.info(cov)
            logger.info("\n")

        means  = mcsamplefile.getMeans()
        stddev = mcsamplefile.std(self.paramList)
        summaryResults = []

        for i, param in enumerate(self.paramList):
            logger.info(self.paramList[i] + " : " + str(round(means[i], 4)) + \
                        "+/-" + str(round(stddev[i], 4)))
            summaryResults.append(self.paramList[i] + " : " + str(round(means[i], 4)) + \
                                  "+/-" + str(round(stddev[i], 4)))
        return summaryResults
 def getMCSamples(self, ini=None, settings={}):
     return loadMCSamples(self.chainRoot,
                          jobItem=self,
                          ini=ini,
                          settings=settings)
Esempio n. 9
0
def test_post_likelihood(tmpdir):
    """
    Swaps likelihood "gaussian" for "target".

    It also tests aggregated chi2's by removing and adding a likelihood to an existing
    type.
    """
    # Generate original chain
    info_params_local = deepcopy(info_params)
    info_params_local["dummy"] = 0
    dummy_loglike_add = 0.1
    dummy_loglike_remove = 0.01
    info = {
        _output_prefix: os.path.join(str(tmpdir), "gaussian"),
        _force: True,
        _params: info_params_local,
        kinds.sampler: info_sampler,
        kinds.likelihood: {
            "gaussian": {
                "external": sampled_pdf,
                "type": "A"
            },
            "dummy": {
                "external": lambda dummy: 1,
                "type": "B"
            },
            "dummy_remove": {
                "external": lambda dummy: dummy_loglike_add,
                "type": "B"
            }
        }
    }
    info_run_out, sampler_run = run(info)
    info_post = {
        _output_prefix: info[_output_prefix],
        _force: True,
        _post: {
            _post_suffix: "foo",
            _post_remove: {
                kinds.likelihood: {
                    "gaussian": None,
                    "dummy_remove": None
                }
            },
            _post_add: {
                kinds.likelihood: {
                    "target": {
                        "external": target_pdf,
                        "type": "A",
                        "output_params": ["cprime"]
                    },
                    "dummy_add": {
                        "external": lambda dummy: dummy_loglike_remove,
                        "type": "B"
                    }
                }
            }
        }
    }
    info_post_out, products_post = post(info_post)
    # Load with GetDist and compare
    mcsamples = loadMCSamples(info_post[_output_prefix] + _post_ +
                              info_post[_post][_post_suffix])
    new_mean = mcsamples.mean(["a", "b"])
    new_cov = mcsamples.getCovMat().matrix
    assert abs(KL_norm(target["mean"], target["cov"], new_mean,
                       new_cov)) < 0.02
    assert np.allclose(products_post["sample"]["chi2__A"],
                       products_post["sample"]["chi2__target"])
    assert np.allclose(
        products_post["sample"]["chi2__B"],
        products_post["sample"]["chi2__dummy"] +
        products_post["sample"]["chi2__dummy_add"])
Esempio n. 10
0
def test_cosmo_run_resume_post(tmpdir, packages_path=None):
    # only vary As, so fast chain. Chain does not need to converge (tested elsewhere).
    info['output'] = os.path.join(tmpdir, 'testchain')
    if packages_path:
        info["packages_path"] = process_packages_path(packages_path)
    run(info, force=True)
    # note that continuing from files leads to text-file precision at read in, so a mix of
    # precision in the output SampleCollection returned from run
    run(info, resume=True, override={'sampler': {'mcmc': {'Rminus1_stop': 0.2}}})
    updated_info, sampler = run(info['output'] + '.updated' + Extension.dill,
                                resume=True,
                                override={'sampler': {'mcmc': {'Rminus1_stop': 0.05}}})
    results = mpi.allgather(sampler.products()["sample"])
    samp = MCSamplesFromCobaya(updated_info, results, ignore_rows=0.2)
    assert np.isclose(samp.mean('As100'), 100 * samp.mean('As'))

    # post-processing
    info_post: PostDict = {'add': {'params': {'h': None},
                                   "likelihood": {"test_likelihood2": likelihood2}},
                           'remove': {'likelihood': ["test_likelihood"]},
                           'suffix': 'testpost',
                           'skip': 0.2, 'thin': 4
                           }

    output_info, products = run(updated_info, override={'post': info_post}, force=True)
    results2 = mpi.allgather(products["sample"])
    samp2 = MCSamplesFromCobaya(output_info, results2)
    samp_test = samp.copy()
    samp_test.weighted_thin(4)
    sigma8 = samp_test.getParams().sigma8
    samp_test.reweightAddingLogLikes(-(sigma8 - 0.7) ** 2 / 0.1 ** 2
                                     + (sigma8 - 0.75) ** 2 / 0.07 ** 2)
    assert np.isclose(samp_test.mean('sigma8'), samp2.mean('sigma8'))

    # from getdist-format chain files
    root = os.path.join(tmpdir, 'getdist_format')
    if mpi.is_main_process():
        samp.saveChainsAsText(root)
    mpi.sync_processes()

    from_txt = dict(updated_info, output=root)
    post_from_text = dict(info_post, skip=0)  # getdist already skipped
    output_info, products = run(from_txt, override={'post': post_from_text}, force=True)
    samp_getdist = MCSamplesFromCobaya(output_info, mpi.allgather(products["sample"]))
    assert not products["stats"]["points_removed"]
    assert samp2.numrows == samp_getdist.numrows
    assert np.isclose(samp2.mean('sigma8'), samp_getdist.mean('sigma8'))

    # again with inferred-inputs for params
    info_conv = cosmomc_root_to_cobaya_info_dict(root)
    # have to manually add consistent likelihoods if re-computing
    info_conv['likelihood'] = info['likelihood']
    info_conv['theory'] = info['theory']
    post_from_text = dict(info_post, skip=0, suffix='getdist2')  # getdist already skipped
    output_info, products = run(info_conv, override={'post': post_from_text},
                                output=False)
    samp_getdist2 = MCSamplesFromCobaya(output_info, mpi.allgather(products["sample"]))
    assert np.isclose(samp2.mean('sigma8'), samp_getdist2.mean('sigma8'))

    # from save info, no output
    info_post['output'] = None
    output_info, products = run({'output': info['output'], 'post': info_post}, force=True)
    results3 = mpi.allgather(products["sample"])
    samp3 = MCSamplesFromCobaya(output_info, results3)
    assert np.isclose(samp3.mean("sigma8"), samp2.mean("sigma8"))
    assert np.isclose(samp3.mean("joint"), samp2.mean("joint"))
    samps4 = loadMCSamples(info['output'] + '.post.testpost')
    assert np.isclose(samp3.mean("joint"), samps4.mean("joint"))

    # test recover original answer swapping likelihoods back
    info_revert = {'add': {'likelihood': info['likelihood']},
                   'remove': {'likelihood': ["test_likelihood2"]},
                   'suffix': 'revert',
                   'skip': 0, 'thin': 1,
                   'output': None
                   }
    output_info, products = run({'output': info['output'] + '.post.testpost',
                                 'post': info_revert}, force=True)
    results_revert = mpi.allgather(products["sample"])
    samp_revert = MCSamplesFromCobaya(output_info, results_revert)

    samp_thin = MCSamplesFromCobaya(updated_info, results, ignore_rows=0.2)
    samp_thin.weighted_thin(4)
    assert samp_thin.numrows == samp_revert.numrows + products["stats"]["points_removed"]
    if not products["stats"]["points_removed"]:
        assert np.isclose(samp_revert.mean("sigma8"), samp_thin.mean("sigma8"))
    else:
        assert abs(samp_revert.mean("sigma8") - samp_thin.mean("sigma8")) < 0.01
    assert not products["stats"]["points_removed"]

    # no remove
    info_post = {
        'add': {'params': {'h': None}, "likelihood": {"test_likelihood2": likelihood2}},
        'suffix': 'test2', 'skip': 0.2, 'thin': 4}
    output_info, products = run(updated_info, override={'post': info_post}, force=True)
    results2 = mpi.allgather(products["sample"])
    samp2 = MCSamplesFromCobaya(output_info, results2)
    assert "chi2__type1" in samp2.paramNames.list()
    # check what has been saved to disk is consistent
    samps4 = loadMCSamples(updated_info['output'] + '.post.test2')
    assert samp2.paramNames.list() == samps4.paramNames.list()
    assert np.isclose(samp2.mean("sigma8"), samps4.mean("sigma8"))

    # adding new theory derived
    info_post['add']['theory'] = {'new_param_theory': BTheory}
    output_info, products = run(updated_info, override={'post': info_post}, output=False)
    results3 = mpi.allgather(products["sample"])
    samp3 = MCSamplesFromCobaya(output_info, results3)
    assert np.isclose(samp3.mean("sigma8"), samp2.mean("sigma8"))
    assert np.isclose(samp3.mean("As1000"), samp2.mean("As") * 1000)

    info_post['add']['theory'] = {'new_param_theory': CTheory}
    with pytest.raises(LoggedError) as e, NoLogging(logging.ERROR):
        run(updated_info, override={'post': info_post}, output=False)
    assert 'Parameter AsX no known value' in str(e)
Esempio n. 11
0
    labels = ["q", r"$\theta$", "y", "x", r"$\theta_E$", r"$\alpha$"]
    #runner = "runner__lens__sie__source__ellipticalsersic_and_kinematics__data__lens__sie__source__ellipticalsersic_and_kinematics"
    #truths = [0.75, 45.0, 0.0, 0.0, 1.0, 0.75, 45.0, 0.5, 1.0, 0.0, 0.0, 0.00005, 0.5, 30.0, 50.0, 0.05, 0.0, 0.0, 16.0, 5.0, 200.0, 50.0]

    optimizer_directory = "./output_cosma/{}/phase_1__version_0.45.0/optimizer_backup".format(
        runner)

    # # NOTE:
    #
    # samples = np.loadtxt(
    #     "{}/{}".format(optimizer_directory, "multinest.txt")
    # )
    # print(samples.shape)
    # exit()

    samples = mcsamples.loadMCSamples(
        "{}/multinest".format(optimizer_directory))

    def get_params(samples):

        params = [name for name in samples.paramNames.names]

        return params
        # if str(name).startswith(
        #     "galaxies_{}".format(galaxy)
        # )

    params = get_params(samples=samples)

    plotter = plots.get_single_plotter(width_inch=16)
    plotter.triangle_plot(
        samples,
Esempio n. 12
0
import matplotlib.pyplot as plt
import matplotlib
from getdist import plots
from getdist.chains import Chains
from getdist.mcsamples import MCSamples, loadMCSamples
import numpy as np
import pandas


matplotlib.use('Qt5Agg')
# g = plots.get_subplot_plotter(chain_dir=r'/home/app/Git/LCDM-NS/cobaya/cobaya_output')

# chains = loadMCSamples(r'/home/app/Git/LCDM-NS/cobaya/cobaya_output/run)
chains = loadMCSamples(r'/Users/app/Git/LCDM-NS/cobaya/cobaya_output/run')
# roots = ['run']
params = ['logA', 'n_s', 'theta_s_1e2', 'omega_b', 'omega_cdm', 'tau_reio']
# g.triangle_plot(roots, params, filled=True, shaded=True)
# plt.show()

# The fact that this doesn't work, is living proof that Anthony Lewis should stick to science.
# This is very poorly thought out, and I'm surprised so many people are using any of his programs.
# print(chains.getMeans(['logA']))

chains.getParamNames().saveAsText('paramnames')
<<<<<<< HEAD

print(np.linalg.inv(chains.getCov(27)))
=======
np.savetxt('means', chains.getMeans(pars=range(27)))
print( chains.getCov(27))
print(np.linalg.inv(chains.getCov(27)))
Esempio n. 13
0
# ## Plot with getdist

# In[19]:

# Export the results to GetDist

from getdist.mcsamples import loadMCSamples
import matplotlib.pyplot as plt
gd_sample = loadMCSamples("chains/")

# Analyze and plot
mean = gd_sample.getMeans()
covmat = gd_sample.getCovMat().matrix
print("Mean:")
print(mean)
print("Covariance matrix:")
print(covmat)
# %matplotlib inline  # uncomment if running from the Jupyter notebook
import getdist.plots as gdplt

true_a = 10
true_b = 2
true_c = -3

gdplot = gdplt.get_subplot_plotter()
gdplot.triangle_plot(gd_sample, ["a", "b", "c"],
                     filled=True,
                     markers={
                         "a": true_a,
                         "b": true_b,
                         "c": true_c
Esempio n. 14
0
# Export the results to GetDist
from getdist.mcsamples import loadMCSamples
# Notice loadMCSamples requires a *full path*
import os

gd_sample = loadMCSamples(os.path.abspath(info["output"]))
# Analyze and plot
# [Exactly the same here...]
Esempio n. 15
0
        "output": "%s/%s/mcmc" % (output_dir, act_map),
        "force": True
    }

    updated_info, sampler = run(info)

# Chain analysis

out_cal_dict = {}

fig, axes = plt.subplots(1, 6, figsize=(15, 5))

for i, act_map in enumerate(reference_maps):

    chains = "%s/%s/mcmc" % (output_dir, act_map)
    samples = loadMCSamples(chains, settings={"ignore_rows": 0.5})
    mean_calib = samples.getMeans(pars=[0])[0]
    print(samples.getLikeStats())
    std_calib = np.sqrt(samples.getCovMat().matrix[0, 0])
    out_cal_dict[act_map] = [mean_calib, std_calib]
    cal_post = samples.get1DDensity('c')
    x = np.linspace(mean_calib - 4 * std_calib, mean_calib + 4 * std_calib,
                    100)
    y = cal_post.Prob(x)
    axes[i].grid(True, ls="dotted")
    axes[i].plot(x, y, color="tab:red", lw=2)
    axes[i].set_xlabel(r"$c\_{%s}$" % act_map.replace("_", "\_"), fontsize=12)

plt.tight_layout()
plt.savefig("%s/planck_all_calibs.pdf" % output_dir)
pickle.dump(out_cal_dict,
Esempio n. 16
0
 def getMCSamples(self, ini=None, settings={}):
     return loadMCSamples(self.chainRoot, jobItem=self, ini=ini, settings=settings)