forked from SIMSON20/dsa2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
core_run.py
197 lines (135 loc) · 5.78 KB
/
core_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
# pylint: disable=C0321,C0103,E1221,C0301,E1305,E1121,C0302,C0330
# -*- coding: utf-8 -*-
"""
Run template
python core_run.py data_profile --config outlier_predict.py::titanic_lightgbm
python core_run.py preprocess --config outlier_predict.py::titanic_lightgbm
python core_run.py train --config outlier_predict.py::titanic_lightgbm
python core_run.py predict --config outlier_predict.py::titanic_lightgbm
"""
import warnings, copy, os, sys
warnings.filterwarnings('ignore')
####################################################################################
from source.util_feature import log
print( os.getcwd())
root = os.path.abspath(os.getcwd()).replace("\\", "/") + "/"
print(root)
dir_data = os.path.abspath( root + "/data/" ) + "/"
dir_data = dir_data.replace("\\", "/")
print(dir_data)
def get_global_pars(config_uri=""):
log("#### Model Params Dynamic loading ##########################################")
from source.util_feature import load_function_uri
model_dict_fun = load_function_uri(uri_name=config_uri )
#### Get dict + Update Global variables
try :
model_dict = model_dict_fun() ### params
except :
model_dict = model_dict_fun
return model_dict
def get_config_path(config=''):
#### Get params where the file is imported #####################
path0 = os.path.abspath( sys.modules['__main__'].__file__)
print("file where imported", path0)
config_default = get_global_pars( path0 + "::config_default")
if len(config) == 0 :
config_uri = path0 + "::" + config_default
config_name = config_default
elif "::" not in config :
config_uri = path0 + "::" + config
config_name = config
else :
config_uri = config
config_name = config.split("::")[1]
##################################################################
print("default: ", config_uri)
return config_uri, config_name
#####################################################################################
########## Profile data #############################################################
def data_profile2(config=''):
"""
:param config:
:return:
"""
config_uri, config_name = get_config_path(config)
from source.run_feature_profile import run_profile
mdict = get_global_pars( config_uri)
m = mdict['global_pars']
log(mdict)
run_profile(path_data = m['path_data_train'],
path_output = m['path_model'] + "/profile/",
n_sample = 5000,
)
def data_profile(path_data="NO PATH", path_output="NO PATH@", n_sample= 5000):
from source.run_feature_profile import run_profile
run_profile(path_data = path_data,
path_output = path_output + "/profile/",
n_sample = n_sample,
)
###################################################################################
########## Preprocess #############################################################
def preprocess(config='', nsample=None):
"""
:param config:
:param nsample:
:return:
"""
config_uri, config_name = get_config_path(config)
mdict = get_global_pars( config_uri)
m = mdict['global_pars']
log(mdict)
from source import run_preprocess
run_preprocess.run_preprocess(config_name = config_name,
config_path = m['config_path'],
n_sample = nsample if nsample is not None else m['n_sample'],
### Optonal
mode = 'run_preprocess')
####################################################################################
########## Train ###################################################################
def train(config='', nsample=None):
""" train a model with confi_name and nsample
:param config:
:param nsample:
:return:
"""
config_uri, config_name = get_config_path(config)
mdict = get_global_pars( config_uri)
m = mdict['global_pars']
log(mdict)
from source import run_train
run_train.run_train(config_name = config_name,
config_path = m['config_path'],
n_sample = nsample if nsample is not None else m['n_sample'],
)
####################################################################################
######### Check model ##############################################################
def check(config='outlier_predict.py::titanic_lightgbm'):
mdict = get_global_pars(config)
m = mdict['global_pars']
log(mdict)
pass
########################################################################################
####### Inference ######################################################################
def predict(config='', nsample=None):
"""
:param config:
:param nsample:
:return:
"""
config_uri, config_name = get_config_path(config)
mdict = get_global_pars( config_uri)
m = mdict['global_pars']
log(mdict)
from source import run_inference
run_inference.run_predict(config_name = config_name,
config_path = m['config_path'],
n_sample = nsample if nsample is not None else m['n_sample'],
#### Optional
path_data = m['path_pred_data'],
path_output = m['path_pred_output'],
model_dict = None
)
##########################################################################################
if __name__ == "__main__":
import fire
fire.Fire()