File: test_synapses.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 (3833 lines) | stat: -rw-r--r-- 120,876 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
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
import logging
import uuid

import pytest
import sympy
from numpy.testing import assert_array_equal, assert_equal

from brian2 import *
from brian2.codegen.generators import NumpyCodeGenerator
from brian2.codegen.permutation_analysis import (
    OrderDependenceError,
    check_for_order_independence,
)
from brian2.codegen.translation import make_statements
from brian2.core.functions import DEFAULT_FUNCTIONS
from brian2.core.network import schedule_propagation_offset
from brian2.core.variables import ArrayVariable, Constant, variables_by_owner
from brian2.devices.cpp_standalone.device import CPPStandaloneDevice
from brian2.devices.device import all_devices, get_device, reinit_and_delete
from brian2.equations.equations import EquationError
from brian2.stateupdaters.base import UnsupportedEquationsException
from brian2.synapses.parse_synaptic_generator_syntax import parse_synapse_generator
from brian2.tests.utils import assert_allclose, exc_isinstance
from brian2.utils.logger import catch_logs
from brian2.utils.stringtools import deindent, get_identifiers, indent, word_substitute


def _compare(synapses, expected):
    conn_matrix = np.zeros((len(synapses.source), len(synapses.target)), dtype=np.int32)
    for _i, _j in zip(synapses.i[:], synapses.j[:]):
        conn_matrix[_i, _j] += 1

    assert_equal(conn_matrix, expected)
    # also compare the correct numbers of incoming and outgoing synapses
    incoming = conn_matrix.sum(axis=0)
    outgoing = conn_matrix.sum(axis=1)
    assert all(
        synapses.N_outgoing[:] == outgoing[synapses.i[:]]
    ), "N_outgoing returned an incorrect value"
    assert_array_equal(
        synapses.N_outgoing_pre, outgoing
    ), "N_outgoing_pre returned an incorrect value"
    assert all(
        synapses.N_incoming[:] == incoming[synapses.j[:]]
    ), "N_incoming returned an incorrect value"
    assert_array_equal(
        synapses.N_incoming_post, incoming
    ), "N_incoming_post returned an incorrect value"

    # Compare the "synapse number" if it exists
    if synapses.multisynaptic_index is not None:
        # Build an array of synapse numbers by counting the number of times
        # a source/target combination exists
        synapse_numbers = np.zeros_like(synapses.i[:])
        numbers = {}
        for _i, (source, target) in enumerate(zip(synapses.i[:], synapses.j[:])):
            number = numbers.get((source, target), 0)
            synapse_numbers[_i] = number
            numbers[(source, target)] = number + 1
        assert all(
            synapses.state(synapses.multisynaptic_index)[:] == synapse_numbers
        ), "synapse_number returned an incorrect value"


@pytest.mark.codegen_independent
def test_creation():
    """
    A basic test that creating a Synapses object works.
    """
    G = NeuronGroup(42, "v: 1", threshold="False")
    S = Synapses(G, G, "w:1", on_pre="v+=w")
    # We store weakref proxys, so we can't directly compare the objects
    assert S.source.name == S.target.name == G.name
    assert len(S) == 0
    S = Synapses(G, model="w:1", on_pre="v+=w")
    assert S.source.name == S.target.name == G.name


@pytest.mark.codegen_independent
def test_creation_errors():
    G = NeuronGroup(42, "v: 1", threshold="False")
    # Check that the old Synapses(..., connect=...) syntax raises an error
    with pytest.raises(TypeError):
        Synapses(G, G, "w:1", on_pre="v+=w", connect=True)
    # Check that using pre and on_pre (resp. post/on_post) at the same time
    # raises an error
    with pytest.raises(TypeError):
        Synapses(G, G, "w:1", pre="v+=w", on_pre="v+=w", connect=True)
    with pytest.raises(TypeError):
        Synapses(G, G, "w:1", post="v+=w", on_post="v+=w", connect=True)


@pytest.mark.codegen_independent
def test_connect_errors():
    G = NeuronGroup(42, "")
    S = Synapses(G, G)

    # Not a boolean condition
    with pytest.raises(TypeError):
        S.connect("i*2")

    # Unit error
    with pytest.raises(DimensionMismatchError):
        S.connect("i > 3*mV")

    # Syntax error
    with pytest.raises(SyntaxError):
        S.connect("sin(3, 4) > 1")

    # Unit error in p argument
    with pytest.raises(TypeError):
        S.connect("1*mV")

    # Syntax error in p argument
    with pytest.raises(SyntaxError):
        S.connect(p="sin(3, 4)")


@pytest.mark.codegen_independent
def test_name_clashes():
    # Using identical names for synaptic and pre- or post-synaptic variables
    # is confusing and should be forbidden
    G1 = NeuronGroup(1, "a : 1")
    G2 = NeuronGroup(1, "b : 1")
    with pytest.raises(ValueError):
        Synapses(G1, G2, "a : 1")
    with pytest.raises(ValueError):
        Synapses(G1, G2, "b : 1")

    # Using _pre or _post as variable names is confusing (even if it is non-
    # ambiguous in unconnected NeuronGroups)
    with pytest.raises(ValueError):
        Synapses(G1, G2, "x_pre : 1")
    with pytest.raises(ValueError):
        Synapses(G1, G2, "x_post : 1")
    with pytest.raises(ValueError):
        Synapses(G1, G2, "x_pre = 1 : 1")
    with pytest.raises(ValueError):
        Synapses(G1, G2, "x_post = 1 : 1")
    with pytest.raises(ValueError):
        NeuronGroup(1, "x_pre : 1")
    with pytest.raises(ValueError):
        NeuronGroup(1, "x_post : 1")
    with pytest.raises(ValueError):
        NeuronGroup(1, "x_pre = 1 : 1")
    with pytest.raises(ValueError):
        NeuronGroup(1, "x_post = 1 : 1")

    # this should all be ok
    Synapses(G1, G2, "c : 1")
    Synapses(G1, G2, "a_syn : 1")
    Synapses(G1, G2, "b_syn : 1")


@pytest.mark.standalone_compatible
def test_incoming_outgoing():
    """
    Test the count of outgoing/incoming synapses per neuron.
    (It will be also automatically tested for all connection patterns that
    use the above _compare function for testing)
    """
    G1 = NeuronGroup(5, "")
    G2 = NeuronGroup(5, "")
    S = Synapses(G1, G2, "")
    S.connect(i=[0, 0, 0, 1, 1, 2], j=[0, 1, 2, 1, 2, 3])
    run(0 * ms)  # to make this work for standalone
    # First source neuron has 3 outgoing synapses, the second 2, the third 1
    assert all(S.N_outgoing[0, :] == 3)
    assert all(S.N_outgoing[1, :] == 2)
    assert all(S.N_outgoing[2, :] == 1)
    assert all(S.N_outgoing[3:, :] == 0)
    assert_array_equal(S.N_outgoing_pre, [3, 2, 1, 0, 0])
    # First target neuron receives 1 input, the second+third each 2, the fourth receives 1
    assert all(S.N_incoming[:, 0] == 1)
    assert all(S.N_incoming[:, 1] == 2)
    assert all(S.N_incoming[:, 2] == 2)
    assert all(S.N_incoming[:, 3] == 1)
    assert all(S.N_incoming[:, 4:] == 0)
    assert_array_equal(S.N_incoming_post, [1, 2, 2, 1, 0])


@pytest.mark.standalone_compatible
def test_connection_arrays():
    """
    Test connecting synapses with explictly given arrays
    """
    G = NeuronGroup(42, "")
    G2 = NeuronGroup(17, "")

    # one-to-one
    expected1 = np.eye(len(G2))
    S1 = Synapses(G2)
    S1.connect(i=np.arange(len(G2)), j=np.arange(len(G2)))

    # full
    expected2 = np.ones((len(G), len(G2)))
    S2 = Synapses(G, G2)
    X, Y = np.meshgrid(np.arange(len(G)), np.arange(len(G2)))
    S2.connect(i=X.flatten(), j=Y.flatten())

    # Multiple synapses
    expected3 = np.zeros((len(G), len(G2)))
    expected3[3, 3] = 2
    S3 = Synapses(G, G2)
    S3.connect(i=[3, 3], j=[3, 3])

    run(0 * ms)  # for standalone
    _compare(S1, expected1)
    _compare(S2, expected2)
    _compare(S3, expected3)

    # Incorrect usage
    S = Synapses(G, G2)
    with pytest.raises(TypeError):
        S.connect(i=[1.1, 2.2], j=[1.1, 2.2])
    with pytest.raises(TypeError):
        S.connect(i=[1, 2], j="string")
    with pytest.raises(TypeError):
        S.connect(i=[1, 2], j=[1, 2], n="i")
    with pytest.raises(TypeError):
        S.connect([1, 2])
    with pytest.raises(ValueError):
        S.connect(i=[1, 2, 3], j=[1, 2])
    with pytest.raises(ValueError):
        S.connect(i=np.ones((3, 3), dtype=np.int32), j=np.ones((3, 1), dtype=np.int32))
    with pytest.raises(IndexError):
        S.connect(i=[41, 42], j=[0, 1])  # source index > max
    with pytest.raises(IndexError):
        S.connect(i=[0, 1], j=[16, 17])  # target index > max
    with pytest.raises(IndexError):
        S.connect(i=[0, -1], j=[0, 1])  # source index < 0
    with pytest.raises(IndexError):
        S.connect(i=[0, 1], j=[0, -1])  # target index < 0
    with pytest.raises(ValueError):
        S.connect("i==j", j=np.arange(10))
    with pytest.raises(TypeError):
        S.connect("i==j", n=object())
    with pytest.raises(TypeError):
        S.connect("i==j", p=object())
    with pytest.raises(TypeError):
        S.connect(object())


@pytest.mark.standalone_compatible
def test_connection_string_deterministic_full():
    G = NeuronGroup(17, "")
    G2 = NeuronGroup(4, "")

    # Full connection
    expected_full = np.ones((len(G), len(G2)))

    S1 = Synapses(G, G2, "")
    S1.connect(True)

    S2 = Synapses(G, G2, "")
    S2.connect("True")

    run(0 * ms)  # for standalone

    _compare(S1, expected_full)
    _compare(S2, expected_full)


@pytest.mark.standalone_compatible
def test_connection_string_deterministic_full_no_self():
    G = NeuronGroup(17, "v : 1")
    G.v = "i"
    G2 = NeuronGroup(4, "v : 1")
    G2.v = "17 + i"

    # Full connection without self-connections
    expected_no_self = np.ones((len(G), len(G))) - np.eye(len(G))

    S1 = Synapses(G, G)
    S1.connect("i != j")

    S2 = Synapses(G, G)
    S2.connect("v_pre != v_post")

    S3 = Synapses(G, G)
    S3.connect(condition="i != j")

    run(0 * ms)  # for standalone

    _compare(S1, expected_no_self)
    _compare(S2, expected_no_self)
    _compare(S3, expected_no_self)


@pytest.mark.standalone_compatible
def test_connection_string_deterministic_full_one_to_one():
    G = NeuronGroup(17, "v : 1")
    G.v = "i"
    G2 = NeuronGroup(4, "v : 1")
    G2.v = "17 + i"

    # One-to-one connectivity
    expected_one_to_one = np.eye(len(G))

    S1 = Synapses(G, G)
    S1.connect("i == j")

    S2 = Synapses(G, G)
    S2.connect("v_pre == v_post")

    S3 = Synapses(
        G,
        G,
        """
        sub_1 = v_pre : 1
        sub_2 = v_post : 1
        w:1
        """,
    )
    S3.connect("sub_1 == sub_2")

    S4 = Synapses(G, G)
    S4.connect(j="i")

    run(0 * ms)  # for standalone

    _compare(S1, expected_one_to_one)
    _compare(S2, expected_one_to_one)
    _compare(S3, expected_one_to_one)
    _compare(S4, expected_one_to_one)


@pytest.mark.standalone_compatible
def test_connection_string_deterministic_full_custom():
    G = NeuronGroup(17, "")
    G2 = NeuronGroup(4, "")
    # Everything except for the upper [2, 2] quadrant
    number = 2
    expected_custom = np.ones((len(G), len(G)))
    expected_custom[:number, :number] = 0
    S1 = Synapses(G, G)
    S1.connect("(i >= number) or (j >= number)")

    S2 = Synapses(G, G)
    S2.connect(
        "(i >= explicit_number) or (j >= explicit_number)",
        namespace={"explicit_number": number},
    )

    # check that this mistaken syntax raises an error
    with pytest.raises(ValueError):
        S2.connect("k for k in range(1)")

    # check that trying to connect to a neuron outside the range raises an error
    if get_device() == all_devices["runtime"]:
        with pytest.raises(BrianObjectException) as exc:
            S2.connect(j="20")
        assert exc_isinstance(exc, IndexError)

    run(0 * ms)  # for standalone

    _compare(S1, expected_custom)
    _compare(S2, expected_custom)


@pytest.mark.standalone_compatible
def test_connection_string_deterministic_multiple_and():
    # In Brian versions 2.1.0-2.1.2, this fails on the numpy target
    # See github issue 900
    group = NeuronGroup(10, "")
    synapses = Synapses(group, group)
    synapses.connect("i>=5 and i<10 and j>=5")
    run(0 * ms)  # for standalone
    assert len(synapses) == 25


@pytest.mark.standalone_compatible
def test_connection_random_with_condition():
    G = NeuronGroup(4, "")

    S1 = Synapses(G, G)
    S1.connect("i!=j", p=0.0)

    S2 = Synapses(G, G)
    S2.connect("i!=j", p=1.0)
    expected2 = np.ones((len(G), len(G))) - np.eye(len(G))

    S3 = Synapses(G, G)
    S3.connect("i>=2", p=0.0)

    S4 = Synapses(G, G)
    S4.connect("i>=2", p=1.0)
    expected4 = np.zeros((len(G), len(G)))
    expected4[2, :] = 1
    expected4[3, :] = 1

    S5 = Synapses(G, G)
    S5.connect("j<2", p=0.0)
    S6 = Synapses(G, G)
    S6.connect("j<2", p=1.0)
    expected6 = np.zeros((len(G), len(G)))
    expected6[:, 0] = 1
    expected6[:, 1] = 1

    with catch_logs() as _:  # Ignore warnings about empty synapses
        run(0 * ms)  # for standalone

    assert len(S1) == 0
    _compare(S2, expected2)
    assert len(S3) == 0
    _compare(S4, expected4)
    assert len(S5) == 0
    _compare(S6, expected6)


@pytest.mark.standalone_compatible
@pytest.mark.long
def test_connection_random_with_condition_2():
    G = NeuronGroup(4, "")

    # Just checking that everything works in principle (we can't check the
    # actual connections)
    S7 = Synapses(G, G)
    S7.connect("i!=j", p=0.01)

    S8 = Synapses(G, G)
    S8.connect("i!=j", p=0.03)

    S9 = Synapses(G, G)
    S9.connect("i!=j", p=0.3)

    S10 = Synapses(G, G)
    S10.connect("i>=2", p=0.01)

    S11 = Synapses(G, G)
    S11.connect("i>=2", p=0.03)

    S12 = Synapses(G, G)
    S12.connect("i>=2", p=0.3)

    S13 = Synapses(G, G)
    S13.connect("j>=2", p=0.01)

    S14 = Synapses(G, G)
    S14.connect("j>=2", p=0.03)

    S15 = Synapses(G, G)
    S15.connect("j>=2", p=0.3)

    S16 = Synapses(G, G)
    S16.connect("i!=j", p="i*0.1")

    S17 = Synapses(G, G)
    S17.connect("i!=j", p="j*0.1")

    # Forces the use of the "jump algorithm"
    big_group = NeuronGroup(10000, "")
    S18 = Synapses(big_group, big_group)
    S18.connect("i != j", p=0.001)

    # See github issue #835 -- this failed when using numpy
    S19 = Synapses(big_group, big_group)
    S19.connect("i < int(N_post*0.5)", p=0.001)

    with catch_logs() as _:  # Ignore warnings about empty synapses
        run(0 * ms)  # for standalone

    assert not any(S7.i == S7.j)
    assert not any(S8.i == S8.j)
    assert not any(S9.i == S9.j)
    assert all(S10.i >= 2)
    assert all(S11.i >= 2)
    assert all(S12.i >= 2)
    assert all(S13.j >= 2)
    assert all(S14.j >= 2)
    assert all(S15.j >= 2)
    assert not any(S16.i == 0)
    assert not any(S17.j == 0)


