File: test_network.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 (1880 lines) | stat: -rw-r--r-- 55,641 bytes parent folder | download
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
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
import copy
import logging
import os
import tempfile
import uuid
import weakref

import numpy as np
import pytest
from numpy.testing import assert_array_equal, assert_equal

from brian2 import (
    BrianLogger,
    BrianObject,
    Clock,
    Hz,
    MagicError,
    MagicNetwork,
    Network,
    NetworkOperation,
    NeuronGroup,
    PoissonGroup,
    PopulationRateMonitor,
    Quantity,
    SpikeGeneratorGroup,
    SpikeMonitor,
    StateMonitor,
    Synapses,
    TimedArray,
    collect,
    defaultclock,
    magic_network,
    ms,
    network_operation,
    prefs,
    profiling_summary,
    restore,
    run,
    second,
    start_scope,
    stop,
    store,
    us,
)
from brian2.core.network import schedule_propagation_offset, scheduling_summary
from brian2.devices.device import (
    Device,
    RuntimeDevice,
    all_devices,
    device,
    get_device,
    reinit_and_delete,
    reset_device,
    set_device,
)
from brian2.tests.utils import assert_allclose
from brian2.utils.logger import catch_logs


@pytest.mark.codegen_independent
def test_incorrect_network_use():
    """Test some wrong uses of `Network` and `MagicNetwork`"""
    with pytest.raises(TypeError):
        Network(name="mynet", anotherkwd="does not exist")
    with pytest.raises(TypeError):
        Network("not a BrianObject")
    net = Network()
    with pytest.raises(TypeError):
        net.add("not a BrianObject")
    with pytest.raises(ValueError):
        MagicNetwork()
    G = NeuronGroup(10, "v:1")
    net.add(G)
    with pytest.raises(TypeError):
        net.remove(object())
    with pytest.raises(MagicError):
        magic_network.add(G)
    with pytest.raises(MagicError):
        magic_network.remove(G)


@pytest.mark.codegen_independent
def test_network_contains():
    """
    Test `Network.__contains__`.
    """
    G = NeuronGroup(1, "v:1", name="mygroup")
    net = Network(G)
    assert "mygroup" in net
    assert "neurongroup" not in net


@pytest.mark.codegen_independent
def test_empty_network():
    # Check that an empty network functions correctly
    net = Network()
    net.run(1 * second)


class Counter(BrianObject):
    add_to_magic_network = True

    def __init__(self, **kwds):
        super().__init__(**kwds)
        self.count = 0
        self.state = {"state": 0}

    def get_states(self, *args, **kwds):
        return dict(self.state)

    def set_states(self, values, *args, **kwds):
        for k, v in values.items():
            self.state[k] = v

    def run(self):
        self.count += 1


class CounterWithContained(Counter):
    add_to_magic_network = True

    def __init__(self, **kwds):
        super().__init__(**kwds)
        self.sub_counter = Counter()
        self.contained_objects.append(self.sub_counter)


@pytest.mark.codegen_independent
def test_network_single_object():
    # Check that a network with a single object functions correctly
    x = Counter()
    net = Network(x)
    net.run(1 * ms)
    assert_equal(x.count, 10)


@pytest.mark.codegen_independent
def test_network_two_objects():
    # Check that a network with two objects and the same clock function correctly
    x = Counter(order=5)
    y = Counter(order=6)
    net = Network()
    net.add([x, [y]])  # check that a funky way of adding objects work correctly
    net.run(1 * ms)
    assert_equal(len(net.objects), 2)
    assert_equal(x.count, 10)
    assert_equal(y.count, 10)


@pytest.mark.codegen_independent
def test_network_from_dict():
    # Check that a network from a dictionary works
    x = Counter()
    y = Counter()
    d = dict(a=x, b=y)
    net = Network()
    net.add(d)
    net.run(1 * ms)
    assert_equal(len(net.objects), 2)
    assert_equal(x.count, 10)
    assert_equal(y.count, 10)


class NameLister(BrianObject):
    add_to_magic_network = True
    updates = []

    def __init__(self, **kwds):
        super().__init__(**kwds)

    def run(self):
        NameLister.updates.append(self.name)


@pytest.mark.codegen_independent
def test_network_different_clocks():
    NameLister.updates[:] = []
    # Check that a network with two different clocks functions correctly
    x = NameLister(name="x", dt=0.1 * ms, order=0)
    y = NameLister(name="y", dt=1 * ms, order=1)
    net = Network(x, y)
    net.run(100 * second + defaultclock.dt, report="text")
    updates = "".join(NameLister.updates)[2:]  # ignore the first time step
    assert updates == ("xxxxxxxxxxy" * 100000)


@pytest.mark.codegen_independent
def test_network_different_when():
    # Check that a network with different when attributes functions correctly
    NameLister.updates[:] = []
    x = NameLister(name="x", when="start")
    y = NameLister(name="y", when="end")
    net = Network(x, y)
    net.run(0.3 * ms)
    assert_equal("".join(NameLister.updates), "xyxyxy")


@pytest.mark.codegen_independent
def test_network_default_schedule():
    net = Network()
    assert net.schedule == [
        "start",
        "groups",
        "thresholds",
        "synapses",
        "resets",
        "end",
    ]
    # Set the preference and check that the change is taken into account
    prefs.core.network.default_schedule = list(
        reversed(["start", "groups", "thresholds", "synapses", "resets", "end"])
    )
    assert net.schedule == list(
        reversed(["start", "groups", "thresholds", "synapses", "resets", "end"])
    )


@pytest.mark.codegen_independent
def test_network_schedule_change():
    # Check that a changed schedule is taken into account correctly
    NameLister.updates[:] = []
    x = NameLister(name="x", when="thresholds")
    y = NameLister(name="y", when="resets")
    net = Network(x, y)
    net.run(0.3 * ms)
    assert_equal("".join(NameLister.updates), "xyxyxy")
    NameLister.updates[:] = []
    net.schedule = ["start", "groups", "synapses", "resets", "thresholds", "end"]
    net.run(0.3 * ms)
    assert_equal("".join(NameLister.updates), "yxyxyx")


@pytest.mark.codegen_independent
def test_network_before_after_schedule():
    # Test that before... and after... slot names can be used
    NameLister.updates[:] = []
    x = NameLister(name="x", when="before_resets")
    y = NameLister(name="y", when="after_thresholds")
    net = Network(x, y)
    net.schedule = ["thresholds", "resets", "end"]
    net.run(0.3 * ms)
    assert_equal("".join(NameLister.updates), "yxyxyx")


@pytest.mark.codegen_independent
def test_network_custom_slots():
    # Check that custom slots can be inserted into the schedule
    NameLister.updates[:] = []
    x = NameLister(name="x", when="thresholds")
    y = NameLister(name="y", when="in_between")
    z = NameLister(name="z", when="resets")
    net = Network(x, y, z)
    net.schedule = [
        "start",
        "groups",
        "thresholds",
        "in_between",
        "synapses",
        "resets",
        "end",
    ]
    net.run(0.3 * ms)
    assert_equal("".join(NameLister.updates), "xyzxyzxyz")


