pywt icon indicating copy to clipboard operation
pywt copied to clipboard

IndexError on Scales with Zero

Open cyschneck opened this issue 4 months ago • 0 comments

Overview

Warning for when using invalid scale range that includes zero which is easy to accidentally do when setting up scales dynamically and would be nice to fail gracefully.

Bug and Steps to Reproduce

When running an invalid scale range (that includes zero), pywt throws an unrelated IndexError

time, sst_data = pywt.data.nino()
scales = [0, 1, 2]
wavelet_coeffs, freqs = pywt.cwt(data=sst_data,
                                 scales=scales,
                                 wavelet="morl")

Throws:

pywt/_cwt.py:154, in cwt(data, scales, wavelet, sampling_period, method, axis)
    152 if j[-1] >= int_psi.size:
    153     j = np.extract(j < int_psi.size, j)
--> 154 int_psi_scale = int_psi[j][::-1]
    156 if method == 'conv':
    157     if data.ndim == 1:

IndexError: index -9223372036854775808 is out of bounds for axis 0 with size 1024

This error actually originates on pywt/_cwt.py:150 when attempting to divide by the scale. If the scale range includes zero then it will attempt to divide by zero. However, this error is hidden by casting with astype.

For example, the example below should throw a divide by zero issue, but doesn't. It instead returns an empty array. As a result, trying to index by the empty array on line 154 will throw IndexError: index -9223372036854775808 is out of bounds for axis 0 with size 1024

For example:

j = np.arange(0 * (28-0)) / (0 * 1)
j = j.astype(int)
print(j[::-1])
--> Returns: []

Solution

Throw relevant ValueError when attempting to use a scale that includes zero

cyschneck avatar Feb 23 '24 19:02 cyschneck