Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MAINT: add input validation to scales argument to cwt #703

Merged
merged 6 commits into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions pywt/_cwt.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,11 @@ def cwt(data, scales, wavelet, sampling_period=1., method='conv', axis=-1):
dt_cplx = np.result_type(dt, np.complex64)
if not isinstance(wavelet, (ContinuousWavelet, Wavelet)):
wavelet = DiscreteContinuousWavelet(wavelet)
if np.isscalar(scales):
scales = np.array([scales])

scales = np.atleast_1d(scales)
if np.any(scales <= 0):
raise ValueError("`scales` must only include positive values")

if not np.isscalar(axis):
raise np.AxisError("axis must be a scalar.")

Expand Down
14 changes: 14 additions & 0 deletions pywt/tests/test_cwt_wavelets.py
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,20 @@ def test_cwt_small_scales():
# extremely short scale factors raise a ValueError
assert_raises(ValueError, pywt.cwt, data, scales=0.01, wavelet='mexh')

def test_cwt_zero_scale():
data = np.zeros(32)
scales = np.arange(0, 4)

# scale that includes 0 throws ValueError to prevent IndexError
assert_raises(ValueError, pywt.cwt, data, scales=scales, wavelet='morl')

def test_cwt_negative_scale():
data = np.zeros(32)
scales = np.asarray([-1, -2, -3])

# scale that includes negative values throws ValueError to prevent IndexError
assert_raises(ValueError, pywt.cwt, data, scales=scales, wavelet='morl')


def test_cwt_method_fft():
rstate = np.random.RandomState(1)
Expand Down
Loading