@pytest.mark.codegen_independent
def test_network_incorrect_schedule():
    # Test that incorrect arguments provided to schedule raise errors
    net = Network()
    # net.schedule = object()
    with pytest.raises(TypeError):
        setattr(net, "schedule", object())
    # net.schedule = 1
    with pytest.raises(TypeError):
        setattr(net, "schedule", 1)
    # net.schedule = {'slot1', 'slot2'}
    with pytest.raises(TypeError):
        setattr(net, "schedule", {"slot1", "slot2"})
    # net.schedule = ['slot', 1]
    with pytest.raises(TypeError):
        setattr(net, "schedule", ["slot", 1])
    # net.schedule = ['start', 'after_start']
    with pytest.raises(ValueError):
        setattr(net, "schedule", ["start", "after_start"])
    # net.schedule = ['before_start', 'start']
    with pytest.raises(ValueError):
        setattr(net, "schedule", ["before_start", "start"])


@pytest.mark.codegen_independent
def test_schedule_warning():
    previous_device = get_device()
    from uuid import uuid4

    # TestDevice1 supports arbitrary schedules, TestDevice2 does not
    class TestDevice1(Device):
        # 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

    class TestDevice2(TestDevice1):
        def __init__(self):
            super().__init__()
            self.network_schedule = [
                "start",
                "groups",
                "synapses",
                "thresholds",
                "resets",
                "end",
            ]

    # Unique names are important for getting the warnings again for multiple
    # runs of the test suite
    name1 = f"testdevice_{str(uuid4())}"
    name2 = f"testdevice_{str(uuid4())}"
    all_devices[name1] = TestDevice1()
    all_devices[name2] = TestDevice2()

    set_device(name1)
    assert schedule_propagation_offset() == 0 * ms
    net = Network()
    assert schedule_propagation_offset(net) == 0 * ms

    # Any schedule should work
    net.schedule = list(reversed(net.schedule))
    with catch_logs() as l:
        net.run(0 * ms)
        assert len(l) == 0, "did not expect a warning"

    assert schedule_propagation_offset(net) == defaultclock.dt

    set_device(name2)
    assert schedule_propagation_offset() == defaultclock.dt

    # Using the correct schedule should work
    net.schedule = ["start", "groups", "synapses", "thresholds", "resets", "end"]
    with catch_logs() as l:
        net.run(0 * ms)
        assert len(l) == 0, "did not expect a warning"
    assert schedule_propagation_offset(net) == defaultclock.dt

    # Using another (e.g. the default) schedule should raise a warning
    net.schedule = None
    with catch_logs() as l:
        net.run(0 * ms)
        assert len(l) == 1 and l[0][1].endswith("schedule_conflict")
    reset_device(previous_device)


@pytest.mark.codegen_independent
def test_scheduling_summary_magic():
    basename = f"name{str(uuid.uuid4()).replace('-', '_')}"
    group = NeuronGroup(
        10, "dv/dt = -v/(10*ms) : 1", threshold="v>1", reset="v=1", name=basename
    )
    group.run_regularly("v = rand()", dt=defaultclock.dt * 10, when="end")
    state_mon = StateMonitor(group, "v", record=True, name=f"{basename}_sm")
    inactive_state_mon = StateMonitor(
        group, "v", record=True, name=f"{basename}_sm_ia", when="after_end"
    )
    inactive_state_mon.active = False
    summary_before = scheduling_summary()

    assert [entry.name for entry in summary_before.entries] == [
        f"{basename}_sm",
        f"{basename}_stateupdater",
        f"{basename}_spike_thresholder",
        f"{basename}_spike_resetter",
        f"{basename}_run_regularly",
        f"{basename}_sm_ia",
    ]
    assert [entry.when for entry in summary_before.entries] == [
        "start",
        "groups",
        "thresholds",
        "resets",
        "end",
        "after_end",
    ]
    assert [entry.dt for entry in summary_before.entries] == [
        defaultclock.dt,
        defaultclock.dt,
        defaultclock.dt,
        defaultclock.dt,
        defaultclock.dt * 10,
        defaultclock.dt,
    ]
    assert [entry.active for entry in summary_before.entries] == [
        True,
        True,
        True,
        True,
        True,
        False,
    ]
    assert len(str(summary_before))
    assert len(summary_before._repr_html_())
    run(defaultclock.dt)
    summary_after = scheduling_summary()
    assert str(summary_after) == str(summary_before)
    assert summary_after._repr_html_() == summary_before._repr_html_()


@pytest.mark.codegen_independent
def test_scheduling_summary():
    basename = f"name{str(uuid.uuid4()).replace('-', '_')}"
    group = NeuronGroup(
        10, "dv/dt = -v/(10*ms) : 1", threshold="v>1", reset="v=1", name=basename
    )
    group.run_regularly("v = rand()", dt=defaultclock.dt * 10, when="end")
    state_mon = StateMonitor(group, "v", record=True, name=f"{basename}_sm")
    inactive_state_mon = StateMonitor(
        group, "v", record=True, name=f"{basename}_sm_ia", when="after_end"
    )
    inactive_state_mon.active = False

    @network_operation(name=f"{basename}_net_op", when="before_end")
    def foo():
        pass

    net = Network(group, state_mon, inactive_state_mon, foo)
    summary_before = scheduling_summary(net)
    assert [entry.name for entry in summary_before.entries] == [
        f"{basename}_sm",
        f"{basename}_stateupdater",
        f"{basename}_spike_thresholder",
        f"{basename}_spike_resetter",
        f"{basename}_net_op",
        f"{basename}_run_regularly",
        f"{basename}_sm_ia",
    ]
    assert [entry.when for entry in summary_before.entries] == [
        "start",
        "groups",
        "thresholds",
        "resets",
        "before_end",
        "end",
        "after_end",
    ]
    assert [entry.dt for entry in summary_before.entries] == [
        defaultclock.dt,
        defaultclock.dt,
        defaultclock.dt,
        defaultclock.dt,
        defaultclock.dt,
        defaultclock.dt * 10,
        defaultclock.dt,
    ]
    assert [entry.active for entry in summary_before.entries] == [
        True,
        True,
        True,
        True,
        True,
        True,
        False,
    ]
    assert len(str(summary_before))
    assert len(summary_before._repr_html_())
    run(defaultclock.dt)
    summary_after = scheduling_summary(net)
    assert str(summary_after) == str(summary_before)
    assert summary_after._repr_html_() == summary_before._repr_html_()


class Preparer(BrianObject):
    add_to_magic_network = True

    def __init__(self, **kwds):
        super().__init__(**kwds)
        self.did_reinit = False
        self.did_pre_run = False
        self.did_post_run = False

    def reinit(self, level=0):
        self.did_reinit = True

    def before_run(self, namespace=None, level=0):
        self.did_pre_run = True

    def after_run(self):
        self.did_post_run = True


@pytest.mark.codegen_independent
def test_magic_network():
    # test that magic network functions correctly
    x = Counter()
    y = Counter()
    run(10 * ms)
    assert_equal(x.count, 100)
    assert_equal(y.count, 100)

    assert len(repr(magic_network))  # very basic test...
    assert len(str(magic_network))  # very basic test...


class Stopper(BrianObject):
    add_to_magic_network = True

    def __init__(self, stoptime, stopfunc, **kwds):
        super().__init__(**kwds)
        self.stoptime = stoptime
        self.stopfunc = stopfunc

    def run(self):
        self.stoptime -= 1
        if self.stoptime <= 0:
            self.stopfunc()


