| 1 |
|
| 2 |
|
| 3 |
from __future__ import division, print_function, absolute_import |
| 4 |
|
| 5 |
import warnings |
| 6 |
from itertools import combinations |
| 7 |
import numpy as np |
| 8 |
import pytest |
| 9 |
from numpy.testing import (assert_almost_equal, assert_allclose, assert_, |
| 10 |
assert_equal, assert_raises, assert_raises_regex, |
| 11 |
assert_array_equal, assert_warns) |
| 12 |
import pywt |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
dtypes_in = [np.int8, np.float16, np.float32, np.float64, np.complex64, |
| 17 |
np.complex128] |
| 18 |
dtypes_out = [np.float64, np.float32, np.float32, np.float64, np.complex64, |
| 19 |
np.complex128] |
| 20 |
|
| 21 |
|
| 22 |
tol_single = 1e-6 |
| 23 |
tol_double = 1e-13 |
| 24 |
dtypes_and_tolerances = [(np.float16, tol_single), (np.float32, tol_single), |
| 25 |
(np.float64, tol_double), (np.int8, tol_double), |
| 26 |
(np.complex64, tol_single), |
| 27 |
(np.complex128, tol_double)] |
| 28 |
|
| 29 |
|
| 30 |
try: |
| 31 |
dtypes_in += [np.complex256, ] |
| 32 |
dtypes_out += [np.complex128, ] |
| 33 |
dtypes_and_tolerances += [(np.complex256, tol_double), ] |
| 34 |
except AttributeError: |
| 35 |
pass |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
wavelist = pywt.wavelist() |
| 40 |
if 'dmey' in wavelist: |
| 41 |
|
| 42 |
wavelist.remove('dmey') |
| 43 |
|
| 44 |
|
| 45 |
del_list = [] |
| 46 |
for wavelet in wavelist: |
| 47 |
with warnings.catch_warnings(): |
| 48 |
warnings.simplefilter('ignore', FutureWarning) |
| 49 |
if not isinstance(pywt.DiscreteContinuousWavelet(wavelet), |
| 50 |
pywt.Wavelet): |
| 51 |
del_list.append(wavelet) |
| 52 |
for del_ind in del_list: |
| 53 |
wavelist.remove(del_ind) |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
def test_wavedec(): |
| 61 |
x = [3, 7, 1, 1, -2, 5, 4, 6] |
| 62 |
db1 = pywt.Wavelet('db1') |
| 63 |
cA3, cD3, cD2, cD1 = pywt.wavedec(x, db1) |
| 64 |
assert_almost_equal(cA3, [8.83883476]) |
| 65 |
assert_almost_equal(cD3, [-0.35355339]) |
| 66 |
assert_allclose(cD2, [4., -3.5]) |
| 67 |
assert_allclose(cD1, [-2.82842712, 0, -4.94974747, -1.41421356]) |
| 68 |
assert_(pywt.dwt_max_level(len(x), db1) == 3) |
| 69 |
|
| 70 |
|
| 71 |
def test_waverec_invalid_inputs(): |
| 72 |
|
| 73 |
assert_raises(ValueError, pywt.waverec, np.ones(8), 'haar') |
| 74 |
|
| 75 |
|
| 76 |
assert_raises(ValueError, pywt.waverec, [], 'haar') |
| 77 |
|
| 78 |
|
| 79 |
x = [3, 7, 1, 1, -2, 5, 4, 6] |
| 80 |
coeffs = pywt.wavedec(x, 'db1') |
| 81 |
arr, coeff_slices = pywt.coeffs_to_array(coeffs) |
| 82 |
coeffs_from_arr = pywt.array_to_coeffs(arr, coeff_slices) |
| 83 |
message = "Unexpected detail coefficient type" |
| 84 |
assert_raises_regex(ValueError, message, pywt.waverec, coeffs_from_arr, |
| 85 |
'haar') |
| 86 |
|
| 87 |
|
| 88 |
def test_waverec_accuracies(): |
| 89 |
rstate = np.random.RandomState(1234) |
| 90 |
x0 = rstate.randn(8) |
| 91 |
for dt, tol in dtypes_and_tolerances: |
| 92 |
x = x0.astype(dt) |
| 93 |
if np.iscomplexobj(x): |
| 94 |
x += 1j*rstate.randn(8).astype(x.real.dtype) |
| 95 |
coeffs = pywt.wavedec(x, 'db1') |
| 96 |
assert_allclose(pywt.waverec(coeffs, 'db1'), x, atol=tol, rtol=tol) |
| 97 |
|
| 98 |
|
| 99 |
def test_waverec_none(): |
| 100 |
x = [3, 7, 1, 1, -2, 5, 4, 6] |
| 101 |
coeffs = pywt.wavedec(x, 'db1') |
| 102 |
|
| 103 |
|
| 104 |
coeffs[2] = None |
| 105 |
coeffs[0] = None |
| 106 |
assert_(pywt.waverec(coeffs, 'db1').size, len(x)) |
| 107 |
|
| 108 |
|
| 109 |
def test_waverec_odd_length(): |
| 110 |
x = [3, 7, 1, 1, -2, 5] |
| 111 |
coeffs = pywt.wavedec(x, 'db1') |
| 112 |
assert_allclose(pywt.waverec(coeffs, 'db1'), x, rtol=1e-12) |
| 113 |
|
| 114 |
|
| 115 |
def test_waverec_complex(): |
| 116 |
x = np.array([3, 7, 1, 1, -2, 5, 4, 6]) |
| 117 |
x = x + 1j |
| 118 |
coeffs = pywt.wavedec(x, 'db1') |
| 119 |
assert_allclose(pywt.waverec(coeffs, 'db1'), x, rtol=1e-12) |
| 120 |
|
| 121 |
|
| 122 |
def test_multilevel_dtypes_1d(): |
| 123 |
|
| 124 |
wavelet = pywt.Wavelet('haar') |
| 125 |
for dt_in, dt_out in zip(dtypes_in, dtypes_out): |
| 126 |
|
| 127 |
x = np.ones(8, dtype=dt_in) |
| 128 |
errmsg = "wrong dtype returned for {0} input".format(dt_in) |
| 129 |
|
| 130 |
coeffs = pywt.wavedec(x, wavelet, level=2) |
| 131 |
for c in coeffs: |
| 132 |
assert_(c.dtype == dt_out, "wavedec: " + errmsg) |
| 133 |
x_roundtrip = pywt.waverec(coeffs, wavelet) |
| 134 |
assert_(x_roundtrip.dtype == dt_out, "waverec: " + errmsg) |
| 135 |
|
| 136 |
|
| 137 |
def test_waverec_all_wavelets_modes(): |
| 138 |
|
| 139 |
rstate = np.random.RandomState(1234) |
| 140 |
r = rstate.randn(80) |
| 141 |
for wavelet in wavelist: |
| 142 |
for mode in pywt.Modes.modes: |
| 143 |
coeffs = pywt.wavedec(r, wavelet, mode=mode) |
| 144 |
assert_allclose(pywt.waverec(coeffs, wavelet, mode=mode), |
| 145 |
r, rtol=tol_single, atol=tol_single) |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
def test_waverec2_accuracies(): |
| 153 |
rstate = np.random.RandomState(1234) |
| 154 |
x0 = rstate.randn(4, 4) |
| 155 |
for dt, tol in dtypes_and_tolerances: |
| 156 |
x = x0.astype(dt) |
| 157 |
if np.iscomplexobj(x): |
| 158 |
x += 1j*rstate.randn(4, 4).astype(x.real.dtype) |
| 159 |
coeffs = pywt.wavedec2(x, 'db1') |
| 160 |
assert_(len(coeffs) == 3) |
| 161 |
assert_allclose(pywt.waverec2(coeffs, 'db1'), x, atol=tol, rtol=tol) |
| 162 |
|
| 163 |
|
| 164 |
def test_multilevel_dtypes_2d(): |
| 165 |
wavelet = pywt.Wavelet('haar') |
| 166 |
for dt_in, dt_out in zip(dtypes_in, dtypes_out): |
| 167 |
|
| 168 |
x = np.ones((8, 8), dtype=dt_in) |
| 169 |
errmsg = "wrong dtype returned for {0} input".format(dt_in) |
| 170 |
cA, coeffsD2, coeffsD1 = pywt.wavedec2(x, wavelet, level=2) |
| 171 |
assert_(cA.dtype == dt_out, "wavedec2: " + errmsg) |
| 172 |
for c in coeffsD1: |
| 173 |
assert_(c.dtype == dt_out, "wavedec2: " + errmsg) |
| 174 |
for c in coeffsD2: |
| 175 |
assert_(c.dtype == dt_out, "wavedec2: " + errmsg) |
| 176 |
x_roundtrip = pywt.waverec2([cA, coeffsD2, coeffsD1], wavelet) |
| 177 |
assert_(x_roundtrip.dtype == dt_out, "waverec2: " + errmsg) |
| 178 |
|
| 179 |
|
| 180 |
@pytest.mark.slow |
| 181 |
def test_waverec2_all_wavelets_modes(): |
| 182 |
|
| 183 |
rstate = np.random.RandomState(1234) |
| 184 |
r = rstate.randn(80, 96) |
| 185 |
for wavelet in wavelist: |
| 186 |
for mode in pywt.Modes.modes: |
| 187 |
coeffs = pywt.wavedec2(r, wavelet, mode=mode) |
| 188 |
assert_allclose(pywt.waverec2(coeffs, wavelet, mode=mode), |
| 189 |
r, rtol=tol_single, atol=tol_single) |
| 190 |
|
| 191 |
|
| 192 |
def test_wavedec2_complex(): |
| 193 |
data = np.ones((4, 4)) + 1j |
| 194 |
coeffs = pywt.wavedec2(data, 'db1') |
| 195 |
assert_(len(coeffs) == 3) |
| 196 |
assert_allclose(pywt.waverec2(coeffs, 'db1'), data, rtol=1e-12) |
| 197 |
|
| 198 |
|
| 199 |
def test_wavedec2_invalid_inputs(): |
| 200 |
|
| 201 |
data = np.ones(4) |
| 202 |
assert_raises(ValueError, pywt.wavedec2, data, 'haar') |
| 203 |
|
| 204 |
|
| 205 |
def test_waverec2_invalid_inputs(): |
| 206 |
|
| 207 |
assert_raises(ValueError, pywt.waverec2, np.ones((8, 8)), 'haar') |
| 208 |
|
| 209 |
|
| 210 |
assert_raises(ValueError, pywt.waverec2, [], 'haar') |
| 211 |
|
| 212 |
|
| 213 |
for dec_func in [pywt.wavedec, pywt.wavedecn]: |
| 214 |
coeffs = dec_func(np.ones((8, 8)), 'haar') |
| 215 |
message = "Unexpected detail coefficient type" |
| 216 |
assert_raises_regex(ValueError, message, pywt.waverec2, coeffs, |
| 217 |
'haar') |
| 218 |
|
| 219 |
|
| 220 |
def test_waverec2_coeff_shape_mismatch(): |
| 221 |
x = np.ones((8, 8)) |
| 222 |
coeffs = pywt.wavedec2(x, 'db1') |
| 223 |
|
| 224 |
|
| 225 |
coeffs = list(coeffs) |
| 226 |
coeffs[1] = list(coeffs[1]) |
| 227 |
coeffs[1][1] = np.zeros((16, 1)) |
| 228 |
assert_raises(ValueError, pywt.waverec2, coeffs, 'db1') |
| 229 |
|
| 230 |
|
| 231 |
def test_waverec2_odd_length(): |
| 232 |
x = np.ones((10, 6)) |
| 233 |
coeffs = pywt.wavedec2(x, 'db1') |
| 234 |
assert_allclose(pywt.waverec2(coeffs, 'db1'), x, rtol=1e-12) |
| 235 |
|
| 236 |
|
| 237 |
def test_waverec2_none_coeffs(): |
| 238 |
x = np.arange(24).reshape(6, 4) |
| 239 |
coeffs = pywt.wavedec2(x, 'db1') |
| 240 |
coeffs[1] = (None, None, None) |
| 241 |
assert_(x.shape == pywt.waverec2(coeffs, 'db1').shape) |
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
def test_waverecn(): |
| 249 |
rstate = np.random.RandomState(1234) |
| 250 |
|
| 251 |
for nd in range(1, 5): |
| 252 |
x = rstate.randn(*(4, )*nd) |
| 253 |
coeffs = pywt.wavedecn(x, 'db1') |
| 254 |
assert_(len(coeffs) == 3) |
| 255 |
assert_allclose(pywt.waverecn(coeffs, 'db1'), x, rtol=tol_double) |
| 256 |
|
| 257 |
|
| 258 |
def test_waverecn_empty_coeff(): |
| 259 |
coeffs = [np.ones((2, 2, 2)), {}, {}] |
| 260 |
assert_equal(pywt.waverecn(coeffs, 'db1').shape, (8, 8, 8)) |
| 261 |
|
| 262 |
assert_equal(pywt.waverecn(coeffs, 'db1').shape, (8, 8, 8)) |
| 263 |
coeffs = [np.ones((2, 2, 2)), {}, {'daa': np.ones((4, 4, 4))}] |
| 264 |
|
| 265 |
coeffs = [np.ones((2, 2, 2)), {}, {}, {'daa': np.ones((8, 8, 8))}] |
| 266 |
assert_equal(pywt.waverecn(coeffs, 'db1').shape, (16, 16, 16)) |
| 267 |
|
| 268 |
|
| 269 |
def test_waverecn_invalid_coeffs(): |
| 270 |
|
| 271 |
coeffs = [None, {}] |
| 272 |
assert_raises(ValueError, pywt.waverecn, coeffs, 'db1') |
| 273 |
|
| 274 |
|
| 275 |
coeffs = [np.ones((2, 2, 2)), {}, {'daa': None}, ] |
| 276 |
assert_raises(ValueError, pywt.waverecn, coeffs, 'db1') |
| 277 |
|
| 278 |
|
| 279 |
coeffs = [np.ones((4, 4, 4)), {'daa': np.ones((4, 4, 4)), |
| 280 |
'foo': np.ones((4, 4, 4))}] |
| 281 |
assert_raises(ValueError, pywt.waverecn, coeffs, 'db1') |
| 282 |
|
| 283 |
|
| 284 |
coeffs = [np.ones((4, 4, 4)), {'daa': np.ones((4, 4, 4)), |
| 285 |
'da': np.ones((4, 4, 4))}] |
| 286 |
assert_raises(ValueError, pywt.waverecn, coeffs, 'db1') |
| 287 |
|
| 288 |
|
| 289 |
coeffs = [[[[1.0]]], {'ad': [[[0.0]]], 'da': [[[0.0]]], 'dd': [[[0.0]]]}] |
| 290 |
assert_raises(ValueError, pywt.waverecn, coeffs, 'db1') |
| 291 |
|
| 292 |
|
| 293 |
assert_raises(ValueError, pywt.waverecn, [], 'haar') |
| 294 |
|
| 295 |
|
| 296 |
def test_waverecn_invalid_inputs(): |
| 297 |
|
| 298 |
|
| 299 |
for dec_func in [pywt.wavedec, pywt.wavedec2]: |
| 300 |
coeffs = dec_func(np.ones((8, 8)), 'haar') |
| 301 |
message = "Unexpected detail coefficient type" |
| 302 |
assert_raises_regex(ValueError, message, pywt.waverecn, coeffs, |
| 303 |
'haar') |
| 304 |
|
| 305 |
|
| 306 |
def test_waverecn_lists(): |
| 307 |
|
| 308 |
coeffs = [[[1.0]], {'ad': [[0.0]], 'da': [[0.0]], 'dd': [[0.0]]}] |
| 309 |
assert_equal(pywt.waverecn(coeffs, 'db1').shape, (2, 2)) |
| 310 |
|
| 311 |
|
| 312 |
def test_waverecn_invalid_coeffs2(): |
| 313 |
|
| 314 |
coeffs = [np.ones((4, 4, 4)), {'ada': np.ones((4, 4))}] |
| 315 |
assert_raises(ValueError, pywt.waverecn, coeffs, 'db1') |
| 316 |
|
| 317 |
|
| 318 |
def test_wavedecn_invalid_inputs(): |
| 319 |
|
| 320 |
data = np.array(0) |
| 321 |
assert_raises(ValueError, pywt.wavedecn, data, 'haar') |
| 322 |
|
| 323 |
|
| 324 |
data = np.ones(16) |
| 325 |
assert_raises(ValueError, pywt.wavedecn, data, 'haar', level=-1) |
| 326 |
|
| 327 |
|
| 328 |
def test_wavedecn_many_levels(): |
| 329 |
|
| 330 |
data = np.arange(64).reshape(8, 8) |
| 331 |
tol = 1e-12 |
| 332 |
dec_funcs = [pywt.wavedec, pywt.wavedec2, pywt.wavedecn] |
| 333 |
rec_funcs = [pywt.waverec, pywt.waverec2, pywt.waverecn] |
| 334 |
with warnings.catch_warnings(): |
| 335 |
warnings.simplefilter('ignore', UserWarning) |
| 336 |
for dec_func, rec_func in zip(dec_funcs, rec_funcs): |
| 337 |
for mode in ['periodization', 'symmetric']: |
| 338 |
coeffs = dec_func(data, 'haar', mode=mode, level=20) |
| 339 |
r = rec_func(coeffs, 'haar', mode=mode) |
| 340 |
assert_allclose(data, r, atol=tol, rtol=tol) |
| 341 |
|
| 342 |
|
| 343 |
def test_waverecn_accuracies(): |
| 344 |
|
| 345 |
rstate = np.random.RandomState(1234) |
| 346 |
x0 = rstate.randn(4, 4, 4) |
| 347 |
for dt, tol in dtypes_and_tolerances: |
| 348 |
x = x0.astype(dt) |
| 349 |
if np.iscomplexobj(x): |
| 350 |
x += 1j*rstate.randn(4, 4, 4).astype(x.real.dtype) |
| 351 |
coeffs = pywt.wavedecn(x.astype(dt), 'db1') |
| 352 |
assert_allclose(pywt.waverecn(coeffs, 'db1'), x, atol=tol, rtol=tol) |
| 353 |
|
| 354 |
|
| 355 |
def test_multilevel_dtypes_nd(): |
| 356 |
wavelet = pywt.Wavelet('haar') |
| 357 |
for dt_in, dt_out in zip(dtypes_in, dtypes_out): |
| 358 |
|
| 359 |
x = np.ones((8, 8), dtype=dt_in) |
| 360 |
errmsg = "wrong dtype returned for {0} input".format(dt_in) |
| 361 |
cA, coeffsD2, coeffsD1 = pywt.wavedecn(x, wavelet, level=2) |
| 362 |
assert_(cA.dtype == dt_out, "wavedecn: " + errmsg) |
| 363 |
for key, c in coeffsD1.items(): |
| 364 |
assert_(c.dtype == dt_out, "wavedecn: " + errmsg) |
| 365 |
for key, c in coeffsD2.items(): |
| 366 |
assert_(c.dtype == dt_out, "wavedecn: " + errmsg) |
| 367 |
x_roundtrip = pywt.waverecn([cA, coeffsD2, coeffsD1], wavelet) |
| 368 |
assert_(x_roundtrip.dtype == dt_out, "waverecn: " + errmsg) |
| 369 |
|
| 370 |
|
| 371 |
def test_wavedecn_complex(): |
| 372 |
data = np.ones((4, 4, 4)) + 1j |
| 373 |
coeffs = pywt.wavedecn(data, 'db1') |
| 374 |
assert_allclose(pywt.waverecn(coeffs, 'db1'), data, rtol=1e-12) |
| 375 |
|
| 376 |
|
| 377 |
def test_waverecn_dtypes(): |
| 378 |
x = np.ones((4, 4, 4)) |
| 379 |
for dt, tol in dtypes_and_tolerances: |
| 380 |
coeffs = pywt.wavedecn(x.astype(dt), 'db1') |
| 381 |
assert_allclose(pywt.waverecn(coeffs, 'db1'), x, atol=tol, rtol=tol) |
| 382 |
|
| 383 |
|
| 384 |
@pytest.mark.slow |
| 385 |
def test_waverecn_all_wavelets_modes(): |
| 386 |
|
| 387 |
rstate = np.random.RandomState(1234) |
| 388 |
r = rstate.randn(80, 96) |
| 389 |
for wavelet in wavelist: |
| 390 |
for mode in pywt.Modes.modes: |
| 391 |
coeffs = pywt.wavedecn(r, wavelet, mode=mode) |
| 392 |
assert_allclose(pywt.waverecn(coeffs, wavelet, mode=mode), |
| 393 |
r, rtol=tol_single, atol=tol_single) |
| 394 |
|
| 395 |
|
| 396 |
def test_coeffs_to_array(): |
| 397 |
|
| 398 |
a_coeffs = [np.arange(8).reshape(2, 4), ] |
| 399 |
arr, arr_slices = pywt.coeffs_to_array(a_coeffs) |
| 400 |
assert_allclose(arr, a_coeffs[0]) |
| 401 |
assert_allclose(arr, arr[arr_slices[0]]) |
| 402 |
|
| 403 |
assert_raises(ValueError, pywt.coeffs_to_array, []) |
| 404 |
|
| 405 |
assert_raises(ValueError, pywt.coeffs_to_array, [a_coeffs[0], ] * 2) |
| 406 |
|
| 407 |
assert_raises(ValueError, pywt.coeffs_to_array, [a_coeffs[0], |
| 408 |
(a_coeffs[0], )]) |
| 409 |
|
| 410 |
assert_raises(ValueError, pywt.coeffs_to_array, [None, ]) |
| 411 |
assert_raises(ValueError, pywt.coeffs_to_array, [a_coeffs, |
| 412 |
(None, None, None)]) |
| 413 |
|
| 414 |
|
| 415 |
assert_raises(ValueError, pywt.coeffs_to_array, [a_coeffs, None]) |
| 416 |
|
| 417 |
|
| 418 |
coeffs = [np.array([0]), dict(d=np.array([0]), c=np.array([0]))] |
| 419 |
assert_raises(ValueError, pywt.coeffs_to_array, coeffs) |
| 420 |
|
| 421 |
|
| 422 |
def test_wavedecn_coeff_reshape_even(): |
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
rng = np.random.RandomState(1234) |
| 427 |
params = {'wavedec': {'d': 1, 'dec': pywt.wavedec, 'rec': pywt.waverec}, |
| 428 |
'wavedec2': {'d': 2, 'dec': pywt.wavedec2, 'rec': pywt.waverec2}, |
| 429 |
'wavedecn': {'d': 3, 'dec': pywt.wavedecn, 'rec': pywt.waverecn}} |
| 430 |
N = 28 |
| 431 |
for f in params: |
| 432 |
x1 = rng.randn(*([N] * params[f]['d'])) |
| 433 |
for mode in pywt.Modes.modes: |
| 434 |
for wave in wavelist: |
| 435 |
w = pywt.Wavelet(wave) |
| 436 |
maxlevel = pywt.dwt_max_level(np.min(x1.shape), w.dec_len) |
| 437 |
if maxlevel == 0: |
| 438 |
continue |
| 439 |
|
| 440 |
coeffs = params[f]['dec'](x1, w, mode=mode) |
| 441 |
coeff_arr, coeff_slices = pywt.coeffs_to_array(coeffs) |
| 442 |
coeffs2 = pywt.array_to_coeffs(coeff_arr, coeff_slices, |
| 443 |
output_format=f) |
| 444 |
x1r = params[f]['rec'](coeffs2, w, mode=mode) |
| 445 |
|
| 446 |
assert_allclose(x1, x1r, rtol=1e-4, atol=1e-4) |
| 447 |
|
| 448 |
|
| 449 |
def test_wavedecn_coeff_reshape_axes_subset(): |
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
rng = np.random.RandomState(1234) |
| 454 |
mode = 'symmetric' |
| 455 |
w = pywt.Wavelet('db2') |
| 456 |
N = 16 |
| 457 |
ndim = 3 |
| 458 |
for axes in [(-1, ), (0, ), (1, ), (0, 1), (1, 2), (0, 2), None]: |
| 459 |
x1 = rng.randn(*([N] * ndim)) |
| 460 |
coeffs = pywt.wavedecn(x1, w, mode=mode, axes=axes) |
| 461 |
coeff_arr, coeff_slices = pywt.coeffs_to_array(coeffs, axes=axes) |
| 462 |
if axes is not None: |
| 463 |
|
| 464 |
assert_raises(ValueError, pywt.coeffs_to_array, coeffs) |
| 465 |
|
| 466 |
|
| 467 |
assert_raises(ValueError, pywt.coeffs_to_array, coeffs, |
| 468 |
axes=(0, 1, 2, 3)) |
| 469 |
assert_raises(ValueError, pywt.coeffs_to_array, coeffs, |
| 470 |
axes=()) |
| 471 |
|
| 472 |
coeffs2 = pywt.array_to_coeffs(coeff_arr, coeff_slices) |
| 473 |
x1r = pywt.waverecn(coeffs2, w, mode=mode, axes=axes) |
| 474 |
|
| 475 |
assert_allclose(x1, x1r, rtol=1e-4, atol=1e-4) |
| 476 |
|
| 477 |
|
| 478 |
def test_coeffs_to_array_padding(): |
| 479 |
rng = np.random.RandomState(1234) |
| 480 |
x1 = rng.randn(32, 32) |
| 481 |
mode = 'symmetric' |
| 482 |
coeffs = pywt.wavedecn(x1, 'db2', mode=mode) |
| 483 |
|
| 484 |
|
| 485 |
assert_raises(ValueError, pywt.coeffs_to_array, coeffs, padding=None) |
| 486 |
|
| 487 |
|
| 488 |
coeff_arr, coeff_slices = pywt.coeffs_to_array(coeffs, padding=np.nan) |
| 489 |
npad = np.sum(np.isnan(coeff_arr)) |
| 490 |
assert_(npad > 0) |
| 491 |
|
| 492 |
|
| 493 |
coeff_arr, coeff_slices = pywt.coeffs_to_array(coeffs, padding=0) |
| 494 |
assert_(np.sum(np.isnan(coeff_arr)) == 0) |
| 495 |
assert_(np.sum(coeff_arr == 0) == npad) |
| 496 |
|
| 497 |
|
| 498 |
coeffs_haar = pywt.wavedecn(x1, 'haar', mode=mode) |
| 499 |
coeff_arr, coeff_slices = pywt.coeffs_to_array(coeffs_haar, padding=None) |
| 500 |
|
| 501 |
assert_equal(coeff_arr.shape, x1.shape) |
| 502 |
|
| 503 |
|
| 504 |
def test_waverecn_coeff_reshape_odd(): |
| 505 |
|
| 506 |
|
| 507 |
rng = np.random.RandomState(1234) |
| 508 |
x1 = rng.randn(35, 33) |
| 509 |
for mode in pywt.Modes.modes: |
| 510 |
for wave in ['haar', ]: |
| 511 |
w = pywt.Wavelet(wave) |
| 512 |
maxlevel = pywt.dwt_max_level(np.min(x1.shape), w.dec_len) |
| 513 |
if maxlevel == 0: |
| 514 |
continue |
| 515 |
coeffs = pywt.wavedecn(x1, w, mode=mode) |
| 516 |
coeff_arr, coeff_slices = pywt.coeffs_to_array(coeffs) |
| 517 |
coeffs2 = pywt.array_to_coeffs(coeff_arr, coeff_slices) |
| 518 |
x1r = pywt.waverecn(coeffs2, w, mode=mode) |
| 519 |
|
| 520 |
x1r = x1r[tuple([slice(s) for s in x1.shape])] |
| 521 |
assert_allclose(x1, x1r, rtol=1e-4, atol=1e-4) |
| 522 |
|
| 523 |
|
| 524 |
def test_array_to_coeffs_invalid_inputs(): |
| 525 |
coeffs = pywt.wavedecn(np.ones(2), 'haar') |
| 526 |
arr, arr_slices = pywt.coeffs_to_array(coeffs) |
| 527 |
|
| 528 |
|
| 529 |
assert_raises(ValueError, pywt.array_to_coeffs, arr, []) |
| 530 |
|
| 531 |
|
| 532 |
assert_raises(ValueError, pywt.array_to_coeffs, arr, arr_slices, 'foo') |
| 533 |
|
| 534 |
|
| 535 |
def test_wavedecn_coeff_ravel(): |
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
rng = np.random.RandomState(1234) |
| 540 |
params = {'wavedec': {'d': 1, 'dec': pywt.wavedec, 'rec': pywt.waverec}, |
| 541 |
'wavedec2': {'d': 2, 'dec': pywt.wavedec2, 'rec': pywt.waverec2}, |
| 542 |
'wavedecn': {'d': 3, 'dec': pywt.wavedecn, 'rec': pywt.waverecn}} |
| 543 |
N = 12 |
| 544 |
for f in params: |
| 545 |
x1 = rng.randn(*([N] * params[f]['d'])) |
| 546 |
for mode in pywt.Modes.modes: |
| 547 |
for wave in wavelist: |
| 548 |
w = pywt.Wavelet(wave) |
| 549 |
maxlevel = pywt.dwt_max_level(np.min(x1.shape), w.dec_len) |
| 550 |
if maxlevel == 0: |
| 551 |
continue |
| 552 |
|
| 553 |
coeffs = params[f]['dec'](x1, w, mode=mode) |
| 554 |
coeff_arr, slices, shapes = pywt.ravel_coeffs(coeffs) |
| 555 |
coeffs2 = pywt.unravel_coeffs(coeff_arr, slices, shapes, |
| 556 |
output_format=f) |
| 557 |
x1r = params[f]['rec'](coeffs2, w, mode=mode) |
| 558 |
|
| 559 |
assert_allclose(x1, x1r, rtol=1e-4, atol=1e-4) |
| 560 |
|
| 561 |
|
| 562 |
def test_wavedecn_coeff_ravel_zero_level(): |
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
rng = np.random.RandomState(1234) |
| 567 |
params = {'wavedec': {'d': 1, 'dec': pywt.wavedec, 'rec': pywt.waverec}, |
| 568 |
'wavedec2': {'d': 2, 'dec': pywt.wavedec2, 'rec': pywt.waverec2}, |
| 569 |
'wavedecn': {'d': 3, 'dec': pywt.wavedecn, 'rec': pywt.waverecn}} |
| 570 |
N = 16 |
| 571 |
for f in params: |
| 572 |
x1 = rng.randn(*([N] * params[f]['d'])) |
| 573 |
for mode in pywt.Modes.modes: |
| 574 |
w = pywt.Wavelet('db2') |
| 575 |
|
| 576 |
coeffs = params[f]['dec'](x1, w, mode=mode, level=0) |
| 577 |
coeff_arr, slices, shapes = pywt.ravel_coeffs(coeffs) |
| 578 |
coeffs2 = pywt.unravel_coeffs(coeff_arr, slices, shapes, |
| 579 |
output_format=f) |
| 580 |
x1r = params[f]['rec'](coeffs2, w, mode=mode) |
| 581 |
|
| 582 |
assert_allclose(x1, x1r, rtol=1e-4, atol=1e-4) |
| 583 |
|
| 584 |
|
| 585 |
def test_waverecn_coeff_ravel_odd(): |
| 586 |
|
| 587 |
|
| 588 |
rng = np.random.RandomState(1234) |
| 589 |
x1 = rng.randn(35, 33) |
| 590 |
for mode in pywt.Modes.modes: |
| 591 |
for wave in ['haar', ]: |
| 592 |
w = pywt.Wavelet(wave) |
| 593 |
maxlevel = pywt.dwt_max_level(np.min(x1.shape), w.dec_len) |
| 594 |
if maxlevel == 0: |
| 595 |
continue |
| 596 |
coeffs = pywt.wavedecn(x1, w, mode=mode) |
| 597 |
coeff_arr, slices, shapes = pywt.ravel_coeffs(coeffs) |
| 598 |
coeffs2 = pywt.unravel_coeffs(coeff_arr, slices, shapes) |
| 599 |
x1r = pywt.waverecn(coeffs2, w, mode=mode) |
| 600 |
|
| 601 |
x1r = x1r[tuple([slice(s) for s in x1.shape])] |
| 602 |
assert_allclose(x1, x1r, rtol=1e-4, atol=1e-4) |
| 603 |
|
| 604 |
|
| 605 |
def test_ravel_wavedec2_with_lists(): |
| 606 |
x1 = np.ones((8, 8)) |
| 607 |
wav = pywt.Wavelet('haar') |
| 608 |
coeffs = pywt.wavedec2(x1, wav) |
| 609 |
|
| 610 |
|
| 611 |
coeffs[1:] = [list(c) for c in coeffs[1:]] |
| 612 |
coeff_arr, slices, shapes = pywt.ravel_coeffs(coeffs) |
| 613 |
coeffs2 = pywt.unravel_coeffs(coeff_arr, slices, shapes, |
| 614 |
output_format='wavedec2') |
| 615 |
x1r = pywt.waverec2(coeffs2, wav) |
| 616 |
assert_allclose(x1, x1r, rtol=1e-4, atol=1e-4) |
| 617 |
|
| 618 |
|
| 619 |
coeffs[1:] = [list(c[:-1]) for c in coeffs[1:]] |
| 620 |
assert_raises(ValueError, pywt.ravel_coeffs, coeffs) |
| 621 |
|
| 622 |
|
| 623 |
def test_ravel_invalid_input(): |
| 624 |
|
| 625 |
coeffs = pywt.wavedec(np.ones(8), 'haar') |
| 626 |
coeffs[1] = None |
| 627 |
assert_raises(ValueError, pywt.ravel_coeffs, coeffs) |
| 628 |
|
| 629 |
|
| 630 |
coeffs = pywt.wavedec2(np.ones((8, 8)), 'haar') |
| 631 |
coeffs[1] = (None, None, None) |
| 632 |
assert_raises(ValueError, pywt.ravel_coeffs, coeffs) |
| 633 |
coeffs[1] = [None, None, None] |
| 634 |
assert_raises(ValueError, pywt.ravel_coeffs, coeffs) |
| 635 |
coeffs[1] = None |
| 636 |
assert_raises(ValueError, pywt.ravel_coeffs, coeffs) |
| 637 |
|
| 638 |
|
| 639 |
coeffs = pywt.wavedecn(np.ones((8, 8, 8)), 'haar') |
| 640 |
coeffs[1]['ddd'] = None |
| 641 |
assert_raises(ValueError, pywt.ravel_coeffs, coeffs) |
| 642 |
|
| 643 |
|
| 644 |
def test_unravel_invalid_inputs(): |
| 645 |
coeffs = pywt.wavedecn(np.ones(2), 'haar') |
| 646 |
arr, slices, shapes = pywt.ravel_coeffs(coeffs) |
| 647 |
|
| 648 |
|
| 649 |
assert_raises(ValueError, pywt.unravel_coeffs, arr, slices, []) |
| 650 |
assert_raises(ValueError, pywt.unravel_coeffs, arr, [], shapes) |
| 651 |
|
| 652 |
|
| 653 |
assert_raises(ValueError, pywt.unravel_coeffs, arr, slices[:-1], shapes) |
| 654 |
|
| 655 |
|
| 656 |
assert_raises(ValueError, pywt.unravel_coeffs, arr, slices, shapes, 'foo') |
| 657 |
|
| 658 |
|
| 659 |
def test_wavedecn_shapes_and_size(): |
| 660 |
wav = pywt.Wavelet('db2') |
| 661 |
for data_shape in [(33, ), (64, 32), (1, 15, 30)]: |
| 662 |
for axes in [None, 0, -1]: |
| 663 |
for mode in pywt.Modes.modes: |
| 664 |
coeffs = pywt.wavedecn(np.ones(data_shape), wav, |
| 665 |
mode=mode, axes=axes) |
| 666 |
|
| 667 |
|
| 668 |
shapes = pywt.wavedecn_shapes(data_shape, wav, |
| 669 |
mode=mode, axes=axes) |
| 670 |
|
| 671 |
assert_equal(coeffs[0].shape, shapes[0]) |
| 672 |
expected_size = coeffs[0].size |
| 673 |
for level in range(1, len(coeffs)): |
| 674 |
for k, v in coeffs[level].items(): |
| 675 |
expected_size += v.size |
| 676 |
assert_equal(shapes[level][k], v.shape) |
| 677 |
|
| 678 |
|
| 679 |
size = pywt.wavedecn_size(shapes) |
| 680 |
assert_equal(size, expected_size) |
| 681 |
|
| 682 |
size = pywt.wavedecn_size(coeffs) |
| 683 |
assert_equal(size, expected_size) |
| 684 |
|
| 685 |
|
| 686 |
def test_dwtn_max_level(): |
| 687 |
|
| 688 |
for wav in [pywt.Wavelet('db2'), 'sym8']: |
| 689 |
for data_shape in [(33, ), (64, 32), (1, 15, 30)]: |
| 690 |
for axes in [None, 0, -1]: |
| 691 |
for mode in pywt.Modes.modes: |
| 692 |
coeffs = pywt.wavedecn(np.ones(data_shape), wav, |
| 693 |
mode=mode, axes=axes) |
| 694 |
max_lev = pywt.dwtn_max_level(data_shape, wav, axes) |
| 695 |
assert_equal(len(coeffs[1:]), max_lev) |
| 696 |
|
| 697 |
|
| 698 |
def test_waverec_axes_subsets(): |
| 699 |
rstate = np.random.RandomState(0) |
| 700 |
data = rstate.standard_normal((8, 8, 8)) |
| 701 |
|
| 702 |
for axis in [0, 1, 2]: |
| 703 |
coefs = pywt.wavedec(data, 'haar', axis=axis) |
| 704 |
rec = pywt.waverec(coefs, 'haar', axis=axis) |
| 705 |
assert_allclose(rec, data, atol=1e-14) |
| 706 |
|
| 707 |
|
| 708 |
def test_waverec_axis_db2(): |
| 709 |
|
| 710 |
rstate = np.random.RandomState(0) |
| 711 |
data = rstate.standard_normal((16, 16)) |
| 712 |
for axis in [0, 1]: |
| 713 |
coefs = pywt.wavedec(data, 'db2', axis=axis) |
| 714 |
rec = pywt.waverec(coefs, 'db2', axis=axis) |
| 715 |
assert_allclose(rec, data, atol=1e-14) |
| 716 |
|
| 717 |
|
| 718 |
def test_waverec2_axes_subsets(): |
| 719 |
rstate = np.random.RandomState(0) |
| 720 |
data = rstate.standard_normal((8, 8, 8)) |
| 721 |
|
| 722 |
for axes in combinations((0, 1, 2), 2): |
| 723 |
coefs = pywt.wavedec2(data, 'haar', axes=axes) |
| 724 |
rec = pywt.waverec2(coefs, 'haar', axes=axes) |
| 725 |
assert_allclose(rec, data, atol=1e-14) |
| 726 |
|
| 727 |
|
| 728 |
def test_waverecn_axes_subsets(): |
| 729 |
rstate = np.random.RandomState(0) |
| 730 |
data = rstate.standard_normal((8, 8, 8, 8)) |
| 731 |
|
| 732 |
for axes in combinations((0, 1, 2, 3), 3): |
| 733 |
coefs = pywt.wavedecn(data, 'haar', axes=axes) |
| 734 |
rec = pywt.waverecn(coefs, 'haar', axes=axes) |
| 735 |
assert_allclose(rec, data, atol=1e-14) |
| 736 |
|
| 737 |
|
| 738 |
def test_waverecn_int_axis(): |
| 739 |
|
| 740 |
rstate = np.random.RandomState(0) |
| 741 |
data = rstate.standard_normal((8, 8)) |
| 742 |
for axis in [0, 1]: |
| 743 |
coefs = pywt.wavedecn(data, 'haar', axes=axis) |
| 744 |
rec = pywt.waverecn(coefs, 'haar', axes=axis) |
| 745 |
assert_allclose(rec, data, atol=1e-14) |
| 746 |
|
| 747 |
|
| 748 |
def test_wavedec_axis_error(): |
| 749 |
data = np.ones(4) |
| 750 |
|
| 751 |
assert_raises(ValueError, pywt.wavedec, data, 'haar', axis=1) |
| 752 |
|
| 753 |
|
| 754 |
def test_waverec_axis_error(): |
| 755 |
c = pywt.wavedec(np.ones(4), 'haar') |
| 756 |
|
| 757 |
assert_raises(ValueError, pywt.waverec, c, 'haar', axis=1) |
| 758 |
|
| 759 |
|
| 760 |
def test_waverec_shape_mismatch_error(): |
| 761 |
c = pywt.wavedec(np.ones(16), 'haar') |
| 762 |
|
| 763 |
c[3] = c[3][:-1] |
| 764 |
assert_raises(ValueError, pywt.waverec, c, 'haar', axis=1) |
| 765 |
|
| 766 |
|
| 767 |
def test_wavedec2_axes_errors(): |
| 768 |
data = np.ones((4, 4)) |
| 769 |
|
| 770 |
assert_raises(TypeError, pywt.wavedec2, data, 'haar', axes=1) |
| 771 |
|
| 772 |
assert_raises(ValueError, pywt.wavedec2, data, 'haar', axes=(0, 0)) |
| 773 |
|
| 774 |
assert_raises(ValueError, pywt.wavedec2, data, 'haar', axes=(0, 2)) |
| 775 |
|
| 776 |
|
| 777 |
def test_waverec2_axes_errors(): |
| 778 |
data = np.ones((4, 4)) |
| 779 |
c = pywt.wavedec2(data, 'haar') |
| 780 |
|
| 781 |
assert_raises(TypeError, pywt.waverec2, c, 'haar', axes=1) |
| 782 |
|
| 783 |
assert_raises(ValueError, pywt.waverec2, c, 'haar', axes=(0, 0)) |
| 784 |
|
| 785 |
assert_raises(ValueError, pywt.waverec2, c, 'haar', axes=(0, 2)) |
| 786 |
|
| 787 |
|
| 788 |
def test_wavedecn_axes_errors(): |
| 789 |
data = np.ones((8, 8, 8)) |
| 790 |
|
| 791 |
assert_raises(ValueError, pywt.wavedecn, data, 'haar', axes=(1, 1)) |
| 792 |
|
| 793 |
assert_raises(ValueError, pywt.wavedecn, data, 'haar', axes=(0, 1, 3)) |
| 794 |
|
| 795 |
|
| 796 |
def test_waverecn_axes_errors(): |
| 797 |
data = np.ones((8, 8, 8)) |
| 798 |
c = pywt.wavedecn(data, 'haar') |
| 799 |
|
| 800 |
assert_raises(ValueError, pywt.waverecn, c, 'haar', axes=(1, 1)) |
| 801 |
|
| 802 |
assert_raises(ValueError, pywt.waverecn, c, 'haar', axes=(0, 1, 3)) |
| 803 |
|
| 804 |
|
| 805 |
def test_per_axis_wavelets_and_modes(): |
| 806 |
|
| 807 |
rstate = np.random.RandomState(1234) |
| 808 |
data = rstate.randn(24, 24, 16) |
| 809 |
|
| 810 |
|
| 811 |
wavelets = (pywt.Wavelet('haar'), 'sym2', 'db2') |
| 812 |
|
| 813 |
|
| 814 |
max_levels = [pywt._dwt.dwt_max_level(nd, nf) for nd, nf in |
| 815 |
zip(data.shape, wavelets)] |
| 816 |
|
| 817 |
|
| 818 |
modes = ('symmetric', 'periodization', |
| 819 |
pywt._extensions._pywt.Modes.reflect) |
| 820 |
|
| 821 |
coefs = pywt.wavedecn(data, wavelets, modes) |
| 822 |
assert_allclose(pywt.waverecn(coefs, wavelets, modes), data, atol=1e-14) |
| 823 |
assert_equal(min(max_levels), len(coefs[1:])) |
| 824 |
|
| 825 |
coefs = pywt.wavedecn(data, wavelets[:1], modes) |
| 826 |
assert_allclose(pywt.waverecn(coefs, wavelets[:1], modes), data, |
| 827 |
atol=1e-14) |
| 828 |
|
| 829 |
coefs = pywt.wavedecn(data, wavelets, modes[:1]) |
| 830 |
assert_allclose(pywt.waverecn(coefs, wavelets, modes[:1]), data, |
| 831 |
atol=1e-14) |
| 832 |
|
| 833 |
|
| 834 |
assert_raises(ValueError, pywt.wavedecn, data, wavelets[:2]) |
| 835 |
assert_raises(ValueError, pywt.wavedecn, data, wavelets, mode=modes[:2]) |
| 836 |
assert_raises(ValueError, pywt.waverecn, coefs, wavelets[:2]) |
| 837 |
assert_raises(ValueError, pywt.waverecn, coefs, wavelets, mode=modes[:2]) |
| 838 |
|
| 839 |
|
| 840 |
data2 = data[..., 0] |
| 841 |
coefs2 = pywt.wavedec2(data2, wavelets[:2], modes[:2]) |
| 842 |
assert_allclose(pywt.waverec2(coefs2, wavelets[:2], modes[:2]), data2, |
| 843 |
atol=1e-14) |
| 844 |
assert_equal(min(max_levels[:2]), len(coefs2[1:])) |
| 845 |
|
| 846 |
|
| 847 |
|
| 848 |
|
| 849 |
def test_fswavedecn_fswaverecn_roundtrip(): |
| 850 |
|
| 851 |
|
| 852 |
rstate = np.random.RandomState(0) |
| 853 |
for ndim in range(1, 5): |
| 854 |
for dt_in, dt_out in zip(dtypes_in, dtypes_out): |
| 855 |
for levels in (1, None): |
| 856 |
data = rstate.standard_normal((8, )*ndim) |
| 857 |
data = data.astype(dt_in) |
| 858 |
T = pywt.fswavedecn(data, 'haar', levels=levels) |
| 859 |
rec = pywt.fswaverecn(T) |
| 860 |
if data.real.dtype in [np.float32, np.float16]: |
| 861 |
assert_allclose(rec, data, rtol=1e-6, atol=1e-6) |
| 862 |
else: |
| 863 |
assert_allclose(rec, data, rtol=1e-14, atol=1e-14) |
| 864 |
assert_(T.coeffs.dtype == dt_out) |
| 865 |
assert_(rec.dtype == dt_out) |
| 866 |
|
| 867 |
|
| 868 |
def test_fswavedecn_fswaverecn_zero_levels(): |
| 869 |
|
| 870 |
rstate = np.random.RandomState(0) |
| 871 |
ndim = 2 |
| 872 |
data = rstate.standard_normal((8, )*ndim) |
| 873 |
T = pywt.fswavedecn(data, 'haar', levels=0) |
| 874 |
assert_array_equal(T.coeffs, data) |
| 875 |
rec = pywt.fswaverecn(T) |
| 876 |
assert_array_equal(T.coeffs, rec) |
| 877 |
|
| 878 |
|
| 879 |
def test_fswavedecn_fswaverecn_variable_levels(): |
| 880 |
|
| 881 |
rstate = np.random.RandomState(0) |
| 882 |
ndim = 3 |
| 883 |
data = rstate.standard_normal((16, )*ndim) |
| 884 |
T = pywt.fswavedecn(data, 'haar', levels=(1, 2, 3)) |
| 885 |
rec = pywt.fswaverecn(T) |
| 886 |
assert_allclose(rec, data, atol=1e-14) |
| 887 |
|
| 888 |
|
| 889 |
assert_raises(ValueError, pywt.fswavedecn, data, 'haar', levels=(1, 1)) |
| 890 |
assert_raises(ValueError, pywt.fswavedecn, data, 'haar', levels=(1, 1, 1, 1)) |
| 891 |
|
| 892 |
|
| 893 |
assert_warns(UserWarning, pywt.fswavedecn, data, 'haar', |
| 894 |
levels=int(np.log2(np.min(data.shape)))+1) |
| 895 |
|
| 896 |
|
| 897 |
def test_fswavedecn_fswaverecn_variable_wavelets_and_modes(): |
| 898 |
|
| 899 |
rstate = np.random.RandomState(0) |
| 900 |
ndim = 3 |
| 901 |
data = rstate.standard_normal((16, )*ndim) |
| 902 |
wavelets = ('haar', 'db2', 'sym3') |
| 903 |
modes = ('periodic', 'symmetric', 'periodization') |
| 904 |
T = pywt.fswavedecn(data, wavelet=wavelets, mode=modes) |
| 905 |
for ax in range(ndim): |
| 906 |
|
| 907 |
assert_equal(len(T.coeff_slices[ax]), |
| 908 |
pywt.dwt_max_level(data.shape[ax], wavelets[ax])+1) |
| 909 |
|
| 910 |
rec = pywt.fswaverecn(T) |
| 911 |
assert_allclose(rec, data, atol=1e-14) |
| 912 |
|
| 913 |
|
| 914 |
assert_raises(ValueError, pywt.fswavedecn, data, wavelets[:2]) |
| 915 |
|
| 916 |
|
| 917 |
assert_raises(ValueError, pywt.fswavedecn, data, wavelets[0], mode=modes[:2]) |
| 918 |
|
| 919 |
|
| 920 |
def test_fswavedecn_fswaverecn_axes_subsets(): |
| 921 |
"""Fully separable DWT over only a subset of axes""" |
| 922 |
rstate = np.random.RandomState(0) |
| 923 |
|
| 924 |
data = rstate.standard_normal((4, 8, 16, 32)) |
| 925 |
|
| 926 |
for axes in combinations((0, 1, 2, 3), 3): |
| 927 |
T = pywt.fswavedecn(data, 'haar', axes=axes) |
| 928 |
rec = pywt.fswaverecn(T) |
| 929 |
assert_allclose(rec, data, atol=1e-14) |
| 930 |
|
| 931 |
|
| 932 |
assert_raises(ValueError, pywt.fswavedecn, data, 'haar', axes=(1, 5)) |
| 933 |
|
| 934 |
|
| 935 |
def test_fswavedecnresult(): |
| 936 |
data = np.ones((32, 32)) |
| 937 |
levels = (1, 2) |
| 938 |
result = pywt.fswavedecn(data, 'sym2', levels=levels) |
| 939 |
|
| 940 |
|
| 941 |
approx_key = (0, ) * data.ndim |
| 942 |
assert_array_equal(result[approx_key], result.approx) |
| 943 |
|
| 944 |
dkeys = result.detail_keys() |
| 945 |
|
| 946 |
assert_(approx_key not in dkeys) |
| 947 |
|
| 948 |
|
| 949 |
for k in dkeys: |
| 950 |
d = result[k] |
| 951 |
assert_equal(d.ndim, data.ndim) |
| 952 |
|
| 953 |
|
| 954 |
result[k] = np.zeros_like(d) |
| 955 |
|
| 956 |
|
| 957 |
assert_raises(ValueError, result.__setitem__, |
| 958 |
k, np.zeros(tuple([s + 1 for s in d.shape]))) |
| 959 |
|
| 960 |
|
| 961 |
assert_warns(UserWarning, result.__setitem__, |
| 962 |
k, np.zeros_like(d).astype(np.float32)) |
| 963 |
|
| 964 |
|
| 965 |
assert_equal(result.coeffs.ndim, data.ndim) |
| 966 |
|
| 967 |
|
| 968 |
def test_error_on_continuous_wavelet(): |
| 969 |
|
| 970 |
data = np.ones((16, 16)) |
| 971 |
for dec_fun, rec_fun in zip([pywt.wavedec, pywt.wavedec2, pywt.wavedecn], |
| 972 |
[pywt.waverec, pywt.waverec2, pywt.waverecn]): |
| 973 |
for cwave in ['morl', pywt.DiscreteContinuousWavelet('morl')]: |
| 974 |
assert_raises(ValueError, dec_fun, data, wavelet=cwave) |
| 975 |
|
| 976 |
c = dec_fun(data, 'db1') |
| 977 |
assert_raises(ValueError, rec_fun, c, wavelet=cwave) |
| 978 |
|
| 979 |
|
| 980 |
def test_default_level(): |
| 981 |
|
| 982 |
data = np.ones((128, 32, 4)) |
| 983 |
wavelet = ('db8', 'db1') |
| 984 |
for dec_func in [pywt.wavedec2, pywt.wavedecn]: |
| 985 |
for axes in [(0, 1), (2, 1), (0, 2)]: |
| 986 |
c = dec_func(data, wavelet, axes=axes) |
| 987 |
max_lev = np.min([pywt.dwt_max_level(data.shape[ax], wav) |
| 988 |
for ax, wav in zip(axes, wavelet)]) |
| 989 |
assert_equal(len(c[1:]), max_lev) |
| 990 |
|
| 991 |
for ax in [0, 1]: |
| 992 |
c = pywt.wavedecn(data, wavelet[ax], axes=(ax, )) |
| 993 |
assert_equal(len(c[1:]), |
| 994 |
pywt.dwt_max_level(data.shape[ax], wavelet[ax])) |
| 995 |
|
| 996 |
|
| 997 |
def test_waverec_mixed_precision(): |
| 998 |
rstate = np.random.RandomState(0) |
| 999 |
for func, ifunc, shape in [(pywt.wavedec, pywt.waverec, (8, )), |
| 1000 |
(pywt.wavedec2, pywt.waverec2, (8, 8)), |
| 1001 |
(pywt.wavedecn, pywt.waverecn, (8, 8, 8))]: |
| 1002 |
x = rstate.randn(*shape) |
| 1003 |
coeffs_real = func(x, 'db1') |
| 1004 |
|
| 1005 |
|
| 1006 |
coeffs_real[0] = coeffs_real[0].astype(np.float32) |
| 1007 |
r = ifunc(coeffs_real, 'db1') |
| 1008 |
assert_allclose(r, x, rtol=1e-7, atol=1e-7) |
| 1009 |
assert_equal(r.dtype, np.float64) |
| 1010 |
|
| 1011 |
x = x + 1j*x |
| 1012 |
coeffs = func(x, 'db1') |
| 1013 |
|
| 1014 |
|
| 1015 |
coeffs[0] = coeffs[0].astype(np.complex64) |
| 1016 |
r = ifunc(coeffs, 'db1') |
| 1017 |
assert_allclose(r, x, rtol=1e-7, atol=1e-7) |
| 1018 |
assert_equal(r.dtype, np.complex128) |
| 1019 |
|
| 1020 |
|
| 1021 |
if x.ndim == 1: |
| 1022 |
coeffs[0] = coeffs[0].astype(np.complex128) |
| 1023 |
coeffs[1] = coeffs[1].astype(np.complex64) |
| 1024 |
if x.ndim == 2: |
| 1025 |
coeffs[0] = coeffs[0].astype(np.complex128) |
| 1026 |
coeffs[1] = tuple([v.astype(np.complex64) for v in coeffs[1]]) |
| 1027 |
if x.ndim == 3: |
| 1028 |
coeffs[0] = coeffs[0].astype(np.complex128) |
| 1029 |
coeffs[1] = {k: v.astype(np.complex64) |
| 1030 |
for k, v in coeffs[1].items()} |
| 1031 |
r = ifunc(coeffs, 'db1') |
| 1032 |
assert_allclose(r, x, rtol=1e-7, atol=1e-7) |
| 1033 |
assert_equal(r.dtype, np.complex128) |