def is_in_box(data, direction, play): cross = {} for i in data.player_num.unique(): cross[i] = 0 pos = get_possession_df(data.loc[play]) for i in range(pos.iloc[-1]['frame']): polygons = {} dfFrame = footyviz.get_frame(data.loc[play], i/20) vor, dfVor = footyviz.calculate_voronoi(dfFrame) for index, region in enumerate(vor.regions): if not -1 in region: if len(region)>0: try: pl = dfVor[dfVor['region']==index] pl = pl.loc[pl['player_num'] != ''] polygon = Polygon([vor.vertices[i] for i in region]/SCALERS).intersection(pitch_polygon) polygons[pl.index[0]] = polygon x, y = polygon.exterior.xy if direction[play] == 'True': if cross[pl.iloc[0]['player_num']] < count_xg_coef(polygon, direction, play): cross[pl.iloc[0]['player_num']] = count_xg_coef(polygon, direction, play) else: if cross[pl.iloc[0]['player_num']] < count_xg_coef(polygon, direction, play): cross[pl.iloc[0]['player_num']] = count_xg_coef(polygon, direction, play) except IndexError: pass except AttributeError: pass return cross
def get_speed_vel(data,play,draw): pos = get_possession_df(data.loc[play]) full_vel = pd.DataFrame(columns = ['Unnamed: 0', 'bgcolor', 'dx', 'dy', 'edgecolor', 'player_num', 'team', 'x', 'y', 'z', 'vx', 'vy', 'speed']) for i in range(pos.iloc[-1]['frame']): dfFrame = footyviz.get_frame(data.loc[play], t=i/20) speed_vel = calc_player_velocities(dfFrame,data.loc[play],t=i/20,draw = draw) full_vel= pd.concat([full_vel,speed_vel]) return full_vel
def calc_player_velocities(dfFrame,data, t,fig = '', ax ='' , draw = True): frame = t * 20 pitch = dfFrame try: prev_pitch = footyviz.get_frame(data, t=(frame - 1)/20 ) except KeyError: if draw: return fig, ax, dfFrame, pitch else: return pitch pitch['vx'] = (pitch['x'] - prev_pitch['x']) / 0.05 pitch['vy'] = (pitch['y'] - prev_pitch['y']) / 0.05 pitch['speed'] = np.sqrt(pitch['vx']**2 + pitch['vy']**2) pitch = pitch[1:] if draw == True: fig, ax, dfFrame = footyviz.draw_frame(data, t=frame/20) ax.quiver(pitch['x'], pitch['y'], pitch['vx'], pitch['vy'], color='k', scale_units='inches', scale=10.,width=0.0015,headlength=5,headwidth=3,alpha=0.7) return fig, ax, dfFrame, pitch if draw == False: return pitch
# In[20]: fig, ax, dfFrame = footyviz.draw_frame(df, t=5.7) # This was probably what was on Trent Alexander-Arnold's head: # In[21]: fig, ax, dfFrame = footyviz.draw_frame(df, t=4) fig, ax, dfFrame = footyviz.add_voronoi_to_fig(fig, ax, dfFrame) # In[22]: #you can mix different frames for the player's positioning and for the voronoi (e.g. fixing the voronoi to the time of the pass) fig, ax, dfFrame = footyviz.draw_frame(df, t=5) dfFrame_for_voronoi = footyviz.get_frame(df, t=4) fig, ax, dfFrame = footyviz.add_voronoi_to_fig(fig, ax, dfFrame_for_voronoi) # # MoviePy # # MoviePy is a Python module for video editing that works nicely with matplotlib and Jupyter and is built on top of FFmpeg. You may think you never used FFMpeg, but you have. From Wikipedia: # # > FFmpeg is used by software such as VLC media player, xine, Cinelerra-GG video editor, Plex, Kodi, Blender, HandBrake, YouTube, and MPC-HC; it handles video and audio playback in Google Chrome, and Linux version of Firefox. # # You can find MoviePy's documentation at: https://zulko.github.io/moviepy/ # # Let's start with a basic animation: # In[23]: from moviepy import editor as mpy
max_speed[i] = [] avr_speed[i] = [] box_mean = {} box = {k: box[k] for k in box if not isnan(k)} #Experiment 1: calculate possesion time before each pass dfpas['possesiom_time'] = (dfpas['to_frame'] - dfpas['from_frame']) / 20 #Experiment 2: calculate area gained from making a pass for ix, pas in dfpas.iterrows(): area_start = pas['from_frame'] area_end = pas['to_frame'] space[pas['from_player_num']].append( liverpool_fot.space_gained( footyviz.get_frame(data.loc[pas['play']], area_start / 20), footyviz.get_frame(data.loc[pas['play']], area_end / 20))) pas_space.append( liverpool_fot.space_gained( footyviz.get_frame(data.loc[pas['play']], area_start / 20), footyviz.get_frame(data.loc[pas['play']], area_end / 20))) dfpas['space_gained'] = pas_space for i in dfpas.from_player_num.unique(): space[i] = np.mean(space[i]) for ix, pas in dfpas.iterrows(): time[pas['from_player_num']].append(pas.loc['possesiom_time']) #Experiment 3: how many players does the player cut off on average