Ejemplo n.º 1
0
def get_player_stints_for_season(season):
    game_log = TeamAdvancedGameLogs().get_data({'Season': season})['GAME_ID'].unique()

    pbp_ep = PlayByPlay()

    stints_df = pd.DataFrame()

    ix = 0
    for game in game_log:
        print(str(ix) + '/' + str(len(game_log)))
        ix += 1

        game_id = str(game)
        if len(game_id) < 10:
            game_id = '00' + str(game_id)

        pbp_df = pbp_ep.get_data({'Season': season, 'GameID': game_id})
        pbp_df['TIME'] = convert_time(pbp_df['PCTIMESTRING'], pbp_df['PERIOD'])

        teams = pbp_df['PLAYER1_TEAM_ABBREVIATION'].unique()[1:]

        for team in teams:
            team_df = get_team_df(pbp_df, team)
            try:
                team_lineups = get_game_lineups_for_team(team_df)
            except ValueError:
                print(ValueError)
                continue
            team_game_player_stints_df = get_game_player_stints_for_team(team_lineups)
            stints_df = stints_df.append(team_game_player_stints_df)

    return stints_df
Ejemplo n.º 2
0
def get_score_data_for_game(game):
    pbp_ep = PlayByPlay()

    pbp_df = pbp_ep.get_data({
        'Season': season,
        'GameID': game
    },
                             override_file=False)
    pbp_df['TIME'] = convert_time(pbp_df['PCTIMESTRING'],
                                  pbp_df['PERIOD']) / 60

    pbp_df = pbp_df[pbp_df['SCOREMARGIN'].notnull()]
    pbp_df = pbp_df[pbp_df['PLAYER1_ID'].notnull()]

    pbp_df['SCOREMARGIN'] = pbp_df['SCOREMARGIN'].map(lambda x: 0
                                                      if x == 'TIE' else x)

    pbp_df = pbp_df.rename(columns={
        'SCOREMARGIN': 'score_margin',
        'TIME': 'minute'
    })
    pbp_df = pbp_df[['score_margin', 'minute']]

    initial_row = [{'score_margin': 0, 'minute': 0}]

    pbp_df = pd.concat([pd.DataFrame(initial_row), pbp_df], ignore_index=True)

    return pbp_df
Ejemplo n.º 3
0
def get_viz_data_for_team_season(team_abbreviation):
    log = TeamAdvancedGameLogs().get_data({'Season': season, 'LastNGames': '3'}, override_file=True)
    log = log[log['TEAM_ABBREVIATION'] == team_abbreviation]

    pbp_ep = PlayByPlay()

    season_player_stints_df = pd.DataFrame()
    games = log.GAME_ID.tolist()
    for game in games:
        game = str(game)
        if len(game) < 10:
            game = '00' + str(game)

        pbp_df = pbp_ep.get_data({'Season': season, 'GameID': game})
        pbp_df['TIME'] = convert_time(pbp_df['PCTIMESTRING'], pbp_df['PERIOD'])

        game_stints_df = get_game_player_stints_for_team(pbp_df, team_abbreviation)
        season_player_stints_df = season_player_stints_df.append(game_stints_df)

    rotation_data = transform_stints_for_viz(season_player_stints_df)

    players = season_player_stints_df['player'].unique()
    players = sorted(players,
                     key=lambda x: -season_player_stints_df[season_player_stints_df['player'] == x]['time'].sum())

    index = 1
    rotation_data['pindex'] = 0
    for player in players:
        sys.stdout.write("\"" + player + "\",")
        cond = rotation_data.player == player
        rotation_data.pindex[cond] = index
        index += 1

    file_check(season_file_path)
    rotation_data.to_csv(season_file_path)
Ejemplo n.º 4
0
def get_most_common_starters_for_team_season(team_abb, season):
    game_log = TeamAdvancedGameLogs().get_data({'Season': season},
                                               override_file=True)
    game_log = game_log[game_log['TEAM_ABBREVIATION'] == team_abb]

    pbp_ep = PlayByPlay()
    starters = dict()
    for ix, game in game_log.iterrows():
        game_pbp = pbp_ep.get_data({'Season': season, 'GameID': game.GAME_ID})
        game_pbp['TIME'] = convert_time(game_pbp['PCTIMESTRING'],
                                        game_pbp['PERIOD'])

        team_pbp = get_team_df(game_pbp, team_abb)

        game_starters = get_initial_lineup(team_pbp[team_pbp['PERIOD'] == 1])

        game_starters = tuple(sorted(game_starters['players']))

        if game_starters in starters:
            starters[game_starters] += 1
        else:
            starters[game_starters] = 1

    sorted_starters = sorted(starters, key=lambda x: -starters[x])
    for ss in sorted_starters:
        print(str(ss) + ' : ' + str(starters[ss]))

    return starters
