Skip to content

Commit 7165989

Browse files
giacomomagniscarlehoff
authored andcommitted
some cleaning
1 parent 314abc2 commit 7165989

File tree

3 files changed

+5
-114
lines changed

3 files changed

+5
-114
lines changed

n3fit/src/evolven3fit/evolve.py

Lines changed: 5 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import numpy as np
99

1010
import eko
11+
from eko import basis_rotation, runner
1112
from eko.interpolation import XGrid
1213
from eko.io import manipulate
1314
from eko import basis_rotation, runner
@@ -26,14 +27,7 @@
2627

2728

2829
def evolve_fit(
29-
fit_folder,
30-
q_fin,
31-
q_points,
32-
op_card_dict,
33-
theory_card_dict,
34-
force,
35-
eko_path,
36-
dump_eko=None,
30+
fit_folder, q_fin, q_points, op_card_dict, theory_card_dict, force, eko_path, dump_eko=None
3731
):
3832
"""
3933
Evolves all the fitted replica in fit_folder/nnfit
@@ -128,12 +122,12 @@ def evolve_fit(
128122
for rep_idx in range(1, n_replicas + 1):
129123
# swap photon postion to match eko.basis_roation.flavor_basis_pids
130124
pdfgrid = np.array(initial_PDFs_dict[f"replica_{rep_idx}"]["pdfgrid"])
131-
pdfgrid = np.append(pdfgrid[:,-1].reshape(x_grid.size,1), pdfgrid[:,:-1], axis=1)
125+
pdfgrid = np.append(pdfgrid[:, -1].reshape(x_grid.size, 1), pdfgrid[:, :-1], axis=1)
132126
# and divide by x
133-
all_replicas.append(pdfgrid.T / x_grid )
127+
all_replicas.append(pdfgrid.T / x_grid)
134128

135129
# reshape the xgrid eko if necessary
136-
if not XGrid(x_grid) == eko_op.xgrid:
130+
if XGrid(x_grid) != eko_op.xgrid:
137131
for _, elem in eko_op.items():
138132
elem = manipulate.xgrid_reshape(
139133
elem,
@@ -201,53 +195,6 @@ def load_fit(usr_path):
201195
return pdf_dict
202196

203197

204-
def evolve_exportgrid(exportgrid, eko, x_grid):
205-
"""
206-
Evolves the provided exportgrid for the desired replica with the eko and returns the evolved block
207-
208-
Parameters
209-
----------
210-
exportgrid: dict
211-
exportgrid of pdf at fitting scale
212-
eko: eko object
213-
eko operator for evolution
214-
xgrid: list
215-
xgrid to be used as the targetgrid
216-
Returns
217-
-------
218-
: list(np.array)
219-
list of evolved blocks
220-
"""
221-
# construct LhapdfLike object
222-
pdf_grid = np.array(exportgrid["pdfgrid"]).transpose()
223-
224-
pdf_to_evolve = utils.LhapdfLike(pdf_grid, exportgrid["q20"], x_grid)
225-
# evolve pdf
226-
evolved_pdf = apply.apply_pdf(eko, pdf_to_evolve)
227-
# generate block to dump
228-
targetgrid = eko.bases.targetgrid.tolist()
229-
230-
# Finally separate by nf block (and order per nf/q)
231-
by_nf = defaultdict(list)
232-
for q, nf in sorted(eko.evolgrid, key=lambda ep: ep[1]):
233-
by_nf[nf].append(q)
234-
q2block_per_nf = {nf: sorted(qs) for nf, qs in by_nf.items()}
235-
236-
blocks = []
237-
for nf, q2grid in q2block_per_nf.items():
238-
239-
def pdf_xq2(pid, x, Q2):
240-
x_idx = targetgrid.index(x)
241-
return x * evolved_pdf[(Q2, nf)]["pdfs"][pid][x_idx]
242-
243-
block = genpdf.generate_block(
244-
pdf_xq2, xgrid=targetgrid, sorted_q2grid=q2grid, pids=basis_rotation.flavor_basis_pids
245-
)
246-
blocks.append(block)
247-
248-
return blocks
249-
250-
251198
def dump_evolved_replica(evolved_blocks, usr_path, replica_num):
252199
"""
253200
Dump the evolved replica given by evolved_block as the replica num "replica_num" in

n3fit/src/evolven3fit/utils.py

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -10,51 +10,6 @@
1010
from .q2grids import Q2GRID_DEFAULT, Q2GRID_NNPDF40
1111

1212

13-
class LhapdfLike:
14-
"""
15-
Class which emulates lhapdf but only for an initial condition PDF (i.e. with only one q2 value).
16-
17-
Q20 is the fitting scale fo the pdf and it is the only available scale for the objects of this class.
18-
19-
X_GRID is the grid of x values on top of which the pdf is interpolated.
20-
21-
PDF_GRID is a dictionary containing the pdf grids at fitting scale for each pid.
22-
"""
23-
24-
def __init__(self, pdf_grid, q20, x_grid):
25-
self.pdf_grid = pdf_grid
26-
self.q20 = q20
27-
self.x_grid = x_grid
28-
self.funcs = [
29-
interp1d(self.x_grid, self.pdf_grid[pid], kind="cubic") for pid in range(len(PIDS_DICT))
30-
]
31-
32-
def xfxQ2(self, pid, x, q2):
33-
"""Return the value of the PDF for the requested pid, x value and, whatever the requested
34-
q2 value, for the fitting q2.
35-
36-
Parameters
37-
----------
38-
39-
pid: int
40-
pid index of particle
41-
x: float
42-
x-value
43-
q2: float
44-
Q square value
45-
46-
Returns
47-
-------
48-
: float
49-
x * PDF value
50-
"""
51-
return self.funcs[list(PIDS_DICT.values()).index(PIDS_DICT[pid])](x)
52-
53-
def hasFlavor(self, pid):
54-
"""Check if the requested pid is in the PDF."""
55-
return pid in PIDS_DICT
56-
57-
5813
def read_runcard(usr_path):
5914
"""Read the runcard and return the relevant information for evolven3fit"""
6015
return yaml_safe.load((usr_path / "filter.yml").read_text(encoding="UTF-8"))

n3fit/src/n3fit/tests/test_evolven3fit.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -98,17 +98,6 @@ def test_generate_q2grid():
9898

9999

100100
def test_utils():
101-
# Testing the fake LHAPDF class
102-
q20 = 1.65**2
103-
x_grid = np.geomspace(1.0e-7, 1.0, 30)
104-
fake_grids = [[x * (1.0 - x) for x in x_grid] for _ in PIDS_DICT.keys()]
105-
pdf_grid = {pid: v for pid, v in zip(range(len(PIDS_DICT)), fake_grids)}
106-
my_PDF = utils.LhapdfLike(pdf_grid, q20, x_grid)
107-
assert my_PDF.hasFlavor(6)
108-
assert not my_PDF.hasFlavor(0)
109-
for pid in PIDS_DICT:
110-
for x in x_grid:
111-
np.testing.assert_allclose(my_PDF.xfxQ2(pid, x, q20), x * (1.0 - x))
112101
# Testing read_runcard
113102
runcard = utils.read_runcard(REGRESSION_FOLDER)
114103
assert isinstance(runcard["description"], str)

0 commit comments

Comments
 (0)