Skip to content

Commit 942692a

Browse files
committed
repurposed some figure-making code I had into a test of the simulations to improve coverage
1 parent b23a368 commit 942692a

1 file changed

Lines changed: 22 additions & 3 deletions

File tree

pynumdiff/tests/test_utils.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
# pylint: skip-file
55

66
import numpy as np
7-
from pynumdiff.utils import utility, simulate, evaluate
7+
from pynumdiff.utils import utility, evaluate
8+
from pynumdiff.utils.simulate import sine, triangle, pop_dyn, linear_autonomous, pi_cruise_control, lorenz_x
89
np.random.seed(42) # The answer to life, the universe, and everything
910

1011

@@ -88,8 +89,26 @@ def identity(x, dt): return x, 0 # should come back the same
8889
assert np.allclose(x, x_hat)
8990

9091

91-
# def test_simulate():
92-
# return
92+
def test_simulations(request):
93+
if request.config.getoption("--plot"):
94+
from matplotlib import pyplot
95+
fig, axes = pyplot.subplots(2, 3, figsize=(18,7), constrained_layout=True)
96+
97+
for i,(sim,title) in enumerate(zip(
98+
[pi_cruise_control, sine, triangle, pop_dyn, linear_autonomous, lorenz_x],
99+
["Cruise Control", "Sum of Sines", "Triangles", "Logistic Growth", "Linear Autonomous", "Lorenz First Dimension"])):
100+
101+
y, x, dxdt = sim(duration=4, dt=0.01, noise_type='normal', noise_parameters=[0,0.1])
102+
assert len(y) == len(x) == len(dxdt)
103+
104+
if request.config.getoption("--plot"):
105+
t = np.arange(len(y))*0.01
106+
ax = axes[i//3, i%3]
107+
ax.plot(t, x, 'k--', linewidth=3, label=r"true $x$")
108+
ax.plot(t, y, '.', color='blue', zorder=-100, markersize=5, label="noisy data")
109+
if i//3 == 0: ax.set_xticklabels([])
110+
ax.set_title(title, fontsize=18)
111+
if i == 5: ax.legend(loc='lower right', fontsize=12)
93112

94113
# def test_evaluate():
95114
# return

0 commit comments

Comments
 (0)