From 294178c213e59c420aeb1e282db9271b03c953fe Mon Sep 17 00:00:00 2001 From: AHMETHAKANBEZIR1 Date: Tue, 13 Oct 2026 15:51:59 +0300 Subject: [PATCH] Use soft thresholding for an infinite firm upper threshold --- pywt/_thresholding.py | 3 +++ pywt/tests/test_thresholding.py | 17 +++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/pywt/_thresholding.py b/pywt/_thresholding.py index 10a0127d..adf2523f 100644 --- a/pywt/_thresholding.py +++ b/pywt/_thresholding.py @@ -238,6 +238,9 @@ def threshold_firm(data, value_low, value_high): raise ValueError( "value_high must be greater than or equal to value_low.") + if np.isinf(value_high): + return soft(data, value_low) + data = np.asarray(data) magnitude = np.absolute(data) with np.errstate(divide='ignore', invalid='ignore'): diff --git a/pywt/tests/test_thresholding.py b/pywt/tests/test_thresholding.py index a4fdc945..84ec8b03 100644 --- a/pywt/tests/test_thresholding.py +++ b/pywt/tests/test_thresholding.py @@ -1,4 +1,5 @@ import numpy as np +import pytest from numpy.testing import assert_, assert_allclose, assert_equal, assert_raises import pywt @@ -200,3 +201,19 @@ 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()) + + +@pytest.mark.parametrize("dtype", [np.float32, np.float64, np.complex64, np.complex128]) +@pytest.mark.parametrize("value_low", [0.0, 2.0]) +def test_threshold_firm_infinite_upper_threshold(dtype, value_low): + data = np.array([-4, -2, -1, 0, 1, 2, 4], dtype=dtype) + if np.issubdtype(dtype, np.complexfloating): + data = data * (1 + 1j) + original = data.copy() + + actual = pywt.threshold_firm(data, value_low, np.inf) + expected = pywt.threshold(data, value_low, mode='soft') + + assert_allclose(actual, expected) + assert actual.dtype == expected.dtype + assert_allclose(data, original)