|
13208 | 13208 | "cell_type": "code", |
13209 | 13209 | "execution_count": null, |
13210 | 13210 | "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 | + ] |
13213 | 13295 | } |
13214 | 13296 | ], |
13215 | 13297 | "metadata": { |
|
0 commit comments