1
2from nose.tools import assert_almost_equal, assert_raises
3from numpy.testing import assert_array_equal, assert_array_almost_equal, assert_allclose
4from .registry import register
5
6
7@register()
8def test_reset(sim):
9    """
10    Run the same simulation n times without recreating the network,
11    and check the results are the same each time.
12    """
13    repeats = 3
14    dt = 1
15    sim.setup(timestep=dt, min_delay=dt, t_flush=10.0)
16    p = sim.Population(1, sim.IF_curr_exp(i_offset=0.1))
17    p.record('v')
18
19    for i in range(repeats):
20        sim.run(10.0)
21        sim.reset()
22    data = p.get_data(clear=False)
23    sim.end()
24
25    assert len(data.segments) == repeats
26    for segment in data.segments[1:]:
27        assert_array_almost_equal(segment.analogsignals[0],
28                                  data.segments[0].analogsignals[0], 10)
29
30
31test_reset.__test__ = False
32
33
34@register()
35def test_reset_with_clear(sim):
36    """
37    Run the same simulation n times without recreating the network,
38    and check the results are the same each time.
39    """
40    repeats = 3
41    dt = 1
42    sim.setup(timestep=dt, min_delay=dt, t_flush=10.0)
43    p = sim.Population(1, sim.IF_curr_exp(i_offset=0.1))
44    p.record('v')
45
46    data = []
47    for i in range(repeats):
48        sim.run(10.0)
49        data.append(p.get_data(clear=True))
50        sim.reset()
51
52    sim.end()
53
54    for rec in data:
55        assert len(rec.segments) == 1
56        assert_allclose(rec.segments[0].analogsignals[0].magnitude,
57                        data[0].segments[0].analogsignals[0].magnitude, 1e-11)
58
59
60test_reset_with_clear.__test__ = False
61
62
63
64@register()
65def test_reset_with_spikes(sim):
66    """
67    Run the same simulation n times without recreating the network,
68    and check the results are the same each time.
69    """
70    repeats = 3
71    dt = 0.1
72    sim.setup(timestep=dt, min_delay=dt, t_flush=200.0)
73    p1 = sim.Population(2, sim.SpikeSourceArray(spike_times=[
74        [1.2, 3.8, 9.2],
75        [1.5, 1.9, 2.7, 4.8, 6.8],
76    ]))
77    p2 = sim.Population(2, sim.IF_curr_exp())
78    p2.record('v')
79    prj = sim.Projection(p1, p2, sim.AllToAllConnector(),
80                         sim.StaticSynapse(weight=0.5, delay=0.5))
81
82    for i in range(repeats):
83        sim.run(10.0)
84        sim.reset()
85    data = p2.get_data(clear=False)
86    sim.end()
87
88    assert len(data.segments) == repeats
89    for segment in data.segments[1:]:
90        assert_array_almost_equal(segment.analogsignals[0],
91                                  data.segments[0].analogsignals[0], 10)
92
93
94test_reset_with_spikes.__test__ = False
95
96
97@register()
98def test_setup(sim):
99    """
100    Run the same simulation n times, recreating the network each time,
101    and check the results are the same each time.
102    """
103    n = 3
104    data = []
105    dt = 1
106
107    for i in range(n):
108        sim.setup(timestep=dt, min_delay=dt)
109        p = sim.Population(1, sim.IF_curr_exp(i_offset=0.1))
110        p.record('v')
111        sim.run(10.0)
112        data.append(p.get_data())
113        sim.end()
114
115    assert len(data) == n
116    for block in data:
117        assert len(block.segments) == 1
118        signals = block.segments[0].analogsignals
119        assert len(signals) == 1
120        assert_array_equal(signals[0], data[0].segments[0].analogsignals[0])
121
122
123test_setup.__test__ = False
124
125
126@register()
127def test_run_until(sim):
128    sim.setup(timestep=0.1)
129    p = sim.Population(1, sim.IF_cond_exp())
130    sim.run_until(12.7)
131    assert_almost_equal(sim.get_current_time(), 12.7, 10)
132    sim.run_until(12.7)
133    assert_almost_equal(sim.get_current_time(), 12.7, 10)
134    sim.run_until(99.9)
135    assert_almost_equal(sim.get_current_time(), 99.9, 10)
136    assert_raises(ValueError, sim.run_until, 88.8)
137    sim.end()
138
139
140test_run_until.__test__ = False
141
142
143if __name__ == '__main__':
144    from pyNN.utility import get_simulator
145    sim, args = get_simulator()
146    test_reset(sim)
147    test_reset_with_clear(sim)
148    test_setup(sim)
149    test_run_until(sim)
150