CosmoFit.ChainFile

class CosmoFit.ChainFile(path, name='mcmc')[source]

Bases: object

An HDF5 file holding one MCMC chain, plus the CosmoFit metadata describing it.

Wraps emcee.backends.HDFBackend – which owns the chain itself and needs no help – with the two things a chain has to carry to be re-usable months later: what posterior it came from, and how to check that against the fit in front of you.

Normally built for you by Fitter.run_mcmc(save="..."). Construct one directly to put several chains in one file (name=), or to inspect a file without a fitter.

Parameters:
  • path (str or Path) – The .h5 file. Parent directories are created on write.

  • name (str) – HDF5 group holding this chain, so one file can hold several (e.g. one per model in a comparison). Defaults to emcee’s own "mcmc".

Examples

>>> chain = ChainFile("chains/models.h5", name="CPL")
>>> chain.exists, chain.iteration
(True, 6000)
__init__(path, name='mcmc')[source]
Parameters:

Methods

__init__(path[, name])

backend([read_only])

An emcee.backends.HDFBackend for this chain -- what gets handed to emcee.EnsembleSampler(backend=...), and what every read below goes through.

info()

A short, printable description of the file: what's in it and how far it got.

open()

Read-only access to the stored chain, as a stand-in for the emcee.EnsembleSampler that produced it -- see StoredSampler.

reset(nwalkers, ndim)

Throw away this chain's stored steps and start it empty at (nwalkers, ndim).

write_metadata(meta)

Store meta (JSON-encoded) next to the chain, replacing whatever was there.

Attributes

exists

Whether the file exists and already holds this chain's group.

iteration

Number of steps already stored (0 if the chain doesn't exist yet).

metadata

The CosmoFit metadata stored alongside the chain (an empty dict for a chain written by plain emcee, or one that doesn't exist yet).

shape

(nwalkers, ndim) of the stored chain, or None if there isn't one.