Debian Codegraph @

pywt/tests/test_concurrent.py

raw symbols SCIP
1 """
2 Tests used to verify running PyWavelets transforms in parallel via
3 concurrent.futures.ThreadPoolExecutor does not raise errors.
4 """
5
6 from __future__ import division, print_function, absolute_import
7
8 import warnings
9 import numpy as np
10 from functools import partial
11 from numpy.testing import assert_array_equal, assert_allclose
12 from pywt._pytest import uses_futures, futures, max_workers
13
14 import pywt
15
16
17 def _assert_all_coeffs_equal(coefs1, coefs2):
18 # return True only if all coefficients of SWT or DWT match over all levels
19 if len(coefs1) != len(coefs2):
20 return False
21 for (c1, c2) in zip(coefs1, coefs2):
22 if isinstance(c1, tuple):
23 # for swt, swt2, dwt, dwt2, wavedec, wavedec2
24 for a1, a2 in zip(c1, c2):
25 assert_array_equal(a1, a2)
26 elif isinstance(c1, dict):
27 # for swtn, dwtn, wavedecn
28 for k, v in c1.items():
29 assert_array_equal(v, c2[k])
30 else:
31 return False
32 return True
33
34
35 @uses_futures
36 def test_concurrent_swt():
37 # tests error-free concurrent operation (see gh-288)
38 # swt on 1D data calls the Cython swt
39 # other cases call swt_axes
40 with warnings.catch_warnings():
41 # can remove catch_warnings once the swt2 FutureWarning is removed
42 warnings.simplefilter('ignore', FutureWarning)
43 for swt_func, x in zip([pywt.swt, pywt.swt2, pywt.swtn],
44 [np.ones(8), np.eye(16), np.eye(16)]):
45 transform = partial(swt_func, wavelet='haar', level=3)
46 for _ in range(10):
47 arrs = [x.copy() for _ in range(100)]
48 with futures.ThreadPoolExecutor(max_workers=max_workers) as ex:
49 results = list(ex.map(transform, arrs))
50
51 # validate result from one of the concurrent runs
52 expected_result = transform(x)
53 _assert_all_coeffs_equal(expected_result, results[-1])
54
55
56 @uses_futures
57 def test_concurrent_wavedec():
58 # wavedec on 1D data calls the Cython dwt_single
59 # other cases call dwt_axis
60 for wavedec_func, x in zip([pywt.wavedec, pywt.wavedec2, pywt.wavedecn],
61 [np.ones(8), np.eye(16), np.eye(16)]):
62 transform = partial(wavedec_func, wavelet='haar', level=1)
63 for _ in range(10):
64 arrs = [x.copy() for _ in range(100)]
65 with futures.ThreadPoolExecutor(max_workers=max_workers) as ex:
66 results = list(ex.map(transform, arrs))
67
68 # validate result from one of the concurrent runs
69 expected_result = transform(x)
70 _assert_all_coeffs_equal(expected_result, results[-1])
71
72
73 @uses_futures
74 def test_concurrent_dwt():
75 # dwt on 1D data calls the Cython dwt_single
76 # other cases call dwt_axis
77 for dwt_func, x in zip([pywt.dwt, pywt.dwt2, pywt.dwtn],
78 [np.ones(8), np.eye(16), np.eye(16)]):
79 transform = partial(dwt_func, wavelet='haar')
80 for _ in range(10):
81 arrs = [x.copy() for _ in range(100)]
82 with futures.ThreadPoolExecutor(max_workers=max_workers) as ex:
83 results = list(ex.map(transform, arrs))
84
85 # validate result from one of the concurrent runs
86 expected_result = transform(x)
87 _assert_all_coeffs_equal([expected_result, ], [results[-1], ])
88
89
90 @uses_futures
91 def test_concurrent_cwt():
92 atol = rtol = 1e-14
93 time, sst = pywt.data.nino()
94 dt = time[1]-time[0]
95 transform = partial(pywt.cwt, scales=np.arange(1, 4), wavelet='cmor1.5-1',
96 sampling_period=dt)
97 for _ in range(10):
98 arrs = [sst.copy() for _ in range(50)]
99 with futures.ThreadPoolExecutor(max_workers=max_workers) as ex:
100 results = list(ex.map(transform, arrs))
101
102 # validate result from one of the concurrent runs
103 expected_result = transform(sst)
104 for a1, a2 in zip(expected_result, results[-1]):
105 assert_allclose(a1, a2, atol=atol, rtol=rtol)