| 1 |
import sys |
| 2 |
|
| 3 |
import numpy as np |
| 4 |
|
| 5 |
try: |
| 6 |
from IPython.lib.pretty import pprint |
| 7 |
except ImportError: |
| 8 |
pprint = None |
| 9 |
import pytest |
| 10 |
|
| 11 |
from brian2 import Equations, Expression, Hz, Unit, farad, metre, ms, mV, second, volt |
| 12 |
from brian2.core.namespace import DEFAULT_UNITS |
| 13 |
from brian2.equations.equations import ( |
| 14 |
BOOLEAN, |
| 15 |
DIFFERENTIAL_EQUATION, |
| 16 |
FLOAT, |
| 17 |
INTEGER, |
| 18 |
PARAMETER, |
| 19 |
SUBEXPRESSION, |
| 20 |
EquationError, |
| 21 |
SingleEquation, |
| 22 |
check_identifier_basic, |
| 23 |
check_identifier_constants, |
| 24 |
check_identifier_functions, |
| 25 |
check_identifier_reserved, |
| 26 |
check_identifier_units, |
| 27 |
dimensions_and_type_from_string, |
| 28 |
extract_constant_subexpressions, |
| 29 |
parse_string_equations, |
| 30 |
) |
| 31 |
from brian2.equations.refractory import check_identifier_refractory |
| 32 |
from brian2.groups.group import Group |
| 33 |
from brian2.units.fundamentalunits import ( |
| 34 |
DIMENSIONLESS, |
| 35 |
DimensionMismatchError, |
| 36 |
get_dimensions, |
| 37 |
) |
| 38 |
|
| 39 |
|
| 40 |
# a simple Group for testing |
| 41 |
class SimpleGroup(Group): |
| 42 |
def __init__(self, variables, namespace=None): |
| 43 |
self.variables = variables |
| 44 |
self.namespace = namespace |
| 45 |
|
| 46 |
|
| 47 |
@pytest.mark.codegen_independent |
| 48 |
def test_utility_functions(): |
| 49 |
unit_namespace = DEFAULT_UNITS |
| 50 |
|
| 51 |
# Some simple tests whether the namespace returned by |
| 52 |
# get_default_namespace() makes sense |
| 53 |
assert "volt" in unit_namespace |
| 54 |
assert "ms" in unit_namespace |
| 55 |
assert unit_namespace["ms"] is ms |
| 56 |
assert unit_namespace["ms"] is unit_namespace["msecond"] |
| 57 |
for unit in unit_namespace.values(): |
| 58 |
assert isinstance(unit, Unit) |
| 59 |
|
| 60 |
assert dimensions_and_type_from_string("second") == (second.dim, FLOAT) |
| 61 |
assert dimensions_and_type_from_string("1") == (DIMENSIONLESS, FLOAT) |
| 62 |
assert dimensions_and_type_from_string("volt") == (volt.dim, FLOAT) |
| 63 |
assert dimensions_and_type_from_string("second ** -1") == (Hz.dim, FLOAT) |
| 64 |
assert dimensions_and_type_from_string("farad / metre**2") == ( |
| 65 |
(farad / metre**2).dim, |
| 66 |
FLOAT, |
| 67 |
) |
| 68 |
assert dimensions_and_type_from_string("boolean") == (DIMENSIONLESS, BOOLEAN) |
| 69 |
assert dimensions_and_type_from_string("integer") == (DIMENSIONLESS, INTEGER) |
| 70 |
with pytest.raises(ValueError): |
| 71 |
dimensions_and_type_from_string("metr / second") |
| 72 |
with pytest.raises(ValueError): |
| 73 |
dimensions_and_type_from_string("metre **") |
| 74 |
with pytest.raises(ValueError): |
| 75 |
dimensions_and_type_from_string("5") |
| 76 |
with pytest.raises(ValueError): |
| 77 |
dimensions_and_type_from_string("2 / second") |
| 78 |
# Only the use of base units is allowed |
| 79 |
with pytest.raises(ValueError): |
| 80 |
dimensions_and_type_from_string("farad / cm**2") |
| 81 |
|
| 82 |
|
| 83 |
@pytest.mark.codegen_independent |
| 84 |
def test_identifier_checks(): |
| 85 |
legal_identifiers = ["v", "Vm", "V", "x", "ge", "g_i", "a2", "gaba_123"] |
| 86 |
illegal_identifiers = ["_v", "1v", "ΓΌ", "ge!", "v.x", "for", "else", "if"] |
| 87 |
|
| 88 |
for identifier in legal_identifiers: |
| 89 |
try: |
| 90 |
check_identifier_basic(identifier) |
| 91 |
check_identifier_reserved(identifier) |
| 92 |
except ValueError as ex: |
| 93 |
raise AssertionError( |
| 94 |
f'check complained about identifier "{identifier}": {ex}' |
| 95 |
) |
| 96 |
|
| 97 |
for identifier in illegal_identifiers: |
| 98 |
with pytest.raises(SyntaxError): |
| 99 |
check_identifier_basic(identifier) |
| 100 |
|
| 101 |
for identifier in ("t", "dt", "xi", "i", "N"): |
| 102 |
with pytest.raises(SyntaxError): |
| 103 |
check_identifier_reserved(identifier) |
| 104 |
|
| 105 |
for identifier in ("not_refractory", "refractory", "refractory_until"): |
| 106 |
with pytest.raises(SyntaxError): |
| 107 |
check_identifier_refractory(identifier) |
| 108 |
|
| 109 |
for identifier in ("exp", "sin", "sqrt"): |
| 110 |
with pytest.raises(SyntaxError): |
| 111 |
check_identifier_functions(identifier) |
| 112 |
|
| 113 |
for identifier in ("e", "pi", "inf"): |
| 114 |
with pytest.raises(SyntaxError): |
| 115 |
check_identifier_constants(identifier) |
| 116 |
|
| 117 |
for identifier in ("volt", "second", "mV", "nA"): |
| 118 |
with pytest.raises(SyntaxError): |
| 119 |
check_identifier_units(identifier) |
| 120 |
|
| 121 |
# Check identifier registry |
| 122 |
assert check_identifier_basic in Equations.identifier_checks |
| 123 |
assert check_identifier_reserved in Equations.identifier_checks |
| 124 |
assert check_identifier_refractory in Equations.identifier_checks |
| 125 |
assert check_identifier_functions in Equations.identifier_checks |
| 126 |
assert check_identifier_constants in Equations.identifier_checks |
| 127 |
assert check_identifier_units in Equations.identifier_checks |
| 128 |
|
| 129 |
# Set up a dummy identifier check that disallows the variable name |
| 130 |
# gaba_123 (that is otherwise valid) |
| 131 |
def disallow_gaba_123(identifier): |
| 132 |
if identifier == "gaba_123": |
| 133 |
raise SyntaxError("I do not like this name") |
| 134 |
|
| 135 |
Equations.check_identifier("gaba_123") |
| 136 |
old_checks = set(Equations.identifier_checks) |
| 137 |
Equations.register_identifier_check(disallow_gaba_123) |
| 138 |
with pytest.raises(SyntaxError): |
| 139 |
Equations.check_identifier("gaba_123") |
| 140 |
Equations.identifier_checks = old_checks |
| 141 |
|
| 142 |
# registering a non-function should not work |
| 143 |
with pytest.raises(ValueError): |
| 144 |
Equations.register_identifier_check("no function") |
| 145 |
|
| 146 |
|
| 147 |
@pytest.mark.codegen_independent |
| 148 |
def test_parse_equations(): |
| 149 |
"""Test the parsing of equation strings""" |
| 150 |
# A simple equation |
| 151 |
eqs = parse_string_equations("dv/dt = -v / tau : 1") |
| 152 |
assert len(eqs) == 1 and "v" in eqs and eqs["v"].type == DIFFERENTIAL_EQUATION |
| 153 |
assert eqs["v"].dim is DIMENSIONLESS |
| 154 |
|
| 155 |
# A complex one |
| 156 |
eqs = parse_string_equations( |
| 157 |
""" |
| 158 |
dv/dt = -(v + |
| 159 |
ge + # excitatory conductance |
| 160 |
I # external current |
| 161 |
)/ tau : volt |
| 162 |
dge/dt = -ge / tau_ge : volt |
| 163 |
I = sin(2 * pi * f * t) : volt |
| 164 |
f : Hz (constant) |
| 165 |
b : boolean |
| 166 |
n : integer |
| 167 |
""" |
| 168 |
) |
| 169 |
assert len(eqs) == 6 |
| 170 |
assert "v" in eqs and eqs["v"].type == DIFFERENTIAL_EQUATION |
| 171 |
assert "ge" in eqs and eqs["ge"].type == DIFFERENTIAL_EQUATION |
| 172 |
assert "I" in eqs and eqs["I"].type == SUBEXPRESSION |
| 173 |
assert "f" in eqs and eqs["f"].type == PARAMETER |
| 174 |
assert "b" in eqs and eqs["b"].type == PARAMETER |
| 175 |
assert "n" in eqs and eqs["n"].type == PARAMETER |
| 176 |
assert eqs["f"].var_type == FLOAT |
| 177 |
assert eqs["b"].var_type == BOOLEAN |
| 178 |
assert eqs["n"].var_type == INTEGER |
| 179 |
assert eqs["v"].dim is volt.dim |
| 180 |
assert eqs["ge"].dim is volt.dim |
| 181 |
assert eqs["I"].dim is volt.dim |
| 182 |
assert eqs["f"].dim is Hz.dim |
| 183 |
assert eqs["v"].flags == [] |
| 184 |
assert eqs["ge"].flags == [] |
| 185 |
assert eqs["I"].flags == [] |
| 186 |
assert eqs["f"].flags == ["constant"] |
| 187 |
|
| 188 |
duplicate_eqs = """ |
| 189 |
dv/dt = -v / tau : 1 |
| 190 |
v = 2 * t : 1 |
| 191 |
""" |
| 192 |
with pytest.raises(EquationError): |
| 193 |
parse_string_equations(duplicate_eqs) |
| 194 |
parse_error_eqs = [ |
| 195 |
""" |
| 196 |
dv/d = -v / tau : 1 |
| 197 |
x = 2 * t : 1 |
| 198 |
""", |
| 199 |
""" |
| 200 |
dv/dt = -v / tau : 1 : volt |
| 201 |
x = 2 * t : 1 |
| 202 |
""", |
| 203 |
"dv/dt = -v / tau : 2 * volt", |
| 204 |
"dv/dt = v / second : boolean", |
| 205 |
] |
| 206 |
for error_eqs in parse_error_eqs: |
| 207 |
with pytest.raises((ValueError, EquationError, TypeError)): |
| 208 |
parse_string_equations(error_eqs) |
| 209 |
|
| 210 |
|
| 211 |
@pytest.mark.codegen_independent |
| 212 |
def test_correct_replacements(): |
| 213 |
"""Test replacing variables via keyword arguments""" |
| 214 |
# replace a variable name with a new name |
| 215 |
eqs = Equations("dv/dt = -v / tau : 1", v="V") |
| 216 |
# Correct left hand side |
| 217 |
assert ("V" in eqs) and not ("v" in eqs) |
| 218 |
# Correct right hand side |
| 219 |
assert ("V" in eqs["V"].identifiers) and not ("v" in eqs["V"].identifiers) |
| 220 |
|
| 221 |
# replace a variable name with a value |
| 222 |
eqs = Equations("dv/dt = -v / tau : 1", tau=10 * ms) |
| 223 |
assert not "tau" in eqs["v"].identifiers |
| 224 |
|
| 225 |
|
| 226 |
@pytest.mark.codegen_independent |
| 227 |
def test_wrong_replacements(): |
| 228 |
"""Tests for replacements that should not work""" |
| 229 |
# Replacing a variable name with an illegal new name |
| 230 |
with pytest.raises(SyntaxError): |
| 231 |
Equations("dv/dt = -v / tau : 1", v="illegal name") |
| 232 |
with pytest.raises(SyntaxError): |
| 233 |
Equations("dv/dt = -v / tau : 1", v="_reserved") |
| 234 |
with pytest.raises(SyntaxError): |
| 235 |
Equations("dv/dt = -v / tau : 1", v="t") |
| 236 |
|
| 237 |
# Replacing a variable name with a value that already exists |
| 238 |
with pytest.raises(EquationError): |
| 239 |
Equations( |
| 240 |
""" |
| 241 |
dv/dt = -v / tau : 1 |
| 242 |
dx/dt = -x / tau : 1 |
| 243 |
""", |
| 244 |
v="x", |
| 245 |
) |
| 246 |
|
| 247 |
# Replacing a model variable name with a value |
| 248 |
with pytest.raises(ValueError): |
| 249 |
Equations("dv/dt = -v / tau : 1", v=3 * mV) |
| 250 |
|
| 251 |
# Replacing with an illegal value |
| 252 |
with pytest.raises(SyntaxError): |
| 253 |
Equations("dv/dt = -v/tau : 1", tau=np.arange(5)) |
| 254 |
|
| 255 |
|
| 256 |
@pytest.mark.codegen_independent |
| 257 |
def test_substitute(): |
| 258 |
# Check that Equations.substitute returns an independent copy |
| 259 |
eqs = Equations("dx/dt = x : 1") |
| 260 |
eqs2 = eqs.substitute(x="y") |
| 261 |
|
| 262 |
# First equation should be unaffected |
| 263 |
assert len(eqs) == 1 and "x" in eqs |
| 264 |
assert eqs["x"].expr == Expression("x") |
| 265 |
|
| 266 |
# Second equation should have x substituted by y |
| 267 |
assert len(eqs2) == 1 and "y" in eqs2 |
| 268 |
assert eqs2["y"].expr == Expression("y") |
| 269 |
|
| 270 |
|
| 271 |
@pytest.mark.codegen_independent |
| 272 |
def test_construction_errors(): |
| 273 |
""" |
| 274 |
Test that the Equations constructor raises errors correctly |
| 275 |
""" |
| 276 |
# parse error |
| 277 |
with pytest.raises(EquationError): |
| 278 |
Equations("dv/dt = -v / tau volt") |
| 279 |
with pytest.raises(EquationError): |
| 280 |
Equations("dv/dt = -v / tau : volt second") |
| 281 |
|
| 282 |
# incorrect unit definition |
| 283 |
with pytest.raises(EquationError): |
| 284 |
Equations("dv/dt = -v / tau : mvolt") |
| 285 |
with pytest.raises(EquationError): |
| 286 |
Equations("dv/dt = -v / tau : voltage") |
| 287 |
with pytest.raises(EquationError): |
| 288 |
Equations("dv/dt = -v / tau : 1.0*volt") |
| 289 |
|
| 290 |
# Only a single string or a list of SingleEquation objects is allowed |
| 291 |
with pytest.raises(TypeError): |
| 292 |
Equations(None) |
| 293 |
with pytest.raises(TypeError): |
| 294 |
Equations(42) |
| 295 |
with pytest.raises(TypeError): |
| 296 |
Equations(["dv/dt = -v / tau : volt"]) |
| 297 |
|
| 298 |
# duplicate variable names |
| 299 |
with pytest.raises(EquationError): |
| 300 |
Equations( |
| 301 |
""" |
| 302 |
dv/dt = -v / tau : volt |
| 303 |
v = 2 * t/second * volt : volt |
| 304 |
""" |
| 305 |
) |
| 306 |
|
| 307 |
eqs = [ |
| 308 |
SingleEquation( |
| 309 |
DIFFERENTIAL_EQUATION, "v", volt.dim, expr=Expression("-v / tau") |
| 310 |
), |
| 311 |
SingleEquation( |
| 312 |
SUBEXPRESSION, "v", volt.dim, expr=Expression("2 * t/second * volt") |
| 313 |
), |
| 314 |
] |
| 315 |
with pytest.raises(EquationError): |
| 316 |
Equations(eqs) |
| 317 |
|
| 318 |
# illegal variable names |
| 319 |
with pytest.raises(SyntaxError): |
| 320 |
Equations("ddt/dt = -dt / tau : volt") |
| 321 |
with pytest.raises(SyntaxError): |
| 322 |
Equations("dt/dt = -t / tau : volt") |
| 323 |
with pytest.raises(SyntaxError): |
| 324 |
Equations("dxi/dt = -xi / tau : volt") |
| 325 |
with pytest.raises(SyntaxError): |
| 326 |
Equations("for : volt") |
| 327 |
with pytest.raises((EquationError, SyntaxError)): |
| 328 |
Equations("d1a/dt = -1a / tau : volt") |
| 329 |
with pytest.raises(SyntaxError): |
| 330 |
Equations("d_x/dt = -_x / tau : volt") |
| 331 |
|
| 332 |
# xi in a subexpression |
| 333 |
with pytest.raises(EquationError): |
| 334 |
Equations( |
| 335 |
""" |
| 336 |
dv/dt = -(v + I) / (5 * ms) : volt |
| 337 |
I = second**-1*xi**-2*volt : volt |
| 338 |
""" |
| 339 |
) |
| 340 |
|
| 341 |
# more than one xi |
| 342 |
with pytest.raises(EquationError): |
| 343 |
Equations( |
| 344 |
""" |
| 345 |
dv/dt = -v / tau + xi/tau**.5 : volt |
| 346 |
dx/dt = -x / tau + 2*xi/tau : volt |
| 347 |
tau : second |
| 348 |
""" |
| 349 |
) |
| 350 |
# using not-allowed flags |
| 351 |
eqs = Equations("dv/dt = -v / (5 * ms) : volt (flag)") |
| 352 |
eqs.check_flags({DIFFERENTIAL_EQUATION: ["flag"]}) # allow this flag |
| 353 |
with pytest.raises(ValueError): |
| 354 |
eqs.check_flags({DIFFERENTIAL_EQUATION: []}) |
| 355 |
with pytest.raises(ValueError): |
| 356 |
eqs.check_flags({}) |
| 357 |
with pytest.raises(ValueError): |
| 358 |
eqs.check_flags({SUBEXPRESSION: ["flag"]}) |
| 359 |
with pytest.raises(ValueError): |
| 360 |
eqs.check_flags({DIFFERENTIAL_EQUATION: ["otherflag"]}) |
| 361 |
eqs = Equations("dv/dt = -v / (5 * ms) : volt (flag1, flag2)") |
| 362 |
eqs.check_flags({DIFFERENTIAL_EQUATION: ["flag1", "flag2"]}) # allow both flags |
| 363 |
# Don't allow the two flags in combination |
| 364 |
with pytest.raises(ValueError): |
| 365 |
eqs.check_flags( |
| 366 |
{DIFFERENTIAL_EQUATION: ["flag1", "flag2"]}, |
| 367 |
incompatible_flags=[("flag1", "flag2")], |
| 368 |
) |
| 369 |
eqs = Equations( |
| 370 |
""" |
| 371 |
dv/dt = -v / (5 * ms) : volt (flag1) |
| 372 |
dw/dt = -w / (5 * ms) : volt (flag2) |
| 373 |
""" |
| 374 |
) |
| 375 |
# They should be allowed when used independently |
| 376 |
eqs.check_flags( |
| 377 |
{DIFFERENTIAL_EQUATION: ["flag1", "flag2"]}, |
| 378 |
incompatible_flags=[("flag1", "flag2")], |
| 379 |
) |
| 380 |
|
| 381 |
# Circular subexpression |
| 382 |
with pytest.raises(ValueError): |
| 383 |
Equations( |
| 384 |
""" |
| 385 |
dv/dt = -(v + w) / (10 * ms) : 1 |
| 386 |
w = 2 * x : 1 |
| 387 |
x = 3 * w : 1 |
| 388 |
""" |
| 389 |
) |
| 390 |
|
| 391 |
# Boolean/integer differential equations |
| 392 |
with pytest.raises(TypeError): |
| 393 |
Equations("dv/dt = -v / (10*ms) : boolean") |
| 394 |
with pytest.raises(TypeError): |
| 395 |
Equations("dv/dt = -v / (10*ms) : integer") |
| 396 |
|
| 397 |
|
| 398 |
@pytest.mark.codegen_independent |
| 399 |
def test_unit_checking(): |
| 400 |
# dummy Variable class |
| 401 |
class S: |
| 402 |
def __init__(self, dimensions): |
| 403 |
self.dim = get_dimensions(dimensions) |
| 404 |
|
| 405 |
# inconsistent unit for a differential equation |
| 406 |
eqs = Equations("dv/dt = -v : volt") |
| 407 |
group = SimpleGroup({"v": S(volt)}) |
| 408 |
with pytest.raises(DimensionMismatchError): |
| 409 |
eqs.check_units(group, {}) |
| 410 |
|
| 411 |
eqs = Equations("dv/dt = -v / tau: volt") |
| 412 |
group = SimpleGroup(namespace={"tau": 5 * mV}, variables={"v": S(volt)}) |
| 413 |
with pytest.raises(DimensionMismatchError): |
| 414 |
eqs.check_units(group, {}) |
| 415 |
group = SimpleGroup(namespace={"I": 3 * second}, variables={"v": S(volt)}) |
| 416 |
eqs = Equations("dv/dt = -(v + I) / (5 * ms): volt") |
| 417 |
with pytest.raises(DimensionMismatchError): |
| 418 |
eqs.check_units(group, {}) |
| 419 |
|
| 420 |
eqs = Equations( |
| 421 |
""" |
| 422 |
dv/dt = -(v + I) / (5 * ms): volt |
| 423 |
I : second |
| 424 |
""" |
| 425 |
) |
| 426 |
group = SimpleGroup(variables={"v": S(volt), "I": S(second)}, namespace={}) |
| 427 |
with pytest.raises(DimensionMismatchError): |
| 428 |
eqs.check_units(group, {}) |
| 429 |
|
| 430 |
# inconsistent unit for a subexpression |
| 431 |
eqs = Equations( |
| 432 |
""" |
| 433 |
dv/dt = -v / (5 * ms) : volt |
| 434 |
I = 2 * v : amp |
| 435 |
""" |
| 436 |
) |
| 437 |
group = SimpleGroup(variables={"v": S(volt), "I": S(second)}, namespace={}) |
| 438 |
with pytest.raises(DimensionMismatchError): |
| 439 |
eqs.check_units(group, {}) |
| 440 |
|
| 441 |
|
| 442 |
@pytest.mark.codegen_independent |
| 443 |
def test_properties(): |
| 444 |
""" |
| 445 |
Test accessing the various properties of equation objects |
| 446 |
""" |
| 447 |
tau = 10 * ms |
| 448 |
eqs = Equations( |
| 449 |
""" |
| 450 |
dv/dt = -(v + I)/ tau : volt |
| 451 |
I = sin(2 * 22/7. * f * t)* volt : volt |
| 452 |
f = freq * Hz: Hz |
| 453 |
freq : 1 |
| 454 |
""" |
| 455 |
) |
| 456 |
assert ( |
| 457 |
len(eqs.diff_eq_expressions) == 1 |
| 458 |
and eqs.diff_eq_expressions[0][0] == "v" |
| 459 |
and isinstance(eqs.diff_eq_expressions[0][1], Expression) |
| 460 |
) |
| 461 |
assert eqs.diff_eq_names == {"v"} |
| 462 |
assert ( |
| 463 |
len(eqs.eq_expressions) == 3 |
| 464 |
and {name for name, _ in eqs.eq_expressions} == {"v", "I", "f"} |
| 465 |
and all((isinstance(expr, Expression) for _, expr in eqs.eq_expressions)) |
| 466 |
) |
| 467 |
assert len(eqs.eq_names) == 3 and eqs.eq_names == {"v", "I", "f"} |
| 468 |
assert set(eqs.keys()) == {"v", "I", "f", "freq"} |
| 469 |
# test that the equations object is iterable itself |
| 470 |
assert all(isinstance(eq, SingleEquation) for eq in eqs.values()) |
| 471 |
assert all(isinstance(eq, str) for eq in eqs) |
| 472 |
assert ( |
| 473 |
len(eqs.ordered) == 4 |
| 474 |
and all(isinstance(eq, SingleEquation) for eq in eqs.ordered) |
| 475 |
and [eq.varname for eq in eqs.ordered] == ["f", "I", "v", "freq"] |
| 476 |
) |
| 477 |
assert [eq.unit for eq in eqs.ordered] == [Hz, volt, volt, 1] |
| 478 |
assert eqs.names == {"v", "I", "f", "freq"} |
| 479 |
assert eqs.parameter_names == {"freq"} |
| 480 |
assert eqs.subexpr_names == {"I", "f"} |
| 481 |
dimensions = eqs.dimensions |
| 482 |
assert set(dimensions.keys()) == {"v", "I", "f", "freq"} |
| 483 |
assert dimensions["v"] is volt.dim |
| 484 |
assert dimensions["I"] is volt.dim |
| 485 |
assert dimensions["f"] is Hz.dim |
| 486 |
assert dimensions["freq"] is DIMENSIONLESS |
| 487 |
assert eqs.names == set(eqs.dimensions.keys()) |
| 488 |
assert eqs.identifiers == {"tau", "volt", "Hz", "sin", "t"} |
| 489 |
|
| 490 |
# stochastic equations |
| 491 |
assert len(eqs.stochastic_variables) == 0 |
| 492 |
assert eqs.stochastic_type is None |
| 493 |
|
| 494 |
eqs = Equations("""dv/dt = -v / tau + 0.1*second**-.5*xi : 1""") |
| 495 |
assert eqs.stochastic_variables == {"xi"} |
| 496 |
assert eqs.stochastic_type == "additive" |
| 497 |
|
| 498 |
eqs = Equations( |
| 499 |
"dv/dt = -v / tau + 0.1*second**-.5*xi_1 + 0.1*second**-.5*xi_2: 1" |
| 500 |
) |
| 501 |
assert eqs.stochastic_variables == {"xi_1", "xi_2"} |
| 502 |
assert eqs.stochastic_type == "additive" |
| 503 |
|
| 504 |
eqs = Equations("dv/dt = -v / tau + 0.1*second**-1.5*xi*t : 1") |
| 505 |
assert eqs.stochastic_type == "multiplicative" |
| 506 |
|
| 507 |
eqs = Equations("dv/dt = -v / tau + 0.1*second**-1.5*xi*v : 1") |
| 508 |
assert eqs.stochastic_type == "multiplicative" |
| 509 |
|
| 510 |
|
| 511 |
@pytest.mark.codegen_independent |
| 512 |
def test_concatenation(): |
| 513 |
eqs1 = Equations( |
| 514 |
""" |
| 515 |
dv/dt = -(v + I) / tau : volt |
| 516 |
I = sin(2*pi*freq*t) : volt |
| 517 |
freq : Hz |
| 518 |
""" |
| 519 |
) |
| 520 |
|
| 521 |
# Concatenate two equation objects |
| 522 |
eqs2 = Equations("dv/dt = -(v + I) / tau : volt") + Equations( |
| 523 |
""" |
| 524 |
I = sin(2*pi*freq*t) : volt |
| 525 |
freq : Hz |
| 526 |
""" |
| 527 |
) |
| 528 |
|
| 529 |
# Concatenate using "in-place" addition (which is not actually in-place) |
| 530 |
eqs3 = Equations("dv/dt = -(v + I) / tau : volt") |
| 531 |
eqs3 += Equations( |
| 532 |
""" |
| 533 |
I = sin(2*pi*freq*t) : volt |
| 534 |
freq : Hz |
| 535 |
""" |
| 536 |
) |
| 537 |
|
| 538 |
# Concatenate with a string (will be parsed first) |
| 539 |
eqs4 = Equations("dv/dt = -(v + I) / tau : volt") |
| 540 |
eqs4 += """I = sin(2*pi*freq*t) : volt |
| 541 |
freq : Hz""" |
| 542 |
|
| 543 |
# Concatenating with something that is not a string should not work |
| 544 |
with pytest.raises(TypeError): |
| 545 |
eqs4 + 5 |
| 546 |
|
| 547 |
# The string representation is canonical, therefore it should be identical |
| 548 |
# in all cases |
| 549 |
assert str(eqs1) == str(eqs2) |
| 550 |
assert str(eqs2) == str(eqs3) |
| 551 |
assert str(eqs3) == str(eqs4) |
| 552 |
|
| 553 |
|
| 554 |
@pytest.mark.codegen_independent |
| 555 |
def test_extract_subexpressions(): |
| 556 |
eqs = Equations( |
| 557 |
""" |
| 558 |
dv/dt = -v / (10*ms) : 1 |
| 559 |
s1 = 2*v : 1 |
| 560 |
s2 = -v : 1 (constant over dt) |
| 561 |
""" |
| 562 |
) |
| 563 |
variable, constant = extract_constant_subexpressions(eqs) |
| 564 |
assert [var in variable for var in ["v", "s1", "s2"]] |
| 565 |
assert variable["s1"].type == SUBEXPRESSION |
| 566 |
assert variable["s2"].type == PARAMETER |
| 567 |
assert constant["s2"].type == SUBEXPRESSION |
| 568 |
|
| 569 |
|
| 570 |
@pytest.mark.codegen_independent |
| 571 |
def test_repeated_construction(): |
| 572 |
eqs1 = Equations("dx/dt = x : 1") |
| 573 |
eqs2 = Equations("dx/dt = x : 1", x="y") |
| 574 |
assert len(eqs1) == 1 |
| 575 |
assert "x" in eqs1 |
| 576 |
assert eqs1["x"].expr == Expression("x") |
| 577 |
assert len(eqs2) == 1 |
| 578 |
assert "y" in eqs2 |
| 579 |
assert eqs2["y"].expr == Expression("y") |
| 580 |
|
| 581 |
|
| 582 |
@pytest.mark.codegen_independent |
| 583 |
def test_str_repr(): |
| 584 |
""" |
| 585 |
Test the string representation (only that it does not throw errors). |
| 586 |
""" |
| 587 |
tau = 10 * ms |
| 588 |
eqs = Equations( |
| 589 |
""" |
| 590 |
dv/dt = -(v + I)/ tau : volt (unless refractory) |
| 591 |
I = sin(2 * 22/7. * f * t)* volt : volt |
| 592 |
f : Hz |
| 593 |
""" |
| 594 |
) |
| 595 |
assert len(str(eqs)) > 0 |
| 596 |
assert len(repr(eqs)) > 0 |
| 597 |
|
| 598 |
# Test str and repr of SingleEquations explicitly (might already have been |
| 599 |
# called by Equations |
| 600 |
for eq in eqs.values(): |
| 601 |
assert (len(str(eq))) > 0 |
| 602 |
assert (len(repr(eq))) > 0 |
| 603 |
|
| 604 |
|
| 605 |
@pytest.mark.codegen_independent |
| 606 |
def test_dependency_calculation(): |
| 607 |
eqs = Equations( |
| 608 |
""" |
| 609 |
dv/dt = I_m / C_m : volt |
| 610 |
I_m = I_ext + I_pas : amp |
| 611 |
I_ext = 1*nA + sin(2*pi*100*Hz*t)*nA : amp |
| 612 |
I_pas = g_L*(E_L - v) : amp |
| 613 |
""" |
| 614 |
) |
| 615 |
deps = eqs.dependencies |
| 616 |
assert set(deps.keys()) == {"v", "I_m", "I_ext", "I_pas"} |
| 617 |
|
| 618 |
# v depends directly on I_m, on I_ext and I_pas via I_m, and on v via I_m -> I_pas |
| 619 |
assert len(deps["v"]) == 4 |
| 620 |
assert {d.equation.varname for d in deps["v"]} == {"I_m", "I_ext", "I_pas", "v"} |
| 621 |
expected_via = { |
| 622 |
"I_m": (), |
| 623 |
"I_pas": ("I_m",), |
| 624 |
"I_ext": ("I_m",), |
| 625 |
"v": ("I_m", "I_pas"), |
| 626 |
} |
| 627 |
assert all([d.via == expected_via[d.equation.varname] for d in deps["v"]]) |
| 628 |
|
| 629 |
# I_m depends directly on I_ext and I_pas, and on v via I_pas |
| 630 |
assert len(deps["I_m"]) == 3 |
| 631 |
assert {d.equation.varname for d in deps["I_m"]} == {"I_ext", "I_pas", "v"} |
| 632 |
expected_via = {"I_ext": (), "I_pas": (), "v": ("I_pas",)} |
| 633 |
assert all([d.via == expected_via[d.equation.varname] for d in deps["I_m"]]) |
| 634 |
|
| 635 |
# I_ext does not depend on anything |
| 636 |
assert len(deps["I_ext"]) == 0 |
| 637 |
|
| 638 |
# I_pas depends on v directly |
| 639 |
assert len(deps["I_pas"]) == 1 |
| 640 |
assert deps["I_pas"][0].equation.varname == "v" |
| 641 |
assert deps["I_pas"][0].via == () |
| 642 |
|
| 643 |
|
| 644 |
@pytest.mark.codegen_independent |
| 645 |
@pytest.mark.skipif(pprint is None, reason="ipython is not installed") |
| 646 |
def test_ipython_pprint(): |
| 647 |
from io import StringIO |
| 648 |
|
| 649 |
eqs = Equations( |
| 650 |
""" |
| 651 |
dv/dt = -(v + I)/ tau : volt (unless refractory) |
| 652 |
I = sin(2 * 22/7. * f * t)* volt : volt |
| 653 |
f : Hz |
| 654 |
""" |
| 655 |
) |
| 656 |
# Test ipython's pretty printing |
| 657 |
old_stdout = sys.stdout |
| 658 |
string_output = StringIO() |
| 659 |
sys.stdout = string_output |
| 660 |
pprint(eqs) |
| 661 |
assert len(string_output.getvalue()) > 0 |
| 662 |
sys.stdout = old_stdout |
| 663 |
|
| 664 |
|
| 665 |
if __name__ == "__main__": |
| 666 |
test_utility_functions() |
| 667 |
test_identifier_checks() |
| 668 |
test_parse_equations() |
| 669 |
test_correct_replacements() |
| 670 |
test_substitute() |
| 671 |
test_wrong_replacements() |
| 672 |
test_construction_errors() |
| 673 |
test_concatenation() |
| 674 |
test_unit_checking() |
| 675 |
test_properties() |
| 676 |
test_extract_subexpressions() |
| 677 |
test_repeated_construction() |
| 678 |
test_str_repr() |