def fix_broken_times_json(err_file): data = json.load(open(err_file, 'r')) fixes = data['fixes'] for fix in fixes: game_id = fix['game_id'] game = Game(game_id) player_id = fix['player_id'] player = Player(player_id) time_data = [(dt.timedelta(seconds=t['start']), dt.timedelta(seconds=t['end'])) for t in fix['times']] print('Updating playing time for {} ({}) in {} ({})'.format( player, player.id, game, game.id)) player.save_timestream(game, time_data) #print(compute_ts_length(time_data, unit='minutes')) boxscore_minutes = game.player_boxscore( player)['total_seconds_played'] / 60.0 computed_minutes = compute_ts_length(player.time_on_court( game, recompute=False), unit='minutes') if not abs(computed_minutes - boxscore_minutes) <= 0.5: err = colored( 'Discrepancy between computed time: {0: 2.2f} and boxscore time: {1:2.2f}' .format(computed_minutes, boxscore_minutes), 'red') print(err) else: print('{} played {} minutes in {}'.format( player, round(computed_minutes, 3), game))
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({}, no_cursor_timeout=True).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)['total_seconds_played'] / 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)) del player del games_played