Skip to content

Commit 05b8f38

Browse files
sichinagamtezzele
authored andcommitted
Test revisions
1 parent 6018d8f commit 05b8f38

File tree

1 file changed

+24
-55
lines changed

1 file changed

+24
-55
lines changed

tests/test_bopdmd.py

Lines changed: 24 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,22 @@ def sort_imag(x):
4545
return x[sorted_inds]
4646

4747

48+
def compute_error(actual, truth):
49+
"""
50+
Helper method that computes relative error.
51+
"""
52+
return np.linalg.norm(truth - actual) / np.linalg.norm(truth)
53+
54+
4855
# Simulate data.
49-
t = np.arange(5000) * 0.01
50-
t_long = np.arange(10000) * 0.01
51-
t_uneven = np.delete(t, np.arange(2000)[1::2])
56+
t = np.arange(4000) * 0.01
57+
t_long = np.arange(6000) * 0.01
58+
t_uneven = np.delete(t, np.arange(1000)[1::2])
5259
Z = simulate_z(t)
5360
Z_long = simulate_z(t_long)
54-
Z_uneven = np.delete(Z, np.arange(2000)[1::2], axis=1)
55-
rng = np.random.default_rng(seed=42)
56-
Z_noisy = Z + 0.2 * rng.standard_normal(Z.shape)
61+
Z_uneven = np.delete(Z, np.arange(1000)[1::2], axis=1)
62+
rng = np.random.default_rng(seed=1234)
63+
Z_noisy = Z + 0.01 * rng.standard_normal(Z.shape)
5764

5865
# Define the true eigenvalues of the system.
5966
expected_eigs = np.array((-1j, 1j))
@@ -112,23 +119,23 @@ def test_A():
112119
"""
113120
bopdmd = BOPDMD(svd_rank=2, compute_A=True)
114121
bopdmd.fit(Z, t)
115-
np.testing.assert_allclose(bopdmd.A, expected_A)
122+
assert compute_error(bopdmd.A, expected_A) < 1e-3
116123

117124
bopdmd = BOPDMD(svd_rank=2, compute_A=True)
118125
bopdmd.fit(Z_uneven, t_uneven)
119-
np.testing.assert_allclose(bopdmd.A, expected_A)
126+
assert compute_error(bopdmd.A, expected_A) < 1e-3
120127

121-
bopdmd = BOPDMD(svd_rank=2, compute_A=True)
128+
bopdmd = BOPDMD(svd_rank=2, compute_A=True, varpro_opts_dict={"tol": 0.05})
122129
bopdmd.fit(Z_noisy, t)
123-
np.testing.assert_allclose(bopdmd.A, expected_A, rtol=0.02)
130+
assert compute_error(bopdmd.A, expected_A) < 1e-3
124131

125132
bopdmd = BOPDMD(svd_rank=2, compute_A=True, use_proj=False)
126133
bopdmd.fit(Z, t)
127-
np.testing.assert_allclose(bopdmd.A, expected_A)
134+
assert compute_error(bopdmd.A, expected_A) < 1e-3
128135

129136
bopdmd = BOPDMD(svd_rank=2, compute_A=True, num_trials=10, trial_size=0.8)
130137
bopdmd.fit(Z, t)
131-
np.testing.assert_allclose(bopdmd.A, expected_A)
138+
assert compute_error(bopdmd.A, expected_A) < 1e-3
132139

133140

134141
def test_reconstruction():
@@ -158,15 +165,15 @@ def test_forecast():
158165
"""
159166
bopdmd = BOPDMD(svd_rank=2)
160167
bopdmd.fit(Z, t)
161-
np.testing.assert_allclose(bopdmd.forecast(t_long), Z_long, rtol=1e-2)
168+
assert compute_error(bopdmd.forecast(t_long), Z_long) < 1e-2
162169

163170
bopdmd = BOPDMD(svd_rank=2)
164171
bopdmd.fit(Z_uneven, t_uneven)
165-
np.testing.assert_allclose(bopdmd.forecast(t_long), Z_long, rtol=1e-2)
172+
assert compute_error(bopdmd.forecast(t_long), Z_long) < 1e-2
166173

167174
bopdmd = BOPDMD(svd_rank=2, num_trials=10, trial_size=0.8)
168175
bopdmd.fit(Z, t)
169-
np.testing.assert_allclose(bopdmd.forecast(t_long)[0], Z_long, rtol=1e-2)
176+
assert compute_error(bopdmd.forecast(t_long)[0], Z_long) < 1e-2
170177

171178

172179
def test_compute_A():
@@ -314,51 +321,13 @@ def bad_func_7(x):
314321

315322
def test_eig_constraints_2():
316323
"""
317-
Tests that if the eig_constraints function...
318-
- discards all real parts, the functionality is the same as
319-
setting eig_constraints={"imag"}
320-
- is a custom function, the functionality is as expected
324+
Tests that if the eig_constraints function discards all real parts,
325+
the functionality is the same as setting eig_constraints={"imag"}.
321326
"""
322327

323328
def make_imag(x):
324329
return 1j * x.imag
325330

326-
def make_real(x):
327-
return x.real
328-
329331
bopdmd1 = BOPDMD(svd_rank=2, eig_constraints={"imag"}).fit(Z, t)
330332
bopdmd2 = BOPDMD(svd_rank=2, eig_constraints=make_imag).fit(Z, t)
331333
np.testing.assert_array_equal(bopdmd1.eigs, bopdmd2.eigs)
332-
333-
bopdmd = BOPDMD(svd_rank=2, eig_constraints=make_real).fit(Z, t)
334-
assert np.all(bopdmd.eigs.imag == 0.0)
335-
336-
337-
def test_bagging_improvement():
338-
"""
339-
Tests that the use of bags improves accuracy on average when using noisy
340-
data for fitting. Uses A matrix error as a proxy for accuracy.
341-
"""
342-
343-
def relative_error(x, x_true):
344-
return np.linalg.norm(x_true - x) / np.linalg.norm(x_true)
345-
346-
optdmd = BOPDMD(svd_rank=2, compute_A=True, varpro_opts_dict={"tol": 0.246})
347-
optdmd.fit(Z_noisy, t)
348-
optdmd_error = relative_error(optdmd.A, expected_A)
349-
350-
test_trials = 10
351-
bop_success = 0
352-
for _ in range(test_trials):
353-
bopdmd = BOPDMD(
354-
svd_rank=2,
355-
compute_A=True,
356-
num_trials=100,
357-
trial_size=0.9,
358-
varpro_opts_dict={"tol": 0.246},
359-
)
360-
bopdmd.fit(Z_noisy, t)
361-
bopdmd_error = relative_error(bopdmd.A, expected_A)
362-
bop_success += bopdmd_error < optdmd_error
363-
364-
assert bop_success > 0.5 * test_trials

0 commit comments

Comments
 (0)