@pytest.mark.codegen_independent
def test_network_stop():
    # test that Network.stop and global stop() work correctly
    net = Network()
    x = Stopper(10, net.stop)
    net.add(x)
    net.run(10 * ms)
    assert_equal(defaultclock.t, 1 * ms)

    x = Stopper(10, stop)
    net = Network(x)
    net.run(10 * ms)
    assert_equal(defaultclock.t, 1 * ms)


@pytest.mark.codegen_independent
def test_network_operations():
    # test NetworkOperation and network_operation
    seq = []

    def f1():
        seq.append("a")

    op1 = NetworkOperation(f1, when="start", order=1)

    @network_operation
    def f2():
        seq.append("b")

    @network_operation(when="end", order=1)
    def f3():
        seq.append("c")

    # In complex frameworks, network operations might be object methods that
    # access some common data
    class Container:
        def __init__(self):
            self.g1_data = "B"
            self.g2_data = "C"

        def g1(self):
            seq.append(self.g1_data)

        def g2(self):
            seq.append(self.g2_data)

    c = Container()
    c_op1 = NetworkOperation(c.g1)
    c_op2 = NetworkOperation(c.g2, when="end", order=1)
    net = Network(op1, f2, f3, c_op1, c_op2)
    net.run(1 * ms)

    assert_equal("".join(seq), "bBacC" * 10)


@pytest.mark.codegen_independent
def test_incorrect_network_operations():
    # Network operations with more than one argument are not allowed
    def func(x, y):
        pass

    class Container:
        def func(self, x, y):
            pass

    c = Container()

    with pytest.raises(TypeError):
        NetworkOperation(func)
    with pytest.raises(TypeError):
        NetworkOperation(c.func)

    # Incorrect use of @network_operation -- it does not work on an instance
    # method
    try:

        class Container:
            @network_operation
            def func(self):
                pass

        raise AssertionError("expected a TypeError")
    except TypeError:
        pass  # this is what we expected


@pytest.mark.codegen_independent
def test_network_operations_name():
    # test NetworkOperation name input
    seq = []

    def f1():
        seq.append("a")

    def f2():
        seq.append("b")

    def x():
        pass

    op = NetworkOperation(lambda: x)
    assert_equal(op.name, "networkoperation")

    op0 = NetworkOperation(lambda: x, name="named_network")
    assert_equal(op0.name, "named_network")

    op1 = NetworkOperation(f1, name="networkoperation_1")
    op2 = NetworkOperation(f1, name="networkoperation_3")
    op3 = NetworkOperation(f2, name="networkoperation_2")

    net = Network(op1, op2, op3)
    net.run(1 * ms)

    assert_equal("".join(seq), "aba" * 10)


@pytest.mark.codegen_independent
def test_network_active_flag():
    # test that the BrianObject.active flag is recognised by Network.run
    x = Counter()
    y = Counter()
    y.active = False
    run(1 * ms)
    assert_equal(x.count, 10)
    assert_equal(y.count, 0)


@pytest.mark.standalone_compatible
@pytest.mark.multiple_runs
def test_spikes_after_deactivating():
    # Make sure that a spike in the last time step gets cleared. See #1319
    always_spike = NeuronGroup(1, "", threshold="True", reset="")
    spike_mon = SpikeMonitor(always_spike)
    run(defaultclock.dt)
    always_spike.active = False
    run(defaultclock.dt)
    device.build(direct_call=False, **device.build_options)
    assert_equal(spike_mon.t[:], [0] * second)


@pytest.mark.codegen_independent
def test_network_t():
    # test that Network.t works as expected
    x = Counter(dt=1 * ms)
    y = Counter(dt=2 * ms)
    net = Network(x, y)
    net.run(4 * ms)
    assert_equal(net.t, 4 * ms)
    net.run(1 * ms)
    assert_equal(net.t, 5 * ms)
    assert_equal(x.count, 5)
    assert_equal(y.count, 3)
    net.run(0.5 * ms)  # should only update x
    assert_equal(net.t, 5.5 * ms)
    assert_equal(x.count, 6)
    assert_equal(y.count, 3)
    net.run(0.5 * ms)  # shouldn't do anything
    assert_equal(net.t, 6 * ms)
    assert_equal(x.count, 6)
    assert_equal(y.count, 3)
    net.run(0.5 * ms)  # should update x and y
    assert_equal(net.t, 6.5 * ms)
    assert_equal(x.count, 7)
    assert_equal(y.count, 4)

    del x, y, net

    # now test with magic run
    x = Counter(dt=1 * ms)
    y = Counter(dt=2 * ms)
    run(4 * ms)
    assert_equal(magic_network.t, 4 * ms)
    assert_equal(x.count, 4)
    assert_equal(y.count, 2)
    run(4 * ms)
    assert_equal(magic_network.t, 8 * ms)
    assert_equal(x.count, 8)
    assert_equal(y.count, 4)
    run(1 * ms)
    assert_equal(magic_network.t, 9 * ms)
    assert_equal(x.count, 9)
    assert_equal(y.count, 5)


@pytest.mark.codegen_independent
def test_incorrect_dt_defaultclock():
    defaultclock.dt = 0.5 * ms
    G = NeuronGroup(1, "dv/dt = -v / (10*ms) : 1")
    net = Network(G)
    net.run(0.5 * ms)
    defaultclock.dt = 1 * ms
    with pytest.raises(ValueError):
        net.run(0 * ms)


@pytest.mark.codegen_independent
def test_incorrect_dt_custom_clock():
    clock = Clock(dt=0.5 * ms)
    G = NeuronGroup(1, "dv/dt = -v / (10*ms) : 1", clock=clock)
    net = Network(G)
    net.run(0.5 * ms)
    clock.dt = 1 * ms
    with pytest.raises(ValueError):
        net.run(0 * ms)


@pytest.mark.codegen_independent
def test_network_remove():
    x = Counter()
    y = Counter()
    net = Network(x, y)
    net.remove(y)
    net.run(1 * ms)
    assert_equal(x.count, 10)
    assert_equal(y.count, 0)
    # the relevance of this test is when we use weakref.proxy objects in
    # Network.objects, we should be able to add and remove these from
    # the Network just as much as the original objects
    # TODO: Does this test make sense now that Network does not store weak
    #       references by default?
    for obj in copy.copy(net.objects):
        net.remove(obj)
    net.run(1 * ms)
    assert_equal(x.count, 10)
    assert_equal(y.count, 0)


@pytest.mark.codegen_independent
def test_contained_objects():
    obj = CounterWithContained()
    net = Network(obj)
    # The contained object should not be stored explicitly
    assert len(net.objects) == 1
    # It should be accessible via the network interface, though
    assert len(net) == 2
    net.run(defaultclock.dt)

    # The contained object should be executed during the run
    assert obj.count == 1
    assert obj.sub_counter.count == 1

    # contained objects should be accessible via get_states/set_states
    states = net.get_states()
    assert len(states) == 2
    assert set(states.keys()) == {obj.name, obj.sub_counter.name}
    assert set(states[obj.name].keys()) == {"state"}
    assert set(states[obj.sub_counter.name].keys()) == {"state"}
    net[obj.name].set_states({"state": 1})
    net[obj.sub_counter.name].set_states({"state": 2})

    net.remove(obj)
    assert len(net.objects) == 0
    assert len(net) == 0
    assert len(net.get_states()) == 0
    net.run(defaultclock.dt)
    assert obj.count == 1
    assert obj.sub_counter.count == 1


