-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
187 lines (157 loc) · 7.42 KB
/
main.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
from multiprocessing import Pool
from file_functions import *
import os
import yaml
import shutil
from dataframe_functions import *
from datetime import datetime
import pytz
import warnings
warnings.filterwarnings('ignore')
def process_game(game):
file_types = {'meta': ['key', 'data_version', 'created', 'revision'],
'toss': ['key', 'toss_winner', 'toss_decision'],
'team': ['key', 'team'],
'umpires': ['key', 'umpire'],
'info': ['key', 'city', 'competition', 'date', 'gender',
'match_type', 'neutral_venue', 'overs', 'player_of_match', 'venue'],
'dates': ['key', 'date'],
'outcome': ['key', 'by_innings', 'by_type', 'by_margin', 'bowl_out',
'eliminator', 'method', 'result', 'winner'],
'pom': ['key', 'player_of_match'],
'bowl_out': ['key', 'bowler', 'outcome'],
'supersub': ['key', 'team', 'player'],
'innings': ['key', 'inning_no', 'batting_team', 'delivery_no', 'batter', 'bowler', 'non_striker',
'runs_batter', 'runs_extras', 'runs_non_boundary', 'runs_total', 'wicket_fielder', 'wicket_kind', 'wicket_player_out', 'extras_type']}
start_time = datetime.now(pytz.utc)
files_list = os.listdir(f'{game}_files')
if not os.path.exists(f'{game}_data'):
os.makedirs(f'{game}_data')
dict_data_df = load_df(file_types, game)
meta_df = dict_data_df['meta']
toss_df = dict_data_df['toss']
team_df = dict_data_df['team']
umpires_df = dict_data_df['umpires']
info_df = dict_data_df['info']
dates_df = dict_data_df['dates']
outcome_df = dict_data_df['outcome']
pom_df = dict_data_df['pom']
bowl_out_df = dict_data_df['bowl_out']
supersub_df = dict_data_df['supersub']
innings_df = dict_data_df['innings']
for file_num in range(1, len(files_list) + 1):
formatted_file_num = str(format(file_num, '04d'))
key = f'{game}{formatted_file_num}'
del formatted_file_num
path = f'./{game}_files/{key}.yaml'
with open(path) as f:
cric_dict = yaml.safe_load(f)
del path
cric_meta = cric_dict['meta']
cric_meta['key'] = key
if key not in list(meta_df.key):
meta_df = pd.concat(
[meta_df, pd.DataFrame([cric_meta])], ignore_index=True)
# meta_df = meta_df.append(cric_meta, ignore_index=True)
cric_info = cric_dict['info']
if 'umpires' in cric_info.keys() and key not in list(umpires_df.key):
umpires_df = pd.concat([umpires_df, pd.DataFrame(umpire_entry(
umpire_info=cric_info['umpires'], key=key))], ignore_index=True)
# umpires_df = umpires_df.append(umpire_entry(
# umpire_info=cric_info['umpires'], key=key), ignore_index=True)
if key not in list(team_df.key):
team_df = pd.concat([team_df, team_entry(
team_info=cric_info['teams'], key=key)], ignore_index=True)
if key not in list(toss_df.key):
toss_df = pd.concat([toss_df, toss_entry(
toss_info=cric_info['toss'], key=key)], ignore_index=True)
if key not in list(outcome_df.key):
outcome_df = pd.concat([outcome_df, outcome_entry(
key=key, outcome_info=cric_info['outcome'])], ignore_index=True)
if key not in list(dates_df.key):
dates_df = pd.concat([dates_df, date_entry(
key=key, dates=cric_info['dates'])], ignore_index=True)
if 'player_of_match' in cric_info.keys() and key not in list(pom_df.key):
pom_df = pd.concat([pom_df, pom_entry(
key=key, pom_list=cric_info['player_of_match'])], ignore_index=True)
if key not in list(info_df.key):
info_df = pd.concat([info_df, info_entry(
key=key, gen_info=cric_info)], ignore_index=True)
if 'bowl_out' in cric_info.keys() and key not in list(bowl_out_df.key):
bowl_out_df = pd.concat([bowl_out_df, bowl_out_entry(
key, cric_info['bowl_out'])], ignore_index=True)
if 'supersubs' in cric_info.keys() and key not in list(supersub_df.key):
supersub_df = pd.concat([supersub_df, supersub_entry(
key=key, supersub_info=cric_info['supersubs'])], ignore_index=True)
if key not in list(innings_df.key):
innings_df = pd.concat([innings_df, innings_entry(
key_id=key, innings_list=cric_dict['innings'])], ignore_index=True)
del cric_info
del cric_dict
del key
toss_df['winner'] = toss_df.winner.replace(
'Rising Pune Supergiant', 'Rising Pune Supergiants')
toss_df['winner'] = toss_df.winner.replace(
'Delhi Daredevils', 'Delhi Capitals')
toss_df['winner'] = toss_df.winner.replace(
'Pune Warriors', 'Pune Warriors India')
toss_df['winner'] = toss_df.winner.replace(
'Kings XI Punjab', 'Punjab Kings')
team_df['team'] = team_df.team.replace(
'Rising Pune Supergiant', 'Rising Pune Supergiants')
team_df['team'] = team_df.team.replace(
'Delhi Daredevils', 'Delhi Capitals')
team_df['team'] = team_df.team.replace(
'Pune Warriors', 'Pune Warriors India')
team_df['team'] = team_df.team.replace(
'Kings XI Punjab', 'Punjab Kings')
outcome_df['winner'] = outcome_df.winner.replace(
'Rising Pune Supergiant', 'Rising Pune Supergiants')
outcome_df['winner'] = outcome_df.winner.replace(
'Delhi Daredevils', 'Delhi Capitals')
outcome_df['winner'] = outcome_df.winner.replace(
'Pune Warriors', 'Pune Warriors India')
outcome_df['winner'] = outcome_df.winner.replace(
'Kings XI Punjab', 'Punjab Kings')
innings_df['batting_team'] = innings_df.batting_team.replace(
'Rising Pune Supergiant', 'Rising Pune Supergiants')
innings_df['batting_team'] = innings_df.batting_team.replace(
'Delhi Daredevils', 'Delhi Capitals')
innings_df['batting_team'] = innings_df.batting_team.replace(
'Pune Warriors', 'Pune Warriors India')
innings_df['batting_team'] = innings_df.batting_team.replace(
'Kings XI Punjab', 'Punjab Kings')
meta_df.to_csv(f'./{game}_data/meta_df.csv', index=False)
umpires_df.to_csv(f'./{game}_data/umpires_df.csv', index=False)
team_df.to_csv(f'./{game}_data/team_df.csv', index=False)
toss_df.to_csv(f'./{game}_data/toss_df.csv', index=False)
outcome_df.to_csv(f'./{game}_data/outcome_df.csv', index=False)
dates_df.to_csv(f'./{game}_data/dates_df.csv', index=False)
pom_df.to_csv(f'./{game}_data/pom_df.csv', index=False)
info_df.to_csv(f'./{game}_data/info_df.csv', index=False)
innings_df.to_csv(f'./{game}_data/innings_df.csv', index=False)
del meta_df
del umpires_df
del team_df
del toss_df
del outcome_df
del dates_df
del pom_df
del info_df
del innings_df
del files_list
del dict_data_df
shutil.rmtree(f'{game}_files')
final_time = datetime.now(pytz.utc)
time_taken = final_time - start_time
print(f'{game}: {time_taken}')
def main():
# download zip files and rename yaml files
file_process()
# Define your game_types dictionary here
games = game_types()
# Parallelize the loop
with Pool(3) as pool:
pool.map(process_game, games.keys())
if __name__ == "__main__":
main()