forked from EpistasisLab/tpot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
441 lines (318 loc) · 14.8 KB
/
tests.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
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
# -*- coding: utf-8 -*-
"""
TPOT Unit Tests
"""
from tpot import TPOTClassifier, TPOTRegressor
from tpot.base import TPOTBase
from tpot.driver import positive_integer, float_range
from tpot.export_utils import export_pipeline, generate_import_code, _indent, generate_pipeline_code
from tpot.decorators import _gp_new_generation
from tpot.gp_types import Output_DF
from tpot.operators import Operator
from tpot.operators.selectors import TPOTSelectKBest
import numpy as np
import inspect
import random
from datetime import datetime
from sklearn.datasets import load_digits, load_boston
from sklearn.model_selection import train_test_split
from deap import creator
from tqdm import tqdm
# Set up the MNIST data set for testing
mnist_data = load_digits()
training_features, testing_features, training_classes, testing_classes = \
train_test_split(mnist_data.data.astype(np.float64), mnist_data.target.astype(np.float64), random_state=42)
# Set up the Boston data set for testing
boston_data = load_boston()
training_features_r, testing_features_r, training_classes_r, testing_classes_r = \
train_test_split(boston_data.data, boston_data.target, random_state=42)
np.random.seed(42)
random.seed(42)
def test_init_custom_parameters():
"""Assert that the TPOT instantiator stores the TPOT variables properly"""
tpot_obj = TPOTClassifier(population_size=500, generations=1000,
mutation_rate=0.05, crossover_rate=0.9,
scoring='accuracy', num_cv_folds=10,
verbosity=1, random_state=42,
disable_update_check=True)
assert tpot_obj.population_size == 500
assert tpot_obj.generations == 1000
assert tpot_obj.mutation_rate == 0.05
assert tpot_obj.crossover_rate == 0.9
assert tpot_obj.scoring_function == 'accuracy'
assert tpot_obj.num_cv_folds == 10
assert tpot_obj.max_time_mins is None
assert tpot_obj.verbosity == 1
assert tpot_obj._optimized_pipeline is None
assert tpot_obj._fitted_pipeline is None
assert not (tpot_obj._pset is None)
assert not (tpot_obj._toolbox is None)
def test_init_default_scoring():
"""Assert that TPOT intitializes with the correct default scoring function"""
tpot_obj = TPOTRegressor()
assert tpot_obj.scoring_function == 'neg_mean_squared_error'
def test_init_max_time_mins():
"""Assert that the TPOT init stores max run time and sets generations to 1000000"""
tpot_obj = TPOTClassifier(max_time_mins=30, generations=1000)
assert tpot_obj.generations == 1000000
assert tpot_obj.max_time_mins == 30
def test_get_params():
"""Assert that get_params returns the exact dictionary of parameters used by TPOT"""
kwargs = {
'population_size': 500,
'generations': 1000,
'verbosity': 1
}
tpot_obj = TPOTClassifier(**kwargs)
# Get default parameters of TPOT and merge with our specified parameters
initializer = inspect.getargspec(TPOTBase.__init__)
default_kwargs = dict(zip(initializer.args[1:], initializer.defaults))
default_kwargs.update(kwargs)
assert tpot_obj.get_params() == default_kwargs
def test_set_params():
"""Assert that set_params returns a reference to the TPOT instance"""
tpot_obj = TPOTClassifier()
assert tpot_obj.set_params() is tpot_obj
def test_set_params_2():
"""Assert that set_params updates TPOT's instance variables"""
tpot_obj = TPOTClassifier(generations=2)
tpot_obj.set_params(generations=3)
assert tpot_obj.generations == 3
def test_score():
"""Assert that the TPOT score function raises a ValueError when no optimized pipeline exists"""
tpot_obj = TPOTClassifier()
try:
tpot_obj.score(testing_features, testing_classes)
assert False # Should be unreachable
except ValueError:
pass
def test_score_2():
"""Assert that the TPOTClassifier score function outputs a known score for a fixed pipeline"""
tpot_obj = TPOTClassifier()
tpot_obj._pbar = tqdm(total=1, disable=True)
known_score = 0.986318199045 # Assumes use of the TPOT balanced_accuracy function
# Reify pipeline with known score
tpot_obj._optimized_pipeline = creator.Individual.\
from_string('RandomForestClassifier(input_matrix)', tpot_obj._pset)
tpot_obj._fitted_pipeline = tpot_obj._toolbox.compile(expr=tpot_obj._optimized_pipeline)
tpot_obj._fitted_pipeline.fit(training_features, training_classes)
# Get score from TPOT
score = tpot_obj.score(testing_features, testing_classes)
# http://stackoverflow.com/questions/5595425/
def isclose(a, b, rel_tol=1e-09, abs_tol=0.0):
return abs(a - b) <= max(rel_tol * max(abs(a), abs(b)), abs_tol)
assert isclose(known_score, score)
def test_score_3():
"""Assert that the TPOTRegressor score function outputs a known score for a fixed pipeline"""
tpot_obj = TPOTRegressor(scoring='neg_mean_squared_error')
tpot_obj._pbar = tqdm(total=1, disable=True)
known_score = 8.9673743407873712 # Assumes use of mse
# Reify pipeline with known score
tpot_obj._optimized_pipeline = creator.Individual.\
from_string('ExtraTreesRegressor(GradientBoostingRegressor(input_matrix, 100.0, 0.11), 0.17999999999999999)', tpot_obj._pset)
tpot_obj._fitted_pipeline = tpot_obj._toolbox.compile(expr=tpot_obj._optimized_pipeline)
tpot_obj._fitted_pipeline.fit(training_features_r, training_classes_r)
# Get score from TPOT
score = tpot_obj.score(testing_features_r, testing_classes_r)
# http://stackoverflow.com/questions/5595425/
def isclose(a, b, rel_tol=1e-09, abs_tol=0.0):
return abs(a - b) <= max(rel_tol * max(abs(a), abs(b)), abs_tol)
assert isclose(known_score, score)
def test_predict():
"""Assert that the TPOT predict function raises a ValueError when no optimized pipeline exists"""
tpot_obj = TPOTClassifier()
try:
tpot_obj.predict(testing_features)
assert False # Should be unreachable
except ValueError:
pass
def test_predict_2():
"""Assert that the TPOT predict function returns a numpy matrix of shape (num_testing_rows,)"""
tpot_obj = TPOTClassifier()
tpot_obj._optimized_pipeline = creator.Individual.\
from_string('DecisionTreeClassifier(input_matrix)', tpot_obj._pset)
tpot_obj._fitted_pipeline = tpot_obj._toolbox.compile(expr=tpot_obj._optimized_pipeline)
tpot_obj._fitted_pipeline.fit(training_features, training_classes)
result = tpot_obj.predict(testing_features)
assert result.shape == (testing_features.shape[0],)
def test_fit():
"""Assert that the TPOT fit function provides an optimized pipeline"""
tpot_obj = TPOTClassifier(random_state=42, population_size=1, generations=1, verbosity=0)
tpot_obj.fit(training_features, training_classes)
assert isinstance(tpot_obj._optimized_pipeline, creator.Individual)
assert tpot_obj._gp_generation == 0
assert not (tpot_obj._start_datetime is None)
def test_gp_new_generation():
"""Assert that the gp_generation count gets incremented when _gp_new_generation is called"""
tpot_obj = TPOTClassifier()
tpot_obj._pbar = tqdm(total=1, disable=True)
assert tpot_obj._gp_generation == 0
# Since _gp_new_generation is a decorator, and we dont want to run a full
# fit(), decorate a dummy function and then call the dummy function.
@_gp_new_generation
def dummy_function(self, foo):
pass
dummy_function(tpot_obj, None)
assert tpot_obj._gp_generation == 1
def check_export(op):
"""Assert that a TPOT operator exports as expected"""
tpot_obj = TPOTClassifier(random_state=42)
prng = np.random.RandomState(42)
np.random.seed(42)
args = []
for type_ in op.parameter_types()[0][1:]:
args.append(prng.choice(tpot_obj._pset.terminals[type_]).value)
export_string = op.export(*args)
assert export_string.startswith(op.__name__ + "(") and export_string.endswith(")")
def test_operators():
"""Assert that the TPOT operators match the output of their sklearn counterparts"""
for op in Operator.inheritors():
check_export.description = ("Assert that the TPOT {} operator exports "
"as expected".format(op.__name__))
yield check_export, op
def test_export():
"""Assert that TPOT's export function throws a ValueError when no optimized pipeline exists"""
tpot_obj = TPOTClassifier()
try:
tpot_obj.export("test_export.py")
assert False # Should be unreachable
except ValueError:
pass
def test_generate_pipeline_code():
"""Assert that generate_pipeline_code() returns the correct code given a specific pipeline"""
pipeline = ['KNeighborsClassifier',
['CombineDFs',
['GradientBoostingClassifier',
'input_matrix',
38.0,
0.87],
['GaussianNB',
['ZeroCount',
'input_matrix']]],
18,
33]
expected_code = """make_pipeline(
make_union(
make_union(VotingClassifier([('branch',
GradientBoostingClassifier(learning_rate=1.0, max_features=1.0, n_estimators=500)
)]), FunctionTransformer(lambda X: X)),
make_union(VotingClassifier([('branch',
make_pipeline(
ZeroCount(),
GaussianNB()
)
)]), FunctionTransformer(lambda X: X))
),
KNeighborsClassifier(n_neighbors=5, weights="distance")
)"""
assert expected_code == generate_pipeline_code(pipeline)
def test_generate_import_code():
"""Assert that generate_import_code() returns the correct set of dependancies for a given pipeline"""
tpot_obj = TPOTClassifier()
pipeline = creator.Individual.\
from_string('DecisionTreeClassifier(SelectKBest(input_matrix, 7), 0.5)', tpot_obj._pset)
expected_code = """import numpy as np
from sklearn.ensemble import VotingClassifier
from sklearn.feature_selection import SelectKBest, f_classif
from sklearn.model_selection import train_test_split
from sklearn.pipeline import make_pipeline, make_union
from sklearn.preprocessing import FunctionTransformer
from sklearn.tree import DecisionTreeClassifier
# NOTE: Make sure that the class is labeled 'class' in the data file
tpot_data = np.recfromcsv('PATH/TO/DATA/FILE', delimiter='COLUMN_SEPARATOR', dtype=np.float64)
features = np.delete(tpot_data.view(np.float64).reshape(tpot_data.size, -1), tpot_data.dtype.names.index('class'), axis=1)
training_features, testing_features, training_classes, testing_classes = \\
train_test_split(features, tpot_data['class'], random_state=42)
"""
assert expected_code == generate_import_code(pipeline)
def test_export_pipeline():
"""Assert that exported_pipeline() generated a compile source file as expected given a fixed pipeline"""
tpot_obj = TPOTClassifier()
pipeline = creator.Individual.\
from_string("KNeighborsClassifier(CombineDFs(GradientBoostingClassifier(input_matrix, 38.0, 0.87), SelectKBest(input_matrix, 5)), 18, 33)", tpot_obj._pset)
expected_code = """import numpy as np
from sklearn.ensemble import GradientBoostingClassifier, VotingClassifier
from sklearn.feature_selection import SelectKBest, f_classif
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.pipeline import make_pipeline, make_union
from sklearn.preprocessing import FunctionTransformer
# NOTE: Make sure that the class is labeled 'class' in the data file
tpot_data = np.recfromcsv('PATH/TO/DATA/FILE', delimiter='COLUMN_SEPARATOR', dtype=np.float64)
features = np.delete(tpot_data.view(np.float64).reshape(tpot_data.size, -1), tpot_data.dtype.names.index('class'), axis=1)
training_features, testing_features, training_classes, testing_classes = \\
train_test_split(features, tpot_data['class'], random_state=42)
exported_pipeline = make_pipeline(
make_union(
make_union(VotingClassifier([('branch',
GradientBoostingClassifier(learning_rate=1.0, max_features=1.0, n_estimators=500)
)]), FunctionTransformer(lambda X: X)),
SelectKBest(k=5, score_func=f_classif)
),
KNeighborsClassifier(n_neighbors=5, weights="distance")
)
exported_pipeline.fit(training_features, training_classes)
results = exported_pipeline.predict(testing_features)
"""
assert expected_code == export_pipeline(pipeline)
def test_operator_export():
"""Assert that a TPOT operator can export properly with a function as a parameter to a classifier"""
export_string = TPOTSelectKBest().export(5)
assert export_string == "SelectKBest(k=5, score_func=f_classif)"
def test_indent():
"""Assert that indenting a multiline string by 4 spaces prepends 4 spaces before each new line"""
multiline_string = """test
test1
test2
test3"""
indented_multiline_string = """ test
test1
test2
test3"""
assert indented_multiline_string == _indent(multiline_string, 4)
def test_operator_type():
"""Assert that TPOT operators return their type, e.g. "Classifier", "Preprocessor" """
assert TPOTSelectKBest().type == "Selector"
def test_get_by_name():
"""Assert that the Operator class returns operators by name appropriately"""
assert Operator.get_by_name("SelectKBest").__class__ == TPOTSelectKBest
def test_gen():
"""Assert that TPOT's gen_grow_safe function returns a pipeline of expected structure"""
tpot_obj = TPOTClassifier()
pipeline = tpot_obj._gen_grow_safe(tpot_obj._pset, 1, 3)
assert len(pipeline) > 1
assert pipeline[0].ret == Output_DF
def test_positive_integer():
"""Assert that the TPOT CLI interface's integer parsing throws an exception when n < 0"""
try:
positive_integer('-1')
assert False # Should be unreachable
except Exception:
pass
def test_positive_integer_2():
"""Assert that the TPOT CLI interface's integer parsing returns the integer value of a string encoded integer when n > 0"""
assert 1 == positive_integer('1')
def test_positive_integer_3():
"""Assert that the TPOT CLI interface's integer parsing throws an exception when n is not an integer"""
try:
positive_integer('foobar')
assert False # Should be unreachable
except Exception:
pass
def test_float_range():
"""Assert that the TPOT CLI interface's float range returns a float with input is in 0. - 1.0"""
assert 0.5 == float_range('0.5')
def test_float_range_2():
"""Assert that the TPOT CLI interface's float range throws an exception when input it out of range"""
try:
float_range('2.0')
assert False # Should be unreachable
except Exception:
pass
def test_float_range_3():
"""Assert that the TPOT CLI interface's float range throws an exception when input is not a float"""
try:
float_range('foobar')
assert False # Should be unreachable
except Exception:
pass