| 1 |
import pytest |
| 2 |
from numpy.testing import assert_array_equal, assert_equal |
| 3 |
|
| 4 |
from brian2 import * |
| 5 |
from brian2.core.clocks import EventClock |
| 6 |
from brian2.tests.test_network import NameLister |
| 7 |
from brian2.units.fundamentalunits import DimensionMismatchError |
| 8 |
from brian2.utils.logger import catch_logs |
| 9 |
|
| 10 |
|
| 11 |
@pytest.mark.codegen_independent |
| 12 |
def test_clock_attributes(): |
| 13 |
clock = Clock(dt=1 * ms) |
| 14 |
assert_array_equal(clock.t, 0 * second) |
| 15 |
assert_array_equal(clock.timestep, 0) |
| 16 |
assert_array_equal(clock.dt, 1 * ms) |
| 17 |
|
| 18 |
|
| 19 |
@pytest.mark.codegen_independent |
| 20 |
def test_clock_comparisons(): |
| 21 |
clock1 = Clock(dt=1 * ms) |
| 22 |
clock2 = Clock(dt=2 * ms) |
| 23 |
event_clock1 = EventClock(times=[0 * ms, 1 * ms]) |
| 24 |
event_clock2 = EventClock(times=[0 * ms, 2 * ms]) |
| 25 |
|
| 26 |
clocks = [clock1, clock2, event_clock1, event_clock2] |
| 27 |
assert clock1.same_time(clock2) and clock2.same_time(clock1) |
| 28 |
assert clock1.same_time(event_clock1) and event_clock1.same_time(clock1) |
| 29 |
assert event_clock1.same_time(event_clock2) and event_clock2.same_time(event_clock1) |
| 30 |
|
| 31 |
for clock in clocks: |
| 32 |
clock.advance() |
| 33 |
assert clock1.same_time(event_clock1) and event_clock1.same_time(clock1) |
| 34 |
assert clock2.same_time(event_clock2) and event_clock2.same_time(clock2) |
| 35 |
assert not (clock1.same_time(clock2) or clock2.same_time(clock1)) |
| 36 |
assert not (clock1.same_time(event_clock2) or event_clock2.same_time(clock1)) |
| 37 |
assert not ( |
| 38 |
event_clock1.same_time(event_clock2) or event_clock2.same_time(event_clock1) |
| 39 |
) |
| 40 |
|
| 41 |
|
| 42 |
@pytest.mark.codegen_independent |
| 43 |
def test_clock_set_interval(): |
| 44 |
clock = Clock(dt=1 * ms) |
| 45 |
clock.set_interval(0 * ms, 2 * ms) |
| 46 |
assert clock.t == 0 * ms |
| 47 |
clock.advance() |
| 48 |
assert clock.t == 1 * ms |
| 49 |
clock.advance() |
| 50 |
assert clock.t == 2 * ms |
| 51 |
with pytest.raises(StopIteration): |
| 52 |
clock.advance() |
| 53 |
assert clock.t == 2 * ms |
| 54 |
|
| 55 |
|
| 56 |
@pytest.mark.codegen_independent |
| 57 |
def test_clock_dt_change(): |
| 58 |
clock = Clock(dt=1 * ms) |
| 59 |
# at time 0s, all dt changes should be allowed |
| 60 |
clock.dt = 0.75 * ms |
| 61 |
clock._set_t_update_dt() |
| 62 |
clock.dt = 2.5 * ms |
| 63 |
clock._set_t_update_dt() |
| 64 |
clock.dt = 1 * ms |
| 65 |
clock._set_t_update_dt() |
| 66 |
|
| 67 |
# at 0.1ms only changes that are still representable as an integer of the |
| 68 |
# current time 1s are allowed |
| 69 |
clock.dt = 0.1 * ms |
| 70 |
clock._set_t_update_dt() |
| 71 |
|
| 72 |
clock.dt = 0.05 * ms |
| 73 |
clock._set_t_update_dt(target_t=0.1 * ms) |
| 74 |
clock.dt = 0.1 * ms |
| 75 |
clock._set_t_update_dt(target_t=0.1 * ms) |
| 76 |
clock.dt = 0.3 * ms |
| 77 |
with pytest.raises(ValueError): |
| 78 |
clock._set_t_update_dt(target_t=0.1 * ms) |
| 79 |
|
| 80 |
|
| 81 |
@pytest.mark.codegen_independent |
| 82 |
def test_defaultclock(): |
| 83 |
defaultclock.dt = 1 * ms |
| 84 |
assert_equal(defaultclock.dt, 1 * ms) |
| 85 |
assert defaultclock.name == "defaultclock" |
| 86 |
|
| 87 |
|
| 88 |
@pytest.mark.codegen_independent |
| 89 |
def test_set_interval_warning(): |
| 90 |
clock = Clock(dt=0.1 * ms) |
| 91 |
with catch_logs() as logs: |
| 92 |
clock.set_interval(0 * second, 1000 * second) # no problem |
| 93 |
assert len(logs) == 0 |
| 94 |
with catch_logs() as logs: |
| 95 |
clock.set_interval(0 * second, 10000000000 * second) # too long |
| 96 |
assert len(logs) == 1 |
| 97 |
assert logs[0][1].endswith("many_timesteps") |
| 98 |
|
| 99 |
|
| 100 |
@pytest.mark.codegen_independent |
| 101 |
def test_event_clock(): |
| 102 |
times = [0.0 * ms, 0.3 * ms, 0.5 * ms, 0.6 * ms] |
| 103 |
event_clock = EventClock(times) |
| 104 |
|
| 105 |
assert_equal(event_clock.variables["t"].get_value(), 0.0 * ms) |
| 106 |
assert_equal(event_clock[1], 0.3 * ms) |
| 107 |
|
| 108 |
event_clock.advance() |
| 109 |
assert_equal(event_clock.variables["timestep"].get_value(), 1) |
| 110 |
assert_equal(event_clock.variables["t"].get_value(), 0.0003) |
| 111 |
|
| 112 |
event_clock.set_interval(0.3 * ms, 0.6 * ms) |
| 113 |
assert_equal(event_clock.variables["timestep"].get_value(), 1) |
| 114 |
assert_equal(event_clock.variables["t"].get_value(), 0.0003) |
| 115 |
event_clock.advance() |
| 116 |
event_clock.advance() |
| 117 |
|
| 118 |
with pytest.raises(StopIteration): |
| 119 |
event_clock.advance() |
| 120 |
|
| 121 |
invalid_times = [0.0 * volt, 0.5 * volt] |
| 122 |
with pytest.raises(DimensionMismatchError) as excinfo: |
| 123 |
EventClock(invalid_times) |
| 124 |
|
| 125 |
|
| 126 |
@pytest.mark.codegen_independent |
| 127 |
def test_combined_clocks_with_run_at(): |
| 128 |
# Reset updates |
| 129 |
NameLister.updates[:] = [] |
| 130 |
|
| 131 |
# Regular NameLister at 1ms interval |
| 132 |
regular_lister = NameLister(name="x", dt=1 * ms, order=0) |
| 133 |
|
| 134 |
# Event NameLister at specific times |
| 135 |
event_times = [0.5 * ms, 2.5 * ms, 4 * ms] |
| 136 |
event_lister = NameLister(name="y", clock=EventClock(times=event_times), order=1) |
| 137 |
|
| 138 |
# Create and run the network |
| 139 |
net = Network(regular_lister, event_lister) |
| 140 |
net.run(5 * ms) |
| 141 |
|
| 142 |
# Get update string |
| 143 |
updates = "".join(NameLister.updates) |
| 144 |
|
| 145 |
# Expected output: "x" at 0,1,2,3,4ms = 5 times |
| 146 |
# "y" at 0.5, 2.5, 4.0ms = 3 times |
| 147 |
expected_x_count = 5 |
| 148 |
expected_y_count = 3 |
| 149 |
|
| 150 |
x_count = updates.count("x") |
| 151 |
y_count = updates.count("y") |
| 152 |
|
| 153 |
assert x_count == expected_x_count, ( |
| 154 |
f"Expected {expected_x_count} x's, got {x_count}" |
| 155 |
) |
| 156 |
assert y_count == expected_y_count, ( |
| 157 |
f"Expected {expected_y_count} y's, got {y_count}" |
| 158 |
) |
| 159 |
|
| 160 |
expected_output = "xyxxyxxy" |
| 161 |
assert updates == expected_output, f"Expected {expected_output}, got {updates}" |
| 162 |
|
| 163 |
|
| 164 |
if __name__ == "__main__": |
| 165 |
test_clock_attributes() |
| 166 |
restore_initial_state() |
| 167 |
test_clock_dt_change() |
| 168 |
restore_initial_state() |
| 169 |
test_defaultclock() |
| 170 |
test_set_interval_warning() |
| 171 |
test_event_clock() |
| 172 |
test_combined_clocks_with_run_at() |