@pytest.mark.standalone_compatible
def test_connection_random_with_indices():
    """
    Test random connections.
    """
    G = NeuronGroup(4, "")
    G2 = NeuronGroup(7, "")

    S1 = Synapses(G, G2)
    S1.connect(i=0, j=0, p=0.0)
    expected1 = np.zeros((len(G), len(G2)))

    S2 = Synapses(G, G2)
    S2.connect(i=0, j=0, p=1.0)
    expected2 = np.zeros((len(G), len(G2)))
    expected2[0, 0] = 1

    S3 = Synapses(G, G2)
    S3.connect(i=[0, 1], j=[0, 2], p=1.0)
    expected3 = np.zeros((len(G), len(G2)))
    expected3[0, 0] = 1
    expected3[1, 2] = 1

    # Just checking that it works in principle
    S4 = Synapses(G, G)
    S4.connect(i=0, j=0, p=0.01)
    S5 = Synapses(G, G)
    S5.connect(i=[0, 1], j=[0, 2], p=0.01)

    S6 = Synapses(G, G)
    S6.connect(i=0, j=0, p=0.03)

    S7 = Synapses(G, G)
    S7.connect(i=[0, 1], j=[0, 2], p=0.03)

    S8 = Synapses(G, G)
    S8.connect(i=0, j=0, p=0.3)

    S9 = Synapses(G, G)
    S9.connect(i=[0, 1], j=[0, 2], p=0.3)

    with catch_logs() as _:  # Ignore warnings about empty synapses
        run(0 * ms)  # for standalone

    _compare(S1, expected1)
    _compare(S2, expected2)
    _compare(S3, expected3)
    assert 0 <= len(S4) <= 1
    assert 0 <= len(S5) <= 2
    assert 0 <= len(S6) <= 1
    assert 0 <= len(S7) <= 2
    assert 0 <= len(S8) <= 1
    assert 0 <= len(S9) <= 2


@pytest.mark.standalone_compatible
def test_connection_random_without_condition():
    G = NeuronGroup(
        4,
        """
        v: 1
        x : integer
        """,
    )
    G.x = "i"
    G2 = NeuronGroup(
        7,
        """
        v: 1
        y : 1
        """,
    )
    G2.y = "1.0*i/N"

    S1 = Synapses(G, G2)
    S1.connect(True, p=0.0)

    S2 = Synapses(G, G2)
    S2.connect(True, p=1.0)

    # Just make sure using values between 0 and 1 work in principle
    S3 = Synapses(G, G2)
    S3.connect(True, p=0.3)

    # Use pre-/post-synaptic variables for "stochastic" connections that are
    # actually deterministic
    S4 = Synapses(G, G2)
    S4.connect(True, p="int(x_pre==2)*1.0")

    # Use pre-/post-synaptic variables for "stochastic" connections that are
    # actually deterministic
    S5 = Synapses(G, G2)
    S5.connect(True, p="int(x_pre==2 and y_post > 0.5)*1.0")

    with catch_logs() as _:  # Ignore warnings about empty synapses
        run(0 * ms)  # for standalone

    _compare(S1, np.zeros((len(G), len(G2))))
    _compare(S2, np.ones((len(G), len(G2))))
    assert 0 <= len(S3) <= len(G) * len(G2)
    assert len(S4) == 7
    assert_equal(S4.i, np.ones(7) * 2)
    assert_equal(S4.j, np.arange(7))
    assert len(S5) == 3
    assert_equal(S5.i, np.ones(3) * 2)
    assert_equal(S5.j, np.arange(3) + 4)


@pytest.mark.standalone_compatible
def test_connection_multiple_synapses():
    """
    Test multiple synapses per connection.
    """
    G = NeuronGroup(42, "v: 1")
    G.v = "i"
    G2 = NeuronGroup(17, "v: 1")
    G2.v = "i"

    S1 = Synapses(G, G2)
    S1.connect(True, n=0)

    S2 = Synapses(G, G2)
    S2.connect(True, n=2)

    S3 = Synapses(G, G2)
    S3.connect(True, n="j")

    S4 = Synapses(G, G2)
    S4.connect(True, n="i")

    S5 = Synapses(G, G2)
    S5.connect(True, n="int(i>j)*2")

    S6 = Synapses(G, G2)
    S6.connect(True, n="int(v_pre>v_post)*2")

    with catch_logs() as _:  # Ignore warnings about empty synapses
        run(0 * ms)  # for standalone

    assert len(S1) == 0
    _compare(S2, 2 * np.ones((len(G), len(G2))))
    _compare(S3, np.arange(len(G2)).reshape(1, len(G2)).repeat(len(G), axis=0))

    _compare(S4, np.arange(len(G)).reshape(len(G), 1).repeat(len(G2), axis=1))
    expected = np.zeros((len(G), len(G2)), dtype=np.int32)
    for source in range(len(G)):
        expected[source, :source] = 2
    _compare(S5, expected)
    _compare(S6, expected)


def test_state_variable_assignment():
    """
    Assign values to state variables in various ways
    """

    G = NeuronGroup(10, "v: volt")
    G.v = "i*mV"
    S = Synapses(G, G, "w:volt")
    S.connect(True)

    # with unit checking
    assignment_expected = [
        (5 * mV, np.ones(100) * 5 * mV),
        (7 * mV, np.ones(100) * 7 * mV),
        (S.i[:] * mV, S.i[:] * np.ones(100) * mV),
        ("5*mV", np.ones(100) * 5 * mV),
        ("i*mV", np.ones(100) * S.i[:] * mV),
        ("i*mV +j*mV", S.i[:] * mV + S.j[:] * mV),
        # reference to pre- and postsynaptic state variables
        ("v_pre", S.i[:] * mV),
        ("v_post", S.j[:] * mV),
        # ('i*mV + j*mV + k*mV', S.i[:]*mV + S.j[:]*mV + S.k[:]*mV) #not supported yet
    ]

    for assignment, expected in assignment_expected:
        S.w = 0 * volt
        S.w = assignment
        assert_allclose(
            S.w[:], expected, err_msg="Assigning %r gave incorrect result" % assignment
        )
        S.w = 0 * volt
        S.w[:] = assignment
        assert_allclose(
            S.w[:], expected, err_msg="Assigning %r gave incorrect result" % assignment
        )

    # without unit checking
    assignment_expected = [
        (5, np.ones(100) * 5 * volt),
        (7, np.ones(100) * 7 * volt),
        (S.i[:], S.i[:] * np.ones(100) * volt),
        ("5", np.ones(100) * 5 * volt),
        ("i", np.ones(100) * S.i[:] * volt),
        ("i +j", S.i[:] * volt + S.j[:] * volt),
        # ('i + j + k', S.i[:]*volt + S.j[:]*volt + S.k[:]*volt) #not supported yet
    ]

    for assignment, expected in assignment_expected:
        S.w = 0 * volt
        S.w_ = assignment
        assert_allclose(
            S.w[:], expected, err_msg="Assigning %r gave incorrect result" % assignment
        )
        S.w = 0 * volt
        S.w_[:] = assignment
        assert_allclose(
            S.w[:], expected, err_msg="Assigning %r gave incorrect result" % assignment
        )


def test_state_variable_indexing():
    G1 = NeuronGroup(5, "v:volt")
    G1.v = "i*mV"
    G2 = NeuronGroup(7, "v:volt")
    G2.v = "10*mV + i*mV"
    S = Synapses(G1, G2, "w:1", multisynaptic_index="k")
    S.connect(True, n=2)
    S.w[:, :, 0] = "5*i + j"
    S.w[:, :, 1] = "35 + 5*i + j"

    # Slicing
    assert len(S.w[:]) == len(S.w[:, :]) == len(S.w[:, :, :]) == len(G1) * len(G2) * 2
    assert len(S.w[0:, 0:]) == len(S.w[0:, 0:, 0:]) == len(G1) * len(G2) * 2
    assert len(S.w[0::2, 0:]) == 3 * len(G2) * 2
    assert len(S.w[0, :]) == len(S.w[0, :, :]) == len(G2) * 2
    assert len(S.w[0:2, :]) == len(S.w[0:2, :, :]) == 2 * len(G2) * 2
    assert len(S.w[:2, :]) == len(S.w[:2, :, :]) == 2 * len(G2) * 2
    assert len(S.w[0:4:2, :]) == len(S.w[0:4:2, :, :]) == 2 * len(G2) * 2
    assert len(S.w[:4:2, :]) == len(S.w[:4:2, :, :]) == 2 * len(G2) * 2
    assert len(S.w[:, 0]) == len(S.w[:, 0, :]) == len(G1) * 2
    assert len(S.w[:, 0:2]) == len(S.w[:, 0:2, :]) == 2 * len(G1) * 2
    assert len(S.w[:, :2]) == len(S.w[:, :2, :]) == 2 * len(G1) * 2
    assert len(S.w[:, 0:4:2]) == len(S.w[:, 0:4:2, :]) == 2 * len(G1) * 2
    assert len(S.w[:, :4:2]) == len(S.w[:, :4:2, :]) == 2 * len(G1) * 2
    assert len(S.w[:, :, 0]) == len(G1) * len(G2)
    assert len(S.w[:, :, 0:2]) == len(G1) * len(G2) * 2
    assert len(S.w[:, :, :2]) == len(G1) * len(G2) * 2
    assert len(S.w[:, :, 0:2:2]) == len(G1) * len(G2)
    assert len(S.w[:, :, :2:2]) == len(G1) * len(G2)

    # 1d indexing is directly indexing synapses!
    assert len(S.w[:]) == len(S.w[0:])
    assert len(S.w[[0, 1]]) == len(S.w[3:5]) == 2
    assert len(S.w[:]) == len(S.w[np.arange(len(G1) * len(G2) * 2)])
    assert S.w[3] == S.w[np.int32(3)] == S.w[np.int64(3)]  # See issue #888

    # Array-indexing (not yet supported for synapse index)
    assert_equal(S.w[:, 0:3], S.w[:, [0, 1, 2]])
    assert_equal(S.w[:, 0:3], S.w[np.arange(len(G1)), [0, 1, 2]])

    # string-based indexing
    assert_equal(S.w[0:3, :], S.w["i<3"])
    assert_equal(S.w[:, 0:3], S.w["j<3"])
    assert_equal(S.w[:, :, 0], S.w["k == 0"])
    assert_equal(S.w[0:3, :], S.w["v_pre < 2.5*mV"])
    assert_equal(S.w[:, 0:3], S.w["v_post < 12.5*mV"])

    # invalid indices
    with pytest.raises(IndexError):
        S.w.__getitem__((1, 2, 3, 4))
    with pytest.raises(IndexError):
        S.w.__getitem__(object())
    with pytest.raises(IndexError):
        S.w.__getitem__(1.5)


def test_indices():
    G = NeuronGroup(10, "v : 1")
    S = Synapses(G, G, "")
    S.connect()
    G.v = "i"

    assert_equal(S.indices[:], np.arange(10 * 10))
    assert len(S.indices[5, :]) == 10
    assert_equal(S.indices["v_pre >=5"], S.indices[5:, :])
    assert_equal(S.indices["j >=5"], S.indices[:, 5:])


def test_subexpression_references():
    """
    Assure that subexpressions in targeted groups are handled correctly.
    """
    G = NeuronGroup(
        10,
        """
        v : 1
        v2 = 2*v : 1
        """,
    )
    G.v = np.arange(10)
    S = Synapses(
        G,
        G,
        """
        w : 1
        u = v2_post + 1 : 1
        x = v2_pre + 1 : 1
        """,
    )
    S.connect("i==(10-1-j)")
    assert_equal(S.u[:], np.arange(10)[::-1] * 2 + 1)
    assert_equal(S.x[:], np.arange(10) * 2 + 1)


@pytest.mark.standalone_compatible
def test_constant_variable_subexpression_in_synapses():
    G = NeuronGroup(10, "")
    S = Synapses(
        G,
        G,
        """
        dv1/dt = -v1**2 / (10*ms) : 1 (clock-driven)
        dv2/dt = -v_const**2 / (10*ms) : 1 (clock-driven)
        dv3/dt = -v_var**2 / (10*ms) : 1 (clock-driven)
        dv4/dt = -v_noflag**2 / (10*ms) : 1 (clock-driven)
        v_const = v2 : 1 (constant over dt)
        v_var = v3 : 1
        v_noflag = v4 : 1
        """,
        method="rk2",
    )
    S.connect(j="i")
    S.v1 = "1.0*i/N"
    S.v2 = "1.0*i/N"
    S.v3 = "1.0*i/N"
    S.v4 = "1.0*i/N"

    run(10 * ms)
    # "variable over dt" subexpressions are directly inserted into the equation
    assert_allclose(S.v3[:], S.v1[:])
    assert_allclose(S.v4[:], S.v1[:])
    # "constant over dt" subexpressions will keep a fixed value over the time
    # step and therefore give a slightly different result for multi-step
    # methods
    assert np.sum((S.v2 - S.v1) ** 2) > 1e-10


@pytest.mark.standalone_compatible
def test_nested_subexpression_references():
    """
    Assure that subexpressions in targeted groups are handled correctly.
    """
    G = NeuronGroup(
        10,
        """
        v : 1
        v2 = 2*v : 1
        v3 = 1.5*v2 : 1
        """,
        threshold="v>=5",
    )
    G2 = NeuronGroup(10, "v : 1")
    G.v = np.arange(10)
    S = Synapses(G, G2, on_pre="v_post += v3_pre")
    S.connect(j="i")
    run(defaultclock.dt)
    assert_allclose(G2.v[:5], 0.0)
    assert_allclose(G2.v[5:], (5 + np.arange(5)) * 3)


@pytest.mark.codegen_independent
def test_equations_unit_check():
    group = NeuronGroup(1, "v : volt", threshold="True")
    syn = Synapses(
        group,
        group,
        """
        sub1 = 3 : 1
        sub2 = sub1 + 1*mV : volt
        """,
        on_pre="v += sub2",
    )
    syn.connect()
    net = Network(group, syn)
    with pytest.raises(BrianObjectException) as exc:
        net.run(0 * ms)
    assert exc_isinstance(exc, DimensionMismatchError)


def test_delay_specification():
    # By default delays are state variables (i.e. arrays), but if they are
    # specified in the initializer, they are scalars.
    G = NeuronGroup(10, "x : meter", threshold="False")
    G.x = "i*mmeter"
    # Array delay
    S = Synapses(G, G, "w:1", on_pre="v+=w")
    S.connect(j="i")
    assert len(S.delay[:]) == len(G)
    S.delay = "i*ms"
    assert_allclose(S.delay[:], np.arange(len(G)) * ms)
    velocity = 1 * meter / second
    S.delay = "abs(x_pre - (N_post-j)*mmeter)/velocity"
    assert_allclose(S.delay[:], abs(G.x - (10 - G.i) * mmeter) / velocity)
    S.delay = 5 * ms
    assert_allclose(S.delay[:], np.ones(len(G)) * 5 * ms)
    # Setting delays without units
    S.delay_ = float(7 * ms)
    assert_allclose(S.delay[:], np.ones(len(G)) * 7 * ms)

    # Scalar delay
    S = Synapses(G, G, "w:1", on_pre="v+=w", delay=5 * ms)
    assert_allclose(S.delay[:], 5 * ms)
    S.connect(j="i")
    S.delay = "3*ms"
    assert_allclose(S.delay[:], 3 * ms)
    S.delay = 10 * ms
    assert_allclose(S.delay[:], 10 * ms)
    # Without units
    S.delay_ = float(20 * ms)
    assert_allclose(S.delay[:], 20 * ms)

    # Invalid arguments
    with pytest.raises(DimensionMismatchError):
        Synapses(G, G, "w:1", on_pre="v+=w", delay=5 * mV)
    with pytest.raises(TypeError):
        Synapses(G, G, "w:1", on_pre="v+=w", delay=object())
    with pytest.raises(ValueError):
        Synapses(G, G, "w:1", delay=5 * ms)
    with pytest.raises(ValueError):
        Synapses(G, G, "w:1", on_pre="v+=w", delay={"post": 5 * ms})


def test_delays_pathways():
    G = NeuronGroup(10, "x: meter", threshold="False")
    G.x = "i*mmeter"
    # Array delay
    S = Synapses(G, G, "w:1", on_pre={"pre1": "v+=w", "pre2": "v+=w"}, on_post="v-=w")
    S.connect(j="i")
    assert len(S.pre1.delay[:]) == len(G)
    assert len(S.pre2.delay[:]) == len(G)
    assert len(S.post.delay[:]) == len(G)
    S.pre1.delay = "i*ms"
    S.pre2.delay = "j*ms"
    velocity = 1 * meter / second
    S.post.delay = "abs(x_pre - (N_post-j)*mmeter)/velocity"
    assert_allclose(S.pre1.delay[:], np.arange(len(G)) * ms)
    assert_allclose(S.pre2.delay[:], np.arange(len(G)) * ms)
    assert_allclose(S.post.delay[:], abs(G.x - (10 - G.i) * mmeter) / velocity)
    S.pre1.delay = 5 * ms
    S.pre2.delay = 10 * ms
    S.post.delay = 1 * ms
    assert_allclose(S.pre1.delay[:], np.ones(len(G)) * 5 * ms)
    assert_allclose(S.pre2.delay[:], np.ones(len(G)) * 10 * ms)
    assert_allclose(S.post.delay[:], np.ones(len(G)) * 1 * ms)
    # Indexing with strings
    assert len(S.pre1.delay["j<5"]) == 5
    assert_allclose(S.pre1.delay["j<5"], 5 * ms)
    # Indexing with 2d indices
    assert len(S.post.delay[[3, 4], :]) == 2
    assert_allclose(S.post.delay[[3, 4], :], 1 * ms)
    assert len(S.pre2.delay[:, 7]) == 1
    assert_allclose(S.pre2.delay[:, 7], 10 * ms)
    assert len(S.pre1.delay[[1, 2], [1, 2]]) == 2
    assert_allclose(S.pre1.delay[[1, 2], [1, 2]], 5 * ms)

    # Scalar delay
    S = Synapses(
        G,
        G,
        "w:1",
        on_pre={"pre1": "v+=w", "pre2": "v+=w"},
        on_post="v-=w",
        delay={"pre1": 5 * ms, "post": 1 * ms},
    )
    assert_allclose(S.pre1.delay[:], 5 * ms)
    assert_allclose(S.post.delay[:], 1 * ms)
    S.connect(j="i")
    assert len(S.pre2.delay[:]) == len(G)
    S.pre1.delay = 10 * ms
    assert_allclose(S.pre1.delay[:], 10 * ms)
    S.post.delay = "3*ms"
    assert_allclose(S.post.delay[:], 3 * ms)


