File: test_equations.py

package info (click to toggle)
brian 2.9.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,872 kB
  • sloc: python: 51,820; cpp: 2,033; makefile: 108; sh: 72
file content (678 lines) | stat: -rw-r--r-- 22,026 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
import sys

import numpy as np

try:
    from IPython.lib.pretty import pprint
except ImportError:
    pprint = None
import pytest

from brian2 import Equations, Expression, Hz, Unit, farad, metre, ms, mV, second, volt
from brian2.core.namespace import DEFAULT_UNITS
from brian2.equations.equations import (
    BOOLEAN,
    DIFFERENTIAL_EQUATION,
    FLOAT,
    INTEGER,
    PARAMETER,
    SUBEXPRESSION,
    EquationError,
    SingleEquation,
    check_identifier_basic,
    check_identifier_constants,
    check_identifier_functions,
    check_identifier_reserved,
    check_identifier_units,
    dimensions_and_type_from_string,
    extract_constant_subexpressions,
    parse_string_equations,
)
from brian2.equations.refractory import check_identifier_refractory
from brian2.groups.group import Group
from brian2.units.fundamentalunits import (
    DIMENSIONLESS,
    DimensionMismatchError,
    get_dimensions,
)


# a simple Group for testing
class SimpleGroup(Group):
    def __init__(self, variables, namespace=None):
        self.variables = variables
        self.namespace = namespace


@pytest.mark.codegen_independent
def test_utility_functions():
    unit_namespace = DEFAULT_UNITS

    # Some simple tests whether the namespace returned by
    # get_default_namespace() makes sense
    assert "volt" in unit_namespace
    assert "ms" in unit_namespace
    assert unit_namespace["ms"] is ms
    assert unit_namespace["ms"] is unit_namespace["msecond"]
    for unit in unit_namespace.values():
        assert isinstance(unit, Unit)

    assert dimensions_and_type_from_string("second") == (second.dim, FLOAT)
    assert dimensions_and_type_from_string("1") == (DIMENSIONLESS, FLOAT)
    assert dimensions_and_type_from_string("volt") == (volt.dim, FLOAT)
    assert dimensions_and_type_from_string("second ** -1") == (Hz.dim, FLOAT)
    assert dimensions_and_type_from_string("farad / metre**2") == (
        (farad / metre**2).dim,
        FLOAT,
    )
    assert dimensions_and_type_from_string("boolean") == (DIMENSIONLESS, BOOLEAN)
    assert dimensions_and_type_from_string("integer") == (DIMENSIONLESS, INTEGER)
    with pytest.raises(ValueError):
        dimensions_and_type_from_string("metr / second")
    with pytest.raises(ValueError):
        dimensions_and_type_from_string("metre **")
    with pytest.raises(ValueError):
        dimensions_and_type_from_string("5")
    with pytest.raises(ValueError):
        dimensions_and_type_from_string("2 / second")
    # Only the use of base units is allowed
    with pytest.raises(ValueError):
        dimensions_and_type_from_string("farad / cm**2")


