From b55fb05cceec5ad367805f8eb268986a064cadea Mon Sep 17 00:00:00 2001 From: giacomomagni Date: Tue, 12 Nov 2024 16:42:35 +0100 Subject: [PATCH] add compatibility layer for theory and operator cards --- src/eko/io/struct.py | 18 +++++++++------- src/eko/io/v1.py | 50 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 60 insertions(+), 8 deletions(-) diff --git a/src/eko/io/struct.py b/src/eko/io/struct.py index a4a346d4c..105ecb273 100644 --- a/src/eko/io/struct.py +++ b/src/eko/io/struct.py @@ -14,7 +14,7 @@ import yaml from .. import interpolation -from . import exceptions, raw +from . import exceptions, raw, v1 from .access import AccessConfigs from .inventory import Inventory from .items import Evolution, Matching, Operator, Recipe, Target @@ -132,16 +132,20 @@ def evolgrid(self) -> List[EPoint]: @property def theory_card(self): """Provide theory card, retrieving from the dump.""" - return TheoryCard.from_dict( - yaml.safe_load(self.paths.theory_card.read_text(encoding="utf-8")) - ) + raw_th = yaml.safe_load(self.paths.theory_card.read_text(encoding="utf-8")) + if self.metadata.data_version in [1, 2]: + raw_th = v1.update_theory(raw_th) + return TheoryCard.from_dict(raw_th) @property def operator_card(self): """Provide operator card, retrieving from the dump.""" - return OperatorCard.from_dict( - yaml.safe_load(self.paths.operator_card.read_text(encoding="utf-8")) - ) + raw_op = yaml.safe_load(self.paths.operator_card.read_text(encoding="utf-8")) + if self.metadata.data_version in [1, 2]: + # here we need to read also the theory card + raw_th = yaml.safe_load(self.paths.theory_card.read_text(encoding="utf-8")) + raw_op = v1.update_operator(raw_op, raw_th) + return OperatorCard.from_dict(raw_op) # persistency control # ------------------- diff --git a/src/eko/io/v1.py b/src/eko/io/v1.py index 4bba1501d..9545cfecc 100644 --- a/src/eko/io/v1.py +++ b/src/eko/io/v1.py @@ -7,7 +7,7 @@ from .paths import InternalPaths -def update_metadata(paths: InternalPaths, raw: dict) -> dict: +def update_metadata(_paths: InternalPaths, raw: dict) -> dict: """Modify the raw metadata to the new format. Parameters @@ -22,4 +22,52 @@ def update_metadata(paths: InternalPaths, raw: dict) -> dict: dict compatible raw yaml content """ + raw["data_version"] = 1 + raw["xgrid"] = raw["bases"]["xgrid"] + del raw["bases"] return raw + + +def update_theory(raw: dict) -> dict: + """Modify the raw theory card to the new format. + + Parameters + ---------- + raw: + raw yaml content + + Returns + ------- + dict + compatible raw yaml content + """ + raw["couplings"]["ref"] = ( + raw["couplings"]["scale"], + raw["couplings"]["num_flavs_ref"], + ) + # adjust couplings + for key in ["num_flavs_ref", "max_num_flavs", "scale"]: + del raw["couplings"][key] + # adjust heavy + for key in ["intrinsic_flavors", "num_flavs_init", "num_flavs_max_pdf"]: + del raw["heavy"][key] + return raw + + +def update_operator(raw_op: dict, raw_th) -> dict: + """Modify the raw operator card to the new format. + + Parameters + ---------- + raw_op: + raw operator yaml content + raw_th: + raw theory yaml content + Returns + ------- + dict + compatible raw yaml content + """ + raw_op["init"] = (raw_op["mu0"], raw_th["heavy"]["num_flavs_init"]) + del raw_op["mu0"] + return raw_op