Skip to content

Commit

Permalink
update evolve_n3fit eko 0.11 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
giacomomagni committed Dec 13, 2022
1 parent 9e270f9 commit 1ce47f5
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 28 deletions.
11 changes: 6 additions & 5 deletions n3fit/src/evolven3fit_new/eko_utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from ekobox import gen_theory, gen_op
from eko import run_dglap
from ekobox import operators_card as oc
from ekobox import theory_card as tc
from eko import run_dglap, output

from validphys.api import API
from . import utils
Expand Down Expand Up @@ -34,15 +35,15 @@ def construct_eko_cards(
theory = API.theoryid(theoryid=theoryID).get_description()
theory.pop("FNS")
theory.update(theory_card_dict)
theory_card = gen_theory.gen_theory_card(theory["PTO"], theory["Q0"], update=theory)
theory_card = tc.generate(theory["PTO"], theory["Q0"], update=theory)
# construct operator card
q2_grid = utils.generate_q2grid(
theory["Q0"],
q_fin,
q_points,
{theory["mb"]: theory["kbThr"], theory["mt"]: theory["ktThr"]},
)
op_card = gen_op.gen_op_card(q2_grid, update={"interpolation_xgrid": x_grid})
op_card = oc.generate(q2_grid, update={"interpolation_xgrid": x_grid})
op_card.update(op_card_dict)
return theory_card, op_card

Expand Down Expand Up @@ -76,7 +77,7 @@ def construct_eko_for_fit(theory_card, op_card, save_path=None):
# Here we want to catch all possible exceptions in order to avoid losing the computed eko
try:
_logger.info(f"Saving computed eko to : {save_path}")
eko_op.dump_tar(save_path)
output.legacy.dump_tar(eko_op, save_path)
except:
_logger.error(f"Error saving the eko to : {save_path}")
pass
Expand Down
44 changes: 23 additions & 21 deletions n3fit/src/evolven3fit_new/evolve.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
import numpy as np
from reportengine.compat import yaml

from ekobox import genpdf, gen_info
from ekobox import genpdf
from ekobox import info_file
from ekomark import apply
from eko import basis_rotation as br
from eko import output
from eko.interpolation import XGrid
from validphys.api import API

from . import utils, eko_utils
Expand All @@ -18,10 +20,12 @@

LOG_FILE = "evolven3fit_new.log"

LOGGING_SETTINGS = {"formatter" : "%(asctime)s %(name)s/%(levelname)s: %(message)s",
"level" : logging.INFO
LOGGING_SETTINGS = {
"formatter": "%(asctime)s %(name)s/%(levelname)s: %(message)s",
"level": logging.INFO,
}


def evolve_fit(
fit_folder,
q_fin,
Expand Down Expand Up @@ -69,9 +73,7 @@ def evolve_fit(
stdout_log = logging.StreamHandler(sys.stdout)
for log in [log_file, stdout_log]:
log.setLevel(LOGGING_SETTINGS["level"])
log.setFormatter(
logging.Formatter(LOGGING_SETTINGS["formatter"])
)
log.setFormatter(logging.Formatter(LOGGING_SETTINGS["formatter"]))
for logger in (_logger, *[logging.getLogger("eko")]):
logger.handlers = []
logger.setLevel(LOGGING_SETTINGS["level"])
Expand All @@ -80,35 +82,35 @@ def evolve_fit(

usr_path = pathlib.Path(fit_folder)
initial_PDFs_dict = load_fit(usr_path)
x_grid = np.array(
initial_PDFs_dict[list(initial_PDFs_dict.keys())[0]]["xgrid"]
).astype(np.float)
x_grid = XGrid(initial_PDFs_dict[list(initial_PDFs_dict.keys())[0]]["xgrid"])
theoryID = utils.get_theoryID_from_runcard(usr_path)
theory, op = eko_utils.construct_eko_cards(
theoryID, q_fin, q_points, x_grid, op_card_dict, theory_card_dict
)
if eko_path is not None:
eko_path = pathlib.Path(eko_path)
_logger.info(f"Loading eko from : {eko_path}")
eko_op = output.Output.load_tar(eko_path)
eko_op = output.legacy.load_tar(eko_path)
else:
try:
_logger.info(f"Loading eko from theory {theoryID}")
theory_eko_path = (API.theoryid(theoryid = theoryID).path)/'eko.tar'
eko_op = output.Output.load_tar(theory_eko_path)
theory_eko_path = (API.theoryid(theoryid=theoryID).path) / "eko.tar"
eko_op = output.legacy.load_tar(theory_eko_path)
except FileNotFoundError:
_logger.info(f"eko not found in theory {theoryID}, we will construct it")
eko_op = eko_utils.construct_eko_for_fit(theory, op, dump_eko)
eko_op.xgrid_reshape(targetgrid=x_grid, inputgrid=x_grid)
info = gen_info.create_info_file(theory, op, 1, info_update={})
eko_op = eko_utils.construct_eko_for_fit(theory, op, dump_eko)
output.manipulate.xgrid_reshape(eko_op, targetgrid=x_grid, inputgrid=x_grid)
info = info_file.build(theory, op, 1, info_update={})
info["NumMembers"] = "REPLACE_NREP"
info["ErrorType"] = "replicas"
info["AlphaS_Qs"] = list(eko_op["Q2grid"])
info["XMin"] = float(x_grid[0])
info["XMax"] = float(x_grid[-1])
info["AlphaS_Qs"] = eko_op.Q2grid.tolist()
info["XMin"] = float(x_grid.raw.min())
info["XMax"] = float(x_grid.raw.max())
dump_info_file(usr_path, info)
for replica in initial_PDFs_dict.keys():
evolved_block = evolve_exportgrid(initial_PDFs_dict[replica], eko_op, x_grid)
evolved_block = evolve_exportgrid(
initial_PDFs_dict[replica], eko_op, x_grid.tolist()
)
dump_evolved_replica(
evolved_block, usr_path, int(replica.removeprefix("replica_"))
)
Expand Down Expand Up @@ -165,15 +167,15 @@ def evolve_exportgrid(exportgrid, eko, x_grid):
# evolve pdf
evolved_pdf = apply.apply_pdf(eko, pdf_to_evolve)
# generate block to dump
targetgrid = list(eko["targetgrid"])
targetgrid = eko.xgrid.tolist()

def ev_pdf(pid, x, Q2):
return x * evolved_pdf[Q2]["pdfs"][pid][targetgrid.index(x)]

block = genpdf.generate_block(
ev_pdf,
xgrid=targetgrid,
Q2grid=list(eko["Q2grid"]),
Q2grid=eko.Q2grid.tolist(),
pids=br.flavor_basis_pids,
)
return block
Expand Down
4 changes: 2 additions & 2 deletions n3fit/src/n3fit/tests/test_evolven3fit.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,5 @@ def test_eko_utils():
assert_allclose(op_card["Q2grid"][1], 4.92**2)
#Testing construct_eko_for_fit
eko_op = eko_utils.construct_eko_for_fit(t_card ,op_card)
assert_allclose(eko_op['interpolation_xgrid'], x_grid)
assert_allclose(list(eko_op['Q2grid']), op_card["Q2grid"])
assert_allclose(eko_op.xgrid.tolist(), x_grid)
assert_allclose(eko_op.Q2grid.tolist(), op_card["Q2grid"])

0 comments on commit 1ce47f5

Please sign in to comment.