@pytest.mark.codegen_independent
def test_identifier_checks():
    legal_identifiers = ["v", "Vm", "V", "x", "ge", "g_i", "a2", "gaba_123"]
    illegal_identifiers = ["_v", "1v", "ΓΌ", "ge!", "v.x", "for", "else", "if"]

    for identifier in legal_identifiers:
        try:
            check_identifier_basic(identifier)
            check_identifier_reserved(identifier)
        except ValueError as ex:
            raise AssertionError(
                f'check complained about identifier "{identifier}": {ex}'
            )

    for identifier in illegal_identifiers:
        with pytest.raises(SyntaxError):
            check_identifier_basic(identifier)

    for identifier in ("t", "dt", "xi", "i", "N"):
        with pytest.raises(SyntaxError):
            check_identifier_reserved(identifier)

    for identifier in ("not_refractory", "refractory", "refractory_until"):
        with pytest.raises(SyntaxError):
            check_identifier_refractory(identifier)

    for identifier in ("exp", "sin", "sqrt"):
        with pytest.raises(SyntaxError):
            check_identifier_functions(identifier)

    for identifier in ("e", "pi", "inf"):
        with pytest.raises(SyntaxError):
            check_identifier_constants(identifier)

    for identifier in ("volt", "second", "mV", "nA"):
        with pytest.raises(SyntaxError):
            check_identifier_units(identifier)

    # Check identifier registry
    assert check_identifier_basic in Equations.identifier_checks
    assert check_identifier_reserved in Equations.identifier_checks
    assert check_identifier_refractory in Equations.identifier_checks
    assert check_identifier_functions in Equations.identifier_checks
    assert check_identifier_constants in Equations.identifier_checks
    assert check_identifier_units in Equations.identifier_checks

    # Set up a dummy identifier check that disallows the variable name
    # gaba_123 (that is otherwise valid)
    def disallow_gaba_123(identifier):
        if identifier == "gaba_123":
            raise SyntaxError("I do not like this name")

    Equations.check_identifier("gaba_123")
    old_checks = set(Equations.identifier_checks)
    Equations.register_identifier_check(disallow_gaba_123)
    with pytest.raises(SyntaxError):
        Equations.check_identifier("gaba_123")
    Equations.identifier_checks = old_checks

    # registering a non-function should not work
    with pytest.raises(ValueError):
        Equations.register_identifier_check("no function")


@pytest.mark.codegen_independent
def test_parse_equations():
    """Test the parsing of equation strings"""
    # A simple equation
    eqs = parse_string_equations("dv/dt = -v / tau : 1")
    assert len(eqs) == 1 and "v" in eqs and eqs["v"].type == DIFFERENTIAL_EQUATION
    assert eqs["v"].dim is DIMENSIONLESS

    # A complex one
    eqs = parse_string_equations(
        """
        dv/dt = -(v +
                  ge + # excitatory conductance
                  I # external current
                 )/ tau : volt
        dge/dt = -ge / tau_ge : volt
        I = sin(2 * pi * f * t) : volt
        f : Hz (constant)
        b : boolean
        n : integer
        """
    )
    assert len(eqs) == 6
    assert "v" in eqs and eqs["v"].type == DIFFERENTIAL_EQUATION
    assert "ge" in eqs and eqs["ge"].type == DIFFERENTIAL_EQUATION
    assert "I" in eqs and eqs["I"].type == SUBEXPRESSION
    assert "f" in eqs and eqs["f"].type == PARAMETER
    assert "b" in eqs and eqs["b"].type == PARAMETER
    assert "n" in eqs and eqs["n"].type == PARAMETER
    assert eqs["f"].var_type == FLOAT
    assert eqs["b"].var_type == BOOLEAN
    assert eqs["n"].var_type == INTEGER
    assert eqs["v"].dim is volt.dim
    assert eqs["ge"].dim is volt.dim
    assert eqs["I"].dim is volt.dim
    assert eqs["f"].dim is Hz.dim
    assert eqs["v"].flags == []
    assert eqs["ge"].flags == []
    assert eqs["I"].flags == []
    assert eqs["f"].flags == ["constant"]

    duplicate_eqs = """
    dv/dt = -v / tau : 1
    v = 2 * t : 1
    """
    with pytest.raises(EquationError):
        parse_string_equations(duplicate_eqs)
    parse_error_eqs = [
        """
        dv/d = -v / tau : 1
        x = 2 * t : 1
        """,
        """
        dv/dt = -v / tau : 1 : volt
        x = 2 * t : 1
        """,
        "dv/dt = -v / tau : 2 * volt",
        "dv/dt = v / second : boolean",
    ]
    for error_eqs in parse_error_eqs:
        with pytest.raises((ValueError, EquationError, TypeError)):
            parse_string_equations(error_eqs)


@pytest.mark.codegen_independent
def test_correct_replacements():
    """Test replacing variables via keyword arguments"""
    # replace a variable name with a new name
    eqs = Equations("dv/dt = -v / tau : 1", v="V")
    # Correct left hand side
    assert ("V" in eqs) and not ("v" in eqs)
    # Correct right hand side
    assert ("V" in eqs["V"].identifiers) and not ("v" in eqs["V"].identifiers)

    # replace a variable name with a value
    eqs = Equations("dv/dt = -v / tau : 1", tau=10 * ms)
    assert not "tau" in eqs["v"].identifiers


