Skip to content

Commit

Permalink
Add play by play template notebook
Browse files Browse the repository at this point in the history
  • Loading branch information
GR-NBA-data-challenge committed Feb 1, 2020
1 parent 881b729 commit ae7b042
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
16 changes: 16 additions & 0 deletions NBA-notebook.ipynb
Expand Up @@ -140,6 +140,22 @@
"data_loader.getPlayersV2('2011')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Play by Play data"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"data_loader.getPlays(12509) # Get plays given a gameId"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down
15 changes: 15 additions & 0 deletions libsimulation.py
Expand Up @@ -5,6 +5,7 @@
import requests
import pandas
import time
import numpy

class SimulationSettings:
env: str = 'prod'
Expand Down Expand Up @@ -59,6 +60,7 @@ def __init__(self, settings: SimulationSettings):
self.seasonColumnsV1Out = ['gameId', 'dateTime', 'homeTeam', 'awayTeam', 'pointsDiff', 'pointsSum', 'homeBlocks', 'homeMinutes', 'homeRebounds', 'homeScore', 'homeSteals', 'quarter0home', 'quarter1home', 'quarter2home', 'quarter3home', 'awayBlocks', 'awayMinutes', 'awayRebounds', 'awayScore', 'awaySteals', 'quarter0away', 'quarter1away', 'quarter2away', 'quarter3away', 'season', 'status']
self.seasonColumnsV2 = ['gameId', 'dateTime', 'homeTeam', 'homeTeamId', 'awayTeam', 'awayTeamId', 'homeBlocks', 'homeMinutes', 'homeRebounds', 'homeScore', 'homeSteals', 'homeAssists', 'quarter0home', 'quarter1home', 'quarter2home', 'quarter3home', 'awayBlocks', 'awayMinutes', 'awayRebounds', 'awayScore', 'awaySteals', 'awayAssists', 'quarter0away', 'quarter1away', 'quarter2away', 'quarter3away', 'season', 'status']
self.seasonColumnsV2Out = ['gameId', 'dateTime', 'homeTeam', 'homeTeamId', 'awayTeam', 'awayTeamId', 'pointsDiff', 'pointsSum', 'homeBlocks', 'homeMinutes', 'homeRebounds', 'homeScore', 'homeSteals', 'homeAssists', 'quarter0home', 'quarter1home', 'quarter2home', 'quarter3home', 'awayBlocks', 'awayMinutes', 'awayRebounds', 'awayScore', 'awaySteals', 'awayAssists', 'quarter0away', 'quarter1away', 'quarter2away', 'quarter3away', 'season', 'status']
self.playsColumns = ['gameId', 'time', 'homeScore', 'awayScore', 'category', 'type', 'description', 'quarter', 'remainingMinutes', 'remainingSeconds', 'playerId', 'playId']

def _formatSeasonDf(self, result, columnsIn, columnsOut):
if len(result) == 0:
Expand Down Expand Up @@ -134,6 +136,19 @@ def getPlayersV2(self, season: str):
result = self._getPlayers(season)
return pandas.DataFrame(result, columns=self.playerColumnsV2)

# Obtain play-by-play for a specific game
def getPlays(self, gameId: int):
data = _getRequest(f'https://{self.settings.env}api.nbadatachallenge.com/data/plays/{urllib.parse.quote(str(gameId))}')
result = []
for d in data:
dateTime = d['time']
if dateTime < self.settings.cutoff:
result.append(d)
dataFrame = pandas.DataFrame(result, columns=self.playsColumns)
dataFrame['playerId'] = dataFrame['playerId'].fillna(-1)
dataFrame['playerId'] = dataFrame['playerId'].astype(numpy.int64)
return dataFrame

def _loadPredictions(settings: SimulationSettings):
url = f'https://{settings.env}api.nbadatachallenge.com/data/predictions/{urllib.parse.quote(settings.cutoff)}'
if settings.cutoffend is not None:
Expand Down

0 comments on commit ae7b042

Please sign in to comment.