-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcreate_leaderboards.py
123 lines (100 loc) · 4.17 KB
/
create_leaderboards.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import json
import os
from pprint import pprint
THIS_PATH = os.path.dirname(os.path.realpath(__file__))
OPTIMIZER_ELO_PATH = THIS_PATH + os.path.sep + 'results'
def load_all_games():
def load_json(file):
with open(os.path.join(OPTIMIZER_ELO_PATH, file), "r") as f:
ret = json.load(f)
return ret
games = list()
i = 0
leaderboard_jsons = dict(
(file, load_json(file)) for file in os.listdir(OPTIMIZER_ELO_PATH) if os.path.isfile(
os.path.join(OPTIMIZER_ELO_PATH, file)
) and os.path.join(OPTIMIZER_ELO_PATH, file).endswith(".json")
)
return leaderboard_jsons # "name" and "rating" keys are important
def get_html_table_rows(data):
data_dict = {}
for name, rating, count, active in zip(data["name"], data["rating"], data["count"], data["active"]):
data_dict[name] = (rating,count, active)
data_dict = dict(sorted(data_dict.items(), key=lambda item: item[1], reverse=True))
html = "<tr><th>Name</th><th>Rating</th><th>Games</th><th>Active</th> </tr>"
for name, (rating,count,active) in data_dict.items():
active_str = 'yes' if active else 'no'
html += f"<tr><td>{name.replace('_cube','')}</td><td>{round(rating, 0)}</td><td>{round(count, 0)}</td><td>{active_str}</td></tr>"
return html
# For overall.json
def get_overall_html_str(file, data, navbar):
return f"""<html>
<head><link rel="stylesheet" href="../style.css" type="text/css"></head>
<body>
<div class="left">{navbar}</div>
<div class="right">
<h1>Overall Optimizer Elo Leaderboard</h1>
Produced by <a href="https://github.com/microprediction/optimizer-elo-ratings">optimizer-elo-ratings</a> using
the <a href="https://github.com/microprediction/humpday">humpday</a> package. See the <a href="https://www.microprediction.com/blog/humpday">article</a>.<br>
<table class="default-table">
{get_html_table_rows(data)}
</table>
</div>
</body>
</html>"""
# For all the other jsons
def get_html_str(file, data, navbar):
parse_file = file.replace(".json", "")
args = parse_file.split("_")
return f"""<html>
<head><link rel="stylesheet" href="../style.css" type="text/css"></head>
<body>
<div class="left">{navbar}</div>
<div class="right">
<h1>Optimizer Elo Ratings</h1>
Produced by <a href="https://github.com/microprediction/optimizer-elo-ratings">optimizer-elo-ratings</a> using
the <a href="https://github.com/microprediction/humpday">humpday</a> package. See the <a href="https://www.microprediction.com/blog/humpday">article</a>.
<h3>Class of objective functions: {args[0]}</h3>
<h3>Dimension {args[1][1:]}</h3>
<h3>Function evaluation limit: {args[2][1:]}</h3>
<table class="default-table">
{get_html_table_rows(data)}
</table>
</div>
</body>
</html>"""
def get_html_navbar(json_names):
div = "<h3>Leaderboards</h3>"
for name in sorted(json_names):
name_html = name.replace(".json", ".html")
div += f"<a href='{name_html}'>{name.replace('.json','')}</a>"
return div
# For index.html
def get_index_html_str(json_names):
navbar = "<h3>Leaderboards</h3>"
for name in sorted(json_names):
name_html = name.replace(".json", ".html")
navbar += f"<a href='html_leaderboards/{name_html}'>{name}</a>"
return f"""<html>
<head><link rel="stylesheet" href="style.css" type="text/css"></head>
<body>
<div class="left">
{navbar}
</div>
</body>
</html>"""
if __name__ == '__main__':
jsons = load_all_games()
HTML_DIR = os.path.join(THIS_PATH, "docs", "html_leaderboards")
if not os.path.exists(HTML_DIR):
os.mkdir(HTML_DIR)
with open(os.path.join(THIS_PATH, "docs", "index.html"), "w") as f:
f.write(get_index_html_str(jsons.keys()))
navbar = get_html_navbar(jsons.keys())
for file, data in jsons.items():
file_html = file.replace(".json", ".html")
with open(os.path.join(HTML_DIR, file_html), "w") as f:
if file == "overall.json":
f.write(get_overall_html_str(file, data, navbar))
else:
f.write(get_html_str(file, data, navbar))