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

Make test/base/ less verbose #1188

Merged
merged 3 commits into from
Nov 19, 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
2 changes: 1 addition & 1 deletion pypesto/ensemble/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def __init__(

def execute(self) -> List[Any]:
"""Execute the task."""
logger.info(f"Executing task {self.id}.")
logger.debug(f"Executing task {self.id}.")
results = []
for index in range(self.vectors.shape[1]):
results.append(self.method(self.vectors[:, index]))
Expand Down
2 changes: 1 addition & 1 deletion pypesto/objective/finite_difference.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ def _update(
self.delta = delta_opt

# log
logger.info(f"Optimal FD delta: {self.delta}")
logger.debug(f"Optimal FD delta: {self.delta}")
self.updates += 1

def get(self) -> np.ndarray:
Expand Down
2 changes: 1 addition & 1 deletion pypesto/predict/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@ def __init__(

def execute(self) -> 'pypesto.predict.PredictionResult': # noqa: F821
"""Execute and return the prediction."""
logger.info(f"Executing task {self.id}.")
logger.debug(f"Executing task {self.id}.")
prediction = self.predictor(self.x, self.sensi_orders, self.mode)
return prediction
49 changes: 27 additions & 22 deletions test/base/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,40 @@

def test_optimize():
# logging
pypesto.logging.log_to_console(logging.WARN)
filename = ".test_logging.tmp"
pypesto.logging.log_to_file(logging.DEBUG, filename)
logger = logging.getLogger('pypesto')
if os.path.exists(filename):
os.remove(filename)
fh = logging.FileHandler(filename)
fh.setLevel(logging.DEBUG)
logger.addHandler(fh)
logger.info("start test")

# problem definition
def fun(_):
raise Exception("This function cannot be called.")

objective = pypesto.Objective(fun=fun)
problem = pypesto.Problem(objective, -1, 1)

optimizer = pypesto.optimize.ScipyOptimizer()
options = {'allow_failed_starts': True}

# optimization
pypesto.optimize.minimize(
problem=problem,
optimizer=optimizer,
n_starts=5,
options=options,
progress_bar=False,
)

old_handlers = logger.handlers
logger.handlers = []
try:
logger.addHandler(fh)
logger.info("start test")

# problem definition
def fun(_):
raise Exception("This function cannot be called.")

objective = pypesto.Objective(fun=fun)
problem = pypesto.Problem(objective, -1, 1)

optimizer = pypesto.optimize.ScipyOptimizer()
options = {'allow_failed_starts': True}

# optimization
pypesto.optimize.minimize(
problem=problem,
optimizer=optimizer,
n_starts=5,
options=options,
progress_bar=False,
)
finally:
logger.handlers = old_handlers

# assert logging worked
assert os.path.exists(filename)
Expand Down
20 changes: 13 additions & 7 deletions test/base/test_objective.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,11 @@
import copy
import numbers

import aesara.tensor as aet
import jax
import jax.numpy as jnp
import numpy as np
import pytest
import sympy as sp

import pypesto
from pypesto.objective.aesara import AesaraObjective
from pypesto.objective.jax import JaxObjective

from ..util import CRProblem, poly_for_sensi, rosen_for_sensi

Expand Down Expand Up @@ -162,7 +157,9 @@ def rel_err(eps_):

# Test the single step size `check_grad` method.
eps = 1e-5
result_single_eps = objective.check_grad(np.array([theta]), eps=eps)
result_single_eps = objective.check_grad(
np.array([theta]), eps=eps, verbosity=False
)
np.testing.assert_almost_equal(
result_single_eps['rel_err'].squeeze(),
rel_err(eps),
Expand All @@ -171,7 +168,7 @@ def rel_err(eps_):
# Test the multiple step size `check_grad_multi_eps` method.
multi_eps = {1e-1, 1e-3, 1e-5, 1e-7, 1e-9}
result_multi_eps = objective.check_grad_multi_eps(
[theta], multi_eps=multi_eps
[theta], multi_eps=multi_eps, verbosity=False
)

np.testing.assert_almost_equal(
Expand All @@ -182,6 +179,10 @@ def rel_err(eps_):

def test_aesara(max_sensi_order, integrated):
"""Test function composition and gradient computation via aesara"""
import aesara.tensor as aet

from pypesto.objective.aesara import AesaraObjective

prob = rosen_for_sensi(max_sensi_order, integrated, [0, 1])

# create aesara specific symbolic tensor variables
Expand Down Expand Up @@ -215,6 +216,11 @@ def test_aesara(max_sensi_order, integrated):

def test_jax(max_sensi_order, integrated):
"""Test function composition and gradient computation via jax"""
import jax
import jax.numpy as jnp

from pypesto.objective.jax import JaxObjective

prob = rosen_for_sensi(max_sensi_order, integrated, [0, 1])

# apply inverse transform such that we evaluate at prob['x']
Expand Down