-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
169 lines (83 loc) · 3.12 KB
/
utils.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
import argparse
import os
import time
import shutil
import csv
import pandas as pd
class Submit():
def __init__(self, path, competition):
self.path = path
self.competition = competition
def submit_to_competition(self):
res = os.popen("kaggle competitions submit" + " -f " + self.path + " " + self.competition)
def upload(self):
res = os.popen("kaggle kernel push -p " + self.path)
class CheckStatus():
def __init__(self,user_name ,kernel_name, frequency):
self.kernel_name = kernel_name
self.user_name = user_name
self.frequency = frequency
def getStatus(self):
#expecting "kaggle kernel status -> complete / running"
if(not self.frequency):
res = os.popen('kaggle kernels status ' + self.user_name + "/" + self.kernel_name).read()
print(res)
elif(self.frequency > 0):
while(True):
res = os.popen('kaggle kernels status ' + self.user_name + "/" + self.kernel_name).read()
print(res)
status = res.split(" ")[-1].strip("\n")
if(status != "\"running\""):
if((status == "\"complete\"") or (status == "cancel.\"")):
return 0
else:
return 1
time.sleep(self.frequency)
class GetOutput():
def __init__(self, user_name, kernel_name, result_dir):
self.user_name = user_name
self.kernel_name = kernel_name
self.result_dir = result_dir
self.temp_dir = "temp/"
try:
os.mkdir(self.temp_dir)
except FileExistsError:
shutil.rmtree("temp/")
try:
os.mkdir(self.result_dir)
except FileExistsError:
pass
def getOutput(self):
print("Fetching output from kaggle...")
try:
os.popen('kaggle kernels output ' + self.user_name + "/" + self.kernel_name + " --path " + self.temp_dir).read()
except FileNotFoundError:
print("Saving output failed because kaggle kernel stored output in subdirectory,so we are not able to download it.Please store outputs in current working directory only.")
return None
self.result_files = os.listdir(self.temp_dir)
for file in self.result_files:
shutil.move(self.temp_dir + "/" + file, self.result_dir + file)
print("Done...")
return self.result_files
def preProcessor():
pass
def postProcessor(user_name, kernel_name, master_path, result_path, all_result_files, result_file_to_append):
"""
append result csv file to master csv file
"""
if all_result_files is not None and result_file_to_append is not None:
data = pd.read_csv(result_path + result_file_to_append)
print("post processing result")
if os.path.isfile(master_path + kernel_name + "-master.csv"): #append to master
master = pd.read_csv(master_path + kernel_name + "-master.csv")
master = pd.concat([master, data], axis = 0)
master.to_csv(master_path + kernel_name + "-master.csv", index = False)
else:
data.to_csv(master_path + kernel_name + "-master.csv")
for file in all_result_files: #remove kernel results from results direcotry after appending to master log
os.remove(result_path + file)
"""with open(master_log, "a") as f:
f.write(data)"""
"""with open(master_log, 'a') as f:
writer = csv.writer(f)
writer.writerow(data)"""