-
Notifications
You must be signed in to change notification settings - Fork 19
/
bb_tools.py
707 lines (628 loc) · 28.1 KB
/
bb_tools.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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
# -*- coding: utf-8 -*-
"""
Created on Wed Dec 9 10:48:26 2015
@author: gmf
"""
import requests
import json
import re
import datetime
import pickle
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
# PATHS : Change these for your system
DATAHOME = '/media/gmf/GMF/unsynced/nba/data' # where to save data
#DATAHOME = '/media/ext/GMF/Data/nba' # where to save data
REPOHOME = '/home/gmf/Code/git/nba' # where are scripts
current_year = '2016' # 2016-17 season
default_season = current_year + '-10'
user_agent = {'User-agent': 'Mozilla/5.0'}
stats_query_names = ['boxscoreadvanced','boxscoreadvancedv2',
'boxscorefourfactors','boxscorefourfactorsv2','boxscoremisc',
'boxscoreplayertrackv2','boxscorescoring','boxscoretraditionalv2',
'boxscoreusage','boxscoreusagev2','commonallplayers',
'commonplayerinfo','commonteamroster','commonTeamYears',
'commonplayoffseries','draftcombinedrillresults',
'draftcombinenonstationaryshooting','draftcombineplayeranthro',
'draftcombinespotshooting','draftcombinestats','drafthistory',
'franchisehistory','homepageleaders','homepagev2',
'leagueleaders','playbyplay','playbyplayv2','playercareerstats',
'playergamelog','playerprofile','playerprofilev2',
'shotchartdetail','shotchartlineupdetail','teamgamelog',
'teaminfocommon','teamplayeronoffdetails','teamplayeronoffsummary','teamvsplayer',
'teamyearbyyearstats','videoStatus','videodetails','videoevents']
# Add elbow touch, post touch, paint touch??
sportvu_query_names = ['drives','defense','catchShoot','speed',
'shooting','rebounding','pullUpShoot','touches','passing',
'drivesTeam','defenseTeam','drivesTeam','defenseTeam',
'catchShootTeam','speedTeam','shootingTeam','reboundingTeam',
'pullUpShootTeam','touchesTeam','passingTeam']
# ADD PUTBACKS??? Add summary??
synergy_query_names = ['team_Transition','team_Cut',
'team_PRBallHandler','team_Handoff','team_Isolation',
'team_Misc','team_Postup','team_PRRollMan',
'team_Spotup','team_OffRebound','team_OffScreen',
'player_Transition','player_Cut','player_PRBallHandler',
'player_Handoff','player_Isolation','player_Misc',
'player_Postup','player_PRRollMan','player_Spotup',
'player_OffRebound','player_OffScreen']
combine_query_names = ['drillresults', 'nonstationaryshooting',
'playeranthro', 'spotshooting', 'stats']
stats_params = {
'LeagueID':'00', # format: d{2}
'SeasonID':'00215',
'Season':0, # format: d{4}-d{2}
'SeasonYear':'2015-10', # d{4}-d{2}
'SeasonType':'Regular Season', # format: (Regular Season)|(Pre Season)|(Playoffs)
'GameID':0,
'GameEventID':0, # format:
'StartPeriod':1, # format: 1-10
'EndPeriod':10, # format: 1-10
'StartRange':0,
'EndRange':0,
'RangeType':0,
'AheadBehind':'', # format: (Ahead or Behind)|(Behind or Tied)|(Ahead or Tied)
'ClutchTime':'', # format: (Last 5 Minutes)|(Last 4 Minutes)|(Last 3 Minutes)|(Last 2 Minutes)|(Last 1 Minute)|(Last 30 Seconds)|(Last 10 Seconds)
'ContextFilter':'', # format:
'ContextMeasure':'FGA', # format: (FGM)|(FGA)|(FG_PCT)|(FG3M)|(FG3A)|(FG3_PCT)|(FTM)|(FTA)|(OREB)|(DREB)|(AST)|(FGM_AST)|(FG3_AST)|(STL)|(BLK)|(BLKA)|(TOV)|(POSS_END_FT)|(PTS_PAINT)|(PTS_FB)|(PTS_OFF_TOV)|(PTS_2ND_CHANCE)|(REB)|(TM_FGM)|(TM_FGA)|(TM_FG3M)|(TM_FG3A)|(TM_FTM)|(TM_FTA)|(TM_OREB)|(TM_DREB)|(TM_REB)|(TM_TEAM_REB)|(TM_AST)|(TM_STL)|(TM_BLK)|(TM_BLKA)|(TM_TOV)|(TM_TEAM_TOV)|(TM_PF)|(TM_PFD)|(TM_PTS)|(TM_PTS_PAINT)|(TM_PTS_FB)|(TM_PTS_OFF_TOV)|(TM_PTS_2ND_CHANCE)|(TM_FGM_AST)|(TM_FG3_AST)|(TM_POSS_END_FT)|(OPP_FTM)|(OPP_FTA)|(OPP_OREB)|(OPP_DREB)|(OPP_REB)|(OPP_TEAM_REB)|(OPP_AST)|(OPP_STL)|(OPP_BLK)|(OPP_BLKA)|(OPP_TOV)|(OPP_TEAM_TOV)|(OPP_PF)|(OPP_PFD)|(OPP_PTS)|(OPP_PTS_PAINT)|(OPP_PTS_FB)|(OPP_PTS_OFF_TOV)|(OPP_PTS_2ND_CHANCE)|(OPP_FGM_AST)|(OPP_FG3_AST)|(OPP_POSS_END_FT)|(PTS))
'DateFrom':'', # format: YYYY-MM-DD
'DateTo':'', # format: YYYY-MM-DD
'DistanceRange':'', # format: (5ft Range)|(8ft Range)|(By Zone)
'GameDate':'', # format: YYYY-MM-DD
# 'GameScope':'', # format (1): (Season)|(Last 10)|(Yesterday)|(Finals)
'GameScope':'', # format (2): (Season)|(Last 10)|(Yesterday)|(Finals)
'GameSegment':'', # format: (First Half)|(Overtime)|(Second Half)
# 'GraphStartSeason':'2015-11', # format: d{4}-d{2}
# 'GraphEndSeason':'2016-01', # format: d{4}-d{2}
# 'GraphStat':'FGM', # format:
'GROUP_ID':0,
'GroupQuantity':1, # format: 1-5
'IsOnlyCurrentSeason':0, # format: 0-1
'LastNGames':0,
'Location':'', # format: (Home)|(Road)
'MeasureType':'', # format: (Base)|(Advanced)|(Misc)|(Four Factors)|(Scoring)|(Opponent)|(Usage)
'Month':0,
'OpponentTeamID':0,
'Outcome':'', # format: W/L
'PaceAdjust':'N', # format: Y/N
'Period':0,
'PerMode':'Totals', # format: (Totals)|(PerGame)|(MinutesPer)|(Per48)|(Per40)|(Per36)|(PerMinute)|(PerPossession)|(PerPlay)|(Per100Possessions)|(Per100Plays)
'PlayerID':0,
'PlayerExperience':'', # format: (Rookie)|(Sophomore)|(Veteran)
'PlayerOrTeam':'', # format: (Player)|(Team)
'PlayerPosition':'', # format: (F)|(C)|(G)|(C-F)|(F-C)|(F-G)|(G-F)
'PlayerScope':'All Players', # format: (All Players)|(Rookies)
'PlayerTeamID':0,
'PlusMinus':'Y', # format: Y/N
'PointDiff':'', # format:
'Position':'',
'Rank':'N', # format: format: Y/N
'RookieYear':'',
'Scope':'', # format: (RS)|(S)|(Rookies)
'Season':'',
'SeasonSegment':'', # format: (Post All-Star)|(Pre All-Star)
'StarterBench':'', # format: (Starters)|(Bench)
'StatCategory':'', # (Points)|(Rebounds)|(Assists)|(Defense)|(Clutch)|(Playmaking)|(Efficiency)|(Fast Break)|(Scoring Breakdown)
'TeamID':0,
'viewShots':'true',
'VsConference':'', # 'VsConference':'East', # format: (East)|(West)
'VsDivision':'' # format: (Atlantic)|(Central)|(Northwest)|(Pacific)|(Southeast)|(Southwest)|(East)|(West)
#'zone-mode':'basic',
}
# Sports Illustrated play-by-play:
si_params = {'json':1, 'sport': 'basketball/nba',
'id':0, 'box':'false', 'pbp':'true', 'linescore':'false'}
## BEGIN HELPER FUNCTIONS
def clock2float(clock_time):
mm,ss = clock_time.split(':')
return int(mm) + float(ss)/60.0
def dateify(date_str, delim='-'):
y,m,d = date_str.split(delim)
return datetime.date(int(y),int(m),int(d))
def dict_inv(d):
return zip2(d.values(), d.keys())
def getclosest(i, N):
N = np.array(N)
dist = abs(i-N)
#return N[np.argmin(dist)]
i=np.argmin(dist)
return N[i],i
def nsec_elapsed(period, pctime_str):
nmin,nsec = pctime_str.split(':')
nmin = int(nmin)
nsec = int(nsec)
if period<5: # regulation : 720 sec per quarter
pctime_sec = (11-nmin)*60 + 60 - nsec
return (period-1)*720 + pctime_sec
else: # overtime : 300 sec per quarter
pctime_sec = (4-nmin)*60 + 60 - nsec
return 2880 + (period-5)*300 + pctime_sec
def nsec_total(Nperiods):
if Nperiods<4:
print 'WARNING: Less than four periods found...'
return np.nan
elif Nperiods==4:
return 2880
else:
return (Nperiods-4)*300 + 2880
def nsec_total_gameid(gameid):
return nsec_total(get_pbp(gameid).iloc[-1].PERIOD)
def nsec_remain_period(pctime_str):
nmin,nsec = pctime_str.split(':')
return int(nmin)*60 + int(nsec)
def zip2(keys,vals):
return dict([(keys[i], vals[i]) for i in range(len(keys))])
## END HELPER FUNCTIONS
## BEGIN PLOTTING FUNCTIONS
# Simple version. Image based
def draw_court(axis=[0,100,0,50]):
#fig = plt.figure(figsize=(15,7.5))
#img = mpimg.imread(REPOHOME + '/image/nba_court_T.png')
img = mpimg.imread(REPOHOME + '/court1.png')
plt.imshow(img,extent=axis, zorder=0)
def draw_court_T(axis=[-250,250,-50,990]):
#fig = plt.figure(figsize=(15,7.5))
#img = mpimg.imread(REPOHOME + '/image/nba_court.png')
img = mpimg.imread(REPOHOME + '/court1_T.png')
plt.imshow(img,extent=axis, zorder=0)
## END PLOTTING FUNCTIONS
## BEGIN DATA GRAB FUNCTIONS
def get_gamelist_by_date(date_iso):
games_url = 'http://data.nba.com/5s/json/cms/noseason/scoreboard/%s/games.json' % date_iso
G = requests.get(games_url, params={}, headers=user_agent)
try:
return G.json()['sports_content']['games']['game']
except:
return []
def get_teams_all():
j = requests.get('http://stats.nba.com/stats/franchisehistory', params=stats_params, headers=user_agent).json()['resultSets'][0]
return pd.DataFrame(data=j['rowSet'],columns=j['headers'])
def get_teams_current():
# T = get_teams_all()
# T = T[T.END_YEAR==current_year]
# for each (unique) team, keep its "oldest version"
# i.e. one with earliest start year
# keep_idx = []
# for t in np.unique(T.TEAM_ID):
# matches = np.where(T.TEAM_ID==t)[0]
# if len(matches)==1:
# keep_idx.append(matches[0])
# else:
# ordered_start_year = np.argsort(T.START_YEAR[matches])
# keep_idx.append(matches[ordered_start_year.values[0]])
# return T.iloc[keep_idx].reset_index()
return pickle.load(open(REPOHOME + '/data/teams.p', 'rb'))
def get_team_roster(teamid,season=default_season):
p = stats_params.copy()
p['TeamID'] = teamid
p['Season'] = season
j = requests.get('http://stats.nba.com/stats/commonteamroster', params=p, headers=user_agent).json()['resultSets'][0]
return pd.DataFrame(data=j['rowSet'],columns=j['headers'])
def get_teams_gameid(gameid):
try:
B = get_boxscore_v2(gameid, box_type='traditional')
except:
B = get_boxscore(gameid)[4]
info_g = get_info(gameid)
away, home = info_g[['VISITOR_TEAM_ID','HOME_TEAM_ID']]
return B[B['TEAM_ID']==away], B[B['TEAM_ID']==home]
def get_team_info(teamid,season=default_season):
p = stats_params.copy()
p['TeamID'] = teamid
p['Season'] = season
j = requests.get('http://stats.nba.com/stats/teaminfocommon', params=p, headers=user_agent).json()['resultSets'][0]
return pd.DataFrame(data=j['rowSet'],columns=j['headers'])
def get_team_history(teamid):
p = stats_params.copy()
p['TeamID'] = teamid
j = requests.get('http://stats.nba.com/stats/teamyearbyyearstats', params=p, headers=user_agent).json()['resultSets'][0]
return pd.DataFrame(data=j['rowSet'],columns=j['headers'])
def get_players_all():
p = stats_params.copy()
p['Season'] = default_season
j = requests.get('http://stats.nba.com/stats/commonallplayers', params=p, headers=user_agent).json()['resultSets'][0]
return pd.DataFrame(data=j['rowSet'],columns=j['headers'])
def get_players_season(season=default_season):
season_year = int(season.split('-')[0])
P = get_players_all()
return P.iloc[np.where([(int(p.FROM_YEAR)<=season_year)*(int(p.TO_YEAR)>=season_year) for n,p in P.iterrows()])[0]].reset_index()
def get_players_current():
P = get_players_all()
return P[P.TO_YEAR==current_year].reset_index()
def get_player_info(playerid):
p = stats_params.copy()
p['PlayerID']=str(playerid)
j = requests.get('http://stats.nba.com/stats/commonplayerinfo', params=p, headers=user_agent).json()['resultSets'][0]
return pd.DataFrame(data=j['rowSet'],columns=j['headers'])
def get_player_career(playerid):
#try:
# 0
#except:
p = stats_params.copy()
p['PlayerID']=str(playerid)
j = requests.get('http://stats.nba.com/stats/playercareerstats', params=p, headers=user_agent).json()['resultSets'][0]
return pd.DataFrame(data=j['rowSet'],columns=j['headers'])
def get_player_gamelog(playerid,season=default_season):
p = stats_params.copy()
p['PlayerID'] = playerid
p['Season'] = season
j = requests.get('http://stats.nba.com/stats/playergamelog', params=p, headers=user_agent).json()['resultSets'][0]
return pd.DataFrame(data=j['rowSet'],columns=j['headers'])
def get_team_gamelog(teamid,season=default_season):
p = stats_params.copy()
p['TeamID'] = teamid
p['Season'] = season
j = requests.get('http://stats.nba.com/stats/teamgamelog', params=p, headers=user_agent).json()['resultSets'][0]
return pd.DataFrame(data=j['rowSet'],columns=j['headers'])
def get_boxscore(gameid):
try:
J = json.load(open('%s/json/box_%s.json' % (DATAHOME, gameid), 'rb'))
except:
p = stats_params.copy()
p['GameID'] = gameid
J = requests.get('http://stats.nba.com/stats/boxscore', params=p, headers=user_agent).json()['resultSets']
#---
box=[]
for j in J:
if bool(j['rowSet']):
box.append(pd.DataFrame(data=j['rowSet'],columns=j['headers']))
else:
box.append([])
return box
#---
#return [pd.DataFrame(data=j['rowSet'],columns=j['headers']) for j in J]
#return [pd.DataFrame(data=j['rowSet'],columns=j['headers']) for j in J[0:6]]
def get_boxscore_v2(gameid, box_type='summary'):
try:
J = json.load(open('%s/json/boxv2_%s_%s.json' % (DATAHOME, box_type, gameid), 'rb'))
except:
p = stats_params.copy()
p['GameID'] = gameid
#box_types = ['summary','traditional','scoring', 'fourfactors','advanced','playertrack','usage','misc']
J = requests.get('http://stats.nba.com/stats/boxscore%sv2' % box_type, params=p, headers=user_agent).json()['resultSets'][0]
return pd.DataFrame(data=J['rowSet'], columns=J['headers'])
def get_info(gameid):
try:
return get_boxscore_v2(gameid, box_type='summary').iloc[0]
except:
return get_boxscore(gameid)[2].iloc[0]
def get_pbp(gameid):
try:
j = json.load(open('%s/json/pbp_%s.json' % (DATAHOME, gameid), 'rb'))
except:
p = stats_params.copy()
p['GameID'] = gameid
#j = requests.get('http://stats.nba.com/stats/playbyplay', params=p, headers=user_agent).json()['resultSets'][0]
j = requests.get('http://stats.nba.com/stats/playbyplayv2', params=p, headers=user_agent).json()['resultSets'][0]
return pd.DataFrame(data=j['rowSet'],columns=j['headers'])
def get_shots(gameid):
try:
j = json.load(open('%s/json/shots_%s.json' % (DATAHOME, gameid), 'rb'))
except:
p = stats_params.copy()
p['ContextMeasure'] = 'FGA'
p['GameID'] = gameid
j = requests.get('http://stats.nba.com/stats/shotchartdetail', params=p, headers=user_agent).json()['resultSets'][0]
return pd.DataFrame(data=j['rowSet'],columns=j['headers'])
def get_average_shots():
try:
j = json.load(open('%s/json/shots_average.json' % DATAHOME, 'rb'))
except:
p = stats_params.copy()
p['GameID']='0021500001' # required but doesn't change output
p['ContextMeasure'] = 'FG_PCT'
j = requests.get('http://stats.nba.com/stats/shotchartdetail', params=p, headers=user_agent).json()['resultSets'][0]
with open('%s/json/shots_average.json' % DATAHOME, 'w') as f:
json.dump(j, f)
return pd.DataFrame(data=j['rowSet'],columns=j['headers'])
def get_synergy_stats(query_name):
j = requests.get('http://stats.nba.com/js/data/playtype' + '/%s.js' % query_name, params={}, headers=user_agent).json()['resultSets'][0]
return pd.DataFrame(data=j['rowSet'],columns=j['headers'])
def get_sportvu_stats(query_name, season='2015'):
j = requests.get('http://stats.nba.com/js/data/sportvu/%s/%sData.json' % (season,query_name),params={},headers=user_agent).json()['resultSets'][0]
return pd.DataFrame(data=j['rowSet'],columns=j['headers'])
def get_drafthistory():
p = stats_params.copy()
j = requests.get('http://stats.nba.com/stats/drafthistory', params=p, headers=user_agent).json()['resultSets'][0]
return pd.DataFrame(data=j['rowSet'],columns=j['headers'])
def get_combine_results(query_name, season=default_season):
# drillresults, nonstationaryshooting, playeranthro
# spotshooting, stats
p = stats_params.copy()
p['Season'] = season
j = requests.get('http://stats.nba.com/stats/draftcombine%s' % query_name, params=p, headers=user_agent).json()['resultSets'][0]
return pd.DataFrame(data=j['rowSet'],columns=j['headers'])
## Sports Illustrated play-by-play:
def get_si_pbp(gameid):
try:
k = json.load(open('%s/json/si_pbp_%s.json' % (DATAHOME, gameid), 'rb'))
except:
p = si_params.copy()
p['id'] = gameid
j = requests.get('http://www.si.com/pbp/liveupdate', params=p, headers=user_agent).json()
k = j['apiResults'][0]['league']['season']['eventType'][0]['events'][0]['pbp']
return pd.DataFrame.from_records(k)
def write_si_pbp(gameid):
# convert gameid to SI id
print 'Game %s, Sports Illustrated' % gameid
p = si_params.copy()
p['id'] = gameid
j = requests.get('http://www.si.com/pbp/liveupdate', params=p, headers=user_agent).json()
with open('%s/json/si_pbp_%s.json' % (DATAHOME, gameid), 'w') as f:
json.dump( j['apiResults'][0]['league']['season']['eventType'][0]['events'][0]['pbp'], f)
#### NOTE: This is for now-removed SportVu endpoint
# Still works for any previously-saved data
# Will need tweaking if/when the endpoint is returned
#
def get_sportvu_locations(gameid, eventnum):
#
# Important to remember: Event number indexing in play-by-play begins at 1!
if eventnum==0:
#print 'Warning: Event Num = 0 passed. Switching to event number 1.'
#eventnum+=1
return
#
try:
#J = json.load( open('%s/json/sv_%s_%s.json' % (DATAHOME, gameid, eventnum), 'rb') )
#J = json.load( open('%s/json/sv_%s_%s.json' % (DATAHOME, gameid, eventnum.zfill(4)), 'rb') )
J = json.load( open('%s/json/sv_%s_%s.json' % (DATAHOME, gameid, eventnum), 'rb') )
except:
p = stats_params.copy()
p['GameEventID'] = eventnum
p['GameID'] = gameid
J = requests.get('http://stats.nba.com/stats/locations_getmoments', params=p, headers=user_agent).json()['resultSets'][0]
home_players = pd.DataFrame.from_records(J['home']['players'])
home_players['teamid'] = J['home']['teamid']
away_players = pd.DataFrame.from_records(J['visitor']['players'])
away_players['teamid'] = J['visitor']['teamid']
players = pd.merge(home_players, away_players, how='outer')
players['playername'] = [pl.firstname + ' ' + pl.lastname for n,pl in players.iterrows()]
namedict = dict(zip(players.playerid.values, players.playername.values))
numberdict = dict(zip(players.playerid.values, players.jersey.values))
positiondict = dict(zip(players.playerid.values, players.position.values))
# SHOULD WE DOWNSAMPLE THE NUMBER OF MOMENTS CHOSEN????
moments = J['moments']
nmoments = len(moments)
# Save simple (1-d) data
nplayers = 10
period = [m[0] for m in moments]
momentid = [m[1] for m in moments]
period_remain = [m[2] for m in moments]
shotclock_remain = [m[3] for m in moments]
ballx = [m[5][0][2] for m in moments]
bally = [m[5][0][3] for m in moments]
ballz = [m[5][0][4] for m in moments]
# Save larger (10-d) data for each player
teamid = np.array(range(nmoments), dtype=object)
playerid = np.array(range(nmoments), dtype=object)
playername = np.array(range(nmoments), dtype=object)
playernumber = np.array(range(nmoments), dtype=object)
position = np.array(range(nmoments), dtype=object)
playerx = np.array(range(nmoments), dtype=object)
playery = np.array(range(nmoments), dtype=object)
for m in range(nmoments):
teamid[m] = [moments[m][5][n+1][0] for n in range(nplayers)]
playerid[m] = [moments[m][5][n+1][1] for n in range(nplayers)]
playerx[m] = [moments[m][5][n+1][2] for n in range(nplayers)]
playery[m] = [moments[m][5][n+1][3] for n in range(nplayers)]
playername[m] = [namedict[pl] for pl in playerid[m]]
playernumber[m] = [numberdict[pl] for pl in playerid[m]]
position[m] = [positiondict[pl] for pl in playerid[m]]
# Return result as Pandas DataFrame
D = pd.DataFrame(data=momentid, columns=['momentid'])
D['period'] = period
D['period_remain'] = period_remain
D['shotclock_remain'] = shotclock_remain
D['ballx'] = ballx
D['bally'] = bally
D['ballz'] = ballz
D['playerid'] = playerid
D['playername'] = playername
D['playernumber'] = playernumber
D['position'] = position
D['teamid'] = teamid
D['playerx'] = playerx
D['playery'] = playery
return D
def get_players_event(gameid, eventnum):
# Important to remember: Event number indexing in play-by-play begins at 1!
if eventnum==0:
print 'Warning: Event Num = 0 passed. Switching to event number 1.'
eventnum+=1
#
try:
#J = json.load( open('%s/json/sv_%s_%s.json' % (DATAHOME, gameid, eventnum), 'rb') )
J = json.load( open('%s/json/sv_%s_%s.json' % (DATAHOME, gameid, eventnum.zfill(4)), 'rb') )
except:
p = stats_params.copy()
p['GameEventID'] = eventnum
p['GameID'] = gameid
J = requests.get('http://stats.nba.com/stats/locations_getmoments', params=p, headers=user_agent).json()['resultSets'][0]
home_players = pd.DataFrame.from_records(J['home']['players'])
home_players['teamid'] = J['home']['teamid']
away_players = pd.DataFrame.from_records(J['visitor']['players'])
away_players['teamid'] = J['visitor']['teamid']
players = pd.merge(home_players, away_players, how='outer')
players['playername'] = [pl.firstname + ' ' + pl.lastname for n,pl in players.iterrows()]
namedict = dict(zip(players.playerid.values, players.playername.values))
numberdict = dict(zip(players.playerid.values, players.jersey.values))
positiondict = dict(zip(players.playerid.values, players.position.values))
moments = [J['moments'][0], J['moments'][-1]]
nmoments = len(moments)
# Initialize arrays
nplayers = 10
momentid = [m[1] for m in moments]
teamid = np.array(range(nmoments), dtype=object)
playerid = np.array(range(nmoments), dtype=object)
playername = np.array(range(nmoments), dtype=object)
playernumber = np.array(range(nmoments), dtype=object)
position = np.array(range(nmoments), dtype=object)
playerx = np.array(range(nmoments), dtype=object)
playery = np.array(range(nmoments), dtype=object)
for m in range(nmoments):
teamid[m] = [moments[m][5][n+1][0] for n in range(nplayers)]
playerid[m] = [moments[m][5][n+1][1] for n in range(nplayers)]
playerx[m] = [moments[m][5][n+1][2] for n in range(nplayers)]
playery[m] = [moments[m][5][n+1][3] for n in range(nplayers)]
playername[m] = [namedict[pl] for pl in playerid[m]]
playernumber[m] = [numberdict[pl] for pl in playerid[m]]
position[m] = [positiondict[pl] for pl in playerid[m]]
D = pd.DataFrame(data=momentid, columns=['momentid'])
D['playerid'] = playerid
D['playername'] = playername
D['playernumber'] = playernumber
D['position'] = position
D['teamid'] = teamid
return D
def get_players_game(gameid):
eventnum = '1'
# add while loop to ensure we keep going eventnum += 1
# until we get a working number, if eventnum=1 doesn't work
try:
#J = json.load( open('%s/json/sv_%s_%s.json' % (DATAHOME, gameid, eventnum), 'rb') )
J = json.load( open('%s/json/sv_%s_%s.json' % (DATAHOME, gameid, eventnum.zfill(4)), 'rb') )
except:
p = stats_params.copy()
p['GameEventID'] = eventnum
p['GameID'] = gameid
J = requests.get('http://stats.nba.com/stats/locations_getmoments', params=p, headers=user_agent).json()['resultSets'][0]
home_players = pd.DataFrame.from_records(J['home']['players'])
home_players['teamid'] = J['home']['teamid']
away_players = pd.DataFrame.from_records(J['visitor']['players'])
away_players['teamid'] = J['visitor']['teamid']
players = pd.merge(home_players, away_players, how='outer')
return players
#return get_boxscore(gameid)[4]
def get_play_team(p):
# given a row of play-by-play data p, this function determines which
# team made the given play
#
# 0 for home team
# 1 for away team
if p.HOMEDESCRIPTION is not None and p.VISITORDESCRIPTION is None:
return 'home'
elif p.HOMEDESCRIPTION is None and p.VISITORDESCRIPTION is not None:
return 'away'
elif p.HOMEDESCRIPTION is None and p.VISITORDESCRIPTION is None:
return np.nan
else: # both filled in -> have to check the description text
h = p.HOMEDESCRIPTION
v = p.VISITORDESCRIPTION
e_type = p.EVENTMSGTYPE
if e_type==1:
if re.search('Shot',h) or re.search('Layup',h) or re.search('Dunk',h): return 'home'
elif re.search('Shot',v) or re.search('Layup',v) or re.search('Dunk',v): return 'away'
elif e_type==2:
if re.search('MISS',h): return 'home'
elif re.search('MISS',v): return 'away'
elif e_type==3:
if re.search('Free Throw',h): return 'home'
elif re.search('Free Throw',v): return 'away'
elif e_type==4:
if re.search('REBOUND',h) or re.search('Rebound',h): return 'home'
elif re.search('REBOUND',v) or re.search('Rebound',v): return 'away'
elif e_type==5:
if re.search('Turnover',h): return 'home'
elif re.search('Turnover',v): return 'away'
#elif e_type==6:
# if re.search('Turnover',h): return 0
# elif re.search('Turnover',v): return 1
else:
print 'PROBLEM!'
print h + '\t' + v
def get_play_desc(p):
if p.HOMEDESCRIPTION is not None and p.VISITORDESCRIPTION is None:
return str(p.HOMEDESCRIPTION).split(' (')[0]
elif p.HOMEDESCRIPTION is None and p.VISITORDESCRIPTION is not None:
return str(p.VISITORDESCRIPTION).split(' (')[0]
elif p.HOMEDESCRIPTION is None and p.VISITORDESCRIPTION is None:
return ''
else: # both filled in -> have to check the description text
h = p.HOMEDESCRIPTION.split(' (')[0]
v = p.VISITORDESCRIPTION.split(' (')[0]
e_type = p.EVENTMSGTYPE
if e_type==1:
if re.search('Shot',h): return h
elif re.search('Shot',v): return v
elif e_type==2:
if re.search('MISS',h): return h
elif re.search('MISS',v): return v
elif e_type==3:
if re.search('Free Throw',h): return h
elif re.search('Free Throw',v): return v
elif e_type==4:
if re.search('REBOUND',h) or re.search('Rebound',h): return h
elif re.search('REBOUND',v) or re.search('Rebound',v): return v
elif e_type==5:
if re.search('Turnover',h): return h
elif re.search('Turnover',v): return v
#elif e_type==6:
# if re.search('Turnover',h): return 0
# elif re.search('Turnover',v): return 1
else:
print 'PROBLEM!'
print h + '\t' + v
#oiuj
#### END OLD ^^^ END OLD ^^^ END OLD ^^^ #######
## END DATA GRAB FUNCTIONS
## WRITE FUNCTIONS:
def write_game_json(gameid, do_sportvu=False):
p = stats_params.copy()
p['ContextMeasure'] = 'FGA'
p['GameID'] = gameid
# Box score endpoints just changed:::
#box_types = ['summary','traditional','scoring', 'fourfactors','advanced','playertrack','usage','misc']
#box_types = ['summary','traditional','scoring', 'fourfactors','advanced']
#for box_type in box_types:
box_type = 'summary'
print 'Game %s, box score (%s)' % (gameid, box_type)
with open('%s/json/boxv2_%s_%s.json' % (DATAHOME, box_type, gameid), 'w') as f:
box = requests.get('http://stats.nba.com/stats/boxscore%sv2' % box_type, params=p, headers=user_agent).json()['resultSets'][0]
json.dump( box, f)
box_type = 'traditional'
#box_type = 'advanced'
#box_type = 'playertrack'
print 'Game %s, box score (%s)' % (gameid, box_type)
with open('%s/json/boxv2_%s_%s.json' % (DATAHOME, box_type, gameid), 'w') as f:
box = requests.get('http://stats.nba.com/stats/boxscore%sv2' % box_type, params=p, headers=user_agent).json()['resultSets'][0]
json.dump( box, f)
box_type = 'playertrack'
print 'Game %s, box score (%s)' % (gameid, box_type)
with open('%s/json/boxv2_%s_%s.json' % (DATAHOME, box_type, gameid), 'w') as f:
box = requests.get('http://stats.nba.com/stats/boxscore%sv2' % box_type, params=p, headers=user_agent).json()['resultSets'][0]
json.dump( box, f)
print 'Game %s, play by play' % gameid
pbp = requests.get('http://stats.nba.com/stats/playbyplayv2', params=p, headers=user_agent).json()['resultSets'][0]
with open('%s/json/pbp_%s.json' % (DATAHOME, gameid), 'w') as f:
json.dump( pbp, f)
print 'Game %s, shot chart' % gameid
shots = requests.get('http://stats.nba.com/stats/shotchartdetail', params=p, headers=user_agent).json()['resultSets'][0]
with open('%s/json/shots_%s.json' % (DATAHOME, gameid), 'w') as f:
json.dump( shots, f)
def write_gamelist_json(filename):
f = open(filename, 'r')
f.readline() # drop headers
for r in f.readlines():
gameid = r.split(',')[0]
write_game_json(gameid)
def write_gamelist_by_date(filename,seasonid,startday,stopday):
numdays = (stopday-startday).days
datelist = [startday + datetime.timedelta(days=x) for x in range(0, numdays+1)]
f = open(filename, 'w')
f.write('GAME_ID,SEASON_ID,GAME_CODE,AWAY,HOME\n') # write headers
for d in datelist:
diso = str.replace(d.isoformat(),'-','')
games = get_gamelist_by_date(diso)
for g in games:
home = g['home']['team_key']
away = g['visitor']['team_key']
gamecode = diso + away + home
print d,g['id'],away,home
#
# VV NEED TO FIX: STILL INCLUDES ALL STAR GAME
#
if seasonid==g['id'][0:5]: # make sure to exclude all start break!
f.write(','.join([g['id'],seasonid,gamecode,away,home]) + '\n')
f.close()