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

ekobox unittests #109

Merged
merged 20 commits into from
Mar 25, 2022
Merged
Show file tree
Hide file tree
Changes from 19 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
33 changes: 7 additions & 26 deletions benchmarks/ekobox/benchmark_evol_pdf.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
# -*- coding: utf-8 -*-
import pathlib

import lhapdf
import numpy as np
import pytest

import eko.output.legacy
from eko import basis_rotation as br
from ekobox import evol_pdf as ev_p
from ekobox import gen_op as g_o
from ekobox import gen_theory as g_t
from ekobox import operators_card as oc
from ekobox import theory_card as tc
from ekobox.genpdf import load

test_pdf = pathlib.Path(__file__).parent / "fakepdf"
lhapdf = pytest.importorskip("lhapdf")
felixhekhorn marked this conversation as resolved.
Show resolved Hide resolved


@pytest.mark.isolated
Expand All @@ -22,8 +21,8 @@ def benchmark_evolve_single_member(tmp_path, cd, lhapdf_path):
100.0,
10000.0,
]
op = g_o.gen_op_card(q2grid)
theory = g_t.gen_theory_card(
op = oc.generate(q2grid)
theory = tc.generate(
0,
5.0,
update={
Expand Down Expand Up @@ -78,8 +77,8 @@ def benchmark_evolve_single_member(tmp_path, cd, lhapdf_path):

@pytest.mark.isolated
def benchmark_evolve_more_members(tmp_path, cd, lhapdf_path):
op = g_o.gen_op_card([10, 100], update={"xgrid": [1e-7, 0.01, 0.1, 0.2, 0.3]})
theory = g_t.gen_theory_card(0, 1.0)
op = oc.generate([10, 100], update={"xgrid": [1e-7, 0.01, 0.1, 0.2, 0.3]})
theory = tc.generate(0, 1.0)
with lhapdf_path(test_pdf):
pdfs = lhapdf.mkPDFs("myMSTW2008nlo90cl")
d = tmp_path / "sub"
Expand All @@ -91,21 +90,3 @@ def benchmark_evolve_more_members(tmp_path, cd, lhapdf_path):
_ = lhapdf.mkPDFs("Debug")
info = load.load_info_from_file("Debug")
assert info["XMin"] == op["xgrid"][0]


@pytest.mark.isolated
def benchmark_gen_and_dump_out(tmp_path):
op = g_o.gen_op_card([100.0], update={"xgrid": [1e-7, 0.01, 0.1, 0.2, 0.3]})
theory = g_t.gen_theory_card(0, 1.0)

out = ev_p.gen_out(theory, op, path=tmp_path)

ops_id = f"o{op['hash'][:6]}_t{theory['hash'][:6]}"
outpath = f"{tmp_path}/{ops_id}.tar"
loaded_out = eko.output.legacy.load_tar(outpath)
assert list(out.xgrid) == list(loaded_out.xgrid)
for el, load_el in zip(out[100.0].operator, loaded_out[100.0].operator):
np.testing.assert_allclose(
out[100.0].operator,
loaded_out[100.0].operator,
)
30 changes: 0 additions & 30 deletions benchmarks/ekobox/benchmark_gen_theory.py

This file was deleted.

88 changes: 0 additions & 88 deletions benchmarks/ekobox/genpdf/benchmark_flavors.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,38 +13,6 @@
lhapdf = pytest.importorskip("lhapdf")


@pytest.mark.isolated
def benchmark_is_evolution():
assert genpdf.flavors.is_evolution_labels(["V", "T3"])
assert not genpdf.flavors.is_evolution_labels(["21", "2"])


@pytest.mark.isolated
def benchmark_is_pids():
assert not genpdf.flavors.is_pid_labels(["V", "T3"])
assert not genpdf.flavors.is_pid_labels(["35", "9"])
assert not genpdf.flavors.is_pid_labels({})
assert genpdf.flavors.is_pid_labels([21, 2])


@pytest.mark.isolated
def benchmark_flavors_pid_to_flavor():
flavs = genpdf.flavors.pid_to_flavor([1, 2, 21, -3])
for f in flavs:
for g in flavs:
if not np.allclose(f, g):
assert f @ g == 0


@pytest.mark.isolated
def benchmark_flavors_evol_to_flavor():
flavs = genpdf.flavors.evol_to_flavor(["S", "g", "T3", "V8"])
for f in flavs:
for g in flavs:
if not np.allclose(f, g):
assert f @ g == 0


@pytest.mark.isolated
def benchmark_flavors_pids_ct14(tmp_path, cd):
with cd(tmp_path):
Expand Down Expand Up @@ -101,59 +69,3 @@ def benchmark_flavors_evol_ct14(tmp_path, cd):
np.testing.assert_allclose(
pdf.xfxQ2(21, x, Q2), gonly.xfxQ2(21, x, Q2)
)


@pytest.mark.isolated
def benchmark_flavors_evol_raw():
blocks = [
{
"Q2grid": np.array([1, 2]),
"xgrid": np.array([0.1, 1.0]),
"pids": np.array([-1, 21, 1]),
"data": np.array([[0.1, 0.2, 0.1]] * 4),
}
]
gonly = genpdf.flavors.project(blocks, genpdf.flavors.evol_to_flavor(["g"]))
assert len(gonly) == 1
np.testing.assert_allclose(
gonly[0]["data"],
np.array(
[[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.2, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]] * 4
),
)
Sonly = genpdf.flavors.project(blocks, genpdf.flavors.evol_to_flavor(["S"]))
assert len(Sonly) == 1
for i in [0, 1, 2, 3]:
# g and gamma are zero
np.testing.assert_allclose(Sonly[0]["data"][i][7], 0)
np.testing.assert_allclose(Sonly[0]["data"][i][0], 0)
# quark are all equal and equal to anti-quarks
for pid in [2, 3, 4, 5, 6, 8, 9, 10, 11, 12, 13]:
np.testing.assert_allclose(Sonly[0]["data"][i][pid], Sonly[0]["data"][i][1])


@pytest.mark.isolated
def benchmark_flavors_evol_nodata():
# try with a block without data
blocks = [
{
"Q2grid": np.array([1, 2]),
"xgrid": np.array([0.1, 1.0]),
"pids": np.array([-1, 21, 1]),
"data": np.array([]),
},
{
"Q2grid": np.array([1, 2]),
"xgrid": np.array([0.1, 1.0]),
"pids": np.array([-1, 21, 1]),
"data": np.array([[0.1, 0.2, 0.1]] * 4),
},
]
gonly = genpdf.flavors.project(blocks, genpdf.flavors.evol_to_flavor(["g"]))
assert len(gonly) == 2
np.testing.assert_allclose(
gonly[1]["data"],
np.array(
[[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.2, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]] * 4
),
)
26 changes: 0 additions & 26 deletions benchmarks/ekobox/genpdf/benchmark_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,6 @@
lhapdf = pytest.importorskip("lhapdf")


@pytest.mark.isolated
def benchmark_genpdf_exceptions(tmp_path, cd):
# using a wrong label and then a wrong parent pdf
with cd(tmp_path):
with pytest.raises(TypeError):
genpdf.generate_pdf(
"test_genpdf_exceptions1",
["f"],
{
21: lambda x, Q2: 3.0 * x * (1.0 - x),
2: lambda x, Q2: 4.0 * x * (1.0 - x),
},
)
with pytest.raises(ValueError):
genpdf.generate_pdf(
"test_genpdf_exceptions2",
["g"],
10,
)
with pytest.raises(FileExistsError):
genpdf.install_pdf("foo")

with pytest.raises(TypeError):
genpdf.generate_pdf("debug", [21], info_update=(10, 15, 20))


@pytest.mark.isolated
def benchmark_genpdf_no_parent_and_install(tmp_path, cd):
with cd(tmp_path):
Expand Down
5 changes: 3 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ python_classes = ['Test*', 'Benchmark*']
python_functions = ['test_*', 'benchmark_*']
addopts = [
'--cov=eko',
# '--cov=ekobox',
'--cov=ekobox',
'--cov-report=html',
'--cov-report=xml',
'--strict-markers',
Expand All @@ -113,7 +113,8 @@ markers = ["isolated: marks benchmarks as isolated"]
# extensions not to check
extension-pkg-whitelist = ["numpy", "numba", "lhapdf", "pegasus"]
ignore-paths = ["benchmarks/", "doc/", "tests/"]
jobs = 1 # has to be 1 as pylint is NOT threadsafe
# jobs has to be 1 as pylint is NOT threadsafe
jobs = 1
[tool.pylint.messages_control]
disable = ["invalid-name", "fixme"]
[tool.pylint.reports]
Expand Down
2 changes: 1 addition & 1 deletion src/ekobox/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# -*- coding: utf-8 -*-
from ekomark import apply

from . import evol_pdf, gen_info, gen_op, gen_theory
from . import evol_pdf, info_file, operators_card, theory_card
felixhekhorn marked this conversation as resolved.
Show resolved Hide resolved
43 changes: 5 additions & 38 deletions src/ekobox/evol_pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from eko import basis_rotation as br
from ekomark import apply

from . import gen_info, genpdf
from . import genpdf, info_file


def evolve_pdfs(
Expand All @@ -27,36 +27,26 @@ def evolve_pdfs(
----------
initial_PDF_list : list(lhapdf object)
list of PDF members to be evolved

theory_card : dict
theory card

operators_card : dict
operators card

path : str
path to cached eko output (if "None" it will be recomputed)

targetgrid : list(float)
target x-grid (if different from input x-grid)

install : bool
set whether to install evolved PDF to lhapdf directory

name : str
set name of evolved PDF

info_update : dict
dict of info to add or update to default info file

"""
eko_output = None
if path is not None:
my_path = pathlib.Path(path)
if my_path.is_dir():
ops_id = f"o{operators_card['hash'][:6]}_t{theory_card['hash'][:6]}.tar"
ops_id_path = pathlib.Path(ops_id)
outpath = my_path / ops_id_path.relative_to(ops_id_path.anchor)
outpath = my_path / ekofileid(theory_card, operators_card)
eko_output = eko.output.legacy.load_tar(outpath)
else:
eko_output = eko.output.legacy.load_tar(my_path)
Expand All @@ -73,7 +63,7 @@ def evolve_pdfs(
info_update = {}
info_update["XMin"] = targetgrid[0]
info_update["XMax"] = targetgrid[-1]
info = gen_info.create_info_file(
info = info_file.build(
theory_card,
operators_card,
len(evolved_PDF_list),
Expand All @@ -100,28 +90,5 @@ def evolve_pdfs(
genpdf.install_pdf(name)


def gen_out(theory_card, op_card, path=None):
"""
Generates EKO output from theory and operators cards and, if requested,
dumps it in tar format

Parameters
----------
theory_card : dict
theory card
op_card : dict
operators card
path : str
path of dumped output (if "None" output is not dumped)

Returns
-------
: eko.output.Output
eko output
"""
eko_output = eko.run_dglap(theory_card, op_card)
if path is not None:
ops_id = f"o{op_card['hash'][:6]}_t{theory_card['hash'][:6]}"
path = f"{path}/{ops_id}.tar"
eko.output.legacy.dump_tar(eko_output, path)
return eko_output
def ekofileid(theory_card, operators_card):
felixhekhorn marked this conversation as resolved.
Show resolved Hide resolved
return f"o{operators_card['hash'][:6]}_t{theory_card['hash'][:6]}.tar"
Loading