forked from vwegmayr/ml-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.py
202 lines (159 loc) · 5.53 KB
/
run.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
"""Scikit runner"""
import numpy as np
import argparse
import os
import sys
import pandas as pd
from sklearn.externals import joblib
from abc import ABC
from abc import abstractmethod
from ml_project import configparse
from pprint import pprint
from os.path import normpath
class Action(ABC):
"""Abstract Action class
Args:
args (Namespace): Parsed arguments
"""
def __init__(self, args):
self.args = args
self._check_action(args.action)
self.X, self.y = self._load_data()
self.save_path = self._mk_save_folder()
self.X_new, self.y_new = None, None
self._X_new_set, self._y_new_set = False, False
@abstractmethod
def _save(self):
pass
@abstractmethod
def _load_model(self):
pass
@abstractmethod
def _check_action(self):
pass
def act(self):
self.model = self._load_model()
getattr(self, self.args.action)()
if self.args.smt_label != "debug":
self._save()
def _load_data(self):
try:
X = np.load(self.args.X)
except FileNotFoundError:
print("{} not found. "
"Please download data first.".format(self.args.X))
exit()
if self.args.y is not None:
try:
y = np.loadtxt(self.args.y)
except FileNotFoundError:
print("{} not found. "
"Please download data first.".format(self.args.y))
exit()
else:
y = None
return X, y
def _mk_save_folder(self):
if self.args.smt_label != "debug":
basename = self.args.smt_label
path = "data/"+basename+"/"
os.mkdir(normpath(path))
return path
else:
return None
class ConfigAction(Action):
"""Class to handle config file actions
Args:
args (Namespace): Parsed arguments
config (dict): Parsed config file
"""
def __init__(self, args, config):
super(ConfigAction, self).__init__(args)
self.config = config
self.pprint_config()
self.act()
def fit(self):
self.model.fit(self.X, self.y)
def transform(self):
self.X_new = self.model.transform(self.X, self.y)
self._X_new_set = True
def fit_transform(self):
self.fit()
self.transform()
def _save(self):
class_name = self.config["class"].__name__
joblib.dump(self.model,
normpath(self.save_path+class_name+".pkl"))
if self._X_new_set:
path = self.save_path+"X_new.npy"
np.save(normpath(path), self.X_new)
def _load_model(self):
if "params" in self.config:
model = self.config["class"](**self.config["params"])
else:
model = self.config["class"]()
if hasattr(model, "set_save_path"):
model.set_save_path(self.save_path)
return model
def _check_action(self, action):
if action not in ["fit", "fit_transform"]:
raise RuntimeError("Can only run fit or fit_transform from config,"
" got {}.".format(action))
def pprint_config(self):
print("\n=========== Config ===========")
pprint(self.config)
print("==============================\n")
sys.stdout.flush()
class ModelAction(Action):
"""Class to model actions
Args:
args (Namespace): Parsed arguments
"""
def __init__(self, args):
super(ModelAction, self).__init__(args)
self.act()
def transform(self):
self.X_new = self.model.transform(self.X, self.y)
self._X_new_set = True
def predict(self):
self.y_new = self.model.predict(self.X)
self._y_new_set = True
def score(self):
self.model.score(self.X, self.y)
def _save(self):
y_path = normpath(self.save_path+"y_"+self.args.smt_label+".csv")
X_path = normpath(self.save_path+"X_new.npy")
if self._X_new_set:
np.save(X_path, self.X_new)
if self._y_new_set:
df = pd.DataFrame({"Prediction": self.y_new})
df.index += 1
df.index.name = "ID"
df.to_csv(y_path)
def _load_model(self):
model = joblib.load(self.args.model)
if hasattr(model, "set_save_path"):
model.set_save_path(self.save_path)
return model
def _check_action(self, action):
if action not in ["transform", "predict", "score"]:
raise RuntimeError("Can only run transform, predict or score from"
"model, got {}.".format(action))
if __name__ == '__main__':
arg_parser = argparse.ArgumentParser(description="Scikit runner.")
arg_parser.add_argument("-C", "--config", help="config file")
arg_parser.add_argument("-M", "--model", help="model file")
arg_parser.add_argument("-X", help="Input data", required=True)
arg_parser.add_argument("-y", help="Input labels")
arg_parser.add_argument("-a", "--action", choices=["transform", "predict",
"fit", "fit_transform", "score"],
help="Action to perform.",
required=True)
arg_parser.add_argument("smt_label", nargs="?", default="debug")
args = arg_parser.parse_args()
if args.config is None:
ModelAction(args)
else:
config_parser = configparse.ConfigParser()
config = config_parser.parse_config(args.config)
ConfigAction(args, config)