| 1 |
import pytest |
| 2 |
from numpy.testing import assert_equal |
| 3 |
|
| 4 |
from brian2 import * |
| 5 |
from brian2.core.network import schedule_propagation_offset |
| 6 |
from brian2.devices.device import reinit_and_delete |
| 7 |
from brian2.tests.utils import assert_allclose, exc_isinstance |
| 8 |
|
| 9 |
|
| 10 |
@pytest.mark.standalone_compatible |
| 11 |
def test_poissoninput(): |
| 12 |
# Test extreme cases and do a very basic test of an intermediate case, we |
| 13 |
# don't want tests to be stochastic |
| 14 |
G = NeuronGroup( |
| 15 |
10, |
| 16 |
""" |
| 17 |
x : volt |
| 18 |
y : volt |
| 19 |
y2 : volt |
| 20 |
z : volt |
| 21 |
z2 : volt |
| 22 |
w : 1 |
| 23 |
""", |
| 24 |
) |
| 25 |
G.w = 0.5 |
| 26 |
|
| 27 |
never_update = PoissonInput(G, "x", 100, 0 * Hz, weight=1 * volt) |
| 28 |
always_update = PoissonInput(G, "y", 50, 1 / defaultclock.dt, weight=2 * volt) |
| 29 |
always_update2 = PoissonInput( |
| 30 |
G, "y2", 50, 1 / defaultclock.dt, weight="1*volt + 1*volt" |
| 31 |
) |
| 32 |
sometimes_update = PoissonInput(G, "z", 10000, 50 * Hz, weight=0.5 * volt) |
| 33 |
sometimes_update2 = PoissonInput(G, "z2", 10000, 50 * Hz, weight="w*volt") |
| 34 |
|
| 35 |
assert_equal(never_update.rate, 0 * Hz) |
| 36 |
assert_equal(never_update.N, 100) |
| 37 |
assert_equal(always_update.rate, 1 / defaultclock.dt) |
| 38 |
assert_equal(always_update.N, 50) |
| 39 |
assert_equal(sometimes_update.rate, 50 * Hz) |
| 40 |
assert_equal(sometimes_update.N, 10000) |
| 41 |
|
| 42 |
mon = StateMonitor(G, ["x", "y", "y2", "z", "z2"], record=True, when="end") |
| 43 |
|
| 44 |
run(1 * ms) |
| 45 |
assert_equal(0, mon.x[:]) |
| 46 |
assert_equal( |
| 47 |
np.tile((1 + np.arange(mon.y[:].shape[1])) * 50 * 2 * volt, (10, 1)), mon.y[:] |
| 48 |
) |
| 49 |
assert_equal( |
| 50 |
np.tile((1 + np.arange(mon.y[:].shape[1])) * 50 * 2 * volt, (10, 1)), mon.y2[:] |
| 51 |
) |
| 52 |
assert all(np.var(np.diff(mon.z[:]), axis=1) > 0) # variability over time |
| 53 |
assert all(np.var(mon.z[:], axis=0) > 0) # variability over neurons |
| 54 |
assert all(np.var(np.diff(mon.z2[:]), axis=1) > 0) # variability over time |
| 55 |
assert all(np.var(mon.z2[:], axis=0) > 0) # variability over neurons |
| 56 |
|
| 57 |
|
| 58 |
@pytest.mark.codegen_independent |
| 59 |
def test_poissoninput_errors(): |
| 60 |
# Targeting non-existing variable |
| 61 |
G = NeuronGroup( |
| 62 |
10, |
| 63 |
""" |
| 64 |
x : volt |
| 65 |
y : 1 |
| 66 |
""", |
| 67 |
) |
| 68 |
with pytest.raises(KeyError): |
| 69 |
PoissonInput(G, "z", 100, 100 * Hz, weight=1.0) |
| 70 |
|
| 71 |
# Incorrect units |
| 72 |
with pytest.raises(DimensionMismatchError): |
| 73 |
PoissonInput(G, "x", 100, 100 * Hz, weight=1.0) |
| 74 |
with pytest.raises(DimensionMismatchError): |
| 75 |
PoissonInput(G, "y", 100, 100 * Hz, weight=1.0 * volt) |
| 76 |
|
| 77 |
# dt change |
| 78 |
old_dt = defaultclock.dt |
| 79 |
inp = PoissonInput(G, "x", 100, 100 * Hz, weight=1 * volt) |
| 80 |
defaultclock.dt = 2 * old_dt |
| 81 |
net = Network(collect()) |
| 82 |
with pytest.raises(BrianObjectException) as exc: |
| 83 |
net.run(0 * ms) |
| 84 |
assert exc_isinstance(exc, NotImplementedError) |
| 85 |
defaultclock.dt = old_dt |
| 86 |
|
| 87 |
|
| 88 |
@pytest.mark.standalone_compatible |
| 89 |
def test_poissoninput_refractory(): |
| 90 |
eqs = """ |
| 91 |
dv/dt = 0/second : 1 (unless refractory) |
| 92 |
""" |
| 93 |
G = NeuronGroup( |
| 94 |
10, eqs, reset="v=0", threshold="v>4.5", refractory=5 * defaultclock.dt |
| 95 |
) |
| 96 |
# Will increase the value by 1.0 at each time step |
| 97 |
P = PoissonInput(G, "v", 1, 1 / defaultclock.dt, weight=1.0) |
| 98 |
mon = StateMonitor(G, "v", record=5) |
| 99 |
run(10 * defaultclock.dt) |
| 100 |
expected = np.arange(10, dtype=float) |
| 101 |
expected[6 - int(schedule_propagation_offset() / defaultclock.dt) :] = 0 |
| 102 |
assert_allclose(mon[5].v[:], expected) |
| 103 |
|
| 104 |
|
| 105 |
if __name__ == "__main__": |
| 106 |
test_poissoninput() |
| 107 |
reinit_and_delete() |
| 108 |
test_poissoninput_errors() |
| 109 |
test_poissoninput_refractory() |