def test_delays_pathways_subgroups():
    G = NeuronGroup(10, "x: meter", threshold="False")
    G.x = "i*mmeter"
    # Array delay
    S = Synapses(
        G[:5], G[5:], "w:1", on_pre={"pre1": "v+=w", "pre2": "v+=w"}, on_post="v-=w"
    )
    S.connect(j="i")
    assert len(S.pre1.delay[:]) == 5
    assert len(S.pre2.delay[:]) == 5
    assert len(S.post.delay[:]) == 5
    S.pre1.delay = "i*ms"
    S.pre2.delay = "j*ms"
    velocity = 1 * meter / second
    S.post.delay = "abs(x_pre - (N_post-j)*mmeter)/velocity"
    assert_allclose(S.pre1.delay[:], np.arange(5) * ms)
    assert_allclose(S.pre2.delay[:], np.arange(5) * ms)
    assert_allclose(S.post.delay[:], abs(G[:5].x - (5 - G[:5].i) * mmeter) / velocity)
    S.pre1.delay = 5 * ms
    S.pre2.delay = 10 * ms
    S.post.delay = 1 * ms
    assert_allclose(S.pre1.delay[:], np.ones(5) * 5 * ms)
    assert_allclose(S.pre2.delay[:], np.ones(5) * 10 * ms)
    assert_allclose(S.post.delay[:], np.ones(5) * 1 * ms)


@pytest.mark.codegen_independent
def test_pre_before_post():
    # The pre pathway should be executed before the post pathway
    G = NeuronGroup(
        1,
        """
        x : 1
        y : 1
        """,
        threshold="True",
    )
    S = Synapses(G, G, "", on_pre="x=1; y=1", on_post="x=2")
    S.connect()
    run(defaultclock.dt)
    # Both pathways should have been executed, but post should have overriden
    # the x value (because it was executed later)
    assert G.x == 2
    assert G.y == 1


@pytest.mark.standalone_compatible
def test_pre_post_simple():
    # Test that pre and post still work correctly
    G1 = SpikeGeneratorGroup(1, [0], [1] * ms)
    G2 = SpikeGeneratorGroup(1, [0], [2] * ms)
    with catch_logs() as l:
        S = Synapses(
            G1,
            G2,
            """
            pre_value : 1
            post_value : 1
            """,
            pre="pre_value +=1",
            post="post_value +=2",
        )
    S.connect()
    syn_mon = StateMonitor(S, ["pre_value", "post_value"], record=[0], when="end")
    run(3 * ms)
    offset = schedule_propagation_offset()
    assert_allclose(syn_mon.pre_value[0][syn_mon.t < 1 * ms + offset], 0)
    assert_allclose(syn_mon.pre_value[0][syn_mon.t >= 1 * ms + offset], 1)
    assert_allclose(syn_mon.post_value[0][syn_mon.t < 2 * ms + offset], 0)
    assert_allclose(syn_mon.post_value[0][syn_mon.t >= 2 * ms + offset], 2)


@pytest.mark.standalone_compatible
def test_transmission_simple():
    source = SpikeGeneratorGroup(2, [0, 1], [2, 1] * ms)
    target = NeuronGroup(2, "v : 1")
    syn = Synapses(source, target, on_pre="v += 1")
    syn.connect(j="i")
    mon = StateMonitor(target, "v", record=True, when="end")
    run(2.5 * ms)
    offset = schedule_propagation_offset()
    assert_allclose(mon[0].v[mon.t < 2 * ms + offset], 0.0)
    assert_allclose(mon[0].v[mon.t >= 2 * ms + offset], 1.0)
    assert_allclose(mon[1].v[mon.t < 1 * ms + offset], 0.0)
    assert_allclose(mon[1].v[mon.t >= 1 * ms + offset], 1.0)


@pytest.mark.standalone_compatible
def test_transmission_custom_event():
    source = NeuronGroup(
        2,
        "",
        events={
            "custom": (
                "timestep(t,dt)>=timestep((2-i)*ms, dt) "
                "and timestep(t,dt)<timestep((2-i)*ms + dt, dt)"
            )
        },
    )
    target = NeuronGroup(2, "v : 1")
    syn = Synapses(source, target, on_pre="v += 1", on_event="custom")
    syn.connect(j="i")
    mon = StateMonitor(target, "v", record=True, when="end")
    run(2.5 * ms)
    assert_allclose(mon[0].v[mon.t < 2 * ms], 0.0)
    assert_allclose(mon[0].v[mon.t >= 2 * ms], 1.0)
    assert_allclose(mon[1].v[mon.t < 1 * ms], 0.0)
    assert_allclose(mon[1].v[mon.t >= 1 * ms], 1.0)


@pytest.mark.standalone_compatible
def test_transmission_custom_event_complex():
    # This was broken with release 2.6, entries in on_event where checked against pre/post
    # instead of the pathway name
    group = NeuronGroup(3, "v:1", threshold="v>1 and v<2", events={"over_2": "v>2"})
    group.v = [0, 1.5, 2.5]

    s = Synapses(
        group,
        group,
        model="""a:integer
                b:integer
                c:integer
                d:integer""",
        on_event={
            "path_a": "spike",
            "path_b": "over_2",
            "path_c": "spike",
            "path_d": "over_2",
        },
        on_pre={"path_a": "a+=1", "path_b": "b+=1"},
        on_post={"path_c": "c+=1", "path_d": "d+=1"},
    )
    s.connect()
    run(defaultclock.dt)
    assert all(s.a[:] == [0, 0, 0, 1, 1, 1, 0, 0, 0])
    assert all(s.b[:] == [0, 0, 0, 0, 0, 0, 1, 1, 1])
    assert all(s.c[:] == [0, 1, 0, 0, 1, 0, 0, 1, 0])
    assert all(s.d[:] == [0, 0, 1, 0, 0, 1, 0, 0, 1])


@pytest.mark.codegen_independent
def test_invalid_custom_event():
    group1 = NeuronGroup(
        2,
        "v : 1",
        events={
            "custom": (
                "timestep(t,dt)>=timesteep((2-i)*ms,dt) "
                "and timestep(t, dt)<timestep((2-i)*ms + dt, dt)"
            )
        },
    )
    group2 = NeuronGroup(2, "v : 1", threshold="v>1")
    with pytest.raises(ValueError) as ex:
        Synapses(group1, group1, on_pre="v+=1")
    assert "threshold" in str(ex)  # Check that it mentions the threshold arg
    with pytest.raises(ValueError):
        Synapses(group2, group2, on_pre="v+=1", on_event="custom")


def test_transmission():
    default_dt = defaultclock.dt
    delays = [
        [0, 0, 0, 0] * ms,
        [1, 1, 1, 0] * ms,
        [0, 1, 2, 3] * ms,
        [2, 2, 0, 0] * ms,
        [2, 1, 0, 1] * ms,
    ]
    for delay in delays:
        # Make sure that the Synapses class actually propagates spikes :)
        source = NeuronGroup(
            4,
            """
            dv/dt = rate : 1
            rate : Hz
            """,
            threshold="v>1",
            reset="v=0",
        )
        source.rate = [51, 101, 101, 51] * Hz
        target = NeuronGroup(4, "v:1", threshold="v>1", reset="v=0")

        source_mon = SpikeMonitor(source)
        target_mon = SpikeMonitor(target)

        S = Synapses(source, target, on_pre="v+=1.1")
        S.connect(j="i")
        S.delay = delay
        net = Network(S, source, target, source_mon, target_mon)
        net.run(50 * ms + default_dt + max(delay))
        # All spikes should trigger spikes in the receiving neurons with
        # the respective delay ( + one dt)
        for d in range(len(delay)):
            assert_allclose(
                source_mon.t[source_mon.i == d],
                target_mon.t[target_mon.i == d] - default_dt - delay[d],
            )


@pytest.mark.standalone_compatible
def test_transmission_all_to_one_heterogeneous_delays():
    source = SpikeGeneratorGroup(
        6,
        [0, 1, 4, 5, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5],
        [0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2] * defaultclock.dt,
    )
    target = NeuronGroup(1, "v : 1")
    synapses = Synapses(source, target, "w : 1", on_pre="v_post += w")
    synapses.connect()
    synapses.w = [1, 2, 3, 4, 5, 6]
    synapses.delay = [0, 0, 0, 1, 2, 1] * defaultclock.dt

    mon = StateMonitor(target, "v", record=True, when="end")
    if schedule_propagation_offset() == 0 * second:
        offset = 0
    else:
        offset = 1
    run((4 + offset) * defaultclock.dt)
    assert mon[0].v[0 + offset] == 3
    assert mon[0].v[1 + offset] == 12
    assert mon[0].v[2 + offset] == 33
    assert mon[0].v[3 + offset] == 48


@pytest.mark.standalone_compatible
def test_transmission_one_to_all_heterogeneous_delays():
    source = SpikeGeneratorGroup(1, [0, 0], [0, 2] * defaultclock.dt)
    target = NeuronGroup(6, "v:integer")
    synapses = Synapses(source, target, on_pre="v_post += 1")
    synapses.connect()
    synapses.delay = [
        1,
        1,
        2,
        4,
        3,
        2,
    ] * defaultclock.dt - schedule_propagation_offset()

    mon = StateMonitor(target, "v", record=True, when="end")
    run(5 * defaultclock.dt)
    assert_allclose(mon[0].v, [0, 1, 1, 2, 2])
    assert_allclose(mon[1].v, [0, 1, 1, 2, 2])
    assert_allclose(mon[2].v, [0, 0, 1, 1, 2])
    assert_allclose(mon[3].v, [0, 0, 0, 0, 1])
    assert_allclose(mon[4].v, [0, 0, 0, 1, 1])
    assert_allclose(mon[5].v, [0, 0, 1, 1, 2])


@pytest.mark.standalone_compatible
def test_transmission_scalar_delay():
    inp = SpikeGeneratorGroup(2, [0, 1], [0, 1] * ms)
    target = NeuronGroup(2, "v:1")
    S = Synapses(inp, target, on_pre="v+=1", delay=0.5 * ms)
    S.connect(j="i")
    mon = StateMonitor(target, "v", record=True, when="end")
    run(2 * ms)
    offset = schedule_propagation_offset()
    assert_allclose(mon[0].v[mon.t < 0.5 * ms + offset - defaultclock.dt / 2], 0)
    assert_allclose(mon[0].v[mon.t >= 0.5 * ms + offset - defaultclock.dt / 2], 1)
    assert_allclose(mon[1].v[mon.t < 1.5 * ms + offset - defaultclock.dt / 2], 0)
    assert_allclose(mon[1].v[mon.t >= 1.5 * ms + offset - defaultclock.dt / 2], 1)


@pytest.mark.standalone_compatible
def test_transmission_scalar_delay_different_clocks():
    inp = SpikeGeneratorGroup(
        2,
        [0, 1],
        [0, 1] * ms,
        dt=0.5 * ms,
        # give the group a unique name to always
        # get a 'fresh' warning
        name="sg_%d" % uuid.uuid4(),
    )
    target = NeuronGroup(2, "v:1", dt=0.1 * ms)
    S = Synapses(inp, target, on_pre="v+=1", delay=0.5 * ms)
    S.connect(j="i")
    mon = StateMonitor(target, "v", record=True, when="end")

    if get_device() == all_devices["runtime"]:
        # We should get a warning when using inconsistent dts
        with catch_logs() as l:
            run(2 * ms)
            assert len(l) == 1, "expected a warning, got %d" % len(l)
            assert l[0][1].endswith("synapses_dt_mismatch")

    run(0 * ms)
    assert_allclose(mon[0].v[mon.t < 0.5 * ms], 0)
    assert_allclose(mon[0].v[mon.t >= 0.5 * ms], 1)
    assert_allclose(mon[1].v[mon.t < 1.5 * ms], 0)
    assert_allclose(mon[1].v[mon.t >= 1.5 * ms], 1)


@pytest.mark.standalone_compatible
def test_transmission_boolean_variable():
    source = SpikeGeneratorGroup(4, [0, 1, 2, 3], [2, 1, 2, 1] * ms)
    target = NeuronGroup(4, "v : 1")
    syn = Synapses(source, target, "use : boolean (constant)", on_pre="v += int(use)")
    syn.connect(j="i")
    syn.use = "i<2"
    mon = StateMonitor(target, "v", record=True, when="end")
    run(2.5 * ms)
    offset = schedule_propagation_offset()
    assert_allclose(mon[0].v[mon.t < 2 * ms + offset], 0.0)
    assert_allclose(mon[0].v[mon.t >= 2 * ms + offset], 1.0)
    assert_allclose(mon[1].v[mon.t < 1 * ms + offset], 0.0)
    assert_allclose(mon[1].v[mon.t >= 1 * ms + offset], 1.0)
    assert_allclose(mon[2].v, 0.0)
    assert_allclose(mon[3].v, 0.0)


@pytest.mark.codegen_independent
def test_clocks():
    """
    Make sure that a `Synapse` object uses the correct clocks.
    """
    source_dt = 0.05 * ms
    target_dt = 0.1 * ms
    synapse_dt = 0.2 * ms
    source = NeuronGroup(1, "v:1", dt=source_dt, threshold="False")
    target = NeuronGroup(1, "v:1", dt=target_dt, threshold="False")
    synapse = Synapses(
        source, target, "w:1", on_pre="v+=1", on_post="v+=1", dt=synapse_dt
    )
    synapse.connect()

    assert synapse.pre.clock is source.clock
    assert synapse.post.clock is target.clock
    assert synapse.pre._clock.dt == source_dt
    assert synapse.post._clock.dt == target_dt
    assert synapse._clock.dt == synapse_dt


def test_equations_with_clocks():
    """
    Make sure that dt of a `Synapse` object is correctly resolved.
    """
    source_dt = 0.1 * ms
    synapse_dt = 1 * ms
    source_target = NeuronGroup(1, "v:1", dt=source_dt, threshold="False")
    synapse = Synapses(
        source_target,
        source_target,
        "dw/dt = 1/ms : 1 (clock-driven)",
        dt=synapse_dt,
        method="euler",
    )
    synapse.connect()
    synapse.w = 0
    run(1 * ms)

    assert synapse.w[0] == 1


def test_changed_dt_spikes_in_queue():
    defaultclock.dt = 0.5 * ms
    G1 = NeuronGroup(1, "v:1", threshold="v>1", reset="v=0")
    G1.v = 1.1
    G2 = NeuronGroup(10, "v:1", threshold="v>1", reset="v=0")
    S = Synapses(G1, G2, on_pre="v+=1.1")
    S.connect(True)
    S.delay = "j*ms"
    mon = SpikeMonitor(G2)
    net = Network(G1, G2, S, mon)
    net.run(5 * ms)
    defaultclock.dt = 1 * ms
    net.run(3 * ms)
    defaultclock.dt = 0.1 * ms
    net.run(2 * ms)
    # Spikes should have delays of 0, 1, 2, ... ms and always
    # trigger a spike one dt later
    expected = [
        0.5,
        1.5,
        2.5,
        3.5,
        4.5,  # dt=0.5ms
        6,
        7,
        8,  # dt = 1ms
        8.1,
        9.1,  # dt=0.1ms
    ] * ms
    assert_allclose(mon.t[:], expected)


@pytest.mark.codegen_independent
def test_no_synapses():
    # Synaptic pathway but no synapses
    G1 = NeuronGroup(1, "", threshold="True")
    G2 = NeuronGroup(1, "v:1")
    S = Synapses(G1, G2, on_pre="v+=1")
    net = Network(G1, G2, S)
    with pytest.raises(BrianObjectException) as exc:
        net.run(0 * ms)
    assert exc_isinstance(exc, TypeError)


@pytest.mark.codegen_independent
def test_no_synapses_variable_write():
    # Synaptic pathway but no synapses
    G1 = NeuronGroup(1, "", threshold="True")
    G2 = NeuronGroup(1, "v:1")
    S = Synapses(G1, G2, "w : 1", on_pre="v+=w")
    # Setting synaptic variables before calling connect is not allowed
    with pytest.raises(TypeError):
        setattr(S, "w", 1)
    with pytest.raises(TypeError):
        setattr(S, "delay", 1 * ms)


@pytest.mark.standalone_compatible
def test_summed_variable():
    source = NeuronGroup(2, "v : volt", threshold="v>1*volt", reset="v=0*volt")
    source.v = 1.1 * volt  # will spike immediately
    target = NeuronGroup(2, "v : volt")
    S = Synapses(
        source,
        target,
        """
        w : volt
        x : volt
        v_post = 2*x : volt (summed)
        """,
        on_pre="x+=w",
        multisynaptic_index="k",
    )
    S.connect("i==j", n=2)
    S.w["k == 0"] = "i*volt"
    S.w["k == 1"] = "(i + 0.5)*volt"
    net = Network(source, target, S)
    net.run(1 * ms)

    # v of the target should be the sum of the two weights
    assert_allclose(target.v, np.array([1.0, 5.0]) * volt)


