Spaces:
Sleeping
Sleeping
File size: 157,465 Bytes
ce11d27 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 | """
GPU-accelerated 3D renderer with interactive controls β faithful port of
Android's My3DScatterRenderer.java + FlowFieldSystem.java + DashboardFragment.
WARNING β Import order matters on Windows:
Do NOT import this module before running pipeline.analyze() (which uses
ChromaDB). Loading OpenGL libraries (via vispy/PyOpenGL) before the
ChromaDB Rust backend can cause an access-violation segfault on Windows.
Use the lazy launch_renderer() wrapper in tracescope/__init__.py instead
of importing this module directly.
GPU rendering (vispy + OpenGL):
- Flow halo particles: circular discard, pow(1.6) alpha falloff, heat bloom
- Cluster points: vibrant boost (*1.3 + 0.1), centre-lit spherical gradient
- Turbo speed colormap: exact degree-5 polynomial coefficients
- 64,000 particles (40^3), auto-rotation 7.5 deg/s, dark background
Interactive controls (Qt panel):
- Flow / Ball / Auto-Rotate toggle checkboxes
- Show Points / Show Path toggles
- Bidirectional X/Y/Z probe sliders (track ball OR set position manually)
- Mark Point / Clear buttons with Catmull-Rom connecting spline
- Cluster legend with counts
- Info panel: axis %, cluster distances, nearest message texts
Keyboard shortcuts (when 3D canvas has focus):
Space drag probe by flow F flow particles B ball
M mark probe point E explain attractor / marked path
P points L path R reset camera Esc quit
+/- particle size
Requires: pip install vispy PyOpenGL PyQt5 (or PySide6)
"""
from __future__ import annotations
import threading
import warnings
import numpy as np
# Silence vispy's isosurface scalar overflow warnings (harmless β they fire
# during marching-cubes shift computation on narrow integer dtypes).
warnings.filterwarnings(
"ignore",
message="overflow encountered in scalar",
category=RuntimeWarning,
module=r"vispy\.geometry\.isosurface",
)
try:
from vispy import app, scene, gloo
from vispy.visuals import Visual
from vispy.scene.visuals import create_visual_node
from vispy.visuals.transforms import STTransform
except ImportError:
raise ImportError(
"vispy is required for the GPU renderer.\n"
"Install with: pip install vispy PyOpenGL PyQt5\n"
" (or replace PyQt5 with PySide6 / pyglet / glfw)"
)
def _ensure_viable_backend():
"""Try to use the best available vispy backend, falling back to software
rendering if GPU/OpenGL is not available. Called once before building the
canvas so that vispy is configured for the current environment.
"""
# If a backend is already set and working, leave it alone.
try:
current = app.use_app()
if current is not None:
return
except Exception:
pass
# Preferred order: PyQt5 (GPU), PySide6 (GPU), pyglet, osmesa (CPU-only)
for backend in ("pyqt5", "pyside6", "pyglet", "osmesa"):
try:
app.use_app(backend)
return
except Exception:
continue
# Last resort β let vispy pick whatever it can find
try:
app.use_app()
except Exception:
pass
# Qt imports β try PyQt5 first (most common), then PySide6
try:
from PyQt5 import QtWidgets, QtCore
except ImportError:
try:
from PySide6 import QtWidgets, QtCore
except ImportError:
QtWidgets = None # type: ignore[assignment]
from .flow_field import FlowFieldSystem
from .scatter3d import catmull_rom_spline
from .probe import probe_point, probe_with_explanation
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# GLSL shaders β exact port of Android My3DScatterRenderer.java
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
FLOW_HALO_VERT = """
attribute vec3 a_position;
attribute vec4 a_color;
varying vec4 v_color;
uniform float u_base_size;
void main() {
vec4 clip = $transform(vec4(a_position, 1.0));
gl_Position = clip;
float size = u_base_size / max(0.001, clip.w);
gl_PointSize = clamp(size, 2.0, 64.0);
v_color = a_color;
}
"""
FLOW_HALO_FRAG = """
varying vec4 v_color;
void main() {
vec2 d = gl_PointCoord - vec2(0.5);
float r2 = dot(d, d);
if (r2 > 0.25) discard;
float alpha = pow(clamp((0.5 - sqrt(r2)) * 2.0, 0.0, 1.0), 1.6);
vec3 rgb = mix(v_color.rgb * 0.65, v_color.rgb, alpha);
gl_FragColor = vec4(rgb, alpha * v_color.a);
}
"""
CLUSTER_POINT_VERT = """
attribute vec3 a_position;
attribute vec4 a_color;
varying vec4 v_color;
uniform float u_point_size;
void main() {
vec4 clip = $transform(vec4(a_position, 1.0));
gl_Position = clip;
float dist = length(clip.xyz);
gl_PointSize = u_point_size + (1.0 / dist);
v_color = a_color;
}
"""
CLUSTER_POINT_FRAG = """
varying vec4 v_color;
void main() {
vec2 coord = gl_PointCoord - vec2(0.5);
float dist = length(coord);
if (dist > 0.5) discard;
float lightFactor = 0.8 + 0.2 * (1.0 - smoothstep(0.0, 0.5, dist));
vec3 vibrant = clamp(v_color.rgb * 1.3 + vec3(0.1), vec3(0.0), vec3(1.0));
gl_FragColor = vec4(vibrant * lightFactor, v_color.a);
}
"""
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# Color palettes (matching Android exactly)
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
CLUSTER_COLORS_GL = np.array([
[1.0, 0.0, 0.0, 1.0], # red
[0.0, 1.0, 0.0, 1.0], # green
[0.0, 0.0, 1.0, 1.0], # blue
[1.0, 1.0, 0.0, 1.0], # yellow
[1.0, 0.0, 1.0, 1.0], # magenta
[0.0, 1.0, 1.0, 1.0], # cyan
[1.0, 0.5, 0.0, 1.0], # orange
[0.5, 0.0, 1.0, 1.0], # purple
[0.0, 0.5, 1.0, 1.0], # sky blue
[0.5, 1.0, 0.5, 1.0], # pastel green
], dtype=np.float32)
AXIS_COLORS = [
np.array([112, 73, 78, 255], dtype=np.float32) / 255,
np.array([73, 112, 78, 255], dtype=np.float32) / 255,
np.array([73, 78, 112, 255], dtype=np.float32) / 255,
]
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# Custom vispy Visuals (unchanged β exact Android shaders)
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
class _FlowHaloVisual(Visual):
def __init__(self, base_size=40.0):
Visual.__init__(self, vcode=FLOW_HALO_VERT, fcode=FLOW_HALO_FRAG)
self._draw_mode = 'points'
self._n = 0
self.set_gl_state(depth_test=True, cull_face=False, blend=True,
blend_func=('src_alpha', 'one_minus_src_alpha'))
self.shared_program['u_base_size'] = float(base_size)
self.shared_program['a_position'] = gloo.VertexBuffer(np.zeros((1, 3), dtype=np.float32))
self.shared_program['a_color'] = gloo.VertexBuffer(np.zeros((1, 4), dtype=np.float32))
def set_data(self, positions, colors):
self._n = len(positions)
pos = positions if positions.flags['C_CONTIGUOUS'] and positions.dtype == np.float32 \
else np.ascontiguousarray(positions, dtype=np.float32)
col = colors if colors.flags['C_CONTIGUOUS'] and colors.dtype == np.float32 \
else np.ascontiguousarray(colors, dtype=np.float32)
self.shared_program['a_position'].set_data(pos)
self.shared_program['a_color'].set_data(col)
self.update()
def set_base_size(self, size):
self.shared_program['u_base_size'] = float(size)
self.update()
def _prepare_transforms(self, view):
view.view_program.vert['transform'] = view.transforms.get_transform()
def _prepare_draw(self, view):
if self._n < 1:
return False
gloo.set_state(depth_mask=False)
return True
def draw(self, *args, **kwargs):
super().draw(*args, **kwargs)
gloo.set_state(depth_mask=True)
class _ClusterPointVisual(Visual):
def __init__(self, point_size=9.0):
Visual.__init__(self, vcode=CLUSTER_POINT_VERT, fcode=CLUSTER_POINT_FRAG)
self._draw_mode = 'points'
self._n = 0
self.set_gl_state(depth_test=True, cull_face=False, blend=True,
blend_func=('src_alpha', 'one_minus_src_alpha'))
self.shared_program['u_point_size'] = float(point_size)
self.shared_program['a_position'] = gloo.VertexBuffer(np.zeros((1, 3), dtype=np.float32))
self.shared_program['a_color'] = gloo.VertexBuffer(np.zeros((1, 4), dtype=np.float32))
def set_data(self, positions, colors):
self._n = len(positions)
self.shared_program['a_position'].set_data(np.ascontiguousarray(positions, dtype=np.float32))
self.shared_program['a_color'].set_data(np.ascontiguousarray(colors, dtype=np.float32))
self.update()
def set_point_size(self, size):
self.shared_program['u_point_size'] = float(size)
self.update()
def _prepare_transforms(self, view):
view.view_program.vert['transform'] = view.transforms.get_transform()
def _prepare_draw(self, view):
return self._n > 0
FlowHalo = create_visual_node(_FlowHaloVisual)
ClusterPoint = create_visual_node(_ClusterPointVisual)
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# Qt dark theme
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def _build_dark_qss(scale: float = 1.0) -> str:
"""Build Qt stylesheet with font sizes scaled for window size."""
fs_12 = max(9, int(round(12 * scale)))
fs_11 = max(9, int(round(11 * scale)))
return f"""
QMainWindow, QWidget#central {{ background: #1E1E1E; }}
QGroupBox {{
background: #2A2A2A; border: 1px solid #3A3A3A; border-radius: 6px;
margin-top: 4px; padding: 24px 8px 8px 8px; font-size: {fs_12}px;
}}
QGroupBox::title {{
subcontrol-origin: padding;
subcontrol-position: top center;
padding: 4px 8px;
color: white; font-weight: bold;
}}
QCheckBox {{ color: #E0E0E0; spacing: 6px; font-size: {fs_12}px; }}
QCheckBox::indicator {{ width: 14px; height: 14px; }}
QLabel {{ color: #E0E0E0; font-size: {fs_12}px; }}
QSlider::groove:horizontal {{
height: 4px; background: #3A3A3A; border-radius: 2px;
}}
QSlider::handle:horizontal {{
width: 14px; height: 14px; margin: -5px 0;
background: #FF6B35; border-radius: 7px;
}}
QPushButton {{
background: #FF6B35; color: white; border: none;
border-radius: 4px; padding: 5px 14px; font-size: {fs_12}px;
}}
QPushButton:hover {{ background: #FF8555; }}
QPushButton#btn-secondary {{ background: #555; }}
QPushButton#btn-secondary:hover {{ background: #777; }}
QTextEdit {{
background: #1E1E1E; color: #E0E0E0; border: 1px solid #3A3A3A;
font-size: {fs_11}px; font-family: Consolas, monospace;
}}
QScrollArea {{ border: none; background: transparent; }}
QScrollBar:vertical {{
width: 4px; background: transparent; margin: 0; padding: 0;
}}
QScrollBar::handle:vertical {{
background: rgba(255, 255, 255, 30); border-radius: 2px; min-height: 20px;
}}
QScrollBar::add-line:vertical, QScrollBar::sub-line:vertical,
QScrollBar::add-page:vertical, QScrollBar::sub-page:vertical {{
background: transparent; height: 0px;
}}
"""
DARK_QSS = _build_dark_qss(1.0)
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# FlowRenderer β GPU rendering + interactive Qt controls
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
class _SliderEventFilter(QtCore.QObject if QtWidgets is not None else object):
"""Detects mouse press/release on sliders to temporarily pause ball sync."""
def __init__(self, renderer):
if QtWidgets is not None:
super().__init__()
self._renderer = renderer
def eventFilter(self, obj, event):
if event.type() == QtCore.QEvent.MouseButtonPress:
self._renderer._manual_slider_override = True
elif event.type() == QtCore.QEvent.MouseButtonRelease:
self._renderer._manual_slider_override = False
return False
class FlowRenderer:
"""GPU-accelerated 3D flow renderer with interactive controls panel."""
AUTO_ROTATE_SPEED = 7.5
BALL_RADIUS = 0.07
BALL_COLOR = (1.0, 0.2, 0.2, 1.0)
TRAIL_COLOR = (1.0, 1.0, 0.0, 1.0)
PATH_COLOR = (1.0, 0.0, 0.0, 1.0)
PROBE_COLOR = np.array([[1.0, 1.0, 0.0, 1.0]], dtype=np.float32)
def __init__(self, result, particle_grid=40, base_size=28.0,
window_size=(1400, 800), title='TraceScope Flow Renderer',
explainer=None, dataset_name=None, initial_state=None):
self.result = result
self.base_size = base_size
self._title = title
self._explainer = explainer
self._dataset_name = dataset_name
self._initial_state = initial_state or {}
# ββ State βββββββββββββββββββββββββββββββββββββ
self._has_flow = result.velocity_grid is not None
self.flow_active = self._has_flow
self.show_points = False
self.show_path = False
self.ball_flowing = self._has_flow
self.auto_rotate = False
self._last_time = None
self._frame_count = 0
self._updating_sliders = False
self._manual_slider_override = False
self._control_points: list = []
self._particle_grid = particle_grid
self._spline_path = False # default: straight lines; True = Catmull-Rom splines
self._explain_pending = None # async explain result (set by bg thread, consumed by timer)
self._explain_pending_stage = None # intermediate progress text
self._debug_prompts = False # set True to print all LLM prompts to stdout
# Persistent explained paths & attractor explanations
self._saved_paths: list = [] # [{control_points, explanation}]
self._saved_path_visuals: list = [] # [{'markers': vis, 'line': vis}]
self._highlighted_path: int | None = None
self._saved_paths_visible = True
self._attractor_explanations: dict = {} # {idx: explanation_text}
# Cluster colors (positions scaled below)
n_cls = result.clusters.n_clusters
self._cluster_colors_arr = np.array([
CLUSTER_COLORS_GL[c % len(CLUSTER_COLORS_GL)][:3]
for c in range(n_cls)
], dtype=np.float32) # (K, 3)
# ββ Bounds ββββββββββββββββββββββββββββββββββββ
pts = result.projected_3d
self._data_center = pts.mean(axis=0).astype(np.float32)
self._axis_min = (np.asarray(result.axis_min, dtype=np.float32)
if result.axis_min is not None
else pts.min(axis=0).astype(np.float32) - 0.1)
self._axis_max = (np.asarray(result.axis_max, dtype=np.float32)
if result.axis_max is not None
else pts.max(axis=0).astype(np.float32) + 0.1)
# ββ Scene Normalization βββββββββββββββββββββββββ
# Apply a SCALE TRANSFORM to the scene so the data always appears
# at a standard extent (target span magnitude = 6.0). This way all
# visual sizes (particles, fonts, camera distance, speed) can be
# FIXED absolute values and look identical across datasets.
_raw_span = self._axis_max - self._axis_min
_raw_mag = float(np.linalg.norm(_raw_span))
_TARGET_SPAN_MAG = 6.0
self._scene_scale = _TARGET_SPAN_MAG / _raw_mag if _raw_mag > 0.01 else 1.0
print(f"[SCALE] raw_span_mag={_raw_mag:.2f}, scene_scale={self._scene_scale:.3f} "
f"(scene rescaled to standard size)")
# Cluster centroids (in original data space)
self._cluster_centroids = (
result.cluster_centroids_3d.astype(np.float32)
if result.cluster_centroids_3d is not None
else np.zeros((n_cls, 3), dtype=np.float32)
)
# Use fixed absolute visual params β same for every dataset
self.base_size = base_size # Fixed (default 32)
# ββ Path sample points for flow blob (computed before flow init) ββ
# Build per-path splines to avoid spurious cross-path connections.
# Always include the raw data points as blob seeds so every data
# point is guaranteed to be inside the blob.
self._path_sample_pts = None
if len(result.projected_3d) >= 2:
path_groups: dict[int | None, list[int]] = {}
for idx, entry in enumerate(result.session.entries):
pid = entry.path_id
path_groups.setdefault(pid, []).append(idx)
spline_parts = [result.projected_3d.copy()] # raw data points first
for pid, indices in path_groups.items():
if len(indices) < 2:
continue
path_pts = result.projected_3d[indices]
sps = max(2, 20 // max(1, len(path_groups) // 10))
spline_parts.append(catmull_rom_spline(path_pts, sps))
self._path_sample_pts = np.concatenate(spline_parts, axis=0).astype(np.float32)
# ββ Flow system ββββββββββββββββββββββββββββββ
self.flow: FlowFieldSystem | None = None
if self._has_flow:
self.flow = FlowFieldSystem(
result.velocity_grid, self._axis_min, self._axis_max,
particle_grid=particle_grid,
path_points=self._path_sample_pts,
confidence_grid=getattr(result, 'confidence_grid', None),
)
# Fixed speed β scene is already normalized via transform
self.flow.speed_multiplier = 0.95
# Pre-allocate flow RGBA buffer for performance
if self.flow is not None:
self._flow_rgba = np.empty((self.flow.particle_count, 4), dtype=np.float32)
else:
self._flow_rgba = None
# ββ Canvas + Scene ββββββββββββββββββββββββββββ
self.canvas = scene.SceneCanvas(
keys='interactive', bgcolor=(0.12, 0.12, 0.12, 1.0),
size=window_size, show=False, title=title,
vsync=False, # Windows DWM otherwise throttles paints on
# small/unfocused windows, making the animation look slow
# until the window is resized larger.
)
self.view = self.canvas.central_widget.add_view()
# Scaled center so camera sees the normalized scene
_scaled_center = tuple((self._data_center * self._scene_scale).tolist())
self.view.camera = scene.TurntableCamera(
fov=60, distance=9.0,
center=_scaled_center,
up='+y', azimuth=0, elevation=20,
)
# Render root: intermediate Node with a scale transform so all
# visuals render at a standard size regardless of raw data extent.
# All 3D visuals are parented to this node instead of view.scene.
self._render_root = scene.Node(parent=self.view.scene)
self._render_root.transform = STTransform(
scale=(self._scene_scale, self._scene_scale, self._scene_scale)
)
# ββ Build 3D visuals ββββββββββββββββββββββββββ
self._build_visuals()
# ββ Build Qt window with controls βββββββββββββ
self.window = None
if QtWidgets is not None:
self._build_qt_window(window_size)
# ββ Keyboard (fallback when canvas has focus) β
self.canvas.events.key_press.connect(self._on_key)
# ββ Mouse double-click to select nearest data point β
self.canvas.events.mouse_double_click.connect(self._on_double_click)
# ββ Ctrl+drag gizmo for probe positioning β
self.canvas.events.mouse_press.connect(self._on_click_track_press)
self.canvas.events.mouse_move.connect(self._on_click_track_move)
self.canvas.events.mouse_release.connect(self._on_click_track_release)
self.canvas.events.mouse_press.connect(self._on_gizmo_press)
self.canvas.events.mouse_release.connect(self._on_attractor_click)
self.canvas.events.mouse_move.connect(self._on_gizmo_move)
self.canvas.events.mouse_release.connect(self._on_gizmo_release)
self.canvas.events.key_press.connect(self._on_gizmo_key)
self.canvas.events.key_release.connect(self._on_gizmo_key_release)
# ββ Timer βββββββββββββββββββββββββββββββββββββ
self._timer = app.Timer(interval=1.0 / 60, connect=self._on_timer)
# Force Qt PreciseTimer so the callback rate isn't throttled when the
# window is small or unfocused (Windows DWM otherwise coalesces the
# default CoarseTimer with paint events, slowing the animation until
# the window is resized/maximized).
try:
self._timer._backend.setTimerType(QtCore.Qt.PreciseTimer)
except Exception:
pass
# βββββββββββββββββββββββββββββββββββββββββββββββββββ
# 3D Visual construction
# βββββββββββββββββββββββββββββββββββββββββββββββββββ
def _build_visuals(self):
# Cluster points
self.vis_clusters = ClusterPoint(point_size=7.0, parent=self._render_root)
self.vis_clusters.order = 0
self._setup_cluster_data()
self.vis_clusters.visible = self.show_points
# Flow halo particles
self.vis_flow = FlowHalo(base_size=self.base_size, parent=self._render_root)
self.vis_flow.order = 1
self.vis_flow.visible = self.flow_active
# Spline path β both Tube and Line visuals (toggle with Simple Lines)
spline_pts, spline_colors = self._compute_path()
self._spline_pts = spline_pts # store for blob computation
# Base alpha for path
self._path_base_alpha = 0.88
spline_colors[:, 3] = self._path_base_alpha
# Tube path (no directional lighting β flat vertex colors)
tube_points = 10
vertex_colors = np.repeat(spline_colors, tube_points, axis=0)
try:
self.vis_path_tube = scene.visuals.Tube(
points=spline_pts, radius=0.015,
tube_points=tube_points, shading=None,
vertex_colors=vertex_colors,
parent=self._render_root,
)
except Exception:
self.vis_path_tube = scene.visuals.Line(
pos=spline_pts, color=spline_colors, width=4,
parent=self._render_root, antialias=True,
)
self.vis_path_tube.order = 2
self.vis_path_tube.visible = self.show_path and self._spline_path
# Simple line path β straight lines between actual data points
straight_pts, straight_colors = self._compute_straight_path()
straight_colors[:, 3] = self._path_base_alpha
self.vis_path_line = scene.visuals.Line(
pos=straight_pts, color=straight_colors, width=3,
parent=self._render_root, antialias=True,
)
self.vis_path_line.order = 2
self.vis_path_line.visible = self.show_path and not self._spline_path
# Probe marker (yellow) β render on top of everything (incl. attractor mesh)
self.vis_probe = ClusterPoint(point_size=15.0, parent=self._render_root)
self.vis_probe.order = 10
self.vis_probe.set_gl_state(
depth_test=False, cull_face=False, blend=True,
blend_func=('src_alpha', 'one_minus_src_alpha'),
)
self.vis_probe.set_data(
self._data_center.reshape(1, 3),
self.PROBE_COLOR,
)
# ββ Gizmo: 3 axis arrows for Ctrl+drag probe positioning ββ
pts = self.result.projected_3d
self._gizmo_arrow_len = float(np.max(pts.max(0) - pts.min(0))) * 0.084
self._gizmo_colors = [
(1.0, 0.2, 0.2, 0.9), # X = red
(0.2, 1.0, 0.2, 0.9), # Y = green
(0.3, 0.5, 1.0, 0.9), # Z = blue
]
self._gizmo_axes = [
np.array([1, 0, 0], dtype=np.float32),
np.array([0, 1, 0], dtype=np.float32),
np.array([0, 0, 1], dtype=np.float32),
]
self._gizmo_lines = [] # shaft lines
self._gizmo_heads = [] # arrowhead lines
for i in range(3):
line = scene.visuals.Line(
pos=np.zeros((2, 3), dtype=np.float32),
color=self._gizmo_colors[i], width=3,
parent=self._render_root, antialias=True,
)
line.order = 10
line.set_gl_state(depth_test=False, blend=True,
blend_func=('src_alpha', 'one_minus_src_alpha'))
line.visible = False
self._gizmo_lines.append(line)
# Arrowhead (two short lines forming a V)
head = scene.visuals.Line(
pos=np.zeros((3, 3), dtype=np.float32),
color=self._gizmo_colors[i], width=3,
connect='segments',
parent=self._render_root, antialias=True,
)
head.order = 10
head.set_gl_state(depth_test=False, blend=True,
blend_func=('src_alpha', 'one_minus_src_alpha'))
head.visible = False
self._gizmo_heads.append(head)
# Gizmo drag state
self._gizmo_visible = False
self._gizmo_dragging = None # axis index (0/1/2) or None
self._gizmo_drag_start_pos = None
self._gizmo_drag_start_click = None
self._gizmo_drag_axis_screen_n = None
self._gizmo_drag_px_per_unit = 1.0
self._gizmo_persistent = False
# Plain-click tracking (for click-near-probe detection)
self._click_track_pos = None
self._click_track_moved = False
# Start stuck so the initial hint invites the user to interact with the probe.
# Flips to False once the ball has actually been moving for a while.
self._ball_is_stuck = True
self._ball_stuck_frames = 0
self._ball_motion_frames = 0
self._last_ball_pos = None
# Control-point markers (white) + connecting spline
self.vis_markers = ClusterPoint(point_size=10.0, parent=self._render_root)
self.vis_markers.order = 3
self.vis_markers.visible = False
self.vis_marked_path = scene.visuals.Line(
pos=np.zeros((2, 3), dtype=np.float32),
color=(1.0, 0.5, 0.2, 0.8), width=3,
parent=self._render_root, antialias=True,
)
self.vis_marked_path.order = 3
self.vis_marked_path.visible = False
# Attractor basin point clouds (like blob debug, but colored by score)
self._attractor_clouds = [] # list of ClusterPoint visuals
self._attractor_labels = [] # list of Text visuals
self._attractor_data = [] # cached attractor dicts
self._attractors_visible = False
self._highlighted_attractor = None # index into _attractor_data or None
self._attractor_highlight_border = None # ClusterPoint for selected border
# Ball sphere
self.vis_ball = scene.visuals.Sphere(
radius=self.BALL_RADIUS, cols=24, rows=24,
color=self.BALL_COLOR, parent=self._render_root, method='latitude',
)
self.vis_ball.order = 4
self.vis_ball.visible = False
# Ball trail
self.vis_trail = scene.visuals.Line(
pos=np.zeros((2, 3), dtype=np.float32),
color=self.TRAIL_COLOR, width=4,
parent=self._render_root, antialias=True,
)
self.vis_trail.order = 4
self.vis_trail.visible = False
# Debug: blob boundary visualization (toggle with D key)
# Shows a point cloud of the blob-occupied cells as semi-transparent markers
self._debug_blob = False
self.vis_blob_debug = ClusterPoint(point_size=4.0, parent=self._render_root)
self.vis_blob_debug.order = 6
self.vis_blob_debug.visible = False
if self.flow is not None:
blob_pts = self.flow.get_blob_surface_points()
if blob_pts is not None:
blob_colors = np.ones((len(blob_pts), 4), dtype=np.float32)
blob_colors[:, :3] = [0.2, 0.8, 0.4] # green
blob_colors[:, 3] = 0.15 # very transparent
self.vis_blob_debug.set_data(blob_pts, blob_colors)
# Axes + labels
self._setup_axes()
self._setup_axis_labels()
def _setup_cluster_data(self):
pts = np.ascontiguousarray(self.result.projected_3d, dtype=np.float32)
colors = np.zeros((len(pts), 4), dtype=np.float32)
for i, label in enumerate(self.result.clusters.labels):
colors[i] = CLUSTER_COLORS_GL[label % len(CLUSTER_COLORS_GL)]
self.vis_clusters.set_data(pts, colors)
def _compute_path(self):
"""Compute Catmull-Rom spline with per-vertex cluster-interpolated colors."""
pts = self.result.projected_3d
if len(pts) < 2:
return np.zeros((2, 3), dtype=np.float32), np.array([[1, 0, 0, 1]] * 2, dtype=np.float32)
spline = catmull_rom_spline(pts, 20).astype(np.float32)
# Assign cluster colors to each spline point by nearest data point
# For each data point, get its cluster color
data_colors = np.zeros((len(pts), 4), dtype=np.float32)
for i, label in enumerate(self.result.clusters.labels):
data_colors[i] = CLUSTER_COLORS_GL[label % len(CLUSTER_COLORS_GL)]
# For each spline point, find nearest data point (KDTree avoids O(S*N) memory)
from scipy.spatial import cKDTree
tree = cKDTree(pts)
_, nearest_idx = tree.query(spline, k=1)
spline_colors = data_colors[nearest_idx]
# Smooth colors with a simple moving average (window=5) for nice transitions
kernel = 5
if len(spline_colors) > kernel:
pad = kernel // 2
padded = np.pad(spline_colors, ((pad, pad), (0, 0)), mode='edge')
smoothed = np.zeros_like(spline_colors)
for j in range(len(spline_colors)):
smoothed[j] = padded[j:j + kernel].mean(axis=0)
smoothed[:, 3] = 1.0 # keep alpha = 1
spline_colors = smoothed
return spline, spline_colors
def _compute_straight_path(self):
"""Straight lines between actual data points with cluster colors."""
pts = self.result.projected_3d
if len(pts) < 2:
return np.zeros((2, 3), dtype=np.float32), np.array([[1, 0, 0, 1]] * 2, dtype=np.float32)
pts = pts.astype(np.float32)
colors = np.zeros((len(pts), 4), dtype=np.float32)
for i, label in enumerate(self.result.clusters.labels):
colors[i] = CLUSTER_COLORS_GL[label % len(CLUSTER_COLORS_GL)]
return pts, colors
def _setup_axes(self):
mn, mx = self._axis_min, self._axis_max
self.vis_axes = []
for i in range(3):
start, end = mn.copy(), mn.copy()
end[i] = mx[i]
line = scene.visuals.Line(
pos=np.array([start, end], dtype=np.float32),
color=AXIS_COLORS[i], width=3,
parent=self._render_root, antialias=True,
)
line.order = 5
self.vis_axes.append(line)
# ββ Axis label config ββββββββββββββββββββββββββββββββ
# Fixed absolute font sizes β scene is pre-normalized to standard extent.
AXIS_LABEL_SIZE = 26
ATTRACTOR_LABEL_SIZE = 15
def _setup_axis_labels(self):
mn, mx = self._axis_min, self._axis_max
labels = getattr(self.result.axis_info, 'labels', ['Axis 1', 'Axis 2', 'Axis 3'])
axis_names = ['X', 'Y', 'Z']
self.vis_labels = []
offset = (mx - mn) * 0.03
for i in range(3):
pos = mn.copy()
pos[i] = mx[i] + offset[i]
label_text = labels[i] if i < len(labels) else f'Axis {i+1}'
t = scene.visuals.Text(
text=f'{axis_names[i]}: {label_text}',
pos=pos.reshape(1, 3).astype(np.float32),
color=AXIS_COLORS[i][:3], font_size=self.AXIS_LABEL_SIZE,
parent=self._render_root, anchor_x='left',
)
t.order = 6
self.vis_labels.append(t)
# βββββββββββββββββββββββββββββββββββββββββββββββββββ
# Qt window + controls panel
# βββββββββββββββββββββββββββββββββββββββββββββββββββ
def _build_qt_window(self, window_size):
self.window = QtWidgets.QMainWindow()
self.window.setWindowTitle(self._title)
self.window.setStyleSheet(DARK_QSS)
central = QtWidgets.QWidget()
central.setObjectName('central')
layout = QtWidgets.QHBoxLayout(central)
layout.setContentsMargins(0, 0, 0, 0)
layout.setSpacing(0)
# Left: vispy 3D canvas
layout.addWidget(self.canvas.native, stretch=3)
# Right: controls panel
layout.addWidget(self._build_controls(), stretch=0)
self.window.setCentralWidget(central)
self.window.resize(window_size[0], window_size[1])
# ββ Canvas overlays βββββββββββββββββββββββββββ
self._build_canvas_overlays()
_BASE_PANEL_WIDTH = 290
def _build_controls(self):
scroll = QtWidgets.QScrollArea()
scroll.setWidgetResizable(True)
scroll.setFixedWidth(self._BASE_PANEL_WIDTH)
self._controls_scroll = scroll
widget = QtWidgets.QWidget()
vbox = QtWidgets.QVBoxLayout(widget)
vbox.setContentsMargins(10, 8, 14, 8)
vbox.setSpacing(6)
axis_labels = getattr(self.result.axis_info, 'labels', ['X', 'Y', 'Z'])
# ββ 1. Flow Controls βββββββββββββββββββββββββββββ
grp = QtWidgets.QGroupBox("Flow Controls")
gl = QtWidgets.QVBoxLayout()
self.chk_flow = QtWidgets.QCheckBox("Flow Animation")
self.chk_flow.setChecked(self.flow_active)
self.chk_flow.setEnabled(self._has_flow)
self.chk_flow.toggled.connect(self._qt_flow_toggle)
gl.addWidget(self.chk_flow)
self.chk_ball = QtWidgets.QCheckBox("Drag probe by the flow")
self.chk_ball.setChecked(self._has_flow)
self.chk_ball.setEnabled(self._has_flow)
self.chk_ball.toggled.connect(self._qt_ball_toggle)
gl.addWidget(self.chk_ball)
self._gizmo_hint = QtWidgets.QLabel("Hold Ctrl + drag gizmo arrows to move probe")
self._gizmo_hint.setStyleSheet("color: #88ccff; font-size: 11px; padding: 2px 0;")
self._gizmo_hint.setVisible(not self._has_flow) # show when ball flow is off
gl.addWidget(self._gizmo_hint)
grp.setLayout(gl)
vbox.addWidget(grp)
# ββ 2. Display βββββββββββββββββββββββββββββββββββ
grp = QtWidgets.QGroupBox("Display")
gl = QtWidgets.QVBoxLayout()
self.chk_points = QtWidgets.QCheckBox("Show Data Points")
self.chk_points.setChecked(False)
self.chk_points.toggled.connect(self._qt_points_toggle)
gl.addWidget(self.chk_points)
self.chk_path = QtWidgets.QCheckBox("Show Path")
self.chk_path.setChecked(False)
self.chk_path.toggled.connect(self._qt_path_toggle)
gl.addWidget(self.chk_path)
self.chk_spline_path = QtWidgets.QCheckBox("Spline Path")
self.chk_spline_path.setChecked(False)
self.chk_spline_path.toggled.connect(self._qt_spline_path)
gl.addWidget(self.chk_spline_path)
self.chk_info_overlay = QtWidgets.QCheckBox("Show Info Overlay")
self.chk_info_overlay.setChecked(True)
self.chk_info_overlay.toggled.connect(self._qt_info_overlay_toggle)
gl.addWidget(self.chk_info_overlay)
self.chk_attractors = QtWidgets.QCheckBox("Show Attractors")
self.chk_attractors.setChecked(False)
self.chk_attractors.toggled.connect(self._qt_attractors_toggle)
gl.addWidget(self.chk_attractors)
# Sensitivity slider (hidden until attractors are shown)
self._attractor_sensitivity = 0.7 # default
self._sensitivity_row = QtWidgets.QWidget()
sl_lay = QtWidgets.QHBoxLayout(self._sensitivity_row)
sl_lay.setContentsMargins(0, 0, 0, 0)
self._lbl_sensitivity = QtWidgets.QLabel("Sensitivity: 70.0%")
sl_lay.addWidget(self._lbl_sensitivity)
self._sl_sensitivity = QtWidgets.QSlider(QtCore.Qt.Horizontal)
self._sl_sensitivity.setRange(0, 1000) # 0.0β100.0% with 0.1 steps
self._sl_sensitivity.setValue(700)
self._sl_sensitivity.setTracking(True) # update label in real-time
self._sl_sensitivity.valueChanged.connect(self._qt_sensitivity_label_update)
self._sl_sensitivity.sliderReleased.connect(self._qt_sensitivity_released)
sl_lay.addWidget(self._sl_sensitivity)
self._sensitivity_row.setVisible(False)
gl.addWidget(self._sensitivity_row)
grp.setLayout(gl)
vbox.addWidget(grp)
# ββ 3. Probe Sliders βββββββββββββββββββββββββββββ
grp = QtWidgets.QGroupBox("Probe")
gl = QtWidgets.QVBoxLayout()
self._slider_labels = []
self._sliders = []
for i in range(3):
lbl = QtWidgets.QLabel(f"{axis_labels[i]}: 50%")
gl.addWidget(lbl)
self._slider_labels.append(lbl)
sl = QtWidgets.QSlider(QtCore.Qt.Horizontal)
sl.setRange(0, 1000)
sl.setValue(500)
sl.valueChanged.connect(lambda v, idx=i: self._qt_slider_changed(idx, v))
gl.addWidget(sl)
self._sliders.append(sl)
self._slider_filter = _SliderEventFilter(self)
for sl in self._sliders:
sl.installEventFilter(self._slider_filter)
btn_row = QtWidgets.QHBoxLayout()
btn_mark = QtWidgets.QPushButton("Mark Point")
btn_mark.clicked.connect(self._qt_mark)
btn_row.addWidget(btn_mark)
btn_clear = QtWidgets.QPushButton("Clear")
btn_clear.setObjectName('btn-secondary')
btn_clear.clicked.connect(self._qt_clear)
btn_row.addWidget(btn_clear)
gl.addLayout(btn_row)
btn_explain = QtWidgets.QPushButton("Explain")
btn_explain.clicked.connect(self._qt_explain)
gl.addWidget(btn_explain)
# Toggle saved paths visibility
self._btn_toggle_paths = QtWidgets.QPushButton("Hide Saved Paths")
self._btn_toggle_paths.setObjectName('btn-secondary')
self._btn_toggle_paths.clicked.connect(self._toggle_saved_paths_visibility)
self._btn_toggle_paths.setVisible(False)
gl.addWidget(self._btn_toggle_paths)
grp.setLayout(gl)
vbox.addWidget(grp)
# ββ 4. Cluster Legend βββββββββββββββββββββββββββββ
grp = QtWidgets.QGroupBox("Clusters")
gl = QtWidgets.QVBoxLayout()
for c in range(self.result.clusters.n_clusters):
col = CLUSTER_COLORS_GL[c % len(CLUSTER_COLORS_GL)]
hexc = '#%02x%02x%02x' % (int(col[0]*255), int(col[1]*255), int(col[2]*255))
name = (self.result.cluster_labels[c]
if c < len(self.result.cluster_labels) else f"Cluster {c}")
cnt = sum(1 for l in self.result.clusters.labels if l == c)
lbl = QtWidgets.QLabel(
f'<span style="color:{hexc};font-size:14px;">●</span> '
f'<b>{name}</b> ({cnt})')
lbl.setTextFormat(QtCore.Qt.RichText)
lbl.setWordWrap(True)
gl.addWidget(lbl)
grp.setLayout(gl)
vbox.addWidget(grp)
# ββ 5. Flow Settings ββββββββββββββββββββββββββββ
grp = QtWidgets.QGroupBox("Flow Settings")
gl = QtWidgets.QVBoxLayout()
self._flow_opacity = 0.8
lbl_op = QtWidgets.QLabel("Opacity: 80%")
gl.addWidget(lbl_op)
sl_opacity = QtWidgets.QSlider(QtCore.Qt.Horizontal)
sl_opacity.setRange(0, 100)
sl_opacity.setValue(80)
sl_opacity.valueChanged.connect(lambda v, l=lbl_op: self._qt_flow_opacity(v, l))
gl.addWidget(sl_opacity)
self._lbl_speed = QtWidgets.QLabel("Speed: 0.95x")
gl.addWidget(self._lbl_speed)
self._sl_speed = QtWidgets.QSlider(QtCore.Qt.Horizontal)
self._sl_speed.setRange(10, 300)
self._sl_speed.setValue(95)
self._sl_speed.valueChanged.connect(lambda v, l=self._lbl_speed: self._qt_flow_speed(v, l))
gl.addWidget(self._sl_speed)
init_count = self._particle_grid ** 3
lbl_pc = QtWidgets.QLabel(f"Particles: {init_count:,} ({self._particle_grid}\u00b3)")
gl.addWidget(lbl_pc)
sl_particles = QtWidgets.QSlider(QtCore.Qt.Horizontal)
sl_particles.setRange(8, 50)
sl_particles.setValue(self._particle_grid)
sl_particles.valueChanged.connect(lambda v, l=lbl_pc: self._qt_particle_count(v, l))
gl.addWidget(sl_particles)
# Confidence opacity slider (MDN only)
has_conf = (self.flow is not None and self.flow._confidence_grid is not None)
self._confidence_strength = 0.0
lbl_conf = QtWidgets.QLabel("Confidence fade: OFF")
gl.addWidget(lbl_conf)
sl_conf = QtWidgets.QSlider(QtCore.Qt.Horizontal)
sl_conf.setRange(0, 100)
sl_conf.setValue(0)
sl_conf.setEnabled(has_conf)
sl_conf.valueChanged.connect(lambda v, l=lbl_conf: self._qt_confidence(v, l))
gl.addWidget(sl_conf)
if not has_conf:
lbl_conf.setText("Confidence fade: N/A (no density data)")
# Constrain to path toggle
self._chk_blob = QtWidgets.QCheckBox("Constrain to paths")
self._chk_blob.setChecked(True)
self._chk_blob.setEnabled(self.flow is not None)
self._chk_blob.toggled.connect(self._qt_blob_toggle)
gl.addWidget(self._chk_blob)
self._flow_color_mode = "speed"
self._chk_entropy = QtWidgets.QCheckBox("Entropy Colors")
self._chk_entropy.setChecked(False)
self._chk_entropy.toggled.connect(self._qt_entropy_colors)
gl.addWidget(self._chk_entropy)
self._chk_cluster_colors = QtWidgets.QCheckBox("Cluster Colors")
self._chk_cluster_colors.setChecked(False)
self._chk_cluster_colors.toggled.connect(self._qt_cluster_colors)
gl.addWidget(self._chk_cluster_colors)
# Score-based coloring (only shown when scores exist)
self._score_color_checkboxes = []
score_channels = self.result.score_channels
if score_channels:
for ch in score_channels:
chk = QtWidgets.QCheckBox(f"Score: {ch}")
chk.setChecked(False)
chk.toggled.connect(lambda checked, c=ch: self._qt_score_colors(c, checked))
gl.addWidget(chk)
self._score_color_checkboxes.append((ch, chk))
grp.setLayout(gl)
grp.setEnabled(self._has_flow)
vbox.addWidget(grp)
# Score-based point coloring (separate from flow)
if score_channels:
grp_score = QtWidgets.QGroupBox("Point Coloring")
gl_score = QtWidgets.QVBoxLayout()
self._point_color_mode = "cluster"
self._score_point_checkboxes = []
for ch in score_channels:
chk = QtWidgets.QCheckBox(f"Color points by: {ch}")
chk.setChecked(False)
chk.toggled.connect(lambda checked, c=ch: self._qt_score_point_coloring(c, checked))
gl_score.addWidget(chk)
self._score_point_checkboxes.append((ch, chk))
grp_score.setLayout(gl_score)
vbox.addWidget(grp_score)
# ββ Explanation Panel (hidden until Explain is pressed) ββ
self._explain_group = QtWidgets.QGroupBox("Explanation")
gl = QtWidgets.QVBoxLayout()
self._explain_text = QtWidgets.QTextEdit()
self._explain_text.setReadOnly(True)
self._explain_text.setMaximumHeight(300)
gl.addWidget(self._explain_text)
btn_close_explain = QtWidgets.QPushButton("Close")
btn_close_explain.setObjectName('btn-secondary')
btn_close_explain.clicked.connect(lambda: self._explain_group.setVisible(False))
gl.addWidget(btn_close_explain)
self._explain_group.setLayout(gl)
self._explain_group.setVisible(False)
vbox.addWidget(self._explain_group)
self.info_text = None
vbox.addStretch()
scroll.setWidget(widget)
# Prevent sidebar widgets from stealing keyboard focus (Space, Enter, etc.)
# so that all key presses go to the vispy canvas.
for child in scroll.findChildren(QtWidgets.QWidget):
child.setFocusPolicy(QtCore.Qt.NoFocus)
scroll.setFocusPolicy(QtCore.Qt.NoFocus)
return scroll
def _build_canvas_overlays(self):
"""Create transparent overlays on the 3D canvas for info + point details."""
canvas_widget = self.canvas.native
overlay_style = (
"background: rgba(20, 20, 20, 180); color: white; "
"font-family: Consolas, monospace; font-size: 11px; "
"padding: 8px; border-radius: 4px;"
)
# ββ Info overlay (bottom-left) ββββββββββββββββ
self._info_overlay = QtWidgets.QLabel(canvas_widget)
self._info_overlay.setStyleSheet(overlay_style)
self._info_overlay.setWordWrap(True)
self._info_overlay.setTextFormat(QtCore.Qt.PlainText)
self._info_overlay.move(10, 10)
self._info_overlay.setMaximumWidth(360)
self._info_overlay.setText("")
self._info_overlay.adjustSize()
self._info_overlay.show()
# ββ Point info overlay (bottom-center) ββββββββ
self._point_overlay = QtWidgets.QFrame(canvas_widget)
self._point_overlay.setStyleSheet(
"background: rgba(30, 30, 30, 220); border-radius: 8px; padding: 0px;"
)
point_layout = QtWidgets.QVBoxLayout(self._point_overlay)
point_layout.setContentsMargins(10, 8, 10, 8)
point_layout.setSpacing(4)
# Header row with close button
header = QtWidgets.QHBoxLayout()
self._point_header_label = QtWidgets.QLabel("")
self._point_header_label.setStyleSheet("color: #FF6B35; font-weight: bold; font-size: 12px;")
header.addWidget(self._point_header_label)
header.addStretch()
btn_x = QtWidgets.QPushButton("X")
btn_x.setFixedSize(20, 20)
btn_x.setStyleSheet(
"background: #555; color: white; border-radius: 10px; "
"font-size: 10px; padding: 0px;"
)
btn_x.clicked.connect(lambda: self._point_overlay.setVisible(False))
header.addWidget(btn_x)
point_layout.addLayout(header)
self._point_text_label = QtWidgets.QLabel("")
self._point_text_label.setStyleSheet("color: #E0E0E0; font-size: 11px;")
self._point_text_label.setWordWrap(True)
self._point_text_label.setMaximumWidth(400)
point_layout.addWidget(self._point_text_label)
self._point_overlay.adjustSize()
self._point_overlay.setVisible(False)
# ββ Hint overlay (center-bottom, orange text) βββββββ
self._hint_overlay = QtWidgets.QLabel(canvas_widget)
self._hint_overlay.setStyleSheet(
"background: rgba(15, 15, 15, 200); color: #FF8A3D; "
"font-family: Consolas, monospace; font-size: 12px; font-weight: bold; "
"padding: 10px 14px; border-radius: 6px;"
)
self._hint_overlay.setWordWrap(True)
self._hint_overlay.setTextFormat(QtCore.Qt.PlainText)
self._hint_overlay.setMaximumWidth(520)
self._hint_overlay.setText("")
self._hint_overlay.setAlignment(QtCore.Qt.AlignCenter)
self._hint_overlay.adjustSize()
self._hint_overlay.setVisible(False)
# ββ Dataset name overlay (top-center, optional) ββββ
self._name_overlay = QtWidgets.QLabel(canvas_widget)
self._name_overlay.setStyleSheet(
"background: transparent; color: rgba(255,255,255,180); "
"font-family: Consolas, monospace; font-size: 16px; font-weight: bold;"
)
self._name_overlay.setAlignment(QtCore.Qt.AlignCenter)
if self._dataset_name:
self._name_overlay.setText(self._dataset_name)
self._name_overlay.adjustSize()
self._name_overlay.setVisible(True)
else:
self._name_overlay.setVisible(False)
# ββ Explanation overlay (bottom-center, bold, larger) ββ
self._explain_overlay = QtWidgets.QLabel(canvas_widget)
self._explain_overlay.setStyleSheet(
"background: rgba(15, 15, 15, 200); color: white; "
"font-family: Consolas, monospace; font-size: 13px; font-weight: bold; "
"padding: 12px 16px; border-radius: 6px;"
)
self._explain_overlay.setWordWrap(True)
self._explain_overlay.setTextFormat(QtCore.Qt.PlainText)
self._explain_overlay.setMaximumWidth(500)
self._explain_overlay.setText("")
self._explain_overlay.adjustSize()
self._explain_overlay.setVisible(False)
self._explain_full_text = "" # backing text for the fullscreen dialog
# Click to open fullscreen viewer
self._explain_overlay.setCursor(QtCore.Qt.PointingHandCursor)
self._explain_overlay.mousePressEvent = lambda e: self._open_explain_fullscreen()
# Connect canvas resize to reposition overlays
self.canvas.events.resize.connect(self._reposition_overlays)
# Default window dimensions for font scaling reference
_DEFAULT_WINDOW_W = 1400
_DEFAULT_WINDOW_H = 800
def _reposition_overlays(self, event=None):
"""Reposition overlays on canvas resize and scale fonts proportionally.
Stylesheet updates (expensive) are only applied when the window scale
actually changes. Repositioning (cheap) runs every call.
"""
if not hasattr(self, '_info_overlay'):
return
w = self.canvas.native.width()
h = self.canvas.native.height()
# Scale fonts relative to default window size (geometric mean of both axes)
scale = max(0.7, min(1.5, (w * h) ** 0.5 / (self._DEFAULT_WINDOW_W * self._DEFAULT_WINDOW_H) ** 0.5))
# Only rebuild stylesheets when scale actually changes (setStyleSheet
# triggers expensive Qt style recalculation β must NOT run every frame).
if getattr(self, '_last_qss_scale', None) != scale:
if hasattr(self, 'window') and self.window is not None:
self.window.setStyleSheet(_build_dark_qss(scale))
if hasattr(self, '_controls_scroll'):
self._controls_scroll.setFixedWidth(
int(round(self._BASE_PANEL_WIDTH * scale))
)
info_fs = max(9, int(11 * scale))
point_hdr_fs = max(10, int(12 * scale))
point_txt_fs = max(9, int(11 * scale))
explain_fs = max(10, int(13 * scale))
self._info_overlay.setStyleSheet(
f"background: rgba(20, 20, 20, 180); color: white; "
f"font-family: Consolas, monospace; font-size: {info_fs}px; "
f"padding: 8px; border-radius: 4px;"
)
self._point_header_label.setStyleSheet(
f"color: #FF6B35; font-weight: bold; font-size: {point_hdr_fs}px;"
)
self._point_text_label.setStyleSheet(
f"color: #E0E0E0; font-size: {point_txt_fs}px;"
)
if hasattr(self, '_explain_overlay'):
self._explain_overlay.setStyleSheet(
f"background: rgba(15, 15, 15, 200); color: white; "
f"font-family: Consolas, monospace; font-size: {explain_fs}px; font-weight: bold; "
f"padding: 12px 16px; border-radius: 6px;"
)
if hasattr(self, '_hint_overlay'):
hint_fs = max(10, int(12 * scale))
self._hint_overlay.setStyleSheet(
f"background: rgba(15, 15, 15, 200); color: #FF8A3D; "
f"font-family: Consolas, monospace; font-size: {hint_fs}px; font-weight: bold; "
f"padding: 10px 14px; border-radius: 6px;"
)
if hasattr(self, '_name_overlay') and self._name_overlay.isVisible():
name_fs = max(12, int(16 * scale))
self._name_overlay.setStyleSheet(
f"background: transparent; color: rgba(255,255,255,180); "
f"font-family: Consolas, monospace; font-size: {name_fs}px; font-weight: bold;"
)
self._last_qss_scale = scale
# Info overlay: bottom-left
self._info_overlay.adjustSize()
self._info_overlay.move(10, h - self._info_overlay.height() - 10)
# Point overlay: bottom-center
self._point_overlay.adjustSize()
pw = self._point_overlay.width()
self._point_overlay.move((w - pw) // 2, h - self._point_overlay.height() - 10)
# Explain overlay: bottom-center, above point overlay
if hasattr(self, '_explain_overlay') and self._explain_overlay.isVisible():
self._explain_overlay.adjustSize()
ew = self._explain_overlay.width()
self._explain_overlay.move(
(w - ew) // 2,
h - self._explain_overlay.height() - 20
)
# Hint overlay: bottom-center, stacked above explain/point overlays
if hasattr(self, '_hint_overlay') and self._hint_overlay.isVisible():
self._hint_overlay.adjustSize()
hw = self._hint_overlay.width()
# Stack above whichever bottom overlay is tallest
y_offset = 20
if (hasattr(self, '_explain_overlay')
and self._explain_overlay.isVisible()):
y_offset = self._explain_overlay.height() + 30
elif self._point_overlay.isVisible():
y_offset = self._point_overlay.height() + 24
self._hint_overlay.move(
(w - hw) // 2,
h - self._hint_overlay.height() - y_offset
)
# Dataset name overlay: top-center
if hasattr(self, '_name_overlay') and self._name_overlay.isVisible():
self._name_overlay.adjustSize()
nw = self._name_overlay.width()
self._name_overlay.move((w - nw) // 2, 12)
def _sync_path_visibility(self):
"""Update tube/line path visibility and alpha based on current flags."""
self.vis_path_tube.visible = self.show_path and self._spline_path
self.vis_path_line.visible = self.show_path and not self._spline_path
# When flow is active alongside paths, make paths more transparent
if self.show_path and self.flow_active:
alpha = self._path_base_alpha - 0.20 # -20% when flow is visible
else:
alpha = self._path_base_alpha
self._update_path_alpha(alpha)
def _update_path_alpha(self, alpha):
"""Update alpha on whichever path visual is active."""
# For the simple line visual β update color array alpha
if hasattr(self, 'vis_path_line') and self.vis_path_line.visible:
straight_pts, straight_colors = self._compute_straight_path()
straight_colors[:, 3] = alpha
self.vis_path_line.set_data(pos=straight_pts, color=straight_colors)
# Tube doesn't support easy alpha updates (mesh), so we rebuild only
# if needed β for now, tube alpha is set at build time
def _qt_info_overlay_toggle(self, checked):
if hasattr(self, '_info_overlay'):
self._info_overlay.setVisible(checked)
def _qt_confidence(self, value, label):
self._confidence_strength = value / 100.0
if self.flow is not None:
self.flow.confidence_strength = self._confidence_strength
if value == 0:
label.setText("Confidence fade: OFF")
else:
label.setText(f"Confidence fade: {value}%")
def _qt_attractors_toggle(self, checked):
if checked:
# Cap flow opacity at 50% so attractors stand out
self._pre_attractor_opacity = self._flow_opacity
if self._flow_opacity > 0.5:
self._flow_opacity = 0.5
self._compute_and_show_attractors()
else:
# Restore previous flow opacity
if hasattr(self, '_pre_attractor_opacity'):
self._flow_opacity = self._pre_attractor_opacity
self._hide_attractors()
self._update_contextual_hint()
# Show/hide sensitivity slider
if hasattr(self, '_sensitivity_row'):
self._sensitivity_row.setVisible(checked)
def _qt_sensitivity_label_update(self, value):
"""Update label in real-time while dragging (no recompute)."""
pct = value / 10.0
self._lbl_sensitivity.setText(f"Sensitivity: {pct:.1f}%")
def _qt_sensitivity_released(self):
"""Recompute attractors when the user releases the slider."""
value = self._sl_sensitivity.value()
self._attractor_sensitivity = value / 1000.0
if not self.chk_attractors.isChecked():
return
pct = value / 10.0
self._sl_sensitivity.setEnabled(False)
self._lbl_sensitivity.setText(f"Sensitivity: {pct:.1f}% (recomputing...)")
QtCore.QTimer.singleShot(50, self._recompute_attractors_async)
def _recompute_attractors_async(self):
"""Recompute attractors with current sensitivity, then re-enable slider."""
try:
self._compute_and_show_attractors()
finally:
self._sl_sensitivity.setEnabled(True)
pct = self._attractor_sensitivity * 100
self._lbl_sensitivity.setText(f"Sensitivity: {pct:.1f}%")
def _compute_and_show_attractors(self):
"""Detect attractors and render each basin as a vibrant isosurface mesh.
Each attractor's boolean basin_mask is upsampled 2Γ for smoother
mesh quality, then gaussian-smoothed and rendered as a semi-transparent
isosurface mesh via vispy.
"""
from scipy.ndimage import gaussian_filter, zoom
if self.flow is None:
return
score_grid = self.flow._score_grid if self.flow._score_grid is not None else None
_cache = getattr(self.result, 'cache_path', None)
_sens = getattr(self, '_attractor_sensitivity', 0.7)
attractors = self.flow.find_attractors(
score_grid=score_grid, cache_path=_cache, sensitivity=_sens,
)
self._attractor_data = attractors
self._hide_attractors()
if not attractors:
print("[ATTRACTORS] No attractors found in flow field")
return
G = self.flow.grid_size
att_colors = self._get_attractor_colors()
# Upsample factor for smoother meshes (3Γ = 118Β³ from 40Β³)
up = 3
G_up = (G - 1) * up + 1
# Transform: map UPSAMPLED grid indices β world coordinates
scale = self.flow.span / (G_up - 1)
translate = self.flow.axis_min.copy()
# Build a convergence-weighted volume for more detailed meshes.
# Instead of a binary mask, use basin_score (occupancy Γ convergence)
# so the isosurface shape reflects actual flow intensity.
vg = self.flow.velocity_grid
spd = np.linalg.norm(vg, axis=-1)
dvx_dx = np.gradient(vg[:, :, :, 0], axis=0)
dvy_dy = np.gradient(vg[:, :, :, 1], axis=1)
dvz_dz = np.gradient(vg[:, :, :, 2], axis=2)
div_grid = dvx_dx + dvy_dy + dvz_dz
# Convergence: 1 where div is most negative, 0 at neutral/positive
div_ref = max(float(np.percentile(np.abs(div_grid[spd > 1e-12]), 90)), 1e-8)
conv_grid = np.clip(-div_grid / div_ref, 0.0, 1.0)
for i, att in enumerate(attractors):
basin_mask = att['basin_mask']
if np.sum(basin_mask) == 0:
continue
base_rgb = att_colors[i] if i < len(att_colors) else np.array([0.5, 0.8, 1.0])
# Build flow-shaped volume: binary mask weighted by convergence
# strength. This makes the isosurface follow flow structure
# (indentations where convergence is weaker, bulges where stronger)
# rather than being a uniform blob.
weighted = np.zeros_like(basin_mask, dtype=np.float32)
weighted[basin_mask] = 0.3 + 0.7 * conv_grid[basin_mask]
volume_hi = zoom(weighted, up, order=1)
volume = gaussian_filter(volume_hi, sigma=0.8)
try:
with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=RuntimeWarning)
iso = scene.visuals.Isosurface(
volume, level=0.45,
color=(*base_rgb.tolist(), 0.55),
shading='smooth',
parent=self._render_root,
)
iso.transform = STTransform(
scale=scale.tolist(),
translate=translate.tolist(),
)
iso.set_gl_state(
depth_test=True, cull_face=False, blend=True,
blend_func=('src_alpha', 'one_minus_src_alpha'),
)
iso.order = 1
self._attractor_clouds.append(iso)
except Exception as e:
# Fallback to point cloud if isosurface fails
print(f"[ATTRACTORS] Isosurface failed for A{i+1}, "
f"falling back to points: {e}")
indices = np.argwhere(basin_mask)
pts = np.zeros((len(indices), 3), dtype=np.float32)
for a in range(3):
pts[:, a] = (self.flow.axis_min[a]
+ indices[:, a] / (G - 1) * self.flow.span[a])
colors = np.empty((len(pts), 4), dtype=np.float32)
colors[:, :3] = base_rgb
colors[:, 3] = 0.30
cloud = ClusterPoint(point_size=6.0, parent=self._render_root)
cloud.order = 1
cloud.set_data(pts, colors)
self._attractor_clouds.append(cloud)
# Label β show relative strength (normalized so strongest = 100%)
label_text = f"A{i + 1} ({att['strength'] * 100:.0f}%)"
center = att['position'].copy()
center[2] += self.flow.span[2] * 0.06
label = scene.visuals.Text(
text=label_text, pos=center,
color=(1, 1, 1, 0.95),
font_size=self.ATTRACTOR_LABEL_SIZE,
bold=True,
parent=self._render_root,
)
label.order = 1
self._attractor_labels.append(label)
self._attractors_visible = True
print(f"[ATTRACTORS] Found {len(attractors)} attractor(s):")
for i, att in enumerate(attractors):
score_str = (f", score={att['mean_score']:.2f}"
if att['mean_score'] is not None else "")
print(f" A{i + 1}: pos=({att['position'][0]:.2f}, "
f"{att['position'][1]:.2f}, {att['position'][2]:.2f}), "
f"strength={att['strength']:.2f}, "
f"basin={att['basin_fraction']:.1%}, "
f"div={att['divergence']:.4f}{score_str}")
def _hide_attractors(self):
"""Remove attractor visuals from scene."""
for cloud in self._attractor_clouds:
cloud.parent = None
for label in self._attractor_labels:
label.parent = None
if self._attractor_highlight_border is not None:
self._attractor_highlight_border.parent = None
self._attractor_highlight_border = None
self._attractor_clouds.clear()
self._attractor_labels.clear()
self._attractors_visible = False
self._highlighted_attractor = None
def _on_attractor_click(self, event):
"""Click to highlight the nearest attractor or saved path.
Fires on mouse_release. Only acts on plain clicks (no drag/orbit)
so camera rotation is not affected. Always checks both attractors
and saved paths and picks whichever is closest in screen space so
paths inside/near attractors are still clickable.
"""
if event.button != 1:
return
# Only act on plain clicks β skip if user dragged (camera orbit)
if self._click_track_moved:
return
# Don't interfere with active gizmo drag
if self._gizmo_dragging is not None:
return
click = np.array([event.pos[0], event.pos[1]], dtype=np.float64)
# Distance to probe in screen space
probe_pos = self._probe_pos_from_sliders()
probe_sc = self._vispy_project(probe_pos)
probe_dist = float(np.linalg.norm(click - probe_sc))
# ββ Find nearest attractor ββ
att_idx = None
att_dist = float('inf')
if self._attractors_visible and self._attractor_data:
for i, att in enumerate(self._attractor_data):
sc = self._vispy_project(att['position'])
dist = float(np.linalg.norm(click - sc))
if dist < att_dist:
att_dist = dist
att_idx = i
# Cap: attractor must be within 200px
if att_dist > 200:
att_idx = None
att_dist = float('inf')
# ββ Find nearest saved path ββ
path_idx, path_dist = self._find_clicked_path(click, return_dist=True)
# ββ Probe wins only when very close (<30px) AND closer than both ββ
best_target_dist = min(att_dist, path_dist)
if probe_dist < 30 and probe_dist < best_target_dist * 0.5:
return
# ββ Nothing close β deselect everything ββ
if att_idx is None and path_idx is None:
if hasattr(self, '_explain_overlay'):
self._explain_overlay.setVisible(False)
self._set_highlighted_attractor(None)
self._highlighted_path = None
self._update_contextual_hint()
return
# ββ Pick closest: path vs attractor ββ
# Attractor meshes are large visual targets that users click "on",
# whereas path splines are thin lines. Give attractors a preference
# margin so clicking in the attractor mesh area selects the attractor
# even when a path spline passes through it. Only pick path when
# it is clearly closer than any attractor (by >30px margin).
if hasattr(self, '_explain_overlay'):
self._explain_overlay.setVisible(False)
ATTRACTOR_MARGIN = 30 # px β attractor wins unless path is 30px closer
if (path_idx is not None and att_idx is not None
and path_dist < att_dist - ATTRACTOR_MARGIN):
# Path is clearly closer β select path, deselect attractor
self._set_highlighted_attractor(None)
self._highlight_saved_path(path_idx)
elif att_idx is not None:
# Attractor wins (or tied / within margin) β select attractor
self._highlighted_path = None
self._set_highlighted_attractor(att_idx)
elif path_idx is not None:
# Only path available β select path
self._set_highlighted_attractor(None)
self._highlight_saved_path(path_idx)
def _set_highlighted_attractor(self, idx):
"""Highlight attractor with thick white contour lines around the mesh.
Extracts a wireframe from the isosurface mesh edges and renders
as thick white lines to clearly indicate selection.
"""
from scipy.ndimage import gaussian_filter, zoom
self._highlighted_attractor = idx
# Remove previous highlight visuals
if hasattr(self, '_attractor_highlight_border') and self._attractor_highlight_border is not None:
self._attractor_highlight_border.parent = None
self._attractor_highlight_border = None
if idx is not None and idx < len(self._attractor_data):
att = self._attractor_data[idx]
basin_mask = att['basin_mask']
G = self.flow.grid_size
# Build the same upsampled isosurface as in _compute_and_show_attractors
up = 3
G_up = (G - 1) * up + 1
scale = self.flow.span / (G_up - 1)
translate = self.flow.axis_min.copy()
# Convergence-weighted volume (same as main rendering)
vg = self.flow.velocity_grid
spd = np.linalg.norm(vg, axis=-1)
dvx_dx = np.gradient(vg[:, :, :, 0], axis=0)
dvy_dy = np.gradient(vg[:, :, :, 1], axis=1)
dvz_dz = np.gradient(vg[:, :, :, 2], axis=2)
div_g = dvx_dx + dvy_dy + dvz_dz
div_ref = max(float(np.percentile(
np.abs(div_g[spd > 1e-12]), 90)), 1e-8)
conv_g = np.clip(-div_g / div_ref, 0.0, 1.0)
weighted = np.zeros_like(basin_mask, dtype=np.float32)
weighted[basin_mask] = 0.3 + 0.7 * conv_g[basin_mask]
volume_hi = zoom(weighted, up, order=1)
volume = gaussian_filter(volume_hi, sigma=0.8)
# Extract mesh for the contour outline
with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=RuntimeWarning)
try:
from skimage.measure import marching_cubes
verts, faces, _, _ = marching_cubes(volume, level=0.45)
except ImportError:
from vispy.geometry.isosurface import isosurface as _vispy_iso
verts, faces = _vispy_iso(volume, level=0.45)
if len(verts) > 0 and len(faces) > 0:
# Transform vertices from grid to world coordinates
world_verts = verts * scale[np.newaxis, :] + translate[np.newaxis, :]
# Extract unique edges from faces for wireframe
edges = set()
for f in faces:
for e in [(f[0], f[1]), (f[1], f[2]), (f[2], f[0])]:
edges.add((min(e), max(e)))
# Subsample edges for performance (keep ~2000 max)
edge_list = list(edges)
if len(edge_list) > 2000:
rng = np.random.default_rng(0)
indices = rng.choice(len(edge_list), 2000, replace=False)
edge_list = [edge_list[i] for i in indices]
# Build line segments: pairs of vertices
line_pts = np.zeros((len(edge_list) * 2, 3), dtype=np.float32)
for j, (a, b) in enumerate(edge_list):
line_pts[j * 2] = world_verts[a]
line_pts[j * 2 + 1] = world_verts[b]
connect = np.zeros(len(edge_list) * 2, dtype=bool)
for j in range(len(edge_list)):
connect[j * 2] = True # connect first to second in each pair
# connect[j*2+1] = False # break between pairs (already False)
border_vis = scene.visuals.Line(
pos=line_pts,
connect=connect,
color=(1.0, 1.0, 1.0, 0.85),
width=3.0,
parent=self._render_root,
)
border_vis.order = 0
self._attractor_highlight_border = border_vis
self._show_attractor_info(idx)
# Show cached explanation if available
cached_key = str(idx)
if cached_key in self._attractor_explanations:
self._show_explain(
self._attractor_explanations[cached_key]
+ "\n\n(Cached Β· Press Shift+E to re-explain)"
)
else:
# Clear attractor info β allow probe to update again
if hasattr(self, '_info_overlay'):
self._update_info(self._probe_pos_from_sliders())
def _get_attractor_colors(self):
"""Return list of (R,G,B) arrays, one per attractor, matching the
current flow color mode palette. Colors are evenly spaced across
the active colormap so attractors are always visually distinct even
when their physical values are similar."""
from tracescope.visualization.flow_field import turbo_colormap, diverging_colormap, score_colormap
n = len(self._attractor_data)
if n == 0:
return []
color_mode = getattr(self, '_flow_color_mode', 'speed')
# Evenly spaced parameter values so each attractor gets a distinct hue
t_vals = np.linspace(0.1, 0.9, n, dtype=np.float32) if n > 1 else np.array([0.5], dtype=np.float32)
if color_mode == 'entropy':
# Diverging colormap expects [-1, 1]
t_div = t_vals * 2.0 - 1.0
rgb = diverging_colormap(t_div).astype(np.float32)
elif color_mode == 'cluster':
# Assign each attractor the color of its nearest cluster centroid
colors_list = []
centroids = self._cluster_centroids
for att in self._attractor_data:
dists = np.linalg.norm(centroids - att['position'], axis=1)
nearest_c = int(np.argmin(dists))
colors_list.append(self._cluster_colors_arr[nearest_c].copy())
# If multiple attractors map to same cluster, shift them slightly
return colors_list
elif color_mode.startswith('score:'):
# Score colormap: redβgreen, use mean_score if available
score_vals = np.array([
att['mean_score'] if att['mean_score'] is not None else 0.5
for att in self._attractor_data
], dtype=np.float32)
# If scores are too similar, spread them for visual distinction
if n > 1 and (score_vals.max() - score_vals.min()) < 0.15:
score_vals = t_vals
rgb = score_colormap(score_vals).astype(np.float32)
else:
# Default: turbo colormap (velocity-like), evenly spaced
rgb = turbo_colormap(t_vals).astype(np.float32)
return [rgb[i] for i in range(n)]
def _refresh_attractor_colors(self):
"""Recolor existing attractor isosurfaces to match the current flow color mode.
Each attractor has ONE isosurface mesh (1:1 mapping with _attractor_data).
"""
if not self._attractors_visible or not self._attractor_clouds:
return
att_colors = self._get_attractor_colors()
for ai in range(len(self._attractor_data)):
if ai >= len(self._attractor_clouds):
break
base_rgb = att_colors[ai] if ai < len(att_colors) else np.array([0.5, 0.8, 1.0])
cloud = self._attractor_clouds[ai]
# Isosurface: update color via mesh_data
try:
cloud.color = (*base_rgb.tolist(), 0.45)
except Exception:
# Fallback for ClusterPoint (if isosurface failed)
pass
# Refresh highlight border if one is active
if self._highlighted_attractor is not None:
self._set_highlighted_attractor(self._highlighted_attractor)
def _show_attractor_info(self, idx):
"""Show detailed info about the highlighted attractor."""
if not hasattr(self, '_info_overlay'):
return
att = self._attractor_data[idx]
pos = att['position']
pts = self.result.projected_3d
axis_labels = getattr(self.result.axis_info, 'labels', ['X', 'Y', 'Z'])
lines = [f"β Attractor A{idx + 1} β"]
# Location as axis percentages
for i in range(3):
mn, mx = pts[:, i].min(), pts[:, i].max()
span = mx - mn
pct = ((pos[i] - mn) / span * 100) if span > 0 else 50
lines.append(f" {axis_labels[i]}: {pct:.0f}%")
# Strength and basin
lines.append(f" Strength: {att['strength']:.0%}")
lines.append(f" Basin: {att['basin_fraction']:.0%} of field")
lines.append(f" Divergence: {att['divergence']:.4f}")
# Score
if att['mean_score'] is not None:
score = att['mean_score']
interp = "positive" if score > 0.6 else "negative" if score < 0.4 else "neutral"
lines.append(f" Score: {score:.2f} ({interp})")
# Nearest data point β only one, and only if short enough to fit
_MAX_NEAREST_LEN = 70
dists = np.linalg.norm(pts - pos, axis=1)
for ni in np.argsort(dists)[:3]:
entry = self.result.session.entries[int(ni)]
if len(entry.text) <= _MAX_NEAREST_LEN:
lines.append("")
lines.append("β Nearest Point β")
lines.append(f" {entry.text}")
break
self._info_overlay.setText("\n".join(lines))
self._info_overlay.adjustSize()
self._update_contextual_hint()
self._reposition_overlays()
def _simulate_probe_to_attractor(self, start_pos, attractor_pos, n_marks=4,
max_steps=500):
"""Simulate a probe flowing from start_pos toward attractor_pos.
Returns a list of n_marks evenly-spaced waypoints along the trajectory,
including the starting point and ending near the attractor.
"""
if self.flow is None:
return [start_pos.copy()]
DT = self.flow.DT
speed_mult = self.flow.speed_multiplier
pos = start_pos.copy().astype(np.float32)
trajectory = [pos.copy()]
for _ in range(max_steps):
vel = self.flow.sample_velocity(float(pos[0]), float(pos[1]), float(pos[2]))
pos += vel * DT * speed_mult
trajectory.append(pos.copy())
# Stop if very close to attractor
if np.linalg.norm(pos - attractor_pos) < 0.3:
break
# Sample n_marks evenly spaced waypoints
trajectory = np.array(trajectory)
total = len(trajectory)
if total <= n_marks:
return list(trajectory)
indices = np.linspace(0, total - 1, n_marks, dtype=int)
return [trajectory[i] for i in indices]
def _find_distant_start_points(self, attractor_pos, basin_mask, n=3):
"""Find n starting positions far from the attractor that will converge to it.
Strategy: pick points from the data cloud that are far away from the
attractor center and spread in different angular directions.
"""
pts = self.result.projected_3d
center = attractor_pos
dists = np.linalg.norm(pts - center, axis=1)
# Get the far-away half of points
median_dist = np.median(dists)
far_mask = dists > median_dist
far_pts = pts[far_mask]
if len(far_pts) < n:
# Fallback: use the farthest n points
far_idx = np.argsort(-dists)[:n]
return [pts[idx].copy() for idx in far_idx]
# Spread angular directions: pick points far from each other
# Start with the farthest point
selected = []
far_dists = dists[far_mask]
candidates = far_pts.copy()
cand_dists = far_dists.copy()
# First: farthest from attractor
best = np.argmax(cand_dists)
selected.append(candidates[best].copy())
for _ in range(n - 1):
# Pick the candidate that maximizes minimum distance to all selected
min_dists = np.full(len(candidates), np.inf)
for sel in selected:
d = np.linalg.norm(candidates - sel, axis=1)
min_dists = np.minimum(min_dists, d)
# Also weight by distance from attractor to avoid picking nearby points
score = min_dists * (cand_dists / cand_dists.max())
best = np.argmax(score)
selected.append(candidates[best].copy())
return selected
def _explain_highlighted_attractor(self, deep=False, force=False):
"""Generate LLM explanation for the currently highlighted attractor.
Args:
deep: If True, runs the full multi-stage process.
force: If True, ignores cached explanation and regenerates.
"""
if self._highlighted_attractor is None:
return
if self._explainer is None:
self._show_explain("No explainer configured.\nPass explainer= to launch_renderer().")
return
idx = self._highlighted_attractor
# Show cached explanation if available (unless forcing re-explain)
cached_key = str(idx)
if not force and cached_key in self._attractor_explanations:
cached = self._attractor_explanations[cached_key]
self._show_explain(cached + "\n\n(Cached Β· Press Shift+E to re-explain)")
return
att = self._attractor_data[idx]
pos = att['position']
debug = getattr(self, '_debug_prompts', False)
self._explain_pending_attractor_idx = idx
if deep:
self._show_explain(
f"Explaining attractor A{idx + 1}...\n\n"
"Stage 1/3: Simulating convergence probes..."
)
else:
self._show_explain(f"Explaining attractor A{idx + 1}...")
# Pre-compute context on main thread (fast)
pts = self.result.projected_3d
axis_labels = getattr(self.result.axis_info, 'labels', ['X', 'Y', 'Z'])
# Axis percentages
axis_pcts = []
for i in range(3):
mn, mx = pts[:, i].min(), pts[:, i].max()
span = mx - mn
pct = int((pos[i] - mn) / span * 100) if span > 0 else 50
axis_pcts.append(pct)
# Cluster distances
cluster_distances = []
centroids = self.result.cluster_centroids_3d
max_dist = self.result.max_cluster_distance
if centroids is not None and max_dist > 0:
for ci in range(len(centroids)):
d = float(np.linalg.norm(pos - centroids[ci]))
closeness = max(0, int((1.0 - d / max_dist) * 100))
label = (self.result.cluster_labels[ci]
if ci < len(self.result.cluster_labels)
else f"Cluster {ci}")
cluster_distances.append((label, closeness))
# Nearest texts
dists = np.linalg.norm(pts - pos, axis=1)
nearest_idxs = np.argsort(dists)[:5]
nearest_texts = [self.result.session.entries[ni].text for ni in nearest_idxs]
# Score context
score_info = None
active_score = getattr(self, '_flow_color_mode', 'speed')
if active_score.startswith('score:') and att['mean_score'] is not None:
ch = active_score[len('score:'):]
raw = att['mean_score']
interp = "positive" if raw > 0.6 else "negative" if raw < 0.4 else "neutral"
score_info = {"channel": ch, "mean_score_raw": raw, "interpretation": interp}
strength = att['strength']
basin_fraction = att['basin_fraction']
divergence = att['divergence']
# Deep mode: find 3 distant starting points + simulate trajectories
trajectories = []
if deep:
start_points = self._find_distant_start_points(pos, att['basin_mask'], n=3)
for sp in start_points:
waypoints = self._simulate_probe_to_attractor(sp, pos, n_marks=4)
trajectories.append(waypoints)
def _run():
try:
trajectory_explanations = None
if deep:
# Stage 2: Explain each convergence trajectory
self._explain_pending_stage = "Stage 2/3: Explaining convergence trajectories..."
trajectory_explanations = []
for ti, waypoints in enumerate(trajectories):
self._explain_pending_stage = (
f"Stage 2/3: Explaining trajectory {ti + 1}/3..."
)
# Build control points for path explain
cp_for_llm = []
for wp in waypoints:
info = probe_point(self.result, float(wp[0]), float(wp[1]), float(wp[2]))
pcts = info["axis_percentages"]
cdists = info["cluster_distances"]
cp_for_llm.append({
"axis_pcts": [int(v) for v in pcts.values()],
"cluster_distances": [(k, int(v)) for k, v in cdists.items()],
})
traj_text = self._explainer.explain_probe_multi(
axis_labels=axis_labels,
control_points=cp_for_llm,
debug=debug,
)
trajectory_explanations.append(traj_text)
if debug:
print(f"[DEBUG] Trajectory {ti+1} explanation: {traj_text}")
# Final attractor explanation (with or without trajectory context)
if deep:
self._explain_pending_stage = "Stage 3/3: Generating attractor explanation..."
text = self._explainer.explain_attractor(
axis_labels=axis_labels,
axis_pcts=axis_pcts,
cluster_distances=cluster_distances,
nearest_texts=nearest_texts,
strength=strength,
basin_fraction=basin_fraction,
divergence=divergence,
score_info=score_info,
trajectory_explanations=trajectory_explanations,
debug=debug,
)
explanation = f"Attractor A{idx + 1} Explanation:\n\n{text}"
if score_info:
explanation += (f"\n\nScore ({score_info['channel']}): "
f"{score_info['mean_score_raw']:.2f} "
f"({score_info['interpretation']})")
self._explain_pending = explanation
except Exception as e:
import traceback
traceback.print_exc()
self._explain_pending = f"Explain error: {e}"
thread = threading.Thread(target=_run, daemon=True)
thread.start()
def _qt_blob_toggle(self, checked):
if self.flow is not None:
self.flow.blob_enabled = checked
# Reinit particles when toggling blob
if self.flow._blob_opacity is not None:
self.flow.set_particle_grid(self.flow.particle_grid)
def _qt_flow_opacity(self, value, label):
self._flow_opacity = value / 100.0
label.setText(f"Opacity: {value}%")
def _qt_flow_speed(self, value, label):
speed = value / 100.0
label.setText(f"Speed: {speed:.1f}x")
if self.flow is not None:
self.flow.speed_multiplier = speed
def _qt_particle_count(self, value, label):
count = value ** 3
label.setText(f"Particles: {count:,} ({value}\u00b3)")
self._particle_grid = value
if self.flow is not None:
self.flow.set_particle_grid(value)
self._flow_rgba = np.empty((self.flow.particle_count, 4), dtype=np.float32)
def _qt_spline_path(self, checked):
self._spline_path = checked
self._sync_path_visibility()
def _qt_entropy_colors(self, checked):
if checked:
self._flow_color_mode = 'entropy'
self._chk_cluster_colors.setChecked(False)
elif self._flow_color_mode == 'entropy':
self._flow_color_mode = 'speed'
self._refresh_attractor_colors()
def _qt_cluster_colors(self, checked):
if checked:
self._flow_color_mode = 'cluster'
self._chk_entropy.setChecked(False)
for _, chk in getattr(self, '_score_color_checkboxes', []):
chk.setChecked(False)
elif self._flow_color_mode == 'cluster':
self._flow_color_mode = 'speed'
self._refresh_attractor_colors()
def _qt_score_colors(self, channel, checked):
if checked:
self._flow_color_mode = f'score:{channel}'
self._chk_entropy.setChecked(False)
self._chk_cluster_colors.setChecked(False)
for ch, chk in getattr(self, '_score_color_checkboxes', []):
if ch != channel:
chk.setChecked(False)
# Precompute score grid for fast per-frame sampling
if self.flow is not None:
self._precompute_score_grid(channel)
elif self._flow_color_mode == f'score:{channel}':
self._flow_color_mode = 'speed'
self._refresh_attractor_colors()
def _gather_scores_normalized(self, channel: str) -> np.ndarray:
"""Gather per-entry score values and normalize to [0, 1].
Raw scores can have arbitrary ranges (e.g. reliability 1β5,
error_rate 0β0.3). We use percentile-based normalization
(5thβ95th percentile) rather than min-max so that skewed
distributions with outliers still produce a meaningful color
spread instead of collapsing most points to one end of the
colormap. Binary data (only 2 unique values) falls back to
min-max so that the two values map cleanly to 0 and 1.
"""
entry_scores = self.result.get_entry_scores(channel)
path_score_map = self.result.get_path_scores(channel)
raw = np.array([
(entry_scores[i] if entry_scores[i] is not None
else path_score_map.get(self.result.session.entries[i].path_id)
if self.result.session.entries[i].path_id is not None else None)
for i in range(len(self.result.projected_3d))
], dtype=object)
# Convert to float, replacing None with NaN
vals = np.array([float(v) if v is not None else np.nan for v in raw],
dtype=np.float32)
# Normalize to [0, 1] β use percentile clipping for graded
# distributions, min-max for binary so the two values stay at
# the extremes of the colormap.
valid = ~np.isnan(vals)
if valid.any():
valid_vals = vals[valid]
unique_count = len(np.unique(valid_vals))
if unique_count <= 2:
# Binary: preserve 0/1 extremes
lo = float(valid_vals.min())
hi = float(valid_vals.max())
else:
# Graded: clip to 5-95 percentile so outliers don't
# squash the main distribution against one color
lo = float(np.percentile(valid_vals, 5))
hi = float(np.percentile(valid_vals, 95))
if hi - lo > 1e-8:
vals[valid] = np.clip(
(valid_vals - lo) / (hi - lo), 0.0, 1.0
)
else:
vals[valid] = 0.5 # all same value β neutral
vals[~valid] = 0.5 # missing β neutral
return vals
def _precompute_score_grid(self, channel: str):
"""Build the score grid on the flow system for fast particle coloring."""
data_pts = self.result.projected_3d
data_scores = self._gather_scores_normalized(channel)
self.flow.build_score_grid(data_pts, data_scores)
def _qt_score_point_coloring(self, channel, checked):
if checked:
self._point_color_mode = f'score:{channel}'
for ch, chk in getattr(self, '_score_point_checkboxes', []):
if ch != channel:
chk.setChecked(False)
self._apply_score_point_colors(channel)
else:
if getattr(self, '_point_color_mode', 'cluster') == f'score:{channel}':
self._point_color_mode = 'cluster'
self._setup_cluster_data()
def _apply_score_point_colors(self, channel):
from tracescope.visualization.flow_field import score_colormap
pts = np.ascontiguousarray(self.result.projected_3d, dtype=np.float32)
normalized = self._gather_scores_normalized(channel)
rgb = score_colormap(normalized).astype(np.float32)
colors = np.ones((len(pts), 4), dtype=np.float32)
colors[:, :3] = rgb
self.vis_clusters.set_data(pts, colors)
# βββββββββββββββββββββββββββββββββββββββββββββββββββ
# Qt signal handlers
# βββββββββββββββββββββββββββββββββββββββββββββββββββ
def _qt_flow_toggle(self, checked):
self.flow_active = checked
self.vis_flow.visible = checked
self._sync_path_visibility() # update path transparency
def _qt_ball_toggle(self, checked):
self.ball_flowing = checked
if self.flow is not None:
if checked:
self._auto_mark_start_point()
self.flow.start_ball_flow()
self._show_gizmo(False)
else:
self.flow.stop_ball_flow()
self.vis_ball.visible = False
self.vis_trail.visible = False
if hasattr(self, '_gizmo_hint'):
self._gizmo_hint.setVisible(not checked)
self._update_contextual_hint()
def _auto_mark_start_point(self):
"""Add the current probe position as a control point (skip if duplicate)."""
pos = self._probe_pos_from_sliders()
if self._control_points:
last = self._control_points[-1]
if float(np.linalg.norm(pos - last)) < 1e-4:
return
self._control_points.append(pos.copy())
self._refresh_control_points()
def _qt_path_toggle(self, checked):
self.show_path = checked
self._sync_path_visibility()
def _qt_points_toggle(self, checked):
self.show_points = checked
self.vis_clusters.visible = checked
def _qt_slider_changed(self, axis, value):
if self._updating_sliders:
return
labels = getattr(self.result.axis_info, 'labels', ['X', 'Y', 'Z'])
self._slider_labels[axis].setText(f"{labels[axis]}: {value / 10:.0f}%")
pos = self._probe_pos_from_sliders()
self.vis_probe.set_data(pos.reshape(1, 3), self.PROBE_COLOR)
if self._gizmo_visible:
self._update_gizmo(pos)
if self.flow is not None and (not self.ball_flowing or self._manual_slider_override):
self.flow.set_ball_position(*pos)
self._update_info(pos)
def _qt_mark(self):
pos = self._probe_pos_from_sliders()
self._control_points.append(pos.copy())
self._refresh_control_points()
def _qt_clear(self):
self._control_points.clear()
self._refresh_control_points()
def _qt_explain(self):
if self._explainer is None:
self._show_explain("No explainer configured.\nPass explainer= to launch_renderer().")
return
# Stop ball flow so probe stays put during explanation
if self.ball_flowing:
self.ball_flowing = False
if self.flow is not None:
self.flow.stop_ball_flow()
self.vis_ball.visible = False
self.vis_trail.visible = False
if hasattr(self, 'chk_ball'):
self.chk_ball.setChecked(False)
if hasattr(self, '_gizmo_hint'):
self._gizmo_hint.setVisible(True)
pos = self._probe_pos_from_sliders()
px, py, pz = float(pos[0]), float(pos[1]), float(pos[2])
control_points = list(self._control_points) # snapshot
# Include current probe position if no marks yet
if not control_points:
control_points = [pos.copy()]
self._show_explain("Generating explanation...")
self._explain_pending = None # will be set by background thread
self._explain_pending_path_points = [p.copy() for p in control_points]
def _run():
import logging
logger = logging.getLogger(__name__)
try:
if len(control_points) > 1:
text = self._build_multi_explain()
else:
info = probe_with_explanation(
self.result, self._explainer, px, py, pz)
lines = [f"LLM Explanation:\n{info['explanation']}\n"]
lines.append("Nearest messages:")
for item in info["nearest_texts"][:3]:
lines.append(f" [{item['role']}] {item['text'][:100]}...")
text = "\n".join(lines)
logger.info("Explain completed successfully")
self._explain_pending = text
except Exception as e:
logger.error(f"Explain failed: {e}", exc_info=True)
self._explain_pending = f"Explain error: {e}"
self._explain_pending_path_points = None # don't save on error
thread = threading.Thread(target=_run, daemon=True)
thread.start()
def _show_hint(self, text: str):
"""Show a centered orange hint on the canvas. Hide if text is empty."""
if not hasattr(self, '_hint_overlay'):
return
if not text:
self._hint_overlay.setVisible(False)
return
self._hint_overlay.setText(text)
self._hint_overlay.adjustSize()
self._hint_overlay.setVisible(True)
self._reposition_overlays()
def _update_contextual_hint(self):
"""Pick the right hint based on current probe / flow / attractor state."""
if not hasattr(self, '_hint_overlay'):
return
# Highlighted saved path
if self._highlighted_path is not None:
self._show_hint(
"Press Delete to remove this path Β· "
"Click elsewhere to deselect"
)
return
# Attractor highlighted takes priority
if self._attractors_visible and self._highlighted_attractor is not None:
idx = self._highlighted_attractor
cached = str(idx) in self._attractor_explanations
if cached:
self._show_hint(
"Press E to show explanation Β· "
"Shift+E to re-explain"
)
else:
self._show_hint(
"Press E to explain Β· "
"Shift+E for deeper explanation (slower)"
)
return
# Probe being dragged by flow
if self.ball_flowing and self._has_flow:
if getattr(self, '_ball_is_stuck', False):
self._show_hint("Click on the probe to move it")
else:
self._show_hint("Press M to mark point Β· Press E to explain path")
return
# Gizmo visible β how to drive it + how to resume
if self._gizmo_visible:
self._show_hint(
"Drag the arrows to move the probe Β· "
"Press Space or enable 'Drag probe by the flow' to resume"
)
return
# Ball flow off, gizmo not visible β how to start editing
if self._has_flow and not self.ball_flowing:
self._show_hint(
"Click on the probe to move it Β· "
"Press Space or enable 'Drag probe by the flow' to resume"
)
return
self._hint_overlay.setVisible(False)
def _show_explain(self, text: str):
"""Show text in both the sidebar panel and the canvas overlay."""
if hasattr(self, '_explain_text'):
self._explain_text.setPlainText(text)
self._explain_group.setVisible(True)
# Also show on canvas overlay (bold, bottom-center)
if hasattr(self, '_explain_overlay'):
self._explain_full_text = text
# Truncate for overlay display (max ~300 chars)
display = text
if len(display) > 300:
display = display[:300] + "β¦ (click to expand)"
self._explain_overlay.setText(display)
self._explain_overlay.adjustSize()
self._explain_overlay.setVisible(True)
self._reposition_overlays()
def _open_explain_fullscreen(self):
"""Open the full explanation in a large modal dialog with scroll."""
if not hasattr(self, '_explain_full_text') or not self._explain_full_text:
self._explain_overlay.setVisible(False)
return
dlg = QtWidgets.QDialog(self.window)
dlg.setWindowTitle("Explanation")
# Size ~80% of screen so the dialog is large regardless of window size
screen_geo = None
try:
screen_geo = QtWidgets.QApplication.primaryScreen().availableGeometry()
except Exception:
pass
if screen_geo is not None:
dlg_w = int(screen_geo.width() * 0.65)
dlg_h = int(screen_geo.height() * 0.65)
elif self.window is not None:
ws = self.window.size()
dlg_w = int(ws.width() * 0.7)
dlg_h = int(ws.height() * 0.7)
else:
dlg_w, dlg_h = 900, 650
dlg.resize(dlg_w, dlg_h)
# Scale font with dialog height so text stays readable at any size
base_font_px = max(20, min(28, int(dlg_h * 0.038)))
btn_font_px = max(16, base_font_px - 2)
dlg.setStyleSheet(
"QDialog { background: #1E1E1E; } "
"QLabel { background: transparent; color: #E0E0E0; "
f"font-family: Consolas, monospace; font-size: {base_font_px}px; "
"line-height: 1.5; } "
"QScrollArea { background: #1E1E1E; border: none; } "
"QPushButton { background: #FF6B35; color: white; border: none; "
f"border-radius: 4px; padding: 10px 22px; font-size: {btn_font_px}px; "
"} QPushButton:hover { background: #FF8555; }"
)
lay = QtWidgets.QVBoxLayout(dlg)
lay.setContentsMargins(0, 0, 0, 0)
lay.setSpacing(0)
# Scrollable text area
scroll = QtWidgets.QScrollArea()
scroll.setWidgetResizable(True)
scroll.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
text_label = QtWidgets.QLabel(self._explain_full_text)
text_label.setWordWrap(True)
text_label.setContentsMargins(28, 24, 28, 24)
text_label.setTextInteractionFlags(
QtCore.Qt.TextSelectableByMouse | QtCore.Qt.TextSelectableByKeyboard
)
scroll.setWidget(text_label)
lay.addWidget(scroll, stretch=1)
btn_row = QtWidgets.QHBoxLayout()
btn_row.setContentsMargins(12, 8, 12, 12)
btn_row.addStretch()
btn_close = QtWidgets.QPushButton("Close")
btn_close.clicked.connect(dlg.accept)
btn_row.addWidget(btn_close)
lay.addLayout(btn_row)
dlg.exec_()
def _build_multi_explain(self) -> str:
"""Build multi-point path explanation via LLM."""
all_pcts = []
all_dists = []
axis_labels = None
for cp in self._control_points:
info = probe_point(self.result, cp[0], cp[1], cp[2])
pcts = info["axis_percentages"]
dists = info["cluster_distances"]
if axis_labels is None:
axis_labels = list(pcts.keys())
all_pcts.append(list(pcts.values()))
all_dists.append(dists)
control_points_for_llm = []
for pcts_vals, dists_dict in zip(all_pcts, all_dists):
control_points_for_llm.append({
"axis_pcts": [int(v) for v in pcts_vals],
"cluster_distances": [(k, int(v)) for k, v in dists_dict.items()],
})
# Gather score context at each control point (if scores exist)
score_context = None
channels = self.result.score_channels
if channels:
score_context = []
pts = self.result.projected_3d
for cp in self._control_points:
probe = np.array([cp[0], cp[1], cp[2]])
dists_to_data = np.linalg.norm(pts - probe, axis=1)
# Weighted average of nearby scores (5 nearest points)
nearest_5 = np.argsort(dists_to_data)[:5]
cp_scores = {}
for ch in channels:
vals = []
for idx in nearest_5:
entry = self.result.session.entries[idx]
s = entry.scores.get(ch)
if s is None and entry.path_id is not None:
s = self.result.session.path_scores.get(
entry.path_id, {}).get(ch)
if s is not None:
vals.append(s)
if vals:
cp_scores[ch] = round(sum(vals) / len(vals), 2)
score_context.append(cp_scores)
explanation = self._explainer.explain_probe_multi(
axis_labels=axis_labels,
control_points=control_points_for_llm,
score_context=score_context,
)
return explanation
# βββββββββββββββββββββββββββββββββββββββββββββββββββ
# Persistent explained paths & attractor explanations
# βββββββββββββββββββββββββββββββββββββββββββββββββββ
def _explanations_cache_path(self):
"""Return base path for explanation cache files, or None."""
cp = getattr(self.result, 'cache_path', None)
return cp
def _load_saved_paths(self):
"""Load saved explained paths from cache."""
import json
cp = self._explanations_cache_path()
if cp is None:
return
path = f"{cp}_explained_paths.json"
try:
with open(path, 'r', encoding='utf-8') as f:
self._saved_paths = json.load(f)
# Convert control_points back to numpy arrays
for sp in self._saved_paths:
sp['control_points'] = [np.array(p, dtype=np.float32)
for p in sp['control_points']]
except (FileNotFoundError, json.JSONDecodeError):
self._saved_paths = []
def _save_paths_to_cache(self):
"""Persist saved paths to JSON cache."""
import json
cp = self._explanations_cache_path()
if cp is None:
return
path = f"{cp}_explained_paths.json"
data = []
for sp in self._saved_paths:
data.append({
'control_points': [p.tolist() for p in sp['control_points']],
'explanation': sp['explanation'],
})
with open(path, 'w', encoding='utf-8') as f:
json.dump(data, f, indent=2)
def _load_attractor_explanations(self):
"""Load cached attractor explanations."""
import json
cp = self._explanations_cache_path()
if cp is None:
return
path = f"{cp}_attractor_explanations.json"
try:
with open(path, 'r', encoding='utf-8') as f:
self._attractor_explanations = json.load(f)
except (FileNotFoundError, json.JSONDecodeError):
self._attractor_explanations = {}
def _save_attractor_explanations_to_cache(self):
"""Persist attractor explanations to JSON cache."""
import json
cp = self._explanations_cache_path()
if cp is None:
return
path = f"{cp}_attractor_explanations.json"
with open(path, 'w', encoding='utf-8') as f:
json.dump(self._attractor_explanations, f, indent=2)
def _add_saved_path(self, control_points, explanation):
"""Save an explained path and render it."""
self._saved_paths.append({
'control_points': [p.copy() for p in control_points],
'explanation': explanation,
})
self._save_paths_to_cache()
self._render_saved_path(len(self._saved_paths) - 1)
self._update_paths_button()
def _render_saved_path(self, idx):
"""Create visuals for one saved path."""
sp = self._saved_paths[idx]
pts = np.array(sp['control_points'], dtype=np.float32)
# Cycle through distinct colors for saved paths
PATH_COLORS = [
(0.2, 0.8, 1.0, 0.9), # cyan
(0.2, 1.0, 0.4, 0.9), # green
(1.0, 0.6, 0.2, 0.9), # orange
(0.8, 0.4, 1.0, 0.9), # purple
(1.0, 1.0, 0.2, 0.9), # yellow
(1.0, 0.4, 0.6, 0.9), # pink
]
color = PATH_COLORS[idx % len(PATH_COLORS)]
# Markers at control points
markers = ClusterPoint(point_size=8.0, parent=self._render_root)
markers.order = 2
marker_colors = np.tile(np.array(color, dtype=np.float32), (len(pts), 1))
markers.set_data(pts, marker_colors)
markers.visible = self._saved_paths_visible
# Spline line
line_vis = None
if len(pts) >= 2:
spline = catmull_rom_spline(pts, 10).astype(np.float32)
line_vis = scene.visuals.Line(
pos=spline, color=color, width=3,
parent=self._render_root, antialias=True,
)
line_vis.order = 2
line_vis.visible = self._saved_paths_visible
# Path label at midpoint
mid_idx = len(pts) // 2
mid = pts[mid_idx].copy()
mid[2] += self.flow.span[2] * 0.04 if self.flow else 0.2
label = scene.visuals.Text(
text=f"P{idx + 1}", pos=mid,
color=color[:3] + (0.95,),
font_size=self.ATTRACTOR_LABEL_SIZE * 0.8,
bold=True, parent=self._render_root,
)
label.order = 2
label.visible = self._saved_paths_visible
self._saved_path_visuals.append({
'markers': markers,
'line': line_vis,
'label': label,
'color': color,
})
def _render_all_saved_paths(self):
"""Render all loaded saved paths."""
self._clear_saved_path_visuals()
for i in range(len(self._saved_paths)):
self._render_saved_path(i)
self._update_paths_button()
def _clear_saved_path_visuals(self):
"""Remove all saved path visuals from scene."""
for vis in self._saved_path_visuals:
vis['markers'].parent = None
if vis['line']:
vis['line'].parent = None
vis['label'].parent = None
self._saved_path_visuals.clear()
self._highlighted_path = None
def _toggle_saved_paths_visibility(self):
"""Show/hide all saved path visuals."""
self._saved_paths_visible = not self._saved_paths_visible
for vis in self._saved_path_visuals:
vis['markers'].visible = self._saved_paths_visible
if vis['line']:
vis['line'].visible = self._saved_paths_visible
vis['label'].visible = self._saved_paths_visible
self._update_paths_button()
def _update_paths_button(self):
"""Update the paths toggle button text."""
if not hasattr(self, '_btn_toggle_paths'):
return
n = len(self._saved_paths)
if n == 0:
self._btn_toggle_paths.setVisible(False)
return
self._btn_toggle_paths.setVisible(True)
vis = "Hide" if self._saved_paths_visible else "Show"
self._btn_toggle_paths.setText(f"{vis} {n} Saved Path(s)")
def _highlight_saved_path(self, idx):
"""Highlight a saved path and show its explanation."""
# Unhighlight previous
if self._highlighted_path is not None and self._highlighted_path < len(self._saved_path_visuals):
vis = self._saved_path_visuals[self._highlighted_path]
vis['markers'].set_gl_state(depth_test=True)
if vis['line']:
vis['line'].set_gl_state(depth_test=True)
self._highlighted_path = idx
if idx is not None and idx < len(self._saved_path_visuals):
vis = self._saved_path_visuals[idx]
# Make markers bigger for highlight
sp = self._saved_paths[idx]
pts = np.array(sp['control_points'], dtype=np.float32)
white = np.ones((len(pts), 4), dtype=np.float32)
vis['markers'].set_data(pts, white)
# Show explanation
self._show_explain(f"Path P{idx + 1} Explanation:\n\n{sp['explanation']}")
self._update_contextual_hint()
# Reposition so hint stacks above the explain overlay
self._reposition_overlays()
else:
self._highlighted_path = None
def _delete_highlighted_path(self):
"""Delete the currently highlighted saved path."""
if self._highlighted_path is None:
return
idx = self._highlighted_path
# Remove visuals
if idx < len(self._saved_path_visuals):
vis = self._saved_path_visuals[idx]
vis['markers'].parent = None
if vis['line']:
vis['line'].parent = None
vis['label'].parent = None
# Remove from lists
if idx < len(self._saved_paths):
self._saved_paths.pop(idx)
if idx < len(self._saved_path_visuals):
self._saved_path_visuals.pop(idx)
self._highlighted_path = None
self._save_paths_to_cache()
# Re-render all paths (to fix indices/labels)
self._render_all_saved_paths()
# Hide explanation
if hasattr(self, '_explain_overlay'):
self._explain_overlay.setVisible(False)
self._show_hint("")
def _find_clicked_path(self, click_pos, return_dist=False):
"""Find which saved path is nearest to a screen click position.
Returns path index or None. When *return_dist* is True, returns
(index, distance) tuple β (None, inf) when nothing found."""
if not self._saved_paths or not self._saved_paths_visible:
return (None, float('inf')) if return_dist else None
best_idx = None
best_dist = 60 # max 60px click distance
try:
for i, sp in enumerate(self._saved_paths):
pts = np.array(sp['control_points'], dtype=np.float32)
if len(pts) >= 2:
spline = catmull_rom_spline(pts, 20).astype(np.float32)
else:
spline = pts
# Project to screen (apply scene scale like _vispy_project)
s = self._scene_scale
scaled = spline * np.array([s, s, s], dtype=np.float32)
pts_h = np.hstack([scaled, np.ones((len(scaled), 1))]).astype(np.float32)
mapped = self.view.scene.transform.map(pts_h)
w_vals = mapped[:, 3:4]
w_vals = np.where(np.abs(w_vals) < 1e-8, 1.0, w_vals)
screen = mapped[:, :2] / w_vals
# Point-to-segment distance along the spline
min_d = float('inf')
for j in range(len(screen) - 1):
d = self._point_to_segment_dist_2d(
click_pos, screen[j], screen[j + 1])
if d < min_d:
min_d = d
if min_d < best_dist:
best_dist = min_d
best_idx = i
except Exception:
pass
if return_dist:
return (best_idx, best_dist if best_idx is not None else float('inf'))
return best_idx
# βββββββββββββββββββββββββββββββββββββββββββββββββββ
# Helpers
# βββββββββββββββββββββββββββββββββββββββββββββββββββ
def _slider_to_raw(self, val, axis):
pts = self.result.projected_3d
mn, mx = pts[:, axis].min(), pts[:, axis].max()
return mn + (val / 1000.0) * (mx - mn)
def _raw_to_slider(self, raw, axis):
pts = self.result.projected_3d
mn, mx = pts[:, axis].min(), pts[:, axis].max()
span = mx - mn
if span == 0:
return 500
return int(np.clip((raw - mn) / span * 1000, 0, 1000))
def _probe_pos_from_sliders(self):
return np.array([
self._slider_to_raw(self._sliders[i].value(), i) for i in range(3)
], dtype=np.float32)
def _sync_sliders_to_ball(self, bp):
"""Push ball position into sliders without triggering callbacks."""
self._updating_sliders = True
labels = getattr(self.result.axis_info, 'labels', ['X', 'Y', 'Z'])
for i in range(3):
sv = self._raw_to_slider(bp[i], i)
self._sliders[i].setValue(sv)
self._slider_labels[i].setText(f"{labels[i]}: {sv / 10:.0f}%")
self._updating_sliders = False
def _update_info(self, pos):
if not hasattr(self, '_info_overlay'):
return
# Don't overwrite attractor info β it has priority
if self._highlighted_attractor is not None:
return
try:
info = probe_point(self.result, float(pos[0]), float(pos[1]), float(pos[2]))
lines = []
# Warn if probe is outside the semantic blob
if self.flow is not None and self.flow.is_outside_blob(
float(pos[0]), float(pos[1]), float(pos[2])):
lines.append("!! OUTSIDE SEMANTIC REGION !!")
lines.append("(sparse data β flow may be unreliable)")
lines.append("")
lines.append("β Probe Location β")
for name, pct in info['axis_percentages'].items():
lines.append(f" {name}: {pct:.1f}%")
lines.append("")
lines.append("β Distance to Clusters β")
for name, d in info['cluster_distances'].items():
lines.append(f" {name}: {d:.1f}%")
self._info_overlay.setText("\n".join(lines))
self._info_overlay.adjustSize()
self._reposition_overlays()
except Exception:
pass
def _refresh_control_points(self):
if not self._control_points:
self.vis_markers.visible = False
self.vis_marked_path.visible = False
return
pts = np.array(self._control_points, dtype=np.float32)
white = np.ones((len(pts), 4), dtype=np.float32)
self.vis_markers.set_data(pts, white)
self.vis_markers.visible = True
if len(pts) >= 2:
spline = catmull_rom_spline(pts, 10).astype(np.float32)
self.vis_marked_path.set_data(pos=spline, color=(1.0, 0.5, 0.2, 0.8))
self.vis_marked_path.visible = True
else:
self.vis_marked_path.visible = False
# βββββββββββββββββββββββββββββββββββββββββββββββββββ
# Animation loop
# βββββββββββββββββββββββββββββββββββββββββββββββββββ
def _on_timer(self, event):
now = event.elapsed
dt = now - self._last_time if self._last_time is not None else 1.0 / 60
self._last_time = now
self._frame_count += 1
# Compensate for variable frame rate: if frames arrive slower than
# 60 Hz, scale the physics step proportionally so animation speed
# stays consistent regardless of window size or GPU/DWM throttling.
dt_scale = min(4.0, dt * 60.0) # cap at 4x to avoid huge jumps
# Check for async explain stage update (intermediate progress)
stage = getattr(self, '_explain_pending_stage', None)
if stage is not None:
self._explain_pending_stage = None
self._show_explain(f"Explaining attractor...\n\n{stage}")
# Check for async explain result (final)
pending = getattr(self, '_explain_pending', None)
if pending is not None:
self._explain_pending = None
self._explain_pending_stage = None
self._show_explain(pending)
# Save explained path if this was a path explanation (not error)
path_pts = getattr(self, '_explain_pending_path_points', None)
if path_pts is not None and not pending.startswith("Explain error:"):
self._explain_pending_path_points = None
self._add_saved_path(path_pts, pending)
# Clear control points so next marking starts fresh
self._control_points.clear()
self._refresh_control_points()
# Save attractor explanation if this was an attractor explain
att_idx = getattr(self, '_explain_pending_attractor_idx', None)
if att_idx is not None and not pending.startswith("Explain error:"):
self._explain_pending_attractor_idx = None
self._attractor_explanations[str(att_idx)] = pending
self._save_attractor_explanations_to_cache()
# --- Auto-rotation disabled (kept for future use) ---
# if self.auto_rotate:
# cam = self.view.camera
# cam.azimuth = (cam.azimuth or 0) + self.AUTO_ROTATE_SPEED * dt
# Flow step (uses pre-allocated buffer for performance)
if self.flow_active and self.flow is not None:
pos, colors, alphas, speeds = self.flow.step(dt_scale=dt_scale)
# Color mode selection
color_mode = getattr(self, '_flow_color_mode', 'speed')
if color_mode == 'entropy':
from tracescope.visualization.flow_field import diverging_colormap
max_speed = speeds.max() if speeds.max() > 0 else 1.0
t = (speeds / max_speed) * 2.0 - 1.0
colors = diverging_colormap(t).astype(np.float32)
elif color_mode == 'cluster':
# Color by inverse-distance-weighted blend of cluster colors.
# Uses squared distances (avoids sqrt) and einsum to
# reduce allocations on the hot path.
centroids = self._cluster_centroids # (K, 3)
diff = pos[:, None, :] - centroids[None, :, :] # (N, K, 3)
dist_sq = np.einsum('nkd,nkd->nk', diff, diff) # (N, K)
eps = 1e-12
inv_dists = 1.0 / (dist_sq + eps) ** 1.5 # power 3 on dist
weights = inv_dists / inv_dists.sum(axis=1, keepdims=True)
colors = (weights @ self._cluster_colors_arr).astype(np.float32)
elif color_mode.startswith('score:'):
# Color flow particles by precomputed score grid (fast)
from tracescope.visualization.flow_field import score_colormap
particle_scores = self.flow.sample_score_batch(pos)
colors = score_colormap(particle_scores).astype(np.float32)
rgba = self._flow_rgba
if rgba is None or len(rgba) != len(pos):
rgba = np.empty((len(pos), 4), dtype=np.float32)
self._flow_rgba = rgba
rgba[:, :3] = colors
rgba[:, 3] = alphas
opacity = getattr(self, '_flow_opacity', 1.0)
if opacity < 1.0:
rgba[:, 3] *= opacity
# Confidence fading: slider controls how much the density
# confidence affects particle opacity.
# fade = (1 - slider) + slider * conf^exponent
# At slider=0%: fade=1 (no change).
# At slider=1%: fade β 0.99 + 0.01*conf (barely visible).
# At slider=100%: fade = conf^exponent (full effect).
# The power curve (exponent>1) makes high-conf particles stay
# bright while low-conf ones drop faster.
conf_str = getattr(self, '_confidence_strength', 0.0)
if conf_str > 0 and self.flow is not None and self.flow._confidence_grid is not None:
conf = self.flow.sample_confidence_batch(pos)
exponent = 3.0 # nonlinear: conf=0.8β0.51, conf=0.2β0.008
conf_powered = np.power(conf, exponent)
fade = (1.0 - conf_str) + conf_str * conf_powered
rgba[:, 3] *= fade
# Skip rendering particles with near-zero alpha
visible = rgba[:, 3] > 0.01
if not np.all(visible):
pos = pos[visible]
rgba = rgba[visible]
self.vis_flow.set_data(pos, rgba)
# Ball flow
if self.ball_flowing and self.flow is not None:
prev_bp = self._last_ball_pos
bp = self.flow.advance_ball(dt_scale=dt_scale)
# Track whether the ball is actually moving (or stuck in a zero-flow region)
if prev_bp is not None:
step = float(np.linalg.norm(bp - prev_bp))
if step < 1e-4:
self._ball_stuck_frames += 1
self._ball_motion_frames = 0
else:
self._ball_stuck_frames = 0
self._ball_motion_frames = getattr(self, '_ball_motion_frames', 0) + 1
self._last_ball_pos = bp.copy()
# Refresh contextual hint on stuck/unstuck transitions (~every 30 frames)
if self._frame_count % 30 == 0:
stuck_frames = self._ball_stuck_frames
motion_frames = getattr(self, '_ball_motion_frames', 0)
if stuck_frames >= 30:
new_stuck = True
elif motion_frames >= 60:
new_stuck = False
else:
new_stuck = self._ball_is_stuck # keep current (initial state)
if new_stuck != self._ball_is_stuck:
self._ball_is_stuck = new_stuck
self._update_contextual_hint()
self.vis_ball.transform = scene.transforms.STTransform(translate=bp.tolist())
self.vis_ball.visible = True
trail = self.flow.ball_trail
if len(trail) >= 2:
self.vis_trail.set_data(
pos=np.array(trail, dtype=np.float32),
color=self.TRAIL_COLOR)
self.vis_trail.visible = True
# Update probe to follow ball
self.vis_probe.set_data(bp.reshape(1, 3).astype(np.float32), self.PROBE_COLOR)
if self._gizmo_visible:
self._update_gizmo(bp.astype(np.float32))
# Sync sliders + info (throttled to every 6 frames, skip when user is dragging)
if QtWidgets is not None and self._frame_count % 6 == 0 and not self._manual_slider_override:
self._sync_sliders_to_ball(bp)
self._update_info(bp)
self.canvas.update()
# βββββββββββββββββββββββββββββββββββββββββββββββββββ
# Keyboard (fallback when canvas has focus)
# βββββββββββββββββββββββββββββββββββββββββββββββββββ
def _on_key(self, event):
key = event.key
if key == 'Space' and self._has_flow and self.flow:
# Space toggles "drag probe by the flow" (ball_flowing)
self.ball_flowing = not self.ball_flowing
if self.ball_flowing:
self._auto_mark_start_point()
self.flow.start_ball_flow()
self._show_gizmo(False)
else:
self.flow.stop_ball_flow()
self.vis_ball.visible = False
self.vis_trail.visible = False
if QtWidgets and hasattr(self, 'chk_ball'):
self.chk_ball.blockSignals(True)
self.chk_ball.setChecked(self.ball_flowing)
self.chk_ball.blockSignals(False)
if hasattr(self, '_gizmo_hint'):
self._gizmo_hint.setVisible(not self.ball_flowing)
self._update_contextual_hint()
elif key == 'B' and self._has_flow and self.flow:
self.ball_flowing = not self.ball_flowing
if self.ball_flowing:
self._auto_mark_start_point()
self.flow.start_ball_flow()
self._show_gizmo(False)
else:
self.flow.stop_ball_flow()
self.vis_ball.visible = False
self.vis_trail.visible = False
if QtWidgets and hasattr(self, 'chk_ball'):
self.chk_ball.blockSignals(True)
self.chk_ball.setChecked(self.ball_flowing)
self.chk_ball.blockSignals(False)
if hasattr(self, '_gizmo_hint'):
self._gizmo_hint.setVisible(not self.ball_flowing)
self._update_contextual_hint()
elif key == 'M':
# Mark current probe position
self._qt_mark()
elif key == 'F' and self._has_flow:
# F toggles the flow particles (moved from Space)
self.flow_active = not self.flow_active
self.vis_flow.visible = self.flow_active
if QtWidgets and hasattr(self, 'chk_flow'):
self.chk_flow.setChecked(self.flow_active)
elif key == 'P':
self.show_points = not self.show_points
self.vis_clusters.visible = self.show_points
if QtWidgets and hasattr(self, 'chk_points'):
self.chk_points.setChecked(self.show_points)
elif key == 'L':
self.show_path = not self.show_path
self._sync_path_visibility()
if QtWidgets and hasattr(self, 'chk_path'):
self.chk_path.setChecked(self.show_path)
# --- Auto-rotate key disabled (kept for future use) ---
# elif key == 'A':
# self.auto_rotate = not self.auto_rotate
# if QtWidgets and hasattr(self, 'chk_rotate'):
# self.chk_rotate.setChecked(self.auto_rotate)
elif key == 'R':
self.view.camera.reset()
self.view.camera.azimuth = 0
self.view.camera.elevation = 20
self.view.camera.distance = 5.0
self.view.camera.center = tuple(self._data_center)
elif key in ('+', '='):
self.base_size *= 1.2
self.vis_flow.set_base_size(self.base_size)
elif key == '-':
self.base_size /= 1.2
self.vis_flow.set_base_size(self.base_size)
elif key == 'E':
# Context-sensitive: attractor explain takes priority, else path explain
if self._attractors_visible and self._highlighted_attractor is not None:
shift = 'Shift' in event.modifiers if hasattr(event, 'modifiers') else False
idx = self._highlighted_attractor
cached_key = str(idx)
has_cached = cached_key in self._attractor_explanations
if shift and has_cached:
# Shift+E with cached = force re-explain (deep)
self._explain_highlighted_attractor(deep=True, force=True)
elif shift:
self._explain_highlighted_attractor(deep=True)
else:
self._explain_highlighted_attractor(deep=False)
else:
self._qt_explain()
elif key == 'Delete':
# Delete highlighted saved path
if self._highlighted_path is not None:
self._delete_highlighted_path()
elif key == 'D':
# Toggle debug blob visualization
self._debug_blob = not self._debug_blob
self.vis_blob_debug.visible = self._debug_blob
elif key == 'Escape':
self.close()
def _on_double_click(self, event):
"""Double-click on 3D canvas to jump sliders to nearest data point."""
if event.button != 1: # left button only
return
# Project data points to screen using vispy's own transform
try:
pts = self.result.projected_3d
pts_h = np.hstack([pts, np.ones((len(pts), 1))]).astype(np.float32)
mapped = self.view.scene.transform.map(pts_h)
w_vals = mapped[:, 3:4]
w_vals = np.where(np.abs(w_vals) < 1e-8, 1.0, w_vals)
screen = mapped[:, :2] / w_vals # perspective divide β canvas px
click_pos = np.array([event.pos[0], event.pos[1]], dtype=np.float64)
dists = np.linalg.norm(screen - click_pos, axis=1)
nearest_idx = int(np.argmin(dists))
# Only snap if click is within 50 canvas pixels
if dists[nearest_idx] > 50:
return
# Jump sliders to that point's 3D position
target = pts[nearest_idx].astype(np.float32)
self._updating_sliders = True
labels = getattr(self.result.axis_info, 'labels', ['X', 'Y', 'Z'])
for i in range(3):
sv = self._raw_to_slider(float(target[i]), i)
self._sliders[i].setValue(sv)
self._slider_labels[i].setText(f"{labels[i]}: {sv / 10:.0f}%")
self._updating_sliders = False
# Update probe + info
self.vis_probe.set_data(target.reshape(1, 3), self.PROBE_COLOR)
if self._gizmo_visible:
self._update_gizmo(target)
if not self.ball_flowing and self.flow is not None:
self.flow.set_ball_position(*target)
self._update_info(target)
# Show point info overlay
self._show_point_info(nearest_idx)
except Exception:
pass
# βββββββββββββββββββββββββββββββββββββββββββββββββββ
# Gizmo β Blender-style axis drag for probe
# βββββββββββββββββββββββββββββββββββββββββββββββββββ
def _update_gizmo(self, center):
"""Reposition the 3 axis arrows with arrowheads around *center*."""
c = np.asarray(center, dtype=np.float32)
head_frac = 0.075 # arrowhead length as fraction of shaft
head_spread = 0.105 # arrowhead width as fraction of shaft length
for i in range(3):
tip = c.copy()
tip[i] += self._gizmo_arrow_len
self._gizmo_lines[i].set_data(
pos=np.array([c, tip], dtype=np.float32),
color=self._gizmo_colors[i],
)
# Arrowhead: two barbs from tip pointing back along shaft + sideways
back = c.copy()
back[i] = tip[i] - self._gizmo_arrow_len * head_frac
perp1 = (i + 1) % 3
perp2 = (i + 2) % 3
barb_offset = self._gizmo_arrow_len * head_spread
b1 = back.copy()
b1[perp1] += barb_offset
b2 = back.copy()
b2[perp2] += barb_offset
head_pts = np.array([tip, b1, tip, b2], dtype=np.float32)
self._gizmo_heads[i].set_data(
pos=head_pts,
color=self._gizmo_colors[i],
)
def _show_gizmo(self, show=True, persistent=False):
"""Show/hide the gizmo. When persistent=True, Ctrl-release won't hide it."""
self._gizmo_visible = show
for line in self._gizmo_lines:
line.visible = show
for head in self._gizmo_heads:
head.visible = show
if show:
if persistent:
self._gizmo_persistent = True
pos = self._probe_pos_from_sliders()
self._update_gizmo(pos)
else:
self._gizmo_persistent = False
if hasattr(self, '_hint_overlay'):
self._update_contextual_hint()
def _on_gizmo_key(self, event):
"""Show gizmo when Ctrl is pressed (not during ball flow)."""
if event.key == 'Control' and not self.ball_flowing:
if not self._gizmo_visible:
self._show_gizmo(True)
def _on_gizmo_key_release(self, event):
"""Hide gizmo when Ctrl is released, unless persistent or mid-drag."""
if (event.key == 'Control'
and self._gizmo_dragging is None
and not getattr(self, '_gizmo_persistent', False)):
self._show_gizmo(False)
@staticmethod
def _point_to_segment_dist_2d(p, a, b):
"""Shortest distance from point *p* to line segment *a*β*b* in 2D."""
ab = b - a
ab_sq = float(np.dot(ab, ab))
if ab_sq < 1e-8:
return float(np.linalg.norm(p - a))
t = float(np.dot(p - a, ab)) / ab_sq
t = max(0.0, min(1.0, t))
closest = a + t * ab
return float(np.linalg.norm(p - closest))
def _vispy_project(self, point_3d):
"""Project a 3D point (in raw data coords) to 2D canvas pixel coords.
Applies the render-root scale (normalizes data extent) then the
vispy scene transform chain, giving canvas-pixel coordinates.
"""
s = self._scene_scale
pt = np.array([[point_3d[0] * s, point_3d[1] * s, point_3d[2] * s, 1.0]],
dtype=np.float32)
mapped = self.view.scene.transform.map(pt)[0]
w = mapped[3]
if abs(w) < 1e-8:
w = 1.0
return np.array([mapped[0] / w, mapped[1] / w], dtype=np.float64)
def _on_click_track_press(self, event):
"""Track press start to differentiate plain click from drag/orbit."""
if event.button != 1:
return
self._click_track_pos = np.array([event.pos[0], event.pos[1]], dtype=np.float64)
self._click_track_moved = False
def _on_click_track_move(self, event):
if self._click_track_pos is None:
return
cur = np.array([event.pos[0], event.pos[1]], dtype=np.float64)
if float(np.linalg.norm(cur - self._click_track_pos)) > 4.0:
self._click_track_moved = True
def _on_click_track_release(self, event):
"""On plain click (no drag) near the probe, spawn the gizmo persistently
and auto-disable ball flow."""
if event.button != 1:
return
start = self._click_track_pos
moved = self._click_track_moved
self._click_track_pos = None
self._click_track_moved = False
if start is None or moved:
return
# Don't interfere with existing gizmo drag
if self._gizmo_dragging is not None:
return
# If gizmo is already visible (transient, not persistent), skip
if self._gizmo_visible and not getattr(self, '_gizmo_persistent', False):
return
# Check proximity to the probe in screen space
probe_pos = self._probe_pos_from_sliders()
probe_sc = self._vispy_project(probe_pos)
click = np.array([event.pos[0], event.pos[1]], dtype=np.float64)
probe_dist = float(np.linalg.norm(click - probe_sc))
if probe_dist > 60.0:
return
# When attractors are visible, only grab the probe if the click is
# really close (<30px) β otherwise let the attractor handler win.
if self._attractors_visible and probe_dist > 30.0:
return
# Dismiss explain overlay when user starts editing the probe
if hasattr(self, '_explain_overlay'):
self._explain_overlay.setVisible(False)
# Auto-disable ball flow and spawn persistent gizmo
if self.ball_flowing:
self.ball_flowing = False
if self.flow is not None:
self.flow.stop_ball_flow()
self.vis_ball.visible = False
self.vis_trail.visible = False
if hasattr(self, 'chk_ball'):
self.chk_ball.blockSignals(True)
self.chk_ball.setChecked(False)
self.chk_ball.blockSignals(False)
if hasattr(self, '_gizmo_hint'):
self._gizmo_hint.setVisible(True)
self._show_gizmo(True, persistent=True)
def _on_gizmo_press(self, event):
"""Ctrl+click: pick the axis whose screen-space line segment is
closest to the click β pure 2D in canvas pixel coordinates."""
if event.button != 1:
return
if not self._gizmo_visible:
return
probe_pos = self._probe_pos_from_sliders()
click = np.array([event.pos[0], event.pos[1]], dtype=np.float64)
# Project gizmo center and tips to canvas pixels (W-divided)
center_sc = self._vispy_project(probe_pos)
axis_names = ['X', 'Y', 'Z']
best_axis = None
best_dist = float('inf')
debug_dists = []
for i in range(3):
tip_3d = probe_pos.copy().astype(np.float64)
tip_3d[i] += self._gizmo_arrow_len
tip_sc = self._vispy_project(tip_3d)
dist = self._point_to_segment_dist_2d(click, center_sc, tip_sc)
debug_dists.append((axis_names[i], dist, tip_sc))
if dist < best_dist:
best_dist = dist
best_axis = i
# Debug output β keep until gizmo confirmed working
print(f"[GIZMO] click=({click[0]:.0f},{click[1]:.0f}) "
f"center=({center_sc[0]:.0f},{center_sc[1]:.0f})")
for name, d, tip in debug_dists:
marker = " <<<" if name == axis_names[best_axis] else ""
print(f" {name}: tip=({tip[0]:.0f},{tip[1]:.0f}) dist={d:.1f}px{marker}")
if best_axis is None or best_dist > 150:
print(f" β no axis picked (best_dist={best_dist:.0f} > 150px)")
return
print(f" β picked {axis_names[best_axis]} axis (dist={best_dist:.1f}px)")
# Disable camera so it doesn't rotate during drag
self.view.camera.interactive = False
# Screen-space drag setup: project +1 world unit along the
# chosen axis to find the screen direction and scale factor.
tip_3d = probe_pos.copy().astype(np.float64)
tip_3d[best_axis] += 1.0
tip_sc = self._vispy_project(tip_3d)
axis_screen_dir = tip_sc - center_sc
px_per_unit = float(np.linalg.norm(axis_screen_dir))
if px_per_unit < 1e-3:
px_per_unit = 1.0
axis_screen_n = axis_screen_dir / px_per_unit
self._gizmo_dragging = best_axis
self._gizmo_drag_start_pos = probe_pos.copy()
self._gizmo_drag_start_click = click.copy()
self._gizmo_drag_axis_screen_n = axis_screen_n
self._gizmo_drag_px_per_unit = px_per_unit
# Highlight the active arrow, dim the others
for j in range(3):
c = list(self._gizmo_colors[j])
c[3] = 1.0 if j == best_axis else 0.3
self._gizmo_lines[j].set_data(color=c)
self._gizmo_heads[j].set_data(color=c)
def _on_gizmo_move(self, event):
"""Drag: project mouse movement along the axis in screen space,
convert to world units using the pixels-per-unit ratio."""
if self._gizmo_dragging is None:
return
axis = self._gizmo_dragging
current = np.array([event.pos[0], event.pos[1]], dtype=np.float64)
mouse_delta = current - self._gizmo_drag_start_click
# Project mouse movement onto the screen-space axis direction
along_px = float(np.dot(mouse_delta, self._gizmo_drag_axis_screen_n))
world_delta = along_px / self._gizmo_drag_px_per_unit
new_pos = self._gizmo_drag_start_pos.copy()
new_pos[axis] += world_delta
# Clamp to data range with margin
pts = self.result.projected_3d
for i in range(3):
mn, mx = pts[:, i].min(), pts[:, i].max()
margin = (mx - mn) * 0.15
new_pos[i] = np.clip(new_pos[i], mn - margin, mx + margin)
# Update sliders
self._updating_sliders = True
labels = getattr(self.result.axis_info, 'labels', ['X', 'Y', 'Z'])
for i in range(3):
sv = self._raw_to_slider(float(new_pos[i]), i)
sv = int(np.clip(sv, 0, 1000))
self._sliders[i].setValue(sv)
self._slider_labels[i].setText(f"{labels[i]}: {sv / 10:.0f}%")
self._updating_sliders = False
# Update visuals
self.vis_probe.set_data(new_pos.reshape(1, 3), self.PROBE_COLOR)
self._update_gizmo(new_pos)
if self.flow is not None and not self.ball_flowing:
self.flow.set_ball_position(*new_pos)
def _on_gizmo_release(self, event):
"""End axis drag, re-enable camera."""
if self._gizmo_dragging is None:
return
# Re-enable camera interaction
self.view.camera.interactive = True
# Final position
pos = self._probe_pos_from_sliders()
self._update_info(pos)
# Reset arrow colors
for j in range(3):
c = list(self._gizmo_colors[j])
c[3] = 0.9
self._gizmo_lines[j].set_data(color=c)
self._gizmo_heads[j].set_data(color=c)
self._gizmo_dragging = None
self._gizmo_drag_start_pos = None
self._gizmo_drag_start_click = None
self._gizmo_drag_axis_screen_n = None
def _show_point_info(self, idx: int):
"""Show text description of a selected data point."""
if not hasattr(self, '_point_overlay'):
return
entry = self.result.session.entries[idx]
cluster_id = self.result.clusters.labels[idx]
cluster_name = (self.result.cluster_labels[cluster_id]
if cluster_id < len(self.result.cluster_labels)
else f"Cluster {cluster_id}")
self._point_header_label.setText(f"Point #{idx} | {cluster_name}")
text = entry.text
if len(text) > 200:
text = text[:200] + "..."
role = entry.role
self._point_text_label.setText(f"[{role}] {text}")
self._point_overlay.adjustSize()
self._point_overlay.setVisible(True)
self._reposition_overlays()
# βββββββββββββββββββββββββββββββββββββββββββββββββββ
# Public API
# βββββββββββββββββββββββββββββββββββββββββββββββββββ
def _apply_initial_state(self):
"""Apply optional initial UI state overrides from self._initial_state."""
state = self._initial_state
if not state:
return
# Speed multiplier (slider value = speed * 100)
if 'speed' in state and hasattr(self, '_sl_speed'):
speed_val = int(state['speed'] * 100)
self._sl_speed.setValue(speed_val)
# Attractor sensitivity (set BEFORE show_attractors so first
# computation uses the correct value)
if 'sensitivity' in state and hasattr(self, '_sl_sensitivity'):
self._attractor_sensitivity = state['sensitivity']
slider_val = int(max(0, min(1000, state['sensitivity'] * 1000)))
self._sl_sensitivity.setValue(slider_val)
pct = state['sensitivity'] * 100
self._lbl_sensitivity.setText(f"Sensitivity: {pct:.1f}%")
# Show attractors
if state.get('show_attractors') and hasattr(self, 'chk_attractors'):
self.chk_attractors.setChecked(True)
# Flow color mode
if 'flow_color' in state:
self._flow_color_mode = state['flow_color']
if state['flow_color'] == 'cluster':
if hasattr(self, '_chk_cluster_colors'):
self._chk_cluster_colors.setChecked(True)
elif state['flow_color'] == 'entropy':
if hasattr(self, '_chk_entropy'):
self._chk_entropy.setChecked(True)
elif state['flow_color'].startswith('score:'):
channel = state['flow_color'].split(':', 1)[1]
for ch, chk in getattr(self, '_score_color_checkboxes', []):
chk.setChecked(ch == channel)
# Show data points
if state.get('show_points') and hasattr(self, 'chk_points'):
self.chk_points.setChecked(True)
def show(self):
# Initial flow step
if self.flow_active and self.flow is not None:
pos, colors, alphas, speeds = self.flow.step()
rgba = np.empty((len(pos), 4), dtype=np.float32)
rgba[:, :3] = colors.astype(np.float32)
rgba[:, 3] = alphas.astype(np.float32)
self.vis_flow.set_data(pos.astype(np.float32), rgba)
# Start ball flow if enabled by default
if self.ball_flowing and self.flow is not None:
self.flow.start_ball_flow()
# Apply optional initial UI state overrides
self._apply_initial_state()
# Load cached explained paths and attractor explanations
self._load_saved_paths()
self._load_attractor_explanations()
if self._saved_paths:
self._render_all_saved_paths()
if self.window is not None:
self.window.show()
# Workaround: some GPU drivers render specific framebuffer
# widths much slower than adjacent ones (observed: widths
# where width%9==8 on some NVIDIA cards drop from 28 to 12
# FPS). A +2px jog after first paint nudges the canvas onto
# a fast alignment without visible change to the layout.
def _jog_resize():
self.window.resize(
self.window.width() + 2,
self.window.height(),
)
QtCore.QTimer.singleShot(100, _jog_resize)
else:
self.canvas.show()
self._timer.start()
# Show initial contextual hint
if hasattr(self, '_hint_overlay'):
self._update_contextual_hint()
def close(self):
self._timer.stop()
if self.window is not None:
self.window.close()
else:
self.canvas.close()
def run(self):
self.show()
app.run()
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# Public API
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def launch_renderer(result, particle_grid=40, base_size=28.0,
window_size=(1400, 800), explainer=None,
dataset_name=None, initial_state=None) -> FlowRenderer:
"""Launch the GPU-accelerated 3D flow renderer with interactive controls.
Opens a native OpenGL window matching Android's My3DScatterRenderer quality,
with a side panel providing all dashboard features: sliders, toggles,
cluster legend, probe info, mark/clear buttons, and LLM explain.
Falls back to software rendering automatically if no GPU is available.
Args:
result: AnalysisResult from pipeline.analyze()
particle_grid: 40 = 64k particles (Android default), 20 = 8k (lighter)
base_size: Particle size for perspective scaling (40.0 for desktop)
window_size: Window dimensions (width, height)
explainer: SemanticExplainer instance for LLM explanations (optional)
dataset_name: Optional display name shown as title on the renderer
initial_state: Optional dict to override default UI state on launch.
Supported keys (all optional):
speed (float): flow speed multiplier (default 1.0)
show_attractors (bool): show attractor meshes
flow_color (str): 'speed'|'cluster'|'entropy' or 'score:<channel>'
show_points (bool): show data point markers
"""
from tracescope.models.analysis import AnalysisResult
if not isinstance(result, AnalysisResult):
raise TypeError(
f"launch_renderer() requires an AnalysisResult from pipeline.analyze(), "
f"got {type(result).__name__}."
)
_ensure_viable_backend()
renderer = FlowRenderer(result, particle_grid=particle_grid,
base_size=base_size, window_size=window_size,
explainer=explainer, dataset_name=dataset_name,
initial_state=initial_state)
renderer.run()
return renderer
|