Skip to content

Commit

Permalink
use nnpdf single-truth theory
Browse files Browse the repository at this point in the history
  • Loading branch information
scarlehoff committed Apr 11, 2024
1 parent 4d1e318 commit 5c11080
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 7 deletions.
11 changes: 9 additions & 2 deletions src/pineko/configs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
]
Expand Down Expand Up @@ -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")

Expand Down
7 changes: 6 additions & 1 deletion src/pineko/theory_card.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import yaml

from . import configs
from .utils import load_nnpdf_theory


def path(theory_id: int) -> pathlib.Path:
Expand All @@ -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]:
Expand All @@ -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
Expand Down
34 changes: 30 additions & 4 deletions src/pineko/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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
Expand All @@ -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)

0 comments on commit 5c11080

Please sign in to comment.