@pytest.mark.standalone_compatible
def test_summed_variable_pre_and_post():
    G1 = NeuronGroup(
        4,
        """
        neuron_var : 1
        syn_sum : 1
        neuron_sum : 1
        """,
    )
    G1.neuron_var = "i"
    G2 = NeuronGroup(
        4,
        """
        neuron_var : 1
        syn_sum : 1
        neuron_sum : 1
        """,
    )
    G2.neuron_var = "i+4"

    synapses = Synapses(
        G1,
        G2,
        """
        syn_var : 1
        neuron_sum_pre = neuron_var_post : 1 (summed)
        syn_sum_pre = syn_var : 1 (summed)
        neuron_sum_post = neuron_var_pre : 1 (summed)
        syn_sum_post = syn_var : 1 (summed)
        """,
    )
    # The first three cells in G1 connect to the first cell in G2
    # The remaining three cells of G2 all connect to the last cell of G1
    synapses.connect(i=[0, 1, 2, 3, 3, 3], j=[0, 0, 0, 1, 2, 3])
    synapses.syn_var = [0, 1, 2, 3, 4, 5]

    run(defaultclock.dt)
    assert_allclose(G1.syn_sum[:], [0, 1, 2, 12])
    assert_allclose(G1.neuron_sum[:], [4, 4, 4, 18])
    assert_allclose(G2.syn_sum[:], [3, 3, 4, 5])
    assert_allclose(G2.neuron_sum[:], [3, 3, 3, 3])


@pytest.mark.standalone_compatible
def test_summed_variable_differing_group_size():
    G1 = NeuronGroup(2, "var : 1", name="G1")
    G2 = NeuronGroup(10, "var : 1", name="G2")
    G2.var[:5] = 1
    G2.var[5:] = 10
    syn1 = Synapses(
        G1,
        G2,
        """
        syn_var : 1
        var_pre = syn_var + var_post : 1 (summed)
        """,
    )
    syn1.connect(i=0, j=[0, 1, 2, 3, 4])
    syn1.connect(i=1, j=[5, 6, 7, 8, 9])
    syn1.syn_var = np.arange(10)
    # The same in the other direction
    G3 = NeuronGroup(10, "var : 1", name="G3")
    G4 = NeuronGroup(2, "var : 1", name="G4")
    G3.var[:5] = 1
    G3.var[5:] = 10
    syn2 = Synapses(
        G3,
        G4,
        """
        syn_var : 1
        var_post = syn_var + var_pre : 1 (summed)
        """,
    )
    syn2.connect(i=[0, 1, 2, 3, 4], j=0)
    syn2.connect(i=[5, 6, 7, 8, 9], j=1)
    syn2.syn_var = np.arange(10)

    run(defaultclock.dt)

    assert_allclose(G1.var[0], 5 * 1 + 0 + 1 + 2 + 3 + 4)
    assert_allclose(G1.var[1], 5 * 10 + 5 + 6 + 7 + 8 + 9)

    assert_allclose(G4.var[0], 5 * 1 + 0 + 1 + 2 + 3 + 4)
    assert_allclose(G4.var[1], 5 * 10 + 5 + 6 + 7 + 8 + 9)


def test_summed_variable_errors():
    G = NeuronGroup(
        10,
        """
        dv/dt = -v / (10*ms) : volt
        sub = 2*v : volt
        p : volt
        """,
        threshold="False",
        reset="",
    )

    # Using the (summed) flag for a differential equation or a parameter
    with pytest.raises(ValueError):
        Synapses(G, G, """dp_post/dt = -p_post / (10*ms) : volt (summed)""")
    with pytest.raises(ValueError):
        Synapses(G, G, """p_post : volt (summed)""")

    # Using the (summed) flag for a variable name without _pre or _post suffix
    with pytest.raises(ValueError):
        Synapses(G, G, """p = 3*volt : volt (summed)""")
    # Using the name of a variable that does not exist
    with pytest.raises(ValueError):
        Synapses(G, G, """q_post = 3*volt : volt (summed)""")

    # Target equation is not a parameter
    with pytest.raises(ValueError):
        Synapses(G, G, """sub_post = 3*volt : volt (summed)""")
    with pytest.raises(ValueError):
        Synapses(G, G, """v_post = 3*volt : volt (summed)""")

    # Unit mismatch between synapses and target
    with pytest.raises(DimensionMismatchError):
        Synapses(G, G, """p_post = 3*second : second (summed)""")

    # Two summed variable equations targetting the same variable
    with pytest.raises(ValueError):
        Synapses(
            G,
            G,
            """
            p_post = 3*volt : volt (summed)
            p_pre = 3*volt : volt (summed)
            """,
        )

    # Summed variable referring to an event-driven variable
    with pytest.raises(EquationError) as ex:
        Synapses(
            G,
            G,
            """
            ds/dt = -s/(3*ms) : volt (event-driven)
            p_post = s : volt (summed)
            """,
            on_pre="s += 1*mV",
        )
    assert "'p_post'" in str(ex.value) and "'s'" in str(ex.value)

    # Indirect dependency
    with pytest.raises(EquationError) as ex:
        Synapses(
            G,
            G,
            """
            ds/dt = -s/(3*ms) : volt (event-driven)
            x = s : volt
            y = x : volt
            p_post = y : 1 (summed)
            """,
            on_pre="s += 1*mV",
        )
    assert "'p_post'" in str(ex.value) and "'s'" in str(ex.value)
    assert "'x'" in str(ex.value) and "'y'" in str(ex.value)

    with pytest.raises(BrianObjectException) as ex:
        S = Synapses(
            G,
            G,
            """
            y : siemens
            p_post = y : volt (summed)
            """,
        )
        run(0 * ms)

    assert isinstance(ex.value.__cause__, DimensionMismatchError)


@pytest.mark.codegen_independent
def test_multiple_summed_variables():
    # See github issue #766
    source = NeuronGroup(1, "")
    target = NeuronGroup(10, "v : 1")
    syn1 = Synapses(source, target, "v_post = 1 : 1 (summed)")
    syn1.connect()
    syn2 = Synapses(source, target, "v_post = 1 : 1 (summed)")
    syn2.connect()
    net = Network(collect())
    with pytest.raises(NotImplementedError):
        net.run(0 * ms)


@pytest.mark.standalone_compatible
def test_summed_variables_subgroups():
    source = NeuronGroup(1, "")
    target = NeuronGroup(10, "v : 1")
    subgroup1 = target[:6]
    subgroup2 = target[6:]
    syn1 = Synapses(source, subgroup1, "v_post = 1 : 1 (summed)")
    syn1.connect(n=2)
    syn2 = Synapses(source, subgroup2, "v_post = 1 : 1 (summed)")
    syn2.connect()
    run(defaultclock.dt)
    assert_allclose(target.v[:6], 2 * np.ones(6))
    assert_allclose(target.v[6:], 1 * np.ones(4))


@pytest.mark.codegen_independent
def test_summed_variables_overlapping_subgroups():
    # See github issue #766
    source = NeuronGroup(1, "")
    target = NeuronGroup(10, "v : 1")
    # overlapping subgroups
    subgroup1 = target[:7]
    subgroup2 = target[6:]
    syn1 = Synapses(source, subgroup1, "v_post = 1 : 1 (summed)")
    syn1.connect(n=2)
    syn2 = Synapses(source, subgroup2, "v_post = 1 : 1 (summed)")
    syn2.connect()
    net = Network(collect())
    with pytest.raises(NotImplementedError):
        net.run(0 * ms)


@pytest.mark.codegen_independent
def test_summed_variables_linked_variables():
    source = NeuronGroup(1, "")
    target1 = NeuronGroup(10, "v : 1")
    target2 = NeuronGroup(10, "v : 1 (linked)")
    target2.v = linked_var(target1.v)
    # Seemingly independent targets, but the variable is the same
    syn1 = Synapses(source, target1, "v_post = 1 : 1 (summed)")
    syn1.connect()
    syn2 = Synapses(source, target2, "v_post = 1 : 1 (summed)")
    syn2.connect()
    net = Network(collect())
    with pytest.raises(NotImplementedError):
        net.run(0 * ms)


@pytest.mark.codegen_independent
def test_linked_to_shared_variables():
    source1 = NeuronGroup(1, "dx/dt = -x / (10*ms) : 1")
    source1.x = "rand()"
    source2 = NeuronGroup(10, "x : 1 (shared)")
    source2.x = "rand()"
    mon = StateMonitor(source1, "x", record=True)
    group = NeuronGroup(10, "")
    syn = Synapses(
        group,
        group,
        """x1 : 1 (linked)
                      x2 : 1 (linked)
                   """,
    )
    syn.x1 = linked_var(source1.x)
    syn.x2 = linked_var(source2.x)
    syn.connect(i=[0, 5], j=[3, 6])
    mon_syn = StateMonitor(syn, ["x1", "x2"], record=True)
    run(2 * defaultclock.dt)
    assert_allclose(mon.x[0], mon_syn.x1[0])
    assert_allclose(mon.x[0], mon_syn.x1[1])
    assert_allclose(source2.x, mon_syn.x2[0])
    assert_allclose(source2.x, mon_syn.x2[1])


@pytest.mark.codegen_independent
def test_linked_to_non_shared_variables():
    source = NeuronGroup(10, "dx/dt = -x / (10*ms) : 1")
    source.x = "rand()"
    mon = StateMonitor(source, "x", record=True)
    group = NeuronGroup(10, "y: 1")
    syn = Synapses(
        group,
        group,
        """x : 1 (linked)
                      x_ind: integer
                      not_an_index : 1
                   """,
    )
    syn.connect(i=[0, 5], j=[3, 6])
    with pytest.raises(TypeError):
        syn.x = linked_var(source.x, index=[0, 1])
    with pytest.raises(ValueError):
        syn.x = linked_var(source.x, index="does_not_exist")
    with pytest.raises(ValueError):
        syn.x = linked_var(source.x, index="y_post")
    with pytest.raises(TypeError):
        syn.x = linked_var(source.x, index="not_an_index")
    syn.x = linked_var(source.x, index="x_ind")
    syn.x_ind = [3, 5]
    mon_syn = StateMonitor(syn, "x", record=True)
    run(2 * defaultclock.dt)
    assert_allclose(mon.x[3], mon_syn.x[0])
    assert_allclose(mon.x[5], mon_syn.x[1])


def test_scalar_parameter_access():
    G = NeuronGroup(
        10,
        """
        v : 1
        scalar : Hz (shared)
        """,
        threshold="False",
    )
    S = Synapses(
        G,
        G,
        """
        w : 1
        s : Hz (shared)
        number : 1 (shared)
        """,
        on_pre="v+=w*number",
    )
    S.connect()

    # Try setting a scalar variable
    S.s = 100 * Hz
    assert_allclose(S.s[:], 100 * Hz)
    S.s[:] = 200 * Hz
    assert_allclose(S.s[:], 200 * Hz)
    S.s = "s - 50*Hz + number*Hz"
    assert_allclose(S.s[:], 150 * Hz)
    S.s[:] = "50*Hz"
    assert_allclose(S.s[:], 50 * Hz)

    # Set a postsynaptic scalar variable
    S.scalar_post = 100 * Hz
    assert_allclose(G.scalar[:], 100 * Hz)
    S.scalar_post[:] = 100 * Hz
    assert_allclose(G.scalar[:], 100 * Hz)

    # Check the second method of accessing that works
    assert_allclose(np.asanyarray(S.s), 50 * Hz)

    # Check error messages
    with pytest.raises(IndexError):
        S.s[0]
    with pytest.raises(IndexError):
        S.s[1]
    with pytest.raises(IndexError):
        S.s[0:1]
    with pytest.raises(IndexError):
        S.s["i>5"]

    with pytest.raises(ValueError):
        S.s.set_item(slice(None), [0, 1] * Hz)
    with pytest.raises(IndexError):
        S.s.set_item(0, 100 * Hz)
    with pytest.raises(IndexError):
        S.s.set_item(1, 100 * Hz)
    with pytest.raises(IndexError):
        S.s.set_item("i>5", 100 * Hz)


def test_scalar_subexpression():
    G = NeuronGroup(
        10,
        """
        v : 1
        number : 1 (shared)
        """,
        threshold="False",
    )
    S = Synapses(
        G,
        G,
        """
        s : 1 (shared)
        sub = number_post + s : 1 (shared)
        """,
        on_pre="v+=s",
    )
    S.connect()
    S.s = 100
    G.number = 50
    assert S.sub[:] == 150

    with pytest.raises(SyntaxError):
        Synapses(
            G,
            G,
            """
            s : 1 (shared)
            sub = v_post + s : 1 (shared)
            """,
            on_pre="v+=s",
        )


@pytest.mark.standalone_compatible
def test_sim_with_scalar_variable():
    inp = SpikeGeneratorGroup(2, [0, 1], [0, 0] * ms)
    out = NeuronGroup(2, "v : 1")
    syn = Synapses(
        inp,
        out,
        """
        w : 1
        s : 1 (shared)
        """,
        on_pre="v += s + w",
    )
    syn.connect(j="i")
    syn.w = [1, 2]
    syn.s = 5
    run(2 * defaultclock.dt)
    assert_allclose(out.v[:], [6, 7])


@pytest.mark.standalone_compatible
def test_sim_with_scalar_subexpression():
    inp = SpikeGeneratorGroup(2, [0, 1], [0, 0] * ms)
    out = NeuronGroup(2, "v : 1")
    syn = Synapses(
        inp,
        out,
        """
        w : 1
        s = 5 : 1 (shared)
        """,
        on_pre="v += s + w",
    )
    syn.connect(j="i")
    syn.w = [1, 2]
    run(2 * defaultclock.dt)
    assert_allclose(out.v[:], [6, 7])


@pytest.mark.standalone_compatible
def test_sim_with_constant_subexpression():
    inp = SpikeGeneratorGroup(2, [0, 1], [0, 0] * ms)
    out = NeuronGroup(2, "v : 1")
    syn = Synapses(
        inp,
        out,
        """
        w : 1
        s = 5 : 1 (constant over dt)
        """,
        on_pre="v += s + w",
    )
    syn.connect(j="i")
    syn.w = [1, 2]
    run(2 * defaultclock.dt)
    assert_allclose(out.v[:], [6, 7])


@pytest.mark.standalone_compatible
def test_external_variables():
    # Make sure that external variables are correctly resolved
    source = SpikeGeneratorGroup(1, [0], [0] * ms)
    target = NeuronGroup(1, "v:1")
    w_var = 1
    amplitude = 2
    syn = Synapses(source, target, "w=w_var : 1", on_pre="v+=amplitude*w")
    syn.connect()
    run(defaultclock.dt)
    assert target.v[0] == 2


@pytest.mark.standalone_compatible
def test_event_driven():
    # Fake example, where the synapse is actually not changing the state of the
    # postsynaptic neuron, the pre- and post spiketrains are regular spike
    # trains with different rates
    pre = NeuronGroup(
        2,
        """
        dv/dt = rate : 1
        rate : Hz
        """,
        threshold="v>1",
        reset="v=0",
    )
    pre.rate = [1000, 1500] * Hz
    post = NeuronGroup(
        2,
        """
        dv/dt = rate : 1
        rate : Hz
        """,
        threshold="v>1",
        reset="v=0",
    )
    post.rate = [1100, 1400] * Hz
    # event-driven formulation
    taupre = 20 * ms
    taupost = taupre
    gmax = 0.01
    dApre = 0.01
    dApost = -dApre * taupre / taupost * 1.05
    dApost *= gmax
    dApre *= gmax
    # event-driven
    S1 = Synapses(
        pre,
        post,
        """
        w : 1
        dApre/dt = -Apre/taupre : 1 (event-driven)
        dApost/dt = -Apost/taupost : 1 (event-driven)
        """,
        on_pre="""
        Apre += dApre
        w = clip(w+Apost, 0, gmax)
        """,
        on_post="""
        Apost += dApost
        w = clip(w+Apre, 0, gmax)
        """,
    )
    S1.connect(j="i")
    # not event-driven
    S2 = Synapses(
        pre,
        post,
        """
        w : 1
        Apre : 1
        Apost : 1
        lastupdate : second
        """,
        on_pre="""
        Apre=Apre*exp((lastupdate-t)/taupre)+dApre
        Apost=Apost*exp((lastupdate-t)/taupost)
        w = clip(w+Apost, 0, gmax)
        lastupdate = t
        """,
        on_post="""
        Apre=Apre*exp((lastupdate-t)/taupre)
        Apost=Apost*exp((lastupdate-t)/taupost) +dApost
        w = clip(w+Apre, 0, gmax)
        lastupdate = t
        """,
    )
    S2.connect(j="i")
    S1.w = 0.5 * gmax
    S2.w = 0.5 * gmax
    run(25 * ms)
    # The two formulations should yield identical results
    assert_allclose(S1.w[:], S2.w[:])


