| 1 |
import uuid |
| 2 |
|
| 3 |
import pytest |
| 4 |
from numpy.testing import assert_equal |
| 5 |
|
| 6 |
from brian2 import * |
| 7 |
from brian2.core.network import schedule_propagation_offset |
| 8 |
from brian2.devices.device import reinit_and_delete |
| 9 |
from brian2.tests.utils import exc_isinstance |
| 10 |
from brian2.utils.logger import catch_logs |
| 11 |
|
| 12 |
|
| 13 |
@pytest.mark.standalone_compatible |
| 14 |
def test_single_rates(): |
| 15 |
# Specifying single rates |
| 16 |
P0 = PoissonGroup(10, 0 * Hz) |
| 17 |
Pfull = PoissonGroup(10, 1.0 / defaultclock.dt) |
| 18 |
|
| 19 |
# Basic properties |
| 20 |
assert len(P0) == len(Pfull) == 10 |
| 21 |
assert len(repr(P0)) and len(str(P0)) |
| 22 |
spikes_P0 = SpikeMonitor(P0) |
| 23 |
spikes_Pfull = SpikeMonitor(Pfull) |
| 24 |
run(2 * defaultclock.dt) |
| 25 |
assert_equal(spikes_P0.count, np.zeros(len(P0))) |
| 26 |
assert_equal(spikes_Pfull.count, 2 * np.ones(len(P0))) |
| 27 |
|
| 28 |
|
| 29 |
@pytest.mark.standalone_compatible |
| 30 |
def test_rate_arrays(): |
| 31 |
P = PoissonGroup(2, np.array([0, 1.0 / defaultclock.dt]) * Hz) |
| 32 |
spikes = SpikeMonitor(P) |
| 33 |
run(2 * defaultclock.dt) |
| 34 |
|
| 35 |
assert_equal(spikes.count, np.array([0, 2])) |
| 36 |
|
| 37 |
|
| 38 |
@pytest.mark.codegen_independent |
| 39 |
def test_rate_unit_check(): |
| 40 |
with pytest.raises(DimensionMismatchError): |
| 41 |
PoissonGroup(1, np.array([1, 2])) |
| 42 |
with pytest.raises(DimensionMismatchError): |
| 43 |
PoissonGroup(1, np.array([1, 2]) * ms) |
| 44 |
P = PoissonGroup(1, "i*mV") |
| 45 |
net = Network(P) |
| 46 |
with pytest.raises(BrianObjectException) as exc: |
| 47 |
net.run(0 * ms) |
| 48 |
assert exc_isinstance(exc, DimensionMismatchError) |
| 49 |
|
| 50 |
P = PoissonGroup(1, "i") |
| 51 |
net = Network(P) |
| 52 |
with pytest.raises(BrianObjectException) as exc: |
| 53 |
net.run(0 * ms) |
| 54 |
assert exc_isinstance(exc, DimensionMismatchError) |
| 55 |
|
| 56 |
|
| 57 |
@pytest.mark.standalone_compatible |
| 58 |
def test_time_dependent_rate(): |
| 59 |
# The following two groups should show the same behaviour |
| 60 |
timed_array = TimedArray( |
| 61 |
np.array([[0, 0], [1.0 / defaultclock.dt, 0]]) * Hz, dt=1 * ms |
| 62 |
) |
| 63 |
group_1 = PoissonGroup(2, rates="timed_array(t, i)") |
| 64 |
group_2 = PoissonGroup(2, rates="int(i==0)*int(t>1*ms-dt/2)*(1/dt)") |
| 65 |
spikes_1 = SpikeMonitor(group_1) |
| 66 |
spikes_2 = SpikeMonitor(group_2) |
| 67 |
run(2 * ms) |
| 68 |
|
| 69 |
assert_equal(spikes_1.count, np.array([int(round(1 * ms / defaultclock.dt)), 0])) |
| 70 |
assert_equal(spikes_2.count, np.array([int(round(1 * ms / defaultclock.dt)), 0])) |
| 71 |
assert sum(spikes_1.t < 1 * ms) == 0 |
| 72 |
assert sum(spikes_2.t < 1 * ms) == 0 |
| 73 |
|
| 74 |
|
| 75 |
@pytest.mark.standalone_compatible |
| 76 |
def test_propagation(): |
| 77 |
# Using a PoissonGroup as a source for Synapses should work as expected |
| 78 |
P = PoissonGroup(2, np.array([0, 1.0 / defaultclock.dt]) * Hz) |
| 79 |
G = NeuronGroup(2, "v:1") |
| 80 |
S = Synapses(P, G, on_pre="v+=1") |
| 81 |
S.connect(j="i") |
| 82 |
run(2 * defaultclock.dt + schedule_propagation_offset()) |
| 83 |
|
| 84 |
assert_equal(G.v[:], np.array([0.0, 2.0])) |
| 85 |
|
| 86 |
|
| 87 |
@pytest.mark.standalone_compatible |
| 88 |
def test_poissongroup_subgroup(): |
| 89 |
# It should be possible to take a subgroup of a PoissonGroup |
| 90 |
P = PoissonGroup(4, [0, 0, 0, 0] * Hz) |
| 91 |
P1 = P[:2] |
| 92 |
P2 = P[2:] |
| 93 |
P2.rates = 1.0 / defaultclock.dt |
| 94 |
G = NeuronGroup(4, "v:1") |
| 95 |
S1 = Synapses(P1, G[:2], on_pre="v+=1") |
| 96 |
S1.connect(j="i") |
| 97 |
S2 = Synapses(P2, G[2:], on_pre="v+=1") |
| 98 |
S2.connect(j="i") |
| 99 |
run(2 * defaultclock.dt + schedule_propagation_offset()) |
| 100 |
|
| 101 |
assert_equal(G.v[:], np.array([0.0, 0.0, 2.0, 2.0])) |
| 102 |
|
| 103 |
|
| 104 |
@pytest.mark.codegen_independent |
| 105 |
def test_poissongroup_namespace(): |
| 106 |
rate_const = 0 * Hz |
| 107 |
P = PoissonGroup( |
| 108 |
1, |
| 109 |
rates="rate_const", |
| 110 |
namespace={"rate_const": 1 / defaultclock.dt}, |
| 111 |
name=f"poissongroup_{uuid.uuid4().hex}", |
| 112 |
) |
| 113 |
P2 = PoissonGroup(1, rates="rate_const") |
| 114 |
mon = SpikeMonitor(P) |
| 115 |
mon2 = SpikeMonitor(P2) |
| 116 |
with catch_logs() as l: |
| 117 |
run(2 * defaultclock.dt) |
| 118 |
assert len(l) == 1 |
| 119 |
assert l[0][1].endswith("resolution_conflict") |
| 120 |
assert mon.num_spikes == 2 |
| 121 |
assert mon2.num_spikes == 0 |
| 122 |
|
| 123 |
|
| 124 |
if __name__ == "__main__": |
| 125 |
test_single_rates() |
| 126 |
test_rate_arrays() |
| 127 |
test_rate_unit_check() |
| 128 |
test_time_dependent_rate() |
| 129 |
test_propagation() |
| 130 |
test_poissongroup_subgroup() |
| 131 |
test_poissongroup_namespace() |