Skip to content

Commit 1b58865

Browse files
committed
clean a bit the cli
1 parent 3ce2d1c commit 1b58865

File tree

4 files changed

+63
-67
lines changed

4 files changed

+63
-67
lines changed

n3fit/src/evolven3fit/eko_utils.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@
2929
NFREF_DEFAULT = 5
3030

3131

32+
def _eko_theory_from_nnpdf_theory(nnpdf_theory):
33+
"""Takes an NNPDF theory and returns an eko theory"""
34+
return runcards.Legacy(nnpdf_theory, {}).new_theory
35+
36+
3237
def construct_eko_cards(
3338
nnpdf_theory,
3439
q_fin,
@@ -53,9 +58,7 @@ def construct_eko_cards(
5358
# eko needs a value for Qedref and for max nf alphas
5459
theory["Qedref"] = theory["Qref"]
5560
theory["MaxNfAs"] = theory["MaxNfPdf"]
56-
57-
# The Legacy function is able to construct a theory card for eko starting from a NNPDF theory
58-
theory_card = runcards.Legacy(theory, {}).new_theory
61+
theory_card = _eko_theory_from_nnpdf_theory(theory)
5962

6063
# construct mugrid
6164

@@ -125,9 +128,7 @@ def construct_eko_photon_cards(
125128
# Now make sure the Legacy class still gets a Qedref, which is equal to Qref
126129
theory["Qedref"] = theory["Qref"]
127130
theory["MaxNfAs"] = theory["MaxNfPdf"]
128-
129-
# The Legacy function is able to construct a theory card for eko starting from a NNPDF theory
130-
theory_card = runcards.Legacy(theory, {}).new_theory
131+
theory_card = _eko_theory_from_nnpdf_theory(theory)
131132

132133
# The photon needs to be evolved down to Q0
133134
q_fin = theory["Q0"]

n3fit/src/evolven3fit/evolve.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,14 +112,14 @@ def evolve_fit(
112112
info["XMax"] = float(x_grid[-1])
113113
# Save the PIDs in the info file in the same order as in the evolution
114114
info["Flavors"] = basis_rotation.flavor_basis_pids
115-
info["NumFlavors"] = 5 # TODO: Maximum number in evol
115+
info.setdefault("NumFlavors", 5)
116116
dump_info_file(usr_path, info)
117117

118118
# Read the information from all the sorted replicas into what eko wants
119119
n_replicas = len(initial_PDFs_dict)
120120
all_replicas = []
121121
for rep_idx in range(1, n_replicas + 1):
122-
# swap photon postion to match eko.basis_roation.flavor_basis_pids
122+
# swap photon position to match eko.basis_rotation.flavor_basis_pids
123123
pdfgrid = np.array(initial_PDFs_dict[f"replica_{rep_idx}"]["pdfgrid"])
124124
pdfgrid = np.append(pdfgrid[:, -1].reshape(x_grid.size, 1), pdfgrid[:, :-1], axis=1)
125125
# and divide by x

n3fit/src/n3fit/scripts/evolven3fit.py

Lines changed: 35 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,27 @@
1919
_logger = logging.getLogger(__name__)
2020

2121

22+
def _add_production_options(parser):
23+
"""Adds the production options that are shared between the normal and the QED EKOs to their parsers"""
24+
parser.add_argument("theoryID", type=int, help="ID of the theory used to produce the eko")
25+
parser.add_argument("dump", type=pathlib.Path, help="Path where the EKO is dumped")
26+
parser.add_argument(
27+
"-i", "--x-grid-ini", default=None, type=float, help="Starting point of the x-grid"
28+
)
29+
parser.add_argument(
30+
"-p", "--x-grid-points", default=None, type=int, help="Number of points of the x-grid"
31+
)
32+
parser.add_argument("-n", "--n-cores", type=int, default=1, help="Number of cores to be used")
33+
parser.add_argument('--use_polarized', action='store_true', help="Use polarized evolution")
34+
parser.add_argument(
35+
"-e",
36+
"--ev-op-iterations",
37+
type=int,
38+
default=None,
39+
help="ev_op_iterations for the EXA theory. Overrides the settings given in the theory card.",
40+
)
41+
42+
2243
def construct_eko_parser(subparsers):
2344
parser = subparsers.add_parser(
2445
"produce_eko",
@@ -30,14 +51,7 @@ def construct_eko_parser(subparsers):
3051
provided number of points. The eko will be dumped in the provided path."""
3152
),
3253
)
33-
parser.add_argument("theoryID", type=int, help="ID of the theory used to produce the eko")
34-
parser.add_argument("dump", type=pathlib.Path, help="Path where the EKO is dumped")
35-
parser.add_argument(
36-
"-i", "--x-grid-ini", default=None, type=float, help="Starting point of the x-grid"
37-
)
38-
parser.add_argument(
39-
"-p", "--x-grid-points", default=None, type=int, help="Number of points of the x-grid"
40-
)
54+
_add_production_options(parser)
4155
parser.add_argument(
4256
"--legacy40",
4357
action="store_true",
@@ -56,14 +70,7 @@ def construct_eko_photon_parser(subparsers):
5670
provided number of points. The eko will be dumped in the provided path."""
5771
),
5872
)
59-
parser.add_argument("theoryID", type=int, help="ID of the theory used to produce the eko")
60-
parser.add_argument("dump", type=pathlib.Path, help="Path where the EKO is dumped")
61-
parser.add_argument(
62-
"-i", "--x-grid-ini", default=None, type=float, help="Starting point of the x-grid"
63-
)
64-
parser.add_argument(
65-
"-p", "--x-grid-points", default=None, type=int, help="Number of points of the x-grid"
66-
)
73+
_add_production_options(parser)
6774
parser.add_argument(
6875
"-g", "--q-gamma", default=100, type=float, help="Scale at which the photon is generated"
6976
)
@@ -76,7 +83,7 @@ def construct_evolven3fit_parser(subparsers):
7683
help="Evolves the fitted PDFs. The q_grid starts at the Q0 given by the theory but the last point is q_fin and its number of points can be specified by q_points. If a path is given for the dump option, the eko will be dumped in that path after the computation. If a path is given for the load option, the eko to be used for the evolution will be loaded from that path. The two options are mutually exclusive.",
7784
)
7885
parser.add_argument(
79-
"configuration_folder", help="Path to the folder containing the (pre-DGLAP) fit result"
86+
"fit_folder", help="Path to the folder containing the (pre-DGLAP) fit result"
8087
)
8188
parser.add_argument(
8289
"-l", "--load", type=pathlib.Path, default=None, help="Path of the EKO to be loaded"
@@ -99,32 +106,14 @@ def evolven3fit_new():
99106

100107

101108
def main():
102-
parser = ArgumentParser(
103-
description="evolven3fit - a script with tools to evolve PDF fits",
104-
usage="""evolven3fit [-h] [-q Q_FIN] [-p Q_POINTS] [-n N_CORES] [-e EV_OP_ITERATIONS]
105-
{produce_eko,produce_eko_photon,evolve} [fit folder]
106-
107-
Note that with the now removed apfel-based version of `evolven3fit` the syntax was
108-
`evolven3fit [fit folder] [number of replicas]`. This syntax is no longer supported in the
109-
eko-based version of evolven3fit.
110-
""",
111-
)
112-
parser.add_argument('--use_polarized', action='store_true', help="Use polarized evolution")
109+
parser = ArgumentParser(description="evolven3fit - a script with tools to evolve PDF fits")
113110
parser.add_argument(
114111
"-q", "--q-fin", type=float, default=None, help="Final q-value of the evolution"
115112
)
116113
parser.add_argument(
117114
"-p", "--q-points", type=int, default=None, help="Number of q points for the evolution"
118115
)
119-
parser.add_argument("-n", "--n-cores", type=int, default=1, help="Number of cores to be used")
120116
parser.add_argument("--no-net", action="store_true", help="Emulates validphys' --no-net")
121-
parser.add_argument(
122-
"-e",
123-
"--ev-op-iterations",
124-
type=int,
125-
default=None,
126-
help="ev_op_iterations for the EXA theory. Overrides the settings given in the theory card.",
127-
)
128117

129118
subparsers = parser.add_subparsers(title="actions", dest="actions")
130119
construct_eko_parser(subparsers)
@@ -133,12 +122,7 @@ def main():
133122

134123
args = parser.parse_args()
135124

136-
op_card_info = {
137-
"configs": {"n_integration_cores": args.n_cores, "polarized": args.use_polarized}
138-
}
139-
if args.ev_op_iterations is not None:
140-
op_card_info["configs"]["ev_op_iterations"] = args.ev_op_iterations
141-
125+
op_card_info = {}
142126
# Here we do not allow any modification of the theory card, for the moment.
143127
theory_card_info = {}
144128

@@ -149,7 +133,7 @@ def main():
149133

150134
if args.actions == "evolve":
151135

152-
fit_folder = pathlib.Path(args.configuration_folder)
136+
fit_folder = pathlib.Path(args.fit_folder)
153137
if args.load is None:
154138
theoryID = utils.get_theoryID_from_runcard(fit_folder)
155139
_logger.info(f"Loading eko from theory {theoryID}")
@@ -194,6 +178,14 @@ def main():
194178
)
195179
else:
196180
x_grid = np.geomspace(args.x_grid_ini, 1.0, args.x_grid_points)
181+
182+
# Prepare the op card config
183+
op_card_info = {
184+
"configs": {"n_integration_cores": args.n_cores, "polarized": args.use_polarized}
185+
}
186+
if args.ev_op_iterations is not None:
187+
op_card_info["configs"]["ev_op_iterations"] = args.ev_op_iterations
188+
197189
if args.actions == "produce_eko":
198190
tcard, opcard = eko_utils.construct_eko_cards(
199191
nnpdf_theory,

n3fit/src/n3fit/vpinterface.py

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
"""
2-
n3fit interface to validphys
2+
n3fit interface to validphys
33
4-
Example
5-
-------
4+
Example
5+
-------
66
7-
>>> import numpy as np
8-
>>> from n3fit.vpinterface import N3PDF
9-
>>> from n3fit.model_gen import pdfNN_layer_generator
10-
>>> from validphys.pdfgrids import xplotting_grid
11-
>>> fake_fl = [{'fl' : i, 'largex' : [0,1], 'smallx': [1,2]} for i in ['u', 'ubar', 'd', 'dbar', 'c', 'cbar', 's', 'sbar']]
12-
>>> fake_x = np.linspace(1e-3,0.8,3)
13-
>>> pdf_model = pdfNN_layer_generator(nodes=[8], activations=['linear'], seed=0, flav_info=fake_fl)
14-
>>> n3pdf = N3PDF(pdf_model)
15-
>>> res = xplotting_grid(n3pdf, 1.6, fake_x)
16-
>>> res.grid_values.error_members().shape
17-
(1, 8, 3)
7+
>>> import numpy as np
8+
>>> from n3fit.vpinterface import N3PDF
9+
>>> from n3fit.model_gen import pdfNN_layer_generator
10+
>>> from validphys.pdfgrids import xplotting_grid
11+
>>> fake_fl = [{'fl' : i, 'largex' : [0,1], 'smallx': [1,2]} for i in ['u', 'ubar', 'd', 'dbar', 'c', 'cbar', 's', 'sbar']]
12+
>>> fake_x = np.linspace(1e-3,0.8,3)
13+
>>> pdf_model = pdfNN_layer_generator(nodes=[8], activations=['linear'], seed=0, flav_info=fake_fl)
14+
>>> n3pdf = N3PDF(pdf_model)
15+
>>> res = xplotting_grid(n3pdf, 1.6, fake_x)
16+
>>> res.grid_values.error_members().shape
17+
(1, 8, 3)
1818
1919
2020
"""
@@ -25,7 +25,6 @@
2525
import numpy as np
2626
import numpy.linalg as la
2727

28-
from n3fit.backends import PREPROCESSING_LAYER_ALL_REPLICAS
2928
from validphys.arclength import arc_lengths, integrability_number
3029
from validphys.core import PDF, MCStats
3130
from validphys.covmats import covmat_from_systematics, sqrt_covmat
@@ -225,9 +224,13 @@ def get_preprocessing_factors(self, replica=None):
225224
otherwise outputs a Nx2 array where [:,0] are alphas and [:,1] betas
226225
"""
227226
# If no replica is explicitly requested, get the preprocessing layer for the first model
227+
# remember replicas start counting at one
228228
if replica is None:
229229
replica = 1
230-
# Replicas start counting in 1 so:
230+
231+
# Keep the import here to avoid loading the backend when it is not necessary
232+
from n3fit.backends import PREPROCESSING_LAYER_ALL_REPLICAS
233+
231234
preprocessing_layer = self._models[replica - 1].get_layer(PREPROCESSING_LAYER_ALL_REPLICAS)
232235

233236
alphas_and_betas = None

0 commit comments

Comments
 (0)