| 1 |
|
| 2 |
|
| 3 |
from __future__ import division, print_function, absolute_import |
| 4 |
|
| 5 |
import warnings |
| 6 |
from copy import deepcopy |
| 7 |
from itertools import combinations, permutations |
| 8 |
import numpy as np |
| 9 |
import pytest |
| 10 |
from numpy.testing import (assert_allclose, assert_, assert_equal, |
| 11 |
assert_raises, assert_array_equal, assert_warns) |
| 12 |
|
| 13 |
import pywt |
| 14 |
from pywt._extensions._swt import swt_axis |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
dtypes_in = [np.int8, np.float16, np.float32, np.float64, np.complex64, |
| 19 |
np.complex128] |
| 20 |
dtypes_out = [np.float64, np.float32, np.float32, np.float64, np.complex64, |
| 21 |
np.complex128] |
| 22 |
|
| 23 |
|
| 24 |
tol_single = 1e-6 |
| 25 |
tol_double = 1e-13 |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
def test_swt_decomposition(): |
| 33 |
x = [3, 7, 1, 3, -2, 6, 4, 6] |
| 34 |
db1 = pywt.Wavelet('db1') |
| 35 |
atol = tol_double |
| 36 |
(cA3, cD3), (cA2, cD2), (cA1, cD1) = pywt.swt(x, db1, level=3) |
| 37 |
expected_cA1 = [7.07106781, 5.65685425, 2.82842712, 0.70710678, |
| 38 |
2.82842712, 7.07106781, 7.07106781, 6.36396103] |
| 39 |
assert_allclose(cA1, expected_cA1, rtol=1e-8, atol=atol) |
| 40 |
expected_cD1 = [-2.82842712, 4.24264069, -1.41421356, 3.53553391, |
| 41 |
-5.65685425, 1.41421356, -1.41421356, 2.12132034] |
| 42 |
assert_allclose(cD1, expected_cD1, rtol=1e-8, atol=atol) |
| 43 |
expected_cA2 = [7, 4.5, 4, 5.5, 7, 9.5, 10, 8.5] |
| 44 |
assert_allclose(cA2, expected_cA2, rtol=tol_double, atol=atol) |
| 45 |
expected_cD2 = [3, 3.5, 0, -4.5, -3, 0.5, 0, 0.5] |
| 46 |
assert_allclose(cD2, expected_cD2, rtol=tol_double, atol=atol) |
| 47 |
expected_cA3 = [9.89949494, ] * 8 |
| 48 |
assert_allclose(cA3, expected_cA3, rtol=1e-8, atol=atol) |
| 49 |
expected_cD3 = [0.00000000, -3.53553391, -4.24264069, -2.12132034, |
| 50 |
0.00000000, 3.53553391, 4.24264069, 2.12132034] |
| 51 |
assert_allclose(cD3, expected_cD3, rtol=1e-8, atol=atol) |
| 52 |
|
| 53 |
|
| 54 |
res = pywt.swt(cA1, db1, level=1, start_level=1) |
| 55 |
cA2, cD2 = res[0] |
| 56 |
assert_allclose(cA2, expected_cA2, rtol=tol_double, atol=atol) |
| 57 |
assert_allclose(cD2, expected_cD2, rtol=tol_double, atol=atol) |
| 58 |
|
| 59 |
coeffs = pywt.swt(x, db1) |
| 60 |
assert_(len(coeffs) == 3) |
| 61 |
assert_(pywt.swt_max_level(len(x)), 3) |
| 62 |
|
| 63 |
|
| 64 |
def test_swt_max_level(): |
| 65 |
|
| 66 |
assert_warns(UserWarning, pywt.swt_max_level, 11) |
| 67 |
with warnings.catch_warnings(): |
| 68 |
warnings.simplefilter('ignore', UserWarning) |
| 69 |
assert_equal(pywt.swt_max_level(11), 0) |
| 70 |
|
| 71 |
|
| 72 |
assert_equal(pywt.swt_max_level(2), 1) |
| 73 |
assert_equal(pywt.swt_max_level(4*3), 2) |
| 74 |
assert_equal(pywt.swt_max_level(16), 4) |
| 75 |
assert_equal(pywt.swt_max_level(16*3), 4) |
| 76 |
|
| 77 |
|
| 78 |
def test_swt_axis(): |
| 79 |
x = [3, 7, 1, 3, -2, 6, 4, 6] |
| 80 |
|
| 81 |
db1 = pywt.Wavelet('db1') |
| 82 |
(cA2, cD2), (cA1, cD1) = pywt.swt(x, db1, level=2) |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
for order in ['C', 'F']: |
| 87 |
|
| 88 |
x_2d = np.asarray(x).reshape((1, -1)) |
| 89 |
x_2d = np.concatenate((x_2d, )*5, axis=0) |
| 90 |
if order == 'C': |
| 91 |
x_2d = np.ascontiguousarray(x_2d) |
| 92 |
elif order == 'F': |
| 93 |
x_2d = np.asfortranarray(x_2d) |
| 94 |
(cA2_2d, cD2_2d), (cA1_2d, cD1_2d) = pywt.swt(x_2d, db1, level=2) |
| 95 |
|
| 96 |
for c in [cA2_2d, cD2_2d, cA1_2d, cD1_2d]: |
| 97 |
assert_(c.shape == x_2d.shape) |
| 98 |
|
| 99 |
for row in cA1_2d: |
| 100 |
assert_array_equal(row, cA1) |
| 101 |
for row in cA2_2d: |
| 102 |
assert_array_equal(row, cA2) |
| 103 |
for row in cD1_2d: |
| 104 |
assert_array_equal(row, cD1) |
| 105 |
for row in cD2_2d: |
| 106 |
assert_array_equal(row, cD2) |
| 107 |
|
| 108 |
|
| 109 |
x_2d = np.asarray(x).reshape((-1, 1)) |
| 110 |
x_2d = np.concatenate((x_2d, )*5, axis=1) |
| 111 |
if order == 'C': |
| 112 |
x_2d = np.ascontiguousarray(x_2d) |
| 113 |
elif order == 'F': |
| 114 |
x_2d = np.asfortranarray(x_2d) |
| 115 |
(cA2_2d, cD2_2d), (cA1_2d, cD1_2d) = pywt.swt(x_2d, db1, level=2, |
| 116 |
axis=0) |
| 117 |
|
| 118 |
for c in [cA2_2d, cD2_2d, cA1_2d, cD1_2d]: |
| 119 |
assert_(c.shape == x_2d.shape) |
| 120 |
|
| 121 |
for row in cA1_2d.transpose((1, 0)): |
| 122 |
assert_array_equal(row, cA1) |
| 123 |
for row in cA2_2d.transpose((1, 0)): |
| 124 |
assert_array_equal(row, cA2) |
| 125 |
for row in cD1_2d.transpose((1, 0)): |
| 126 |
assert_array_equal(row, cD1) |
| 127 |
for row in cD2_2d.transpose((1, 0)): |
| 128 |
assert_array_equal(row, cD2) |
| 129 |
|
| 130 |
|
| 131 |
assert_raises(ValueError, pywt.swt, x, db1, level=2, axis=5) |
| 132 |
|
| 133 |
|
| 134 |
def test_swt_iswt_integration(): |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
max_level = 3 |
| 145 |
wavelets = pywt.wavelist(kind='discrete') |
| 146 |
if 'dmey' in wavelets: |
| 147 |
|
| 148 |
wavelets.remove('dmey') |
| 149 |
for current_wavelet_str in wavelets: |
| 150 |
current_wavelet = pywt.Wavelet(current_wavelet_str) |
| 151 |
input_length_power = int(np.ceil(np.log2(max( |
| 152 |
current_wavelet.dec_len, |
| 153 |
current_wavelet.rec_len)))) |
| 154 |
input_length = 2**(input_length_power + max_level - 1) |
| 155 |
X = np.arange(input_length) |
| 156 |
for norm in [True, False]: |
| 157 |
if norm and not current_wavelet.orthogonal: |
| 158 |
|
| 159 |
continue |
| 160 |
for trim_approx in [True, False]: |
| 161 |
coeffs = pywt.swt(X, current_wavelet, max_level, |
| 162 |
trim_approx=trim_approx, norm=norm) |
| 163 |
Y = pywt.iswt(coeffs, current_wavelet, norm=norm) |
| 164 |
assert_allclose(Y, X, rtol=1e-5, atol=1e-7) |
| 165 |
|
| 166 |
|
| 167 |
def test_swt_dtypes(): |
| 168 |
wavelet = pywt.Wavelet('haar') |
| 169 |
for dt_in, dt_out in zip(dtypes_in, dtypes_out): |
| 170 |
errmsg = "wrong dtype returned for {0} input".format(dt_in) |
| 171 |
|
| 172 |
|
| 173 |
x = np.ones(8, dtype=dt_in) |
| 174 |
(cA2, cD2), (cA1, cD1) = pywt.swt(x, wavelet, level=2) |
| 175 |
assert_(cA2.dtype == cD2.dtype == cA1.dtype == cD1.dtype == dt_out, |
| 176 |
"swt: " + errmsg) |
| 177 |
|
| 178 |
|
| 179 |
x = np.ones((8, 8), dtype=dt_in) |
| 180 |
cA, (cH, cV, cD) = pywt.swt2(x, wavelet, level=1)[0] |
| 181 |
assert_(cA.dtype == cH.dtype == cV.dtype == cD.dtype == dt_out, |
| 182 |
"swt2: " + errmsg) |
| 183 |
|
| 184 |
|
| 185 |
def test_swt_roundtrip_dtypes(): |
| 186 |
|
| 187 |
rstate = np.random.RandomState(5) |
| 188 |
wavelet = pywt.Wavelet('haar') |
| 189 |
for dt_in, dt_out in zip(dtypes_in, dtypes_out): |
| 190 |
|
| 191 |
x = rstate.standard_normal((8, )).astype(dt_in) |
| 192 |
c = pywt.swt(x, wavelet, level=2) |
| 193 |
xr = pywt.iswt(c, wavelet) |
| 194 |
assert_allclose(x, xr, rtol=1e-6, atol=1e-7) |
| 195 |
|
| 196 |
|
| 197 |
x = rstate.standard_normal((8, 8)).astype(dt_in) |
| 198 |
c = pywt.swt2(x, wavelet, level=2) |
| 199 |
xr = pywt.iswt2(c, wavelet) |
| 200 |
assert_allclose(x, xr, rtol=1e-6, atol=1e-7) |
| 201 |
|
| 202 |
|
| 203 |
def test_swt_default_level_by_axis(): |
| 204 |
|
| 205 |
wav = 'db2' |
| 206 |
x = np.ones((2**3, 2**4, 2**5)) |
| 207 |
for axis in (0, 1, 2): |
| 208 |
sdec = pywt.swt(x, wav, level=None, start_level=0, axis=axis) |
| 209 |
assert_equal(len(sdec), pywt.swt_max_level(x.shape[axis])) |
| 210 |
|
| 211 |
|
| 212 |
def test_swt2_ndim_error(): |
| 213 |
x = np.ones(8) |
| 214 |
with warnings.catch_warnings(): |
| 215 |
warnings.simplefilter('ignore', FutureWarning) |
| 216 |
assert_raises(ValueError, pywt.swt2, x, 'haar', level=1) |
| 217 |
|
| 218 |
|
| 219 |
@pytest.mark.slow |
| 220 |
def test_swt2_iswt2_integration(wavelets=None): |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
max_level = 3 |
| 231 |
if wavelets is None: |
| 232 |
wavelets = pywt.wavelist(kind='discrete') |
| 233 |
if 'dmey' in wavelets: |
| 234 |
|
| 235 |
wavelets.remove('dmey') |
| 236 |
for current_wavelet_str in wavelets: |
| 237 |
current_wavelet = pywt.Wavelet(current_wavelet_str) |
| 238 |
input_length_power = int(np.ceil(np.log2(max( |
| 239 |
current_wavelet.dec_len, |
| 240 |
current_wavelet.rec_len)))) |
| 241 |
input_length = 2**(input_length_power + max_level - 1) |
| 242 |
X = np.arange(input_length**2).reshape(input_length, input_length) |
| 243 |
|
| 244 |
for norm in [True, False]: |
| 245 |
if norm and not current_wavelet.orthogonal: |
| 246 |
|
| 247 |
continue |
| 248 |
for trim_approx in [True, False]: |
| 249 |
coeffs = pywt.swt2(X, current_wavelet, max_level, |
| 250 |
trim_approx=trim_approx, norm=norm) |
| 251 |
Y = pywt.iswt2(coeffs, current_wavelet, norm=norm) |
| 252 |
assert_allclose(Y, X, rtol=1e-5, atol=1e-5) |
| 253 |
|
| 254 |
|
| 255 |
def test_swt2_iswt2_quick(): |
| 256 |
test_swt2_iswt2_integration(wavelets=['db1', ]) |
| 257 |
|
| 258 |
|
| 259 |
def test_swt2_iswt2_non_square(wavelets=None): |
| 260 |
for nrows in [8, 16, 48]: |
| 261 |
X = np.arange(nrows*32).reshape(nrows, 32) |
| 262 |
current_wavelet = 'db1' |
| 263 |
with warnings.catch_warnings(): |
| 264 |
warnings.simplefilter('ignore', FutureWarning) |
| 265 |
coeffs = pywt.swt2(X, current_wavelet, level=2) |
| 266 |
Y = pywt.iswt2(coeffs, current_wavelet) |
| 267 |
assert_allclose(Y, X, rtol=tol_single, atol=tol_single) |
| 268 |
|
| 269 |
|
| 270 |
def test_swt2_axes(): |
| 271 |
atol = 1e-14 |
| 272 |
current_wavelet = pywt.Wavelet('db2') |
| 273 |
input_length_power = int(np.ceil(np.log2(max( |
| 274 |
current_wavelet.dec_len, |
| 275 |
current_wavelet.rec_len)))) |
| 276 |
input_length = 2**(input_length_power) |
| 277 |
X = np.arange(input_length**2).reshape(input_length, input_length) |
| 278 |
|
| 279 |
(cA1, (cH1, cV1, cD1)) = pywt.swt2(X, current_wavelet, level=1)[0] |
| 280 |
|
| 281 |
(cA2, (cH2, cV2, cD2)) = pywt.swt2(X, current_wavelet, level=1, |
| 282 |
axes=(1, 0))[0] |
| 283 |
assert_allclose(cA1, cA2, atol=atol) |
| 284 |
assert_allclose(cH1, cV2, atol=atol) |
| 285 |
assert_allclose(cV1, cH2, atol=atol) |
| 286 |
assert_allclose(cD1, cD2, atol=atol) |
| 287 |
|
| 288 |
|
| 289 |
r1 = pywt.iswt2([cA1, (cH1, cV1, cD1)], current_wavelet) |
| 290 |
assert_allclose(X, r1, atol=atol) |
| 291 |
r2 = pywt.iswt2([cA2, (cH2, cV2, cD2)], current_wavelet, axes=(1, 0)) |
| 292 |
assert_allclose(X, r2, atol=atol) |
| 293 |
|
| 294 |
|
| 295 |
assert_raises(ValueError, pywt.swt2, X, current_wavelet, 1, |
| 296 |
axes=(0, 0)) |
| 297 |
|
| 298 |
assert_raises(ValueError, pywt.swt2, X, current_wavelet, 1, axes=(0, )) |
| 299 |
|
| 300 |
|
| 301 |
def test_swtn_axes(): |
| 302 |
atol = 1e-14 |
| 303 |
current_wavelet = pywt.Wavelet('db2') |
| 304 |
input_length_power = int(np.ceil(np.log2(max( |
| 305 |
current_wavelet.dec_len, |
| 306 |
current_wavelet.rec_len)))) |
| 307 |
input_length = 2**(input_length_power) |
| 308 |
X = np.arange(input_length**2).reshape(input_length, input_length) |
| 309 |
coeffs = pywt.swtn(X, current_wavelet, level=1, axes=None)[0] |
| 310 |
|
| 311 |
coeffs2 = pywt.swtn(X, current_wavelet, level=1, axes=(1, 0))[0] |
| 312 |
assert_allclose(coeffs['aa'], coeffs2['aa'], atol=atol) |
| 313 |
assert_allclose(coeffs['ad'], coeffs2['da'], atol=atol) |
| 314 |
assert_allclose(coeffs['da'], coeffs2['ad'], atol=atol) |
| 315 |
assert_allclose(coeffs['dd'], coeffs2['dd'], atol=atol) |
| 316 |
|
| 317 |
|
| 318 |
empty = pywt.swtn(X, current_wavelet, level=0) |
| 319 |
assert_equal(empty, []) |
| 320 |
|
| 321 |
|
| 322 |
assert_raises(ValueError, pywt.swtn, X, current_wavelet, 1, axes=(0, 0)) |
| 323 |
|
| 324 |
|
| 325 |
assert_raises(ValueError, pywt.swtn, np.asarray([]), current_wavelet, 1) |
| 326 |
|
| 327 |
|
| 328 |
assert_raises(ValueError, pywt.swtn, X, current_wavelet, |
| 329 |
level=1, start_level=2) |
| 330 |
|
| 331 |
|
| 332 |
assert_raises(ValueError, swt_axis, X, current_wavelet, level=0, |
| 333 |
start_level=0) |
| 334 |
|
| 335 |
assert_raises(ValueError, swt_axis, X[:-1, :], current_wavelet, level=0, |
| 336 |
start_level=0, axis=0) |
| 337 |
|
| 338 |
|
| 339 |
@pytest.mark.slow |
| 340 |
def test_swtn_iswtn_integration(wavelets=None): |
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
max_level = 3 |
| 351 |
if wavelets is None: |
| 352 |
wavelets = pywt.wavelist(kind='discrete') |
| 353 |
if 'dmey' in wavelets: |
| 354 |
|
| 355 |
wavelets.remove('dmey') |
| 356 |
for ndim_transform in range(1, 3): |
| 357 |
ndim = ndim_transform + 1 |
| 358 |
for axes in combinations(range(ndim), ndim_transform): |
| 359 |
for current_wavelet_str in wavelets: |
| 360 |
wav = pywt.Wavelet(current_wavelet_str) |
| 361 |
if wav.dec_len > 8: |
| 362 |
continue |
| 363 |
input_length_power = int(np.ceil(np.log2(max( |
| 364 |
wav.dec_len, |
| 365 |
wav.rec_len)))) |
| 366 |
N = 2**(input_length_power + max_level - 1) |
| 367 |
X = np.arange(N**ndim).reshape((N, )*ndim) |
| 368 |
|
| 369 |
for norm in [True, False]: |
| 370 |
if norm and not wav.orthogonal: |
| 371 |
|
| 372 |
continue |
| 373 |
for trim_approx in [True, False]: |
| 374 |
coeffs = pywt.swtn(X, wav, max_level, axes=axes, |
| 375 |
trim_approx=trim_approx, norm=norm) |
| 376 |
coeffs_copy = deepcopy(coeffs) |
| 377 |
Y = pywt.iswtn(coeffs, wav, axes=axes, norm=norm) |
| 378 |
assert_allclose(Y, X, rtol=1e-5, atol=1e-5) |
| 379 |
|
| 380 |
|
| 381 |
for c, c2 in zip(coeffs, coeffs_copy): |
| 382 |
for k, v in c.items(): |
| 383 |
assert_array_equal(c2[k], v) |
| 384 |
|
| 385 |
|
| 386 |
def test_swtn_iswtn_quick(): |
| 387 |
test_swtn_iswtn_integration(wavelets=['db1', ]) |
| 388 |
|
| 389 |
|
| 390 |
def test_iswtn_errors(): |
| 391 |
x = np.arange(8**3).reshape(8, 8, 8) |
| 392 |
max_level = 2 |
| 393 |
axes = (0, 1) |
| 394 |
w = pywt.Wavelet('db1') |
| 395 |
coeffs = pywt.swtn(x, w, max_level, axes=axes) |
| 396 |
|
| 397 |
|
| 398 |
assert_raises(ValueError, pywt.iswtn, coeffs, w, axes=(0, 1, 2)) |
| 399 |
|
| 400 |
assert_raises(ValueError, pywt.iswtn, coeffs, w, axes=(0, 0)) |
| 401 |
|
| 402 |
coeffs[0]['da'] = coeffs[0]['da'][:-1, :] |
| 403 |
assert_raises(RuntimeError, pywt.iswtn, coeffs, w, axes=axes) |
| 404 |
|
| 405 |
|
| 406 |
def test_swtn_iswtn_unique_shape_per_axis(): |
| 407 |
|
| 408 |
_shape = (1, 48, 32) |
| 409 |
wav = 'sym2' |
| 410 |
max_level = 3 |
| 411 |
rstate = np.random.RandomState(0) |
| 412 |
for shape in permutations(_shape): |
| 413 |
|
| 414 |
axes = [ax for ax, s in enumerate(shape) if s != 1] |
| 415 |
x = rstate.standard_normal(shape) |
| 416 |
c = pywt.swtn(x, wav, max_level, axes=axes) |
| 417 |
r = pywt.iswtn(c, wav, axes=axes) |
| 418 |
assert_allclose(x, r, rtol=1e-10, atol=1e-10) |
| 419 |
|
| 420 |
|
| 421 |
def test_per_axis_wavelets(): |
| 422 |
|
| 423 |
rstate = np.random.RandomState(1234) |
| 424 |
data = rstate.randn(16, 16, 16) |
| 425 |
level = 3 |
| 426 |
|
| 427 |
|
| 428 |
wavelets = (pywt.Wavelet('haar'), 'sym2', 'db4') |
| 429 |
|
| 430 |
coefs = pywt.swtn(data, wavelets, level=level) |
| 431 |
assert_allclose(pywt.iswtn(coefs, wavelets), data, atol=1e-14) |
| 432 |
|
| 433 |
|
| 434 |
coefs = pywt.swtn(data, wavelets[:1], level=level) |
| 435 |
assert_allclose(pywt.iswtn(coefs, wavelets[:1]), data, atol=1e-14) |
| 436 |
|
| 437 |
|
| 438 |
assert_raises(ValueError, pywt.swtn, data, wavelets[:2], level) |
| 439 |
assert_raises(ValueError, pywt.iswtn, coefs, wavelets[:2]) |
| 440 |
|
| 441 |
with warnings.catch_warnings(): |
| 442 |
warnings.simplefilter('ignore', FutureWarning) |
| 443 |
|
| 444 |
data2 = data[..., 0] |
| 445 |
coefs2 = pywt.swt2(data2, wavelets[:2], level) |
| 446 |
assert_allclose(pywt.iswt2(coefs2, wavelets[:2]), data2, atol=1e-14) |
| 447 |
|
| 448 |
|
| 449 |
def test_error_on_continuous_wavelet(): |
| 450 |
|
| 451 |
data = np.ones((16, 16)) |
| 452 |
for dec_func, rec_func in zip([pywt.swt, pywt.swt2, pywt.swtn], |
| 453 |
[pywt.iswt, pywt.iswt2, pywt.iswtn]): |
| 454 |
for cwave in ['morl', pywt.DiscreteContinuousWavelet('morl')]: |
| 455 |
assert_raises(ValueError, dec_func, data, wavelet=cwave, |
| 456 |
level=3) |
| 457 |
|
| 458 |
c = dec_func(data, 'db1', level=3) |
| 459 |
assert_raises(ValueError, rec_func, c, wavelet=cwave) |
| 460 |
|
| 461 |
|
| 462 |
def test_iswt_mixed_dtypes(): |
| 463 |
|
| 464 |
x_real = np.arange(16).astype(np.float64) |
| 465 |
x_complex = x_real + 1j*x_real |
| 466 |
wav = 'sym2' |
| 467 |
for dtype1, dtype2 in [(np.float64, np.float32), |
| 468 |
(np.float32, np.float64), |
| 469 |
(np.float16, np.float64), |
| 470 |
(np.complex128, np.complex64), |
| 471 |
(np.complex64, np.complex128)]: |
| 472 |
|
| 473 |
if dtype1 in [np.complex64, np.complex128]: |
| 474 |
x = x_complex |
| 475 |
output_dtype = np.complex128 |
| 476 |
else: |
| 477 |
x = x_real |
| 478 |
output_dtype = np.float64 |
| 479 |
|
| 480 |
coeffs = pywt.swt(x, wav, 2) |
| 481 |
|
| 482 |
coeffs[0] = [coeffs[0][0].astype(dtype1), |
| 483 |
coeffs[0][1].astype(dtype2)] |
| 484 |
y = pywt.iswt(coeffs, wav) |
| 485 |
assert_equal(output_dtype, y.dtype) |
| 486 |
assert_allclose(y, x, rtol=1e-3, atol=1e-3) |
| 487 |
|
| 488 |
|
| 489 |
def test_iswt2_mixed_dtypes(): |
| 490 |
|
| 491 |
rstate = np.random.RandomState(0) |
| 492 |
x_real = rstate.randn(8, 8) |
| 493 |
x_complex = x_real + 1j*x_real |
| 494 |
wav = 'sym2' |
| 495 |
for dtype1, dtype2 in [(np.float64, np.float32), |
| 496 |
(np.float32, np.float64), |
| 497 |
(np.float16, np.float64), |
| 498 |
(np.complex128, np.complex64), |
| 499 |
(np.complex64, np.complex128)]: |
| 500 |
|
| 501 |
if dtype1 in [np.complex64, np.complex128]: |
| 502 |
x = x_complex |
| 503 |
output_dtype = np.complex128 |
| 504 |
else: |
| 505 |
x = x_real |
| 506 |
output_dtype = np.float64 |
| 507 |
|
| 508 |
coeffs = pywt.swt2(x, wav, 2) |
| 509 |
|
| 510 |
coeffs[0] = [coeffs[0][0].astype(dtype1), |
| 511 |
tuple([c.astype(dtype2) for c in coeffs[0][1]])] |
| 512 |
y = pywt.iswt2(coeffs, wav) |
| 513 |
assert_equal(output_dtype, y.dtype) |
| 514 |
assert_allclose(y, x, rtol=1e-3, atol=1e-3) |
| 515 |
|
| 516 |
|
| 517 |
def test_iswtn_mixed_dtypes(): |
| 518 |
|
| 519 |
rstate = np.random.RandomState(0) |
| 520 |
x_real = rstate.randn(8, 8, 8) |
| 521 |
x_complex = x_real + 1j*x_real |
| 522 |
wav = 'sym2' |
| 523 |
for dtype1, dtype2 in [(np.float64, np.float32), |
| 524 |
(np.float32, np.float64), |
| 525 |
(np.float16, np.float64), |
| 526 |
(np.complex128, np.complex64), |
| 527 |
(np.complex64, np.complex128)]: |
| 528 |
|
| 529 |
if dtype1 in [np.complex64, np.complex128]: |
| 530 |
x = x_complex |
| 531 |
output_dtype = np.complex128 |
| 532 |
else: |
| 533 |
x = x_real |
| 534 |
output_dtype = np.float64 |
| 535 |
|
| 536 |
coeffs = pywt.swtn(x, wav, 2) |
| 537 |
|
| 538 |
a = coeffs[0].pop('a' * x.ndim) |
| 539 |
a = a.astype(dtype1) |
| 540 |
coeffs[0] = {k: c.astype(dtype2) for k, c in coeffs[0].items()} |
| 541 |
coeffs[0]['a' * x.ndim] = a |
| 542 |
y = pywt.iswtn(coeffs, wav) |
| 543 |
assert_equal(output_dtype, y.dtype) |
| 544 |
assert_allclose(y, x, rtol=1e-3, atol=1e-3) |
| 545 |
|
| 546 |
|
| 547 |
def test_swt_zero_size_axes(): |
| 548 |
|
| 549 |
assert_raises(ValueError, pywt.swt, [], 'db2') |
| 550 |
|
| 551 |
|
| 552 |
x = np.ones((1, 4))[0:0, :] |
| 553 |
assert_raises(ValueError, pywt.swtn, x, 'db2', level=1, axes=(0,)) |
| 554 |
|
| 555 |
|
| 556 |
def test_swt_variance_and_energy_preservation(): |
| 557 |
"""Verify that the 1D SWT partitions variance among the coefficients.""" |
| 558 |
|
| 559 |
|
| 560 |
wav = 'db2' |
| 561 |
rstate = np.random.RandomState(5) |
| 562 |
x = rstate.randn(256) |
| 563 |
coeffs = pywt.swt(x, wav, trim_approx=True, norm=True) |
| 564 |
variances = [np.var(c) for c in coeffs] |
| 565 |
assert_allclose(np.sum(variances), np.var(x)) |
| 566 |
|
| 567 |
|
| 568 |
assert_allclose(np.linalg.norm(x), |
| 569 |
np.linalg.norm(np.concatenate(coeffs))) |
| 570 |
|
| 571 |
|
| 572 |
assert_warns(UserWarning, pywt.swt, x, 'bior2.2', norm=True) |
| 573 |
|
| 574 |
|
| 575 |
def test_swt2_variance_and_energy_preservation(): |
| 576 |
"""Verify that the 2D SWT partitions variance among the coefficients.""" |
| 577 |
|
| 578 |
|
| 579 |
wav = 'db2' |
| 580 |
rstate = np.random.RandomState(5) |
| 581 |
x = rstate.randn(64, 64) |
| 582 |
coeffs = pywt.swt2(x, wav, level=4, trim_approx=True, norm=True) |
| 583 |
coeff_list = [coeffs[0].ravel()] |
| 584 |
for d in coeffs[1:]: |
| 585 |
for v in d: |
| 586 |
coeff_list.append(v.ravel()) |
| 587 |
variances = [np.var(v) for v in coeff_list] |
| 588 |
assert_allclose(np.sum(variances), np.var(x)) |
| 589 |
|
| 590 |
|
| 591 |
assert_allclose(np.linalg.norm(x), |
| 592 |
np.linalg.norm(np.concatenate(coeff_list))) |
| 593 |
|
| 594 |
|
| 595 |
assert_warns(UserWarning, pywt.swt2, x, 'bior2.2', level=4, norm=True) |
| 596 |
|
| 597 |
|
| 598 |
def test_swtn_variance_and_energy_preservation(): |
| 599 |
"""Verify that the nD SWT partitions variance among the coefficients.""" |
| 600 |
|
| 601 |
|
| 602 |
wav = 'db2' |
| 603 |
rstate = np.random.RandomState(5) |
| 604 |
x = rstate.randn(64, 64) |
| 605 |
coeffs = pywt.swtn(x, wav, level=4, trim_approx=True, norm=True) |
| 606 |
coeff_list = [coeffs[0].ravel()] |
| 607 |
for d in coeffs[1:]: |
| 608 |
for k, v in d.items(): |
| 609 |
coeff_list.append(v.ravel()) |
| 610 |
variances = [np.var(v) for v in coeff_list] |
| 611 |
assert_allclose(np.sum(variances), np.var(x)) |
| 612 |
|
| 613 |
|
| 614 |
assert_allclose(np.linalg.norm(x), |
| 615 |
np.linalg.norm(np.concatenate(coeff_list))) |
| 616 |
|
| 617 |
|
| 618 |
assert_warns(UserWarning, pywt.swtn, x, 'bior2.2', level=4, norm=True) |
| 619 |
|
| 620 |
|
| 621 |
def test_swt_ravel_and_unravel(): |
| 622 |
|
| 623 |
for ndim, _swt, _iswt, ravel_type in [ |
| 624 |
(1, pywt.swt, pywt.iswt, 'swt'), |
| 625 |
(2, pywt.swt2, pywt.iswt2, 'swt2'), |
| 626 |
(3, pywt.swtn, pywt.iswtn, 'swtn')]: |
| 627 |
x = np.ones((16, ) * ndim) |
| 628 |
c = _swt(x, 'sym2', level=3, trim_approx=True) |
| 629 |
arr, slices, shapes = pywt.ravel_coeffs(c) |
| 630 |
c = pywt.unravel_coeffs(arr, slices, shapes, output_format=ravel_type) |
| 631 |
r = _iswt(c, 'sym2') |
| 632 |
assert_allclose(x, r) |