Wavelet Packets¶
New in version 0.2.
Version 0.2 of PyWavelets includes many new features and improvements. One of such new feature is a twodimensional wavelet packet transform structure that is almost completely sharing programming interface with the onedimensional tree structure.
In order to achieve this simplification, a new inheritance scheme was used
in which a BaseNode
base node class is a superclass for both
Node
and Node2D
node classes.
The node classes are used as data wrappers and can be organized in trees (binary
trees for 1D transform case and quadtrees for the 2D one). They are also
superclasses to the WaveletPacket
class and
WaveletPacket2D
class that are used as the decomposition tree
roots and contain a couple additional methods.
The below diagram illustrates the inheritance tree:
BaseNode
 common interface for 1D and 2D nodes:
Node
 data carrier node in a 1D decomposition tree
WaveletPacket
 1D decomposition tree root nodeNode2D
 data carrier node in a 2D decomposition tree
WaveletPacket2D
 2D decomposition tree root node
BaseNode  a common interface of WaveletPacket and WaveletPacket2D¶

class
pywt.
BaseNode
¶ 
class
pywt.
Node
(BaseNode)¶ 
class
pywt.
WaveletPacket
(Node)¶ 
class
pywt.
Node2D
(BaseNode)¶ 
class
pywt.
WaveletPacket2D
(Node2D)¶ Note
The BaseNode is a base class for
Node
andNode2D
. It should not be used directly unless creating a new transformation type. It is included here to document the common interface of 1D and 2D node an wavelet packet transform classes.
__init__
(parent, data, node_name)¶ Parameters:  parent – parent node. If parent is
None
then the node is considered detached.  data – data associated with the node. 1D or 2D numeric array, depending on the transform type.
 node_name – a name identifying the coefficients type.
See
Node.node_name
andNode2D.node_name
for information on the accepted subnodes names.
 parent – parent node. If parent is

data
¶ Data associated with the node. 1D or 2D numeric array (depends on the transform type).

parent
¶ Parent node. Used in tree navigation.
None
for root node.

mode
¶ Signal extension mode for the
dwt()
(dwt2()
) andidwt()
(idwt2()
) decomposition and reconstruction functions. Inherited from parent node.

level
¶ Decomposition level of the current node.
0
for root (original data),1
for the first decomposition level, etc.

path
¶ Path string defining position of the node in the decomposition tree.

node_name
¶ Node name describing
data
coefficients type of the current subnode.See
Node.node_name
andNode2D.node_name
.

maxlevel
¶ Maximum allowed level of decomposition. Evaluated from parent or child nodes.

is_empty
¶ Checks if
data
attribute isNone
.

has_any_subnode
¶ Checks if node has any subnodes (is not a leaf node).

decompose
()¶ Performs Discrete Wavelet Transform on the
data
and returns transform coefficients.

reconstruct
([update=False])¶ Performs Inverse Discrete Wavelet Transform on subnodes coefficients and returns reconstructed data for the current level.
Parameters: update – If set, the data
attribute will be updated with the reconstructed value.Note
Descends to subnodes and recursively calls
reconstruct()
on them.

get_subnode
(part[, decompose=True])¶ Returns subnode or None (see decomposition flag description).
Parameters:  part – Subnode name
 decompose – If True and subnode does not exist, it will be created using coefficients from the DWT decomposition of the current node.

__getitem__
(path)¶ Used to access nodes in the decomposition tree by string
path
.Parameters: path – Path string composed from valid node names. See Node.node_name
andNode2D.node_name
for node naming convention.Similar to
get_subnode()
method with decompose=True, but can access nodes on any level in the decomposition tree.If node does not exist yet, it will be created by decomposition of its parent node.

__setitem__
(path, data)¶ Used to set node or node’s data in the decomposition tree. Nodes are identified by string
path
.Parameters:  path – Path string composed from valid node names.
See
Node.node_name
andNode2D.node_name
for node naming convention.  data – numeric array or
BaseNode
subclass.
 path – Path string composed from valid node names.
See

__delitem__
(path)¶ Used to delete node from the decomposition tree.
Parameters: path – Path string composed from valid node names. See Node.node_name
andNode2D.node_name
for node naming convention.