class NoninvalidatingCounter(Counter):
    add_to_magic_network = True
    invalidates_magic_network = False


@pytest.mark.codegen_independent
def test_invalid_magic_network():
    x = Counter()
    run(1 * ms)
    assert_equal(x.count, 10)
    y = Counter()
    try:
        run(1 * ms)
        raise AssertionError("Expected a MagicError")
    except MagicError:
        pass  # this is expected
    del x, y
    x = Counter()
    run(1 * ms)
    y = NoninvalidatingCounter()
    run(1 * ms)
    assert_equal(x.count, 20)
    assert_equal(y.count, 10)
    del y
    run(1 * ms)
    assert_equal(x.count, 30)
    del x
    x = Counter()
    run(1 * ms)
    assert_equal(magic_network.t, 1 * ms)
    del x
    x = Counter()
    y = Counter()
    run(1 * ms)
    assert_equal(x.count, 10)
    assert_equal(y.count, 10)


@pytest.mark.codegen_independent
def test_multiple_networks_invalid():
    x = Counter()
    net = Network(x)
    net.run(1 * ms)
    try:
        run(1 * ms)
        raise AssertionError("Expected a RuntimeError")
    except RuntimeError:
        pass  # this is expected

    try:
        net2 = Network(x)
        raise AssertionError("Expected a RuntimeError")
    except RuntimeError:
        pass  # this is expected


@pytest.mark.codegen_independent
def test_magic_weak_reference():
    """
    Test that holding a weak reference to an object does not make it get
    simulated."""

    G1 = NeuronGroup(1, "v:1")

    # this object should not be included
    G2 = weakref.ref(NeuronGroup(1, "v:1"))

    with catch_logs(log_level=logging.DEBUG) as l:
        run(1 * ms)
        # Check the debug messages for the number of included objects
        magic_objects = [
            msg[2] for msg in l if msg[1] == "brian2.core.magic.magic_objects"
        ][0]
        assert "2 objects" in magic_objects, f"Unexpected log message: {magic_objects}"


@pytest.mark.codegen_independent
def test_magic_unused_object():
    """Test that creating unused objects does not affect the magic system."""

    def create_group():
        # Produce two objects but return only one
        G1 = NeuronGroup(1, "v:1")  # no Thresholder or Resetter
        G2 = NeuronGroup(1, "v:1")  # This object should be garbage collected
        return G1

    G = create_group()
    with catch_logs(log_level=logging.DEBUG) as l:
        run(1 * ms)

        # Check the debug messages for the number of included objects
        magic_objects = [
            msg[2] for msg in l if msg[1] == "brian2.core.magic.magic_objects"
        ][0]
        assert "2 objects" in magic_objects, f"Unexpected log message: {magic_objects}"


@pytest.mark.codegen_independent
def test_network_access():
    x = Counter(name="counter")
    net = Network(x)
    assert len(net) == 1
    assert len(repr(net))  # very basic test...
    assert len(str(net))  # very basic test...

    # accessing objects
    assert net["counter"] is x
    with pytest.raises(TypeError):
        net[123]
    with pytest.raises(TypeError):
        net[1:3]
    with pytest.raises(KeyError):
        net["non-existing"]

    objects = [obj for obj in net]
    assert set(objects) == set(net.objects)

    # deleting objects
    del net["counter"]
    with pytest.raises(TypeError):
        net.__delitem__(123)
    with pytest.raises(TypeError):
        net.__delitem__(slice(1, 3))
    with pytest.raises(KeyError):
        net.__delitem__("counter")


@pytest.mark.codegen_independent
def test_dependency_check():
    def create_net():
        G = NeuronGroup(10, "v: 1", threshold="False")
        dependent_objects = [
            StateMonitor(G, "v", record=True),
            SpikeMonitor(G),
            PopulationRateMonitor(G),
            Synapses(G, G, on_pre="v+=1"),
        ]
        return dependent_objects

    dependent_objects = create_net()
    # Trying to simulate the monitors/synapses without the group should fail
    for obj in dependent_objects:
        with pytest.raises(ValueError):
            Network(obj).run(0 * ms)

    # simulation with a magic network should work when we have an explicit
    # reference to one of the objects, but the object should be inactive and
    # we should get a warning
    assert all(obj.active for obj in dependent_objects)
    for obj in dependent_objects:  # obj is our explicit reference
        with catch_logs() as l:
            run(0 * ms)
            dependency_warnings = [
                msg[2] for msg in l if msg[1] == "brian2.core.magic.dependency_warning"
            ]
            assert len(dependency_warnings) == 1
        assert not obj.active


def test_loop():
    """
    Somewhat realistic test with a loop of magic networks
    """

    def run_simulation():
        G = NeuronGroup(10, "dv/dt = -v / (10*ms) : 1", reset="v=0", threshold="v>1")
        G.v = np.linspace(0, 1, 10)
        run(1 * ms)
        # We return potentially problematic references to a VariableView
        return G.v

    # First run
    with catch_logs(log_level=logging.DEBUG) as l:
        v = run_simulation()
        assert v[0] == 0 and 0 < v[-1] < 1
        # Check the debug messages for the number of included objects
        magic_objects = [
            msg[2] for msg in l if msg[1] == "brian2.core.magic.magic_objects"
        ][0]
        assert "4 objects" in magic_objects

    # Second run
    with catch_logs(log_level=logging.DEBUG) as l:
        v = run_simulation()
        assert v[0] == 0 and 0 < v[-1] < 1
        # Check the debug messages for the number of included objects
        magic_objects = [
            msg[2] for msg in l if msg[1] == "brian2.core.magic.magic_objects"
        ][0]
        assert "4 objects" in magic_objects


@pytest.mark.codegen_independent
def test_magic_collect():
    """
    Make sure all expected objects are collected in a magic network
    """
    P = PoissonGroup(10, rates=100 * Hz)
    G = NeuronGroup(10, "v:1", threshold="False")
    S = Synapses(G, G, "")

    state_mon = StateMonitor(G, "v", record=True)
    spike_mon = SpikeMonitor(G)
    rate_mon = PopulationRateMonitor(G)

    objects = collect()

    assert len(objects) == 6, f"expected {int(6)} objects, got {len(objects)}"


import sys
from contextlib import contextmanager
from io import BytesIO, StringIO


@contextmanager
def captured_output():
    new_out, new_err = StringIO(), StringIO()
    old_out, old_err = sys.stdout, sys.stderr
    try:
        sys.stdout, sys.stderr = new_out, new_err
        yield sys.stdout, sys.stderr
    finally:
        sys.stdout, sys.stderr = old_out, old_err


