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

Download this file

2531 lines (2348 with data), 105.8 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
"""Figure classes for MNE-Python's 2D plots.
Class Hierarchy
---------------
MNEFigParams Container object, attached to MNEFigure by default. Sets
close_key='escape' plus whatever other key-value pairs are
passed to its constructor.
matplotlib.figure.Figure
└ MNEFigure
├ MNEBrowseFigure Interactive figure for scrollable data.
│ Generated by:
│ - raw.plot()
│ - epochs.plot()
│ - ica.plot_sources(raw)
│ - ica.plot_sources(epochs)
├ MNEAnnotationFigure GUI for adding annotations to Raw
├ MNESelectionFigure GUI for spatial channel selection. raw.plot()
│ and epochs.plot() will generate one of these
│ alongside an MNEBrowseFigure when
│ group_by == 'selection' or 'position'
└ MNELineFigure Interactive figure for non-scrollable data.
Generated by:
- spectrum.plot()
- evoked.plot() TODO Not yet implemented
- evoked.plot_white() TODO Not yet implemented
- evoked.plot_joint() TODO Not yet implemented
"""
# Authors: The MNE-Python contributors.
# License: BSD-3-Clause
# Copyright the MNE-Python contributors.
import datetime
import platform
from collections import OrderedDict
from contextlib import contextmanager
from functools import partial
import matplotlib.pyplot as plt
import numpy as np
from matplotlib import get_backend
from matplotlib.figure import Figure
from .._fiff.pick import (
_DATA_CH_TYPES_ORDER_DEFAULT,
_DATA_CH_TYPES_SPLIT,
_EYETRACK_CH_TYPES_SPLIT,
_FNIRS_CH_TYPES_SPLIT,
_VALID_CHANNEL_TYPES,
channel_indices_by_type,
pick_types,
)
from ..fixes import _close_event
from ..utils import Bunch, _click_ch_name, check_version, logger
from ._figure import BrowserBase
from .utils import (
DraggableLine,
_events_off,
_fake_click,
_fake_keypress,
_fake_scroll,
_merge_annotations,
_set_window_title,
_validate_if_list_of_axes,
plot_sensors,
plt_show,
)
name = "matplotlib"
BACKEND = get_backend()
# CONSTANTS (inches)
ANNOTATION_FIG_PAD = 0.1
ANNOTATION_FIG_MIN_H = 2.9 # fixed part, not including radio buttons/labels
ANNOTATION_FIG_W = 5.0
ANNOTATION_FIG_CHECKBOX_COLUMN_W = 0.5
_OLD_BUTTONS = not check_version("matplotlib", "3.7")
class MNEFigure(Figure):
"""Base class for 2D figures & dialogs; wraps matplotlib.figure.Figure."""
def __init__(self, **kwargs):
from matplotlib import rcParams
# figsize is the only kwarg we pass to matplotlib Figure()
figsize = kwargs.pop("figsize", None)
super().__init__(figsize=figsize)
# things we'll almost always want
defaults = dict(
fgcolor=rcParams["axes.edgecolor"], bgcolor=rcParams["axes.facecolor"]
)
for key, value in defaults.items():
if key not in kwargs:
kwargs[key] = value
# add param object if not already added (e.g. by BrowserBase)
if not hasattr(self, "mne"):
from mne.viz._figure import BrowserParams
self.mne = BrowserParams(**kwargs)
else:
for key in [k for k in kwargs if not hasattr(self.mne, k)]:
setattr(self.mne, key, kwargs[key])
def _close(self, event=None):
"""Handle close events."""
logger.debug(f"Closing {self!r}")
# remove references from parent fig to child fig
is_child = getattr(self.mne, "parent_fig", None) is not None
is_named = getattr(self.mne, "fig_name", None) is not None
if is_child:
try:
self.mne.parent_fig.mne.child_figs.remove(self)
except ValueError:
pass # already removed (on its own, probably?)
if is_named:
setattr(self.mne.parent_fig.mne, self.mne.fig_name, None)
def _keypress(self, event):
"""Handle keypress events."""
if event.key == self.mne.close_key:
plt.close(self)
elif event.key == "f11": # full screen
self.canvas.manager.full_screen_toggle()
def _buttonpress(self, event):
"""Handle buttonpress events."""
pass
def _pick(self, event):
"""Handle matplotlib pick events."""
pass
def _resize(self, event):
"""Handle window resize events."""
pass
def _add_default_callbacks(self, **kwargs):
"""Remove some matplotlib default callbacks and add MNE-Python ones."""
# Remove matplotlib default keypress catchers
default_callbacks = list(
self.canvas.callbacks.callbacks.get("key_press_event", {})
)
for callback in default_callbacks:
self.canvas.callbacks.disconnect(callback)
# add our event callbacks
callbacks = dict(
resize_event=self._resize,
key_press_event=self._keypress,
button_press_event=self._buttonpress,
close_event=self._close,
pick_event=self._pick,
)
callbacks.update(kwargs)
callback_ids = dict()
for event, callback in callbacks.items():
callback_ids[event] = self.canvas.mpl_connect(event, callback)
# store callback references so they aren't garbage-collected
self.mne._callback_ids = callback_ids
def _get_dpi_ratio(self):
"""Get DPI ratio (to handle hi-DPI screens)."""
dpi_ratio = 1.0
for key in ("_dpi_ratio", "_device_scale"):
dpi_ratio = getattr(self.canvas, key, dpi_ratio)
return dpi_ratio
def _get_size_px(self):
"""Get figure size in pixels."""
dpi_ratio = self._get_dpi_ratio()
return self.get_size_inches() * self.dpi / dpi_ratio
def _inch_to_rel(self, dim_inches, horiz=True):
"""Convert inches to figure-relative distances."""
fig_w, fig_h = self.get_size_inches()
w_or_h = fig_w if horiz else fig_h
return dim_inches / w_or_h
class MNEAnnotationFigure(MNEFigure):
"""Interactive dialog figure for annotations."""
def _close(self, event=None):
"""Handle close events (via keypress or window [x])."""
parent = self.mne.parent_fig
# disable span selector
parent.mne.ax_main.selector.active = False
# clear hover line
parent._remove_annotation_hover_line()
# disconnect hover callback
callback_id = parent.mne._callback_ids["motion_notify_event"]
parent.canvas.callbacks.disconnect(callback_id)
# do all the other cleanup activities
super()._close(event)
def _keypress(self, event):
"""Handle keypress events."""
text = self.label.get_text()
key = event.key
if key == self.mne.close_key:
plt.close(self)
elif key == "backspace":
text = text[:-1]
elif key == "enter":
self.mne.parent_fig._add_annotation_label(event)
return
elif len(key) > 1 or key == ";": # ignore modifier keys
return
else:
text = text + key
self.label.set_text(text)
self.canvas.draw()
def _radiopress(self, event, *, draw=True):
"""Handle Radiobutton clicks for Annotation label selection."""
# update which button looks active
buttons = self.mne.radio_ax.buttons
labels = [label.get_text() for label in buttons.labels]
idx = labels.index(buttons.value_selected)
self._set_active_button(idx, draw=False)
# update click-drag rectangle color
color = self.mne.parent_fig.mne.annotation_segment_colors[labels[idx]]
selector = self.mne.parent_fig.mne.ax_main.selector
# https://github.com/matplotlib/matplotlib/issues/20618
# https://github.com/matplotlib/matplotlib/pull/20693
selector.set_props(color=color, facecolor=color)
if draw:
self.canvas.draw()
def _click_override(self, event):
"""Override MPL radiobutton click detector to use transData."""
assert _OLD_BUTTONS
ax = self.mne.radio_ax
buttons = ax.buttons
if buttons.ignore(event) or event.button != 1 or event.inaxes != ax:
return
pclicked = ax.transData.inverted().transform((event.x, event.y))
distances = {}
for i, (p, t) in enumerate(zip(buttons.circles, buttons.labels)):
if (
t.get_window_extent().contains(event.x, event.y)
or np.linalg.norm(pclicked - p.center) < p.radius
):
distances[i] = np.linalg.norm(pclicked - p.center)
if len(distances) > 0:
closest = min(distances, key=distances.get)
buttons.set_active(closest)
def _set_active_button(self, idx, *, draw=True):
"""Set active button in annotation dialog figure."""
buttons = self.mne.radio_ax.buttons
logger.debug(f"buttons: {buttons}")
logger.debug(f"active idx: {idx}")
with _events_off(buttons):
buttons.set_active(idx)
if _OLD_BUTTONS:
logger.debug(f"circles: {buttons.circles}")
for circle in buttons.circles:
circle.set_facecolor(self.mne.parent_fig.mne.bgcolor)
# active circle gets filled in, partially transparent
color = list(buttons.circles[idx].get_edgecolor())
logger.debug(f"color: {color}")
color[-1] = 0.5
buttons.circles[idx].set_facecolor(color)
if draw:
self.canvas.draw()
class MNESelectionFigure(MNEFigure):
"""Interactive dialog figure for channel selections."""
def _close(self, event=None):
"""Handle close events."""
self.mne.parent_fig.mne.child_figs.remove(self)
self.mne.fig_selection = None
# selection fig & main fig tightly integrated; closing one closes both
plt.close(self.mne.parent_fig)
def _keypress(self, event):
"""Handle keypress events."""
if event.key in ("up", "down", "b"):
self.mne.parent_fig._keypress(event)
else: # check for close key
super()._keypress(event)
def _radiopress(self, event):
"""Handle RadioButton clicks for channel selection groups."""
logger.debug(f"Got radio press: {repr(event)}")
selections_dict = self.mne.parent_fig.mne.ch_selections
buttons = self.mne.radio_ax.buttons
labels = [label.get_text() for label in buttons.labels]
this_label = buttons.value_selected
parent = self.mne.parent_fig
if this_label == "Custom" and not len(selections_dict["Custom"]):
with _events_off(buttons):
buttons.set_active(self.mne.old_selection)
return
# clicking a selection cancels butterfly mode
if parent.mne.butterfly:
logger.debug("Disabling butterfly mode")
parent._toggle_butterfly()
with _events_off(buttons):
buttons.set_active(labels.index(this_label))
parent._update_selection()
def _set_custom_selection(self):
"""Set custom selection by lasso selector."""
chs = self.lasso.selection
parent = self.mne.parent_fig
buttons = self.mne.radio_ax.buttons
if not len(chs):
return
labels = [label.get_text() for label in buttons.labels]
inds = np.isin(parent.mne.ch_names, chs)
parent.mne.ch_selections["Custom"] = inds.nonzero()[0]
buttons.set_active(labels.index("Custom"))
def _style_radio_buttons_butterfly(self):
"""Handle RadioButton state for keyboard interactions."""
# Show all radio buttons as selected when in butterfly mode
parent = self.mne.parent_fig
buttons = self.mne.radio_ax.buttons
color = buttons.activecolor if parent.mne.butterfly else parent.mne.bgcolor
if _OLD_BUTTONS:
for circle in buttons.circles:
circle.set_facecolor(color)
# when leaving butterfly mode, make most-recently-used selection active
if not parent.mne.butterfly:
with _events_off(buttons):
buttons.set_active(self.mne.old_selection)
# update the sensors too
parent._update_highlighted_sensors()
class MNEBrowseFigure(BrowserBase, MNEFigure):
"""Interactive figure with scrollbars, for data browsing."""
def __init__(self, inst, figsize, ica=None, xlabel="Time (s)", **kwargs):
from matplotlib.colors import to_rgba_array
from matplotlib.patches import Rectangle
from matplotlib.ticker import (
FixedFormatter,
FixedLocator,
FuncFormatter,
NullFormatter,
)
from matplotlib.transforms import blended_transform_factory
from matplotlib.widgets import Button
from mpl_toolkits.axes_grid1.axes_divider import make_axes_locatable
from mpl_toolkits.axes_grid1.axes_size import Fixed
self.backend_name = "matplotlib"
kwargs.update({"inst": inst, "figsize": figsize, "ica": ica, "xlabel": xlabel})
BrowserBase.__init__(self, **kwargs)
MNEFigure.__init__(self, **kwargs)
# MAIN AXES: default sizes (inches)
# XXX simpler with constrained_layout? (when it's no longer "beta")
l_margin = 1.0
r_margin = 0.1
b_margin = 0.45
t_margin = 0.25
scroll_width = 0.25
hscroll_dist = 0.25
vscroll_dist = 0.1
help_width = scroll_width * 2
# MAIN AXES: default margins (figure-relative coordinates)
left = self._inch_to_rel(l_margin - vscroll_dist - help_width)
right = 1 - self._inch_to_rel(r_margin)
bottom = self._inch_to_rel(b_margin, horiz=False)
top = 1 - self._inch_to_rel(t_margin, horiz=False)
width = right - left
height = top - bottom
position = [left, bottom, width, height]
# Main axes must be a subplot for subplots_adjust to work (so user can
# adjust margins). That's why we don't use the Divider class directly.
ax_main = self.add_subplot(1, 1, 1, position=position)
self.subplotpars.update(left=left, bottom=bottom, top=top, right=right)
div = make_axes_locatable(ax_main)
# this only gets shown in zen mode
self.mne.zen_xlabel = ax_main.set_xlabel(xlabel)
self.mne.zen_xlabel.set_visible(not self.mne.scrollbars_visible)
# make sure background color of the axis is set
if "bgcolor" in kwargs:
ax_main.set_facecolor(kwargs["bgcolor"])
# SCROLLBARS
ax_hscroll = div.append_axes(
position="bottom", size=Fixed(scroll_width), pad=Fixed(hscroll_dist)
)
ax_vscroll = div.append_axes(
position="right", size=Fixed(scroll_width), pad=Fixed(vscroll_dist)
)
ax_hscroll.get_yaxis().set_visible(False)
ax_hscroll.set_xlabel(xlabel)
ax_vscroll.set_axis_off()
# HORIZONTAL SCROLLBAR PATCHES (FOR MARKING BAD EPOCHS)
if self.mne.is_epochs:
epoch_nums = self.mne.inst.selection
for ix, _ in enumerate(epoch_nums):
start = self.mne.boundary_times[ix]
width = np.diff(self.mne.boundary_times[:2])[0]
ax_hscroll.add_patch(
Rectangle(
(start, 0),
width,
1,
color="none",
zorder=self.mne.zorder["patch"],
)
)
# both axes, major ticks: gridlines
for _ax in (ax_main, ax_hscroll):
_ax.xaxis.set_major_locator(FixedLocator(self.mne.boundary_times[1:-1]))
_ax.xaxis.set_major_formatter(NullFormatter())
grid_kwargs = dict(
color=self.mne.fgcolor, axis="x", zorder=self.mne.zorder["grid"]
)
ax_main.grid(linewidth=2, linestyle="dashed", **grid_kwargs)
ax_hscroll.grid(alpha=0.5, linewidth=0.5, linestyle="solid", **grid_kwargs)
# main axes, minor ticks: ticklabel (epoch number) for every epoch
ax_main.xaxis.set_minor_locator(FixedLocator(self.mne.midpoints))
ax_main.xaxis.set_minor_formatter(FixedFormatter(epoch_nums))
# hscroll axes, minor ticks: up to 20 ticklabels (epoch numbers)
ax_hscroll.xaxis.set_minor_locator(
FixedLocator(self.mne.midpoints, nbins=20)
)
ax_hscroll.xaxis.set_minor_formatter(
FuncFormatter(lambda x, pos: self._get_epoch_num_from_time(x))
)
# hide some ticks
ax_main.tick_params(axis="x", which="major", bottom=False)
ax_hscroll.tick_params(axis="x", which="both", bottom=False)
else:
# RAW / ICA X-AXIS TICK & LABEL FORMATTING
ax_main.xaxis.set_major_formatter(
FuncFormatter(partial(self._xtick_formatter, ax_type="main"))
)
ax_hscroll.xaxis.set_major_formatter(
FuncFormatter(partial(self._xtick_formatter, ax_type="hscroll"))
)
if self.mne.time_format != "float":
for _ax in (ax_main, ax_hscroll):
_ax.set_xlabel("Time (HH:MM:SS)")
# VERTICAL SCROLLBAR PATCHES (COLORED BY CHANNEL TYPE)
ch_order = self.mne.ch_order
for ix, pick in enumerate(ch_order):
this_color = (
self.mne.ch_color_bad
if self.mne.ch_names[pick] in self.mne.info["bads"]
else self.mne.ch_color_dict
)
if isinstance(this_color, dict):
this_color = this_color[self.mne.ch_types[pick]]
ax_vscroll.add_patch(
Rectangle(
(0, ix), 1, 1, color=this_color, zorder=self.mne.zorder["patch"]
)
)
ax_vscroll.set_ylim(len(ch_order), 0)
ax_vscroll.set_visible(not self.mne.butterfly)
# SCROLLBAR VISIBLE SELECTION PATCHES
sel_kwargs = dict(
alpha=0.3, linewidth=4, clip_on=False, edgecolor=self.mne.fgcolor
)
vsel_patch = Rectangle(
(0, 0), 1, self.mne.n_channels, facecolor=self.mne.bgcolor, **sel_kwargs
)
ax_vscroll.add_patch(vsel_patch)
hsel_facecolor = np.average(
np.vstack(
(to_rgba_array(self.mne.fgcolor), to_rgba_array(self.mne.bgcolor))
),
axis=0,
weights=(3, 1),
) # 75% foreground, 25% background
hsel_patch = Rectangle(
(self.mne.t_start, 0),
self.mne.duration,
1,
facecolor=hsel_facecolor,
**sel_kwargs,
)
ax_hscroll.add_patch(hsel_patch)
ax_hscroll.set_xlim(
self.mne.first_time,
self.mne.first_time + self.mne.n_times / self.mne.info["sfreq"],
)
# VLINE
vline_color = (0.0, 0.75, 0.0)
vline_kwargs = dict(visible=False, zorder=self.mne.zorder["vline"])
if self.mne.is_epochs:
x = np.arange(self.mne.n_epochs)
vline = ax_main.vlines(x, 0, 1, colors=vline_color, **vline_kwargs)
vline.set_transform(
blended_transform_factory(ax_main.transData, ax_main.transAxes)
)
vline_hscroll = None
else:
vline = ax_main.axvline(0, color=vline_color, **vline_kwargs)
vline_hscroll = ax_hscroll.axvline(0, color=vline_color, **vline_kwargs)
vline_text = ax_main.annotate(
"",
xy=(0, 0),
xycoords="axes fraction",
xytext=(-2, 0),
textcoords="offset points",
fontsize=10,
ha="right",
va="center",
color=vline_color,
**vline_kwargs,
)
# HELP BUTTON: initialize in the wrong spot...
ax_help = div.append_axes(
position="left", size=Fixed(help_width), pad=Fixed(vscroll_dist)
)
# HELP BUTTON: ...move it down by changing its locator
loc = div.new_locator(nx=0, ny=0)
ax_help.set_axes_locator(loc)
# HELP BUTTON: make it a proper button
with _patched_canvas(ax_help.figure):
self.mne.button_help = Button(ax_help, "Help")
# PROJ BUTTON
ax_proj = None
if len(self.mne.projs) and not self.mne.inst.proj:
proj_button_pos = [
1 - self._inch_to_rel(r_margin + scroll_width), # left
self._inch_to_rel(b_margin, horiz=False), # bottom
self._inch_to_rel(scroll_width), # width
self._inch_to_rel(scroll_width, horiz=False), # height
]
loc = div.new_locator(nx=4, ny=0)
ax_proj = self.add_axes(proj_button_pos)
ax_proj.set_axes_locator(loc)
with _patched_canvas(ax_help.figure):
self.mne.button_proj = Button(ax_proj, "Prj")
# INIT TRACES
self.mne.trace_kwargs = dict(antialiased=True, linewidth=0.5)
self.mne.traces = ax_main.plot(
np.full((1, self.mne.n_channels), np.nan), **self.mne.trace_kwargs
)
# SAVE UI ELEMENT HANDLES
vars(self.mne).update(
ax_main=ax_main,
ax_help=ax_help,
ax_proj=ax_proj,
ax_hscroll=ax_hscroll,
ax_vscroll=ax_vscroll,
vsel_patch=vsel_patch,
hsel_patch=hsel_patch,
vline=vline,
vline_hscroll=vline_hscroll,
vline_text=vline_text,
)
def _get_size(self):
return self.get_size_inches()
def _resize(self, event):
"""Handle resize event for mne_browse-style plots (Raw/Epochs/ICA)."""
old_width, old_height = self.mne.fig_size_px
new_width, new_height = self._get_size_px()
new_margins = _calc_new_margins(
self, old_width, old_height, new_width, new_height
)
self.subplots_adjust(**new_margins)
# zen mode bookkeeping
self.mne.zen_w *= old_width / new_width
self.mne.zen_h *= old_height / new_height
self.mne.fig_size_px = (new_width, new_height)
self.canvas.draw_idle()
def _hover(self, event):
"""Handle motion event when annotating."""
if (
event.button is not None
or event.xdata is None
or event.inaxes != self.mne.ax_main
):
return
if not self.mne.draggable_annotations:
self._remove_annotation_hover_line()
return
from matplotlib.patheffects import Normal, Stroke
for coll in self.mne.annotations:
if coll.contains(event)[0]:
path = coll.get_paths()
assert len(path) == 1
path = path[0]
color = coll.get_edgecolors()[0]
ylim = self.mne.ax_main.get_ylim()
# are we on the left or right edge?
_l = path.vertices[:, 0].min()
_r = path.vertices[:, 0].max()
x = _l if abs(event.xdata - _l) < abs(event.xdata - _r) else _r
mask = path.vertices[:, 0] == x
def drag_callback(x0):
path.vertices[mask, 0] = x0
# create or update the DraggableLine
hover_line = self.mne.annotation_hover_line
if hover_line is None:
line = self.mne.ax_main.plot(
[x, x], ylim, color=color, linewidth=2, pickradius=5.0
)[0]
hover_line = DraggableLine(
line, self._modify_annotation, drag_callback
)
else:
hover_line.set_x(x)
hover_line.drag_callback = drag_callback
# style the line
line = hover_line.line
patheff = [Stroke(linewidth=4, foreground=color, alpha=0.5), Normal()]
line.set_path_effects(
patheff if line.contains(event)[0] else patheff[1:]
)
self.mne.ax_main.selector.active = False
self.mne.annotation_hover_line = hover_line
self.canvas.draw_idle()
return
self._remove_annotation_hover_line()
def _keypress(self, event):
"""Handle keypress events."""
key = event.key
n_channels = self.mne.n_channels
if self.mne.is_epochs:
last_time = self.mne.n_times / self.mne.info["sfreq"]
else:
last_time = self.mne.inst.times[-1]
# scroll up/down
if key in ("down", "up", "shift+down", "shift+up"):
key = key.split("+")[-1]
direction = -1 if key == "up" else 1
# butterfly case
if self.mne.butterfly:
return
# group_by case
elif self.mne.fig_selection is not None:
buttons = self.mne.fig_selection.mne.radio_ax.buttons
labels = [label.get_text() for label in buttons.labels]
current_label = buttons.value_selected
current_idx = labels.index(current_label)
selections_dict = self.mne.ch_selections
penult = current_idx < (len(labels) - 1)
pre_penult = current_idx < (len(labels) - 2)
has_custom = selections_dict.get("Custom", None) is not None
def_custom = len(selections_dict.get("Custom", list()))
up_ok = key == "up" and current_idx > 0
down_ok = key == "down" and (
pre_penult
or (penult and not has_custom)
or (penult and has_custom and def_custom)
)
if up_ok or down_ok:
buttons.set_active(current_idx + direction)
# normal case
else:
ceiling = len(self.mne.ch_order) - n_channels
ch_start = self.mne.ch_start + direction * n_channels
self.mne.ch_start = np.clip(ch_start, 0, ceiling)
self._update_picks()
self._update_vscroll()
self._redraw()
# scroll left/right
elif key in ("right", "left", "shift+right", "shift+left"):
old_t_start = self.mne.t_start
direction = 1 if key.endswith("right") else -1
if self.mne.is_epochs:
denom = 1 if key.startswith("shift") else self.mne.n_epochs
else:
denom = 1 if key.startswith("shift") else 4
t_max = last_time - self.mne.duration
t_start = self.mne.t_start + direction * self.mne.duration / denom
self.mne.t_start = np.clip(t_start, self.mne.first_time, t_max)
if self.mne.t_start != old_t_start:
self._update_hscroll()
self._redraw(annotations=True)
# scale traces
elif key in ("=", "+", "-"):
scaler = 1 / 1.1 if key == "-" else 1.1
self.mne.scale_factor *= scaler
self._redraw(update_data=False)
# change number of visible channels
elif (
key in ("pageup", "pagedown")
and self.mne.fig_selection is None
and not self.mne.butterfly
):
new_n_ch = n_channels + (1 if key == "pageup" else -1)
self.mne.n_channels = np.clip(new_n_ch, 1, len(self.mne.ch_order))
# add new chs from above if we're at the bottom of the scrollbar
ch_end = self.mne.ch_start + self.mne.n_channels
if ch_end > len(self.mne.ch_order) and self.mne.ch_start > 0:
self.mne.ch_start -= 1
self._update_vscroll()
# redraw only if changed
if self.mne.n_channels != n_channels:
self._update_picks()
self._update_trace_offsets()
self._redraw(annotations=True)
# change duration
elif key in ("home", "end"):
old_dur = self.mne.duration
dur_delta = 1 if key == "end" else -1
if self.mne.is_epochs:
# prevent from showing zero epochs, or more epochs than we have
self.mne.n_epochs = np.clip(
self.mne.n_epochs + dur_delta, 1, len(self.mne.inst)
)
# use the length of one epoch as duration change
min_dur = len(self.mne.inst.times) / self.mne.info["sfreq"]
new_dur = self.mne.duration + dur_delta * min_dur
else:
# never show fewer than 3 samples
min_dur = 3 * np.diff(self.mne.inst.times[:2])[0]
# use multiplicative dur_delta
dur_delta = 5 / 4 if dur_delta > 0 else 4 / 5
new_dur = self.mne.duration * dur_delta
self.mne.duration = np.clip(new_dur, min_dur, last_time)
if self.mne.duration != old_dur:
if self.mne.t_start + self.mne.duration > last_time:
self.mne.t_start = last_time - self.mne.duration
self._update_hscroll()
self._redraw(annotations=True)
elif key == "?": # help window
self._toggle_help_fig(event)
elif key == "a": # annotation mode
self._toggle_annotation_fig()
elif key == "b" and self.mne.instance_type != "ica": # butterfly mode
self._toggle_butterfly()
elif key == "d": # DC shift
self.mne.remove_dc = not self.mne.remove_dc
self._redraw()
elif key == "h": # histogram
self._toggle_epoch_histogram()
elif key == "j" and len(self.mne.projs): # SSP window
self._toggle_proj_fig()
elif key == "J" and len(self.mne.projs):
self._toggle_proj_checkbox(event, toggle_all=True)
elif key == "p": # toggle draggable annotations
self._toggle_draggable_annotations(event)
if self.mne.fig_annotation is not None:
checkbox = self.mne.fig_annotation.mne.drag_checkbox
with _events_off(checkbox):
checkbox.set_active(0)
elif key == "s": # scalebars
self._toggle_scalebars(event)
elif key == "w": # toggle noise cov whitening
self._toggle_whitening()
elif key == "z": # zen mode: hide scrollbars and buttons
self._toggle_scrollbars()
self._redraw(update_data=False)
elif key == "t":
self._toggle_time_format()
else: # check for close key / fullscreen toggle
super()._keypress(event)
def _buttonpress(self, event):
"""Handle mouse clicks."""
from matplotlib.collections import PolyCollection
from ..annotations import _sync_onset
butterfly = self.mne.butterfly
annotating = self.mne.fig_annotation is not None
ax_main = self.mne.ax_main
inst = self.mne.inst
# ignore middle clicks, scroll wheel events, and clicks outside axes
if event.button not in (1, 3) or event.inaxes is None:
return
elif event.button == 1: # left-click (primary)
# click in main axes
if event.inaxes == ax_main and not annotating:
if self.mne.instance_type == "epochs" or not butterfly:
for line in self.mne.traces + self.mne.epoch_traces:
if line.contains(event)[0]:
if self.mne.instance_type == "epochs":
self._toggle_bad_epoch(event)
else:
idx = self.mne.traces.index(line)
self._toggle_bad_channel(idx)
return
self._show_vline(event.xdata) # butterfly / not on data trace
self._redraw(update_data=False, annotations=False)
return
# click in vertical scrollbar
elif event.inaxes == self.mne.ax_vscroll:
if self.mne.fig_selection is not None:
self._change_selection_vscroll(event)
elif self._check_update_vscroll_clicked(event):
self._redraw()
# click in horizontal scrollbar
elif event.inaxes == self.mne.ax_hscroll:
if self._check_update_hscroll_clicked(event):
self._redraw(annotations=True)
# click on proj button
elif event.inaxes == self.mne.ax_proj:
self._toggle_proj_fig(event)
# click on help button
elif event.inaxes == self.mne.ax_help:
self._toggle_help_fig(event)
else: # right-click (secondary)
if annotating:
spans = [
span
for span in ax_main.collections
if isinstance(span, PolyCollection)
]
if any(span.contains(event)[0] for span in spans):
xdata = event.xdata - self.mne.first_time
start = _sync_onset(inst, inst.annotations.onset)
end = start + inst.annotations.duration
is_onscreen = self.mne.onscreen_annotations # boolean array
was_clicked = (xdata > start) & (xdata < end) & is_onscreen
# determine which annotation label is "selected"
buttons = self.mne.fig_annotation.mne.radio_ax.buttons
current_label = buttons.value_selected
is_active_label = inst.annotations.description == current_label
# use z-order as tiebreaker (or if click wasn't on an active span)
# (ax_main.collections only includes *visible* annots, so we offset)
visible_zorders = [span.zorder for span in spans]
zorders = np.zeros_like(is_onscreen).astype(int)
offset = np.where(is_onscreen)[0][0]
zorders[offset : (offset + len(visible_zorders))] = visible_zorders
# among overlapping clicked spans, prefer removing spans whose label
# is the active label; then fall back to zorder as deciding factor
active_clicked = was_clicked & is_active_label
mask = active_clicked if any(active_clicked) else was_clicked
highest = zorders == zorders[mask].max()
idx = np.where(highest)[0]
inst.annotations.delete(idx)
self._remove_annotation_hover_line()
self._draw_annotations()
self.canvas.draw_idle()
elif event.inaxes == ax_main:
self._toggle_vline(False)
def _pick(self, event):
"""Handle matplotlib pick events."""
from matplotlib.text import Text
if self.mne.butterfly:
return
# clicked on channel name
if isinstance(event.artist, Text):
ch_name = event.artist.get_text()
ind = self.mne.ch_names[self.mne.picks].tolist().index(ch_name)
if event.mouseevent.button == 1: # left click
self._toggle_bad_channel(ind)
elif event.mouseevent.button == 3: # right click
self._create_ch_context_fig(ind)
def _create_ch_context_fig(self, idx):
fig = super()._create_ch_context_fig(idx)
plt_show(fig=fig)
def _new_child_figure(self, fig_name, *, layout=None, **kwargs):
"""Instantiate a new MNE dialog figure (with event listeners)."""
fig = _figure(
toolbar=False,
parent_fig=self,
fig_name=fig_name,
layout=layout,
**kwargs,
)
fig._add_default_callbacks()
self.mne.child_figs.append(fig)
if isinstance(fig_name, str):
setattr(self.mne, fig_name, fig)
return fig
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# HELP DIALOG
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
def _create_help_fig(self):
"""Create help dialog window."""
text = {
key: val for key, val in self._get_help_text().items() if val is not None
}
keys = ""
vals = ""
for key, val in text.items():
newsection = "\n" if key.startswith("_") else ""
key = key[1:] if key.startswith("_") else key
newlines = "\n" * len(val.split("\n")) # handle multiline values
keys += f"{newsection}{key} {newlines}"
vals += f"{newsection}{val}\n"
# calc figure size
n_lines = len(keys.split("\n"))
longest_key = max(len(k) for k in text.keys())
longest_val = max(
max(len(w) for w in v.split("\n")) if "\n" in v else len(v)
for v in text.values()
)
width = (longest_key + longest_val) / 12
height = (n_lines) / 5
# create figure and axes
fig = self._new_child_figure(
figsize=(width, height), fig_name="fig_help", window_title="Help"
)
ax = fig.add_axes((0.01, 0.01, 0.98, 0.98))
ax.set_axis_off()
kwargs = dict(va="top", linespacing=1.5, usetex=False)
ax.text(0.42, 1, keys, ma="right", ha="right", **kwargs)
ax.text(0.42, 1, vals, ma="left", ha="left", **kwargs)
def _toggle_help_fig(self, event):
"""Show/hide the help dialog window."""
if self.mne.fig_help is None:
self._create_help_fig()
plt_show(fig=self.mne.fig_help)
else:
plt.close(self.mne.fig_help)
def _get_help_text(self):
"""Generate help dialog text; `None`-valued entries removed later."""
inst = self.mne.instance_type
is_raw = inst == "raw"
is_epo = inst == "epochs"
is_ica = inst == "ica"
has_proj = bool(len(self.mne.projs))
# adapt keys to different platforms
is_mac = platform.system() == "Darwin"
dur_keys = ("fn + ←", "fn + →") if is_mac else ("Home", "End")
ch_keys = ("fn + ↑", "fn + ↓") if is_mac else ("Page up", "Page down")
# adapt descriptions to different instance types
ch_cmp = "component" if is_ica else "channel"
ch_epo = "epoch" if is_epo else "channel"
ica_bad = "Mark/unmark component for exclusion"
dur_vals = (
[f"Show {n} epochs" for n in ("fewer", "more")]
if self.mne.is_epochs
else [f"Show {d} time window" for d in ("shorter", "longer")]
)
ch_vals = [
f"{inc_dec} number of visible {ch_cmp}s"
for inc_dec in ("Increase", "Decrease")
]
lclick_data = ica_bad if is_ica else f"Mark/unmark bad {ch_epo}"
lclick_name = ica_bad if is_ica else "Mark/unmark bad channel"
rclick_name = dict(
ica="Show diagnostics for component",
epochs="Show imageplot for channel",
raw="Show channel location",
)[inst]
# TODO not yet implemented
# ldrag = ('Show spectrum plot for selected time span;\nor (in '
# 'annotation mode) add annotation') if inst== 'raw' else None
ldrag = "add annotation (in annotation mode)" if is_raw else None
noise_cov = None if self.mne.noise_cov is None else "Toggle signal whitening"
scrl = "1 epoch" if self.mne.is_epochs else "¼ window"
# below, value " " is a hack to make "\n".split(value) have length 1
help_text = OrderedDict(
[
("_NAVIGATION", " "),
("→", f"Scroll {scrl} right (scroll full window with Shift + →)"),
("←", f"Scroll {scrl} left (scroll full window with Shift + ←)"),
(dur_keys[0], dur_vals[0]),
(dur_keys[1], dur_vals[1]),
("↑", f"Scroll up ({ch_cmp}s)"),
("↓", f"Scroll down ({ch_cmp}s)"),
(ch_keys[0], ch_vals[0]),
(ch_keys[1], ch_vals[1]),
("_SIGNAL TRANSFORMATIONS", " "),
("+ or =", "Increase signal scaling"),
("-", "Decrease signal scaling"),
("b", "Toggle butterfly mode" if not is_ica else None),
("d", "Toggle DC removal" if is_raw else None),
("w", noise_cov),
("_USER INTERFACE", " "),
("a", "Toggle annotation mode" if is_raw else None),
("h", "Toggle peak-to-peak histogram" if is_epo else None),
("j", "Toggle SSP projector window" if has_proj else None),
("shift+j", "Toggle all SSPs"),
("p", "Toggle draggable annotations" if is_raw else None),
("s", "Toggle scalebars" if not is_ica else None),
("z", "Toggle scrollbars"),
("t", "Toggle time format" if not is_epo else None),
("F11", "Toggle fullscreen" if not is_mac else None),
("?", "Open this help window"),
("esc", "Close focused figure or dialog window"),
("_MOUSE INTERACTION", " "),
(f"Left-click {ch_cmp} name", lclick_name),
(f"Left-click {ch_cmp} data", lclick_data),
("Left-click-and-drag on plot", ldrag),
("Left-click on plot background", "Place vertical guide"),
("Right-click on plot background", "Clear vertical guide"),
("Right-click on channel name", rclick_name),
]
)
return help_text
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# ANNOTATIONS
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
def _create_annotation_fig(self):
"""Create the annotation dialog window."""
from matplotlib.widgets import Button, CheckButtons, SpanSelector
from mpl_toolkits.axes_grid1.axes_divider import make_axes_locatable
from mpl_toolkits.axes_grid1.axes_size import Fixed
# make figure
labels = np.array(sorted(set(self.mne.inst.annotations.description)))
radio_button_h = self._compute_annotation_figsize(len(labels))
figsize = (ANNOTATION_FIG_W, ANNOTATION_FIG_MIN_H + radio_button_h)
fig = self._new_child_figure(
figsize=figsize,
FigureClass=MNEAnnotationFigure,
fig_name="fig_annotation",
window_title="Annotations",
)
# make main axes
left = fig._inch_to_rel(ANNOTATION_FIG_PAD)
bottom = fig._inch_to_rel(ANNOTATION_FIG_PAD, horiz=False)
width = 1 - 2 * left
height = 1 - 2 * bottom
fig.mne.radio_ax = fig.add_axes(
(left, bottom, width, height), frame_on=False, aspect="equal"
)
div = make_axes_locatable(fig.mne.radio_ax)
# append show/hide checkboxes at right
fig.mne.show_hide_ax = div.append_axes(
position="right",
size=Fixed(ANNOTATION_FIG_CHECKBOX_COLUMN_W),
pad=Fixed(ANNOTATION_FIG_PAD),
aspect="equal",
sharey=fig.mne.radio_ax,
)
# populate w/ radio buttons & labels
self._update_annotation_fig()
# append instructions at top
instructions_ax = div.append_axes(
position="top", size=Fixed(1), pad=Fixed(5 * ANNOTATION_FIG_PAD)
)
instructions = "\n".join(
[
r"$\mathbf{Left‐click~&~drag~on~plot:}$ create/modify annotation",
r"$\mathbf{Right‐click~on~plot~annotation:}$ delete annotation",
r"$\mathbf{Type~in~annotation~window:}$ modify new label name",
r"$\mathbf{Enter~(or~click~button):}$ add new label to list",
r"$\mathbf{Esc:}$ exit annotation mode & close this window",
]
)
instructions_ax.text(
0, 1, instructions, va="top", ha="left", linespacing=1.7, usetex=False
) # force use of MPL mathtext parser
instructions_ax.set_axis_off()
# append text entry axes at bottom
text_entry_ax = div.append_axes(
position="bottom",
size=Fixed(3 * ANNOTATION_FIG_PAD),
pad=Fixed(ANNOTATION_FIG_PAD),
)
text_entry_ax.text(
0.4, 0.5, "New label:", va="center", ha="right", weight="bold"
)
fig.label = text_entry_ax.text(0.5, 0.5, "BAD_", va="center", ha="left")
text_entry_ax.set_axis_off()
# append button at bottom
button_ax = div.append_axes(
position="bottom",
size=Fixed(3 * ANNOTATION_FIG_PAD),
pad=Fixed(ANNOTATION_FIG_PAD),
)
fig.button = Button(button_ax, "Add new label")
fig.button.on_clicked(self._add_annotation_label)
plt_show(fig=fig)
# add "draggable" checkbox
drag_ax_height = 3 * ANNOTATION_FIG_PAD
drag_ax = div.append_axes(
"bottom", size=Fixed(drag_ax_height), pad=Fixed(ANNOTATION_FIG_PAD)
)
check_kwargs = _get_check_kwargs()
checkbox = CheckButtons(
drag_ax,
labels=("Draggable edges?",),
actives=(self.mne.draggable_annotations,),
**check_kwargs,
)
checkbox.on_clicked(self._toggle_draggable_annotations)
fig.mne.drag_checkbox = checkbox
# reposition & resize axes
width_in, _ = fig.get_size_inches()
width_ax = fig._inch_to_rel(
width_in - ANNOTATION_FIG_CHECKBOX_COLUMN_W - 3 * ANNOTATION_FIG_PAD
)
aspect = width_ax / fig._inch_to_rel(drag_ax_height)
drag_ax.set(xlim=(0, aspect), ylim=(0, 1))
drag_ax.set_axis_off()
if _OLD_BUTTONS:
rect = checkbox.rectangles[0]
_pad, _size = (0.2, 0.6)
rect.set_bounds(_pad, _pad, _size, _size)
lines = checkbox.lines[0]
for line, direction in zip(lines, (1, -1)):
line.set_xdata((_pad, _pad + _size)[::direction])
line.set_ydata((_pad, _pad + _size))
text = checkbox.labels[0]
text.set(position=(3 * _pad + _size, 0.45), va="center")
for artist in lines + (rect, text):
artist.set_transform(drag_ax.transData)
# setup interactivity in plot window
if fig.mne.radio_ax.buttons is None:
col = "#ff0000"
else:
col = self.mne.annotation_segment_colors[self._get_annotation_labels()[0]]
selector = SpanSelector(
self.mne.ax_main,
self._select_annotation_span,
"horizontal",
minspan=0.1,
useblit=True,
button=1,
props=dict(alpha=0.5, facecolor=col),
)
self.mne.ax_main.selector = selector
self.mne._callback_ids["motion_notify_event"] = self.canvas.mpl_connect(
"motion_notify_event", self._hover
)
def _toggle_visible_annotations(self, event):
"""Enable/disable display of annotations on a per-label basis."""
checkboxes = self.mne.show_hide_annotation_checkboxes
labels = [t.get_text() for t in checkboxes.labels]
actives = checkboxes.get_status()
self.mne.visible_annotations = dict(zip(labels, actives))
self._redraw(update_data=False, annotations=True)
def _toggle_draggable_annotations(self, event):
"""Enable/disable draggable annotation edges."""
self.mne.draggable_annotations = not self.mne.draggable_annotations
def _update_annotation_fig(self, *, draw=True):
"""Draw or redraw the radio buttons and annotation labels."""
from matplotlib.colors import to_rgba
from matplotlib.widgets import CheckButtons, RadioButtons
# define shorthand variables
fig = self.mne.fig_annotation
ax = fig.mne.radio_ax
labels = self._get_annotation_labels()
# compute new figsize
radio_button_h = self._compute_annotation_figsize(len(labels))
fig.set_size_inches(
ANNOTATION_FIG_W, ANNOTATION_FIG_MIN_H + radio_button_h, forward=True
)
# populate center axes with labels & radio buttons
ax.clear()
title = "Existing labels:" if len(labels) else "No existing labels"
ax.set_title(title, size=None, loc="left")
if len(labels):
if _OLD_BUTTONS:
ax.buttons = RadioButtons(ax, labels)
radius = 0.15
circles = ax.buttons.circles
for circle, label in zip(circles, ax.buttons.labels):
circle.set_transform(ax.transData)
center = ax.transData.inverted().transform(
ax.transAxes.transform((0.1, 0))
)
circle.set_center((center[0], circle.center[1]))
circle.set_edgecolor(
self.mne.annotation_segment_colors[label.get_text()]
)
circle.set_linewidth(4)
circle.set_radius(radius / len(labels))
else:
edgecolors = [
self.mne.annotation_segment_colors[label] for label in labels
]
facecolors = [to_rgba(col)[:3] + (0.5,) for col in edgecolors]
radio_props = dict(
s=144,
linewidth=4,
edgecolor=edgecolors,
facecolor=facecolors,
)
ax.buttons = RadioButtons(ax, labels, radio_props=radio_props)
else:
ax.buttons = None
# adjust xlim to keep equal aspect & full width (keep circles round)
aspect = (
ANNOTATION_FIG_W - ANNOTATION_FIG_CHECKBOX_COLUMN_W - 3 * ANNOTATION_FIG_PAD
) / radio_button_h
ax.set_xlim((0, aspect))
# style the selected button
if len(labels):
fig._set_active_button(0, draw=False)
# add event listeners
if ax.buttons is not None:
if _OLD_BUTTONS:
ax.buttons.disconnect_events() # clear MPL default listeners
ax.buttons.on_clicked(fig._radiopress)
if _OLD_BUTTONS:
ax.buttons.connect_event("button_press_event", fig._click_override)
ax.set_axis_off()
# now do the show/hide checkboxes
show_hide_ax = fig.mne.show_hide_ax
show_hide_ax.clear()
show_hide_ax.set_axis_on()
aspect = ANNOTATION_FIG_CHECKBOX_COLUMN_W / radio_button_h
show_hide_ax.set(xlim=(0, aspect), ylim=(0, 1))
# ensure new labels have checkbox values
check_values = {label: False for label in labels}
check_values.update(self.mne.visible_annotations) # existing checks
actives = [check_values[label] for label in labels]
# regenerate checkboxes
check_kwargs = _get_check_kwargs()
checkboxes = CheckButtons(
ax=fig.mne.show_hide_ax, labels=labels, actives=actives, **check_kwargs
)
checkboxes.on_clicked(self._toggle_visible_annotations)
# add title, hide labels
show_hide_title = "show/\nhide " if len(labels) else ""
show_hide_ax.set_title(show_hide_title, size=None, loc="right")
for label in checkboxes.labels:
label.set_visible(False)
show_hide_ax.set_axis_off()
# fix aspect and right-align
if _OLD_BUTTONS:
if len(labels) == 1:
bounds = (0.05, 0.375, 0.25, 0.25) # undo MPL special case
checkboxes.rectangles[0].set_bounds(bounds)
for line, step in zip(checkboxes.lines[0], (1, -1)):
line.set_xdata((bounds[0], bounds[0] + bounds[2]))
line.set_ydata((bounds[1], bounds[1] + bounds[3])[::step])
for rect in checkboxes.rectangles:
rect.set_transform(show_hide_ax.transData)
bbox = rect.get_bbox()
bounds = (aspect, bbox.ymin, -bbox.width, bbox.height)
rect.set_bounds(bounds)
rect.set_clip_on(False)
for line in np.array(checkboxes.lines).ravel():
line.set_transform(show_hide_ax.transData)
line.set_xdata(aspect + 0.05 - np.array(line.get_xdata()))
# store state
self.mne.visible_annotations = check_values
self.mne.show_hide_annotation_checkboxes = checkboxes
if draw:
fig.canvas.draw_idle()
def _toggle_annotation_fig(self):
"""Show/hide the annotation dialog window."""
if self.mne.fig_annotation is None and not self.mne.is_epochs:
self._create_annotation_fig()
else:
plt.close(self.mne.fig_annotation)
def _compute_annotation_figsize(self, n_labels):
"""Adapt size of Annotation UI to accommodate the number of buttons.
self._create_annotation_fig() implements the following:
Fixed part of height:
0.1 top margin
1.0 instructions
0.5 padding below instructions
--- (variable-height axis for label list, returned by this method)
0.1 padding above text entry
0.3 text entry
0.1 padding above button
0.3 button
0.1 padding above checkbox
0.3 checkbox
0.1 bottom margin
------------------------------------------
2.9 total fixed height
"""
return max(ANNOTATION_FIG_PAD, 0.7 * n_labels)
def _add_annotation_label(self, event):
"""Add new annotation description."""
text = self.mne.fig_annotation.label.get_text()
# If it exists, change this title. If it doesn't, the title will
# be set in _update_annotation_fig()
if text in self.mne.new_annotation_labels:
self.mne.fig_annotation.mne.radio_ax.set_title(
f"Existing labels: (duplicate label: {repr(text)})",
size=None,
loc="left",
)
self.mne.fig_annotation.canvas.draw()
return
self.mne.new_annotation_labels.append(text)
self._setup_annotation_colors()
self._update_annotation_fig(draw=False)
# automatically activate new label's radio button
idx = [
label.get_text()
for label in self.mne.fig_annotation.mne.radio_ax.buttons.labels
].index(text)
self.mne.fig_annotation._set_active_button(idx, draw=False)
# simulate a click on the radiobutton → update the span selector color
self.mne.fig_annotation._radiopress(event=None, draw=False)
# reset the text entry box's text
self.mne.fig_annotation.label.set_text("BAD_")
self.mne.fig_annotation.canvas.draw()
def _select_annotation_span(self, vmin, vmax):
"""Handle annotation span selector."""
from ..annotations import _sync_onset
onset = _sync_onset(self.mne.inst, vmin, True) - self.mne.first_time
duration = vmax - vmin
buttons = self.mne.fig_annotation.mne.radio_ax.buttons
if buttons is None or buttons.value_selected is None:
logger.warning(
"No annotation-label exists! "
"Add one by typing the name and clicking "
'on "Add new label" in the annotation-dialog.'
)
else:
labels = [label.get_text() for label in buttons.labels]
active_idx = labels.index(buttons.value_selected)
_merge_annotations(
onset, onset + duration, labels[active_idx], self.mne.inst.annotations
)
# if adding a span with an annotation label that is hidden, show it
if not self.mne.visible_annotations[buttons.value_selected]:
self.mne.show_hide_annotation_checkboxes.set_active(active_idx)
self._redraw(update_data=False, annotations=True)
def _remove_annotation_hover_line(self):
"""Remove annotation line from the plot and reactivate selector."""
if self.mne.annotation_hover_line is not None:
self.mne.annotation_hover_line.remove()
self.mne.annotation_hover_line = None
self.mne.ax_main.selector.active = True
self.canvas.draw()
def _modify_annotation(self, old_x, new_x):
"""Modify annotation."""
from ..annotations import _sync_onset
segment = np.array(np.where(self.mne.annotation_segments == old_x))
if segment.shape[1] == 0:
return
raw = self.mne.inst
annotations = raw.annotations
first_time = self.mne.first_time
idx = [segment[0][0], segment[1][0]]
onset = _sync_onset(raw, self.mne.annotation_segments[idx[0]][0], True)
ann_idx = np.where(annotations.onset == onset - first_time)[0]
if idx[1] == 0: # start of annotation
onset = _sync_onset(raw, new_x, True) - first_time
duration = annotations.duration[ann_idx] + old_x - new_x
else: # end of annotation
onset = annotations.onset[ann_idx]
duration = _sync_onset(raw, new_x, True) - onset - first_time
if duration < 0:
onset += duration
duration *= -1.0
_merge_annotations(
onset,
onset + duration,
annotations.description[ann_idx],
annotations,
ann_idx,
)
self._draw_annotations()
self._remove_annotation_hover_line()
self.canvas.draw_idle()
def _clear_annotations(self):
"""Clear all annotations from the figure."""
for annot in list(self.mne.annotations):
annot.remove()
self.mne.annotations.remove(annot)
for annot in list(self.mne.hscroll_annotations):
annot.remove()
self.mne.hscroll_annotations.remove(annot)
for text in list(self.mne.annotation_texts):
text.remove()
self.mne.annotation_texts.remove(text)
def _draw_annotations(self):
"""Draw (or redraw) the annotation spans."""
self._clear_annotations()
self._update_annotation_segments()
segments = self.mne.annotation_segments
onscreen_annotations = np.zeros(len(segments), dtype=bool)
times = self.mne.times
ax = self.mne.ax_main
ylim = ax.get_ylim()
for idx, (start, end) in enumerate(segments):
descr = self.mne.inst.annotations.description[idx]
segment_color = self.mne.annotation_segment_colors[descr]
zorder = self.mne.zorder["ann"] + idx
kwargs = dict(color=segment_color, alpha=0.3, zorder=zorder)
if self.mne.visible_annotations[descr]:
# draw all segments on ax_hscroll
annot = self.mne.ax_hscroll.fill_betweenx((0, 1), start, end, **kwargs)
self.mne.hscroll_annotations.append(annot)
# draw only visible segments on ax_main
visible_segment = np.clip([start, end], times[0], times[-1])
if np.diff(visible_segment) > 0:
annot = ax.fill_betweenx(ylim, *visible_segment, **kwargs)
self.mne.annotations.append(annot)
onscreen_annotations[idx] = True
xy = (visible_segment.mean(), ylim[1])
text = ax.annotate(
descr,
xy,
xytext=(0, 9),
textcoords="offset points",
ha="center",
va="baseline",
color=segment_color,
)
self.mne.annotation_texts.append(text)
self.mne.onscreen_annotations = onscreen_annotations
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# CHANNEL SELECTION GUI
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
def _create_selection_fig(self):
"""Create channel selection dialog window."""
from matplotlib.colors import to_rgb
from matplotlib.widgets import RadioButtons
# make figure
fig = self._new_child_figure(
figsize=(3, 7),
FigureClass=MNESelectionFigure,
fig_name="fig_selection",
window_title="Channel selection",
)
gs = fig.add_gridspec(15, 1)
# add sensor plot at top
fig.mne.sensor_ax = fig.add_subplot(gs[:5])
plot_sensors(
self.mne.info,
kind="select",
ch_type="all",
title="",
axes=fig.mne.sensor_ax,
ch_groups=self.mne.group_by,
show=False,
)
fig.subplots_adjust(bottom=0.01, top=0.99, left=0.01, right=0.99)
# style the sensors so the selection is easier to distinguish
fig.lasso.linewidth_selected = 2
self._update_highlighted_sensors()
# add radio button axes
radio_ax = fig.add_subplot(gs[5:-3], frame_on=False, aspect="equal")
fig.mne.radio_ax = radio_ax
selections_dict = self.mne.ch_selections
selections_dict.update(Custom=np.array([], dtype=int)) # for lasso
labels = list(selections_dict)
# make & style the radio buttons
activecolor = to_rgb(self.mne.fgcolor) + (0.5,)
radio_ax.buttons = RadioButtons(radio_ax, labels, activecolor=activecolor)
fig.mne.old_selection = 0
if _OLD_BUTTONS:
for circle in radio_ax.buttons.circles:
circle.set_radius(0.25 / len(labels))
circle.set_linewidth(2)
circle.set_edgecolor(self.mne.fgcolor)
fig._style_radio_buttons_butterfly()
# add instructions at bottom
instructions = (
"To use a custom selection, first click-drag on the sensor plot "
'to "lasso" the sensors you want to select, or hold Ctrl while '
"clicking individual sensors. Holding Ctrl while click-dragging "
"allows a lasso selection adding to (rather than replacing) the "
"existing selection."
)
instructions_ax = fig.add_subplot(gs[-3:], frame_on=False)
instructions_ax.text(
0.04, 0.08, instructions, va="bottom", ha="left", ma="left", wrap=True
)
instructions_ax.set_axis_off()
# add event listeners
radio_ax.buttons.on_clicked(fig._radiopress)
fig.lasso.callbacks.append(fig._set_custom_selection)
def _change_selection_vscroll(self, event):
"""Handle clicks on vertical scrollbar when using selections."""
buttons = self.mne.fig_selection.mne.radio_ax.buttons
labels = [label.get_text() for label in buttons.labels]
offset = 0
selections_dict = self.mne.ch_selections
for idx, label in enumerate(labels):
offset += len(selections_dict[label])
if event.ydata < offset:
with _events_off(buttons):
buttons.set_active(idx)
self.mne.fig_selection._radiopress(event)
return
def _update_selection(self):
"""Update visible channels based on selection dialog interaction."""
selections_dict = self.mne.ch_selections
fig = self.mne.fig_selection
buttons = fig.mne.radio_ax.buttons
label = buttons.value_selected
labels = [_label.get_text() for _label in buttons.labels]
self.mne.fig_selection.mne.old_selection = labels.index(label)
self.mne.picks = selections_dict[label]
self.mne.n_channels = len(self.mne.picks)
self._update_highlighted_sensors()
# if "Vertex" is defined, some channels appear twice, so if
# "Vertex" is selected, ch_start should be the *first* match;
# otherwise it should be the *last* match (since "Vertex" is
# always the first selection group, if it exists).
index = 0 if label == "Vertex" else -1
ch_order = np.concatenate(list(selections_dict.values()))
ch_start = np.where(ch_order == self.mne.picks[0])[0][index]
self.mne.ch_start = ch_start
self._update_trace_offsets()
self._update_vscroll()
self._redraw(annotations=True)
def _update_highlighted_sensors(self):
"""Update the sensor plot to show what is selected."""
inds = np.isin(
self.mne.fig_selection.lasso.names, self.mne.ch_names[self.mne.picks]
).nonzero()[0]
self.mne.fig_selection.lasso.select_many(inds)
def _update_bad_sensors(self, pick, mark_bad):
"""Update the sensor plot to reflect (un)marked bad channels."""
# replicate plotting order from plot_sensors(), to get index right
sensor_picks = list()
ch_indices = channel_indices_by_type(self.mne.info)
for this_type in _DATA_CH_TYPES_SPLIT:
if this_type in self.mne.ch_types:
sensor_picks.extend(ch_indices[this_type])
sensor_idx = np.isin(sensor_picks, pick).nonzero()[0]
# change the sensor color
fig = self.mne.fig_selection
fig.lasso.ec[sensor_idx, 0] = float(mark_bad) # change R of RGBA array
fig.lasso.collection.set_edgecolors(fig.lasso.ec)
fig.canvas.draw_idle()
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# PROJECTORS & BAD CHANNELS
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
def _create_proj_fig(self):
"""Create the projectors dialog window."""
from matplotlib.widgets import Button, CheckButtons
projs = self.mne.projs
labels = [p["desc"] for p in projs]
for ix, active in enumerate(self.mne.projs_active):
if active:
labels[ix] += " (already applied)"
# make figure
width = max([4.5, max([len(label) for label in labels]) / 8 + 0.5])
height = (len(projs) + 1) / 6 + 1.5
fig = self._new_child_figure(
figsize=(width, height),
fig_name="fig_proj",
window_title="SSP projection vectors",
)
# pass through some proj fig keypresses to the parent
fig.canvas.mpl_connect(
"key_press_event", lambda ev: self._keypress(ev) if ev.key in "jJ" else None
)
# make axes
offset = 1 / 6 / height
position = (0, offset, 1, 0.8 - offset)
ax = fig.add_axes(position, frame_on=False, aspect="equal")
# make title
first_line = (
"Projectors already applied to the data are dimmed.\n"
if any(self.mne.projs_active)
else ""
)
second_line = 'Projectors marked with "X" are active on the plot.'
ax.set_title(f"{first_line}{second_line}")
# draw checkboxes
checkboxes = CheckButtons(
ax,
labels=labels,
actives=self.mne.projs_on,
**_get_check_kwargs(labels=labels),
)
# gray-out already applied projectors
if _OLD_BUTTONS:
for label, rect, lines in zip(
checkboxes.labels, checkboxes.rectangles, checkboxes.lines
):
if label.get_text().endswith("(already applied)"):
label.set_color("0.5")
rect.set_edgecolor("0.7")
[x.set_color("0.7") for x in lines]
rect.set_linewidth(1)
# add "toggle all" button
ax_all = fig.add_axes((0.25, 0.01, 0.5, offset), frame_on=True)
fig.mne.proj_all = Button(ax_all, "Toggle all")
# add event listeners
checkboxes.on_clicked(self._toggle_proj_checkbox)
fig.mne.proj_all.on_clicked(
partial(self._toggle_proj_checkbox, toggle_all=True)
)
# save params
fig.mne.proj_checkboxes = checkboxes
# show figure
self.mne.fig_proj.canvas.draw()
plt_show(fig=self.mne.fig_proj, warn=False)
def _toggle_proj_fig(self, event=None):
"""Show/hide the projectors dialog window."""
if self.mne.fig_proj is None:
self._create_proj_fig()
else:
plt.close(self.mne.fig_proj)
def _toggle_proj_checkbox(self, event, toggle_all=False):
"""Perform operations when proj boxes clicked."""
on = self.mne.projs_on
applied = self.mne.projs_active
fig = self.mne.fig_proj
new_state = (
np.full_like(on, not all(on))
if toggle_all
else np.array(fig.mne.proj_checkboxes.get_status())
)
# update Xs when toggling all
if fig is not None:
if toggle_all:
with _events_off(fig.mne.proj_checkboxes):
for ix in np.where(on != new_state)[0]:
fig.mne.proj_checkboxes.set_active(ix)
# don't allow disabling already-applied projs
with _events_off(fig.mne.proj_checkboxes):
for ix in np.where(applied)[0]:
if not new_state[ix]:
fig.mne.proj_checkboxes.set_active(ix)
new_state[applied] = True
# update the data if necessary
if not np.array_equal(on, new_state):
self.mne.projs_on = new_state
self._update_projector()
self._redraw()
def _toggle_epoch_histogram(self):
"""Show or hide peak-to-peak histogram of channel amplitudes."""
if self.mne.instance_type == "epochs":
if self.mne.fig_histogram is None:
self._create_epoch_histogram()
else:
plt.close(self.mne.fig_histogram)
def _toggle_bad_channel(self, idx):
"""Mark/unmark bad channels; `idx` is index of *visible* channels."""
color, pick, marked_bad = super()._toggle_bad_channel(idx)
# update sensor color (if in selection mode)
if self.mne.fig_selection is not None:
self._update_bad_sensors(pick, marked_bad)
# update vscroll color
vscroll_idx = (self.mne.ch_order == pick).nonzero()[0]
for _idx in vscroll_idx:
self.mne.ax_vscroll.patches[_idx].set_color(color)
# redraw
self._redraw()
def _toggle_bad_epoch(self, event):
"""Mark/unmark bad epochs."""
epoch_ix, color = super()._toggle_bad_epoch(event.xdata)
self.mne.ax_hscroll.patches[epoch_ix].set_color(color)
self._redraw(update_data=False)
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# SCROLLBARS
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
def _update_zen_mode_offsets(self):
"""Compute difference between main axes edges and scrollbar edges."""
self.mne.fig_size_px = self._get_size_px()
self.mne.zen_w = (
self.mne.ax_vscroll.get_position().xmax
- self.mne.ax_main.get_position().xmax
)
self.mne.zen_h = (
self.mne.ax_main.get_position().ymin
- self.mne.ax_hscroll.get_position().ymin
)
def _toggle_scrollbars(self):
"""Show or hide scrollbars (A.K.A. zen mode)."""
self._update_zen_mode_offsets()
# grow/shrink main axes to take up space from (or make room for)
# scrollbars. We can't use ax.set_position() because axes are
# locatable, so we use subplots_adjust
should_show = not self.mne.scrollbars_visible
margins = {
side: getattr(self.subplotpars, side)
for side in ("left", "bottom", "right", "top")
}
# if should_show, bottom margin moves up; right margin moves left
margins["bottom"] += (1 if should_show else -1) * self.mne.zen_h
margins["right"] += (-1 if should_show else 1) * self.mne.zen_w
self.subplots_adjust(**margins)
# handle x-axis label
self.mne.zen_xlabel.set_visible(not should_show)
# show/hide other UI elements
for elem in ("ax_hscroll", "ax_vscroll", "ax_proj", "ax_help"):
if elem == "ax_vscroll" and self.mne.butterfly:
continue
# sometimes we don't have a proj button (ax_proj)
if getattr(self.mne, elem, None) is not None:
getattr(self.mne, elem).set_visible(should_show)
self.mne.scrollbars_visible = should_show
def _update_vscroll(self):
"""Update the vertical scrollbar (channel) selection indicator."""
self.mne.vsel_patch.set_xy((0, self.mne.ch_start))
self.mne.vsel_patch.set_height(self.mne.n_channels)
self._update_yaxis_labels()
def _update_hscroll(self):
"""Update the horizontal scrollbar (time) selection indicator."""
self.mne.hsel_patch.set_xy((self.mne.t_start, 0))
self.mne.hsel_patch.set_width(self.mne.duration)
def _check_update_hscroll_clicked(self, event):
"""Handle clicks on horizontal scrollbar."""
time = event.xdata - self.mne.duration / 2
max_time = (
self.mne.n_times / self.mne.info["sfreq"]
+ self.mne.first_time
- self.mne.duration
)
time = np.clip(time, self.mne.first_time, max_time)
if self.mne.is_epochs:
ix = np.searchsorted(self.mne.boundary_times[1:], time)
time = self.mne.boundary_times[ix]
if self.mne.t_start != time:
self.mne.t_start = time
self._update_hscroll()
return True
return False
def _check_update_vscroll_clicked(self, event):
"""Update vscroll patch on click, return True if location changed."""
new_ch_start = np.clip(
int(round(event.ydata - self.mne.n_channels / 2)),
0,
len(self.mne.ch_order) - self.mne.n_channels,
)
if self.mne.ch_start != new_ch_start:
self.mne.ch_start = new_ch_start
self._update_picks()
self._update_vscroll()
return True
return False
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# SCALEBARS & AXIS LABELS
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
def _show_scalebars(self):
"""Add channel scale bars."""
for pi, pick in enumerate(self.mne.picks):
this_name = self.mne.ch_names[pick]
this_type = self.mne.ch_types[pick]
# TODO: Simplify this someday -- we have to duplicate the challenging
# logic of _draw_traces here
offset_ixs = (
self.mne.picks
if self.mne.butterfly and self.mne.ch_selections is None
else slice(None)
)
offset = self.mne.trace_offsets[offset_ixs][pi]
if (
this_type not in self.mne.scalebars
and this_type != "stim"
and this_type in self.mne.scalings
and this_type in getattr(self.mne, "units", {})
and this_type in getattr(self.mne, "unit_scalings", {})
and this_name not in self.mne.info["bads"]
and this_name not in self.mne.whitened_ch_names
):
x = (self.mne.times[0] + self.mne.first_time,) * 2
denom = 4 if self.mne.butterfly else 2
y = tuple(np.array([-1, 1]) / denom + offset)
self._draw_one_scalebar(x, y, this_type)
if self.mne.is_epochs:
x = (
self.mne.times[0],
self.mne.times[0] + self.mne.boundary_times[1] / 2,
)
y_value = self.mne.n_channels - 0.5
y = (y_value, y_value)
self._draw_one_scalebar(x, y, "time")
def _hide_scalebars(self):
"""Remove channel scale bars."""
for bar in self.mne.scalebars.values():
bar.remove()
for text in self.mne.scalebar_texts.values():
text.remove()
self.mne.scalebars = dict()
self.mne.scalebar_texts = dict()
def _toggle_scalebars(self, event):
"""Show/hide the scalebars."""
if self.mne.scalebars_visible:
self._hide_scalebars()
else:
self._update_picks()
self._show_scalebars()
# toggle
self.mne.scalebars_visible = not self.mne.scalebars_visible
self._redraw(update_data=False)
def _draw_one_scalebar(self, x, y, ch_type):
"""Draw a scalebar."""
from .utils import _simplify_float
color = "#AA3377" # purple
kwargs = dict(color=color, zorder=self.mne.zorder["scalebar"])
if ch_type == "time":
label = f"{self.mne.boundary_times[1] / 2:.2f} s"
text = self.mne.ax_main.text(
x[0] + 0.015,
y[1] - 0.05,
label,
va="bottom",
ha="left",
size="xx-small",
**kwargs,
)
else:
scaler = 1 if self.mne.butterfly else 2
inv_norm = (
scaler
* self.mne.scalings[ch_type]
* self.mne.unit_scalings[ch_type]
/ self.mne.scale_factor
)
label = f"{_simplify_float(inv_norm)} {self.mne.units[ch_type]} "
text = self.mne.ax_main.text(
x[1], y[1], label, va="baseline", ha="right", size="xx-small", **kwargs
)
bar = self.mne.ax_main.plot(x, y, lw=4, **kwargs)[0]
self.mne.scalebars[ch_type] = bar
self.mne.scalebar_texts[ch_type] = text
def _update_yaxis_labels(self):
"""Change the y-axis labels."""
if self.mne.butterfly and self.mne.fig_selection is not None:
exclude = ("Vertex", "Custom")
ticklabels = list(self.mne.ch_selections)
keep_mask = np.isin(ticklabels, exclude, invert=True)
ticklabels = [
t.replace("Left-", "L-").replace("Right-", "R-") for t in ticklabels
] # avoid having to rotate labels
ticklabels = np.array(ticklabels)[keep_mask]
elif self.mne.butterfly:
_, ixs, _ = np.intersect1d(
_DATA_CH_TYPES_ORDER_DEFAULT, self.mne.ch_types, return_indices=True
)
ixs.sort()
ticklabels = np.array(_DATA_CH_TYPES_ORDER_DEFAULT)[ixs]
else:
ticklabels = self.mne.ch_names[self.mne.picks]
texts = self.mne.ax_main.set_yticklabels(ticklabels, picker=True)
for text in texts:
sty = (
"italic" if text.get_text() in self.mne.whitened_ch_names else "normal"
)
text.set_style(sty)
def _xtick_formatter(self, x, pos=None, ax_type="main"):
"""Change the x-axis labels."""
tickdiff = np.diff(self.mne.ax_main.get_xticks())[0]
digits = np.ceil(-np.log10(tickdiff) + 1).astype(int)
# always show millisecond precision for vline text
if ax_type == "vline":
digits = 3
if self.mne.time_format == "float":
# round to integers when possible ('9.0' → '9')
if int(x) == x:
digits = None
if ax_type == "vline":
return f"{round(x, digits)} s"
return str(round(x, digits))
# format as timestamp
meas_date = self.mne.inst.info["meas_date"]
first_time = datetime.timedelta(seconds=self.mne.inst.first_time)
xtime = datetime.timedelta(seconds=x)
xdatetime = meas_date + first_time + xtime
xdtstr = xdatetime.strftime("%H:%M:%S")
if digits and ax_type != "hscroll" and int(xdatetime.microsecond):
xdtstr += f"{round(xdatetime.microsecond * 1e-6, digits)}"[1:]
return xdtstr
def _toggle_time_format(self):
if self.mne.time_format == "float":
self.mne.time_format = "clock"
x_axis_label = "Time (HH:MM:SS)"
else:
self.mne.time_format = "float"
x_axis_label = "Time (s)"
# Change x-axis label
for _ax in (self.mne.ax_main, self.mne.ax_hscroll):
_ax.set_xlabel(x_axis_label)
self._redraw(update_data=False, annotations=False)
# Update vline-text if displayed
if self.mne.vline is not None and self.mne.vline.get_visible():
self._show_vline(self.mne.vline.get_xdata())
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# DATA TRACES
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
def _toggle_butterfly(self):
"""Enter or leave butterfly mode."""
self.mne.ax_vscroll.set_visible(self.mne.butterfly)
self.mne.butterfly = not self.mne.butterfly
self.mne.scale_factor *= 0.5 if self.mne.butterfly else 2.0
self._update_picks()
self._update_trace_offsets()
self._redraw(annotations=True)
if self.mne.fig_selection is not None:
self.mne.fig_selection._style_radio_buttons_butterfly()
def _update_trace_offsets(self):
"""Compute viewport height and adjust offsets."""
# simultaneous selection and butterfly modes
if self.mne.butterfly and self.mne.ch_selections is not None:
self._update_picks()
selections_dict = self._make_butterfly_selections_dict()
n_offsets = len(selections_dict)
sel_order = list(selections_dict)
offsets = np.array([])
for pick in self.mne.picks:
for sel in sel_order:
if pick in selections_dict[sel]:
offsets = np.append(offsets, sel_order.index(sel))
# butterfly only
elif self.mne.butterfly:
unique_ch_types = set(self.mne.ch_types)
n_offsets = len(unique_ch_types)
ch_type_order = [
_type
for _type in _DATA_CH_TYPES_ORDER_DEFAULT
if _type in unique_ch_types
]
offsets = np.array(
[ch_type_order.index(ch_type) for ch_type in self.mne.ch_types]
)
# normal mode
else:
n_offsets = self.mne.n_channels
offsets = np.arange(n_offsets, dtype=float)
# update ylim, ticks, vertline, and scrollbar patch
ylim = (n_offsets - 0.5, -0.5) # inverted y axis → new chs at bottom
self.mne.ax_main.set_ylim(ylim)
self.mne.ax_main.set_yticks(np.unique(offsets))
self.mne.vsel_patch.set_height(self.mne.n_channels)
# store new offsets, update axis labels
self.mne.trace_offsets = offsets
self._update_yaxis_labels()
def _draw_traces(self):
"""Draw (or redraw) the channel data."""
from matplotlib.colors import to_rgba_array
from matplotlib.patches import Rectangle
# clear scalebars
if self.mne.scalebars_visible:
self._hide_scalebars()
# get info about currently visible channels
picks = self.mne.picks
ch_names = self.mne.ch_names[picks]
ch_types = self.mne.ch_types[picks]
offset_ixs = (
picks
if self.mne.butterfly and self.mne.ch_selections is None
else slice(None)
)
offsets = self.mne.trace_offsets[offset_ixs]
bad_bool = np.isin(ch_names, self.mne.info["bads"])
# colors
good_ch_colors = [self.mne.ch_color_dict[_type] for _type in ch_types]
ch_colors = to_rgba_array(
[
self.mne.ch_color_bad if _bad else _color
for _bad, _color in zip(bad_bool, good_ch_colors)
]
)
self.mne.ch_colors = np.array(good_ch_colors) # use for unmarking bads
labels = self.mne.ax_main.yaxis.get_ticklabels()
if self.mne.butterfly:
for label in labels:
label.set_color(self.mne.fgcolor)
else:
for label, color in zip(labels, ch_colors):
label.set_color(color)
# decim
decim = np.ones_like(picks)
data_picks_mask = np.isin(picks, self.mne.picks_data)
decim[data_picks_mask] = self.mne.decim
# decim can vary by channel type, so compute different `times` vectors
decim_times = {
decim_value: self.mne.times[::decim_value] + self.mne.first_time
for decim_value in set(decim)
}
# add more traces if needed
n_picks = len(picks)
if n_picks > len(self.mne.traces):
n_new_chs = n_picks - len(self.mne.traces)
new_traces = self.mne.ax_main.plot(
np.full((1, n_new_chs), np.nan), **self.mne.trace_kwargs
)
self.mne.traces.extend(new_traces)
# remove extra traces if needed
extra_traces = self.mne.traces[n_picks:]
for trace in extra_traces:
trace.remove()
self.mne.traces = self.mne.traces[:n_picks]
# check for bad epochs
time_range = (self.mne.times + self.mne.first_time)[[0, -1]]
if self.mne.instance_type == "epochs":
epoch_ix = np.searchsorted(self.mne.boundary_times, time_range)
epoch_ix = np.arange(epoch_ix[0], epoch_ix[1])
epoch_nums = self.mne.inst.selection[epoch_ix[0] : epoch_ix[-1] + 1]
(visible_bad_epoch_ix,) = np.isin(epoch_nums, self.mne.bad_epochs).nonzero()
while len(self.mne.epoch_traces):
self.mne.epoch_traces.pop(-1).remove()
# handle custom epoch colors (for autoreject integration)
if self.mne.epoch_colors is None:
# shape: n_traces × RGBA → n_traces × n_epochs × RGBA
custom_colors = np.tile(
ch_colors[:, None, :], (1, self.mne.n_epochs, 1)
)
else:
custom_colors = np.empty((len(self.mne.picks), self.mne.n_epochs, 4))
for ii, _epoch_ix in enumerate(epoch_ix):
this_colors = self.mne.epoch_colors[_epoch_ix]
custom_colors[:, ii] = to_rgba_array(
[this_colors[_ch] for _ch in picks]
)
# override custom color on bad epochs
for _ix in visible_bad_epoch_ix:
_cols = np.array(
[self.mne.epoch_color_bad, self.mne.ch_color_bad], dtype=object
)[bad_bool.astype(int)]
custom_colors[:, _ix] = to_rgba_array(_cols)
# update traces
ylim = self.mne.ax_main.get_ylim()
for ii, line in enumerate(self.mne.traces):
this_name = ch_names[ii]
this_type = ch_types[ii]
this_offset = offsets[ii]
this_times = decim_times[decim[ii]]
this_data = this_offset - self.mne.data[ii] * self.mne.scale_factor
this_data = this_data[..., :: decim[ii]]
# clip
if self.mne.clipping == "clamp":
this_data = np.clip(this_data, -0.5, 0.5)
elif self.mne.clipping is not None:
clip = self.mne.clipping * (0.2 if self.mne.butterfly else 1)
bottom = max(this_offset - clip, ylim[1])
height = min(2 * clip, ylim[0] - bottom)
rect = Rectangle(
xy=np.array([time_range[0], bottom]),
width=time_range[1] - time_range[0],
height=height,
transform=self.mne.ax_main.transData,
)
line.set_clip_path(rect)
# prep z order
is_bad_ch = this_name in self.mne.info["bads"]
this_z = self.mne.zorder["bads" if is_bad_ch else "data"]
if self.mne.butterfly and not is_bad_ch:
this_z = self.mne.zorder.get(this_type, this_z)
# plot each trace multiple times to get the desired epoch coloring.
# use masked arrays to plot discontinuous epochs that have the same
# color in a single plot() call.
if self.mne.instance_type == "epochs":
this_colors = custom_colors[ii]
for cix, color in enumerate(np.unique(this_colors, axis=0)):
bool_ixs = (this_colors == color).all(axis=1)
mask = np.zeros_like(this_times, dtype=bool)
_starts = self.mne.boundary_times[epoch_ix][bool_ixs]
_stops = self.mne.boundary_times[epoch_ix + 1][bool_ixs]
for _start, _stop in zip(_starts, _stops):
_mask = np.logical_and(_start < this_times, this_times <= _stop)
mask = mask | _mask
_times = np.ma.masked_array(this_times, mask=~mask)
# always use the existing traces first
if cix == 0:
line.set_xdata(_times)
line.set_ydata(this_data)
line.set_color(color)
line.set_zorder(this_z)
else: # make new traces as needed
_trace = self.mne.ax_main.plot(
_times,
this_data,
color=color,
zorder=this_z,
**self.mne.trace_kwargs,
)
self.mne.epoch_traces.extend(_trace)
else:
line.set_xdata(this_times)
line.set_ydata(this_data)
line.set_color(ch_colors[ii])
line.set_zorder(this_z)
# update xlim
self.mne.ax_main.set_xlim(*time_range)
# draw scalebars maybe
if self.mne.scalebars_visible:
self._show_scalebars()
# redraw event lines
if self.mne.event_times is not None:
self._draw_event_lines()
def _redraw(self, update_data=True, annotations=False):
"""Redraw (convenience method for frequently grouped actions)."""
super()._redraw(update_data, annotations)
if self.mne.vline_visible and self.mne.is_epochs:
# prevent flickering
_ = self._recompute_epochs_vlines(None)
self.canvas.draw_idle()
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# EVENT LINES AND MARKER LINES
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
def _draw_event_lines(self):
"""Draw the event lines and their labels."""
from matplotlib.collections import LineCollection
from matplotlib.colors import to_rgba_array
if self.mne.event_nums is not None:
mask = np.logical_and(
self.mne.event_times >= self.mne.times[0],
self.mne.event_times <= self.mne.times[-1],
)
this_event_times = self.mne.event_times[mask]
this_event_nums = self.mne.event_nums[mask]
n_visible_events = len(this_event_times)
colors = to_rgba_array(
[self.mne.event_color_dict[n] for n in this_event_nums]
)
# create event lines
ylim = self.mne.ax_main.get_ylim()
xs = np.repeat(this_event_times, 2)
ys = np.tile(ylim, n_visible_events)
segs = np.vstack([xs, ys]).T.reshape(n_visible_events, 2, 2)
event_lines = LineCollection(
segs, linewidths=0.5, colors=colors, zorder=self.mne.zorder["events"]
)
self.mne.ax_main.add_collection(event_lines)
self.mne.event_lines = event_lines
# create event labels
while len(self.mne.event_texts):
self.mne.event_texts.pop().remove()
for _t, _n, _c in zip(this_event_times, this_event_nums, colors):
label = self.mne.event_id_rev.get(_n, _n)
this_text = self.mne.ax_main.annotate(
label,
(_t, ylim[1]),
ha="center",
va="baseline",
color=self.mne.fgcolor,
xytext=(0, 2),
textcoords="offset points",
fontsize=8,
)
self.mne.event_texts.append(this_text)
def _recompute_epochs_vlines(self, xdata):
"""Recompute vline x-coords for epochs plots (after scrolling, etc)."""
# special case: changed view duration w/ "home" or "end" key
# (no click event, hence no xdata)
if xdata is None:
xdata = np.array(self.mne.vline.get_segments())[0, 0, 0]
# compute the (continuous) times for the lines on each epoch
epoch_dur = np.diff(self.mne.boundary_times[:2])[0]
rel_time = xdata % epoch_dur
abs_time = self.mne.times[0]
xs = np.arange(self.mne.n_epochs) * epoch_dur + abs_time + rel_time
segs = np.array(self.mne.vline.get_segments())
# recreate segs from scratch in case view duration changed
# (i.e., handle case when n_segments != n_epochs)
segs = np.tile([[0.0], [1.0]], (len(xs), 1, 2)) # y values
segs[..., 0] = np.tile(xs[:, None], 2) # x values
self.mne.vline.set_segments(segs)
return rel_time
def _show_vline(self, xdata):
"""Show the vertical line(s)."""
if self.mne.is_epochs:
# convert xdata to be epoch-relative (for the text)
rel_time = self._recompute_epochs_vlines(xdata)
xdata = rel_time + self.mne.inst.times[0]
else:
self.mne.vline.set_xdata([xdata])
self.mne.vline_hscroll.set_xdata([xdata])
text = self._xtick_formatter(xdata, ax_type="vline")[:12]
self.mne.vline_text.set_text(text)
self._toggle_vline(True)
def _toggle_vline(self, visible):
"""Show or hide the vertical line(s)."""
for artist in (self.mne.vline, self.mne.vline_hscroll, self.mne.vline_text):
if artist is not None:
artist.set_visible(visible)
self.draw_artist(artist)
self.mne.vline_visible = visible
self.canvas.draw_idle()
# workaround: plt.close() doesn't spawn close_event on Agg backend, this method
# can be removed once the _close_event in fixes.py is removed
def _close_event(self, fig=None):
"""Force calling of the MPL figure close event."""
fig = fig or self
_close_event(fig)
def _fake_keypress(self, key, fig=None):
fig = fig or self
_fake_keypress(fig, key)
def _fake_click(
self,
point,
add_points=None,
fig=None,
ax=None,
xform="ax",
button=1,
kind="press",
):
"""Fake a click at a relative point within axes."""
fig = fig or self
ax = ax or self.mne.ax_main
if kind == "drag" and add_points is not None:
_fake_click(
fig=fig, ax=ax, point=point, xform=xform, button=button, kind="press"
)
for apoint in add_points:
_fake_click(
fig=fig,
ax=ax,
point=apoint,
xform=xform,
button=button,
kind="motion",
)
_fake_click(
fig=fig,
ax=ax,
point=add_points[-1],
xform=xform,
button=button,
kind="release",
)
else:
_fake_click(
fig=fig, ax=ax, point=point, xform=xform, button=button, kind=kind
)
def _fake_scroll(self, x, y, step, fig=None):
fig = fig or self
_fake_scroll(fig, x, y, step)
def _click_ch_name(self, ch_index, button):
_click_ch_name(self, ch_index, button)
def _resize_by_factor(self, factor=None):
size = self.canvas.manager.canvas.get_width_height()
if isinstance(factor, tuple):
size = int(size[0] * factor[0], size[1] * factor[1])
else:
size = [int(x * factor) for x in size]
self.canvas.manager.resize(*size)
def _get_ticklabels(self, orientation):
if orientation == "x":
labels = self.mne.ax_main.get_xticklabels(minor=self.mne.is_epochs)
elif orientation == "y":
labels = self.mne.ax_main.get_yticklabels()
label_texts = [lb.get_text() for lb in labels]
return label_texts
def _get_scale_bar_texts(self):
texts = tuple(t.get_text().strip() for t in self.mne.ax_main.texts)
# First text is empty because of vline
texts = texts[1:]
return texts
class MNELineFigure(MNEFigure):
"""Interactive figure for non-scrolling line plots."""
def __init__(self, inst, n_axes, figsize, *, layout="constrained", **kwargs):
super().__init__(
figsize=figsize,
inst=inst,
layout=layout,
sharex=True,
**kwargs,
)
for ix in range(n_axes):
self.add_subplot(n_axes, 1, ix + 1)
def _close_all():
"""Close all figures (only used in our tests)."""
plt.close("all")
def _get_n_figs():
return len(plt.get_fignums())
def _figure(toolbar=True, FigureClass=MNEFigure, **kwargs):
"""Instantiate a new figure."""
from matplotlib import rc_context
title = kwargs.pop("window_title", None) # extract title before init
if "layout" not in kwargs:
kwargs["layout"] = "constrained"
rc = dict() if toolbar else dict(toolbar="none")
with rc_context(rc=rc):
fig = plt.figure(FigureClass=FigureClass, **kwargs)
# BACKEND defined globally at the top of this file
fig.mne.backend = BACKEND
if title is not None:
_set_window_title(fig, title)
# TODO: for some reason for topomaps->_prepare_trellis the layout=constrained does
# not work the first time (maybe toolbar=False?)
if kwargs.get("layout") == "constrained":
fig.set_layout_engine("constrained")
# add event callbacks
fig._add_default_callbacks()
return fig
def _line_figure(inst, axes=None, picks=None, **kwargs):
"""Instantiate a new line figure."""
from matplotlib.axes import Axes
# if picks is None, only show data channels
allowed_ch_types = _DATA_CH_TYPES_SPLIT if picks is None else _VALID_CHANNEL_TYPES
# figure out expected number of axes
ch_types = np.array(inst.get_channel_types())
if picks is not None:
ch_types = ch_types[picks]
n_axes = len(np.intersect1d(ch_types, allowed_ch_types))
# handle user-provided axes
if axes is not None:
if isinstance(axes, Axes):
axes = [axes]
_validate_if_list_of_axes(axes, n_axes)
fig = axes[0].get_figure()
else:
figsize = kwargs.pop("figsize", (10, 2.5 * n_axes + 1))
fig = _figure(
inst=inst,
toolbar=True,
FigureClass=MNELineFigure,
figsize=figsize,
n_axes=n_axes,
**kwargs,
)
fig.mne.fig_size_px = fig._get_size_px() # can't do in __init__
axes = fig.axes
return fig, axes
def _split_picks_by_type(inst, picks, units, scalings, titles):
"""Separate picks, units, etc, for plotting on separate subplots."""
picks_list = list()
units_list = list()
scalings_list = list()
titles_list = list()
# if picks is None, only show data channels
allowed_ch_types = _DATA_CH_TYPES_SPLIT if picks is None else _VALID_CHANNEL_TYPES
for ch_type in allowed_ch_types:
pick_kwargs = dict(meg=False, ref_meg=False, exclude=[])
if ch_type in ("mag", "grad"):
pick_kwargs["meg"] = ch_type
elif ch_type in _FNIRS_CH_TYPES_SPLIT:
pick_kwargs["fnirs"] = ch_type
elif ch_type in _EYETRACK_CH_TYPES_SPLIT:
pick_kwargs["eyetrack"] = ch_type
else:
pick_kwargs[ch_type] = True
these_picks = pick_types(inst.info, **pick_kwargs)
these_picks = np.intersect1d(these_picks, picks)
if len(these_picks) > 0:
picks_list.append(these_picks)
units_list.append(units[ch_type])
scalings_list.append(scalings[ch_type])
titles_list.append(titles[ch_type])
if len(picks_list) == 0:
raise RuntimeError("No data channels found")
return picks_list, units_list, scalings_list, titles_list
def _calc_new_margins(fig, old_width, old_height, new_width, new_height):
"""Compute new figure-relative values to maintain fixed-size margins."""
new_margins = dict()
for side in ("left", "right", "bottom", "top"):
ratio = (
(old_width / new_width)
if side in ("left", "right")
else (old_height / new_height)
)
rel_dim = getattr(fig.subplotpars, side)
if side in ("right", "top"):
new_margins[side] = 1 - ratio * (1 - rel_dim)
else:
new_margins[side] = ratio * rel_dim
# gh-8304: don't allow resizing too small
if (
new_margins["bottom"] < new_margins["top"]
and new_margins["left"] < new_margins["right"]
):
return new_margins
@contextmanager
def _patched_canvas(fig):
old_canvas = fig.canvas
if fig.canvas is None: # XXX old MPL (at least 3.0.3) does this for Agg
fig.canvas = Bunch(mpl_connect=lambda event, callback: None)
try:
yield
finally:
fig.canvas = old_canvas
def _init_browser(**kwargs):
"""Instantiate a new MNE browse-style figure."""
from mne.io import BaseRaw
fig = _figure(toolbar=False, FigureClass=MNEBrowseFigure, layout=None, **kwargs)
# splash is ignored (maybe we could do it for mpl if we get_backend() and
# check if it's Qt... but seems overkill)
# initialize zen mode
# (can't do in __init__ due to get_position() calls)
fig.canvas.draw()
fig._update_zen_mode_offsets()
fig._resize(None) # needed for MPL
# if scrollbars are supposed to start hidden,
# set to True and then toggle
if not fig.mne.scrollbars_visible:
fig.mne.scrollbars_visible = True
fig._toggle_scrollbars()
# Initialize parts of the plot
is_ica = fig.mne.instance_type == "ica"
if not is_ica:
# make channel selection dialog,
# if requested (doesn't work well in init)
if fig.mne.group_by in ("selection", "position"):
fig._create_selection_fig()
# start with projectors dialog open, if requested
if getattr(fig.mne, "show_options", False):
fig._toggle_proj_fig()
# update data, and plot
fig._update_trace_offsets()
fig._redraw(update_data=True, annotations=False)
if isinstance(fig.mne.inst, BaseRaw):
fig._setup_annotation_colors()
fig._draw_annotations()
return fig
def _get_check_kwargs(labels=None):
check_kwargs = dict()
if not _OLD_BUTTONS:
check_kwargs.update(
check_props=dict(s=144, clip_on=False),
frame_props=dict(s=144, clip_on=False),
)
if labels is not None:
textcolor = list()
checkcolor = list()
for label in labels:
if label.endswith("(already applied)"):
textcolor.append("0.5")
checkcolor.append("0.7")
else:
textcolor.append("k")
checkcolor.append("k")
check_kwargs["check_props"].update(facecolor=checkcolor, linewidth=1)
check_kwargs["frame_props"].update(edgecolor=checkcolor, linewidth=1)
check_kwargs["label_props"] = dict(color=textcolor)
return check_kwargs