-
Notifications
You must be signed in to change notification settings - Fork 19
/
test_controls.py
57 lines (41 loc) · 1.73 KB
/
test_controls.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
from pathlib import Path
import ipywidgets as widgets
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.widgets import Slider
import mpl_interactions.ipyplot as iplt
from mpl_interactions.controller import Controls
def test_eager_register():
zoop = []
def cb(**kwargs):
zoop.append(1)
ctrls = Controls(beep=(0, 1), boop=(0, 1))
ctrls.register_callback(cb, None, eager=True)
assert len(zoop) == 1
ctrls = Controls(beep=(0, 1), boop=(0, 1))
ctrls.register_callback(cb, "beep", eager=False)
assert len(zoop) == 1
ctrls.controls["beep"].set_val(0.5)
assert len(zoop) == 2
ctrls.controls["boop"].set_val(0.5)
assert len(zoop) == 2
def test_save_animation(tmp_path: Path):
# just test that no errors are thrown, don't actually check
# that the saved animation makes any sense. Doing that seems hard :(
x = np.linspace(0, np.pi, 100)
tau = widgets.FloatSlider(min=1, max=10, step=1.5)
def f1(x, amp, tau, beta):
return amp * np.sin(x * tau) * x * beta
def f2(x, amp, tau, beta):
return amp * np.sin(x * beta) * x * tau
x = np.linspace(0, 2 * np.pi, 200)
fig, ax = plt.subplots()
plt.subplots_adjust(bottom=0.25)
axfreq = plt.axes([0.25, 0.1, 0.65, 0.03])
amp_slider = Slider(axfreq, label="amp", valmin=0.05, valmax=10)
ctrls = Controls(use_ipywidgets=True, tau=tau, beta=(1, 10, 15), amp=amp_slider)
iplt.plot(x, f1, ax=ax, controls=ctrls)
iplt.plot(x, f2, ax=ax, controls=ctrls)
ctrls.save_animation(str(tmp_path / "animation-amp.gif"), fig, "amp", N_frames=10)
ctrls.save_animation(str(tmp_path / "animation-beta.gif"), fig, "beta")
ctrls.save_animation(str(tmp_path / "animation-tau.gif"), fig, "tau")