File: test_devices.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 (120 lines) | stat: -rw-r--r-- 3,580 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
import numpy as np
import pytest
from numpy.testing import assert_equal

from brian2.core.magic import run
from brian2.devices.device import (
    Device,
    RuntimeDevice,
    all_devices,
    get_device,
    reset_device,
    runtime_device,
    set_device,
)
from brian2.groups.neurongroup import NeuronGroup
from brian2.units import ms


class ATestDevice(Device):
    def activate(self, build_on_run, **kwargs):
        super().activate(build_on_run, **kwargs)
        self.build_on_run = build_on_run
        self._options = kwargs

    # These functions are needed during the setup of the defaultclock
    def get_value(self, var):
        return np.array([0.0001])

    def add_array(self, var):
        pass

    def init_with_zeros(self, var, dtype):
        pass

    def fill_with_array(self, var, arr):
        pass


@pytest.mark.codegen_independent
def test_set_reset_device_implicit():
    from brian2.devices import device_module

    old_prev_devices = list(device_module.previous_devices)
    device_module.previous_devices = []
    test_device1 = ATestDevice()
    all_devices["test1"] = test_device1
    test_device2 = ATestDevice()
    all_devices["test2"] = test_device2

    set_device("test1", build_on_run=False, my_opt=1)
    set_device("test2", build_on_run=True, my_opt=2)
    assert get_device() is test_device2
    assert get_device()._options["my_opt"] == 2
    assert get_device().build_on_run

    reset_device()
    assert get_device() is test_device1
    assert get_device()._options["my_opt"] == 1
    assert not get_device().build_on_run

    reset_device()
    assert get_device() is runtime_device

    reset_device()  # If there is no previous device, will reset to runtime device
    assert get_device() is runtime_device
    del all_devices["test1"]
    del all_devices["test2"]
    device_module.previous_devices = old_prev_devices


@pytest.mark.codegen_independent
def test_set_reset_device_explicit():
    original_device = get_device()
    test_device1 = ATestDevice()
    all_devices["test1"] = test_device1
    test_device2 = ATestDevice()
    all_devices["test2"] = test_device2
    test_device3 = ATestDevice()
    all_devices["test3"] = test_device3

    set_device("test1", build_on_run=False, my_opt=1)
    set_device("test2", build_on_run=True, my_opt=2)
    set_device("test3", build_on_run=False, my_opt=3)

    reset_device("test1")  # Directly jump back to the first device
    assert get_device() is test_device1
    assert get_device()._options["my_opt"] == 1
    assert not get_device().build_on_run

    del all_devices["test1"]
    del all_devices["test2"]
    del all_devices["test3"]
    reset_device(original_device)


@pytest.mark.skipif(
    not isinstance(get_device(), RuntimeDevice),
    reason="Getting/setting random number state only supported for runtime device.",
)
def test_get_set_random_generator_state():
    group = NeuronGroup(10, "dv/dt = -v/(10*ms) + (10*ms)**-0.5*xi : 1", method="euler")
    group.v = "rand()"
    run(10 * ms)
    assert np.var(group.v) > 0  # very basic test for randomness ;)
    old_v = np.array(group.v)
    random_state = get_device().get_random_state()
    group.v = "rand()"
    run(10 * ms)
    assert np.var(group.v - old_v) > 0  # just checking for *some* difference
    old_v = np.array(group.v)
    get_device().set_random_state(random_state)
    group.v = "rand()"
    run(10 * ms)
    assert_equal(group.v, old_v)


if __name__ == "__main__":
    test_set_reset_device_implicit()
    test_set_reset_device_explicit()
    test_get_set_random_generator_state()