Debian Codegraph @

doc/source/ref/wavelet-packets.rst

raw
1 .. _ref-wp:
2
3 .. currentmodule:: pywt
4 .. include:: ../substitutions.rst
5
6 ===============
7 Wavelet Packets
8 ===============
9
10 PyWavelets implements one-dimensional, two-dimensional and n-dimensional
11 wavelet packet transform structures. The higher dimensional structures almost
12 completely sharing programming interface with the one-dimensional tree
13 structure.
14
15 In order to achieve this simplification, a new inheritance scheme was used
16 in which a :class:`~pywt.BaseNode` base node class is a superclass for the
17 :class:`~pywt.Node`, :class:`~pywt.Node2D` and :class:`~pywt.NodeND`
18 classes.
19
20 The node classes are used as data wrappers and can be organized in trees (
21 binary trees for 1D transform case, quad-trees for the 2D one and 2**N-ary
22 trees in ND). They are also superclasses to the :class:`~pywt.WaveletPacket`,
23 :class:`~pywt.WaveletPacket2D` and :class:`~pywt.WaveletPacketND` classes that
24 are used as the decomposition tree roots and contain a couple additional
25 methods.
26
27 Here 1D, 2D and ND refer to the number of axes of the data to be transformed.
28 All wavelet packet objects can operate on general n-dimensional arrays, but the
29 1D or 2D classes apply transforms along only 1 or 2 dimensions. The ND classes
30 allow transforms over an arbitrary number of axes of n-dimensional data.
31
32 The below diagram illustrates the inheritance tree:
33
34 - :class:`~pywt.BaseNode` - common interface for 1D and 2D nodes:
35
36 - :class:`~pywt.Node` - data carrier node in a 1D decomposition tree
37
38 - :class:`~pywt.WaveletPacket` - 1D decomposition tree root node
39
40 - :class:`~pywt.Node2D` - data carrier node in a 2D decomposition tree
41
42 - :class:`~pywt.WaveletPacket2D` - 2D decomposition tree root node
43
44 - :class:`~pywt.NodeND` - data carrier node in a ND decomposition tree
45
46 - :class:`~pywt.WaveletPacketND` - ND decomposition tree root node
47
48
49 BaseNode - a common interface of WaveletPacket, WaveletPacket2D and WaveletPacketND
50 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
51
52 .. class:: BaseNode
53
54 .. note:: The BaseNode is a base class for :class:`Node`, :class:`Node2D`,
55 and :class:`NodeND`. It should not be used directly unless creating
56 a new transformation type. It is included here to document the
57 common interface of the node and wavelet packet transform classes.
58
59 .. method:: __init__(parent, data, node_name)
60
61 :param parent: parent node. If parent is ``None`` then the node is
62 considered detached.
63
64 :param data: The data associated with the node. An n-dimensional
65 numeric array.
66
67 :param node_name: a name identifying the coefficients type.
68 See :attr:`Node.node_name` and :attr:`Node2D.node_name`
69 for information on the accepted subnodes names.
70
71 .. attribute:: data
72
73 Data associated with the node. An n-dimensional numeric array.
74
75 .. attribute:: parent
76
77 Parent node. Used in tree navigation. ``None`` for root node.
78
79 .. attribute:: wavelet
80
81 :class:`~pywt.Wavelet` used for decomposition and reconstruction. Inherited
82 from parent node.
83
84 .. attribute:: axes
85
86 A tuple of ints containing the axes along which the wavelet packet
87 transform is to be applied.
88
89 .. attribute:: mode
90
91 Signal extension :ref:`mode <ref-modes>` for the :func:`dwt` (:func:`dwt2`)
92 and :func:`idwt` (:func:`idwt2`) decomposition and reconstruction
93 functions. Inherited from parent node.
94
95 .. attribute:: level
96
97 Decomposition level of the current node. ``0`` for root (original data),
98 ``1`` for the first decomposition level, etc.
99
100 .. attribute:: path
101
102 Path string defining position of the node in the decomposition tree.
103
104 .. attribute:: path_tuple
105
106 A version of :attr:`path`, but in tuple form rather than as a single
107 string. The tuple form is easier to work with for n-dimensional transforms.
108 The length of the tuple will be equal to the number of levels of
109 decomposition at the current node.
110
111 .. attribute:: node_name
112
113 Node name describing :attr:`~BaseNode.data` coefficients type of the
114 current subnode.
115
116 See :attr:`Node.node_name` and :attr:`Node2D.node_name`.
117
118 .. attribute:: maxlevel
119
120 Maximum allowed level of decomposition. Evaluated from parent or child
121 nodes.
122
123 .. attribute:: is_empty
124
125 Checks if :attr:`~BaseNode.data` attribute is ``None``.
126
127 .. attribute:: has_any_subnode
128
129 Checks if node has any subnodes (is not a leaf node).
130
131 .. method:: decompose()
132
133 Performs Discrete Wavelet Transform on the :attr:`~BaseNode.data` and
134 returns transform coefficients.
135
136 .. method:: reconstruct([update=False])
137
138 Performs Inverse Discrete Wavelet Transform on subnodes coefficients and
139 returns reconstructed data for the current level.
140
141 :param update: If set, the :attr:`~BaseNode.data` attribute will be
142 updated with the reconstructed value.
143
144 .. note:: Descends to subnodes and recursively
145 calls :meth:`~BaseNode.reconstruct` on them.
146
147 .. method:: get_subnode(part[, decompose=True])
148
149 Returns subnode or None (see *decomposition* flag description).
150
151 :param part: Subnode name
152
153 :param decompose: If True and subnode does not exist, it will be created
154 using coefficients from the DWT decomposition of the
155 current node.
156
157 .. method:: __getitem__(path)
158
159 Used to access nodes in the decomposition tree by string ``path``.
160
161 :param path: Path string composed from valid node names. See
162 :attr:`Node.node_name` and :attr:`Node2D.node_name` for node
163 naming convention.
164
165 Similar to :meth:`~BaseNode.get_subnode` method with `decompose=True`, but
166 can access nodes on any level in the decomposition tree.
167
168 If node does not exist yet, it will be created by decomposition of its
169 parent node.
170
171 .. method:: __setitem__(path, data)
172
173 Used to set node or node's data in the decomposition tree. Nodes are
174 identified by string ``path``.
175
176 :param path: Path string composed from valid node names.
177 See :attr:`Node.node_name` and :attr:`Node2D.node_name` for
178 node naming convention.
179
180 :param data: numeric array or :class:`~BaseNode` subclass.
181
182 .. method:: __delitem__(path)
183
184 Used to delete node from the decomposition tree.
185
186 :param path: Path string composed from valid node names.
187 See :attr:`Node.node_name` and :attr:`Node2D.node_name` for
188 node naming convention.
189
190 .. method:: get_leaf_nodes([decompose=False])
191
192 Traverses through the decomposition tree and collects leaf nodes (nodes
193 without any subnodes).
194
195 :param decompose: If ``decompose`` is ``True``, the method will try to
196 decompose the tree up to the
197 :attr:`maximum level <BaseNode.maxlevel>`.
198
199 .. method:: walk(self, func, [args=(), [kwargs={}, [decompose=True]]])
200
201 Traverses the decomposition tree and calls ``func(node, *args, **kwargs)``
202 on every node. If `func` returns ``True``, descending to subnodes will
203 continue.
204
205 :param func: callable accepting :class:`BaseNode` as the first param and
206 optional positional and keyword arguments::
207
208 func(node, *args, **kwargs)
209
210 :args: arguments to pass to the ``func``
211
212 :kwargs: keyword arguments to pass to the ``func``
213
214 :param decompose: If ``decompose`` is ``True`` (default), the method will
215 also try to decompose the tree up to the
216 :attr:`maximum level <BaseNode.maxlevel>`.
217
218 .. method:: walk_depth(self, func, [args=(), [kwargs={}, [decompose=False]]])
219
220 Similar to :meth:`~BaseNode.walk` but traverses the tree in depth-first
221 order.
222
223 :param func: callable accepting :class:`BaseNode` as the first param and
224 optional positional and keyword arguments::
225
226 func(node, *args, **kwargs)
227
228 :args: arguments to pass to the ``func``
229
230 :kwargs: keyword arguments to pass to the ``func``
231
232 :param decompose: If ``decompose`` is ``True``, the method will also try
233 to decompose the tree up to the
234 :attr:`maximum level <BaseNode.maxlevel>`.
235
236
237 WaveletPacket and Node
238 ~~~~~~~~~~~~~~~~~~~~~~
239
240 .. class:: Node(BaseNode)
241
242 .. attribute:: node_name
243
244 Node name describing :attr:`~BaseNode.data` coefficients type of the
245 current subnode.
246
247 For :class:`WaveletPacket` case it is just as in :func:`dwt`:
248 - ``a`` - approximation coefficients
249 - ``d`` - details coefficients
250
251 .. method:: decompose()
252
253 .. seealso::
254
255 :func:`dwt` for 1D Discrete Wavelet Transform output coefficients.
256
257 .. method:: reconstruct()
258
259 .. seealso::
260
261 :func:`idwt` for 1D Inverse Discrete Wavelet Transform
262
263
264 .. class:: WaveletPacket(Node)
265
266 .. method:: __init__(data, wavelet, [mode='symmetric', [maxlevel=None, [axis=-1]]])
267
268 :param data: data associated with the node. N-dimensional numeric array.
269
270 :param wavelet: |wavelet|
271
272 :param mode: Signal extension :ref:`mode <ref-modes>` for the :func:`dwt`
273 and :func:`idwt` decomposition and reconstruction functions.
274
275 :param maxlevel: Maximum allowed level of decomposition. If not specified
276 it will be calculated based on the ``wavelet`` and
277 ``data`` length using :func:`pywt.dwt_max_level`.
278
279 :param axis: The axis of the array that is to be transformed.
280
281 .. method:: get_level(level, [order="natural", [decompose=True]])
282
283 Collects nodes from the given level of decomposition.
284
285 :param level: Specifies decomposition ``level`` from which the nodes will
286 be collected.
287
288 :param order: Specifies nodes order - natural (``natural``) or frequency
289 (``freq``).
290
291 :param decompose: If set then the method will try to decompose the data up
292 to the specified ``level``.
293
294 If nodes at the given level are missing (i.e. the tree is partially
295 decomposed) and the ``decompose`` is set to ``False``, only existing nodes
296 will be returned.
297
298 .. method:: reconstruct([update=True])
299
300 Reconstruct data from the subnodes.
301
302 :param update: A boolean indicating whether the coefficients of the
303 current node and its subnodes will be replaced with values
304 from the reconstruction.
305
306
307 WaveletPacket2D and Node2D
308 ~~~~~~~~~~~~~~~~~~~~~~~~~~
309
310 .. class:: Node2D(BaseNode)
311
312 .. attribute:: node_name
313
314 For :class:`WaveletPacket2D` case it is just as in :func:`dwt2`:
315 - ``a`` - approximation coefficients (`LL`)
316 - ``h`` - horizontal detail coefficients (`LH`)
317 - ``v`` - vertical detail coefficients (`HL`)
318 - ``d`` - diagonal detail coefficients (`HH`)
319
320 .. method:: decompose()
321
322 .. seealso::
323
324 :func:`dwt2` for 2D Discrete Wavelet Transform output coefficients.
325
326 .. method:: reconstruct()
327
328 .. seealso::
329
330 :func:`idwt2` for 2D Inverse Discrete Wavelet Transform
331
332 .. method:: expand_2d_path(self, path):
333
334
335 .. class:: WaveletPacket2D(Node2D)
336
337 .. method:: __init__(data, wavelet, [mode='symmetric', [maxlevel=None, [axes=(-2, -1)]]])
338
339 :param data: data associated with the node. N-dimensional numeric array.
340
341 :param wavelet: |wavelet|
342
343 :param mode: Signal extension :ref:`mode <ref-modes>` for the :func:`dwt`
344 and :func:`idwt` decomposition and reconstruction functions.
345
346 :param maxlevel: Maximum allowed level of decomposition. If not specified
347 it will be calculated based on the ``wavelet`` and
348 ``data`` length using :func:`pywt.dwt_max_level`.
349
350 :param axes: The axes of the array that are to be transformed.
351
352 .. method:: get_level(level, [order="natural", [decompose=True]])
353
354 Collects nodes from the given level of decomposition.
355
356 :param level: Specifies decomposition ``level`` from which the nodes will
357 be collected.
358
359 :param order: Specifies nodes order - natural (``natural``) or frequency
360 (``freq``).
361
362 :param decompose: If set then the method will try to decompose the data up
363 to the specified ``level``.
364
365 If nodes at the given level are missing (i.e. the tree is partially
366 decomposed) and the ``decompose`` is set to ``False``, only existing nodes
367 will be returned.
368
369 .. method:: reconstruct([update=True])
370
371 Reconstruct data from the subnodes.
372
373 :param update: A boolean indicating whether the coefficients of the
374 current node and its subnodes will be replaced with values
375 from the reconstruction.
376
377 WaveletPacketND and NodeND
378 ~~~~~~~~~~~~~~~~~~~~~~~~~~
379
380 .. class:: NodeND(BaseNode)
381
382 .. attribute:: node_name
383
384 For :class:`WaveletPacketND` case it is just as in :func:`dwtn`:
385 - in 1D it has keys 'a' and 'd'
386 - in 2D it has keys 'aa', 'ad', 'da', 'dd'
387 - in 3D it has keys 'aaa', 'aad', 'ada', 'daa', ..., 'ddd'
388
389 .. method:: decompose()
390
391 .. seealso::
392
393 :func:`dwtn` for ND Discrete Wavelet Transform output coefficients.
394
395 .. method:: reconstruct()
396
397 .. seealso::
398
399 :func:`idwtn` for ND Inverse Discrete Wavelet Transform
400
401
402 .. class:: WaveletPacketND(NodeND)
403
404 .. method:: __init__(data, wavelet, [mode='symmetric', [maxlevel=None, [axes=None]]])
405
406 :param data: data associated with the node. N-dimensional numeric array.
407
408 :param wavelet: |wavelet|
409
410 :param mode: Signal extension :ref:`mode <ref-modes>` for the :func:`dwt`
411 and :func:`idwt` decomposition and reconstruction functions.
412
413 :param maxlevel: Maximum allowed level of decomposition. If not specified
414 it will be calculated based on the ``wavelet`` and
415 ``data`` length using :func:`pywt.dwt_max_level`.
416
417 :param axes: The axes of the array that are to be transformed.
418
419 .. method:: get_level(level, [decompose=True])
420
421 Collects nodes from the given level of decomposition.
422
423 :param level: Specifies decomposition ``level`` from which the nodes will
424 be collected.
425
426 :param decompose: If set then the method will try to decompose the data up
427 to the specified ``level``.
428
429 If nodes at the given level are missing (i.e. the tree is partially
430 decomposed) and the ``decompose`` is set to ``False``, only existing nodes
431 will be returned.
432
433 .. method:: reconstruct([update=True])
434
435 Reconstruct data from the subnodes.
436
437 :param update: A boolean indicating whether the coefficients of the
438 current node and its subnodes will be replaced with values
439 from the reconstruction.