-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathanimator.py
196 lines (170 loc) · 6.77 KB
/
animator.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
# Modified from: https://github.com/linouk23/NBA-Player-Movements.
import matplotlib.pyplot as plt
import numpy as np
import pickle
from matplotlib import animation
from settings import *
class Game:
def __init__(self, data_dir, games_dir, gameid):
baller2vec_config = pickle.load(
open(f"{data_dir}/baller2vec_config.pydict", "rb")
)
self.player_idx2props = baller2vec_config["player_idx2props"]
event2event_idx = baller2vec_config["event2event_idx"]
self.event_idx2event = {
event_idx: event for (event, event_idx) in event2event_idx.items()
}
X = np.load(f"{games_dir}/{gameid}_X.npy")
self.y = np.load(f"{games_dir}/{gameid}_y.npy")
self.periods = X[:, 3].astype(int)
self.period_times = X[:, 1]
self.shot_clocks = X[:, 2]
self.left_scores = X[:, 4].astype(int)
self.right_scores = X[:, 5].astype(int)
self.player_idxs = X[:, 10:20].astype(int)
self.ball_xs = X[:, 7]
self.ball_ys = X[:, 8]
self.ball_zs = X[:, 9]
self.player_xs = X[:, 20:30]
self.player_ys = X[:, 30:40]
self.player_hoop_sides = X[:, 40:50]
def update_radius(
self,
i,
start,
player_circles,
ball_circle,
annotations,
clock_info,
player_idx2circle_idx,
):
time_step = start + i
for (idx, player_idx) in enumerate(self.player_idxs[time_step]):
circle_idx = player_idx2circle_idx[player_idx]
player_circles[circle_idx].center = (
self.player_xs[time_step, idx],
Y_MAX - self.player_ys[time_step, idx],
)
annotations[circle_idx].set_position(player_circles[circle_idx].center)
name = self.player_idx2props[player_idx]["name"].split()
initials = name[0][0] + name[1][0]
annotations[circle_idx].set_text(initials)
if self.player_hoop_sides[time_step, idx]:
player_circles[circle_idx].set_facecolor("white")
player_circles[circle_idx].set_edgecolor("white")
else:
player_circles[circle_idx].set_facecolor("gray")
player_circles[circle_idx].set_edgecolor("gray")
clock_str = f"{self.periods[time_step]}/"
period_time = int(720 - self.period_times[time_step])
clock_str += f"{period_time // 60:02}:{period_time % 60:02}/"
clock_str += f"{self.shot_clocks[time_step]:03.1f}/"
clock_str += f"{self.left_scores[time_step]} - {self.right_scores[time_step]}/"
clock_str += self.event_idx2event[self.y[time_step]]
clock_info.set_text(clock_str)
ball_circle.center = (self.ball_xs[time_step], Y_MAX - self.ball_ys[time_step])
ball_circle.radius = self.ball_zs[time_step] / NORMALIZATION_COEF
return (player_circles, ball_circle)
def show_seq(
self, start_period, start_time, stop_period, stop_time, save_gif=False
):
# Leave some space for inbound passes.
ax = plt.axes(xlim=(X_MIN, X_MAX), ylim=(Y_MIN, Y_MAX))
ax.axis("off")
fig = plt.gcf()
# Remove grid.
ax.grid(False)
clock_info = ax.annotate(
"",
xy=[X_CENTER, Y_CENTER + 2],
color="black",
horizontalalignment="center",
verticalalignment="center",
)
(start_min, start_sec) = start_time.split(":")
(start_min, start_sec) = (int(start_min), int(start_sec))
period_time = 720 - (60 * start_min + start_sec)
start = np.argwhere(
(self.period_times > period_time) & (self.periods == start_period)
).min()
(stop_min, stop_sec) = stop_time.split(":")
(stop_min, stop_sec) = (int(stop_min), int(stop_sec))
period_time = 720 - (60 * stop_min + stop_sec)
stop = np.argwhere(
(self.period_times > period_time) & (self.periods == stop_period)
).min()
player_idxs = set(self.player_idxs[start])
for time_step in range(start + 1, stop):
# End sequence early at lineup change.
if len(player_idxs & set(self.player_idxs[time_step])) != 10:
stop = time_step
break
annotations = []
player_idx2circle_idx = {}
team_a_players = []
team_b_players = []
player_circles = []
for (circle_idx, player_idx) in enumerate(self.player_idxs[start]):
player_idx2circle_idx[player_idx] = circle_idx
name = self.player_idx2props[player_idx]["name"].split()
initials = name[0][0] + name[1][0]
annotations.append(
ax.annotate(
initials,
xy=[0, 0],
color="black",
horizontalalignment="center",
verticalalignment="center",
fontweight="bold",
)
)
if self.player_hoop_sides[start, circle_idx]:
team_a_players.append(
self.player_idx2props[player_idx]["name"] + f": {initials}"
)
player_circles.append(
plt.Circle((0, 0), PLAYER_CIRCLE_SIZE, color="white")
)
else:
team_b_players.append(
self.player_idx2props[player_idx]["name"] + f": {initials}"
)
player_circles.append(
plt.Circle((0, 0), PLAYER_CIRCLE_SIZE, color="gray")
)
# Prepare table.
column_labels = tuple(["Team A", "Team B"])
players_data = list(zip(team_a_players, team_b_players))
table = plt.table(
cellText=players_data,
colLabels=column_labels,
colWidths=[COL_WIDTH, COL_WIDTH],
loc="bottom",
fontsize=FONTSIZE,
cellLoc="center",
)
table.scale(1, SCALE)
# Add animated objects.
for circle in player_circles:
ax.add_patch(circle)
ball_circle = plt.Circle((0, 0), PLAYER_CIRCLE_SIZE, color=BALL_COLOR)
ax.add_patch(ball_circle)
anim = animation.FuncAnimation(
fig,
self.update_radius,
fargs=(
start,
player_circles,
ball_circle,
annotations,
clock_info,
player_idx2circle_idx,
),
frames=stop - start,
interval=INTERVAL,
)
court = plt.imread("court.png")
plt.imshow(court, zorder=0, extent=[X_MIN, X_MAX - DIFF, Y_MAX, Y_MIN])
if save_gif:
anim.save("animation.mp4", writer="imagemagick", fps=25)
plt.show()