tests.test_catch   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 82
dl 0
loc 117
rs 10
c 0
b 0
f 0
wmc 5

5 Functions

Rating   Name   Duplication   Size   Complexity  
A test_catch_2() 0 14 1
A test_catch_3() 0 14 1
A test_catch_1() 0 14 1
A test_catch_all_1() 0 28 1
A test_catch_all_0() 0 24 1
1
import copy
2
import pytest
3
import math
4
import numpy as np
5
import pandas as pd
6
7
from hyperactive import Hyperactive
8
9
10
search_space = {
11
    "x1": list(np.arange(-100, 100, 1)),
12
}
13
14
15
def test_catch_1():
16
    def objective_function(access):
17
        a = 1 + "str"
18
19
        return 0
20
21
    hyper = Hyperactive()
22
    hyper.add_search(
23
        objective_function,
24
        search_space,
25
        n_iter=100,
26
        catch={TypeError: np.nan},
27
    )
28
    hyper.run()
29
30
31
def test_catch_2():
32
    def objective_function(access):
33
        math.sqrt(-10)
34
35
        return 0
36
37
    hyper = Hyperactive()
38
    hyper.add_search(
39
        objective_function,
40
        search_space,
41
        n_iter=100,
42
        catch={ValueError: np.nan},
43
    )
44
    hyper.run()
45
46
47
def test_catch_3():
48
    def objective_function(access):
49
        x = 1 / 0
50
51
        return 0
52
53
    hyper = Hyperactive()
54
    hyper.add_search(
55
        objective_function,
56
        search_space,
57
        n_iter=100,
58
        catch={ZeroDivisionError: np.nan},
59
    )
60
    hyper.run()
61
62
63
def test_catch_all_0():
64
    def objective_function(access):
65
        a = 1 + "str"
66
        math.sqrt(-10)
67
        x = 1 / 0
68
69
        return 0
70
71
    hyper = Hyperactive()
72
    hyper.add_search(
73
        objective_function,
74
        search_space,
75
        n_iter=100,
76
        catch={
77
            TypeError: np.nan,
78
            ValueError: np.nan,
79
            ZeroDivisionError: np.nan,
80
        },
81
    )
82
    hyper.run()
83
84
    nan_ = hyper.search_data(objective_function)["score"].values[0]
85
86
    assert math.isnan(nan_)
87
88
89
def test_catch_all_1():
90
    def objective_function(access):
91
        a = 1 + "str"
92
        math.sqrt(-10)
93
        x = 1 / 0
94
95
        return 0, {"error": False}
96
97
    catch_return = (np.nan, {"error": True})
98
99
    hyper = Hyperactive()
100
    hyper.add_search(
101
        objective_function,
102
        search_space,
103
        n_iter=100,
104
        catch={
105
            TypeError: catch_return,
106
            ValueError: catch_return,
107
            ZeroDivisionError: catch_return,
108
        },
109
    )
110
    hyper.run()
111
112
    nan_ = hyper.search_data(objective_function)["score"].values[0]
113
    error_ = hyper.search_data(objective_function)["error"].values[0]
114
115
    assert math.isnan(nan_)
116
    assert error_ == True
117