-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathutils.py
59 lines (44 loc) · 1.55 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
"""Json utils to print, save and load training results."""
from bson import json_util
import json
import os
__author__ = "Vooban Inc."
__copyright__ = "Copyright 2017, Vooban Inc."
__license__ = "MIT License"
# See: https://github.com/Vooban/Hyperopt-Keras-CNN-CIFAR-100/blob/master/LICENSE
RESULTS_DIR = "results/"
def print_json(result):
"""Pretty-print a jsonable structure (e.g.: result)."""
print(json.dumps(
result,
default=json_util.default, sort_keys=True,
indent=4, separators=(',', ': ')
))
def save_json_result(model_name, result):
"""Save json to a directory and a filename."""
result_name = '{}.txt.json'.format(model_name)
if not os.path.exists(RESULTS_DIR):
os.makedirs(RESULTS_DIR)
with open(os.path.join(RESULTS_DIR, result_name), 'w') as f:
json.dump(
result, f,
default=json_util.default, sort_keys=True,
indent=4, separators=(',', ': ')
)
def load_json_result(best_result_name):
"""Load json from a path (directory + filename)."""
result_path = os.path.join(RESULTS_DIR, best_result_name)
with open(result_path, 'r') as f:
return json.JSONDecoder().decode(
f.read()
# default=json_util.default,
# separators=(',', ': ')
)
def load_best_hyperspace():
results = [
f for f in list(sorted(os.listdir(RESULTS_DIR))) if 'json' in f
]
if len(results) == 0:
return None
best_result_name = results[-1]
return load_json_result(best_result_name)["space"]