Skip to content

Commit 62519bb

Browse files
committed
Comply to abstractmethod interface in BayesianOptimizer
1 parent c6ce680 commit 62519bb

File tree

4 files changed

+25
-15
lines changed

4 files changed

+25
-15
lines changed

climada/test/test_util_calibrate.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ def test_single(self):
176176
init_points=10, n_iter=20, max_iterations=1
177177
)
178178
optimizer = BayesianOptimizer(self.input, random_state=1)
179-
output = optimizer.run(controller)
179+
output = optimizer.run(controller=controller)
180180

181181
# Check result (low accuracy)
182182
self.assertAlmostEqual(output.params["slope"], 1.0, places=2)
@@ -210,7 +210,7 @@ def test_multiple_constrained(self):
210210
controller = BayesianOptimizerController.from_input(
211211
self.input, sampling_base=5, max_iterations=3
212212
)
213-
output = optimizer.run(controller)
213+
output = optimizer.run(controller=controller)
214214

215215
# Check results (low accuracy)
216216
self.assertEqual(output.p_space.dim, 2)
@@ -246,7 +246,7 @@ def test_plots(self):
246246
controller = BayesianOptimizerController.from_input(
247247
self.input, max_iterations=1
248248
)
249-
output = optimizer.run(controller)
249+
output = optimizer.run(controller=controller)
250250

251251
output_eval = OutputEvaluator(self.input, output)
252252
output_eval.impf_set.plot()

climada/util/calibrate/bayesian_optimizer.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -622,7 +622,7 @@ def _target_func(
622622
"""Invert the cost function because BayesianOptimization maximizes the target"""
623623
return -self.input.cost_func(data, predicted, weights)
624624

625-
def run(self, controller: BayesianOptimizerController) -> BayesianOptimizerOutput:
625+
def run(self, **opt_kwargs) -> BayesianOptimizerOutput:
626626
"""Execute the optimization
627627
628628
``BayesianOptimization`` *maximizes* a target function. Therefore, this class
@@ -633,15 +633,25 @@ def run(self, controller: BayesianOptimizerController) -> BayesianOptimizerOutpu
633633
----------
634634
controller : BayesianOptimizerController
635635
The controller instance used to set the optimization iteration parameters.
636-
opt_kwargs
637-
Further keyword arguments passed to ``BayesianOptimization.maximize``.
636+
kwargs
637+
Further keyword arguments passed to ``BayesianOptimization.maximize``. Note
638+
that some arguments are also provided by
639+
:py:meth:`BayesianOptimizerController.optimizer_params`.
638640
639641
Returns
640642
-------
641643
output : BayesianOptimizerOutput
642644
Optimization output. :py:attr:`BayesianOptimizerOutput.p_space` stores data
643645
on the sampled parameter space.
644646
"""
647+
# Take the controller
648+
try:
649+
controller = opt_kwargs.pop("controller")
650+
except KeyError as err:
651+
raise RuntimeError(
652+
"BayesianOptimizer.run requires 'controller' as keyword argument"
653+
) from err
654+
645655
# Register the controller
646656
for event in (Events.OPTIMIZATION_STEP, Events.OPTIMIZATION_END):
647657
self.optimizer.subscribe(event, controller)
@@ -662,7 +672,7 @@ def run(self, controller: BayesianOptimizerController) -> BayesianOptimizerOutpu
662672
while controller.iterations < controller.max_iterations:
663673
try:
664674
LOGGER.info(f"Optimization iteration: {controller.iterations}")
665-
self.optimizer.maximize(**controller.optimizer_params())
675+
self.optimizer.maximize(**controller.optimizer_params(), **opt_kwargs)
666676
except StopEarly:
667677
# Start a new iteration
668678
continue

climada/util/calibrate/test/test_bayesian_optimizer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ def test_kwargs_to_impact_func_creator(self, _):
317317
# Call 'run'
318318
with patch.object(self.input, "impact_to_aligned_df") as align:
319319
align.return_value = (None, None)
320-
self.optimizer.run(self.controller)
320+
self.optimizer.run(controller=self.controller)
321321

322322
# Check call to '_kwargs_to_impact_func_gen'
323323
call_args = self.input.impact_func_creator.call_args_list
@@ -341,7 +341,7 @@ def test_target_func(self, _):
341341
# Call 'run'
342342
with patch.object(self.input, "impact_to_aligned_df") as align:
343343
align.return_value = (None, None)
344-
output = self.optimizer.run(self.controller)
344+
output = self.optimizer.run(controller=self.controller)
345345

346346
# Check target space
347347
npt.assert_array_equal(output.p_space.target, [-1.0, 1.0])

doc/user-guide/climada_util_calibrate.ipynb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
},
6161
{
6262
"cell_type": "code",
63-
"execution_count": 2,
63+
"execution_count": null,
6464
"metadata": {},
6565
"outputs": [
6666
{
@@ -144,7 +144,7 @@
144144
"\n",
145145
"# Run optimization\n",
146146
"with log_level(\"WARNING\", \"climada.engine.impact_calc\"):\n",
147-
" output = opt.run(controller)\n",
147+
" output = opt.run(controller=controller)\n",
148148
"\n",
149149
"# Analyse results\n",
150150
"output.plot_p_space()\n",
@@ -2192,7 +2192,7 @@
21922192
},
21932193
{
21942194
"cell_type": "code",
2195-
"execution_count": 13,
2195+
"execution_count": null,
21962196
"metadata": {},
21972197
"outputs": [
21982198
{
@@ -2231,7 +2231,7 @@
22312231
" # Create and run the optimizer\n",
22322232
" opt = BayesianOptimizer(input)\n",
22332233
" controller = BayesianOptimizerController.from_input(input)\n",
2234-
" bayes_output = opt.run(controller)\n",
2234+
" bayes_output = opt.run(controller=controller)\n",
22352235
" bayes_output.params # The optimal parameters"
22362236
]
22372237
},
@@ -3718,7 +3718,7 @@
37183718
},
37193719
{
37203720
"cell_type": "code",
3721-
"execution_count": 27,
3721+
"execution_count": null,
37223722
"metadata": {},
37233723
"outputs": [],
37243724
"source": [
@@ -3750,7 +3750,7 @@
37503750
" with log_level(\"INFO\", name_prefix=\"climada.util.calibrate\"):\n",
37513751
" opt = BayesianOptimizer(input)\n",
37523752
" controller = BayesianOptimizerController.from_input(input)\n",
3753-
" bayes_output = opt.run(controller)\n",
3753+
" bayes_output = opt.run(controller=controller)\n",
37543754
"\n",
37553755
" # Evaluate output\n",
37563756
" output_eval = OutputEvaluator(input, bayes_output)\n",

0 commit comments

Comments
 (0)