Debian Codegraph @

pywt/tests/test_wpnd.py

raw symbols SCIP
1 #!/usr/bin/env python
2
3 from __future__ import division, print_function, absolute_import
4
5 from itertools import product
6 from functools import reduce
7 import operator
8 import numpy as np
9 from numpy.testing import (assert_allclose, assert_, assert_raises,
10 assert_equal)
11
12 import pywt
13
14
15 def test_traversing_tree_nd():
16 x = np.array([[1, 2, 3, 4, 5, 6, 7, 8]] * 8, dtype=np.float64)
17 wp = pywt.WaveletPacketND(data=x, wavelet='db1', mode='symmetric')
18
19 assert_(np.all(wp.data == x))
20 assert_(wp.path == '')
21 assert_(wp.level == 0)
22 assert_(wp.maxlevel == 3)
23
24 assert_allclose(wp['aa'].data, np.array([[3., 7., 11., 15.]] * 4),
25 rtol=1e-12)
26 assert_allclose(wp['da'].data, np.zeros((4, 4)), rtol=1e-12, atol=1e-14)
27 assert_allclose(wp['ad'].data, -np.ones((4, 4)), rtol=1e-12, atol=1e-14)
28 assert_allclose(wp['dd'].data, np.zeros((4, 4)), rtol=1e-12, atol=1e-14)
29
30 assert_allclose(wp['aa'*2].data, np.array([[10., 26.]] * 2), rtol=1e-12)
31 # __getitem__ using a tuple access instead
32 assert_allclose(wp[('aa', 'aa')].data, np.array([[10., 26.]] * 2),
33 rtol=1e-12)
34
35 assert_(wp['aa']['aa'].data is wp['aa'*2].data)
36 assert_allclose(wp['aa'*3].data, np.array([[36.]]), rtol=1e-12)
37
38 assert_raises(IndexError, lambda: wp['aa'*(wp.maxlevel+1)])
39 assert_raises(ValueError, lambda: wp['f'])
40
41 # getitem input must be a string or tuple of strings
42 assert_raises(TypeError, wp.__getitem__, (5, 3))
43 assert_raises(TypeError, wp.__getitem__, 5)
44
45
46 def test_accessing_node_attributes_nd():
47 x = np.array([[1, 2, 3, 4, 5, 6, 7, 8]] * 8, dtype=np.float64)
48 wp = pywt.WaveletPacketND(data=x, wavelet='db1', mode='symmetric')
49
50 assert_allclose(wp['aa'+'ad'].data, np.zeros((2, 2)) - 4, rtol=1e-12)
51 assert_(wp['aa'+'ad'].path == 'aa'+'ad')
52 assert_(wp['aa'+'ad'].node_name == 'ad')
53 assert_(wp['aa'+'ad'].parent.path == 'aa')
54
55 assert_allclose(wp['aa'+'ad'].parent.data,
56 np.array([[3., 7., 11., 15.]] * 4), rtol=1e-12)
57 # can also index via a tuple instead of concatenated strings
58 assert_(wp[('aa', 'ad')].level == 2)
59 assert_(wp[('aa', 'ad')].maxlevel == 3)
60 assert_(wp[('aa', 'ad')].mode == 'symmetric')
61
62 # can access a node's path as either a single string or in tuple form
63 node = wp[('ad', 'dd')]
64 assert_(node.path == 'addd')
65 assert_(node.path_tuple == ('ad', 'dd'))
66
67
68 def test_collecting_nodes_nd():
69 x = np.array([[1, 2, 3, 4, 5, 6, 7, 8]] * 8, dtype=np.float64)
70 wp = pywt.WaveletPacketND(data=x, wavelet='db1', mode='symmetric')
71
72 assert_(len(wp.get_level(0)) == 1)
73 assert_(wp.get_level(0)[0].path == '')
74
75 # First level
76 assert_(len(wp.get_level(1)) == 4)
77 assert_(
78 [node.path for node in wp.get_level(1)] == ['aa', 'ad', 'da', 'dd'])
79
80 # Second and third levels
81 for lev in [2, 3]:
82 assert_(len(wp.get_level(lev)) == (2**x.ndim)**lev)
83 paths = [node.path for node in wp.get_level(lev)]
84 expected_paths = [
85 reduce(operator.add, p) for
86 p in sorted(product(['aa', 'ad', 'da', 'dd'], repeat=lev))]
87 assert_(paths == expected_paths)
88
89
90 def test_data_reconstruction_delete_nodes_nd():
91 x = np.array([[1, 2, 3, 4, 5, 6, 7, 8]] * 8, dtype=np.float64)
92 wp = pywt.WaveletPacketND(data=x, wavelet='db1', mode='symmetric')
93
94 # The user must supply either data or axes
95 assert_raises(ValueError, pywt.WaveletPacketND, data=None, wavelet='db1',
96 axes=None)
97
98 new_wp = pywt.WaveletPacketND(data=None, wavelet='db1', mode='symmetric',
99 axes=range(x.ndim))
100
101 new_wp['ad'+'da'] = wp['ad'+'da'].data
102 new_wp['ad'*2] = wp['ad'+'da'].data
103 new_wp['ad'+'dd'] = np.zeros((2, 2), dtype=np.float64)
104 new_wp['aa'] = [[3.0, 7.0, 11.0, 15.0]] * 4
105 new_wp['dd'] = np.zeros((4, 4), dtype=np.float64)
106 new_wp['da'] = wp['da'] # all zeros
107
108 assert_allclose(new_wp.reconstruct(update=False),
109 np.array([[1.5, 1.5, 3.5, 3.5, 5.5, 5.5, 7.5, 7.5]] * 8),
110 rtol=1e-12)
111
112 new_wp['ad'+'aa'] = wp['ad'+'aa'].data
113 assert_allclose(new_wp.reconstruct(update=False), x, rtol=1e-12)
114
115 del(new_wp['ad'+'aa'])
116 # TypeError on accessing deleted node
117 assert_raises(TypeError, lambda: new_wp['ad'+'aa'])
118
119 new_wp['ad'+'aa'] = wp['ad'+'aa'].data
120 assert_(new_wp.data is None)
121
122 assert_allclose(new_wp.reconstruct(update=True), x, rtol=1e-12)
123 assert_allclose(new_wp.data, x, rtol=1e-12)
124
125 # TODO: decompose=True
126
127
128 def test_wavelet_packet_dtypes():
129 shape = (16, 8, 8)
130 for dtype in [np.float32, np.float64, np.complex64, np.complex128]:
131 x = np.random.randn(*shape).astype(dtype)
132 if np.iscomplexobj(x):
133 x = x + 1j*np.random.randn(*shape).astype(x.real.dtype)
134 wp = pywt.WaveletPacketND(data=x, wavelet='db1', mode='symmetric')
135 # no unnecessary copy made
136 assert_(wp.data is x)
137
138 # full decomposition
139 wp.get_level(wp.maxlevel)
140
141 # reconstruction from coefficients should preserve dtype
142 r = wp.reconstruct(False)
143 assert_equal(r.dtype, x.dtype)
144 assert_allclose(r, x, atol=1e-6, rtol=1e-6)
145
146
147 def test_wavelet_packet_axes():
148 rstate = np.random.RandomState(0)
149 shape = (32, 16, 8)
150 x = rstate.standard_normal(shape)
151 for axes in [(0, 1), 1, (-3, -2, -1), (0, 2), (1, )]:
152 wp = pywt.WaveletPacketND(data=x, wavelet='db1', mode='symmetric',
153 axes=axes)
154
155 # partial decomposition
156 nodes = wp.get_level(1)
157 # size along the transformed axes has changed
158 for ax2 in range(x.ndim):
159 if ax2 in tuple(np.atleast_1d(axes) % x.ndim):
160 nodes[0].data.shape[ax2] < x.shape[ax2]
161 else:
162 nodes[0].data.shape[ax2] == x.shape[ax2]
163
164 # recontsruction from coefficients should preserve dtype
165 r = wp.reconstruct(False)
166 assert_equal(r.dtype, x.dtype)
167 assert_allclose(r, x, atol=1e-12, rtol=1e-12)
168
169 # must have non-duplicate axes
170 assert_raises(ValueError, pywt.WaveletPacketND, data=x, wavelet='db1',
171 axes=(0, 0))