-
Notifications
You must be signed in to change notification settings - Fork 0
/
combine_depricated.py
241 lines (187 loc) · 7.15 KB
/
combine_depricated.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
import os
import glob
from tqdm import tqdm
import pandas as pd
# import numpy as np
from multiprocessing import Pool
def reader(filename):
return pd.read_csv(filename, encoding="latin-1")
def mergeFilesMultithreaded(filePath=""):
# global filecount
# filecount = 0
num_cpus = os.cpu_count()
print(f"{num_cpus} cpu cores advalible to this script.")
pool = Pool(num_cpus - 1)
main_df = pd.DataFrame()
l = filePath
file_list = glob.iglob(l + "/*csv")
file_list = list(file_list)
df_list = pool.map(reader, tqdm(file_list))
main_df = pd.concat(df_list)
return main_df
def mergeFiles(filePath=""):
main_df = pd.DataFrame()
f = 0
l = filePath
file_list = glob.iglob(l + "/*csv")
for file in file_list:
f += 1
# with open('filelist.txt','w+',encoding='utf-8') as f:
# f.write(str(file_list))
for file in tqdm(glob.iglob(l + "/*csv"), total=f):
# len_file = len(file)
# if os.stat(file).st_size == 0:
# print(f'{file} is empty')
# else:
df = pd.read_csv(file)
main_df = pd.concat([main_df, df], ignore_index=True)
# main_df = pd.concat([pd.read_csv(f) for f in file_list])
return main_df
def mergeBattingLogs():
f = "PlayerStats/Batting"
df = mergeFilesMultithreaded(f)
df.to_csv("PlayerStats/batting_logs.csv", index=False)
def mergePitchingLogs():
f = "PlayerStats/Pitching"
df = mergeFilesMultithreaded(f)
df.to_csv("PlayerStats/pitching_logs.csv", index=False)
def mergeFieldingLogs():
f = "PlayerStats/Fielding"
df = mergeFilesMultithreaded(f)
df.to_csv("PlayerStats/fielding_logs.csv", index=False)
def mergeRosters():
f = "TeamRosters/teams"
df = mergeFilesMultithreaded(f)
df.to_csv("TeamRosters/rosters.csv", index=False)
def pbpReader(filename):
game_id = str(os.path.basename(filename)).replace(".csv", "")
df = pd.read_csv(filename, encoding="latin-1")
df["game_id"] = game_id
return df
def splitRosters():
print("Reading the rosters file.")
df = pd.read_csv("TeamRosters/rosters.csv")
print("Done!\n")
max_season = df["season"].max()
min_season = df["season"].min()
for i in range(min_season, max_season + 1):
print(f"Creating the roster file for the {i} season.")
s_df = df[df["season"] == i]
# len_s_df = len(s_df)
# len_s_df = len_s_df // 2
# partOne = s_df.iloc[:len_s_df,:]
# partTwo = s_df.iloc[len_s_df:,:]
# partOne.to_csv(f'TeamRosters/{i}_roster_01.csv',index=False)
# partTwo.to_csv(f'TeamRosters/{i}_roster_02.csv',index=False)
s_df.to_csv(f"TeamRosters/{i}_roster.csv", index=False)
def mergePbpMultithreaded(filePath=""):
# global filecount
# filecount = 0
num_cpus = os.cpu_count()
print(f"{num_cpus} cpu cores advalible to this script.")
pool = Pool(num_cpus - 1)
main_df = pd.DataFrame()
l = filePath
file_list = glob.iglob(l + "/*csv")
file_list = list(file_list)
df_list = pool.map(pbpReader, tqdm(file_list))
main_df = pd.concat(df_list)
return main_df
def mergePbpLogs():
f = "pbp/games"
df = mergePbpMultithreaded(f)
df.to_csv("pbp/pbp_logs.csv", index=False)
def splitBattingStats():
print("Reading the batting logs file.")
df = pd.read_csv("PlayerStats/batting_logs.csv")
print("Done!\n")
max_season = df["season"].max()
min_season = df["season"].min()
for i in range(min_season, max_season + 1):
print(f"Creating batting logs for the {i} season.")
s_df = df[df["season"] == i]
len_s_df = len(s_df)
len_s_df = len_s_df // 4
partOne = s_df.iloc[:len_s_df]
partTwo = s_df.iloc[len_s_df : 2 * len_s_df]
partThree = s_df.iloc[2 * len_s_df : 3 * len_s_df]
partFour = s_df.iloc[3 * len_s_df :]
partOne.to_csv(f"PlayerStats/{i}_batting_01.csv", index=False)
partTwo.to_csv(f"PlayerStats/{i}_batting_02.csv", index=False)
partThree.to_csv(f"PlayerStats/{i}_batting_03.csv", index=False)
partFour.to_csv(f"PlayerStats/{i}_batting_04.csv", index=False)
# s_df.to_csv(f'PlayerStats/{i}_batting.csv')
def splitPitchingStats():
print("Reading the pitching logs file.")
df = pd.read_csv("PlayerStats/pitching_logs.csv")
print("Done!\n")
max_season = df["season"].max()
min_season = df["season"].min()
for i in range(min_season, max_season + 1):
print(f"Creating pitching logs for the {i} season.")
s_df = df[df["season"] == i]
len_s_df = len(s_df)
len_s_df = len_s_df // 2
partOne = s_df.iloc[:len_s_df, :]
partTwo = s_df.iloc[len_s_df:, :]
partOne.to_csv(f"PlayerStats/{i}_pitching_01.csv", index=False)
partTwo.to_csv(f"PlayerStats/{i}_pitching_02.csv", index=False)
# s_df.to_csv(f'PlayerStats/{i}_batting.csv')
def splitFieldingStats():
print("Reading the fielding logs file.")
df = pd.read_csv("PlayerStats/fielding_logs.csv")
print("Done!\n")
max_season = df["season"].max()
min_season = df["season"].min()
for i in range(min_season, max_season + 1):
print(f"Creating fielding logs for the {i} season.")
s_df = df[df["season"] == i]
len_s_df = len(s_df)
len_s_df = len_s_df // 4
partOne = s_df.iloc[:len_s_df]
partTwo = s_df.iloc[len_s_df : 2 * len_s_df]
partThree = s_df.iloc[2 * len_s_df : 3 * len_s_df]
partFour = s_df.iloc[3 * len_s_df :]
partOne.to_csv(f"PlayerStats/{i}_fielding_01.csv", index=False)
partTwo.to_csv(f"PlayerStats/{i}_fielding_02.csv", index=False)
partThree.to_csv(f"PlayerStats/{i}_fielding_03.csv", index=False)
partFour.to_csv(f"PlayerStats/{i}_fielding_04.csv", index=False)
def splitPbpLogs():
print("Reading the play-by-play logs file.")
df = pd.read_csv("pbp/pbp_logs.csv")
print("Done!\n")
df["season"] = pd.DatetimeIndex(df["date"]).year
max_season = df["season"].max()
min_season = df["season"].min()
for i in range(min_season, max_season + 1):
print(f"Creating play-by-play logs for the {i} season.")
s_df = df[df["season"] == i]
len_s_df = len(s_df)
len_s_df = len_s_df // 4
partOne = s_df.iloc[:len_s_df]
partTwo = s_df.iloc[len_s_df : 2 * len_s_df]
partThree = s_df.iloc[2 * len_s_df : 3 * len_s_df]
partFour = s_df.iloc[3 * len_s_df :]
partOne.to_csv(f"pbp/{i}_pbp_01.csv", index=False)
partTwo.to_csv(f"pbp/{i}_pbp_02.csv", index=False)
partThree.to_csv(f"pbp/{i}_pbp_03.csv", index=False)
partFour.to_csv(f"pbp/{i}_pbp_04.csv", index=False)
def main():
print("Starting Up...")
mergeBattingLogs()
mergePitchingLogs()
mergeFieldingLogs()
mergePbpLogs()
mergeRosters()
splitBattingStats()
splitPitchingStats()
splitFieldingStats()
splitPbpLogs()
splitRosters()
os.remove("PlayerStats/batting_logs.csv")
os.remove("PlayerStats/pitching_logs.csv")
os.remove("PlayerStats/fielding_logs.csv")
os.remove("pbp/pbp_logs.csv")
os.remove("TeamRosters/rosters.csv")
if __name__ == "__main__":
main()