@pytest.mark.codegen_independent
def test_wrong_replacements():
    """Tests for replacements that should not work"""
    # Replacing a variable name with an illegal new name
    with pytest.raises(SyntaxError):
        Equations("dv/dt = -v / tau : 1", v="illegal name")
    with pytest.raises(SyntaxError):
        Equations("dv/dt = -v / tau : 1", v="_reserved")
    with pytest.raises(SyntaxError):
        Equations("dv/dt = -v / tau : 1", v="t")

    # Replacing a variable name with a value that already exists
    with pytest.raises(EquationError):
        Equations(
            """
            dv/dt = -v / tau : 1
            dx/dt = -x / tau : 1
            """,
            v="x",
        )

    # Replacing a model variable name with a value
    with pytest.raises(ValueError):
        Equations("dv/dt = -v / tau : 1", v=3 * mV)

    # Replacing with an illegal value
    with pytest.raises(SyntaxError):
        Equations("dv/dt = -v/tau : 1", tau=np.arange(5))


@pytest.mark.codegen_independent
def test_substitute():
    # Check that Equations.substitute returns an independent copy
    eqs = Equations("dx/dt = x : 1")
    eqs2 = eqs.substitute(x="y")

    # First equation should be unaffected
    assert len(eqs) == 1 and "x" in eqs
    assert eqs["x"].expr == Expression("x")

    # Second equation should have x substituted by y
    assert len(eqs2) == 1 and "y" in eqs2
    assert eqs2["y"].expr == Expression("y")


@pytest.mark.codegen_independent
def test_construction_errors():
    """
    Test that the Equations constructor raises errors correctly
    """
    # parse error
    with pytest.raises(EquationError):
        Equations("dv/dt = -v / tau volt")
    with pytest.raises(EquationError):
        Equations("dv/dt = -v / tau : volt second")

    # incorrect unit definition
    with pytest.raises(EquationError):
        Equations("dv/dt = -v / tau : mvolt")
    with pytest.raises(EquationError):
        Equations("dv/dt = -v / tau : voltage")
    with pytest.raises(EquationError):
        Equations("dv/dt = -v / tau : 1.0*volt")

    # Only a single string or a list of SingleEquation objects is allowed
    with pytest.raises(TypeError):
        Equations(None)
    with pytest.raises(TypeError):
        Equations(42)
    with pytest.raises(TypeError):
        Equations(["dv/dt = -v / tau : volt"])

    # duplicate variable names
    with pytest.raises(EquationError):
        Equations(
            """
            dv/dt = -v / tau : volt
            v = 2 * t/second * volt : volt
            """
        )

    eqs = [
        SingleEquation(
            DIFFERENTIAL_EQUATION, "v", volt.dim, expr=Expression("-v / tau")
        ),
        SingleEquation(
            SUBEXPRESSION, "v", volt.dim, expr=Expression("2 * t/second * volt")
        ),
    ]
    with pytest.raises(EquationError):
        Equations(eqs)

    # illegal variable names
    with pytest.raises(SyntaxError):
        Equations("ddt/dt = -dt / tau : volt")
    with pytest.raises(SyntaxError):
        Equations("dt/dt = -t / tau : volt")
    with pytest.raises(SyntaxError):
        Equations("dxi/dt = -xi / tau : volt")
    with pytest.raises(SyntaxError):
        Equations("for : volt")
    with pytest.raises((EquationError, SyntaxError)):
        Equations("d1a/dt = -1a / tau : volt")
    with pytest.raises(SyntaxError):
        Equations("d_x/dt = -_x / tau : volt")

    # xi in a subexpression
    with pytest.raises(EquationError):
        Equations(
            """
            dv/dt = -(v + I) / (5 * ms) : volt
            I = second**-1*xi**-2*volt : volt
            """
        )

    # more than one xi
    with pytest.raises(EquationError):
        Equations(
            """
            dv/dt = -v / tau + xi/tau**.5 : volt
            dx/dt = -x / tau + 2*xi/tau : volt
            tau : second
            """
        )
    # using not-allowed flags
    eqs = Equations("dv/dt = -v / (5 * ms) : volt (flag)")
    eqs.check_flags({DIFFERENTIAL_EQUATION: ["flag"]})  # allow this flag
    with pytest.raises(ValueError):
        eqs.check_flags({DIFFERENTIAL_EQUATION: []})
    with pytest.raises(ValueError):
        eqs.check_flags({})
    with pytest.raises(ValueError):
        eqs.check_flags({SUBEXPRESSION: ["flag"]})
    with pytest.raises(ValueError):
        eqs.check_flags({DIFFERENTIAL_EQUATION: ["otherflag"]})
    eqs = Equations("dv/dt = -v / (5 * ms) : volt (flag1, flag2)")
    eqs.check_flags({DIFFERENTIAL_EQUATION: ["flag1", "flag2"]})  # allow both flags
    # Don't allow the two flags in combination
    with pytest.raises(ValueError):
        eqs.check_flags(
            {DIFFERENTIAL_EQUATION: ["flag1", "flag2"]},
            incompatible_flags=[("flag1", "flag2")],
        )
    eqs = Equations(
        """
        dv/dt = -v / (5 * ms) : volt (flag1)
        dw/dt = -w / (5 * ms) : volt (flag2)
        """
    )
    # They should be allowed when used independently
    eqs.check_flags(
        {DIFFERENTIAL_EQUATION: ["flag1", "flag2"]},
        incompatible_flags=[("flag1", "flag2")],
    )

    # Circular subexpression
    with pytest.raises(ValueError):
        Equations(
            """
            dv/dt = -(v + w) / (10 * ms) : 1
            w = 2 * x : 1
            x = 3 * w : 1
            """
        )

    # Boolean/integer differential equations
    with pytest.raises(TypeError):
        Equations("dv/dt = -v / (10*ms) : boolean")
    with pytest.raises(TypeError):
        Equations("dv/dt = -v / (10*ms) : integer")


