-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scorer.py
370 lines (321 loc) · 12.7 KB
/
scorer.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
#!/usr/bin/env python
import cgi
import io
import re
from urllib.parse import urlparse, unquote
import numpy as np
import pandas as pd
import requests
GSHEET_NETLOC = "docs.google.com"
GSHEET_PATH_PREFIX = "/spreadsheets/d/"
TEAM_COLUMN = "Your Team"
OPPONENT_COLUMN = "Opponent Team"
OPPONENT_SCORE_COLUMNS = [
"Rules Knowledge and Use",
"Fouls and Body Contact",
"Fair-Mindedness",
"Positive Attitude and Self-Control",
"Communication",
]
TEAM_SCORE_COLUMNS = [
"Rules Knowledge and Use (self)",
"Fouls and Body Contact (self)",
"Fair-Mindedness (self)",
"Positive Attitude and Self-Control (self)",
"Communication (self)",
]
DAY_COLUMN = "Day"
TOTAL_SCORE_COLUMN = "Score"
TOTAL_SELF_SCORE_COLUMN = "Self Score"
ALL_COLUMNS = (
[TEAM_COLUMN, OPPONENT_COLUMN, DAY_COLUMN]
+ OPPONENT_SCORE_COLUMNS
+ TEAM_SCORE_COLUMNS
)
def to_numbers(x):
"""Convert an element to a number."""
if isinstance(x, str):
try:
return int(x.split()[0])
except Exception:
return 0
return x
def requires_login(url):
response = requests.get(url)
return response.url.startswith("https://accounts.google.com")
def gsheet_id(url):
parsed = urlparse(url)
if not (
parsed.netloc == GSHEET_NETLOC and parsed.path.startswith(GSHEET_PATH_PREFIX)
):
raise InvalidURLException("Not a google spreadsheet URL")
if requires_login(url):
raise InvalidURLException("Spreadsheet is not accessible without login")
return parsed.path.split("/")[3]
def export_url(sheet_id):
"""Return the export URL using sheet_id."""
base = sheet_url(sheet_id)
return f"{base}/export?format=csv"
def sheet_url(sheet_id):
"""Return the sheet URL using sheet_id."""
return f"https://{GSHEET_NETLOC}{GSHEET_PATH_PREFIX}{sheet_id}"
def get_missing_scores(outer, inner, left_on, right_on):
outer_left = {tuple(s) for s in outer[left_on].fillna("").values if s[0]}
inner_left = {tuple(s) for s in inner[left_on].fillna("").values if s[0]}
missing_left = outer_left - inner_left
outer_right = {tuple(s) for s in outer[right_on].fillna("").values if s[0]}
inner_right = {tuple(s) for s in inner[right_on].fillna("").values if s[0]}
missing_right = outer_right - inner_right
return missing_left, missing_right
class InvalidURLException(Exception):
pass
class SOTGScorer:
"""A class to do all the spirit scoring"""
def __init__(self, sheet_id, columns=None):
self.url = export_url(sheet_id)
self.sheet_url = sheet_url(sheet_id)
self.csv, self.name, self.show_rankings = self.get_csv_and_mode()
self.columns = columns or {}
def get_csv_and_mode(self):
response = requests.get(self.url)
header = response.headers.get("Content-Disposition", "")
_, headers = cgi.parse_header(header)
filename = headers.get("filename*", "")
if not filename:
name = "Spirit Scores"
show_rankings = False
else:
filename = unquote(filename)
show_rankings = "show-rankings" in filename
name = filename
name = (
name.replace("UTF-8", "")
.replace("(Responses)", "")
.lstrip("'")
.rsplit("-", 1)[0]
.strip()
)
return response.text, name, show_rankings
@property
def data(self):
"""Return the data as a Pandas DataFrame."""
if not hasattr(self, "_data"):
self._data = pd.read_csv(io.StringIO(self.csv))
COLUMNS = self._data.columns
columns = self.columns
self.team_column = (
COLUMNS[int(columns.get("team"))]
if columns.get("team")
else TEAM_COLUMN
)
self.team_score_columns = (
[COLUMNS[int(column)] for column in columns.get("team-score-columns")]
if columns.get("team-score-columns")
else TEAM_SCORE_COLUMNS
)
self.opponent_column = (
COLUMNS[int(columns.get("opponent"))]
if columns.get("opponent")
else OPPONENT_COLUMN
)
self.opponent_score_columns = (
[
COLUMNS[int(column)]
for column in columns.get("opponent-score-columns")
]
if columns.get("opponent-score-columns")
else OPPONENT_SCORE_COLUMNS
)
self.day_column = (
COLUMNS[int(columns.get("day"))] if columns.get("day") else DAY_COLUMN
)
return self._data
@property
def column_names(self):
if not hasattr(self, "_data"):
return ALL_COLUMNS
return (
[self.team_column, self.opponent_column, self.day_column]
+ self.opponent_score_columns
+ self.team_score_columns
)
@property
def teams(self):
"""Return team names from the data."""
if not hasattr(self, "_teams"):
team = self.team_column
opponent = self.opponent_column
data = self.data
teams = set(data[data[team].notna()][team].unique())
opponents = set(data[data[opponent].notna()][opponent].unique())
self._teams = sorted(teams | opponents)
return self._teams
@property
def rankings(self):
"""Return spirit rankings given data, teams, and column names.
The rankings are in the following format:
|Team|Matches|Score|Self score|Avg spirit score|Avg self score|Difference|
"""
self._make_scores_numbers()
# Get total scores and averages
score, avg_score = self._get_scores(
self.opponent_column, self.opponent_score_columns, TOTAL_SCORE_COLUMN
)
self_score, avg_self_score = self._get_scores(
self.team_column, self.team_score_columns, TOTAL_SELF_SCORE_COLUMN
)
# Create dataframe to use for ranking
rankings = pd.DataFrame([score, self_score], dtype=np.int).transpose()
rankings["Avg score"] = avg_score
rankings["Avg self score"] = avg_self_score
rankings["Avg score difference"] = avg_score - avg_self_score
# Compute and order by ranks
rankings = rankings.sort_values("Avg score", ascending=False)
ranks = rankings["Avg score"].rank(method="min", ascending=False)
rankings["Rank"] = pd.Series(ranks, dtype=np.int)
rankings["Team"] = rankings.index
column_order = [
"Rank",
"Team",
"Avg score",
"Avg self score",
"Score",
"Self Score",
"Avg score difference",
]
rankings = rankings[column_order]
# Styling
rankings = (
rankings.style.set_precision("2")
.set_table_attributes(
'border="0" class="dataframe table table-hover table-striped"'
)
.set_table_styles(
[
dict(selector=".row_heading", props=[("display", "none")]),
dict(selector=".blank", props=[("display", "none")]),
]
)
.apply(self._bold_columns, axis=0)
)
return rankings
@property
def received_scores(self):
"""Return spirit scores received by each team."""
detailed_scores = [
(team, self._get_received_scores(team)) for team in self.teams
]
return detailed_scores
@property
def awarded_scores(self):
"""Return spirit scores awarded by each team."""
detailed_scores = [
(team, self._get_awarded_scores(team)) for team in self.teams
]
return detailed_scores
@property
def all_scores(self):
"""Return rankings, received and awarded scores."""
return self.rankings, self.received_scores, self.awarded_scores
@property
def missing_columns(self):
# NOTE: accessing self.data populates attributes required by self.column_names
data_columns = self.data.columns
return set(self.column_names) - set(data_columns)
def _bold_columns(self, column):
"""Set font-weight if column needs to be bold"""
return [
"font-weight: 700;" if column.name in {"Rank", "Team", "Avg score"} else ""
for _ in column
]
def _get_scores(self, groupby_column, score_columns, total_column):
total_score = self.data[score_columns].sum(axis=1)
self.data[total_column] = total_score
score_data = self.data.groupby(groupby_column)[total_column]
return (score_data.sum(), score_data.mean())
def _get_received_scores(self, team):
"""Return all the spirit scores received by the specified team
Also, appends the self scores for that match, beside the scores
received.
"""
columns = (
[self.team_column, self.day_column]
+ self.opponent_score_columns
+ [TOTAL_SCORE_COLUMN]
)
scores = self.data[self.data[self.opponent_column] == team][columns]
team_columns = (
[self.opponent_column, self.day_column]
+ self.team_score_columns
+ [TOTAL_SELF_SCORE_COLUMN]
)
team_scores = self.data[self.data[self.team_column] == team][team_columns]
left_on = [self.team_column, self.day_column]
right_on = [self.opponent_column, self.day_column]
merged_scores_inner = scores.merge(
team_scores, how="inner", left_on=left_on, right_on=right_on
)
merged_scores_outer = scores.merge(
team_scores, how="outer", left_on=left_on, right_on=right_on
)
missing_our, missing_other = get_missing_scores(
merged_scores_outer, merged_scores_inner, left_on, right_on
)
merged_scores = (
merged_scores_outer if self.show_rankings else merged_scores_inner
)
# Replace NaN in team column with names from opponent column (self scores)
merged_scores[self.team_column] = merged_scores[self.team_column].mask(
pd.isna, merged_scores[self.opponent_column]
)
columns = columns + self.team_score_columns + [TOTAL_SELF_SCORE_COLUMN]
display_scores = merged_scores[columns].rename(
columns={self.team_column: "Scored by"}
)
return display_scores, missing_our, missing_other
def _get_awarded_scores(self, team):
"""Return all the spirit scores awarded by the specified team.
Also, appends the self scores of the team for that match, beside the
scores awarded
"""
columns = (
[self.opponent_column, self.day_column]
+ self.opponent_score_columns
+ [TOTAL_SCORE_COLUMN]
)
scores = self.data[self.data[self.team_column] == team][columns]
team_columns = (
[self.team_column, self.day_column]
+ self.team_score_columns
+ [TOTAL_SELF_SCORE_COLUMN]
)
team_scores = self.data[self.data[self.opponent_column] == team][team_columns]
left_on = [self.opponent_column, self.day_column]
right_on = [self.team_column, self.day_column]
merged_scores_outer = scores.merge(
team_scores, how="outer", left_on=left_on, right_on=right_on
)
merged_scores_inner = scores.merge(
team_scores, how="inner", left_on=left_on, right_on=right_on
)
missing_other, missing_our = get_missing_scores(
merged_scores_outer, merged_scores_inner, left_on, right_on
)
merged_scores = (
merged_scores_outer if self.show_rankings else merged_scores_inner
)
# Replace NaN in team column with names from opponent column (self scores)
merged_scores[self.opponent_column] = merged_scores[self.opponent_column].mask(
pd.isna, merged_scores[self.team_column]
)
columns = columns + self.team_score_columns + [TOTAL_SELF_SCORE_COLUMN]
return merged_scores[columns], missing_our, missing_other
def _make_scores_numbers(self):
"""Convert str score columns to numbers"""
data = self.data
opponent_scores = data[self.opponent_score_columns]
if not opponent_scores.dtypes.apply(lambda x: x.type == np.float64).all():
data[self.opponent_score_columns] = opponent_scores.applymap(to_numbers)
self_scores = data[self.team_score_columns]
if not self_scores.dtypes.apply(lambda x: x.type == np.float64).all():
data[self.team_score_columns] = self_scores.applymap(to_numbers)