Debian Codegraph @

pywt/tests/data/generate_matlab_data_cwt.py

raw symbols SCIP
1 """ This script was used to generate dwt_matlabR2012a_result.npz by storing
2 the outputs from Matlab R2012a. """
3
4 from __future__ import division, print_function, absolute_import
5
6 import numpy as np
7 import pywt
8
9 try:
10 from pymatbridge import Matlab
11 mlab = Matlab()
12 _matlab_missing = False
13 except ImportError:
14 print("To run Matlab compatibility tests you need to have MathWorks "
15 "MATLAB, MathWorks Wavelet Toolbox and the pymatbridge Python "
16 "package installed.")
17 _matlab_missing = True
18
19 if _matlab_missing:
20 raise EnvironmentError("Can't generate matlab data files without MATLAB")
21
22 size_set = 'reduced'
23
24 # list of mode names in pywt and matlab
25 modes = [('zero', 'zpd'),
26 ('constant', 'sp0'),
27 ('symmetric', 'sym'),
28 ('periodic', 'ppd'),
29 ('smooth', 'sp1'),
30 ('periodization', 'per')]
31
32 families = ('gaus', 'mexh', 'morl', 'cgau', 'shan', 'fbsp', 'cmor')
33 wavelets = sum([pywt.wavelist(name) for name in families], [])
34
35 rstate = np.random.RandomState(1234)
36 mlab.start()
37 try:
38 all_matlab_results = {}
39 for wavelet in wavelets:
40 w = pywt.ContinuousWavelet(wavelet)
41 if np.any((wavelet == np.array(['shan', 'cmor'])),axis=0):
42 mlab.set_variable('wavelet', wavelet+str(w.bandwidth_frequency)+'-'+str(w.center_frequency))
43 elif wavelet == 'fbsp':
44 mlab.set_variable('wavelet', wavelet+str(w.fbsp_order)+'-'+str(w.bandwidth_frequency)+'-'+str(w.center_frequency))
45 else:
46 mlab.set_variable('wavelet', wavelet)
47 if size_set == 'full':
48 data_sizes = list(range(100, 101)) + \
49 [100, 200, 500, 1000, 50000]
50 Scales = (1,np.arange(1,3),np.arange(1,4),np.arange(1,5))
51 else:
52 data_sizes = (1000, 1000 + 1)
53 Scales = (1,np.arange(1,3))
54 mlab_code = ("psi = wavefun(wavelet,10)")
55 res = mlab.run_code(mlab_code)
56 if not res['success']:
57 raise RuntimeError(
58 "Matlab failed to execute the provided code. "
59 "Check that the wavelet toolbox is installed.")
60 psi = np.asarray(mlab.get_variable('psi'))
61 psi_key = '_'.join([wavelet, 'psi'])
62 all_matlab_results[psi_key] = psi
63 for N in data_sizes:
64 data = rstate.randn(N)
65 mlab.set_variable('data', data)
66
67 # Matlab result
68 scale_count = 0
69 for scales in Scales:
70 scale_count += 1
71 mlab.set_variable('scales', scales)
72 mlab_code = ("coefs = cwt(data, scales, wavelet)")
73 res = mlab.run_code(mlab_code)
74 if not res['success']:
75 raise RuntimeError(
76 "Matlab failed to execute the provided code. "
77 "Check that the wavelet toolbox is installed.")
78 # need np.asarray because sometimes the output is type float
79 coefs = np.asarray(mlab.get_variable('coefs'))
80 coefs_key = '_'.join([str(scale_count), wavelet, str(N), 'coefs'])
81 all_matlab_results[coefs_key] = coefs
82
83 finally:
84 mlab.stop()
85
86 np.savez('cwt_matlabR2015b_result.npz', **all_matlab_results)