Skip to content

Commit 2907c4d

Browse files
0.27.14
1 parent 5776cb3 commit 2907c4d

3 files changed

Lines changed: 155 additions & 3 deletions

File tree

notebooks/00_spotPython_tests.ipynb

Lines changed: 84 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13208,8 +13208,90 @@
1320813208
"cell_type": "code",
1320913209
"execution_count": null,
1321013210
"metadata": {},
13211-
"outputs": [],
13212-
"source": []
13211+
"outputs": [
13212+
{
13213+
"data": {
13214+
"text/plain": [
13215+
"array([11, 12, 13])"
13216+
]
13217+
},
13218+
"execution_count": 1,
13219+
"metadata": {},
13220+
"output_type": "execute_result"
13221+
}
13222+
],
13223+
"source": [
13224+
"import pytest\n",
13225+
"import numpy as np\n",
13226+
"from spotpython.utils.parallel import evaluate_row\n",
13227+
"\n",
13228+
"def sample_objective(row, control):\n",
13229+
" return row + control.get('offset', 0)\n",
13230+
"\n",
13231+
"def test_evaluate_row_with_list():\n",
13232+
" row = [1, 2, 3]\n",
13233+
" fun_control = {'offset': 10}\n",
13234+
" result = evaluate_row(row, sample_objective, fun_control)\n",
13235+
" result = np.squeeze(result) # Remove the extra dimension\n",
13236+
" expected_result = np.array([11, 12, 13])\n",
13237+
" assert np.array_equal(result, expected_result), f\"Expected {expected_result}, but got {result}\"\n",
13238+
"\n",
13239+
"def test_evaluate_row_with_ndarray():\n",
13240+
" row = np.array([1, 2, 3])\n",
13241+
" fun_control = {'offset': 10}\n",
13242+
" result = evaluate_row(row, sample_objective, fun_control)\n",
13243+
" result = np.squeeze(result) # Remove the extra dimension\n",
13244+
" expected_result = np.array([11, 12, 13])\n",
13245+
" assert np.array_equal(result, expected_result), f\"Expected {expected_result}, but got {result}\"\n",
13246+
"\n",
13247+
"def test_evaluate_row_without_offset():\n",
13248+
" row = np.array([4, 5, 6])\n",
13249+
" fun_control = {}\n",
13250+
" result = evaluate_row(row, sample_objective, fun_control)\n",
13251+
" result = np.squeeze(result) # Remove the extra dimension\n",
13252+
" expected_result = np.array([4, 5, 6])\n",
13253+
" assert np.array_equal(result, expected_result), f\"Expected {expected_result}, but got {result}\"\n",
13254+
"\n",
13255+
"def test_evaluate_row_with_different_offset():\n",
13256+
" row = np.array([7, 8, 9])\n",
13257+
" fun_control = {'offset': -5}\n",
13258+
" result = evaluate_row(row, sample_objective, fun_control)\n",
13259+
" result = np.squeeze(result) # Remove the extra dimension\n",
13260+
" expected_result = np.array([2, 3, 4])\n",
13261+
" assert np.array_equal(result, expected_result), f\"Expected {expected_result}, but got {result}\"\n",
13262+
"\n",
13263+
"def test_evaluate_row_with_float_values():\n",
13264+
" row = np.array([1.5, 2.5, 3.5])\n",
13265+
" fun_control = {'offset': 10}\n",
13266+
" result = evaluate_row(row, sample_objective, fun_control)\n",
13267+
" result = np.squeeze(result) # Remove the extra dimension\n",
13268+
" expected_result = np.array([11.5, 12.5, 13.5])\n",
13269+
" assert np.array_equal(result, expected_result), f\"Expected {expected_result}, but got {result}\"\n",
13270+
"\n",
13271+
"def test_evaluate_row_with_negative_values():\n",
13272+
" row = np.array([-1, -2, -3])\n",
13273+
" fun_control = {'offset': 10}\n",
13274+
" result = evaluate_row(row, sample_objective, fun_control)\n",
13275+
" result = np.squeeze(result) # Remove the extra dimension\n",
13276+
" expected_result = np.array([9, 8, 7])\n",
13277+
" assert np.array_equal(result, expected_result), f\"Expected {expected_result}, but got {result}\"\n",
13278+
"\n",
13279+
"def test_evaluate_row_with_zero_values():\n",
13280+
" row = np.array([0, 0, 0])\n",
13281+
" fun_control = {'offset': 10}\n",
13282+
" result = evaluate_row(row, sample_objective, fun_control)\n",
13283+
" result = np.squeeze(result) # Remove the extra dimension\n",
13284+
" expected_result = np.array([10, 10, 10])\n",
13285+
" assert np.array_equal(result, expected_result), f\"Expected {expected_result}, but got {result}\"\n",
13286+
"\n",
13287+
"def test_evaluate_row_with_empty_row():\n",
13288+
" row = np.array([])\n",
13289+
" fun_control = {'offset': 10}\n",
13290+
" result = evaluate_row(row, sample_objective, fun_control)\n",
13291+
" result = np.squeeze(result) # Remove the extra dimension\n",
13292+
" expected_result = np.array([])\n",
13293+
" assert np.array_equal(result, expected_result), f\"Expected {expected_result}, but got {result}\""
13294+
]
1321313295
}
1321413296
],
1321513297
"metadata": {

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.27.13"
10+
version = "0.27.14"
1111
authors = [
1212
{ name="T. Bartz-Beielstein", email="tbb@bartzundbartz.de" }
1313
]

test/test_parallel.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import pytest
2+
import numpy as np
3+
from spotpython.utils.parallel import evaluate_row
4+
5+
def sample_objective(row, control):
6+
return row + control.get('offset', 0)
7+
8+
def test_evaluate_row_with_list():
9+
row = [1, 2, 3]
10+
fun_control = {'offset': 10}
11+
result = evaluate_row(row, sample_objective, fun_control)
12+
result = np.squeeze(result) # Remove the extra dimension
13+
expected_result = np.array([11, 12, 13])
14+
assert np.array_equal(result, expected_result), f"Expected {expected_result}, but got {result}"
15+
16+
def test_evaluate_row_with_ndarray():
17+
row = np.array([1, 2, 3])
18+
fun_control = {'offset': 10}
19+
result = evaluate_row(row, sample_objective, fun_control)
20+
result = np.squeeze(result) # Remove the extra dimension
21+
expected_result = np.array([11, 12, 13])
22+
assert np.array_equal(result, expected_result), f"Expected {expected_result}, but got {result}"
23+
24+
def test_evaluate_row_without_offset():
25+
row = np.array([4, 5, 6])
26+
fun_control = {}
27+
result = evaluate_row(row, sample_objective, fun_control)
28+
result = np.squeeze(result) # Remove the extra dimension
29+
expected_result = np.array([4, 5, 6])
30+
assert np.array_equal(result, expected_result), f"Expected {expected_result}, but got {result}"
31+
32+
def test_evaluate_row_with_different_offset():
33+
row = np.array([7, 8, 9])
34+
fun_control = {'offset': -5}
35+
result = evaluate_row(row, sample_objective, fun_control)
36+
result = np.squeeze(result) # Remove the extra dimension
37+
expected_result = np.array([2, 3, 4])
38+
assert np.array_equal(result, expected_result), f"Expected {expected_result}, but got {result}"
39+
40+
def test_evaluate_row_with_float_values():
41+
row = np.array([1.5, 2.5, 3.5])
42+
fun_control = {'offset': 10}
43+
result = evaluate_row(row, sample_objective, fun_control)
44+
result = np.squeeze(result) # Remove the extra dimension
45+
expected_result = np.array([11.5, 12.5, 13.5])
46+
assert np.array_equal(result, expected_result), f"Expected {expected_result}, but got {result}"
47+
48+
def test_evaluate_row_with_negative_values():
49+
row = np.array([-1, -2, -3])
50+
fun_control = {'offset': 10}
51+
result = evaluate_row(row, sample_objective, fun_control)
52+
result = np.squeeze(result) # Remove the extra dimension
53+
expected_result = np.array([9, 8, 7])
54+
assert np.array_equal(result, expected_result), f"Expected {expected_result}, but got {result}"
55+
56+
def test_evaluate_row_with_zero_values():
57+
row = np.array([0, 0, 0])
58+
fun_control = {'offset': 10}
59+
result = evaluate_row(row, sample_objective, fun_control)
60+
result = np.squeeze(result) # Remove the extra dimension
61+
expected_result = np.array([10, 10, 10])
62+
assert np.array_equal(result, expected_result), f"Expected {expected_result}, but got {result}"
63+
64+
def test_evaluate_row_with_empty_row():
65+
row = np.array([])
66+
fun_control = {'offset': 10}
67+
result = evaluate_row(row, sample_objective, fun_control)
68+
result = np.squeeze(result) # Remove the extra dimension
69+
expected_result = np.array([])
70+
assert np.array_equal(result, expected_result), f"Expected {expected_result}, but got {result}"

0 commit comments

Comments
 (0)