-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCombineDataScript.py
More file actions
183 lines (141 loc) · 5.62 KB
/
CombineDataScript.py
File metadata and controls
183 lines (141 loc) · 5.62 KB
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
import sys
import os
import pandas as pd
import re
def parse_game_line(line):
# Extract date explicitly
date_match = re.match(r"\s*(\d{1,2}/\d{1,2}/\d{4})", line)
if not date_match:
return None
date = date_match.group(1)
rest = line[date_match.end():]
# Find remaining integers (scores)
matches = list(re.finditer(r"\b\d+\b", rest))
if len(matches) < 2:
return None
team1_score = int(matches[-2].group())
team2_score = int(matches[-1].group())
# Slice team names
team1 = rest[:matches[-2].start()].strip()
team2 = rest[matches[-2].end():matches[-1].start()].strip()
if team1_score > team2_score:
team1_result = "W"
team2_result = "L"
else:
team1_result = "L"
team2_result = "W"
return {
"date": date,
"team1": team1,
"team1_score": team1_score,
"team1_result": team1_result,
"team2": team2,
"team2_score": team2_score,
"team2_result": team2_result
}
def game_in_first_half(team, date, dates_dict):
N = len(dates_dict[team])
k = dates_dict[team].index(date) + 1
midpoint = (N + 1) / 2
if k < midpoint:
return -1
elif k == midpoint:
return 0
else:
return 1
YEAR = 2026
stats_df = pd.read_csv(
f"PreTournamentStats/{YEAR}.csv",
encoding="utf-8-sig"
)
num_teams = len(stats_df)
gameDict = {}
records = []
with open(f"PreTournamentGameLogs/{YEAR}.txt", encoding="utf-8", errors="ignore") as f:
for line in f:
record = parse_game_line(line)
if record is not None:
records.append(record)
games_df = pd.DataFrame(records)
stats_df.set_index("Team", inplace=True)
for i, row in games_df.iterrows():
team1 = row["team1"]
team2 = row["team2"]
# Initialize empty dictionary to hold new features
new_features = {}
if team1 in stats_df.index:
new_features["team1_rank"] = stats_df.loc[team1, "Rk"]
if team2 in stats_df.index:
new_features["team2_rank"] = stats_df.loc[team2, "Rk"]
# Append these to DataFrame
for key, value in new_features.items():
games_df.at[i, key] = value
games_df = games_df[games_df["team1_rank"].notna() & games_df["team2_rank"].notna()]
dates_dict = {}
for i, row in games_df.iterrows():
team1 = row["team1"]
team2 = row["team2"]
if team1 not in dates_dict:
dates_dict[team1] = []
if team2 not in dates_dict:
dates_dict[team2] = []
dates_dict[team1].append(row["date"])
dates_dict[team2].append(row["date"])
# stats_df["AdjEM"] = stats_df["AdjOE"] - stats_df["AdjDE"]
# stats_df["EFG%M"] = stats_df["EFG%"] - stats_df["EFGD%"]
# stats_df["TORM"] = stats_df["TOR"] - stats_df["TORD"]
# stats_df["FTRM"] = stats_df["FTR"] - stats_df["FTRD"]
# stats_df["TRB"] = stats_df["ORB"] + stats_df["DRB"]
# stats_df["FTRM"] = stats_df["FTR"] - stats_df["FTRD"]
# stats_df["3P%M"] = stats_df["3P%"] - stats_df["3P%D"]
# stats_df["2P%M"] = stats_df["2P%"] - stats_df["2P%D"]
# stats_df["3PRM"] = stats_df["3PR"] - stats_df["3PRD"]
stats_df["First_Half_Potential"] = 0.0
stats_df["Second_Half_Potential"] = 0.0
for i, row in games_df.iterrows():
team1 = row["team1"]
team2 = row["team2"]
team1_potential_change = (num_teams - stats_df.loc[team2, "Rk"]) / num_teams
team2_potential_change = (num_teams - stats_df.loc[team1, "Rk"]) / num_teams
if game_in_first_half(team1, row["date"], dates_dict) == -1:
stats_df.loc[team1, "First_Half_Potential"] += team1_potential_change
elif game_in_first_half(team1, row["date"], dates_dict) == 1:
stats_df.loc[team1, "Second_Half_Potential"] += team1_potential_change
if game_in_first_half(team2, row["date"], dates_dict) == -1:
stats_df.loc[team2, "First_Half_Potential"] += team2_potential_change
elif game_in_first_half(team2, row["date"], dates_dict) == 1:
stats_df.loc[team2, "Second_Half_Potential"] += team2_potential_change
stats_df["First_Half_Momentum"] = 0.0
stats_df["Second_Half_Momentum"] = 0.0
for i, row in games_df.iterrows():
team1 = row["team1"]
team2 = row["team2"]
score1 = row["team1_score"]
score2 = row["team2_score"]
# Determine winner and loser
if score1 > score2:
winner = team1
loser = team2
elif score2 > score1:
winner = team2
loser = team1
# Get ranks
winner_rank = stats_df.loc[winner, "Rk"]
loser_rank = stats_df.loc[loser, "Rk"]
# Compute rating changes
loser_change = (winner_rank - 1) / num_teams
winner_change = (num_teams - loser_rank) / num_teams
if game_in_first_half(winner, row["date"], dates_dict) == -1:
stats_df.loc[winner, "First_Half_Momentum"] += winner_change
elif game_in_first_half(winner, row["date"], dates_dict) == 1:
stats_df.loc[winner, "Second_Half_Momentum"] += winner_change
if game_in_first_half(loser, row["date"], dates_dict) == -1:
stats_df.loc[loser, "First_Half_Momentum"] -= loser_change
elif game_in_first_half(loser, row["date"], dates_dict) == 1:
stats_df.loc[loser, "Second_Half_Momentum"] -= loser_change
stats_df["Momentum"] = (stats_df["Second_Half_Momentum"]/stats_df["Second_Half_Potential"]) - (stats_df["First_Half_Momentum"]/stats_df["First_Half_Potential"])
print(games_df)
print(stats_df['Momentum'])
os.makedirs("Momentum", exist_ok=True)
stats_df = stats_df.reset_index()[['Team', 'Momentum']]
stats_df.to_csv(f"Momentum/{YEAR}.csv", index=False)