|
1 | 1 | import numpy as np
|
2 | 2 | import pytest
|
3 | 3 | from pysteps.postprocessing.probmatching import resample_distributions
|
| 4 | +from pysteps.postprocessing.probmatching import nonparam_match_empirical_cdf |
4 | 5 |
|
5 | 6 |
|
6 | 7 | class TestResampleDistributions:
|
@@ -39,19 +40,161 @@ def test_probability_one(self):
|
39 | 40 | )
|
40 | 41 | assert np.array_equal(result, np.sort(first_array)[::-1])
|
41 | 42 |
|
42 |
| - def test_nan_in_inputs(self): |
| 43 | + def test_nan_in_arr1_prob_1(self): |
| 44 | + array_with_nan = np.array([1, 3, np.nan, 7, 9]) |
| 45 | + array_without_nan = np.array([2.0, 4, 6, 8, 10]) |
| 46 | + probability_first_array = 1.0 |
| 47 | + result = resample_distributions( |
| 48 | + array_with_nan, array_without_nan, probability_first_array |
| 49 | + ) |
| 50 | + expected_result = np.array([np.nan, 9, 7, 3, 1], dtype=float) |
| 51 | + assert np.allclose(result, expected_result, equal_nan=True) |
| 52 | + |
| 53 | + def test_nan_in_arr1_prob_0(self): |
43 | 54 | array_with_nan = np.array([1, 3, np.nan, 7, 9])
|
44 | 55 | array_without_nan = np.array([2, 4, 6, 8, 10])
|
45 |
| - probability_first_array = 0.6 |
46 |
| - with pytest.raises(ValueError, match="Input arrays must not contain NaNs"): |
47 |
| - resample_distributions( |
48 |
| - array_with_nan, array_without_nan, probability_first_array |
49 |
| - ) |
50 |
| - with pytest.raises(ValueError, match="Input arrays must not contain NaNs"): |
51 |
| - resample_distributions( |
52 |
| - array_without_nan, array_with_nan, probability_first_array |
53 |
| - ) |
54 |
| - with pytest.raises(ValueError, match="Input arrays must not contain NaNs"): |
55 |
| - resample_distributions( |
56 |
| - array_with_nan, array_with_nan, probability_first_array |
57 |
| - ) |
| 56 | + probability_first_array = 0.0 |
| 57 | + result = resample_distributions( |
| 58 | + array_with_nan, array_without_nan, probability_first_array |
| 59 | + ) |
| 60 | + expected_result = np.array([np.nan, 10, 8, 4, 2], dtype=float) |
| 61 | + assert np.allclose(result, expected_result, equal_nan=True) |
| 62 | + |
| 63 | + def test_nan_in_arr2_prob_1(self): |
| 64 | + array_without_nan = np.array([1, 3, 5, 7, 9]) |
| 65 | + array_with_nan = np.array([2.0, 4, 6, np.nan, 10]) |
| 66 | + probability_first_array = 1.0 |
| 67 | + result = resample_distributions( |
| 68 | + array_without_nan, array_with_nan, probability_first_array |
| 69 | + ) |
| 70 | + expected_result = np.array([np.nan, 9, 5, 3, 1], dtype=float) |
| 71 | + assert np.allclose(result, expected_result, equal_nan=True) |
| 72 | + |
| 73 | + def test_nan_in_arr2_prob_0(self): |
| 74 | + array_without_nan = np.array([1, 3, 5, 7, 9]) |
| 75 | + array_with_nan = np.array([2, 4, 6, np.nan, 10]) |
| 76 | + probability_first_array = 0.0 |
| 77 | + result = resample_distributions( |
| 78 | + array_without_nan, array_with_nan, probability_first_array |
| 79 | + ) |
| 80 | + expected_result = np.array([np.nan, 10, 6, 4, 2], dtype=float) |
| 81 | + assert np.allclose(result, expected_result, equal_nan=True) |
| 82 | + |
| 83 | + def test_nan_in_both_prob_1(self): |
| 84 | + array1_with_nan = np.array([1, np.nan, np.nan, 7, 9]) |
| 85 | + array2_with_nan = np.array([2.0, 4, np.nan, np.nan, 10]) |
| 86 | + probability_first_array = 1.0 |
| 87 | + result = resample_distributions( |
| 88 | + array1_with_nan, array2_with_nan, probability_first_array |
| 89 | + ) |
| 90 | + expected_result = np.array([np.nan, np.nan, np.nan, 9, 1], dtype=float) |
| 91 | + assert np.allclose(result, expected_result, equal_nan=True) |
| 92 | + |
| 93 | + def test_nan_in_both_prob_0(self): |
| 94 | + array1_with_nan = np.array([1, np.nan, np.nan, 7, 9]) |
| 95 | + array2_with_nan = np.array([2.0, 4, np.nan, np.nan, 10]) |
| 96 | + probability_first_array = 0.0 |
| 97 | + result = resample_distributions( |
| 98 | + array1_with_nan, array2_with_nan, probability_first_array |
| 99 | + ) |
| 100 | + expected_result = np.array([np.nan, np.nan, np.nan, 10, 2], dtype=float) |
| 101 | + assert np.allclose(result, expected_result, equal_nan=True) |
| 102 | + |
| 103 | + |
| 104 | +class TestNonparamMatchEmpiricalCDF: |
| 105 | + @pytest.fixture(autouse=True) |
| 106 | + def setup(self): |
| 107 | + # Set the seed for reproducibility |
| 108 | + np.random.seed(42) |
| 109 | + |
| 110 | + def test_ignore_indices_with_nans_both(self): |
| 111 | + initial_array = np.array([np.nan, np.nan, 6, 2, 0, 0, 0, 0, 0, 0]) |
| 112 | + target_array = np.array([np.nan, np.nan, 9, 5, 4, 0, 0, 0, 0, 0]) |
| 113 | + result = nonparam_match_empirical_cdf( |
| 114 | + initial_array, target_array, ignore_indices=np.isnan(initial_array) |
| 115 | + ) |
| 116 | + expected_result = np.array([np.nan, np.nan, 9, 5, 0, 0, 0, 0, 0, 0]) |
| 117 | + assert np.allclose(result, expected_result, equal_nan=True) |
| 118 | + |
| 119 | + def test_zeroes_initial(self): |
| 120 | + initial_array = np.zeros(10) |
| 121 | + target_array = np.array([0, 2, 3, 4, 5, 6, 7, 8, 9, 10]) |
| 122 | + result = nonparam_match_empirical_cdf(initial_array, target_array) |
| 123 | + expected_result = np.zeros(10) |
| 124 | + assert np.allclose(result, expected_result) |
| 125 | + |
| 126 | + def test_nans_initial(self): |
| 127 | + initial_array = np.array( |
| 128 | + [0, 1, 2, 3, 4, np.nan, np.nan, np.nan, np.nan, np.nan] |
| 129 | + ) |
| 130 | + target_array = np.array([0, 2, 3, 4, 5, 6, 7, 8, 9, 10]) |
| 131 | + with pytest.raises( |
| 132 | + ValueError, |
| 133 | + match="Initial array contains non-finite values outside ignore_indices mask.", |
| 134 | + ): |
| 135 | + nonparam_match_empirical_cdf(initial_array, target_array) |
| 136 | + |
| 137 | + def test_all_nans_initial(self): |
| 138 | + initial_array = np.full(10, np.nan) |
| 139 | + target_array = np.array([0, 2, 3, 4, 5, 6, 7, 8, 9, 10]) |
| 140 | + with pytest.raises(ValueError, match="Initial array contains only nans."): |
| 141 | + nonparam_match_empirical_cdf(initial_array, target_array) |
| 142 | + |
| 143 | + def test_ignore_indices_nans_initial(self): |
| 144 | + initial_array = np.array( |
| 145 | + [0, 1, 2, 3, 4, np.nan, np.nan, np.nan, np.nan, np.nan] |
| 146 | + ) |
| 147 | + target_array = np.array([0, 2, 3, 4, 5, 6, 7, 8, 9, 10]) |
| 148 | + result = nonparam_match_empirical_cdf( |
| 149 | + initial_array, target_array, ignore_indices=np.isnan(initial_array) |
| 150 | + ) |
| 151 | + expected_result = np.array( |
| 152 | + [0, 7, 8, 9, 10, np.nan, np.nan, np.nan, np.nan, np.nan] |
| 153 | + ) |
| 154 | + assert np.allclose(result, expected_result, equal_nan=True) |
| 155 | + |
| 156 | + def test_ignore_indices_nans_target(self): |
| 157 | + # We expect the initial_array values for which ignore_indices is true to be conserved as-is. |
| 158 | + initial_array = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) |
| 159 | + target_array = np.array( |
| 160 | + [0, 2, 3, 4, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan] |
| 161 | + ) |
| 162 | + result = nonparam_match_empirical_cdf( |
| 163 | + initial_array, target_array, ignore_indices=np.isnan(target_array) |
| 164 | + ) |
| 165 | + expected_result = np.array([0, 2, 3, 4, 4, 5, 6, 7, 8, 9]) |
| 166 | + assert np.allclose(result, expected_result, equal_nan=True) |
| 167 | + |
| 168 | + def test_more_zeroes_in_initial(self): |
| 169 | + initial_array = np.array([1, 4, 0, 0, 0, 0, 0, 0, 0, 0]) |
| 170 | + target_array = np.array([10, 8, 6, 4, 2, 0, 0, 0, 0, 0]) |
| 171 | + result = nonparam_match_empirical_cdf( |
| 172 | + initial_array, target_array, ignore_indices=np.isnan(initial_array) |
| 173 | + ) |
| 174 | + expected_result = np.array([8, 10, 0, 0, 0, 0, 0, 0, 0, 0]) |
| 175 | + assert np.allclose(result, expected_result, equal_nan=True) |
| 176 | + |
| 177 | + def test_more_zeroes_in_initial_unsrt(self): |
| 178 | + initial_array = np.array([1, 4, 0, 0, 0, 0, 0, 0, 0, 0]) |
| 179 | + target_array = np.array([6, 4, 2, 0, 0, 0, 0, 0, 10, 8]) |
| 180 | + result = nonparam_match_empirical_cdf( |
| 181 | + initial_array, target_array, ignore_indices=np.isnan(initial_array) |
| 182 | + ) |
| 183 | + expected_result = np.array([8, 10, 0, 0, 0, 0, 0, 0, 0, 0]) |
| 184 | + assert np.allclose(result, expected_result, equal_nan=True) |
| 185 | + |
| 186 | + def test_more_zeroes_in_target(self): |
| 187 | + initial_array = np.array([1, 3, 7, 5, 0, 0, 0, 0, 0, 0]) |
| 188 | + target_array = np.array([10, 8, 0, 0, 0, 0, 0, 0, 0, 0]) |
| 189 | + result = nonparam_match_empirical_cdf( |
| 190 | + initial_array, target_array, ignore_indices=np.isnan(initial_array) |
| 191 | + ) |
| 192 | + expected_result = np.array([0, 0, 10, 8, 0, 0, 0, 0, 0, 0]) |
| 193 | + assert np.allclose(result, expected_result, equal_nan=True) |
| 194 | + |
| 195 | + def test_2dim_array(self): |
| 196 | + initial_array = np.array([[1, 3, 5], [11, 9, 7]]) |
| 197 | + target_array = np.array([[2, 4, 6], [8, 10, 12]]) |
| 198 | + result = nonparam_match_empirical_cdf(initial_array, target_array) |
| 199 | + expected_result = np.array([[2, 4, 6], [12, 10, 8]]) |
| 200 | + assert np.allclose(result, expected_result, equal_nan=True) |
0 commit comments