-
Notifications
You must be signed in to change notification settings - Fork 0
/
preprocess.py
executable file
·223 lines (199 loc) · 7.45 KB
/
preprocess.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
#!/usr/bin/env python3
import argparse
import csv
import subprocess
import glob
import json
import logging
import os.path
from datetime import datetime
import config
import subprocess
logger = logging.getLogger("preprocess")
def get_submissions_data(dir: str):
submissions = {}
for path in glob.glob(os.path.join(dir, "*")):
id = os.path.basename(path)[: -len(".json")]
with open(path, "r") as f:
data = json.load(f)
submissions[id] = data
return submissions
def get_subchanges_data(dir: str):
subchanges = []
for path in glob.glob(os.path.join(dir, "*")):
with open(path, "r") as f:
data = json.load(f)
subchanges.append(data)
return subchanges
def get_terry_data(dir: str):
submissions = {}
subchanges = []
for path in glob.glob(os.path.join(dir, "*", "*", "*", "info.txt")):
with open(path, "r") as f:
date, score = f.readlines()
date = int(datetime.strptime(date[6:-1], "%Y-%m-%d %H:%M:%S").timestamp())
score = int(float(score[7:-1]))
p = os.path.dirname(path)
p, sub = os.path.split(p)
p, user = os.path.split(p)
task = os.path.basename(p)
sub = path
submissions[sub] = {
"user" : user,
"task" : task,
"time" : date,
}
subchanges.append({
"submission" : sub,
"time" : date,
"score" : score,
"extra" : [score],
})
qmsfile = os.path.join(dir, "submissions.json")
if os.path.exists(qmsfile):
with open("ranking.csv") as f:
usernames = {row['name']: row['username'] for row in csv.DictReader(f)}
with open(qmsfile, "r") as f:
for data in json.load(f).values():
user = data['name'].title() + ' ' + data['surname'].title()
if user not in usernames:
user = data['surname'].title() + ' ' + data['name'].title()
if user not in usernames:
continue
user = usernames[user]
for i, s in enumerate(data['submissions']):
sub = data['uid'] + '-' + str(i)
date = s['timestamp']
date = int(datetime.strptime(date[:-5], "%Y-%m-%dT%H:%M:%S").timestamp()) + 7200
score = s['score']
submissions[sub] = {
"user" : user,
"task" : "quizms",
"time" : date,
}
subchanges.append({
"submission" : sub,
"time" : date,
"score" : score,
"extra" : [score],
})
return submissions, subchanges
def fake_screenshots(dir: str, usernames, start, end):
screens = glob.glob(os.path.join(dir, "20*00.0.png"))
if len(screens) == 0:
subprocess.run(
["asy", "clock.asy", "-f", "png", "-globalwrite", "-antialias", "4"],
input=bytes(config.CONTEST_START + " " + config.CONTEST_END, "utf-8")
)
screens = glob.glob(os.path.join(dir, "20*00.0.png"))
for user in usernames:
if not os.path.exists(os.path.join(dir, user)):
os.makedirs(os.path.join(dir, user))
for path in screens:
file = os.path.basename(path)
if not os.path.exists(os.path.join(dir, user, file)):
os.symlink(os.path.join("..", file), os.path.join(dir, user, file))
def main(args):
if not os.path.exists(args.ranking_dir):
raise RuntimeError("Missing ranking directory")
os.makedirs(args.output_dir, exist_ok=True)
if args.terry:
submissions, subchanges = get_terry_data(args.ranking_dir)
else:
submissions = get_submissions_data(os.path.join(args.ranking_dir, "submissions"))
subchanges = get_subchanges_data(os.path.join(args.ranking_dir, "subchanges"))
subchanges.sort(key=lambda x: x["time"])
logger.info("%s submissions", len(submissions))
logger.info("%s subchanges", len(subchanges))
user_task_score = {}
user_score_history = {}
for subch in subchanges:
sub = submissions[subch["submission"]]
username = sub["user"]
task = sub["task"]
subtasks = list(map(float, subch["extra"]))
time = subch["time"]
user = user_task_score.setdefault(username, {})
if task not in user:
user[task] = subtasks
else:
for i, score in enumerate(subtasks):
user[task][i] = max(score, user[task][i])
history = user_score_history.setdefault(username, [])
score = sum(sum(subtasks) for subtasks in user.values())
history.append(dict(time=time, score=score))
logger.info("%s users have submitted", len(user_task_score))
with open(os.path.join(args.output_dir, "final_scores.json"), "w") as f:
json.dump(user_task_score, f, indent=4)
with open(os.path.join(args.output_dir, "history.json"), "w") as f:
json.dump(user_score_history, f, indent=4)
with open(args.ranking_csv, "r") as f:
reader = csv.DictReader(f)
ranking = list(reader)
for user in ranking:
user["po"] = "po" in user and user["po"] != "" and user["po"] != "F"
user["position"] = int(user["position"])
user["class"] = int(user["class"])
with open(os.path.join(args.output_dir, "ranking.json"), "w") as f:
json.dump(ranking, f, indent=4)
if os.path.exists("./screenshots/background.png") and os.path.exists("./screenshots/filler.png"):
fake_screenshots("./screenshots", [user["username"] for user in ranking if user["medal"]], config.CONTEST_START, config.CONTEST_END)
logger.info("Resizing faces...")
os.makedirs(os.path.join(args.output_dir, "faces"), exist_ok=True)
for path in glob.glob(os.path.join(args.faces_dir, "*")):
name = os.path.basename(path)
name, _ = os.path.splitext(name)
target = os.path.join(args.output_dir, "faces", name + ".jpg")
if os.path.exists(target):
continue
subprocess.check_call(
[
"convert",
path,
"-resize",
"1000x1000^",
"-gravity",
"Center",
"-extent",
"1000x1000",
target,
]
)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"--faces-dir",
help="directory with the faces",
default="./faces/",
)
parser.add_argument(
"--ranking-dir",
help="directory with the ranking",
default="./ranking/",
)
parser.add_argument(
"--ranking-csv",
help="path to ranking.csv",
default="./ranking.csv",
)
parser.add_argument(
"--output-dir",
help="directory to write the output",
default="./output/",
)
parser.add_argument(
"--terry", "-t",
help="import data in terry format",
action="store_true",
)
parser.add_argument(
"--verbose", "-v",
help="enable verbose output",
action="store_true",
)
args = parser.parse_args()
logging.basicConfig(
level=logging.DEBUG if args.verbose else logging.INFO,
format="%(asctime)s [%(levelname)s] [%(name)s] %(message)s",
)
main(args)