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_0(): |
||
16 | def objective_function(access): |
||
17 | x = y |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Loading history...
|
|||
18 | |||
19 | return 0 |
||
20 | |||
21 | hyper = Hyperactive() |
||
22 | hyper.add_search( |
||
23 | objective_function, |
||
24 | search_space, |
||
25 | n_iter=100, |
||
26 | catch={NameError: np.nan}, |
||
27 | ) |
||
28 | hyper.run() |
||
29 | |||
30 | |||
31 | def test_catch_1(): |
||
32 | def objective_function(access): |
||
33 | a = 1 + "str" |
||
34 | |||
35 | return 0 |
||
36 | |||
37 | hyper = Hyperactive() |
||
38 | hyper.add_search( |
||
39 | objective_function, |
||
40 | search_space, |
||
41 | n_iter=100, |
||
42 | catch={TypeError: np.nan}, |
||
43 | ) |
||
44 | hyper.run() |
||
45 | |||
46 | |||
47 | def test_catch_2(): |
||
48 | def objective_function(access): |
||
49 | math.sqrt(-10) |
||
50 | |||
51 | return 0 |
||
52 | |||
53 | hyper = Hyperactive() |
||
54 | hyper.add_search( |
||
55 | objective_function, |
||
56 | search_space, |
||
57 | n_iter=100, |
||
58 | catch={ValueError: np.nan}, |
||
59 | ) |
||
60 | hyper.run() |
||
61 | |||
62 | |||
63 | def test_catch_3(): |
||
64 | def objective_function(access): |
||
65 | x = 1 / 0 |
||
66 | |||
67 | return 0 |
||
68 | |||
69 | hyper = Hyperactive() |
||
70 | hyper.add_search( |
||
71 | objective_function, |
||
72 | search_space, |
||
73 | n_iter=100, |
||
74 | catch={ZeroDivisionError: np.nan}, |
||
75 | ) |
||
76 | hyper.run() |
||
77 | |||
78 | |||
79 | def test_catch_all_0(): |
||
80 | def objective_function(access): |
||
81 | x = y |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
82 | a = 1 + "str" |
||
83 | math.sqrt(-10) |
||
84 | x = 1 / 0 |
||
85 | |||
86 | return 0 |
||
87 | |||
88 | hyper = Hyperactive() |
||
89 | hyper.add_search( |
||
90 | objective_function, |
||
91 | search_space, |
||
92 | n_iter=100, |
||
93 | catch={ |
||
94 | NameError: np.nan, |
||
95 | TypeError: np.nan, |
||
96 | ValueError: np.nan, |
||
97 | ZeroDivisionError: np.nan, |
||
98 | }, |
||
99 | ) |
||
100 | hyper.run() |
||
101 | |||
102 | nan_ = hyper.search_data(objective_function)["score"].values[0] |
||
103 | |||
104 | assert math.isnan(nan_) |
||
105 | |||
106 | |||
107 | def test_catch_all_1(): |
||
108 | def objective_function(access): |
||
109 | x = y |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
110 | a = 1 + "str" |
||
111 | math.sqrt(-10) |
||
112 | x = 1 / 0 |
||
113 | |||
114 | return 0, {"error": False} |
||
115 | |||
116 | catch_return = (np.nan, {"error": True}) |
||
117 | |||
118 | hyper = Hyperactive() |
||
119 | hyper.add_search( |
||
120 | objective_function, |
||
121 | search_space, |
||
122 | n_iter=100, |
||
123 | catch={ |
||
124 | NameError: catch_return, |
||
125 | TypeError: catch_return, |
||
126 | ValueError: catch_return, |
||
127 | ZeroDivisionError: catch_return, |
||
128 | }, |
||
129 | ) |
||
130 | hyper.run() |
||
131 | |||
132 | nan_ = hyper.search_data(objective_function)["score"].values[0] |
||
133 | error_ = hyper.search_data(objective_function)["error"].values[0] |
||
134 | |||
135 | assert math.isnan(nan_) |
||
136 | assert error_ == True |
||
137 |