Skip to content

Commit

Permalink
Update to probmatching comments to keep in track with main
Browse files Browse the repository at this point in the history
  • Loading branch information
sidekock committed Jan 8, 2025
1 parent 48187c4 commit 9b216a7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
9 changes: 7 additions & 2 deletions pysteps/postprocessing/probmatching.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,9 @@ def _get_error(scale):
return shift, scale, R.reshape(shape)


def resample_distributions(first_array, second_array, probability_first_array, randgen):
def resample_distributions(
first_array, second_array, probability_first_array, randgen=np.random
):
"""
Merges two distributions (e.g., from the extrapolation nowcast and NWP in the blending module)
to effectively combine two distributions for probability matching without losing extremes.
Expand All @@ -287,10 +289,13 @@ def resample_distributions(first_array, second_array, probability_first_array, r
cascade). It must be of the same shape as `second_array`. Input must not contain NaNs.
second_array: array_like
One of the two arrays from which the distribution should be sampled (e.g., the NWP (model)
cascade). It must be of the same shape as `first_array`.. Input must not contain NaNs.
cascade). It must be of the same shape as `first_array`. Input must not contain NaNs.
probability_first_array: float
The weight that `first_array` should get (a value between 0 and 1). This determines the
likelihood of selecting elements from `first_array` over `second_array`.
randgen: numpy.random or numpy.RandomState
The random number generator to be used for the binomial distribution. You can pass a seeded
random state here for reproducibility. Default is numpy.random.
Returns
-------
Expand Down
18 changes: 9 additions & 9 deletions pysteps/tests/test_postprocessing_probmatching.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def test_valid_inputs(self):
second_array = np.array([2, 4, 6, 8, 10])
probability_first_array = 0.6
result = resample_distributions(
first_array, second_array, probability_first_array, np.random
first_array, second_array, probability_first_array
)
expected_result = np.array([9, 8, 6, 3, 1]) # Expected result based on the seed
assert result.shape == first_array.shape
Expand All @@ -30,7 +30,7 @@ def test_probability_zero(self):
second_array = np.array([2, 4, 6, 8, 10])
probability_first_array = 0.0
result = resample_distributions(
first_array, second_array, probability_first_array, np.random
first_array, second_array, probability_first_array
)
assert np.array_equal(result, np.sort(second_array)[::-1])

Expand All @@ -39,7 +39,7 @@ def test_probability_one(self):
second_array = np.array([2, 4, 6, 8, 10])
probability_first_array = 1.0
result = resample_distributions(
first_array, second_array, probability_first_array, np.random
first_array, second_array, probability_first_array
)
assert np.array_equal(result, np.sort(first_array)[::-1])

Expand All @@ -48,7 +48,7 @@ def test_nan_in_arr1_prob_1(self):
array_without_nan = np.array([2.0, 4, 6, 8, 10])
probability_first_array = 1.0
result = resample_distributions(
array_with_nan, array_without_nan, probability_first_array, np.random
array_with_nan, array_without_nan, probability_first_array
)
expected_result = np.array([np.nan, 9, 7, 3, 1], dtype=float)
assert np.allclose(result, expected_result, equal_nan=True)
Expand All @@ -58,7 +58,7 @@ def test_nan_in_arr1_prob_0(self):
array_without_nan = np.array([2, 4, 6, 8, 10])
probability_first_array = 0.0
result = resample_distributions(
array_with_nan, array_without_nan, probability_first_array, np.random
array_with_nan, array_without_nan, probability_first_array
)
expected_result = np.array([np.nan, 10, 8, 4, 2], dtype=float)
assert np.allclose(result, expected_result, equal_nan=True)
Expand All @@ -68,7 +68,7 @@ def test_nan_in_arr2_prob_1(self):
array_with_nan = np.array([2.0, 4, 6, np.nan, 10])
probability_first_array = 1.0
result = resample_distributions(
array_without_nan, array_with_nan, probability_first_array, np.random
array_without_nan, array_with_nan, probability_first_array
)
expected_result = np.array([np.nan, 9, 5, 3, 1], dtype=float)
assert np.allclose(result, expected_result, equal_nan=True)
Expand All @@ -78,7 +78,7 @@ def test_nan_in_arr2_prob_0(self):
array_with_nan = np.array([2, 4, 6, np.nan, 10])
probability_first_array = 0.0
result = resample_distributions(
array_without_nan, array_with_nan, probability_first_array, np.random
array_without_nan, array_with_nan, probability_first_array
)
expected_result = np.array([np.nan, 10, 6, 4, 2], dtype=float)
assert np.allclose(result, expected_result, equal_nan=True)
Expand All @@ -88,7 +88,7 @@ def test_nan_in_both_prob_1(self):
array2_with_nan = np.array([2.0, 4, np.nan, np.nan, 10])
probability_first_array = 1.0
result = resample_distributions(
array1_with_nan, array2_with_nan, probability_first_array, np.random
array1_with_nan, array2_with_nan, probability_first_array
)
expected_result = np.array([np.nan, np.nan, np.nan, 9, 1], dtype=float)
assert np.allclose(result, expected_result, equal_nan=True)
Expand All @@ -98,7 +98,7 @@ def test_nan_in_both_prob_0(self):
array2_with_nan = np.array([2.0, 4, np.nan, np.nan, 10])
probability_first_array = 0.0
result = resample_distributions(
array1_with_nan, array2_with_nan, probability_first_array, np.random
array1_with_nan, array2_with_nan, probability_first_array
)
expected_result = np.array([np.nan, np.nan, np.nan, 10, 2], dtype=float)
assert np.allclose(result, expected_result, equal_nan=True)
Expand Down

0 comments on commit 9b216a7

Please sign in to comment.