@pytest.mark.codegen_independent
def test_event_driven_dependency_checks():
    dummy = NeuronGroup(1, "", threshold="False", reset="")

    # Dependency on parameter
    syn = Synapses(
        dummy,
        dummy,
        """
        da/dt = (a-b) / (5*ms): 1 (event-driven)
        b : 1""",
        on_pre="b+=1",
    )
    syn.connect()

    # Dependency on parameter via subexpression
    syn2 = Synapses(
        dummy,
        dummy,
        """
        da/dt = (a-b) / (5*ms): 1 (event-driven)
        b = c : 1
        c : 1""",
        on_pre="c+=1",
    )
    syn2.connect()
    run(0 * ms)


@pytest.mark.codegen_independent
def test_event_driven_dependency_error():
    stim = SpikeGeneratorGroup(1, [0], [0] * ms, period=5 * ms)
    syn = Synapses(
        stim,
        stim,
        """
        da/dt = -a / (5*ms) : 1 (event-driven)
        db/dt = -b / (5*ms) : 1 (event-driven)
        dc/dt = a*b / (5*ms) : 1 (event-driven)""",
        on_pre="a+=1",
    )
    syn.connect()
    net = Network(collect())
    with pytest.raises(BrianObjectException) as exc:
        net.run(0 * ms)
    assert exc_isinstance(exc, UnsupportedEquationsException)


@pytest.mark.codegen_independent
def test_event_driven_dependency_error2():
    stim = SpikeGeneratorGroup(1, [0], [0] * ms, period=5 * ms)
    tau = 5 * ms
    with pytest.raises(EquationError) as exc:
        syn = Synapses(
            stim,
            stim,
            """
            da/dt = -a / (5*ms) : 1 (clock-driven)
            db/dt = -b / (5*ms) : 1 (clock-driven)
            dc/dt = a*b / (5*ms) : 1 (event-driven)
            """,
            on_pre="a+=1",
        )
    assert "'c'" in str(exc.value) and (
        "'a'" in str(exc.value) or "'b'" in str(exc.value)
    )

    # Indirect dependency
    with pytest.raises(EquationError) as exc:
        syn = Synapses(
            stim,
            stim,
            """
            da/dt = -a / (5*ms) : 1 (clock-driven)
            b = a : 1
            dc/dt = b / (5*ms) : 1 (event-driven)
            """,
            on_pre="a+=1",
        )
    assert (
        "'c'" in str(exc.value) and "'a'" in str(exc.value) and "'b'" in str(exc.value)
    )


@pytest.mark.codegen_independent
def test_event_driven_dependency_error3():
    P = NeuronGroup(10, "dv/dt = -v/(10*ms) : volt")
    with pytest.raises(EquationError) as ex:
        Synapses(
            P,
            P,
            """
            ds/dt = -s/(3*ms) : 1 (event-driven)
            df/dt = f*s/(5*ms) : 1 (clock-driven)
            """,
            on_pre="s += 1",
        )
    assert "'s'" in str(ex.value) and "'f'" in str(ex.value)

    # Indirect dependency
    with pytest.raises(EquationError) as ex:
        Synapses(
            P,
            P,
            """
            ds/dt = -s/(3*ms) : 1 (event-driven)
            x = s : 1
            y = x : 1
            df/dt = f*y/(5*ms) : 1 (clock-driven)
            """,
            on_pre="s += 1",
        )
    assert "'s'" in str(ex.value) and "'f'" in str(ex.value)
    assert "'x'" in str(ex.value) and "'y'" in str(ex.value)


@pytest.mark.codegen_independent
def test_repr():
    G = NeuronGroup(1, "v: volt", threshold="False")
    S = Synapses(
        G,
        G,
        """
        w : 1
        dApre/dt = -Apre/taupre : 1 (event-driven)
        dApost/dt = -Apost/taupost : 1 (event-driven)
        """,
        on_pre="""
        Apre += dApre
        w = clip(w+Apost, 0, gmax)
        """,
        on_post="""
        Apost += dApost
        w = clip(w+Apre, 0, gmax)
        """,
    )
    # Test that string/LaTeX representations do not raise errors
    for func in [str, repr, sympy.latex]:
        assert len(func(S.equations))


@pytest.mark.codegen_independent
def test_pre_post_variables():
    G = NeuronGroup(10, "v : 1", threshold="False")
    G2 = NeuronGroup(
        10,
        """
        v : 1
        w : 1
        """,
        threshold="False",
    )
    S = Synapses(G, G2, "x : 1")
    # Check for the most important variables
    for var in [
        "v_pre",
        "v",
        "v_post",
        "w",
        "w_post",
        "x",
        "N_pre",
        "N_post",
        "N_incoming",
        "N_outgoing",
        "i",
        "j",
        "t",
        "dt",
    ]:
        assert var in S.variables
    # Check that postsynaptic variables without suffix refer to the correct
    # variable
    assert S.variables["v"] is S.variables["v_post"]
    assert S.variables["w"] is S.variables["w_post"]

    # Check that internal pre-/post-synaptic variables are not accessible
    assert "_spikespace_pre" not in S.variables
    assert "_spikespace" not in S.variables
    assert "_spikespace_post" not in S.variables


@pytest.mark.codegen_independent
def test_variables_by_owner():
    # Test the `variables_by_owner` convenience function
    G = NeuronGroup(10, "v : 1")
    G2 = NeuronGroup(
        10,
        """
        v : 1
        w : 1
        """,
    )
    S = Synapses(G, G2, "x : 1")

    # Check that the variables returned as owned by the pre/post groups are the
    # variables stored in the respective groups. We only compare the `Variable`
    # objects, as the names may be different (e.g. ``v_post`` vs. ``v``)
    G_variables = {
        key: value for key, value in G.variables.items() if value.owner.name == G.name
    }  # exclude dt
    G2_variables = {
        key: value for key, value in G2.variables.items() if value.owner.name == G2.name
    }
    assert set(G_variables.values()) == set(variables_by_owner(S.variables, G).values())
    assert set(G2_variables.values()) == set(
        variables_by_owner(S.variables, G2).values()
    )
    assert len(set(variables_by_owner(S.variables, S)) & set(G_variables.values())) == 0
    assert (
        len(set(variables_by_owner(S.variables, S)) & set(G2_variables.values())) == 0
    )
    # Just test a few examples for synaptic variables
    assert all(
        varname in variables_by_owner(S.variables, S)
        for varname in ["x", "N", "N_incoming", "N_outgoing"]
    )


@pytest.mark.codegen_independent
def check_permutation_code(code):
    from collections import defaultdict

    vars = get_identifiers(code)
    indices = defaultdict(lambda: "_idx")
    for var in vars:
        if var.endswith("_syn"):
            indices[var] = "_idx"
        elif var.endswith("_pre"):
            indices[var] = "_presynaptic_idx"
        elif var.endswith("_post"):
            indices[var] = "_postsynaptic_idx"
        elif var.endswith("_const"):
            indices[var] = "0"
    variables = dict()
    variables.update(DEFAULT_FUNCTIONS)
    for var in indices:
        if var.endswith("_const"):
            variables[var] = Constant(var, 42, owner=device)
        else:
            variables[var] = ArrayVariable(var, None, 10, device)
    variables["_presynaptic_idx"] = ArrayVariable(var, None, 10, device)
    variables["_postsynaptic_idx"] = ArrayVariable(var, None, 10, device)
    scalar_statements, vector_statements = make_statements(code, variables, float64)
    check_for_order_independence(vector_statements, variables, indices)


def numerically_check_permutation_code(code):
    # numerically checks that a code block used in the test below is permutation-independent by creating a
    # presynaptic and postsynaptic group of 3 neurons each, and a full connectivity matrix between them, then
    # repeatedly filling in random values for each of the variables, and checking for several random shuffles of
    # the synapse order that the result doesn't depend on it. This is a sort of test of the test itself, to make
    # sure we didn't accidentally assign a good/bad example to the wrong class.
    code = deindent(code)
    from collections import defaultdict

    vars = get_identifiers(code)
    indices = defaultdict(lambda: "_idx")
    vals = {}
    for var in vars:
        if var.endswith("_syn"):
            indices[var] = "_idx"
            vals[var] = zeros(9)
        elif var.endswith("_pre"):
            indices[var] = "_presynaptic_idx"
            vals[var] = zeros(3)
        elif var.endswith("_post"):
            indices[var] = "_postsynaptic_idx"
            vals[var] = zeros(3)
        elif var.endswith("_shared"):
            indices[var] = "0"
            vals[var] = zeros(1)
        elif var.endswith("_const"):
            indices[var] = "0"
            vals[var] = 42
    subs = {
        var: var + "[" + idx + "]"
        for var, idx in indices.items()
        if not var.endswith("_const")
    }
    code = word_substitute(code, subs)
    code = f"""
from numpy import *
from numpy.random import rand, randn
for _idx in shuffled_indices:
    _presynaptic_idx = presyn[_idx]
    _postsynaptic_idx = postsyn[_idx]
{indent(code)}
    """
    ns = vals.copy()
    ns["shuffled_indices"] = arange(9)
    ns["presyn"] = arange(9) % 3
    ns["postsyn"] = arange(9) / 3
    for _ in range(10):
        origvals = {}
        for k, v in vals.items():
            if not k.endswith("_const"):
                v[:] = randn(len(v))
                origvals[k] = v.copy()
        exec(code, ns)
        endvals = {}
        for k, v in vals.items():
            endvals[k] = copy(v)
        for _ in range(10):
            for k, v in vals.items():
                if not k.endswith("_const"):
                    v[:] = origvals[k]
            shuffle(ns["shuffled_indices"])
            exec(code, ns)
            for k, v in vals.items():
                try:
                    assert_allclose(v, endvals[k])
                except AssertionError:
                    raise OrderDependenceError()


SANITY_CHECK_PERMUTATION_ANALYSIS_EXAMPLE = False

permutation_analysis_good_examples = [
    "v_post += w_syn",
    "v_post *= w_syn",
    "v_post = v_post + w_syn",
    "v_post = v_post * w_syn",
    "v_post = w_syn * v_post",
    "v_post += 1",
    "v_post = 1",
    "v_post = c_const",
    "v_post = x_shared",
    "v_post += v_post # NOT_UFUNC_AT_VECTORISABLE",
    "v_post += c_const",
    "v_post += x_shared",
    #'v_post += w_syn*v_post', # this is a hard one (it is good for w*v but bad for w+v)
    "v_post += sin(-v_post) # NOT_UFUNC_AT_VECTORISABLE",
    "v_post += u_post",
    "v_post += w_syn*v_pre",
    "v_post += sin(-v_post) # NOT_UFUNC_AT_VECTORISABLE",
    "v_post -= sin(v_post) # NOT_UFUNC_AT_VECTORISABLE",
    "v_post += v_pre",
    "v_pre += v_post",
    "v_pre += c_const",
    "v_pre += x_shared",
    "w_syn = v_pre",
    "w_syn = a_syn",
    "w_syn += a_syn",
    "w_syn *= a_syn",
    "w_syn -= a_syn",
    "w_syn /= a_syn",
    "w_syn += 1",
    "w_syn += c_const",
    "w_syn += x_shared",
    "w_syn *= 2",
    "w_syn *= c_const",
    "w_syn *= x_shared",
    """
    w_syn = a_syn
    a_syn += 1
    """,
    """
    w_syn = a_syn
    a_syn += c_const
    """,
    """
    w_syn = a_syn
    a_syn += x_shared
    """,
    "v_post *= 2",
    "v_post *= w_syn",
    """
    v_pre = 0
    w_syn = v_pre
    """,
    """
    v_pre = c_const
    w_syn = v_pre
    """,
    """
    v_pre = x_shared
    w_syn = v_pre
    """,
    """
    ge_syn += w_syn
    Apre_syn += 3
    w_syn = clip(w_syn + Apost_syn, 0, 10)
    """,
    """
    ge_syn += w_syn
    Apre_syn += c_const
    w_syn = clip(w_syn + Apost_syn, 0, 10)
    """,
    """
    ge_syn += w_syn
    Apre_syn += x_shared
    w_syn = clip(w_syn + Apost_syn, 0, 10)
    """,
    """
    a_syn = v_pre
    v_post += a_syn
    """,
    """
    v_post += v_post # NOT_UFUNC_AT_VECTORISABLE
    v_post += v_post
    """,
    """
    v_post += 1
    x = v_post
    """,
]

permutation_analysis_bad_examples = [
    "v_pre = w_syn",
    "v_post = v_pre",
    "v_post = w_syn",
    "v_post += w_syn+v_post",
    "v_post += rand()",  # rand() has state, and therefore this is order dependent
    """
    a_syn = v_post
    v_post += w_syn
    """,
    """
    x = w_syn
    v_pre = x
    """,
    """
    x = v_pre
    v_post = x
    """,
    """
    v_post += v_pre
    v_pre += v_post
    """,
    """
    b_syn = v_post
    v_post += a_syn
    """,
    """
    v_post += w_syn
    u_post += v_post
    """,
    """
    v_post += 1
    w_syn = v_post
    """,
]


@pytest.mark.codegen_independent
def test_permutation_analysis():
    # Examples that should work
    for example in permutation_analysis_good_examples:
        if SANITY_CHECK_PERMUTATION_ANALYSIS_EXAMPLE:
            try:
                numerically_check_permutation_code(example)
            except OrderDependenceError:
                raise AssertionError(
                    "Test unexpectedly raised a numerical "
                    "OrderDependenceError on these "
                    "statements:\n" + example
                )
        try:
            check_permutation_code(example)
        except OrderDependenceError:
            raise AssertionError(
                "Test unexpectedly raised an "
                "OrderDependenceError on these "
                "statements:\n" + example
            )

    for example in permutation_analysis_bad_examples:
        if SANITY_CHECK_PERMUTATION_ANALYSIS_EXAMPLE:
            try:
                with pytest.raises(OrderDependenceError):
                    numerically_check_permutation_code(example)
            except AssertionError:
                raise AssertionError(
                    "Order dependence not raised numerically for example: " + example
                )
        try:
            with pytest.raises(OrderDependenceError):
                check_permutation_code(example)
        except AssertionError:
            raise AssertionError("Order dependence not raised for example: " + example)


@pytest.mark.standalone_compatible
def test_vectorisation():
    source = NeuronGroup(10, "v : 1", threshold="v>1")
    target = NeuronGroup(
        10,
        """
        x : 1
        y : 1
        """,
    )
    syn = Synapses(
        source,
        target,
        "w_syn : 1",
        on_pre="""
        v_pre += w_syn
        x_post = y_post
        """,
    )
    syn.connect()
    syn.w_syn = 1
    source.v["i<5"] = 2
    target.y = "i"
    run(defaultclock.dt)
    assert_allclose(source.v[:5], 12)
    assert_allclose(source.v[5:], 0)
    assert_allclose(target.x[:], target.y[:])


@pytest.mark.standalone_compatible
def test_vectorisation_STDP_like():
    # Test the use of pre- and post-synaptic traces that are stored in the
    # pre/post group instead of in the synapses
    w_max = 10
    neurons = NeuronGroup(
        6,
        """
        dv/dt = rate : 1
        ge : 1
        rate : Hz
        dA/dt = -A/(1*ms) : 1
        """,
        threshold="v>1",
        reset="v=0",
    )
    # Note that the synapse does not actually increase the target v, we want
    # to have simple control about when neurons spike. Also, we separate the
    # "depression" and "facilitation" completely. The example also uses
    # subgroups, which should complicate things further.
    # This test should try to capture the spirit of indexing in such a use case,
    # it simply compares the results to fixed pre-calculated values
    syn = Synapses(
        neurons[:3],
        neurons[3:],
        """
        w_dep : 1
        w_fac : 1
        """,
        on_pre="""
        ge_post += w_dep - w_fac
        A_pre += 1
        w_dep = clip(w_dep + A_post, 0, w_max)
        """,
        on_post="""
        A_post += 1
        w_fac = clip(w_fac + A_pre, 0, w_max)
        """,
    )
    syn.connect()
    neurons.rate = 1000 * Hz
    neurons.v = "abs(3-i)*0.1 + 0.7"
    run(2 * ms)
    # Make sure that this test is invariant to synapse order
    indices = np.argsort(
        np.array(list(zip(syn.i[:], syn.j[:])), dtype=[("i", "<i4"), ("j", "<i4")]),
        order=["i", "j"],
    )
    assert_allclose(
        syn.w_dep[:][indices],
        [
            1.29140162,
            1.16226149,
            1.04603529,
            1.16226149,
            1.04603529,
            0.94143176,
            1.04603529,
            0.94143176,
            6.2472887,
        ],
        atol=0.0001,
    )
    assert_allclose(
        syn.w_fac[:][indices],
        [
            5.06030369,
            5.62256002,
            6.2472887,
            5.62256002,
            6.2472887,
            6.941432,
            6.2472887,
            6.941432,
            1.04603529,
        ],
        atol=0.0001,
    )
    assert_allclose(
        neurons.A[:],
        [1.69665715, 1.88517461, 2.09463845, 2.32737606, 2.09463845, 1.88517461],
        atol=0.0001,
    )
    assert_allclose(
        neurons.ge[:],
        [0.0, 0.0, 0.0, -7.31700015, -8.13000011, -4.04603529],
        atol=0.0001,
    )