get_leaf_nodes
([decompose=False])¶ Traverses through the decomposition tree and collects leaf nodes (nodes without any subnodes).
Parameters: decompose – If decompose
isTrue
, the method will try to decompose the tree up to themaximum level
.

walk
(self, func[, args=()[, kwargs={}[, decompose=True]]])¶ Traverses the decomposition tree and calls
func(node, *args, **kwargs)
on every node. If func returnsTrue
, descending to subnodes will continue.Parameters:  func –
callable accepting
BaseNode
as the first param and optional positional and keyword arguments:func(node, *args, **kwargs)
 decompose – If
decompose
isTrue
(default), the method will also try to decompose the tree up to themaximum level
.
Args: arguments to pass to the
func
Kwargs: keyword arguments to pass to the
func
 func –

walk_depth
(self, func[, args=()[, kwargs={}[, decompose=False]]])¶ Similar to
walk()
but traverses the tree in depthfirst order.Parameters:  func –
callable accepting
BaseNode
as the first param and optional positional and keyword arguments:func(node, *args, **kwargs)
 decompose – If
decompose
isTrue
, the method will also try to decompose the tree up to themaximum level
.
Args: arguments to pass to the
func
Kwargs: keyword arguments to pass to the
func
 func –

WaveletPacket and WaveletPacket tree Node¶

class
pywt.
Node
(BaseNode) 
class
pywt.
WaveletPacket
(Node) 
node_name
¶ Node name describing
data
coefficients type of the current subnode. For
WaveletPacket
case it is just as indwt()
: a
 approximation coefficientsd
 details coefficients
 For


class
pywt.
WaveletPacket
(Node) 
__init__
(data, wavelet[, mode='symmetric'[, maxlevel=None]])¶ Parameters:  data – data associated with the node. 1D numeric array.
 wavelet – Wavelet to use in the transform. This can be a name of the wavelet from the
wavelist()
list or aWavelet
object instance.  mode – Signal extension mode for the
dwt()
andidwt()
decomposition and reconstruction functions.  maxlevel – Maximum allowed level of decomposition. If not specified
it will be calculated based on the
wavelet
anddata
length usingpywt.dwt_max_level()
.

get_level
(level[, order="natural"[, decompose=True]])¶ Collects nodes from the given level of decomposition.
Parameters:  level – Specifies decomposition
level
from which the nodes will be collected.  order – Specifies nodes order  natural (
natural
) or frequency (freq
).  decompose – If set then the method will try to decompose the data up
to the specified
level
.
If nodes at the given level are missing (i.e. the tree is partially decomposed) and the
decompose
is set toFalse
, only existing nodes will be returned. level – Specifies decomposition

WaveletPacket2D and WaveletPacket2D tree Node2D¶

class
pywt.
Node2D
(BaseNode) 
class
pywt.
WaveletPacket2D
(Node2D) 
node_name
 For
WaveletPacket2D
case it is just as indwt2()
: a
 approximation coefficients (LL)h
 horizontal detail coefficients (LH)v
 vertical detail coefficients (HL)d
 diagonal detail coefficients (HH)
 For

decompose
() See also
dwt2()
for 2D Discrete Wavelet Transform output coefficients.

expand_2d_path(self, path):


class
pywt.
WaveletPacket2D
(Node2D) 
__init__
(data, wavelet[, mode='symmetric'[, maxlevel=None]]) Parameters:  data – data associated with the node. 2D numeric array.
 wavelet – Wavelet to use in the transform. This can be a name of the wavelet from the
wavelist()
list or aWavelet
object instance.  mode – Signal extension mode for the
dwt()
andidwt()
decomposition and reconstruction functions.  maxlevel – Maximum allowed level of decomposition. If not specified
it will be calculated based on the
wavelet
anddata
length usingpywt.dwt_max_level()
.

get_level
(level[, order="natural"[, decompose=True]])¶ Collects nodes from the given level of decomposition.
Parameters:  level – Specifies decomposition
level
from which the nodes will be collected.  order – Specifies nodes order  natural (
natural
) or frequency (freq
).  decompose – If set then the method will try to decompose the data up
to the specified
level
.
If nodes at the given level are missing (i.e. the tree is partially decomposed) and the
decompose
is set toFalse
, only existing nodes will be returned. level – Specifies decomposition
