Skip to content

Commit 8a6a118

Browse files
tests
1 parent 35dc965 commit 8a6a118

4 files changed

Lines changed: 102 additions & 32 deletions

File tree

notebooks/testKriging.ipynb

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -133,31 +133,62 @@
133133
},
134134
{
135135
"cell_type": "code",
136-
"execution_count": 2,
136+
"execution_count": 21,
137137
"metadata": {},
138-
"outputs": [
139-
{
140-
"name": "stdout",
141-
"output_type": "stream",
142-
"text": [
143-
"[1 2]\n",
144-
"[3]\n"
145-
]
146-
}
147-
],
138+
"outputs": [],
148139
"source": [
149140
"import numpy as np\n",
150141
"from spotPython.build.kriging import Kriging\n",
151142
"n=2\n",
152-
"p=4\n",
143+
"p=2\n",
153144
"S = Kriging(name='kriging', seed=124, n_theta=n, n_p=p, optim_p=True, noise=False)\n",
154145
"S.extract_from_bounds(np.array([1, 2, 3]))\n",
155-
"print(S.theta)\n",
156-
"print(S.p)\n",
146+
"assert len(S.theta) == n\n",
147+
"\n",
157148
"\n",
158149
"\n"
159150
]
160151
},
152+
{
153+
"cell_type": "markdown",
154+
"metadata": {},
155+
"source": [
156+
"## optimize_model"
157+
]
158+
},
159+
{
160+
"cell_type": "code",
161+
"execution_count": 19,
162+
"metadata": {},
163+
"outputs": [],
164+
"source": [
165+
"from spotPython.build.kriging import Kriging\n",
166+
"import numpy as np\n",
167+
"nat_X = np.array([[1, 2], [3, 4]])\n",
168+
"nat_y = np.array([1, 2])\n",
169+
"n=2\n",
170+
"p=2\n",
171+
"S=Kriging(name='kriging', seed=124, n_theta=n, n_p=p, optim_p=True, noise=True)\n",
172+
"S.initialize_variables(nat_X, nat_y)\n",
173+
"S.set_variable_types()\n",
174+
"S.nat_to_cod_init()\n",
175+
"S.set_theta_values()\n",
176+
"S.initialize_matrices()\n",
177+
"S.set_de_bounds()\n",
178+
"new_theta_p_Lambda = S.optimize_model()\n",
179+
"assert len(new_theta_p_Lambda) == n+p+1\n",
180+
"# no noise, so Lambda is not considered\n",
181+
"S=Kriging(name='kriging', seed=124, n_theta=n, n_p=p, optim_p=True, noise=False)\n",
182+
"S.initialize_variables(nat_X, nat_y)\n",
183+
"S.set_variable_types()\n",
184+
"S.nat_to_cod_init()\n",
185+
"S.set_theta_values()\n",
186+
"S.initialize_matrices()\n",
187+
"S.set_de_bounds()\n",
188+
"new_theta_p_Lambda = S.optimize_model()\n",
189+
"assert len(new_theta_p_Lambda) == n+p"
190+
]
191+
},
161192
{
162193
"cell_type": "code",
163194
"execution_count": null,

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ build-backend = "setuptools.build_meta"
77

88
[project]
99
name = "spotPython"
10-
version = "0.7.7"
10+
version = "0.7.8"
1111
authors = [
1212
{ name="T. Bartz-Beielstein", email="tbb@bartzundbartz.de" }
1313
]

src/spotPython/build/kriging.py

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -294,17 +294,16 @@ def extract_from_bounds(self, new_theta_p_Lambda: np.ndarray) -> None:
294294
1d-array with theta, p, and Lambda values. Order is important.
295295
296296
Examples:
297-
298-
>>> from spotPython.build.kriging import Kriging
299-
>>> MyClass = Kriging(name='kriging', seed=124)
300-
>>> obj = MyClass()
301-
>>> obj.extract_from_bounds(np.array([1, 2, 3]))
302-
>>> print(obj.theta)
303-
[1]
304-
>>> print(obj.p)
305-
[2]
306-
>>> print(obj.Lambda)
307-
3
297+
>>> import numpy as np
298+
from spotPython.build.kriging import Kriging
299+
n=2
300+
p=4
301+
S = Kriging(name='kriging', seed=124, n_theta=n, n_p=p, optim_p=True, noise=False)
302+
S.extract_from_bounds(np.array([1, 2, 3]))
303+
print(S.theta)
304+
print(S.p)
305+
[1 2]
306+
[3]
308307
309308
Returns:
310309
None
@@ -332,13 +331,21 @@ def optimize_model(self) -> Union[List[float], Tuple[float]]:
332331
self (object): The Kriging object.
333332
334333
Examples:
335-
336334
>>> from spotPython.build.kriging import Kriging
337-
>>> MyClass = Kriging(name='kriging', seed=124)
338-
>>> obj = MyClass()
339-
>>> result = obj.optimize_model()
340-
>>> print(result)
341-
[optimized_theta, optimized_p, optimized_Lambda]
335+
import numpy as np
336+
nat_X = np.array([[1, 2], [3, 4]])
337+
nat_y = np.array([1, 2])
338+
n=2
339+
p=2
340+
S=Kriging(name='kriging', seed=124, n_theta=n, n_p=p, optim_p=True, noise=True)
341+
S.initialize_variables(nat_X, nat_y)
342+
S.set_variable_types()
343+
S.nat_to_cod_init()
344+
S.set_theta_values()
345+
S.initialize_matrices()
346+
S.set_de_bounds()
347+
new_theta_p_Lambda = S.optimize_model()
348+
print(new_theta_p_Lambda)
342349
343350
Returns:
344351
result["x"] (Union[List[float], Tuple[float]]):

test/test_kriging.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,35 @@ def test_de_bounds():
5858
S = Kriging(name='kriging', seed=124, n_theta=n, n_p=p, optim_p=False)
5959
S.set_de_bounds()
6060
assert len(S.de_bounds) == n
61+
62+
def test_extract_from_bounds():
63+
n=2
64+
p=2
65+
S = Kriging(name='kriging', seed=124, n_theta=n, n_p=p, optim_p=True, noise=False)
66+
S.extract_from_bounds(np.array([1, 2, 3]))
67+
assert len(S.theta) == n
68+
69+
def test_optimize_model():
70+
nat_X = np.array([[1, 2], [3, 4]])
71+
nat_y = np.array([1, 2])
72+
n=2
73+
p=2
74+
S=Kriging(name='kriging', seed=124, n_theta=n, n_p=p, optim_p=True, noise=True)
75+
S.initialize_variables(nat_X, nat_y)
76+
S.set_variable_types()
77+
S.nat_to_cod_init()
78+
S.set_theta_values()
79+
S.initialize_matrices()
80+
S.set_de_bounds()
81+
new_theta_p_Lambda = S.optimize_model()
82+
assert len(new_theta_p_Lambda) == n+p+1
83+
# no noise, so Lambda is not considered
84+
S=Kriging(name='kriging', seed=124, n_theta=n, n_p=p, optim_p=True, noise=False)
85+
S.initialize_variables(nat_X, nat_y)
86+
S.set_variable_types()
87+
S.nat_to_cod_init()
88+
S.set_theta_values()
89+
S.initialize_matrices()
90+
S.set_de_bounds()
91+
new_theta_p_Lambda = S.optimize_model()
92+
assert len(new_theta_p_Lambda) == n+p

0 commit comments

Comments
 (0)