@pytest.mark.codegen_independent
def test_unit_checking():
    # dummy Variable class
    class S:
        def __init__(self, dimensions):
            self.dim = get_dimensions(dimensions)

    # inconsistent unit for a differential equation
    eqs = Equations("dv/dt = -v : volt")
    group = SimpleGroup({"v": S(volt)})
    with pytest.raises(DimensionMismatchError):
        eqs.check_units(group, {})

    eqs = Equations("dv/dt = -v / tau: volt")
    group = SimpleGroup(namespace={"tau": 5 * mV}, variables={"v": S(volt)})
    with pytest.raises(DimensionMismatchError):
        eqs.check_units(group, {})
    group = SimpleGroup(namespace={"I": 3 * second}, variables={"v": S(volt)})
    eqs = Equations("dv/dt = -(v + I) / (5 * ms): volt")
    with pytest.raises(DimensionMismatchError):
        eqs.check_units(group, {})

    eqs = Equations(
        """
        dv/dt = -(v + I) / (5 * ms): volt
        I : second
        """
    )
    group = SimpleGroup(variables={"v": S(volt), "I": S(second)}, namespace={})
    with pytest.raises(DimensionMismatchError):
        eqs.check_units(group, {})

    # inconsistent unit for a subexpression
    eqs = Equations(
        """
        dv/dt = -v / (5 * ms) : volt
        I = 2 * v : amp
        """
    )
    group = SimpleGroup(variables={"v": S(volt), "I": S(second)}, namespace={})
    with pytest.raises(DimensionMismatchError):
        eqs.check_units(group, {})


