|
1 | 1 | import numpy as np |
2 | 2 | from spotPython.utils.repair import remove_nan |
3 | 3 | import pytest |
| 4 | +import warnings |
| 5 | + |
| 6 | + |
| 7 | +def test_remove_nan_dimension_error(): |
| 8 | + # Case that should raise ValueError |
| 9 | + X = np.array([[1, 2]]) |
| 10 | + y = np.array([np.nan]) |
| 11 | + with pytest.raises(ValueError): |
| 12 | + X_cleaned, y_cleaned = remove_nan(X, y, stop_on_zero_return=True) |
4 | 13 |
|
5 | 14 |
|
6 | 15 | def test_remove_nan_no_nan(): |
@@ -30,9 +39,44 @@ def test_remove_nan_dimension_warning(): |
30 | 39 | assert y_cleaned.shape[0] < y.shape[0], "Expected dimension reduction did not trigger a warning." |
31 | 40 |
|
32 | 41 |
|
33 | | -def test_remove_nan_dimension_error(): |
34 | | - # Case that should raise ValueError |
35 | | - X = np.array([[1, 2]]) |
36 | | - y = np.array([np.nan]) |
37 | | - with pytest.raises(ValueError): |
| 42 | +def test_remove_nan_basic(): |
| 43 | + X = np.array([[1, 2], [3, 4], [5, 6]]) |
| 44 | + y = np.array([1, np.nan, 2]) |
| 45 | + X_cleaned, y_cleaned = remove_nan(X, y) |
| 46 | + np.testing.assert_array_equal(X_cleaned, np.array([[1, 2], [5, 6]])) |
| 47 | + np.testing.assert_array_equal(y_cleaned, np.array([1, 2])) |
| 48 | + |
| 49 | + |
| 50 | +def test_remove_nan_warning(): |
| 51 | + X = np.array([[1, 2], [3, 4], [5, 6]]) |
| 52 | + y = np.array([1, np.nan, 2]) |
| 53 | + with warnings.catch_warnings(record=True) as w: |
| 54 | + warnings.simplefilter("always") |
38 | 55 | X_cleaned, y_cleaned = remove_nan(X, y) |
| 56 | + assert len(w) == 2 |
| 57 | + assert issubclass(w[-1].category, UserWarning) |
| 58 | + assert "smaller than the original dimension" in str(w[0].message) |
| 59 | + assert "Check whether to continue with the reduced dimension is useful" in str(w[1].message) |
| 60 | + |
| 61 | + |
| 62 | +def test_remove_nan_value_error(): |
| 63 | + X = np.array([[1, 2], [3, 4], [5, 6]]) |
| 64 | + y = np.array([np.nan, np.nan, np.nan]) |
| 65 | + with pytest.raises(ValueError): |
| 66 | + remove_nan(X, y, stop_on_zero_return=True) |
| 67 | + |
| 68 | + |
| 69 | +def test_no_nan(): |
| 70 | + X = np.array([[1, 2], [3, 4], [5, 6]]) |
| 71 | + y = np.array([1, 2, 3]) |
| 72 | + X_cleaned, y_cleaned = remove_nan(X, y) |
| 73 | + np.testing.assert_array_equal(X_cleaned, X) |
| 74 | + np.testing.assert_array_equal(y_cleaned, y) |
| 75 | + |
| 76 | + |
| 77 | +def test_remove_nan_empty_X(): |
| 78 | + X = np.array([[], [], []]) |
| 79 | + y = np.array([1, np.nan, 2]) |
| 80 | + X_cleaned, y_cleaned = remove_nan(X, y) |
| 81 | + np.testing.assert_array_equal(X_cleaned, np.array([[], []])) |
| 82 | + np.testing.assert_array_equal(y_cleaned, np.array([1, 2])) |
0 commit comments