Debian Codegraph @

brian2/tests/test_timedarray.py

raw
1 import pytest
2
3 from brian2 import *
4 from brian2.devices.device import reinit_and_delete
5 from brian2.tests.utils import assert_allclose
6 from brian2.utils.caching import _hashable
7
8
9 @pytest.mark.codegen_independent
10 def test_timedarray_direct_use():
11 ta = TimedArray(np.linspace(0, 10, 11), 1 * ms)
12 assert ta(-1 * ms) == 0
13 assert ta(5 * ms) == 5
14 assert ta(10 * ms) == 10
15 assert ta(15 * ms) == 10
16 ta = TimedArray(np.linspace(0, 10, 11) * amp, 1 * ms)
17 assert ta(-1 * ms) == 0 * amp
18 assert ta(5 * ms) == 5 * amp
19 assert ta(10 * ms) == 10 * amp
20 assert ta(15 * ms) == 10 * amp
21 ta2d = TimedArray((np.linspace(0, 11, 12) * amp).reshape(4, 3), 1 * ms)
22 assert ta2d(-1 * ms, 0) == 0 * amp
23 assert ta2d(0 * ms, 0) == 0 * amp
24 assert ta2d(0 * ms, 1) == 1 * amp
25 assert ta2d(1 * ms, 1) == 4 * amp
26 assert_allclose(ta2d(1 * ms, [0, 1, 2]), [3, 4, 5] * amp)
27 assert_allclose(ta2d(15 * ms, [0, 1, 2]), [9, 10, 11] * amp)
28
29
30 @pytest.mark.standalone_compatible
31 def test_timedarray_semantics():
32 # Make sure that timed arrays are interpreted as specifying the values
33 # between t and t+dt (not between t-dt/2 and t+dt/2 as in Brian1)
34 ta = TimedArray(array([0, 1]), dt=0.4 * ms)
35 G = NeuronGroup(1, "value = ta(t) : 1", dt=0.1 * ms)
36 mon = StateMonitor(G, "value", record=0)
37 run(0.8 * ms)
38 assert_allclose(mon[0].value, [0, 0, 0, 0, 1, 1, 1, 1])
39 assert_allclose(mon[0].value, ta(mon.t))
40
41
42 @pytest.mark.standalone_compatible
43 def test_timedarray_no_units():
44 ta = TimedArray(np.arange(10), dt=0.1 * ms)
45 G = NeuronGroup(1, "value = ta(t) + 1: 1", dt=0.1 * ms)
46 mon = StateMonitor(G, "value", record=True, dt=0.1 * ms)
47 run(1.1 * ms)
48 assert_allclose(mon[0].value_, np.clip(np.arange(len(mon[0].t)), 0, 9) + 1)
49
50
51 @pytest.mark.standalone_compatible
52 def test_timedarray_with_units():
53 ta = TimedArray(np.arange(10) * amp, dt=0.1 * ms)
54 G = NeuronGroup(1, "value = ta(t) + 2*nA: amp", dt=0.1 * ms)
55 mon = StateMonitor(G, "value", record=True, dt=0.1 * ms)
56 run(1.1 * ms)
57 assert_allclose(
58 mon[0].value, np.clip(np.arange(len(mon[0].t)), 0, 9) * amp + 2 * nA
59 )
60
61
62 @pytest.mark.standalone_compatible
63 def test_timedarray_2d():
64 # 4 time steps, 3 neurons
65 ta2d = TimedArray(np.arange(12).reshape(4, 3), dt=0.1 * ms)
66 G = NeuronGroup(3, "value = ta2d(t, i) + 1: 1", dt=0.1 * ms)
67 mon = StateMonitor(G, "value", record=True, dt=0.1 * ms)
68 run(0.5 * ms)
69 assert_allclose(mon[0].value_, np.array([0, 3, 6, 9, 9]) + 1)
70 assert_allclose(mon[1].value_, np.array([1, 4, 7, 10, 10]) + 1)
71 assert_allclose(mon[2].value_, np.array([2, 5, 8, 11, 11]) + 1)
72
73
74 @pytest.mark.codegen_independent
75 def test_timedarray_incorrect_use():
76 ta = TimedArray(np.linspace(0, 10, 11), 1 * ms)
77 ta2d = TimedArray((np.linspace(0, 11, 12) * amp).reshape(4, 3), 1 * ms)
78 G = NeuronGroup(3, "I : amp")
79 with pytest.raises(ValueError):
80 setattr(G, "I", "ta2d(t)*amp")
81 with pytest.raises(ValueError):
82 setattr(G, "I", "ta(t, i)*amp")
83 with pytest.raises(ValueError):
84 setattr(G, "I", "ta()*amp")
85 with pytest.raises(ValueError):
86 setattr(G, "I", "ta*amp")
87
88
89 @pytest.mark.standalone_compatible
90 def test_timedarray_no_upsampling():
91 # Test a TimedArray where no upsampling is necessary because the monitor's
92 # dt is bigger than the TimedArray's
93 ta = TimedArray(np.arange(10), dt=0.01 * ms)
94 G = NeuronGroup(1, "value = ta(t): 1", dt=0.1 * ms)
95 mon = StateMonitor(G, "value", record=True, dt=1 * ms)
96 run(2.1 * ms)
97 assert_allclose(mon[0].value, [0, 9, 9])
98
99
100 # @pytest.mark.standalone_compatible # see FIXME comment below
101 def test_long_timedarray():
102 """
103 Use a very long timedarray (with a big dt), where the upsampling can lead
104 to integer overflow.
105 """
106 ta = TimedArray(np.arange(16385), dt=1 * second)
107 G = NeuronGroup(1, "value = ta(t) : 1")
108 mon = StateMonitor(G, "value", record=True)
109 net = Network(G, mon)
110 # We'll start the simulation close to the critical boundary
111 # FIXME: setting the time like this does not work for standalone
112 net.t_ = float(16384 * second - 5 * ms)
113 net.run(10 * ms)
114 assert_allclose(mon[0].value[mon.t < 16384 * second], 16383)
115 assert_allclose(mon[0].value[mon.t >= 16384 * second], 16384)
116
117
118 def test_timedarray_repeated_use():
119 # Check that recreating a TimedArray with different values does work
120 # correctly (no issues with caching)
121 values = np.array([[1, 2, 3], [2, 4, 6]])
122 for run_idx in range(2):
123 ta = TimedArray(values[run_idx], dt=defaultclock.dt, name="ta")
124 G = NeuronGroup(1, "dx/dt = ta(t)/dt : 1", name="G")
125 run(3 * defaultclock.dt)
126 assert G.x[0] == 6 * (run_idx + 1)
127
128
129 if __name__ == "__main__":
130 test_timedarray_direct_use()
131 test_timedarray_semantics()
132 test_timedarray_no_units()
133 test_timedarray_with_units()
134 test_timedarray_2d()
135 test_timedarray_incorrect_use()
136 test_timedarray_no_upsampling()
137 test_long_timedarray()
138 test_timedarray_repeated_use()