1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893
|
# Authors: The MNE-Python contributors.
# License: BSD-3-Clause
# Copyright the MNE-Python contributors.
from collections import Counter
from functools import partial
from math import factorial
from os import path as op
from pathlib import Path
import numpy as np
from scipy import linalg
from scipy.special import lpmv, sph_harm
from .. import __version__
from .._fiff.compensator import make_compensator
from .._fiff.constants import FIFF, FWD
from .._fiff.meas_info import Info, _simplify_info
from .._fiff.pick import pick_info, pick_types
from .._fiff.proc_history import _read_ctc
from .._fiff.proj import Projection
from .._fiff.tag import _coil_trans_to_loc, _loc_to_coil_trans
from .._fiff.write import DATE_NONE, _generate_meas_id
from ..annotations import _annotations_starts_stops
from ..bem import _check_origin
from ..channels.channels import _get_T1T2_mag_inds, fix_mag_coil_types
from ..fixes import _safe_svd, bincount
from ..forward import _concatenate_coils, _create_meg_coils, _prep_meg_channels
from ..io import BaseRaw, RawArray
from ..surface import _normalize_vectors
from ..transforms import (
Transform,
_average_quats,
_cart_to_sph,
_deg_ord_idx,
_find_vector_rotation,
_get_n_moments,
_get_trans,
_sh_complex_to_real,
_sh_negate,
_sh_real_to_complex,
_sph_to_cart_partials,
_str_to_frame,
apply_trans,
quat_to_rot,
rot_to_quat,
)
from ..utils import (
_check_option,
_clean_names,
_ensure_int,
_pl,
_time_mask,
_validate_type,
logger,
use_log_level,
verbose,
warn,
)
# Note: MF uses single precision and some algorithms might use
# truncated versions of constants (e.g., μ0), which could lead to small
# differences between algorithms
@verbose
def maxwell_filter_prepare_emptyroom(
raw_er,
*,
raw,
bads="from_raw",
annotations="from_raw",
meas_date="keep",
emit_warning=False,
verbose=None,
):
"""Prepare an empty-room recording for Maxwell filtering.
Empty-room data by default lacks certain properties that are required to
ensure running :func:`~mne.preprocessing.maxwell_filter` will process the
empty-room recording the same way as the experimental data. This function
preconditions an empty-room raw data instance accordingly so it can be used
for Maxwell filtering. Please see the ``Notes`` section for details.
Parameters
----------
raw_er : instance of Raw
The empty-room recording. It will not be modified.
raw : instance of Raw
The experimental recording, typically this will be the reference run
used for Maxwell filtering.
bads : 'from_raw' | 'union' | 'keep'
How to populate the list of bad channel names to be injected into
the empty-room recording. If ``'from_raw'`` (default) the list of bad
channels will be overwritten with that of ``raw``. If ``'union'``, will
use the union of bad channels in ``raw`` and ``raw_er``. Note that
this may lead to additional bad channels in the empty-room in
comparison to the experimental recording. If ``'keep'``, don't alter
the existing list of bad channels.
.. note::
Non-MEG channels are silently dropped from the list of bads.
annotations : 'from_raw' | 'union' | 'keep'
Whether to copy the annotations over from ``raw`` (default),
use the union of the annotations, or to keep them unchanged.
meas_date : 'keep' | 'from_raw'
Whether to transfer the measurement date from ``raw`` or to keep
it as is (default). If you intend to manually transfer annotations
from ``raw`` **after** running this function, you should set this to
``'from_raw'``.
%(emit_warning)s
Unlike :meth:`raw.set_annotations <mne.io.Raw.set_annotations>`, the
default here is ``False``, as empty-room recordings are often shorter
than raw.
%(verbose)s
Returns
-------
raw_er_prepared : instance of Raw
A copy of the passed empty-room recording, ready for Maxwell filtering.
Notes
-----
This function will:
* Compile the list of bad channels according to the ``bads`` parameter.
* Inject the device-to-head transformation matrix from the experimental
recording into the empty-room recording.
* Set the following properties of the empty-room recording to match the
experimental recording:
* Montage
* ``raw.first_time`` and ``raw.first_samp``
* Adjust annotations according to the ``annotations`` parameter.
* Adjust the measurement date according to the ``meas_date`` parameter.
.. versionadded:: 1.1
""" # noqa: E501
_validate_type(item=raw_er, types=BaseRaw, item_name="raw_er")
_validate_type(item=raw, types=BaseRaw, item_name="raw")
_validate_type(item=bads, types=str, item_name="bads")
_check_option(
parameter="bads", value=bads, allowed_values=["from_raw", "union", "keep"]
)
_validate_type(item=annotations, types=str, item_name="annotations")
_check_option(
parameter="annotations",
value=annotations,
allowed_values=["from_raw", "union", "keep"],
)
_validate_type(item=meas_date, types=str, item_name="meas_date")
_check_option(
parameter="meas_date", value=annotations, allowed_values=["from_raw", "keep"]
)
raw_er_prepared = raw_er.copy()
del raw_er # just to be sure
# handle bads; only keep MEG channels
if bads == "from_raw":
bads = raw.info["bads"]
elif bads == "union":
bads = sorted(set(raw.info["bads"] + raw_er_prepared.info["bads"]))
elif bads == "keep":
bads = raw_er_prepared.info["bads"]
bads = [ch_name for ch_name in bads if ch_name.startswith("MEG")]
raw_er_prepared.info["bads"] = bads
# handle dev_head_t
raw_er_prepared.info["dev_head_t"] = raw.info["dev_head_t"]
# handle montage
montage = raw.get_montage()
raw_er_prepared.set_montage(montage)
# handle first_samp
raw_er_prepared.annotations.onset += raw.first_time - raw_er_prepared.first_time
# don't copy _cropped_samp directly, as sfreqs may differ
raw_er_prepared._cropped_samp = raw_er_prepared.time_as_index(raw.first_time).item()
# handle annotations
if annotations != "keep":
er_annot = raw_er_prepared.annotations
if annotations == "from_raw":
er_annot.delete(np.arange(len(er_annot)))
er_annot.append(
raw.annotations.onset,
raw.annotations.duration,
raw.annotations.description,
raw.annotations.ch_names,
)
if raw_er_prepared.info["meas_date"] is None:
er_annot.onset -= raw_er_prepared.first_time
raw_er_prepared.set_annotations(er_annot, emit_warning)
# handle measurement date
if meas_date == "from_raw":
raw_er_prepared.set_meas_date(raw.info["meas_date"])
return raw_er_prepared
# Changes to arguments here should also be made in find_bad_channels_maxwell
@verbose
def maxwell_filter(
raw,
origin="auto",
int_order=8,
ext_order=3,
calibration=None,
cross_talk=None,
st_duration=None,
st_correlation=0.98,
coord_frame="head",
destination=None,
regularize="in",
ignore_ref=False,
bad_condition="error",
head_pos=None,
st_fixed=True,
st_only=False,
mag_scale=100.0,
skip_by_annotation=("edge", "bad_acq_skip"),
extended_proj=(),
verbose=None,
):
"""Maxwell filter data using multipole moments.
Parameters
----------
raw : instance of Raw
Data to be filtered.
.. warning:: It is critical to mark bad channels in
``raw.info['bads']`` prior to processing in order to
prevent artifact spreading. Manual inspection and use
of :func:`~find_bad_channels_maxwell` is recommended.
%(origin_maxwell)s
%(int_order_maxwell)s
%(ext_order_maxwell)s
%(calibration_maxwell_cal)s
%(cross_talk_maxwell)s
st_duration : float | None
If not None, apply spatiotemporal SSS with specified buffer duration
(in seconds). MaxFilter™'s default is 10.0 seconds in v2.2.
Spatiotemporal SSS acts as implicitly as a high-pass filter where the
cut-off frequency is 1/st_duration Hz. For this (and other) reasons,
longer buffers are generally better as long as your system can handle
the higher memory usage. To ensure that each window is processed
identically, choose a buffer length that divides evenly into your data.
Any data at the trailing edge that doesn't fit evenly into a whole
buffer window will be lumped into the previous buffer.
st_correlation : float
Correlation limit between inner and outer subspaces used to reject
overlapping intersecting inner/outer signals during spatiotemporal SSS.
%(coord_frame_maxwell)s
%(destination_maxwell_dest)s
%(regularize_maxwell_reg)s
%(ignore_ref_maxwell)s
%(bad_condition_maxwell_cond)s
%(head_pos_maxwell)s
.. versionadded:: 0.12
%(st_fixed_maxwell_only)s
%(mag_scale_maxwell)s
.. versionadded:: 0.13
%(skip_by_annotation_maxwell)s
.. versionadded:: 0.17
%(extended_proj_maxwell)s
%(verbose)s
Returns
-------
raw_sss : instance of Raw
The raw data with Maxwell filtering applied.
See Also
--------
mne.preprocessing.annotate_amplitude
mne.preprocessing.find_bad_channels_maxwell
mne.chpi.filter_chpi
mne.chpi.read_head_pos
mne.epochs.average_movements
Notes
-----
.. versionadded:: 0.11
Some of this code was adapted and relicensed (with BSD form) with
permission from Jussi Nurminen. These algorithms are based on work
from :footcite:`TauluKajola2005` and :footcite:`TauluSimola2006`.
It will likely use multiple CPU cores, see the :ref:`FAQ <faq_cpu>`
for more information.
.. warning:: Maxwell filtering in MNE is not designed or certified
for clinical use.
Compared to the MEGIN MaxFilter™ software, the MNE Maxwell filtering
routines currently provide the following features:
.. table::
:widths: auto
+-----------------------------------------------------------------------------+-----+-----------+
| Feature | MNE | MaxFilter |
+=============================================================================+=====+===========+
| Maxwell filtering software shielding | ✓ | ✓ |
+-----------------------------------------------------------------------------+-----+-----------+
| Bad channel reconstruction | ✓ | ✓ |
+-----------------------------------------------------------------------------+-----+-----------+
| Cross-talk cancellation | ✓ | ✓ |
+-----------------------------------------------------------------------------+-----+-----------+
| Fine calibration correction (1D) | ✓ | ✓ |
+-----------------------------------------------------------------------------+-----+-----------+
| Fine calibration correction (3D) | ✓ | |
+-----------------------------------------------------------------------------+-----+-----------+
| Spatio-temporal SSS (tSSS) | ✓ | ✓ |
+-----------------------------------------------------------------------------+-----+-----------+
| Coordinate frame translation | ✓ | ✓ |
+-----------------------------------------------------------------------------+-----+-----------+
| Regularization using information theory | ✓ | ✓ |
+-----------------------------------------------------------------------------+-----+-----------+
| Movement compensation (raw) | ✓ | ✓ |
+-----------------------------------------------------------------------------+-----+-----------+
| Movement compensation (:func:`epochs <mne.epochs.average_movements>`) | ✓ | |
+-----------------------------------------------------------------------------+-----+-----------+
| :func:`cHPI subtraction <mne.chpi.filter_chpi>` | ✓ | ✓ |
+-----------------------------------------------------------------------------+-----+-----------+
| Double floating point precision | ✓ | |
+-----------------------------------------------------------------------------+-----+-----------+
| Seamless processing of split (``-1.fif``) and concatenated files | ✓ | |
+-----------------------------------------------------------------------------+-----+-----------+
| Automatic bad channel detection (:func:`~find_bad_channels_maxwell`) | ✓ | ✓ |
+-----------------------------------------------------------------------------+-----+-----------+
| Head position estimation (:func:`~mne.chpi.compute_head_pos`) | ✓ | ✓ |
+-----------------------------------------------------------------------------+-----+-----------+
| Certified for clinical use | | ✓ |
+-----------------------------------------------------------------------------+-----+-----------+
| Extended external basis (eSSS) | ✓ | |
+-----------------------------------------------------------------------------+-----+-----------+
Epoch-based movement compensation is described in :footcite:`TauluKajola2005`.
Use of Maxwell filtering routines with non-Neuromag systems is currently
**experimental**. Worse results for non-Neuromag systems are expected due
to (at least):
* Missing fine-calibration and cross-talk cancellation data for
other systems.
* Processing with reference sensors has not been vetted.
* Regularization of components may not work well for all systems.
* Coil integration has not been optimized using Abramowitz/Stegun
definitions.
.. note:: Various Maxwell filtering algorithm components are covered by
patents owned by MEGIN. These patents include, but may not be
limited to:
- US2006031038 (Signal Space Separation)
- US6876196 (Head position determination)
- WO2005067789 (DC fields)
- WO2005078467 (MaxShield)
- WO2006114473 (Temporal Signal Space Separation)
These patents likely preclude the use of Maxwell filtering code
in commercial applications. Consult a lawyer if necessary.
Currently, in order to perform Maxwell filtering, the raw data must not
have any projectors applied. During Maxwell filtering, the spatial
structure of the data is modified, so projectors are discarded (unless
in ``st_only=True`` mode).
References
----------
.. footbibliography::
""" # noqa: E501
logger.info("Maxwell filtering raw data")
params = _prep_maxwell_filter(
raw=raw,
origin=origin,
int_order=int_order,
ext_order=ext_order,
calibration=calibration,
cross_talk=cross_talk,
st_duration=st_duration,
st_correlation=st_correlation,
coord_frame=coord_frame,
destination=destination,
regularize=regularize,
ignore_ref=ignore_ref,
bad_condition=bad_condition,
head_pos=head_pos,
st_fixed=st_fixed,
st_only=st_only,
mag_scale=mag_scale,
skip_by_annotation=skip_by_annotation,
extended_proj=extended_proj,
)
raw_sss = _run_maxwell_filter(raw, **params)
# Update info
_update_sss_info(raw_sss, **params["update_kwargs"])
logger.info("[done]")
return raw_sss
@verbose
def _prep_maxwell_filter(
raw,
origin="auto",
int_order=8,
ext_order=3,
calibration=None,
cross_talk=None,
st_duration=None,
st_correlation=0.98,
coord_frame="head",
destination=None,
regularize="in",
ignore_ref=False,
bad_condition="error",
head_pos=None,
st_fixed=True,
st_only=False,
mag_scale=100.0,
skip_by_annotation=("edge", "bad_acq_skip"),
extended_proj=(),
reconstruct="in",
verbose=None,
):
# There are an absurd number of different possible notations for spherical
# coordinates, which confounds the notation for spherical harmonics. Here,
# we purposefully stay away from shorthand notation in both and use
# explicit terms (like 'azimuth' and 'polar') to avoid confusion.
# See mathworld.wolfram.com/SphericalHarmonic.html for more discussion.
# Our code follows the same standard that ``scipy`` uses for ``sph_harm``.
# triage inputs ASAP to avoid late-thrown errors
_validate_type(raw, BaseRaw, "raw")
_check_usable(raw, ignore_ref)
_check_regularize(regularize)
st_correlation = float(st_correlation)
if st_correlation <= 0.0 or st_correlation > 1.0:
raise ValueError(f"Need 0 < st_correlation <= 1., got {st_correlation}")
_check_option("coord_frame", coord_frame, ["head", "meg"])
head_frame = True if coord_frame == "head" else False
recon_trans = _check_destination(destination, raw.info, head_frame)
if st_duration is not None:
st_duration = float(st_duration)
st_correlation = float(st_correlation)
st_duration = int(round(st_duration * raw.info["sfreq"]))
if not 0.0 < st_correlation <= 1:
raise ValueError("st_correlation must be between 0. and 1.")
_check_option(
"bad_condition", bad_condition, ["error", "warning", "ignore", "info"]
)
if raw.info["dev_head_t"] is None and coord_frame == "head":
raise RuntimeError(
'coord_frame cannot be "head" because '
'info["dev_head_t"] is None; if this is an '
"empty room recording, consider using "
'coord_frame="meg"'
)
if st_only and st_duration is None:
raise ValueError("st_duration must not be None if st_only is True")
head_pos = _check_pos(head_pos, head_frame, raw, st_fixed, raw.info["sfreq"])
_check_info(
raw.info,
sss=not st_only,
tsss=st_duration is not None,
calibration=not st_only and calibration is not None,
ctc=not st_only and cross_talk is not None,
)
# Now we can actually get moving
info = raw.info.copy()
meg_picks, mag_picks, grad_picks, good_mask, mag_or_fine = _get_mf_picks_fix_mags(
info, int_order, ext_order, ignore_ref
)
# Magnetometers are scaled to improve numerical stability
coil_scale, mag_scale = _get_coil_scale(
meg_picks, mag_picks, grad_picks, mag_scale, info
)
#
# Extended projection vectors
#
_validate_type(extended_proj, (list, tuple), "extended_proj")
good_names = [info["ch_names"][c] for c in meg_picks[good_mask]]
if len(extended_proj) > 0:
extended_proj_ = list()
for pi, proj in enumerate(extended_proj):
item = f"extended_proj[{pi}]"
_validate_type(proj, Projection, item)
got_names = proj["data"]["col_names"]
missing = sorted(set(good_names) - set(got_names))
if missing:
raise ValueError(
f"{item} channel names were missing some "
f"good MEG channel names:\n{', '.join(missing)}"
)
idx = [got_names.index(name) for name in good_names]
extended_proj_.append(proj["data"]["data"][:, idx])
extended_proj = np.concatenate(extended_proj_)
logger.info(
" Extending external SSS basis using %d projection " "vectors",
len(extended_proj),
)
#
# Fine calibration processing (load fine cal and overwrite sensor geometry)
#
sss_cal = dict()
if calibration is not None:
# Modifies info in place, so make a copy for recon later
info_recon = info.copy()
calibration, sss_cal = _update_sensor_geometry(info, calibration, ignore_ref)
mag_or_fine.fill(True) # all channels now have some mag-type data
else:
info_recon = info
# Determine/check the origin of the expansion
origin = _check_origin(origin, info, coord_frame, disp=True)
# Convert to the head frame
if coord_frame == "meg" and info["dev_head_t"] is not None:
origin_head = apply_trans(info["dev_head_t"], origin)
else:
origin_head = origin
update_kwargs = dict(
origin=origin,
coord_frame=coord_frame,
sss_cal=sss_cal,
int_order=int_order,
ext_order=ext_order,
extended_proj=extended_proj,
)
del origin, coord_frame, sss_cal
origin_head.setflags(write=False)
#
# Cross-talk processing
#
meg_ch_names = [info["ch_names"][p] for p in meg_picks]
ctc, sss_ctc = _read_cross_talk(cross_talk, meg_ch_names)
update_kwargs["sss_ctc"] = sss_ctc
del sss_ctc
#
# Translate to destination frame (always use non-fine-cal bases)
#
exp = dict(origin=origin_head, int_order=int_order, ext_order=0)
all_coils = _prep_mf_coils(info, ignore_ref)
all_coils_recon = _prep_mf_coils(info_recon, ignore_ref)
S_recon = _trans_sss_basis(exp, all_coils_recon, recon_trans, coil_scale)
exp["ext_order"] = ext_order
exp["extended_proj"] = extended_proj
del extended_proj
# Reconstruct data from internal space only (Eq. 38), and rescale S_recon
if recon_trans is not None:
# warn if we have translated too far
diff = 1000 * (info["dev_head_t"]["trans"][:3, 3] - recon_trans["trans"][:3, 3])
dist = np.sqrt(np.sum(_sq(diff)))
if dist > 25.0:
warn(
f'Head position change is over 25 mm '
f'({", ".join(f"{x:0.1f}" for x in diff)}) = {dist:0.1f} mm'
)
# Reconstruct raw file object with spatiotemporal processed data
max_st = dict()
if st_duration is not None:
if st_only:
job = FIFF.FIFFV_SSS_JOB_TPROJ
else:
job = FIFF.FIFFV_SSS_JOB_ST
max_st.update(
job=job, subspcorr=st_correlation, buflen=st_duration / info["sfreq"]
)
logger.info(
f" Processing data using tSSS with st_duration={max_st['buflen']}"
)
st_when = "before" if st_fixed else "after" # relative to movecomp
else:
# st_duration from here on will act like the chunk size
st_duration = min(max(int(round(10.0 * info["sfreq"])), 1), len(raw.times))
st_correlation = None
st_when = "never"
update_kwargs["max_st"] = max_st
del st_fixed, max_st
# Figure out which transforms we need for each tSSS block
# (and transform pos[1] to times)
head_pos[1] = raw.time_as_index(head_pos[1], use_rounding=True)
# Compute the first bit of pos_data for cHPI reporting
if info["dev_head_t"] is not None and head_pos[0] is not None:
this_pos_quat = np.concatenate(
[
rot_to_quat(info["dev_head_t"]["trans"][:3, :3]),
info["dev_head_t"]["trans"][:3, 3],
np.zeros(3),
]
)
else:
this_pos_quat = None
# Figure out our linear operator
mult = _get_sensor_operator(raw, meg_picks)
if mult is not None:
S_recon = mult @ S_recon
S_recon /= coil_scale
_get_this_decomp_trans = partial(
_get_decomp,
all_coils=all_coils,
cal=calibration,
regularize=regularize,
exp=exp,
ignore_ref=ignore_ref,
coil_scale=coil_scale,
grad_picks=grad_picks,
mag_picks=mag_picks,
good_mask=good_mask,
mag_or_fine=mag_or_fine,
bad_condition=bad_condition,
mag_scale=mag_scale,
mult=mult,
)
update_kwargs.update(
nchan=good_mask.sum(), st_only=st_only, recon_trans=recon_trans
)
params = dict(
skip_by_annotation=skip_by_annotation,
st_duration=st_duration,
st_correlation=st_correlation,
st_only=st_only,
st_when=st_when,
ctc=ctc,
coil_scale=coil_scale,
this_pos_quat=this_pos_quat,
meg_picks=meg_picks,
good_mask=good_mask,
grad_picks=grad_picks,
head_pos=head_pos,
info=info,
_get_this_decomp_trans=_get_this_decomp_trans,
S_recon=S_recon,
update_kwargs=update_kwargs,
ignore_ref=ignore_ref,
)
return params
def _run_maxwell_filter(
raw,
skip_by_annotation,
st_duration,
st_correlation,
st_only,
st_when,
ctc,
coil_scale,
this_pos_quat,
meg_picks,
good_mask,
grad_picks,
head_pos,
info,
_get_this_decomp_trans,
S_recon,
update_kwargs,
*,
ignore_ref=False,
reconstruct="in",
copy=True,
):
# Eventually find_bad_channels_maxwell could be sped up by moving this
# outside the loop (e.g., in the prep function) but regularization depends
# on which channels are being used, so easier just to include it here.
# The time it takes to recompute S and pS themselves is roughly on par
# with the np.dot with the data, so not a huge gain to be made there.
S_decomp, S_decomp_full, pS_decomp, reg_moments, n_use_in = _get_this_decomp_trans(
info["dev_head_t"], t=0.0
)
update_kwargs.update(reg_moments=reg_moments.copy())
if ctc is not None:
ctc = ctc[good_mask][:, good_mask]
add_channels = (head_pos[0] is not None) and (not st_only) and copy
raw_sss, pos_picks = _copy_preload_add_channels(raw, add_channels, copy, info)
sfreq = info["sfreq"]
del raw
if not st_only:
# remove MEG projectors, they won't apply now
_remove_meg_projs_comps(raw_sss, ignore_ref)
# Figure out which segments of data we can use
onsets, ends = _annotations_starts_stops(raw_sss, skip_by_annotation, invert=True)
max_samps = (ends - onsets).max()
if not 0.0 < st_duration <= max_samps + 1.0:
raise ValueError(
f"st_duration ({st_duration / sfreq:0.1f}s) must be between 0 and the "
"longest contiguous duration of the data "
"({max_samps / sfreq:0.1f}s)."
)
# Generate time points to break up data into equal-length windows
starts, stops = list(), list()
for onset, end in zip(onsets, ends):
read_lims = np.arange(onset, end + 1, st_duration)
if len(read_lims) == 1:
read_lims = np.concatenate([read_lims, [end]])
if read_lims[-1] != end:
read_lims[-1] = end
# fold it into the previous buffer
n_last_buf = read_lims[-1] - read_lims[-2]
if st_correlation is not None and len(read_lims) > 2:
if n_last_buf >= st_duration:
logger.info(
" Spatiotemporal window did not fit evenly into"
"contiguous data segment. "
f"{(n_last_buf - st_duration) / sfreq:0.2f} seconds "
"were lumped into the previous window."
)
else:
logger.info(
f" Contiguous data segment of duration "
f"{n_last_buf / sfreq:0.2f} "
"seconds is too short to be processed with tSSS "
f"using duration {st_duration / sfreq:0.2f}"
)
assert len(read_lims) >= 2
assert read_lims[0] == onset and read_lims[-1] == end
starts.extend(read_lims[:-1])
stops.extend(read_lims[1:])
del read_lims
st_duration = min(max_samps, st_duration)
# Loop through buffer windows of data
n_sig = int(np.floor(np.log10(max(len(starts), 0)))) + 1
logger.info(f" Processing {len(starts)} data chunk{_pl(starts)}")
for ii, (start, stop) in enumerate(zip(starts, stops)):
if start == stop:
continue # Skip zero-length annotations
tsss_valid = (stop - start) >= st_duration
rel_times = raw_sss.times[start:stop]
t_str = f"{rel_times[[0, -1]][0]:8.3f} - {rel_times[[0, -1]][1]:8.3f} s"
t_str += (f"(#{ii + 1}/{len(starts)})").rjust(2 * n_sig + 5)
# Get original data
orig_data = raw_sss._data[meg_picks[good_mask], start:stop]
# This could just be np.empty if not st_only, but shouldn't be slow
# this way so might as well just always take the original data
out_meg_data = raw_sss._data[meg_picks, start:stop]
# Apply cross-talk correction
if ctc is not None:
orig_data = ctc.dot(orig_data)
out_pos_data = np.empty((len(pos_picks), stop - start))
# Figure out which positions to use
t_s_s_q_a = _trans_starts_stops_quats(head_pos, start, stop, this_pos_quat)
n_positions = len(t_s_s_q_a[0])
# Set up post-tSSS or do pre-tSSS
if st_correlation is not None:
# If doing tSSS before movecomp...
resid = orig_data.copy() # to be safe let's operate on a copy
if st_when == "after":
orig_in_data = np.empty((len(meg_picks), stop - start))
else: # 'before'
avg_trans = t_s_s_q_a[-1]
if avg_trans is not None:
# if doing movecomp
(
S_decomp_st,
_,
pS_decomp_st,
_,
n_use_in_st,
) = _get_this_decomp_trans(avg_trans, t=rel_times[0])
else:
S_decomp_st, pS_decomp_st = S_decomp, pS_decomp
n_use_in_st = n_use_in
orig_in_data = np.dot(
np.dot(S_decomp_st[:, :n_use_in_st], pS_decomp_st[:n_use_in_st]),
resid,
)
resid -= np.dot(
np.dot(S_decomp_st[:, n_use_in_st:], pS_decomp_st[n_use_in_st:]),
resid,
)
resid -= orig_in_data
# Here we operate on our actual data
proc = out_meg_data if st_only else orig_data
_do_tSSS(
proc,
orig_in_data,
resid,
st_correlation,
n_positions,
t_str,
tsss_valid,
)
if not st_only or st_when == "after":
# Do movement compensation on the data
for trans, rel_start, rel_stop, this_pos_quat in zip(*t_s_s_q_a[:4]):
# Recalculate bases if necessary (trans will be None iff the
# first position in this interval is the same as last of the
# previous interval)
if trans is not None:
(
S_decomp,
S_decomp_full,
pS_decomp,
reg_moments,
n_use_in,
) = _get_this_decomp_trans(trans, t=rel_times[rel_start])
# Determine multipole moments for this interval
mm_in = np.dot(pS_decomp[:n_use_in], orig_data[:, rel_start:rel_stop])
# Our output data
if not st_only:
if reconstruct == "in":
proj = S_recon.take(reg_moments[:n_use_in], axis=1)
mult = mm_in
else:
assert reconstruct == "orig"
proj = S_decomp_full # already picked reg
mm_out = np.dot(
pS_decomp[n_use_in:], orig_data[:, rel_start:rel_stop]
)
mult = np.concatenate((mm_in, mm_out))
out_meg_data[:, rel_start:rel_stop] = np.dot(proj, mult)
if len(pos_picks) > 0:
out_pos_data[:, rel_start:rel_stop] = this_pos_quat[:, np.newaxis]
# Transform orig_data to store just the residual
if st_when == "after":
# Reconstruct data using original location from external
# and internal spaces and compute residual
rel_resid_data = resid[:, rel_start:rel_stop]
orig_in_data[:, rel_start:rel_stop] = np.dot(
S_decomp[:, :n_use_in], mm_in
)
rel_resid_data -= np.dot(
np.dot(S_decomp[:, n_use_in:], pS_decomp[n_use_in:]),
rel_resid_data,
)
rel_resid_data -= orig_in_data[:, rel_start:rel_stop]
# If doing tSSS at the end
if st_when == "after":
_do_tSSS(
out_meg_data,
orig_in_data,
resid,
st_correlation,
n_positions,
t_str,
tsss_valid,
)
elif st_when == "never" and head_pos[0] is not None:
logger.info(
f" Used {n_positions: 2d} head position{_pl(n_positions)} "
f"for {t_str}",
)
raw_sss._data[meg_picks, start:stop] = out_meg_data
raw_sss._data[pos_picks, start:stop] = out_pos_data
return raw_sss
def _get_coil_scale(meg_picks, mag_picks, grad_picks, mag_scale, info):
"""Get the magnetometer scale factor."""
if isinstance(mag_scale, str):
if mag_scale != "auto":
raise ValueError(f'mag_scale must be a float or "auto", got "{mag_scale}"')
if len(mag_picks) in (0, len(meg_picks)):
mag_scale = 100.0 # only one coil type, doesn't matter
logger.info(
f" Setting mag_scale={mag_scale:0.2f} because only one "
"coil type is present"
)
else:
# Find our physical distance between gradiometer pickup loops
# ("base line")
coils = _create_meg_coils(
[info["chs"][pick] for pick in meg_picks], "accurate"
)
grad_base = {coils[pick]["base"] for pick in grad_picks}
if len(grad_base) != 1 or list(grad_base)[0] <= 0:
raise RuntimeError(
"Could not automatically determine "
"mag_scale, could not find one "
f"proper gradiometer distance from: {list(grad_base)}"
)
grad_base = list(grad_base)[0]
mag_scale = 1.0 / grad_base
logger.info(
f" Setting mag_scale={mag_scale:0.2f} based on gradiometer "
f"distance {1000 * grad_base:0.2f} mm"
)
mag_scale = float(mag_scale)
coil_scale = np.ones((len(meg_picks), 1))
coil_scale[mag_picks] = mag_scale
return coil_scale, mag_scale
def _get_sensor_operator(raw, meg_picks):
comp = raw.compensation_grade
if comp not in (0, None):
mult = make_compensator(raw.info, 0, comp)
logger.info(f" Accounting for compensation grade {comp}")
assert mult.shape[0] == mult.shape[1] == len(raw.ch_names)
mult = mult[np.ix_(meg_picks, meg_picks)]
else:
mult = None
return mult
def _remove_meg_projs_comps(inst, ignore_ref):
"""Remove inplace existing MEG projectors (assumes inactive)."""
meg_picks = pick_types(inst.info, meg=True, exclude=[])
meg_channels = [inst.ch_names[pi] for pi in meg_picks]
non_meg_proj = list()
for proj in inst.info["projs"]:
if not any(c in meg_channels for c in proj["data"]["col_names"]):
non_meg_proj.append(proj)
inst.add_proj(non_meg_proj, remove_existing=True, verbose=False)
if ignore_ref and inst.info["comps"]:
assert inst.compensation_grade in (None, 0)
with inst.info._unlock():
inst.info["comps"] = []
def _check_destination(destination, info, head_frame):
"""Triage our reconstruction trans."""
if destination is None:
return info["dev_head_t"]
if not head_frame:
raise RuntimeError(
"destination can only be set if using the head coordinate frame"
)
if isinstance(destination, str | Path):
recon_trans = _get_trans(destination, "meg", "head")[0]
elif isinstance(destination, Transform):
recon_trans = destination
else:
destination = np.array(destination, float)
if destination.shape != (3,):
raise ValueError("destination must be a 3-element vector, str, or None")
recon_trans = np.eye(4)
recon_trans[:3, 3] = destination
recon_trans = Transform("meg", "head", recon_trans)
if recon_trans.to_str != "head" or recon_trans.from_str != "MEG device":
raise RuntimeError(
"Destination transform is not MEG device -> head, "
f"got {recon_trans.from_str} -> {recon_trans.to_str}"
)
return recon_trans
@verbose
def _prep_mf_coils(info, ignore_ref=True, *, accuracy="accurate", verbose=None):
"""Get all coil integration information loaded and sorted."""
meg_sensors = _prep_meg_channels(
info, head_frame=False, ignore_ref=ignore_ref, accuracy=accuracy, verbose=False
)
coils = meg_sensors["defs"]
mag_mask = _get_mag_mask(coils)
# Now coils is a sorted list of coils. Time to do some vectorization.
n_coils = len(coils)
rmags = np.concatenate([coil["rmag"] for coil in coils])
cosmags = np.concatenate([coil["cosmag"] for coil in coils])
ws = np.concatenate([coil["w"] for coil in coils])
cosmags *= ws[:, np.newaxis]
del ws
n_int = np.array([len(coil["rmag"]) for coil in coils])
bins = np.repeat(np.arange(len(n_int)), n_int)
bd = np.concatenate(([0], np.cumsum(n_int)))
slice_map = {
ii: slice(start, stop) for ii, (start, stop) in enumerate(zip(bd[:-1], bd[1:]))
}
return rmags, cosmags, bins, n_coils, mag_mask, slice_map
def _trans_starts_stops_quats(pos, start, stop, this_pos_data):
"""Get all trans and limits we need."""
pos_idx = np.arange(*np.searchsorted(pos[1], [start, stop]))
used = np.zeros(stop - start, bool)
trans = list()
rel_starts = list()
rel_stops = list()
quats = list()
weights = list()
for ti in range(-1, len(pos_idx)):
# first iteration for this block of data
if ti < 0:
rel_start = 0
rel_stop = pos[1][pos_idx[0]] if len(pos_idx) > 0 else stop
rel_stop = rel_stop - start
if rel_start == rel_stop:
continue # our first pos occurs on first time sample
# Don't calculate S_decomp here, use the last one
trans.append(None) # meaning: use previous
quats.append(this_pos_data)
else:
rel_start = pos[1][pos_idx[ti]] - start
if ti == len(pos_idx) - 1:
rel_stop = stop - start
else:
rel_stop = pos[1][pos_idx[ti + 1]] - start
trans.append(pos[0][pos_idx[ti]])
quats.append(pos[2][pos_idx[ti]])
assert 0 <= rel_start
assert rel_start < rel_stop
assert rel_stop <= stop - start
assert not used[rel_start:rel_stop].any()
used[rel_start:rel_stop] = True
rel_starts.append(rel_start)
rel_stops.append(rel_stop)
weights.append(rel_stop - rel_start)
assert used.all()
# Use weighted average for average trans over the window
if this_pos_data is None:
avg_trans = None
else:
weights = np.array(weights)
quats = np.array(quats)
weights = weights / weights.sum().astype(float) # int -> float
avg_quat = _average_quats(quats[:, :3], weights)
avg_t = np.dot(weights, quats[:, 3:6])
avg_trans = np.vstack(
[
np.hstack([quat_to_rot(avg_quat), avg_t[:, np.newaxis]]),
[[0.0, 0.0, 0.0, 1.0]],
]
)
return trans, rel_starts, rel_stops, quats, avg_trans
def _do_tSSS(
clean_data, orig_in_data, resid, st_correlation, n_positions, t_str, tsss_valid
):
"""Compute and apply SSP-like projection vectors based on min corr."""
if not tsss_valid:
t_proj = np.empty((clean_data.shape[1], 0))
else:
np.asarray_chkfinite(resid)
t_proj = _overlap_projector(orig_in_data, resid, st_correlation)
# Apply projector according to Eq. 12 in :footcite:`TauluSimola2006`
msg = (
f" Projecting {t_proj.shape[1]:2d} intersecting tSSS "
f"component{_pl(t_proj.shape[1], ' ')} for {t_str}"
)
if n_positions > 1:
msg += f" (across {n_positions:2d} position{_pl(n_positions, ' ')})"
logger.info(msg)
clean_data -= np.dot(np.dot(clean_data, t_proj), t_proj.T)
def _copy_preload_add_channels(raw, add_channels, copy, info):
"""Load data for processing and (maybe) add cHPI pos channels."""
if copy:
raw = raw.copy()
with raw.info._unlock():
raw.info["chs"] = info["chs"] # updated coil types
if add_channels:
kinds = [
FIFF.FIFFV_QUAT_1,
FIFF.FIFFV_QUAT_2,
FIFF.FIFFV_QUAT_3,
FIFF.FIFFV_QUAT_4,
FIFF.FIFFV_QUAT_5,
FIFF.FIFFV_QUAT_6,
FIFF.FIFFV_HPI_G,
FIFF.FIFFV_HPI_ERR,
FIFF.FIFFV_HPI_MOV,
]
out_shape = (len(raw.ch_names) + len(kinds), len(raw.times))
out_data = np.zeros(out_shape, np.float64)
msg = " Appending head position result channels and "
if raw.preload:
logger.info(msg + "copying original raw data")
out_data[: len(raw.ch_names)] = raw._data
raw._data = out_data
else:
logger.info(msg + "loading raw data from disk")
with use_log_level(False):
raw._preload_data(out_data[: len(raw.ch_names)])
raw._data = out_data
assert raw.preload is True
off = len(raw.ch_names)
chpi_chs = [
dict(
ch_name=f"CHPI{ii:03d}",
logno=ii + 1,
scanno=off + ii + 1,
unit_mul=-1,
range=1.0,
unit=-1,
kind=kinds[ii],
coord_frame=FIFF.FIFFV_COORD_UNKNOWN,
cal=1e-4,
coil_type=FWD.COIL_UNKNOWN,
loc=np.zeros(12),
)
for ii in range(len(kinds))
]
raw.info["chs"].extend(chpi_chs)
raw.info._update_redundant()
raw.info._check_consistency()
assert raw._data.shape == (raw.info["nchan"], len(raw.times))
# Return the pos picks
pos_picks = np.arange(len(raw.ch_names) - len(chpi_chs), len(raw.ch_names))
return raw, pos_picks
else:
if copy:
if not raw.preload:
logger.info(" Loading raw data from disk")
raw.load_data(verbose=False)
else:
logger.info(" Using loaded raw data")
return raw, np.array([], int)
def _check_pos(pos, head_frame, raw, st_fixed, sfreq):
"""Check for a valid pos array and transform it to a more usable form."""
_validate_type(pos, (np.ndarray, None), "head_pos")
if pos is None:
return [None, np.array([-1])]
if not head_frame:
raise ValueError('positions can only be used if coord_frame="head"')
if not st_fixed:
warn("st_fixed=False is untested, use with caution!")
if not isinstance(pos, np.ndarray):
raise TypeError("pos must be an ndarray")
if pos.ndim != 2 or pos.shape[1] != 10:
raise ValueError("pos must be an array of shape (N, 10)")
t = pos[:, 0]
if not np.array_equal(t, np.unique(t)):
raise ValueError("Time points must unique and in ascending order")
# We need an extra 1e-3 (1 ms) here because MaxFilter outputs values
# only out to 3 decimal places
if not _time_mask(t, tmin=raw._first_time - 1e-3, tmax=None, sfreq=sfreq).all():
raise ValueError(
"Head position time points must be greater than "
f"first sample offset, but found {t[0]:0.4f} < {raw._first_time:0.4f}"
)
max_dist = np.sqrt(np.sum(pos[:, 4:7] ** 2, axis=1)).max()
if max_dist > 1.0:
warn(
f"Found a distance greater than 1 m ({max_dist:0.3g} m) from the device "
"origin, positions may be invalid and Maxwell filtering could "
"fail"
)
dev_head_ts = np.zeros((len(t), 4, 4))
dev_head_ts[:, 3, 3] = 1.0
dev_head_ts[:, :3, 3] = pos[:, 4:7]
dev_head_ts[:, :3, :3] = quat_to_rot(pos[:, 1:4])
pos = [dev_head_ts, t - raw._first_time, pos[:, 1:]]
return pos
def _get_decomp(
trans,
*,
all_coils,
cal,
regularize,
exp,
ignore_ref,
coil_scale,
grad_picks,
mag_picks,
good_mask,
mag_or_fine,
bad_condition,
t,
mag_scale,
mult,
):
"""Get a decomposition matrix and pseudoinverse matrices."""
#
# Fine calibration processing (point-like magnetometers and calib. coeffs)
#
S_decomp_full = _get_s_decomp(
exp,
all_coils,
trans,
coil_scale,
cal,
ignore_ref,
grad_picks,
mag_picks,
mag_scale,
)
if mult is not None:
S_decomp_full = mult @ S_decomp_full
S_decomp = S_decomp_full[good_mask]
#
# Extended SSS basis (eSSS)
#
extended_proj = exp.get("extended_proj", ())
if len(extended_proj) > 0:
rcond = 1e-4
thresh = 1e-4
extended_proj = extended_proj.T * coil_scale[good_mask]
extended_proj /= np.linalg.norm(extended_proj, axis=0)
n_int = _get_n_moments(exp["int_order"])
if S_decomp.shape[1] > n_int:
S_ext = S_decomp[:, n_int:].copy()
S_ext /= np.linalg.norm(S_ext, axis=0)
S_ext_orth = linalg.orth(S_ext, rcond=rcond)
assert S_ext_orth.shape[1] == S_ext.shape[1]
extended_proj -= np.dot(S_ext_orth, np.dot(S_ext_orth.T, extended_proj))
scale = np.mean(np.linalg.norm(S_decomp[n_int:], axis=0))
else:
scale = np.mean(np.linalg.norm(S_decomp[:n_int], axis=0))
mask = np.linalg.norm(extended_proj, axis=0) > thresh
extended_remove = list(np.where(~mask)[0] + S_decomp.shape[1])
logger.debug(" Reducing %d -> %d", extended_proj.shape[1], mask.sum())
extended_proj /= np.linalg.norm(extended_proj, axis=0) / scale
S_decomp = np.concatenate([S_decomp, extended_proj], axis=-1)
if extended_proj.shape[1]:
S_decomp_full = np.pad(
S_decomp_full, ((0, 0), (0, extended_proj.shape[1])), "constant"
)
S_decomp_full[good_mask, -extended_proj.shape[1] :] = extended_proj
else:
extended_remove = list()
del extended_proj
#
# Regularization
#
S_decomp, reg_moments, n_use_in = _regularize(
regularize, exp, S_decomp, mag_or_fine, extended_remove, t=t
)
S_decomp_full = S_decomp_full.take(reg_moments, axis=1)
#
# Pseudo-inverse of total multipolar moment basis set (Part of Eq. 37)
#
pS_decomp, sing = _col_norm_pinv(S_decomp.copy())
cond = sing[0] / sing[-1]
if bad_condition != "ignore" and cond >= 1000.0:
msg = f"Matrix is badly conditioned: {cond:0.0f} >= 1000"
if bad_condition == "error":
raise RuntimeError(msg)
elif bad_condition == "warning":
warn(msg)
else: # condition == 'info'
logger.info(msg)
# Build in our data scaling here
pS_decomp *= coil_scale[good_mask].T
S_decomp /= coil_scale[good_mask]
S_decomp_full /= coil_scale
return S_decomp, S_decomp_full, pS_decomp, reg_moments, n_use_in
def _get_s_decomp(
exp, all_coils, trans, coil_scale, cal, ignore_ref, grad_picks, mag_picks, mag_scale
):
"""Get S_decomp."""
S_decomp = _trans_sss_basis(exp, all_coils, trans, coil_scale)
if cal is not None:
# Compute point-like mags to incorporate gradiometer imbalance
grad_cals = _sss_basis_point(exp, trans, cal, ignore_ref, mag_scale)
# Add point like magnetometer data to bases.
if len(grad_picks) > 0:
S_decomp[grad_picks, :] += grad_cals
# Scale magnetometers by calibration coefficient
if len(mag_picks) > 0:
S_decomp[mag_picks, :] /= cal["mag_cals"]
# We need to be careful about KIT gradiometers
return S_decomp
@verbose
def _regularize(
regularize, exp, S_decomp, mag_or_fine, extended_remove, t, verbose=None
):
"""Regularize a decomposition matrix."""
# ALWAYS regularize the out components according to norm, since
# gradiometer-only setups (e.g., KIT) can have zero first-order
# (homogeneous field) components
int_order, ext_order = exp["int_order"], exp["ext_order"]
n_in = _get_n_moments(int_order)
n_out = S_decomp.shape[1] - n_in
t_str = f"{t:8.3f}"
if regularize is not None: # regularize='in'
in_removes, out_removes = _regularize_in(
int_order, ext_order, S_decomp, mag_or_fine, extended_remove
)
else:
in_removes = []
out_removes = _regularize_out(
int_order, ext_order, mag_or_fine, extended_remove
)
reg_in_moments = np.setdiff1d(np.arange(n_in), in_removes)
reg_out_moments = np.setdiff1d(np.arange(n_in, S_decomp.shape[1]), out_removes)
n_use_in = len(reg_in_moments)
n_use_out = len(reg_out_moments)
reg_moments = np.concatenate((reg_in_moments, reg_out_moments))
S_decomp = S_decomp.take(reg_moments, axis=1)
if regularize is not None or n_use_out != n_out:
logger.info(
f" Using {n_use_in + n_use_out}/{n_in + n_out} harmonic components "
f"for {t_str} ({n_use_in}/{n_in} in, {n_use_out}/{n_out} out)"
)
return S_decomp, reg_moments, n_use_in
@verbose
def _get_mf_picks_fix_mags(info, int_order, ext_order, ignore_ref=False, verbose=None):
"""Pick types for Maxwell filtering and fix magnetometers."""
# Check for T1/T2 mag types
mag_inds_T1T2 = _get_T1T2_mag_inds(info, use_cal=True)
if len(mag_inds_T1T2) > 0:
fix_mag_coil_types(info, use_cal=True)
# Get indices of channels to use in multipolar moment calculation
ref = not ignore_ref
meg_picks = pick_types(info, meg=True, ref_meg=ref, exclude=[])
meg_info = pick_info(_simplify_info(info), meg_picks)
del info
good_mask = np.zeros(
len(
meg_picks,
),
bool,
)
good_mask[pick_types(meg_info, meg=True, ref_meg=ref, exclude="bads")] = 1
n_bases = _get_n_moments([int_order, ext_order]).sum()
if n_bases > good_mask.sum():
raise ValueError(
f"Number of requested bases ({n_bases}) exceeds number of "
f"good sensors ({good_mask.sum()})"
)
recons = [ch for ch in meg_info["bads"]]
if len(recons) > 0:
msg = f" Bad MEG channels being reconstructed: {recons}"
else:
msg = " No bad MEG channels"
logger.info(msg)
ref_meg = False if ignore_ref else "mag"
mag_picks = pick_types(meg_info, meg="mag", ref_meg=ref_meg, exclude=[])
ref_meg = False if ignore_ref else "grad"
grad_picks = pick_types(meg_info, meg="grad", ref_meg=ref_meg, exclude=[])
assert len(mag_picks) + len(grad_picks) == len(meg_info["ch_names"])
# Determine which are magnetometers for external basis purposes
mag_or_fine = np.zeros(len(meg_picks), bool)
mag_or_fine[mag_picks] = True
# KIT gradiometers are marked as having units T, not T/M (argh)
# We need a separate variable for this because KIT grads should be
# treated mostly like magnetometers (e.g., scaled by 100) for reg
coil_types = np.array([ch["coil_type"] for ch in meg_info["chs"]])
mag_or_fine[(coil_types & 0xFFFF) == FIFF.FIFFV_COIL_KIT_GRAD] = False
# The same thing goes for CTF gradiometers...
ctf_grads = [
FIFF.FIFFV_COIL_CTF_GRAD,
FIFF.FIFFV_COIL_CTF_REF_GRAD,
FIFF.FIFFV_COIL_CTF_OFFDIAG_REF_GRAD,
]
mag_or_fine[np.isin(coil_types, ctf_grads)] = False
msg = (
f" Processing {len(grad_picks)} gradiometers "
f"and {len(mag_picks)} magnetometers"
)
n_kit = len(mag_picks) - mag_or_fine.sum()
if n_kit > 0:
msg += f" (of which {n_kit} are actually KIT gradiometers)"
logger.info(msg)
return meg_picks, mag_picks, grad_picks, good_mask, mag_or_fine
def _check_regularize(regularize):
"""Ensure regularize is valid."""
if not (
regularize is None or (isinstance(regularize, str) and regularize in ("in",))
):
raise ValueError('regularize must be None or "in"')
def _check_usable(inst, ignore_ref):
"""Ensure our data are clean."""
if inst.proj:
raise RuntimeError(
"Projectors cannot be applied to data during Maxwell filtering."
)
current_comp = inst.compensation_grade
if current_comp not in (0, None) and ignore_ref:
raise RuntimeError(
"Maxwell filter cannot be done on compensated "
"channels (data have been compensated with "
"grade {current_comp}) when ignore_ref=True"
)
def _col_norm_pinv(x):
"""Compute the pinv with column-normalization to stabilize calculation.
Note: will modify/overwrite x.
"""
norm = np.sqrt(np.sum(x * x, axis=0))
x /= norm
u, s, v = _safe_svd(x, full_matrices=False, **check_disable)
v /= norm
return np.dot(v.T * 1.0 / s, u.T), s
def _sq(x):
"""Square quickly."""
return x * x
def _sph_harm_norm(order, degree):
"""Compute normalization factor for spherical harmonics."""
# we could use scipy.special.poch(degree + order + 1, -2 * order)
# here, but it's slower for our fairly small degree
norm = np.sqrt((2 * degree + 1.0) / (4 * np.pi))
if order != 0:
norm *= np.sqrt(factorial(degree - order) / float(factorial(degree + order)))
return norm
def _concatenate_sph_coils(coils):
"""Concatenate MEG coil parameters for spherical harmoncs."""
rs = np.concatenate([coil["r0_exey"] for coil in coils])
wcoils = np.concatenate([coil["w"] for coil in coils])
ezs = np.concatenate(
[np.tile(coil["ez"][np.newaxis, :], (len(coil["rmag"]), 1)) for coil in coils]
)
bins = np.repeat(np.arange(len(coils)), [len(coil["rmag"]) for coil in coils])
return rs, wcoils, ezs, bins
_mu_0 = 4e-7 * np.pi # magnetic permeability
def _get_mag_mask(coils):
"""Get the coil_scale for Maxwell filtering."""
return np.array([coil["coil_class"] == FWD.COILC_MAG for coil in coils])
def _sss_basis_basic(exp, coils, mag_scale=100.0, method="standard"):
"""Compute SSS basis using non-optimized (but more readable) algorithms."""
int_order, ext_order = exp["int_order"], exp["ext_order"]
origin = exp["origin"]
assert "extended_proj" not in exp # advanced option not supported
# Compute vector between origin and coil, convert to spherical coords
if method == "standard":
# Get position, normal, weights, and number of integration pts.
rmags, cosmags, ws, bins = _concatenate_coils(coils)
rmags -= origin
# Convert points to spherical coordinates
rad, az, pol = _cart_to_sph(rmags).T
cosmags *= ws[:, np.newaxis]
del rmags, ws
out_type = np.float64
else: # testing equivalence method
rs, wcoils, ezs, bins = _concatenate_sph_coils(coils)
rs -= origin
rad, az, pol = _cart_to_sph(rs).T
ezs *= wcoils[:, np.newaxis]
del rs, wcoils
out_type = np.complex128
del origin
# Set up output matrices
n_in, n_out = _get_n_moments([int_order, ext_order])
S_tot = np.empty((len(coils), n_in + n_out), out_type)
S_in = S_tot[:, :n_in]
S_out = S_tot[:, n_in:]
coil_scale = np.ones((len(coils), 1))
coil_scale[_get_mag_mask(coils)] = mag_scale
# Compute internal/external basis vectors (exclude degree 0; L/RHS Eq. 5)
for degree in range(1, max(int_order, ext_order) + 1):
# Only loop over positive orders, negative orders are handled
# for efficiency within
for order in range(degree + 1):
S_in_out = list()
grads_in_out = list()
# Same spherical harmonic is used for both internal and external
sph = sph_harm(order, degree, az, pol)
sph_norm = _sph_harm_norm(order, degree)
# Compute complex gradient for all integration points
# in spherical coordinates (Eq. 6). The gradient for rad, az, pol
# is obtained by taking the partial derivative of Eq. 4 w.r.t. each
# coordinate.
az_factor = 1j * order * sph / np.sin(np.maximum(pol, 1e-16))
pol_factor = (
-sph_norm
* np.sin(pol)
* np.exp(1j * order * az)
* _alegendre_deriv(order, degree, np.cos(pol))
)
if degree <= int_order:
S_in_out.append(S_in)
in_norm = _mu_0 * rad ** -(degree + 2)
g_rad = in_norm * (-(degree + 1.0) * sph)
g_az = in_norm * az_factor
g_pol = in_norm * pol_factor
grads_in_out.append(_sph_to_cart_partials(az, pol, g_rad, g_az, g_pol))
if degree <= ext_order:
S_in_out.append(S_out)
out_norm = _mu_0 * rad ** (degree - 1)
g_rad = out_norm * degree * sph
g_az = out_norm * az_factor
g_pol = out_norm * pol_factor
grads_in_out.append(_sph_to_cart_partials(az, pol, g_rad, g_az, g_pol))
for spc, grads in zip(S_in_out, grads_in_out):
# We could convert to real at the end, but it's more efficient
# to do it now
if method == "standard":
grads_pos_neg = [_sh_complex_to_real(grads, order)]
orders_pos_neg = [order]
# Deal with the negative orders
if order > 0:
# it's faster to use the conjugation property for
# our normalized spherical harmonics than recalculate
grads_pos_neg.append(
_sh_complex_to_real(_sh_negate(grads, order), -order)
)
orders_pos_neg.append(-order)
for gr, oo in zip(grads_pos_neg, orders_pos_neg):
# Gradients dotted w/integration point weighted normals
gr = np.einsum("ij,ij->i", gr, cosmags)
vals = np.bincount(bins, gr, len(coils))
spc[:, _deg_ord_idx(degree, oo)] = -vals
else:
grads = np.einsum("ij,ij->i", grads, ezs)
v = np.bincount(bins, grads.real, len(coils)) + 1j * np.bincount(
bins, grads.imag, len(coils)
)
spc[:, _deg_ord_idx(degree, order)] = -v
if order > 0:
spc[:, _deg_ord_idx(degree, -order)] = -_sh_negate(v, order)
# Scale magnetometers
S_tot *= coil_scale
if method != "standard":
# Eventually we could probably refactor this for 2x mem (and maybe CPU)
# savings by changing how spc/S_tot is assigned above (real only)
S_tot = _bases_complex_to_real(S_tot, int_order, ext_order)
return S_tot
def _sss_basis(exp, all_coils):
"""Compute SSS basis for given conditions.
Parameters
----------
exp : dict
Must contain the following keys:
origin : ndarray, shape (3,)
Origin of the multipolar moment space in meters
int_order : int
Order of the internal multipolar moment space
ext_order : int
Order of the external multipolar moment space
coils : list
List of MEG coils. Each should contain coil information dict specifying
position, normals, weights, number of integration points and channel
type. All coil geometry must be in the same coordinate frame
as ``origin`` (``head`` or ``meg``).
Returns
-------
bases : ndarray, shape (n_coils, n_mult_moments)
Internal and external basis sets as a single ndarray.
Notes
-----
Does not incorporate magnetometer scaling factor or normalize spaces.
Adapted from code provided by Jukka Nenonen.
"""
rmags, cosmags, bins, n_coils = all_coils[:4]
int_order, ext_order = exp["int_order"], exp["ext_order"]
n_in, n_out = _get_n_moments([int_order, ext_order])
rmags = rmags - exp["origin"]
# do the heavy lifting
max_order = max(int_order, ext_order)
L = _tabular_legendre(rmags, max_order)
phi = np.arctan2(rmags[:, 1], rmags[:, 0])
r_n = np.sqrt(np.sum(rmags * rmags, axis=1))
r_xy = np.sqrt(rmags[:, 0] * rmags[:, 0] + rmags[:, 1] * rmags[:, 1])
cos_pol = rmags[:, 2] / r_n # cos(theta); theta 0...pi
sin_pol = np.sqrt(1.0 - cos_pol * cos_pol) # sin(theta)
z_only = r_xy <= 1e-16
sin_pol_nz = sin_pol.copy()
sin_pol_nz[z_only] = 1.0 # will be overwritten later
r_xy[z_only] = 1.0
cos_az = rmags[:, 0] / r_xy # cos(phi)
cos_az[z_only] = 1.0
sin_az = rmags[:, 1] / r_xy # sin(phi)
sin_az[z_only] = 0.0
# Appropriate vector spherical harmonics terms
# JNE 2012-02-08: modified alm -> 2*alm, blm -> -2*blm
r_nn2 = r_n.copy()
r_nn1 = 1.0 / (r_n * r_n)
S_tot = np.empty((n_coils, n_in + n_out), np.float64)
S_in = S_tot[:, :n_in]
S_out = S_tot[:, n_in:]
for degree in range(max_order + 1):
if degree <= ext_order:
r_nn1 *= r_n # r^(l-1)
if degree <= int_order:
r_nn2 *= r_n # r^(l+2)
# mu_0*sqrt((2l+1)/4pi (l-m)!/(l+m)!)
mult = 2e-7 * np.sqrt((2 * degree + 1) * np.pi)
if degree > 0:
idx = _deg_ord_idx(degree, 0)
# alpha
if degree <= int_order:
b_r = mult * (degree + 1) * L[degree][0] / r_nn2
b_pol = -mult * L[degree][1] / r_nn2
S_in[:, idx] = _integrate_points(
cos_az,
sin_az,
cos_pol,
sin_pol,
b_r,
0.0,
b_pol,
cosmags,
bins,
n_coils,
)
# beta
if degree <= ext_order:
b_r = -mult * degree * L[degree][0] * r_nn1
b_pol = -mult * L[degree][1] * r_nn1
S_out[:, idx] = _integrate_points(
cos_az,
sin_az,
cos_pol,
sin_pol,
b_r,
0.0,
b_pol,
cosmags,
bins,
n_coils,
)
for order in range(1, degree + 1):
ord_phi = order * phi
sin_order = np.sin(ord_phi)
cos_order = np.cos(ord_phi)
mult /= np.sqrt((degree - order + 1) * (degree + order))
factor = mult * np.sqrt(2) # equivalence fix (MF uses 2.)
# Real
idx = _deg_ord_idx(degree, order)
r_fact = factor * L[degree][order] * cos_order
az_fact = factor * order * sin_order * L[degree][order]
pol_fact = (
-factor
* (
L[degree][order + 1]
- (degree + order) * (degree - order + 1) * L[degree][order - 1]
)
* cos_order
)
# alpha
if degree <= int_order:
b_r = (degree + 1) * r_fact / r_nn2
b_az = az_fact / (sin_pol_nz * r_nn2)
b_az[z_only] = 0.0
b_pol = pol_fact / (2 * r_nn2)
S_in[:, idx] = _integrate_points(
cos_az,
sin_az,
cos_pol,
sin_pol,
b_r,
b_az,
b_pol,
cosmags,
bins,
n_coils,
)
# beta
if degree <= ext_order:
b_r = -degree * r_fact * r_nn1
b_az = az_fact * r_nn1 / sin_pol_nz
b_az[z_only] = 0.0
b_pol = pol_fact * r_nn1 / 2.0
S_out[:, idx] = _integrate_points(
cos_az,
sin_az,
cos_pol,
sin_pol,
b_r,
b_az,
b_pol,
cosmags,
bins,
n_coils,
)
# Imaginary
idx = _deg_ord_idx(degree, -order)
r_fact = factor * L[degree][order] * sin_order
az_fact = factor * order * cos_order * L[degree][order]
pol_fact = (
factor
* (
L[degree][order + 1]
- (degree + order) * (degree - order + 1) * L[degree][order - 1]
)
* sin_order
)
# alpha
if degree <= int_order:
b_r = -(degree + 1) * r_fact / r_nn2
b_az = az_fact / (sin_pol_nz * r_nn2)
b_az[z_only] = 0.0
b_pol = pol_fact / (2 * r_nn2)
S_in[:, idx] = _integrate_points(
cos_az,
sin_az,
cos_pol,
sin_pol,
b_r,
b_az,
b_pol,
cosmags,
bins,
n_coils,
)
# beta
if degree <= ext_order:
b_r = degree * r_fact * r_nn1
b_az = az_fact * r_nn1 / sin_pol_nz
b_az[z_only] = 0.0
b_pol = pol_fact * r_nn1 / 2.0
S_out[:, idx] = _integrate_points(
cos_az,
sin_az,
cos_pol,
sin_pol,
b_r,
b_az,
b_pol,
cosmags,
bins,
n_coils,
)
return S_tot
def _integrate_points(
cos_az, sin_az, cos_pol, sin_pol, b_r, b_az, b_pol, cosmags, bins, n_coils
):
"""Integrate points in spherical coords."""
grads = _sp_to_cart(cos_az, sin_az, cos_pol, sin_pol, b_r, b_az, b_pol).T
grads = (grads * cosmags).sum(axis=1)
return bincount(bins, grads, n_coils)
def _tabular_legendre(r, nind):
"""Compute associated Legendre polynomials."""
r_n = np.sqrt(np.sum(r * r, axis=1))
x = r[:, 2] / r_n # cos(theta)
L = list()
for degree in range(nind + 1):
L.append(np.zeros((degree + 2, len(r))))
L[0][0] = 1.0
pnn = np.ones(x.shape)
fact = 1.0
sx2 = np.sqrt((1.0 - x) * (1.0 + x))
for degree in range(nind + 1):
L[degree][degree] = pnn
pnn *= -fact * sx2
fact += 2.0
if degree < nind:
L[degree + 1][degree] = x * (2 * degree + 1) * L[degree][degree]
if degree >= 2:
for order in range(degree - 1):
L[degree][order] = (
x * (2 * degree - 1) * L[degree - 1][order]
- (degree + order - 1) * L[degree - 2][order]
) / (degree - order)
return L
def _sp_to_cart(cos_az, sin_az, cos_pol, sin_pol, b_r, b_az, b_pol):
"""Convert spherical coords to cartesian."""
out = np.empty((3,) + sin_pol.shape)
out[0] = sin_pol * cos_az * b_r + cos_pol * cos_az * b_pol - sin_az * b_az
out[1] = sin_pol * sin_az * b_r + cos_pol * sin_az * b_pol + cos_az * b_az
out[2] = cos_pol * b_r - sin_pol * b_pol
return out
def _get_degrees_orders(order):
"""Get the set of degrees used in our basis functions."""
degrees = np.zeros(_get_n_moments(order), int)
orders = np.zeros_like(degrees)
for degree in range(1, order + 1):
# Only loop over positive orders, negative orders are handled
# for efficiency within
for order in range(degree + 1):
ii = _deg_ord_idx(degree, order)
degrees[ii] = degree
orders[ii] = order
ii = _deg_ord_idx(degree, -order)
degrees[ii] = degree
orders[ii] = -order
return degrees, orders
def _alegendre_deriv(order, degree, val):
"""Compute the derivative of the associated Legendre polynomial at a value.
Parameters
----------
order : int
Order of spherical harmonic. (Usually) corresponds to 'm'.
degree : int
Degree of spherical harmonic. (Usually) corresponds to 'l'.
val : float
Value to evaluate the derivative at.
Returns
-------
dPlm : float
Associated Legendre function derivative
"""
assert order >= 0
return (
order * val * lpmv(order, degree, val)
+ (degree + order)
* (degree - order + 1.0)
* np.sqrt(1.0 - val * val)
* lpmv(order - 1, degree, val)
) / (1.0 - val * val)
def _bases_complex_to_real(complex_tot, int_order, ext_order):
"""Convert complex spherical harmonics to real."""
n_in, n_out = _get_n_moments([int_order, ext_order])
complex_in = complex_tot[:, :n_in]
complex_out = complex_tot[:, n_in:]
real_tot = np.empty(complex_tot.shape, np.float64)
real_in = real_tot[:, :n_in]
real_out = real_tot[:, n_in:]
for comp, real, exp_order in zip(
[complex_in, complex_out], [real_in, real_out], [int_order, ext_order]
):
for deg in range(1, exp_order + 1):
for order in range(deg + 1):
idx_pos = _deg_ord_idx(deg, order)
idx_neg = _deg_ord_idx(deg, -order)
real[:, idx_pos] = _sh_complex_to_real(comp[:, idx_pos], order)
if order != 0:
# This extra mult factor baffles me a bit, but it works
# in round-trip testing, so we'll keep it :(
mult = -1 if order % 2 == 0 else 1
real[:, idx_neg] = mult * _sh_complex_to_real(
comp[:, idx_neg], -order
)
return real_tot
def _bases_real_to_complex(real_tot, int_order, ext_order):
"""Convert real spherical harmonics to complex."""
n_in, n_out = _get_n_moments([int_order, ext_order])
real_in = real_tot[:, :n_in]
real_out = real_tot[:, n_in:]
comp_tot = np.empty(real_tot.shape, np.complex128)
comp_in = comp_tot[:, :n_in]
comp_out = comp_tot[:, n_in:]
for real, comp, exp_order in zip(
[real_in, real_out], [comp_in, comp_out], [int_order, ext_order]
):
for deg in range(1, exp_order + 1):
# only loop over positive orders, figure out neg from pos
for order in range(deg + 1):
idx_pos = _deg_ord_idx(deg, order)
idx_neg = _deg_ord_idx(deg, -order)
this_comp = _sh_real_to_complex(
[real[:, idx_pos], real[:, idx_neg]], order
)
comp[:, idx_pos] = this_comp
comp[:, idx_neg] = _sh_negate(this_comp, order)
return comp_tot
def _check_info(info, sss=True, tsss=True, calibration=True, ctc=True):
"""Ensure that Maxwell filtering has not been applied yet."""
for ent in info["proc_history"]:
for msg, key, doing in (
("SSS", "sss_info", sss),
("tSSS", "max_st", tsss),
("fine calibration", "sss_cal", calibration),
("cross-talk cancellation", "sss_ctc", ctc),
):
if not doing:
continue
if len(ent["max_info"][key]) > 0:
raise RuntimeError(
f"Maxwell filtering {msg} step has already "
"been applied, cannot reapply"
)
def _update_sss_info(
raw,
origin,
int_order,
ext_order,
nchan,
coord_frame,
sss_ctc,
sss_cal,
max_st,
reg_moments,
st_only,
recon_trans,
extended_proj,
):
"""Update info inplace after Maxwell filtering.
Parameters
----------
raw : instance of Raw
Data to be filtered
origin : array-like, shape (3,)
Origin of internal and external multipolar moment space in head coords
(in meters)
int_order : int
Order of internal component of spherical expansion
ext_order : int
Order of external component of spherical expansion
nchan : int
Number of sensors
sss_ctc : dict
The cross talk information.
sss_cal : dict
The calibration information.
max_st : dict
The tSSS information.
reg_moments : ndarray | slice
The moments that were used.
st_only : bool
Whether tSSS only was performed.
recon_trans : instance of Transform
The reconstruction trans.
extended_proj : ndarray
Extended external bases.
"""
n_in, n_out = _get_n_moments([int_order, ext_order])
with raw.info._unlock():
raw.info["maxshield"] = False
components = np.zeros(n_in + n_out + len(extended_proj)).astype("int32")
components[reg_moments] = 1
sss_info_dict = dict(
in_order=int_order,
out_order=ext_order,
nchan=nchan,
origin=origin.astype("float32"),
job=FIFF.FIFFV_SSS_JOB_FILTER,
nfree=np.sum(components[:n_in]),
frame=_str_to_frame[coord_frame],
components=components,
)
max_info_dict = dict(max_st=max_st)
if st_only:
max_info_dict.update(sss_info=dict(), sss_cal=dict(), sss_ctc=dict())
else:
max_info_dict.update(sss_info=sss_info_dict, sss_cal=sss_cal, sss_ctc=sss_ctc)
# Reset 'bads' for any MEG channels since they've been reconstructed
_reset_meg_bads(raw.info)
# set the reconstruction transform
with raw.info._unlock():
raw.info["dev_head_t"] = recon_trans
block_id = _generate_meas_id()
with raw.info._unlock():
raw.info["proc_history"].insert(
0,
dict(
max_info=max_info_dict,
block_id=block_id,
date=DATE_NONE,
creator=f"mne-python v{__version__}",
experimenter="",
),
)
def _reset_meg_bads(info):
"""Reset MEG bads."""
meg_picks = pick_types(info, meg=True, exclude=[])
info["bads"] = [
bad for bad in info["bads"] if info["ch_names"].index(bad) not in meg_picks
]
check_disable = dict(check_finite=False)
def _orth_overwrite(A):
"""Create a slightly more efficient 'orth'."""
# adapted from scipy/linalg/decomp_svd.py
u, s = _safe_svd(A, full_matrices=False, **check_disable)[:2]
M, N = A.shape
eps = np.finfo(float).eps
tol = max(M, N) * np.amax(s) * eps
num = np.sum(s > tol, dtype=int)
return u[:, :num]
def _overlap_projector(data_int, data_res, corr):
"""Calculate projector for removal of subspace intersection in tSSS."""
# corr necessary to deal with noise when finding identical signal
# directions in the subspace. See the end of the Results section in
# :footcite:`TauluSimola2006`
# Note that the procedure here is an updated version of
# :footcite:`TauluSimola2006` (and used in MF's tSSS) that uses residuals
# instead of internal/external spaces directly. This provides more degrees
# of freedom when analyzing for intersections between internal and
# external spaces.
# Normalize data, then compute orth to get temporal bases. Matrices
# must have shape (n_samps x effective_rank) when passed into svd
# computation
# we use np.linalg.norm instead of sp.linalg.norm here: ~2x faster!
n = np.linalg.norm(data_int)
n = 1.0 if n == 0 else n # all-zero data should gracefully continue
data_int = _orth_overwrite((data_int / n).T)
n = np.linalg.norm(data_res)
n = 1.0 if n == 0 else n
data_res = _orth_overwrite((data_res / n).T)
if data_int.shape[1] == 0 or data_res.shape[1] == 0:
return np.empty((data_int.shape[0], 0))
Q_int = linalg.qr(data_int, overwrite_a=True, mode="economic", **check_disable)[0].T
Q_res = linalg.qr(data_res, overwrite_a=True, mode="economic", **check_disable)[0]
C_mat = np.dot(Q_int, Q_res)
del Q_int
# Compute angles between subspace and which bases to keep
S_intersect, Vh_intersect = _safe_svd(C_mat, full_matrices=False, **check_disable)[
1:
]
del C_mat
intersect_mask = S_intersect >= corr
del S_intersect
# Compute projection operator as (I-LL_T) Eq. 12 in
# :footcite:`TauluSimola2006` V_principal should be shape
# (n_time_pts x n_retained_inds)
Vh_intersect = Vh_intersect[intersect_mask].T
V_principal = np.dot(Q_res, Vh_intersect)
return V_principal
def _prep_fine_cal(info, fine_cal, *, ignore_ref):
from ._fine_cal import read_fine_calibration
_validate_type(fine_cal, (dict, "path-like"))
if not isinstance(fine_cal, dict):
extra = op.basename(str(fine_cal))
fine_cal = read_fine_calibration(fine_cal)
else:
extra = "dict"
logger.info(f" Using fine calibration {extra}")
ch_names = _clean_names(info["ch_names"], remove_whitespace=True)
info_to_cal = dict()
missing = list()
names_clean = _clean_names(fine_cal["ch_names"], remove_whitespace=True)
for ci, (name, name_clean) in enumerate(zip(fine_cal["ch_names"], names_clean)):
if name_clean not in ch_names:
missing.append(name)
else:
oi = ch_names.index(name_clean)
info_to_cal[oi] = ci
meg_picks = pick_types(info, meg=True, exclude=[], ref_meg=not ignore_ref)
if len(info_to_cal) != len(meg_picks):
bad = sorted({ch_names[pick] for pick in meg_picks} - set(names_clean))
raise RuntimeError(
f"Not all MEG channels found in fine calibration file, missing:\n{bad}"
)
if len(missing):
warn(f"Found cal channel{_pl(missing)} not in data: {missing}")
return info_to_cal, fine_cal, ch_names
def _update_sensor_geometry(info, fine_cal, ignore_ref):
"""Replace sensor geometry information and reorder cal_chs."""
info_to_cal, fine_cal, _ = _prep_fine_cal(info, fine_cal, ignore_ref=ignore_ref)
grad_picks = pick_types(info, meg="grad", exclude=(), ref_meg=not ignore_ref)
mag_picks = pick_types(info, meg="mag", exclude=(), ref_meg=not ignore_ref)
# Determine gradiometer imbalances and magnetometer calibrations
grad_imbalances = np.array(
[fine_cal["imb_cals"][info_to_cal[gi]] for gi in grad_picks]
).T
if grad_imbalances.shape[0] not in [0, 1, 3]:
raise ValueError(
"Must have 1 (x) or 3 (x, y, z) point-like "
f"magnetometers. Currently have {grad_imbalances.shape[0]}."
)
mag_cals = np.array([fine_cal["imb_cals"][info_to_cal[mi]] for mi in mag_picks])
# Now let's actually construct our point-like adjustment coils for grads
grad_coilsets = _get_grad_point_coilsets(
info, n_types=len(grad_imbalances), ignore_ref=ignore_ref
)
calibration = dict(
grad_imbalances=grad_imbalances, grad_coilsets=grad_coilsets, mag_cals=mag_cals
)
# Replace sensor locations (and track differences) for fine calibration
ang_shift = list()
used = np.zeros(len(info["chs"]), bool)
cal_corrs = list()
cal_chans = list()
adjust_logged = False
for oi, ci in info_to_cal.items():
assert not used[oi]
used[oi] = True
info_ch = info["chs"][oi]
# This only works for VV-like names
try:
ch_num = int(fine_cal["ch_names"][ci].lstrip("MEG").lstrip("0"))
except ValueError: # invalid literal for int() with base 10
ch_num = oi
cal_chans.append([ch_num, info_ch["coil_type"]])
# Some .dat files might only rotate EZ, so we must check first that
# EX and EY are orthogonal to EZ. If not, we find the rotation between
# the original and fine-cal ez, and rotate EX and EY accordingly:
ch_coil_rot = _loc_to_coil_trans(info_ch["loc"])[:3, :3]
cal_loc = fine_cal["locs"][ci].copy()
cal_coil_rot = _loc_to_coil_trans(cal_loc)[:3, :3]
if (
np.max(
[
np.abs(np.dot(cal_coil_rot[:, ii], cal_coil_rot[:, 2]))
for ii in range(2)
]
)
> 1e-6
): # X or Y not orthogonal
if not adjust_logged:
logger.info(" Adjusting non-orthogonal EX and EY")
adjust_logged = True
# find the rotation matrix that goes from one to the other
this_trans = _find_vector_rotation(ch_coil_rot[:, 2], cal_coil_rot[:, 2])
cal_loc[3:] = np.dot(this_trans, ch_coil_rot).T.ravel()
# calculate shift angle
v1 = _loc_to_coil_trans(cal_loc)[:3, :3]
_normalize_vectors(v1)
v2 = _loc_to_coil_trans(info_ch["loc"])[:3, :3]
_normalize_vectors(v2)
ang_shift.append(np.sum(v1 * v2, axis=0))
if oi in grad_picks:
extra = [1.0, fine_cal["imb_cals"][ci][0]]
else:
extra = [fine_cal["imb_cals"][ci][0], 0.0]
cal_corrs.append(np.concatenate([extra, cal_loc]))
# Adjust channel normal orientations with those from fine calibration
# Channel positions are not changed
info_ch["loc"][3:] = cal_loc[3:]
assert info_ch["coord_frame"] == FIFF.FIFFV_COORD_DEVICE
meg_picks = pick_types(info, meg=True, exclude=(), ref_meg=not ignore_ref)
assert used[meg_picks].all()
assert not used[np.setdiff1d(np.arange(len(used)), meg_picks)].any()
# This gets written to the Info struct
sss_cal = dict(cal_corrs=np.array(cal_corrs), cal_chans=np.array(cal_chans))
# Log quantification of sensor changes
# Deal with numerical precision giving absolute vals slightly more than 1.
ang_shift = np.array(ang_shift)
np.clip(ang_shift, -1.0, 1.0, ang_shift)
np.rad2deg(np.arccos(ang_shift), ang_shift) # Convert to degrees
logger.info(
" Adjusted coil orientations by (μ ± σ): "
f"{np.mean(ang_shift):0.1f}° ± {np.std(ang_shift):0.1f}° "
f"(max: {np.max(np.abs(ang_shift)):0.1f}°)"
)
return calibration, sss_cal
def _get_grad_point_coilsets(info, n_types, ignore_ref):
"""Get point-type coilsets for gradiometers."""
_rotations = dict(
x=np.array([[0, 0, 1, 0], [0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 0, 1.0]]),
y=np.array([[1, 0, 0, 0], [0, 0, 1, 0], [0, 1, 0, 0], [0, 0, 0, 1.0]]),
z=np.eye(4),
)
grad_coilsets = list()
grad_picks = pick_types(info, meg="grad", exclude=[])
if len(grad_picks) == 0:
return grad_coilsets
grad_info = pick_info(_simplify_info(info), grad_picks)
# Coil_type values for x, y, z point magnetometers
# Note: 1D correction files only have x-direction corrections
for ch in grad_info["chs"]:
ch["coil_type"] = FIFF.FIFFV_COIL_POINT_MAGNETOMETER
orig_locs = [ch["loc"].copy() for ch in grad_info["chs"]]
for rot in "xyz"[:n_types]:
# Rotate the Z magnetometer orientation to the destination orientation
for ci, ch in enumerate(grad_info["chs"]):
ch["loc"][3:] = _coil_trans_to_loc(
np.dot(_loc_to_coil_trans(orig_locs[ci]), _rotations[rot])
)[3:]
grad_coilsets.append(_prep_mf_coils(grad_info, ignore_ref))
return grad_coilsets
def _sss_basis_point(exp, trans, cal, ignore_ref=False, mag_scale=100.0):
"""Compute multipolar moments for point-like mags (in fine cal)."""
# Loop over all coordinate directions desired and create point mags
S_tot = 0.0
# These are magnetometers, so use a uniform coil_scale of 100.
this_cs = np.array([mag_scale], float)
for imb, coils in zip(cal["grad_imbalances"], cal["grad_coilsets"]):
S_add = _trans_sss_basis(exp, coils, trans, this_cs)
# Scale spaces by gradiometer imbalance
S_add *= imb[:, np.newaxis]
S_tot += S_add
# Return point-like mag bases
return S_tot
def _regularize_out(int_order, ext_order, mag_or_fine, extended_remove):
"""Regularize out components based on norm."""
n_in = _get_n_moments(int_order)
remove_homog = ext_order > 0 and not mag_or_fine.any()
return list(range(n_in, n_in + 3 * remove_homog)) + extended_remove
def _regularize_in(int_order, ext_order, S_decomp, mag_or_fine, extended_remove):
"""Regularize basis set using idealized SNR measure."""
n_in, n_out = _get_n_moments([int_order, ext_order])
# The "signal" terms depend only on the inner expansion order
# (i.e., not sensor geometry or head position / expansion origin)
a_lm_sq, rho_i = _compute_sphere_activation_in(np.arange(int_order + 1))
degrees, orders = _get_degrees_orders(int_order)
a_lm_sq = a_lm_sq[degrees]
I_tots = np.zeros(n_in) # we might not traverse all, so use np.zeros
in_keepers = list(range(n_in))
out_removes = _regularize_out(int_order, ext_order, mag_or_fine, extended_remove)
out_keepers = list(np.setdiff1d(np.arange(n_in, S_decomp.shape[1]), out_removes))
remove_order = []
S_decomp = S_decomp.copy()
use_norm = np.sqrt(np.sum(S_decomp * S_decomp, axis=0))
S_decomp /= use_norm
eigs = np.zeros((n_in, 2))
# plot = False # for debugging
# if plot:
# import matplotlib.pyplot as plt
# fig, axs = plt.subplots(3, figsize=[6, 12])
# plot_ord = np.empty(n_in, int)
# plot_ord.fill(-1)
# count = 0
# # Reorder plot to match MF
# for degree in range(1, int_order + 1):
# for order in range(0, degree + 1):
# assert plot_ord[count] == -1
# plot_ord[count] = _deg_ord_idx(degree, order)
# count += 1
# if order > 0:
# assert plot_ord[count] == -1
# plot_ord[count] = _deg_ord_idx(degree, -order)
# count += 1
# assert count == n_in
# assert (plot_ord >= 0).all()
# assert len(np.unique(plot_ord)) == n_in
noise_lev = 5e-13 # noise level in T/m
noise_lev *= noise_lev # effectively what would happen by earlier multiply
for ii in range(n_in):
this_S = S_decomp.take(in_keepers + out_keepers, axis=1)
u, s, v = _safe_svd(this_S, full_matrices=False, **check_disable)
del this_S
eigs[ii] = s[[0, -1]]
v = v.T[: len(in_keepers)]
v /= use_norm[in_keepers][:, np.newaxis]
eta_lm_sq = np.dot(v * 1.0 / s, u.T)
del u, s, v
eta_lm_sq *= eta_lm_sq
eta_lm_sq = eta_lm_sq.sum(axis=1)
eta_lm_sq *= noise_lev
# Mysterious scale factors to match MF, likely due to differences
# in the basis normalizations...
eta_lm_sq[orders[in_keepers] == 0] *= 2
eta_lm_sq *= 0.0025
snr = a_lm_sq[in_keepers] / eta_lm_sq
I_tots[ii] = 0.5 * np.log2(snr + 1.0).sum()
remove_order.append(in_keepers[np.argmin(snr)])
in_keepers.pop(in_keepers.index(remove_order[-1]))
# heuristic to quit if we're past the peak to save cycles
if ii > 10 and (I_tots[ii - 1 : ii + 1] < 0.95 * I_tots.max()).all():
break
# if plot and ii == 0:
# axs[0].semilogy(snr[plot_ord[in_keepers]], color='k')
# if plot:
# axs[0].set(ylabel='SNR', ylim=[0.1, 500], xlabel='Component')
# axs[1].plot(I_tots)
# axs[1].set(ylabel='Information', xlabel='Iteration')
# axs[2].plot(eigs[:, 0] / eigs[:, 1])
# axs[2].set(ylabel='Condition', xlabel='Iteration')
# Pick the components that give at least 98% of max info
# This is done because the curves can be quite flat, and we err on the
# side of including rather than excluding components
if n_in:
max_info = np.max(I_tots)
lim_idx = np.where(I_tots >= 0.98 * max_info)[0][0]
in_removes = remove_order[:lim_idx]
for ii, ri in enumerate(in_removes):
eig = eigs[ii]
logger.debug(
f" Condition {eig[0]:0.3f} / {eig[1]:0.3f} = "
f"{eig[0] / eig[1]:03.1f}, Removing in component "
f"{ri}: l={degrees[ri]}, m={orders[ri]:+0.0f}"
)
logger.debug(
f" Resulting information: {I_tots[lim_idx]:0.1f} "
f"bits/sample ({100 * I_tots[lim_idx] / max_info:0.1f}% of peak "
f"{max_info:0.1f})"
)
else:
in_removes = remove_order[:0]
return in_removes, out_removes
def _compute_sphere_activation_in(degrees):
"""Compute the "in" power from random currents in a sphere.
Parameters
----------
degrees : ndarray
The degrees to evaluate.
Returns
-------
a_power : ndarray
The a_lm associated for the associated degrees (see
:footcite:`KnuutilaEtAl1993`).
rho_i : float
The current density.
References
----------
.. footbibliography::
"""
r_in = 0.080 # radius of the randomly-activated sphere
# set the observation point r=r_s, az=el=0, so we can just look at m=0 term
# compute the resulting current density rho_i
# This is the "surface" version of the equation:
# b_r_in = 100e-15 # fixed radial field amplitude at distance r_s = 100 fT
# r_s = 0.13 # 5 cm from the surface
# rho_degrees = np.arange(1, 100)
# in_sum = (rho_degrees * (rho_degrees + 1.) /
# ((2. * rho_degrees + 1.)) *
# (r_in / r_s) ** (2 * rho_degrees + 2)).sum() * 4. * np.pi
# rho_i = b_r_in * 1e7 / np.sqrt(in_sum)
# rho_i = 5.21334885574e-07 # value for r_s = 0.125
rho_i = 5.91107375632e-07 # deterministic from above, so just store it
a_power = _sq(rho_i) * (
degrees
* r_in ** (2 * degrees + 4)
/ (_sq(2.0 * degrees + 1.0) * (degrees + 1.0))
)
return a_power, rho_i
def _trans_sss_basis(exp, all_coils, trans=None, coil_scale=100.0):
"""Compute SSS basis (optionally) using a dev<->head trans."""
if trans is not None:
if not isinstance(trans, Transform):
trans = Transform("meg", "head", trans)
assert not np.isnan(trans["trans"]).any()
all_coils = (
apply_trans(trans, all_coils[0]),
apply_trans(trans, all_coils[1], move=False),
) + all_coils[2:]
if not isinstance(coil_scale, np.ndarray):
# Scale all magnetometers (with `coil_class` == 1.0) by `mag_scale`
cs = coil_scale
coil_scale = np.ones((all_coils[3], 1))
coil_scale[all_coils[4]] = cs
S_tot = _sss_basis(exp, all_coils)
S_tot *= coil_scale
return S_tot
# intentionally omitted: st_duration, st_correlation, destination, st_fixed,
# st_only
@verbose
def find_bad_channels_maxwell(
raw,
limit=7.0,
duration=5.0,
min_count=5,
return_scores=False,
origin="auto",
int_order=8,
ext_order=3,
calibration=None,
cross_talk=None,
coord_frame="head",
regularize="in",
ignore_ref=False,
bad_condition="error",
head_pos=None,
mag_scale=100.0,
skip_by_annotation=("edge", "bad_acq_skip"),
h_freq=40.0,
extended_proj=(),
verbose=None,
):
r"""Find bad channels using Maxwell filtering.
Parameters
----------
raw : instance of Raw
Raw data to process.
limit : float
Detection limit for noisy segments (default is 7.). Smaller values will
find more bad channels at increased risk of including good ones. This
value can be interpreted as the standard score of differences between
the original and Maxwell-filtered data. See the ``Notes`` section for
details.
.. note:: This setting only concerns *noisy* channel detection.
The limit for *flat* channel detection currently cannot be
controlled by the user. Flat channel detection is always run
before noisy channel detection.
duration : float
Duration of the segments into which to slice the data for processing,
in seconds. Default is 5.
min_count : int
Minimum number of times a channel must show up as bad in a chunk.
Default is 5.
return_scores : bool
If ``True``, return a dictionary with scoring information for each
evaluated segment of the data. Default is ``False``.
.. warning:: This feature is experimental and may change in a future
version of MNE-Python without prior notice. Please
report any problems and enhancement proposals to the
developers.
.. versionadded:: 0.21
%(origin_maxwell)s
%(int_order_maxwell)s
%(ext_order_maxwell)s
%(calibration_maxwell_cal)s
%(cross_talk_maxwell)s
%(coord_frame_maxwell)s
%(regularize_maxwell_reg)s
%(ignore_ref_maxwell)s
%(bad_condition_maxwell_cond)s
%(head_pos_maxwell)s
%(mag_scale_maxwell)s
%(skip_by_annotation_maxwell)s
h_freq : float | None
The cutoff frequency (in Hz) of the low-pass filter that will be
applied before processing the data. This defaults to ``40.``, which
should provide similar results to MaxFilter. If you do not wish to
apply a filter, set this to ``None``.
%(extended_proj_maxwell)s
%(verbose)s
Returns
-------
noisy_chs : list
List of bad MEG channels that were automatically detected as being
noisy among the good MEG channels.
flat_chs : list
List of MEG channels that were detected as being flat in at least
``min_count`` segments.
scores : dict
A dictionary with information produced by the scoring algorithms.
Only returned when ``return_scores`` is ``True``. It contains the
following keys:
- ``ch_names`` : ndarray, shape (n_meg,)
The names of the MEG channels. Their order corresponds to the
order of rows in the ``scores`` and ``limits`` arrays.
- ``ch_types`` : ndarray, shape (n_meg,)
The types of the MEG channels in ``ch_names`` (``'mag'``,
``'grad'``).
- ``bins`` : ndarray, shape (n_windows, 2)
The inclusive window boundaries (start and stop; in seconds) used
to calculate the scores.
- ``scores_flat`` : ndarray, shape (n_meg, n_windows)
The scores for testing whether MEG channels are flat. These values
correspond to the standard deviation of a segment.
See the ``Notes`` section for details.
- ``limits_flat`` : ndarray, shape (n_meg, 1)
The score thresholds (in standard deviation) above which a segment
was classified as "flat".
- ``scores_noisy`` : ndarray, shape (n_meg, n_windows)
The scores for testing whether MEG channels are noisy. These values
correspond to the standard score of a segment.
See the ``Notes`` section for details.
- ``limits_noisy`` : ndarray, shape (n_meg, 1)
The score thresholds (in standard scores) above which a segment was
classified as "noisy".
.. note:: The scores and limits for channels marked as ``bad`` in the
input data will be set to ``np.nan``.
See Also
--------
annotate_amplitude
maxwell_filter
Notes
-----
All arguments after ``raw``, ``limit``, ``duration``, ``min_count``, and
``return_scores`` are the same as :func:`~maxwell_filter`, except that the
following are not allowed in this function because they are unused:
``st_duration``, ``st_correlation``, ``destination``, ``st_fixed``, and
``st_only``.
This algorithm, for a given chunk of data:
1. Runs SSS on the data, without removing external components.
2. Excludes channels as *flat* that have had low variability
(standard deviation < 0.01 fT or fT/cm in a 30 ms window) in the given
or any previous chunk.
3. For each channel :math:`k`, computes the *range* or peak-to-peak
:math:`d_k` of the difference between the reconstructed and original
data.
4. Computes the average :math:`\mu_d` and standard deviation
:math:`\sigma_d` of the differences (after scaling magnetometer data
to roughly match the scale of the gradiometer data using ``mag_scale``).
5. Marks channels as bad for the chunk when
:math:`d_k > \mu_d + \textrm{limit} \times \sigma_d`. Note that this
expression can be easily transformed into
:math:`(d_k - \mu_d) / \sigma_d > \textrm{limit}`, which is equivalent
to :math:`z(d_k) > \textrm{limit}`, with :math:`z(d_k)` being the
standard or z-score of the difference.
Data are processed in chunks of the given ``duration``, and channels that
are bad for at least ``min_count`` chunks are returned.
Channels marked as *flat* in step 2 are excluded from all subsequent steps
of noisy channel detection.
This algorithm gives results similar to, but not identical with,
MaxFilter. Differences arise because MaxFilter processes on a
buffer-by-buffer basis (using buffer-size-dependent downsampling logic),
uses different filtering characteristics, and possibly other factors.
Channels that are near the ``limit`` for a given ``min_count`` are
particularly susceptible to being different between the two
implementations.
.. versionadded:: 0.20
"""
if h_freq is not None:
if raw.info.get("lowpass") and raw.info["lowpass"] <= h_freq:
freq_loc = "below" if raw.info["lowpass"] < h_freq else "equal to"
msg = (
f"The input data has already been low-pass filtered with a "
f'{raw.info["lowpass"]} Hz cutoff frequency, which is '
f"{freq_loc} the requested cutoff of {h_freq} Hz. Not "
f"applying low-pass filter."
)
logger.info(msg)
else:
logger.info(
f"Applying low-pass filter with {h_freq} Hz cutoff frequency ..."
)
raw = raw.copy().load_data().filter(l_freq=None, h_freq=h_freq)
limit = float(limit)
onsets, ends = _annotations_starts_stops(raw, skip_by_annotation, invert=True)
del skip_by_annotation
# operate on chunks
starts = list()
stops = list()
step = int(round(raw.info["sfreq"] * duration))
for onset, end in zip(onsets, ends):
if end - onset >= step:
ss = np.arange(onset, end - step + 1, step)
starts.extend(ss)
ss = ss + step
ss[-1] = end
stops.extend(ss)
min_count = min(_ensure_int(min_count, "min_count"), len(starts))
logger.info(
"Scanning for bad channels in %d interval%s (%0.1f s) ...",
len(starts),
_pl(starts),
step / raw.info["sfreq"],
)
params = _prep_maxwell_filter(
raw,
skip_by_annotation=[], # already accounted for
origin=origin,
int_order=int_order,
ext_order=ext_order,
calibration=calibration,
cross_talk=cross_talk,
coord_frame=coord_frame,
regularize=regularize,
ignore_ref=ignore_ref,
bad_condition=bad_condition,
head_pos=head_pos,
mag_scale=mag_scale,
extended_proj=extended_proj,
)
del origin, int_order, ext_order, calibration, cross_talk, coord_frame
del regularize, ignore_ref, bad_condition, head_pos, mag_scale
good_meg_picks = params["meg_picks"][params["good_mask"]]
assert len(params["meg_picks"]) == len(params["coil_scale"])
assert len(params["good_mask"]) == len(params["meg_picks"])
noisy_chs = Counter()
flat_chs = Counter()
flat_limits = dict(grad=0.01e-13, mag=0.01e-15)
these_limits = np.array(
[
flat_limits["grad"] if pick in params["grad_picks"] else flat_limits["mag"]
for pick in good_meg_picks
]
)
flat_step = max(20, int(30 * raw.info["sfreq"] / 1000.0))
all_flats = set()
# Prepare variables to return if `return_scores=True`.
bins = np.empty((len(starts), 2)) # To store start, stop of each segment
# We create ndarrays with one row per channel, regardless of channel type
# and whether the channel has been marked as "bad" in info or not. This
# makes indexing in the loop easier. We only filter this down to the subset
# of MEG channels after all processing is done.
ch_names = np.array(raw.ch_names)
ch_types = np.array(raw.get_channel_types())
scores_flat = np.full((len(ch_names), len(starts)), np.nan)
scores_noisy = np.full_like(scores_flat, fill_value=np.nan)
thresh_flat = np.full((len(ch_names), 1), np.nan)
thresh_noisy = np.full_like(thresh_flat, fill_value=np.nan)
for si, (start, stop) in enumerate(zip(starts, stops)):
n_iter = 0
orig_data = raw.get_data(None, start, stop, verbose=False)
chunk_raw = RawArray(
orig_data,
params["info"],
first_samp=raw.first_samp + start,
copy="data",
verbose=False,
)
t = chunk_raw.times[[0, -1]] + start / raw.info["sfreq"]
logger.info(f" Interval {si + 1:3d}: {t[0]:8.3f} - {t[-1]:8.3f}")
# Flat pass: SD < 0.01 fT/cm or 0.01 fT for at 30 ms (or 20 samples)
n = stop - start
flat_stop = n - (n % flat_step)
data = chunk_raw.get_data(good_meg_picks, 0, flat_stop)
data.shape = (data.shape[0], -1, flat_step)
delta = np.std(data, axis=-1).min(-1) # min std across segments
# We may want to return this later if `return_scores=True`.
bins[si, :] = t[0], t[-1]
scores_flat[good_meg_picks, si] = delta
thresh_flat[good_meg_picks] = these_limits.reshape(-1, 1)
chunk_flats = delta < these_limits
chunk_flats = np.where(chunk_flats)[0]
chunk_flats = [
raw.ch_names[good_meg_picks[chunk_flat]] for chunk_flat in chunk_flats
]
flat_chs.update(chunk_flats)
all_flats |= set(chunk_flats)
chunk_flats = sorted(all_flats)
these_picks = [
pick for pick in good_meg_picks if raw.ch_names[pick] not in chunk_flats
]
if len(these_picks) == 0:
logger.info(f" Flat ({len(chunk_flats):2d}): <all>")
warn(
"All-flat segment detected, all channels will be marked as "
f"flat and processing will stop (t={t[0]:0.3f}). "
"Consider using annotate_amplitude before calling this "
'function with skip_by_annotation="bad_flat" (or similar) to '
"properly process all segments."
)
break # no reason to continue
# Bad pass
chunk_noisy = list()
params["st_duration"] = int(round(chunk_raw.times[-1] * raw.info["sfreq"]))
for n_iter in range(1, 101): # iteratively exclude the worst ones
assert set(raw.info["bads"]) & set(chunk_noisy) == set()
params["good_mask"][:] = [
chunk_raw.ch_names[pick]
not in raw.info["bads"] + chunk_noisy + chunk_flats
for pick in params["meg_picks"]
]
chunk_raw._data[:] = orig_data
delta = chunk_raw.get_data(these_picks)
with use_log_level(False):
_run_maxwell_filter(chunk_raw, reconstruct="orig", copy=False, **params)
if n_iter == 1 and len(chunk_flats):
logger.info(
" Flat (%2d): %s",
len(chunk_flats),
" ".join(chunk_flats),
)
delta -= chunk_raw.get_data(these_picks)
# p2p
range_ = np.ptp(delta, axis=-1)
cs_picks = np.searchsorted(params["meg_picks"], these_picks)
range_ *= params["coil_scale"][cs_picks, 0]
mean, std = np.mean(range_), np.std(range_)
# z score
z = (range_ - mean) / std
idx = np.argmax(z)
max_ = z[idx]
# We may want to return this later if `return_scores=True`.
scores_noisy[these_picks, si] = z
thresh_noisy[these_picks] = limit
if max_ < limit:
break
name = raw.ch_names[these_picks[idx]]
logger.debug(f" Bad: {name} {max_:0.1f}")
these_picks.pop(idx)
chunk_noisy.append(name)
noisy_chs.update(chunk_noisy)
noisy_chs = sorted(
(b for b, c in noisy_chs.items() if c >= min_count),
key=lambda x: raw.ch_names.index(x),
)
flat_chs = sorted(
(f for f, c in flat_chs.items() if c >= min_count),
key=lambda x: raw.ch_names.index(x),
)
# Only include MEG channels.
ch_names = ch_names[params["meg_picks"]]
ch_types = ch_types[params["meg_picks"]]
scores_flat = scores_flat[params["meg_picks"]]
thresh_flat = thresh_flat[params["meg_picks"]]
scores_noisy = scores_noisy[params["meg_picks"]]
thresh_noisy = thresh_noisy[params["meg_picks"]]
logger.info(f" Static bad channels: {noisy_chs}")
logger.info(f" Static flat channels: {flat_chs}")
logger.info("[done]")
if return_scores:
scores = dict(
ch_names=ch_names,
ch_types=ch_types,
bins=bins,
scores_flat=scores_flat,
limits_flat=thresh_flat,
scores_noisy=scores_noisy,
limits_noisy=thresh_noisy,
)
return noisy_chs, flat_chs, scores
else:
return noisy_chs, flat_chs
def _read_cross_talk(cross_talk, ch_names):
sss_ctc = dict()
ctc = None
if cross_talk is not None:
sss_ctc = _read_ctc(cross_talk)
ctc_chs = sss_ctc["proj_items_chs"]
# checking for extra space ambiguity in channel names
# between old and new fif files
if ch_names[0] not in ctc_chs:
ctc_chs = _clean_names(ctc_chs, remove_whitespace=True)
ch_names = _clean_names(ch_names, remove_whitespace=True)
missing = sorted(list(set(ch_names) - set(ctc_chs)))
if len(missing) != 0:
raise RuntimeError(f"Missing MEG channels in cross-talk matrix:\n{missing}")
missing = sorted(list(set(ctc_chs) - set(ch_names)))
if len(missing) > 0:
warn(f"Not all cross-talk channels in raw:\n{missing}")
ctc_picks = [ctc_chs.index(name) for name in ch_names]
ctc = sss_ctc["decoupler"][ctc_picks][:, ctc_picks]
# I have no idea why, but MF transposes this for storage..
sss_ctc["decoupler"] = sss_ctc["decoupler"].T.tocsc()
return ctc, sss_ctc
@verbose
def compute_maxwell_basis(
info,
origin="auto",
int_order=8,
ext_order=3,
calibration=None,
coord_frame="head",
regularize="in",
ignore_ref=True,
bad_condition="error",
mag_scale=100.0,
extended_proj=(),
verbose=None,
):
r"""Compute the SSS basis for a given measurement info structure.
Parameters
----------
%(info_not_none)s
%(origin_maxwell)s
%(int_order_maxwell)s
%(ext_order_maxwell)s
%(calibration_maxwell_cal)s
%(coord_frame_maxwell)s
%(regularize_maxwell_reg)s
%(ignore_ref_maxwell)s
%(bad_condition_maxwell_cond)s
%(mag_scale_maxwell)s
%(extended_proj_maxwell)s
%(verbose)s
Returns
-------
S : ndarray, shape (n_meg, n_moments)
The basis that can be used to reconstruct the data.
pS : ndarray, shape (n_moments, n_good_meg)
The (stabilized) pseudoinverse of the S array.
reg_moments : ndarray, shape (n_moments,)
The moments that were kept after regularization.
n_use_in : int
The number of kept moments that were in the internal space.
Notes
-----
This outputs variants of :math:`\mathbf{S}` and :math:`\mathbf{S^\dagger}`
from equations 27 and 37 of :footcite:`TauluKajola2005` with the coil scale
for magnetometers already factored in so that the resulting denoising
transform of the data to obtain :math:`\hat{\phi}_{in}` from equation
38 would be::
phi_in = S[:, :n_use_in] @ pS[:n_use_in] @ data_meg_good
.. versionadded:: 0.23
References
----------
.. footbibliography::
"""
_validate_type(info, Info, "info")
raw = RawArray(np.zeros((len(info["ch_names"]), 1)), info.copy(), verbose=False)
logger.info("Computing Maxwell basis")
params = _prep_maxwell_filter(
raw=raw,
origin=origin,
int_order=int_order,
ext_order=ext_order,
calibration=calibration,
coord_frame=coord_frame,
destination=None,
regularize=regularize,
ignore_ref=ignore_ref,
bad_condition=bad_condition,
mag_scale=mag_scale,
extended_proj=extended_proj,
)
_, S_decomp_full, pS_decomp, reg_moments, n_use_in = params[
"_get_this_decomp_trans"
](info["dev_head_t"], t=0.0)
return S_decomp_full, pS_decomp, reg_moments, n_use_in
|