@pytest.mark.codegen_independent
def test_progress_report():
    """
    Very basic test of progress reporting
    """
    G = NeuronGroup(1, "")
    net = Network(G)

    # No output
    with captured_output() as (out, err):
        net.run(1 * ms, report=None)
    # There should be at least two lines of output
    out, err = out.getvalue(), err.getvalue()
    assert len(out) == 0 and len(err) == 0

    with captured_output() as (out, err):
        net.run(1 * ms)
    # There should be at least two lines of output
    out, err = out.getvalue(), err.getvalue()
    assert len(out) == 0 and len(err) == 0

    # Progress should go to stdout
    with captured_output() as (out, err):
        net.run(1 * ms, report="text")
    # There should be at least two lines of output
    out, err = out.getvalue(), err.getvalue()
    assert len(out.split("\n")) >= 2 and len(err) == 0

    with captured_output() as (out, err):
        net.run(1 * ms, report="stdout")
    # There should be at least two lines of output
    out, err = out.getvalue(), err.getvalue()
    assert len(out.split("\n")) >= 2 and len(err) == 0

    # Progress should go to stderr
    with captured_output() as (out, err):
        net.run(1 * ms, report="stderr")
    # There should be at least two lines of output
    out, err = out.getvalue(), err.getvalue()
    assert len(err.split("\n")) >= 2 and len(out) == 0

    # Custom function
    calls = []

    def capture_progress(elapsed, complete, start, duration):
        calls.append((elapsed, complete, start, duration))

    with captured_output() as (out, err):
        net.run(1 * ms, report=capture_progress)
    out, err = out.getvalue(), err.getvalue()

    assert len(err) == 0 and len(out) == 0
    # There should be at least a call for the start and the end
    assert len(calls) >= 2 and calls[0][1] == 0.0 and calls[-1][1] == 1.0


@pytest.mark.codegen_independent
def test_progress_report_incorrect():
    """
    Test wrong use of the report option
    """
    G = NeuronGroup(1, "")
    net = Network(G)
    with pytest.raises(ValueError):
        net.run(1 * ms, report="unknown")
    with pytest.raises(TypeError):
        net.run(1 * ms, report=object())


@pytest.mark.standalone_compatible
@pytest.mark.multiple_runs
def test_multiple_runs_report_standalone():
    group = NeuronGroup(1, "dv/dt = 1*Hz : 1")
    run(1 * ms, report="text")
    run(1 * ms)
    device.build(direct_call=False, **device.build_options)


@pytest.mark.standalone_compatible
@pytest.mark.multiple_runs
def test_multiple_runs_report_standalone_2():
    group = NeuronGroup(1, "dv/dt = 1*Hz : 1")
    run(1 * ms)
    run(1 * ms, report="text")
    device.build(direct_call=False, **device.build_options)


@pytest.mark.standalone_compatible
@pytest.mark.multiple_runs
def test_multiple_runs_report_standalone_3():
    group = NeuronGroup(1, "dv/dt = 1*Hz : 1")
    run(1 * ms, report="text")
    run(1 * ms, report="text")
    device.build(direct_call=False, **device.build_options)


# This tests a specific limitation of the C++ standalone mode (cannot mix
# multiple report methods)
@pytest.mark.cpp_standalone
@pytest.mark.standalone_only
def test_multiple_runs_report_standalone_incorrect():
    set_device("cpp_standalone", build_on_run=False)
    group = NeuronGroup(1, "dv/dt = 1*Hz : 1")
    run(1 * ms, report="text")
    with pytest.raises(NotImplementedError):
        run(1 * ms, report="stderr")


@pytest.mark.codegen_independent
def test_store_restore():
    source = NeuronGroup(
        10,
        """dv/dt = rates : 1
                                rates : Hz""",
        threshold="v>1",
        reset="v=0",
    )
    source.rates = "i*100*Hz"
    target = NeuronGroup(10, "v:1")
    synapses = Synapses(source, target, model="w:1", on_pre="v+=w")
    synapses.connect(j="i")
    synapses.w = "i*1.0"
    synapses.delay = "i*ms"
    state_mon = StateMonitor(target, "v", record=True)
    spike_mon = SpikeMonitor(source)
    net = Network(source, target, synapses, state_mon, spike_mon)
    net.store()  # default time slot
    net.run(10 * ms)
    net.store("second")
    net.run(10 * ms)
    v_values = state_mon.v[:, :]
    spike_indices, spike_times = spike_mon.it_
    net.restore()  # Go back to beginning
    assert defaultclock.t == 0 * ms
    assert net.t == 0 * ms
    net.run(20 * ms)
    assert_equal(v_values, state_mon.v[:, :])
    assert_equal(spike_indices, spike_mon.i[:])
    assert_equal(spike_times, spike_mon.t_[:])

    # Go back to middle
    net.restore("second")
    assert defaultclock.t == 10 * ms
    assert net.t == 10 * ms
    net.run(10 * ms)
    assert_equal(v_values, state_mon.v[:, :])
    assert_equal(spike_indices, spike_mon.i[:])
    assert_equal(spike_times, spike_mon.t_[:])

    # Go back again (see github issue #681)
    net.restore("second")
    assert defaultclock.t == 10 * ms
    assert net.t == 10 * ms


@pytest.mark.codegen_independent
def test_store_restore_to_file():
    filename = tempfile.mktemp(suffix="state", prefix="brian_test")
    source = NeuronGroup(
        10,
        """
        dv/dt = rates : 1
        rates : Hz
        """,
        threshold="v>1",
        reset="v=0",
    )
    source.rates = "i*100*Hz"
    target = NeuronGroup(10, "v:1")
    synapses = Synapses(source, target, model="w:1", on_pre="v+=w")
    synapses.connect(j="i")
    synapses.w = "i*1.0"
    synapses.delay = "i*ms"
    state_mon = StateMonitor(target, "v", record=True)
    spike_mon = SpikeMonitor(source)
    net = Network(source, target, synapses, state_mon, spike_mon)
    net.store(filename=filename)  # default time slot
    net.run(10 * ms)
    net.store("second", filename=filename)
    net.run(10 * ms)
    v_values = state_mon.v[:, :]
    spike_indices, spike_times = spike_mon.it_

    net.restore(filename=filename)  # Go back to beginning
    assert defaultclock.t == 0 * ms
    assert net.t == 0 * ms
    net.run(20 * ms)
    assert_equal(v_values, state_mon.v[:, :])
    assert_equal(spike_indices, spike_mon.i[:])
    assert_equal(spike_times, spike_mon.t_[:])

    # Go back to middle
    net.restore("second", filename=filename)
    assert defaultclock.t == 10 * ms
    assert net.t == 10 * ms
    net.run(10 * ms)
    assert_equal(v_values, state_mon.v[:, :])
    assert_equal(spike_indices, spike_mon.i[:])
    assert_equal(spike_times, spike_mon.t_[:])
    try:
        os.remove(filename)
    except OSError:
        pass


