Skip to content

Commit 1feb597

Browse files
committed
fixes #325
1 parent 2c1308a commit 1feb597

8 files changed

+58
-48
lines changed

bayes_opt/logger.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
from __future__ import print_function
22
import os
33
import json
4-
4+
import warnings
5+
from pathlib import Path
56
from .observer import _Tracker
67
from .event import Events
78
from .util import Colours
@@ -135,7 +136,10 @@ def update(self, event, instance):
135136

136137
class JSONLogger(_Tracker):
137138
def __init__(self, path, reset=True):
138-
self._path = path if path[-5:] == ".json" else path + ".json"
139+
140+
if not (Path(path).suffix) == '.log':
141+
warnings.warn('overwriting log extension with *.log"')
142+
self._path = str(Path(path).with_suffix('.log'))
139143
if reset:
140144
try:
141145
os.remove(self._path)

bayes_opt/util.py

+4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from scipy.stats import norm
44
from scipy.optimize import minimize
55
from colorama import just_fix_windows_console
6+
from pathlib import Path
67

78

89
def acq_max(ac, gp, y_max, bounds, random_state, constraint=None, n_warmup=10000, n_iter=10):
@@ -209,6 +210,9 @@ def load_logs(optimizer, logs):
209210
logs = [logs]
210211

211212
for log in logs:
213+
if not (Path(log).suffix) == '.log':
214+
warnings.warn('overwriting log extension with *.log"')
215+
log = str(Path(log).with_suffix('.log'))
212216
with open(log, "r") as j:
213217
while True:
214218
try:

examples/basic-tour.ipynb