@pytest.mark.standalone_compatible
def test_synaptic_equations():
    # Check that integration works for synaptic equations
    G = NeuronGroup(10, "")
    tau = 10 * ms
    S = Synapses(G, G, "dw/dt = -w / tau : 1 (clock-driven)")
    S.connect(j="i")
    S.w = "i"
    run(10 * ms)
    assert_allclose(S.w[:], np.arange(10) * np.exp(-1))


@pytest.mark.standalone_compatible
def test_synapse_with_run_regularly():
    # Check that integration works for synaptic equations
    G = NeuronGroup(10, "v : 1", threshold="False")
    tau = 10 * ms
    S = Synapses(G, G, "w : 1", on_pre="v += w")
    S.connect(j="i")
    S.run_regularly("w = i")
    run(defaultclock.dt)
    assert_allclose(S.w[:], np.arange(10))


@pytest.mark.standalone_compatible
def test_synapses_to_synapses():
    source = SpikeGeneratorGroup(3, [0, 1, 2], [0, 0, 0] * ms, period=2 * ms)
    modulator = SpikeGeneratorGroup(3, [0, 2], [1, 3] * ms)
    target = NeuronGroup(3, "v : integer")
    conn = Synapses(source, target, "w : integer", on_pre="v += w")
    conn.connect(j="i")
    conn.w = 1
    modulatory_conn = Synapses(modulator, conn, on_pre="w += 1")
    modulatory_conn.connect(j="i")
    run(5 * ms)
    # First group has its weight increased to 2 after the first spike
    # Third group has its weight increased to 2 after the second spike
    assert_array_equal(target.v, [5, 3, 4])


@pytest.mark.standalone_compatible
def test_synapses_to_synapses_connection_array():
    source = SpikeGeneratorGroup(3, [0, 1, 2], [0, 0, 0] * ms, period=2 * ms)
    modulator = SpikeGeneratorGroup(3, [0, 2], [1, 3] * ms)
    target = NeuronGroup(3, "v : integer")
    conn = Synapses(source, target, "w : integer", on_pre="v += w")
    conn.connect(i=[0, 1, 2], j=[0, 1, 2])
    conn.w = 1
    modulatory_conn = Synapses(modulator, conn, on_pre="w += 1")
    modulatory_conn.connect(i=[0, 1, 2], j=[0, 1, 2])
    run(5 * ms)
    # First group has its weight increased to 2 after the first spike
    # Third group has its weight increased to 2 after the second spike
    assert_array_equal(target.v, [5, 3, 4])


@pytest.mark.standalone_compatible
def test_synapses_to_synapses_rule_and_array():
    source = SpikeGeneratorGroup(3, [0, 1, 2], [0, 0, 0] * ms, period=2 * ms)
    modulator = SpikeGeneratorGroup(3, [0, 2], [1, 3] * ms)
    target = NeuronGroup(3, "v : integer")
    conn = Synapses(source, target, "w : integer", on_pre="v += w")
    conn.connect(j="i")
    conn.w = 1
    modulatory_conn = Synapses(modulator, conn, on_pre="w += 1")
    # This will raise a warning in standalone mode, since we cannot check
    # whether the 'j' indices actually correspond to existing synapses
    # (the j='i' connection from above has not been executed yet)
    with catch_logs() as l:
        modulatory_conn.connect(i=[0, 1, 2], j=[0, 1, 2])
    if isinstance(get_device(), CPPStandaloneDevice):
        assert len(l) == 1
        assert l[0][0] == "WARNING"
        assert l[0][1] == "brian2.synapses.synapses.cannot_check_synapse_indices"
    else:
        assert len(l) == 0
    run(5 * ms)
    # First group has its weight increased to 2 after the first spike
    # Third group has its weight increased to 2 after the second spike
    assert_array_equal(target.v, [5, 3, 4])


@pytest.mark.standalone_compatible
def test_synapses_to_synapses_statevar_access():
    source = NeuronGroup(10, "v:1")
    modulator = NeuronGroup(40, "")
    target = NeuronGroup(10, "v:1")
    conn = Synapses(source, target)
    conn.connect(j="i", n=2)
    modulator_to_conn = Synapses(modulator, conn)
    modulator_to_conn.connect(j="int(i/2)")
    conn_to_modulator = Synapses(conn, modulator)
    conn_to_modulator.connect(j="i")
    conn_to_modulator.connect(j="i + 20")
    run(0 * ms)
    assert_equal(modulator_to_conn.i, np.arange(40))
    assert_equal(modulator_to_conn.j, np.repeat(np.arange(20), 2))
    assert_equal(modulator_to_conn.i_post, np.repeat(np.arange(10), 4))
    assert_equal(modulator_to_conn.j_post, np.repeat(np.arange(10), 4))
    assert_equal(conn_to_modulator.i, np.hstack([np.arange(20), np.arange(20)]))
    assert_equal(
        conn_to_modulator.i_pre,
        np.hstack([np.repeat(np.arange(10), 2), np.repeat(np.arange(10), 2)]),
    )
    assert_equal(
        conn_to_modulator.j_pre,
        np.hstack([np.repeat(np.arange(10), 2), np.repeat(np.arange(10), 2)]),
    )
    assert_equal(conn_to_modulator.j, np.arange(40))


@pytest.mark.standalone_compatible
def test_synapses_to_synapses_different_sizes():
    source = NeuronGroup(100, "v : 1", threshold="False")
    source.v = "i"
    modulator = NeuronGroup(1, "v : 1", threshold="False")
    target = NeuronGroup(100, "v : 1")
    target.v = "i + 100"
    conn = Synapses(source, target, "w:1", multisynaptic_index="k")
    conn.connect(j="i", n=2)
    conn.w = "i + j"
    modulatory_conn = Synapses(modulator, conn)
    modulatory_conn.connect("k_post == 1")  # only second synapse is targeted
    run(0 * ms)
    assert_allclose(modulatory_conn.w_post, 2 * np.arange(100))


def test_ufunc_at_vectorisation():
    if prefs.codegen.target != "numpy":
        pytest.skip("numpy-only test")
    for code in permutation_analysis_good_examples:
        should_be_able_to_use_ufunc_at = not "NOT_UFUNC_AT_VECTORISABLE" in code
        if should_be_able_to_use_ufunc_at:
            use_ufunc_at_list = [False, True]
        else:
            use_ufunc_at_list = [True]
        code = deindent(code)
        vars = get_identifiers(code)
        vars_src = []
        vars_tgt = []
        vars_syn = []
        vars_shared = []
        vars_const = {}
        for var in vars:
            if var.endswith("_pre"):
                vars_src.append(var[:-4])
            elif var.endswith("_post"):
                vars_tgt.append(var[:-5])
            elif var.endswith("_syn"):
                vars_syn.append(var[:-4])
            elif var.endswith("_shared"):
                vars_shared.append(var[:-7])
            elif var.endswith("_const"):
                vars_const[var[:-6]] = 42
        eqs_src = "\n".join(var + ":1" for var in vars_src)
        eqs_tgt = "\n".join(var + ":1" for var in vars_tgt)
        eqs_syn = "\n".join(var + ":1" for var in vars_syn)
        eqs_syn += "\n" + "\n".join(var + ":1 (shared)" for var in vars_shared)
        origvals = {}
        endvals = {}
        try:
            BrianLogger._log_messages.clear()
            with catch_logs(log_level=logging.INFO) as caught_logs:
                for use_ufunc_at in use_ufunc_at_list:
                    NumpyCodeGenerator._use_ufunc_at_vectorisation = use_ufunc_at
                    src = NeuronGroup(3, eqs_src, threshold="True", name="src")
                    tgt = NeuronGroup(3, eqs_tgt, name="tgt")
                    syn = Synapses(
                        src,
                        tgt,
                        eqs_syn,
                        on_pre=code.replace("_syn", "")
                        .replace("_const", "")
                        .replace("_shared", ""),
                        name="syn",
                        namespace=vars_const,
                    )
                    syn.connect()
                    for G, vars in [(src, vars_src), (tgt, vars_tgt), (syn, vars_syn)]:
                        for var in vars:
                            fullvar = var + G.name
                            if fullvar in origvals:
                                G.state(var)[:] = origvals[fullvar]
                            else:
                                val = rand(len(G))
                                G.state(var)[:] = val
                                origvals[fullvar] = val.copy()
                    Network(src, tgt, syn).run(defaultclock.dt)
                    for G, vars in [(src, vars_src), (tgt, vars_tgt), (syn, vars_syn)]:
                        for var in vars:
                            fullvar = var + G.name
                            val = G.state(var)[:].copy()
                            if fullvar in endvals:
                                assert_allclose(val, endvals[fullvar])
                            else:
                                endvals[fullvar] = val
                numpy_generator_messages = [
                    l
                    for l in caught_logs
                    if l[1] == "brian2.codegen.generators.numpy_generator"
                ]
                if should_be_able_to_use_ufunc_at:
                    assert len(numpy_generator_messages) == 0
                else:
                    assert len(numpy_generator_messages) == 1
                    log_lev, log_mod, log_msg = numpy_generator_messages[0]
                    assert log_msg.startswith("Failed to vectorise code")
        finally:
            NumpyCodeGenerator._use_ufunc_at_vectorisation = True  # restore it


def test_fallback_loop_and_stateless_func():
    # See github issue #1024
    if prefs.codegen.target != "numpy":
        pytest.skip("numpy-only test")
    source = NeuronGroup(2, "", threshold="True")
    target = NeuronGroup(1, "v : 1")
    synapses = Synapses(
        source,
        target,
        "x : 1",
        on_pre="""x = rand()
                                  v_post += 0.5*(1-v_post)""",
    )
    synapses.connect()
    with catch_logs():  # Suppress the warning
        run(defaultclock.dt)


@pytest.mark.standalone_compatible
def test_synapses_to_synapses_summed_variable():
    source = NeuronGroup(5, "")
    target = NeuronGroup(5, "")
    conn = Synapses(source, target, "w : integer")
    conn.connect(j="i")
    conn.w = 1
    summed_conn = Synapses(
        source,
        conn,
        """
        w_post = x : integer (summed)
        x : integer
        """,
    )
    summed_conn.connect("i>=j")
    summed_conn.x = "i"
    run(defaultclock.dt)
    assert_array_equal(conn.w[:], [10, 10, 9, 7, 4])


@pytest.mark.codegen_independent
def test_synapse_generator_syntax():
    parsed = parse_synapse_generator("k for k in sample(1, N, p=p) if abs(i-k)<10")
    assert parsed["element"] == "k"
    assert parsed["inner_variable"] == "k"
    assert parsed["iterator_func"] == "sample"
    assert parsed["iterator_kwds"]["low"] == "1"
    assert parsed["iterator_kwds"]["high"] == "N"
    assert parsed["iterator_kwds"]["step"] == "1"
    assert parsed["iterator_kwds"]["p"] == "p"
    assert parsed["iterator_kwds"]["size"] is None
    assert parsed["iterator_kwds"]["sample_size"] == "random"
    assert parsed["if_expression"] == "abs(i - k) < 10"
    parsed = parse_synapse_generator("k for k in sample(N, size=5) if abs(i-k)<10")
    assert parsed["element"] == "k"
    assert parsed["inner_variable"] == "k"
    assert parsed["iterator_func"] == "sample"
    assert parsed["iterator_kwds"]["low"] == "0"
    assert parsed["iterator_kwds"]["high"] == "N"
    assert parsed["iterator_kwds"]["step"] == "1"
    assert parsed["iterator_kwds"]["p"] is None
    assert parsed["iterator_kwds"]["size"] == "5"
    assert parsed["iterator_kwds"]["sample_size"] == "fixed"
    assert parsed["if_expression"] == "abs(i - k) < 10"
    parsed = parse_synapse_generator("k+1 for k in range(i-100, i+100, 2)")
    assert parsed["element"] == "k + 1"
    assert parsed["inner_variable"] == "k"
    assert parsed["iterator_func"] == "range"
    assert parsed["iterator_kwds"]["low"] == "i - 100"
    assert parsed["iterator_kwds"]["high"] == "i + 100"
    assert parsed["iterator_kwds"]["step"] == "2"
    assert parsed["if_expression"] == "True"
    with pytest.raises(SyntaxError):
        parse_synapse_generator("mad rubbish")
    with pytest.raises(SyntaxError):
        parse_synapse_generator("k+1")
    with pytest.raises(SyntaxError):
        parse_synapse_generator("k for k in range()")
    with pytest.raises(SyntaxError):
        parse_synapse_generator("k for k in range(1,2,3,4)")
    with pytest.raises(SyntaxError):
        parse_synapse_generator("k for k in range(1,2,3) if ")
    with pytest.raises(SyntaxError):
        parse_synapse_generator("k[1:3] for k in range(1,2,3)")
    with pytest.raises(SyntaxError):
        parse_synapse_generator("k for k in x")
    with pytest.raises(SyntaxError):
        parse_synapse_generator("k for k in x[1:5]")
    with pytest.raises(SyntaxError):
        parse_synapse_generator("k for k in sample()")
    with pytest.raises(SyntaxError):
        parse_synapse_generator("k for k in sample(N, p=0.1, size=5)")
    with pytest.raises(SyntaxError):
        parse_synapse_generator("k for k in sample(N, q=0.1)")


def test_synapse_generator_out_of_range():
    G = NeuronGroup(16, "v : 1")
    G2 = NeuronGroup(4, "v : 1")
    G2.v = "16 + i"

    S1 = Synapses(G, G2, "")
    with pytest.raises(BrianObjectException) as exc:
        S1.connect(j="k for k in range(0, N_post*2)")
        exc.errisinstance(IndexError)

    # This should be fine
    S2 = Synapses(G, G, "")
    S2.connect(j="i+k for k in range(0, 5) if i <= N_post-5")
    expected = np.zeros((len(G), len(G)))
    expected[np.triu_indices(len(G))] = 1
    expected[np.triu_indices(len(G), 5)] = 0
    expected[len(G) - 4 :, :] = 0
    _compare(S2, expected)

    # This should be fine (see #1037)
    S2 = Synapses(G, G, "")
    S2.connect(j="i+k for k in range(0, 5) if i <= N_post-5 and rand() <= 1")
    _compare(S2, expected)

    # This could in principle be fine, but we cannot test the condition without
    # accessing the post-synaptic variable outside of its range. By analyzing
    # the post-synaptic condition, we could find out that the value of this
    # variable is actually irrelevant, but that makes things too complicated.
    S3 = Synapses(G, G, "")
    with pytest.raises(BrianObjectException) as exc:
        S3.connect(j="i+k for k in range(0, 5) if i <= N_post-5 and v_post >= 0")
    assert exc_isinstance(exc, IndexError)
    assert "outside allowed range" in str(exc.value.__cause__)


@pytest.mark.standalone_compatible
def test_synapse_generator_deterministic():
    # Same as "test_connection_string_deterministic" but using the generator
    # syntax
    G = NeuronGroup(16, "v : 1")
    G.v = "i"
    G2 = NeuronGroup(4, "v : 1")
    G2.v = "16 + i"

    # Full connection
    expected_full = np.ones((len(G), len(G2)))

    S1 = Synapses(G, G2)
    S1.connect(j="k for k in range(N_post)")

    # Full connection without self-connections
    expected_no_self = np.ones((len(G), len(G))) - np.eye(len(G))

    S2 = Synapses(G, G)
    S2.connect(j="k for k in range(N_post) if k != i")

    S3 = Synapses(G, G)
    # slightly confusing with j on the RHS, but it should work...
    S3.connect(j="k for k in range(N_post) if j != i")

    S4 = Synapses(G, G)
    S4.connect(j="k for k in range(N_post) if v_post != v_pre")

    # One-to-one connectivity
    expected_one_to_one = np.eye(len(G))

    S5 = Synapses(G, G)
    S5.connect(j="k for k in range(N_post) if k == i")  # inefficient

    S6 = Synapses(G, G)
    # slightly confusing with j on the RHS, but it should work...
    S6.connect(j="k for k in range(N_post) if j == i")  # inefficient

    S7 = Synapses(G, G)
    S7.connect(j="k for k in range(N_post) if v_pre == v_post")  # inefficient

    S8 = Synapses(G, G)
    S8.connect(j="i for _ in range(1)")  # efficient

    S9 = Synapses(G, G)
    S9.connect(j="i")  # short form of the above

    with catch_logs() as _:  # Ignore warnings about empty synapses
        run(0 * ms)  # for standalone

    _compare(S1, expected_full)
    _compare(S2, expected_no_self)
    _compare(S3, expected_no_self)
    _compare(S4, expected_no_self)
    _compare(S5, expected_one_to_one)
    _compare(S6, expected_one_to_one)
    _compare(S7, expected_one_to_one)
    _compare(S8, expected_one_to_one)
    _compare(S9, expected_one_to_one)


