Debian Codegraph @

brian2/tests/test_base.py

raw ◀ ▶
1 Debian patch touch this file
•1 import re
2 import pytest
3 from numpy.testing import assert_equal
4 from packaging.version import parse as parse_version
5
6 from brian2 import *
7 from brian2.devices.device import reinit_and_delete
8 from brian2.tests.utils import assert_allclose
9
10
11 class DerivedBrianObject(BrianObject):
12 def __init__(self, name="derivedbrianobject*"):
13 super().__init__(name=name)
14
15 def __str__(self):
16 return self.name
17
18 __repr__ = __str__
19
20
21 @pytest.mark.codegen_independent
22 def test_base():
23 x = DerivedBrianObject("x")
24 y = DerivedBrianObject("y")
25 assert_equal(x.when, "start")
26 assert_equal(x.order, 0)
27 assert_equal(len(x.contained_objects), 0)
28 with pytest.raises(AttributeError):
29 setattr(x, "contained_objects", [])
30 x.contained_objects.append(y)
31 assert_equal(len(x.contained_objects), 1)
32 assert x.contained_objects[0] is y
33
34 assert_equal(x.active, True)
35 assert_equal(y.active, True)
36 y.active = False
37 assert_equal(x.active, True)
38 assert_equal(y.active, False)
39 y.active = True
40 assert_equal(x.active, True)
41 assert_equal(y.active, True)
42 x.active = False
43 assert_equal(x.active, False)
44 assert_equal(y.active, False)
45
46
47 @pytest.mark.codegen_independent
48 def test_names():
49 obj = BrianObject()
50 obj2 = BrianObject()
51 obj3 = DerivedBrianObject()
52 assert_equal(obj.name, "brianobject")
53 assert_equal(obj2.name, "brianobject_1")
54 assert_equal(obj3.name, "derivedbrianobject")
55
56
57 @pytest.mark.codegen_independent
58 def test_duplicate_names():
59 # duplicate names are allowed, as long as they are not part of the
60 # same network
61 obj1 = BrianObject(name="name1")
62 obj2 = BrianObject(name="name2")
63 obj3 = BrianObject(name="name")
64 obj4 = BrianObject(name="name")
65 net = Network(obj1, obj2)
66 # all is good
67 net.run(0 * ms)
68 net = Network(obj3, obj4)
69 with pytest.raises(ValueError):
70 net.run(0 * ms)
71
72
73 @pytest.mark.standalone_compatible
74 @pytest.mark.multiple_runs
75 def test_active_flag():
76 G = NeuronGroup(1, "dv/dt = 1/ms : 1")
77 mon = StateMonitor(G, "v", record=0)
78 mon.active = False
79 run(1 * ms)
80 mon.active = True
81 G.active = False
82 run(1 * ms)
83 device.build(direct_call=False, **device.build_options)
84 # Monitor should start recording at 1ms
85 # Neurongroup should not integrate after 1ms (but should have integrated before)
86 assert_allclose(mon[0].t[0], 1 * ms)
87 assert_allclose(mon[0].v, 1.0)
88
89
90 @pytest.mark.codegen_independent
91 def test_version():
92 # Check that determining the Brian version works correctly
93 import brian2
94
95 version = brian2.__version__
96 assert version.startswith("2.")
97
98 # Check that the version tuple is correct
99 version_tuple = brian2.__version_tuple__
100
101 expected = []
•102 for v in tuple(int(i) if i.isnumeric() else i for i in re.split(r"\.|\+", version)):
103 try:
104 expected.append(int(v))
105 except ValueError:
106 expected.append(v)
107
108 # Note that setuptools_scm >= v8.2.1 include a post suffix in the
109 # version tutple, but earlier versions don't
110 assert version_tuple == tuple(expected) or version_tuple == tuple(
111 t for t in expected if isinstance(t, int)
112 )
113
114
115 if __name__ == "__main__":
116 test_base()
117 test_names()
118 test_duplicate_names()
119 test_active_flag()