-
Notifications
You must be signed in to change notification settings - Fork 3
/
README.Rmd
151 lines (113 loc) · 4.35 KB
/
README.Rmd
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
---
title: "ngscleanR"
output: github_document
---
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.width=9,
fig.height=5,
tidy = 'styler'
)
```
`ngscleanR` is a set of functions to clean up and standardize NFL player tracking data. The package handles some of the necessary, but boring, parts of dealing with player tracking data. The included functions:
* **`clean_and_rotate()`**: Makes all plays go from left to right, append some play information from `nflfastR` (yard line, play description, play type, etc), and add some post-standardized information about where the player is moving and facing (e.g., `s_x`, `s_y`, `o_x`, `o_y`, etc)
* **`compute_o_diff()`**: Computes difference in orientation between direction player is currently facing and
#' orientation if player were facing towards a given x and y location. For example, this could be used to determine the extent to which a player is facing towards the quarterback on a given frame.
* **`cut_plays()`** Trim frames for a play and/or remove plays based on how quickly provided events happen in the play. For example, this could be used to remove frames after a pass was thrown or discard plays where a pass is thrown very quickly.
* **`prepare_bdb_week()`**: A wrapper around the above three functions that cleans the raw data from the 2021 Big Data Bowl (2018 season).
* **`plot_play()`**: A wrapper around `ggplot` and `gganimate` for plotting a play.
## Installation
Install from github using:
```{r, eval = FALSE}
if (!require("remotes")) install.packages("remotes")
remotes::install_github("guga31bb/ngscleanR")
```
## Usage
First we load the necessary packages (`patchwork` is for the plot at the end).
```{r setup, message=FALSE}
library(ngscleanR)
library(tidyverse)
library(patchwork)
```
## Load sample week
To demonstrate the package features, we start by loading some small sample data stored in the package github repo that come from 2021 Big Data Bowl:
```{r}
tracking <- readRDS("data-raw/sample_bdb_2021.rds")
names(tracking)
```
### The main function
This will clean up the data, attach some information associated with the play, and make everything face from left to right.
```{r}
cleaned <- tracking %>%
clean_and_rotate()
names(cleaned)
```
### Play cutting function
This discards any plays where the throw happens before frame 25 (i.e. 1.5 seconds into the play). In addition, it removes any frames that took place more than 10 frames after a pass was thrown or some other play ending event (sack, fumble, etc).
```{r}
cleaned <- cleaned %>%
cut_plays(
# get rid of plays with throw before this frame
throw_frame = 25,
# get rid of frames that happen after this many frames after pass released
time_after_event = 10
)
names(cleaned)
```
### Plot some sample plays
Here is a demonstration of the `plot_play` function on some still frames:
```{r plots, warning = FALSE, message = FALSE, results = 'hide', fig.keep = 'all', dpi = 400, layout="l-body-outset"}
ex <- sample(cleaned$play, 4)
plots <- map(ex, ~{
plot <- cleaned %>%
filter(play == .x) %>%
plot_play(
# show still frame, not animation
animated = FALSE,
# just plot this frame_id
frame = 28,
segment_length = 6,
segment_size = 3,
dot_size = 4
)
plot +
theme(plot.title = element_blank(),
plot.caption = element_blank(),
plot.margin = unit(c(0, 0, 0, 0), "cm")
)
})
(plots[[1]] + plots[[2]]) / (plots[[3]] + plots[[4]])
```
Or we can animate a play:
```{r gif, warning = FALSE, message = FALSE, fig.keep = 'all', dpi = 400, layout="l-body-outset"}
ex <- sample(cleaned$play, 1)
plot <- cleaned %>%
filter(play == ex) %>%
plot_play(
# show still frame, not animation
animated = TRUE,
# just plot this frame_id
segment_length = 6,
segment_size = 3,
dot_size = 4,
animated_h = 4,
animated_w = 8,
animated_res = 150
)
plot
```
### The big cleaning function
And the wrapper that can be used to prepare raw 2021 Big Data Bowl data. See [this Open Source Football post](https://www.opensourcefootball.com/posts/2021-05-31-computer-vision-in-r-using-torch/) for how it might be useful.
```{r}
prepare_bdb_week(
week = 1,
dir = "../nfl-big-data-bowl-2021/input",
trim_frame = 25,
frames_after_throw = 10,
keep_frames = c(30),
drop_positions = c("QB")
) %>%
str()
```