From bc14266926eb6a01efe13b423437838cdbf37e93 Mon Sep 17 00:00:00 2001 From: AryanRG Date: Wed, 2 Dec 2026 05:09:07 +0530 Subject: [PATCH] BUG: preserve coarse complex details in inverse stationary wavelet transforms --- pywt/_swt.py | 13 ++++++------- pywt/tests/test_swt.py | 14 ++++++++++++++ 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/pywt/_swt.py b/pywt/_swt.py index 5588b8f7c..ab6c2ecc8 100644 --- a/pywt/_swt.py +++ b/pywt/_swt.py @@ -446,10 +446,9 @@ def iswt2(coeffs, wavelet, norm=False, axes=(-2, -1)): raise RuntimeError( "Mismatch in shape of intermediate coefficient arrays") - # make sure output shares the common dtype - # (conversion of dtype for individual coeffs is handled within idwt2 ) - common_dtype = np.result_type(*( - [dt, ] + [_check_dtype(c) for c in [cH, cV, cD]])) + # Include the approximation reconstructed from all coarser levels. + common_dtype = np.result_type(output.dtype, _check_dtype(cH), + _check_dtype(cV), _check_dtype(cD)) if output.dtype != common_dtype: output = output.astype(common_dtype) @@ -703,9 +702,9 @@ def iswtn(coeffs, wavelet, axes=None, norm=False): if not trim_approx: a = coeffs[j].pop('a'*ndim_transform) # will restore later details = coeffs[j] - # make sure dtype matches the coarsest level approximation coefficients - common_dtype = np.result_type(*( - [dt, ] + [v.dtype for v in details.values()])) + # Include the approximation reconstructed from all coarser levels. + common_dtype = np.result_type(output.dtype, + *(v.dtype for v in details.values())) if output.dtype != common_dtype: output = output.astype(common_dtype) diff --git a/pywt/tests/test_swt.py b/pywt/tests/test_swt.py index 98e722ae2..61682d409 100644 --- a/pywt/tests/test_swt.py +++ b/pywt/tests/test_swt.py @@ -534,6 +534,20 @@ def test_iswt2_mixed_dtypes(): assert_allclose(y, x, rtol=1e-3, atol=1e-3) +@pytest.mark.parametrize('axes', [(-2, -1), (0, 1)]) +def test_iswt2_complex_coarse_detail(axes): + # The equivalent axis specifications exercise iswt2 and its iswtn path. + zeros = np.zeros((8, 8)) + detail = zeros.copy() + detail[0, 0] = 1 + + def reconstruct(horizontal): + coeffs = [zeros, (horizontal, zeros, zeros), (zeros, zeros, zeros)] + return pywt.iswt2(coeffs, 'haar', axes=axes) + + assert_allclose(reconstruct(1j * detail), 1j * reconstruct(detail)) + + def test_iswtn_mixed_dtypes(): # Mixed precision inputs give double precision output rstate = np.random.RandomState(0)