Skip to content

Commit 3a9969e

Browse files
committed
TST: Add test cases for swt and dwt with zero-sized inputs.
make swt raise a ValueError for consistency with the dwt case
1 parent 1c32578 commit 3a9969e

3 files changed

Lines changed: 22 additions & 0 deletions

File tree

pywt/_extensions/_swt.pyx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ def swt_max_level(size_t input_len):
3636
multiple of ``2**n``. ``numpy.pad`` can be used to pad a signal up to an
3737
appropriate length as needed.
3838
"""
39+
if input_len < 1:
40+
raise ValueError("Cannot apply swt to a size 0 signal.")
3941
max_level = common.swt_max_level(input_len)
4042
if max_level == 0:
4143
warnings.warn(

pywt/tests/test_dwt_idwt.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,3 +223,13 @@ def test_error_on_continuous_wavelet():
223223

224224
cA, cD = pywt.dwt(data, 'db1')
225225
assert_raises(ValueError, pywt.idwt, cA, cD, cwave)
226+
227+
228+
def test_dwt_zero_size_axes():
229+
# raise on empty input array
230+
assert_raises(ValueError, pywt.dwt, [], 'db2')
231+
232+
# >1D case uses a different code path so check there as well
233+
x = np.ones((1, 4))[0:0, :] # 2D with a size zero axis
234+
assert_raises(ValueError, pywt.dwt, x, 'db2', axis=0)
235+

pywt/tests/test_swt.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,3 +525,13 @@ def test_iswtn_mixed_dtypes():
525525
y = pywt.iswtn(coeffs, wav)
526526
assert_equal(output_dtype, y.dtype)
527527
assert_allclose(y, x, rtol=1e-3, atol=1e-3)
528+
529+
530+
def test_swt_zero_size_axes():
531+
# raise on empty input array
532+
assert_raises(ValueError, pywt.swt, [], 'db2')
533+
534+
# >1D case uses a different code path so check there as well
535+
x = np.ones((1, 4))[0:0, :] # 2D with a size zero axis
536+
assert_raises(ValueError, pywt.swtn, x, 'db2', level=1, axes=(0,))
537+

0 commit comments

Comments
 (0)