forked from Friends-of-Tracking-Data-FoTD/LaurieOnTracking
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMetrica_Velocities.py
More file actions
44 lines (34 loc) · 1.92 KB
/
Metrica_Velocities.py
File metadata and controls
44 lines (34 loc) · 1.92 KB
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Apr 6 14:52:19 2020
Module for measuring player velocities, smoothed using a Savitzky-Golay filter, with Metrica tracking data.
Data can be found at: https://github.com/metrica-sports/sample-data
@author: Laurie Shaw (@EightyFivePoint)
"""
import numpy as np
import scipy.signal as signal
def calc_player_velocities(team, filter_='Savitzky-Golay', window=7, polyorder=1, maxspeed = 10):
# Get the player ids
player_ids = np.unique( [ c[:-2] for c in team.columns if c[:4] in ['Home','Away'] ] )
# Calculate the timestep from one frame to the next. Should always be 0.04 within the same half
dt = team['Time [s]'].diff()
# index of first frame in second half
second_half_idx = team.Period.idxmax(2)
# estimate velocities for players in team
for player in player_ids: # cycle through players individually
# difference player positions in timestep dt to get unsmoothed estimate of velicity
vx = team[player+"_x"].diff() / dt
vy = team[player+"_y"].diff() / dt
# calculate first half velocity
vx.loc[:second_half_idx] = signal.savgol_filter(vx.loc[:second_half_idx],window_length=window,polyorder=polyorder)
vy.loc[:second_half_idx] = signal.savgol_filter(vy.loc[:second_half_idx],window_length=window,polyorder=polyorder)
# calculate second half velocity
vx.loc[second_half_idx:] = signal.savgol_filter(vx.loc[second_half_idx:],window_length=window,polyorder=polyorder)
vy.loc[second_half_idx:] = signal.savgol_filter(vy.loc[second_half_idx:],window_length=window,polyorder=polyorder)
# put player speed in x,y direction, and total speed back in the data frame
team[player + "_vx"] = vx
team[player + "_vy"] = vy
team[player + "_speed"] = np.sqrt( vx**2 + vy**2 )
#still need to implement a maxspeed cut-off
return team