diff --git a/src/pineko/configs.py b/src/pineko/configs.py index 90eec3a4..d92572b0 100644 --- a/src/pineko/configs.py +++ b/src/pineko/configs.py @@ -12,11 +12,12 @@ configs = {} "Holds loaded configurations" +THEORY_PATH_KEY = "theory_cards" NEEDED_KEYS = [ "operator_cards", "grids", "operator_card_template_name", - "theory_cards", + THEORY_PATH_KEY, "fktables", "ekos", ] @@ -65,11 +66,17 @@ def enhance_paths(configs_): if generic_options.get("nnpdf", False): # Fail as soon as possible try: - import validphys + import nnpdf_data except ModuleNotFoundError: raise ModuleNotFoundError( "Cannot use `nnpdf=True` without a valid installation of NNPDF" ) + # If ``nnpdf_data`` is available, then override also the theory path + # UNLESS the debug-option ``nnpdf_theory`` is set explicitly to false! + if generic_options.get("nnpdf_theory", True): + from nnpdf_data import theory_cards + + configs_["paths"][THEORY_PATH_KEY] = theory_cards else: required_keys.append("ymldb") diff --git a/src/pineko/theory_card.py b/src/pineko/theory_card.py index 931a29fc..88be852f 100644 --- a/src/pineko/theory_card.py +++ b/src/pineko/theory_card.py @@ -6,6 +6,7 @@ import yaml from . import configs +from .utils import load_nnpdf_theory def path(theory_id: int) -> pathlib.Path: @@ -22,7 +23,7 @@ def path(theory_id: int) -> pathlib.Path: theory card path """ - return configs.configs["paths"]["theory_cards"] / f"{theory_id}.yaml" + return configs.configs["paths"][configs.THEORY_PATH_KEY] / f"{theory_id}.yaml" def load(theory_id: int) -> Dict[str, Any]: @@ -39,6 +40,10 @@ def load(theory_id: int) -> Dict[str, Any]: theory card """ + nnpdf_theory = load_nnpdf_theory(theory_id, configs.configs) + if nnpdf_theory is not None: + return nnpdf_theory + with open(path(theory_id), encoding="utf-8") as f: theory_card = yaml.safe_load(f) return theory_card diff --git a/src/pineko/utils.py b/src/pineko/utils.py index f40f2919..66076ee7 100644 --- a/src/pineko/utils.py +++ b/src/pineko/utils.py @@ -3,7 +3,14 @@ Common tools typically used by several pineko functions. """ -from .configs import GENERIC_OPTIONS +from .configs import GENERIC_OPTIONS, THEORY_PATH_KEY + + +def _nnpdf_enabled(configs): + """Check whether NNPDF is enabled.""" + if configs is None: + return True + return configs.get(GENERIC_OPTIONS, {}).get("nnpdf", False) def read_grids_from_nnpdf(dataset_name, configs=None): @@ -18,9 +25,8 @@ def read_grids_from_nnpdf(dataset_name, configs=None): dictionary of configuration options if None it it assumed that the NNPDF version is required """ - if configs is not None: - if not configs.get(GENERIC_OPTIONS, {}).get("nnpdf", False): - return None + if not _nnpdf_enabled(configs): + return None # Import NNPDF only if we really want it! from nnpdf_data import legacy_to_new_map @@ -33,3 +39,23 @@ def read_grids_from_nnpdf(dataset_name, configs=None): fks = cd.metadata.theory.FK_tables # Return it flat return [f"{i}.{EXT}" for operand in fks for i in operand] + + +def load_nnpdf_theory(theory_id, configs): + """Load a theory using the NNPDF data utilities. + + If NNPDF is not available, returns None. + + Parameters + ---------- + theory_id: int + configs: dict + dictionary of configuration options + """ + if not _nnpdf_enabled(configs): + return None + + from nnpdf_data.theorydbutils import fetch_theory + + theory_path = configs["paths"][THEORY_PATH_KEY] + return fetch_theory(theory_path, theory_id)