Skip to content

Commit

Permalink
some cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
giacomomagni committed Nov 7, 2024
1 parent 0b20b32 commit 13c969a
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 115 deletions.
64 changes: 5 additions & 59 deletions n3fit/src/evolven3fit/evolve.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
import numpy as np

import eko
from eko import basis_rotation, runner
from eko.interpolation import XGrid
from eko.io import manipulate
from eko import basis_rotation, runner
from reportengine.compat import yaml

from . import eko_utils, utils
Expand All @@ -26,14 +26,7 @@


def evolve_fit(
fit_folder,
q_fin,
q_points,
op_card_dict,
theory_card_dict,
force,
eko_path,
dump_eko=None,
fit_folder, q_fin, q_points, op_card_dict, theory_card_dict, force, eko_path, dump_eko=None
):
"""
Evolves all the fitted replica in fit_folder/nnfit
Expand Down Expand Up @@ -128,12 +121,12 @@ def evolve_fit(
for rep_idx in range(1, n_replicas + 1):
# swap photon postion to match eko.basis_roation.flavor_basis_pids
pdfgrid = np.array(initial_PDFs_dict[f"replica_{rep_idx}"]["pdfgrid"])
pdfgrid = np.append(pdfgrid[:,-1].reshape(x_grid.size,1), pdfgrid[:,:-1], axis=1)
pdfgrid = np.append(pdfgrid[:, -1].reshape(x_grid.size, 1), pdfgrid[:, :-1], axis=1)
# and divide by x
all_replicas.append(pdfgrid.T / x_grid )
all_replicas.append(pdfgrid.T / x_grid)

# reshape the xgrid eko if necessary
if not XGrid(x_grid) == eko_op.xgrid:
if XGrid(x_grid) != eko_op.xgrid:
for _, elem in eko_op.items():
elem = manipulate.xgrid_reshape(
elem,
Expand Down Expand Up @@ -201,53 +194,6 @@ def load_fit(usr_path):
return pdf_dict


def evolve_exportgrid(exportgrid, eko, x_grid):
"""
Evolves the provided exportgrid for the desired replica with the eko and returns the evolved block
Parameters
----------
exportgrid: dict
exportgrid of pdf at fitting scale
eko: eko object
eko operator for evolution
xgrid: list
xgrid to be used as the targetgrid
Returns
-------
: list(np.array)
list of evolved blocks
"""
# construct LhapdfLike object
pdf_grid = np.array(exportgrid["pdfgrid"]).transpose()

pdf_to_evolve = utils.LhapdfLike(pdf_grid, exportgrid["q20"], x_grid)
# evolve pdf
evolved_pdf = apply.apply_pdf(eko, pdf_to_evolve)
# generate block to dump
targetgrid = eko.bases.targetgrid.tolist()

# Finally separate by nf block (and order per nf/q)
by_nf = defaultdict(list)
for q, nf in sorted(eko.evolgrid, key=lambda ep: ep[1]):
by_nf[nf].append(q)
q2block_per_nf = {nf: sorted(qs) for nf, qs in by_nf.items()}

blocks = []
for nf, q2grid in q2block_per_nf.items():

def pdf_xq2(pid, x, Q2):
x_idx = targetgrid.index(x)
return x * evolved_pdf[(Q2, nf)]["pdfs"][pid][x_idx]

block = genpdf.generate_block(
pdf_xq2, xgrid=targetgrid, sorted_q2grid=q2grid, pids=basis_rotation.flavor_basis_pids
)
blocks.append(block)

return blocks


def dump_evolved_replica(evolved_blocks, usr_path, replica_num):
"""
Dump the evolved replica given by evolved_block as the replica num "replica_num" in
Expand Down
45 changes: 0 additions & 45 deletions n3fit/src/evolven3fit/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,51 +10,6 @@
from .q2grids import Q2GRID_DEFAULT, Q2GRID_NNPDF40


class LhapdfLike:
"""
Class which emulates lhapdf but only for an initial condition PDF (i.e. with only one q2 value).
Q20 is the fitting scale fo the pdf and it is the only available scale for the objects of this class.
X_GRID is the grid of x values on top of which the pdf is interpolated.
PDF_GRID is a dictionary containing the pdf grids at fitting scale for each pid.
"""

def __init__(self, pdf_grid, q20, x_grid):
self.pdf_grid = pdf_grid
self.q20 = q20
self.x_grid = x_grid
self.funcs = [
interp1d(self.x_grid, self.pdf_grid[pid], kind="cubic") for pid in range(len(PIDS_DICT))
]

def xfxQ2(self, pid, x, q2):
"""Return the value of the PDF for the requested pid, x value and, whatever the requested
q2 value, for the fitting q2.
Parameters
----------
pid: int
pid index of particle
x: float
x-value
q2: float
Q square value
Returns
-------
: float
x * PDF value
"""
return self.funcs[list(PIDS_DICT.values()).index(PIDS_DICT[pid])](x)

def hasFlavor(self, pid):
"""Check if the requested pid is in the PDF."""
return pid in PIDS_DICT


def read_runcard(usr_path):
"""Read the runcard and return the relevant information for evolven3fit"""
return yaml.safe_load((usr_path / "filter.yml").read_text(encoding="UTF-8"))
Expand Down
11 changes: 0 additions & 11 deletions n3fit/src/n3fit/tests/test_evolven3fit.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,17 +98,6 @@ def test_generate_q2grid():


def test_utils():
# Testing the fake LHAPDF class
q20 = 1.65**2
x_grid = np.geomspace(1.0e-7, 1.0, 30)
fake_grids = [[x * (1.0 - x) for x in x_grid] for _ in PIDS_DICT.keys()]
pdf_grid = {pid: v for pid, v in zip(range(len(PIDS_DICT)), fake_grids)}
my_PDF = utils.LhapdfLike(pdf_grid, q20, x_grid)
assert my_PDF.hasFlavor(6)
assert not my_PDF.hasFlavor(0)
for pid in PIDS_DICT:
for x in x_grid:
np.testing.assert_allclose(my_PDF.xfxQ2(pid, x, q20), x * (1.0 - x))
# Testing read_runcard
runcard = utils.read_runcard(REGRESSION_FOLDER)
assert isinstance(runcard["description"], str)
Expand Down

0 comments on commit 13c969a

Please sign in to comment.