File: test_mra.py

package info (click to toggle)
pywavelets 1.4.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 13,680 kB
  • sloc: python: 8,849; ansic: 5,134; makefile: 93
file content (255 lines) | stat: -rw-r--r-- 8,928 bytes parent folder | download
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
#!/usr/bin/env python

import numpy as np
import pytest
from numpy.testing import assert_allclose

import pywt
from pywt import data

# tolerances used in accuracy comparisons
tol_single = 1e-6
tol_double = 1e-13
atol = 1e-7


####
# 1d mra tests
####

@pytest.mark.parametrize('wavelet', ['db2', 'sym4', 'coif5'])
@pytest.mark.parametrize('transform', ['dwt', 'swt'])
@pytest.mark.parametrize('mode', pywt.Modes.modes)
@pytest.mark.parametrize(
    'dtype', ['float32', 'float64', 'complex64', 'complex128']
)
def test_mra_roundtrip(wavelet, transform, mode, dtype):
    x = data.ecg()[:64].astype(dtype)
    if x.dtype.kind == 'c':
        # fill some data for the imaginary channel
        x.imag = x[::-1].real

    if transform == 'swt':
        # swt mode only supports periodization
        if mode != 'periodization':
            with pytest.raises(ValueError):
                pywt.mra(x, wavelet, transform=transform, mode=mode)
            return

    coeffs = pywt.mra(x, wavelet, transform=transform, mode=mode)
    assert isinstance(coeffs, list)
    assert isinstance(coeffs[0], np.ndarray)
    # assert all(isinstance(coeffs[i], dict) for i in range(1, len(coeffs)))

    y = pywt.imra(coeffs)
    rtol = tol_single if x.real.dtype.kind == 'f' else tol_double
    assert_allclose(x, y, rtol=rtol, atol=rtol)


@pytest.mark.parametrize('wavelet', ['rbio1.3', 'bior2.4'])
@pytest.mark.parametrize('transform', ['dwt', 'swt'])
def test_mra_warns_on_non_orthogonal(wavelet, transform):
    dtype = np.float64
    x = data.ecg()[:64].astype(dtype)

    assert not pywt.Wavelet(wavelet).orthogonal

    if transform == 'swt':
        # bi-orthogonal wavelets raise a warning for SWT case
        msg = 'norm=True, but the wavelet is not orthogonal'
        with pytest.warns(UserWarning, match=msg):
            coeffs = pywt.mra(x, wavelet, transform=transform)
    else:
        coeffs = pywt.mra(x, wavelet, transform=transform)

    y = pywt.imra(coeffs)
    rtol = tol_single if x.real.dtype.kind == 'f' else tol_double
    assert_allclose(x, y, rtol=rtol, atol=rtol)


@pytest.mark.parametrize('axis', [0, -1, 1, 2, -3])
@pytest.mark.parametrize('ndim', [1, 2, 3])
@pytest.mark.parametrize('transform', ['dwt', 'swt'])
@pytest.mark.parametrize('dtype', [np.float64, np.complex128])
def test_mra_axis(transform, ndim, axis, dtype):
    # Test transforms over a specific axis of 1D, 2D or 3D data
    if ndim == 1:
        x = data.ecg()[:64]
    elif ndim == 2:
        x = data.camera()[:64, :32]
    elif ndim == 3:
        x = data.camera()[:48, :8]
        x = np.stack((x,) * 8, axis=-1)
    x = x.astype(dtype, copy=False)

    # out of range axis
    if axis < -x.ndim or axis >= x.ndim:
        with pytest.raises(np.AxisError):
            pywt.mra(x, 'db1', transform=transform, axis=axis)
        return

    coeffs = pywt.mra(x, 'db1', transform=transform, axis=axis)
    y = pywt.imra(coeffs)
    rtol = tol_single if x.real.dtype.kind == 'f' else tol_double
    assert_allclose(x, y, rtol=rtol, atol=rtol)


####
# 2d mra tests
####

@pytest.mark.parametrize('wavelet', ['db2', 'sym4', 'coif5'])
@pytest.mark.parametrize('transform', ['dwt2', 'swt2'])
@pytest.mark.parametrize('mode', pywt.Modes.modes)
@pytest.mark.parametrize(
    'dtype', ['float32', 'float64', 'complex64', 'complex128']
)
def test_mra2_roundtrip(wavelet, transform, mode, dtype):
    x = data.camera()[:32, :16].astype(dtype, copy=False)
    if x.dtype.kind == 'c':
        # fill some data for the imaginary channel
        x.imag = x[::-1, :].real

    if transform == 'swt2':
        # swt mode only supports periodization
        if mode != 'periodization':
            with pytest.raises(ValueError):
                pywt.mra2(x, wavelet, transform=transform, mode=mode)
            return

    coeffs = pywt.mra2(x, wavelet, transform=transform, mode=mode)
    assert isinstance(coeffs, list)
    assert isinstance(coeffs[0], np.ndarray)
    # assert all(isinstance(coeffs[i], dict) for i in range(1, len(coeffs)))

    y = pywt.imra2(coeffs)
    rtol = tol_single if x.real.dtype.kind == 'f' else tol_double
    assert_allclose(x, y, rtol=rtol, atol=rtol)