@pytest.mark.standalone_compatible
def test_synapse_generator_deterministic_over_postsynaptic():
    # Same as "test_connection_string_deterministic" but using the generator
    # syntax and iterating over post-synaptic variables
    G = NeuronGroup(16, "v : 1")
    G.v = "i"
    G2 = NeuronGroup(4, "v : 1")
    G2.v = "16 + i"

    # Full connection
    expected_full = np.ones((len(G), len(G2)))

    S1 = Synapses(G, G2)
    S1.connect(i="k for k in range(N_pre)")

    # Full connection without self-connections
    expected_no_self = np.ones((len(G), len(G))) - np.eye(len(G))

    S2 = Synapses(G, G)
    S2.connect(i="k for k in range(N_pre) if k != j")

    S3 = Synapses(G, G)
    # slightly confusing with i on the RHS, but it should work...
    S3.connect(i="k for k in range(N_pre) if i != j")

    S4 = Synapses(G, G)
    S4.connect(j="k for k in range(N_pre) if v_pre != v_post")

    # One-to-one connectivity
    expected_one_to_one = np.eye(len(G))

    S5 = Synapses(G, G)
    S5.connect(i="k for k in range(N_pre) if k == j")  # inefficient

    S6 = Synapses(G, G)
    # slightly confusing with j on the RHS, but it should work...
    S6.connect(i="k for k in range(N_pre) if i == j")  # inefficient

    S7 = Synapses(G, G)
    S7.connect(i="k for k in range(N_pre) if v_pre == v_post")  # inefficient

    S8 = Synapses(G, G)
    S8.connect(i="j for _ in range(1)")  # efficient

    S9 = Synapses(G, G)
    S9.connect(i="j")  # short form of the above

    with catch_logs() as _:  # Ignore warnings about empty synapses
        run(0 * ms)  # for standalone

    _compare(S1, expected_full)
    _compare(S2, expected_no_self)
    _compare(S3, expected_no_self)
    _compare(S4, expected_no_self)
    _compare(S5, expected_one_to_one)
    _compare(S6, expected_one_to_one)
    _compare(S7, expected_one_to_one)
    _compare(S8, expected_one_to_one)
    _compare(S9, expected_one_to_one)


@pytest.mark.standalone_compatible
@pytest.mark.long
def test_synapse_generator_deterministic_2():
    # Same as "test_connection_string_deterministic" but using the generator
    # syntax
    G = NeuronGroup(16, "")
    G2 = NeuronGroup(4, "")
    # A few more tests of deterministic connections where the generator syntax
    # is particularly useful

    # Ring structure
    S10 = Synapses(G, G)
    S10.connect(j="(i + (-1)**k) % N_post for k in range(2)")
    expected_ring = np.zeros((len(G), len(G)), dtype=np.int32)
    expected_ring[np.arange(15), np.arange(15) + 1] = 1  # Next cell
    expected_ring[np.arange(1, 16), np.arange(15)] = 1  # Previous cell
    expected_ring[[0, 15], [15, 0]] = 1  # wrap around the ring

    # Diverging connection pattern
    S11 = Synapses(G2, G)
    S11.connect(j="i*4 + k for k in range(4)")
    expected_diverging = np.zeros((len(G2), len(G)), dtype=np.int32)
    for source in range(4):
        expected_diverging[source, np.arange(4) + source * 4] = 1

    # Diverging connection pattern within population (no self-connections)
    S11b = Synapses(G2, G2)
    S11b.connect(j="k for k in range(i-3, i+4) if i!=k", skip_if_invalid=True)
    expected_diverging_b = np.zeros((len(G2), len(G2)), dtype=np.int32)
    for source in range(len(G2)):
        expected_diverging_b[
            source, np.clip(np.arange(-3, 4) + source, 0, len(G2) - 1)
        ] = 1
        expected_diverging_b[source, source] = 0

    # Converging connection pattern
    S12 = Synapses(G, G2)
    S12.connect(j="int(i/4)")
    expected_converging = np.zeros((len(G), len(G2)), dtype=np.int32)
    for target in range(4):
        expected_converging[np.arange(4) + target * 4, target] = 1

    # skip if invalid
    S13 = Synapses(G2, G2)
    S13.connect(j="i+(-1)**k for k in range(2)", skip_if_invalid=True)
    expected_offdiagonal = np.zeros((len(G2), len(G2)), dtype=np.int32)
    expected_offdiagonal[np.arange(len(G2) - 1), np.arange(len(G2) - 1) + 1] = 1
    expected_offdiagonal[np.arange(len(G2) - 1) + 1, np.arange(len(G2) - 1)] = 1

    # Converging connection pattern with restriction
    S14 = Synapses(G, G2)
    S14.connect(j="int(i/4) if i % 2 == 0")
    expected_converging_restricted = np.zeros((len(G), len(G2)), dtype=np.int32)
    for target in range(4):
        expected_converging_restricted[np.arange(4, step=2) + target * 4, target] = 1

    # Connecting to post indices >= source index
    expected_diagonal = np.zeros((len(G), len(G)), dtype=np.int32)
    expected_diagonal[np.triu_indices(len(G))] = 1
    S15 = Synapses(G, G)
    S15.connect(j="i + k for k in range(0, N_post-i)")

    S15b = Synapses(G, G)
    S15b.connect(j="i + k for k in range(0, N_post)", skip_if_invalid=True)

    S15c = Synapses(G, G)
    S15c.connect(j="i + k for k in range(0, N_post) if j < N_post")

    S15d = Synapses(G, G)
    S15d.connect(j="i + k for k in range(0, N_post) if i + k < N_post")

    with catch_logs() as _:  # Ignore warnings about empty synapses
        run(0 * ms)  # for standalone

    _compare(S10, expected_ring)
    _compare(S11, expected_diverging)
    _compare(S11b, expected_diverging_b)
    _compare(S12, expected_converging)
    _compare(S13, expected_offdiagonal)
    _compare(S14, expected_converging_restricted)
    _compare(S15, expected_diagonal)
    _compare(S15b, expected_diagonal)
    _compare(S15c, expected_diagonal)
    _compare(S15d, expected_diagonal)


@pytest.mark.standalone_compatible
def test_synapse_generator_random():
    # The same tests as test_connection_random_without_condition, but using
    # the generator syntax
    G = NeuronGroup(4, "x : integer")
    G.x = "i"
    G2 = NeuronGroup(7, "")

    S1 = Synapses(G, G2)
    S1.connect(j="k for k in sample(N_post, p=0)")

    S2 = Synapses(G, G2)
    S2.connect(j="k for k in sample(N_post, p=1)")

    # Just make sure using values between 0 and 1 work in principle
    S3 = Synapses(G, G2)
    S3.connect(j="k for k in sample(N_post, p=0.3)")

    # Use pre-/post-synaptic variables for "stochastic" connections that are
    # actually deterministic
    S4 = Synapses(G, G2)
    S4.connect(j="k for k in sample(N_post, p=int(x_pre==2)*1.0)")

    with catch_logs() as _:  # Ignore warnings about empty synapses
        run(0 * ms)  # for standalone

    assert len(S1) == 0
    _compare(S2, np.ones((len(G), len(G2))))
    assert 0 <= len(S3) <= len(G) * len(G2)
    assert len(S4) == 7
    assert_equal(S4.i, np.ones(7) * 2)
    assert_equal(S4.j, np.arange(7))


@pytest.mark.standalone_compatible
def test_synapse_generator_random_over_postsynaptic():
    # The same tests as test_connection_random_without_condition, but using
    # the generator syntax and iterating over post-synaptic neurons
    G = NeuronGroup(4, "")
    G2 = NeuronGroup(7, "y : 1")
    G2.y = "i"

    S1 = Synapses(G, G2)
    S1.connect(i="k for k in sample(N_pre, p=0)")

    S2 = Synapses(G, G2)
    S2.connect(i="k for k in sample(N_pre, p=1)")

    # Just make sure using values between 0 and 1 work in principle
    S3 = Synapses(G, G2)
    S3.connect(i="k for k in sample(N_pre, p=0.3)")

    # Use pre-/post-synaptic variables for "stochastic" connections that are
    # actually deterministic
    S4 = Synapses(G, G2)
    S4.connect(i="k for k in sample(N_pre, p=int(y_post==2)*1.0)")

    with catch_logs() as _:  # Ignore warnings about empty synapses
        run(0 * ms)  # for standalone

    assert len(S1) == 0
    _compare(S2, np.ones((len(G), len(G2))))
    assert 0 <= len(S3) <= len(G) * len(G2)
    assert len(S4) == 4
    assert_equal(S4.i, np.arange(4))
    assert_equal(S4.j, np.ones(4) * 2)


@pytest.mark.standalone_compatible
def test_synapse_generator_random_positive_steps():
    # Test generator with sampling from stepped ranges (e.g. all even numbers)
    G = NeuronGroup(4, "x : integer")
    G.x = "i"
    G2 = NeuronGroup(7, "")

    S1 = Synapses(G, G2)
    S1.connect(j="k for k in sample(2, N_post, 2, p=0)")

    S2 = Synapses(G, G2)
    S2.connect(j="k for k in sample(2, N_post, 2, p=1)")

    # Just make sure using values between 0 and 1 work in principle (note that
    # 0.25 is the cutoff between the general method and the "jump method", so
    # we test a value above and below
    S3 = Synapses(G, G2)
    S3.connect(j="k for k in sample(2, N_post, 2, p=0.2)")

    S3b = Synapses(G, G2)
    S3b.connect(j="k for k in sample(2, N_post, 2, p=0.3)")

    # Use pre-/post-synaptic variables for "stochastic" connections that are
    # actually deterministic
    S4 = Synapses(G, G2)
    S4.connect(j="k for k in sample(2, N_post, 2, p=int(x_pre==2)*1.0)")

    with catch_logs() as _:  # Ignore warnings about empty synapses
        run(0 * ms)  # for standalone

    assert len(S1) == 0
    S2_comp = np.zeros((len(G), len(G2)))
    S2_comp[:, 2::2] = 1
    _compare(S2, S2_comp)
    assert 0 <= len(S3) <= len(G) * 3
    assert all(S3.j[:] % 2 == 0)
    assert all(S3.j >= 2)
    assert 0 <= len(S3b) <= len(G) * 3
    assert all(S3b.j[:] % 2 == 0)
    assert all(S3b.j >= 2)
    assert len(S4) == 3
    assert_equal(S4.i, np.ones(3) * 2)
    assert_equal(S4.j, np.arange(2, 7, 2))


@pytest.mark.standalone_compatible
def test_synapse_generator_random_negative_steps():
    # Test generator with sampling from stepped ranges (e.g. all even numbers)
    # going backwards
    G = NeuronGroup(4, "x : integer")
    G.x = "i"
    G2 = NeuronGroup(7, "")

    S1 = Synapses(G, G2)
    S1.connect(j="k for k in sample(N_post-1, 0, -2, p=0)")

    S2 = Synapses(G, G2)
    S2.connect(j="k for k in sample(N_post-1, 0, -2, p=1)")

    # Just make sure using values between 0 and 1 work in principle (note that
    # 0.25 is the cutoff between the general method and the "jump method", so
    # we test a value above and below
    S3 = Synapses(G, G2)
    S3.connect(j="k for k in sample(N_post-1, 0, -2, p=0.2)")

    S3b = Synapses(G, G2)
    S3b.connect(j="k for k in sample(N_post-1, 0, -2, p=0.3)")

    # Use pre-/post-synaptic variables for "stochastic" connections that are
    # actually deterministic
    S4 = Synapses(G, G2)
    S4.connect(j="k for k in sample(N_post-1, 0, -2, p=int(x_pre==2)*1.0)")

    with catch_logs() as _:  # Ignore warnings about empty synapses
        run(0 * ms)  # for standalone

    assert len(S1) == 0
    S2_comp = np.zeros((len(G), len(G2)))
    S2_comp[:, 2::2] = 1
    _compare(S2, S2_comp)
    assert 0 <= len(S3) <= len(G) * 3
    assert all(S3.j[:] % 2 == 0)
    assert all(S3.j >= 2)
    assert 0 <= len(S3b) <= len(G) * 3
    assert all(S3b.j[:] % 2 == 0)
    assert all(S3b.j >= 2)
    assert len(S4) == 3
    assert_array_equal(S4.i, np.ones(3) * 2)
    assert_array_equal(S4.j, [6, 4, 2])


@pytest.mark.standalone_compatible
def test_synapse_generator_fixed_random():
    # Random samples with fixed size
    G = NeuronGroup(4, "x : integer")
    G.x = "i"
    G2 = NeuronGroup(7, "")

    S1 = Synapses(G, G2)
    S1.connect(j="k for k in sample(N_post, size=0)")

    S2 = Synapses(G, G2)
    S2.connect(j="k for k in sample(N_post, size=N_post)")

    S3 = Synapses(G, G2)
    S3.connect(j="k for k in sample(N_post, size=3)")

    # Use pre-/post-synaptic variables for "stochastic" connections that are
    # actually deterministic
    S4 = Synapses(G, G2)
    S4.connect(j="k for k in sample(N_post, size=int(x_pre==2)*N_post)")

    with catch_logs() as _:  # Ignore warnings about empty synapses
        run(0 * ms)  # for standalone

    assert len(S1) == 0
    _compare(S2, np.ones((len(G), len(G2))))
    # Each neuron should have 3 outgoing connections
    assert_array_equal(S3.N_outgoing_pre, np.ones(4) * 3)
    # Synapses should be sorted and unique
    for source_idx in range(4):
        assert len(set(S3.j[source_idx, :])) == 3
        assert all(S3.j[source_idx, :] == sorted(S3.j[source_idx, :]))
    assert len(S4) == 7
    assert_equal(S4.i, np.ones(7) * 2)
    assert_equal(S4.j, np.arange(7))


@pytest.mark.standalone_compatible
def test_synapse_generator_fixed_random_over_postsynaptic():
    # Random samples with fixed size, iterating over post-synaptic neurons
    G = NeuronGroup(4, "")
    G2 = NeuronGroup(7, "y : integer")
    G2.y = "i"

    S1 = Synapses(G, G2)
    S1.connect(i="k for k in sample(N_pre, size=0)")

    S2 = Synapses(G, G2)
    S2.connect(i="k for k in sample(N_pre, size=N_pre)")

    S3 = Synapses(G, G2)
    S3.connect(i="k for k in sample(N_pre, size=3)")

    # Use pre-/post-synaptic variables for "stochastic" connections that are
    # actually deterministic
    S4 = Synapses(G, G2)
    S4.connect(i="k for k in sample(N_pre, size=int(y_post==2)*N_pre)")

    with catch_logs() as _:  # Ignore warnings about empty synapses
        run(0 * ms)  # for standalone

    assert len(S1) == 0
    _compare(S2, np.ones((len(G), len(G2))))
    # Each neuron should have 3 incoming connections
    assert_array_equal(S3.N_incoming_post, np.ones(7) * 3)
    # Synapses should be sorted and unique
    for target_idx in range(7):
        assert len(set(S3.i[:, target_idx])) == 3
        assert all(S3.i[:, target_idx] == sorted(S3.i[:, target_idx]))
    assert len(S4) == 4
    assert_equal(S4.j, np.ones(4) * 2)
    assert_equal(S4.i, np.arange(4))


@pytest.mark.standalone_compatible
def test_synapse_generator_fixed_random_positive_steps():
    # Test generator with fixed-size sampling from stepped ranges (e.g. all
    # even numbers)
    G = NeuronGroup(4, "x : integer")
    G.x = "i"
    G2 = NeuronGroup(7, "")

    S1 = Synapses(G, G2)
    S1.connect(j="k for k in sample(2, N_post, 2, size=0)")

    S2 = Synapses(G, G2)
    S2.connect(j="k for k in sample(2, N_post, 2, size=3)")

    # Just make sure using values between 0 and 1 work in principle
    S3 = Synapses(G, G2)
    S3.connect(j="k for k in sample(2, N_post, 2, size=2)")

    # Use pre-/post-synaptic variables for "stochastic" connections that are
    # actually deterministic
    S4 = Synapses(G, G2)
    S4.connect(j="k for k in sample(2, N_post, 2, size=int(x_pre==2)*3)")

    with catch_logs() as _:  # Ignore warnings about empty synapses
        run(0 * ms)  # for standalone

    assert len(S1) == 0
    S2_comp = np.zeros((len(G), len(G2)))
    S2_comp[:, 2::2] = 1
    _compare(S2, S2_comp)
    assert len(S3) == len(G) * 2
    assert all(S3.N_outgoing_pre == 2)
    assert all(S3.j[:] % 2 == 0)
    assert all(S3.j >= 2)
    assert all([len(S3.j[x, :]) == len(set(S3.j[x, :])) for x in range(len(G))])
    assert len(S4) == 3
    assert_equal(S4.i, np.ones(3) * 2)
    assert_equal(S4.j, np.arange(2, 7, 2))


@pytest.mark.standalone_compatible
def test_synapse_generator_fixed_random_negative_steps():
    # Test generator with fixed-size sampling from stepped ranges (e.g. all
    # even numbers) going backwards
    G = NeuronGroup(4, "x : integer")
    G.x = "i"
    G2 = NeuronGroup(7, "")

    S1 = Synapses(G, G2)
    S1.connect(j="k for k in sample(N_post-1, 0, -2, size=0)")

    S2 = Synapses(G, G2)
    S2.connect(j="k for k in sample(N_post-1, 0, -2, size=3)")

    # Just make sure using intermediate values between 0 and 1 work in principle
    S3 = Synapses(G, G2)
    S3.connect(j="k for k in sample(N_post-1, 0, -2, size=2)")

    # Use pre-/post-synaptic variables for "stochastic" connections that are
    # actually deterministic
    S4 = Synapses(G, G2, "w:1")
    S4.connect(j="k for k in sample(N_post-1, 0, -2, size=int(x_pre==2)*3)")

    with catch_logs() as _:  # Ignore warnings about empty synapses
        run(0 * ms)  # for standalone

    assert len(S1) == 0
    S2_comp = np.zeros((len(G), len(G2)))
    S2_comp[:, 2::2] = 1
    _compare(S2, S2_comp)
    assert len(S3) == len(G) * 2
    assert all(S3.N_outgoing_pre == 2)
    assert all(S3.j[:] % 2 == 0)
    assert all(S3.j >= 2)
    assert all([len(S3.j[x, :]) == len(set(S3.j[x, :])) for x in range(len(G))])
    assert len(S4) == 3
    assert_equal(S4.i, np.ones(3) * 2)
    assert_equal(S4.j, np.arange(6, 0, -2))


