-
Notifications
You must be signed in to change notification settings - Fork 0
/
Metrica_Viz.py
314 lines (262 loc) · 16.9 KB
/
Metrica_Viz.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Apr 5 09:10:58 2020
Module for visualising Metrica tracking and event data
Data can be found at: https://github.com/metrica-sports/sample-data
@author: Laurie Shaw (@EightyFivePoint)
"""
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.animation as animation
import Metrica_PitchControl as mpc
def plot_pitch( field_dimen = (106.0,68.0), field_color ='green', linewidth=2, markersize=20):
""" plot_pitch
Plots a soccer pitch. All distance units converted to meters.
Parameters
-----------
field_dimen: (length, width) of field in meters. Default is (106,68)
field_color: color of field. options are {'green','white'}
linewidth : width of lines. default = 2
markersize : size of markers (e.g. penalty spot, centre spot, posts). default = 20
Returrns
-----------
fig,ax : figure and aixs objects (so that other data can be plotted onto the pitch)
"""
fig,ax = plt.subplots(figsize=(12,8)) # create a figure
# decide what color we want the field to be. Default is green, but can also choose white
if field_color=='green':
ax.set_facecolor('mediumseagreen')
lc = 'whitesmoke' # line color
pc = 'w' # 'spot' colors
elif field_color=='white':
lc = 'k'
pc = 'k'
# ALL DIMENSIONS IN m
border_dimen = (3,3) # include a border arround of the field of width 3m
meters_per_yard = 0.9144 # unit conversion from yards to meters
half_pitch_length = field_dimen[0]/2. # length of half pitch
half_pitch_width = field_dimen[1]/2. # width of half pitch
signs = [-1,1]
# Soccer field dimensions typically defined in yards, so we need to convert to meters
goal_line_width = 8*meters_per_yard
box_width = 20*meters_per_yard
box_length = 6*meters_per_yard
area_width = 44*meters_per_yard
area_length = 18*meters_per_yard
penalty_spot = 12*meters_per_yard
corner_radius = 1*meters_per_yard
D_length = 8*meters_per_yard
D_radius = 10*meters_per_yard
D_pos = 12*meters_per_yard
centre_circle_radius = 10*meters_per_yard
# plot half way line # center circle
ax.plot([0,0],[-half_pitch_width,half_pitch_width],lc,linewidth=linewidth)
ax.scatter(0.0,0.0,marker='o',facecolor=lc,linewidth=0,s=markersize)
y = np.linspace(-1,1,50)*centre_circle_radius
x = np.sqrt(centre_circle_radius**2-y**2)
ax.plot(x,y,lc,linewidth=linewidth)
ax.plot(-x,y,lc,linewidth=linewidth)
for s in signs: # plots each line seperately
# plot pitch boundary
ax.plot([-half_pitch_length,half_pitch_length],[s*half_pitch_width,s*half_pitch_width],lc,linewidth=linewidth)
ax.plot([s*half_pitch_length,s*half_pitch_length],[-half_pitch_width,half_pitch_width],lc,linewidth=linewidth)
# goal posts & line
ax.plot( [s*half_pitch_length,s*half_pitch_length],[-goal_line_width/2.,goal_line_width/2.],pc+'s',markersize=6*markersize/20.,linewidth=linewidth)
# 6 yard box
ax.plot([s*half_pitch_length,s*half_pitch_length-s*box_length],[box_width/2.,box_width/2.],lc,linewidth=linewidth)
ax.plot([s*half_pitch_length,s*half_pitch_length-s*box_length],[-box_width/2.,-box_width/2.],lc,linewidth=linewidth)
ax.plot([s*half_pitch_length-s*box_length,s*half_pitch_length-s*box_length],[-box_width/2.,box_width/2.],lc,linewidth=linewidth)
# penalty area
ax.plot([s*half_pitch_length,s*half_pitch_length-s*area_length],[area_width/2.,area_width/2.],lc,linewidth=linewidth)
ax.plot([s*half_pitch_length,s*half_pitch_length-s*area_length],[-area_width/2.,-area_width/2.],lc,linewidth=linewidth)
ax.plot([s*half_pitch_length-s*area_length,s*half_pitch_length-s*area_length],[-area_width/2.,area_width/2.],lc,linewidth=linewidth)
# penalty spot
ax.scatter(s*half_pitch_length-s*penalty_spot,0.0,marker='o',facecolor=lc,linewidth=0,s=markersize)
# corner flags
y = np.linspace(0,1,50)*corner_radius
x = np.sqrt(corner_radius**2-y**2)
ax.plot(s*half_pitch_length-s*x,-half_pitch_width+y,lc,linewidth=linewidth)
ax.plot(s*half_pitch_length-s*x,half_pitch_width-y,lc,linewidth=linewidth)
# draw the D
y = np.linspace(-1,1,50)*D_length # D_length is the chord of the circle that defines the D
x = np.sqrt(D_radius**2-y**2)+D_pos
ax.plot(s*half_pitch_length-s*x,y,lc,linewidth=linewidth)
# remove axis labels and ticks
ax.set_xticklabels([])
ax.set_yticklabels([])
ax.set_xticks([])
ax.set_yticks([])
# set axis limits
xmax = field_dimen[0]/2. + border_dimen[0]
ymax = field_dimen[1]/2. + border_dimen[1]
ax.set_xlim([-xmax,xmax])
ax.set_ylim([-ymax,ymax])
ax.set_axisbelow(True)
return fig,ax
def plot_frame( hometeam, awayteam, figax=None, team_colors=('r','b'), field_dimen = (106.0,68.0), include_player_velocities=False, PlayerMarkerSize=10, PlayerAlpha=0.7, annotate=False ):
""" plot_frame( hometeam, awayteam )
Plots a frame of Metrica tracking data (player positions and the ball) on a football pitch. All distances should be in meters.
Parameters
-----------
hometeam: row (i.e. instant) of the home team tracking data frame
awayteam: row of the away team tracking data frame
fig,ax: Can be used to pass in the (fig,ax) objects of a previously generated pitch. Set to (fig,ax) to use an existing figure, or None (the default) to generate a new pitch plot,
team_colors: Tuple containing the team colors of the home & away team. Default is 'r' (red, home team) and 'b' (blue away team)
field_dimen: tuple containing the length and width of the pitch in meters. Default is (106,68)
include_player_velocities: Boolean variable that determines whether player velocities are also plotted (as quivers). Default is False
PlayerMarkerSize: size of the individual player marlers. Default is 10
PlayerAlpha: alpha (transparency) of player markers. Defaault is 0.7
annotate: Boolean variable that determines with player jersey numbers are added to the plot (default is False)
Returrns
-----------
fig,ax : figure and aixs objects (so that other data can be plotted onto the pitch)
"""
if figax is None: # create new pitch
fig,ax = plot_pitch( field_dimen = field_dimen )
else: # overlay on a previously generated pitch
fig,ax = figax # unpack tuple
# plot home & away teams in order
for team,color in zip( [hometeam,awayteam], team_colors) :
x_columns = [c for c in team.keys() if c[-2:].lower()=='_x' and c!='ball_x'] # column header for player x positions
y_columns = [c for c in team.keys() if c[-2:].lower()=='_y' and c!='ball_y'] # column header for player y positions
ax.plot( team[x_columns], team[y_columns], color+'o', MarkerSize=PlayerMarkerSize, alpha=PlayerAlpha ) # plot player positions
if include_player_velocities:
vx_columns = ['{}_vx'.format(c[:-2]) for c in x_columns] # column header for player x positions
vy_columns = ['{}_vy'.format(c[:-2]) for c in y_columns] # column header for player y positions
ax.quiver( team[x_columns], team[y_columns], team[vx_columns], team[vy_columns], color=color, scale_units='inches', scale=10.,width=0.0015,headlength=5,headwidth=3,alpha=PlayerAlpha)
if annotate:
[ ax.text( team[x]+0.5, team[y]+0.5, x.split('_')[1], fontsize=10, color=color ) for x,y in zip(x_columns,y_columns) if not ( np.isnan(team[x]) or np.isnan(team[y]) ) ]
# plot ball
ax.plot( hometeam['ball_x'], hometeam['ball_y'], 'ko', MarkerSize=6, alpha=1.0, LineWidth=0)
return fig,ax
def save_match_clip(hometeam,awayteam, fpath, fname='clip_test', figax=None, frames_per_second=25, team_colors=('r','b'), field_dimen = (106.0,68.0), include_player_velocities=False, PlayerMarkerSize=10, PlayerAlpha=0.7):
""" save_match_clip( hometeam, awayteam, fpath )
Generates a movie from Metrica tracking data, saving it in the 'fpath' directory with name 'fname'
Parameters
-----------
hometeam: home team tracking data DataFrame. Movie will be created from all rows in the DataFrame
awayteam: away team tracking data DataFrame. The indices *must* match those of the hometeam DataFrame
fpath: directory to save the movie
fname: movie filename. Default is 'clip_test.mp4'
fig,ax: Can be used to pass in the (fig,ax) objects of a previously generated pitch. Set to (fig,ax) to use an existing figure, or None (the default) to generate a new pitch plot,
frames_per_second: frames per second to assume when generating the movie. Default is 25.
team_colors: Tuple containing the team colors of the home & away team. Default is 'r' (red, home team) and 'b' (blue away team)
field_dimen: tuple containing the length and width of the pitch in meters. Default is (106,68)
include_player_velocities: Boolean variable that determines whether player velocities are also plotted (as quivers). Default is False
PlayerMarkerSize: size of the individual player marlers. Default is 10
PlayerAlpha: alpha (transparency) of player markers. Defaault is 0.7
Returrns
-----------
fig,ax : figure and aixs objects (so that other data can be plotted onto the pitch)
"""
# check that indices match first
assert np.all( hometeam.index==awayteam.index ), "Home and away team Dataframe indices must be the same"
# in which case use home team index
index = hometeam.index
# Set figure and movie settings
FFMpegWriter = animation.writers['ffmpeg']
metadata = dict(title='Tracking Data', artist='Matplotlib', comment='Metrica tracking data clip')
writer = FFMpegWriter(fps=frames_per_second, metadata=metadata)
fname = fpath + '/' + fname + '.mp4' # path and filename
# create football pitch
if figax is None:
fig,ax = plot_pitch(field_dimen=field_dimen)
else:
fig,ax = figax
fig.set_tight_layout(True)
# Generate movie
print("Generating movie...",end='')
with writer.saving(fig, fname, 100):
for i in index:
figobjs = [] # this is used to collect up all the axis objects so that they can be deleted after each iteration
for team,color in zip( [hometeam.loc[i],awayteam.loc[i]], team_colors) :
x_columns = [c for c in team.keys() if c[-2:].lower()=='_x' and c!='ball_x'] # column header for player x positions
y_columns = [c for c in team.keys() if c[-2:].lower()=='_y' and c!='ball_y'] # column header for player y positions
objs, = ax.plot( team[x_columns], team[y_columns], color+'o', MarkerSize=PlayerMarkerSize, alpha=PlayerAlpha ) # plot player positions
figobjs.append(objs)
if include_player_velocities:
vx_columns = ['{}_vx'.format(c[:-2]) for c in x_columns] # column header for player x positions
vy_columns = ['{}_vy'.format(c[:-2]) for c in y_columns] # column header for player y positions
objs = ax.quiver( team[x_columns], team[y_columns], team[vx_columns], team[vy_columns], color=color, scale_units='inches', scale=10.,width=0.0015,headlength=5,headwidth=3,alpha=PlayerAlpha)
figobjs.append(objs)
# plot ball
objs, = ax.plot( team['ball_x'], team['ball_y'], 'ko', MarkerSize=6, alpha=1.0, LineWidth=0)
figobjs.append(objs)
# include match time at the top
frame_minute = int( team['Time [s]']/60. )
frame_second = ( team['Time [s]']/60. - frame_minute ) * 60.
timestring = "%d:%1.2f" % ( frame_minute, frame_second )
objs = ax.text(-2.5,field_dimen[1]/2.+1., timestring, fontsize=14 )
figobjs.append(objs)
writer.grab_frame()
# Delete all axis objects (other than pitch lines) in preperation for next frame
for figobj in figobjs:
figobj.remove()
print("done")
plt.clf()
plt.close(fig)
def plot_events( events, figax=None, field_dimen = (106.0,68), indicators = ['Marker','Arrow'], color='r', marker_style = 'o', alpha = 0.5, annotate=False):
""" plot_events( events )
Plots Metrica event positions on a football pitch. event data can be a single or several rows of a data frame. All distances should be in meters.
Parameters
-----------
events: row (i.e. instant) of the home team tracking data frame
fig,ax: Can be used to pass in the (fig,ax) objects of a previously generated pitch. Set to (fig,ax) to use an existing figure, or None (the default) to generate a new pitch plot,
field_dimen: tuple containing the length and width of the pitch in meters. Default is (106,68)
indicators: List containing choices on how to plot the event. 'Marker' places a marker at the 'Start X/Y' location of the event; 'Arrow' draws an arrow from the start to end locations. Can choose one or both.
color: color of indicator. Default is 'r' (red)
marker_style: Marker type used to indicate the event position. Default is 'o' (filled ircle).
alpha: alpha of event marker. Default is 0.5
annotate: Boolean determining whether text annotation from event data 'Type' and 'From' fields is shown on plot. Default is False.
Returrns
-----------
fig,ax : figure and aixs objects (so that other data can be plotted onto the pitch)
"""
if figax is None: # create new pitch
fig,ax = plot_pitch( field_dimen = field_dimen )
else: # overlay on a previously generated pitch
fig,ax = figax
for i,row in events.iterrows():
if 'Marker' in indicators:
ax.plot( row['Start X'], row['Start Y'], color+marker_style, alpha=alpha )
if 'Arrow' in indicators:
ax.annotate("", xy=row[['End X','End Y']], xytext=row[['Start X','Start Y']], alpha=alpha, arrowprops=dict(alpha=alpha,arrowstyle="->",color=color),annotation_clip=False)
if annotate:
textstring = row['Type'] + ': ' + row['From']
ax.text( row['Start X'], row['Start Y'], textstring, fontsize=10, color=color)
return fig,ax
def plot_pitchcontrol_for_event( event_id, events, tracking_home, tracking_away, PPCF, xgrid, ygrid, alpha = 0.7, include_player_velocities=True, annotate=False, field_dimen = (106.0,68)):
""" plot_pitchcontrol_for_event( event_id, events, tracking_home, tracking_away, PPCF, xgrid, ygrid )
Plots the pitch control surface at the instant of the event given by the event_id. Player and ball positions are overlaid.
Parameters
-----------
event_id: Index (not row) of the event that describes the instant at which the pitch control surface should be calculated
events: Dataframe containing the event data
tracking_home: (entire) tracking DataFrame for the Home team
tracking_away: (entire) tracking DataFrame for the Away team
PPCF: Pitch control surface (dimen (n_grid_cells_x,n_grid_cells_y) ) containing pitch control probability for the attcking team (as returned by the generate_pitch_control_for_event in Metrica_PitchControl)
xgrid: Positions of the pixels in the x-direction (field length) as returned by the generate_pitch_control_for_event in Metrica_PitchControl
ygrid: Positions of the pixels in the y-direction (field width) as returned by the generate_pitch_control_for_event in Metrica_PitchControl
alpha: alpha (transparency) of player markers. Default is 0.7
include_player_velocities: Boolean variable that determines whether player velocities are also plotted (as quivers). Default is False
annotate: Boolean variable that determines with player jersey numbers are added to the plot (default is False)
field_dimen: tuple containing the length and width of the pitch in meters. Default is (106,68)
Returrns
-----------
fig,ax : figure and aixs objects (so that other data can be plotted onto the pitch)
"""
# pick a pass at which to generate the pitch control surface
pass_frame = events.loc[event_id]['Start Frame']
pass_team = events.loc[event_id].Team
# plot frame and event
fig,ax = plot_pitch(field_color='white', field_dimen = field_dimen)
plot_frame( tracking_home.loc[pass_frame], tracking_away.loc[pass_frame], figax=(fig,ax), PlayerAlpha=alpha, include_player_velocities=include_player_velocities, annotate=annotate )
plot_events( events.loc[event_id:event_id], figax = (fig,ax), indicators = ['Marker','Arrow'], annotate=False, color= 'k', alpha=1 )
# plot pitch control surface
if pass_team=='Home':
cmap = 'bwr'
else:
cmap = 'bwr_r'
ax.imshow(np.flipud(PPCF), extent=(np.amin(xgrid), np.amax(xgrid), np.amin(ygrid), np.amax(ygrid)),interpolation='hanning',vmin=0.0,vmax=1.0,cmap=cmap,alpha=0.5)
return fig,ax