Exemple #1
0
# For the purposes of the demo, we will take the first five event and lineup files
# Comment this out if you want the whole of the open-data
lineup_links = lineup_links[:5]
event_links = event_links[:5]

##############################################################################
# Lineup data
# -----------

# Amend this path to where you want to store the data
LINEUP_FOLDER = os.path.join(DATA_FOLDER, 'lineup_raw')
# loop through all the changed links and store as parquet files - small and fast files
for file in lineup_links:
    save_path = f'{os.path.basename(file)[:-4]}parquet'
    try:
        df_lineup = sbapi.read_lineup(file, warn=False)
        df_lineup.to_parquet(os.path.join(LINEUP_FOLDER, save_path))
    except ValueError:
        print('Skipping file:', file)

##############################################################################
# Get the lineup files as a single dataframe

if len(lineup_links) == 0:
    print('No update')
else:
    lineup_files = glob.glob(os.path.join(LINEUP_FOLDER, '*.parquet'))
    df_lineup = pd.concat([pd.read_parquet(file) for file in lineup_files])
    df_lineup.to_parquet(os.path.join(DATA_FOLDER, 'lineup.parquet'))
    df_lineup.info()
Exemple #2
0
##############################################################################
# Pass maps
# ---------
# The following example plots pass maps for each player on a team.
# The plot design is copied from
# `Brad @DymondFormation <https://twitter.com/DymondFormation>`_.
#
# First we need to get the StatsBomb events, tactics, and lineup data
JSON = '15946.json'
kwargs = {'related_event_df': False, 'shot_freeze_frame_df': False, 'tactics_lineup_df': True}
event_dict = read_event(f'{EVENT_SLUG}/{JSON}', **kwargs)
events = event_dict['event']
tactics = event_dict['tactics_lineup']
# read the lineup (has all the players even if they didn't play)
lineup = read_lineup(f'{LINEUP_SLUG}/{JSON}', warn=False)

##############################################################################
# Add on the subbed on/ off times to the lineup dataframe

# dataframe with player_id and when they were subbed off
time_off = events.loc[(events.type_name == 'Substitution'),
                      ['player_id', 'minute']]
time_off.rename({'minute': 'off'}, axis='columns', inplace=True)
# dataframe with player_id and when they were subbed on
time_on = events.loc[(events.type_name == 'Substitution'),
                     ['substitution_replacement_id', 'minute']]
time_on.rename({'substitution_replacement_id': 'player_id',
                'minute': 'on'}, axis='columns', inplace=True)
players_on = time_on.player_id
# merge on times subbed on/off
from mplsoccer.statsbomb import read_event, read_lineup, EVENT_SLUG, LINEUP_SLUG
import matplotlib.pyplot as plt
plt.style.use('ggplot')

# get event and lineup dataframes for game 7478

# event data
dict_event = read_event(f'{EVENT_SLUG}/7478.json',
                        related_event_df=False,
                        tactics_lineup_df=False,
                        warn=False)
df_event = dict_event['event']
df_freeze = dict_event['shot_freeze_frame']

# lineup data
df_lineup = read_lineup(f'{LINEUP_SLUG}/7478.json', warn=False)
df_lineup = df_lineup[['player_id', 'player_jersey_number',
                       'team_name']].copy()

##############################################################################
# Subset a shot

shot_id = '8bb8bbc2-68a6-4c01-93de-53a194e7a1cf'
df_freeze_frame = df_freeze[df_freeze.id == shot_id].copy()
df_shot_event = df_event[df_event.id == shot_id].dropna(axis=1,
                                                        how='all').copy()

# add the jersey number
df_freeze_frame = df_freeze_frame.merge(df_lineup, how='left', on='player_id')

##############################################################################