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

FD objective with fixed parameters fix #1446

Merged
merged 5 commits into from
Sep 5, 2024
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
1 change: 1 addition & 0 deletions pypesto/objective/finite_difference.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,7 @@ def __init__(
self.delta_grad: FDDelta = to_delta(delta_grad)
self.delta_res: FDDelta = to_delta(delta_res)
self.method: str = method
self.pre_post_processor = obj.pre_post_processor

if method not in FD.METHODS:
raise ValueError(
Expand Down
15 changes: 11 additions & 4 deletions test/base/test_objective.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,12 +342,20 @@ def fd_delta(request):
return request.param


def test_fds(fd_method, fd_delta):
# add a fixture for fixed and unfixed parameters
@pytest.mark.parametrize("fixed", [True, False])
def test_fds(fd_method, fd_delta, fixed):
"""Test finite differences."""
problem = CRProblem()

# reference objective
obj = problem.get_objective()
if fixed:
fixed_problem = problem.get_problem()
fixed_problem.fix_parameters([1], problem.p_true[1])
obj = fixed_problem.objective
p = problem.p_true[0]
else:
obj = problem.get_objective()
p = problem.p_true

# FDs for everything
obj_fd = pypesto.FD(
Expand Down Expand Up @@ -394,7 +402,6 @@ def test_fds(fd_method, fd_delta):
delta_grad=fd_delta,
delta_res=fd_delta,
)
p = problem.p_true

# check that function values coincide (call delegated)
for attr in ["fval", "res"]:
Expand Down
4 changes: 2 additions & 2 deletions test/hierarchical/test_censored.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def calculate(problem, x_dct):
problem.objective,
)
finite_differences_results = finite_differences(
petab_problem.x_nominal_scaled,
petab_problem.x_nominal_free_scaled,
(
0,
1,
Expand All @@ -100,7 +100,7 @@ def calculate(problem, x_dct):
# with finite differences.
assert np.allclose(
finite_differences_results[1],
calculator_result["grad"],
calculator_result["grad"][petab_problem.x_free_indices],
)


Expand Down
4 changes: 2 additions & 2 deletions test/hierarchical/test_ordinal.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ def inner_calculate(problem, x_dct):
problem.objective,
)
finite_differences_results = finite_differences(
petab_problem.x_nominal_scaled,
petab_problem.x_nominal_free_scaled,
(
0,
1,
Expand Down Expand Up @@ -209,7 +209,7 @@ def inner_calculate(problem, x_dct):
# with finite differences.
assert np.allclose(
finite_differences_results[1],
calculator_results[STANDARD]["grad"],
calculator_results[STANDARD]["grad"][petab_problem.x_free_indices],
)

# Since the nominal parameters are close to true ones,
Expand Down
6 changes: 4 additions & 2 deletions test/hierarchical/test_spline.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ def inner_calculate(problem, x_dct):

finite_differences = pypesto.objective.FD(problem.objective)
FD_results = finite_differences(
x=petab_problem.x_nominal_scaled,
x=petab_problem.x_nominal_free_scaled,
sensi_orders=(0, 1),
mode=MODE_FUN,
)
Expand Down Expand Up @@ -210,7 +210,9 @@ def inner_calculate(problem, x_dct):
# The gradient should be close to the one calculated using
# finite differences.
assert np.allclose(
calculator_results["minimal_diff_on"]["grad"],
calculator_results["minimal_diff_on"]["grad"][
petab_problem.x_free_indices
],
FD_results[1],
atol=atol,
)
Expand Down