From 49885120bbb0d1977821eb4e80a3a7fd9e9d41ad Mon Sep 17 00:00:00 2001 From: Arthur031221 Date: Thu, 1 Oct 2026 13:51:22 +0800 Subject: [PATCH] BUG: threshold_firm dropped the large-value restore when it was only at index 0 The check np.any(large_vals[0]) tested index values for truthiness, so a value above value_high located at index 0 of the first axis was returned shrunk instead of unchanged. Use a boolean mask. --- pywt/_thresholding.py | 5 ++--- pywt/tests/test_thresholding.py | 12 ++++++++++++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/pywt/_thresholding.py b/pywt/_thresholding.py index 10a0127d..3605dcb0 100644 --- a/pywt/_thresholding.py +++ b/pywt/_thresholding.py @@ -250,7 +250,6 @@ def threshold_firm(data, value_low, value_high): thresholded[magnitude == 0] = 0 # restore hard-thresholding behavior for values > value_high - large_vals = np.where(magnitude > value_high) - if np.any(large_vals[0]): - thresholded[large_vals] = data[large_vals] + large_vals = magnitude > value_high + thresholded[large_vals] = data[large_vals] return thresholded diff --git a/pywt/tests/test_thresholding.py b/pywt/tests/test_thresholding.py index a4fdc945..24b1979f 100644 --- a/pywt/tests/test_thresholding.py +++ b/pywt/tests/test_thresholding.py @@ -200,3 +200,15 @@ def test_threshold_zero_value_with_zeros(): assert_(not np.isnan(out_soft).any()) assert_(not np.isnan(out_garrote).any()) assert_(not np.isnan(out_firm).any()) + + +def test_threshold_firm_large_value_first(): + # Values above value_high must be returned unchanged even when the only + # such value sits at index 0 (of the first axis). + assert_allclose(pywt.threshold_firm(np.array([5.0, 0.5]), 1.0, 2.0), + [5.0, 0.0], rtol=1e-12) + assert_allclose(pywt.threshold_firm(np.array([[-5.0, 0.5], [0.5, 0.5]]), + 1.0, 2.0), + [[-5.0, 0.0], [0.0, 0.0]], rtol=1e-12) + assert_allclose(pywt.threshold_firm(np.array([3.0 + 4.0j, 0.5]), 1.0, 2.0), + [3.0 + 4.0j, 0.0], rtol=1e-12)