@pytest.mark.codegen_independent
def test_store_restore_to_file_new_objects():
    # A more realistic test where the objects are completely re-created
    filename = tempfile.mktemp(suffix="state", prefix="brian_test")

    def create_net():
        # Use a bit of a complicated spike and connection pattern with
        # heterogeneous delays

        # Note: it is important that all objects have the same name, this would
        # be the case if we were running this in a new process but to not rely
        # on garbage collection we will assign explicit names here
        source = SpikeGeneratorGroup(
            5,
            np.arange(5).repeat(3),
            [3, 4, 1, 2, 3, 7, 5, 4, 1, 0, 5, 9, 7, 8, 9] * ms,
            name="source",
        )
        target = NeuronGroup(10, "v:1", name="target")
        synapses = Synapses(source, target, model="w:1", on_pre="v+=w", name="synapses")
        synapses.connect("j>=i")
        synapses.w = "i*1.0 + j*2.0"
        synapses.delay = "(5-i)*ms"
        state_mon = StateMonitor(target, "v", record=True, name="statemonitor")
        input_spikes = SpikeMonitor(source, name="input_spikes")
        net = Network(source, target, synapses, state_mon, input_spikes)
        return net

    net = create_net()
    net.store(filename=filename)  # default time slot
    net.run(5 * ms)
    net.store("second", filename=filename)
    net.run(5 * ms)
    input_spike_indices = np.array(net["input_spikes"].i)
    input_spike_times = Quantity(net["input_spikes"].t, copy=True)
    v_values_full_sim = Quantity(net["statemonitor"].v[:, :], copy=True)

    net = create_net()
    net.restore(filename=filename)  # Go back to beginning
    net.run(10 * ms)
    assert_equal(input_spike_indices, net["input_spikes"].i)
    assert_equal(input_spike_times, net["input_spikes"].t)
    assert_equal(v_values_full_sim, net["statemonitor"].v[:, :])

    net = create_net()
    net.restore("second", filename=filename)  # Go back to middle
    net.run(5 * ms)
    assert_equal(input_spike_indices, net["input_spikes"].i)
    assert_equal(input_spike_times, net["input_spikes"].t)
    assert_equal(v_values_full_sim, net["statemonitor"].v[:, :])

    try:
        os.remove(filename)
    except OSError:
        pass


@pytest.mark.codegen_independent
def test_store_restore_to_file_differing_nets():
    # Check that the store/restore mechanism is not used with differing
    # networks
    filename = tempfile.mktemp(suffix="state", prefix="brian_test")

    source = SpikeGeneratorGroup(
        5, [0, 1, 2, 3, 4], [0, 1, 2, 3, 4] * ms, name="source_1"
    )
    mon = SpikeMonitor(source, name="monitor")
    net = Network(source, mon)
    net.store(filename=filename)

    source_2 = SpikeGeneratorGroup(
        5, [0, 1, 2, 3, 4], [0, 1, 2, 3, 4] * ms, name="source_2"
    )
    mon = SpikeMonitor(source_2, name="monitor")
    net = Network(source_2, mon)
    with pytest.raises(KeyError):
        net.restore(filename=filename)

    net = Network(source)  # Without the monitor
    with pytest.raises(KeyError):
        net.restore(filename=filename)


@pytest.mark.codegen_independent
def test_store_restore_magic():
    source = NeuronGroup(
        10,
        """
        dv/dt = rates : 1
        rates : Hz
        """,
        threshold="v>1",
        reset="v=0",
    )
    source.rates = "i*100*Hz"
    target = NeuronGroup(10, "v:1")
    synapses = Synapses(source, target, model="w:1", on_pre="v+=w")
    synapses.connect(j="i")
    synapses.w = "i*1.0"
    synapses.delay = "i*ms"
    state_mon = StateMonitor(target, "v", record=True)
    spike_mon = SpikeMonitor(source)
    store()  # default time slot
    run(10 * ms)
    store("second")
    run(10 * ms)
    v_values = state_mon.v[:, :]
    spike_indices, spike_times = spike_mon.it_

    restore()  # Go back to beginning
    assert magic_network.t == 0 * ms
    run(20 * ms)
    assert defaultclock.t == 20 * ms
    assert_equal(v_values, state_mon.v[:, :])
    assert_equal(spike_indices, spike_mon.i[:])
    assert_equal(spike_times, spike_mon.t_[:])

    # Go back to middle
    restore("second")
    assert magic_network.t == 10 * ms
    run(10 * ms)
    assert defaultclock.t == 20 * ms
    assert_equal(v_values, state_mon.v[:, :])
    assert_equal(spike_indices, spike_mon.i[:])
    assert_equal(spike_times, spike_mon.t_[:])


@pytest.mark.codegen_independent
def test_store_restore_magic_to_file():
    filename = tempfile.mktemp(suffix="state", prefix="brian_test")
    source = NeuronGroup(
        10,
        """
        dv/dt = rates : 1
        rates : Hz
        """,
        threshold="v>1",
        reset="v=0",
    )
    source.rates = "i*100*Hz"
    target = NeuronGroup(10, "v:1")
    synapses = Synapses(source, target, model="w:1", on_pre="v+=w")
    synapses.connect(j="i")
    synapses.w = "i*1.0"
    synapses.delay = "i*ms"
    state_mon = StateMonitor(target, "v", record=True)
    spike_mon = SpikeMonitor(source)
    store(filename=filename)  # default time slot
    run(10 * ms)
    store("second", filename=filename)
    run(10 * ms)
    v_values = state_mon.v[:, :]
    spike_indices, spike_times = spike_mon.it_

    restore(filename=filename)  # Go back to beginning
    assert magic_network.t == 0 * ms
    run(20 * ms)
    assert defaultclock.t == 20 * ms
    assert_equal(v_values, state_mon.v[:, :])
    assert_equal(spike_indices, spike_mon.i[:])
    assert_equal(spike_times, spike_mon.t_[:])

    # Go back to middle
    restore("second", filename=filename)
    assert magic_network.t == 10 * ms
    run(10 * ms)
    assert defaultclock.t == 20 * ms
    assert_equal(v_values, state_mon.v[:, :])
    assert_equal(spike_indices, spike_mon.i[:])
    assert_equal(spike_times, spike_mon.t_[:])
    try:
        os.remove(filename)
    except OSError:
        pass


@pytest.mark.codegen_independent
def test_store_restore_spikequeue():
    # See github issue #938
    source = SpikeGeneratorGroup(1, [0], [0] * ms)
    target = NeuronGroup(1, "v : 1")
    conn = Synapses(source, target, on_pre="v += 1", delay=2 * defaultclock.dt)
    conn.connect()
    run(defaultclock.dt)  # Spike is not yet delivered
    store()
    run(2 * defaultclock.dt)
    assert target.v[0] == 1
    restore()
    run(2 * defaultclock.dt)
    assert target.v[0] == 1
    restore()
    run(2 * defaultclock.dt)
    assert target.v[0] == 1


@pytest.mark.skipif(
    not isinstance(get_device(), RuntimeDevice),
    reason="Getting/setting random number state only supported for runtime device.",
)
def test_restore_with_random_state():
    group = NeuronGroup(10, "dv/dt = -v/(10*ms) + (10*ms)**-0.5*xi : 1", method="euler")
    group.v = "rand()"
    mon = StateMonitor(group, "v", record=True)
    store()
    run(10 * ms)
    old_v = np.array(group.v)

    restore()  # Random state is not restored
    run(10 * ms)
    assert np.var(old_v - group.v) > 0  # very basic test for difference

    restore(restore_random_state=True)  # Random state is restored
    run(10 * ms)
    assert_equal(old_v, group.v)


@pytest.mark.codegen_independent
def test_store_restore_restore_synapses():
    group = NeuronGroup(10, "x : 1", threshold="False", reset="", name="group")
    synapses = Synapses(group, group, on_pre="x += 1", name="synapses")
    synapses.connect(i=[1, 3, 5], j=[6, 4, 2])
    net = Network(group, synapses)

    tmp_file = tempfile.mktemp()
    net.store(filename=tmp_file)

    # clear up
    del net
    del synapses
    del group

    # Recreate the network without connecting the synapses
    group = NeuronGroup(10, "x: 1", threshold="False", reset="", name="group")
    synapses = Synapses(group, group, "", on_pre="x += 1", name="synapses")
    net = Network(group, synapses)
    try:
        net.restore(filename=tmp_file)

        assert len(synapses) == 3
        assert_array_equal(synapses.i, [1, 3, 5])
        assert_array_equal(synapses.j, [6, 4, 2])

        # Tunning the network should not raise an error, despite the lack
        # of Synapses.connect
        net.run(0 * ms)
    finally:
        os.remove(tmp_file)


