| 1 |
|
| 2 |
|
| 3 |
import numpy as np |
| 4 |
import pytest |
| 5 |
from numpy.testing import assert_allclose |
| 6 |
|
| 7 |
import pywt |
| 8 |
from pywt import data |
| 9 |
|
| 10 |
|
| 11 |
tol_single = 1e-6 |
| 12 |
tol_double = 1e-13 |
| 13 |
atol = 1e-7 |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
@pytest.mark.parametrize('wavelet', ['db2', 'sym4', 'coif5']) |
| 21 |
@pytest.mark.parametrize('transform', ['dwt', 'swt']) |
| 22 |
@pytest.mark.parametrize('mode', pywt.Modes.modes) |
| 23 |
@pytest.mark.parametrize( |
| 24 |
'dtype', ['float32', 'float64', 'complex64', 'complex128'] |
| 25 |
) |
| 26 |
def test_mra_roundtrip(wavelet, transform, mode, dtype): |
| 27 |
x = data.ecg()[:64].astype(dtype) |
| 28 |
if x.dtype.kind == 'c': |
| 29 |
|
| 30 |
x.imag = x[::-1].real |
| 31 |
|
| 32 |
if transform == 'swt': |
| 33 |
|
| 34 |
if mode != 'periodization': |
| 35 |
with pytest.raises(ValueError): |
| 36 |
pywt.mra(x, wavelet, transform=transform, mode=mode) |
| 37 |
return |
| 38 |
|
| 39 |
coeffs = pywt.mra(x, wavelet, transform=transform, mode=mode) |
| 40 |
assert isinstance(coeffs, list) |
| 41 |
assert isinstance(coeffs[0], np.ndarray) |
| 42 |
|
| 43 |
|
| 44 |
y = pywt.imra(coeffs) |
| 45 |
rtol = tol_single if x.real.dtype.kind == 'f' else tol_double |
| 46 |
assert_allclose(x, y, rtol=rtol, atol=rtol) |
| 47 |
|
| 48 |
|
| 49 |
@pytest.mark.parametrize('wavelet', ['rbio1.3', 'bior2.4']) |
| 50 |
@pytest.mark.parametrize('transform', ['dwt', 'swt']) |
| 51 |
def test_mra_warns_on_non_orthogonal(wavelet, transform): |
| 52 |
dtype = np.float64 |
| 53 |
x = data.ecg()[:64].astype(dtype) |
| 54 |
|
| 55 |
assert not pywt.Wavelet(wavelet).orthogonal |
| 56 |
|
| 57 |
if transform == 'swt': |
| 58 |
|
| 59 |
msg = 'norm=True, but the wavelet is not orthogonal' |
| 60 |
with pytest.warns(UserWarning, match=msg): |
| 61 |
coeffs = pywt.mra(x, wavelet, transform=transform) |
| 62 |
else: |
| 63 |
coeffs = pywt.mra(x, wavelet, transform=transform) |
| 64 |
|
| 65 |
y = pywt.imra(coeffs) |
| 66 |
rtol = tol_single if x.real.dtype.kind == 'f' else tol_double |
| 67 |
assert_allclose(x, y, rtol=rtol, atol=rtol) |
| 68 |
|
| 69 |
|
| 70 |
@pytest.mark.parametrize('axis', [0, -1, 1, 2, -3]) |
| 71 |
@pytest.mark.parametrize('ndim', [1, 2, 3]) |
| 72 |
@pytest.mark.parametrize('transform', ['dwt', 'swt']) |
| 73 |
@pytest.mark.parametrize('dtype', [np.float64, np.complex128]) |
| 74 |
def test_mra_axis(transform, ndim, axis, dtype): |
| 75 |
|
| 76 |
if ndim == 1: |
| 77 |
x = data.ecg()[:64] |
| 78 |
elif ndim == 2: |
| 79 |
x = data.camera()[:64, :32] |
| 80 |
elif ndim == 3: |
| 81 |
x = data.camera()[:48, :8] |
| 82 |
x = np.stack((x,) * 8, axis=-1) |
| 83 |
x = x.astype(dtype, copy=False) |
| 84 |
|
| 85 |
|
| 86 |
if axis < -x.ndim or axis >= x.ndim: |
| 87 |
with pytest.raises(np.AxisError): |
| 88 |
pywt.mra(x, 'db1', transform=transform, axis=axis) |
| 89 |
return |
| 90 |
|
| 91 |
coeffs = pywt.mra(x, 'db1', transform=transform, axis=axis) |
| 92 |
y = pywt.imra(coeffs) |
| 93 |
rtol = tol_single if x.real.dtype.kind == 'f' else tol_double |
| 94 |
assert_allclose(x, y, rtol=rtol, atol=rtol) |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
@pytest.mark.parametrize('wavelet', ['db2', 'sym4', 'coif5']) |
| 102 |
@pytest.mark.parametrize('transform', ['dwt2', 'swt2']) |
| 103 |
@pytest.mark.parametrize('mode', pywt.Modes.modes) |
| 104 |
@pytest.mark.parametrize( |
| 105 |
'dtype', ['float32', 'float64', 'complex64', 'complex128'] |
| 106 |
) |
| 107 |
def test_mra2_roundtrip(wavelet, transform, mode, dtype): |
| 108 |
x = data.camera()[:32, :16].astype(dtype, copy=False) |
| 109 |
if x.dtype.kind == 'c': |
| 110 |
|
| 111 |
x.imag = x[::-1, :].real |
| 112 |
|
| 113 |
if transform == 'swt2': |
| 114 |
|
| 115 |
if mode != 'periodization': |
| 116 |
with pytest.raises(ValueError): |
| 117 |
pywt.mra2(x, wavelet, transform=transform, mode=mode) |
| 118 |
return |
| 119 |
|
| 120 |
coeffs = pywt.mra2(x, wavelet, transform=transform, mode=mode) |
| 121 |
assert isinstance(coeffs, list) |
| 122 |
assert isinstance(coeffs[0], np.ndarray) |
| 123 |
|
| 124 |
|
| 125 |
y = pywt.imra2(coeffs) |
| 126 |
rtol = tol_single if x.real.dtype.kind == 'f' else tol_double |
| 127 |
assert_allclose(x, y, rtol=rtol, atol=rtol) |
| 128 |
|
| 129 |
|
| 130 |
@pytest.mark.parametrize('wavelet', ['rbio1.3', 'bior2.4']) |
| 131 |
@pytest.mark.parametrize('transform', ['dwt2', 'swt2']) |
| 132 |
def test_mra2_warns_on_non_orthogonal(wavelet, transform): |
| 133 |
dtype = np.float64 |
| 134 |
x = data.camera()[:32, :8].astype(dtype, copy=False) |
| 135 |
|
| 136 |
assert not pywt.Wavelet(wavelet).orthogonal |
| 137 |
|
| 138 |
if transform == 'swt2': |
| 139 |
|
| 140 |
msg = 'norm=True, but the wavelets used are not orthogonal' |
| 141 |
with pytest.warns(UserWarning, match=msg): |
| 142 |
coeffs = pywt.mra2(x, wavelet, transform=transform) |
| 143 |
else: |
| 144 |
coeffs = pywt.mra2(x, wavelet, transform=transform) |
| 145 |
|
| 146 |
y = pywt.imra2(coeffs) |
| 147 |
rtol = tol_single if x.real.dtype.kind == 'f' else tol_double |
| 148 |
assert_allclose(x, y, rtol=rtol, atol=rtol) |
| 149 |
|
| 150 |
|
| 151 |
@pytest.mark.parametrize('transform', ['dwt2', 'swt2']) |
| 152 |
@pytest.mark.parametrize('ndim', [2, 3]) |
| 153 |
@pytest.mark.parametrize('axes', [(0, 1), (-2, -1), (0, 2), (-3, 1), (0, 4)]) |
| 154 |
@pytest.mark.parametrize('dtype', [np.float64, np.complex128]) |
| 155 |
def test_mra2_axes(transform, axes, ndim, dtype): |
| 156 |
|
| 157 |
x = data.camera()[:32, :16].astype(dtype, copy=False) |
| 158 |
if ndim == 3: |
| 159 |
x = np.stack((x,) * 8, axis=-1) |
| 160 |
|
| 161 |
|
| 162 |
if any([axis < -x.ndim or axis >= x.ndim for axis in axes]): |
| 163 |
with pytest.raises(np.AxisError): |
| 164 |
pywt.mra2(x, 'db1', transform=transform, axes=axes) |
| 165 |
return |
| 166 |
|
| 167 |
coeffs = pywt.mra2(x, 'db1', transform=transform, axes=axes) |
| 168 |
y = pywt.imra2(coeffs) |
| 169 |
rtol = tol_single if x.real.dtype.kind == 'f' else tol_double |
| 170 |
assert_allclose(x, y, rtol=rtol, atol=rtol) |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
@pytest.mark.parametrize('wavelet', ['sym2', ]) |
| 178 |
@pytest.mark.parametrize('transform', ['dwtn', 'swtn']) |
| 179 |
@pytest.mark.parametrize('mode', pywt.Modes.modes) |
| 180 |
@pytest.mark.parametrize( |
| 181 |
'dtype', ['float32', 'float64', 'complex64', 'complex128'] |
| 182 |
) |
| 183 |
@pytest.mark.parametrize('ndim', [1, 2, 3]) |
| 184 |
def test_mran_roundtrip(wavelet, transform, mode, dtype, ndim): |
| 185 |
if ndim == 1: |
| 186 |
x = data.ecg()[:48].astype(dtype, copy=False) |
| 187 |
elif ndim == 2: |
| 188 |
x = data.camera()[:16, :8].astype(dtype, copy=False) |
| 189 |
elif ndim == 3: |
| 190 |
x = data.camera()[:16, :8].astype(dtype, copy=False) |
| 191 |
x = np.stack((x,) * 8, axis=-1) |
| 192 |
|
| 193 |
if x.dtype.kind == 'c': |
| 194 |
|
| 195 |
x.imag = x[::-1, ...].real |
| 196 |
|
| 197 |
if transform == 'swtn': |
| 198 |
|
| 199 |
if mode != 'periodization': |
| 200 |
with pytest.raises(ValueError): |
| 201 |
pywt.mran(x, wavelet, transform=transform, mode=mode) |
| 202 |
return |
| 203 |
|
| 204 |
coeffs = pywt.mran(x, wavelet, transform=transform, mode=mode) |
| 205 |
assert isinstance(coeffs, list) |
| 206 |
assert isinstance(coeffs[0], np.ndarray) |
| 207 |
|
| 208 |
|
| 209 |
y = pywt.imran(coeffs) |
| 210 |
rtol = tol_single if x.real.dtype.kind == 'f' else tol_double |
| 211 |
assert_allclose(x, y, rtol=rtol, atol=rtol) |
| 212 |
|
| 213 |
|
| 214 |
@pytest.mark.parametrize('wavelet', ['rbio1.3', 'bior2.4']) |
| 215 |
@pytest.mark.parametrize('transform', ['dwtn', 'swtn']) |
| 216 |
def test_mran_warns_on_non_orthogonal(wavelet, transform): |
| 217 |
dtype = np.float64 |
| 218 |
x = data.camera()[:32, :8].astype(dtype, copy=False) |
| 219 |
|
| 220 |
assert not pywt.Wavelet(wavelet).orthogonal |
| 221 |
|
| 222 |
if transform == 'swtn': |
| 223 |
|
| 224 |
msg = 'norm=True, but the wavelets used are not orthogonal' |
| 225 |
with pytest.warns(UserWarning, match=msg): |
| 226 |
coeffs = pywt.mran(x, wavelet, transform=transform) |
| 227 |
else: |
| 228 |
coeffs = pywt.mran(x, wavelet, transform=transform) |
| 229 |
|
| 230 |
y = pywt.imran(coeffs) |
| 231 |
rtol = tol_single if x.real.dtype.kind == 'f' else tol_double |
| 232 |
assert_allclose(x, y, rtol=rtol, atol=rtol) |
| 233 |
|
| 234 |
|
| 235 |
@pytest.mark.parametrize( |
| 236 |
'axes', [(0, 1), (-2, -1), (0, 2), (-3, 1), (0, 4), (-3, -2, -1), |
| 237 |
(0, 2, 1), (0, 5, 1), (0,), (1,), (2,), (-2,), (-3,), (-4,)]) |
| 238 |
@pytest.mark.parametrize('transform', ['dwtn', 'swtn']) |
| 239 |
def test_mran_axes(axes, transform): |
| 240 |
|
| 241 |
|
| 242 |
dtype = np.float64 |
| 243 |
x = data.camera()[:32, :16].astype(dtype, copy=False) |
| 244 |
x3d = np.stack((x,) * 8, axis=-1) |
| 245 |
|
| 246 |
|
| 247 |
if any([axis < -x.ndim or axis >= x.ndim for axis in axes]): |
| 248 |
with pytest.raises(np.AxisError): |
| 249 |
pywt.mran(x, 'db1', transform='dwtn', axes=axes) |
| 250 |
return |
| 251 |
|
| 252 |
coeffs = pywt.mran(x3d, 'db1', transform='dwtn', axes=axes) |
| 253 |
y = pywt.imran(coeffs) |
| 254 |
rtol = tol_single if x3d.real.dtype.kind == 'f' else tol_double |
| 255 |
assert_allclose(x3d, y, rtol=rtol, atol=rtol) |