def compute_all_season_lineups(year): print('Loading the {}-{} NBA season'.format(year, year + 1)) season = Season(year) print('Loaded season') print('Computing lineups for all teams in all games...') for game in tqdm(season): print('Computing lineups for {}'.format(game)) home_team = game.home_team away_team = game.away_team home_lineups = game.lineup_combinations(home_team) away_lineups = game.lineup_combinations(away_team) try: for lineup in home_lineups: home_timestream = game.time_by_lineup(lineup) for lineup in away_lineups: away_timestream = game.time_by_lineup(lineup) home_minutes = compute_ts_length(home_timestream) away_minutes = compute_ts_length(away_timestream) print('Home team: {} minutes, Away team: {} minutes'.format(home_minutes, away_minutes)) except Exception as ex: print('Oh no, something terrible happened while computing lineups for {}'.format(game)) print(ex)
def test_player_time_on_court(self): player = Player.Player(399725) game = Game.Game(1349077) time_on_court = player.time_on_court(game, recompute=True) print compute_ts_length(time_on_court) self.assertEqual(int(round(compute_ts_length(time_on_court))), 2409)
def fix_broken_times(err_file): with open(err_file, 'r') as f: for line in f: matches = re.findall('\([\d]+\)', line) if matches and matches != []: ids = map(lambda x: int(x.replace('(', '').replace(')', '')), matches) player_id, game_id = ids[0], ids[1] player = Player(player_id) game = Game(game_id) print('Calculating time on court for {} ({}) in {} ({})'.format(player, player.id, game, game.id)) time_on_court = player.time_on_court(game, recompute=True) computed_minutes = compute_ts_length(time_on_court, unit='minutes') boxscore_minutes = game.player_boxscore(player)['totalSecondsPlayed'] / 60.0 if not abs(computed_minutes - boxscore_minutes) <= 1.0: print('In computing playing time for {} ({}) in {} ({}):'.format(player, player.id, game, game.id), file=sys.stderr) print('Discrepancy between computed time: {0:2.2f}, and boxscore time: {1:2.2f}'.format(computed_minutes, boxscore_minutes), file=sys.stderr) else: print('{} played {} minutes in {}'.format(player, round(computed_minutes, 3), game))
def calc_all_player_times(year, recompute=False): class TimeComputationError(Exception): def __init__(self, msg): self.msg = msg def __str__(self): return self.msg all_players = players.find({}).sort('id', pymongo.ASCENDING) print('Loading the {}-{} NBA season'.format(year, year + 1)) season = Season(year) print('Loaded season') print('Computing time on court for all players in all games...') for player_data in all_players: player = Player(player_data['id']) games_played = season.get_player_games_in_range(player) for game in games_played: print('Calculating time on court for {} ({}) in {} ({})'.format(player, player.id, game, game.id)) boxscore_minutes = game.player_boxscore(player)['totalSecondsPlayed'] / 60.0 if boxscore_minutes > 0: time_on_court = player.time_on_court(game, recompute=recompute) computed_minutes = compute_ts_length(time_on_court, unit='minutes') else: # there's never anything to calculate anyway computed_minutes = 0 if not abs(computed_minutes - boxscore_minutes) <= 0.5: print('In computing playing time for {} ({}) in {} ({}):'.format(player, player.id, game, game.id), file=sys.stderr) print('Discrepancy between computed time: {0:2.2f}, and boxscore time: {1:2.2f}'.format(computed_minutes, boxscore_minutes), file=sys.stderr) #raise TimeComputationError('Discrepancy between computed time: {}, and boxscore time: {}'.format(computed_minutes, boxscore_minutes) else: print('{} played {} minutes in {}'.format(player, round(computed_minutes, 3), game))
def test_player_time(): game = Game(1349050) quarter_plays = sorted([ev for ev in game.events if ev.period == 1], reverse=True) for play in quarter_plays: print play, play.event_id game.quarter_enders()[1] ev1 = quarter_plays[5] ev2 = quarter_plays[7] #import pdb; pdb.set_trace(); # print ev1, ev1.id # print ev2, ev2.id # # print ev1 > ev2 # print ev1.__cmp__(ev2) player = Player(3102) sh = player.time_on_court(game, recompute=True) print '{} in {}'.format(player, game) print 'minutes played: {0:2.3f}'.format(compute_ts_length(sh) / 60.0) pretty_print_times(sh)
def time_played(self, game, unit='seconds'): return compute_ts_length(self.time_on_court(game), unit=unit)
def regress_lineups_team_efg_mult_games(game_ids, team_id, self_or_opp='self'): all_game_stats = [] all_players = [] for game in game_ids: team1_name, team1_id, team2_name, team2_id = game_teams(game) print 'Processing {} vs {}'.format(team1_name, team2_name) if self_or_opp == 'self': game_stats = game_stats_by_lineup(game, team_id, team_id, ['efg']) if self_or_opp == 'opp': if team_id == team1_id: opp_id = team2_id elif team_id == team2_id: opp_id = team1_id game_stats = game_stats_by_lineup(game, team_id, opp_id, ['efg']) players = sorted(game_players(game, team_id)) all_players += players all_game_stats += game_stats all_players = sorted(pylab.unique(all_players)) efgs = [[] for i in range(len(all_game_stats))] X = pylab.zeros((len(all_game_stats), len(all_players))) w = pylab.zeros(len(all_game_stats)) used_lineups = [] sample_counter = 0 total_gt = 0 for i, item in enumerate(all_game_stats): lineup = item['lineup'] efgs[sample_counter].append(item['stats']) total_gt += compute_ts_length(item['tof']) #print used_lineups if used_lineup(lineup, used_lineups): w[sample_counter] += compute_ts_length(item['tof']) else: w[sample_counter] += compute_ts_length(item['tof']) used_lineups.append(lineup) for player in lineup: ind = all_players.index(player) X[sample_counter][ind] = 1 sample_counter += 1 print i, sample_counter #X[i][len(all_players)] = compute_ts_length(item['tof']) #w[i] = compute_ts_length(item['tof']) #efgs[i] = item['stats']['efg'] y = [] for i, item in enumerate(efgs): #if i < len(used_lineups): total_threes = sum([stat['total_threes'] for stat in item]) total_made = sum([stat['total_made'] for stat in item]) total_attempts = sum([stat['total_attempts'] for stat in item]) if total_attempts > 0: efg = (total_threes * 0.5 + total_made) / total_attempts else: efg = 0 y.append(efg) y = pylab.array(y) w = pylab.array(w) #result = sm.WLS(efgs, X, w).fit() #return [(player, param) for player, param in zip(players, result.params)] return y, X, w, all_players
def lineup_minutes(lineup): lineup_times = game.time_by_lineup(lineup) return compute_ts_length(lineup_times) / 60.0
def regress_lineups_team_efg_mult_games(game_ids, team_id, self_or_opp='self'): all_game_stats = [] all_players = [] for game in game_ids: team1_name, team1_id, team2_name, team2_id = game_teams(game) print 'Processing {} vs {}'.format(team1_name, team2_name) if self_or_opp == 'self': game_stats = game_stats_by_lineup(game, team_id, team_id, ['efg']) if self_or_opp == 'opp': if team_id == team1_id: opp_id = team2_id elif team_id == team2_id: opp_id = team1_id game_stats = game_stats_by_lineup(game, team_id, opp_id, ['efg']) players = sorted(game_players(game, team_id)) all_players += players all_game_stats += game_stats all_players = sorted(pylab.unique(all_players)) efgs = [ [] for i in range(len(all_game_stats))] X = pylab.zeros((len(all_game_stats), len(all_players))) w = pylab.zeros(len(all_game_stats)) used_lineups = [] sample_counter = 0 total_gt = 0 for i, item in enumerate(all_game_stats): lineup = item['lineup'] efgs[sample_counter].append(item['stats']) total_gt += compute_ts_length(item['tof']) #print used_lineups if used_lineup(lineup, used_lineups): w[sample_counter] += compute_ts_length(item['tof']) else: w[sample_counter] += compute_ts_length(item['tof']) used_lineups.append(lineup) for player in lineup: ind = all_players.index(player) X[sample_counter][ind] = 1 sample_counter += 1 print i, sample_counter #X[i][len(all_players)] = compute_ts_length(item['tof']) #w[i] = compute_ts_length(item['tof']) #efgs[i] = item['stats']['efg'] y = [] for i, item in enumerate(efgs): #if i < len(used_lineups): total_threes = sum([stat['total_threes'] for stat in item]) total_made = sum([stat['total_made'] for stat in item]) total_attempts = sum([stat['total_attempts'] for stat in item]) if total_attempts > 0: efg = (total_threes * 0.5 + total_made) / total_attempts else: efg = 0 y.append(efg) y = pylab.array(y) w = pylab.array(w) #result = sm.WLS(efgs, X, w).fit() #return [(player, param) for player, param in zip(players, result.params)] return y, X, w, all_players