@pytest.mark.codegen_independent
def test_defaultclock_dt_changes():
    BrianLogger.suppress_name("resolution_conflict")
    for dt in [0.1 * ms, 0.01 * ms, 0.5 * ms, 1 * ms, 3.3 * ms]:
        defaultclock.dt = dt
        G = NeuronGroup(1, "v:1")
        mon = StateMonitor(G, "v", record=True)
        net = Network(G, mon)
        net.run(2 * dt)
        assert_equal(mon.t[:], [0, dt / ms] * ms)


@pytest.mark.standalone_compatible
@pytest.mark.multiple_runs
def test_dt_changes_between_runs():
    defaultclock.dt = 0.1 * ms
    G = NeuronGroup(1, "v:1")
    mon = StateMonitor(G, "v", record=True)
    run(0.5 * ms)
    defaultclock.dt = 0.5 * ms
    run(0.5 * ms)
    defaultclock.dt = 0.1 * ms
    run(0.5 * ms)
    device.build(direct_call=False, **device.build_options)
    assert len(mon.t[:]) == 5 + 1 + 5
    assert_allclose(
        mon.t[:], [0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 1.0, 1.1, 1.2, 1.3, 1.4] * ms
    )


@pytest.mark.codegen_independent
def test_dt_restore():
    defaultclock.dt = 0.5 * ms
    G = NeuronGroup(1, "dv/dt = -v/(10*ms) : 1")
    mon = StateMonitor(G, "v", record=True)
    net = Network(G, mon)
    net.store()

    net.run(1 * ms)
    assert_equal(mon.t[:], [0, 0.5] * ms)
    defaultclock.dt = 1 * ms
    net.run(2 * ms)
    assert_equal(mon.t[:], [0, 0.5, 1, 2] * ms)
    net.restore()
    assert_equal(mon.t[:], [])
    net.run(1 * ms)
    assert defaultclock.dt == 0.5 * ms
    assert_equal(mon.t[:], [0, 0.5] * ms)


@pytest.mark.codegen_independent
def test_continuation():
    defaultclock.dt = 1 * ms
    G = NeuronGroup(1, "dv/dt = -v / (10*ms) : 1")
    G.v = 1
    mon = StateMonitor(G, "v", record=True)
    net = Network(G, mon)
    net.run(2 * ms)

    # Run the same simulation but with two runs that use sub-dt run times
    G2 = NeuronGroup(1, "dv/dt = -v / (10*ms) : 1")
    G2.v = 1
    mon2 = StateMonitor(G2, "v", record=True)
    net2 = Network(G2, mon2)
    net2.run(0.5 * ms)
    net2.run(1.5 * ms)

    assert_equal(mon.t[:], mon2.t[:])
    assert_equal(mon.v[:], mon2.v[:])


@pytest.mark.codegen_independent
def test_get_set_states():
    G = NeuronGroup(10, "v:1", name="a_neurongroup")
    G.v = "i"
    net = Network(G)
    states1 = net.get_states()
    states2 = magic_network.get_states()
    states3 = net.get_states(read_only_variables=False)
    assert (
        set(states1.keys())
        == set(states2.keys())
        == set(states3.keys())
        == {"a_neurongroup"}
    )
    assert (
        set(states1["a_neurongroup"].keys())
        == set(states2["a_neurongroup"].keys())
        == {"i", "dt", "N", "t", "v", "t_in_timesteps"}
    )
    assert set(states3["a_neurongroup"]) == {"v"}

    # Try re-setting the state
    G.v = 0
    net.set_states(states3)
    assert_equal(G.v, np.arange(10))


@pytest.mark.codegen_independent
def test_multiple_runs_defaultclock():
    defaultclock.dt = 0.1 * ms
    G = NeuronGroup(1, "dv/dt = -v / (10*ms) : 1")
    net = Network(G)
    net.run(0.5 * ms)

    # The new dt is not compatible with the previous time but it should not
    # raise an error because we start a new simulation at time 0
    defaultclock.dt = 1 * ms
    G = NeuronGroup(1, "dv/dt = -v / (10*ms) : 1")
    net = Network(G)
    net.run(1 * ms)


@pytest.mark.codegen_independent
def test_multiple_runs_defaultclock_incorrect():
    defaultclock.dt = 0.1 * ms
    G = NeuronGroup(1, "dv/dt = -v / (10*ms) : 1")
    net = Network(G)
    net.run(0.5 * ms)

    # The new dt is not compatible with the previous time since we cannot
    # continue at 0.5ms with a dt of 1ms
    defaultclock.dt = 1 * ms
    with pytest.raises(ValueError):
        net.run(1 * ms)


@pytest.mark.standalone_compatible
def test_profile():
    G = NeuronGroup(
        10,
        "dv/dt = -v / (10*ms) : 1",
        threshold="v>1",
        reset="v=0",
        name="profile_test",
    )
    G.v = 1.1
    net = Network(G)
    net.run(1 * ms, profile=True)
    # The should be four simulated CodeObjects, one for the group and one each
    # for state update, threshold and reset + 1 for the clock
    info = net.profiling_info
    info_dict = dict(info)
    # Standalone does not include the NeuronGroup object (which is not doing
    # anything during the run) in the profiling information, while runtime
    # does
    assert 3 <= len(info) <= 4
    assert len(info) == 3 or "profile_test" in info_dict
    for obj in ["stateupdater", "spike_thresholder", "spike_resetter"]:
        name = f"profile_test_{obj}"
        assert name in info_dict or f"{name}_codeobject" in info_dict
    assert all([t >= 0 * second for _, t in info])


@pytest.mark.standalone_compatible
def test_profile_off():
    G = NeuronGroup(
        10,
        "dv/dt = -v / (10*ms) : 1",
        threshold="v>1",
        reset="v=0",
        name="profile_test",
    )
    net = Network(G)
    net.run(1 * ms, profile=False)
    with pytest.raises(ValueError):
        profiling_summary(net)


@pytest.mark.codegen_independent
def test_profile_ipython_html():
    G = NeuronGroup(
        10,
        "dv/dt = -v / (10*ms) : 1",
        threshold="v>1",
        reset="v=0",
        name="profile_test",
    )
    G.v = 1.1
    net = Network(G)
    net.run(1 * ms, profile=True)
    summary = profiling_summary(net)
    assert len(summary._repr_html_())


@pytest.mark.codegen_independent
def test_magic_scope():
    """
    Check that `start_scope` works as expected.
    """
    G1 = NeuronGroup(1, "v:1", name="G1")
    G2 = NeuronGroup(1, "v:1", name="G2")
    objs1 = {obj.name for obj in collect()}
    start_scope()
    G3 = NeuronGroup(1, "v:1", name="G3")
    G4 = NeuronGroup(1, "v:1", name="G4")
    objs2 = {obj.name for obj in collect()}
    assert objs1 == {"G1", "G2"}
    assert objs2 == {"G3", "G4"}


