@@ -165,3 +165,38 @@ def test_threshold_firm():
165165 mt_abs_firm = np .abs (d_firm [mt ])
166166 assert_ (np .all (mt_abs_firm < np .abs (d_hard [mt ])))
167167 assert_ (np .all (mt_abs_firm > np .abs (d_soft [mt ])))
168+
169+
170+ def test_threshold_zero_value_with_zeros ():
171+ # Issue 866: value==0 and exact-zero data used to yield NaN (0/0).
172+ data = np .array ([0.0 , 1.0 , - 2.0 ])
173+ expected = np .array ([0.0 , 1.0 , - 2.0 ])
174+
175+ assert_allclose (pywt .threshold (data , 0.0 , 'soft' ), expected , rtol = 1e-12 )
176+ assert_allclose (pywt .threshold (data , 0.0 , 'garrote' ), expected , rtol = 1e-12 )
177+ assert_allclose (pywt .threshold_firm (data , 0.0 , 0.0 ), expected , rtol = 1e-12 )
178+
179+ # all zeros remain zeros (and must not warn: pytest treats warnings as errors)
180+ zeros = np .zeros (8 )
181+ assert_allclose (pywt .threshold (zeros , 0.0 , 'soft' ), zeros , rtol = 1e-12 )
182+ assert_allclose (pywt .threshold (zeros , 0.0 , 'garrote' ), zeros , rtol = 1e-12 )
183+ assert_allclose (pywt .threshold_firm (zeros , 0.0 , 0.0 ), zeros , rtol = 1e-12 )
184+
185+ # complex: same 0/0 path, result must be 0+0j not nan+nanj
186+ cdata = np .array ([0.0 , 1.0 + 1.0j ])
187+ cexpected = np .array ([0.0 + 0.0j , 1.0 + 1.0j ])
188+ assert_allclose (pywt .threshold (cdata , 0.0 , 'soft' ), cexpected , rtol = 1e-12 )
189+ assert_allclose (pywt .threshold (cdata , 0.0 , 'garrote' ), cexpected , rtol = 1e-12 )
190+ assert_allclose (pywt .threshold_firm (cdata , 0.0 , 0.0 ), cexpected , rtol = 1e-12 )
191+
192+ for dtype in float_dtypes :
193+ typed = np .asarray (data if dtype in real_dtypes else cdata , dtype = dtype )
194+ out_soft = pywt .threshold (typed , 0.0 , 'soft' )
195+ out_garrote = pywt .threshold (typed , 0.0 , 'garrote' )
196+ out_firm = pywt .threshold_firm (typed , 0.0 , 0.0 )
197+ assert_equal (out_soft .dtype , typed .dtype )
198+ assert_equal (out_garrote .dtype , typed .dtype )
199+ assert_equal (out_firm .dtype , typed .dtype )
200+ assert_ (not np .isnan (out_soft ).any ())
201+ assert_ (not np .isnan (out_garrote ).any ())
202+ assert_ (not np .isnan (out_firm ).any ())
0 commit comments