Passed
Push — master ( 910140...3a068d )
by Simon
06:34 queued 35s
created

tests.test_checks.test_memory_value()   A

Complexity

Conditions 2

Size

Total Lines 9
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
nop 0
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
# Author: Simon Blanke
2
# Email: [email protected]
3
# License: MIT License
4
5
import pytest
6
import numpy as np
7
8
from sklearn.datasets import load_iris
9
from sklearn.model_selection import cross_val_score
10
from sklearn.tree import DecisionTreeClassifier
11
12
from hyperactive import Optimizer
13
14
data = load_iris()
15
X, y = data.data, data.target
16
17
18
def objective_function(para):
19
    dtc = DecisionTreeClassifier(
20
        max_depth=para["max_depth"],
21
        min_samples_split=para["min_samples_split"],
22
        min_samples_leaf=para["min_samples_leaf"],
23
    )
24
    scores = cross_val_score(dtc, para["features"], para["target"], cv=2)
25
26
    return scores.mean()
27
28
29
search_space = {
30
    "max_depth": range(1, 21),
31
    "min_samples_split": range(2, 21),
32
    "min_samples_leaf": range(1, 21),
33
}
34
35
36
def _base_test(search, opt_args={}, time=None):
37
    opt = Optimizer(**opt_args)
38
    opt.add_search(**search)
39
    opt.run(time)
40
41
42
def test_objective_function_key():
43
    search = {
44
        "objective_function_": objective_function,
45
        "function_parameter": {"features": X, "target": y},
46
        "search_space": search_space,
47
    }
48
    with pytest.raises(TypeError):
49
        _base_test(search)
50
51
52
def test_objective_function_value():
53
    search = {
54
        "objective_function": 1,
55
        "function_parameter": {"features": X, "target": y},
56
        "search_space": search_space,
57
    }
58
    with pytest.raises(ValueError):
59
        _base_test(search)
60
61
62
def test_function_parameter_key():
63
    search = {
64
        "objective_function": objective_function,
65
        "function_parameter_": {"features": X, "target": y},
66
        "search_space": search_space,
67
    }
68
    with pytest.raises(TypeError):
69
        _base_test(search)
70
71
72
def test_function_parameter_value():
73
    search = {
74
        "objective_function": objective_function,
75
        "function_parameter": 1,
76
        "search_space": search_space,
77
    }
78
    with pytest.raises(ValueError):
79
        _base_test(search)
80
81
82
def test_search_space_key():
83
    search = {
84
        "objective_function": objective_function,
85
        "function_parameter": {"features": X, "target": y},
86
        "search_space_": search_space,
87
    }
88
    with pytest.raises(TypeError):
89
        _base_test(search)
90
91
92
def test_search_space_value():
93
    search = {
94
        "objective_function": objective_function,
95
        "function_parameter": {"features": X, "target": y},
96
        "search_space": 1,
97
    }
98
    with pytest.raises(ValueError):
99
        _base_test(search)
100
101
102
def test_memory_key():
103
    search = {
104
        "objective_function": objective_function,
105
        "function_parameter": {"features": X, "target": y},
106
        "search_space": search_space,
107
        "memory_": "short",
108
    }
109
    with pytest.raises(TypeError):
110
        _base_test(search)
111
112
113
def test_memory_value():
114
    search = {
115
        "objective_function": objective_function,
116
        "function_parameter": {"features": X, "target": y},
117
        "search_space": search_space,
118
        "memory": 1,
119
    }
120
    with pytest.raises(ValueError):
121
        _base_test(search)
122
123
124
def test_optimizer_key():
125
    search = {
126
        "objective_function": objective_function,
127
        "function_parameter": {"features": X, "target": y},
128
        "search_space": search_space,
129
        "optimizer_": 1,
130
    }
131
    with pytest.raises(TypeError):
132
        _base_test(search)
133
134
135
def test_optimizer_value():
136
    search = {
137
        "objective_function": objective_function,
138
        "function_parameter": {"features": X, "target": y},
139
        "search_space": search_space,
140
        "optimizer": 1,
141
    }
142
    with pytest.raises(ValueError):
143
        _base_test(search)
144
145
146
def test_n_iter_key():
147
    search = {
148
        "objective_function": objective_function,
149
        "function_parameter": {"features": X, "target": y},
150
        "search_space": search_space,
151
        "n_iter_": 10,
152
    }
153
    with pytest.raises(TypeError):
154
        _base_test(search)
155
156
157
def test_n_iter_value():
158
    search = {
159
        "objective_function": objective_function,
160
        "function_parameter": {"features": X, "target": y},
161
        "search_space": search_space,
162
        "n_iter": 0.1,
163
    }
164
    with pytest.raises(ValueError):
165
        _base_test(search)
166
167
168
def test_n_jobs_key():
169
    search = {
170
        "objective_function": objective_function,
171
        "function_parameter": {"features": X, "target": y},
172
        "search_space": search_space,
173
        "n_jobs_": 1,
174
    }
175
    with pytest.raises(TypeError):
176
        _base_test(search)
177
178
179
def test_n_jobs_value():
180
    search = {
181
        "objective_function": objective_function,
182
        "function_parameter": {"features": X, "target": y},
183
        "search_space": search_space,
184
        "n_jobs": 0.1,
185
    }
186
    with pytest.raises(ValueError):
187
        _base_test(search)
188
189
190
def test_init_para_key():
191
    search = {
192
        "objective_function": objective_function,
193
        "function_parameter": {"features": X, "target": y},
194
        "search_space": search_space,
195
        "init_para_": {},
196
    }
197
    with pytest.raises(TypeError):
198
        _base_test(search)
199
200
201
def test_init_para_value():
202
    search = {
203
        "objective_function": objective_function,
204
        "function_parameter": {"features": X, "target": y},
205
        "search_space": search_space,
206
        "init_para": 1,
207
    }
208
    with pytest.raises(ValueError):
209
        _base_test(search)
210
211
212
def test_distribution_key():
213
    search = {
214
        "objective_function": objective_function,
215
        "function_parameter": {"features": X, "target": y},
216
        "search_space": search_space,
217
        "distribution_": {},
218
    }
219
    with pytest.raises(TypeError):
220
        _base_test(search)
221
222
223
def test_distribution_value():
224
    search = {
225
        "objective_function": objective_function,
226
        "function_parameter": {"features": X, "target": y},
227
        "search_space": search_space,
228
        "distribution": 1,
229
    }
230
    with pytest.raises(ValueError):
231
        _base_test(search)
232
233