| 1 |
from collections import Counter |
| 2 |
|
| 3 |
import pytest |
| 4 |
from numpy.testing import assert_equal |
| 5 |
|
| 6 |
from brian2 import * |
| 7 |
from brian2.core.functions import timestep |
| 8 |
from brian2.devices.device import reinit_and_delete |
| 9 |
from brian2.equations.refractory import add_refractoriness |
| 10 |
from brian2.tests.utils import assert_allclose, exc_isinstance |
| 11 |
from brian2.utils.logger import catch_logs |
| 12 |
|
| 13 |
|
| 14 |
@pytest.mark.codegen_independent |
| 15 |
def test_add_refractoriness(): |
| 16 |
eqs = Equations( |
| 17 |
""" |
| 18 |
dv/dt = -x*v/second : volt (unless refractory) |
| 19 |
dw/dt = -w/second : amp |
| 20 |
x : 1 |
| 21 |
""" |
| 22 |
) |
| 23 |
# only make sure it does not throw an error |
| 24 |
eqs = add_refractoriness(eqs) |
| 25 |
# Check that the parameters were added |
| 26 |
assert "not_refractory" in eqs |
| 27 |
assert "lastspike" in eqs |
| 28 |
|
| 29 |
|
| 30 |
@pytest.mark.codegen_independent |
| 31 |
def test_missing_refractory_warning(): |
| 32 |
# Forgotten refractory argument |
| 33 |
with catch_logs() as l: |
| 34 |
group = NeuronGroup( |
| 35 |
1, |
| 36 |
"dv/dt = -v / (10*ms) : 1 (unless refractory)", |
| 37 |
threshold="v > 1", |
| 38 |
reset="v = 0", |
| 39 |
) |
| 40 |
assert len(l) == 1 |
| 41 |
assert l[0][0] == "WARNING" and l[0][1].endswith("no_refractory") |
| 42 |
|
| 43 |
|
| 44 |
@pytest.mark.standalone_compatible |
| 45 |
def test_refractoriness_basic(): |
| 46 |
G = NeuronGroup( |
| 47 |
1, |
| 48 |
""" |
| 49 |
dv/dt = 99.999*Hz : 1 (unless refractory) |
| 50 |
dw/dt = 99.999*Hz : 1 |
| 51 |
""", |
| 52 |
threshold="v>1", |
| 53 |
reset="v=0;w=0", |
| 54 |
refractory=5 * ms, |
| 55 |
) |
| 56 |
# It should take 10ms to reach the threshold, then v should stay at 0 |
| 57 |
# for 5ms, while w continues to increase |
| 58 |
mon = StateMonitor(G, ["v", "w"], record=True, when="end") |
| 59 |
run(20 * ms) |
| 60 |
# No difference before the spike |
| 61 |
assert_allclose( |
| 62 |
mon[0].v[: timestep(10 * ms, defaultclock.dt)], |
| 63 |
mon[0].w[: timestep(10 * ms, defaultclock.dt)], |
| 64 |
) |
| 65 |
# v is not updated during refractoriness |
| 66 |
in_refractoriness = mon[0].v[ |
| 67 |
timestep(10 * ms, defaultclock.dt) : timestep(15 * ms, defaultclock.dt) |
| 68 |
] |
| 69 |
assert_equal(in_refractoriness, np.zeros_like(in_refractoriness)) |
| 70 |
# w should evolve as before |
| 71 |
assert_allclose( |
| 72 |
mon[0].w[: timestep(5 * ms, defaultclock.dt)], |
| 73 |
mon[0].w[ |
| 74 |
timestep(10 * ms, defaultclock.dt) + 1 : timestep(15 * ms, defaultclock.dt) |
| 75 |
+ 1 |
| 76 |
], |
| 77 |
) |
| 78 |
assert np.all( |
| 79 |
mon[0].w[ |
| 80 |
timestep(10 * ms, defaultclock.dt) + 1 : timestep(15 * ms, defaultclock.dt) |
| 81 |
+ 1 |
| 82 |
] |
| 83 |
> 0 |
| 84 |
) |
| 85 |
# After refractoriness, v should increase again |
| 86 |
assert np.all( |
| 87 |
mon[0].v[ |
| 88 |
timestep(15 * ms, defaultclock.dt) : timestep(20 * ms, defaultclock.dt) |
| 89 |
] |
| 90 |
> 0 |
| 91 |
) |
| 92 |
|
| 93 |
|
| 94 |
@pytest.mark.standalone_compatible |
| 95 |
@pytest.mark.parametrize( |
| 96 |
"ref_time", |
| 97 |
[ |
| 98 |
"5*ms", |
| 99 |
"(t-lastspike + 1e-3*dt) < 5*ms", |
| 100 |
"time_since_spike + 1e-3*dt < 5*ms", |
| 101 |
"ref_subexpression", |
| 102 |
"(t-lastspike + 1e-3*dt) < ref", |
| 103 |
"ref", |
| 104 |
"ref_no_unit*ms", |
| 105 |
], |
| 106 |
) |
| 107 |
def test_refractoriness_variables(ref_time): |
| 108 |
# Try a string evaluating to a quantity, and an explicit boolean |
| 109 |
# condition -- all should do the same thing |
| 110 |
G = NeuronGroup( |
| 111 |
1, |
| 112 |
""" |
| 113 |
dv/dt = 99.999*Hz : 1 (unless refractory) |
| 114 |
dw/dt = 99.999*Hz : 1 |
| 115 |
ref : second |
| 116 |
ref_no_unit : 1 |
| 117 |
time_since_spike = (t - lastspike) +1e-3*dt : second |
| 118 |
ref_subexpression = (t - lastspike + 1e-3*dt) < ref : boolean |
| 119 |
""", |
| 120 |
threshold="v>1", |
| 121 |
reset="v=0;w=0", |
| 122 |
refractory=ref_time, |
| 123 |
dtype={ |
| 124 |
"ref": defaultclock.variables["t"].dtype, |
| 125 |
"ref_no_unit": defaultclock.variables["t"].dtype, |
| 126 |
"lastspike": defaultclock.variables["t"].dtype, |
| 127 |
"time_since_spike": defaultclock.variables["t"].dtype, |
| 128 |
}, |
| 129 |
) |
| 130 |
G.ref = 5 * ms |
| 131 |
G.ref_no_unit = 5 |
| 132 |
# It should take 10ms to reach the threshold, then v should stay at 0 |
| 133 |
# for 5ms, while w continues to increase |
| 134 |
mon = StateMonitor(G, ["v", "w"], record=True, when="end") |
| 135 |
run(20 * ms) |
| 136 |
try: |
| 137 |
# No difference before the spike |
| 138 |
assert_allclose( |
| 139 |
mon[0].v[: timestep(10 * ms, defaultclock.dt)], |
| 140 |
mon[0].w[: timestep(10 * ms, defaultclock.dt)], |
| 141 |
) |
| 142 |
# v is not updated during refractoriness |
| 143 |
in_refractoriness = mon[0].v[ |
| 144 |
timestep(10 * ms, defaultclock.dt) : timestep(15 * ms, defaultclock.dt) |
| 145 |
] |
| 146 |
assert_allclose(in_refractoriness, np.zeros_like(in_refractoriness)) |
| 147 |
# w should evolve as before |
| 148 |
assert_allclose( |
| 149 |
mon[0].w[: timestep(5 * ms, defaultclock.dt)], |
| 150 |
mon[0].w[ |
| 151 |
timestep(10 * ms, defaultclock.dt) + 1 : timestep( |
| 152 |
15 * ms, defaultclock.dt |
| 153 |
) |
| 154 |
+ 1 |
| 155 |
], |
| 156 |
) |
| 157 |
assert np.all( |
| 158 |
mon[0].w[ |
| 159 |
timestep(10 * ms, defaultclock.dt) + 1 : timestep( |
| 160 |
15 * ms, defaultclock.dt |
| 161 |
) |
| 162 |
+ 1 |
| 163 |
] |
| 164 |
> 0 |
| 165 |
) |
| 166 |
# After refractoriness, v should increase again |
| 167 |
assert np.all( |
| 168 |
mon[0].v[ |
| 169 |
timestep(15 * ms, defaultclock.dt) : timestep(20 * ms, defaultclock.dt) |
| 170 |
] |
| 171 |
> 0 |
| 172 |
) |
| 173 |
except AssertionError as ex: |
| 174 |
raise |
| 175 |
raise AssertionError( |
| 176 |
f"Assertion failed when using {ref_time!r} as refractory argument:\n{ex}" |
| 177 |
) |
| 178 |
|
| 179 |
|
| 180 |
@pytest.mark.standalone_compatible |
| 181 |
def test_refractoriness_threshold_basic(): |
| 182 |
G = NeuronGroup( |
| 183 |
1, |
| 184 |
""" |
| 185 |
dv/dt = 199.99*Hz : 1 |
| 186 |
""", |
| 187 |
threshold="v > 1", |
| 188 |
reset="v=0", |
| 189 |
refractory=10 * ms, |
| 190 |
) |
| 191 |
# The neuron should spike after 5ms but then not spike for the next |
| 192 |
# 10ms. The state variable should continue to integrate so there should |
| 193 |
# be a spike after 15ms |
| 194 |
spike_mon = SpikeMonitor(G) |
| 195 |
run(16 * ms) |
| 196 |
assert_allclose(spike_mon.t, [5, 15] * ms) |
| 197 |
|
| 198 |
|
| 199 |
@pytest.mark.standalone_compatible |
| 200 |
def test_refractoriness_repeated(): |
| 201 |
# Create a group that spikes whenever it can |
| 202 |
group = NeuronGroup(1, "", threshold="True", refractory=10 * defaultclock.dt) |
| 203 |
spike_mon = SpikeMonitor(group) |
| 204 |
run(10000 * defaultclock.dt) |
| 205 |
assert spike_mon.t[0] == 0 * ms |
| 206 |
assert_allclose(np.diff(spike_mon.t), 10 * defaultclock.dt) |
| 207 |
|
| 208 |
|
| 209 |
@pytest.mark.standalone_compatible |
| 210 |
def test_refractoriness_repeated_legacy(): |
| 211 |
if prefs.core.default_float_dtype == np.float32: |
| 212 |
pytest.skip( |
| 213 |
"Not testing legacy refractory mechanism with single precision floats." |
| 214 |
) |
| 215 |
# Switch on behaviour from versions <= 2.1.2 |
| 216 |
prefs.legacy.refractory_timing = True |
| 217 |
# Create a group that spikes whenever it can |
| 218 |
group = NeuronGroup(1, "", threshold="True", refractory=10 * defaultclock.dt) |
| 219 |
spike_mon = SpikeMonitor(group) |
| 220 |
run(10000 * defaultclock.dt) |
| 221 |
assert spike_mon.t[0] == 0 * ms |
| 222 |
|
| 223 |
# Empirical values from running with earlier Brian versions |
| 224 |
assert_allclose( |
| 225 |
np.diff(spike_mon.t)[:10], [1.1, 1, 1.1, 1, 1.1, 1.1, 1.1, 1.1, 1, 1.1] * ms |
| 226 |
) |
| 227 |
steps = Counter(np.diff(np.int_(np.round(spike_mon.t / defaultclock.dt)))) |
| 228 |
assert len(steps) == 2 and steps[10] == 899 and steps[11] == 91 |
| 229 |
prefs.legacy.refractory_timing = False |
| 230 |
|
| 231 |
|
| 232 |
@pytest.mark.standalone_compatible |
| 233 |
@pytest.mark.parametrize( |
| 234 |
"ref_time", |
| 235 |
[ |
| 236 |
10 * ms, |
| 237 |
"10*ms", |
| 238 |
"timestep(t-lastspike, dt) < timestep(10*ms, dt)", |
| 239 |
"timestep(t-lastspike, dt) < timestep(ref, dt)", |
| 240 |
"ref", |
| 241 |
"ref_no_unit*ms", |
| 242 |
], |
| 243 |
) |
| 244 |
def test_refractoriness_threshold(ref_time): |
| 245 |
# Try a quantity, a string evaluating to a quantity, and an explicit boolean |
| 246 |
# condition -- all should do the same thing |
| 247 |
G = NeuronGroup( |
| 248 |
1, |
| 249 |
""" |
| 250 |
dv/dt = 199.999*Hz : 1 |
| 251 |
ref : second |
| 252 |
ref_no_unit : 1 |
| 253 |
""", |
| 254 |
threshold="v > 1", |
| 255 |
reset="v=0", |
| 256 |
refractory=ref_time, |
| 257 |
dtype={ |
| 258 |
"ref": defaultclock.variables["t"].dtype, |
| 259 |
"ref_no_unit": defaultclock.variables["t"].dtype, |
| 260 |
}, |
| 261 |
) |
| 262 |
G.ref = 10 * ms |
| 263 |
G.ref_no_unit = 10 |
| 264 |
# The neuron should spike after 5ms but then not spike for the next |
| 265 |
# 10ms. The state variable should continue to integrate so there should |
| 266 |
# be a spike after 15ms |
| 267 |
spike_mon = SpikeMonitor(G) |
| 268 |
run(16 * ms) |
| 269 |
assert_allclose(spike_mon.t, [5, 15] * ms) |
| 270 |
|
| 271 |
|
| 272 |
@pytest.mark.codegen_independent |
| 273 |
def test_refractoriness_types(): |
| 274 |
# make sure that using a wrong type of refractoriness does not work |
| 275 |
group = NeuronGroup(1, "", refractory="3*Hz") |
| 276 |
with pytest.raises(BrianObjectException) as exc: |
| 277 |
Network(group).run(0 * ms) |
| 278 |
assert exc_isinstance(exc, TypeError) |
| 279 |
group = NeuronGroup(1, "ref: Hz", refractory="ref") |
| 280 |
with pytest.raises(BrianObjectException) as exc: |
| 281 |
Network(group).run(0 * ms) |
| 282 |
assert exc_isinstance(exc, TypeError) |
| 283 |
group = NeuronGroup(1, "", refractory="3") |
| 284 |
with pytest.raises(BrianObjectException) as exc: |
| 285 |
Network(group).run(0 * ms) |
| 286 |
assert exc_isinstance(exc, TypeError) |
| 287 |
group = NeuronGroup(1, "ref: 1", refractory="ref") |
| 288 |
with pytest.raises(BrianObjectException) as exc: |
| 289 |
Network(group).run(0 * ms) |
| 290 |
assert exc_isinstance(exc, TypeError) |
| 291 |
|
| 292 |
|
| 293 |
@pytest.mark.codegen_independent |
| 294 |
def test_conditional_write_set(): |
| 295 |
""" |
| 296 |
Test that the conditional_write attribute is set correctly |
| 297 |
""" |
| 298 |
G = NeuronGroup( |
| 299 |
1, |
| 300 |
""" |
| 301 |
dv/dt = 10*Hz : 1 (unless refractory) |
| 302 |
dw/dt = 10*Hz : 1 |
| 303 |
""", |
| 304 |
refractory=2 * ms, |
| 305 |
) |
| 306 |
assert G.variables["v"].conditional_write is G.variables["not_refractory"] |
| 307 |
assert G.variables["w"].conditional_write is None |
| 308 |
|
| 309 |
|
| 310 |
@pytest.mark.standalone_compatible |
| 311 |
def test_conditional_write_behaviour(): |
| 312 |
H = NeuronGroup(1, "v:1", threshold="v>-1") |
| 313 |
|
| 314 |
tau = 1 * ms |
| 315 |
eqs = """ |
| 316 |
dv/dt = (2-v)/tau : 1 (unless refractory) |
| 317 |
dx/dt = 0/tau : 1 (unless refractory) |
| 318 |
dy/dt = 0/tau : 1 |
| 319 |
""" |
| 320 |
reset = """ |
| 321 |
v = 0 |
| 322 |
x -= 0.05 |
| 323 |
y -= 0.05 |
| 324 |
""" |
| 325 |
G = NeuronGroup(1, eqs, threshold="v>1", reset=reset, refractory=1 * ms) |
| 326 |
|
| 327 |
Sx = Synapses(H, G, on_pre="x += dt*100*Hz") |
| 328 |
Sx.connect(True) |
| 329 |
|
| 330 |
Sy = Synapses(H, G, on_pre="y += dt*100*Hz") |
| 331 |
Sy.connect(True) |
| 332 |
|
| 333 |
M = StateMonitor(G, variables=True, record=True) |
| 334 |
|
| 335 |
run(10 * ms) |
| 336 |
|
| 337 |
assert G.x[0] < 0.2 |
| 338 |
assert G.y[0] > 0.2 |
| 339 |
assert G.v[0] < 1.1 |
| 340 |
|
| 341 |
|
| 342 |
@pytest.mark.standalone_compatible |
| 343 |
def test_conditional_write_automatic_and_manual(): |
| 344 |
source = NeuronGroup(1, "", threshold="True") # spiking all the time |
| 345 |
target = NeuronGroup( |
| 346 |
2, |
| 347 |
""" |
| 348 |
dv/dt = 0/ms : 1 (unless refractory) |
| 349 |
dw/dt = 0/ms : 1 |
| 350 |
""", |
| 351 |
threshold="t == 0*ms", |
| 352 |
refractory="False", |
| 353 |
) # only refractory for the first time step |
| 354 |
# Cell is spiking/refractory only in the first time step |
| 355 |
syn = Synapses( |
| 356 |
source, |
| 357 |
target, |
| 358 |
on_pre=""" |
| 359 |
v += 1 |
| 360 |
w += 1 * int(not_refractory_post) |
| 361 |
""", |
| 362 |
) |
| 363 |
syn.connect() |
| 364 |
mon = StateMonitor(target, ["v", "w"], record=True, when="end") |
| 365 |
run(2 * defaultclock.dt) |
| 366 |
|
| 367 |
# Synapse should not have been effective in the first time step |
| 368 |
assert_allclose(mon.v[:, 0], 0) |
| 369 |
assert_allclose(mon.v[:, 1], 1) |
| 370 |
assert_allclose(mon.w[:, 0], 0) |
| 371 |
assert_allclose(mon.w[:, 1], 1) |
| 372 |
|
| 373 |
|
| 374 |
if __name__ == "__main__": |
| 375 |
test_add_refractoriness() |
| 376 |
test_missing_refractory_warning() |
| 377 |
test_refractoriness_basic() |
| 378 |
test_refractoriness_variables() |
| 379 |
test_refractoriness_threshold() |
| 380 |
test_refractoriness_threshold_basic() |
| 381 |
test_refractoriness_repeated() |
| 382 |
test_refractoriness_repeated_legacy() |
| 383 |
test_refractoriness_types() |
| 384 |
test_conditional_write_set() |
| 385 |
test_conditional_write_behaviour() |
| 386 |
test_conditional_write_automatic_and_manual() |