Ejemplo n.º 5
0
def get_viz_data_for_team_game_set(team_abb, games, season):
    pbp_ep = PlayByPlay()

    player_stints_df = pd.DataFrame()

    for game in games:
        game = str(game)
        if len(game) < 10:
            game = '00' + str(game)

        pbp_df = pbp_ep.get_data({'Season': season, 'GameID': game})
        pbp_df['TIME'] = convert_time(pbp_df['PCTIMESTRING'], pbp_df['PERIOD'])

        team_df = get_team_df(pbp_df, team_abb)
        team_lineups = get_team_game_lineups(team_df)
        game_stints_df = get_team_game_player_stints(team_lineups)
        player_stints_df = player_stints_df.append(game_stints_df)

    rotation_data = transform_stints_for_viz(player_stints_df,
                                             include_ot=False)

    starters = player_stints_df[player_stints_df['start_time'] ==
                                0]['player'].unique()
    starters = sorted(starters,
                      key=lambda x: -len(player_stints_df[
                          (player_stints_df['player'] == x) &
                          (player_stints_df['start_time'] == 0)]))
    starters = starters[:5]
    starters = sorted(starters,
                      key=lambda x: -player_stints_df[player_stints_df[
                          'player'] == x]['time'].sum())

    bench = player_stints_df[~player_stints_df['player'].
                             isin(starters)]['player'].unique()
    bench = sorted(bench,
                   key=lambda x: -player_stints_df[player_stints_df['player']
                                                   == x]['time'].sum())

    players = starters + bench

    index = 1
    rotation_data['pindex'] = 0
    for player in players:
        cond = rotation_data.player == player
        rotation_data.pindex[cond] = index
        index += 1

    return rotation_data.to_dict(orient='records')
Ejemplo n.º 6
0
def get_rotation_data_for_game(game_id,
                               year='2017-18',
                               single_game_file_path='./single_game/'):
    pbp_ep = PlayByPlay()

    game_id = str(game_id)
    if len(game_id) < 10:
        game_id = '00' + str(game_id)

    pbp_df = pbp_ep.get_data({'Season': year, 'GameID': game_id})
    pbp_df['TIME'] = convert_time(pbp_df['PCTIMESTRING'], pbp_df['PERIOD'])

    teams = pbp_df['PLAYER1_TEAM_ABBREVIATION'].unique()[1:]
    rotation_df = pd.DataFrame()
    index = 1
    for t in teams:
        team_df = get_team_df(pbp_df, t)
        team_lineups = get_game_lineups_for_team(team_df)
        team_game_player_stints_df = get_game_player_stints_for_team(
            team_lineups)
        team_rotation_df = transform_stints_for_viz(team_game_player_stints_df)

        players = team_game_player_stints_df['player'].unique()
        players = sorted(
            players,
            key=lambda x: -team_game_player_stints_df[
                team_game_player_stints_df['player'] == x]['time'].sum())

        team_rotation_df['pindex'] = 0
        for player in players:
            cond = team_rotation_df.player == player
            team_rotation_df.pindex[cond] = index
            index += 1

        index += 1

        rotation_df = rotation_df.append(team_rotation_df)

    file_check(single_game_file_path)
    rotation_df.to_csv(single_game_file_path + 'data.csv')

    score_df = get_score_data_for_game(game_id)
    score_df.to_csv(single_game_file_path + 'score.csv')
Ejemplo n.º 7
0
def get_score_data_for_game(game):
    pbp_ep = PlayByPlay()

    pbp_df = pbp_ep.get_data({'Season': season, 'GameID': game}, override_file=False)
    pbp_df['TIME'] = convert_time(pbp_df['PCTIMESTRING'], pbp_df['PERIOD']) / 60

    pbp_df = pbp_df[pbp_df['SCOREMARGIN'].notnull()]
    pbp_df = pbp_df[pbp_df['PLAYER1_ID'].notnull()]

    pbp_df['SCOREMARGIN'] = pbp_df['SCOREMARGIN'].map(lambda x: 0 if x == 'TIE' else x)

    pbp_df = pbp_df.rename(columns={'SCOREMARGIN': 'score_margin', 'TIME': 'minute'})
    pbp_df = pbp_df[['score_margin', 'minute']]

    initial_row = [{'score_margin': 0, 'minute': 0}]

    pbp_df = pd.concat([pd.DataFrame(initial_row), pbp_df], ignore_index=True)

    return pbp_df
