Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid using resources at the module level #1763

Merged
merged 2 commits into from
Jun 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions validphys2/src/validphys/photon/compute.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ def __init__(self, theoryid, lux_params, replicas):
self.path_to_eko_photon = theoryid.path / "eko_photon.tar"
with EKO.read(self.path_to_eko_photon) as eko:
self.q_in = np.sqrt(eko.mu20)


# set fiatlux
self.lux = {}
Expand Down Expand Up @@ -121,7 +120,9 @@ def __init__(self, theoryid, lux_params, replicas):
self.lux[replica].PlugStructureFunctions(f2.fxq, fl.fxq, f2lo.fxq)

photon_array = self.compute_photon_array(replica)
self.interpolator.append(interp1d(XGRID, photon_array, fill_value="extrapolate", kind="cubic"))
self.interpolator.append(
interp1d(XGRID, photon_array, fill_value="extrapolate", kind="cubic")
)
self.integral.append(trapezoid(photon_array, XGRID))

def compute_photon_array(self, replica):
Expand All @@ -140,7 +141,9 @@ def compute_photon_array(self, replica):
"""
# Compute photon PDF
log.info(f"Computing photon")
photon_qin = np.array([self.lux[replica].EvaluatePhoton(x, self.q_in**2).total for x in XGRID])
photon_qin = np.array(
[self.lux[replica].EvaluatePhoton(x, self.q_in**2).total for x in XGRID]
)
photon_qin += self.generate_errors(replica)
# fiatlux computes x * gamma(x)
photon_qin /= XGRID
Expand Down
1 change: 1 addition & 0 deletions validphys2/src/validphys/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ def tmp(tmpdir):
HESSIAN_PDF = "NNPDF40_nnlo_as_01180_hessian"
THEORYID = 162
THEORYID_NEW = 399
THEORY_QED = 398
FIT = "NNPDF40_nnlo_lowprecision"
FIT_3REPLICAS = "Basic_runcard_3replicas_lowprec_221130"
FIT_3REPLICAS_DCUTS = "Basic_runcard_3replicas_diffcuts_230221"
Expand Down
55 changes: 30 additions & 25 deletions validphys2/src/validphys/tests/photon/test_compute.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,39 +12,41 @@
from validphys.photon import structure_functions as sf
from validphys.photon.compute import FIATLUX_DEFAULT, Alpha, Photon

from ..conftest import PDF
from ..conftest import PDF, THEORY_QED

TEST_THEORY = API.theoryid(theoryid=398)

FIATLUX_RUNCARD = {
"luxset": PDFset(PDF),
# check if "LUXqed17_plus_PDF4LHC15_nnlo_100" is installed
"additional_errors": FallbackLoader().check_pdf("LUXqed17_plus_PDF4LHC15_nnlo_100"),
"luxseed": 123456789,
"eps_base": 1e-2, # using low precision to speed up tests
}
def generate_fiatlux_runcard():
return {
"luxset": PDFset(PDF),
# check if "LUXqed17_plus_PDF4LHC15_nnlo_100" is installed
"additional_errors": FallbackLoader().check_pdf("LUXqed17_plus_PDF4LHC15_nnlo_100"),
"luxseed": 123456789,
"eps_base": 1e-2, # using low precision to speed up tests
}


def test_parameters_init():
"test initailization of the parameters from Photon class"
fiatlux_runcard = FIATLUX_RUNCARD.copy()
fiatlux_runcard = generate_fiatlux_runcard()
test_theory = API.theoryid(theoryid=THEORY_QED)

# we are not testing the photon here so we make it faster
fiatlux_runcard['eps_base'] = 1e-1

photon = Photon(TEST_THEORY, fiatlux_runcard, [1, 2, 3])
photon = Photon(test_theory, fiatlux_runcard, [1, 2, 3])

np.testing.assert_equal(photon.replicas, [1, 2, 3])
np.testing.assert_equal(photon.luxpdfset._name, FIATLUX_RUNCARD["luxset"].name)
np.testing.assert_equal(photon.luxpdfset._name, fiatlux_runcard["luxset"].name)
np.testing.assert_equal(photon.additional_errors.name, "LUXqed17_plus_PDF4LHC15_nnlo_100")
np.testing.assert_equal(photon.luxseed, FIATLUX_RUNCARD["luxseed"])
np.testing.assert_equal(photon.path_to_eko_photon, TEST_THEORY.path / "eko_photon.tar")
np.testing.assert_equal(photon.luxseed, fiatlux_runcard["luxseed"])
np.testing.assert_equal(photon.path_to_eko_photon, test_theory.path / "eko_photon.tar")
np.testing.assert_equal(photon.q_in, 100.0)


def test_masses_init():
"test thresholds values in Alpha class"
theory = TEST_THEORY.get_description()
test_theory = API.theoryid(theoryid=THEORY_QED)
theory = test_theory.get_description()
alpha = Alpha(theory)
np.testing.assert_equal(alpha.thresh_t, np.inf)
np.testing.assert_almost_equal(alpha.thresh_b, theory["mb"])
Expand All @@ -53,7 +55,8 @@ def test_masses_init():

def test_set_thresholds_alpha_em():
"test value of alpha_em at threshold values"
theory = TEST_THEORY.get_description()
test_theory = API.theoryid(theoryid=THEORY_QED)
theory = test_theory.get_description()

alpha = Alpha(theory)

Expand All @@ -76,7 +79,8 @@ def test_set_thresholds_alpha_em():

def test_betas():
"test betas for different nf"
alpha = Alpha(TEST_THEORY.get_description())
test_theory = API.theoryid(theoryid=THEORY_QED)
alpha = Alpha(test_theory.get_description())
vec_beta0 = [
-0.5305164769729844,
-0.6719875374991137,
Expand All @@ -98,17 +102,18 @@ def test_photon():
"""test that photon coming out of Photon interpolator matches the photon array
for XGRID points
"""
fiatlux_runcard = FIATLUX_RUNCARD.copy()
fiatlux_runcard = generate_fiatlux_runcard()
fiatlux_runcard["additional_errors"] = False
theory = TEST_THEORY.get_description()
test_theory = API.theoryid(theoryid=THEORY_QED)
theory = test_theory.get_description()

for replica in [1, 2, 3]:
photon = Photon(TEST_THEORY, fiatlux_runcard, [replica])
photon = Photon(test_theory, fiatlux_runcard, [replica])

# set up fiatlux
path_to_F2 = TEST_THEORY.path / "fastkernel/fiatlux_dis_F2.pineappl.lz4"
path_to_FL = TEST_THEORY.path / "fastkernel/fiatlux_dis_FL.pineappl.lz4"
pdfs = FIATLUX_RUNCARD["luxset"].load()
path_to_F2 = test_theory.path / "fastkernel/fiatlux_dis_F2.pineappl.lz4"
path_to_FL = test_theory.path / "fastkernel/fiatlux_dis_FL.pineappl.lz4"
pdfs = fiatlux_runcard["luxset"].load()
f2 = sf.InterpStructureFunction(path_to_F2, pdfs.members[replica])
fl = sf.InterpStructureFunction(path_to_FL, pdfs.members[replica])
f2lo = sf.F2LO(pdfs.members[replica], theory)
Expand All @@ -118,7 +123,7 @@ def test_photon():
fiatlux_default['mproton'] = theory['MP']
fiatlux_default["qed_running"] = bool(np.isclose(theory["Qedref"], theory["Qref"]))
fiatlux_default["q2_max"] = float(f2.q2_max)
fiatlux_default["eps_base"] = FIATLUX_RUNCARD["eps_base"]
fiatlux_default["eps_base"] = fiatlux_runcard["eps_base"]

# load fiatlux
with tempfile.NamedTemporaryFile(mode="w") as tmp:
Expand All @@ -136,7 +141,7 @@ def test_photon():
]
)
lux.PlugStructureFunctions(f2.fxq, fl.fxq, f2lo.fxq)
path_to_eko_photon = TEST_THEORY.path / "eko_photon.tar"
path_to_eko_photon = test_theory.path / "eko_photon.tar"
with EKO.read(path_to_eko_photon) as eko:
photon_fiatlux_qin = np.array([lux.EvaluatePhoton(x, eko.mu20).total for x in XGRID])
photon_fiatlux_qin /= XGRID
Expand Down
19 changes: 10 additions & 9 deletions validphys2/src/validphys/tests/photon/test_structurefunctions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
from validphys.core import PDF as PDFset
import validphys.photon.structure_functions as sf

from ..conftest import PDF

TEST_THEORY = API.theoryid(theoryid=398)
from ..conftest import PDF, THEORY_QED


class ZeroPdfs:
Expand Down Expand Up @@ -44,9 +42,10 @@ def convolute_with_one(self, pdgid, xfxQ2):
def test_zero_pdfs():
"test that a zero PDF gives a zero structure function"
pdfs = ZeroPdfs()
theory = TEST_THEORY.get_description()
path_to_F2 = TEST_THEORY.path / "fastkernel/fiatlux_dis_F2.pineappl.lz4"
path_to_FL = TEST_THEORY.path / "fastkernel/fiatlux_dis_FL.pineappl.lz4"
test_theory = API.theoryid(theoryid=THEORY_QED)
theory = test_theory.get_description()
path_to_F2 = test_theory.path / "fastkernel/fiatlux_dis_F2.pineappl.lz4"
path_to_FL = test_theory.path / "fastkernel/fiatlux_dis_FL.pineappl.lz4"

f2 = sf.InterpStructureFunction(path_to_F2, pdfs)
fl = sf.InterpStructureFunction(path_to_FL, pdfs)
Expand Down Expand Up @@ -76,10 +75,11 @@ def test_params():
"test initialization of parameters"
pdfs = PDFset(PDF).load()
replica = 1
theory = TEST_THEORY.get_description()
test_theory = API.theoryid(theoryid=THEORY_QED)
theory = test_theory.get_description()
for channel in ["F2", "FL"]:
tmp = "fastkernel/fiatlux_dis_" + channel + ".pineappl.lz4"
path_to_fktable = TEST_THEORY.path / tmp
path_to_fktable = test_theory.path / tmp
struct_func = sf.InterpStructureFunction(path_to_fktable, pdfs.members[replica])
np.testing.assert_allclose(struct_func.q2_max, 1e8)
f2lo = sf.F2LO(pdfs.members[replica], theory)
Expand All @@ -91,10 +91,11 @@ def test_params():
def test_interpolation_grid():
"""test that the values coming out of InterpStructureFunction match the grid ones"""
pdfs = PDFset(PDF).load()
test_theory = API.theoryid(theoryid=THEORY_QED)
for replica in [1, 2, 3]:
for channel in ["F2", "FL"]:
tmp = "fastkernel/fiatlux_dis_" + channel + ".pineappl.lz4"
path_to_fktable = TEST_THEORY.path / tmp
path_to_fktable = test_theory.path / tmp
fktable = pineappl.fk_table.FkTable.read(path_to_fktable)
x = np.unique(fktable.bin_left(1))
q2 = np.unique(fktable.bin_left(0))
Expand Down