@pytest.mark.standalone_compatible
def test_synapse_generator_fixed_random_error1():
    G = NeuronGroup(5, "")
    G2 = NeuronGroup(7, "")
    S = Synapses(G, G2)
    with pytest.raises((BrianObjectException, IndexError, RuntimeError)):
        # Won't work for i=4
        S.connect(j="k for k in sample(N_post, size=i+4)")
        run(0 * ms)  # for standalone


@pytest.mark.standalone_compatible
def test_synapse_generator_fixed_random_error2():
    G = NeuronGroup(5, "")
    G2 = NeuronGroup(7, "")
    S = Synapses(G, G2)
    with pytest.raises((BrianObjectException, IndexError, RuntimeError)):
        # Won't work for i=4
        S.connect(j="k for k in sample(N_post, size=3-i)")
        run(0 * ms)  # for standalone


@pytest.mark.standalone_compatible
def test_synapse_generator_fixed_random_skip_if_invalid():
    G = NeuronGroup(5, "")
    G2 = NeuronGroup(7, "")
    S1 = Synapses(G, G2)
    S2 = Synapses(G, G2)
    # > N_post for i=4
    S1.connect(j="k for k in sample(N_post, size=i+4)", skip_if_invalid=True)
    # < 0 for i=4
    S2.connect(j="k for k in sample(N_post, size=3-i)", skip_if_invalid=True)
    run(0 * ms)  # for standalone
    assert_array_equal(S1.N_outgoing_pre, [4, 5, 6, 7, 7])
    assert_array_equal(S2.N_outgoing_pre, [3, 2, 1, 0, 0])


@pytest.mark.standalone_compatible
def test_synapse_generator_random_with_condition():
    G = NeuronGroup(4, "")

    S1 = Synapses(G, G)
    S1.connect(j="k for k in sample(N_post, p=0) if i != k")

    S2 = Synapses(G, G)
    S2.connect(j="k for k in sample(N_post, p=1) if i != k")
    expected2 = np.ones((len(G), len(G))) - np.eye(len(G))

    S3 = Synapses(G, G)
    S3.connect(j="k for k in sample(N_post, p=0) if i >= 2")

    S4 = Synapses(G, G)
    S4.connect(j="k for k in sample(N_post, p=1.0) if i >= 2")
    expected4 = np.zeros((len(G), len(G)))
    expected4[2, :] = 1
    expected4[3, :] = 1

    S5 = Synapses(G, G)
    S5.connect(j="k for k in sample(N_post, p=0) if j < 2")  # inefficient

    S6 = Synapses(G, G)
    S6.connect(j="k for k in sample(2, p=0)")  # better

    S7 = Synapses(G, G)
    expected7 = np.zeros((len(G), len(G)))
    expected7[:, 0] = 1
    expected7[:, 1] = 1
    S7.connect(j="k for k in sample(N_post, p=1.0) if j < 2")  # inefficient

    S8 = Synapses(G, G)
    S8.connect(j="k for k in sample(2, p=1.0)")  # better

    with catch_logs() as _:  # Ignore warnings about empty synapses
        run(0 * ms)  # for standalone

    assert len(S1) == 0
    _compare(S2, expected2)
    assert len(S3) == 0
    _compare(S4, expected4)
    assert len(S5) == 0
    assert len(S6) == 0
    _compare(S7, expected7)
    _compare(S8, expected7)


@pytest.mark.standalone_compatible
@pytest.mark.long
def test_synapse_generator_random_with_condition_2():
    G = NeuronGroup(4, "")

    # Just checking that everything works in principle (we can't check the
    # actual connections)
    S9 = Synapses(G, G)
    S9.connect(j="k for k in sample(N_post, p=0.001) if i != k")

    S10 = Synapses(G, G)
    S10.connect(j="k for k in sample(N_post, p=0.03) if i != k")

    S11 = Synapses(G, G)
    S11.connect(j="k for k in sample(N_post, p=0.1) if i != k")

    S12 = Synapses(G, G)
    S12.connect(j="k for k in sample(N_post, p=0.9) if i != k")

    S13 = Synapses(G, G)
    S13.connect(j="k for k in sample(N_post, p=0.001) if i >= 2")

    S14 = Synapses(G, G)
    S14.connect(j="k for k in sample(N_post, p=0.03) if i >= 2")

    S15 = Synapses(G, G)
    S15.connect(j="k for k in sample(N_post, p=0.1) if i >= 2")

    S16 = Synapses(G, G)
    S16.connect(j="k for k in sample(N_post, p=0.9) if i >= 2")

    S17 = Synapses(G, G)
    S17.connect(j="k for k in sample(N_post, p=0.001) if j < 2")

    S18 = Synapses(G, G)
    S18.connect(j="k for k in sample(N_post, p=0.03) if j < 2")

    S19 = Synapses(G, G)
    S19.connect(j="k for k in sample(N_post, p=0.1) if j < 2")

    S20 = Synapses(G, G)
    S20.connect(j="k for k in sample(N_post, p=0.9) if j < 2")

    S21 = Synapses(G, G)
    S21.connect(j="k for k in sample(2, p=0.001)")

    S22 = Synapses(G, G)
    S22.connect(j="k for k in sample(2, p=0.03)")

    S23 = Synapses(G, G)
    S23.connect(j="k for k in sample(2, p=0.1)")

    S24 = Synapses(G, G)
    S24.connect(j="k for k in sample(2, p=0.9)")

    # Some more tests specific to the generator syntax
    S25 = Synapses(G, G)
    S25.connect(j="i+1 for _ in sample(1, p=0.5) if i < N_post-1")

    S26 = Synapses(G, G)
    S26.connect(j="i+k for k in sample(N_post-i, p=0.5)")

    with catch_logs() as _:  # Ignore warnings about empty synapses
        run(0 * ms)  # for standalone

    assert not any(S9.i == S9.j)
    assert 0 <= len(S9) <= len(G) * (len(G) - 1)
    assert not any(S10.i == S10.j)
    assert 0 <= len(S10) <= len(G) * (len(G) - 1)
    assert not any(S11.i == S11.j)
    assert 0 <= len(S11) <= len(G) * (len(G) - 1)
    assert not any(S12.i == S12.j)
    assert 0 <= len(S12) <= len(G) * (len(G) - 1)
    assert all(S13.i[:] >= 2)
    assert 0 <= len(S13) <= len(G) * (len(G) - 1)
    assert all(S14.i[:] >= 2)
    assert 0 <= len(S14) <= len(G) * (len(G) - 1)
    assert all(S15.i[:] >= 2)
    assert 0 <= len(S15) <= len(G) * (len(G) - 1)
    assert all(S16.i[:] >= 2)
    assert 0 <= len(S16) <= len(G) * (len(G) - 1)
    assert all(S17.j[:] < 2)
    assert 0 <= len(S17) <= len(G) * (len(G) - 1)
    assert all(S18.j[:] < 2)
    assert 0 <= len(S18) <= len(G) * (len(G) - 1)
    assert all(S19.j[:] < 2)
    assert 0 <= len(S19) <= len(G) * (len(G) - 1)
    assert all(S20.j[:] < 2)
    assert 0 <= len(S20) <= len(G) * (len(G) - 1)
    assert all(S21.j[:] < 2)
    assert 0 <= len(S21) <= len(G) * (len(G) - 1)
    assert all(S22.j[:] < 2)
    assert 0 <= len(S22) <= len(G) * (len(G) - 1)
    assert all(S23.j[:] < 2)
    assert 0 <= len(S23) <= len(G) * (len(G) - 1)
    assert all(S24.j[:] < 2)
    assert 0 <= len(S24) <= len(G) * (len(G) - 1)
    assert 0 <= len(S25) <= len(G)
    assert_equal(S25.j[:], S25.i[:] + 1)
    assert 0 <= len(S26) <= (1 + len(G)) * (len(G) / 2)
    assert all(S26.j[:] >= S26.i[:])


@pytest.mark.standalone_compatible
def test_synapses_refractory():
    source = NeuronGroup(10, "", threshold="True")
    target = NeuronGroup(
        10,
        "dv/dt = 0/second : 1 (unless refractory)",
        threshold="i>=5",
        refractory=defaultclock.dt,
    )
    S = Synapses(source, target, on_pre="v += 1")
    S.connect(j="i")
    run(defaultclock.dt + schedule_propagation_offset())
    assert_allclose(target.v[:5], 1)
    assert_allclose(target.v[5:], 0)


@pytest.mark.standalone_compatible
def test_synapses_refractory_rand():
    source = NeuronGroup(10, "", threshold="True")
    target = NeuronGroup(
        10,
        "dv/dt = 0/second : 1 (unless refractory)",
        threshold="i>=5",
        refractory=defaultclock.dt,
    )
    S = Synapses(source, target, on_pre="v += rand()")
    S.connect(j="i")
    with catch_logs() as _:
        # Currently, rand() is a stateful function (we do not make use of
        # _vectorisation_idx yet to make random numbers completely
        # reproducible), which will lead to a warning, since the result depends
        # on the order of execution.
        run(defaultclock.dt + schedule_propagation_offset())
    assert all(target.v[:5] > 0)
    assert_allclose(target.v[5:], 0)


@pytest.mark.codegen_independent
def test_synapse_generator_range_noint():
    # arguments to `range` should only be integers (issue #781)
    G = NeuronGroup(42, "")
    S = Synapses(G, G)
    msg = (
        r"The '{}' argument of the range function was .+, but it needs to be an"
        r" integer\."
    )
    with pytest.raises(TypeError, match=msg.format("high")):
        S.connect(j="k for k in range(42.0)")
    with pytest.raises(TypeError, match=msg.format("low")):
        S.connect(j="k for k in range(0.0, 42)")
    with pytest.raises(TypeError, match=msg.format("high")):
        S.connect(j="k for k in range(0, 42.0)")
    with pytest.raises(TypeError, match=msg.format("step")):
        S.connect(j="k for k in range(0, 42, 1.0)")
    with pytest.raises(TypeError, match=msg.format("low")):
        S.connect(j="k for k in range(True, 42)")
    with pytest.raises(TypeError, match=msg.format("high")):
        S.connect(j="k for k in range(0, True)")
    with pytest.raises(TypeError, match=msg.format("step")):
        S.connect(j="k for k in range(0, 42, True)")


@pytest.mark.codegen_independent
def test_missing_lastupdate_error_syn_pathway():
    G = NeuronGroup(1, "v : 1", threshold="False")
    S = Synapses(G, G, on_pre="v += exp(-lastupdate/dt)")
    S.connect()
    with pytest.raises(BrianObjectException) as exc:
        run(0 * ms)
    assert exc_isinstance(exc, KeyError)
    assert "lastupdate = t" in str(exc.value.__cause__)
    assert "lastupdate : second" in str(exc.value.__cause__)


@pytest.mark.codegen_independent
def test_missing_lastupdate_error_run_regularly():
    G = NeuronGroup(1, "v : 1")
    S = Synapses(G, G)
    S.connect()
    S.run_regularly("v += exp(-lastupdate/dt")
    with pytest.raises(BrianObjectException) as exc:
        run(0 * ms)
    assert exc_isinstance(exc, KeyError)
    assert "lastupdate = t" in str(exc.value.__cause__)
    assert "lastupdate : second" in str(exc.value.__cause__)


@pytest.mark.codegen_independent
def test_synaptic_subgroups():
    source = NeuronGroup(5, "")
    target = NeuronGroup(3, "")
    syn = Synapses(source, target)
    syn.connect()
    assert len(syn) == 15

    from_3 = syn[3, :]
    assert len(from_3) == 3
    assert all(syn.i[from_3] == 3)
    assert_array_equal(syn.j[from_3], np.arange(3))

    to_2 = syn[:, 2]
    assert len(to_2) == 5
    assert all(syn.j[to_2] == 2)
    assert_array_equal(syn.i[to_2], np.arange(5))

    mixed = syn[1:3, :2]
    assert len(mixed) == 4
    connections = {(i, j) for i, j in zip(syn.i[mixed], syn.j[mixed])}
    assert connections == {(1, 0), (1, 1), (2, 0), (2, 1)}


@pytest.mark.codegen_independent
def test_incorrect_connect_N_incoming_outgoing():
    # See github issue #1227
    source = NeuronGroup(5, "")
    target = NeuronGroup(3, "")
    syn = Synapses(source, target)

    with pytest.raises(ValueError) as ex:
        syn.connect("N_incoming < 5")
        assert "N_incoming" in str(ex)

    with pytest.raises(ValueError) as ex:
        syn.connect("N_outgoing < 5")
        assert "N_outgoing" in str(ex)


@pytest.mark.codegen_independent
def test_setting_from_weight_matrix():
    # fully connected weight matrix
    # weights[source_index, target_index]
    weights = np.array([[1, 2, 3], [4, 5, 6]])

    source = NeuronGroup(2, "")
    target = NeuronGroup(3, "")

    syn = Synapses(source, target, "w : 1")
    syn.connect()
    syn.w[:] = weights.flatten()

    for (i, j), w in np.ndenumerate(weights):
        assert all(syn.w[i, j] == weights[i, j])


if __name__ == "__main__":
    SANITY_CHECK_PERMUTATION_ANALYSIS_EXAMPLE = True
    # prefs.codegen.target = 'numpy'
    # prefs._backup()
    import time

    from _pytest.outcomes import Skipped

    from brian2 import prefs

    start = time.time()

    test_creation()
    test_name_clashes()
    test_incoming_outgoing()
    test_connection_string_deterministic_full()
    test_connection_string_deterministic_full_no_self()
    test_connection_string_deterministic_full_one_to_one()
    test_connection_string_deterministic_full_custom()
    test_connection_string_deterministic_multiple_and()
    test_connection_random_with_condition()
    test_connection_random_with_condition_2()
    test_connection_random_without_condition()
    test_connection_random_with_indices()
    test_connection_multiple_synapses()
    test_connection_arrays()
    reinit_and_delete()
    test_state_variable_assignment()
    test_state_variable_indexing()
    test_indices()
    test_subexpression_references()
    test_nested_subexpression_references()
    test_constant_variable_subexpression_in_synapses()
    test_equations_unit_check()
    test_delay_specification()
    test_delays_pathways()
    test_delays_pathways_subgroups()
    test_pre_before_post()
    test_pre_post_simple()
    test_transmission_simple()
    test_transmission_custom_event()
    test_invalid_custom_event()
    test_transmission()
    test_transmission_all_to_one_heterogeneous_delays()
    test_transmission_one_to_all_heterogeneous_delays()
    test_transmission_scalar_delay()
    test_transmission_scalar_delay_different_clocks()
    test_transmission_boolean_variable()
    test_clocks()
    test_changed_dt_spikes_in_queue()
    test_no_synapses()
    test_no_synapses_variable_write()
    test_summed_variable()
    test_summed_variable_pre_and_post()
    test_summed_variable_differing_group_size()
    test_summed_variable_errors()
    test_multiple_summed_variables()
    test_summed_variables_subgroups()
    test_summed_variables_overlapping_subgroups()
    test_summed_variables_linked_variables()
    test_scalar_parameter_access()
    test_scalar_subexpression()
    test_sim_with_scalar_variable()
    test_sim_with_scalar_subexpression()
    test_sim_with_constant_subexpression()
    test_external_variables()
    test_event_driven()
    test_event_driven_dependency_error()
    test_event_driven_dependency_error2()
    test_event_driven_dependency_error3()
    test_repr()
    test_pre_post_variables()
    test_variables_by_owner()
    test_permutation_analysis()
    test_vectorisation()
    test_vectorisation_STDP_like()
    test_synaptic_equations()
    test_synapse_with_run_regularly()
    test_synapses_to_synapses()
    test_synapses_to_synapses_statevar_access()
    test_synapses_to_synapses_different_sizes()
    test_synapses_to_synapses_summed_variable()
    try:
        test_ufunc_at_vectorisation()
        test_fallback_loop_and_stateless_func()
    except Skipped:
        print("Skipping numpy-only test")
    test_synapse_generator_syntax()
    test_synapse_generator_out_of_range()
    test_synapse_generator_deterministic()
    test_synapse_generator_deterministic_2()
    test_synapse_generator_random()
    test_synapse_generator_random_with_condition()
    test_synapse_generator_random_with_condition_2()
    test_synapses_refractory()
    test_synapses_refractory_rand()
    test_synapse_generator_range_noint()
    test_missing_lastupdate_error_syn_pathway()
    test_missing_lastupdate_error_run_regularly()
    test_synaptic_subgroups()
    test_incorrect_connect_N_incoming_outgoing()
    test_setting_from_weight_matrix()
    print("Tests took", time.time() - start)