Debian Codegraph @

doc/source/regression/wp2d.rst

raw
1 .. _reg-wp2d:
2
3 .. currentmodule:: pywt
4
5
6 2D Wavelet Packets
7 ==================
8
9 Import pywt
10 -----------
11
12 >>> from __future__ import print_function
13 >>> import pywt
14 >>> import numpy
15
16
17 Create 2D Wavelet Packet structure
18 ----------------------------------
19
20 Start with preparing test data:
21
22 >>> x = numpy.array([[1, 2, 3, 4, 5, 6, 7, 8]] * 8, 'd')
23 >>> print(x)
24 [[ 1. 2. 3. 4. 5. 6. 7. 8.]
25 [ 1. 2. 3. 4. 5. 6. 7. 8.]
26 [ 1. 2. 3. 4. 5. 6. 7. 8.]
27 [ 1. 2. 3. 4. 5. 6. 7. 8.]
28 [ 1. 2. 3. 4. 5. 6. 7. 8.]
29 [ 1. 2. 3. 4. 5. 6. 7. 8.]
30 [ 1. 2. 3. 4. 5. 6. 7. 8.]
31 [ 1. 2. 3. 4. 5. 6. 7. 8.]]
32
33 Now create a :class:`2D Wavelet Packet <WaveletPacket2D>` object:
34
35 >>> wp = pywt.WaveletPacket2D(data=x, wavelet='db1', mode='symmetric')
36
37 The input ``data`` and decomposition coefficients are stored in the
38 :attr:`WaveletPacket2D.data` attribute:
39
40 >>> print(wp.data)
41 [[ 1. 2. 3. 4. 5. 6. 7. 8.]
42 [ 1. 2. 3. 4. 5. 6. 7. 8.]
43 [ 1. 2. 3. 4. 5. 6. 7. 8.]
44 [ 1. 2. 3. 4. 5. 6. 7. 8.]
45 [ 1. 2. 3. 4. 5. 6. 7. 8.]
46 [ 1. 2. 3. 4. 5. 6. 7. 8.]
47 [ 1. 2. 3. 4. 5. 6. 7. 8.]
48 [ 1. 2. 3. 4. 5. 6. 7. 8.]]
49
50 :class:`Nodes <Node2D>` are identified by paths. For the root node the path is
51 ``''`` and the decomposition level is ``0``.
52
53 >>> print(repr(wp.path))
54 ''
55 >>> print(wp.level)
56 0
57
58 The :attr:`WaveletPacket2D.maxlevel`, if not given in the constructor, is
59 automatically computed based on the data size:
60
61 >>> print(wp.maxlevel)
62 3
63
64
65 Traversing WP tree:
66 -------------------
67
68 Wavelet Packet :class:`nodes <Node2D>` are arranged in a tree. Each node in a WP
69 tree is uniquely identified and addressed by a :attr:`~Node2D.path` string.
70
71 In the 1D :class:`WaveletPacket` case nodes were accessed using ``'a'``
72 (approximation) and ``'d'`` (details) path names (each node has two 1D
73 children).
74
75 Because now we deal with a bit more complex structure (each node has four
76 children), we have four basic path names based on the dwt 2D output convention
77 to address the WP2D structure:
78
79 * ``a`` - LL, low-low coefficients
80 * ``h`` - LH, low-high coefficients
81 * ``v`` - HL, high-low coefficients
82 * ``d`` - HH, high-high coefficients
83
84 In other words, subnode naming corresponds to the :func:`dwt2` function output
85 naming convention (as wavelet packet transform is based on the dwt2 transform)::
86
87 -------------------
88 | | |
89 | cA(LL) | cH(LH) |
90 | | |
91 (cA, (cH, cV, cD)) <---> -------------------
92 | | |
93 | cV(HL) | cD(HH) |
94 | | |
95 -------------------
96
97 (fig.1: DWT 2D output and interpretation)
98
99
100 Knowing what the nodes names are, we can now access them using the indexing
101 operator ``obj[x]`` (:meth:`WaveletPacket2D.__getitem__`):
102
103 >>> print(wp['a'].data)
104 [[ 3. 7. 11. 15.]
105 [ 3. 7. 11. 15.]
106 [ 3. 7. 11. 15.]
107 [ 3. 7. 11. 15.]]
108 >>> print(wp['h'].data)
109 [[ 0. 0. 0. 0.]
110 [ 0. 0. 0. 0.]
111 [ 0. 0. 0. 0.]
112 [ 0. 0. 0. 0.]]
113 >>> print(wp['v'].data)
114 [[-1. -1. -1. -1.]
115 [-1. -1. -1. -1.]
116 [-1. -1. -1. -1.]
117 [-1. -1. -1. -1.]]
118 >>> print(wp['d'].data)
119 [[ 0. 0. 0. 0.]
120 [ 0. 0. 0. 0.]
121 [ 0. 0. 0. 0.]
122 [ 0. 0. 0. 0.]]
123
124 Similarly, a subnode of a subnode can be accessed by:
125
126 >>> print(wp['aa'].data)
127 [[ 10. 26.]
128 [ 10. 26.]]
129
130 Indexing base :class:`WaveletPacket2D` (as well as 1D :class:`WaveletPacket`)
131 using compound path is just the same as indexing WP subnode:
132
133 >>> node = wp['a']
134 >>> print(node['a'].data)
135 [[ 10. 26.]
136 [ 10. 26.]]
137 >>> print(wp['a']['a'].data is wp['aa'].data)
138 True
139
140 Following down the decomposition path:
141
142 >>> print(wp['aaa'].data)
143 [[ 36.]]
144 >>> print(wp['aaaa'].data)
145 Traceback (most recent call last):
146 ...
147 IndexError: Path length is out of range.
148
149 Ups, we have reached the maximum level of decomposition for the ``'aaaa'`` path,
150 which btw. was:
151
152 >>> print(wp.maxlevel)
153 3
154
155
156 Now try some invalid path:
157
158 >>> print(wp['f'])
159 Traceback (most recent call last):
160 ...
161 ValueError: Subnode name must be in ['a', 'h', 'v', 'd'], not 'f'.
162
163
164 Accessing Node2D's attributes:
165 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
166
167 :class:`WaveletPacket2D` is a tree data structure, which evaluates to a set
168 of :class:`Node2D` objects. :class:`WaveletPacket2D` is just a special subclass
169 of the :class:`Node2D` class (which in turn inherits from a :class:`BaseNode`,
170 just like with :class:`Node` and :class:`WaveletPacket` for the 1D case.).
171
172 >>> print(wp['av'].data)
173 [[-4. -4.]
174 [-4. -4.]]
175
176 >>> print(wp['av'].path)
177 av
178
179 >>> print(wp['av'].node_name)
180 v
181
182 >>> print(wp['av'].parent.path)
183 a
184
185 >>> print(wp['av'].parent.data)
186 [[ 3. 7. 11. 15.]
187 [ 3. 7. 11. 15.]
188 [ 3. 7. 11. 15.]
189 [ 3. 7. 11. 15.]]
190
191 >>> print(wp['av'].level)
192 2
193
194 >>> print(wp['av'].maxlevel)
195 3
196
197 >>> print(wp['av'].mode)
198 symmetric
199
200
201 Collecting nodes
202 ~~~~~~~~~~~~~~~~
203
204 We can get all nodes on the particular level using the
205 :meth:`WaveletPacket2D.get_level` method:
206
207 * 0 level - the root `wp` node:
208
209 >>> len(wp.get_level(0))
210 1
211 >>> print([node.path for node in wp.get_level(0)])
212 ['']
213
214 * 1st level of decomposition:
215
216 >>> len(wp.get_level(1))
217 4
218 >>> print([node.path for node in wp.get_level(1)])
219 ['a', 'h', 'v', 'd']
220
221 * 2nd level of decomposition:
222
223 >>> len(wp.get_level(2))
224 16
225 >>> paths = [node.path for node in wp.get_level(2)]
226 >>> for i, path in enumerate(paths):
227 ... if (i+1) % 4 == 0:
228 ... print(path)
229 ... else:
230 ... print(path, end=' ')
231 aa ah av ad
232 ha hh hv hd
233 va vh vv vd
234 da dh dv dd
235
236 * 3rd level of decomposition:
237
238 >>> print(len(wp.get_level(3)))
239 64
240 >>> paths = [node.path for node in wp.get_level(3)]
241 >>> for i, path in enumerate(paths):
242 ... if (i+1) % 8 == 0:
243 ... print(path)
244 ... else:
245 ... print(path, end=' ')
246 aaa aah aav aad aha ahh ahv ahd
247 ava avh avv avd ada adh adv add
248 haa hah hav had hha hhh hhv hhd
249 hva hvh hvv hvd hda hdh hdv hdd
250 vaa vah vav vad vha vhh vhv vhd
251 vva vvh vvv vvd vda vdh vdv vdd
252 daa dah dav dad dha dhh dhv dhd
253 dva dvh dvv dvd dda ddh ddv ddd
254
255 Note that :meth:`WaveletPacket2D.get_level` performs automatic decomposition
256 until it reaches the given level.
257
258
259 Reconstructing data from Wavelet Packets:
260 -----------------------------------------
261
262 Let's create a new empty 2D Wavelet Packet structure and set its nodes
263 values with known data from the previous examples:
264
265 >>> new_wp = pywt.WaveletPacket2D(data=None, wavelet='db1', mode='symmetric')
266
267 >>> new_wp['vh'] = wp['vh'].data # [[0.0, 0.0], [0.0, 0.0]]
268 >>> new_wp['vv'] = wp['vh'].data # [[0.0, 0.0], [0.0, 0.0]]
269 >>> new_wp['vd'] = [[0.0, 0.0], [0.0, 0.0]]
270
271 >>> new_wp['a'] = [[3.0, 7.0, 11.0, 15.0], [3.0, 7.0, 11.0, 15.0],
272 ... [3.0, 7.0, 11.0, 15.0], [3.0, 7.0, 11.0, 15.0]]
273 >>> new_wp['d'] = [[0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0],
274 ... [0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0]]
275
276 For convenience, :attr:`Node2D.data` gets automatically extracted from the
277 base :class:`Node2D` object:
278
279 >>> new_wp['h'] = wp['h'] # all zeros
280
281 Note: just remember to not assign to the node.data parameter directly (todo).
282
283 And reconstruct the data from the ``a``, ``d``, ``vh``, ``vv``, ``vd`` and ``h``
284 packets (Note that ``va`` node was not set and the WP tree is "not complete"
285 - the ``va`` branch will be treated as *zero-array*):
286
287 >>> print(new_wp.reconstruct(update=False))
288 [[ 1.5 1.5 3.5 3.5 5.5 5.5 7.5 7.5]
289 [ 1.5 1.5 3.5 3.5 5.5 5.5 7.5 7.5]
290 [ 1.5 1.5 3.5 3.5 5.5 5.5 7.5 7.5]
291 [ 1.5 1.5 3.5 3.5 5.5 5.5 7.5 7.5]
292 [ 1.5 1.5 3.5 3.5 5.5 5.5 7.5 7.5]
293 [ 1.5 1.5 3.5 3.5 5.5 5.5 7.5 7.5]
294 [ 1.5 1.5 3.5 3.5 5.5 5.5 7.5 7.5]
295 [ 1.5 1.5 3.5 3.5 5.5 5.5 7.5 7.5]]
296
297 Now set the ``va`` node with the known values and do the reconstruction again:
298
299 >>> new_wp['va'] = wp['va'].data # [[-2.0, -2.0], [-2.0, -2.0]]
300 >>> print(new_wp.reconstruct(update=False))
301 [[ 1. 2. 3. 4. 5. 6. 7. 8.]
302 [ 1. 2. 3. 4. 5. 6. 7. 8.]
303 [ 1. 2. 3. 4. 5. 6. 7. 8.]
304 [ 1. 2. 3. 4. 5. 6. 7. 8.]
305 [ 1. 2. 3. 4. 5. 6. 7. 8.]
306 [ 1. 2. 3. 4. 5. 6. 7. 8.]
307 [ 1. 2. 3. 4. 5. 6. 7. 8.]
308 [ 1. 2. 3. 4. 5. 6. 7. 8.]]
309
310 which is just the same as the base sample data ``x``.
311
312 Of course we can go the other way and remove nodes from the tree. If we delete
313 the ``va`` node, again, we get the "not complete" tree from one of the previous
314 examples:
315
316 >>> del new_wp['va']
317 >>> print(new_wp.reconstruct(update=False))
318 [[ 1.5 1.5 3.5 3.5 5.5 5.5 7.5 7.5]
319 [ 1.5 1.5 3.5 3.5 5.5 5.5 7.5 7.5]
320 [ 1.5 1.5 3.5 3.5 5.5 5.5 7.5 7.5]
321 [ 1.5 1.5 3.5 3.5 5.5 5.5 7.5 7.5]
322 [ 1.5 1.5 3.5 3.5 5.5 5.5 7.5 7.5]
323 [ 1.5 1.5 3.5 3.5 5.5 5.5 7.5 7.5]
324 [ 1.5 1.5 3.5 3.5 5.5 5.5 7.5 7.5]
325 [ 1.5 1.5 3.5 3.5 5.5 5.5 7.5 7.5]]
326
327 Just restore the node before next examples.
328
329 >>> new_wp['va'] = wp['va'].data
330
331 If the ``update`` param in the :meth:`WaveletPacket2D.reconstruct` method is set
332 to ``False``, the node's :attr:`Node2D.data` attribute will not be updated.
333
334 >>> print(new_wp.data)
335 None
336
337 Otherwise, the :attr:`WaveletPacket2D.data` attribute will be set to the
338 reconstructed value.
339
340 >>> print(new_wp.reconstruct(update=True))
341 [[ 1. 2. 3. 4. 5. 6. 7. 8.]
342 [ 1. 2. 3. 4. 5. 6. 7. 8.]
343 [ 1. 2. 3. 4. 5. 6. 7. 8.]
344 [ 1. 2. 3. 4. 5. 6. 7. 8.]
345 [ 1. 2. 3. 4. 5. 6. 7. 8.]
346 [ 1. 2. 3. 4. 5. 6. 7. 8.]
347 [ 1. 2. 3. 4. 5. 6. 7. 8.]
348 [ 1. 2. 3. 4. 5. 6. 7. 8.]]
349 >>> print(new_wp.data)
350 [[ 1. 2. 3. 4. 5. 6. 7. 8.]
351 [ 1. 2. 3. 4. 5. 6. 7. 8.]
352 [ 1. 2. 3. 4. 5. 6. 7. 8.]
353 [ 1. 2. 3. 4. 5. 6. 7. 8.]
354 [ 1. 2. 3. 4. 5. 6. 7. 8.]
355 [ 1. 2. 3. 4. 5. 6. 7. 8.]
356 [ 1. 2. 3. 4. 5. 6. 7. 8.]
357 [ 1. 2. 3. 4. 5. 6. 7. 8.]]
358
359 Since we have an interesting WP structure built, it is a good occasion to
360 present the :meth:`WaveletPacket2D.get_leaf_nodes` method, which collects
361 non-zero leaf nodes from the WP tree:
362
363 >>> print([n.path for n in new_wp.get_leaf_nodes()])
364 ['a', 'h', 'va', 'vh', 'vv', 'vd', 'd']
365
366 Passing the ``decompose = True`` parameter to the method will force the WP
367 object to do a full decomposition up to the *maximum level* of decomposition:
368
369 >>> paths = [n.path for n in new_wp.get_leaf_nodes(decompose=True)]
370 >>> len(paths)
371 64
372 >>> for i, path in enumerate(paths):
373 ... if (i+1) % 8 == 0:
374 ... print(path)
375 ... else:
376 ... try:
377 ... print(path, end=' ')
378 ... except:
379 ... print(path, end=' ')
380 aaa aah aav aad aha ahh ahv ahd
381 ava avh avv avd ada adh adv add
382 haa hah hav had hha hhh hhv hhd
383 hva hvh hvv hvd hda hdh hdv hdd
384 vaa vah vav vad vha vhh vhv vhd
385 vva vvh vvv vvd vda vdh vdv vdd
386 daa dah dav dad dha dhh dhv dhd
387 dva dvh dvv dvd dda ddh ddv ddd
388
389 Lazy evaluation:
390 ----------------
391
392 .. note:: This section is for demonstration of pywt internals purposes
393 only. Do not rely on the attribute access to nodes as presented in
394 this example.
395
396 >>> x = numpy.array([[1, 2, 3, 4, 5, 6, 7, 8]] * 8)
397 >>> wp = pywt.WaveletPacket2D(data=x, wavelet='db1', mode='symmetric')
398
399 1) At first the wp's attribute ``a`` is ``None``
400
401 >>> print(wp.a)
402 None
403
404 **Remember that you should not rely on the attribute access.**
405
406 2) During the first attempt to access the node it is computed
407 via decomposition of its parent node (the wp object itself).
408
409 >>> print(wp['a'])
410 a: [[ 3. 7. 11. 15.]
411 [ 3. 7. 11. 15.]
412 [ 3. 7. 11. 15.]
413 [ 3. 7. 11. 15.]]
414
415 3) Now the ``a`` is set to the newly created node:
416
417 >>> print(wp.a)
418 a: [[ 3. 7. 11. 15.]
419 [ 3. 7. 11. 15.]
420 [ 3. 7. 11. 15.]
421 [ 3. 7. 11. 15.]]
422
423 And so is `wp.d`:
424
425 >>> print(wp.d)
426 d: [[ 0. 0. 0. 0.]
427 [ 0. 0. 0. 0.]
428 [ 0. 0. 0. 0.]
429 [ 0. 0. 0. 0.]]