-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalyze2015.py
279 lines (201 loc) · 9.84 KB
/
analyze2015.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
# -*- coding: utf-8 -*-
"""
Created on Sun Jan 7 10:20:33 2018
2015 Scouting Rank Analysis Tools
Starting with 2015 because there ranking scores didn't include any ranking
points other than for WLT. More recent years with bonus RP for specific
actions are going to be harder to model. Can't do 2014 because component score
data isn't available on TBA.
"""
'''
Seeding
1. Qualification Average
2. Cumulative sum of Coop
3. Cumulative sum of AUTO
4. Cumulative sum of Container
5. Cumulative sum of Tote
6. Cumulative sum of Litter (Container + Landfull + unprocessed)
7. Random FMS
'''
from pprint import pprint
import pandas as pd
import tbaUtils as tba
YEAR = 2015
def maketeamlist(event='mokc'):
teamdf = pd.DataFrame(tba.get_event_teams(event,YEAR))
#print(teamdf)
teamseries = teamdf.team_number
answer = pd.Series.tolist(teamseries)
print(type(answer[0]))
return(answer)
def makematchlist(event='mokc'):
'''
This JSON response includes nested dictionaries, so it doesn't pandas
directly well. Going to deparse it a bit.
Basic strategy is to take the nested levels from a single match and
move them into a single-level dictionary, then append them to a new list of
matches. Also calculating ranking points for each alliance and fixing types.
After finishing, I force the matches into a pandas DataFrame using the
match key formatted like '2015mokc_qm64' as the index.
'''
matchlist = tba.get_event_matches(event,YEAR)
#pprint(matchlist[0:5])
# Initialize variables then iterate over the matches
flatmatchlist = []
key = []
for match in matchlist:
#Start empty for each match
flatmatch = {}
#Add the key to the dictionary and to the list of keys
flatmatch['key'] = match['key']
key.append(match['key'])
#Pick up the basic information
flatmatch['matchtype'] = match['comp_level']
flatmatch['matchnum'] = int(match['match_number'])
# Convert the lists of teams to individual positions
# Change'frc2164' -> '2164' as I go to match the team list
flatmatch['blue1'] = int(match['alliances']['blue']['team_keys'][0][3:])
flatmatch['blue2'] = int(match['alliances']['blue']['team_keys'][1][3:])
flatmatch['blue3'] = int(match['alliances']['blue']['team_keys'][2][3:])
flatmatch['red1'] = int(match['alliances']['red']['team_keys'][0][3:])
flatmatch['red2'] = int(match['alliances']['red']['team_keys'][1][3:])
flatmatch['red3'] = int(match['alliances']['red']['team_keys'][2][3:])
# Make a list of the teams that don't get seeding points for this match
flatmatch['ineligible'] = []
if len(match['alliances']['blue']['dq_team_keys']) > 0:
flatmatch['ineligible'].extend(match['alliances']['blue']['dq_team_keys'])
if len(match['alliances']['blue']['surrogate_team_keys']) > 0:
flatmatch['ineligible'].extend(match['alliances']['blue']['surrogate_team_keys'])
if len(match['alliances']['red']['dq_team_keys']) > 0:
flatmatch['ineligible'].extend(match['alliances']['red']['dq_team_keys'])
if len(match['alliances']['red']['surrogate_team_keys']) > 0:
flatmatch['ineligible'].extend(match['alliances']['red']['surrogate_team_keys'])
if len(flatmatch['ineligible']) > 0:
print(flatmatch['ineligible'])
# Pick up all the items in the scoring breakdown that matter for seeding
flatmatch['cooppts'] = match['score_breakdown']['coopertition_points']
flatmatch['bluescore'] = match['alliances']['blue']['score']
flatmatch['blueautopts'] = match['score_breakdown']['blue']['auto_points']
flatmatch['bluecontainerpts'] = match['score_breakdown']['blue']['container_points']
flatmatch['bluetotepts'] = match['score_breakdown']['blue']['tote_points']
flatmatch['bluelitterpts'] = match['score_breakdown']['blue']['litter_points']
flatmatch['redscore'] = match['alliances']['red']['score']
flatmatch['redautopts'] = match['score_breakdown']['red']['auto_points']
flatmatch['redcontainerpts'] = match['score_breakdown']['red']['container_points']
flatmatch['redtotepts'] = match['score_breakdown']['red']['tote_points']
flatmatch['redlitterpts'] = match['score_breakdown']['red']['litter_points']
# Calculate RP for red and blue based on match winner
if flatmatch['bluescore'] > flatmatch['redscore']:
flatmatch['bluerp'] = 2
flatmatch['redrp'] = 0
elif flatmatch['bluescore'] < flatmatch['redscore']:
flatmatch['bluerp'] = 0
flatmatch['redrp'] = 2
else: #tied
flatmatch['bluerp'] = 1
flatmatch['redrp'] = 1
#Take the complete match and add it to the match list
flatmatchlist.append(flatmatch)
key = pd.Series(key, name='key')
flatmatchdf = pd.DataFrame(flatmatchlist, index=key)
return flatmatchdf
def team_seedpts(matchdf):
'''
Take a match results dataframe and create a dataframe
that contains the seeding points to be applied to each team match by match
as the event progresses
'''
bluecolumns = ['bluescore', 'bluerp', 'cooppts', 'blueautopts',
'bluecontainerpts', 'bluetotepts', 'bluelitterpts',
'ineligible']
redcolumns = ['redscore', 'redrp', 'cooppts', 'redautopts',
'redcontainerpts', 'redtotepts', 'redlitterpts',
'ineligible']
columns = ['matchnum', 'team', 'score', 'rp', 'cooppts', 'autopts',
'containerpts', 'totepts', 'litterpts', 'ineligible']
# For each blue team, make a dataframe with their correct columns
blue1df = matchdf.reset_index().set_index(['matchnum', 'blue1'])[bluecolumns]
blue2df = matchdf.reset_index().set_index(['matchnum', 'blue2'])[bluecolumns]
blue3df = matchdf.reset_index().set_index(['matchnum', 'blue3'])[bluecolumns]
# Merge the tables together, then fix the headers to a generic
blues = [blue1df, blue2df, blue3df]
bluedf = pd.concat(blues)
bluedf.reset_index(inplace = True)
bluedf.columns = columns
# Same again, but for red
red1df = matchdf.reset_index().set_index(['matchnum', 'red1'])[redcolumns]
red2df = matchdf.reset_index().set_index(['matchnum', 'red2'])[redcolumns]
red3df = matchdf.reset_index().set_index(['matchnum', 'red3'])[redcolumns]
reds = [red1df, red2df, red3df]
reddf = pd.concat(reds)
reddf.reset_index(inplace = True)
reddf.columns = columns
# Now that the columns are aligned, glue red and blue together
fulldf = pd.concat([reddf,bluedf])
print(fulldf[fulldf['team'] == 1939])
# Going to need to fix this later to delete row if ineligible.
return fulldf
def strip_elims(matchdf):
'''
Remove matches that are not qualification matches from the match dataframe.
Change the key to the match number so they will sort better.
'''
newdf = matchdf[matchdf.matchtype == 'qm']
newdf.set_index('matchnum', inplace = True)
#print(newdf.head())
return newdf
def matchmtx(teamPts, teamlist):
'''
Take the points for each match and apply them to the teams
'''
print(teamPts[teamPts['matchnum'] == 1])
print('Matches Played', max(teamPts['matchnum']))
maxmatch = max(teamPts['matchnum'])
teamPtsByMatch = []
for team in teamlist:
shortdf = teamPts[teamPts['team'] == team]
#print(shortdf)
rp = [0] #at match 0, you have 0 RP
coop = [0]
auto = [0]
cont = [0]
tote = [0]
litter = [0]
for match in range(1, maxmatch+1):
if shortdf[shortdf['matchnum'] == match].empty:
rpts = rp[match - 1]
cpts = coop[match - 1]
apts = auto[match - 1]
ctpts = cont[match - 1]
tpts = tote[match - 1]
lpts = litter[match - 1]
else:
#print(shortdf[shortdf['matchnum'] == match]['rp'])
rpts = rp[match - 1] + shortdf[shortdf['matchnum']== match]['rp'].iloc[0]
cpts = coop[match - 1] + shortdf[shortdf['matchnum']== match]['cooppts'].iloc[0]
apts = auto[match - 1] + shortdf[shortdf['matchnum']== match]['autopts'].iloc[0]
ctpts = cont[match - 1] + shortdf[shortdf['matchnum']== match]['containerpts'].iloc[0]
tpts = tote[match - 1] + shortdf[shortdf['matchnum']== match]['totepts'].iloc[0]
lpts = litter[match - 1] + shortdf[shortdf['matchnum']== match]['litterpts'].iloc[0]
rp.append(rpts)
coop.append(cpts)
auto.append(apts)
cont.append(ctpts)
tote.append(tpts)
litter.append(lpts)
print(team, cont, tote, litter)
teamPtsByMatch.append({'team': team, 'rp': rp, 'coop': coop, 'auto': auto,
'container': cont, 'tote':tote, 'litter': litter})
def Main():
'''
Runs functions in correct order to generate the rankings
'''
print('Getting team list\n')
teamlist = maketeamlist()
print('Making match list and stripping elim rounds\n')
matchdf = strip_elims(makematchlist())
print('Calculating seeding points\n')
teamSeedPts = team_seedpts(matchdf)
print('Calculating seeding points over time\n')
matchseedmtx = matchmtx(teamSeedPts, teamlist)
Main()