@pytest.mark.codegen_independent
def test_properties():
    """
    Test accessing the various properties of equation objects
    """
    tau = 10 * ms
    eqs = Equations(
        """
        dv/dt = -(v + I)/ tau : volt
        I = sin(2 * 22/7. * f * t)* volt : volt
        f = freq * Hz: Hz
        freq : 1
        """
    )
    assert (
        len(eqs.diff_eq_expressions) == 1
        and eqs.diff_eq_expressions[0][0] == "v"
        and isinstance(eqs.diff_eq_expressions[0][1], Expression)
    )
    assert eqs.diff_eq_names == {"v"}
    assert (
        len(eqs.eq_expressions) == 3
        and {name for name, _ in eqs.eq_expressions} == {"v", "I", "f"}
        and all((isinstance(expr, Expression) for _, expr in eqs.eq_expressions))
    )
    assert len(eqs.eq_names) == 3 and eqs.eq_names == {"v", "I", "f"}
    assert set(eqs.keys()) == {"v", "I", "f", "freq"}
    # test that the equations object is iterable itself
    assert all(isinstance(eq, SingleEquation) for eq in eqs.values())
    assert all(isinstance(eq, str) for eq in eqs)
    assert (
        len(eqs.ordered) == 4
        and all(isinstance(eq, SingleEquation) for eq in eqs.ordered)
        and [eq.varname for eq in eqs.ordered] == ["f", "I", "v", "freq"]
    )
    assert [eq.unit for eq in eqs.ordered] == [Hz, volt, volt, 1]
    assert eqs.names == {"v", "I", "f", "freq"}
    assert eqs.parameter_names == {"freq"}
    assert eqs.subexpr_names == {"I", "f"}
    dimensions = eqs.dimensions
    assert set(dimensions.keys()) == {"v", "I", "f", "freq"}
    assert dimensions["v"] is volt.dim
    assert dimensions["I"] is volt.dim
    assert dimensions["f"] is Hz.dim
    assert dimensions["freq"] is DIMENSIONLESS
    assert eqs.names == set(eqs.dimensions.keys())
    assert eqs.identifiers == {"tau", "volt", "Hz", "sin", "t"}

    # stochastic equations
    assert len(eqs.stochastic_variables) == 0
    assert eqs.stochastic_type is None

    eqs = Equations("""dv/dt = -v / tau + 0.1*second**-.5*xi : 1""")
    assert eqs.stochastic_variables == {"xi"}
    assert eqs.stochastic_type == "additive"

    eqs = Equations(
        "dv/dt = -v / tau + 0.1*second**-.5*xi_1 +  0.1*second**-.5*xi_2: 1"
    )
    assert eqs.stochastic_variables == {"xi_1", "xi_2"}
    assert eqs.stochastic_type == "additive"

    eqs = Equations("dv/dt = -v / tau + 0.1*second**-1.5*xi*t : 1")
    assert eqs.stochastic_type == "multiplicative"

    eqs = Equations("dv/dt = -v / tau + 0.1*second**-1.5*xi*v : 1")
    assert eqs.stochastic_type == "multiplicative"


@pytest.mark.codegen_independent
def test_concatenation():
    eqs1 = Equations(
        """
        dv/dt = -(v + I) / tau : volt
        I = sin(2*pi*freq*t) : volt
        freq : Hz
        """
    )

    # Concatenate two equation objects
    eqs2 = Equations("dv/dt = -(v + I) / tau : volt") + Equations(
        """
        I = sin(2*pi*freq*t) : volt
        freq : Hz
        """
    )

    # Concatenate using "in-place" addition (which is not actually in-place)
    eqs3 = Equations("dv/dt = -(v + I) / tau : volt")
    eqs3 += Equations(
        """
        I = sin(2*pi*freq*t) : volt
        freq : Hz
        """
    )

    # Concatenate with a string (will be parsed first)
    eqs4 = Equations("dv/dt = -(v + I) / tau : volt")
    eqs4 += """I = sin(2*pi*freq*t) : volt
               freq : Hz"""

    # Concatenating with something that is not a string should not work
    with pytest.raises(TypeError):
        eqs4 + 5

    # The string representation is canonical, therefore it should be identical
    # in all cases
    assert str(eqs1) == str(eqs2)
    assert str(eqs2) == str(eqs3)
    assert str(eqs3) == str(eqs4)


