-
Notifications
You must be signed in to change notification settings - Fork 0
/
app_funcs.py
341 lines (281 loc) · 11.4 KB
/
app_funcs.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
def get_ordinal(number):
'''
Takes a number and returns the ordinal as a string
'''
if 10 <= number % 100 <= 20:
suffix = 'th'
else:
suffix = {1: 'st', 2: 'nd', 3: 'rd'}.get(number % 10, 'th')
return f"{number:.0f}{suffix}"
def get_games(source):
'''
Takes in the source as Live or Week #
Retrieves a list of games either from API or historical data
Returns
list of games
list of game ids
list of tuples with (home_team, away_team)
week #
raw game data (to reduce re-querying)
'''
import requests
import json
if source == 'Live':
url = 'http://site.api.espn.com/apis/site/v2/sports/football/nfl/scoreboard'
r = requests.get(url)
d = json.loads(r.text)
# get game that are in progress (status.startswith 2)
games = [x['shortName'] for x in d['events'] if (x['status']['type']['id'].startswith('2'))]
game_ids = [x['id'] for x in d['events'] if (x['status']['type']['id'].startswith('2'))]
home_away_tms = [(x.split('@ ')[1], x.split(' @')[0]) for x in games]
week = d['week']['number']
return games, game_ids, home_away_tms, week, d
# if historical game
else:
import nfl_data_py as nfl
week = int(source[-1]) # get week num
data = nfl.import_pbp_data([2023],
downcast=True,
cache=False,
alt_path=None,
columns=[
'play_id',
'qtr',
'home_team',
'away_team',
'week',
'posteam',
'defteam',
'yardline_100',
'half_seconds_remaining',
'game_seconds_remaining',
'down',
'goal_to_go',
'ydstogo',
'posteam_score',
'defteam_score',
'play_type',
'game_id'
])
# filter to selected week
data = data[data['week']==week].copy()
# sort plays into correct order
data = data.sort_values(by=['game_seconds_remaining'], ascending=False)
# get unique list of games
game_df = data[['home_team', 'away_team', 'game_id']].drop_duplicates()
home_away_tms = tuple(zip(game_df['home_team'], game_df['away_team']))
games = list(game_df["away_team"].astype(str) + ' @ ' + game_df["home_team"].astype(str))
game_ids = game_df['game_id'].to_list()
return games, game_ids, home_away_tms, week, data
def get_game_data(source, week, gameid, teams, game_data):
'''
Produce play data based on the source, week and game
Intakes game_data to prevent requerying for historical data
Writes play information and game_series to session state
for display and input to prediction
'''
import pandas as pd
import streamlit as st
# get selected play
play_index = st.session_state['play_index']
if source == 'Live':
import requests
import json
# query API for specific game
url = f'https://sports.core.api.espn.com/v2/sports/football/leagues/nfl/events/{gameid}3/competitions/{gameid}/plays?limit=300'
r = requests.get(url)
d = json.loads(r.text)
# get gameclock data
raw_game_data = d['items'][play_index]
seconds = d['items'][play_index]['clock']['value']
quarter = d['items'][play_index]['period']['number']
# create padding for seconds left in half and quarter
if quarter==5: # if game is in OT
game_seconds_remaining = seconds
half_seconds_remaining = seconds
else:
game_pad = 15*60*(4-quarter)
game_seconds_remaining = seconds + game_pad
if quarter < 3:
half_pad = 15*60*(2-quarter)
half_seconds_remaining = seconds+half_pad
if quarter > 2:
half_seconds_remaining = game_seconds_remaining
down = d['items'][play_index]['end']['down']
ydstogo = d['items'][play_index]['end']['distance']
yardline = d['items'][play_index]['end']['yardsToEndzone']
if ydstogo >= yardline:
goal_to_go = 1.
else:
goal_to_go = 0.
if down == -1:
st.write('Invalid play, try the next one!')
return pd.Series(), '', ''
else:
down_distance_text = d['items'][play_index]['end']['downDistanceText']
home_score = d['items'][play_index]['homeScore']
away_score = d['items'][play_index]['awayScore']
actual_play = d['items'][play_index+1]['type']['text']
# API does not have a field for posessing team
# it only provides the url for the team
url = d['items'][play_index]['team']['$ref']
r = requests.get(url)
d = json.loads(r.text)
pos_team = d['abbreviation']
def_team = teams[teams.index(pos_team) - 1]
# assign scores by home or away
if pos_team == teams[0]:
pos_score = home_score
def_score = away_score
is_pos_home = 1
else:
pos_score = away_score
def_score = home_score
is_pos_home = 0
# create index for series that will be predicted from
index = ['week',
'yardline_100',
'half_seconds_remaining',
'game_seconds_remaining',
'down',
'goal_to_go',
'ydstogo',
'posteam_score',
'defteam_score',
'is_pos_home',]
# organize data for series
data = [week,
yardline,
half_seconds_remaining,
game_seconds_remaining,
down,
goal_to_go,
ydstogo,
pos_score,
def_score,
is_pos_home]
series = pd.Series(data, index=index)
else:
from app_funcs import get_ordinal
game_data = game_data[[
'play_id',
'qtr',
'week',
'yardline_100',
'posteam',
'defteam',
'half_seconds_remaining',
'game_seconds_remaining',
'down',
'goal_to_go',
'ydstogo',
'posteam_score',
'defteam_score',
'play_type'
]]
# located selected play as series
play_data = game_data[game_data['play_id']==play_index].iloc[0]
# create is_pos_home col
home_team = teams[0]
play_data['is_pos_home'] = 0
if play_data['posteam']==home_team:
play_data['is_pos_home'] == 1
# create down distance text
down_text = get_ordinal(play_data['down'])
down_distance_text = f'{down_text} & {play_data["ydstogo"]:.0f}'
# assign vars for writing to session_state
pos_team = play_data['posteam']
def_team = play_data['defteam']
raw_game_data = play_data
quarter = play_data['qtr']
quarters_left = 4-quarter
seconds = play_data['game_seconds_remaining'] - (15*60*quarters_left)
actual_play = play_data['play_type']
# create series for prediction
series = play_data[['week',
'yardline_100',
'half_seconds_remaining',
'game_seconds_remaining',
'down',
'goal_to_go',
'ydstogo',
'posteam_score',
'defteam_score',
'is_pos_home',]]
logo = f'https://a.espncdn.com/i/teamlogos/nfl/500-dark/scoreboard/{pos_team}.png'
# identify states to write to session_state
states = {'posteam':pos_team,
'defteam':def_team,
'game_series':series,
'raw_game_data':raw_game_data,
'down_text':down_distance_text,
'quarter':quarter,
'seconds':seconds,
'posteam_logo':logo,
'actual_play':actual_play}
for s, v in states.items():
st.session_state[s] = v
def generate_play_text(selected_tms):
'''
Reads from session state and generates display text for play information
Writes play text to session_state
'''
import streamlit as st
# retrieve data from session state
posteam = st.session_state['posteam']
defteam = st.session_state['defteam']
game_series = st.session_state['game_series']
raw_game_data = st.session_state['raw_game_data']
down_distance_text = st.session_state['down_text']
home_team = selected_tms[0]
away_team = selected_tms[1]
#
if posteam == home_team:
home_score = game_series['posteam_score']
away_score = game_series['defteam_score']
else:
home_score = game_series['defteam_score']
away_score = game_series['posteam_score']
# game clock
seconds = st.session_state['seconds']
sec_remainder = seconds % 60
min = (seconds / 60) - (sec_remainder/60)
quarter = st.session_state['quarter']
game_time = f'{min:.0f}:{int(sec_remainder):02} {quarter:.0f}Q'
st.session_state['score_data'] = f'{away_team} {away_score:.0f} - {home_team} {home_score:.0f}'
st.session_state['has_the_ball'] = f'{posteam} has the ball'
st.session_state['game_time'] = game_time
def predict_play():
'''
Reads games series from session_state
Loads RF model
Predicts play_type
'''
import pandas as pd
import pickle
import streamlit as st
import numpy as np
import pickle
# read in data
game_series = st.session_state['game_series']
posteam = st.session_state['posteam']
defteam = st.session_state['defteam']
# import feature cols used for training
with open('data/feature_cols.pkl', 'rb') as f:
input_cols = pickle.load(f)
# get list of all cols and create if missing
missing_cols = list(set(input_cols) - set(game_series.index))
for c in missing_cols:
game_series[c] = 0
# find proper dummy cols and fill with 1 (e.g. posteam_CIN if CIN posseses the ball)
pos_team_col = f'posteam_{posteam}'
def_team_col = f'defteam_{defteam}'
game_series[pos_team_col] = 1
game_series[def_team_col] = 1
input_data = game_series.to_numpy().reshape(1, -1)
# import model
with open('model/rf_v1.pkl', 'rb') as f:
model = pickle.load(f)
pred = model.predict(input_data)
# write to session state
st.session_state['pred'] = pred