+36-40
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
]
1717
},
1818
{
19-
"attachments": {},
2019
"cell_type": "markdown",
2120
"metadata": {},
2221
"source": [
@@ -44,7 +43,6 @@
4443
]
4544
},
4645
{
47-
"attachments": {},
4846
"cell_type": "markdown",
4947
"metadata": {},
5048
"source": [
@@ -108,11 +106,11 @@
108106
"text": [
109107
"| iter | target | x | y |\n",
110108
"-------------------------------------------------\n",
111-
"| \u001b[0m 1 \u001b[0m | \u001b[0m-7.135 \u001b[0m | \u001b[0m 2.834 \u001b[0m | \u001b[0m 1.322 \u001b[0m |\n",
112-
"| \u001b[0m 2 \u001b[0m | \u001b[0m-7.78 \u001b[0m | \u001b[0m 2.0 \u001b[0m | \u001b[0m-1.186 \u001b[0m |\n",
113-
"| \u001b[0m 3 \u001b[0m | \u001b[0m-19.0 \u001b[0m | \u001b[0m 4.0 \u001b[0m | \u001b[0m 3.0 \u001b[0m |\n",
114-
"| \u001b[0m 4 \u001b[0m | \u001b[0m-16.3 \u001b[0m | \u001b[0m 2.378 \u001b[0m | \u001b[0m-2.413 \u001b[0m |\n",
115-
"| \u001b[95m 5 \u001b[0m | \u001b[95m-4.441 \u001b[0m | \u001b[95m 2.105 \u001b[0m | \u001b[95m-0.005822\u001b[0m |\n",
109+
"| \u001b[0m1 \u001b[0m | \u001b[0m-7.135 \u001b[0m | \u001b[0m2.834 \u001b[0m | \u001b[0m1.322 \u001b[0m |\n",
110+
"| \u001b[0m2 \u001b[0m | \u001b[0m-7.78 \u001b[0m | \u001b[0m2.0 \u001b[0m | \u001b[0m-1.186 \u001b[0m |\n",
111+
"| \u001b[95m3 \u001b[0m | \u001b[95m-7.11 \u001b[0m | \u001b[95m2.218 \u001b[0m | \u001b[95m-0.7867 \u001b[0m |\n",
112+
"| \u001b[0m4 \u001b[0m | \u001b[0m-12.4 \u001b[0m | \u001b[0m3.66 \u001b[0m | \u001b[0m0.9608 \u001b[0m |\n",
113+
"| \u001b[95m5 \u001b[0m | \u001b[95m-6.999 \u001b[0m | \u001b[95m2.23 \u001b[0m | \u001b[95m-0.7392 \u001b[0m |\n",
116114
"=================================================\n"
117115
]
118116
}
@@ -140,7 +138,7 @@
140138
"name": "stdout",
141139
"output_type": "stream",
142140
"text": [
143-
"{'target': -4.441293113411222, 'params': {'y': -0.005822117636089974, 'x': 2.104665051994087}}\n"
141+
"{'target': -6.999472814518675, 'params': {'x': 2.2303920156083024, 'y': -0.7392021938893159}}\n"
144142
]
145143
}
146144
],
@@ -165,15 +163,15 @@
165163
"output_type": "stream",
166164
"text": [
167165
"Iteration 0: \n",
168-
"\t{'target': -7.135455292718879, 'params': {'y': 1.3219469606529488, 'x': 2.8340440094051482}}\n",
166+
"\t{'target': -7.135455292718879, 'params': {'x': 2.8340440094051482, 'y': 1.3219469606529488}}\n",
169167
"Iteration 1: \n",
170-
"\t{'target': -7.779531005607566, 'params': {'y': -1.1860045642089614, 'x': 2.0002287496346898}}\n",
168+
"\t{'target': -7.779531005607566, 'params': {'x': 2.0002287496346898, 'y': -1.1860045642089614}}\n",
171169
"Iteration 2: \n",
172-
"\t{'target': -19.0, 'params': {'y': 3.0, 'x': 4.0}}\n",
170+
"\t{'target': -7.109925819441113, 'params': {'x': 2.2175526295255183, 'y': -0.7867249801593896}}\n",
173171
"Iteration 3: \n",
174-
"\t{'target': -16.29839645063864, 'params': {'y': -2.412527795983739, 'x': 2.3776144540856503}}\n",
172+
"\t{'target': -12.397162416009818, 'params': {'x': 3.660003815774634, 'y': 0.9608275029525108}}\n",
175173
"Iteration 4: \n",
176-
"\t{'target': -4.441293113411222, 'params': {'y': -0.005822117636089974, 'x': 2.104665051994087}}\n"
174+
"\t{'target': -6.999472814518675, 'params': {'x': 2.2303920156083024, 'y': -0.7392021938893159}}\n"
177175
]
178176
}
179177
],
@@ -211,11 +209,11 @@
211209
"text": [
212210
"| iter | target | x | y |\n",
213211
"-------------------------------------------------\n",
214-
"| \u001b[0m 6 \u001b[0m | \u001b[0m-5.145 \u001b[0m | \u001b[0m 2.115 \u001b[0m | \u001b[0m-0.2924 \u001b[0m |\n",
215-
"| \u001b[0m 7 \u001b[0m | \u001b[0m-5.379 \u001b[0m | \u001b[0m 2.337 \u001b[0m | \u001b[0m 0.04124 \u001b[0m |\n",
216-
"| \u001b[95m 8 \u001b[0m | \u001b[95m-3.581 \u001b[0m | \u001b[95m 1.874 \u001b[0m | \u001b[95m-0.03428 \u001b[0m |\n",
217-
"| \u001b[95m 9 \u001b[0m | \u001b[95m-2.624 \u001b[0m | \u001b[95m 1.702 \u001b[0m | \u001b[95m 0.1472 \u001b[0m |\n",
218-
"| \u001b[95m 10 \u001b[0m | \u001b[95m-1.762 \u001b[0m | \u001b[95m 1.442 \u001b[0m | \u001b[95m 0.1735 \u001b[0m |\n",
212+
"| \u001b[95m6 \u001b[0m | \u001b[95m-2.942 \u001b[0m | \u001b[95m1.98 \u001b[0m | \u001b[95m0.8567 \u001b[0m |\n",
213+
"| \u001b[95m7 \u001b[0m | \u001b[95m-0.4597 \u001b[0m | \u001b[95m1.096 \u001b[0m | \u001b[95m1.508 \u001b[0m |\n",
214+
"| \u001b[95m8 \u001b[0m | \u001b[95m0.5304 \u001b[0m | \u001b[95m-0.6807 \u001b[0m | \u001b[95m1.079 \u001b[0m |\n",
215+
"| \u001b[0m9 \u001b[0m | \u001b[0m-5.33 \u001b[0m | \u001b[0m-1.526 \u001b[0m | \u001b[0m3.0 \u001b[0m |\n",
216+
"| \u001b[0m10 \u001b[0m | \u001b[0m-5.419 \u001b[0m | \u001b[0m-2.0 \u001b[0m | \u001b[0m-0.5552 \u001b[0m |\n",
219217
"=================================================\n"
220218
]
221219
}
@@ -297,8 +295,8 @@
297295
"text": [
298296
"| iter | target | x | y |\n",
299297
"-------------------------------------------------\n",
300-
"| \u001b[95m 11 \u001b[0m | \u001b[95m 0.66 \u001b[0m | \u001b[95m 0.5 \u001b[0m | \u001b[95m 0.7 \u001b[0m |\n",
301-
"| \u001b[0m 12 \u001b[0m | \u001b[0m 0.1 \u001b[0m | \u001b[0m-0.3 \u001b[0m | \u001b[0m 0.1 \u001b[0m |\n",
298+
"| \u001b[95m11 \u001b[0m | \u001b[95m0.66 \u001b[0m | \u001b[95m0.5 \u001b[0m | \u001b[95m0.7 \u001b[0m |\n",
299+
"| \u001b[0m12 \u001b[0m | \u001b[0m0.1 \u001b[0m | \u001b[0m-0.3 \u001b[0m | \u001b[0m0.1 \u001b[0m |\n",
302300
"=================================================\n"
303301
]
304302
}
@@ -308,7 +306,6 @@
308306
]
309307
},
310308
{
311-
"attachments": {},
312309
"cell_type": "markdown",
313310
"metadata": {},
314311
"source": [
@@ -330,7 +327,6 @@
330327
]
331328
},
332329
{
333-
"attachments": {},
334330
"cell_type": "markdown",
335331
"metadata": {},
336332
"source": [
@@ -349,7 +345,7 @@
349345
"metadata": {},
350346
"outputs": [],
351347
"source": [
352-
"logger = JSONLogger(path=\"./logs.json\")\n",
348+
"logger = JSONLogger(path=\"./logs.log\")\n",
353349
"optimizer.subscribe(Events.OPTIMIZATION_STEP, logger)"
354350
]
355351
},
@@ -364,11 +360,11 @@
364360
"text": [
365361
"| iter | target | x | y |\n",
366362
"-------------------------------------------------\n",
367-
"| \u001b[0m 13 \u001b[0m | \u001b[0m-12.48 \u001b[0m | \u001b[0m-1.266 \u001b[0m | \u001b[0m-2.446 \u001b[0m |\n",
368-
"| \u001b[0m 14 \u001b[0m | \u001b[0m-3.854 \u001b[0m | \u001b[0m-1.069 \u001b[0m | \u001b[0m-0.9266 \u001b[0m |\n",
369-
"| \u001b[0m 15 \u001b[0m | \u001b[0m 0.3932 \u001b[0m | \u001b[0m 0.3099 \u001b[0m | \u001b[0m 0.2853 \u001b[0m |\n",
370-
"| \u001b[95m 16 \u001b[0m | \u001b[95m 0.8768 \u001b[0m | \u001b[95m 0.02197 \u001b[0m | \u001b[95m 0.6497 \u001b[0m |\n",
371-
"| \u001b[95m 17 \u001b[0m | \u001b[95m 0.9446 \u001b[0m | \u001b[95m 0.198 \u001b[0m | \u001b[95m 0.8727 \u001b[0m |\n",
363+
"| \u001b[0m13 \u001b[0m | \u001b[0m-12.48 \u001b[0m | \u001b[0m-1.266 \u001b[0m | \u001b[0m-2.446 \u001b[0m |\n",
364+
"| \u001b[0m14 \u001b[0m | \u001b[0m-3.854 \u001b[0m | \u001b[0m-1.069 \u001b[0m | \u001b[0m-0.9266 \u001b[0m |\n",
365+
"| \u001b[0m15 \u001b[0m | \u001b[0m-3.594 \u001b[0m | \u001b[0m0.7709 \u001b[0m | \u001b[0m3.0 \u001b[0m |\n",
366+
"| \u001b[95m16 \u001b[0m | \u001b[95m0.8238 \u001b[0m | \u001b[95m0.03434 \u001b[0m | \u001b[95m1.418 \u001b[0m |\n",
367+
"| \u001b[95m17 \u001b[0m | \u001b[95m0.9721 \u001b[0m | \u001b[95m-0.1051 \u001b[0m | \u001b[95m0.87 \u001b[0m |\n",
372368
"=================================================\n"
373369
]
374370
}
@@ -427,7 +423,7 @@
427423
"metadata": {},
428424
"outputs": [],
429425
"source": [
430-
"load_logs(new_optimizer, logs=[\"./logs.json\"]);"
426+
"load_logs(new_optimizer, logs=[\"./logs.log\"]);"
431427
]
432428
},
433429
{
@@ -458,16 +454,16 @@
458454
"text": [
459455
"| iter | target | x | y |\n",
460456
"-------------------------------------------------\n",
461-
"| \u001b[0m 1 \u001b[0m | \u001b[0m 0.6131 \u001b[0m | \u001b[0m 0.5571 \u001b[0m | \u001b[0m 0.7233 \u001b[0m |\n",
462-
"| \u001b[0m 2 \u001b[0m | \u001b[0m 0.8609 \u001b[0m | \u001b[0m-0.3295 \u001b[0m | \u001b[0m 1.175 \u001b[0m |\n",
463-
"| \u001b[0m 3 \u001b[0m | \u001b[0m 0.3761 \u001b[0m | \u001b[0m 0.2406 \u001b[0m | \u001b[0m 1.752 \u001b[0m |\n",
464-
"| \u001b[0m 4 \u001b[0m | \u001b[0m-0.7845 \u001b[0m | \u001b[0m-0.8972 \u001b[0m | \u001b[0m 1.99 \u001b[0m |\n",
465-
"| \u001b[0m 5 \u001b[0m | \u001b[0m 0.1401 \u001b[0m | \u001b[0m-0.8733 \u001b[0m | \u001b[0m 0.6882 \u001b[0m |\n",
466-
"| \u001b[0m 6 \u001b[0m | \u001b[0m-1.798 \u001b[0m | \u001b[0m 1.545 \u001b[0m | \u001b[0m 1.642 \u001b[0m |\n",
467-
"| \u001b[0m 7 \u001b[0m | \u001b[0m 0.9331 \u001b[0m | \u001b[0m 0.05549 \u001b[0m | \u001b[0m 1.253 \u001b[0m |\n",
468-
"| \u001b[0m 8 \u001b[0m | \u001b[0m-5.369 \u001b[0m | \u001b[0m 2.0 \u001b[0m | \u001b[0m-0.5391 \u001b[0m |\n",
469-
"| \u001b[95m 9 \u001b[0m | \u001b[95m 0.97 \u001b[0m | \u001b[95m-0.1587 \u001b[0m | \u001b[95m 0.9305 \u001b[0m |\n",
470-
"| \u001b[0m 10 \u001b[0m | \u001b[0m 0.7718 \u001b[0m | \u001b[0m 0.4293 \u001b[0m | \u001b[0m 1.21 \u001b[0m |\n",
457+
"| \u001b[0m1 \u001b[0m | \u001b[0m-3.548 \u001b[0m | \u001b[0m-2.0 \u001b[0m | \u001b[0m1.74 \u001b[0m |\n",
458+
"| \u001b[0m2 \u001b[0m | \u001b[0m-3.041 \u001b[0m | \u001b[0m1.914 \u001b[0m | \u001b[0m0.3844 \u001b[0m |\n",
459+
"| \u001b[0m3 \u001b[0m | \u001b[0m-12.0 \u001b[0m | \u001b[0m2.0 \u001b[0m | \u001b[0m-2.0 \u001b[0m |\n",
460+
"| \u001b[0m4 \u001b[0m | \u001b[0m-3.969 \u001b[0m | \u001b[0m2.0 \u001b[0m | \u001b[0m1.984 \u001b[0m |\n",
461+
"| \u001b[0m5 \u001b[0m | \u001b[0m-0.7794 \u001b[0m | \u001b[0m-1.238 \u001b[0m | \u001b[0m0.5022 \u001b[0m |\n",
462+
"| \u001b[0m6 \u001b[0m | \u001b[0m0.529 \u001b[0m | \u001b[0m0.685 \u001b[0m | \u001b[0m0.9576 \u001b[0m |\n",
463+
"| \u001b[0m7 \u001b[0m | \u001b[0m0.2987 \u001b[0m | \u001b[0m0.1242 \u001b[0m | \u001b[0m0.1718 \u001b[0m |\n",
464+
"| \u001b[0m8 \u001b[0m | \u001b[0m0.9544 \u001b[0m | \u001b[0m0.2123 \u001b[0m | \u001b[0m0.9766 \u001b[0m |\n",
465+
"| \u001b[0m9 \u001b[0m | \u001b[0m0.7157 \u001b[0m | \u001b[0m-0.437 \u001b[0m | \u001b[0m1.305 \u001b[0m |\n",
466+
"| \u001b[95m10 \u001b[0m | \u001b[95m0.983 \u001b[0m | \u001b[95m-0.06785 \u001b[0m | \u001b[95m1.111 \u001b[0m |\n",
471467
"=================================================\n"
472468
]
473469
}
@@ -505,7 +501,7 @@
505501
"name": "python",
506502
"nbconvert_exporter": "python",
507503
"pygments_lexer": "ipython3",
508-
"version": "3.9.6"
504+
"version": "3.10.6"
509505
}
510506
},
511507
"nbformat": 4,
File renamed without changes.
File renamed without changes.
File renamed without changes.

