From 33946ddc493bebb276ead99d46c161b09e0e39e0 Mon Sep 17 00:00:00 2001 From: Andrew Hearin Date: Tue, 7 Jan 2020 12:29:59 -0600 Subject: [PATCH] Fixed bug in treatment of noisy_percentile=-1 edge case. Resolves #939. Agreed, @mclaughlin6464? --- halotools/empirical_models/abunmatch/noisy_percentile.py | 4 +++- .../abunmatch/tests/test_noisy_percentile.py | 8 ++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/halotools/empirical_models/abunmatch/noisy_percentile.py b/halotools/empirical_models/abunmatch/noisy_percentile.py index 81663d49e..4d1b54dd9 100644 --- a/halotools/empirical_models/abunmatch/noisy_percentile.py +++ b/halotools/empirical_models/abunmatch/noisy_percentile.py @@ -147,8 +147,10 @@ def noisy_percentile(percentile, correlation_coeff, seed=None, random_percentile Correlation strengths between zero and unity span the intermediary cases. """ - if np.all(np.abs(correlation_coeff) == 1): + if np.all(correlation_coeff == 1): return percentile + elif np.all(correlation_coeff == -1): + return percentile[::-1] percentile = np.atleast_1d(percentile) correlation_coeff = np.atleast_1d(correlation_coeff) diff --git a/halotools/empirical_models/abunmatch/tests/test_noisy_percentile.py b/halotools/empirical_models/abunmatch/tests/test_noisy_percentile.py index ad28aa92c..5fc8b09d7 100644 --- a/halotools/empirical_models/abunmatch/tests/test_noisy_percentile.py +++ b/halotools/empirical_models/abunmatch/tests/test_noisy_percentile.py @@ -39,3 +39,11 @@ def test_noisy_percentile3(): u2 = noisy_percentile(u, r_desired, random_percentile=uran) r_achieved = spearmanr(u, u2)[0] assert np.allclose(r_desired, r_achieved, atol=0.02) + + +def test_negative_unity_noisy_percentile(): + """Regression test for #939. + """ + r_desired = -1 + u2 = noisy_percentile(u, r_desired, seed=fixed_seed) + assert np.allclose(u2, u[::-1])