[074d3d]: / mne / viz / _brain / _brain.py

Download this file

4146 lines (3786 with data), 154.3 kB

   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
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
# Authors: The MNE-Python contributors.
# License: BSD-3-Clause
# Copyright the MNE-Python contributors.
import copy
import os
import os.path as op
import time
import traceback
import warnings
from functools import partial
from io import BytesIO
import numpy as np
from scipy.interpolate import interp1d
from scipy.sparse import csr_array
from scipy.spatial.distance import cdist
from ..._fiff.meas_info import Info
from ..._fiff.pick import pick_types
from ..._freesurfer import (
_estimate_talxfm_rigid,
_get_aseg,
_get_head_surface,
_get_skull_surface,
read_freesurfer_lut,
read_talxfm,
vertex_to_mni,
)
from ...defaults import DEFAULTS, _handle_default
from ...surface import _marching_cubes, _mesh_borders, mesh_edges
from ...transforms import (
Transform,
_frame_to_str,
_get_trans,
_get_transforms_to_coord_frame,
apply_trans,
)
from ...utils import (
Bunch,
_auto_weakref,
_check_fname,
_check_option,
_ensure_int,
_path_like,
_ReuseCycle,
_to_rgb,
_validate_type,
fill_doc,
get_subjects_dir,
logger,
use_log_level,
verbose,
warn,
)
from .._3d import (
_check_views,
_handle_sensor_types,
_handle_time,
_plot_forward,
_plot_helmet,
_plot_sensors_3d,
_process_clim,
)
from .._3d_overlay import _LayeredMesh
from ..ui_events import (
ColormapRange,
PlaybackSpeed,
TimeChange,
VertexSelect,
_get_event_channel,
disable_ui_events,
publish,
subscribe,
unsubscribe,
)
from ..utils import (
_generate_default_filename,
_get_color_list,
_save_ndarray_img,
_show_help_fig,
concatenate_images,
safe_event,
)
from .colormap import calculate_lut
from .surface import _Surface
from .view import _lh_views_dict, views_dicts
@fill_doc
class Brain:
"""Class for visualizing a brain.
.. warning::
The API for this class is not currently complete. We suggest using
:meth:`mne.viz.plot_source_estimates` with the PyVista backend
enabled to obtain a ``Brain`` instance.
Parameters
----------
subject : str
Subject name in Freesurfer subjects dir.
.. versionchanged:: 1.2
This parameter was renamed from ``subject_id`` to ``subject``.
hemi : str
Hemisphere id (ie 'lh', 'rh', 'both', or 'split'). In the case
of 'both', both hemispheres are shown in the same window.
In the case of 'split' hemispheres are displayed side-by-side
in different viewing panes.
surf : str
FreeSurfer surface mesh name (ie 'white', 'inflated', etc.).
title : str
Title for the window.
cortex : str, list, dict
Specifies how the cortical surface is rendered. Options:
1. The name of one of the preset cortex styles:
``'classic'`` (default), ``'high_contrast'``,
``'low_contrast'``, or ``'bone'``.
2. A single color-like argument to render the cortex as a single
color, e.g. ``'red'`` or ``(0.1, 0.4, 1.)``.
3. A list of two color-like used to render binarized curvature
values for gyral (first) and sulcal (second). regions, e.g.,
``['red', 'blue']`` or ``[(1, 0, 0), (0, 0, 1)]``.
4. A dict containing keys ``'vmin', 'vmax', 'colormap'`` with
values used to render the binarized curvature (where 0 is gyral,
1 is sulcal).
.. versionchanged:: 0.24
Add support for non-string arguments.
alpha : float in [0, 1]
Alpha level to control opacity of the cortical surface.
size : int | array-like, shape (2,)
The size of the window, in pixels. can be one number to specify
a square window, or a length-2 sequence to specify (width, height).
background : tuple(int, int, int)
The color definition of the background: (red, green, blue).
foreground : matplotlib color
Color of the foreground (will be used for colorbars and text).
None (default) will use black or white depending on the value
of ``background``.
figure : list of Figure | None
If None (default), a new window will be created with the appropriate
views.
subjects_dir : str | None
If not None, this directory will be used as the subjects directory
instead of the value set using the SUBJECTS_DIR environment
variable.
%(views)s
offset : bool | str
If True, shifts the right- or left-most x coordinate of the left and
right surfaces, respectively, to be at zero. This is useful for viewing
inflated surface where hemispheres typically overlap. Can be "auto"
(default) use True with inflated surfaces and False otherwise
(Default: 'auto'). Only used when ``hemi='both'``.
.. versionchanged:: 0.23
Default changed to "auto".
interaction : str
Can be "trackball" (default) or "terrain", i.e. a turntable-style
camera.
units : str
Can be 'm' or 'mm' (default).
%(view_layout)s
silhouette : dict | bool
As a dict, it contains the ``color``, ``linewidth``, ``alpha`` opacity
and ``decimate`` (level of decimation between 0 and 1 or None) of the
brain's silhouette to display. If True, the default values are used
and if False, no silhouette will be displayed. Defaults to False.
%(theme_3d)s
show : bool
Display the window as soon as it is ready. Defaults to True.
Attributes
----------
geo : dict
A dictionary of PyVista surface objects for each hemisphere.
overlays : dict
The overlays.
Notes
-----
The figure will publish and subscribe to the following UI events:
* :class:`~mne.viz.ui_events.TimeChange`
* :class:`~mne.viz.ui_events.PlaybackSpeed`
* :class:`~mne.viz.ui_events.ColormapRange`, ``kind="distributed_source_power"``
* :class:`~mne.viz.ui_events.VertexSelect`
This table shows the capabilities of each Brain backend ("✓" for full
support, and "-" for partial support):
.. table::
:widths: auto
+-------------------------------------+--------------+---------------+
| 3D function: | surfer.Brain | mne.viz.Brain |
+=====================================+==============+===============+
| :meth:`add_annotation` | ✓ | ✓ |
+-------------------------------------+--------------+---------------+
| :meth:`add_data` | ✓ | ✓ |
+-------------------------------------+--------------+---------------+
| :meth:`add_dipole` | | ✓ |
+-------------------------------------+--------------+---------------+
| :meth:`add_foci` | ✓ | ✓ |
+-------------------------------------+--------------+---------------+
| :meth:`add_forward` | | ✓ |
+-------------------------------------+--------------+---------------+
| :meth:`add_head` | | ✓ |
+-------------------------------------+--------------+---------------+
| :meth:`add_label` | ✓ | ✓ |
+-------------------------------------+--------------+---------------+
| :meth:`add_sensors` | | ✓ |
+-------------------------------------+--------------+---------------+
| :meth:`add_skull` | | ✓ |
+-------------------------------------+--------------+---------------+
| :meth:`add_text` | ✓ | ✓ |
+-------------------------------------+--------------+---------------+
| :meth:`add_volume_labels` | | ✓ |
+-------------------------------------+--------------+---------------+
| :meth:`close` | ✓ | ✓ |
+-------------------------------------+--------------+---------------+
| data | ✓ | ✓ |
+-------------------------------------+--------------+---------------+
| foci | ✓ | |
+-------------------------------------+--------------+---------------+
| labels | ✓ | ✓ |
+-------------------------------------+--------------+---------------+
| :meth:`remove_data` | | ✓ |
+-------------------------------------+--------------+---------------+
| :meth:`remove_dipole` | | ✓ |
+-------------------------------------+--------------+---------------+
| :meth:`remove_forward` | | ✓ |
+-------------------------------------+--------------+---------------+
| :meth:`remove_head` | | ✓ |
+-------------------------------------+--------------+---------------+
| :meth:`remove_labels` | ✓ | ✓ |
+-------------------------------------+--------------+---------------+
| :meth:`remove_annotations` | - | ✓ |
+-------------------------------------+--------------+---------------+
| :meth:`remove_sensors` | | ✓ |
+-------------------------------------+--------------+---------------+
| :meth:`remove_skull` | | ✓ |
+-------------------------------------+--------------+---------------+
| :meth:`remove_text` | | ✓ |
+-------------------------------------+--------------+---------------+
| :meth:`remove_volume_labels` | | ✓ |
+-------------------------------------+--------------+---------------+
| :meth:`save_image` | ✓ | ✓ |
+-------------------------------------+--------------+---------------+
| :meth:`save_movie` | ✓ | ✓ |
+-------------------------------------+--------------+---------------+
| :meth:`screenshot` | ✓ | ✓ |
+-------------------------------------+--------------+---------------+
| :meth:`show_view` | ✓ | ✓ |
+-------------------------------------+--------------+---------------+
| TimeViewer | ✓ | ✓ |
+-------------------------------------+--------------+---------------+
| :meth:`get_picked_points` | | ✓ |
+-------------------------------------+--------------+---------------+
| :meth:`add_data(volume) <add_data>` | | ✓ |
+-------------------------------------+--------------+---------------+
| view_layout | | ✓ |
+-------------------------------------+--------------+---------------+
| flatmaps | | ✓ |
+-------------------------------------+--------------+---------------+
| vertex picking | | ✓ |
+-------------------------------------+--------------+---------------+
| label picking | | ✓ |
+-------------------------------------+--------------+---------------+
"""
def __init__(
self,
subject,
hemi="both",
surf="pial",
title=None,
cortex="classic",
alpha=1.0,
size=800,
background="black",
foreground=None,
figure=None,
subjects_dir=None,
views="auto",
*,
offset="auto",
interaction="trackball",
units="mm",
view_layout="vertical",
silhouette=False,
theme=None,
show=True,
):
from ..backends.renderer import _get_renderer, backend
_validate_type(subject, str, "subject")
self._surf = surf
if hemi is None:
hemi = "vol"
hemi = self._check_hemi(hemi, extras=("both", "split", "vol"))
if hemi in ("both", "split"):
self._hemis = ("lh", "rh")
else:
assert hemi in ("lh", "rh", "vol")
self._hemis = (hemi,)
self._view_layout = _check_option(
"view_layout", view_layout, ("vertical", "horizontal")
)
if figure is not None and not isinstance(figure, int):
backend._check_3d_figure(figure)
if title is None:
self._title = subject
else:
self._title = title
self._interaction = "trackball"
self._bg_color = _to_rgb(background, name="background")
if foreground is None:
foreground = "w" if sum(self._bg_color) < 2 else "k"
self._fg_color = _to_rgb(foreground, name="foreground")
del background, foreground
views = _check_views(surf, views, hemi)
col_dict = dict(lh=1, rh=1, both=1, split=2, vol=1)
shape = (len(views), col_dict[hemi])
if self._view_layout == "horizontal":
shape = shape[::-1]
self._subplot_shape = shape
size = tuple(np.atleast_1d(size).round(0).astype(int).flat)
if len(size) not in (1, 2):
raise ValueError(
'"size" parameter must be an int or length-2 sequence of ints.'
)
size = size if len(size) == 2 else size * 2 # 1-tuple to 2-tuple
subjects_dir = get_subjects_dir(subjects_dir)
if subjects_dir is not None:
subjects_dir = str(subjects_dir)
self.time_viewer = False
self._hash = time.time_ns()
self._hemi = hemi
self._units = units
self._alpha = float(alpha)
self._subject = subject
self._subjects_dir = subjects_dir
self._views = views
self._times = None
self._vertex_to_label_id = dict()
self._annotation_labels = dict()
self._labels = {"lh": list(), "rh": list()}
self._unnamed_label_id = 0 # can only grow
self._annots = {"lh": list(), "rh": list()}
self._layered_meshes = dict()
self._actors = dict()
self._cleaned = False
# default values for silhouette
self._silhouette = {
"color": self._bg_color,
"line_width": 2,
"alpha": alpha,
"decimate": 0.9,
}
_validate_type(silhouette, (dict, bool), "silhouette")
if isinstance(silhouette, dict):
self._silhouette.update(silhouette)
self.silhouette = True
else:
self.silhouette = silhouette
self._scalar_bar = None
# for now only one time label can be added
# since it is the same for all figures
self._time_label_added = False
# array of data used by TimeViewer
self._data = {}
self.geo = {}
self.set_time_interpolation("nearest")
geo_kwargs = self._cortex_colormap(cortex)
# evaluate at the midpoint of the used colormap
val = -geo_kwargs["vmin"] / (geo_kwargs["vmax"] - geo_kwargs["vmin"])
self._brain_color = geo_kwargs["colormap"](val)
# load geometry for one or both hemispheres as necessary
_validate_type(offset, (str, bool), "offset")
if isinstance(offset, str):
_check_option("offset", offset, ("auto",), extra="when str")
offset = surf in ("inflated", "flat")
offset = None if (not offset or hemi != "both") else 0.0
logger.debug(f"Hemi offset: {offset}")
_validate_type(theme, (str, None), "theme")
self._renderer = _get_renderer(
name=self._title, size=size, bgcolor=self._bg_color, shape=shape, fig=figure
)
self._renderer._window_close_connect(self._clean)
self._renderer._window_set_theme(theme)
self.plotter = self._renderer.plotter
self.widgets = dict()
self._setup_canonical_rotation()
# plot hemis
for h in ("lh", "rh"):
if h not in self._hemis:
continue # don't make surface if not chosen
# Initialize a Surface object as the geometry
geo = _Surface(
self._subject,
h,
surf,
self._subjects_dir,
offset,
units=self._units,
x_dir=self._rigid[0, :3],
)
# Load in the geometry and curvature
geo.load_geometry()
geo.load_curvature()
self.geo[h] = geo
for _, _, v in self._iter_views(h):
if self._layered_meshes.get(h) is None:
mesh = _LayeredMesh(
renderer=self._renderer,
vertices=self.geo[h].coords,
triangles=self.geo[h].faces,
normals=self.geo[h].nn,
)
mesh.map() # send to GPU
if self.geo[h].bin_curv is None:
scalars = mesh._default_scalars[:, 0]
else:
scalars = self.geo[h].bin_curv
mesh.add_overlay(
scalars=scalars,
colormap=geo_kwargs["colormap"],
rng=[geo_kwargs["vmin"], geo_kwargs["vmax"]],
opacity=alpha,
name="curv",
)
self._layered_meshes[h] = mesh
# add metadata to the mesh for picking
mesh._polydata._hemi = h
else:
actor = self._layered_meshes[h]._actor
self._renderer.plotter.add_actor(actor, render=False)
if self.silhouette:
mesh = self._layered_meshes[h]
self._renderer._silhouette(
mesh=mesh._polydata,
color=self._silhouette["color"],
line_width=self._silhouette["line_width"],
alpha=self._silhouette["alpha"],
decimate=self._silhouette["decimate"],
)
self._set_camera(**views_dicts[h][v])
self.interaction = interaction
self._closed = False
if show:
self.show()
# update the views once the geometry is all set
for h in self._hemis:
for ri, ci, v in self._iter_views(h):
self.show_view(v, row=ri, col=ci, hemi=h, update=False)
if surf == "flat":
self._renderer.set_interaction("rubber_band_2d")
self._renderer._update()
def _setup_canonical_rotation(self):
self._rigid = np.eye(4)
try:
xfm = _estimate_talxfm_rigid(self._subject, self._subjects_dir)
except Exception:
logger.info(
"Could not estimate rigid Talairach alignment, using identity matrix"
)
else:
self._rigid[:] = xfm
def setup_time_viewer(self, time_viewer=True, show_traces=True):
"""Configure the time viewer parameters.
Parameters
----------
time_viewer : bool
If True, enable widgets interaction. Defaults to True.
show_traces : bool
If True, enable visualization of time traces. Defaults to True.
Notes
-----
The keyboard shortcuts are the following:
'?': Display help window
'i': Toggle interface
's': Apply auto-scaling
'r': Restore original clim
'c': Clear all traces
'n': Shift the time forward by the playback speed
'b': Shift the time backward by the playback speed
'Space': Start/Pause playback
'Up': Decrease camera elevation angle
'Down': Increase camera elevation angle
'Left': Decrease camera azimuth angle
'Right': Increase camera azimuth angle
"""
if self.time_viewer:
return
if not self._data:
raise ValueError("No data to visualize. See ``add_data``.")
self.time_viewer = time_viewer
self.orientation = list(_lh_views_dict.keys())
self.default_smoothing_range = [-1, 15]
# Default configuration
self.visibility = False
self.default_playback_speed_range = [0.01, 1]
self.default_playback_speed_value = 0.01
self.default_status_bar_msg = "Press ? for help"
self.default_label_extract_modes = {
"stc": ["mean", "max"],
"src": ["mean_flip", "pca_flip", "auto"],
}
self.annot = None
self.label_extract_mode = None
all_keys = ("lh", "rh", "vol")
self.act_data_smooth = {key: (None, None) for key in all_keys}
# remove grey for better contrast on the brain
self.color_list = _get_color_list(remove=("#7f7f7f",))
self.color_cycle = _ReuseCycle(self.color_list)
self.mpl_canvas = None
self.help_canvas = None
self.rms = None
self.picked_patches = {key: list() for key in all_keys}
self.picked_points = {key: list() for key in all_keys}
self.pick_table = dict()
self._spheres = list()
self._mouse_no_mvt = -1
# Derived parameters:
self.playback_speed = self.default_playback_speed_value
_validate_type(show_traces, (bool, str, "numeric"), "show_traces")
self.interactor_fraction = 0.25
if isinstance(show_traces, str):
self.show_traces = True
self.separate_canvas = False
self.traces_mode = "vertex"
if show_traces == "separate":
self.separate_canvas = True
elif show_traces == "label":
self.traces_mode = "label"
else:
assert show_traces == "vertex" # guaranteed above
else:
if isinstance(show_traces, bool):
self.show_traces = show_traces
else:
show_traces = float(show_traces)
if not 0 < show_traces < 1:
raise ValueError(
"show traces, if numeric, must be between 0 and 1, "
f"got {show_traces}"
)
self.show_traces = True
self.interactor_fraction = show_traces
self.traces_mode = "vertex"
self.separate_canvas = False
del show_traces
self._configure_time_label()
self._configure_scalar_bar()
self._configure_shortcuts()
self._configure_picking()
self._configure_dock()
self._configure_tool_bar()
self._configure_menu()
self._configure_status_bar()
self._configure_help()
# show everything at the end
self.toggle_interface()
self._renderer.show()
# sizes could change, update views
for hemi in ("lh", "rh"):
for ri, ci, v in self._iter_views(hemi):
self.show_view(view=v, row=ri, col=ci)
self._renderer._process_events()
self._renderer._update()
# finally, show the MplCanvas
if self.show_traces:
self.mpl_canvas.show()
@safe_event
def _clean(self):
# resolve the reference cycle
self._renderer._window_close_disconnect()
self.clear_glyphs()
self.remove_annotations()
# clear init actors
for hemi in self._layered_meshes:
self._layered_meshes[hemi]._clean()
self._clear_callbacks()
self._clear_widgets()
if getattr(self, "mpl_canvas", None) is not None:
self.mpl_canvas.clear()
if getattr(self, "act_data_smooth", None) is not None:
for key in list(self.act_data_smooth.keys()):
self.act_data_smooth[key] = None
# XXX this should be done in PyVista
for renderer in self._renderer._all_renderers:
renderer.RemoveAllLights()
# app_window cannot be set to None because it is used in __del__
for key in ("lighting", "interactor", "_RenderWindow"):
setattr(self.plotter, key, None)
# Qt LeaveEvent requires _Iren so we use _FakeIren instead of None
# to resolve the ref to vtkGenericRenderWindowInteractor
self.plotter._Iren = _FakeIren()
if getattr(self.plotter, "picker", None) is not None:
self.plotter.picker = None
# XXX end PyVista
for key in (
"plotter",
"window",
"dock",
"tool_bar",
"menu_bar",
"interactor",
"mpl_canvas",
"time_actor",
"picked_renderer",
"act_data_smooth",
"_scalar_bar",
"actions",
"widgets",
"geo",
"_data",
):
setattr(self, key, None)
self._cleaned = True
def toggle_interface(self, value=None):
"""Toggle the interface.
Parameters
----------
value : bool | None
If True, the widgets are shown and if False, they
are hidden. If None, the state of the widgets is
toggled. Defaults to None.
"""
if value is None:
self.visibility = not self.visibility
else:
self.visibility = value
# update tool bar and dock
with self._renderer._window_ensure_minimum_sizes():
if self.visibility:
self._renderer._dock_show()
self._renderer._tool_bar_update_button_icon(
name="visibility", icon_name="visibility_on"
)
else:
self._renderer._dock_hide()
self._renderer._tool_bar_update_button_icon(
name="visibility", icon_name="visibility_off"
)
self._renderer._update()
def apply_auto_scaling(self):
"""Detect automatically fitting scaling parameters."""
self._update_auto_scaling()
def restore_user_scaling(self):
"""Restore original scaling parameters."""
self._update_auto_scaling(restore=True)
def toggle_playback(self, value=None):
"""Toggle time playback.
Parameters
----------
value : bool | None
If True, automatic time playback is enabled and if False,
it's disabled. If None, the state of time playback is toggled.
Defaults to None.
"""
self._renderer._toggle_playback(value)
def reset(self):
"""Reset view, current time and time step."""
self.reset_view()
self._renderer._reset_time()
def set_playback_speed(self, speed):
"""Set the time playback speed.
Parameters
----------
speed : float
The speed of the playback.
"""
publish(self, PlaybackSpeed(speed=speed))
def _configure_time_label(self):
self.time_actor = self._data.get("time_actor")
if self.time_actor is not None:
self.time_actor.SetPosition(0.5, 0.03)
self.time_actor.GetTextProperty().SetJustificationToCentered()
self.time_actor.GetTextProperty().BoldOn()
def _configure_scalar_bar(self):
if self._scalar_bar is not None:
self._scalar_bar.SetOrientationToVertical()
self._scalar_bar.SetHeight(0.6)
self._scalar_bar.SetWidth(0.05)
self._scalar_bar.SetPosition(0.02, 0.2)
def _configure_dock_playback_widget(self, name):
len_time = len(self._data["time"]) - 1
# Time widget
if len_time < 1:
self.widgets["time"] = None
self.widgets["min_time"] = None
self.widgets["max_time"] = None
self.widgets["current_time"] = None
else:
@_auto_weakref
def current_time_func():
return self._current_time
self._renderer._enable_time_interaction(
self,
current_time_func,
self._data["time"],
self.default_playback_speed_value,
self.default_playback_speed_range,
)
# Time label
current_time = self._current_time
assert current_time is not None # should never be the case, float
time_label = self._data["time_label"]
if callable(time_label):
current_time = time_label(current_time)
else:
current_time = time_label
if self.time_actor is not None:
self.time_actor.SetInput(current_time)
del current_time
def _configure_dock_orientation_widget(self, name):
layout = self._renderer._dock_add_group_box(name)
# Renderer widget
rends = [str(i) for i in range(len(self._renderer._all_renderers))]
if len(rends) > 1:
@_auto_weakref
def select_renderer(idx):
idx = int(idx)
loc = self._renderer._index_to_loc(idx)
self.plotter.subplot(*loc)
self.widgets["renderer"] = self._renderer._dock_add_combo_box(
name="Renderer",
value="0",
rng=rends,
callback=select_renderer,
layout=layout,
)
# Use 'lh' as a reference for orientation for 'both'
if self._hemi == "both":
hemis_ref = ["lh"]
else:
hemis_ref = self._hemis
orientation_data = [None] * len(rends)
for hemi in hemis_ref:
for ri, ci, v in self._iter_views(hemi):
idx = self._renderer._loc_to_index((ri, ci))
if v == "flat":
_data = None
else:
_data = dict(default=v, hemi=hemi, row=ri, col=ci)
orientation_data[idx] = _data
@_auto_weakref
def set_orientation(value, orientation_data=orientation_data):
if "renderer" in self.widgets:
idx = int(self.widgets["renderer"].get_value())
else:
idx = 0
if orientation_data[idx] is not None:
self.show_view(
value,
row=orientation_data[idx]["row"],
col=orientation_data[idx]["col"],
hemi=orientation_data[idx]["hemi"],
)
self.widgets["orientation"] = self._renderer._dock_add_combo_box(
name=None,
value=self.orientation[0],
rng=self.orientation,
callback=set_orientation,
layout=layout,
)
def _configure_dock_colormap_widget(self, name):
fmax, fscale, fscale_power = _get_range(self)
rng = [0, fmax * fscale]
self._data["fscale"] = fscale
layout = self._renderer._dock_add_group_box(name)
text = "min / mid / max"
if fscale_power != 0:
text += f" (×1e{fscale_power:d})"
self._renderer._dock_add_label(
value=text,
align=True,
layout=layout,
)
@_auto_weakref
def update_single_lut_value(value, key):
# Called by the sliders and spin boxes.
self.update_lut(**{key: value / self._data["fscale"]})
keys = ("fmin", "fmid", "fmax")
for key in keys:
hlayout = self._renderer._dock_add_layout(vertical=False)
self.widgets[key] = self._renderer._dock_add_slider(
name=None,
value=self._data[key] * self._data["fscale"],
rng=rng,
callback=partial(update_single_lut_value, key=key),
double=True,
layout=hlayout,
)
self.widgets[f"entry_{key}"] = self._renderer._dock_add_spin_box(
name=None,
value=self._data[key] * self._data["fscale"],
callback=partial(update_single_lut_value, key=key),
rng=rng,
layout=hlayout,
)
self._renderer._layout_add_widget(layout, hlayout)
# reset / minus / plus
hlayout = self._renderer._dock_add_layout(vertical=False)
self._renderer._dock_add_label(
value="Rescale",
align=True,
layout=hlayout,
)
self.widgets["reset"] = self._renderer._dock_add_button(
name="↺",
callback=self.restore_user_scaling,
layout=hlayout,
style="toolbutton",
)
@_auto_weakref
def fminus():
self._update_fscale(1.2**-0.25)
self.widgets["fminus"] = self._renderer._dock_add_button(
name="➖",
callback=fminus,
layout=hlayout,
style="toolbutton",
)
@_auto_weakref
def fplus():
self._update_fscale(1.2**0.25)
self.widgets["fplus"] = self._renderer._dock_add_button(
name="➕",
callback=fplus,
layout=hlayout,
style="toolbutton",
)
self._renderer._layout_add_widget(layout, hlayout)
def _configure_dock_trace_widget(self, name):
if not self.show_traces:
return
# do not show trace mode for volumes
if (
self._data.get("src", None) is not None
and self._data["src"].kind == "volume"
):
self._configure_vertex_time_course()
return
layout = self._renderer._dock_add_group_box(name)
# setup candidate annots
@_auto_weakref
def _set_annot(annot):
self.clear_glyphs()
self.remove_labels()
self.remove_annotations()
self.annot = annot
if annot == "None":
self.traces_mode = "vertex"
self._configure_vertex_time_course()
else:
self.traces_mode = "label"
self._configure_label_time_course()
self._renderer._update()
# setup label extraction parameters
@_auto_weakref
def _set_label_mode(mode):
if self.traces_mode != "label":
return
glyphs = copy.deepcopy(self.picked_patches)
self.label_extract_mode = mode
self.clear_glyphs()
for hemi in self._hemis:
for label_id in glyphs[hemi]:
label = self._annotation_labels[hemi][label_id]
vertex_id = label.vertices[0]
self._add_label_glyph(hemi, None, vertex_id)
self.mpl_canvas.axes.relim()
self.mpl_canvas.axes.autoscale_view()
self.mpl_canvas.update_plot()
self._renderer._update()
from ...label import _read_annot_cands
from ...source_estimate import _get_allowed_label_modes
dir_name = op.join(self._subjects_dir, self._subject, "label")
cands = _read_annot_cands(dir_name, raise_error=False)
cands = cands + ["None"]
self.annot = cands[0]
stc = self._data["stc"]
modes = _get_allowed_label_modes(stc)
if self._data["src"] is None:
modes = [
m for m in modes if m not in self.default_label_extract_modes["src"]
]
self.label_extract_mode = modes[-1]
if self.traces_mode == "vertex":
_set_annot("None")
else:
_set_annot(self.annot)
self.widgets["annotation"] = self._renderer._dock_add_combo_box(
name="Annotation",
value=self.annot,
rng=cands,
callback=_set_annot,
layout=layout,
)
self.widgets["extract_mode"] = self._renderer._dock_add_combo_box(
name="Extract mode",
value=self.label_extract_mode,
rng=modes,
callback=_set_label_mode,
layout=layout,
)
def _configure_dock(self):
self._renderer._dock_initialize()
self._configure_dock_playback_widget(name="Playback")
self._configure_dock_orientation_widget(name="Orientation")
self._configure_dock_colormap_widget(name="Color Limits")
self._configure_dock_trace_widget(name="Trace")
# Smoothing widget
self.widgets["smoothing"] = self._renderer._dock_add_spin_box(
name="Smoothing",
value=self._data["smoothing_steps"],
rng=self.default_smoothing_range,
callback=self.set_data_smoothing,
double=False,
)
self._renderer._dock_finalize()
def _configure_mplcanvas(self):
# Get the fractional components for the brain and mpl
self.mpl_canvas = self._renderer._window_get_mplcanvas(
brain=self,
interactor_fraction=self.interactor_fraction,
show_traces=self.show_traces,
separate_canvas=self.separate_canvas,
)
xlim = [np.min(self._data["time"]), np.max(self._data["time"])]
with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=UserWarning)
self.mpl_canvas.axes.set(xlim=xlim)
if not self.separate_canvas:
self._renderer._window_adjust_mplcanvas_layout()
self.mpl_canvas.set_color(
bg_color=self._bg_color,
fg_color=self._fg_color,
)
def _configure_vertex_time_course(self):
if not self.show_traces:
return
if self.mpl_canvas is None:
self._configure_mplcanvas()
else:
self.clear_glyphs()
# plot RMS of the activation
y = np.concatenate(
list(v[0] for v in self.act_data_smooth.values() if v[0] is not None)
)
rms = np.linalg.norm(y, axis=0) / np.sqrt(len(y))
del y
(self.rms,) = self.mpl_canvas.axes.plot(
self._data["time"],
rms,
lw=3,
label="RMS",
zorder=3,
color=self._fg_color,
alpha=0.5,
ls=":",
)
# now plot the time line
self.plot_time_line(update=False)
# then the picked points
for idx, hemi in enumerate(["lh", "rh", "vol"]):
act_data = self.act_data_smooth.get(hemi, [None])[0]
if act_data is None:
continue
hemi_data = self._data[hemi]
vertices = hemi_data["vertices"]
# simulate a picked renderer
if self._hemi in ("both", "rh") or hemi == "vol":
idx = 0
self.picked_renderer = self._renderer._all_renderers[idx]
# initialize the default point
if self._data["initial_time"] is not None:
# pick at that time
use_data = act_data[:, [np.round(self._data["time_idx"]).astype(int)]]
else:
use_data = act_data
ind = np.unravel_index(
np.argmax(np.abs(use_data), axis=None), use_data.shape
)
vertex_id = vertices[ind[0]]
publish(self, VertexSelect(hemi=hemi, vertex_id=vertex_id))
def _configure_picking(self):
# get data for each hemi
for idx, hemi in enumerate(["vol", "lh", "rh"]):
hemi_data = self._data.get(hemi)
if hemi_data is not None:
act_data = hemi_data["array"]
if act_data.ndim == 3:
act_data = np.linalg.norm(act_data, axis=1)
smooth_mat = hemi_data.get("smooth_mat")
vertices = hemi_data["vertices"]
if hemi == "vol":
assert smooth_mat is None
smooth_mat = csr_array(
(np.ones(len(vertices)), (vertices, np.arange(len(vertices))))
)
self.act_data_smooth[hemi] = (act_data, smooth_mat)
self._renderer._update_picking_callback(
self._on_mouse_move,
self._on_button_press,
self._on_button_release,
self._on_pick,
)
subscribe(self, "vertex_select", self._on_vertex_select)
def _configure_tool_bar(self):
if not hasattr(self._renderer, "_tool_bar") or self._renderer._tool_bar is None:
self._renderer._tool_bar_initialize(name="Toolbar")
@_auto_weakref
def save_image(filename):
self.save_image(filename)
self._renderer._tool_bar_add_file_button(
name="screenshot",
desc="Take a screenshot",
func=save_image,
)
@_auto_weakref
def save_movie(filename):
self.save_movie(
filename=filename, time_dilation=(1.0 / self.playback_speed)
)
self._renderer._tool_bar_add_file_button(
name="movie",
desc="Save movie...",
func=save_movie,
shortcut="ctrl+shift+s",
)
self._renderer._tool_bar_add_button(
name="visibility",
desc="Toggle Controls",
func=self.toggle_interface,
icon_name="visibility_on",
)
self._renderer._tool_bar_add_button(
name="scale",
desc="Auto-Scale",
func=self.apply_auto_scaling,
)
self._renderer._tool_bar_add_button(
name="clear",
desc="Clear traces",
func=self.clear_glyphs,
)
self._renderer._tool_bar_add_spacer()
self._renderer._tool_bar_add_button(
name="help",
desc="Help",
func=self.help,
shortcut="?",
)
def _rotate_camera(self, which, value):
_, _, azimuth, elevation, _ = self._renderer.get_camera(rigid=self._rigid)
kwargs = dict(update=True)
if which == "azimuth":
value = azimuth + value
# Our view_up threshold is 5/175, so let's be safe here
if elevation < 7.5 or elevation > 172.5:
kwargs["elevation"] = np.clip(elevation, 10, 170)
else:
value = np.clip(elevation + value, 10, 170)
kwargs[which] = value
self._set_camera(**kwargs)
def _configure_shortcuts(self):
# Remove the default key binding
if getattr(self, "iren", None) is not None:
self.plotter.iren.clear_key_event_callbacks()
# Then, we add our own:
self.plotter.add_key_event("i", self.toggle_interface)
self.plotter.add_key_event("s", self.apply_auto_scaling)
self.plotter.add_key_event("r", self.restore_user_scaling)
self.plotter.add_key_event("c", self.clear_glyphs)
for key, which, amt in (
("Left", "azimuth", 10),
("Right", "azimuth", -10),
("Up", "elevation", 10),
("Down", "elevation", -10),
):
self.plotter.clear_events_for_key(key)
self.plotter.add_key_event(key, partial(self._rotate_camera, which, amt))
def _configure_menu(self):
self._renderer._menu_initialize()
self._renderer._menu_add_submenu(
name="help",
desc="Help",
)
self._renderer._menu_add_button(
menu_name="help",
name="help",
desc="Show MNE key bindings\t?",
func=self.help,
)
def _configure_status_bar(self):
self._renderer._status_bar_initialize()
self.status_msg = self._renderer._status_bar_add_label(
self.default_status_bar_msg, stretch=1
)
self.status_progress = self._renderer._status_bar_add_progress_bar()
if self.status_progress is not None:
self.status_progress.hide()
def _on_mouse_move(self, vtk_picker, event):
if self._mouse_no_mvt:
self._mouse_no_mvt -= 1
def _on_button_press(self, vtk_picker, event):
self._mouse_no_mvt = 2
def _on_button_release(self, vtk_picker, event):
if self._mouse_no_mvt > 0:
x, y = vtk_picker.GetEventPosition()
# programmatically detect the picked renderer
try:
# pyvista<0.30.0
self.picked_renderer = self.plotter.iren.FindPokedRenderer(x, y)
except AttributeError:
# pyvista>=0.30.0
self.picked_renderer = self.plotter.iren.interactor.FindPokedRenderer(
x, y
)
# trigger the pick
self.plotter.picker.Pick(x, y, 0, self.picked_renderer)
self._mouse_no_mvt = 0
def _on_pick(self, vtk_picker, event):
if not self.show_traces:
return
# vtk_picker is a vtkCellPicker
cell_id = vtk_picker.GetCellId()
mesh = vtk_picker.GetDataSet()
if mesh is None or cell_id == -1 or not self._mouse_no_mvt:
return # don't pick
# 1) Check to see if there are any spheres along the ray
if len(self._spheres):
collection = vtk_picker.GetProp3Ds()
found_sphere = None
for ii in range(collection.GetNumberOfItems()):
actor = collection.GetItemAsObject(ii)
for sphere in self._spheres:
if any(a is actor for a in sphere._actors):
found_sphere = sphere
break
if found_sphere is not None:
break
if found_sphere is not None:
assert found_sphere._is_glyph
mesh = found_sphere
# 2) Remove sphere if it's what we have
if hasattr(mesh, "_is_glyph"):
self._remove_vertex_glyph(mesh)
return
# 3) Otherwise, pick the objects in the scene
try:
hemi = mesh._hemi
except AttributeError: # volume
hemi = "vol"
else:
assert hemi in ("lh", "rh")
if self.act_data_smooth[hemi][0] is None: # no data to add for hemi
return
pos = np.array(vtk_picker.GetPickPosition())
if hemi == "vol":
# VTK will give us the point closest to the viewer in the vol.
# We want to pick the point with the maximum value along the
# camera-to-click array, which fortunately we can get "just"
# by inspecting the points that are sufficiently close to the
# ray.
grid = mesh = self._data[hemi]["grid"]
vertices = self._data[hemi]["vertices"]
coords = self._data[hemi]["grid_coords"][vertices]
scalars = grid.cell_data["values"][vertices]
spacing = np.array(grid.GetSpacing())
max_dist = np.linalg.norm(spacing) / 2.0
origin = vtk_picker.GetRenderer().GetActiveCamera().GetPosition()
ori = pos - origin
ori /= np.linalg.norm(ori)
# the magic formula: distance from a ray to a given point
dists = np.linalg.norm(np.cross(ori, coords - pos), axis=1)
assert dists.shape == (len(coords),)
mask = dists <= max_dist
idx = np.where(mask)[0]
if len(idx) == 0:
return # weird point on edge of volume?
# useful for debugging the ray by mapping it into the volume:
# dists = dists - dists.min()
# dists = (1. - dists / dists.max()) * self._cmap_range[1]
# grid.cell_data['values'][vertices] = dists * mask
idx = idx[np.argmax(np.abs(scalars[idx]))]
vertex_id = vertices[idx]
# Naive way: convert pos directly to idx; i.e., apply mri_src_t
# shape = self._data[hemi]['grid_shape']
# taking into account the cell vs point difference (spacing/2)
# shift = np.array(grid.GetOrigin()) + spacing / 2.
# ijk = np.round((pos - shift) / spacing).astype(int)
# vertex_id = np.ravel_multi_index(ijk, shape, order='F')
else:
vtk_cell = mesh.GetCell(cell_id)
cell = [
vtk_cell.GetPointId(point_id)
for point_id in range(vtk_cell.GetNumberOfPoints())
]
vertices = mesh.points[cell]
idx = np.argmin(abs(vertices - pos), axis=0)
vertex_id = cell[idx[0]]
publish(self, VertexSelect(hemi=hemi, vertex_id=vertex_id))
def _on_time_change(self, event):
"""Respond to a time change UI event."""
if event.time == self._current_time:
return
time_idx = self._to_time_index(event.time)
self._update_current_time_idx(time_idx)
if self.time_viewer:
with disable_ui_events(self):
if "time" in self.widgets:
self.widgets["time"].set_value(time_idx)
if "current_time" in self.widgets:
self.widgets["current_time"].set_value(f"{self._current_time: .3f}")
self.plot_time_line(update=True)
def _on_colormap_range(self, event):
"""Respond to the colormap_range UI event."""
if event.kind != "distributed_source_power":
return
lims = {key: getattr(event, key) for key in ("fmin", "fmid", "fmax", "alpha")}
# Check if limits have changed at all.
if all(val is None or val == self._data[key] for key, val in lims.items()):
return
# Update the GUI elements.
with disable_ui_events(self):
for key, val in lims.items():
if val is not None:
if key in self.widgets:
self.widgets[key].set_value(val * self._data["fscale"])
entry_key = "entry_" + key
if entry_key in self.widgets:
self.widgets[entry_key].set_value(val * self._data["fscale"])
# Update the render.
self._update_colormap_range(**lims)
def _on_vertex_select(self, event):
"""Respond to vertex_select UI event."""
if event.hemi == "vol":
try:
mesh = self._data[event.hemi]["grid"]
except KeyError:
return
else:
try:
mesh = self._layered_meshes[event.hemi]._polydata
except KeyError:
return
if self.traces_mode == "label":
self._add_label_glyph(event.hemi, mesh, event.vertex_id)
else:
self._add_vertex_glyph(event.hemi, mesh, event.vertex_id)
def _add_label_glyph(self, hemi, mesh, vertex_id):
if hemi == "vol":
return
label_id = self._vertex_to_label_id[hemi][vertex_id]
label = self._annotation_labels[hemi][label_id]
# remove the patch if already picked
if label_id in self.picked_patches[hemi]:
self._remove_label_glyph(hemi, label_id)
return
if hemi == label.hemi:
self.add_label(label, borders=True)
self.picked_patches[hemi].append(label_id)
def _remove_label_glyph(self, hemi, label_id):
label = self._annotation_labels[hemi][label_id]
label._line.remove()
self.color_cycle.restore(label._color)
self.mpl_canvas.update_plot()
self._layered_meshes[hemi].remove_overlay(label.name)
self.picked_patches[hemi].remove(label_id)
def _add_vertex_glyph(self, hemi, mesh, vertex_id, update=True):
if vertex_id in self.picked_points[hemi]:
return
# skip if the wrong hemi is selected
if self.act_data_smooth[hemi][0] is None:
return
color = next(self.color_cycle)
line = self.plot_time_course(hemi, vertex_id, color, update=update)
if hemi == "vol":
ijk = np.unravel_index(
vertex_id, np.array(mesh.GetDimensions()) - 1, order="F"
)
voxel = mesh.GetCell(*ijk)
center = np.empty(3)
voxel.GetCentroid(center)
else:
center = mesh.GetPoints().GetPoint(vertex_id)
del mesh
# from the picked renderer to the subplot coords
try:
lst = self._renderer._all_renderers._renderers
except AttributeError:
lst = self._renderer._all_renderers
rindex = lst.index(self.picked_renderer)
row, col = self._renderer._index_to_loc(rindex)
actors = list()
spheres = list()
for _ in self._iter_views(hemi):
# Using _sphere() instead of renderer.sphere() for 2 reasons:
# 1) renderer.sphere() fails on Windows in a scenario where a lot
# of picking requests are done in a short span of time (could be
# mitigated with synchronization/delay?)
# 2) the glyph filter is used in renderer.sphere() but only one
# sphere is required in this function.
actor, sphere = self._renderer._sphere(
center=np.array(center),
color=color,
radius=4.0,
)
actors.append(actor)
spheres.append(sphere)
# add metadata for picking
for sphere in spheres:
sphere._is_glyph = True
sphere._hemi = hemi
sphere._line = line
sphere._actors = actors
sphere._color = color
sphere._vertex_id = vertex_id
self.picked_points[hemi].append(vertex_id)
self._spheres.extend(spheres)
self.pick_table[vertex_id] = spheres
return sphere
def _remove_vertex_glyph(self, mesh, render=True):
vertex_id = mesh._vertex_id
if vertex_id not in self.pick_table:
return
hemi = mesh._hemi
color = mesh._color
spheres = self.pick_table[vertex_id]
spheres[0]._line.remove()
self.mpl_canvas.update_plot()
self.picked_points[hemi].remove(vertex_id)
with warnings.catch_warnings(record=True):
# We intentionally ignore these in case we have traversed the
# entire color cycle
warnings.simplefilter("ignore")
self.color_cycle.restore(color)
for sphere in spheres:
# remove all actors
self.plotter.remove_actor(sphere._actors, render=False)
sphere._actors = None
self._spheres.pop(self._spheres.index(sphere))
if render:
self._renderer._update()
self.pick_table.pop(vertex_id)
def clear_glyphs(self):
"""Clear the picking glyphs."""
if not self.time_viewer:
return
for sphere in list(self._spheres): # will remove itself, so copy
self._remove_vertex_glyph(sphere, render=False)
assert sum(len(v) for v in self.picked_points.values()) == 0
assert len(self.pick_table) == 0
assert len(self._spheres) == 0
for hemi in self._hemis:
for label_id in list(self.picked_patches[hemi]):
self._remove_label_glyph(hemi, label_id)
assert sum(len(v) for v in self.picked_patches.values()) == 0
if self.rms is not None:
self.rms.remove()
self.rms = None
self._renderer._update()
@fill_doc
def plot_time_course(self, hemi, vertex_id, color, update=True):
"""Plot the vertex time course.
Parameters
----------
hemi : str
The hemisphere id of the vertex.
vertex_id : int
The vertex identifier in the mesh.
color : matplotlib color
The color of the time course.
%(brain_update)s
Returns
-------
line : matplotlib object
The time line object.
"""
if self.mpl_canvas is None:
return
time = self._data["time"].copy() # avoid circular ref
mni = None
if hemi == "vol":
hemi_str = "V"
xfm = read_talxfm(self._subject, self._subjects_dir)
if self._units == "mm":
xfm["trans"][:3, 3] *= 1000.0
ijk = np.unravel_index(vertex_id, self._data[hemi]["grid_shape"], order="F")
src_mri_t = self._data[hemi]["grid_src_mri_t"]
mni = apply_trans(xfm["trans"] @ src_mri_t, ijk)
else:
hemi_str = "L" if hemi == "lh" else "R"
try:
mni = vertex_to_mni(
vertices=vertex_id,
hemis=0 if hemi == "lh" else 1,
subject=self._subject,
subjects_dir=self._subjects_dir,
)
except Exception:
mni = None
if mni is not None:
mni = " MNI: " + ", ".join(f"{m:5.1f}" for m in mni)
else:
mni = ""
label = f"{hemi_str}:{str(vertex_id).ljust(6)}{mni}"
act_data, smooth = self.act_data_smooth[hemi]
if smooth is not None:
act_data = (smooth[[vertex_id]] @ act_data)[0]
else:
act_data = act_data[vertex_id].copy()
line = self.mpl_canvas.plot(
time,
act_data,
label=label,
lw=1.0,
color=color,
zorder=4,
update=update,
)
return line
@fill_doc
def plot_time_line(self, update=True):
"""Add the time line to the MPL widget.
Parameters
----------
%(brain_update)s
"""
if self.mpl_canvas is None:
return
if isinstance(self.show_traces, bool) and self.show_traces:
# add time information
current_time = self._current_time
if not hasattr(self, "time_line"):
self.time_line = self.mpl_canvas.plot_time_line(
x=current_time,
label="time",
color=self._fg_color,
lw=1,
update=update,
)
self.time_line.set_xdata([current_time])
if update:
self.mpl_canvas.update_plot()
def _configure_help(self):
pairs = [
("?", "Display help window"),
("i", "Toggle interface"),
("s", "Apply auto-scaling"),
("r", "Restore original clim"),
("c", "Clear all traces"),
("n", "Shift the time forward by the playback speed"),
("b", "Shift the time backward by the playback speed"),
("Space", "Start/Pause playback"),
("Up", "Decrease camera elevation angle"),
("Down", "Increase camera elevation angle"),
("Left", "Decrease camera azimuth angle"),
("Right", "Increase camera azimuth angle"),
]
text1, text2 = zip(*pairs)
text1 = "\n".join(text1)
text2 = "\n".join(text2)
self.help_canvas = self._renderer._window_get_simple_canvas(
width=5, height=2, dpi=80
)
_show_help_fig(
col1=text1,
col2=text2,
fig_help=self.help_canvas.fig,
ax=self.help_canvas.axes,
show=False,
)
def help(self):
"""Display the help window."""
self.help_canvas.show()
def _clear_callbacks(self):
# Remove the default key binding
if getattr(self, "iren", None) is not None:
self.plotter.iren.clear_key_event_callbacks()
def _clear_widgets(self):
if not hasattr(self, "widgets"):
return
for widget in self.widgets.values():
if widget is not None:
for key in ("triggered", "floatValueChanged"):
setattr(widget, key, None)
self.widgets.clear()
@property
def interaction(self):
"""The interaction style."""
return self._interaction
@interaction.setter
def interaction(self, interaction):
"""Set the interaction style."""
_validate_type(interaction, str, "interaction")
_check_option("interaction", interaction, ("trackball", "terrain"))
for _ in self._iter_views("vol"): # will traverse all
self._renderer.set_interaction(interaction)
def _cortex_colormap(self, cortex):
"""Return the colormap corresponding to the cortex."""
from matplotlib.colors import ListedColormap
from .._3d import _get_cmap
colormap_map = dict(
classic=dict(colormap="Greys", vmin=-1, vmax=2),
high_contrast=dict(colormap="Greys", vmin=-0.1, vmax=1.3),
low_contrast=dict(colormap="Greys", vmin=-5, vmax=5),
bone=dict(colormap="bone_r", vmin=-0.2, vmax=2),
)
_validate_type(cortex, (str, dict, list, tuple), "cortex")
if isinstance(cortex, str):
if cortex in colormap_map:
cortex = colormap_map[cortex]
else:
cortex = [cortex] * 2
if isinstance(cortex, list | tuple):
_check_option(
"len(cortex)",
len(cortex),
(2, 3),
extra="when cortex is a list or tuple",
)
if len(cortex) == 3:
cortex = [cortex] * 2
cortex = list(cortex)
for ci, c in enumerate(cortex):
cortex[ci] = _to_rgb(c, name="cortex")
cortex = dict(
colormap=ListedColormap(cortex, name="custom binary"), vmin=0, vmax=1
)
cortex = dict(
vmin=float(cortex["vmin"]),
vmax=float(cortex["vmax"]),
colormap=_get_cmap(cortex["colormap"]),
)
return cortex
def _remove(self, item, render=False):
"""Remove actors from the rendered scene."""
if item in self._actors:
logger.debug(f"Removing {len(self._actors[item])} {item} actor(s)")
for actor in self._actors[item]:
self._renderer.plotter.remove_actor(actor, render=False)
self._actors.pop(item) # remove actor list
if render:
self._renderer._update()
def _add_actor(self, item, actor):
"""Add an actor to the internal register."""
if item in self._actors: # allows adding more than one
self._actors[item].append(actor)
else:
self._actors[item] = [actor]
@verbose
def add_data(
self,
array,
fmin=None,
fmid=None,
fmax=None,
thresh=None,
center=None,
transparent=False,
colormap="auto",
alpha=1,
vertices=None,
smoothing_steps=None,
time=None,
time_label="auto",
colorbar=True,
hemi=None,
remove_existing=None,
time_label_size=None,
initial_time=None,
scale_factor=None,
vector_alpha=None,
clim=None,
src=None,
volume_options=0.4,
colorbar_kwargs=None,
verbose=None,
):
"""Display data from a numpy array on the surface or volume.
This provides a similar interface to PySurfer, but it displays
it with a single colormap. It offers more flexibility over the
colormap, and provides a way to display four-dimensional data
(i.e., a timecourse) or five-dimensional data (i.e., a
vector-valued timecourse).
.. note:: ``fmin`` sets the low end of the colormap, and is separate
from thresh (this is a different convention from PySurfer).
Parameters
----------
array : numpy array, shape (n_vertices[, 3][, n_times])
Data array. For the data to be understood as vector-valued
(3 values per vertex corresponding to X/Y/Z surface RAS),
then ``array`` must be have all 3 dimensions.
If vectors with no time dimension are desired, consider using a
singleton (e.g., ``np.newaxis``) to create a "time" dimension
and pass ``time_label=None`` (vector values are not supported).
%(fmin_fmid_fmax)s
%(thresh)s
%(center)s
%(transparent)s
colormap : str, list of color, or array
Name of matplotlib colormap to use, a list of matplotlib colors,
or a custom look up table (an n x 4 array coded with RBGA values
between 0 and 255), the default "auto" chooses a default divergent
colormap, if "center" is given (currently "icefire"), otherwise a
default sequential colormap (currently "rocket").
alpha : float in [0, 1]
Alpha level to control opacity of the overlay.
vertices : numpy array
Vertices for which the data is defined (needed if
``len(data) < nvtx``).
smoothing_steps : int or None
Number of smoothing steps (smoothing is used if len(data) < nvtx)
The value 'nearest' can be used too. None (default) will use as
many as necessary to fill the surface.
time : numpy array
Time points in the data array (if data is 2D or 3D).
%(time_label)s
colorbar : bool
Whether to add a colorbar to the figure. Can also be a tuple
to give the (row, col) index of where to put the colorbar.
hemi : str | None
If None, it is assumed to belong to the hemisphere being
shown. If two hemispheres are being shown, an error will
be thrown.
remove_existing : bool
Not supported yet.
Remove surface added by previous "add_data" call. Useful for
conserving memory when displaying different data in a loop.
time_label_size : int
Font size of the time label (default 14).
initial_time : float | None
Time initially shown in the plot. ``None`` to use the first time
sample (default).
scale_factor : float | None (default)
The scale factor to use when displaying glyphs for vector-valued
data.
vector_alpha : float | None
Alpha level to control opacity of the arrows. Only used for
vector-valued data. If None (default), ``alpha`` is used.
clim : dict
Original clim arguments.
%(src_volume_options)s
colorbar_kwargs : dict | None
Options to pass to ``pyvista.Plotter.add_scalar_bar``
(e.g., ``dict(title_font_size=10)``).
%(verbose)s
Notes
-----
If the data is defined for a subset of vertices (specified
by the "vertices" parameter), a smoothing method is used to interpolate
the data onto the high resolution surface. If the data is defined for
subsampled version of the surface, smoothing_steps can be set to None,
in which case only as many smoothing steps are applied until the whole
surface is filled with non-zeros.
Due to a VTK alpha rendering bug, ``vector_alpha`` is
clamped to be strictly < 1.
"""
_validate_type(transparent, bool, "transparent")
_validate_type(vector_alpha, ("numeric", None), "vector_alpha")
_validate_type(scale_factor, ("numeric", None), "scale_factor")
# those parameters are not supported yet, only None is allowed
_check_option("thresh", thresh, [None])
_check_option("remove_existing", remove_existing, [None])
_validate_type(time_label_size, (None, "numeric"), "time_label_size")
if time_label_size is not None:
time_label_size = float(time_label_size)
if time_label_size < 0:
raise ValueError(
f"time_label_size must be positive, got {time_label_size}"
)
hemi = self._check_hemi(hemi, extras=["vol"])
stc, array, vertices = self._check_stc(hemi, array, vertices)
array = np.asarray(array)
vector_alpha = alpha if vector_alpha is None else vector_alpha
self._data["vector_alpha"] = vector_alpha
self._data["scale_factor"] = scale_factor
# Create time array and add label if > 1D
if array.ndim <= 1:
time_idx = 0
else:
# check time array
if time is None:
time = np.arange(array.shape[-1])
else:
time = np.asarray(time)
if time.shape != (array.shape[-1],):
raise ValueError(
f"time has shape {time.shape}, but need shape "
f"{(array.shape[-1],)} (array.shape[-1])"
)
self._data["time"] = time
if self._n_times is None:
self._times = time
elif len(time) != self._n_times:
raise ValueError("New n_times is different from previous n_times")
elif not np.array_equal(time, self._times):
raise ValueError(
"Not all time values are consistent with previously set times."
)
# initial time
if initial_time is None:
time_idx = 0
else:
time_idx = self._to_time_index(initial_time)
# time label
time_label, _ = _handle_time(time_label, "s", time)
y_txt = 0.05 + 0.1 * bool(colorbar)
if array.ndim == 3:
if array.shape[1] != 3:
raise ValueError(
"If array has 3 dimensions, array.shape[1] must equal 3, got "
f"{array.shape[1]}"
)
fmin, fmid, fmax = _update_limits(fmin, fmid, fmax, center, array)
if colormap == "auto":
colormap = "mne" if center is not None else "hot"
if smoothing_steps is None:
smoothing_steps = 7
elif smoothing_steps == "nearest":
smoothing_steps = -1
elif isinstance(smoothing_steps, int):
if smoothing_steps < 0:
raise ValueError(
"Expected value of `smoothing_steps` is positive but "
f"{smoothing_steps} was given."
)
else:
raise TypeError(
"Expected type of `smoothing_steps` is int or NoneType but "
f"{type(smoothing_steps)} was given."
)
self._data["stc"] = stc
self._data["src"] = src
self._data["smoothing_steps"] = smoothing_steps
self._data["clim"] = clim
self._data["time"] = time
self._data["initial_time"] = initial_time
self._data["time_label"] = time_label
self._data["initial_time_idx"] = time_idx
self._data["time_idx"] = time_idx
self._data["transparent"] = transparent
# data specific for a hemi
self._data[hemi] = dict()
self._data[hemi]["glyph_dataset"] = None
self._data[hemi]["glyph_mapper"] = None
self._data[hemi]["glyph_actor"] = None
self._data[hemi]["array"] = array
self._data[hemi]["vertices"] = vertices
self._data["alpha"] = alpha
self._data["colormap"] = colormap
self._data["center"] = center
self._data["fmin"] = fmin
self._data["fmid"] = fmid
self._data["fmax"] = fmax
self._update_colormap_range()
# 1) add the surfaces first
actor = None
for _ in self._iter_views(hemi):
if hemi in ("lh", "rh"):
actor = self._layered_meshes[hemi]._actor
else:
src_vol = src[2:] if src.kind == "mixed" else src
actor, _ = self._add_volume_data(hemi, src_vol, volume_options)
assert actor is not None # should have added one
self._add_actor("data", actor)
# 2) update time and smoothing properties
# set_data_smoothing calls "_update_current_time_idx" for us, which will set
# _current_time
self.set_time_interpolation(self.time_interpolation)
self.set_data_smoothing(self._data["smoothing_steps"])
# 3) add the other actors
if colorbar is True:
# bottom left by default
colorbar = (self._subplot_shape[0] - 1, 0)
for ri, ci, v in self._iter_views(hemi):
# Add the time label to the bottommost view
do = (ri, ci) == colorbar
if not self._time_label_added and time_label is not None and do:
time_actor = self._renderer.text2d(
x_window=0.95,
y_window=y_txt,
color=self._fg_color,
size=time_label_size,
text=time_label(self._current_time),
justification="right",
)
self._data["time_actor"] = time_actor
self._time_label_added = True
if colorbar and self._scalar_bar is None and do:
kwargs = dict(
source=actor,
n_labels=8,
color=self._fg_color,
bgcolor=self._brain_color[:3],
)
kwargs.update(colorbar_kwargs or {})
self._scalar_bar = self._renderer.scalarbar(**kwargs)
self._set_camera(**views_dicts[hemi][v])
# 4) update the scalar bar and opacity (and render)
self._update_colormap_range(alpha=alpha)
# 5) enable UI events to interact with the data
subscribe(self, "colormap_range", self._on_colormap_range)
if time is not None and len(time) > 1:
subscribe(self, "time_change", self._on_time_change)
def remove_data(self):
"""Remove rendered data from the mesh."""
self._remove("data", render=True)
# Stop listening to events
if "time_change" in _get_event_channel(self):
unsubscribe(self, "time_change")
def _iter_views(self, hemi):
"""Iterate over rows and columns that need to be added to."""
hemi_dict = dict(lh=[0], rh=[0], vol=[0])
if self._hemi == "split":
hemi_dict.update(rh=[1], vol=[0, 1])
for vi, view in enumerate(self._views):
view_dict = dict(lh=[vi], rh=[vi], vol=[vi])
if self._hemi == "split":
view_dict.update(vol=[vi, vi])
if self._view_layout == "vertical":
rows, cols = view_dict, hemi_dict # views are rows, hemis cols
else:
rows, cols = hemi_dict, view_dict # hemis are rows, views cols
for ri, ci in zip(rows[hemi], cols[hemi]):
self._renderer.subplot(ri, ci)
yield ri, ci, view
def remove_labels(self):
"""Remove all the ROI labels from the image."""
for hemi in self._hemis:
mesh = self._layered_meshes[hemi]
for label in self._labels[hemi]:
mesh.remove_overlay(label.name)
self._labels[hemi].clear()
self._renderer._update()
def remove_annotations(self):
"""Remove all annotations from the image."""
for hemi in self._hemis:
if hemi in self._layered_meshes:
mesh = self._layered_meshes[hemi]
mesh.remove_overlay(self._annots[hemi])
if hemi in self._annots:
self._annots[hemi].clear()
self._renderer._update()
def _add_volume_data(self, hemi, src, volume_options):
from ...source_space import SourceSpaces
from ..backends._pyvista import _hide_testing_actor
_validate_type(src, SourceSpaces, "src")
_check_option("src.kind", src.kind, ("volume",))
_validate_type(volume_options, (dict, "numeric", None), "volume_options")
assert hemi == "vol"
if not isinstance(volume_options, dict):
volume_options = dict(
resolution=float(volume_options) if volume_options is not None else None
)
volume_options = _handle_default("volume_options", volume_options)
allowed_types = (
["resolution", (None, "numeric")],
["blending", (str,)],
["alpha", ("numeric", None)],
["surface_alpha", (None, "numeric")],
["silhouette_alpha", (None, "numeric")],
["silhouette_linewidth", ("numeric",)],
)
for key, types in allowed_types:
_validate_type(volume_options[key], types, f"volume_options[{repr(key)}]")
extra_keys = set(volume_options) - set(a[0] for a in allowed_types)
if len(extra_keys):
raise ValueError(f"volume_options got unknown keys {sorted(extra_keys)}")
blending = _check_option(
'volume_options["blending"]',
volume_options["blending"],
("composite", "mip"),
)
alpha = volume_options["alpha"]
if alpha is None:
alpha = 0.4 if self._data[hemi]["array"].ndim == 3 else 1.0
alpha = np.clip(float(alpha), 0.0, 1.0)
resolution = volume_options["resolution"]
surface_alpha = volume_options["surface_alpha"]
if surface_alpha is None:
surface_alpha = min(alpha / 2.0, 0.1)
silhouette_alpha = volume_options["silhouette_alpha"]
if silhouette_alpha is None:
silhouette_alpha = surface_alpha / 4.0
silhouette_linewidth = volume_options["silhouette_linewidth"]
del volume_options
volume_pos = self._data[hemi].get("grid_volume_pos")
volume_neg = self._data[hemi].get("grid_volume_neg")
center = self._data["center"]
if volume_pos is None:
xyz = np.meshgrid(*[np.arange(s) for s in src[0]["shape"]], indexing="ij")
dimensions = np.array(src[0]["shape"], int)
mult = 1000 if self._units == "mm" else 1
src_mri_t = src[0]["src_mri_t"]["trans"].copy()
src_mri_t[:3] *= mult
if resolution is not None:
resolution = resolution * mult / 1000.0 # to mm
del src, mult
coords = np.array([c.ravel(order="F") for c in xyz]).T
coords = apply_trans(src_mri_t, coords)
self.geo[hemi] = Bunch(coords=coords)
vertices = self._data[hemi]["vertices"]
assert self._data[hemi]["array"].shape[0] == len(vertices)
# MNE constructs the source space on a uniform grid in MRI space,
# but mne coreg can change it to be non-uniform, so we need to
# use all three elements here
assert np.allclose(src_mri_t[:3, :3], np.diag(np.diag(src_mri_t)[:3]))
spacing = np.diag(src_mri_t)[:3]
origin = src_mri_t[:3, 3] - spacing / 2.0
scalars = np.zeros(np.prod(dimensions))
scalars[vertices] = 1.0 # for the outer mesh
grid, grid_mesh, volume_pos, volume_neg = self._renderer._volume(
dimensions,
origin,
spacing,
scalars,
surface_alpha,
resolution,
blending,
center,
)
self._data[hemi]["alpha"] = alpha # incorrectly set earlier
self._data[hemi]["grid"] = grid
self._data[hemi]["grid_mesh"] = grid_mesh
self._data[hemi]["grid_coords"] = coords
self._data[hemi]["grid_src_mri_t"] = src_mri_t
self._data[hemi]["grid_shape"] = dimensions
self._data[hemi]["grid_volume_pos"] = volume_pos
self._data[hemi]["grid_volume_neg"] = volume_neg
actor_pos, _ = self._renderer.plotter.add_actor(
volume_pos, name=None, culling=False, reset_camera=False, render=False
)
actor_neg = actor_mesh = None
if volume_neg is not None:
actor_neg, _ = self._renderer.plotter.add_actor(
volume_neg, name=None, culling=False, reset_camera=False, render=False
)
grid_mesh = self._data[hemi]["grid_mesh"]
if grid_mesh is not None:
actor_mesh, prop = self._renderer.plotter.add_actor(
grid_mesh,
name=None,
culling=False,
pickable=False,
reset_camera=False,
render=False,
)
prop.SetColor(*self._brain_color[:3])
prop.SetOpacity(surface_alpha)
if silhouette_alpha > 0 and silhouette_linewidth > 0:
for _ in self._iter_views("vol"):
self._renderer._silhouette(
mesh=grid_mesh.GetInput(),
color=self._brain_color[:3],
line_width=silhouette_linewidth,
alpha=silhouette_alpha,
)
for actor in (actor_pos, actor_neg, actor_mesh):
if actor is not None:
_hide_testing_actor(actor)
return actor_pos, actor_neg
def add_label(
self,
label,
color=None,
alpha=1,
scalar_thresh=None,
borders=False,
hemi=None,
subdir=None,
):
"""Add an ROI label to the image.
Parameters
----------
label : str | instance of Label
Label filepath or name. Can also be an instance of
an object with attributes "hemi", "vertices", "name", and
optionally "color" and "values" (if scalar_thresh is not None).
color : matplotlib-style color | None
Anything matplotlib accepts: string, RGB, hex, etc. (default
"crimson").
alpha : float in [0, 1]
Alpha level to control opacity.
scalar_thresh : None | float
Threshold the label ids using this value in the label
file's scalar field (i.e. label only vertices with
scalar >= thresh).
borders : bool | int
Show only label borders. If int, specify the number of steps
(away from the true border) along the cortical mesh to include
as part of the border definition.
hemi : str | None
If None, it is assumed to belong to the hemisphere being
shown.
subdir : None | str
If a label is specified as name, subdir can be used to indicate
that the label file is in a sub-directory of the subject's
label directory rather than in the label directory itself (e.g.
for ``$SUBJECTS_DIR/$SUBJECT/label/aparc/lh.cuneus.label``
``brain.add_label('cuneus', subdir='aparc')``).
Notes
-----
To remove previously added labels, run Brain.remove_labels().
"""
from ...label import read_label
if isinstance(label, str):
if color is None:
color = "crimson"
if os.path.isfile(label):
filepath = label
label = read_label(filepath)
hemi = label.hemi
label_name = os.path.basename(filepath).split(".")[1]
else:
hemi = self._check_hemi(hemi)
label_name = label
label_fname = ".".join([hemi, label_name, "label"])
if subdir is None:
filepath = op.join(
self._subjects_dir, self._subject, "label", label_fname
)
else:
filepath = op.join(
self._subjects_dir, self._subject, "label", subdir, label_fname
)
if not os.path.exists(filepath):
raise ValueError(f"Label file {filepath} does not exist")
label = read_label(filepath)
ids = label.vertices
scalars = label.values
else:
# try to extract parameters from label instance
try:
hemi = label.hemi
ids = label.vertices
if label.name is None:
label.name = "unnamed" + str(self._unnamed_label_id)
self._unnamed_label_id += 1
label_name = str(label.name)
if color is None:
if hasattr(label, "color") and label.color is not None:
color = label.color
else:
color = "crimson"
if scalar_thresh is not None:
scalars = label.values
except Exception:
raise ValueError(
"Label was not a filename (str), and could "
"not be understood as a class. The class "
'must have attributes "hemi", "vertices", '
'"name", and (if scalar_thresh is not None)'
'"values"'
)
hemi = self._check_hemi(hemi)
if scalar_thresh is not None:
ids = ids[scalars >= scalar_thresh]
if self.time_viewer and self.show_traces and self.traces_mode == "label":
stc = self._data["stc"]
src = self._data["src"]
tc = stc.extract_label_time_course(
label, src=src, mode=self.label_extract_mode
)
tc = tc[0] if tc.ndim == 2 else tc[0, 0, :]
color = next(self.color_cycle)
line = self.mpl_canvas.plot(
self._data["time"], tc, label=label_name, color=color
)
else:
line = None
orig_color = color
color = _to_rgb(color, alpha, alpha=True)
cmap = np.array(
[
(
0,
0,
0,
0,
),
color,
]
)
ctable = np.round(cmap * 255).astype(np.uint8)
scalars = np.zeros(self.geo[hemi].coords.shape[0])
scalars[ids] = 1
if borders:
keep_idx = _mesh_borders(self.geo[hemi].faces, scalars)
show = np.zeros(scalars.size, dtype=np.int64)
if isinstance(borders, int):
for _ in range(borders):
keep_idx = np.isin(self.geo[hemi].faces.ravel(), keep_idx)
keep_idx.shape = self.geo[hemi].faces.shape
keep_idx = self.geo[hemi].faces[np.any(keep_idx, axis=1)]
keep_idx = np.unique(keep_idx)
show[keep_idx] = 1
scalars *= show
for _, _, v in self._iter_views(hemi):
mesh = self._layered_meshes[hemi]
mesh.add_overlay(
scalars=scalars,
colormap=ctable,
rng=[np.min(scalars), np.max(scalars)],
opacity=alpha,
name=label_name,
)
if self.time_viewer and self.show_traces and self.traces_mode == "label":
label._color = orig_color
label._line = line
self._labels[hemi].append(label)
self._renderer._update()
@fill_doc
def add_forward(self, fwd, trans, alpha=1, scale=None):
"""Add a quiver to render positions of dipoles.
Parameters
----------
%(fwd)s
%(trans_not_none)s
%(alpha)s Default 1.
scale : None | float
The size of the arrow representing the dipoles in
:class:`mne.viz.Brain` units. Default 1.5mm.
Notes
-----
.. versionadded:: 1.0
"""
head_mri_t = _get_trans(trans, "head", "mri", allow_none=False)[0]
del trans
if scale is None:
scale = 1.5 if self._units == "mm" else 1.5e-3
error_msg = (
'Unexpected forward model coordinate frame {}, must be "head" or "mri"'
)
if fwd["coord_frame"] in _frame_to_str:
fwd_frame = _frame_to_str[fwd["coord_frame"]]
if fwd_frame == "mri":
fwd_trans = Transform("mri", "mri")
elif fwd_frame == "head":
fwd_trans = head_mri_t
else:
raise RuntimeError(error_msg.format(fwd_frame))
else:
raise RuntimeError(error_msg.format(fwd["coord_frame"]))
for actor in _plot_forward(
self._renderer,
fwd,
fwd_trans,
fwd_scale=1e3 if self._units == "mm" else 1,
scale=scale,
alpha=alpha,
):
self._add_actor("forward", actor)
self._renderer._update()
def remove_forward(self):
"""Remove forward sources from the rendered scene."""
self._remove("forward", render=True)
@fill_doc
def add_dipole(
self, dipole, trans, colors="red", alpha=1, scales=None, *, mode="arrow"
):
"""Add a quiver to render positions of dipoles.
Parameters
----------
dipole : instance of Dipole
Dipole object containing position, orientation and amplitude of
one or more dipoles or in the forward solution.
%(trans_not_none)s
colors : list | matplotlib-style color | None
A single color or list of anything matplotlib accepts:
string, RGB, hex, etc. Default red.
%(alpha)s Default 1.
scales : list | float | None
The size of the arrow representing the dipole in
:class:`mne.viz.Brain` units. Default 5mm.
mode : "2darrow" | "arrow" | "cone" | "cylinder" | "sphere" | "oct"
The drawing mode for the dipole to render.
Defaults to ``"arrow"``.
Notes
-----
.. versionadded:: 1.0
"""
head_mri_t = _get_trans(trans, "head", "mri", allow_none=False)[0]
del trans
n_dipoles = len(dipole)
if not isinstance(colors, list | tuple):
colors = [colors] * n_dipoles # make into list
if len(colors) != n_dipoles:
raise ValueError(
f"The number of colors ({len(colors)}) "
f"and dipoles ({n_dipoles}) must match"
)
colors = [
_to_rgb(color, name=f"colors[{ci}]") for ci, color in enumerate(colors)
]
if scales is None:
scales = 5 if self._units == "mm" else 5e-3
if not isinstance(scales, list | tuple):
scales = [scales] * n_dipoles # make into list
if len(scales) != n_dipoles:
raise ValueError(
f"The number of scales ({len(scales)}) "
f"and dipoles ({n_dipoles}) must match"
)
pos = apply_trans(head_mri_t, dipole.pos)
pos *= 1e3 if self._units == "mm" else 1
for _ in self._iter_views("vol"):
for this_pos, this_ori, color, scale in zip(
pos, dipole.ori, colors, scales
):
actor, _ = self._renderer.quiver3d(
*this_pos,
*this_ori,
color=color,
opacity=alpha,
mode=mode,
scale=scale,
)
self._add_actor("dipole", actor)
self._renderer._update()
def remove_dipole(self):
"""Remove dipole objects from the rendered scene."""
self._remove("dipole", render=True)
@fill_doc
def add_head(self, dense=True, color="gray", alpha=0.5):
"""Add a mesh to render the outer head surface.
Parameters
----------
dense : bool
Whether to plot the dense head (``seghead``) or the less dense head
(``head``).
%(color_matplotlib)s
%(alpha)s
Notes
-----
.. versionadded:: 0.24
"""
# load head
surf = _get_head_surface(
"seghead" if dense else "head", self._subject, self._subjects_dir
)
verts, triangles = surf["rr"], surf["tris"]
verts *= 1e3 if self._units == "mm" else 1
color = _to_rgb(color)
for _ in self._iter_views("vol"):
actor, _ = self._renderer.mesh(
*verts.T,
triangles=triangles,
color=color,
opacity=alpha,
render=False,
)
self._add_actor("head", actor)
self._renderer._update()
def remove_head(self):
"""Remove head objects from the rendered scene."""
self._remove("head", render=True)
@fill_doc
def add_skull(self, outer=True, color="gray", alpha=0.5):
"""Add a mesh to render the skull surface.
Parameters
----------
outer : bool
Adds the outer skull if ``True``, otherwise adds the inner skull.
%(color_matplotlib)s
%(alpha)s
Notes
-----
.. versionadded:: 0.24
"""
surf = _get_skull_surface(
"outer" if outer else "inner", self._subject, self._subjects_dir
)
verts, triangles = surf["rr"], surf["tris"]
verts *= 1e3 if self._units == "mm" else 1
color = _to_rgb(color)
for _ in self._iter_views("vol"):
actor, _ = self._renderer.mesh(
*verts.T,
triangles=triangles,
color=color,
opacity=alpha,
reset_camera=False,
render=False,
)
self._add_actor("skull", actor)
self._renderer._update()
def remove_skull(self):
"""Remove skull objects from the rendered scene."""
self._remove("skull", render=True)
@fill_doc
def add_volume_labels(
self,
aseg="auto",
labels=None,
colors=None,
alpha=0.5,
smooth=0.9,
fill_hole_size=None,
legend=None,
):
"""Add labels to the rendering from an anatomical segmentation.
Parameters
----------
%(aseg)s
labels : list
Labeled regions of interest to plot. See
:func:`mne.get_montage_volume_labels`
for one way to determine regions of interest. Regions can also be
chosen from the :term:`FreeSurfer LUT`.
colors : list | matplotlib-style color | None
A list of anything matplotlib accepts: string, RGB, hex, etc.
(default :term:`FreeSurfer LUT` colors).
%(alpha)s
%(smooth)s
fill_hole_size : int | None
The size of holes to remove in the mesh in voxels. Default is None,
no holes are removed. Warning, this dilates the boundaries of the
surface by ``fill_hole_size`` number of voxels so use the minimal
size.
legend : bool | None | dict
Add a legend displaying the names of the ``labels``. Default (None)
is ``True`` if the number of ``labels`` is 10 or fewer.
Can also be a dict of ``kwargs`` to pass to
``pyvista.Plotter.add_legend``.
Notes
-----
.. versionadded:: 0.24
"""
aseg, aseg_data = _get_aseg(aseg, self._subject, self._subjects_dir)
vox_mri_t = aseg.header.get_vox2ras_tkr()
mult = 1e-3 if self._units == "m" else 1
vox_mri_t[:3] *= mult
del aseg
# read freesurfer lookup table
lut, fs_colors = read_freesurfer_lut()
if labels is None: # assign default ROI labels based on indices
lut_r = {v: k for k, v in lut.items()}
labels = [lut_r[idx] for idx in DEFAULTS["volume_label_indices"]]
_validate_type(fill_hole_size, (int, None), "fill_hole_size")
_validate_type(legend, (bool, None, dict), "legend")
if legend is None:
legend = len(labels) < 11
if colors is None:
colors = [fs_colors[label] / 255 for label in labels]
elif not isinstance(colors, list | tuple):
colors = [colors] * len(labels) # make into list
colors = [
_to_rgb(color, name=f"colors[{ci}]") for ci, color in enumerate(colors)
]
surfs = _marching_cubes(
aseg_data,
[lut[label] for label in labels],
smooth=smooth,
fill_hole_size=fill_hole_size,
)
for label, color, (verts, triangles) in zip(labels, colors, surfs):
if len(verts) == 0: # not in aseg vals
warn(
f"Value {lut[label]} not found for label "
f"{repr(label)} in anatomical segmentation file "
)
continue
verts = apply_trans(vox_mri_t, verts)
for _ in self._iter_views("vol"):
actor, _ = self._renderer.mesh(
*verts.T,
triangles=triangles,
color=color,
opacity=alpha,
reset_camera=False,
render=False,
)
self._add_actor("volume_labels", actor)
if legend or isinstance(legend, dict):
# use empty kwargs for legend = True
legend = legend if isinstance(legend, dict) else dict()
self._renderer.plotter.add_legend(list(zip(labels, colors)), **legend)
self._renderer._update()
def remove_volume_labels(self):
"""Remove the volume labels from the rendered scene."""
self._remove("volume_labels", render=True)
self._renderer.plotter.remove_legend()
@fill_doc
def add_foci(
self,
coords,
coords_as_verts=False,
map_surface=None,
scale_factor=1,
color="white",
alpha=1,
name=None,
hemi=None,
resolution=50,
):
"""Add spherical foci, possibly mapping to displayed surf.
The foci spheres can be displayed at the coordinates given, or
mapped through a surface geometry. In other words, coordinates
from a volume-based analysis in MNI space can be displayed on an
inflated average surface by finding the closest vertex on the
white surface and mapping to that vertex on the inflated mesh.
Parameters
----------
coords : ndarray, shape (n_coords, 3)
Coordinates in stereotaxic space (default) or array of
vertex ids (with ``coord_as_verts=True``).
coords_as_verts : bool
Whether the coords parameter should be interpreted as vertex ids.
map_surface : str | None
Surface to project the coordinates to, or None to use raw coords.
When set to a surface, each foci is positioned at the closest
vertex in the mesh.
scale_factor : float
Controls the size of the foci spheres (relative to 1cm).
%(color_matplotlib)s
%(alpha)s Default is 1.
name : str
Internal name to use.
hemi : str | None
If None, it is assumed to belong to the hemisphere being
shown. If two hemispheres are being shown, an error will
be thrown.
resolution : int
The resolution of the spheres.
"""
hemi = self._check_hemi(hemi, extras=["vol"])
# Figure out how to interpret the first parameter
if coords_as_verts:
coords = self.geo[hemi].coords[coords]
map_surface = None
# Possibly map the foci coords through a surface
if map_surface is not None:
foci_surf = _Surface(
self._subject,
hemi,
map_surface,
self._subjects_dir,
offset=0,
units=self._units,
x_dir=self._rigid[0, :3],
)
foci_surf.load_geometry()
foci_vtxs = np.argmin(cdist(foci_surf.coords, coords), axis=0)
coords = self.geo[hemi].coords[foci_vtxs]
# Convert the color code
color = _to_rgb(color)
if self._units == "m":
scale_factor = scale_factor / 1000.0
for _, _, v in self._iter_views(hemi):
self._renderer.sphere(
center=coords,
color=color,
scale=(10.0 * scale_factor),
opacity=alpha,
resolution=resolution,
)
self._set_camera(**views_dicts[hemi][v])
self._renderer._update()
# Store the foci in the Brain._data dictionary
data_foci = coords
if "foci" in self._data.get(hemi, []):
data_foci = np.vstack((self._data[hemi]["foci"], data_foci))
self._data[hemi] = self._data.get(hemi, dict()) # no data added yet
self._data[hemi]["foci"] = data_foci
@verbose
def add_sensors(
self,
info,
trans,
meg=None,
eeg="original",
fnirs=True,
ecog=True,
seeg=True,
dbs=True,
max_dist=0.004,
*,
sensor_colors=None,
sensor_scales=None,
verbose=None,
):
"""Add mesh objects to represent sensor positions.
Parameters
----------
%(info_not_none)s
%(trans_not_none)s
%(meg)s
%(eeg)s
%(fnirs)s
%(ecog)s
%(seeg)s
%(dbs)s
%(max_dist_ieeg)s
%(sensor_colors)s
.. versionadded:: 1.6
%(sensor_scales)s
.. versionadded:: 1.9
%(verbose)s
Notes
-----
.. versionadded:: 0.24
"""
from ...preprocessing.ieeg._projection import _project_sensors_onto_inflated
_validate_type(info, Info, "info")
meg, eeg, fnirs, warn_meg, sensor_alpha = _handle_sensor_types(meg, eeg, fnirs)
picks = pick_types(
info,
meg=("sensors" in meg),
ref_meg=("ref" in meg),
eeg=(len(eeg) > 0),
ecog=ecog,
seeg=seeg,
dbs=dbs,
fnirs=(len(fnirs) > 0),
)
head_mri_t = _get_trans(trans, "head", "mri", allow_none=False)[0]
if self._surf in ("inflated", "flat"):
for modality, check in dict(seeg=seeg, ecog=ecog).items():
if pick_types(info, **{modality: check}).size > 0:
info = _project_sensors_onto_inflated(
info.copy(),
head_mri_t,
subject=self._subject,
subjects_dir=self._subjects_dir,
picks=modality,
max_dist=max_dist,
flat=self._surf == "flat",
)
del trans
# get transforms to "mri" window
to_cf_t = _get_transforms_to_coord_frame(info, head_mri_t, coord_frame="mri")
if pick_types(info, eeg=True, exclude=()).size > 0 and "projected" in eeg:
head_surf = _get_head_surface("seghead", self._subject, self._subjects_dir)
else:
head_surf = None
# Do the main plotting
for _ in self._iter_views("vol"):
if picks.size > 0:
sensors_actors = _plot_sensors_3d(
self._renderer,
info,
to_cf_t,
picks,
meg,
eeg,
fnirs,
warn_meg,
head_surf,
self._units,
sensor_alpha=sensor_alpha,
sensor_colors=sensor_colors,
sensor_scales=sensor_scales,
)
# sensors_actors can still be None
for item, actors in (sensors_actors or {}).items():
for actor in actors:
self._add_actor(item, actor)
if "helmet" in meg and pick_types(info, meg=True).size > 0:
actor, _, _ = _plot_helmet(
self._renderer,
info,
to_cf_t,
head_mri_t,
"mri",
alpha=sensor_alpha["meg_helmet"],
scale=1 if self._units == "m" else 1e3,
)
self._add_actor("helmet", actor)
self._renderer._update()
def remove_sensors(self, kind=None):
"""Remove sensors from the rendered scene.
Parameters
----------
kind : str | list | None
If None, removes all sensor-related data including the helmet.
Can be "meg", "eeg", "fnirs", "ecog", "seeg", "dbs" or "helmet"
to remove that item.
"""
all_kinds = ("meg", "eeg", "fnirs", "ecog", "seeg", "dbs", "helmet")
if kind is None:
for item in all_kinds:
self._remove(item, render=False)
else:
if isinstance(kind, str):
kind = [kind]
for this_kind in kind:
_check_option("kind", this_kind, all_kinds)
self._remove(this_kind, render=False)
self._renderer._update()
def add_text(
self,
x,
y,
text,
name=None,
color=None,
opacity=1.0,
row=0,
col=0,
font_size=None,
justification=None,
):
"""Add a text to the visualization.
Parameters
----------
x : float
X coordinate.
y : float
Y coordinate.
text : str
Text to add.
name : str
Name of the text (text label can be updated using update_text()).
color : tuple
Color of the text. Default is the foreground color set during
initialization (default is black or white depending on the
background color).
opacity : float
Opacity of the text (default 1.0).
row : int | None
Row index of which brain to use. Default is the top row.
col : int | None
Column index of which brain to use. Default is the left-most
column.
font_size : float | None
The font size to use.
justification : str | None
The text justification.
"""
_validate_type(name, (str, None), "name")
name = text if name is None else name
if "text" in self._actors and name in self._actors["text"]:
raise ValueError(f"Text with the name {name} already exists")
if color is None:
color = self._fg_color
for ri, ci, _ in self._iter_views("vol"):
if (row is None or row == ri) and (col is None or col == ci):
actor = self._renderer.text2d(
x_window=x,
y_window=y,
text=text,
color=color,
size=font_size,
justification=justification,
)
if "text" not in self._actors:
self._actors["text"] = dict()
self._actors["text"][name] = actor
def remove_text(self, name=None):
"""Remove text from the rendered scene.
Parameters
----------
name : str | None
Remove specific text by name. If None, all text will be removed.
"""
_validate_type(name, (str, None), "name")
if name is None:
for actor in self._actors["text"].values():
self._renderer.plotter.remove_actor(actor, render=False)
self._actors.pop("text")
else:
names = [None]
if "text" in self._actors:
names += list(self._actors["text"].keys())
_check_option("name", name, names)
self._renderer.plotter.remove_actor(
self._actors["text"][name], render=False
)
self._actors["text"].pop(name)
self._renderer._update()
def _configure_label_time_course(self):
from ...label import read_labels_from_annot
if not self.show_traces:
return
if self.mpl_canvas is None:
self._configure_mplcanvas()
else:
self.clear_glyphs()
self.traces_mode = "label"
self.add_annotation(self.annot, color="w", alpha=0.75)
# now plot the time line
self.plot_time_line(update=False)
self.mpl_canvas.update_plot()
for hemi in self._hemis:
labels = read_labels_from_annot(
subject=self._subject,
parc=self.annot,
hemi=hemi,
subjects_dir=self._subjects_dir,
)
self._vertex_to_label_id[hemi] = np.full(self.geo[hemi].coords.shape[0], -1)
self._annotation_labels[hemi] = labels
for idx, label in enumerate(labels):
self._vertex_to_label_id[hemi][label.vertices] = idx
@fill_doc
def add_annotation(
self, annot, borders=True, alpha=1, hemi=None, remove_existing=True, color=None
):
"""Add an annotation file.
Parameters
----------
annot : str | tuple
Either path to annotation file or annotation name. Alternatively,
the annotation can be specified as a ``(labels, ctab)`` tuple per
hemisphere, i.e. ``annot=(labels, ctab)`` for a single hemisphere
or ``annot=((lh_labels, lh_ctab), (rh_labels, rh_ctab))`` for both
hemispheres. ``labels`` and ``ctab`` should be arrays as returned
by :func:`nibabel.freesurfer.io.read_annot`.
borders : bool | int
Show only label borders. If int, specify the number of steps
(away from the true border) along the cortical mesh to include
as part of the border definition.
%(alpha)s Default is 1.
hemi : str | None
If None, it is assumed to belong to the hemisphere being
shown. If two hemispheres are being shown, data must exist
for both hemispheres.
remove_existing : bool
If True (default), remove old annotations.
color : matplotlib-style color code
If used, show all annotations in the same (specified) color.
Probably useful only when showing annotation borders.
"""
from ...label import _read_annot
hemis = self._check_hemis(hemi)
# Figure out where the data is coming from
if _path_like(annot):
if os.path.isfile(annot):
filepath = _check_fname(annot, overwrite="read")
file_hemi, annot = filepath.name.split(".", 1)
if len(hemis) > 1:
if file_hemi == "lh":
filepaths = [filepath, filepath.parent / ("rh." + annot)]
elif file_hemi == "rh":
filepaths = [filepath.parent / ("lh." + annot), filepath]
else:
raise RuntimeError(
"To add both hemispheres simultaneously, filename must "
'begin with "lh." or "rh."'
)
else:
filepaths = [filepath]
else:
filepaths = []
for hemi in hemis:
filepath = op.join(
self._subjects_dir,
self._subject,
"label",
".".join([hemi, annot, "annot"]),
)
if not os.path.exists(filepath):
raise ValueError(f"Annotation file {filepath} does not exist")
filepaths += [filepath]
annots = []
for hemi, filepath in zip(hemis, filepaths):
# Read in the data
labels, cmap, _ = _read_annot(filepath)
annots.append((labels, cmap))
else:
annots = [annot] if len(hemis) == 1 else annot
annot = "annotation"
for hemi, (labels, cmap) in zip(hemis, annots):
# Maybe zero-out the non-border vertices
self._to_borders(labels, hemi, borders)
# Handle null labels properly
cmap[:, 3] = 255
bgcolor = np.round(np.array(self._brain_color) * 255).astype(int)
bgcolor[-1] = 0
cmap[cmap[:, 4] < 0, 4] += 2**24 # wrap to positive
cmap[cmap[:, 4] <= 0, :4] = bgcolor
if np.any(labels == 0) and not np.any(cmap[:, -1] <= 0):
cmap = np.vstack((cmap, np.concatenate([bgcolor, [0]])))
# Set label ids sensibly
order = np.argsort(cmap[:, -1])
cmap = cmap[order]
ids = np.searchsorted(cmap[:, -1], labels)
cmap = cmap[:, :4]
# Set the alpha level
alpha_vec = cmap[:, 3]
alpha_vec[alpha_vec > 0] = alpha * 255
# Override the cmap when a single color is used
if color is not None:
rgb = np.round(np.multiply(_to_rgb(color), 255))
cmap[:, :3] = rgb.astype(cmap.dtype)
ctable = cmap.astype(np.float64)
for _ in self._iter_views(hemi):
mesh = self._layered_meshes[hemi]
mesh.add_overlay(
scalars=ids,
colormap=ctable,
rng=[np.min(ids), np.max(ids)],
opacity=alpha,
name=annot,
)
self._annots[hemi].append(annot)
if not self.time_viewer or self.traces_mode == "vertex":
self._renderer._set_colormap_range(
mesh._actor, cmap.astype(np.uint8), None
)
self._renderer._update()
def close(self):
"""Close all figures and cleanup data structure."""
self._closed = True
self._renderer.close()
def show(self):
"""Display the window."""
self._renderer.show()
@fill_doc
def get_view(self, row=0, col=0, *, align=True):
"""Get the camera orientation for a given subplot display.
Parameters
----------
row : int
The row to use, default is the first one.
col : int
The column to check, the default is the first one.
%(align_view)s
Returns
-------
%(roll)s
%(distance)s
%(azimuth)s
%(elevation)s
%(focalpoint)s
"""
row = _ensure_int(row, "row")
col = _ensure_int(col, "col")
rigid = self._rigid if align else None
for h in self._hemis:
for ri, ci, _ in self._iter_views(h):
if (row == ri) and (col == ci):
return self._renderer.get_camera(rigid=rigid)
return (None,) * 5
@verbose
def show_view(
self,
view=None,
roll=None,
distance=None,
*,
row=None,
col=None,
hemi=None,
align=True,
azimuth=None,
elevation=None,
focalpoint=None,
update=True,
verbose=None,
):
"""Orient camera to display view.
Parameters
----------
%(view)s
%(roll)s
%(distance)s
row : int | None
The row to set. Default all rows.
col : int | None
The column to set. Default all columns.
hemi : str | None
Which hemi to use for view lookup (when in "both" mode).
%(align_view)s
%(azimuth)s
%(elevation)s
%(focalpoint)s
%(brain_update)s
.. versionadded:: 1.6
%(verbose)s
Notes
-----
The builtin string views are the following perspectives, based on the
:term:`RAS` convention. If not otherwise noted, the view will have the
top of the brain (superior, +Z) in 3D space shown upward in the 2D
perspective:
``'lateral'``
From the left or right side such that the lateral (outside)
surface of the given hemisphere is visible.
``'medial'``
From the left or right side such that the medial (inside)
surface of the given hemisphere is visible (at least when in split
or single-hemi mode).
``'rostral'``
From the front.
``'caudal'``
From the rear.
``'dorsal'``
From above, with the front of the brain pointing up.
``'ventral'``
From below, with the front of the brain pointing up.
``'frontal'``
From the front and slightly lateral, with the brain slightly
tilted forward (yielding a view from slightly above).
``'parietal'``
From the rear and slightly lateral, with the brain slightly tilted
backward (yielding a view from slightly above).
``'axial'``
From above with the brain pointing up (same as ``'dorsal'``).
``'sagittal'``
From the right side.
``'coronal'``
From the rear.
Three letter abbreviations (e.g., ``'lat'``) of all of the above are
also supported.
"""
_validate_type(row, ("int-like", None), "row")
_validate_type(col, ("int-like", None), "col")
hemi = self._hemi if hemi is None else hemi
if hemi == "split":
if (
self._view_layout == "vertical"
and col == 1
or self._view_layout == "horizontal"
and row == 1
):
hemi = "rh"
else:
hemi = "lh"
_validate_type(view, (str, None), "view")
view_params = dict(
azimuth=azimuth,
elevation=elevation,
roll=roll,
distance=distance,
focalpoint=focalpoint,
)
if view is not None: # view_params take precedence
view_params = {
param: val for param, val in view_params.items() if val is not None
} # no overwriting with None
view_params = dict(views_dicts[hemi].get(view), **view_params)
for h in self._hemis:
for ri, ci, _ in self._iter_views(h):
if (row is None or row == ri) and (col is None or col == ci):
self._set_camera(**view_params, align=align)
if update:
self._renderer._update()
def _set_camera(
self,
*,
distance=None,
focalpoint=None,
update=False,
align=True,
verbose=None,
**kwargs,
):
# Wrap to self._renderer.set_camera safely, always passing self._rigid
# and using better no-op-like defaults
return self._renderer.set_camera(
distance=distance,
focalpoint=focalpoint,
update=update,
rigid=self._rigid if align else None,
**kwargs,
)
def reset_view(self):
"""Reset the camera."""
for h in self._hemis:
for _, _, v in self._iter_views(h):
self._set_camera(**views_dicts[h][v])
self._renderer._update()
def save_image(self, filename=None, mode="rgb"):
"""Save view from all panels to disk.
Parameters
----------
filename : path-like
Path to new image file.
mode : str
Either ``'rgb'`` or ``'rgba'`` for values to return.
"""
if filename is None:
filename = _generate_default_filename(".png")
_save_ndarray_img(filename, self.screenshot(mode=mode, time_viewer=True))
@fill_doc
def screenshot(self, mode="rgb", time_viewer=False):
"""Generate a screenshot of current view.
Parameters
----------
mode : str
Either ``'rgb'`` or ``'rgba'`` for values to return.
%(time_viewer_brain_screenshot)s
Returns
-------
screenshot : array
Image pixel values.
"""
n_channels = 3 if mode == "rgb" else 4
img = self._renderer.screenshot(mode)
logger.debug(f"Got screenshot of size {img.shape}")
if (
time_viewer
and self.time_viewer
and self.show_traces
and not self.separate_canvas
):
from matplotlib.image import imread
canvas = self.mpl_canvas.fig.canvas
canvas.draw_idle()
fig = self.mpl_canvas.fig
with BytesIO() as output:
# Need to pass dpi here so it uses the physical (HiDPI) DPI
# rather than logical DPI when saving in most cases.
# But when matplotlib uses HiDPI and VTK doesn't
# (e.g., macOS w/Qt 5.14+ and VTK9) then things won't work,
# so let's just calculate the DPI we need to get
# the correct size output based on the widths being equal
size_in = fig.get_size_inches()
dpi = fig.get_dpi()
want_size = tuple(x * dpi for x in size_in)
n_pix = want_size[0] * want_size[1]
logger.debug(
f"Saving figure of size {size_in} @ {dpi} DPI "
f"({want_size} = {n_pix} pixels)"
)
# Sometimes there can be off-by-one errors here (e.g.,
# if in mpl int() rather than int(round()) is used to
# compute the number of pixels) so rather than use "raw"
# format and try to reshape ourselves, just write to PNG
# and read it, which has the dimensions encoded for us.
fig.savefig(
output,
dpi=dpi,
format="png",
facecolor=self._bg_color,
edgecolor="none",
)
output.seek(0)
trace_img = imread(output, format="png")[:, :, :n_channels]
trace_img = np.clip(np.round(trace_img * 255), 0, 255).astype(np.uint8)
bgcolor = np.array(self._brain_color[:n_channels]) / 255
img = concatenate_images(
[img, trace_img], bgcolor=bgcolor, n_channels=n_channels
)
return img
@fill_doc
def update_lut(self, fmin=None, fmid=None, fmax=None, alpha=None):
"""Update the range of the color map.
Parameters
----------
%(fmin_fmid_fmax)s
%(alpha)s
"""
publish(
self,
ColormapRange(
kind="distributed_source_power",
fmin=fmin,
fmid=fmid,
fmax=fmax,
alpha=alpha,
),
)
@fill_doc
def _update_colormap_range(self, fmin=None, fmid=None, fmax=None, alpha=None):
"""Update the range of the color map.
Parameters
----------
%(fmin_fmid_fmax)s
%(alpha)s
"""
args = f"{fmin}, {fmid}, {fmax}, {alpha}"
logger.debug(f"Updating LUT with {args}")
center = self._data["center"]
colormap = self._data["colormap"]
transparent = self._data["transparent"]
lims = {key: self._data[key] for key in ("fmin", "fmid", "fmax")}
_update_monotonic(lims, fmin=fmin, fmid=fmid, fmax=fmax)
assert all(val is not None for val in lims.values())
self._data.update(lims)
self._data["ctable"] = np.round(
calculate_lut(
colormap, alpha=1.0, center=center, transparent=transparent, **lims
)
* 255
).astype(np.uint8)
# update our values
rng = self._cmap_range
ctable = self._data["ctable"]
for hemi in ["lh", "rh", "vol"]:
hemi_data = self._data.get(hemi)
if hemi_data is not None:
if hemi in self._layered_meshes:
mesh = self._layered_meshes[hemi]
mesh.update_overlay(
name="data",
colormap=self._data["ctable"],
opacity=alpha,
rng=rng,
)
self._renderer._set_colormap_range(
mesh._actor, ctable, self._scalar_bar, rng, self._brain_color
)
grid_volume_pos = hemi_data.get("grid_volume_pos")
grid_volume_neg = hemi_data.get("grid_volume_neg")
for grid_volume in (grid_volume_pos, grid_volume_neg):
if grid_volume is not None:
self._renderer._set_volume_range(
grid_volume,
ctable,
hemi_data["alpha"],
self._scalar_bar,
rng,
)
glyph_actor = hemi_data.get("glyph_actor")
if glyph_actor is not None:
for glyph_actor_ in glyph_actor:
self._renderer._set_colormap_range(
glyph_actor_, ctable, self._scalar_bar, rng
)
self._renderer._update()
def set_data_smoothing(self, n_steps):
"""Set the number of smoothing steps.
Parameters
----------
n_steps : int
Number of smoothing steps.
"""
from ...morph import _hemi_morph
for hemi in ["lh", "rh"]:
hemi_data = self._data.get(hemi)
if hemi_data is not None:
if len(hemi_data["array"]) >= self.geo[hemi].x.shape[0]:
continue
vertices = hemi_data["vertices"]
if vertices is None:
raise ValueError(
f"len(data) < nvtx ({len(hemi_data)} < "
f"{self.geo[hemi].x.shape[0]}): the vertices "
"parameter must not be None"
)
morph_n_steps = "nearest" if n_steps == -1 else n_steps
with use_log_level(False):
smooth_mat = _hemi_morph(
self.geo[hemi].orig_faces,
np.arange(len(self.geo[hemi].coords)),
vertices,
morph_n_steps,
maps=None,
warn=False,
)
self._data[hemi]["smooth_mat"] = smooth_mat
self._update_current_time_idx(self._data["time_idx"])
self._data["smoothing_steps"] = n_steps
@property
def _n_times(self):
return len(self._times) if self._times is not None else None
@property
def time_interpolation(self):
"""The interpolation mode."""
return self._time_interpolation
@fill_doc
def set_time_interpolation(self, interpolation):
"""Set the interpolation mode.
Parameters
----------
%(interpolation_brain_time)s
"""
self._time_interpolation = _check_option(
"interpolation",
interpolation,
("linear", "nearest", "zero", "slinear", "quadratic", "cubic"),
)
self._time_interp_funcs = dict()
self._time_interp_inv = None
if self._times is not None:
idx = np.arange(self._n_times)
for hemi in ["lh", "rh", "vol"]:
hemi_data = self._data.get(hemi)
if hemi_data is not None:
array = hemi_data["array"]
self._time_interp_funcs[hemi] = _safe_interp1d(
idx,
array,
self._time_interpolation,
axis=-1,
assume_sorted=True,
)
self._time_interp_inv = _safe_interp1d(idx, self._times)
def _update_current_time_idx(self, time_idx):
"""Update all widgets in the figure to reflect a new time point.
Parameters
----------
time_idx : int | float
The time index to use. Can be a float to use interpolation
between indices.
"""
self._current_act_data = dict()
time_actor = self._data.get("time_actor", None)
time_label = self._data.get("time_label", None)
for hemi in ["lh", "rh", "vol"]:
hemi_data = self._data.get(hemi)
if hemi_data is not None:
array = hemi_data["array"]
# interpolate in time
vectors = None
if array.ndim == 1:
act_data = array
self._current_time = 0
else:
act_data = self._time_interp_funcs[hemi](time_idx)
self._current_time = self._time_interp_inv(time_idx)
if array.ndim == 3:
vectors = act_data
act_data = np.linalg.norm(act_data, axis=1)
self._current_time = self._time_interp_inv(time_idx)
self._current_act_data[hemi] = act_data
if time_actor is not None and time_label is not None:
time_actor.SetInput(time_label(self._current_time))
# update the volume interpolation
grid = hemi_data.get("grid")
if grid is not None:
vertices = self._data["vol"]["vertices"]
values = self._current_act_data["vol"]
rng = self._cmap_range
fill = 0 if self._data["center"] is not None else rng[0]
grid.cell_data["values"].fill(fill)
# XXX for sided data, we probably actually need two
# volumes as composite/MIP needs to look at two
# extremes... for now just use abs. Eventually we can add
# two volumes if we want.
grid.cell_data["values"][vertices] = values
# interpolate in space
smooth_mat = hemi_data.get("smooth_mat")
if smooth_mat is not None:
act_data = smooth_mat.dot(act_data)
# update the mesh scalar values
if hemi in self._layered_meshes:
mesh = self._layered_meshes[hemi]
if "data" in mesh._overlays:
mesh.update_overlay(name="data", scalars=act_data)
else:
mesh.add_overlay(
scalars=act_data,
colormap=self._data["ctable"],
rng=self._cmap_range,
opacity=None,
name="data",
)
# update the glyphs
if vectors is not None:
self._update_glyphs(hemi, vectors)
self._data["time_idx"] = time_idx
self._renderer._update()
def set_time_point(self, time_idx):
"""Set the time point to display (can be a float to interpolate).
Parameters
----------
time_idx : int | float
The time index to use. Can be a float to use interpolation
between indices.
"""
if self._times is None:
raise ValueError("Cannot set time when brain has no defined times.")
elif 0 <= time_idx <= len(self._times):
publish(self, TimeChange(time=self._time_interp_inv(time_idx)))
else:
raise ValueError(
f"Requested time point ({time_idx}) is outside the range of "
f"available time points (0-{len(self._times)})."
)
def set_time(self, time):
"""Set the time to display (in seconds).
Parameters
----------
time : float
The time to show, in seconds.
"""
if self._times is None:
raise ValueError("Cannot set time when brain has no defined times.")
elif min(self._times) <= time <= max(self._times):
publish(self, TimeChange(time=time))
else:
raise ValueError(
f"Requested time ({time} s) is outside the range of "
f"available times ({min(self._times)}-{max(self._times)} s)."
)
def _update_glyphs(self, hemi, vectors):
hemi_data = self._data.get(hemi)
assert hemi_data is not None
vertices = hemi_data["vertices"]
vector_alpha = self._data["vector_alpha"]
scale_factor = self._data["scale_factor"]
vertices = slice(None) if vertices is None else vertices
x, y, z = np.array(self.geo[hemi].coords)[vertices].T
if hemi_data["glyph_actor"] is None:
add = True
hemi_data["glyph_actor"] = list()
else:
add = False
count = 0
for _ in self._iter_views(hemi):
if hemi_data["glyph_dataset"] is None:
glyph_mapper, glyph_dataset = self._renderer.quiver3d(
x,
y,
z,
vectors[:, 0],
vectors[:, 1],
vectors[:, 2],
color=None,
mode="2darrow",
scale_mode="vector",
scale=scale_factor,
opacity=vector_alpha,
)
hemi_data["glyph_dataset"] = glyph_dataset
hemi_data["glyph_mapper"] = glyph_mapper
else:
glyph_dataset = hemi_data["glyph_dataset"]
glyph_dataset.point_data["vec"] = vectors
glyph_mapper = hemi_data["glyph_mapper"]
if add:
glyph_actor = self._renderer._actor(glyph_mapper)
prop = glyph_actor.GetProperty()
prop.SetLineWidth(2.0)
prop.SetOpacity(vector_alpha)
self._renderer.plotter.add_actor(glyph_actor, render=False)
hemi_data["glyph_actor"].append(glyph_actor)
else:
glyph_actor = hemi_data["glyph_actor"][count]
count += 1
self._renderer._set_colormap_range(
actor=glyph_actor,
ctable=self._data["ctable"],
scalar_bar=None,
rng=self._cmap_range,
)
@property
def _cmap_range(self):
dt_max = self._data["fmax"]
if self._data["center"] is None:
dt_min = self._data["fmin"]
else:
dt_min = -1 * dt_max
rng = [dt_min, dt_max]
return rng
def _update_fscale(self, fscale):
"""Scale the colorbar points."""
fmin = self._data["fmin"] * fscale
fmid = self._data["fmid"] * fscale
fmax = self._data["fmax"] * fscale
self.update_lut(fmin=fmin, fmid=fmid, fmax=fmax)
def _update_auto_scaling(self, restore=False):
user_clim = self._data["clim"]
if user_clim is not None and "lims" in user_clim:
allow_pos_lims = False
else:
allow_pos_lims = True
if user_clim is not None and restore:
clim = user_clim
else:
clim = "auto"
colormap = self._data["colormap"]
transparent = self._data["transparent"]
mapdata = _process_clim(
clim,
colormap,
transparent,
np.concatenate(list(self._current_act_data.values())),
allow_pos_lims,
)
diverging = "pos_lims" in mapdata["clim"]
colormap = mapdata["colormap"]
scale_pts = mapdata["clim"]["pos_lims" if diverging else "lims"]
transparent = mapdata["transparent"]
del mapdata
fmin, fmid, fmax = scale_pts
center = 0.0 if diverging else None
self._data["center"] = center
self._data["colormap"] = colormap
self._data["transparent"] = transparent
self.update_lut(fmin=fmin, fmid=fmid, fmax=fmax)
def _to_time_index(self, value):
"""Return the interpolated time index of the given time value."""
time = self._data["time"]
value = np.interp(value, time, np.arange(len(time)))
return value
@property
def data(self):
"""Data used by time viewer and color bar widgets."""
return self._data
@property
def labels(self):
return self._labels
@property
def views(self):
return self._views
@property
def hemis(self):
return self._hemis
def _save_movie(
self,
filename,
time_dilation=4.0,
tmin=None,
tmax=None,
framerate=24,
interpolation=None,
codec=None,
bitrate=None,
callback=None,
time_viewer=False,
**kwargs,
):
import imageio
with self._renderer._disabled_interaction():
images = self._make_movie_frames(
time_dilation,
tmin,
tmax,
framerate,
interpolation,
callback,
time_viewer,
)
# find imageio FFMPEG parameters
if "fps" not in kwargs:
kwargs["fps"] = framerate
if codec is not None:
kwargs["codec"] = codec
if bitrate is not None:
kwargs["bitrate"] = bitrate
# when using GIF we need to convert FPS to duration in milliseconds for Pillow
if str(filename).endswith(".gif"):
kwargs["duration"] = 1000 * len(images) / kwargs.pop("fps")
imageio.mimwrite(filename, images, **kwargs)
def _save_movie_tv(
self,
filename,
time_dilation=4.0,
tmin=None,
tmax=None,
framerate=24,
interpolation=None,
codec=None,
bitrate=None,
callback=None,
time_viewer=False,
**kwargs,
):
def frame_callback(frame, n_frames):
if frame == n_frames:
# On the ImageIO step
self.status_msg.set_value(f"Saving with ImageIO: {filename}")
self.status_msg.show()
self.status_progress.hide()
self._renderer._status_bar_update()
else:
self.status_msg.set_value(
f"Rendering images (frame {frame + 1} / {n_frames}) ..."
)
self.status_msg.show()
self.status_progress.show()
self.status_progress.set_range([0, n_frames - 1])
self.status_progress.set_value(frame)
self.status_progress.update()
self.status_msg.update()
self._renderer._status_bar_update()
# set cursor to busy
default_cursor = self._renderer._window_get_cursor()
self._renderer._window_set_cursor(
self._renderer._window_new_cursor("WaitCursor")
)
try:
self._save_movie(
filename,
time_dilation,
tmin,
tmax,
framerate,
interpolation,
codec,
bitrate,
frame_callback,
time_viewer,
**kwargs,
)
except (Exception, KeyboardInterrupt):
warn("Movie saving aborted:\n" + traceback.format_exc())
finally:
self._renderer._window_set_cursor(default_cursor)
@fill_doc
def save_movie(
self,
filename=None,
time_dilation=4.0,
tmin=None,
tmax=None,
framerate=24,
interpolation=None,
codec=None,
bitrate=None,
callback=None,
time_viewer=False,
**kwargs,
):
"""Save a movie (for data with a time axis).
The movie is created through the :mod:`imageio` module. The format is
determined by the extension, and additional options can be specified
through keyword arguments that depend on the format, see
:doc:`imageio's format page <imageio:formats/index>`.
.. Warning::
This method assumes that time is specified in seconds when adding
data. If time is specified in milliseconds this will result in
movies 1000 times longer than expected.
Parameters
----------
filename : str
Path at which to save the movie. The extension determines the
format (e.g., ``'*.mov'``, ``'*.gif'``, ...; see the :mod:`imageio`
documentation for available formats).
time_dilation : float
Factor by which to stretch time (default 4). For example, an epoch
from -100 to 600 ms lasts 700 ms. With ``time_dilation=4`` this
would result in a 2.8 s long movie.
tmin : float
First time point to include (default: all data).
tmax : float
Last time point to include (default: all data).
framerate : float
Framerate of the movie (frames per second, default 24).
%(interpolation_brain_time)s
If None, it uses the current ``brain.interpolation``,
which defaults to ``'nearest'``. Defaults to None.
codec : str | None
The codec to use.
bitrate : float | None
The bitrate to use.
callback : callable | None
A function to call on each iteration. Useful for status message
updates. It will be passed keyword arguments ``frame`` and
``n_frames``.
%(time_viewer_brain_screenshot)s
**kwargs : dict
Specify additional options for :mod:`imageio`.
"""
if filename is None:
filename = _generate_default_filename(".mp4")
func = self._save_movie_tv if self.time_viewer else self._save_movie
func(
filename,
time_dilation,
tmin,
tmax,
framerate,
interpolation,
codec,
bitrate,
callback,
time_viewer,
**kwargs,
)
def _make_movie_frames(
self, time_dilation, tmin, tmax, framerate, interpolation, callback, time_viewer
):
from math import floor
# find tmin
if tmin is None:
tmin = self._times[0]
elif tmin < self._times[0]:
raise ValueError(
f"tmin={repr(tmin)} is smaller than the first time point "
f"({repr(self._times[0])})"
)
# find indexes at which to create frames
if tmax is None:
tmax = self._times[-1]
elif tmax > self._times[-1]:
raise ValueError(
f"tmax={repr(tmax)} is greater than the latest time point "
f"({repr(self._times[-1])})"
)
n_frames = floor((tmax - tmin) * time_dilation * framerate)
times = np.arange(n_frames, dtype=float)
times /= framerate * time_dilation
times += tmin
time_idx = np.interp(times, self._times, np.arange(self._n_times))
n_times = len(time_idx)
if n_times == 0:
raise ValueError("No time points selected")
logger.debug(f"Save movie for time points/samples\n{times}\n{time_idx}")
# Sometimes the first screenshot is rendered with a different
# resolution on OS X
self.screenshot(time_viewer=time_viewer)
old_mode = self.time_interpolation
if interpolation is not None:
self.set_time_interpolation(interpolation)
try:
images = [
self.screenshot(time_viewer=time_viewer)
for _ in self._iter_time(time_idx, callback)
]
finally:
self.set_time_interpolation(old_mode)
if callback is not None:
callback(frame=len(time_idx), n_frames=len(time_idx))
return images
def _iter_time(self, time_idx, callback):
"""Iterate through time points, then reset to current time.
Parameters
----------
time_idx : array_like
Time point indexes through which to iterate.
callback : callable | None
Callback to call before yielding each frame.
Yields
------
idx : int | float
Current index.
Notes
-----
Used by movie and image sequence saving functions.
"""
current_time_idx = self._data["time_idx"]
for ii, idx in enumerate(time_idx):
self.set_time_point(idx)
if callback is not None:
callback(frame=ii, n_frames=len(time_idx))
yield idx
# Restore original time index
self.set_time_point(current_time_idx)
def _check_stc(self, hemi, array, vertices):
from ...source_estimate import (
_BaseMixedSourceEstimate,
_BaseSourceEstimate,
_BaseSurfaceSourceEstimate,
_BaseVolSourceEstimate,
)
if isinstance(array, _BaseSourceEstimate):
stc = array
stc_surf = stc_vol = None
if isinstance(stc, _BaseSurfaceSourceEstimate):
stc_surf = stc
elif isinstance(stc, _BaseMixedSourceEstimate):
stc_surf = stc.surface() if hemi != "vol" else None
stc_vol = stc.volume() if hemi == "vol" else None
elif isinstance(stc, _BaseVolSourceEstimate):
stc_vol = stc if hemi == "vol" else None
else:
raise TypeError("stc not supported")
if stc_surf is None and stc_vol is None:
raise ValueError("No data to be added")
if stc_surf is not None:
array = getattr(stc_surf, hemi + "_data")
vertices = stc_surf.vertices[0 if hemi == "lh" else 1]
if stc_vol is not None:
array = stc_vol.data
vertices = np.concatenate(stc_vol.vertices)
else:
stc = None
return stc, array, vertices
def _check_hemi(self, hemi, extras=()):
"""Check for safe single-hemi input, returns str."""
_validate_type(hemi, (None, str), "hemi")
if hemi is None:
if self._hemi not in ["lh", "rh"]:
raise ValueError(
"hemi must not be None when both hemispheres are displayed"
)
hemi = self._hemi
_check_option("hemi", hemi, ("lh", "rh") + tuple(extras))
return hemi
def _check_hemis(self, hemi):
"""Check for safe dual or single-hemi input, returns list."""
if hemi is None:
if self._hemi not in ["lh", "rh"]:
hemi = ["lh", "rh"]
else:
hemi = [self._hemi]
elif hemi not in ["lh", "rh"]:
extra = " or None" if self._hemi in ["lh", "rh"] else ""
raise ValueError('hemi must be either "lh" or "rh"' + extra)
else:
hemi = [hemi]
return hemi
def _to_borders(self, label, hemi, borders, restrict_idx=None):
"""Convert a label/parc to borders."""
if not isinstance(borders, bool | int) or borders < 0:
raise ValueError("borders must be a bool or positive integer")
if borders:
n_vertices = label.size
edges = mesh_edges(self.geo[hemi].orig_faces)
edges = edges.tocoo()
border_edges = label[edges.row] != label[edges.col]
show = np.zeros(n_vertices, dtype=np.int64)
keep_idx = np.unique(edges.row[border_edges])
if isinstance(borders, int):
for _ in range(borders):
keep_idx = np.isin(self.geo[hemi].orig_faces.ravel(), keep_idx)
keep_idx.shape = self.geo[hemi].orig_faces.shape
keep_idx = self.geo[hemi].orig_faces[np.any(keep_idx, axis=1)]
keep_idx = np.unique(keep_idx)
if restrict_idx is not None:
keep_idx = keep_idx[np.isin(keep_idx, restrict_idx)]
show[keep_idx] = 1
label *= show
def get_picked_points(self):
"""Return the vertices of the picked points.
Returns
-------
points : list of int | None
The vertices picked by the time viewer.
"""
if hasattr(self, "time_viewer"):
return self.picked_points
def __hash__(self):
"""Hash the object."""
return self._hash
def _safe_interp1d(x, y, kind="linear", axis=-1, assume_sorted=False):
"""Work around interp1d not liking singleton dimensions."""
if y.shape[axis] == 1:
def func(x):
return np.take(y, np.zeros(np.asarray(x).shape, int), axis=axis)
return func
else:
return interp1d(x, y, kind, axis=axis, assume_sorted=assume_sorted)
def _update_limits(fmin, fmid, fmax, center, array):
if center is None:
if fmin is None:
fmin = array.min() if array.size > 0 else 0
if fmax is None:
fmax = array.max() if array.size > 0 else 1
else:
if fmin is None:
fmin = 0
if fmax is None:
fmax = np.abs(center - array).max() if array.size > 0 else 1
if fmid is None:
fmid = (fmin + fmax) / 2.0
if fmin >= fmid:
raise RuntimeError(f"min must be < mid, got {fmin:0.4g} >= {fmid:0.4g}")
if fmid >= fmax:
raise RuntimeError(f"mid must be < max, got {fmid:0.4g} >= {fmax:0.4g}")
return fmin, fmid, fmax
def _update_monotonic(lims, fmin, fmid, fmax):
if fmin is not None:
lims["fmin"] = fmin
if lims["fmax"] < fmin:
logger.debug(f" Bumping fmax = {lims['fmax']} to {fmin}")
lims["fmax"] = fmin
if lims["fmid"] < fmin:
logger.debug(f" Bumping fmid = {lims['fmid']} to {fmin}")
lims["fmid"] = fmin
assert lims["fmin"] <= lims["fmid"] <= lims["fmax"]
if fmid is not None:
lims["fmid"] = fmid
if lims["fmin"] > fmid:
logger.debug(f" Bumping fmin = {lims['fmin']} to {fmid}")
lims["fmin"] = fmid
if lims["fmax"] < fmid:
logger.debug(f" Bumping fmax = {lims['fmax']} to {fmid}")
lims["fmax"] = fmid
assert lims["fmin"] <= lims["fmid"] <= lims["fmax"]
if fmax is not None:
lims["fmax"] = fmax
if lims["fmin"] > fmax:
logger.debug(f" Bumping fmin = {lims['fmin']} to {fmax}")
lims["fmin"] = fmax
if lims["fmid"] > fmax:
logger.debug(f" Bumping fmid = {lims['fmid']} to {fmax}")
lims["fmid"] = fmax
assert lims["fmin"] <= lims["fmid"] <= lims["fmax"]
def _get_range(brain):
"""Get the data limits.
Since they may be very small (1E-10 and such), we apply a scaling factor
such that the data range lies somewhere between 0.01 and 100. This makes
for more usable sliders. When setting a value on the slider, the value is
multiplied by the scaling factor and when getting a value, this value
should be divided by the scaling factor.
"""
fmax = abs(brain._data["fmax"])
if 1e-02 <= fmax <= 1e02:
fscale_power = 0
else:
fscale_power = int(np.log10(max(fmax, np.finfo("float32").smallest_normal)))
if fscale_power < 0:
fscale_power -= 1
fscale = 10**-fscale_power
return fmax, fscale, fscale_power
class _FakeIren:
def EnterEvent(self):
pass
def MouseMoveEvent(self):
pass
def LeaveEvent(self):
pass
def SetEventInformation(self, *args, **kwargs):
pass
def CharEvent(self):
pass
def KeyPressEvent(self, *args, **kwargs):
pass
def KeyReleaseEvent(self, *args, **kwargs):
pass