Skip to content

Commit 691345a

Browse files
committed
Moved to module-level logging, fixed Python 2.7 syntax error in ctrsbox, removed default logging from constrained example
1 parent 8819a16 commit 691345a

File tree

8 files changed

+61
-52
lines changed

8 files changed

+61
-52
lines changed

dfols/controller.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343
'EXIT_INPUT_ERROR', 'EXIT_TR_INCREASE_ERROR', 'EXIT_LINALG_ERROR', 'EXIT_FALSE_SUCCESS_WARNING',
4444
'EXIT_AUTO_DETECT_RESTART_WARNING']
4545

46+
module_logger = logging.getLogger(__name__)
47+
4648
EXIT_TR_INCREASE_WARNING = 5 # warning, TR increase in proj constrained case - likely due to multiple active constraints
4749
EXIT_AUTO_DETECT_RESTART_WARNING = 4 # warning, auto-detected restart criteria
4850
EXIT_FALSE_SUCCESS_WARNING = 3 # warning, maximum fake successful steps reached
@@ -134,7 +136,7 @@ def npt(self):
134136

135137
def initialise_coordinate_directions(self, number_of_samples, num_directions, params):
136138
if self.do_logging:
137-
logging.debug("Initialising with coordinate directions")
139+
module_logger.debug("Initialising with coordinate directions")
138140
# self.model already has x0 evaluated, so only need to initialise the other points
139141
# num_directions = params("growing.ndirs_initial")
140142
assert self.model.num_pts <= (self.n() + 1) * (self.n() + 2) // 2, "prelim: must have npt <= (n+1)(n+2)/2"
@@ -311,7 +313,7 @@ def initialise_coordinate_directions(self, number_of_samples, num_directions, pa
311313

312314
def initialise_random_directions(self, number_of_samples, num_directions, params):
313315
if self.do_logging:
314-
logging.debug("Initialising with random orthogonal directions")
316+
module_logger.debug("Initialising with random orthogonal directions")
315317
# self.model already has x0 evaluated, so only need to initialise the other points
316318
assert 1 <= num_directions < self.model.num_pts, "Initialisation: must have 1 <= ndirs_initial < npt"
317319

@@ -442,7 +444,7 @@ def trust_region_step(self, params):
442444

443445
def geometry_step(self, knew, adelt, number_of_samples, params):
444446
if self.do_logging:
445-
logging.debug("Running geometry-fixing step")
447+
module_logger.debug("Running geometry-fixing step")
446448
try:
447449
c, g = self.model.lagrange_gradient(knew)
448450
# c = 1.0 if knew == self.model.kopt else 0.0 # based at xopt, just like d
@@ -643,12 +645,12 @@ def terminate_from_slow_iterations(self, current_iter, params):
643645
if this_iter_slow:
644646
self.num_slow_iters += 1
645647
if self.do_logging:
646-
logging.info("Slow iteration (%g consecutive so far, max allowed %g)"
648+
module_logger.info("Slow iteration (%g consecutive so far, max allowed %g)"
647649
% (self.num_slow_iters, params("slow.max_slow_iters")))
648650
else:
649651
self.num_slow_iters = 0
650652
if self.do_logging:
651-
logging.debug("Non-slow iteration")
653+
module_logger.debug("Non-slow iteration")
652654
return this_iter_slow, self.num_slow_iters >= params("slow.max_slow_iters")
653655

654656
def soft_restart(self, number_of_samples, nruns_so_far, params, x_in_abs_coords_to_save=None, rvec_to_save=None,
@@ -677,7 +679,7 @@ def soft_restart(self, number_of_samples, nruns_so_far, params, x_in_abs_coords_
677679
self.model.nsamples[self.model.kopt], x_in_abs_coords=True)
678680

679681
if self.do_logging:
680-
logging.info("Soft restart [currently, f = %g after %g function evals]" % (self.model.fopt(), self.nf))
682+
module_logger.info("Soft restart [currently, f = %g after %g function evals]" % (self.model.fopt(), self.nf))
681683
# Resetting method: reset delta and rho, then move the closest 'num_steps' points to xk to improve geometry
682684
# Note: closest points because we are suddenly increasing delta & rho, so we want to encourage spreading out points
683685
self.delta = self.rhobeg
@@ -734,7 +736,7 @@ def soft_restart(self, number_of_samples, nruns_so_far, params, x_in_abs_coords_
734736
self.model.add_new_sample(self.model.npt() - 1, rvec_extra=rvec_list[i, :])
735737

736738
if self.do_logging:
737-
logging.info("Soft restart: added %g new directions, npt is now %g" % (num_pts_to_add, self.model.npt()))
739+
module_logger.info("Soft restart: added %g new directions, npt is now %g" % (num_pts_to_add, self.model.npt()))
738740

739741
# Otherwise, we are doing a restart
740742
self.last_successful_iter = 0

dfols/model.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@
4040

4141
__all__ = ['Model']
4242

43+
module_logger = logging.getLogger(__name__)
44+
45+
4346
class Model(object):
4447
def __init__(self, npt, x0, r0, xl, xu, projections, r0_nsamples, n=None, m=None, abs_tol=1e-12, rel_tol=1e-20, precondition=True,
4548
do_logging=True):
@@ -306,7 +309,7 @@ def solve_geom_system(self, rhs):
306309
return col_scale(LA.solve_triangular(self.R, Qb), self.right_scaling)
307310
else:
308311
if self.do_logging:
309-
logging.warning("model.solve_geom_system not using factorisation")
312+
module_logger.warning("model.solve_geom_system not using factorisation")
310313
W, left_scaling, right_scaling = self.interpolation_matrix()
311314
return col_scale(LA.lstsq(W, col_scale(rhs * left_scaling))[0], right_scaling)
312315

@@ -334,7 +337,7 @@ def interpolate_mini_models_svd(self, verbose=False, make_full_rank=False, min_s
334337
rhs = self.fval_v[fval_row_idx, :] # size npt * m
335338
if np.any(np.isnan(rhs)) and throw_error_on_nans:
336339
if self.do_logging:
337-
logging.warning("model.interpolate_mini_models_svd: NaNs encountered in objective evaluations, raising error")
340+
module_logger.warning("model.interpolate_mini_models_svd: NaNs encountered in objective evaluations, raising error")
338341
raise np.linalg.LinAlgError("NaN encountered in objective evaluations")
339342
try:
340343
dg = self.solve_geom_system(rhs) # size (n+1)*m

0 commit comments

Comments
 (0)