Debian Codegraph @

pywt/tests/test_cwt_wavelets.py

raw symbols SCIP
1 #!/usr/bin/env python
2 import os
3 from itertools import product
4 import pickle
5
6 from numpy.testing import (assert_allclose, assert_warns, assert_almost_equal,
7 assert_raises, assert_equal)
8 import pytest
9 import numpy as np
10 import pywt
11
12
13 def ref_gaus(LB, UB, N, num):
14 X = np.linspace(LB, UB, N)
15 F0 = (2./np.pi)**(1./4.)*np.exp(-(X**2))
16 if (num == 1):
17 psi = -2.*X*F0
18 elif (num == 2):
19 psi = -2/(3**(1/2))*(-1 + 2*X**2)*F0
20 elif (num == 3):
21 psi = -4/(15**(1/2))*X*(3 - 2*X**2)*F0
22 elif (num == 4):
23 psi = 4/(105**(1/2))*(3 - 12*X**2 + 4*X**4)*F0
24 elif (num == 5):
25 psi = 8/(3*(105**(1/2)))*X*(-15 + 20*X**2 - 4*X**4)*F0
26 elif (num == 6):
27 psi = -8/(3*(1155**(1/2)))*(-15 + 90*X**2 - 60*X**4 + 8*X**6)*F0
28 elif (num == 7):
29 psi = -16/(3*(15015**(1/2)))*X*(105 - 210*X**2 + 84*X**4 - 8*X**6)*F0
30 elif (num == 8):
31 psi = 16/(45*(1001**(1/2)))*(105 - 840*X**2 + 840*X**4 -
32 224*X**6 + 16*X**8)*F0
33 return (psi, X)
34
35
36 def ref_cgau(LB, UB, N, num):
37 X = np.linspace(LB, UB, N)
38 F0 = np.exp(-X**2)
39 F1 = np.exp(-1j*X)
40 F2 = (F1*F0)/(np.exp(-1/2)*2**(1/2)*np.pi**(1/2))**(1/2)
41 if (num == 1):
42 psi = F2*(-1j - 2*X)*2**(1/2)
43 elif (num == 2):
44 psi = 1/3*F2*(-3 + 4j*X + 4*X**2)*6**(1/2)
45 elif (num == 3):
46 psi = 1/15*F2*(7j + 18*X - 12j*X**2 - 8*X**3)*30**(1/2)
47 elif (num == 4):
48 psi = 1/105*F2*(25 - 56j*X - 72*X**2 + 32j*X**3 + 16*X**4)*210**(1/2)
49 elif (num == 5):
50 psi = 1/315*F2*(-81j - 250*X + 280j*X**2 + 240*X**3 -
51 80j*X**4 - 32*X**5)*210**(1/2)
52 elif (num == 6):
53 psi = 1/3465*F2*(-331 + 972j*X + 1500*X**2 - 1120j*X**3 - 720*X**4 +
54 192j*X**5 + 64*X**6)*2310**(1/2)
55 elif (num == 7):
56 psi = 1/45045*F2*(
57 1303j + 4634*X - 6804j*X**2 - 7000*X**3 + 3920j*X**4 + 2016*X**5 -
58 448j*X**6 - 128*X**7)*30030**(1/2)
59 elif (num == 8):
60 psi = 1/45045*F2*(
61 5937 - 20848j*X - 37072*X**2 + 36288j*X**3 + 28000*X**4 -
62 12544j*X**5 - 5376*X**6 + 1024j*X**7 + 256*X**8)*2002**(1/2)
63
64 psi = psi/np.real(np.sqrt(np.real(np.sum(psi*np.conj(psi)))*(X[1] - X[0])))
65 return (psi, X)
66
67
68 def sinc2(x):
69 y = np.ones_like(x)
70 k = np.where(x)[0]
71 y[k] = np.sin(np.pi*x[k])/(np.pi*x[k])
72 return y
73
74
75 def ref_shan(LB, UB, N, Fb, Fc):
76 x = np.linspace(LB, UB, N)
77 psi = np.sqrt(Fb)*(sinc2(Fb*x)*np.exp(2j*np.pi*Fc*x))
78 return (psi, x)
79
80
81 def ref_fbsp(LB, UB, N, m, Fb, Fc):
82 x = np.linspace(LB, UB, N)
83 psi = np.sqrt(Fb)*((sinc2(Fb*x/m)**m)*np.exp(2j*np.pi*Fc*x))
84 return (psi, x)
85
86
87 def ref_cmor(LB, UB, N, Fb, Fc):
88 x = np.linspace(LB, UB, N)
89 psi = ((np.pi*Fb)**(-0.5))*np.exp(2j*np.pi*Fc*x)*np.exp(-(x**2)/Fb)
90 return (psi, x)
91
92
93 def ref_morl(LB, UB, N):
94 x = np.linspace(LB, UB, N)
95 psi = np.exp(-(x**2)/2)*np.cos(5*x)
96 return (psi, x)
97
98
99 def ref_mexh(LB, UB, N):
100 x = np.linspace(LB, UB, N)
101 psi = (2/(np.sqrt(3)*np.pi**0.25))*np.exp(-(x**2)/2)*(1 - (x**2))
102 return (psi, x)
103
104
105 def test_gaus():
106 LB = -5
107 UB = 5
108 N = 1000
109 for num in np.arange(1, 9):
110 [psi, x] = ref_gaus(LB, UB, N, num)
111 w = pywt.ContinuousWavelet("gaus" + str(num))
112 PSI, X = w.wavefun(length=N)
113
114 assert_allclose(np.real(PSI), np.real(psi))
115 assert_allclose(np.imag(PSI), np.imag(psi))
116 assert_allclose(X, x)
117
118
119 @pytest.mark.parametrize('dtype', [np.float32, np.float64])
120 def test_continuous_wavelet_dtype(dtype):
121 wavelet = pywt.ContinuousWavelet('cmor1.5-1.0', dtype)
122 int_psi, x = pywt.integrate_wavelet(wavelet)
123 assert int_psi.real.dtype == dtype
124 assert x.dtype == dtype
125
126
127 def test_continuous_wavelet_invalid_dtype():
128 with pytest.raises(ValueError):
129 pywt.ContinuousWavelet('gaus5', np.complex64)
130 with pytest.raises(ValueError):
131 pywt.ContinuousWavelet('gaus5', np.int_)
132
133
134 def test_cgau():
135 LB = -5
136 UB = 5
137 N = 1000
138 for num in np.arange(1, 9):
139 [psi, x] = ref_cgau(LB, UB, N, num)
140 w = pywt.ContinuousWavelet("cgau" + str(num))
141 PSI, X = w.wavefun(length=N)
142
143 assert_allclose(np.real(PSI), np.real(psi))
144 assert_allclose(np.imag(PSI), np.imag(psi))
145 assert_allclose(X, x)
146
147
148 def test_shan():
149 LB = -20
150 UB = 20
151 N = 1000
152 Fb = 1
153 Fc = 1.5
154
155 [psi, x] = ref_shan(LB, UB, N, Fb, Fc)
156 w = pywt.ContinuousWavelet("shan{}-{}".format(Fb, Fc))
157 assert_almost_equal(w.center_frequency, Fc)
158 assert_almost_equal(w.bandwidth_frequency, Fb)
159 w.upper_bound = UB
160 w.lower_bound = LB
161 PSI, X = w.wavefun(length=N)
162
163 assert_allclose(np.real(PSI), np.real(psi), atol=1e-15)
164 assert_allclose(np.imag(PSI), np.imag(psi), atol=1e-15)
165 assert_allclose(X, x, atol=1e-15)
166
167 LB = -20
168 UB = 20
169 N = 1000
170 Fb = 1.5
171 Fc = 1
172
173 [psi, x] = ref_shan(LB, UB, N, Fb, Fc)
174 w = pywt.ContinuousWavelet("shan{}-{}".format(Fb, Fc))
175 assert_almost_equal(w.center_frequency, Fc)
176 assert_almost_equal(w.bandwidth_frequency, Fb)
177 w.upper_bound = UB
178 w.lower_bound = LB
179 PSI, X = w.wavefun(length=N)
180
181 assert_allclose(np.real(PSI), np.real(psi), atol=1e-15)
182 assert_allclose(np.imag(PSI), np.imag(psi), atol=1e-15)
183 assert_allclose(X, x, atol=1e-15)
184
185
186 def test_cmor():
187 LB = -20
188 UB = 20
189 N = 1000
190 Fb = 1
191 Fc = 1.5
192
193 [psi, x] = ref_cmor(LB, UB, N, Fb, Fc)
194 w = pywt.ContinuousWavelet("cmor{}-{}".format(Fb, Fc))
195 assert_almost_equal(w.center_frequency, Fc)
196 assert_almost_equal(w.bandwidth_frequency, Fb)
197 w.upper_bound = UB
198 w.lower_bound = LB
199 PSI, X = w.wavefun(length=N)
200
201 assert_allclose(np.real(PSI), np.real(psi), atol=1e-15)
202 assert_allclose(np.imag(PSI), np.imag(psi), atol=1e-15)
203 assert_allclose(X, x, atol=1e-15)
204
205 LB = -20
206 UB = 20
207 N = 1000
208 Fb = 1.5
209 Fc = 1
210
211 [psi, x] = ref_cmor(LB, UB, N, Fb, Fc)
212 w = pywt.ContinuousWavelet("cmor{}-{}".format(Fb, Fc))
213 assert_almost_equal(w.center_frequency, Fc)
214 assert_almost_equal(w.bandwidth_frequency, Fb)
215 w.upper_bound = UB
216 w.lower_bound = LB
217 PSI, X = w.wavefun(length=N)
218
219 assert_allclose(np.real(PSI), np.real(psi), atol=1e-15)
220 assert_allclose(np.imag(PSI), np.imag(psi), atol=1e-15)
221 assert_allclose(X, x, atol=1e-15)
222
223
224 def test_fbsp():
225 LB = -20
226 UB = 20
227 N = 1000
228 M = 2
229 Fb = 1
230 Fc = 1.5
231
232 [psi, x] = ref_fbsp(LB, UB, N, M, Fb, Fc)
233
234 w = pywt.ContinuousWavelet("fbsp{}-{}-{}".format(M, Fb, Fc))
235 assert_almost_equal(w.center_frequency, Fc)
236 assert_almost_equal(w.bandwidth_frequency, Fb)
237 w.fbsp_order = M
238 w.upper_bound = UB
239 w.lower_bound = LB
240 PSI, X = w.wavefun(length=N)
241
242 assert_allclose(np.real(PSI), np.real(psi), atol=1e-15)
243 assert_allclose(np.imag(PSI), np.imag(psi), atol=1e-15)
244 assert_allclose(X, x, atol=1e-15)
245
246 LB = -20
247 UB = 20
248 N = 1000
249 M = 2
250 Fb = 1.5
251 Fc = 1
252
253 [psi, x] = ref_fbsp(LB, UB, N, M, Fb, Fc)
254 w = pywt.ContinuousWavelet("fbsp{}-{}-{}".format(M, Fb, Fc))
255 assert_almost_equal(w.center_frequency, Fc)
256 assert_almost_equal(w.bandwidth_frequency, Fb)
257 w.fbsp_order = M
258 w.upper_bound = UB
259 w.lower_bound = LB
260 PSI, X = w.wavefun(length=N)
261
262 assert_allclose(np.real(PSI), np.real(psi), atol=1e-15)
263 assert_allclose(np.imag(PSI), np.imag(psi), atol=1e-15)
264 assert_allclose(X, x, atol=1e-15)
265
266 LB = -20
267 UB = 20
268 N = 1000
269 M = 3
270 Fb = 1.5
271 Fc = 1.2
272
273 [psi, x] = ref_fbsp(LB, UB, N, M, Fb, Fc)
274 w = pywt.ContinuousWavelet("fbsp{}-{}-{}".format(M, Fb, Fc))
275 assert_almost_equal(w.center_frequency, Fc)
276 assert_almost_equal(w.bandwidth_frequency, Fb)
277 w.fbsp_order = M
278 w.upper_bound = UB
279 w.lower_bound = LB
280 PSI, X = w.wavefun(length=N)
281 # TODO: investigate why atol = 1e-5 is necessary
282 assert_allclose(np.real(PSI), np.real(psi), atol=1e-5)
283 assert_allclose(np.imag(PSI), np.imag(psi), atol=1e-5)
284 assert_allclose(X, x, atol=1e-15)
285
286
287 def test_morl():
288 LB = -5
289 UB = 5
290 N = 1000
291
292 [psi, x] = ref_morl(LB, UB, N)
293 w = pywt.ContinuousWavelet("morl")
294 w.upper_bound = UB
295 w.lower_bound = LB
296 PSI, X = w.wavefun(length=N)
297
298 assert_allclose(np.real(PSI), np.real(psi))
299 assert_allclose(np.imag(PSI), np.imag(psi))
300 assert_allclose(X, x)
301
302
303 def test_mexh():
304 LB = -5
305 UB = 5
306 N = 1000
307
308 [psi, x] = ref_mexh(LB, UB, N)
309 w = pywt.ContinuousWavelet("mexh")
310 w.upper_bound = UB
311 w.lower_bound = LB
312 PSI, X = w.wavefun(length=N)
313
314 assert_allclose(np.real(PSI), np.real(psi))
315 assert_allclose(np.imag(PSI), np.imag(psi))
316 assert_allclose(X, x)
317
318 LB = -5
319 UB = 5
320 N = 1001
321
322 [psi, x] = ref_mexh(LB, UB, N)
323 w = pywt.ContinuousWavelet("mexh")
324 w.upper_bound = UB
325 w.lower_bound = LB
326 PSI, X = w.wavefun(length=N)
327
328 assert_allclose(np.real(PSI), np.real(psi))
329 assert_allclose(np.imag(PSI), np.imag(psi))
330 assert_allclose(X, x)
331
332
333 def test_cwt_parameters_in_names():
334
335 for func in [pywt.ContinuousWavelet, pywt.DiscreteContinuousWavelet]:
336 for name in ['fbsp', 'cmor', 'shan']:
337 # additional parameters should be specified within the name
338 assert_warns(FutureWarning, func, name)
339
340 for name in ['cmor', 'shan']:
341 # valid names
342 func(name + '1.5-1.0')
343 func(name + '1-4')
344
345 # invalid names
346 assert_raises(ValueError, func, name + '1.0')
347 assert_raises(ValueError, func, name + 'B-C')
348 assert_raises(ValueError, func, name + '1.0-1.0-1.0')
349
350 # valid names
351 func('fbsp1-1.5-1.0')
352 func('fbsp1.0-1.5-1')
353 func('fbsp2-5-1')
354
355 # invalid name (non-integer order)
356 assert_raises(ValueError, func, 'fbsp1.5-1-1')
357 assert_raises(ValueError, func, 'fbspM-B-C')
358
359 # invalid name (too few or too many params)
360 assert_raises(ValueError, func, 'fbsp1.0')
361 assert_raises(ValueError, func, 'fbsp1.0-0.4')
362 assert_raises(ValueError, func, 'fbsp1-1-1-1')
363
364
365 @pytest.mark.parametrize('dtype, tol, method',
366 [(np.float32, 1e-5, 'conv'),
367 (np.float32, 1e-5, 'fft'),
368 (np.float64, 1e-13, 'conv'),
369 (np.float64, 1e-13, 'fft')])
370 def test_cwt_complex(dtype, tol, method):
371 time, sst = pywt.data.nino()
372 sst = np.asarray(sst, dtype=dtype)
373 dt = time[1] - time[0]
374 wavelet = 'cmor1.5-1.0'
375 scales = np.arange(1, 32)
376
377 # real-valued tranfsorm as a reference
378 [cfs, f] = pywt.cwt(sst, scales, wavelet, dt, method=method)
379
380 # verify same precision
381 assert_equal(cfs.real.dtype, sst.dtype)
382
383 # complex-valued transform equals sum of the transforms of the real
384 # and imaginary components
385 sst_complex = sst + 1j*sst
386 [cfs_complex, f] = pywt.cwt(sst_complex, scales, wavelet, dt,
387 method=method)
388 assert_allclose(cfs + 1j*cfs, cfs_complex, atol=tol, rtol=tol)
389 # verify dtype is preserved
390 assert_equal(cfs_complex.dtype, sst_complex.dtype)
391
392
393 @pytest.mark.parametrize('axis, method', product([0, 1], ['conv', 'fft']))
394 def test_cwt_batch(axis, method):
395 dtype = np.float64
396 time, sst = pywt.data.nino()
397 n_batch = 8
398 batch_axis = 1 - axis
399 sst1 = np.asarray(sst, dtype=dtype)
400 sst = np.stack((sst1, ) * n_batch, axis=batch_axis)
401 dt = time[1] - time[0]
402 wavelet = 'cmor1.5-1.0'
403 scales = np.arange(1, 32)
404
405 # non-batch transform as reference
406 [cfs1, f] = pywt.cwt(sst1, scales, wavelet, dt, method=method, axis=axis)
407
408 shape_in = sst.shape
409 [cfs, f] = pywt.cwt(sst, scales, wavelet, dt, method=method, axis=axis)
410
411 # shape of input is not modified
412 assert_equal(shape_in, sst.shape)
413
414 # verify same precision
415 assert_equal(cfs.real.dtype, sst.dtype)
416
417 # verify expected shape
418 assert_equal(cfs.shape[0], len(scales))
419 assert_equal(cfs.shape[1 + batch_axis], n_batch)
420 assert_equal(cfs.shape[1 + axis], sst.shape[axis])
421
422 # batch result on stacked input is the same as stacked 1d result
423 assert_almost_equal(cfs, np.stack((cfs1,) * n_batch, axis=batch_axis + 1),
424 decimal=12)
425
426
427 def test_cwt_small_scales():
428 data = np.zeros(32)
429
430 # A scale of 0.1 was chosen specifically to give a filter of length 2 for
431 # mexh. This corner case should not raise an error.
432 cfs, f = pywt.cwt(data, scales=0.1, wavelet='mexh')
433 assert_allclose(cfs, np.zeros_like(cfs))
434
435 # extremely short scale factors raise a ValueError
436 assert_raises(ValueError, pywt.cwt, data, scales=0.01, wavelet='mexh')
437
438
439 def test_cwt_method_fft():
440 rstate = np.random.RandomState(1)
441 data = rstate.randn(50)
442 data[15] = 1.
443 scales = np.arange(1, 64)
444 wavelet = 'cmor1.5-1.0'
445
446 # build a reference cwt with the legacy np.conv() method
447 cfs_conv, _ = pywt.cwt(data, scales, wavelet, method='conv')
448
449 # compare with the fft based convolution
450 cfs_fft, _ = pywt.cwt(data, scales, wavelet, method='fft')
451 assert_allclose(cfs_conv, cfs_fft, rtol=0, atol=1e-13)
452
453
454 def test_continuous_wavelet_pickle(tmpdir):
455 wavelet = pywt.ContinuousWavelet('cmor1.5-1.0')
456 filename = os.path.join(tmpdir, 'cwav.pickle')
457 with open(filename, 'wb') as f:
458 pickle.dump(wavelet, f)
459 with open(filename, 'rb') as f:
460 wavelet2 = pickle.load(f)
461 assert isinstance(wavelet2, pywt.ContinuousWavelet)
462 assert wavelet2.name == wavelet.name