| 1 |
from __future__ import division, print_function, absolute_import |
| 2 |
import numpy as np |
| 3 |
from numpy.testing import assert_allclose, assert_raises, assert_, assert_equal |
| 4 |
|
| 5 |
import pywt |
| 6 |
|
| 7 |
|
| 8 |
float_dtypes = [np.float32, np.float64, np.complex64, np.complex128] |
| 9 |
real_dtypes = [np.float32, np.float64] |
| 10 |
|
| 11 |
|
| 12 |
def _sign(x): |
| 13 |
|
| 14 |
return x / np.abs(x) |
| 15 |
|
| 16 |
|
| 17 |
def _soft(x, thresh): |
| 18 |
"""soft thresholding supporting complex values. |
| 19 |
|
| 20 |
Notes |
| 21 |
----- |
| 22 |
This version is not robust to zeros in x. |
| 23 |
""" |
| 24 |
return _sign(x) * np.maximum(np.abs(x) - thresh, 0) |
| 25 |
|
| 26 |
|
| 27 |
def test_threshold(): |
| 28 |
data = np.linspace(1, 4, 7) |
| 29 |
|
| 30 |
|
| 31 |
soft_result = [0., 0., 0., 0.5, 1., 1.5, 2.] |
| 32 |
assert_allclose(pywt.threshold(data, 2, 'soft'), |
| 33 |
np.array(soft_result), rtol=1e-12) |
| 34 |
assert_allclose(pywt.threshold(-data, 2, 'soft'), |
| 35 |
-np.array(soft_result), rtol=1e-12) |
| 36 |
assert_allclose(pywt.threshold([[1, 2]] * 2, 1, 'soft'), |
| 37 |
[[0, 1]] * 2, rtol=1e-12) |
| 38 |
assert_allclose(pywt.threshold([[1, 2]] * 2, 2, 'soft'), |
| 39 |
[[0, 0]] * 2, rtol=1e-12) |
| 40 |
|
| 41 |
|
| 42 |
assert_allclose(pywt.threshold([[1j, 2j]] * 2, 1, 'soft'), |
| 43 |
[[0j, 1j]] * 2, rtol=1e-12) |
| 44 |
assert_allclose(pywt.threshold([[1+1j, 2+2j]] * 2, 6, 'soft'), |
| 45 |
[[0, 0]] * 2, rtol=1e-12) |
| 46 |
complex_data = [[1+2j, 2+2j]]*2 |
| 47 |
for thresh in [1, 2]: |
| 48 |
assert_allclose(pywt.threshold(complex_data, thresh, 'soft'), |
| 49 |
_soft(complex_data, thresh), rtol=1e-12) |
| 50 |
|
| 51 |
|
| 52 |
s = 5 |
| 53 |
assert_allclose(pywt.threshold([[1j, 2]] * 2, 1.5, 'soft', substitute=s), |
| 54 |
[[s, 0.5]] * 2, rtol=1e-12) |
| 55 |
|
| 56 |
|
| 57 |
assert_allclose(pywt.threshold(np.zeros(16), 2, 'soft'), |
| 58 |
np.zeros(16), rtol=1e-12) |
| 59 |
|
| 60 |
|
| 61 |
hard_result = [0., 0., 2., 2.5, 3., 3.5, 4.] |
| 62 |
assert_allclose(pywt.threshold(data, 2, 'hard'), |
| 63 |
np.array(hard_result), rtol=1e-12) |
| 64 |
assert_allclose(pywt.threshold(-data, 2, 'hard'), |
| 65 |
-np.array(hard_result), rtol=1e-12) |
| 66 |
assert_allclose(pywt.threshold([[1, 2]] * 2, 1, 'hard'), |
| 67 |
[[1, 2]] * 2, rtol=1e-12) |
| 68 |
assert_allclose(pywt.threshold([[1, 2]] * 2, 2, 'hard'), |
| 69 |
[[0, 2]] * 2, rtol=1e-12) |
| 70 |
assert_allclose(pywt.threshold([[1, 2]] * 2, 2, 'hard', substitute=s), |
| 71 |
[[s, 2]] * 2, rtol=1e-12) |
| 72 |
assert_allclose(pywt.threshold([[1+1j, 2+2j]] * 2, 2, 'hard'), |
| 73 |
[[0, 2+2j]] * 2, rtol=1e-12) |
| 74 |
|
| 75 |
|
| 76 |
greater_result = [0., 0., 2., 2.5, 3., 3.5, 4.] |
| 77 |
assert_allclose(pywt.threshold(data, 2, 'greater'), |
| 78 |
np.array(greater_result), rtol=1e-12) |
| 79 |
assert_allclose(pywt.threshold([[1, 2]] * 2, 1, 'greater'), |
| 80 |
[[1, 2]] * 2, rtol=1e-12) |
| 81 |
assert_allclose(pywt.threshold([[1, 2]] * 2, 2, 'greater'), |
| 82 |
[[0, 2]] * 2, rtol=1e-12) |
| 83 |
assert_allclose(pywt.threshold([[1, 2]] * 2, 2, 'greater', substitute=s), |
| 84 |
[[s, 2]] * 2, rtol=1e-12) |
| 85 |
|
| 86 |
assert_raises(ValueError, pywt.threshold, [1j, 2j], 2, 'greater') |
| 87 |
|
| 88 |
|
| 89 |
assert_allclose(pywt.threshold(data, 2, 'less'), |
| 90 |
np.array([1., 1.5, 2., 0., 0., 0., 0.]), rtol=1e-12) |
| 91 |
assert_allclose(pywt.threshold([[1, 2]] * 2, 1, 'less'), |
| 92 |
[[1, 0]] * 2, rtol=1e-12) |
| 93 |
assert_allclose(pywt.threshold([[1, 2]] * 2, 1, 'less', substitute=s), |
| 94 |
[[1, s]] * 2, rtol=1e-12) |
| 95 |
assert_allclose(pywt.threshold([[1, 2]] * 2, 2, 'less'), |
| 96 |
[[1, 2]] * 2, rtol=1e-12) |
| 97 |
|
| 98 |
|
| 99 |
assert_raises(ValueError, pywt.threshold, [1j, 2j], 2, 'less') |
| 100 |
|
| 101 |
|
| 102 |
assert_raises(ValueError, pywt.threshold, data, 2, 'foo') |
| 103 |
|
| 104 |
|
| 105 |
def test_nonnegative_garotte(): |
| 106 |
thresh = 0.3 |
| 107 |
data_real = np.linspace(-1, 1, 100) |
| 108 |
for dtype in float_dtypes: |
| 109 |
if dtype in real_dtypes: |
| 110 |
data = np.asarray(data_real, dtype=dtype) |
| 111 |
else: |
| 112 |
data = np.asarray(data_real + 0.1j, dtype=dtype) |
| 113 |
d_hard = pywt.threshold(data, thresh, 'hard') |
| 114 |
d_soft = pywt.threshold(data, thresh, 'soft') |
| 115 |
d_garotte = pywt.threshold(data, thresh, 'garotte') |
| 116 |
|
| 117 |
|
| 118 |
assert_equal(d_hard.dtype, data.dtype) |
| 119 |
assert_equal(d_soft.dtype, data.dtype) |
| 120 |
assert_equal(d_garotte.dtype, data.dtype) |
| 121 |
|
| 122 |
|
| 123 |
lt = np.where(np.abs(data) < thresh) |
| 124 |
assert_(np.all(d_garotte[lt] == 0)) |
| 125 |
|
| 126 |
|
| 127 |
gt = np.where(np.abs(data) > thresh) |
| 128 |
gt_abs_garotte = np.abs(d_garotte[gt]) |
| 129 |
assert_(np.all(gt_abs_garotte < np.abs(d_hard[gt]))) |
| 130 |
assert_(np.all(gt_abs_garotte > np.abs(d_soft[gt]))) |
| 131 |
|
| 132 |
|
| 133 |
def test_threshold_firm(): |
| 134 |
thresh = 0.2 |
| 135 |
thresh2 = 3 * thresh |
| 136 |
data_real = np.linspace(-1, 1, 100) |
| 137 |
for dtype in float_dtypes: |
| 138 |
if dtype in real_dtypes: |
| 139 |
data = np.asarray(data_real, dtype=dtype) |
| 140 |
else: |
| 141 |
data = np.asarray(data_real + 0.1j, dtype=dtype) |
| 142 |
if data.real.dtype == np.float32: |
| 143 |
rtol = atol = 1e-6 |
| 144 |
else: |
| 145 |
rtol = atol = 1e-14 |
| 146 |
d_hard = pywt.threshold(data, thresh, 'hard') |
| 147 |
d_soft = pywt.threshold(data, thresh, 'soft') |
| 148 |
d_firm = pywt.threshold_firm(data, thresh, thresh2) |
| 149 |
|
| 150 |
|
| 151 |
assert_equal(d_hard.dtype, data.dtype) |
| 152 |
assert_equal(d_soft.dtype, data.dtype) |
| 153 |
assert_equal(d_firm.dtype, data.dtype) |
| 154 |
|
| 155 |
|
| 156 |
lt = np.where(np.abs(data) < thresh) |
| 157 |
assert_(np.all(d_firm[lt] == 0)) |
| 158 |
|
| 159 |
|
| 160 |
gt = np.where(np.abs(data) >= thresh2) |
| 161 |
assert_allclose(np.abs(d_hard[gt]), np.abs(d_firm[gt]), |
| 162 |
rtol=rtol, atol=atol) |
| 163 |
|
| 164 |
|
| 165 |
mt = np.where(np.logical_and(np.abs(data) > thresh, |
| 166 |
np.abs(data) < thresh2)) |
| 167 |
mt_abs_firm = np.abs(d_firm[mt]) |
| 168 |
assert_(np.all(mt_abs_firm < np.abs(d_hard[mt]))) |
| 169 |
assert_(np.all(mt_abs_firm > np.abs(d_soft[mt]))) |