/usr/lib/python2.7/dist-packages/pywt/_swt.py is in python-pywt 0.5.1-1.1ubuntu4.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 | from ._extensions._swt import swt_max_level, swt as _swt, swt_axis as _swt_axis
from ._extensions._pywt import Wavelet, _check_dtype
import warnings
import numpy as np
from ._extensions._swt import swt_axis as _swt_axis
from ._dwt import idwt
from ._multidim import idwt2
__all__ = ["swt", "swt_max_level", 'iswt', 'swt2', 'iswt2', 'swtn']
def swt(data, wavelet, level=None, start_level=0, axis=-1):
"""
Multilevel 1D stationary wavelet transform.
Parameters
----------
data :
Input signal
wavelet :
Wavelet to use (Wavelet object or name)
level : int, optional
The number of decomposition steps to perform.
start_level : int, optional
The level at which the decomposition will begin (it allows one to
skip a given number of transform steps and compute
coefficients starting from start_level) (default: 0)
axis: int, optional
Axis over which to compute the SWT. If not given, the
last axis is used.
Returns
-------
coeffs : list
List of approximation and details coefficients pairs in order
similar to wavedec function::
[(cAn, cDn), ..., (cA2, cD2), (cA1, cD1)]
where n equals input parameter ``level``.
If ``start_level = m`` is given, then the beginning m steps are
skipped::
[(cAm+n, cDm+n), ..., (cAm+1, cDm+1), (cAm, cDm)]
"""
if np.iscomplexobj(data):
data = np.asarray(data)
coeffs_real = swt(data.real, wavelet, level, start_level)
coeffs_imag = swt(data.imag, wavelet, level, start_level)
coeffs_cplx = []
for (cA_r, cD_r), (cA_i, cD_i) in zip(coeffs_real, coeffs_imag):
coeffs_cplx.append((cA_r + 1j*cA_i, cD_r + 1j*cD_i))
return coeffs_cplx
# accept array_like input; make a copy to ensure a contiguous array
dt = _check_dtype(data)
data = np.array(data, dtype=dt)
if not isinstance(wavelet, Wavelet):
wavelet = Wavelet(wavelet)
if level is None:
level = swt_max_level(len(data))
if axis < 0:
axis = axis + data.ndim
if not 0 <= axis < data.ndim:
raise ValueError("Axis greater than data dimensions")
if data.ndim == 1:
ret = _swt(data, wavelet, level, start_level)
else:
ret = _swt_axis(data, wavelet, level, start_level, axis)
return [(np.asarray(cA), np.asarray(cD)) for cA, cD in ret]
def iswt(coeffs, wavelet):
"""
Multilevel 1D inverse discrete stationary wavelet transform.
Parameters
----------
coeffs : array_like
Coefficients list of tuples::
[(cAn, cDn), ..., (cA2, cD2), (cA1, cD1)]
where cA is approximation, cD is details. Index 1 corresponds to
``start_level`` from ``pywt.swt``.
wavelet : Wavelet object or name string
Wavelet to use
Returns
-------
1D array of reconstructed data.
Examples
--------
>>> import pywt
>>> coeffs = pywt.swt([1,2,3,4,5,6,7,8], 'db2', level=2)
>>> pywt.iswt(coeffs, 'db2')
array([ 1., 2., 3., 4., 5., 6., 7., 8.])
"""
output = coeffs[0][0].copy() # Avoid modification of input data
# num_levels, equivalent to the decomposition level, n
num_levels = len(coeffs)
if not isinstance(wavelet, Wavelet):
wavelet = Wavelet(wavelet)
for j in range(num_levels, 0, -1):
step_size = int(pow(2, j-1))
last_index = step_size
_, cD = coeffs[num_levels - j]
for first in range(last_index): # 0 to last_index - 1
# Getting the indices that we will transform
indices = np.arange(first, len(cD), step_size)
# select the even indices
even_indices = indices[0::2]
# select the odd indices
odd_indices = indices[1::2]
# perform the inverse dwt on the selected indices,
# making sure to use periodic boundary conditions
x1 = idwt(output[even_indices], cD[even_indices],
wavelet, 'periodization')
x2 = idwt(output[odd_indices], cD[odd_indices],
wavelet, 'periodization')
# perform a circular shift right
x2 = np.roll(x2, 1)
# average and insert into the correct indices
output[indices] = (x1 + x2)/2.
return output
def swt2(data, wavelet, level, start_level=0, axes=(-2, -1)):
"""
Multilevel 2D stationary wavelet transform.
Parameters
----------
data : array_like
2D array with input data
wavelet : Wavelet object or name string
Wavelet to use
level : int
The number of decomposition steps to perform.
start_level : int, optional
The level at which the decomposition will start (default: 0)
axes : 2-tuple of ints, optional
Axes over which to compute the SWT. Repeated elements are not allowed.
Returns
-------
coeffs : list
Approximation and details coefficients::
[
(cA_m,
(cH_m, cV_m, cD_m)
),
(cA_m+1,
(cH_m+1, cV_m+1, cD_m+1)
),
...,
(cA_m+level,
(cH_m+level, cV_m+level, cD_m+level)
)
]
where cA is approximation, cH is horizontal details, cV is
vertical details, cD is diagonal details and m is ``start_level``.
"""
axes = tuple(axes)
data = np.asarray(data)
if len(axes) != 2:
raise ValueError("Expected 2 axes")
if len(axes) != len(set(axes)):
raise ValueError("The axes passed to swt2 must be unique.")
if data.ndim < len(np.unique(axes)):
raise ValueError("Input array has fewer dimensions than the specified "
"axes")
coefs = swtn(data, wavelet, level, start_level, axes)
ret = []
for c in coefs:
ret.append((c['aa'], (c['da'], c['ad'], c['dd'])))
warnings.warn(
"For consistency with the rest of PyWavelets, the order of the list "
"returned by swt2 will be reversed in the next release. "
"In other words, the levels will be sorted in descending rather than "
"ascending order.", FutureWarning)
# reverse order for backwards compatiblity
ret.reverse()
return ret
def iswt2(coeffs, wavelet):
"""
Multilevel 2D inverse discrete stationary wavelet transform.
Parameters
----------
coeffs : list
Approximation and details coefficients::
[
(cA_1,
(cH_1, cV_1, cD_1)
),
(cA_2,
(cH_2, cV_2, cD_2)
),
...,
(cA_n
(cH_n, cV_n, cD_n)
)
]
where cA is approximation, cH is horizontal details, cV is
vertical details, cD is diagonal details and n is the number of
levels. Index 1 corresponds to ``start_level`` from ``pywt.swt2``.
wavelet : Wavelet object or name string
Wavelet to use
Returns
-------
2D array of reconstructed data.
Examples
--------
>>> import pywt
>>> coeffs = pywt.swt2([[1,2,3,4],[5,6,7,8],
... [9,10,11,12],[13,14,15,16]],
... 'db1', level=2)
>>> pywt.iswt2(coeffs, 'db1')
array([[ 1., 2., 3., 4.],
[ 5., 6., 7., 8.],
[ 9., 10., 11., 12.],
[ 13., 14., 15., 16.]])
"""
output = coeffs[-1][0].copy() # Avoid modification of input data
# num_levels, equivalent to the decomposition level, n
num_levels = len(coeffs)
if not isinstance(wavelet, Wavelet):
wavelet = Wavelet(wavelet)
for j in range(num_levels, 0, -1):
step_size = int(pow(2, j-1))
last_index = step_size
_, (cH, cV, cD) = coeffs[j-1]
# We are going to assume cH, cV, and cD are square and of equal size
if (cH.shape != cV.shape) or (cH.shape != cD.shape) or (
cH.shape[0] != cH.shape[1]):
raise RuntimeError(
"Mismatch in shape of intermediate coefficient arrays")
for first_h in range(last_index): # 0 to last_index - 1
for first_w in range(last_index): # 0 to last_index - 1
# Getting the indices that we will transform
indices_h = slice(first_h, cH.shape[0], step_size)
indices_w = slice(first_w, cH.shape[1], step_size)
even_idx_h = slice(first_h, cH.shape[0], 2*step_size)
even_idx_w = slice(first_w, cH.shape[1], 2*step_size)
odd_idx_h = slice(first_h + step_size, cH.shape[0], 2*step_size)
odd_idx_w = slice(first_w + step_size, cH.shape[1], 2*step_size)
# perform the inverse dwt on the selected indices,
# making sure to use periodic boundary conditions
x1 = idwt2((output[even_idx_h, even_idx_w],
(cH[even_idx_h, even_idx_w],
cV[even_idx_h, even_idx_w],
cD[even_idx_h, even_idx_w])),
wavelet, 'periodization')
x2 = idwt2((output[even_idx_h, odd_idx_w],
(cH[even_idx_h, odd_idx_w],
cV[even_idx_h, odd_idx_w],
cD[even_idx_h, odd_idx_w])),
wavelet, 'periodization')
x3 = idwt2((output[odd_idx_h, even_idx_w],
(cH[odd_idx_h, even_idx_w],
cV[odd_idx_h, even_idx_w],
cD[odd_idx_h, even_idx_w])),
wavelet, 'periodization')
x4 = idwt2((output[odd_idx_h, odd_idx_w],
(cH[odd_idx_h, odd_idx_w],
cV[odd_idx_h, odd_idx_w],
cD[odd_idx_h, odd_idx_w])),
wavelet, 'periodization')
# perform a circular shifts
x2 = np.roll(x2, 1, axis=1)
x3 = np.roll(x3, 1, axis=0)
x4 = np.roll(x4, 1, axis=0)
x4 = np.roll(x4, 1, axis=1)
output[indices_h, indices_w] = (x1 + x2 + x3 + x4) / 4
warnings.warn(
"For consistency with the rest of PyWavelets, the order of levels in "
"coeffs expected by iswt2 will be reversed in the next release. "
"In other words, the levels will be sorted in descending rather than "
"ascending order.", FutureWarning)
return output
def swtn(data, wavelet, level, start_level=0, axes=None):
"""
n-dimensional stationary wavelet transform.
Parameters
----------
data : array_like
n-dimensional array with input data.
wavelet : Wavelet object or name string
Wavelet to use.
level : int
The number of decomposition steps to perform.
start_level : int, optional
The level at which the decomposition will start (default: 0)
axes : sequence of ints, optional
Axes over which to compute the SWT. A value of ``None`` (the
default) selects all axes. Axes may not be repeated.
Returns
-------
[{coeffs_level_n}, ..., {coeffs_level_1}]: list of dict
Results for each level are arranged in a dictionary, where the key
specifies the transform type on each dimension and value is a
n-dimensional coefficients array.
For example, for a 2D case the result at a given level will look
something like this::
{'aa': <coeffs> # A(LL) - approx. on 1st dim, approx. on 2nd dim
'ad': <coeffs> # V(LH) - approx. on 1st dim, det. on 2nd dim
'da': <coeffs> # H(HL) - det. on 1st dim, approx. on 2nd dim
'dd': <coeffs> # D(HH) - det. on 1st dim, det. on 2nd dim
}
For user-specified ``axes``, the order of the characters in the
dictionary keys map to the specified ``axes``.
"""
data = np.asarray(data)
if np.iscomplexobj(data):
real = swtn(data.real, wavelet, level, start_level, axes)
imag = swtn(data.imag, wavelet, level, start_level, axes)
cplx = []
for rdict, idict in zip(real, imag):
cplx.append(
dict((k, rdict[k] + 1j * idict[k]) for k in rdict.keys()))
return cplx
if data.dtype == np.dtype('object'):
raise TypeError("Input must be a numeric array-like")
if data.ndim < 1:
raise ValueError("Input data must be at least 1D")
if axes is None:
axes = range(data.ndim)
axes = [a + data.ndim if a < 0 else a for a in axes]
if len(axes) != len(set(axes)):
raise ValueError("The axes passed to swtn must be unique.")
num_axes = len(axes)
if not isinstance(wavelet, Wavelet):
wavelet = Wavelet(wavelet)
ret = []
for i in range(start_level, start_level + level):
coeffs = [('', data)]
for axis in axes:
new_coeffs = []
for subband, x in coeffs:
cA, cD = _swt_axis(x, wavelet, level=1, start_level=i,
axis=axis)[0]
new_coeffs.extend([(subband + 'a', cA),
(subband + 'd', cD)])
coeffs = new_coeffs
coeffs = dict(coeffs)
ret.append(coeffs)
# data for the next level is the approximation coeffs from this level
data = coeffs['a' * num_axes]
ret.reverse()
return ret
|