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

Remove CorrectionWithGradient.eval_dict #35

Merged
merged 1 commit into from
Oct 29, 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
13 changes: 4 additions & 9 deletions src/correctionlib_gradients/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,16 +108,11 @@ def __init__(self, c: schema.Correction):

def evaluate(self, *inputs: Value) -> Value:
if (n_in := len(inputs)) != (n_expected := len(self._input_names)):
msg = f"This correction requires {n_expected} input(s), {n_in} provided"
msg = (
f"This correction requires {n_expected} input(s), {n_in} provided."
f" Required inputs are {self._input_names}"
)
raise ValueError(msg)

input_dict = dict(zip(self._input_names, inputs))
return self._dag.evaluate(input_dict)

def eval_dict(self, inputs: dict[str, Value]) -> Value:
for n in self._input_names:
if n not in inputs:
msg = f"Variable '{n}' is required by correction '{self._name}' but is not present in input"
raise ValueError(msg)

return self._dag.evaluate(inputs)
27 changes: 2 additions & 25 deletions tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ def test_missing_input():
cg = CorrectionWithGradient(schemas["scale"])

with pytest.raises(
ValueError, match="Variable 'x' is required by correction 'test scalar' but is not present in input"
ValueError, match="This correction requires 1 input\\(s\\), 0 provided. Required inputs are \\['x'\\]"
):
cg.eval_dict({})
cg.evaluate()


def test_unsupported_correction():
Expand Down Expand Up @@ -146,16 +146,6 @@ def test_evaluate_scale(jit):
assert grad == 0.0


@pytest.mark.parametrize("jit", [False, True])
def test_eval_dict_scale(jit):
cg = CorrectionWithGradient(schemas["scale"])
eval_dict = jax.jit(cg.eval_dict) if jit else cg.eval_dict
value, grad = jax.value_and_grad(eval_dict)({"x": 4.2})
assert math.isclose(value, 1.234)
assert list(grad.keys()) == ["x"]
assert grad["x"] == 0.0


@pytest.mark.parametrize("jit", [False, True])
def test_vectorized_evaluate_scale(jit):
cg = CorrectionWithGradient(schemas["scale"])
Expand All @@ -168,19 +158,6 @@ def test_vectorized_evaluate_scale(jit):
assert grads[1] == 0.0


@pytest.mark.parametrize("jit", [False, True])
def test_vectorized_eval_dict_scale(jit):
cg = CorrectionWithGradient(schemas["scale"])
eval_dict = jax.jit(cg.eval_dict) if jit else cg.eval_dict
x = np.array([0.0, 1.0])
values, grads = jax.value_and_grad(eval_dict)({"x": x})
assert np.allclose(values, [1.234, 1.234])
assert list(grads.keys()) == ["x"]
assert len(grads["x"]) == 2
assert grads["x"][0] == 0.0
assert grads["x"][1] == 0.0


def test_vectorized_evaluate_simple_uniform_binning():
cg = CorrectionWithGradient(schemas["simple-uniform-binning"])
x = [3.0, 5.0, 11.0] # 11. overflows: it tests clamping
Expand Down