@pytest.mark.parametrize('wavelet', ['rbio1.3', 'bior2.4'])
@pytest.mark.parametrize('transform', ['dwt2', 'swt2'])
def test_mra2_warns_on_non_orthogonal(wavelet, transform):
    dtype = np.float64
    x = data.camera()[:32, :8].astype(dtype, copy=False)

    assert not pywt.Wavelet(wavelet).orthogonal

    if transform == 'swt2':
        # bi-orthogonal wavelets raise a warning for SWT case
        msg = 'norm=True, but the wavelets used are not orthogonal'
        with pytest.warns(UserWarning, match=msg):
            coeffs = pywt.mra2(x, wavelet, transform=transform)
    else:
        coeffs = pywt.mra2(x, wavelet, transform=transform)

    y = pywt.imra2(coeffs)
    rtol = tol_single if x.real.dtype.kind == 'f' else tol_double
    assert_allclose(x, y, rtol=rtol, atol=rtol)


@pytest.mark.parametrize('transform', ['dwt2', 'swt2'])
@pytest.mark.parametrize('ndim', [2, 3])
@pytest.mark.parametrize('axes', [(0, 1), (-2, -1), (0, 2), (-3, 1), (0, 4)])
@pytest.mark.parametrize('dtype', [np.float64, np.complex128])
def test_mra2_axes(transform, axes, ndim, dtype):
    # Test transforms over various axes of 2D or 3D data.
    x = data.camera()[:32, :16].astype(dtype, copy=False)
    if ndim == 3:
        x = np.stack((x,) * 8, axis=-1)

    # out of range axis
    if any([axis < -x.ndim or axis >= x.ndim for axis in axes]):
        with pytest.raises(np.AxisError):
            pywt.mra2(x, 'db1', transform=transform, axes=axes)
        return

    coeffs = pywt.mra2(x, 'db1', transform=transform, axes=axes)
    y = pywt.imra2(coeffs)
    rtol = tol_single if x.real.dtype.kind == 'f' else tol_double
    assert_allclose(x, y, rtol=rtol, atol=rtol)


####
# nd mra tests
####

@pytest.mark.parametrize('wavelet', ['sym2', ])
@pytest.mark.parametrize('transform', ['dwtn', 'swtn'])
@pytest.mark.parametrize('mode', pywt.Modes.modes)
@pytest.mark.parametrize(
    'dtype', ['float32', 'float64', 'complex64', 'complex128']
)
@pytest.mark.parametrize('ndim', [1, 2, 3])
def test_mran_roundtrip(wavelet, transform, mode, dtype, ndim):
    if ndim == 1:
        x = data.ecg()[:48].astype(dtype, copy=False)
    elif ndim == 2:
        x = data.camera()[:16, :8].astype(dtype, copy=False)
    elif ndim == 3:
        x = data.camera()[:16, :8].astype(dtype, copy=False)
        x = np.stack((x,) * 8, axis=-1)

    if x.dtype.kind == 'c':
        # fill some data for the imaginary channel
        x.imag = x[::-1, ...].real

    if transform == 'swtn':
        # swt mode only supports periodization
        if mode != 'periodization':
            with pytest.raises(ValueError):
                pywt.mran(x, wavelet, transform=transform, mode=mode)
            return

    coeffs = pywt.mran(x, wavelet, transform=transform, mode=mode)
    assert isinstance(coeffs, list)
    assert isinstance(coeffs[0], np.ndarray)
    # assert all(isinstance(coeffs[i], dict) for i in range(1, len(coeffs)))

    y = pywt.imran(coeffs)
    rtol = tol_single if x.real.dtype.kind == 'f' else tol_double
    assert_allclose(x, y, rtol=rtol, atol=rtol)


@pytest.mark.parametrize('wavelet', ['rbio1.3', 'bior2.4'])
@pytest.mark.parametrize('transform', ['dwtn', 'swtn'])
def test_mran_warns_on_non_orthogonal(wavelet, transform):
    dtype = np.float64
    x = data.camera()[:32, :8].astype(dtype, copy=False)

    assert not pywt.Wavelet(wavelet).orthogonal

    if transform == 'swtn':
        # bi-orthogonal wavelets raise a warning for SWT case
        msg = 'norm=True, but the wavelets used are not orthogonal'
        with pytest.warns(UserWarning, match=msg):
            coeffs = pywt.mran(x, wavelet, transform=transform)
    else:
        coeffs = pywt.mran(x, wavelet, transform=transform)

    y = pywt.imran(coeffs)
    rtol = tol_single if x.real.dtype.kind == 'f' else tol_double
    assert_allclose(x, y, rtol=rtol, atol=rtol)


@pytest.mark.parametrize(
    'axes', [(0, 1), (-2, -1), (0, 2), (-3, 1), (0, 4), (-3, -2, -1),
             (0, 2, 1), (0, 5, 1), (0,), (1,), (2,), (-2,),  (-3,), (-4,)])
@pytest.mark.parametrize('transform', ['dwtn', 'swtn'])
def test_mran_axes(axes, transform):
    # Test with transforms over 1, 2 or 3 axes of 3d data.
    # Cases with out of range axes are also tested
    dtype = np.float64
    x = data.camera()[:32, :16].astype(dtype, copy=False)
    x3d = np.stack((x,) * 8, axis=-1)

    # out of range axis
    if any([axis < -x.ndim or axis >= x.ndim for axis in axes]):
        with pytest.raises(np.AxisError):
            pywt.mran(x, 'db1', transform='dwtn', axes=axes)
        return

    coeffs = pywt.mran(x3d, 'db1', transform='dwtn', axes=axes)
    y = pywt.imran(coeffs)
    rtol = tol_single if x3d.real.dtype.kind == 'f' else tol_double
    assert_allclose(x3d, y, rtol=rtol, atol=rtol)