Skip to content

Commit

Permalink
Merge pull request #2096 from NNPDF/allow_for_underscore_in_theoryfiles
Browse files Browse the repository at this point in the history
Allow for underscore in theoryfiles
  • Loading branch information
RoyStegeman committed Jul 31, 2024
2 parents b0ca369 + f99a84e commit 6fa4cb1
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 12 deletions.
41 changes: 31 additions & 10 deletions nnpdf_data/nnpdf_data/theorydbutils.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# -*- coding: utf-8 -*-
"""
theorydbutils.py
low level utilities for querying the theory database file and representing the
data as a python object.
"""

from functools import lru_cache
from pathlib import Path

Expand All @@ -23,10 +23,8 @@ def parse_theory_card(theory_card):
"""Read the theory card using validobj parsing
Returns the theory as a dictionary
"""
if theory_card.exists():
tcard = parse_yaml_inp(theory_card, TheoryCard)
return tcard.asdict()
raise TheoryNotFoundInDatabase(f"Theory card {theory_card} not found")
tcard = parse_yaml_inp(theory_card, TheoryCard)
return tcard.asdict()


def fetch_theory(theory_database: Path, theoryID: int):
Expand All @@ -51,13 +49,35 @@ def fetch_theory(theory_database: Path, theoryID: int):
>>> from nnpdf_data.theorydbutils import fetch_theory
>>> theory = fetch_theory(theory_cards, 700)
"""
filepath = theory_database / f"{theoryID}.yaml"
tdict = parse_theory_card(filepath)

available_theories = get_available_theory_cards(theory_database)
try:
theoryfile = available_theories[theoryID]
except KeyError as e:
raise TheoryNotFoundInDatabase(f"Theorycard for theory not found: {e}")

tdict = parse_theory_card(theoryfile)
if tdict["ID"] != int(theoryID):
raise ValueError(f"The theory ID in {filepath} doesn't correspond with its ID entry")
raise ValueError(f"The theory ID in {theoryfile} doesn't correspond with its ID entry")
return tdict


@lru_cache
def get_available_theory_cards(path):
"""Since theoryfile names may contain underscores, the theoryfile name cannot uniquely be
determined from the theoryID. Therefore we create a mapping between the theoryid as python
int and the corresponding file.
"""
available_theories = {}
for theoryfile in path.glob("*.yaml"):
tmp_id = int(theoryfile.stem)
if tmp_id in available_theories:
another = available_theories[tmp_id]
raise ValueError(f"Two theory files with same id: {theoryfile} and {another}")
available_theories[tmp_id] = theoryfile
return available_theories


def fetch_all(theory_database: Path):
"""Looks in the theory database and returns a dataframe with theory info
for all theories
Expand All @@ -74,12 +94,13 @@ def fetch_all(theory_database: Path):
Example
------
>>> from validphys.datafiles import theory_cards
>>> from nnpdf_data import theory_cards
>>> from nnpdf_data.theorydbutils import fetch_all
>>> theory_df = fetch_all(theory_cards)
"""
available_theories = get_available_theory_cards(theory_database)
theories = []
for theory_path in theory_database.glob("*.yaml"):
for theory_path in available_theories.values():
theories.append(parse_theory_card(theory_path))
df = pd.DataFrame(theories)
return df.set_index(['ID']).sort_index()
2 changes: 1 addition & 1 deletion validphys2/src/validphys/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -1658,7 +1658,7 @@ def produce_scale_variation_theories(self, theoryid, point_prescription):

scalevarsfor_list = lsv["scale_variations_for"]
# Allowed central theoryids
cent_thids = [str(scalevarsfor_dict["theoryid"]) for scalevarsfor_dict in scalevarsfor_list]
cent_thids = [int(scalevarsfor_dict["theoryid"]) for scalevarsfor_dict in scalevarsfor_list]
if th not in cent_thids:
valid_thids = ", ".join(cent_thids)
raise ConfigError(
Expand Down
2 changes: 1 addition & 1 deletion validphys2/src/validphys/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ def check_commondata(

@functools.lru_cache
def check_theoryID(self, theoryID):
theoryID = str(theoryID)
theoryID = int(theoryID)
theopath = self._theories_path / f"theory_{theoryID}"
if not theopath.exists():
raise TheoryNotFound(f"Could not find theory {theoryID}. Folder '{theopath}' not found")
Expand Down

0 comments on commit 6fa4cb1

Please sign in to comment.