@pytest.mark.codegen_independent
def test_extract_subexpressions():
    eqs = Equations(
        """
        dv/dt = -v / (10*ms) : 1
        s1 = 2*v : 1
        s2 = -v : 1 (constant over dt)
        """
    )
    variable, constant = extract_constant_subexpressions(eqs)
    assert [var in variable for var in ["v", "s1", "s2"]]
    assert variable["s1"].type == SUBEXPRESSION
    assert variable["s2"].type == PARAMETER
    assert constant["s2"].type == SUBEXPRESSION


@pytest.mark.codegen_independent
def test_repeated_construction():
    eqs1 = Equations("dx/dt = x : 1")
    eqs2 = Equations("dx/dt = x : 1", x="y")
    assert len(eqs1) == 1
    assert "x" in eqs1
    assert eqs1["x"].expr == Expression("x")
    assert len(eqs2) == 1
    assert "y" in eqs2
    assert eqs2["y"].expr == Expression("y")


@pytest.mark.codegen_independent
def test_str_repr():
    """
    Test the string representation (only that it does not throw errors).
    """
    tau = 10 * ms
    eqs = Equations(
        """
        dv/dt = -(v + I)/ tau : volt (unless refractory)
        I = sin(2 * 22/7. * f * t)* volt : volt
        f : Hz
        """
    )
    assert len(str(eqs)) > 0
    assert len(repr(eqs)) > 0

    # Test str and repr of SingleEquations explicitly (might already have been
    # called by Equations
    for eq in eqs.values():
        assert (len(str(eq))) > 0
        assert (len(repr(eq))) > 0


@pytest.mark.codegen_independent
def test_dependency_calculation():
    eqs = Equations(
        """
        dv/dt = I_m / C_m : volt
        I_m = I_ext + I_pas : amp
        I_ext = 1*nA + sin(2*pi*100*Hz*t)*nA : amp
        I_pas = g_L*(E_L - v) : amp
        """
    )
    deps = eqs.dependencies
    assert set(deps.keys()) == {"v", "I_m", "I_ext", "I_pas"}

    # v depends directly on I_m, on I_ext and I_pas via I_m, and on v via I_m -> I_pas
    assert len(deps["v"]) == 4
    assert {d.equation.varname for d in deps["v"]} == {"I_m", "I_ext", "I_pas", "v"}
    expected_via = {
        "I_m": (),
        "I_pas": ("I_m",),
        "I_ext": ("I_m",),
        "v": ("I_m", "I_pas"),
    }
    assert all([d.via == expected_via[d.equation.varname] for d in deps["v"]])

    # I_m depends directly on I_ext and I_pas, and on v via I_pas
    assert len(deps["I_m"]) == 3
    assert {d.equation.varname for d in deps["I_m"]} == {"I_ext", "I_pas", "v"}
    expected_via = {"I_ext": (), "I_pas": (), "v": ("I_pas",)}
    assert all([d.via == expected_via[d.equation.varname] for d in deps["I_m"]])

    # I_ext does not depend on anything
    assert len(deps["I_ext"]) == 0

    # I_pas depends on v directly
    assert len(deps["I_pas"]) == 1
    assert deps["I_pas"][0].equation.varname == "v"
    assert deps["I_pas"][0].via == ()


@pytest.mark.codegen_independent
@pytest.mark.skipif(pprint is None, reason="ipython is not installed")
def test_ipython_pprint():
    from io import StringIO

    eqs = Equations(
        """
        dv/dt = -(v + I)/ tau : volt (unless refractory)
        I = sin(2 * 22/7. * f * t)* volt : volt
        f : Hz
        """
    )
    # Test ipython's pretty printing
    old_stdout = sys.stdout
    string_output = StringIO()
    sys.stdout = string_output
    pprint(eqs)
    assert len(string_output.getvalue()) > 0
    sys.stdout = old_stdout


if __name__ == "__main__":
    test_utility_functions()
    test_identifier_checks()
    test_parse_equations()
    test_correct_replacements()
    test_substitute()
    test_wrong_replacements()
    test_construction_errors()
    test_concatenation()
    test_unit_checking()
    test_properties()
    test_extract_subexpressions()
    test_repeated_construction()
    test_str_repr()