-
Notifications
You must be signed in to change notification settings - Fork 132
/
xthreat.py
257 lines (198 loc) · 9.22 KB
/
xthreat.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
import numpy as np # type: ignore
import pandas as pd # type: ignore
import warnings # type: ignore
from typing import Tuple, List, Callable
import socceraction.spadl.config as spadlconfig
M : int = 12
N: int = 16
def _get_cell_indexes(
x: pd.Series, y: pd.Series, l: int = N, w: int = M
) -> Tuple[pd.Series, pd.Series]:
xmin = 0
ymin = 0
xi = (x - xmin) / spadlconfig.field_length * l
yj = (y - ymin) / spadlconfig.field_width * w
xi = xi.astype(int).clip(0, l - 1)
yj = yj.astype(int).clip(0, w - 1)
return xi, yj
def _get_flat_indexes(x: pd.Series, y: pd.Series, l: int = N, w: int = M) -> pd.Series:
xi, yj = _get_cell_indexes(x, y, l, w)
return l * (w - 1 - yj) + xi
def _count(x: pd.Series, y: pd.Series, l: int = N, w: int = M) -> np.ndarray:
""" Count the number of actions occurring in each cell of the grid.
:param x: The x-coordinates of the actions.
:param y: The y-coordinates of the actions.
:param l: Amount of grid cells in the x-dimension of the grid.
:param w: Amount of grid cells in the y-dimension of the grid.
:return: A matrix, denoting the amount of actions occurring in each cell. The top-left corner is the origin.
"""
x = x[~np.isnan(x) & ~np.isnan(y)]
y = y[~np.isnan(x) & ~np.isnan(y)]
flat_indexes = _get_flat_indexes(x, y, l, w)
vc = flat_indexes.value_counts(sort=False)
vector = np.zeros(w * l)
vector[vc.index] = vc
return vector.reshape((w, l))
def safe_divide(a: np.ndarray, b: np.ndarray) -> np.ndarray:
return np.divide(a, b, out=np.zeros_like(a), where=b != 0)
def scoring_prob(actions: pd.DataFrame, l: int = N, w: int = M) -> np.ndarray:
""" Compute the probability of scoring when taking a shot for each cell.
:param actions: Actions, in SPADL format.
:param l: Amount of grid cells in the x-dimension of the grid.
:param w: Amount of grid cells in the y-dimension of the grid.
:return: A matrix, denoting the probability of scoring for each cell.
"""
shot_actions = actions[(actions.type_name == "shot")]
goals = shot_actions[(shot_actions.result_name == "success")]
shotmatrix = _count(shot_actions.start_x, shot_actions.start_y, l, w)
goalmatrix = _count(goals.start_x, goals.start_y, l, w)
return safe_divide(goalmatrix, shotmatrix)
def get_move_actions(actions: pd.DataFrame) -> pd.DataFrame:
return actions[
(actions.type_name == "pass")
| (actions.type_name == "dribble")
| (actions.type_name == "cross")
]
def get_successful_move_actions(actions: pd.DataFrame) -> pd.DataFrame:
move_actions = get_move_actions(actions)
return move_actions[move_actions.result_name == "success"]
def action_prob(
actions: pd.DataFrame, l: int = N, w: int = M
) -> Tuple[np.ndarray, np.ndarray]:
""" Compute the probability of taking an action in each cell of the grid. The options are: shooting or moving.
:param actions: Actions, in SPADL format.
:param l: Amount of grid cells in the x-dimension of the grid.
:param w: Amount of grid cells in the y-dimension of the grid.
:return: 2 matrices, denoting for each cell the probability of choosing to shoot
and the probability of choosing to move.
"""
move_actions = get_move_actions(actions)
shot_actions = actions[(actions.type_name == "shot")]
movematrix = _count(move_actions.start_x, move_actions.start_y, l, w)
shotmatrix = _count(shot_actions.start_x, shot_actions.start_y, l, w)
totalmatrix = movematrix + shotmatrix
return safe_divide(shotmatrix, totalmatrix), safe_divide(movematrix, totalmatrix)
def move_transition_matrix(
actions: pd.DataFrame, l: int = N, w: int = M
) -> Tuple[np.ndarray, np.ndarray]:
""" Compute the move transition matrix from the given actions.
This is, when a player chooses to move, the probability that he will
end up in each of the other cells of the grid successfully.
:param actions: Actions, in SPADL format.
:param l: Amount of grid cells in the x-dimension of the grid.
:param w: Amount of grid cells in the y-dimension of the grid.
:return: The transition matrix.
"""
move_actions = get_move_actions(actions)
X = pd.DataFrame()
X["start_cell"] = _get_flat_indexes(
move_actions.start_x, move_actions.start_y, l, w
)
X["end_cell"] = _get_flat_indexes(move_actions.end_x, move_actions.end_y, l, w)
X["result_name"] = move_actions.result_name
vc = X.start_cell.value_counts(sort=False)
start_counts = np.zeros(w * l)
start_counts[vc.index] = vc
transition_matrix = np.zeros((w * l, w * l))
for i in range(0, w * l):
vc2 = X[
((X.start_cell == i) & (X.result_name == "success"))
].end_cell.value_counts(sort=False)
transition_matrix[i, vc2.index] = vc2 / start_counts[i]
return transition_matrix
class ExpectedThreat:
"""An implementation of Karun Singh's Expected Threat model (https://karun.in/blog/expected-threat.html)."""
def __init__(self, l: int = N, w: int = M, eps: float = 1e-5):
self.l = l
self.w = w
self.eps = eps
self.heatmaps: List[np.ndarray] = []
self.xT: np.ndarray = np.zeros((w, l))
self.scoring_prob_matrix: np.ndarray = np.zeros((w, l))
self.shot_prob_matrix: np.ndarray = np.zeros((w, l))
self.move_prob_matrix: np.ndarray = np.zeros((w, l))
self.transition_matrix: np.ndarray = np.zeros((w * l, w * l))
def __solve(
self,
p_scoring: np.ndarray,
p_shot: np.ndarray,
p_move: np.ndarray,
transition_matrix: np.ndarray,
) -> None:
"""Solves the expected threat equation with dynamic programming.
:param p_scoring (matrix, shape(M, N)): Probability of scoring at each grid cell, when shooting from that cell.
:param p_shot (matrix, shape(M,N)): For each grid cell, the probability of choosing to shoot from there.
:param p_move (matrix, shape(M,N)): For each grid cell, the probability of choosing to move from there.
:param transition_matrix (matrix, shape(M*N,M*N)): When moving, the probability of moving to each of the other zones.
"""
gs = p_scoring * p_shot
diff = 1
it = 0
self.heatmaps.append(self.xT.copy())
while np.any(diff > self.eps):
total_payoff = np.zeros((self.w, self.l))
for y in range(0, self.w):
for x in range(0, self.l):
for q in range(0, self.w):
for z in range(0, self.l):
total_payoff[y, x] += (
transition_matrix[self.l * y + x, self.l * q + z]
* self.xT[q, z]
)
newxT = gs + (p_move * total_payoff)
diff = newxT - self.xT
self.xT = newxT
self.heatmaps.append(self.xT.copy())
it += 1
print("# iterations: ", it)
def fit(self, actions: pd.DataFrame):
""" Fits the xT model with the given actions.
:param actions: Actions, in SPADL format.
"""
self.scoring_prob_matrix = scoring_prob(actions, self.l, self.w)
self.shot_prob_matrix, self.move_prob_matrix = action_prob(
actions, self.l, self.w
)
self.transition_matrix = move_transition_matrix(actions, self.l, self.w)
self.__solve(
self.scoring_prob_matrix,
self.shot_prob_matrix,
self.move_prob_matrix,
self.transition_matrix,
)
return self
def interpolator(
self, kind: str = "linear"
) -> Callable[[np.ndarray, np.ndarray], np.ndarray]:
from scipy.interpolate import interp2d # type: ignore
cell_length = spadlconfig.field_length / self.l
cell_width = spadlconfig.field_width / self.w
x = np.arange(0.0, spadlconfig.field_length, cell_length) + 0.5 * cell_length
y = np.arange(0.0, spadlconfig.field_width, cell_width) + 0.5 * cell_width
return interp2d(x=x, y=y, z=self.xT, kind=kind, bounds_error=False)
def predict(
self, actions: pd.DataFrame, use_interpolation: bool = True
) -> pd.Series:
""" Predicts the xT values for the given actions.
:param actions: Actions, in SPADL format.
:param use_interpolation: Indicates whether to use bilinear interpolation when inferring xT values.
:return: Each action, including its xT value.
"""
if not use_interpolation:
l = self.l
w = self.w
grid = self.xT
else:
# Use interpolation to create a
# more fine-grained 1050 x 680 grid
interp = self.interpolator()
l = int(spadlconfig.field_length * 10)
w = int(spadlconfig.field_width * 10)
xs = np.linspace(0, spadlconfig.field_length, l)
ys = np.linspace(0, spadlconfig.field_width, w)
grid = interp(xs, ys)
startxc, startyc = _get_cell_indexes(actions.start_x, actions.start_y, l, w)
endxc, endyc = _get_cell_indexes(actions.end_x, actions.end_y, l, w)
xT_start = grid[w - 1 - startyc, startxc]
xT_end = grid[w - 1 - endyc, endxc]
return xT_end - xT_start