Skip to content

Commit

Permalink
Updating to most recent version of cobaya (#1)
Browse files Browse the repository at this point in the history
* Add description for Hillipop/Lollipop likelihoods (CobayaSampler#385)

* Add description for Hillipop/Lollipop likelihoods

* correct trailing whitespace

* Update tests

* Put below the CamSpec NPIPE

* Also below CamSpec for the gui

* fix planck bibs

* config defaults to {} if yaml empty

* tidy numpy 2 np.int64 output log

* tidy numpy 2 np.float64 output log

* debug log for ignored CAMB errors

---------

Co-authored-by: Matthieu TRISTRAM <[email protected]>
Co-authored-by: Antony Lewis <[email protected]>
  • Loading branch information
3 people authored Oct 29, 2024
1 parent 6000ccd commit c81e696
Show file tree
Hide file tree
Showing 14 changed files with 70 additions and 34 deletions.
42 changes: 33 additions & 9 deletions cobaya/cosmo_input/input_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -415,8 +415,8 @@

like_cmb: InfoDict = {
none: {},
"planck_NPIPE": {
"desc": "Planck NPIPE (native; polarized NPIPE CMB + lensing)",
"planck_NPIPE_CamSpec": {
"desc": "Planck NPIPE CamSpec (native; polarized NPIPE CMB + lensing)",
"sampler": cmb_sampler_recommended,
"theory": {theo: {"extra_args": cmb_precision[theo]}
for theo in ["camb", "classy"]},
Expand All @@ -427,6 +427,22 @@
"planckpr4lensing":
{'package_install': {'github_repository': 'carronj/planck_PR4_lensing',
'min_version': '1.0.2'}}}},
"planck_NPIPE_Hillipop": {
"desc": "Planck NPIPE Hillipop+Lollipop (polarized NPIPE CMB + lensing)",
"sampler": cmb_sampler_recommended,
"theory": {theo: {"extra_args": cmb_precision[theo]}
for theo in ["camb", "classy"]},
"likelihood": {
"planck_2018_lowl.TT": None,
"planck_2020_lollipop.lowlE":
{'package_install': {'pip': 'planck-npipe/lollipop',
'min_version': '4.1.1'}},
"planck_2020_hillipop.TTTEEE":
{'package_install': {'pip': 'planck-npipe/hillipop',
'min_version': '4.2.2'}},
"planckpr4lensing":
{'package_install': {'github_repository': 'carronj/planck_PR4_lensing',
'min_version': '1.0.2'}}}},
"planck_2018": {
"desc": "Planck 2018 (Polarized CMB + lensing)",
"sampler": cmb_sampler_recommended,
Expand Down Expand Up @@ -610,14 +626,22 @@
preset: InfoDict = dict([
(none, {"desc": "(No preset chosen)"}),
# Pure CMB #######################################################
("planck_NPIPE_camb", {
"desc": "Planck NPIPE with CAMB (all native Python)",
("planck_NPIPE_CamSpec_camb", {
"desc": "Planck NPIPE CamSpec with CAMB (all native Python)",
"theory": "camb",
"like_cmb": "planck_NPIPE_CamSpec"}),
("planck_NPIPE_CamSpec_classy", {
"desc": "Planck NPIPE CamSpec with CLASS (all native Python)",
"theory": "classy",
"like_cmb": "planck_NPIPE_CamSpec"}),
("planck_NPIPE_Hillipop_camb", {
"desc": "Planck NPIPE Hillipop+Lollipop with CAMB (all native Python)",
"theory": "camb",
"like_cmb": "planck_NPIPE"}),
("planck_NPIPE_classy", {
"desc": "Planck NPIPE with CLASS (all native Python)",
"like_cmb": "planck_NPIPE_Hillipop"}),
("planck_NPIPE_Hillipop_classy", {
"desc": "Planck NPIPE Hillipop+Lollipop with CLASS (all native Python)",
"theory": "classy",
"like_cmb": "planck_NPIPE"}),
"like_cmb": "planck_NPIPE_Hillipop"}),
("planck_2018_camb", {
"desc": "Planck 2018 with CAMB",
"theory": "camb",
Expand Down Expand Up @@ -745,7 +769,7 @@
# BASIC INSTALLATION #####################################################################
install_basic: InfoDict = {
"theory": theory,
"likelihood": dict(like_cmb["planck_NPIPE"]["likelihood"], **{
"likelihood": dict(like_cmb["planck_NPIPE_CamSpec"]["likelihood"], **{
# 2018 lensing ensured covmat database also installed
"planck_2018_lensing.native": None,
"sn.pantheon": None,
Expand Down
2 changes: 1 addition & 1 deletion cobaya/likelihood.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def calculate(self, state, want_derived=True, **params_values_dict):
derived: Optional[ParamValuesDict] = {} if want_derived else None
state["logp"] = -np.inf # in case of exception
state["logp"] = self.logp(_derived=derived, **params_values_dict)
self.log.debug("Computed log-likelihood = %r", state["logp"])
self.log.debug("Computed log-likelihood = %s", state["logp"])
if derived is not None:
state["derived"] = derived.copy()

Expand Down
6 changes: 4 additions & 2 deletions cobaya/likelihoods/base_classes/planck_2018_CamSpec_python.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,10 @@ class Planck2018CamSpecPython(DataSetLikelihood):

@classmethod
def get_bibtex(cls):
from cobaya.likelihoods.base_classes import Planck2018Clik
return Planck2018Clik.get_bibtex()
if not (res := super().get_bibtex()):
from cobaya.likelihoods.base_classes import Planck2018Clik
return Planck2018Clik.get_bibtex()
return res

def read_normalized(self, filename, pivot=None):
# arrays all based at L=0, in L(L+1)/2pi units
Expand Down
6 changes: 6 additions & 0 deletions cobaya/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ def format(self, record):
"%(message)s")
self._style._fmt = fmt
return super().format(record)

# Configure stdout handler
handle_stdout = logging.StreamHandler(sys.stdout)
handle_stdout.setLevel(level)
Expand Down Expand Up @@ -300,3 +301,8 @@ def mpi_info(self, msg, *args, **kwargs):
@mpi.root_only
def mpi_debug(self, msg, *args, **kwargs):
self.log.debug(msg, *args, **kwargs)

def param_dict_debug(self, msg, dic: dict):
"""Removes numpy2 np.float64 for consistent output"""
if self.log.getEffectiveLevel() <= logging.DEBUG:
self.log.debug(msg, {k: float(v) for k, v in dic.items()})
19 changes: 9 additions & 10 deletions cobaya/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class LogPosterior:
A consistency check will be performed if initialized simultaneously with
log-posterior, log-priors and log-likelihoods, so, for faster initialisation,
you may prefer to pass log-priors and log-likelhoods only, and only pass all three
you may prefer to pass log-priors and log-likelihoods only, and only pass all three
(so that the test is performed) only when e.g. loading from an old sample.
If ``finite=True`` (default: False), it will try to represent infinities as the
Expand Down Expand Up @@ -368,7 +368,7 @@ def _loglikes_input_params(
outpar_dict: ParamValuesDict = {}
compute_success = True
self.provider.set_current_input_params(input_params)
self.log.debug("Got input parameters: %r", input_params)
self.param_dict_debug("Got input parameters: %r", input_params)
loglikes = np.zeros(len(self.likelihood))
need_derived = self.requires_derived or return_derived or return_output_params
for (component, like_index), param_dep in zip(self._component_order.items(),
Expand All @@ -391,7 +391,7 @@ def _loglikes_input_params(
raise LoggedError(
self.log,
"Likelihood %s has not returned a valid log-likelihood, "
"but %r instead.", component,
"but %s instead.", component,
component.current_logp) from type_excpt
if make_finite:
loglikes = np.nan_to_num(loglikes)
Expand All @@ -413,7 +413,7 @@ def _loglikes_input_params(
else list(outpar_dict.values()))
else: # explicitly derived, instead of output params
derived_dict = self.parameterization.to_derived(outpar_dict)
self.log.debug("Computed derived parameters: %s", derived_dict)
self.param_dict_debug("Computed derived parameters: %s", derived_dict)
return_params = (derived_dict if as_dict
else list(derived_dict.values()))
return return_likes, return_params
Expand Down Expand Up @@ -537,12 +537,12 @@ def logposterior(self,
self.log.debug(
"Posterior to be computed for parameters %s",
dict(zip(self.parameterization.sampled_params(),
params_values_array)))
params_values_array.astype(float))))
if not np.all(np.isfinite(params_values_array)):
raise LoggedError(
self.log, "Got non-finite parameter values: %r",
dict(zip(self.parameterization.sampled_params(),
params_values_array)))
params_values_array.astype(float))))
# Notice that we don't use the make_finite in the prior call,
# to correctly check if we have to compute the likelihood
logpriors_1d = self.prior.logps_internal(params_values_array)
Expand Down Expand Up @@ -591,8 +591,8 @@ def logpost(self,

def get_valid_point(self, max_tries: int, ignore_fixed_ref: bool = False,
logposterior_as_dict: bool = False, random_state=None,
) -> Union[Tuple[np.ndarray, LogPosterior],
Tuple[np.ndarray, dict]]:
) \
-> Union[Tuple[np.ndarray, LogPosterior], Tuple[np.ndarray, dict]]:
"""
Finds a point with finite posterior, sampled from the reference pdf.
Expand Down Expand Up @@ -1087,8 +1087,7 @@ def _assign_params(self, info_likelihood, info_theory=None,
# Update infos! (helper theory parameters stored in yaml with host)
inf = (info_likelihood if component in self.likelihood.values() else
info_theory)
inf = inf.get(component.get_name())
if inf:
if inf := inf.get(component.get_name()):
inf.pop("params", None)
inf[option] = component.get_attr_list_with_helpers(option)
if self.is_debug_and_mpi_root():
Expand Down
2 changes: 1 addition & 1 deletion cobaya/prior.py
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,7 @@ def logps_internal(self, x: np.ndarray) -> float:
if len(self._non_uniform_indices) else 0)
else:
logps = -np.inf
self.log.debug("Got logpriors (internal) = %r", logps)
self.log.debug("Got logpriors (internal) = %s", logps)
return logps

def logps_external(self, input_params) -> List[float]:
Expand Down
8 changes: 4 additions & 4 deletions cobaya/samplers/mcmc/mcmc.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,8 @@ def initialize(self):
# If resuming but no existing chains, assume failed run and ignore blocking
# if speeds measurement requested
if (
self.output.is_resuming() and not existing_chains_any_process and
self.measure_speeds
self.output.is_resuming() and not existing_chains_any_process and
self.measure_speeds
):
self.blocking = None
if self.measure_speeds and self.blocking:
Expand Down Expand Up @@ -746,8 +746,8 @@ def check_convergence_and_learn_proposal(self):
condition_number = Rminus1 / min(np.abs(eigvals))
self.log.debug(" - Condition number = %g", condition_number)
self.log.debug(" - Eigenvalues = %r", eigvals)
accpt_multi_str = \
" = sum(%r)" % list(Ns) if more_than_one_process() else ""
accpt_multi_str = " = sum(%r)" % Ns.astype(int).tolist() \
if more_than_one_process() else ""
self.log.info(
" - Convergence of means: R-1 = %f after %d accepted steps%s",
Rminus1, sum(Ns), accpt_multi_str)
Expand Down
4 changes: 3 additions & 1 deletion cobaya/theories/camb/camb.py
Original file line number Diff line number Diff line change
Expand Up @@ -888,14 +888,16 @@ def set(self, params_values_dict, state):
else:
self.log.debug("Out of bounds parameters. "
"Assigning 0 likelihood and going on.")
except (self.camb.baseconfig.CAMBValueError, self.camb.baseconfig.CAMBError):
except (self.camb.baseconfig.CAMBValueError, self.camb.baseconfig.CAMBError) as e:
if self.stop_at_error:
self.log.error(
"Error setting parameters (see traceback below)! "
"Parameters sent to CAMB: %r and %r.\n"
"To ignore this kind of error, make 'stop_at_error: False'.",
dict(state["params"]), dict(self.extra_args))
raise
else:
self.log.debug("Error setting parameters: %s", e)
except self.camb.baseconfig.CAMBUnknownArgumentError as e:
raise LoggedError(
self.log,
Expand Down
2 changes: 1 addition & 1 deletion cobaya/theories/classy/classy.py
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ def set(self, params_values_dict):
args = {self.translate_param(p): v for p, v in params_values_dict.items()}
args.update(self.extra_args)
# Generate and save
self.log.debug("Setting parameters: %r", args)
self.param_dict_debug("Setting parameters: %r", args)
self.classy.set(**args)

def calculate(self, state, want_derived=True, **params_values_dict):
Expand Down
3 changes: 1 addition & 2 deletions cobaya/theories/cosmo/boltzmannbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,7 @@ def check_no_repeated_input_extra(self):
Should be called at initialisation, and at the end of every call to must_provide()
"""
common = set(self.input_params).intersection(self.extra_args)
if common:
if common := set(self.input_params).intersection(self.extra_args):
raise LoggedError(
self.log, "The following parameters appear both as input parameters and "
"as extra arguments: %s. Please, remove one of the definitions "
Expand Down
2 changes: 1 addition & 1 deletion cobaya/theory.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ def check_cache_and_compute(self, params_values_dict,
params_values_dict.update(
zip(self._input_params_extra,
self.provider.get_param(self._input_params_extra)))
self.log.debug("Got parameters %r", params_values_dict)
self.param_dict_debug("Got parameters %r", params_values_dict)
state = None
if cached:
for _state in self._states:
Expand Down
2 changes: 1 addition & 1 deletion cobaya/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -993,7 +993,7 @@ def load_config_file():
from cobaya.yaml import yaml_load_file
try:
return yaml_load_file(
os.path.join(get_config_path(), packages_path_config_file))
os.path.join(get_config_path(), packages_path_config_file)) or {}
except:
return {}

Expand Down
2 changes: 1 addition & 1 deletion tests/test_cosmo_docs_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

path = os.path.join(docs_folder, "src_examples", "cosmo_basic")
file_pre = "basic_"
preset_pre = "planck_NPIPE_"
preset_pre = "planck_NPIPE_CamSpec_"


def test_cosmo_docs_basic():
Expand Down
4 changes: 4 additions & 0 deletions tests/test_scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ def test_doc():


def test_bib(tmpdir):

with stdout_check('rosenberg22'):
bib_script(['planck_NPIPE_highl_CamSpec.TTTEEE'])

with stdout_check('Neal:2005'):
bib_script(['des_y1.shear', 'camb', 'mcmc'])

Expand Down

0 comments on commit c81e696

Please sign in to comment.