| 1 |
import itertools |
| 2 |
import os |
| 3 |
|
| 4 |
import pytest |
| 5 |
from numpy.testing import assert_equal |
| 6 |
|
| 7 |
from brian2 import * |
| 8 |
from brian2.devices.device import reinit_and_delete |
| 9 |
from brian2.tests.utils import assert_allclose |
| 10 |
|
| 11 |
try: |
| 12 |
import scipy |
| 13 |
except ImportError: |
| 14 |
scipy = None |
| 15 |
|
| 16 |
|
| 17 |
numpy_needs_scipy = pytest.mark.skipif( |
| 18 |
# Using condition string, since we cannot yet know |
| 19 |
# prefs.codegen.target at module import time |
| 20 |
"prefs.codegen.target == 'numpy' and not scipy", |
| 21 |
reason="multi-compartmental models need scipy to run with numpy", |
| 22 |
) |
| 23 |
|
| 24 |
|
| 25 |
@pytest.mark.codegen_independent |
| 26 |
@numpy_needs_scipy |
| 27 |
def test_custom_events(): |
| 28 |
# Set (could be moved in a setup) |
| 29 |
EL = -65 * mV |
| 30 |
gL = 0.0003 * siemens / cm**2 |
| 31 |
ev = """ |
| 32 |
Im = gL * (EL - v) : amp/meter**2 |
| 33 |
event_time1 : second |
| 34 |
""" |
| 35 |
# Create a three compartments morphology |
| 36 |
morpho = Soma(diameter=10 * um) |
| 37 |
morpho.dend1 = Cylinder(n=1, diameter=1 * um, length=10 * um) |
| 38 |
morpho.dend2 = Cylinder(n=1, diameter=1 * um, length=10 * um) |
| 39 |
G = SpatialNeuron( |
| 40 |
morphology=morpho, model=ev, events={"event1": "t>=i*ms and t<i*ms+dt"} |
| 41 |
) |
| 42 |
G.run_on_event("event1", "event_time1 = 0.1*ms") |
| 43 |
run(0.2 * ms) |
| 44 |
# Event has size three now because there are three compartments |
| 45 |
assert_allclose(G.event_time1[:], [0.1, 0, 0] * ms) |
| 46 |
|
| 47 |
|
| 48 |
@pytest.mark.codegen_independent |
| 49 |
def test_construction(): |
| 50 |
BrianLogger.suppress_name("resolution_conflict") |
| 51 |
morpho = Soma(diameter=30 * um) |
| 52 |
morpho.L = Cylinder(length=10 * um, diameter=1 * um, n=10) |
| 53 |
morpho.LL = Cylinder(length=5 * um, diameter=2 * um, n=5) |
| 54 |
morpho.LR = Cylinder(length=5 * um, diameter=2 * um, n=10) |
| 55 |
morpho.right = Cylinder(length=3 * um, diameter=1 * um, n=7) |
| 56 |
morpho.right.nextone = Cylinder(length=2 * um, diameter=1 * um, n=3) |
| 57 |
gL = 1e-4 * siemens / cm**2 |
| 58 |
EL = -70 * mV |
| 59 |
eqs = """ |
| 60 |
Im=gL*(EL-v) : amp/meter**2 |
| 61 |
I : meter (point current) |
| 62 |
""" |
| 63 |
|
| 64 |
# Check units of currents |
| 65 |
with pytest.raises(DimensionMismatchError): |
| 66 |
SpatialNeuron(morphology=morpho, model=eqs) |
| 67 |
|
| 68 |
eqs = """ |
| 69 |
Im=gL*(EL-v) : amp/meter**2 |
| 70 |
""" |
| 71 |
neuron = SpatialNeuron( |
| 72 |
morphology=morpho, model=eqs, Cm=1 * uF / cm**2, Ri=100 * ohm * cm |
| 73 |
) |
| 74 |
# Test initialization of values |
| 75 |
neuron.LL.v = EL |
| 76 |
assert_allclose(neuron.L.main.v, 0 * mV) |
| 77 |
assert_allclose(neuron.LL.v, EL) |
| 78 |
neuron.LL[1 * um : 3 * um].v = 0 * mV |
| 79 |
assert_allclose(neuron.LL.v, Quantity([EL, 0 * mV, 0 * mV, EL, EL])) |
| 80 |
assert_allclose(neuron.Cm, 1 * uF / cm**2) |
| 81 |
|
| 82 |
# Test morphological variables |
| 83 |
assert_allclose(neuron.L.main.distance, morpho.L.distance) |
| 84 |
assert_allclose(neuron.L.main.area, morpho.L.area) |
| 85 |
assert_allclose(neuron.L.main.length, morpho.L.length) |
| 86 |
assert_allclose(neuron.L.main.volume, morpho.L.volume) |
| 87 |
|
| 88 |
# Check basic consistency of the flattened representation |
| 89 |
assert all( |
| 90 |
neuron.diffusion_state_updater._ends[:].flat |
| 91 |
>= neuron.diffusion_state_updater._starts[:].flat |
| 92 |
) |
| 93 |
|
| 94 |
# Check that length and distances make sense |
| 95 |
assert_allclose(sum(morpho.L.length), 10 * um) |
| 96 |
assert_allclose(morpho.L.distance, (0.5 + np.arange(10)) * um) |
| 97 |
assert_allclose(sum(morpho.LL.length), 5 * um) |
| 98 |
assert_allclose(morpho.LL.distance, (10 + 0.5 + np.arange(5)) * um) |
| 99 |
assert_allclose(sum(morpho.LR.length), 5 * um) |
| 100 |
assert_allclose(morpho.LR.distance, (10 + 0.25 + np.arange(10) * 0.5) * um) |
| 101 |
assert_allclose(sum(morpho.right.length), 3 * um) |
| 102 |
assert_allclose(morpho.right.distance, (0.5 + np.arange(7)) * 3.0 / 7.0 * um) |
| 103 |
assert_allclose(sum(morpho.right.nextone.length), 2 * um) |
| 104 |
assert_allclose( |
| 105 |
morpho.right.nextone.distance, 3 * um + (0.5 + np.arange(3)) * 2.0 / 3.0 * um |
| 106 |
) |
| 107 |
|
| 108 |
|
| 109 |
@pytest.mark.codegen_independent |
| 110 |
def test_construction_coordinates(): |
| 111 |
# Same as test_construction, but uses coordinates instead of lengths to |
| 112 |
# set up everything |
| 113 |
# Note that all coordinates here are relative to the origin of the |
| 114 |
# respective cylinder |
| 115 |
BrianLogger.suppress_name("resolution_conflict") |
| 116 |
morpho = Soma(diameter=30 * um) |
| 117 |
morpho.L = Cylinder(x=[0, 10] * um, diameter=1 * um, n=10) |
| 118 |
morpho.LL = Cylinder(y=[0, 5] * um, diameter=2 * um, n=5) |
| 119 |
morpho.LR = Cylinder(z=[0, 5] * um, diameter=2 * um, n=10) |
| 120 |
morpho.right = Cylinder( |
| 121 |
x=[0, sqrt(2) * 1.5] * um, y=[0, sqrt(2) * 1.5] * um, diameter=1 * um, n=7 |
| 122 |
) |
| 123 |
morpho.right.nextone = Cylinder( |
| 124 |
y=[0, sqrt(2)] * um, z=[0, sqrt(2)] * um, diameter=1 * um, n=3 |
| 125 |
) |
| 126 |
gL = 1e-4 * siemens / cm**2 |
| 127 |
EL = -70 * mV |
| 128 |
eqs = """ |
| 129 |
Im=gL*(EL-v) : amp/meter**2 |
| 130 |
I : meter (point current) |
| 131 |
""" |
| 132 |
|
| 133 |
# Check units of currents |
| 134 |
with pytest.raises(DimensionMismatchError): |
| 135 |
SpatialNeuron(morphology=morpho, model=eqs) |
| 136 |
|
| 137 |
eqs = """ |
| 138 |
Im=gL*(EL-v) : amp/meter**2 |
| 139 |
""" |
| 140 |
neuron = SpatialNeuron( |
| 141 |
morphology=morpho, model=eqs, Cm=1 * uF / cm**2, Ri=100 * ohm * cm |
| 142 |
) |
| 143 |
|
| 144 |
# Test initialization of values |
| 145 |
neuron.LL.v = EL |
| 146 |
assert_allclose(neuron.L.main.v, 0 * mV) |
| 147 |
assert_allclose(neuron.LL.v, EL) |
| 148 |
neuron.LL[1 * um : 3 * um].v = 0 * mV |
| 149 |
assert_allclose(neuron.LL.v, Quantity([EL, 0 * mV, 0 * mV, EL, EL])) |
| 150 |
assert_allclose(neuron.Cm, 1 * uF / cm**2) |
| 151 |
|
| 152 |
# Test morphological variables |
| 153 |
assert_allclose(neuron.L.main.x, morpho.L.x) |
| 154 |
assert_allclose(neuron.LL.main.x, morpho.LL.x) |
| 155 |
assert_allclose(neuron.right.main.x, morpho.right.x) |
| 156 |
assert_allclose(neuron.L.main.distance, morpho.L.distance) |
| 157 |
# assert_allclose(neuron.L.main.diameter, morpho.L.diameter) |
| 158 |
assert_allclose(neuron.L.main.area, morpho.L.area) |
| 159 |
assert_allclose(neuron.L.main.length, morpho.L.length) |
| 160 |
|
| 161 |
# Check basic consistency of the flattened representation |
| 162 |
assert all( |
| 163 |
neuron.diffusion_state_updater._ends[:].flat |
| 164 |
>= neuron.diffusion_state_updater._starts[:].flat |
| 165 |
) |
| 166 |
|
| 167 |
# Check that length and distances make sense |
| 168 |
assert_allclose(sum(morpho.L.length), 10 * um) |
| 169 |
assert_allclose(morpho.L.distance, (0.5 + np.arange(10)) * um) |
| 170 |
assert_allclose(sum(morpho.LL.length), 5 * um) |
| 171 |
assert_allclose(morpho.LL.distance, (10 + 0.5 + np.arange(5)) * um) |
| 172 |
assert_allclose(sum(morpho.LR.length), 5 * um) |
| 173 |
assert_allclose(morpho.LR.distance, (10 + 0.25 + np.arange(10) * 0.5) * um) |
| 174 |
assert_allclose(sum(morpho.right.length), 3 * um) |
| 175 |
assert_allclose(morpho.right.distance, (0.5 + np.arange(7)) * 3.0 / 7.0 * um) |
| 176 |
assert_allclose(sum(morpho.right.nextone.length), 2 * um) |
| 177 |
assert_allclose( |
| 178 |
morpho.right.nextone.distance, 3 * um + (0.5 + np.arange(3)) * 2.0 / 3.0 * um |
| 179 |
) |
| 180 |
|
| 181 |
|
| 182 |
@pytest.mark.long |
| 183 |
@numpy_needs_scipy |
| 184 |
def test_infinitecable(): |
| 185 |
""" |
| 186 |
Test simulation of an infinite cable vs. theory for current pulse (Green function) |
| 187 |
""" |
| 188 |
BrianLogger.suppress_name("resolution_conflict") |
| 189 |
defaultclock.dt = 0.001 * ms |
| 190 |
|
| 191 |
# Morphology |
| 192 |
diameter = 1 * um |
| 193 |
Cm = 1 * uF / cm**2 |
| 194 |
Ri = 100 * ohm * cm |
| 195 |
N = 500 |
| 196 |
morpho = Cylinder(diameter=diameter, length=3 * mm, n=N) |
| 197 |
|
| 198 |
# Passive channels |
| 199 |
gL = 1e-4 * siemens / cm**2 |
| 200 |
eqs = """ |
| 201 |
Im=-gL*v : amp/meter**2 |
| 202 |
I : amp (point current) |
| 203 |
""" |
| 204 |
|
| 205 |
neuron = SpatialNeuron(morphology=morpho, model=eqs, Cm=Cm, Ri=Ri) |
| 206 |
|
| 207 |
# Monitors |
| 208 |
mon = StateMonitor(neuron, "v", record=N / 2 - 20) |
| 209 |
|
| 210 |
neuron.I[len(neuron) // 2] = 1 * nA # injecting in the middle |
| 211 |
run(0.02 * ms) |
| 212 |
neuron.I = 0 * amp |
| 213 |
run(3 * ms) |
| 214 |
t = mon.t |
| 215 |
v = mon[N // 2 - 20].v |
| 216 |
# Theory (incorrect near cable ends) |
| 217 |
x = 20 * morpho.length[0] |
| 218 |
la = neuron.space_constant[0] |
| 219 |
taum = Cm / gL # membrane time constant |
| 220 |
theory = ( |
| 221 |
1.0 |
| 222 |
/ (la * Cm * pi * diameter) |
| 223 |
* sqrt(taum / (4 * pi * (t + defaultclock.dt))) |
| 224 |
* exp( |
| 225 |
-(t + defaultclock.dt) / taum |
| 226 |
- taum / (4 * (t + defaultclock.dt)) * (x / la) ** 2 |
| 227 |
) |
| 228 |
) |
| 229 |
theory = theory * 1 * nA * 0.02 * ms |
| 230 |
assert_allclose( |
| 231 |
v[t > 0.5 * ms], theory[t > 0.5 * ms], atol=float(6.32 * uvolt) |
| 232 |
) # high error tolerance (not exact because not infinite cable) |
| 233 |
|
| 234 |
|
| 235 |
@pytest.mark.standalone_compatible |
| 236 |
@numpy_needs_scipy |
| 237 |
def test_finitecable(): |
| 238 |
""" |
| 239 |
Test simulation of short cylinder vs. theory for constant current. |
| 240 |
""" |
| 241 |
BrianLogger.suppress_name("resolution_conflict") |
| 242 |
|
| 243 |
defaultclock.dt = 0.01 * ms |
| 244 |
|
| 245 |
# Morphology |
| 246 |
diameter = 1 * um |
| 247 |
length = 300 * um |
| 248 |
Cm = 1 * uF / cm**2 |
| 249 |
Ri = 150 * ohm * cm |
| 250 |
N = 200 |
| 251 |
morpho = Cylinder(diameter=diameter, length=length, n=N) |
| 252 |
|
| 253 |
# Passive channels |
| 254 |
gL = 1e-4 * siemens / cm**2 |
| 255 |
EL = -70 * mV |
| 256 |
eqs = """ |
| 257 |
Im=gL*(EL-v) : amp/meter**2 |
| 258 |
I : amp (point current) |
| 259 |
""" |
| 260 |
|
| 261 |
neuron = SpatialNeuron(morphology=morpho, model=eqs, Cm=Cm, Ri=Ri) |
| 262 |
neuron.v = EL |
| 263 |
|
| 264 |
neuron.I[0] = 0.02 * nA # injecting at the left end |
| 265 |
|
| 266 |
run(100 * ms) |
| 267 |
|
| 268 |
# Theory |
| 269 |
x = neuron.distance |
| 270 |
v = neuron.v |
| 271 |
la = neuron.space_constant[0] |
| 272 |
ra = la * 4 * Ri / (pi * diameter**2) |
| 273 |
theory = EL + ra * neuron.I[0] * cosh((length - x) / la) / sinh(length / la) |
| 274 |
assert_allclose(v - EL, theory - EL, atol=1e-6) |
| 275 |
|
| 276 |
|
| 277 |
@pytest.mark.standalone_compatible |
| 278 |
@numpy_needs_scipy |
| 279 |
def test_rallpack1(): |
| 280 |
""" |
| 281 |
Rallpack 1 |
| 282 |
""" |
| 283 |
if prefs.core.default_float_dtype is np.float32: |
| 284 |
pytest.skip("Need double precision for this test") |
| 285 |
defaultclock.dt = 0.05 * ms |
| 286 |
|
| 287 |
# Morphology |
| 288 |
diameter = 1 * um |
| 289 |
length = 1 * mm |
| 290 |
Cm = 1 * uF / cm**2 |
| 291 |
Ri = 100 * ohm * cm |
| 292 |
N = 1000 |
| 293 |
morpho = Cylinder(diameter=diameter, length=length, n=N) |
| 294 |
|
| 295 |
# Passive channels |
| 296 |
gL = 1.0 / (40000 * ohm * cm**2) |
| 297 |
EL = -65 * mV |
| 298 |
eqs = """ |
| 299 |
Im = gL*(EL - v) : amp/meter**2 |
| 300 |
I : amp (point current, constant) |
| 301 |
""" |
| 302 |
neuron = SpatialNeuron(morphology=morpho, model=eqs, Cm=Cm, Ri=Ri) |
| 303 |
neuron.v = EL |
| 304 |
|
| 305 |
neuron.I[0] = 0.1 * nA # injecting at the left end |
| 306 |
|
| 307 |
# Record at the two ends |
| 308 |
mon = StateMonitor(neuron, "v", record=[0, 999], when="start", dt=0.05 * ms) |
| 309 |
|
| 310 |
run(250 * ms + defaultclock.dt) |
| 311 |
|
| 312 |
# Load the theoretical results |
| 313 |
basedir = os.path.join(os.path.abspath(os.path.dirname(__file__)), "rallpack_data") |
| 314 |
data_0 = np.loadtxt(os.path.join(basedir, "ref_cable.0")) |
| 315 |
data_x = np.loadtxt(os.path.join(basedir, "ref_cable.x")) |
| 316 |
|
| 317 |
scale_0 = max(data_0[:, 1] * volt) - min(data_0[:, 1] * volt) |
| 318 |
scale_x = max(data_x[:, 1] * volt) - min(data_x[:, 1] * volt) |
| 319 |
squared_diff_0 = (data_0[:, 1] * volt - mon[0].v) ** 2 |
| 320 |
squared_diff_x = (data_x[:, 1] * volt - mon[999].v) ** 2 |
| 321 |
rel_RMS_0 = sqrt(mean(squared_diff_0)) / scale_0 |
| 322 |
rel_RMS_x = sqrt(mean(squared_diff_x)) / scale_x |
| 323 |
max_rel_0 = sqrt(max(squared_diff_0)) / scale_0 |
| 324 |
max_rel_x = sqrt(max(squared_diff_x)) / scale_x |
| 325 |
|
| 326 |
# sanity check: times are the same |
| 327 |
assert_allclose(mon.t / second, data_0[:, 0]) |
| 328 |
assert_allclose(mon.t / second, data_x[:, 0]) |
| 329 |
|
| 330 |
# RMS error should be < 0.1%, maximum error along the curve should be < 0.5% |
| 331 |
assert 100 * rel_RMS_0 < 0.1 |
| 332 |
assert 100 * rel_RMS_x < 0.1 |
| 333 |
assert 100 * max_rel_0 < 0.5 |
| 334 |
assert 100 * max_rel_x < 0.5 |
| 335 |
|
| 336 |
|
| 337 |
@pytest.mark.standalone_compatible |
| 338 |
@numpy_needs_scipy |
| 339 |
def test_rallpack2(): |
| 340 |
""" |
| 341 |
Rallpack 2 |
| 342 |
""" |
| 343 |
if prefs.core.default_float_dtype is np.float32: |
| 344 |
pytest.skip("Need double precision for this test") |
| 345 |
defaultclock.dt = 0.1 * ms |
| 346 |
|
| 347 |
# Morphology |
| 348 |
diameter = 32 * um |
| 349 |
length = 16 * um |
| 350 |
Cm = 1 * uF / cm**2 |
| 351 |
Ri = 100 * ohm * cm |
| 352 |
|
| 353 |
# Construct binary tree according to Rall's formula |
| 354 |
morpho = Cylinder(n=1, diameter=diameter, y=[0, float(length)] * meter) |
| 355 |
endpoints = {morpho} |
| 356 |
for depth in range(1, 10): |
| 357 |
diameter /= 2.0 ** (1.0 / 3.0) |
| 358 |
length /= 2.0 ** (2.0 / 3.0) |
| 359 |
new_endpoints = set() |
| 360 |
for endpoint in endpoints: |
| 361 |
new_L = Cylinder(n=1, diameter=diameter, length=length) |
| 362 |
new_R = Cylinder(n=1, diameter=diameter, length=length) |
| 363 |
new_endpoints.add(new_L) |
| 364 |
new_endpoints.add(new_R) |
| 365 |
endpoint.L = new_L |
| 366 |
endpoint.R = new_R |
| 367 |
endpoints = new_endpoints |
| 368 |
|
| 369 |
# Passive channels |
| 370 |
gL = 1.0 / (40000 * ohm * cm**2) |
| 371 |
EL = -65 * mV |
| 372 |
eqs = """ |
| 373 |
Im = gL*(EL - v) : amp/meter**2 |
| 374 |
I : amp (point current, constant) |
| 375 |
""" |
| 376 |
neuron = SpatialNeuron(morphology=morpho, model=eqs, Cm=Cm, Ri=Ri, method="rk4") |
| 377 |
neuron.v = EL |
| 378 |
|
| 379 |
neuron.I[0] = 0.1 * nA # injecting at the origin |
| 380 |
|
| 381 |
endpoint_indices = [endpoint.indices[0] for endpoint in endpoints] |
| 382 |
mon = StateMonitor( |
| 383 |
neuron, "v", record=[0] + endpoint_indices, when="start", dt=0.1 * ms |
| 384 |
) |
| 385 |
|
| 386 |
run(250 * ms + defaultclock.dt) |
| 387 |
|
| 388 |
# Load the theoretical results |
| 389 |
basedir = os.path.join(os.path.abspath(os.path.dirname(__file__)), "rallpack_data") |
| 390 |
# Only use very second time step, since we run with 0.1ms instead of 0.05ms |
| 391 |
data_0 = np.loadtxt(os.path.join(basedir, "ref_branch.0"))[::2] |
| 392 |
data_x = np.loadtxt(os.path.join(basedir, "ref_branch.x"))[::2] |
| 393 |
|
| 394 |
# sanity check: times are the same |
| 395 |
assert_allclose(mon.t / second, data_0[:, 0]) |
| 396 |
assert_allclose(mon.t / second, data_x[:, 0]) |
| 397 |
|
| 398 |
# Check that all endpoints are the same: |
| 399 |
for endpoint in endpoints: |
| 400 |
assert_allclose(mon[endpoint].v, mon[endpoint[0]].v) |
| 401 |
|
| 402 |
scale_0 = max(data_0[:, 1] * volt) - min(data_0[:, 1] * volt) |
| 403 |
scale_x = max(data_x[:, 1] * volt) - min(data_x[:, 1] * volt) |
| 404 |
squared_diff_0 = (data_0[:, 1] * volt - mon[0].v) ** 2 |
| 405 |
|
| 406 |
# One endpoint |
| 407 |
squared_diff_x = (data_x[:, 1] * volt - mon[endpoint_indices[0]].v) ** 2 |
| 408 |
rel_RMS_0 = sqrt(mean(squared_diff_0)) / scale_0 |
| 409 |
rel_RMS_x = sqrt(mean(squared_diff_x)) / scale_x |
| 410 |
max_rel_0 = sqrt(max(squared_diff_0)) / scale_0 |
| 411 |
max_rel_x = sqrt(max(squared_diff_x)) / scale_x |
| 412 |
|
| 413 |
# RMS error should be < 0.25%, maximum error along the curve should be < 0.5% |
| 414 |
assert 100 * rel_RMS_0 < 0.25 |
| 415 |
assert 100 * rel_RMS_x < 0.25 |
| 416 |
assert 100 * max_rel_0 < 0.5 |
| 417 |
assert 100 * max_rel_x < 0.5 |
| 418 |
|
| 419 |
|
| 420 |
@pytest.mark.standalone_compatible |
| 421 |
@pytest.mark.long |
| 422 |
@numpy_needs_scipy |
| 423 |
def test_rallpack3(): |
| 424 |
""" |
| 425 |
Rallpack 3 |
| 426 |
""" |
| 427 |
if prefs.core.default_float_dtype is np.float32: |
| 428 |
pytest.skip("Need double precision for this test") |
| 429 |
defaultclock.dt = 1 * usecond |
| 430 |
|
| 431 |
# Morphology |
| 432 |
diameter = 1 * um |
| 433 |
length = 1 * mm |
| 434 |
N = 1000 |
| 435 |
morpho = Cylinder(diameter=diameter, length=length, n=N) |
| 436 |
# Passive properties |
| 437 |
gl = 1.0 / (40000 * ohm * cm**2) |
| 438 |
El = -65 * mV |
| 439 |
Cm = 1 * uF / cm**2 |
| 440 |
Ri = 100 * ohm * cm |
| 441 |
# Active properties |
| 442 |
ENa = 50 * mV |
| 443 |
EK = -77 * mV |
| 444 |
gNa = 120 * msiemens / cm**2 |
| 445 |
gK = 36 * msiemens / cm**2 |
| 446 |
eqs = """ |
| 447 |
Im = gl * (El-v) + gNa * m**3 * h * (ENa-v) + gK * n**4 * (EK-v) : amp/meter**2 |
| 448 |
dm/dt = alpham * (1-m) - betam * m : 1 |
| 449 |
dn/dt = alphan * (1-n) - betan * n : 1 |
| 450 |
dh/dt = alphah * (1-h) - betah * h : 1 |
| 451 |
v_shifted = v - El : volt |
| 452 |
alpham = (0.1/mV) * (-v_shifted+25*mV) / (exp((-v_shifted+25*mV) / (10*mV)) - 1)/ms : Hz |
| 453 |
betam = 4 * exp(-v_shifted/(18*mV))/ms : Hz |
| 454 |
alphah = 0.07 * exp(-v_shifted/(20*mV))/ms : Hz |
| 455 |
betah = 1/(exp((-v_shifted+30*mV) / (10*mV)) + 1)/ms : Hz |
| 456 |
alphan = (0.01/mV) * (-v_shifted+10*mV) / (exp((-v_shifted+10*mV) / (10*mV)) - 1)/ms : Hz |
| 457 |
betan = 0.125*exp(-v_shifted/(80*mV))/ms : Hz |
| 458 |
I : amp (point current, constant) |
| 459 |
""" |
| 460 |
axon = SpatialNeuron( |
| 461 |
morphology=morpho, model=eqs, Cm=Cm, Ri=Ri, method="exponential_euler" |
| 462 |
) |
| 463 |
axon.v = El |
| 464 |
# Pre-calculated equilibrium values at v = El |
| 465 |
axon.m = 0.0529324852572 |
| 466 |
axon.n = 0.317676914061 |
| 467 |
axon.h = 0.596120753508 |
| 468 |
axon.I[0] = 0.1 * nA # injecting at the left end |
| 469 |
|
| 470 |
# Record at the two ends |
| 471 |
mon = StateMonitor(axon, "v", record=[0, 999], when="start", dt=0.05 * ms) |
| 472 |
|
| 473 |
run(250 * ms + defaultclock.dt) |
| 474 |
|
| 475 |
# Load the theoretical results |
| 476 |
basedir = os.path.join(os.path.abspath(os.path.dirname(__file__)), "rallpack_data") |
| 477 |
data_0 = np.loadtxt(os.path.join(basedir, "ref_axon.0.neuron")) |
| 478 |
data_x = np.loadtxt(os.path.join(basedir, "ref_axon.x.neuron")) |
| 479 |
|
| 480 |
# sanity check: times are the same |
| 481 |
assert_allclose(mon.t / second, data_0[:, 0]) |
| 482 |
assert_allclose(mon.t / second, data_x[:, 0]) |
| 483 |
|
| 484 |
scale_0 = max(data_0[:, 1] * volt) - min(data_0[:, 1] * volt) |
| 485 |
scale_x = max(data_x[:, 1] * volt) - min(data_x[:, 1] * volt) |
| 486 |
squared_diff_0 = (data_0[:, 1] * volt - mon[0].v) ** 2 |
| 487 |
squared_diff_x = (data_x[:, 1] * volt - mon[999].v) ** 2 |
| 488 |
|
| 489 |
rel_RMS_0 = sqrt(mean(squared_diff_0)) / scale_0 |
| 490 |
rel_RMS_x = sqrt(mean(squared_diff_x)) / scale_x |
| 491 |
max_rel_0 = sqrt(max(squared_diff_0)) / scale_0 |
| 492 |
max_rel_x = sqrt(max(squared_diff_x)) / scale_x |
| 493 |
|
| 494 |
# RMS error should be < 0.1%, maximum error along the curve should be < 0.5% |
| 495 |
# Note that this is much stricter than the original Rallpack evaluation, but |
| 496 |
# with the 1us time step, the voltage traces are extremely similar |
| 497 |
assert 100 * rel_RMS_0 < 0.1 |
| 498 |
assert 100 * rel_RMS_x < 0.1 |
| 499 |
assert 100 * max_rel_0 < 0.5 |
| 500 |
assert 100 * max_rel_x < 0.5 |
| 501 |
|
| 502 |
|
| 503 |
@pytest.mark.standalone_compatible |
| 504 |
@numpy_needs_scipy |
| 505 |
def test_rall(): |
| 506 |
""" |
| 507 |
Test simulation of a cylinder plus two branches, with diameters according to Rall's formula |
| 508 |
""" |
| 509 |
BrianLogger.suppress_name("resolution_conflict") |
| 510 |
|
| 511 |
defaultclock.dt = 0.01 * ms |
| 512 |
|
| 513 |
# Passive channels |
| 514 |
gL = 1e-4 * siemens / cm**2 |
| 515 |
EL = -70 * mV |
| 516 |
|
| 517 |
# Morphology |
| 518 |
diameter = 1 * um |
| 519 |
length = 300 * um |
| 520 |
Cm = 1 * uF / cm**2 |
| 521 |
Ri = 150 * ohm * cm |
| 522 |
N = 500 |
| 523 |
rm = 1 / (gL * pi * diameter) # membrane resistance per unit length |
| 524 |
ra = (4 * Ri) / (pi * diameter**2) # axial resistance per unit length |
| 525 |
la = sqrt(rm / ra) # space length |
| 526 |
morpho = Cylinder(diameter=diameter, length=length, n=N) |
| 527 |
d1 = 0.5 * um |
| 528 |
L1 = 200 * um |
| 529 |
rm = 1 / (gL * pi * d1) # membrane resistance per unit length |
| 530 |
ra = (4 * Ri) / (pi * d1**2) # axial resistance per unit length |
| 531 |
l1 = sqrt(rm / ra) # space length |
| 532 |
morpho.L = Cylinder(diameter=d1, length=L1, n=N) |
| 533 |
d2 = (diameter**1.5 - d1**1.5) ** (1.0 / 1.5) |
| 534 |
rm = 1 / (gL * pi * d2) # membrane resistance per unit length |
| 535 |
ra = (4 * Ri) / (pi * d2**2) # axial resistance per unit length |
| 536 |
l2 = sqrt(rm / ra) # space length |
| 537 |
L2 = (L1 / l1) * l2 |
| 538 |
morpho.R = Cylinder(diameter=d2, length=L2, n=N) |
| 539 |
|
| 540 |
eqs = """ |
| 541 |
Im=gL*(EL-v) : amp/meter**2 |
| 542 |
I : amp (point current) |
| 543 |
""" |
| 544 |
|
| 545 |
neuron = SpatialNeuron(morphology=morpho, model=eqs, Cm=Cm, Ri=Ri) |
| 546 |
neuron.v = EL |
| 547 |
|
| 548 |
neuron.I[0] = 0.02 * nA # injecting at the left end |
| 549 |
run(100 * ms) |
| 550 |
|
| 551 |
# Check space constant calculation |
| 552 |
assert_allclose(la, neuron.space_constant[0]) |
| 553 |
assert_allclose(l1, neuron.L.space_constant[0]) |
| 554 |
assert_allclose(l2, neuron.R.space_constant[0]) |
| 555 |
|
| 556 |
# Theory |
| 557 |
x = neuron.main.distance |
| 558 |
ra = la * 4 * Ri / (pi * diameter**2) |
| 559 |
l = length / la + L1 / l1 |
| 560 |
theory = EL + ra * neuron.I[0] * cosh(l - x / la) / sinh(l) |
| 561 |
v = neuron.main.v |
| 562 |
assert_allclose(v - EL, theory - EL, atol=2e-6) |
| 563 |
x = neuron.L.distance |
| 564 |
theory = EL + ra * neuron.I[0] * cosh( |
| 565 |
l - neuron.main.distance[-1] / la - (x - neuron.main.distance[-1]) / l1 |
| 566 |
) / sinh(l) |
| 567 |
v = neuron.L.v |
| 568 |
assert_allclose(v - EL, theory - EL, atol=2e-6) |
| 569 |
x = neuron.R.distance |
| 570 |
theory = EL + ra * neuron.I[0] * cosh( |
| 571 |
l - neuron.main.distance[-1] / la - (x - neuron.main.distance[-1]) / l2 |
| 572 |
) / sinh(l) |
| 573 |
v = neuron.R.v |
| 574 |
assert_allclose(v - EL, theory - EL, atol=2e-6) |
| 575 |
|
| 576 |
|
| 577 |
@pytest.mark.standalone_compatible |
| 578 |
@numpy_needs_scipy |
| 579 |
def test_basic_diffusion(): |
| 580 |
# A very basic test that shows that propagation is working in a very basic |
| 581 |
# sense, testing all morphological classes |
| 582 |
|
| 583 |
defaultclock.dt = 0.01 * ms |
| 584 |
|
| 585 |
EL = -70 * mV |
| 586 |
gL = 1e-4 * siemens / cm**2 |
| 587 |
target = -10 * mV |
| 588 |
eqs = """ |
| 589 |
Im = gL*(EL-v) + gClamp*(target-v): amp/meter**2 |
| 590 |
gClamp : siemens/meter**2 |
| 591 |
""" |
| 592 |
|
| 593 |
morph = Soma(diameter=30 * um) |
| 594 |
morph.axon = Cylinder(n=10, diameter=10 * um, length=100 * um) |
| 595 |
morph.dend = Section( |
| 596 |
n=10, |
| 597 |
diameter=[10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0.1] * um, |
| 598 |
length=np.ones(10) * 10 * um, |
| 599 |
) |
| 600 |
|
| 601 |
neuron = SpatialNeuron(morph, eqs) |
| 602 |
neuron.v = EL |
| 603 |
neuron.axon.gClamp[0] = 100 * siemens / cm**2 |
| 604 |
|
| 605 |
mon = StateMonitor(neuron, "v", record=True) |
| 606 |
|
| 607 |
run(0.25 * ms) |
| 608 |
assert all(abs(mon.v[:, -1] / mV + 10) < 0.25), mon.v[:, -1] / mV |
| 609 |
|
| 610 |
|
| 611 |
@pytest.mark.codegen_independent |
| 612 |
def test_allowed_integration(): |
| 613 |
morph = Soma(diameter=30 * um) |
| 614 |
EL = -70 * mV |
| 615 |
gL = 1e-4 * siemens / cm**2 |
| 616 |
ENa = 115 * mV |
| 617 |
gNa = 120 * msiemens / cm**2 |
| 618 |
VT = -50.4 * mV |
| 619 |
DeltaT = 2 * mV |
| 620 |
ENMDA = 0.0 * mV |
| 621 |
|
| 622 |
@check_units(voltage=volt, result=volt) |
| 623 |
def user_fun(voltage): |
| 624 |
return voltage # could be an arbitrary function and is therefore unsafe |
| 625 |
|
| 626 |
allowed_eqs = [ |
| 627 |
"Im = gL*(EL-v) : amp/meter**2", |
| 628 |
""" |
| 629 |
Im = gl * (El-v) + gNa * m**3 * h * (ENa-v) : amp/meter**2 |
| 630 |
dm/dt = alpham * (1-m) - betam * m : 1 |
| 631 |
dh/dt = alphah * (1-h) - betah * h : 1 |
| 632 |
alpham = (0.1/mV) * (-v+25*mV) / (exp((-v+25*mV) / (10*mV)) - 1)/ms : Hz |
| 633 |
betam = 4 * exp(-v/(18*mV))/ms : Hz |
| 634 |
alphah = 0.07 * exp(-v/(20*mV))/ms : Hz |
| 635 |
betah = 1/(exp((-v+30*mV) / (10*mV)) + 1)/ms : Hz |
| 636 |
""", |
| 637 |
""" |
| 638 |
Im = gl * (El-v) : amp/meter**2 |
| 639 |
I_ext = 1*nA + sin(2*pi*100*Hz*t)*nA : amp (point current) |
| 640 |
""", |
| 641 |
""" |
| 642 |
Im = I_leak + I_spike : amp/meter**2 |
| 643 |
I_leak = gL*(EL - v) : amp/meter**2 |
| 644 |
I_spike = gL*DeltaT*exp((v - VT)/DeltaT): amp/meter**2 (constant over dt) |
| 645 |
""", |
| 646 |
""" |
| 647 |
Im = gL*(EL-v) : amp/meter**2 |
| 648 |
I_NMDA = gNMDA*(ENMDA-v)*Mgblock : amp (point current) |
| 649 |
gNMDA : siemens |
| 650 |
Mgblock = 1./(1. + exp(-0.062*v/mV)/3.57) : 1 (constant over dt) |
| 651 |
""", |
| 652 |
"Im = gL*(EL - v) + gL*DeltaT*exp((v - VT)/DeltaT) : amp/meter**2", |
| 653 |
""" |
| 654 |
Im = I_leak + I_spike : amp/meter**2 |
| 655 |
I_leak = gL*(EL - v) : amp/meter**2 |
| 656 |
I_spike = gL*DeltaT*exp((v - VT)/DeltaT): amp/meter**2 |
| 657 |
""", |
| 658 |
""" |
| 659 |
Im = gL*(EL-v) : amp/meter**2 |
| 660 |
I_NMDA = gNMDA*(ENMDA-v)*Mgblock : amp (point current) |
| 661 |
gNMDA : siemens |
| 662 |
Mgblock = 1./(1. + exp(-0.062*v/mV)/3.57) : 1 |
| 663 |
""", |
| 664 |
] |
| 665 |
forbidden_eqs = [ |
| 666 |
"""Im = gl * (El-v + user_fun(v)) : amp/meter**2""", |
| 667 |
"""Im = gl * clip(El-v, -100*mV, 100*mV) : amp/meter**2""", |
| 668 |
] |
| 669 |
for eqs in allowed_eqs: |
| 670 |
# Should not raise an error |
| 671 |
neuron = SpatialNeuron(morph, eqs) |
| 672 |
|
| 673 |
for eqs in forbidden_eqs: |
| 674 |
# Should raise an error |
| 675 |
with pytest.raises(TypeError): |
| 676 |
SpatialNeuron(morph, eqs) |
| 677 |
|
| 678 |
|
| 679 |
@pytest.mark.codegen_independent |
| 680 |
def test_spatialneuron_indexing(): |
| 681 |
sec = Cylinder(length=50 * um, diameter=10 * um, n=1) |
| 682 |
sec.sec1 = Cylinder(length=50 * um, diameter=10 * um, n=2) |
| 683 |
sec.sec1.sec11 = Cylinder(length=50 * um, diameter=10 * um, n=4) |
| 684 |
sec.sec1.sec12 = Cylinder(length=50 * um, diameter=10 * um, n=8) |
| 685 |
sec.sec2 = Cylinder(length=50 * um, diameter=10 * um, n=16) |
| 686 |
sec.sec2.sec21 = Cylinder(length=50 * um, diameter=10 * um, n=32) |
| 687 |
neuron = SpatialNeuron(sec, "Im = 0*amp/meter**2 : amp/meter**2") |
| 688 |
neuron.v = "i*volt" |
| 689 |
# Accessing indices/variables of a subtree refers to the full subtree |
| 690 |
assert len(neuron.indices[:]) == 1 + 2 + 4 + 8 + 16 + 32 |
| 691 |
assert len(neuron.sec1.indices[:]) == 2 + 4 + 8 |
| 692 |
assert len(neuron.sec1.sec11.indices[:]) == 4 |
| 693 |
assert len(neuron.sec1.sec12.indices[:]) == 8 |
| 694 |
assert len(neuron.sec2.indices[:]) == 16 + 32 |
| 695 |
assert len(neuron.sec2.sec21.indices[:]) == 32 |
| 696 |
assert len(neuron.v[:]) == 1 + 2 + 4 + 8 + 16 + 32 |
| 697 |
assert len(neuron.sec1.v[:]) == 2 + 4 + 8 |
| 698 |
assert len(neuron.sec1.sec11.v[:]) == 4 |
| 699 |
assert len(neuron.sec1.sec12.v[:]) == 8 |
| 700 |
assert len(neuron.sec2.v[:]) == 16 + 32 |
| 701 |
assert len(neuron.sec2.sec21.v[:]) == 32 |
| 702 |
# Accessing indices/variables with ".main" only refers to the section |
| 703 |
assert len(neuron.main.indices[:]) == 1 |
| 704 |
assert len(neuron.sec1.main.indices[:]) == 2 |
| 705 |
assert len(neuron.sec1.sec11.main.indices[:]) == 4 |
| 706 |
assert len(neuron.sec1.sec12.main.indices[:]) == 8 |
| 707 |
assert len(neuron.sec2.main.indices[:]) == 16 |
| 708 |
assert len(neuron.sec2.sec21.main.indices[:]) == 32 |
| 709 |
assert len(neuron.main.v[:]) == 1 |
| 710 |
assert len(neuron.sec1.main.v[:]) == 2 |
| 711 |
assert len(neuron.sec1.sec11.main.v[:]) == 4 |
| 712 |
assert len(neuron.sec1.sec12.main.v[:]) == 8 |
| 713 |
assert len(neuron.sec2.main.v[:]) == 16 |
| 714 |
assert len(neuron.sec2.sec21.main.v[:]) == 32 |
| 715 |
# Accessing subgroups |
| 716 |
assert len(neuron[0].indices[:]) == 1 |
| 717 |
assert len(neuron[0 * um : 50 * um].indices[:]) == 1 |
| 718 |
assert len(neuron[0:1].indices[:]) == 1 |
| 719 |
assert len(neuron[sec.sec2.indices[:]]) == 16 |
| 720 |
assert len(neuron[sec.sec2]) == 16 |
| 721 |
assert_equal(neuron.sec1.sec11.v, [3, 4, 5, 6] * volt) |
| 722 |
assert_equal(neuron.sec1.sec11[1].v, neuron.sec1.sec11.v[1]) |
| 723 |
assert_equal(neuron.sec1.sec11[1:3].v, neuron.sec1.sec11.v[1:3]) |
| 724 |
assert_equal(neuron.sec1.sec11[1:3].v, [4, 5] * volt) |
| 725 |
|
| 726 |
|
| 727 |
@pytest.mark.codegen_independent |
| 728 |
def test_tree_index_consistency(): |
| 729 |
# Test all possible trees with depth 3 and a maximum of 3 branches subtree |
| 730 |
# (a total of 84 trees) |
| 731 |
# This tests whether the indices (i.e. where the compartments are placed in |
| 732 |
# the overall flattened 1D structure) make sense: for the `SpatialSubgroup` |
| 733 |
# mechanism to work correctly, each subtree has to have contiguous indices. |
| 734 |
# Separate subtrees should of course have non-overlapping indices. |
| 735 |
for tree_description in itertools.product( |
| 736 |
[1, 2, 3], # children of root |
| 737 |
[0, 1, 2, 3], # children of first branch |
| 738 |
[0, 1, 2, 3], # children of second branch |
| 739 |
[0, 1, 2, 3], # children of third branch |
| 740 |
): |
| 741 |
sec = Cylinder(length=50 * um, diameter=10 * um, n=1) |
| 742 |
root_children = tree_description[0] |
| 743 |
if not all([tree_description[x] == 0 for x in range(root_children + 1, 4)]): |
| 744 |
# skip redundant descriptions (differing number of branches in a |
| 745 |
# subtree that does not exist) |
| 746 |
continue |
| 747 |
|
| 748 |
# Create a tree according to the description |
| 749 |
for idx in range(root_children): |
| 750 |
setattr( |
| 751 |
sec, |
| 752 |
f"sec{int(idx + 1)}", |
| 753 |
Cylinder(length=50 * um, diameter=10 * um, n=2 * (idx + 1)), |
| 754 |
) |
| 755 |
for child in range(root_children): |
| 756 |
subsec = getattr(sec, f"sec{int(child + 1)}") |
| 757 |
subsec_children = tree_description[child + 1] |
| 758 |
for idx in range(subsec_children): |
| 759 |
setattr( |
| 760 |
subsec, |
| 761 |
f"sec{int(child + 1)}{int(idx + 1)}", |
| 762 |
Cylinder(length=50 * um, diameter=10 * um, n=1 + (child + 1) * idx), |
| 763 |
) |
| 764 |
|
| 765 |
neuron = SpatialNeuron(sec, "Im = 0*amp/meter**2 : amp/meter**2") |
| 766 |
# Check the indicies for the full neuron: |
| 767 |
assert_equal(neuron.indices[:], np.arange(sec.total_compartments)) |
| 768 |
|
| 769 |
all_subsec_indices = [] |
| 770 |
for child in range(root_children): |
| 771 |
subsec = getattr(neuron, f"sec{int(child + 1)}") |
| 772 |
sub_indices = set(subsec.main.indices[:]) |
| 773 |
subsec_children = tree_description[child + 1] |
| 774 |
for idx in range(subsec_children): |
| 775 |
subsubsec = getattr(subsec, f"sec{int(child + 1)}{int(idx + 1)}") |
| 776 |
sub_indices |= set(subsubsec.main.indices[:]) |
| 777 |
# The indices for a full subtree should be the union of the indices |
| 778 |
# for all subsections within that subtree |
| 779 |
assert sub_indices == set(subsec.indices[:]) |
| 780 |
all_subsec_indices.extend(subsec.indices[:]) |
| 781 |
# Separate subtrees should not overlap |
| 782 |
assert len(all_subsec_indices) == len(set(all_subsec_indices)) |
| 783 |
|
| 784 |
|
| 785 |
@pytest.mark.codegen_independent |
| 786 |
def test_spatialneuron_subtree_assignment(): |
| 787 |
sec = Cylinder(length=50 * um, diameter=10 * um, n=2) |
| 788 |
sec.sec1 = Cylinder(length=50 * um, diameter=10 * um, n=2) |
| 789 |
sec.sec1.sec11 = Cylinder(length=50 * um, diameter=10 * um, n=2) |
| 790 |
sec.sec1.sec12 = Cylinder(length=50 * um, diameter=10 * um, n=2) |
| 791 |
sec.sec2 = Cylinder(length=50 * um, diameter=10 * um, n=2) |
| 792 |
sec.sec2.sec21 = Cylinder(length=50 * um, diameter=10 * um, n=2) |
| 793 |
neuron = SpatialNeuron(sec, "Im = 0*amp/meter**2 : amp/meter**2") |
| 794 |
|
| 795 |
neuron.v = 1 * volt |
| 796 |
assert_allclose(neuron.v[:], np.ones(12) * volt) |
| 797 |
neuron.sec1.v += 1 * volt |
| 798 |
assert_allclose(neuron.main.v[:], np.ones(2) * volt) |
| 799 |
assert_allclose(neuron.sec1.v[:], np.ones(6) * 2 * volt) |
| 800 |
assert_allclose(neuron.sec1.main.v[:], np.ones(2) * 2 * volt) |
| 801 |
assert_allclose(neuron.sec1.sec11.v[:], np.ones(2) * 2 * volt) |
| 802 |
assert_allclose(neuron.sec1.sec12.v[:], np.ones(2) * 2 * volt) |
| 803 |
assert_allclose(neuron.sec2.v[:], np.ones(4) * volt) |
| 804 |
neuron.sec2.v = 5 * volt |
| 805 |
assert_allclose(neuron.sec2.v[:], np.ones(4) * 5 * volt) |
| 806 |
assert_allclose(neuron.sec2.main.v[:], np.ones(2) * 5 * volt) |
| 807 |
assert_allclose(neuron.sec2.sec21.v[:], np.ones(2) * 5 * volt) |
| 808 |
|
| 809 |
|
| 810 |
@pytest.mark.codegen_independent |
| 811 |
def test_spatialneuron_morphology_assignment(): |
| 812 |
sec = Cylinder(length=50 * um, diameter=10 * um, n=2) |
| 813 |
sec.sec1 = Cylinder(length=50 * um, diameter=10 * um, n=2) |
| 814 |
sec.sec1.sec11 = Cylinder(length=50 * um, diameter=10 * um, n=2) |
| 815 |
sec.sec1.sec12 = Cylinder(length=50 * um, diameter=10 * um, n=2) |
| 816 |
sec.sec2 = Cylinder(length=50 * um, diameter=10 * um, n=2) |
| 817 |
sec.sec2.sec21 = Cylinder(length=50 * um, diameter=10 * um, n=2) |
| 818 |
neuron = SpatialNeuron(sec, "Im = 0*amp/meter**2 : amp/meter**2") |
| 819 |
|
| 820 |
neuron.v[sec.sec1.sec11] = 1 * volt |
| 821 |
assert_allclose(neuron.sec1.sec11.v[:], np.ones(2) * volt) |
| 822 |
assert_allclose(neuron.sec1.sec12.v[:], np.zeros(2) * volt) |
| 823 |
assert_allclose(neuron.sec1.main.v[:], np.zeros(2) * volt) |
| 824 |
assert_allclose(neuron.main.v[:], np.zeros(2) * volt) |
| 825 |
assert_allclose(neuron.sec2.v[:], np.zeros(4) * volt) |
| 826 |
|
| 827 |
neuron.v[sec.sec2[25 * um :]] = 2 * volt |
| 828 |
neuron.v[sec.sec1[: 25 * um]] = 3 * volt |
| 829 |
assert_allclose(neuron.main.v[:], np.zeros(2) * volt) |
| 830 |
assert_allclose(neuron.sec2.main.v[:], [0, 2] * volt) |
| 831 |
assert_allclose(neuron.sec2.sec21.v[:], np.zeros(2) * volt) |
| 832 |
assert_allclose(neuron.sec1.main.v[:], [3, 0] * volt) |
| 833 |
assert_allclose(neuron.sec1.sec11.v[:], np.ones(2) * volt) |
| 834 |
assert_allclose(neuron.sec1.sec12.v[:], np.zeros(2) * volt) |
| 835 |
|
| 836 |
|
| 837 |
@pytest.mark.standalone_compatible |
| 838 |
@pytest.mark.multiple_runs |
| 839 |
@numpy_needs_scipy |
| 840 |
def test_spatialneuron_capacitive_currents(): |
| 841 |
if prefs.core.default_float_dtype is np.float32: |
| 842 |
pytest.skip("Need double precision for this test") |
| 843 |
defaultclock.dt = 0.1 * ms |
| 844 |
morpho = Cylinder(x=[0, 10] * cm, diameter=2 * 238 * um, n=200, type="axon") |
| 845 |
|
| 846 |
El = 10.613 * mV |
| 847 |
ENa = 115 * mV |
| 848 |
EK = -12 * mV |
| 849 |
gl = 0.3 * msiemens / cm**2 |
| 850 |
gNa0 = 120 * msiemens / cm**2 |
| 851 |
gK = 36 * msiemens / cm**2 |
| 852 |
|
| 853 |
# Typical equations |
| 854 |
eqs = """ |
| 855 |
# The same equations for the whole neuron, but possibly different parameter values |
| 856 |
# distributed transmembrane current |
| 857 |
Im = gl * (El-v) + gNa * m**3 * h * (ENa-v) + gK * n**4 * (EK-v) : amp/meter**2 |
| 858 |
I : amp (point current) # applied current |
| 859 |
dm/dt = alpham * (1-m) - betam * m : 1 |
| 860 |
dn/dt = alphan * (1-n) - betan * n : 1 |
| 861 |
dh/dt = alphah * (1-h) - betah * h : 1 |
| 862 |
alpham = (0.1/mV) * (-v+25*mV) / (exp((-v+25*mV) / (10*mV)) - 1)/ms : Hz |
| 863 |
betam = 4 * exp(-v/(18*mV))/ms : Hz |
| 864 |
alphah = 0.07 * exp(-v/(20*mV))/ms : Hz |
| 865 |
betah = 1/(exp((-v+30*mV) / (10*mV)) + 1)/ms : Hz |
| 866 |
alphan = (0.01/mV) * (-v+10*mV) / (exp((-v+10*mV) / (10*mV)) - 1)/ms : Hz |
| 867 |
betan = 0.125*exp(-v/(80*mV))/ms : Hz |
| 868 |
gNa : siemens/meter**2 |
| 869 |
""" |
| 870 |
|
| 871 |
neuron = SpatialNeuron( |
| 872 |
morphology=morpho, |
| 873 |
model=eqs, |
| 874 |
Cm=1 * uF / cm**2, |
| 875 |
Ri=35.4 * ohm * cm, |
| 876 |
method="exponential_euler", |
| 877 |
) |
| 878 |
mon = StateMonitor(neuron, ["Im", "Ic"], record=True, when="end") |
| 879 |
run(10 * ms) |
| 880 |
neuron.I[0] = 1 * uA # current injection at one end |
| 881 |
run(3 * ms) |
| 882 |
neuron.I = 0 * amp |
| 883 |
run(10 * ms) |
| 884 |
device.build(direct_call=False, **device.build_options) |
| 885 |
assert_allclose( |
| 886 |
(mon.Im - mon.Ic).sum(axis=0) / (mA / cm**2), np.zeros(230), atol=1e6 |
| 887 |
) |
| 888 |
|
| 889 |
|
| 890 |
@pytest.mark.codegen_independent |
| 891 |
def test_point_current(): |
| 892 |
soma = Soma(10 * um) |
| 893 |
eqs = """Im = 0*nA/cm**2 : amp/meter**2 |
| 894 |
I1 = 1*nA : amp (point current) |
| 895 |
I2 = 1*nA : amp (point current, constant over dt)""" |
| 896 |
neuron = SpatialNeuron(soma, eqs) |
| 897 |
assert "I1/area" in neuron.equations["Im"].expr.code |
| 898 |
assert "I2/area" in neuron.equations["Im"].expr.code # see issue #1160 |
| 899 |
|
| 900 |
|
| 901 |
@pytest.mark.standalone_compatible |
| 902 |
@pytest.mark.multiple_runs |
| 903 |
@numpy_needs_scipy |
| 904 |
def test_spatialneuron_threshold_location(): |
| 905 |
morpho = Soma(10 * um) |
| 906 |
morpho.axon = Cylinder(1 * um, n=2, length=20 * um) |
| 907 |
model = """ |
| 908 |
Im = 0*nA/cm**2 : amp/meter**2 |
| 909 |
should_spike : boolean (constant) |
| 910 |
""" |
| 911 |
neuron = SpatialNeuron( |
| 912 |
morpho, model, threshold_location=morpho.axon[15 * um], threshold="should_spike" |
| 913 |
) |
| 914 |
# Different variants that should do the same thing |
| 915 |
neuron2 = SpatialNeuron( |
| 916 |
morpho, |
| 917 |
model, |
| 918 |
threshold_location=morpho.axon.indices[15 * um], |
| 919 |
threshold="should_spike", |
| 920 |
) |
| 921 |
neuron3 = SpatialNeuron( |
| 922 |
morpho, model, threshold_location=2, threshold="should_spike" |
| 923 |
) |
| 924 |
# Cannot use multiple compartments |
| 925 |
with pytest.raises(AttributeError): |
| 926 |
SpatialNeuron( |
| 927 |
morpho, model, threshold_location=[2, 3], threshold="should_spike" |
| 928 |
) |
| 929 |
with pytest.raises(AttributeError): |
| 930 |
SpatialNeuron( |
| 931 |
morpho, |
| 932 |
model, |
| 933 |
threshold_location=morpho.axon[5 * um : 15 * um], |
| 934 |
threshold="should_spike", |
| 935 |
) |
| 936 |
neurons = [neuron, neuron2, neuron3] |
| 937 |
monitors = [SpikeMonitor(n) for n in neurons] |
| 938 |
|
| 939 |
net = Network(neurons, monitors) |
| 940 |
for n in neurons: |
| 941 |
n.should_spike = True # all compartments want to spike |
| 942 |
net.run(defaultclock.dt) |
| 943 |
for n in neurons: |
| 944 |
n.should_spike = False # no compartment wants to spike |
| 945 |
net.run(defaultclock.dt) |
| 946 |
for n in neurons: |
| 947 |
n.should_spike = [False, False, True] |
| 948 |
net.run(defaultclock.dt) |
| 949 |
for n in neurons: |
| 950 |
n.should_spike = [True, True, False] |
| 951 |
net.run(defaultclock.dt) |
| 952 |
device.build(direct_call=False, **device.build_options) |
| 953 |
for mon in monitors: |
| 954 |
assert len(mon.i) == 2 |
| 955 |
assert all(mon.i == 2) |
| 956 |
assert_allclose(mon.t, [0 * ms, 2 * defaultclock.dt]) |
| 957 |
|
| 958 |
|
| 959 |
@pytest.mark.standalone_compatible |
| 960 |
@numpy_needs_scipy |
| 961 |
def test_spatialneuron_timedarray(): |
| 962 |
# See GitHub issue 1427 |
| 963 |
ta = TimedArray([0, 1] * nA, dt=1 * ms) |
| 964 |
morpho = Soma(diameter=10 * um) |
| 965 |
neuron = SpatialNeuron(morpho, "Im = ta(t)/area : amp/meter**2", method="euler") |
| 966 |
mon = StateMonitor(neuron, "v", record=0, when="after_groups") |
| 967 |
run(2 * ms) |
| 968 |
assert_allclose( |
| 969 |
np.diff(mon.v_[0]), |
| 970 |
np.r_[ |
| 971 |
np.zeros(9), |
| 972 |
np.array( |
| 973 |
np.ones(10) * 1 * nA / neuron.area[0] / neuron.Cm * defaultclock.dt |
| 974 |
), |
| 975 |
], |
| 976 |
) |
| 977 |
|
| 978 |
|
| 979 |
if __name__ == "__main__": |
| 980 |
test_custom_events() |
| 981 |
test_construction() |
| 982 |
test_construction_coordinates() |
| 983 |
test_infinitecable() |
| 984 |
test_finitecable() |
| 985 |
test_rallpack1() |
| 986 |
test_rallpack2() |
| 987 |
test_rallpack3() |
| 988 |
test_rall() |
| 989 |
test_basic_diffusion() |
| 990 |
test_allowed_integration() |
| 991 |
test_spatialneuron_indexing() |
| 992 |
test_tree_index_consistency() |
| 993 |
test_spatialneuron_subtree_assignment() |
| 994 |
test_spatialneuron_morphology_assignment() |
| 995 |
test_spatialneuron_capacitive_currents() |
| 996 |
test_spatialneuron_timedarray() |