Debian Codegraph @

brian2/tests/test_base.py

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