def test_prior_inherit_differentgiven(): test_info = deepcopy(test_info_common) test_info["prior"] = {"third": "lambda a1: 1"} updated_info, _ = run(test_info) likname = list(test_info_common["likelihood"])[0] default_info = get_default_info(likname, "likelihood") default_info["prior"].update(test_info["prior"]) assert updated_info["prior"] == default_info["prior"]
def test_prior_inherit_samegiven(): test_info = deepcopy(test_info_common) likname = list(test_info_common["likelihood"])[0] default_info = get_default_info(likname, "likelihood") name, prior = deepcopy(default_info["prior"]).popitem() test_info["prior"] = {name: prior} updated_info, _ = run(test_info) assert updated_info["prior"] == default_info["prior"]
def test_prior_inherit_differentgiven(): test_info = deepcopy(test_info_common) test_info[_prior] = {"third": "lambda a1: 1"} updated_info, products = run(test_info) likname = list(test_info_common[_likelihood].keys())[0] default_info = get_default_info(likname, _likelihood) default_info[_prior].update(test_info[_prior]) assert updated_info[_prior] == default_info[_prior]
def test_prior_inherit_samegiven_differentdefinition(): test_info = deepcopy(test_info_common) likname = list(test_info_common[_likelihood].keys())[0] default_info = get_default_info(likname, _likelihood) name, prior = deepcopy(default_info[_prior]).popitem() test_info[_prior] = {name: "this is not a prior"} with pytest.raises(HandledException): updated_info, products = run(test_info)
def test_prior_inherit_samegiven(): test_info = deepcopy(test_info_common) likname = list(test_info_common[_likelihood].keys())[0] default_info = get_default_info(likname, _likelihood) name, prior = deepcopy(default_info[_prior]).popitem() test_info[_prior] = {name: prior} updated_info, products = run(test_info) assert updated_info[_prior] == default_info[_prior]
def test_prior_inherit_samegiven_differentdefinition(): test_info = deepcopy(test_info_common) likname = list(test_info_common[kinds.likelihood])[0] default_info = get_default_info(likname, kinds.likelihood) name, prior = deepcopy(default_info[_prior]).popitem() test_info[_prior] = {name: "this is not a prior"} with pytest.raises(LoggedError): updated_info, products = run(test_info)
def test_prior_inherit_samegiven_differentdefinition(): test_info = deepcopy(test_info_common) likname = list(test_info_common["likelihood"])[0] default_info = get_default_info(likname, "likelihood") name, prior = deepcopy(default_info["prior"]).popitem() test_info["prior"] = {name: "this is not a prior"} with pytest.raises(LoggedError): run(test_info)
def test_run_file(tmpdir): input_file = os.path.join(tmpdir, 'pars.yaml') root = os.path.join(tmpdir, 'test') yaml_dump_file(input_file, dict(test_info_common, output=root)) run_script([input_file, '--force']) likname = list(test_info_common["likelihood"])[0] default_info = get_default_info(likname, "likelihood") updated_info = yaml_load_file(root + '.updated.yaml') assert updated_info["prior"] == default_info["prior"]
def test_inherit_label_and_bounds(): test_info = deepcopy(test_info_common) likname = list(test_info_common[kinds.likelihood])[0] default_info_params = get_default_info(likname, kinds.likelihood)[_params] test_info[_params] = deepcopy(default_info_params) test_info[_params]["a1"].pop(partag.latex, None) # Remove limits, so they are inherited test_info = deepcopy(test_info_common) test_info[_params] = deepcopy(default_info_params) test_info[_params]["b1"].pop("min") test_info[_params]["b1"].pop("max") updated_info, products = run(test_info) assert updated_info[_params]["b1"]["min"] == default_info_params["b1"][ "min"] assert updated_info[_params]["b1"]["max"] == default_info_params["b1"][ "max"]
def test_inherit_label_and_bounds(): test_info = deepcopy(test_info_common) likname = list(test_info_common["likelihood"])[0] default_info_params = get_default_info(likname, "likelihood")["params"] test_info["params"] = deepcopy(default_info_params) test_info["params"]["a1"].pop("latex", None) # Remove limits, so they are inherited test_info = deepcopy(test_info_common) test_info["params"] = deepcopy(default_info_params) test_info["params"]["b1"].pop("min") test_info["params"]["b1"].pop("max") updated_info, _ = run(test_info) assert updated_info["params"]["b1"]["min"] == default_info_params["b1"][ "min"] assert updated_info["params"]["b1"]["max"] == default_info_params["b1"][ "max"]
def __init__(self, kind, component, parent=None): super().__init__() self.clipboard = parent.clipboard self.setWindowTitle("%s : %s" % (kind, component)) self.setGeometry(0, 0, 500, 500) # noinspection PyArgumentList self.move(QApplication.desktop().screenGeometry().center() - self.rect().center()) self.show() # Main layout self.layout = QVBoxLayout() self.setLayout(self.layout) self.display_tabs = QTabWidget() self.display = {} for k in ["yaml", "python", "bibliography"]: self.display[k] = QTextEdit() self.display[k].setLineWrapMode(QTextEdit.NoWrap) self.display[k].setFontFamily("mono") self.display[k].setCursorWidth(0) self.display[k].setReadOnly(True) self.display_tabs.addTab(self.display[k], k) self.layout.addWidget(self.display_tabs) # Fill text defaults_txt = get_default_info(component, kind, return_yaml=True) _indent = " " defaults_txt = (kind + ":\n" + _indent + component + ":\n" + 2 * _indent + ("\n" + 2 * _indent).join(defaults_txt.split("\n"))) from cobaya.yaml import yaml_load self.display["python"].setText(pformat(yaml_load(defaults_txt))) self.display["yaml"].setText(defaults_txt) self.display["bibliography"].setText(get_bib_component( component, kind)) # Buttons self.buttons = QHBoxLayout() self.close_button = QPushButton('Close', self) self.copy_button = QPushButton('Copy to clipboard', self) self.buttons.addWidget(self.close_button) self.buttons.addWidget(self.copy_button) self.close_button.released.connect(self.close) self.copy_button.released.connect(self.copy_clipb) self.layout.addLayout(self.buttons)
def __init__(self, kind, module, parent=None): super(DefaultsDialog, self).__init__() self.clipboard = parent.clipboard self.setWindowTitle("%s : %s" % (kind, module)) self.setGeometry(0, 0, 500, 500) self.move( QApplication.desktop().screenGeometry().center() - self.rect().center()) self.show() # Main layout self.layout = QVBoxLayout() self.setLayout(self.layout) self.display_tabs = QTabWidget() self.display = {} for k in ["yaml", "python", "bibliography"]: self.display[k] = QTextEdit() self.display[k].setLineWrapMode(QTextEdit.NoWrap) self.display[k].setFontFamily("mono") self.display[k].setCursorWidth(0) self.display[k].setReadOnly(True) self.display_tabs.addTab(self.display[k], k) self.layout.addWidget(self.display_tabs) # Fill text defaults_txt = get_default_info( module, kind, return_yaml=True, fail_if_not_found=True) from cobaya.yaml import yaml_load self.display["python"].setText( "from collections import OrderedDict\n\ninfo = " + pformat(yaml_load(defaults_txt))) self.display["yaml"].setText(defaults_txt) self.display["bibliography"].setText(get_bib_module(module, kind)) # Buttons self.buttons = QHBoxLayout() self.close_button = QPushButton('Close', self) self.copy_button = QPushButton('Copy to clipboard', self) self.buttons.addWidget(self.close_button) self.buttons.addWidget(self.copy_button) self.close_button.released.connect(self.close) self.copy_button.released.connect(self.copy_clipb) self.layout.addLayout(self.buttons)
def test_inherit_label_and_bounds(): test_info = deepcopy(test_info_common) likname = list(test_info_common[_likelihood].keys())[0] default_info_params = get_default_info(likname, _likelihood)[_params] test_info[_params] = deepcopy(default_info_params) test_info[_params]["a1"].pop(_p_label, None) # First, change one limit (so no inheritance at all) test_info[_params]["b1"].pop("min") new_max = 2 test_info[_params]["b1"]["max"] = new_max updated_info, products = run(test_info) assert updated_info[_params]["a1"] == default_info_params["a1"] assert updated_info[_params]["b1"].get("min") is None assert updated_info[_params]["b1"]["max"] == new_max # Second, remove limits, so they are inherited test_info = deepcopy(test_info_common) test_info[_params] = deepcopy(default_info_params) test_info[_params]["b1"].pop("min") test_info[_params]["b1"].pop("max") updated_info, products = run(test_info) assert updated_info[_params]["b1"]["min"] == default_info_params["b1"]["min"] assert updated_info[_params]["b1"]["max"] == default_info_params["b1"]["max"]
def get_product_id_and_clik_file(name): """Gets the PLA product info from the defaults file.""" defaults = get_default_info(name, _likelihood)[_likelihood][name] return defaults.get("product_id"), defaults.get("clik_file")
def doc_script(args=None): """Command line script for the documentation.""" warn_deprecation() logger_setup() logger = get_logger("doc") # Parse arguments import argparse parser = argparse.ArgumentParser( prog="cobaya doc", description="Prints defaults for Cobaya's components.") parser.add_argument( "component", action="store", nargs="?", default="", metavar="component_name", help=("The component whose defaults are requested. " "Pass a component kind (sampler, theory, likelihood) to " "list all available (internal) ones, pass nothing to list " "all available (internal) components of all kinds.")) parser.add_argument("-p", "--python", action="store_true", default=False, help="Request Python instead of YAML.") expand_flag, expand_flag_ishort = "expand", 1 parser.add_argument("-" + expand_flag[expand_flag_ishort], "--" + expand_flag, action="store_true", default=False, help="Expand YAML defaults.") arguments = parser.parse_args(args) # Nothing passed: list all if not arguments.component: msg = "Available components: (some may need external code/data)" print(msg + "\n" + "-" * len(msg)) for kind in kinds: print("%s:" % kind) print(_indent + ("\n" + _indent).join(get_available_internal_class_names(kind))) return # A kind passed (plural or singular): list all of that kind if arguments.component.lower() in subfolders.values(): arguments.component = next(k for k in subfolders if arguments.component == subfolders[k]) if arguments.component.lower() in kinds: print("%s:" % arguments.component.lower()) print(_indent + ("\n" + _indent).join( get_available_internal_class_names(arguments.component.lower()))) return # Otherwise, try to identify the component try: cls = get_component_class(arguments.component, logger=logger) except ComponentNotFoundError: suggestions = similar_internal_class_names(arguments.component) logger.error( f"Could not identify component '{arguments.component}'. " f"Did you mean any of the following? {suggestions} (mind capitalization!)" ) return 1 to_print = get_default_info(cls, return_yaml=not arguments.python, yaml_expand_defaults=arguments.expand) if arguments.python: print(pformat({cls.get_kind(): {arguments.component: to_print}})) else: print(cls.get_kind() + ":\n" + _indent + arguments.component + ":\n" + 2 * _indent + ("\n" + 2 * _indent).join(to_print.split("\n"))) if "!defaults" in to_print: print("# This file contains defaults. " "To populate them, use the flag --%s (or -%s)." % (expand_flag, expand_flag[expand_flag_ishort]))
def doc_script(): from cobaya.mpi import am_single_or_primary_process if not am_single_or_primary_process(): return warn_deprecation() # Parse arguments import argparse parser = argparse.ArgumentParser( description="Prints defaults for Cobaya's internal modules.") parser.add_argument("module", action="store", nargs="?", default="", metavar="module_name", help="The module whose defaults are requested.") kind_opt, kind_opt_ishort = "kind", 0 parser.add_argument( "-" + kind_opt[kind_opt_ishort], "--" + kind_opt, action="store", nargs=1, default=None, metavar="module_kind", help=("Kind of module whose defaults are requested: " + ", ".join(['%s' % kind for kind in _kinds]) + ". " + "Use only when module name is not unique (it would fail).")) parser.add_argument("-p", "--python", action="store_true", default=False, help="Request Python instead of YAML.") expand_flag, expand_flag_ishort = "expand", 1 parser.add_argument("-" + expand_flag[expand_flag_ishort], "--" + expand_flag, action="store_true", default=False, help="Expand YAML defaults.") arguments = parser.parse_args() # Remove plurals (= name of src subfolders), for user-friendliness if arguments.module.lower() in subfolders.values(): arguments.module = next(k for k in subfolders if arguments.module == subfolders[k]) # Kind given, list all if not arguments.module: msg = "Available modules: (some may need external code/data)" print(msg + "\n" + "-" * len(msg)) for kind in _kinds: print("%s:" % kind) print(_indent + ("\n" + _indent).join(get_available_modules(kind))) return # Kind given: list all modules of that kind if arguments.module.lower() in _kinds: print("%s:" % arguments.module.lower()) print(_indent + ("\n" + _indent).join(get_available_modules(arguments.module.lower()))) return # Otherwise, check if it's a unique module name try: if arguments.kind: arguments.kind = arguments.kind[0].lower() to_print = get_default_info(arguments.module, arguments.kind, return_yaml=not arguments.python, yaml_expand_defaults=arguments.expand, fail_if_not_found=True) if arguments.python: print(import_odict + pformat(to_print)) else: print(to_print) if "!defaults" in to_print: print("# This file contains defaults. " "To populate them, use the flag --%s (or -%s)." % (expand_flag, expand_flag[expand_flag_ishort])) except: if not arguments.kind: print("Specify its kind with '--%s [module_kind]'." % kind_opt) return 1 return
def doc_script(): from cobaya.mpi import is_main_process if not is_main_process(): return warn_deprecation() # Parse arguments import argparse parser = argparse.ArgumentParser( description="Prints defaults for Cobaya's components.") parser.add_argument("component", action="store", nargs="?", default="", metavar="component_name", help="The component whose defaults are requested.") kind_opt, kind_opt_ishort = "kind", 0 parser.add_argument( "-" + kind_opt[kind_opt_ishort], "--" + kind_opt, action="store", nargs=1, default=None, metavar="component_kind", help=("Kind of component whose defaults are requested: " + ", ".join(['%s' % kind for kind in kinds]) + ". " + "Use only when component name is not unique (it would fail).")) parser.add_argument("-p", "--python", action="store_true", default=False, help="Request Python instead of YAML.") expand_flag, expand_flag_ishort = "expand", 1 parser.add_argument("-" + expand_flag[expand_flag_ishort], "--" + expand_flag, action="store_true", default=False, help="Expand YAML defaults.") arguments = parser.parse_args() # Remove plurals (= name of src subfolders), for user-friendliness if arguments.component.lower() in subfolders.values(): arguments.component = next(k for k in subfolders if arguments.component == subfolders[k]) # Kind given, list all if not arguments.component: msg = "Available components: (some may need external code/data)" print(msg + "\n" + "-" * len(msg)) for kind in kinds: print("%s:" % kind) print(_indent + ("\n" + _indent).join(get_available_internal_class_names(kind))) return # Kind given: list all components of that kind if arguments.component.lower() in kinds: print("%s:" % arguments.component.lower()) print(_indent + ("\n" + _indent).join( get_available_internal_class_names(arguments.component.lower()))) return # Otherwise, check if it's a unique component name try: if arguments.kind: arguments.kind = arguments.kind[0].lower() if arguments.kind not in kinds: print("Kind %r not recognized. Try one of %r" % (arguments.kind, tuple(kinds))) raise ValueError else: arguments.kind = get_kind(arguments.component) to_print = get_default_info(arguments.component, arguments.kind, return_yaml=not arguments.python, yaml_expand_defaults=arguments.expand) if arguments.python: print(pformat({arguments.kind: {arguments.component: to_print}})) else: print(arguments.kind + ":\n" + _indent + arguments.component + ":\n" + 2 * _indent + ("\n" + 2 * _indent).join(to_print.split("\n"))) if "!defaults" in to_print: print("# This file contains defaults. " "To populate them, use the flag --%s (or -%s)." % (expand_flag, expand_flag[expand_flag_ishort])) except Exception: if isinstance(Exception, LoggedError.__class__): pass else: if not arguments.kind: print("Specify its kind with '--%s [component_kind]'." % kind_opt) return 1 return
def body_of_test(modules, best_fit, info_likelihood, info_theory, ref_chi2, best_fit_derived=None): assert modules, "I need a modules folder!" info = { _path_install: modules, _theory: info_theory, _likelihood: info_likelihood, _sampler: { "evaluate": None } } # Add best fit info[_params] = merge_params_info(*([yaml_load(baseline_cosmology)] + [ get_default_info(lik, _likelihood).get(_params, {}) for lik in info[_likelihood] ])) for p in best_fit: try: if _prior in info[_params].get(p, {}): info[_params][p]["ref"] = best_fit[p] except: # was fixed or derived info[_params][p] = best_fit[p] # We'll pop some derived parameters, so copy derived = deepcopy(baseline_cosmology_derived) best_fit_derived = deepcopy(best_fit_derived) if list(info[_theory].keys())[0] == "classy": # More stuff that CLASS needs for the Planck model info[_params].update(baseline_cosmology_classy_extra) # Remove "cosmomc_theta" in favour of "H0" (remove it from derived then!) info[_params].pop("cosmomc_theta") info[_params].pop("theta") info[_params]["H0"] = { _prior: { "min": 0, "max": 100 }, _p_ref: best_fit["H0"] } derived.pop("H0") if best_fit_derived is not None: best_fit_derived.pop("H0", None) # Don't test those that have not been implemented yet for p in [ "zstar", "rstar", "thetastar", "DAstar", "zdrag", "kd", "thetad", "zeq", "keq", "thetaeq", "thetarseq", # BBN!!! "DH", "Y_p" ]: derived.pop(p) best_fit_derived.pop(p, None) else: info[_theory]["camb"] = info[_theory]["camb"] or {} info[_theory]["camb"].update({"stop_at_error": True}) # Add derived if best_fit_derived is not None: info[_params].update(derived) updated_info, products = run(info) # Check value of likelihoods for lik in info[_likelihood]: chi2 = products["sample"][_chi2 + separator + lik][0] assert abs(chi2 - ref_chi2[lik]) < ref_chi2["tolerance"], ( "Testing likelihood '%s': | %g - %g | = %g >= %g" % (lik, chi2, ref_chi2[lik], abs(chi2 - ref_chi2[lik]), ref_chi2["tolerance"])) # Check value of derived parameters not_tested = [] not_passed = [] for p in best_fit_derived or {}: if best_fit_derived[p][0] is None or p not in best_fit_derived: not_tested += [p] continue rel = (abs(products["sample"][_derived_pre + p][0] - best_fit_derived[p][0]) / best_fit_derived[p][1]) if rel > tolerance_derived * (2 if p in ("YHe", "Y_p", "DH", "sigma8", "s8omegamp5") else 1): not_passed += [(p, rel)] print("Derived parameters not tested because not implemented: %r" % not_tested) assert not not_passed, "Some derived parameters were off: %r" % not_passed
def create_input(**kwargs): get_comments = kwargs.pop("get_comments", False) preset = kwargs.pop("preset", None) if preset: fields = deepcopy(input_database.preset[preset]) fields.update(kwargs) return create_input(get_comments=get_comments, **fields) # Need to copy to select theory. # Likelihoods always AT THE END! # (to check that sampled parameters are not redefined as derived) infos = odict() kwargs_params = [ "primordial", "geometry", "hubble", "matter", "neutrinos", "dark_energy", "bbn", "reionization" ] kwargs_likes = ["like_cmb", "like_bao", "like_sn", "like_H0"] for k in kwargs_params + kwargs_likes: if k not in kwargs: infos[k] = odict() try: infos[k] = deepcopy( getattr(input_database, k)[kwargs.get(k, input_database._none)]) except KeyError: raise ValueError("Unknown value '%s' for '%s'" % (kwargs.get(k, input_database._none), k)) theory_requested = kwargs.get(_theory) for i, (field, info) in enumerate(infos.items()): if not info: continue error_msg = info.pop(input_database._error_msg, None) try: info[_theory] = {theory_requested: info[_theory][theory_requested]} except KeyError: return ( "There is no preset for\n'%s'" % (info.get(input_database._desc, field)) + "with theory code '%s'." % theory_requested + ("\n--> " + error_msg if error_msg else "\nThis does not mean that you cannot use this model with that " "theory code; just that we have not implemented this yet.")) # Add non-translatable parameters (in info["theory"][classy|camb][params]) if _params not in info: info[_params] = odict() info[_params].update((info[_theory][theory_requested] or odict()).pop(_params, odict())) # Remove the *derived* parameters mentioned by the likelihoods that # are already *sampled* by some part of the model if field.startswith("like_") and _params in info: remove_derived = [] for p in info[_params]: if any([(p in info_part[_params]) for info_part in list(infos.values())[:i]]): remove_derived += [p] for p in remove_derived: info[_params].pop(p) # Prepare sampler info info_sampler = deepcopy( input_database.sampler.get(kwargs.get(_sampler), {})) if info_sampler: sampler_name = list(info_sampler[_sampler])[0] info_sampler[_sampler][ sampler_name] = info_sampler[_sampler][sampler_name] or {} # Add recommended options for samplers for info in infos.values(): this_info_sampler = info.pop(_sampler, {}) if sampler_name in this_info_sampler: info_sampler[_sampler][sampler_name].update( this_info_sampler[sampler_name]) # Reorder, to have the *parameters* shown in the correct order, and *merge* all_infos = [info_sampler] + [ infos[k] for k in kwargs_likes[::-1] + kwargs_params[::-1] ] comments = [info.pop(input_database._comment, None) for info in all_infos] comments = [c for c in comments if c] [info.pop(input_database._desc, None) for info in all_infos] merged = merge_info(*all_infos) # Simplify parameter infos for p, info in merged[_params].items(): merged[_params][p] = reduce_info_param(info) # Translate from Planck param names planck_to_theo = get_default_info( theory_requested, _theory)[_theory][theory_requested][_p_renames] if kwargs.get("planck_names", False): merged[_theory][ theory_requested] = merged[_theory][theory_requested] or {} merged[_theory][theory_requested]["use_planck_names"] = True else: merged_params_translated = odict([ translate(p, info, planck_to_theo) for p, info in merged[_params].items() ]) merged[_params] = merged_params_translated if get_comments and comments: merged[input_database._comment] = comments return merged
def test_prior_inherit_nonegiven(): updated_info, products = run(test_info_common) likname = list(test_info_common[_likelihood].keys())[0] default_info = get_default_info(likname, _likelihood) assert updated_info[_prior] == default_info[_prior]
def test_prior_inherit_nonegiven(): updated_info, _ = run(test_info_common) likname = list(test_info_common["likelihood"])[0] default_info = get_default_info(likname, "likelihood") assert updated_info["prior"] == default_info["prior"]
from copy import deepcopy from collections import OrderedDict as odict from six import string_types import numpy as np from cobaya.input import merge_info from cobaya.conventions import _theory, _params, _p_drop, _p_alias from cobaya.input import get_default_info import input_database planck_to_classy = get_default_info("classy", _theory)[_theory]["classy"]["planck_to_classy"] planck_to_camb = get_default_info("camb", _theory)[_theory]["camb"]["planck_to_camb"] def translate(p, info=None, dictionary=None, add_alias=False): dictionary = dictionary or {} # Ignore if dropped if not (info if hasattr(info, "keys") else {}).get(_p_drop, False): p_old = p p = dictionary.get(p, p) if hasattr(info, "keys") and p_old != p: pre_aliases = np.atleast_1d(info.get(_p_alias, [])).tolist() info.update({_p_alias: pre_aliases + [p_old]}) # Try to modify lambda parameters too! if isinstance(info, string_types): if info.startswith("lambda"): arguments = info.split(":")[0].split()[1:] if not isinstance(arguments, string_types): arguments = "".join(arguments) arguments = arguments.split(",") arguments_t = [translate(pi, dictionary=dictionary)[0] for pi in arguments]