-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrankings.py
executable file
·290 lines (234 loc) · 9.35 KB
/
rankings.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#!/usr/bin/env python
from app import app, contest
import argparse
from datetime import datetime
import os
import math
import numpy as np
import pandas as pd
import requests
NUM_DIGITS = 2
FLOAT_FORMAT = '%.{}f'.format(NUM_DIGITS)
TEAM_NAME_MAX_LEN = 120
BALANCES_URL = app.config['BALANCES_URL'] or "http://localhost:5000/lambda/getbalances"
TEAMS_URL = app.config['TEAMS_URL'] or "http://localhost:5000/teams"
def get_team_info(root, f=contest.TEAM_ID_FILE):
info = 0
with open(os.path.join(root, f), 'r') as f:
info = f.readline().rstrip('\n')
return info
def get_latest_submissions(grade_dir, time):
subs = []
for root, dirs, files in os.walk(grade_dir):
# Descend into per-team directories
if root != grade_dir:
# Get only last submission
dirs = list(filter(lambda d: d <= time, dirs))
path = None
ts = None
if len(dirs) > 0:
dirs = [sorted(dirs)[-1]]
ts = dirs[0]
path = os.path.join(root, ts)
if ts is not None:
t_id = get_team_info(root, contest.TEAM_ID_FILE)
t_name = get_team_info(root, contest.TEAM_NAME_FILE)
subs.append({'id':t_id, 'name':t_name, 'time':ts, 'path':path})
return subs
def get_raw_ranking(grade_dir, time):
latest_subs = get_latest_submissions(grade_dir, time)
# num_problems = max problems in _any_ submission
# Doing it this way so this script doesn't need to know about different
# stages in the contest.
num_problems = 0
raw_subs = []
for sub in latest_subs:
path = os.path.join(sub['path'], contest.SCORE_FILE)
s = pd.read_csv(path, header=None, names=['prob', 'score', 'msg'])
s.drop(columns='msg', inplace=True)
s.set_index('prob', inplace=True)
num_problems = max(num_problems, len(s))
raw_subs.append(s.T)
ids = pd.DataFrame(latest_subs)
scores = pd.concat(raw_subs) if len(raw_subs) > 0 else pd.DataFrame()
scores.reset_index(inplace=True, drop=True)
# Automatically handles "expanding" subs to num_problems (for submissions
# that came before a stage change).
ranking = pd.concat([ids, scores], axis=1)
ranking.fillna(0, inplace=True, downcast='infer')
return num_problems, ranking
def compute_score(row, best, multiplier):
total = 0
for prob, score in row.iteritems():
if score > 0:
total += math.ceil((best[prob] / score) * 1000 * multiplier[prob])
return total
def compute_unspent(row, balances):
t_id = row['id']
sp = os.path.join(row['path'], contest.SPENT_LAM_FILE)
total = balances.get(t_id, 0)
spent = 0
# Get value in spent.txt
try:
with open(sp, 'r') as f:
spent = int(f.read())
except:
pass
unspent = total - spent
return unspent, total
def get_ranking(num_problems, raw_ranking, multiplier, consider_coins):
best = {}
ranking = raw_ranking.copy()
probs = list(range(1, num_problems + 1))
for p in probs:
nonzero = raw_ranking[raw_ranking[p] > 0][p]
best[p] = nonzero.min()
ranking.drop(columns=p, inplace=True)
scores = []
raw_ranking = raw_ranking[probs]
for row_id in range(len(ranking)):
scores.append(compute_score(raw_ranking.loc[row_id], best, multiplier))
# Collect teams with no submission
response = requests.get(TEAMS_URL, allow_redirects=True)
teams = response.json()
to_add = []
for t_id, t_name in teams.items():
exists = (len(ranking) > 0) and (ranking['id'] == t_id).any()
if not exists:
to_add.append({'id': t_id, 'name': t_name, 'path': '', 'time': ''})
scores.append(0)
# Insert teams with no submission into ranking
to_add = pd.DataFrame(to_add)
ranking = ranking.append(to_add, ignore_index=True)
# If len(ranking) == 0, just break; no one has registered yet
if len(ranking) == 0:
return None, None
scores = pd.DataFrame(scores)
scores.columns = ['score']
hodl = None
if consider_coins:
# Get LAM balances for all teams
response = requests.get(BALANCES_URL, allow_redirects=True)
balances = response.json()
unspent_coins = []
total_coins = []
for row_id in range(len(ranking)):
u, t = compute_unspent(ranking.loc[row_id], balances)
unspent_coins.append(u)
total_coins.append(t)
unspent = pd.DataFrame(unspent_coins)
unspent.columns = ['unspent LAM']
hodl = ranking.copy()
hodl = pd.concat([hodl, unspent], axis=1)
hodl.sort_values('unspent LAM', ascending=False, inplace=True)
ranking = pd.concat([ranking, scores, unspent], axis=1)
ranking['score + unspent LAM'] = ranking['score'] + ranking['unspent LAM']
ranking.drop(columns=['unspent LAM'], inplace=True)
ranking.sort_values('score + unspent LAM', ascending=False, inplace=True)
else:
ranking = pd.concat([ranking, scores], axis=1)
ranking.sort_values('score', ascending=False, inplace=True)
return ranking, hodl
def parse_sizes_file(path):
mult = {}
x = pd.read_csv(path, header=None)
for row_id in range(len(x)):
row = x.loc[row_id]
# Start at 1
mult[row_id + 1] = math.log2(row[1] * row[2])
return mult
def write_latest(ranking, args):
# Write latest.html
html_latest = os.path.join(args.output_folder, 'latest.html')
wrapper = """<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Live Rankings</title>
<link rel="stylesheet" href="https://icfpcontest2019.github.io/assets/main.css">
</head>
<center>
<h1>Live Rankings</h1>
<pre>Last updated: {}</pre>
{}
</center>
</html>"""
time = datetime.strptime(args.t, contest.ZIP_TIME_FORMAT).strftime("%c")
pd.option_context('display.max_colwidth', TEAM_NAME_MAX_LEN)
table = ranking.to_html(float_format=FLOAT_FORMAT, justify='center')
page=wrapper.format(time, table)
with open(html_latest, 'w') as f:
f.write(page)
def write_hodl(hodl, args):
# Write hodl.html
hodl_latest = os.path.join(args.output_folder, 'hodl.html')
wrapper = """<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Biggest HODLers</title>
<link rel="stylesheet" href="https://icfpcontest2019.github.io/assets/main.css">
</head>
<center>
<h1>Biggest HODLers</h1>
<h2>(Teams with most unspent LAM)</h2>
<pre>Last updated: {}</pre>
{}
</center>
</html>"""
time = datetime.strptime(args.t, contest.ZIP_TIME_FORMAT).strftime("%c")
pd.option_context('display.max_colwidth', TEAM_NAME_MAX_LEN)
table = hodl.to_html(float_format=FLOAT_FORMAT, justify='center')
page=wrapper.format(time, table)
with open(hodl_latest, 'w') as f:
f.write(page)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Compute rankings.')
start = datetime.utcnow()
default_time = start.strftime(contest.ZIP_TIME_FORMAT)
parser.add_argument('-t', metavar='TIME', default=default_time, help='latest submission time to consider')
parser.add_argument('-p', metavar='PATH', required=True, help='problems folder')
parser.add_argument('-g', metavar='PATH', required=True, help='grades folder')
parser.add_argument('--output-folder', metavar='PATH', required=True, help='output path to save CSV and HTML files')
parser.add_argument('--coins', action="store_true", help='take LAM balance into consideration?')
args = parser.parse_args()
# XXX: stages; args.coins
args.coins = args.coins or contest.rankings_coins()
# Write empty hodl.html (to handle stage transition when rankings frozen)
if args.coins:
hodl_path = os.path.join(args.output_folder, 'hodl.html')
# ... only if file doesn't exist already
if not os.path.exists(hodl_path):
write_hodl(pd.DataFrame(), args)
if contest.rankings_frozen():
print("[{}] Rankings frozen.".format(default_time))
exit(0)
# Parse multiplier
mutliplier = parse_sizes_file(os.path.join(args.p, contest.SIZES_FILE))
num_probs, raw = get_raw_ranking(args.g, args.t)
ranking, hodl = get_ranking(num_probs, raw, mutliplier, args.coins)
# Remove seconds and microseconds from filename
filename = start.strftime(contest.ZIP_TIME_MINUTE)
csv_output = os.path.join(args.output_folder, filename + '.csv')
html_output = os.path.join(args.output_folder, filename + '.html')
# Create folder if it doesn't exist
os.makedirs(args.output_folder, exist_ok=True)
# Reset indices and make something up if nothing exists yet
if ranking is not None:
ranking.reset_index(inplace=True)
ranking.index += 1
ranking.drop(columns=['index', 'id', 'time', 'path'], inplace=True)
else:
ranking = pd.DataFrame()
if hodl is not None:
hodl.reset_index(inplace=True)
hodl.index += 1
hodl.drop(columns=['index', 'id', 'time', 'path'], inplace=True)
else:
hodl = pd.DataFrame()
## Write the files!
ranking.to_csv(csv_output, float_format=FLOAT_FORMAT, index=True, encoding='utf-8')
ranking.to_html(html_output, float_format=FLOAT_FORMAT, justify='center')
write_latest(ranking, args)
if args.coins:
write_hodl(hodl, args)