Ejemplo n.º 8
0
def get_viz_data_for_team_season(team_abbreviation):
    log = TeamAdvancedGameLogs().get_data({
        'Season': season,
        'LastNGames': '3'
    },
                                          override_file=True)
    log = log[log['TEAM_ABBREVIATION'] == team_abbreviation]

    pbp_ep = PlayByPlay()

    season_player_stints_df = pd.DataFrame()
    games = log.GAME_ID.tolist()
    for game in games:
        game = str(game)
        if len(game) < 10:
            game = '00' + str(game)

        pbp_df = pbp_ep.get_data({'Season': season, 'GameID': game})
        pbp_df['TIME'] = convert_time(pbp_df['PCTIMESTRING'], pbp_df['PERIOD'])

        game_stints_df = get_game_player_stints_for_team(
            pbp_df, team_abbreviation)
        season_player_stints_df = season_player_stints_df.append(
            game_stints_df)

    rotation_data = transform_stints_for_viz(season_player_stints_df)

    players = season_player_stints_df['player'].unique()
    players = sorted(players,
                     key=lambda x: -season_player_stints_df[
                         season_player_stints_df['player'] == x]['time'].sum())

    index = 1
    rotation_data['pindex'] = 0
    for player in players:
        sys.stdout.write("\"" + player + "\",")
        cond = rotation_data.player == player
        rotation_data.pindex[cond] = index
        index += 1

    file_check(season_file_path)
    rotation_data.to_csv(season_file_path)
Ejemplo n.º 9
0
def get_rotation_data_for_game(pbp_df):
    pbp_df['TIME'] = convert_time(pbp_df['PCTIMESTRING'], pbp_df['PERIOD'])

    teams = pbp_df['PLAYER1_TEAM_ABBREVIATION'].unique()[1:]
    if not is_home(pbp_df, teams[0]):
        teams = reversed(teams)
    rotation_df = pd.DataFrame()
    index = 1
    for t in teams:
        team_df = get_team_df(pbp_df, t)
        team_lineups = get_team_game_lineups(team_df)
        team_game_player_stints_df = get_team_game_player_stints(team_lineups)

        starters = team_game_player_stints_df[
            team_game_player_stints_df['start_time'] == 0]['player'].unique()
        starters = sorted(
            starters,
            key=lambda x: -team_game_player_stints_df[
                team_game_player_stints_df['player'] == x]['time'].sum())

        bench = team_game_player_stints_df[
            ~team_game_player_stints_df['player'].isin(starters
                                                       )]['player'].unique()
        bench = sorted(
            bench,
            key=lambda x: -team_game_player_stints_df[
                team_game_player_stints_df['player'] == x]['time'].sum())

        players = starters + bench
        team_game_player_stints_df['pindex'] = 1
        for player in players:
            cond = team_game_player_stints_df.player == player
            team_game_player_stints_df.loc[cond, 'pindex'] = index
            index += 1

        index += 1

        rotation_df = rotation_df.append(team_game_player_stints_df)

    return rotation_df
Ejemplo n.º 10
0
def get_rotation_data_for_game(game_id, year='2017-18', single_game_file_path='./single_game/'):
    pbp_ep = PlayByPlay()

    game_id = str(game_id)
    if len(game_id) < 10:
        game_id = '00' + str(game_id)

    pbp_df = pbp_ep.get_data({'Season': year, 'GameID': game_id})
    pbp_df['TIME'] = convert_time(pbp_df['PCTIMESTRING'], pbp_df['PERIOD'])

    teams = pbp_df['PLAYER1_TEAM_ABBREVIATION'].unique()[1:]
    rotation_df = pd.DataFrame()
    index = 1
    for t in teams:
        team_df = get_team_df(pbp_df, t)
        team_lineups = get_game_lineups_for_team(team_df)
        team_game_player_stints_df = get_game_player_stints_for_team(team_lineups)
        team_rotation_df = transform_stints_for_viz(team_game_player_stints_df)

        players = team_game_player_stints_df['player'].unique()
        players = sorted(players,
                         key=lambda x: -team_game_player_stints_df[team_game_player_stints_df['player'] == x][
                             'time'].sum())

        team_rotation_df['pindex'] = 0
        for player in players:
            cond = team_rotation_df.player == player
            team_rotation_df.pindex[cond] = index
            index += 1

        index += 1

        rotation_df = rotation_df.append(team_rotation_df)

    file_check(single_game_file_path)
    rotation_df.to_csv(single_game_file_path + 'data.csv')

    score_df = get_score_data_for_game(game_id)
    score_df.to_csv(single_game_file_path + 'score.csv')
Ejemplo n.º 11
0
def get_game_ids_for_team_with_starters(starters, team_abb, season):
    starters = tuple(sorted(starters))

    game_log = TeamAdvancedGameLogs().get_data({'Season': season},
                                               override_file=True)
    game_log = game_log[game_log['TEAM_ABBREVIATION'] == team_abb]

    pbp_ep = PlayByPlay()
    games = []
    for ix, game in game_log.iterrows():
        game_pbp = pbp_ep.get_data({'Season': season, 'GameID': game.GAME_ID})
        game_pbp['TIME'] = convert_time(game_pbp['PCTIMESTRING'],
                                        game_pbp['PERIOD'])

        team_pbp = get_team_df(game_pbp, team_abb)

        game_starters = get_initial_lineup(team_pbp[team_pbp['PERIOD'] == 1])

        game_starters = tuple(sorted(game_starters['players']))

        if game_starters == starters:
            games.append(game.GAME_ID)

    return games