-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
driver_tests.py
376 lines (311 loc) · 12.4 KB
/
driver_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
# -*- coding: utf-8 -*-
"""This file is part of the TPOT library.
TPOT was primarily developed at the University of Pennsylvania by:
- Randal S. Olson (rso@randalolson.com)
- Weixuan Fu (weixuanf@upenn.edu)
- Daniel Angell (dpa34@drexel.edu)
- and many more generous open source contributors
TPOT is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
TPOT is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with TPOT. If not, see <http://www.gnu.org/licenses/>.
"""
import subprocess
import sys
import warnings
from os import remove, path
from contextlib import contextmanager
from distutils.version import LooseVersion
try:
from StringIO import StringIO
except ImportError:
from io import StringIO
import numpy as np
import pandas as pd
import sklearn
from tpot.driver import positive_integer, float_range, _get_arg_parser, _print_args, _read_data_file, load_scoring_function, tpot_driver
from nose.tools import assert_raises, assert_equal, assert_in
from unittest import TestCase
@contextmanager
def captured_output():
new_out, new_err = StringIO(), StringIO()
old_out, old_err = sys.stdout, sys.stderr
try:
sys.stdout, sys.stderr = new_out, new_err
yield sys.stdout, sys.stderr
finally:
sys.stdout, sys.stderr = old_out, old_err
def test_scoring_function_argument():
with captured_output() as (out, err):
# regular argument returns regular string
assert_equal(load_scoring_function("roc_auc"), "roc_auc")
# bad function returns exception
assert_raises(Exception, load_scoring_function, scoring_func="tests.__fake_BAD_FUNC_NAME")
# manual function loads the function
assert_equal(load_scoring_function('driver_tests.test_scoring_function_argument').__name__, test_scoring_function_argument.__name__)
# installed-module function test
assert_equal(load_scoring_function('sklearn.metrics.auc').__name__, "auc")
out, err = out.getvalue(), err.getvalue()
assert_in("failed importing custom scoring function", out)
assert_in("manual scoring function: <function auc", out)
assert_in("taken from module: sklearn.metrics", out)
assert_in("manual scoring function: <function test_scoring_function_argument", out)
assert_in("taken from module: driver_tests", out)
assert_equal(err, "")
def test_driver():
"""Assert that the TPOT driver outputs normal result in mode mode."""
batcmd = "python -m tpot.driver tests/tests.csv -is , -target class -g 1 -p 2 -os 4 -cv 5 -s 45 -v 1"
ret_stdout = subprocess.check_output(batcmd, shell=True)
try:
ret_val = float(ret_stdout.decode('UTF-8').split('\n')[-2].split(': ')[-1])
except Exception as e:
ret_val = -float('inf')
assert ret_val > 0.0
def test_driver_2():
"""Assert that the tpot_driver() in TPOT driver outputs normal result with verbosity = 1."""
args_list = [
'tests/tests.csv',
'-is', ',',
'-target', 'class',
'-g', '1',
'-p', '2',
'-cv', '2',
'-s',' 45',
'-config', 'TPOT light',
'-v', '1'
]
args = _get_arg_parser().parse_args(args_list)
with captured_output() as (out, err):
tpot_driver(args)
ret_stdout = out.getvalue()
assert "TPOT settings" not in ret_stdout
assert "Final Pareto front testing scores" not in ret_stdout
try:
ret_val = float(ret_stdout.split('\n')[-2].split(': ')[-1])
except Exception:
ret_val = -float('inf')
assert ret_val > 0.0
def test_driver_3():
"""Assert that the tpot_driver() in TPOT driver outputs normal result with verbosity = 2."""
args_list = [
'tests/tests.csv',
'-is', ',',
'-target', 'class',
'-g', '1',
'-p', '2',
'-cv', '3',
'-s',' 45',
'-config', 'TPOT light',
'-v', '2'
]
args = _get_arg_parser().parse_args(args_list)
with captured_output() as (out, err):
tpot_driver(args)
ret_stdout = out.getvalue()
assert "TPOT settings" in ret_stdout
assert "Final Pareto front testing scores" not in ret_stdout
try:
ret_val = float(ret_stdout.split('\n')[-2].split(': ')[-1])
except Exception:
ret_val = -float('inf')
assert ret_val > 0.0
def test_driver_4():
"""Assert that the tpot_driver() in TPOT driver outputs normal result with verbosity = 3."""
args_list = [
'tests/tests.csv',
'-is', ',',
'-target', 'class',
'-g', '1',
'-p', '2',
'-cv', '3',
'-s', '42',
'-config', 'TPOT light',
'-v', '3'
]
args = _get_arg_parser().parse_args(args_list)
with captured_output() as (out, err):
tpot_driver(args)
ret_stdout = out.getvalue()
assert "TPOT settings" in ret_stdout
assert "Final Pareto front testing scores" in ret_stdout
try:
ret_val = float(ret_stdout.split('\n')[-2].split('\t')[1])
except Exception:
ret_val = -float('inf')
assert ret_val > 0.0
def test_driver_5():
"""Assert that the tpot_driver() in TPOT driver outputs normal result with exported python file and verbosity = 0."""
# Catch FutureWarning https://github.com/scikit-learn/scikit-learn/issues/11785
handle_warning = (np.__version__ >= LooseVersion("1.15.0") and
sklearn.__version__ <= LooseVersion("0.20.0"))
args_list = [
'tests/tests.csv',
'-is', ',',
'-target', 'class',
'-o', 'test_export.py',
'-g', '1',
'-p', '2',
'-cv', '3',
'-s', '42',
'-config', 'TPOT light',
'-v', '0'
]
args = _get_arg_parser().parse_args(args_list)
with captured_output() as (out, err):
with warnings.catch_warnings():
if handle_warning:
warnings.filterwarnings("ignore",
category=FutureWarning,
module="numpy", append=True)
tpot_driver(args)
ret_stdout = out.getvalue()
assert ret_stdout == ""
assert path.isfile("test_export.py")
remove("test_export.py") # clean up exported file
def test_read_data_file():
"""Assert that _read_data_file raises ValueError when the targe column is missing."""
# Mis-spelled target
args_list = [
'tests/tests.csv',
'-is', ',',
'-target', 'clas' # typo for right target 'class'
]
args = _get_arg_parser().parse_args(args_list)
assert_raises(ValueError, _read_data_file, args=args)
# Correctly spelled target
args_list = [
'tests/tests.csv',
'-is', ',',
'-target', 'class'
]
args = _get_arg_parser().parse_args(args_list)
input_data = _read_data_file(args)
assert isinstance(input_data, pd.DataFrame)
class ParserTest(TestCase):
def setUp(self):
self.parser = _get_arg_parser()
def test_default_param(self):
"""Assert that the TPOT driver stores correct default values for all parameters."""
args = self.parser.parse_args(['tests/tests.csv'])
self.assertEqual(args.CONFIG_FILE, None)
self.assertEqual(args.CROSSOVER_RATE, 0.1)
self.assertEqual(args.EARLY_STOP, None)
self.assertEqual(args.DISABLE_UPDATE_CHECK, False)
self.assertEqual(args.GENERATIONS, 100)
self.assertEqual(args.INPUT_FILE, 'tests/tests.csv')
self.assertEqual(args.INPUT_SEPARATOR, '\t')
self.assertEqual(args.MAX_EVAL_MINS, 5)
self.assertEqual(args.MAX_TIME_MINS, None)
self.assertEqual(args.MEMORY, None)
self.assertEqual(args.MUTATION_RATE, 0.9)
self.assertEqual(args.NUM_CV_FOLDS, 5)
self.assertEqual(args.NUM_JOBS, 1)
self.assertEqual(args.OFFSPRING_SIZE, None)
self.assertEqual(args.OUTPUT_FILE, None)
self.assertEqual(args.POPULATION_SIZE, 100)
self.assertEqual(args.RANDOM_STATE, None)
self.assertEqual(args.SUBSAMPLE, 1.0)
self.assertEqual(args.SCORING_FN, None)
self.assertEqual(args.TARGET_NAME, 'class')
self.assertEqual(args.TPOT_MODE, 'classification')
self.assertEqual(args.VERBOSITY, 1)
def test_print_args(self):
"""Assert that _print_args prints correct values for all parameters in default settings."""
args_list = [
'tests/tests.csv',
'-is', ','
]
args = self.parser.parse_args(args_list)
with captured_output() as (out, err):
_print_args(args)
output = out.getvalue()
expected_output = """
TPOT settings:
CHECKPOINT_FOLDER = None
CONFIG_FILE = None
CROSSOVER_RATE = 0.1
EARLY_STOP = None
GENERATIONS = 100
INPUT_FILE = tests/tests.csv
INPUT_SEPARATOR = ,
MAX_EVAL_MINS = 5
MAX_TIME_MINS = None
MEMORY = None
MUTATION_RATE = 0.9
NUM_CV_FOLDS = 5
NUM_JOBS = 1
OFFSPRING_SIZE = 100
OUTPUT_FILE = None
POPULATION_SIZE = 100
RANDOM_STATE = None
SCORING_FN = accuracy
SUBSAMPLE = 1.0
TARGET_NAME = class
TPOT_MODE = classification
VERBOSITY = 1
"""
print
self.assertEqual(_sort_lines(expected_output), _sort_lines(output))
def test_print_args_2(self):
"""Assert that _print_args prints correct values for all parameters in regression mode."""
args_list = [
'tests/tests.csv',
'-mode', 'regression',
'-is', ','
]
args = self.parser.parse_args(args_list)
with captured_output() as (out, err):
_print_args(args)
output = out.getvalue()
expected_output = """
TPOT settings:
CHECKPOINT_FOLDER = None
CONFIG_FILE = None
CROSSOVER_RATE = 0.1
EARLY_STOP = None
GENERATIONS = 100
INPUT_FILE = tests/tests.csv
INPUT_SEPARATOR = ,
MAX_EVAL_MINS = 5
MAX_TIME_MINS = None
MEMORY = None
MUTATION_RATE = 0.9
NUM_CV_FOLDS = 5
NUM_JOBS = 1
OFFSPRING_SIZE = 100
OUTPUT_FILE = None
POPULATION_SIZE = 100
RANDOM_STATE = None
SCORING_FN = neg_mean_squared_error
SUBSAMPLE = 1.0
TARGET_NAME = class
TPOT_MODE = regression
VERBOSITY = 1
"""
self.assertEqual(_sort_lines(expected_output), _sort_lines(output))
def _sort_lines(text):
return '\n'.join(sorted(text.split('\n')))
def test_positive_integer():
"""Assert that the TPOT CLI interface's integer parsing throws an exception when n < 0."""
assert_raises(Exception, positive_integer, '-1')
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."""
assert_raises(Exception, positive_integer, 'foobar')
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."""
assert_raises(Exception, float_range, '2.0')
def test_float_range_3():
"""Assert that the TPOT CLI interface's float range throws an exception when input is not a float."""
assert_raises(Exception, float_range, 'foobar')