Passed
Push — master ( be5baa...7f8683 )
by Simon
03:16
created

tests.test_optimizers.test_inf_nan   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 44
dl 0
loc 71
rs 10
c 0
b 0
f 0
wmc 7

4 Functions

Rating   Name   Duplication   Size   Complexity  
A objective_function_m_inf() 0 7 2
A test_best_results_0() 0 23 1
A objective_function_nan() 0 7 2
A objective_function_inf() 0 7 2
1
import pytest
2
import random
3
import numpy as np
4
5
from ._parametrize import optimizers
6
7
8
def objective_function_nan(para):
9
    rand = random.randint(0, 1)
10
11
    if rand == 0:
12
        return 1
13
    else:
14
        return np.nan
15
16
17
def objective_function_m_inf(para):
18
    rand = random.randint(0, 1)
19
20
    if rand == 0:
21
        return 1
22
    else:
23
        return -np.inf
24
25
26
def objective_function_inf(para):
27
    rand = random.randint(0, 1)
28
29
    if rand == 0:
30
        return 1
31
    else:
32
        return np.inf
33
34
35
search_space = {"x1": np.arange(0, 20, 1)}
36
37
38
objective_para = (
39
    "objective",
40
    [
41
        (objective_function_nan),
42
        (objective_function_m_inf),
43
        (objective_function_inf),
44
    ],
45
)
46
47
48
@pytest.mark.parametrize(*objective_para)
49
@pytest.mark.parametrize(*optimizers)
50
def test_best_results_0(Optimizer, objective):
51
    objective_function = objective
52
    initialize = {"random": 25}
53
54
    opt = Optimizer(search_space, initialize=initialize)
55
    opt.search(
56
        objective_function,
57
        n_iter=50,
58
        memory=False,
59
        verbosity={"print_results": False, "progress_bar": False},
60
    )
61
62
    search_data = opt.results
63
    print("\n search_data \n", search_data)
64
65
    non_inf_mask = ~np.isinf(search_data["score"].values)
66
    non_nan_mask = ~np.isnan(search_data["score"].values)
67
68
    non_inf_nan = np.sum(non_inf_mask * non_nan_mask)
69
70
    assert 10 < non_inf_nan < 40
71