@pytest.mark.standalone_compatible
def test_runtime_rounding():
    # Test that runtime and standalone round in the same way, see github issue
    # #695 for details
    defaultclock.dt = 20.000000000020002 * us
    G = NeuronGroup(1, "v:1")
    mon = StateMonitor(G, "v", record=True)
    run(defaultclock.dt * 250)
    assert len(mon.t) == 250


@pytest.mark.codegen_independent
def test_small_runs():
    # One long run and multiple small runs should give the same results
    group_1 = NeuronGroup(10, "dv/dt = -v / (10*ms) : 1")
    group_1.v = "(i + 1) / N"
    mon_1 = StateMonitor(group_1, "v", record=True)
    net_1 = Network(group_1, mon_1)
    net_1.run(1 * second)

    group_2 = NeuronGroup(10, "dv/dt = -v / (10*ms) : 1")
    group_2.v = "(i + 1) / N"
    mon_2 = StateMonitor(group_2, "v", record=True)
    net_2 = Network(group_2, mon_2)
    runtime = 1 * ms
    while True:
        runtime *= 3
        runtime = min([runtime, 1 * second - net_2.t])
        net_2.run(runtime)
        if net_2.t >= 1 * second:
            break

    assert_allclose(mon_1.t_[:], mon_2.t_[:])
    assert_allclose(mon_1.v_[:], mon_2.v_[:])


@pytest.mark.codegen_independent
def test_both_equal():
    # check all objects added by Network.add() also have their contained_objects added to 'Network'
    tau = 10 * ms
    diff_eqn = """dv/dt = (1-v)/tau : 1"""
    chg_code = """v = 2*v"""

    Ng = NeuronGroup(1, diff_eqn, method="exact")
    M1 = StateMonitor(Ng, "v", record=True)
    netObj = Network(Ng, M1)
    Ng.run_regularly(chg_code, dt=20 * ms)
    netObj.run(100 * ms)

    start_scope()
    Ng = NeuronGroup(1, diff_eqn, method="exact")
    M2 = StateMonitor(Ng, "v", record=True)
    Ng.run_regularly(chg_code, dt=20 * ms)
    run(100 * ms)

    assert (M1.v == M2.v).all()


@pytest.mark.standalone_compatible
@pytest.mark.multiple_runs
def test_long_run():
    defaultclock.dt = 0.1 * ms
    group = NeuronGroup(1, "x : 1")
    group.run_regularly("x += 1")
    # Timesteps are internally stored as 64bit integers, but previous versions
    # converted them into 32bit integers along the way. We'll make sure that
    # this is not the case and everything runs fine. To not actually run such a
    # long simulation we run a single huge time step
    start_step = 2**31 - 5
    defaultclock.dt = 0.1 * ms
    start_time = start_step * defaultclock.dt
    defaultclock.dt = start_time
    run(start_time)  # A single, *very* long time step
    defaultclock.dt = 0.1 * ms
    run(6 * defaultclock.dt)
    device.build(direct_call=False, **device.build_options)
    assert group.x == 7


@pytest.mark.codegen_independent
def test_long_run_dt_change():
    # Check that the dt check is not too restrictive, see issue #730 for details
    group = NeuronGroup(1, "")  # does nothing...
    defaultclock.dt = 0.1 * ms
    run(100 * second)
    # print profiling_summary()
    defaultclock.dt = 0.01 * ms
    run(1 * second)


@pytest.mark.standalone_compatible
@pytest.mark.multiple_runs
def test_multiple_runs_constant_change():
    const_v = 1
    group = NeuronGroup(1, "v = const_v : 1")
    mon = StateMonitor(group, "v", record=0)
    run(defaultclock.dt)
    const_v = 2
    run(defaultclock.dt)
    device.build(direct_call=False, **device.build_options)
    assert_equal(mon.v[0], [1, 2])


@pytest.mark.standalone_compatible
@pytest.mark.multiple_runs
def test_multiple_runs_function_change():
    inp = TimedArray([1, 2], dt=defaultclock.dt)
    group = NeuronGroup(1, "v = inp(t) : 1")
    mon = StateMonitor(group, "v", record=0)
    run(2 * defaultclock.dt)
    inp = TimedArray([0, 0, 3, 4], dt=defaultclock.dt)
    run(2 * defaultclock.dt)
    device.build(direct_call=False, **device.build_options)
    assert_equal(mon.v[0], [1, 2, 3, 4])


@pytest.mark.codegen_independent
def test_unused_object_warning():
    with catch_logs() as logs:
        # Create a NeuronGroup that is not used in the network
        NeuronGroup(1, "v:1", name="never_used")
        # Make sure that it gets garbage collected
        import gc

        gc.collect()
    assert len(logs) == 1
    assert logs[0][0] == "WARNING"
    assert logs[0][1].endswith("unused_brian_object")
    assert "never_used" in logs[0][2]


@pytest.mark.codegen_independent
def test_negative_duration_in_run():
    G = NeuronGroup(1, "v:1")
    with pytest.raises(ValueError):
        run(-1 * second)


@pytest.mark.codegen_independent
def test_negative_duration_in_net_run():
    G = NeuronGroup(1, "v:1")
    net = Network(G)
    with pytest.raises(ValueError):
        net.run(-1 * second)


if __name__ == "__main__":
    BrianLogger.log_level_warn()
    for t in [
        test_incorrect_network_use,
        test_network_contains,
        test_empty_network,
        test_network_single_object,
        test_network_two_objects,
        test_network_from_dict,
        test_network_different_clocks,
        test_network_different_when,
        test_network_default_schedule,
        test_network_schedule_change,
        test_network_before_after_schedule,
        test_network_custom_slots,
        test_network_incorrect_schedule,
        test_schedule_warning,
        test_scheduling_summary_magic,
        test_scheduling_summary,
        test_magic_network,
        test_network_stop,
        test_network_operations,
        test_incorrect_network_operations,
        test_network_operations_name,
        test_network_active_flag,
        test_network_t,
        test_incorrect_dt_defaultclock,
        test_incorrect_dt_custom_clock,
        test_network_remove,
        test_magic_weak_reference,
        test_magic_unused_object,
        test_invalid_magic_network,
        test_multiple_networks_invalid,
        test_network_access,
        test_loop,
        test_magic_collect,
        test_progress_report,
        test_progress_report_incorrect,
        test_multiple_runs_report_standalone,
        test_multiple_runs_report_standalone_2,
        test_multiple_runs_report_standalone_3,
        test_multiple_runs_report_standalone_incorrect,
        test_store_restore,
        test_store_restore_to_file,
        test_store_restore_to_file_new_objects,
        test_store_restore_to_file_differing_nets,
        test_store_restore_magic,
        test_store_restore_magic_to_file,
        test_store_restore_spikequeue,
        test_store_restore_restore_synapses,
        test_defaultclock_dt_changes,
        test_dt_changes_between_runs,
        test_dt_restore,
        test_continuation,
        test_get_set_states,
        test_multiple_runs_defaultclock,
        test_multiple_runs_defaultclock_incorrect,
        test_profile,
        test_profile_off,
        test_profile_ipython_html,
        test_magic_scope,
        test_runtime_rounding,
        test_small_runs,
        test_both_equal,
        test_long_run,
        test_long_run_dt_change,
        test_multiple_runs_constant_change,
        test_multiple_runs_function_change,
    ]:
        set_device(all_devices["runtime"])
        t()
        reinit_and_delete()