Passed
Push — master ( 4bb259...06915f )
by Simon
04:09
created

tests.test_checks.model()   A

Complexity

Conditions 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nop 3
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 Hyperactive
13
14
data = load_iris()
15
X, y = data.data, data.target
16
17
18
def model(para, X, y):
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, X, y, 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
    hyper = Hyperactive(X, y, **opt_args)
38
    hyper.add_search(**search)
39
    hyper.run(time)
40
41
42
def test_model_key():
43
    search = {
44
        "model_": model,
45
        "search_space": search_space,
46
    }
47
    with pytest.raises(TypeError):
48
        _base_test(search)
49
50
51
def test_model_value():
52
    search = {
53
        "model": 1,
54
        "search_space": search_space,
55
    }
56
    with pytest.raises(ValueError):
57
        _base_test(search)
58
59
60
def test_search_space_key():
61
    search = {
62
        "model": model,
63
        "search_space_": search_space,
64
    }
65
    with pytest.raises(TypeError):
66
        _base_test(search)
67
68
69
def test_search_space_value():
70
    search = {
71
        "model": model,
72
        "search_space": 1,
73
    }
74
    with pytest.raises(ValueError):
75
        _base_test(search)
76
77
78
def test_memory_key():
79
    search = {
80
        "model": model,
81
        "search_space": search_space,
82
        "memory_": "short",
83
    }
84
    with pytest.raises(TypeError):
85
        _base_test(search)
86
87
88
def test_memory_value():
89
    search = {
90
        "model": model,
91
        "search_space": search_space,
92
        "memory": 1,
93
    }
94
    with pytest.raises(ValueError):
95
        _base_test(search)
96
97
98
def test_optimizer_key():
99
    search = {
100
        "model": model,
101
        "search_space": search_space,
102
        "optimizer_": 1,
103
    }
104
    with pytest.raises(TypeError):
105
        _base_test(search)
106
107
108
def test_optimizer_value():
109
    search = {
110
        "model": model,
111
        "search_space": search_space,
112
        "optimizer": 1,
113
    }
114
    with pytest.raises(ValueError):
115
        _base_test(search)
116
117
118
def test_n_iter_key():
119
    search = {
120
        "model": model,
121
        "search_space": search_space,
122
        "n_iter_": 10,
123
    }
124
    with pytest.raises(TypeError):
125
        _base_test(search)
126
127
128
def test_n_iter_value():
129
    search = {
130
        "model": model,
131
        "search_space": search_space,
132
        "n_iter": 0.1,
133
    }
134
    with pytest.raises(ValueError):
135
        _base_test(search)
136
137
138
def test_n_jobs_key():
139
    search = {
140
        "model": model,
141
        "search_space": search_space,
142
        "n_jobs_": 1,
143
    }
144
    with pytest.raises(TypeError):
145
        _base_test(search)
146
147
148
def test_n_jobs_value():
149
    search = {
150
        "model": model,
151
        "search_space": search_space,
152
        "n_jobs": 0.1,
153
    }
154
    with pytest.raises(ValueError):
155
        _base_test(search)
156
157
158
def test_init_para_key():
159
    search = {
160
        "model": model,
161
        "search_space": search_space,
162
        "init_para_": {},
163
    }
164
    with pytest.raises(TypeError):
165
        _base_test(search)
166
167
168
def test_init_para_value():
169
    search = {
170
        "model": model,
171
        "search_space": search_space,
172
        "init_para": 1,
173
    }
174
    with pytest.raises(ValueError):
175
        _base_test(search)
176
177