Debian Codegraph @

brian2/tests/test_codestrings.py

raw
1 import numpy as np
2 import pytest
3 import sympy
4 from numpy.testing import assert_equal
5
6 import brian2
7 from brian2 import (
8 DimensionMismatchError,
9 Expression,
10 Hz,
11 Statements,
12 get_dimensions,
13 ms,
14 mV,
15 second,
16 volt,
17 )
18 from brian2.core.preferences import prefs
19 from brian2.utils.logger import catch_logs
20
21
22 def sympy_equals(expr1, expr2):
23 """
24 Test that whether two string expressions are equal using sympy, allowing
25 e.g. for ``sympy_equals("x * x", "x ** 2") == True``.
26 """
27 s_expr1 = sympy.nsimplify(sympy.sympify(expr1).expand())
28 s_expr2 = sympy.nsimplify(sympy.sympify(expr2).expand())
29 return s_expr1 == s_expr2
30
31
32 @pytest.mark.codegen_independent
33 def test_expr_creation():
34 """
35 Test creating expressions.
36 """
37 expr = Expression("v > 5 * mV")
38 assert expr.code == "v > 5 * mV"
39 assert (
40 "v" in expr.identifiers
41 and "mV" in expr.identifiers
42 and not "V" in expr.identifiers
43 )
44 with pytest.raises(SyntaxError):
45 Expression("v 5 * mV")
46
47
48 @pytest.mark.codegen_independent
49 def test_split_stochastic():
50 tau = 5 * ms
51 expr = Expression("(-v + I) / tau")
52 # No stochastic part
53 assert expr.split_stochastic() == (expr, None)
54
55 # No non-stochastic part -- note that it should return 0 and not None
56 expr = Expression("sigma*xi/tau**.5")
57 non_stochastic, stochastic = expr.split_stochastic()
58 assert sympy_equals(non_stochastic.code, 0)
59 assert "xi" in stochastic
60 assert len(stochastic) == 1
61 assert sympy_equals(stochastic["xi"].code, "sigma/tau**.5")
62
63 expr = Expression("(-v + I) / tau + sigma*xi/tau**.5")
64 non_stochastic, stochastic = expr.split_stochastic()
65 assert "xi" in stochastic
66 assert len(stochastic) == 1
67 assert sympy_equals(non_stochastic.code, "(-v + I) / tau")
68 assert sympy_equals(stochastic["xi"].code, "sigma/tau**.5")
69
70 expr = Expression("(-v + I) / tau + sigma*xi_1/tau**.5 + xi_2*sigma2/sqrt(tau_2)")
71 non_stochastic, stochastic = expr.split_stochastic()
72 assert set(stochastic.keys()) == {"xi_1", "xi_2"}
73 assert sympy_equals(non_stochastic.code, "(-v + I) / tau")
74 assert sympy_equals(stochastic["xi_1"].code, "sigma/tau**.5")
75 assert sympy_equals(stochastic["xi_2"].code, "sigma2/tau_2**.5")
76
77 expr = Expression("-v / tau + 1 / xi")
78 with pytest.raises(ValueError):
79 expr.split_stochastic()
80
81
82 @pytest.mark.codegen_independent
83 def test_str_repr():
84 """
85 Test the string representation of expressions and statements. Assumes that
86 __str__ returns the complete expression/statement string and __repr__ a
87 string of the form "Expression(...)" or "Statements(...)" that can be
88 evaluated.
89 """
90 expr_string = "(v - I)/ tau"
91 expr = Expression(expr_string)
92
93 # use sympy to check for equivalence of expressions (terms may have be
94 # re-arranged by sympy)
95 assert sympy_equals(expr_string, str(expr))
96 assert sympy_equals(expr_string, eval(repr(expr)).code)
97
98 # Use exact string equivalence for statements
99 statement_string = "v += w"
100 statement = Statements(statement_string)
101
102 assert str(statement) == "v += w"
103 assert repr(statement) == "Statements('v += w')"
104
105
106 if __name__ == "__main__":
107 test_expr_creation()
108 test_split_stochastic()
109 test_str_repr()