Debian Codegraph @

pywt/tests/test_modes.py

raw symbols SCIP
1 #!/usr/bin/env python
2 from __future__ import division, print_function, absolute_import
3
4 import numpy as np
5 from numpy.testing import assert_raises, assert_equal, assert_allclose
6
7 import pywt
8
9
10 def test_available_modes():
11 modes = ['zero', 'constant', 'symmetric', 'periodic', 'smooth',
12 'periodization', 'reflect', 'antisymmetric', 'antireflect']
13 assert_equal(pywt.Modes.modes, modes)
14 assert_equal(pywt.Modes.from_object('constant'), 2)
15
16
17 def test_invalid_modes():
18 x = np.arange(4)
19 assert_raises(ValueError, pywt.dwt, x, 'db2', 'unknown')
20 assert_raises(ValueError, pywt.dwt, x, 'db2', -1)
21 assert_raises(ValueError, pywt.dwt, x, 'db2', 9)
22 assert_raises(TypeError, pywt.dwt, x, 'db2', None)
23
24 assert_raises(ValueError, pywt.Modes.from_object, 'unknown')
25 assert_raises(ValueError, pywt.Modes.from_object, -1)
26 assert_raises(ValueError, pywt.Modes.from_object, 9)
27 assert_raises(TypeError, pywt.Modes.from_object, None)
28
29
30 def test_dwt_idwt_allmodes():
31 # Test that :func:`dwt` and :func:`idwt` can be performed using every mode
32 x = [1, 2, 1, 5, -1, 8, 4, 6]
33 dwt_results = {
34 'zero': ([-0.03467518, 1.73309178, 3.40612438, 6.32928585, 6.95094948],
35 [-0.12940952, -2.15599552, -5.95034847, -1.21545369,
36 -1.8625013]),
37 'constant': ([1.28480404, 1.73309178, 3.40612438, 6.32928585,
38 7.51935555],
39 [-0.48296291, -2.15599552, -5.95034847, -1.21545369,
40 0.25881905]),
41 'symmetric': ([1.76776695, 1.73309178, 3.40612438, 6.32928585,
42 7.77817459],
43 [-0.61237244, -2.15599552, -5.95034847, -1.21545369,
44 1.22474487]),
45 'reflect': ([2.12132034, 1.73309178, 3.40612438, 6.32928585,
46 6.81224877],
47 [-0.70710678, -2.15599552, -5.95034847, -1.21545369,
48 -2.38013939]),
49 'periodic': ([6.9162743, 1.73309178, 3.40612438, 6.32928585,
50 6.9162743],
51 [-1.99191082, -2.15599552, -5.95034847, -1.21545369,
52 -1.99191082]),
53 'smooth': ([-0.51763809, 1.73309178, 3.40612438, 6.32928585,
54 7.45000519],
55 [0, -2.15599552, -5.95034847, -1.21545369, 0]),
56 'periodization': ([4.053172, 3.05257099, 2.85381112, 8.42522221],
57 [0.18946869, 4.18258152, 4.33737503, 2.60428326]),
58 'antisymmetric': ([-1.83711731, 1.73309178, 3.40612438, 6.32928585,
59 6.12372436],
60 [0.353553391, -2.15599552, -5.95034847, -1.21545369,
61 -4.94974747]),
62 'antireflect': ([0.44828774, 1.73309178, 3.40612438, 6.32928585,
63 8.22646233],
64 [-0.25881905, -2.15599552, -5.95034847, -1.21545369,
65 2.89777748])
66 }
67
68 for mode in pywt.Modes.modes:
69 cA, cD = pywt.dwt(x, 'db2', mode)
70 assert_allclose(cA, dwt_results[mode][0], rtol=1e-7, atol=1e-8)
71 assert_allclose(cD, dwt_results[mode][1], rtol=1e-7, atol=1e-8)
72 assert_allclose(pywt.idwt(cA, cD, 'db2', mode), x, rtol=1e-10)
73
74
75 def test_dwt_short_input_allmodes():
76 # some test cases where the input is shorter than the DWT filter
77 x = [1, 3, 2]
78 wavelet = 'db2'
79 # manually pad each end by the filter size (4 for 'db2' used here)
80 padded_x = {'zero': [0, 0, 0, 0, 1, 3, 2, 0, 0, 0, 0],
81 'constant': [1, 1, 1, 1, 1, 3, 2, 2, 2, 2, 2],
82 'symmetric': [2, 2, 3, 1, 1, 3, 2, 2, 3, 1, 1],
83 'reflect': [1, 3, 2, 3, 1, 3, 2, 3, 1, 3, 2],
84 'periodic': [2, 1, 3, 2, 1, 3, 2, 1, 3, 2, 1],
85 'smooth': [-7, -5, -3, -1, 1, 3, 2, 1, 0, -1, -2],
86 'antisymmetric': [2, -2, -3, -1, 1, 3, 2, -2, -3, -1, 1],
87 'antireflect': [1, -1, 0, -1, 1, 3, 2, 1, 3, 5, 4],
88 }
89 for mode, xpad in padded_x.items():
90 # DWT of the manually padded array. will discard edges later so
91 # symmetric mode used here doesn't matter.
92 cApad, cDpad = pywt.dwt(xpad, wavelet, mode='symmetric')
93
94 # central region of the padded output (unaffected by mode )
95 expected_result = (cApad[2:-2], cDpad[2:-2])
96
97 cA, cD = pywt.dwt(x, wavelet, mode)
98 assert_allclose(cA, expected_result[0], rtol=1e-7, atol=1e-8)
99 assert_allclose(cD, expected_result[1], rtol=1e-7, atol=1e-8)
100
101
102 def test_default_mode():
103 # The default mode should be 'symmetric'
104 x = [1, 2, 1, 5, -1, 8, 4, 6]
105 cA, cD = pywt.dwt(x, 'db2')
106 cA2, cD2 = pywt.dwt(x, 'db2', mode='symmetric')
107 assert_allclose(cA, cA2)
108 assert_allclose(cD, cD2)
109 assert_allclose(pywt.idwt(cA, cD, 'db2'), x)