-
Notifications
You must be signed in to change notification settings - Fork 262
Expand file tree
/
Copy pathutils.py
More file actions
343 lines (268 loc) · 12.4 KB
/
utils.py
File metadata and controls
343 lines (268 loc) · 12.4 KB
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
import subprocess
from contextlib import contextmanager
from functools import partial, wraps
from itertools import product
from pathlib import Path
from shutil import rmtree
from tempfile import mkdtemp
import lightgbm as lgb
import numpy as np
import pytest
import statsmodels.api as sm
import xgboost as xgb
from lightning.impl.base import BaseClassifier as LightBaseClassifier
from sklearn import datasets
from sklearn.base import BaseEstimator, RegressorMixin, clone
from sklearn.ensemble._forest import BaseForest, ForestClassifier
from sklearn.linear_model._base import LinearClassifierMixin
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC, NuSVC, OneClassSVM
from sklearn.svm._base import BaseLibSVM
from sklearn.tree import DecisionTreeClassifier
from sklearn.tree._classes import BaseDecisionTree
from m2cgen import ast
from m2cgen.assemblers import _get_full_model_name
from m2cgen.interpreters.utils import format_float
class StatsmodelsSklearnLikeWrapper(BaseEstimator, RegressorMixin):
def __init__(self, model, params):
self.model = model
self.params = params
# mock class module and name to show appropriate model name in tests
self.__class__.__module__ = model.__module__
self.__class__.__name__ = model.__name__
def fit(self, X, y):
init_params = self.params.get("init", {})
self.fit_intercept_ = init_params.pop("fit_intercept", False)
if self.fit_intercept_:
X = sm.add_constant(X)
est = self.model(y, X, **init_params)
if "fit_regularized" in self.params:
self.fitted_model_ = est.fit_regularized(**self.params["fit_regularized"])
elif "iterative_fit" in self.params:
self.fitted_model_ = est.iterative_fit(**self.params["iterative_fit"])
elif "fit_constrained" in self.params:
self.fitted_model_ = est.fit_constrained(**self.params["fit_constrained"])
else:
self.fitted_model_ = est.fit(**self.params.get("fit", {}))
# mock class module and name to show appropriate model name in tests
self.__class__.__module__ = type(self.fitted_model_).__module__
self.__class__.__name__ = type(self.fitted_model_).__name__
return self.fitted_model_
def predict(self, X):
if self.fit_intercept_:
X = sm.add_constant(X)
return self.fitted_model_.predict(X)
class ModelTrainer:
_class_instances = {}
def __init__(self, dataset_name, test_fraction):
self.dataset_name = dataset_name
self.test_fraction = test_fraction
additional_test_data = None
np.random.seed(seed=7)
if dataset_name == "boston":
self.name = "train_model_regression"
self.X, self.y = datasets.load_boston(return_X_y=True)
elif dataset_name == "multiout_regression":
self.name = "train_model_regression_multioutput"
self.X, self.y = datasets.make_regression(n_samples=20, n_features=5, n_targets=3, random_state=1)
elif dataset_name == "boston_y_bounded":
self.name = "train_model_regression_bounded"
self.X, self.y = datasets.load_boston(return_X_y=True)
self.y = np.arctan(self.y) / np.pi + 0.5 # (0; 1)
elif dataset_name == "diabetes":
self.name = "train_model_regression_w_missing_values"
self.X, self.y = datasets.load_diabetes(return_X_y=True)
additional_test_data = np.array([
[np.NaN] * self.X.shape[1],
])
elif dataset_name == "iris":
self.name = "train_model_classification"
self.X, self.y = datasets.load_iris(return_X_y=True)
elif dataset_name == "breast_cancer":
self.name = "train_model_classification_binary"
self.X, self.y = datasets.load_breast_cancer(return_X_y=True)
elif dataset_name == "regression_rnd":
self.name = "train_model_regression_random_data"
N = 1000
self.X = np.random.random(size=(N, 200))
self.y = np.random.random(size=(N,))
elif dataset_name == "classification_rnd":
self.name = "train_model_classification_random_data"
N = 1000
self.X = np.random.random(size=(N, 200))
self.y = np.random.randint(3, size=(N,))
elif dataset_name == "classification_rnd_w_missing_values":
self.name = "train_model_classification_rnd_w_missing_values"
N = 100
self.X = np.random.random(size=(N, 20)) - 0.5
self.y = np.random.randint(3, size=(N,))
additional_test_data = np.array([
[np.NaN] * self.X.shape[1],
])
elif dataset_name == "classification_binary_rnd":
self.name = "train_model_classification_binary_random_data"
N = 1000
self.X = np.random.random(size=(N, 200))
self.y = np.random.randint(2, size=(N,))
elif dataset_name == "classification_binary_rnd_w_missing_values":
self.name = "train_model_classification_binary_rnd_w_missing_values"
N = 100
self.X = np.random.random(size=(N, 20)) - 0.5
self.y = np.random.randint(2, size=(N,))
additional_test_data = np.array([
[np.NaN] * self.X.shape[1],
])
else:
raise ValueError(f"Unknown dataset name: {dataset_name}")
self.X_train, self.X_test, self.y_train, _ = train_test_split(
self.X, self.y, test_size=test_fraction, random_state=15)
if additional_test_data is not None:
self.X_test = np.vstack((additional_test_data, self.X_test))
@classmethod
def get_instance(cls, dataset_name, test_fraction=0.02):
key = f"{dataset_name} {test_fraction}"
if key not in cls._class_instances:
cls._class_instances[key] = ModelTrainer(dataset_name, test_fraction)
return cls._class_instances[key]
def __call__(self, estimator):
fitted_estimator = estimator.fit(self.X_train, self.y_train)
if isinstance(
estimator,
(
LinearClassifierMixin,
SVC,
NuSVC,
OneClassSVM,
LightBaseClassifier,
)):
y_pred = estimator.decision_function(self.X_test)
elif isinstance(
estimator,
(
ForestClassifier,
DecisionTreeClassifier,
xgb.XGBClassifier,
lgb.LGBMClassifier,
)):
y_pred = estimator.predict_proba(self.X_test)
else:
y_pred = estimator.predict(self.X_test)
# Some models force input data to be particular type
# during prediction phase in their native Python libraries.
# For correct comparison of testing results we mimic the same behavior
if isinstance(
estimator,
(
BaseDecisionTree,
BaseForest,
)):
self.X_test = self.X_test.astype(np.float32, copy=False)
elif isinstance(
estimator,
(
BaseLibSVM,
)):
self.X_test = self.X_test.astype(np.float64, copy=False)
return self.X_test, y_pred, fitted_estimator
def cmp_exprs(left, right):
"""Recursively compares two ast expressions."""
if isinstance(left, ast.VectorVal) and isinstance(right, ast.Expr):
left_exprs = left.exprs
right_exprs = right.exprs
assert len(left_exprs) == len(right_exprs)
for left_expr, right_expr in zip(left_exprs, right_exprs):
assert cmp_exprs(left_expr, right_expr)
return True
if not isinstance(left, ast.Expr) and not isinstance(right, ast.Expr):
if _is_float(left) and _is_float(right):
comp_res = np.isclose(left, right)
else:
comp_res = left == right
assert comp_res, f"{left} != {right}"
return True
if isinstance(left, ast.Expr) and isinstance(right, ast.Expr):
assert isinstance(left, type(right)), f"Expected instance of {type(right)}, received {type(left)}"
# Only compare attributes which don't start with __
attrs_to_compare = filter(lambda attr_name: not attr_name.startswith('__'), dir(left))
for attr_name in attrs_to_compare:
assert cmp_exprs(getattr(left, attr_name), getattr(right, attr_name))
return True
return False
def assert_code_equal(actual, expected):
assert actual.strip() == expected.strip()
get_regression_model_trainer = partial(ModelTrainer.get_instance, "boston")
get_multioutput_regression_model_trainer = partial(ModelTrainer.get_instance, "multiout_regression")
get_classification_model_trainer = partial(ModelTrainer.get_instance, "iris")
get_binary_classification_model_trainer = partial(ModelTrainer.get_instance, "breast_cancer")
get_regression_random_data_model_trainer = partial(ModelTrainer.get_instance, "regression_rnd")
get_classification_random_data_model_trainer = partial(ModelTrainer.get_instance, "classification_rnd")
get_classification_binary_random_data_model_trainer = partial(ModelTrainer.get_instance, "classification_binary_rnd")
get_bounded_regression_model_trainer = partial(ModelTrainer.get_instance, "boston_y_bounded")
get_regression_w_missing_values_model_trainer = partial(ModelTrainer.get_instance, "diabetes")
get_classification_random_w_missing_values_model_trainer = partial(
ModelTrainer.get_instance, "classification_rnd_w_missing_values")
get_classification_binary_random_w_missing_values_model_trainer = partial(
ModelTrainer.get_instance, "classification_binary_rnd_w_missing_values")
@contextmanager
def tmp_dir():
dirpath = Path(mkdtemp())
try:
yield dirpath
finally:
rmtree(dirpath)
def verify_python_model_is_expected(model_code, input, expected_output):
input_str = f"[{', '.join(map(str, input))}]"
code = f"""
{model_code}
result = score({input_str})"""
context = {}
exec(code, context)
assert np.isclose(context["result"], expected_output)
def execute_command(exec_args, shell=False):
result = subprocess.Popen(exec_args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=shell)
stdout, stderr = result.communicate()
if result.returncode != 0:
raise RuntimeError(f"Bad exit code ({result.returncode}), stderr:\n{stderr.decode('utf-8')}")
return stdout.decode("utf-8").strip()
def predict_from_commandline(exec_args):
items = execute_command(exec_args).split(" ")
if len(items) == 1:
return np.float64(items[0])
else:
return [np.float64(i) for i in items]
def cartesian_e2e_params(executors_with_marks, models_with_trainers_with_marks,
skip_executor_trainer_pairs, *additional_params):
result_params = list(additional_params)
# Specifying None for additional parameters makes pytest to generate
# automatic ids. If we don't do this pytest will throw exception that
# number of parameters doesn't match number of provided ids
ids = [None] * len(additional_params)
prod = product(executors_with_marks, models_with_trainers_with_marks)
for (executor, executor_mark), (model, trainer, trainer_mark) in prod:
if (executor_mark, trainer_mark) in skip_executor_trainer_pairs:
continue
# Since we reuse the same model across multiple tests we want it
# to be clean.
model = clone(model)
# We use custom id since pytest for some reason can't show name of
# the model in the automatic id. Which sucks.
ids.append(f"{_get_full_model_name(model)} - {executor_mark.name} - {trainer.name}")
result_params.append(pytest.param(
model, executor, trainer, marks=[executor_mark, trainer_mark],
))
param_names = "estimator,executor_cls,model_trainer"
def wrap(func):
@pytest.mark.parametrize(param_names, result_params, ids=ids)
@wraps(func)
def inner(*args, **kwarg):
return func(*args, **kwarg)
return inner
return wrap
def _is_float(value):
return isinstance(value, (float, np.floating))
def format_arg(value):
if np.isnan(value):
return "NaN"
return format_float(value)
def write_content_to_file(content, path):
path.write_text(content, encoding="utf-8")