/
test_solve.py
245 lines (182 loc) · 6.89 KB
/
test_solve.py
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
"""
Smoke testing--just see if the system runs.
"""
# These tests are quick and crude.
# TODO: extend the tests by cycling through various combinations
# of configuration and data input.
from __future__ import (absolute_import, division, print_function)
import numpy as np
from utide._ut_constants import ut_constants
from utide import solve
from utide import reconstruct
from utide.utilities import Bunch
def test_roundtrip():
"""Minimal conversion from simple_utide_test."""
ts = 735604
duration = 35
time = np.linspace(ts, ts+duration, 842)
tref = (time[-1] + time[0]) / 2
const = ut_constants.const
amp = 1.0
phase = 53
lat = 45.5
freq_cpd = 24 * const.freq
jj = 48-1 # Python index for M2
arg = 2 * np.pi * (time - tref) * freq_cpd[jj] - np.deg2rad(phase)
time_series = amp * np.cos(arg)
opts = dict(constit='auto',
phase='raw',
nodal=False,
trend=False,
method='ols',
conf_int='linear',
Rayleigh_min=0.95,
)
speed_coef = solve(time, time_series, time_series, lat=lat, **opts)
elev_coef = solve(time, time_series, lat=lat, **opts)
amp_err = amp - elev_coef['A'][0]
phase_err = phase - elev_coef['g'][0]
ts_recon = reconstruct(time, elev_coef).h
# pure smoke testing of reconstruct
vel = reconstruct(time, speed_coef)
vel = reconstruct(time, speed_coef, constit=('M2', 'S2'))
htmp = reconstruct(time, elev_coef, constit=('M2', 'S2'))
vel = reconstruct(time, speed_coef, min_SNR=3)
htmp = reconstruct(time, elev_coef, min_SNR=3)
vel = reconstruct(time, speed_coef, min_PE=10)
htmp = reconstruct(time, elev_coef, min_PE=10)
vel = reconstruct(time, speed_coef, min_SNR=0)
htmp = reconstruct(time, elev_coef, min_SNR=0)
assert isinstance(vel, Bunch)
assert isinstance(htmp, Bunch)
# Now the round-trip check, just for the elevation.
err = np.sqrt(np.mean((time_series-ts_recon)**2))
print(amp_err, phase_err, err)
print(elev_coef['aux']['reftime'], tref)
print(elev_coef['aux']['opt'])
np.testing.assert_almost_equal(amp_err, 0)
np.testing.assert_almost_equal(phase_err, 0)
np.testing.assert_almost_equal(err, 0)
def test_masked_input():
"""Masked values in time and/or time series."""
ts = 735604
duration = 35
time = np.linspace(ts, ts+duration, 842)
tref = (time[-1] + time[0]) / 2
const = ut_constants.const
amp = 1.0
phase = 53
lat = 45.5
freq_cpd = 24 * const.freq
jj = 48-1 # Python index for M2
arg = 2 * np.pi * (time - tref) * freq_cpd[jj] - np.deg2rad(phase)
time_series = amp * np.cos(arg)
opts = dict(constit='auto',
phase='raw',
nodal=False,
trend=False,
method='ols',
conf_int='linear',
Rayleigh_min=0.95,
)
t = np.ma.array(time)
t[[10, 15, 20, 21]] = np.ma.masked
series = np.ma.array(time_series)
series[[11, 17, 22, 25]] = np.ma.masked
speed_coef = solve(t, series, series, lat=lat, **opts)
elev_coef = solve(t, series, lat=lat, **opts)
amp_err = amp - elev_coef['A'][0]
phase_err = phase - elev_coef['g'][0]
ts_recon = reconstruct(time, elev_coef).h
assert isinstance(ts_recon, np.ndarray)
# pure smoke testing of reconstruct
vel = reconstruct(time, speed_coef)
assert isinstance(vel, Bunch)
elev = reconstruct(time, elev_coef)
assert isinstance(elev, Bunch)
np.testing.assert_almost_equal(amp_err, 0)
np.testing.assert_almost_equal(phase_err, 0)
def test_robust():
"""
Quick check that method='robust' works; no real checking
of results, other than by using "py.test -s" and noting that
the results are reasonable, and the weights for the outliers
are very small.
Minimal conversion from simple_utide_test
"""
ts = 735604
duration = 35
time = np.linspace(ts, ts+duration, 842)
tref = (time[-1] + time[0]) / 2
const = ut_constants.const
amp = 1.0
phase = 53
lat = 45.5
freq_cpd = 24 * const.freq
jj = 48-1 # Python index for M2
arg = 2 * np.pi * (time - tref) * freq_cpd[jj] - np.deg2rad(phase)
time_series = amp * np.cos(arg)
# Add noise
np.random.seed(1)
time_series += 0.01 * np.random.randn(len(time_series))
# Add wild points
time_series[:5] = 10
time_series[-5:] = -10
opts = dict(constit='auto',
phase='raw',
nodal=False,
trend=False,
method='robust',
conf_int='linear',
Rayleigh_min=0.95,
)
speed_coef = solve(time, time_series, time_series, lat=lat, **opts)
elev_coef = solve(time, time_series, lat=lat, **opts)
print(speed_coef.weights, elev_coef.weights)
print(speed_coef.rf, elev_coef.rf)
def test_MC():
ts = 735604
duration = 35
time = np.linspace(ts, ts+duration, 842)
tref = (time[-1] + time[0]) / 2
const = ut_constants.const
amp = 1.0
phase = 53
lat = 45.5
freq_cpd = 24 * const.freq
jj = 48-1 # Python index for M2
arg = 2 * np.pi * (time - tref) * freq_cpd[jj] - np.deg2rad(phase)
time_series = amp * np.cos(arg)
# Add noise
np.random.seed(1)
time_series += 0.01 * np.random.randn(len(time_series))
opts = dict(constit='auto',
phase='raw',
nodal=False,
trend=False,
method='ols',
conf_int='MC',
white=False,
Rayleigh_min=0.95,
)
speed_coef = solve(time, time_series, time_series, lat=lat, **opts)
elev_coef = solve(time, time_series, lat=lat, **opts)
for name, AA, AA_ci, gg, gg_ci in zip(elev_coef.name,
elev_coef.A,
elev_coef.A_ci,
elev_coef.g,
elev_coef.g_ci):
print('%5s %10.4g %10.4g %10.4g %10.4g' %
(name, AA, AA_ci, gg, gg_ci))
for (name, Lsmaj, Lsmaj_ci, Lsmin, Lsmin_ci,
theta, theta_ci, gg, gg_ci) in zip(speed_coef.name,
speed_coef.Lsmaj,
speed_coef.Lsmaj_ci,
speed_coef.Lsmin,
speed_coef.Lsmin_ci,
speed_coef.theta,
speed_coef.theta_ci,
speed_coef.g,
speed_coef.g_ci):
print('%5s %10.4g %10.4g %10.4g %10.4g %10.4g %10.4g %10.4g %10.4g' %
(name, Lsmaj, Lsmaj_ci, Lsmin, Lsmin_ci, theta, theta_ci, gg, gg_ci))