| 1 |
import functools |
| 2 |
|
| 3 |
import pytest |
| 4 |
|
| 5 |
from brian2 import * |
| 6 |
from brian2.codegen.runtime.GSLcython_rt import IntegrationError |
| 7 |
from brian2.core.preferences import PreferenceError |
| 8 |
from brian2.devices.device import auto_target |
| 9 |
from brian2.stateupdaters.base import UnsupportedEquationsException |
| 10 |
from brian2.tests.utils import exc_isinstance |
| 11 |
|
| 12 |
max_difference = 0.1 * mV |
| 13 |
|
| 14 |
pytestmark = pytest.mark.gsl |
| 15 |
|
| 16 |
|
| 17 |
def skip_if_not_implemented(func): |
| 18 |
@functools.wraps(func) |
| 19 |
def wrapped(): |
| 20 |
if prefs.codegen.target == "numpy" or ( |
| 21 |
prefs.codegen.target == "auto" and auto_target().class_name == "numpy" |
| 22 |
): |
| 23 |
pytest.skip("GSL support for numpy has not been implemented yet") |
| 24 |
else: |
| 25 |
return func() |
| 26 |
|
| 27 |
return wrapped |
| 28 |
|
| 29 |
|
| 30 |
@pytest.mark.standalone_compatible |
| 31 |
@skip_if_not_implemented |
| 32 |
def test_GSL_stateupdater_basic(): |
| 33 |
# just the adaptive_threshold example: run for exponential_euler and GSL and see |
| 34 |
# if results are comparable (same amount of spikes and spikes > 0) |
| 35 |
eqs = """ |
| 36 |
dv/dt = -v/(10*ms) : volt |
| 37 |
dvt/dt = (10*mV-vt)/(15*ms) : volt |
| 38 |
""" |
| 39 |
reset = """ |
| 40 |
v = 0*mV |
| 41 |
vt += 3*mV |
| 42 |
""" |
| 43 |
neurons_conventional = NeuronGroup( |
| 44 |
1, model=eqs, reset=reset, threshold="v>vt", method="exponential_euler" |
| 45 |
) |
| 46 |
neurons_GSL = NeuronGroup(1, model=eqs, reset=reset, threshold="v>vt", method="gsl") |
| 47 |
neurons_conventional.vt = 10 * mV |
| 48 |
neurons_GSL.vt = 10 * mV |
| 49 |
# 50 'different' neurons so no neuron spikes more than once per dt |
| 50 |
P = SpikeGeneratorGroup(1, [0] * 50, np.arange(50) / 50.0 * 100 * ms) |
| 51 |
C_conventional = Synapses(P, neurons_conventional, on_pre="v += 3*mV") |
| 52 |
C_GSL = Synapses(P, neurons_GSL, on_pre="v += 3*mV") |
| 53 |
C_conventional.connect() |
| 54 |
C_GSL.connect() |
| 55 |
SM_conventional = SpikeMonitor(neurons_conventional, variables="v") |
| 56 |
SM_GSL = SpikeMonitor(neurons_GSL, variables="v") |
| 57 |
net = Network( |
| 58 |
neurons_conventional, |
| 59 |
neurons_GSL, |
| 60 |
P, |
| 61 |
C_conventional, |
| 62 |
C_GSL, |
| 63 |
SM_conventional, |
| 64 |
SM_GSL, |
| 65 |
) |
| 66 |
net.run(100 * ms) |
| 67 |
assert SM_conventional.num_spikes > 0, ( |
| 68 |
"simulation should produce spiking, but no spikes monitored" |
| 69 |
) |
| 70 |
assert SM_conventional.num_spikes == SM_GSL.num_spikes, ( |
| 71 |
"GSL_statupdater produced different number of spikes than integration with ", |
| 72 |
"exponential euler", |
| 73 |
) |
| 74 |
|
| 75 |
|
| 76 |
@pytest.mark.standalone_compatible |
| 77 |
@skip_if_not_implemented |
| 78 |
def test_GSL_different_clocks(): |
| 79 |
vt = 10 * mV |
| 80 |
eqs = "dv/dt = -v/(10*ms) : volt" |
| 81 |
neurons = NeuronGroup(1, model=eqs, threshold="v>vt", method="gsl", dt=0.2 * ms) |
| 82 |
# for this test just check if it compiles |
| 83 |
run(0 * ms) |
| 84 |
|
| 85 |
|
| 86 |
@pytest.mark.standalone_compatible |
| 87 |
@skip_if_not_implemented |
| 88 |
def test_GSL_default_function(): |
| 89 |
# phase_locking example |
| 90 |
tau = 20 * ms |
| 91 |
n = 100 |
| 92 |
b = 1.2 # constant current mean, the modulation varies |
| 93 |
freq = 10 * Hz |
| 94 |
eqs = """ |
| 95 |
dv/dt = (-v + a * sin(2 * pi * freq * t) + b) / tau : 1 |
| 96 |
a : 1 |
| 97 |
""" |
| 98 |
vrand = rand(n) |
| 99 |
neurons_conventional = NeuronGroup( |
| 100 |
n, model=eqs, threshold="v > 1", reset="v = 0", method="exponential_euler" |
| 101 |
) |
| 102 |
neurons_GSL = NeuronGroup( |
| 103 |
n, model=eqs, threshold="v > 1", reset="v = 0", method="gsl" |
| 104 |
) |
| 105 |
neurons_conventional.v = vrand |
| 106 |
neurons_GSL.v = vrand |
| 107 |
neurons_conventional.a = "0.05 + 0.7*i/n" |
| 108 |
neurons_GSL.a = "0.05 + 0.7*i/n" |
| 109 |
|
| 110 |
trace_conventional = StateMonitor(neurons_conventional, "v", record=50) |
| 111 |
trace_GSL = StateMonitor(neurons_GSL, "v", record=50) |
| 112 |
net = Network(neurons_conventional, neurons_GSL, trace_conventional, trace_GSL) |
| 113 |
net.run(10 * ms) |
| 114 |
|
| 115 |
assert max(trace_conventional.v[0] - trace_GSL.v[0]) < max_difference / mV, ( |
| 116 |
"difference between conventional and GSL output is larger than max_difference" |
| 117 |
) |
| 118 |
|
| 119 |
|
| 120 |
@pytest.mark.standalone_compatible |
| 121 |
@skip_if_not_implemented |
| 122 |
def test_GSL_user_defined_function(): |
| 123 |
# phase_locking example with user_defined sin |
| 124 |
eqs = """ |
| 125 |
dv/dt = (-v + a * sin(2 * pi * freq * t) + b) / tau : 1 |
| 126 |
a : 1 |
| 127 |
""" |
| 128 |
|
| 129 |
@implementation( |
| 130 |
"cpp", |
| 131 |
""" |
| 132 |
double user_sin(double phase) |
| 133 |
{ |
| 134 |
return sin(phase); |
| 135 |
} |
| 136 |
""", |
| 137 |
) |
| 138 |
@implementation( |
| 139 |
"cython", |
| 140 |
""" |
| 141 |
cdef double user_sin(double phase): |
| 142 |
return sin(phase) |
| 143 |
""", |
| 144 |
) |
| 145 |
@check_units(phase=1, result=1) |
| 146 |
def user_sin(phase): |
| 147 |
raise Exception |
| 148 |
|
| 149 |
tau = 20 * ms |
| 150 |
n = 100 |
| 151 |
b = 1.2 # constant current mean, the modulation varies |
| 152 |
freq = 10 * Hz |
| 153 |
eqs = """ |
| 154 |
dv/dt = (-v + a * user_sin(2 * pi * freq * t) + b) / tau : 1 |
| 155 |
a : 1 |
| 156 |
""" |
| 157 |
vrand = rand(n) |
| 158 |
neurons_conventional = NeuronGroup( |
| 159 |
n, model=eqs, threshold="v > 1", reset="v = 0", method="exponential_euler" |
| 160 |
) |
| 161 |
neurons_GSL = NeuronGroup( |
| 162 |
n, model=eqs, threshold="v > 1", reset="v = 0", method="gsl" |
| 163 |
) |
| 164 |
neurons_conventional.v = vrand |
| 165 |
neurons_GSL.v = vrand |
| 166 |
neurons_conventional.a = "0.05 + 0.7*i/n" |
| 167 |
neurons_GSL.a = "0.05 + 0.7*i/n" |
| 168 |
|
| 169 |
trace_conventional = StateMonitor(neurons_conventional, "v", record=50) |
| 170 |
trace_GSL = StateMonitor(neurons_GSL, "v", record=50) |
| 171 |
net = Network(neurons_conventional, neurons_GSL, trace_conventional, trace_GSL) |
| 172 |
net.run(10 * ms) |
| 173 |
|
| 174 |
assert max(trace_conventional.v[0] - trace_GSL.v[0]) < max_difference / mV, ( |
| 175 |
"difference between conventional and GSL output is larger than max_difference" |
| 176 |
) |
| 177 |
# assert not all(trace_conventional.v[0]==trace_GSL.v[0]), \ |
| 178 |
# ('output of GSL stateupdater is exactly the same as Brians stateupdater (unlikely to be right)') |
| 179 |
|
| 180 |
|
| 181 |
@pytest.mark.standalone_compatible |
| 182 |
@skip_if_not_implemented |
| 183 |
def test_GSL_x_variable(): |
| 184 |
neurons = NeuronGroup( |
| 185 |
2, "dx/dt = 300*Hz : 1", threshold="x>1", reset="x=0", method="gsl" |
| 186 |
) |
| 187 |
# just testing compilation |
| 188 |
run(0 * ms) |
| 189 |
|
| 190 |
|
| 191 |
@pytest.mark.codegen_independent |
| 192 |
def test_GSL_failing_directory(): |
| 193 |
def set_dir(arg): |
| 194 |
prefs.GSL.directory = arg |
| 195 |
|
| 196 |
with pytest.raises(PreferenceError): |
| 197 |
set_dir(1) |
| 198 |
with pytest.raises(PreferenceError): |
| 199 |
set_dir("/usr/") |
| 200 |
with pytest.raises(PreferenceError): |
| 201 |
set_dir("/usr/blablabla/") |
| 202 |
|
| 203 |
|
| 204 |
@pytest.mark.codegen_independent |
| 205 |
@skip_if_not_implemented |
| 206 |
def test_GSL_stochastic(): |
| 207 |
tau = 20 * ms |
| 208 |
sigma = 0.015 |
| 209 |
eqs = """ |
| 210 |
dx/dt = (1.1 - x) / tau + sigma * (2 / tau)**.5 * xi : 1 |
| 211 |
""" |
| 212 |
neuron = NeuronGroup(1, eqs, method="gsl") |
| 213 |
net = Network(neuron) |
| 214 |
with pytest.raises(BrianObjectException) as exc: |
| 215 |
net.run(0 * ms, namespace={"tau": tau, "sigma": sigma}) |
| 216 |
assert exc_isinstance( |
| 217 |
exc, UnsupportedEquationsException, raise_not_implemented=True |
| 218 |
) |
| 219 |
|
| 220 |
|
| 221 |
@pytest.mark.standalone_compatible |
| 222 |
@skip_if_not_implemented |
| 223 |
def test_GSL_error_dimension_mismatch_unit(): |
| 224 |
eqs = """ |
| 225 |
dv/dt = (v0 - v)/(10*ms) : volt |
| 226 |
v0 : volt |
| 227 |
""" |
| 228 |
options = {"absolute_error_per_variable": {"v": 1 * nS}} |
| 229 |
neuron = NeuronGroup( |
| 230 |
1, |
| 231 |
eqs, |
| 232 |
threshold="v > 10*mV", |
| 233 |
reset="v = 0*mV", |
| 234 |
method="gsl", |
| 235 |
method_options=options, |
| 236 |
) |
| 237 |
net = Network(neuron) |
| 238 |
with pytest.raises(BrianObjectException) as exc: |
| 239 |
net.run(0 * ms, namespace={}) |
| 240 |
assert exc_isinstance(exc, DimensionMismatchError, raise_not_implemented=True) |
| 241 |
|
| 242 |
|
| 243 |
@pytest.mark.standalone_compatible |
| 244 |
@skip_if_not_implemented |
| 245 |
def test_GSL_error_dimension_mismatch_dimensionless1(): |
| 246 |
eqs = """ |
| 247 |
dv/dt = (v0 - v)/(10*ms) : 1 |
| 248 |
v0 : 1 |
| 249 |
""" |
| 250 |
options = {"absolute_error_per_variable": {"v": 1 * mV}} |
| 251 |
neuron = NeuronGroup( |
| 252 |
1, eqs, threshold="v > 10", reset="v = 0", method="gsl", method_options=options |
| 253 |
) |
| 254 |
net = Network(neuron) |
| 255 |
with pytest.raises(BrianObjectException) as exc: |
| 256 |
net.run(0 * ms, namespace={}) |
| 257 |
assert exc_isinstance(exc, DimensionMismatchError, raise_not_implemented=True) |
| 258 |
|
| 259 |
|
| 260 |
@pytest.mark.standalone_compatible |
| 261 |
@skip_if_not_implemented |
| 262 |
def test_GSL_error_dimension_mismatch_dimensionless2(): |
| 263 |
eqs = """ |
| 264 |
dv/dt = (v0 - v)/(10*ms) : volt |
| 265 |
v0 : volt |
| 266 |
""" |
| 267 |
options = {"absolute_error_per_variable": {"v": 1e-3}} |
| 268 |
neuron = NeuronGroup( |
| 269 |
1, |
| 270 |
eqs, |
| 271 |
threshold="v > 10*mV", |
| 272 |
reset="v = 0*mV", |
| 273 |
method="gsl", |
| 274 |
method_options=options, |
| 275 |
) |
| 276 |
net = Network(neuron) |
| 277 |
with pytest.raises(BrianObjectException) as exc: |
| 278 |
net.run(0 * ms, namespace={}) |
| 279 |
assert exc_isinstance(exc, DimensionMismatchError, raise_not_implemented=True) |
| 280 |
|
| 281 |
|
| 282 |
@pytest.mark.standalone_compatible |
| 283 |
@skip_if_not_implemented |
| 284 |
def test_GSL_error_nonexisting_variable(): |
| 285 |
eqs = """ |
| 286 |
dv/dt = (v0 - v)/(10*ms) : volt |
| 287 |
v0 : volt |
| 288 |
""" |
| 289 |
options = {"absolute_error_per_variable": {"dummy": 1e-3 * mV}} |
| 290 |
neuron = NeuronGroup( |
| 291 |
1, |
| 292 |
eqs, |
| 293 |
threshold="v > 10*mV", |
| 294 |
reset="v = 0*mV", |
| 295 |
method="gsl", |
| 296 |
method_options=options, |
| 297 |
) |
| 298 |
net = Network(neuron) |
| 299 |
with pytest.raises(BrianObjectException) as exc: |
| 300 |
net.run(0 * ms, namespace={}) |
| 301 |
assert exc_isinstance(exc, KeyError, raise_not_implemented=True) |
| 302 |
|
| 303 |
|
| 304 |
@pytest.mark.standalone_compatible |
| 305 |
@skip_if_not_implemented |
| 306 |
def test_GSL_error_incorrect_error_format(): |
| 307 |
eqs = """ |
| 308 |
dv/dt = (v0 - v)/(10*ms) : volt |
| 309 |
v0 : volt |
| 310 |
""" |
| 311 |
options = {"absolute_error_per_variable": object()} |
| 312 |
neuron = NeuronGroup( |
| 313 |
1, |
| 314 |
eqs, |
| 315 |
threshold="v > 10*mV", |
| 316 |
reset="v = 0*mV", |
| 317 |
method="gsl", |
| 318 |
method_options=options, |
| 319 |
) |
| 320 |
net = Network(neuron) |
| 321 |
options2 = {"absolute_error": "not a float"} |
| 322 |
neuron2 = NeuronGroup( |
| 323 |
1, |
| 324 |
eqs, |
| 325 |
threshold="v > 10*mV", |
| 326 |
reset="v = 0*mV", |
| 327 |
method="gsl", |
| 328 |
method_options=options2, |
| 329 |
) |
| 330 |
net2 = Network(neuron2) |
| 331 |
with pytest.raises(BrianObjectException) as exc: |
| 332 |
net.run(0 * ms, namespace={}) |
| 333 |
assert exc_isinstance(exc, TypeError, raise_not_implemented=True) |
| 334 |
with pytest.raises(BrianObjectException) as exc: |
| 335 |
net2.run(0 * ms, namespace={}) |
| 336 |
assert exc_isinstance(exc, TypeError, raise_not_implemented=True) |
| 337 |
|
| 338 |
|
| 339 |
@pytest.mark.standalone_compatible |
| 340 |
@skip_if_not_implemented |
| 341 |
def test_GSL_error_nonODE_variable(): |
| 342 |
eqs = """ |
| 343 |
dv/dt = (v0 - v)/(10*ms) : volt |
| 344 |
v0 : volt |
| 345 |
""" |
| 346 |
options = {"absolute_error_per_variable": {"v0": 1e-3 * mV}} |
| 347 |
neuron = NeuronGroup( |
| 348 |
1, |
| 349 |
eqs, |
| 350 |
threshold="v > 10*mV", |
| 351 |
reset="v = 0*mV", |
| 352 |
method="gsl", |
| 353 |
method_options=options, |
| 354 |
) |
| 355 |
net = Network(neuron) |
| 356 |
with pytest.raises(BrianObjectException) as exc: |
| 357 |
net.run(0 * ms, namespace={}) |
| 358 |
assert exc_isinstance(exc, KeyError, raise_not_implemented=True) |
| 359 |
|
| 360 |
|
| 361 |
@pytest.mark.standalone_compatible |
| 362 |
@skip_if_not_implemented |
| 363 |
def test_GSL_error_bounds(): |
| 364 |
runtime = 50 * ms |
| 365 |
error1 = 1e-2 * volt |
| 366 |
error2 = 1e-4 * volt |
| 367 |
error3 = 1e-6 * volt # default error |
| 368 |
eqs = """ |
| 369 |
dv/dt = (stimulus(t) + -v)/(.1*ms) : volt |
| 370 |
""" |
| 371 |
stimulus = TimedArray(rand(int(runtime / (10 * ms))) * 3 * volt, dt=5 * ms) |
| 372 |
neuron1 = NeuronGroup( |
| 373 |
1, |
| 374 |
model=eqs, |
| 375 |
reset="v=0*mV", |
| 376 |
threshold="v>10*volt", |
| 377 |
method="gsl", |
| 378 |
method_options={"absolute_error_per_variable": {"v": error1}}, |
| 379 |
dt=1 * ms, |
| 380 |
) |
| 381 |
neuron2 = NeuronGroup( |
| 382 |
1, |
| 383 |
model=eqs, |
| 384 |
reset="v=0*mV", |
| 385 |
threshold="v>10*volt", |
| 386 |
method="gsl", |
| 387 |
method_options={"absolute_error_per_variable": {"v": error2}}, |
| 388 |
dt=1 * ms, |
| 389 |
) |
| 390 |
neuron3 = NeuronGroup( |
| 391 |
1, |
| 392 |
model=eqs, |
| 393 |
reset="v=0*mV", |
| 394 |
threshold="v>10*volt", |
| 395 |
method="gsl", |
| 396 |
method_options={"absolute_error_per_variable": {}}, |
| 397 |
dt=1 * ms, |
| 398 |
) # Uses default error |
| 399 |
neuron_control = NeuronGroup(1, model=eqs, method="linear", dt=1 * ms) |
| 400 |
mon1 = StateMonitor(neuron1, "v", record=True) |
| 401 |
mon2 = StateMonitor(neuron2, "v", record=True) |
| 402 |
mon3 = StateMonitor(neuron3, "v", record=True) |
| 403 |
mon_control = StateMonitor(neuron_control, "v", record=True) |
| 404 |
run(runtime) |
| 405 |
err1 = abs(mon1.v[0] - mon_control.v[0]) |
| 406 |
err2 = abs(mon2.v[0] - mon_control.v[0]) |
| 407 |
err3 = abs(mon3.v[0] - mon_control.v[0]) |
| 408 |
assert max(err1) < error1, ( |
| 409 |
f"Error bound exceeded, error bound: {error1:e}, obtained error: {max(err1):e}" |
| 410 |
) |
| 411 |
assert max(err2) < error2, "Error bound exceeded" |
| 412 |
assert max(err3) < error3, "Error bound exceeded" |
| 413 |
assert max(err1) > max(err2), ( |
| 414 |
"The simulation with smaller error bound produced a bigger maximum error" |
| 415 |
) |
| 416 |
assert max(err2) > max(err3), ( |
| 417 |
"The simulation with smaller error bound produced a bigger maximum error" |
| 418 |
) |
| 419 |
|
| 420 |
|
| 421 |
@pytest.mark.standalone_compatible |
| 422 |
@skip_if_not_implemented |
| 423 |
def test_GSL_non_autonomous(): |
| 424 |
eqs = """dv/dt = sin(2*pi*freq*t)/ms : 1 |
| 425 |
freq : Hz""" |
| 426 |
neuron = NeuronGroup(10, eqs, method="gsl") |
| 427 |
neuron.freq = "i*10*Hz + 10*Hz" |
| 428 |
neuron2 = NeuronGroup(10, eqs, method="euler") |
| 429 |
neuron2.freq = "i*10*Hz + 10*Hz" |
| 430 |
mon = StateMonitor(neuron, "v", record=True) |
| 431 |
mon2 = StateMonitor(neuron2, "v", record=True) |
| 432 |
run(20 * ms) |
| 433 |
abs_err = np.abs(mon.v.T - mon2.v.T) |
| 434 |
max_allowed = 1000 * np.finfo(prefs.core.default_float_dtype).eps |
| 435 |
assert np.max(abs_err) < max_allowed |
| 436 |
|
| 437 |
|
| 438 |
@pytest.mark.standalone_compatible |
| 439 |
@skip_if_not_implemented |
| 440 |
def test_GSL_non_autonomous(): |
| 441 |
eqs = """dv/dt = sin(2*pi*freq*t)/ms : 1 |
| 442 |
freq : Hz""" |
| 443 |
neuron = NeuronGroup(10, eqs, method="gsl") |
| 444 |
neuron.freq = "i*10*Hz + 10*Hz" |
| 445 |
neuron2 = NeuronGroup(10, eqs, method="euler") |
| 446 |
neuron2.freq = "i*10*Hz + 10*Hz" |
| 447 |
mon = StateMonitor(neuron, "v", record=True) |
| 448 |
mon2 = StateMonitor(neuron2, "v", record=True) |
| 449 |
run(20 * ms) |
| 450 |
abs_err = np.abs(mon.v.T - mon2.v.T) |
| 451 |
max_allowed = 1000 * np.finfo(prefs.core.default_float_dtype).eps |
| 452 |
assert np.max(abs_err) < max_allowed |
| 453 |
|
| 454 |
|
| 455 |
@pytest.mark.standalone_compatible |
| 456 |
@skip_if_not_implemented |
| 457 |
def test_GSL_refractory(): |
| 458 |
eqs = """dv/dt = 99.99*Hz : 1 (unless refractory)""" |
| 459 |
neuron = NeuronGroup( |
| 460 |
1, eqs, method="gsl", threshold="v>1", reset="v=0", refractory=3 * ms |
| 461 |
) |
| 462 |
neuron2 = NeuronGroup( |
| 463 |
1, eqs, method="euler", threshold="v>1", reset="v=0", refractory=3 * ms |
| 464 |
) |
| 465 |
mon = SpikeMonitor(neuron, "v") |
| 466 |
mon2 = SpikeMonitor(neuron2, "v") |
| 467 |
run(20 * ms) |
| 468 |
assert mon.count[0] == mon2.count[0] |
| 469 |
|
| 470 |
|
| 471 |
@skip_if_not_implemented |
| 472 |
def test_GSL_save_step_count(): |
| 473 |
eqs = """ |
| 474 |
dv/dt = -v/(.1*ms) : volt |
| 475 |
""" |
| 476 |
neuron = NeuronGroup( |
| 477 |
1, model=eqs, method="gsl", method_options={"save_step_count": True}, dt=1 * ms |
| 478 |
) |
| 479 |
run(0 * ms) |
| 480 |
mon = StateMonitor(neuron, "_step_count", record=True, when="end") |
| 481 |
run(10 * ms) |
| 482 |
assert mon._step_count[0][0] > 0, "Monitor did not save GSL step count" |
| 483 |
|
| 484 |
|
| 485 |
HH_namespace = { |
| 486 |
"Cm": 1 * ufarad * cm**-2, |
| 487 |
"gl": 5e-5 * siemens * cm**-2, |
| 488 |
"El": -65 * mV, |
| 489 |
"EK": -90 * mV, |
| 490 |
"ENa": 50 * mV, |
| 491 |
"g_na": 100 * msiemens * cm**-2, |
| 492 |
"g_kd": 30 * msiemens * cm**-2, |
| 493 |
"VT": -63 * mV, |
| 494 |
} |
| 495 |
|
| 496 |
HH_eqs = Equations( |
| 497 |
""" |
| 498 |
dv/dt = (gl*(El-v) - g_na*(m*m*m)*h*(v-ENa) - g_kd*(n*n*n*n)*(v-EK) + I)/Cm : volt |
| 499 |
dm/dt = 0.32*(mV**-1)*(13.*mV-v+VT)/ |
| 500 |
(exp((13.*mV-v+VT)/(4.*mV))-1.)/ms*(1-m)-0.28*(mV**-1)*(v-VT-40.*mV)/ |
| 501 |
(exp((v-VT-40.*mV)/(5.*mV))-1.)/ms*m : 1 |
| 502 |
dn/dt = 0.032*(mV**-1)*(15.*mV-v+VT)/ |
| 503 |
(exp((15.*mV-v+VT)/(5.*mV))-1.)/ms*(1.-n)-.5*exp((10.*mV-v+VT)/(40.*mV))/ms*n : 1 |
| 504 |
dh/dt = 0.128*exp((17.*mV-v+VT)/(18.*mV))/ms*(1.-h)-4./(1+exp((40.*mV-v+VT)/(5.*mV)))/ms*h : 1 |
| 505 |
I : amp/metre**2 |
| 506 |
""" |
| 507 |
) |
| 508 |
|
| 509 |
|
| 510 |
@pytest.mark.standalone_compatible |
| 511 |
@skip_if_not_implemented |
| 512 |
def test_GSL_fixed_timestep_big_dt_small_error(): |
| 513 |
# should raise integration error |
| 514 |
neuron = NeuronGroup( |
| 515 |
1, |
| 516 |
model=HH_eqs, |
| 517 |
threshold="v > -40*mV", |
| 518 |
refractory="v > -40*mV", |
| 519 |
method="gsl", |
| 520 |
method_options={"adaptable_timestep": False, "absolute_error": 1e-12}, |
| 521 |
dt=0.001 * ms, |
| 522 |
namespace=HH_namespace, |
| 523 |
) |
| 524 |
neuron.I = 0.7 * nA / (20000 * umetre**2) |
| 525 |
neuron.v = HH_namespace["El"] |
| 526 |
net = Network(neuron) |
| 527 |
with pytest.raises((BrianObjectException, RuntimeError)): |
| 528 |
net.run(10 * ms) |
| 529 |
|
| 530 |
|
| 531 |
@pytest.mark.codegen_independent |
| 532 |
@skip_if_not_implemented |
| 533 |
def test_GSL_internal_variable(): |
| 534 |
with pytest.raises(SyntaxError): |
| 535 |
Equations("d_p/dt = 300*Hz : 1") |
| 536 |
|
| 537 |
|
| 538 |
@pytest.mark.standalone_compatible |
| 539 |
@skip_if_not_implemented |
| 540 |
def test_GSL_method_options_neurongroup(): |
| 541 |
neuron1 = NeuronGroup( |
| 542 |
1, |
| 543 |
model="dp/dt = 300*Hz : 1", |
| 544 |
method="gsl", |
| 545 |
method_options={"adaptable_timestep": True}, |
| 546 |
) |
| 547 |
neuron2 = NeuronGroup( |
| 548 |
1, |
| 549 |
model="dp/dt = 300*Hz : 1", |
| 550 |
method="gsl", |
| 551 |
method_options={"adaptable_timestep": False}, |
| 552 |
) |
| 553 |
run(0 * ms) |
| 554 |
assert "if (gsl_odeiv2_driver_apply_fixed_step" not in str( |
| 555 |
neuron1.state_updater.codeobj.code |
| 556 |
), "This neuron should not call gsl_odeiv2_driver_apply_fixed_step()" |
| 557 |
assert "if (gsl_odeiv2_driver_apply_fixed_step" in str( |
| 558 |
neuron2.state_updater.codeobj.code |
| 559 |
), "This neuron should call gsl_odeiv2_driver_apply_fixed_step()" |
| 560 |
|
| 561 |
|
| 562 |
@pytest.mark.standalone_compatible |
| 563 |
@skip_if_not_implemented |
| 564 |
def test_GSL_method_options_spatialneuron(): |
| 565 |
morpho = Soma(30 * um) |
| 566 |
eqs = """ |
| 567 |
Im = g * v : amp/meter**2 |
| 568 |
dg/dt = siemens/metre**2/second : siemens/metre**2 |
| 569 |
""" |
| 570 |
neuron1 = SpatialNeuron( |
| 571 |
morphology=morpho, |
| 572 |
model=eqs, |
| 573 |
Cm=1 * uF / cm**2, |
| 574 |
Ri=100 * ohm * cm, |
| 575 |
method="gsl_rkf45", |
| 576 |
method_options={"adaptable_timestep": True}, |
| 577 |
) |
| 578 |
neuron2 = SpatialNeuron( |
| 579 |
morphology=morpho, |
| 580 |
model=eqs, |
| 581 |
Cm=1 * uF / cm**2, |
| 582 |
Ri=100 * ohm * cm, |
| 583 |
method="gsl_rkf45", |
| 584 |
method_options={"adaptable_timestep": False}, |
| 585 |
) |
| 586 |
run(0 * ms) |
| 587 |
assert "if (gsl_odeiv2_driver_apply_fixed_step" not in str( |
| 588 |
neuron1.state_updater.codeobj.code |
| 589 |
), "This neuron should not call gsl_odeiv2_driver_apply_fixed_step()" |
| 590 |
assert "if (gsl_odeiv2_driver_apply_fixed_step" in str( |
| 591 |
neuron2.state_updater.codeobj.code |
| 592 |
), "This neuron should call gsl_odeiv2_driver_apply_fixed_step()" |
| 593 |
|
| 594 |
|
| 595 |
@skip_if_not_implemented |
| 596 |
def test_GSL_method_options_synapses(): |
| 597 |
N = 1000 |
| 598 |
taum = 10 * ms |
| 599 |
taupre = 20 * ms |
| 600 |
taupost = taupre |
| 601 |
Ee = 0 * mV |
| 602 |
vt = -54 * mV |
| 603 |
vr = -60 * mV |
| 604 |
El = -74 * mV |
| 605 |
taue = 5 * ms |
| 606 |
F = 15 * Hz |
| 607 |
gmax = 0.01 |
| 608 |
dApre = 0.01 |
| 609 |
dApost = -dApre * taupre / taupost * 1.05 |
| 610 |
dApost *= gmax |
| 611 |
dApre *= gmax |
| 612 |
eqs_neurons = """ |
| 613 |
dv/dt = (ge * (Ee-vr) + El - v) / taum : volt |
| 614 |
dge/dt = -ge / taue : 1 |
| 615 |
""" |
| 616 |
poisson_input = PoissonGroup(N, rates=F) |
| 617 |
neurons = NeuronGroup( |
| 618 |
1, eqs_neurons, threshold="v>vt", reset="v = vr", method="gsl_rkf45" |
| 619 |
) |
| 620 |
S1 = Synapses( |
| 621 |
poisson_input, |
| 622 |
neurons, |
| 623 |
""" |
| 624 |
w : 1 |
| 625 |
dApre/dt = -Apre / taupre : 1 (clock-driven) |
| 626 |
dApost/dt = -Apost / taupost : 1 (clock-driven) |
| 627 |
""", |
| 628 |
method="gsl_rkf45", |
| 629 |
method_options={"adaptable_timestep": True}, |
| 630 |
) |
| 631 |
S2 = Synapses( |
| 632 |
poisson_input, |
| 633 |
neurons, |
| 634 |
""" |
| 635 |
w : 1 |
| 636 |
dApre/dt = -Apre / taupre : 1 (clock-driven) |
| 637 |
dApost/dt = -Apost / taupost : 1 (clock-driven) |
| 638 |
""", |
| 639 |
method="gsl_rkf45", |
| 640 |
method_options={"adaptable_timestep": False}, |
| 641 |
) |
| 642 |
run(0 * ms) |
| 643 |
assert "if (gsl_odeiv2_driver_apply_fixed_step" not in str( |
| 644 |
S1.state_updater.codeobj.code |
| 645 |
), "This state_updater should not call gsl_odeiv2_driver_apply_fixed_step()" |
| 646 |
assert "if (gsl_odeiv2_driver_apply_fixed_step" in str( |
| 647 |
S2.state_updater.codeobj.code |
| 648 |
), "This state_updater should call gsl_odeiv2_driver_apply_fixed_step()" |
| 649 |
|
| 650 |
|
| 651 |
if __name__ == "__main__": |
| 652 |
from _pytest.outcomes import Skipped |
| 653 |
|
| 654 |
for t in [ |
| 655 |
test_GSL_stateupdater_basic, |
| 656 |
test_GSL_different_clocks, |
| 657 |
test_GSL_default_function, |
| 658 |
test_GSL_user_defined_function, |
| 659 |
test_GSL_x_variable, |
| 660 |
test_GSL_failing_directory, |
| 661 |
test_GSL_stochastic, |
| 662 |
test_GSL_error_dimension_mismatch_unit, |
| 663 |
test_GSL_error_dimension_mismatch_dimensionless1, |
| 664 |
test_GSL_error_dimension_mismatch_dimensionless2, |
| 665 |
test_GSL_error_nonexisting_variable, |
| 666 |
test_GSL_error_incorrect_error_format, |
| 667 |
test_GSL_error_nonODE_variable, |
| 668 |
test_GSL_error_bounds, |
| 669 |
test_GSL_non_autonomous, |
| 670 |
test_GSL_refractory, |
| 671 |
test_GSL_save_step_count, |
| 672 |
test_GSL_fixed_timestep_big_dt_small_error, |
| 673 |
test_GSL_method_options_neurongroup, |
| 674 |
test_GSL_method_options_spatialneuron, |
| 675 |
test_GSL_method_options_synapses, |
| 676 |
]: |
| 677 |
try: |
| 678 |
t() |
| 679 |
except Skipped as ex: |
| 680 |
print(f"Skipped: {t.__name__} ({str(ex)})") |