-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsackmann.py
156 lines (121 loc) · 4.14 KB
/
sackmann.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
import numpy as np
from glob import glob
from toolz import pipe, partial
import pandas as pd
from os.path import join, splitext
def get_data(
sackmann_dir,
tour="atp",
keep_davis_cup=False,
discard_retirements=True,
include_qualifying_and_challengers=False,
include_futures=False,
):
all_csvs = glob(join(sackmann_dir, f"*{tour}_matches_????.csv"))
if include_qualifying_and_challengers:
all_csvs = all_csvs + glob(
join(sackmann_dir, f"*{tour}_matches_qual_chall_????.csv")
)
if include_futures:
all_csvs = all_csvs + glob(
join(sackmann_dir, f"*{tour}_matches_futures_????.csv")
)
all_csvs = sorted(all_csvs, key=lambda x: int(splitext(x)[0][-4:]))
levels_to_drop = []
if not include_futures:
levels_to_drop.append("S")
if not include_qualifying_and_challengers:
levels_to_drop.append("C")
if not keep_davis_cup:
levels_to_drop.append("D")
data = pipe(
all_csvs,
# Read CSV
lambda y: map(partial(pd.read_csv, encoding="ISO=8859-1"), y),
# Drop NAs in important fields
lambda y: map(
lambda x: x.dropna(subset=["winner_name", "loser_name", "score"]), y
),
# Drop retirements and walkovers
# TODO: Make this optional
lambda y: map(
lambda x: x
if not discard_retirements
else x[~x["score"].astype(str).str.contains("RET|W/O|DEF|nbsp|Def.")],
y,
),
# Drop scores that appear truncated
lambda y: map(lambda x: x[x["score"].astype(str).str.len() > 4], y),
# Drop challengers and futures
# TODO: Make this optional too
lambda y: map(lambda x: x[~x["tourney_level"].isin(levels_to_drop)], y),
pd.concat,
)
round_numbers = {
"R128": 1,
"RR": 1,
"R64": 2,
"R32": 3,
"R16": 4,
"QF": 5,
"SF": 6,
"F": 7,
}
# Drop rounds outside this list
to_keep = data["round"].isin(round_numbers)
data = data[to_keep]
# Add a numerical round number
data["round_number"] = data["round"].replace(round_numbers)
# Add date information
data["tourney_date"] = pd.to_datetime(
data["tourney_date"].astype(int).astype(str), format="%Y%m%d"
)
data["year"] = data["tourney_date"].dt.year
# Sort by date and round and reset index
data = data.sort_values(["tourney_date", "round_number"])
data = data.reset_index(drop=True)
data["pts_won_serve_winner"] = data["w_1stWon"] + data["w_2ndWon"]
data["pts_won_serve_loser"] = data["l_1stWon"] + data["l_2ndWon"]
data["pts_played_serve_winner"] = data["w_svpt"]
data["pts_played_serve_loser"] = data["l_svpt"]
# Add serve % won
data["spw_winner"] = (data["w_1stWon"] + data["w_2ndWon"]) / data["w_svpt"]
data["spw_loser"] = (data["l_1stWon"] + data["l_2ndWon"]) / data["l_svpt"]
data["spw_margin"] = data["spw_winner"] - data["spw_loser"]
return data
def compute_game_margins(string_scores):
def compute_margin(sample_set):
if "[" in sample_set:
return 0
try:
split_set = sample_set.split("-")
margin = int(split_set[0]) - int(split_set[1].split("(")[0])
except ValueError:
margin = np.nan
return margin
margins = pipe(
string_scores,
lambda y: map(lambda x: x.split(" "), y),
lambda y: map(lambda x: [compute_margin(z) for z in x], y),
lambda y: map(sum, y),
partial(np.fromiter, dtype=np.float),
)
return margins
def get_player_info(sackmann_dir, tour="atp"):
player_info = pd.read_csv(
join(sackmann_dir, f"{tour}_players.csv"),
header=None,
names=[
"ID",
"First Name",
"Last Name",
"Handedness",
"Birthdate",
"Nationality",
],
)
player_info = player_info.dropna()
str_date = player_info["Birthdate"].astype(int).astype(str)
str_date = pd.to_datetime(str_date, format="%Y%m%d")
player_info["Birthdate"] = str_date
return player_info