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

Refactor runner #159

Merged
merged 3 commits into from
Nov 8, 2022
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
40 changes: 1 addition & 39 deletions src/eko/runner.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
"""This file contains the main application class of eko."""
"""Main application class of eko."""
andreab1997 marked this conversation as resolved.
Show resolved Hide resolved
import copy
import logging
from typing import Optional
Expand Down Expand Up @@ -75,13 +75,6 @@ def __init__(self, theory_card: dict, operators_card: dict):
# setup operator grid
self.op_grid = OperatorGrid.from_dict(new_theory, new_operators, tc, sc, bfd)

# save bases manipulations for a post processing step
rot = operators_card.get("rotations", {})
self.post_process = {}
for key in ("inputgrid", "targetgrid", "inputpids", "targetpids"):
self.post_process[key] = rot.get(key, None)
new_operators["rotations"][key] = None

self.out = EKO.new(theory=theory_card, operator=new_operators)

def get_output(self) -> EKO:
Expand All @@ -103,35 +96,4 @@ def get_output(self) -> EKO:
for final_scale, op in self.op_grid.compute().items():
self.out[float(final_scale)] = Operator.from_dict(op)

def similar_to_none(name: str) -> Optional[np.ndarray]:
grid = self.post_process[name]

default = self.out.xgrid.grid if "grid" in name else self.out.rotations.pids
if grid is None or (
len(grid) == default.size and np.allclose(grid, default, atol=1e-12)
):
return None

return np.array(grid)

# reshape xgrid
inputgrid = similar_to_none("inputgrid")
targetgrid = similar_to_none("targetgrid")
if inputgrid is not None or targetgrid is not None:
if inputgrid is not None:
inputgrid = interpolation.XGrid(inputgrid)
if targetgrid is not None:
targetgrid = interpolation.XGrid(targetgrid)
manipulate.xgrid_reshape(
self.out, targetgrid=targetgrid, inputgrid=inputgrid
)

# reshape flavors
inputpids = similar_to_none("inputpids")
targetpids = similar_to_none("targetpids")
if inputpids is not None or targetpids is not None:
manipulate.flavor_reshape(
self.out, targetpids=targetpids, inputpids=inputpids
)

return copy.deepcopy(self.out)
23 changes: 18 additions & 5 deletions tests/eko/test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,14 @@ def test_targetgrid():
oc["rotations"]["targetgrid"] = tgrid
r = eko.runner.Runner(tc, oc)
o = r.get_output()
# We are testing if (as expected) the runner ignores inputgrid and targetgrid.
actual_grid = oc["rotations"]["xgrid"]
check_shapes(
o, eko.interpolation.XGrid(tgrid), eko.interpolation.XGrid(igrid), tc, oc
o,
eko.interpolation.XGrid(actual_grid),
eko.interpolation.XGrid(actual_grid),
tc,
oc,
)
# check actual value
np.testing.assert_allclose(o.rotations.targetgrid.raw, tgrid)
Expand All @@ -95,19 +101,26 @@ def test_rotate_pids():
o = r.get_output()
check_shapes(o, o.xgrid, o.xgrid, tc, oc)
# check actual values
assert (o.rotations.targetpids == [0] * 14).all()
assert (o.rotations.inputpids == [0] * 14).all()
assert (o.rotations.targetpids == oc["rotations"]["targetpids"]).all()
assert (o.rotations.inputpids == oc["rotations"]["inputpids"]).all()


def check_shapes(o, txs, ixs, theory_card, operators_card):
tpids = len(o.rotations.targetpids)
ipids = len(o.rotations.inputpids)
op_shape = (tpids, len(txs), ipids, len(ixs))

op_targetgrid = operators_card["rotations"]["targetgrid"]
op_inputgrid = operators_card["rotations"]["inputgrid"]
# check output = input
np.testing.assert_allclose(o.xgrid.raw, operators_card["rotations"]["xgrid"])
np.testing.assert_allclose(o.rotations.targetgrid.raw, txs.raw)
np.testing.assert_allclose(o.rotations.inputgrid.raw, ixs.raw)
np.testing.assert_allclose(
o.rotations.targetgrid.raw,
op_targetgrid if op_targetgrid is not None else txs.raw,
)
np.testing.assert_allclose(
o.rotations.inputgrid.raw, op_inputgrid if op_inputgrid is not None else ixs.raw
)
for k in ["interpolation_polynomial_degree", "interpolation_is_log"]:
assert getattr(o.configs, k) == operators_card["configs"][k]
np.testing.assert_allclose(o.Q02, theory_card["Q0"] ** 2)
Expand Down