tests/test_notebooks_run.py

+3
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66
from nbconvert.preprocessors import ExecutePreprocessor
77
from pathlib import Path
88
from glob import glob
9+
from matplotlib import pyplot as plt # this script doesn't use this but the notebooks do
910
this_file_loc = Path(__file__).parent
1011

1112
def check_notebook_runs(notebook_loc):
1213

14+
print(f'running: {notebook_loc}...')
1315
try:
1416
with open(notebook_loc, encoding='utf8') as f:
1517
nb = nbformat.read(f, as_version=4)
@@ -18,6 +20,7 @@ def check_notebook_runs(notebook_loc):
1820
except Exception as e:
1921
print(f'failed to run notebook {notebook_loc}: rethrowing exception:')
2022
raise e
23+
print(f'success!')
2124

2225
def test_all_notebooks_run():
2326
# get all notebooks:

tests/test_util.py

+9-6
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
from sklearn.gaussian_process import GaussianProcessRegressor
1010

1111
from scipy.optimize import NonlinearConstraint
12+
from pathlib import Path
13+
test_dir = Path(__file__).parent.resolve()
14+
1215

1316
def get_globals():
1417
X = np.array([
@@ -133,22 +136,22 @@ def f(x, y):
133136
)
134137
assert len(optimizer.space) == 0
135138

136-
load_logs(optimizer, "./tests/test_logs.json")
139+
load_logs(optimizer, [str(test_dir / "test_logs.log")])
137140
assert len(optimizer.space) == 5
138141

139-
load_logs(optimizer, ["./tests/test_logs.json"])
142+
load_logs(optimizer, [str(test_dir / "test_logs.log")])
140143
assert len(optimizer.space) == 5
141144

142145
other_optimizer = BayesianOptimization(
143146
f=lambda x: -x ** 2,
144147
pbounds={"x": (-2, 2)}
145148
)
146149
with pytest.raises(ValueError):
147-
load_logs(other_optimizer, ["./tests/test_logs.json"])
150+
load_logs(other_optimizer, [str(test_dir / "test_logs.log")])
148151

149152

150153
def test_logs_constraint():
151-
import pytest
154+
152155
def f(x, y):
153156
return -x ** 2 - (y - 1) ** 2 + 1
154157

@@ -164,9 +167,9 @@ def c(x, y):
164167
)
165168

166169
with pytest.raises(KeyError):
167-
load_logs(optimizer, ["./tests/test_logs.json"])
170+
load_logs(optimizer, [str(test_dir / "test_logs.log")])
168171

169-
load_logs(optimizer, ["./tests/test_logs_constrained.json"])
172+
load_logs(optimizer, [str(test_dir / "test_logs_constrained.log")])
170173

171174
assert len(optimizer.space) == 7
172175

0 commit comments

Comments
 (0)