Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]

# VS Code
.vscode/

# Jupyter NB Checkpoints
.ipynb_checkpoints/

# Mac OS-specific storage files
.DS_Store

*.profraw

# Own
data/
94 changes: 0 additions & 94 deletions Lesson4.py

This file was deleted.

143 changes: 0 additions & 143 deletions Lesson5.py

This file was deleted.

27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,30 @@ The sample data can be found in Metrica's github repository here: https://github
We'll be updating this repo as the friends of tracking series develops, adding code for measuring player velocity and acceleration, measuring team formations, and evaluating pitch control using a model published by Will Spearman.

To create movies from the tracking data you'll need to have ffmpeg installed. You can do this by following the instructions here: https://anaconda.org/conda-forge/ffmpeg (you may need to restart python afterwards).

## Notebooks
If you want to run the notebooks for each session, you can either install the repository locally or run it directly on Binder or Google Colab. In case you do not know, use Google Colab!
- Run each cell in the notebook and ignore the warning about the unknown environment.
- The data is downloaded directly from Metricas repository.
- Give it some time when it says "Readining team home" in a cell.

| Lesson | Binder | Colab |
| --- | --- | --- |
| [Lesson 4 - Basic Plotting of Event and Tracking Data](notebooks/Lesson4.ipynb) | [![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/seidlr/LaurieOnTracking/master?filepath=notebooks/Lesson4.ipynb) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/seidlr/LaurieOnTracking/blob/master/notebooks/Lesson4.ipynb) |
| [Lesson 5 - Advanced Plotting and Summary Statistics](notebooks/Lesson5.ipynb) | [![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/seidlr/LaurieOnTracking/master?filepath=notebooks/Lesson5.ipynb) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/seidlr/LaurieOnTracking/blob/master/notebooks/Lesson5.ipynb) |
| [Lesson 6 - Pitch Control](notebooks/Lesson6.ipynb) | [![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/seidlr/LaurieOnTracking/master?filepath=notebooks/Lesson6.ipynb) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/seidlr/LaurieOnTracking/blob/master/notebooks/Lesson6.ipynb) |



### Local installation
1. Download Anaconda
2. Clone repository
3. Create environment `fotd-laurie-on-tracking`:
```
conda env update
```
4. Start jupyter notebook server
```
jupyter notebook
```
5. Select the kernel `fotd-laurie-on-tracking` for your notebook
10 changes: 10 additions & 0 deletions environment.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
name: fotd-laurie-on-tracking
channels:
- conda-forge
dependencies:
- ipykernel
- numpy=1.18.1
- matplotlib=3.2.1
- pandas=1.0.3
- requests=2.23.0
- scipy=1.4.1
Empty file added friendsoftracking/__init__.py
Empty file.
41 changes: 24 additions & 17 deletions Metrica_IO.py → friendsoftracking/metrica/IO.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,39 +9,46 @@

@author: Laurie Shaw (@EightyFivePoint)
"""

import requests
import pandas as pd
import csv as csv

def read_match_data(DATADIR,gameid):
BASE_URL = 'https://raw.githubusercontent.com/metrica-sports/sample-data/master/data/'
EVENT_URL = 'Sample_Game_{game_id}/Sample_Game_{game_id}_RawEventsData.csv'
TRACKING_URL = 'Sample_Game_{game_id}/Sample_Game_{game_id}_RawTrackingData_{teamname}_Team.csv'

def read_match_data(game_id):
'''
read_match_data(DATADIR,gameid):
read_match_data(gameid):
read all Metrica match data (tracking data for home & away teams, and ecvent data)
'''
tracking_home = tracking_data(DATADIR,gameid,'Home')
tracking_away = tracking_data(DATADIR,gameid,'Away')
events = read_event_data(DATADIR,gameid)
tracking_home = read_tracking_data(game_id, 'Home')
tracking_away = read_tracking_data(game_id, 'Away')
events = read_event_data(game_id)
return tracking_home,tracking_away,events

def read_event_data(DATADIR,game_id):
def read_event_data(game_id):
'''
read_event_data(DATADIR,game_id):
read_event_data(game_id):
read Metrica event data for game_id and return as a DataFrame
'''
eventfile = '/Sample_Game_%d/Sample_Game_%d_RawEventsData.csv' % (game_id,game_id) # filename
events = pd.read_csv('{}/{}'.format(DATADIR, eventfile)) # read data
url = BASE_URL + EVENT_URL
events = pd.read_csv(url.format(game_id=game_id)) # read data
return events

def tracking_data(DATADIR,game_id,teamname):
def read_tracking_data(game_id,teamname):
'''
tracking_data(DATADIR,game_id,teamname):
read_tracking_data(game_id,teamname):
read Metrica tracking data for game_id and return as a DataFrame.
teamname is the name of the team in the filename. For the sample data this is either 'Home' or 'Away'.
'''
teamfile = '/Sample_Game_%d/Sample_Game_%d_RawTrackingData_%s_Team.csv' % (game_id,game_id,teamname)
# First: deal with file headers so that we can get the player names correct
csvfile = open('{}/{}'.format(DATADIR, teamfile), 'r') # create a csv file reader
reader = csv.reader(csvfile)

url = BASE_URL + TRACKING_URL
url = url.format(game_id=game_id, teamname=teamname)
response = requests.get(url)
decoded = response.content.decode('utf-8')

reader = csv.reader(decoded.splitlines())
teamnamefull = next(reader)[3].lower()
print("Reading team: %s" % teamnamefull)
# construct column names
Expand All @@ -53,7 +60,7 @@ def tracking_data(DATADIR,game_id,teamname):
columns[-2] = "ball_x" # column headers for the x & y positions of the ball
columns[-1] = "ball_y"
# Second: read in tracking data and place into pandas Dataframe
tracking = pd.read_csv('{}/{}'.format(DATADIR, teamfile), names=columns, index_col='Frame', skiprows=3)
tracking = pd.read_csv(url, names=columns, index_col='Frame', skiprows=3)
return tracking

def merge_tracking_data(home,away):
Expand Down
Loading