efficient_pca 0.1.8

Principal component computation using SVD and covariance matrix trick
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
// In tests/eigensnp_tests.rs
// eigensnp is primarily designed for large-scale genomic datasets,
// such as those found in biobanks or large reference panels.
// These datasets typically have many more features (SNPs) than samples.
// Therefore, the tests below focus on validating eigensnp's performance
// and correctness on large matrices where the number of features significantly
// exceeds the number of samples. Small test cases or cases where samples >= features
// have been deemphasized or removed to better reflect real-world usage scenarios.

#[path = "python_bootstrap.rs"]
mod python_bootstrap;

use python_bootstrap::ensure_python_packages_installed;

use efficient_pca::eigensnp::{
    reorder_array_owned, reorder_columns_owned, EigenSNPCoreAlgorithm, EigenSNPCoreAlgorithmConfig,
    EigenSNPCoreOutput, LdBlockSpecification, PcaReadyGenotypeAccessor, PcaSnpId, PcaSnpMetadata,
    QcSampleId, ThreadSafeStdError,
};
use ndarray::{arr2, s, Array1, Array2, ArrayView1, ArrayView2, Axis}; // ArrayView2 was already added, Array removed
use ndarray_rand::rand_distr::{Normal, StandardNormal, Uniform}; // Added Normal, StandardNormal
use ndarray_rand::RandomExt;
use rand::Rng; // Added for the .sample() method
use rand::SeedableRng; // Already present, but ensure it's here
use rand_chacha::ChaCha8Rng; // Already present, but ensure it's here
use std::fmt::Write as FmtWrite;
use std::fs::{self, File}; // Add fs for create_dir_all
use std::io::Write; // Removed BufReader, BufRead
use std::path::PathBuf;
use std::process::{Command, Stdio};
use std::str::FromStr; // Import with an alias to avoid conflict with std::io::Write
                       // use std::io::Write; // Already present
use std::path::Path; // Add Path
                     // use ndarray::{ArrayView1, ArrayView2}; // These are brought in by `use ndarray::{arr2, s, Array1, Array2, ArrayView1, Axis};`
use lazy_static::lazy_static;
use std::fmt::Display; // To constrain T

// Removed: use crate::eigensnp_integration_tests::parse_pca_py_output;
use crate::eigensnp_integration_tests::generate_structured_data;
use crate::eigensnp_integration_tests::get_python_reference_pca;
use crate::eigensnp_integration_tests::TestDataAccessor;
use crate::eigensnp_integration_tests::TestResultRecord;
use crate::eigensnp_integration_tests::TEST_RESULTS;
const DEFAULT_FLOAT_TOLERANCE_F32: f32 = 1e-4; // Slightly looser for cross-implementation comparison
const DEFAULT_FLOAT_TOLERANCE_F64: f64 = 1e-4; // Slightly looser for cross-implementation comparison

// Helper function for comparing Array1<f64>
fn assert_f64_arrays_are_close(
    arr1: ArrayView1<f64>, // Changed from &Array1<f64>
    arr2: ArrayView1<f64>, // Changed from &Array1<f64>
    tolerance: f64,
    context: &str,
) {
    assert_eq!(
        arr1.dim(),
        arr2.dim(),
        "Array dimensions differ for {}. Left: {:?}, Right: {:?}",
        context,
        arr1.dim(),
        arr2.dim()
    );
    for (i, val1) in arr1.iter().enumerate() {
        let val2 = arr2[i]; // Indexing on ArrayView1 is fine
        assert!(
            (val1 - val2).abs() < tolerance,
            "Mismatch at index {} for {}: {} vs {} (diff: {})",
            i,
            context,
            val1,
            val2,
            (val1 - val2).abs()
        );
    }
}

// Helper for comparing Array2<f32> allowing for sign flips per column
fn assert_f32_arrays_are_close_with_sign_flips(
    arr1: ndarray::ArrayView2<f32>, // Qualified with ndarray::
    arr2: ndarray::ArrayView2<f32>, // Qualified with ndarray::
    tolerance: f32,
    context: &str,
) {
    assert_eq!(
        arr1.dim(),
        arr2.dim(),
        "Array dimensions differ for {}. Left: {:?}, Right: {:?}",
        context,
        arr1.dim(),
        arr2.dim()
    );
    if arr1.ncols() == 0 && arr2.ncols() == 0 {
        // Both empty, considered close
        return;
    }
    if arr1.ncols() == 0 || arr2.ncols() == 0 {
        // One empty, one not
        panic!("Array column count mismatch for {}: Left: {}, Right: {}. Both must be empty or non-empty.", context, arr1.ncols(), arr2.ncols());
    }

    for c_idx in 0..arr1.ncols() {
        let col1 = arr1.column(c_idx); // .column() on ArrayView2 is fine
        let col2 = arr2.column(c_idx); // .column() on ArrayView2 is fine

        let mut direct_match = true;
        for r_idx in 0..col1.len() {
            if (col1[r_idx] - col2[r_idx]).abs() >= tolerance {
                direct_match = false;
                break;
            }
        }

        if direct_match {
            continue;
        }

        let mut flipped_match = true;
        for r_idx in 0..col1.len() {
            if (col1[r_idx] - (-col2[r_idx])).abs() >= tolerance {
                flipped_match = false;
                break;
            }
        }

        assert!(
            flipped_match,
            "Column {} mismatch for {} (even with sign flip check). Max diff: {}. First elements: {} vs {}",
            c_idx, context,
            col1.iter().zip(col2.iter()).map(|(a,b)| (a-b).abs().max((a-(-b)).abs())).fold(0.0f32, f32::max),
            col1.get(0).unwrap_or(&0.0f32), col2.get(0).unwrap_or(&0.0f32)
        );
    }
}

// Standardizes each feature (SNP, row) across samples (columns).
fn standardize_features_across_samples(mut data: Array2<f32>) -> Array2<f32> {
    if data.ncols() <= 1 {
        if data.ncols() == 1 && data.nrows() > 0 {
            data.fill(0.0);
        }
        return data;
    }
    for mut feature_row in data.axis_iter_mut(Axis(0)) {
        let mean = feature_row.mean().unwrap_or(0.0);
        feature_row.mapv_inplace(|x| x - mean);
        let std_dev = feature_row.std(0.0);
        if std_dev.abs() > 1e-7 {
            feature_row.mapv_inplace(|x| x / std_dev);
        } else {
            feature_row.fill(0.0);
        }
    }
    data
}

// Helper functions to save arrays/vectors to TSV for debugging
fn save_matrix_to_tsv<T: Display>(
    matrix: &ArrayView2<T>,
    dir_path: &str,
    file_name: &str,
) -> Result<(), Box<dyn std::error::Error>> {
    let full_path = Path::new(dir_path).join(file_name);
    fs::create_dir_all(Path::new(dir_path))?; // Create directory if it doesn't exist
    let mut file = File::create(full_path)?;
    for row_idx in 0..matrix.nrows() {
        for col_idx in 0..matrix.ncols() {
            write!(file, "{}", matrix[[row_idx, col_idx]])?;
            if col_idx < matrix.ncols() - 1 {
                write!(file, "	")?; // Tab separated
            }
        }
        writeln!(file)?;
    }
    Ok(())
}

fn save_vector_to_tsv<T: Display>(
    vector: &ArrayView1<T>,
    dir_path: &str,
    file_name: &str,
) -> Result<(), Box<dyn std::error::Error>> {
    let full_path = Path::new(dir_path).join(file_name);
    fs::create_dir_all(Path::new(dir_path))?; // Create directory
    let mut file = File::create(full_path)?;
    for i in 0..vector.len() {
        writeln!(file, "{}", vector[i])?;
    }
    Ok(())
}

#[cfg(test)]
mod eigensnp_integration_tests {
    use super::*;
    // Ensure std::io::Write is available for the summary writer function
    use ctor::dtor;
    use std::fs::OpenOptions;
    use std::io::Write;
    use std::path::Path; // Path is used by write_summary_file_impl for Path::new(artifact_dir)
    use std::sync::Mutex; // Mutex is used for TEST_RESULTS // dtor is used for the final_summary_writer attribute

    // Define TestResultRecord struct
    #[derive(Clone, Debug)] // Added Debug
    pub struct TestResultRecord {
        pub test_name: String,
        pub num_features_d: usize,
        pub num_samples_n: usize,
        pub num_pcs_requested_k: usize,
        pub num_pcs_computed: usize,
        pub success: bool,
        pub outcome_details: String,
        pub notes: String,
    }

    // Global static for results
    lazy_static! {
        pub static ref TEST_RESULTS: Mutex<Vec<TestResultRecord>> = Mutex::new(Vec::new());
    }

    // Renamed and modified function to write results to TSV
    fn write_summary_file_impl() -> Result<(), std::io::Error> {
        let results_guard = TEST_RESULTS.lock().unwrap();
        if results_guard.is_empty() {
            println!("[SUMMARY_WRITER] TEST_RESULTS is empty. No summary file will be written.");
            return Ok(()); // No results to write
        }

        let artifact_dir = "target/test_artifacts";
        let tsv_path = Path::new(artifact_dir).join("eigensnp_summary_results.tsv");

        println!(
            "[SUMMARY_WRITER] Attempting to write {} records to {:?}",
            results_guard.len(),
            tsv_path
        );

        if let Err(e) = std::fs::create_dir_all(artifact_dir) {
            eprintln!(
                "[SUMMARY_WRITER] Error creating artifact_dir '{}': {:?}",
                artifact_dir, e
            );
            return Err(e);
        }

        // Use OpenOptions to create or truncate the file
        let mut file = OpenOptions::new()
            .write(true)
            .create(true)
            .truncate(true)
            .open(&tsv_path)?; // Pass tsv_path by reference

        // Write header
        writeln!(file, "TestName	NumFeatures_D	NumSamples_N	NumPCsRequested_K	NumPCsComputed	Success	OutcomeDetails	Notes")?;

        for record in results_guard.iter() {
            writeln!(
                file,
                "{}	{}	{}	{}	{}	{}	{}	{}",
                record.test_name,
                record.num_features_d,
                record.num_samples_n,
                record.num_pcs_requested_k,
                record.num_pcs_computed,
                record.success,
                record.outcome_details.replace("	", " ").replace("\n", "; "), // Sanitize details
                record.notes.replace("	", " ").replace("\n", "; ")            // Sanitize notes
            )?;
        }
        println!(
            "[SUMMARY_WRITER] Successfully wrote summary to {:?}",
            tsv_path
        );
        Ok(())
    }

    // Destructor function to write summary at the end of test execution
    #[dtor]
    fn final_summary_writer() {
        println!(
            "[SUMMARY_WRITER_DTOR] Test execution finished. Running summary writer destructor."
        );
        if let Err(e) = write_summary_file_impl() {
            eprintln!("[SUMMARY_WRITER_DTOR] CRITICAL: Failed to write eigensnp_summary_results.tsv: {:?}", e);
            // Do not panic in dtor
        }
    }

    // Function to call pca.py and parse its output
    pub fn get_python_reference_pca(
        standardized_data: &Array2<f32>,
        k_components_to_request: usize,
        artifact_dir_prefix: &str,
    ) -> Result<(Array2<f32>, Array2<f32>, Array1<f64>), Box<dyn std::error::Error>> {
        let mut stdin_data = String::new();
        for i in 0..standardized_data.nrows() {
            for j in 0..standardized_data.ncols() {
                stdin_data.push_str(&standardized_data[[i, j]].to_string());
                if j < standardized_data.ncols() - 1 {
                    stdin_data.push(' ');
                }
            }
            stdin_data.push('\n');
        }

        let mut script_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
        script_path.push("tests/pca.py");

        ensure_python_packages_installed();
        let mut process = Command::new("python3")
            .arg(script_path.to_str().ok_or("Invalid script path")?)
            .arg("--generate-reference-pca")
            .arg("-k")
            .arg(k_components_to_request.to_string())
            .stdin(Stdio::piped())
            .stdout(Stdio::piped())
            .stderr(Stdio::piped())
            .spawn()?;

        if let Some(mut stdin_pipe) = process.stdin.take() {
            // Write to stdin in a separate thread to avoid deadlocks if the buffer fills up
            std::thread::spawn(move || {
                if let Err(e) = stdin_pipe.write_all(stdin_data.as_bytes()) {
                    // eprintln is okay for a background thread error message in tests
                    eprintln!("Failed to write to stdin of pca.py: {}", e); // Ensure this eprintln is acceptable or use logging framework
                }
            });
        } else {
            return Err(Box::new(std::io::Error::new(
                std::io::ErrorKind::Other,
                "Failed to open stdin pipe for pca.py",
            )));
        }

        let py_cmd_output = process.wait_with_output()?;
        let stdout_str = String::from_utf8_lossy(&py_cmd_output.stdout);
        let stderr_str = String::from_utf8_lossy(&py_cmd_output.stderr);

        if !py_cmd_output.status.success() {
            let error_artifact_dir_name = format!("{}_py_error", artifact_dir_prefix);
            let error_artifact_path =
                Path::new("target/test_artifacts").join(error_artifact_dir_name);
            fs::create_dir_all(&error_artifact_path)?;

            let stdout_path = error_artifact_path.join("pca_stdout.txt");
            let stderr_path = error_artifact_path.join("pca_stderr.txt");

            fs::write(&stdout_path, stdout_str.as_bytes())?;
            fs::write(&stderr_path, stderr_str.as_bytes())?;

            return Err(format!(
                "Python script pca.py failed with status {}. Stdout saved to '{}', Stderr saved to '{}'. Stderr Preview: {}",
                py_cmd_output.status,
                stdout_path.display(),
                stderr_path.display(),
                stderr_str.chars().take(500).collect::<String>() // Preview of stderr
            ).into());
        }

        parse_pca_py_output(&stdout_str).map_err(|e| {
            format!(
                "Failed to parse pca.py output: {}. Output:\n{}",
                e, stdout_str
            )
            .into()
        })
    }

    // Orthonormalizes the columns of a mutable Array2<f32> matrix using Gram-Schmidt process.
    // Columns are assumed to be features or components, rows are samples or observations.
    // This version handles matrices where ncols > nrows by only orthonormalizing the first min(nrows, ncols) columns.
    pub fn orthonormalize_columns(matrix: &mut Array2<f32>) {
        if matrix.ncols() == 0 || matrix.nrows() == 0 {
            return;
        }
        for j in 0..matrix.ncols() {
            // Inner loop to subtract projections of previous vectors
            for i in 0..j {
                // Get an owned copy of column i. This is crucial.
                // matrix.column(i) is an immutable borrow of matrix.
                // .to_owned() clones it, so col_i_owned is now independent of matrix's borrow state for subsequent ops.
                let col_i_owned = matrix.column(i).to_owned();

                // Temporarily get a mutable view of column j for this specific operation.
                // This re-borrows matrix mutably, but only for the scope of col_j_view.
                let mut col_j_view = matrix.column_mut(j);

                // Calculate dot product using the mutable view of col_j and the owned col_i.
                let dot_product = col_j_view.view().dot(&col_i_owned);

                // Perform subtraction using the owned col_i_owned and the view col_j_view.
                // mapv creates a new owned array, so scaled_col_i is also independent.
                let scaled_col_i = col_i_owned.mapv(|x| x * dot_product);
                col_j_view.zip_mut_with(&scaled_col_i, |cj_val, scaled_ci_val| {
                    *cj_val -= scaled_ci_val
                });
                // col_j_view (and its mutable borrow of matrix) goes out of scope here.
            }

            // Normalize column j - re-borrow column j mutably for this operation.
            // This is another short-lived mutable borrow.
            let mut col_j_for_norm = matrix.column_mut(j);
            let norm = col_j_for_norm.mapv(|x| x.powi(2)).sum().sqrt();
            if norm > 1e-6 {
                col_j_for_norm.mapv_inplace(|x| x / norm);
            } else {
                // If column becomes zero (e.g. linear dependency), fill with zeros.
                col_j_for_norm.fill(0.0);
            }
            // col_j_for_norm (and its mutable borrow of matrix) goes out of scope here.
        }
    }

    // Generates structured data: D_snps x N_samples
    // The first K components explain most of the variance.
    // Remaining D_snps - K components are essentially noise or linear combinations.
    // Genotype data is then standardized.
    pub fn generate_structured_data(
        d_total_snps: usize,      // D
        n_samples: usize,         // N
        k_true_components: usize, // K
        signal_strength: f32,     // Multiplier for signal components
        noise_std_dev: f32,       // Standard deviation for noise components
        seed: u64,
    ) -> Array2<f32> {
        let mut rng = ChaCha8Rng::seed_from_u64(seed);

        // Ensure K is not greater than D or N, as it wouldn't make sense for true components
        let k_eff = k_true_components.min(d_total_snps).min(n_samples);

        // 1. Generate K true underlying latent components (N_samples x K_eff)
        // These are random vectors that will form the basis of our structured data.
        // We'll make them orthonormal to ensure they are independent sources of variance.
        let mut latent_components_n_x_k =
            Array2::random_using((n_samples, k_eff), StandardNormal, &mut rng) * signal_strength;
        orthonormalize_columns(&mut latent_components_n_x_k); // Orthonormalize columns (K_eff components)

        // 2. Generate SNP loadings (D_total_snps x K_eff) that map latent components to SNPs
        // These define how each SNP is influenced by the K_eff true components.
        let mut loadings_d_x_k = Array2::zeros((d_total_snps, k_eff));
        // For the first K_eff SNPs, give each a strong loading on one component
        for i in 0..k_eff.min(d_total_snps) {
            // Ensure i < d_total_snps
            loadings_d_x_k[[i, i]] = 1.0;
        }
        // For remaining SNPs (up to d_total_snps), create some varied loadings
        // This makes the structure a bit more complex than a simple identity matrix for loadings
        if d_total_snps > k_eff {
            for i in k_eff..d_total_snps {
                for j in 0..k_eff {
                    // Example: create some linear combinations or random small loadings
                    if i % (j + 1) == 0 {
                        // Arbitrary condition for variety
                        loadings_d_x_k[[i, j]] = rng.sample(Normal::new(0.0, 0.5).unwrap());
                    }
                }
            }
        }
        // Optionally, orthonormalize these loading vectors (columns of loadings_d_x_k) as well,
        // depending on the desired properties of the "true" SNP effects.
        // For this example, we might skip it to allow for correlated SNP effects from components.

        // 3. Construct the "signal" part of the SNP data: (D_total_snps x K_eff) dot (K_eff x N_samples) -> (D_total_snps x N_samples)
        // Note:
        // Latent components are N x K (rows are samples, columns are components)
        // Loadings are D x K (rows are SNPs, columns are components)
        // We want SNP data as D x N. So, SNP_data = Loadings_D_x_K * Latent_Components_K_x_N (where Latent_Components_K_x_N is latent_components_n_x_k.t())
        let signal_data_d_x_n = loadings_d_x_k.dot(&latent_components_n_x_k.t());

        // 4. Generate "noise" data (D_total_snps x N_samples)
        // This represents the variance not explained by the K true components.
        let noise_data_d_x_n = Array2::random_using(
            (d_total_snps, n_samples),
            Normal::new(0.0, noise_std_dev).unwrap(),
            &mut rng,
        );

        // 5. Combine signal and noise
        let combined_data_d_x_n = signal_data_d_x_n + noise_data_d_x_n;

        // 6. Standardize the final data (features across samples)
        // This is a crucial step as PCA typically works on standardized data.
        standardize_features_across_samples(combined_data_d_x_n)
    }

    #[derive(Clone)]
    pub struct TestDataAccessor {
        standardized_data: Array2<f32>,
    }

    impl TestDataAccessor {
        pub fn new(standardized_data: Array2<f32>) -> Self {
            // The line `let num_samples = standardized_data.ncols();` has been removed.
            Self { standardized_data }
        }

        pub fn new_empty(num_pca_snps: usize, num_qc_samples: usize) -> Self {
            let standardized_data = Array2::zeros((num_pca_snps, num_qc_samples));
            Self { standardized_data }
        }
    }

    impl PcaReadyGenotypeAccessor for TestDataAccessor {
        fn get_standardized_snp_sample_block(
            &self,
            snp_ids: &[PcaSnpId],
            sample_ids: &[QcSampleId],
        ) -> Result<Array2<f32>, ThreadSafeStdError> {
            if snp_ids.is_empty() {
                return Ok(Array2::zeros((0, sample_ids.len())));
            }
            if sample_ids.is_empty() {
                return Ok(Array2::zeros((snp_ids.len(), 0)));
            }

            if self.standardized_data.nrows() == 0 && !snp_ids.is_empty() {
                return Err(Box::new(std::io::Error::new(
                    std::io::ErrorKind::InvalidInput,
                    format!(
                        "Requested {} SNPs from an accessor with 0 SNPs.",
                        snp_ids.len()
                    ),
                )));
            }
            if self.standardized_data.ncols() == 0 && !sample_ids.is_empty() {
                return Err(Box::new(std::io::Error::new(
                    std::io::ErrorKind::InvalidInput,
                    format!(
                        "Requested {} samples from an accessor with 0 samples.",
                        sample_ids.len()
                    ),
                )));
            }

            let mut result_block = Array2::zeros((snp_ids.len(), sample_ids.len()));
            for (i, pca_snp_id) in snp_ids.iter().enumerate() {
                let target_row_idx = pca_snp_id.0;
                if target_row_idx >= self.standardized_data.nrows() {
                    return Err(Box::new(std::io::Error::new(
                        std::io::ErrorKind::InvalidInput,
                        format!(
                            "SNP ID PcaSnpId({}) out of bounds for {} SNPs",
                            target_row_idx,
                            self.standardized_data.nrows()
                        ),
                    )));
                }
                for (j, qc_sample_id) in sample_ids.iter().enumerate() {
                    let target_col_idx = qc_sample_id.0;
                    if target_col_idx >= self.standardized_data.ncols() {
                        return Err(Box::new(std::io::Error::new(
                            std::io::ErrorKind::InvalidInput,
                            format!(
                                "Sample ID QcSampleId({}) out of bounds for {} samples",
                                target_col_idx,
                                self.standardized_data.ncols()
                            ),
                        )));
                    }
                    result_block[[i, j]] = self.standardized_data[[target_row_idx, target_col_idx]];
                }
            }
            Ok(result_block)
        }

        fn num_pca_snps(&self) -> usize {
            self.standardized_data.nrows()
        }
        fn num_qc_samples(&self) -> usize {
            self.standardized_data.ncols()
        }
    }

    fn parse_section<T: FromStr>(
        lines: &mut std::iter::Peekable<std::str::Lines<'_>>,
        expected_dim2: Option<usize>,
    ) -> Result<Array2<T>, String>
    where
        <T as FromStr>::Err: std::fmt::Debug,
    {
        let mut data_vec = Vec::new();
        let mut current_dim2 = None;

        loop {
            match lines.peek() {
                Some(line_peek) => {
                    if line_peek.is_empty()
                        || line_peek.starts_with("LOADINGS:")
                        || line_peek.starts_with("SCORES:")
                        || line_peek.starts_with("EIGENVALUES:")
                    {
                        // This is a separator or next section header, so stop parsing for current section
                        break;
                    }
                    // If not a separator, it's data for the current section. Consume and parse.
                    let line = lines.next().unwrap(); // Safe due to peek

                    let row: Vec<T> = line
                        .split_whitespace()
                        .map(|s| {
                            s.parse::<T>().map_err(|e| {
                                format!("Failed to parse value: {:?}, error: {:?}", s, e)
                            })
                        })
                        .collect::<Result<Vec<T>, String>>()?;

                    if let Some(d2) = current_dim2 {
                        if row.len() != d2 {
                            return Err(format!(
                                "Inconsistent row length. Expected {}, got {}",
                                d2,
                                row.len()
                            ));
                        }
                    } else {
                        current_dim2 = Some(row.len());
                        if let Some(exp_d2) = expected_dim2 {
                            // Allow empty rows if section is empty (e.g. 0-component PCA, eigenvalues line is just empty)
                            // If row.is_empty() is true, it means current_dim2 became Some(0).
                            // If exp_d2 is Some(1) (e.g. for eigenvalues), and we got an empty line (parsed to row.len() == 0),
                            // this is a valid case for an empty section (e.g. 0 eigenvalues).
                            // The check `!row.is_empty()` was there before, let's see if it's still needed.
                            // If row.is_empty(), current_dim2 will be Some(0).
                            // If expected_dim2 is Some(1), and current_dim2 is Some(0), this is fine for an empty section.
                            // The problem is if the first non-empty row has a different number of columns than expected.
                            if !row.is_empty() && row.len() != exp_d2 {
                                return Err(format!(
                                    "Unexpected row length for section. Expected {}, got {}",
                                    exp_d2,
                                    row.len()
                                ));
                            }
                            // If row is empty and exp_d2 is Some(k) where k > 0: current_dim2 becomes Some(0). This is okay for an empty section.
                        }
                    }
                    data_vec.extend(row);
                }
                None => {
                    // End of input
                    break;
                }
            }
        }

        let actual_dim2 = current_dim2.unwrap_or_else(|| expected_dim2.unwrap_or(0));
        let num_rows = if actual_dim2 == 0 {
            0
        } else {
            data_vec.len() / actual_dim2
        };

        Array2::from_shape_vec((num_rows, actual_dim2), data_vec)
            .map_err(|e| format!("Failed to create Array2: {}", e))
    }

    pub fn parse_pca_py_output(
        output_str: &str,
    ) -> Result<(Array2<f32>, Array2<f32>, Array1<f64>), String> {
        let mut lines = output_str.lines().peekable();

        let mut py_loadings: Option<Array2<f32>> = None;
        let mut py_scores: Option<Array2<f32>> = None;
        let mut py_eigenvalues: Option<Array1<f64>> = None;

        while let Some(line_peek) = lines.peek() {
            let current_line_is_empty = line_peek.is_empty();
            if line_peek.starts_with("LOADINGS:") {
                lines.next(); // Consume header
                py_loadings = Some(parse_section(&mut lines, None)?);
            } else if line_peek.starts_with("SCORES:") {
                lines.next(); // Consume header
                py_scores = Some(parse_section(&mut lines, None)?);
            } else if line_peek.starts_with("EIGENVALUES:") {
                lines.next(); // Consume header
                let eig_array2 = parse_section(&mut lines, Some(1))?;
                let eig_len = eig_array2.len();
                py_eigenvalues = Some(
                    eig_array2
                        .into_shape_with_order((eig_len,))
                        .expect("Failed to reshape py_eigenvalues"),
                );
            } else if current_line_is_empty {
                lines.next(); // Consume the empty line
                              // Continue to next iteration to peek at next line
            } else {
                // Unexpected line
                return Err(format!(
                    "Unexpected content in pca.py output. Line: '{}'",
                    line_peek
                ));
            }
        }

        Ok((
            py_loadings.ok_or_else(|| "LOADINGS section not found".to_string())?,
            py_scores.ok_or_else(|| "SCORES section not found".to_string())?,
            py_eigenvalues.ok_or_else(|| "EIGENVALUES section not found".to_string())?,
        ))
    }

    // Helper function for PC scores orthogonality tests to avoid code duplication
    pub fn run_pc_scores_orthogonality_test(
        test_name_str: &str,
        num_snps: usize,
        num_samples: usize,
        num_pcs_target: usize,
        seed: u64,
    ) {
        let test_name = test_name_str.to_string();
        let mut test_successful = true;
        let mut outcome_details = String::new();
        let notes = format!(
            "Matrix: {}x{}, PCs: {}",
            num_snps, num_samples, num_pcs_target
        );

        let mut max_off_diagonal_cov = 0.0f64;
        let mut max_diag_eigenvalue_diff = 0.0f64;

        let output_result_tuple = std::panic::catch_unwind(|| {
            let mut rng = ChaCha8Rng::seed_from_u64(seed);
            let raw_genos =
                Array2::random_using((num_snps, num_samples), Uniform::new(0.0, 3.0), &mut rng);
            let standardized_genos = standardize_features_across_samples(raw_genos);
            let test_data = TestDataAccessor::new(standardized_genos);

            let config = EigenSNPCoreAlgorithmConfig {
                target_num_global_pcs: num_pcs_target,
                subset_factor_for_local_basis_learning: 0.5, // Example value
                min_subset_size_for_local_basis_learning: (num_samples / 4)
                    .max(1)
                    .min(num_samples.max(1)),
                max_subset_size_for_local_basis_learning: (num_samples / 2)
                    .max(10)
                    .min(num_samples.max(1)),
                components_per_ld_block: 10
                    .min(num_snps.min((num_samples / 2).max(10).min(num_samples.max(1)))),
                random_seed: seed,
                ..Default::default()
            };
            let algorithm = EigenSNPCoreAlgorithm::new(config);
            let ld_blocks = vec![LdBlockSpecification {
                user_defined_block_tag: "block1".to_string(),
                pca_snp_ids_in_block: (0..num_snps).map(PcaSnpId).collect(),
            }];
            let snp_metadata = create_dummy_snp_metadata(num_snps);
            algorithm.compute_pca(&test_data, &ld_blocks, &snp_metadata)
        });

        match output_result_tuple {
            Ok(Ok((output, _))) => {
                if output.num_principal_components_computed != num_pcs_target {
                    test_successful = false;
                    outcome_details.push_str(&format!(
                        "Did not compute target PCs. Expected: {}, Got: {}. ",
                        num_pcs_target, output.num_principal_components_computed
                    ));
                }

                let scores = &output.final_sample_principal_component_scores;
                if scores.nrows() != num_samples {
                    test_successful = false;
                    outcome_details.push_str(&format!(
                        "Scores nrows mismatch. Expected: {}, Got: {}. ",
                        num_samples,
                        scores.nrows()
                    ));
                }
                if scores.ncols() != output.num_principal_components_computed {
                    // Check against actual computed PCs
                    test_successful = false;
                    outcome_details.push_str(&format!(
                        "Scores ncols mismatch. Expected: {}, Got: {}. ",
                        output.num_principal_components_computed,
                        scores.ncols()
                    ));
                }

                if num_samples <= 1 || output.num_principal_components_computed == 0 {
                    outcome_details.push_str("Test condition (num_samples <=1 or num_pcs_computed == 0) means no further checks performed. ");
                } else if test_successful {
                    // Only proceed if initial checks passed
                    let scores_f64 = scores.mapv(|x| x as f64);
                    let denominator = if output.num_qc_samples_used > 1 {
                        output.num_qc_samples_used as f64 - 1.0
                    } else {
                        1.0 // Avoid division by zero if num_qc_samples_used is 1 or 0
                    };
                    if denominator == 0.0 {
                        // Should not happen if num_samples > 1
                        test_successful = false;
                        outcome_details
                            .push_str("Denominator for covariance calculation is zero. ");
                    } else {
                        let covariance_matrix = scores_f64.t().dot(&scores_f64) / denominator;
                        let k_eff = output.num_principal_components_computed;

                        for r in 0..k_eff {
                            for c in 0..k_eff {
                                if r == c {
                                    let diff = (covariance_matrix[[r, c]]
                                        - output.final_principal_component_eigenvalues[r])
                                        .abs();
                                    if diff > max_diag_eigenvalue_diff {
                                        max_diag_eigenvalue_diff = diff;
                                    }
                                    if diff >= DEFAULT_FLOAT_TOLERANCE_F64 * 100.0 {
                                        test_successful = false;
                                        outcome_details.push_str(&format!(
                                            "Covariance diagonal [{},{}] {} does not match eigenvalue {}. Diff: {}. ",
                                            r, c, covariance_matrix[[r, c]], output.final_principal_component_eigenvalues[r], diff
                                        ));
                                    }
                                } else {
                                    let off_diag_val = covariance_matrix[[r, c]].abs();
                                    if off_diag_val > max_off_diagonal_cov {
                                        max_off_diagonal_cov = off_diag_val;
                                    }
                                    if off_diag_val >= DEFAULT_FLOAT_TOLERANCE_F64 * 100.0 {
                                        test_successful = false;
                                        outcome_details.push_str(&format!(
                                            "Covariance off-diagonal [{},{}] {} is not close to 0. Value: {}. ",
                                            r, c, covariance_matrix[[r, c]], off_diag_val
                                        ));
                                    }
                                }
                            }
                        }
                        if test_successful {
                            outcome_details.push_str(&format!("All orthogonality checks passed. Max off-diag cov: {:.2e}, Max diag-eigenvalue diff: {:.2e}. ", max_off_diagonal_cov, max_diag_eigenvalue_diff));
                        } else {
                            outcome_details.push_str(&format!("Orthogonality checks failed. Max off-diag cov: {:.2e}, Max diag-eigenvalue diff: {:.2e}. ", max_off_diagonal_cov, max_diag_eigenvalue_diff));
                        }
                    }
                }
                let record = TestResultRecord {
                    test_name: test_name.clone(),
                    num_features_d: num_snps,
                    num_samples_n: num_samples,
                    num_pcs_requested_k: num_pcs_target,
                    num_pcs_computed: output.num_principal_components_computed,
                    success: test_successful,
                    outcome_details: outcome_details.clone(),
                    notes,
                };
                eigensnp_integration_tests::TEST_RESULTS
                    .lock()
                    .unwrap()
                    .push(record);
            }
            Ok(Err(e)) => {
                // PCA computation itself failed
                test_successful = false;
                outcome_details = format!("PCA computation failed: {:?}", e);
                let record = TestResultRecord {
                    test_name: test_name.clone(),
                    num_features_d: num_snps,
                    num_samples_n: num_samples,
                    num_pcs_requested_k: num_pcs_target,
                    num_pcs_computed: 0, // Failed to compute
                    success: test_successful,
                    outcome_details: outcome_details.clone(),
                    notes,
                };
                eigensnp_integration_tests::TEST_RESULTS
                    .lock()
                    .unwrap()
                    .push(record);
            }
            Err(e) => {
                // Panic during PCA computation or setup
                test_successful = false;
                outcome_details = format!("Test panicked: {:?}", e);
                let record = TestResultRecord {
                    test_name: test_name.clone(),
                    num_features_d: num_snps,
                    num_samples_n: num_samples,
                    num_pcs_requested_k: num_pcs_target,
                    num_pcs_computed: 0, // Panic, no output
                    success: test_successful,
                    outcome_details: outcome_details.clone(),
                    notes,
                };
                eigensnp_integration_tests::TEST_RESULTS
                    .lock()
                    .unwrap()
                    .push(record);
            }
        }
        assert!(
            test_successful,
            "Test {} failed. Max off-diag: {:.2e}, Max diag-eig diff: {:.2e}. Details: {}",
            test_name, max_off_diagonal_cov, max_diag_eigenvalue_diff, outcome_details
        );
    }

    #[test]
    fn test_pc_scores_orthogonality_large_500x100() {
        run_pc_scores_orthogonality_test(
            "test_pc_scores_orthogonality_large_500x100",
            500,
            100,
            5,
            123,
        );
    }

    #[test]
    fn test_pc_scores_orthogonality_large_1000x200() {
        run_pc_scores_orthogonality_test(
            "test_pc_scores_orthogonality_large_1000x200",
            1000,
            200,
            10,
            124,
        );
    }

    // Helper function for SNP loadings orthonormality tests
    pub fn run_snp_loadings_orthonormality_test(
        test_name_str: &str,
        num_snps: usize,
        num_samples: usize,
        num_pcs_target: usize,
        seed: u64,
    ) {
        let test_name = test_name_str.to_string();
        let mut test_successful = true;
        let mut outcome_details = String::new();
        let notes = format!(
            "Matrix: {}x{}, PCs: {}",
            num_snps, num_samples, num_pcs_target
        );
        let mut max_deviation_from_identity = 0.0f32;

        let output_result_tuple = std::panic::catch_unwind(|| {
            let mut rng = ChaCha8Rng::seed_from_u64(seed);
            let raw_genos =
                Array2::random_using((num_snps, num_samples), Uniform::new(0.0, 3.0), &mut rng);
            let standardized_genos = standardize_features_across_samples(raw_genos);
            let test_data = TestDataAccessor::new(standardized_genos);

            let config = EigenSNPCoreAlgorithmConfig {
                target_num_global_pcs: num_pcs_target,
                random_seed: seed,
                // Use appropriate subset settings for larger data
                subset_factor_for_local_basis_learning: 0.5,
                min_subset_size_for_local_basis_learning: (num_samples / 4)
                    .max(1)
                    .min(num_samples.max(1)),
                max_subset_size_for_local_basis_learning: (num_samples / 2)
                    .max(10)
                    .min(num_samples.max(1)),
                components_per_ld_block: 10
                    .min(num_snps.min((num_samples / 2).max(10).min(num_samples.max(1)))),
                ..Default::default()
            };
            let algorithm = EigenSNPCoreAlgorithm::new(config);
            let ld_blocks = vec![LdBlockSpecification {
                user_defined_block_tag: "block1".to_string(),
                pca_snp_ids_in_block: (0..num_snps).map(PcaSnpId).collect(),
            }];
            let snp_metadata = create_dummy_snp_metadata(num_snps);
            algorithm.compute_pca(&test_data, &ld_blocks, &snp_metadata)
        });

        match output_result_tuple {
            Ok(Ok((output, _))) => {
                if output.num_principal_components_computed != num_pcs_target {
                    test_successful = false;
                    outcome_details.push_str(&format!(
                        "Did not compute target PCs. Expected: {}, Got: {}. ",
                        num_pcs_target, output.num_principal_components_computed
                    ));
                }

                let loadings = &output.final_snp_principal_component_loadings;
                if loadings.nrows() != num_snps {
                    test_successful = false;
                    outcome_details.push_str(&format!(
                        "Loadings nrows mismatch. Expected: {}, Got: {}. ",
                        num_snps,
                        loadings.nrows()
                    ));
                }
                if loadings.ncols() != output.num_principal_components_computed {
                    test_successful = false;
                    outcome_details.push_str(&format!(
                        "Loadings ncols mismatch. Expected: {}, Got: {}. ",
                        output.num_principal_components_computed,
                        loadings.ncols()
                    ));
                }

                if output.num_principal_components_computed == 0 {
                    outcome_details.push_str("No PCs computed, skipping orthonormality check. ");
                } else if test_successful {
                    let check_identity = loadings.t().dot(loadings);
                    let k_eff = output.num_principal_components_computed;

                    for r_idx in 0..k_eff {
                        for c_idx in 0..k_eff {
                            let expected_val = if r_idx == c_idx { 1.0 } else { 0.0 };
                            let deviation = (check_identity[[r_idx, c_idx]] - expected_val).abs();
                            if deviation > max_deviation_from_identity {
                                max_deviation_from_identity = deviation;
                            }
                            if deviation >= DEFAULT_FLOAT_TOLERANCE_F32 {
                                test_successful = false;
                                outcome_details.push_str(&format!(
                                    "Loadings orthonormality check: Identity matrix mismatch at [{},{}]. Expected {}, Got {}. Diff: {}. ",
                                    r_idx, c_idx, expected_val, check_identity[[r_idx, c_idx]], deviation
                                ));
                            }
                        }
                    }
                    if test_successful {
                        outcome_details.push_str(&format!("All orthonormality checks passed. Max deviation from identity: {:.2e}. ", max_deviation_from_identity));
                    } else {
                        outcome_details.push_str(&format!(
                            "Orthonormality checks failed. Max deviation from identity: {:.2e}. ",
                            max_deviation_from_identity
                        ));
                    }
                }
                let record = eigensnp_integration_tests::TestResultRecord {
                    test_name: test_name.clone(),
                    num_features_d: num_snps,
                    num_samples_n: num_samples,
                    num_pcs_requested_k: num_pcs_target,
                    num_pcs_computed: output.num_principal_components_computed,
                    success: test_successful,
                    outcome_details: outcome_details.clone(),
                    notes,
                };
                TEST_RESULTS.lock().unwrap().push(record);
            }
            Ok(Err(e)) => {
                test_successful = false;
                outcome_details = format!("PCA computation failed: {:?}", e);
                let record = TestResultRecord {
                    test_name: test_name.clone(),
                    num_features_d: num_snps,
                    num_samples_n: num_samples,
                    num_pcs_requested_k: num_pcs_target,
                    num_pcs_computed: 0,
                    success: test_successful,
                    outcome_details: outcome_details.clone(),
                    notes,
                };
                TEST_RESULTS.lock().unwrap().push(record);
            }
            Err(e) => {
                test_successful = false;
                outcome_details = format!("Test panicked: {:?}", e);
                let record = TestResultRecord {
                    test_name: test_name.clone(),
                    num_features_d: num_snps,
                    num_samples_n: num_samples,
                    num_pcs_requested_k: num_pcs_target,
                    num_pcs_computed: 0,
                    success: test_successful,
                    outcome_details: outcome_details.clone(),
                    notes,
                };
                TEST_RESULTS.lock().unwrap().push(record);
            }
        }
        assert!(
            test_successful,
            "Test {} failed. Max deviation from identity: {:.2e}. Details: {}",
            test_name, max_deviation_from_identity, outcome_details
        );
    }

    #[test]
    fn test_snp_loadings_orthonormality_large_500x100() {
        run_snp_loadings_orthonormality_test(
            "test_snp_loadings_orthonormality_large_500x100",
            500,
            100,
            5,
            456,
        );
    }

    #[test]
    fn test_snp_loadings_orthonormality_large_1000x200() {
        run_snp_loadings_orthonormality_test(
            "test_snp_loadings_orthonormality_large_1000x200",
            1000,
            200,
            10,
            457,
        );
    }

    // Helper function for eigenvalue-score variance correspondence tests
    pub fn run_eigenvalue_score_variance_correspondence_test(
        test_name_str: &str,
        num_snps: usize,
        num_samples: usize,
        num_pcs_target: usize,
        seed: u64,
    ) {
        let test_name = test_name_str.to_string();
        let mut test_successful = true;
        let mut outcome_details = String::new();
        let notes = format!(
            "Matrix: {}x{}, PCs: {}",
            num_snps, num_samples, num_pcs_target
        );
        let mut max_variance_eigenvalue_diff = 0.0f64;

        let output_result_tuple = std::panic::catch_unwind(|| {
            let mut rng = ChaCha8Rng::seed_from_u64(seed);
            let raw_genos =
                Array2::random_using((num_snps, num_samples), Uniform::new(0.0, 3.0), &mut rng);
            let standardized_genos = standardize_features_across_samples(raw_genos);
            let test_data = TestDataAccessor::new(standardized_genos);

            let config = EigenSNPCoreAlgorithmConfig {
                target_num_global_pcs: num_pcs_target,
                random_seed: seed,
                // Use appropriate subset settings for larger data
                subset_factor_for_local_basis_learning: 0.5,
                min_subset_size_for_local_basis_learning: (num_samples / 4)
                    .max(1)
                    .min(num_samples.max(1)),
                max_subset_size_for_local_basis_learning: (num_samples / 2)
                    .max(10)
                    .min(num_samples.max(1)),
                components_per_ld_block: 10
                    .min(num_snps.min((num_samples / 2).max(10).min(num_samples.max(1)))),
                ..Default::default()
            };
            let algorithm = EigenSNPCoreAlgorithm::new(config);
            let ld_blocks = vec![LdBlockSpecification {
                user_defined_block_tag: "block1".to_string(),
                pca_snp_ids_in_block: (0..num_snps).map(PcaSnpId).collect(),
            }];
            let snp_metadata = create_dummy_snp_metadata(num_snps);
            algorithm.compute_pca(&test_data, &ld_blocks, &snp_metadata)
        });

        match output_result_tuple {
            Ok(Ok((output, _))) => {
                if output.num_principal_components_computed != num_pcs_target {
                    test_successful = false;
                    outcome_details.push_str(&format!(
                        "Did not compute target PCs. Expected: {}, Got: {}. ",
                        num_pcs_target, output.num_principal_components_computed
                    ));
                }

                if num_samples <= 1 || output.num_principal_components_computed == 0 {
                    outcome_details.push_str("Test condition (num_samples <=1 or num_pcs_computed == 0) means no further checks performed. ");
                } else if test_successful {
                    let scores = &output.final_sample_principal_component_scores;
                    let eigenvalues = &output.final_principal_component_eigenvalues;
                    let k_eff = output.num_principal_components_computed;
                    let denominator = if output.num_qc_samples_used > 1 {
                        output.num_qc_samples_used as f64 - 1.0
                    } else {
                        1.0 // Avoid division by zero
                    };

                    if denominator == 0.0 {
                        test_successful = false;
                        outcome_details.push_str("Denominator for variance calculation is zero. ");
                    } else {
                        for k_idx in 0..k_eff {
                            let score_column_k = scores.column(k_idx);
                            let sum_sq_f64 = score_column_k
                                .iter()
                                .map(|&x| (x as f64).powi(2))
                                .sum::<f64>();
                            let variance_of_score_k = sum_sq_f64 / denominator;

                            let diff = (variance_of_score_k - eigenvalues[k_idx]).abs();
                            if diff > max_variance_eigenvalue_diff {
                                max_variance_eigenvalue_diff = diff;
                            }
                            if diff >= DEFAULT_FLOAT_TOLERANCE_F64 * 100.0 {
                                // Relaxed tolerance
                                test_successful = false;
                                outcome_details.push_str(&format!(
                                    "Variance of score column {} ({}) does not match eigenvalue {} ({}). Diff: {}. ",
                                    k_idx, variance_of_score_k, k_idx, eigenvalues[k_idx], diff
                                ));
                            }
                        }
                        if test_successful {
                            outcome_details.push_str(&format!(
                                "All variance-eigenvalue checks passed. Max diff: {:.2e}. ",
                                max_variance_eigenvalue_diff
                            ));
                        } else {
                            outcome_details.push_str(&format!(
                                "Variance-eigenvalue checks failed. Max diff: {:.2e}. ",
                                max_variance_eigenvalue_diff
                            ));
                        }
                    }
                }
                let record = eigensnp_integration_tests::TestResultRecord {
                    test_name: test_name.clone(),
                    num_features_d: num_snps,
                    num_samples_n: num_samples,
                    num_pcs_requested_k: num_pcs_target,
                    num_pcs_computed: output.num_principal_components_computed,
                    success: test_successful,
                    outcome_details: outcome_details.clone(),
                    notes,
                };
                TEST_RESULTS.lock().unwrap().push(record);
            }
            Ok(Err(e)) => {
                test_successful = false;
                outcome_details = format!("PCA computation failed: {:?}", e);
                let record = TestResultRecord {
                    test_name: test_name.clone(),
                    num_features_d: num_snps,
                    num_samples_n: num_samples,
                    num_pcs_requested_k: num_pcs_target,
                    num_pcs_computed: 0,
                    success: test_successful,
                    outcome_details: outcome_details.clone(),
                    notes,
                };
                TEST_RESULTS.lock().unwrap().push(record);
            }
            Err(e) => {
                test_successful = false;
                outcome_details = format!("Test panicked: {:?}", e);
                let record = TestResultRecord {
                    test_name: test_name.clone(),
                    num_features_d: num_snps,
                    num_samples_n: num_samples,
                    num_pcs_requested_k: num_pcs_target,
                    num_pcs_computed: 0,
                    success: test_successful,
                    outcome_details: outcome_details.clone(),
                    notes,
                };
                TEST_RESULTS.lock().unwrap().push(record);
            }
        }
        assert!(
            test_successful,
            "Test {} failed. Max variance-eigenvalue diff: {:.2e}. Details: {}",
            test_name, max_variance_eigenvalue_diff, outcome_details
        );
    }

    #[test]
    fn test_eigenvalue_score_variance_correspondence_large_500x100() {
        run_eigenvalue_score_variance_correspondence_test(
            "test_eigenvalue_score_variance_correspondence_large_500x100",
            500,
            100,
            5,
            789,
        );
    }

    #[test]
    fn test_eigenvalue_score_variance_correspondence_large_1000x200() {
        run_eigenvalue_score_variance_correspondence_test(
            "test_eigenvalue_score_variance_correspondence_large_1000x200",
            1000,
            200,
            10,
            790,
        );
    }

    #[test]
    fn test_pca_zero_snps() {
        let num_samples = 10;
        let num_snps = 0; // Explicit for clarity in logging
        let k_requested = 2;

        let test_data = TestDataAccessor::new_empty(num_snps, num_samples);

        let config = EigenSNPCoreAlgorithmConfig {
            target_num_global_pcs: k_requested,
            ..Default::default()
        };
        let algorithm = EigenSNPCoreAlgorithm::new(config);
        let ld_blocks = vec![];

        let snp_metadata = create_dummy_snp_metadata(num_snps);
        let (output, _) = algorithm
            .compute_pca(&test_data, &ld_blocks, &snp_metadata)
            .expect("PCA with 0 SNPs failed");

        assert_eq!(output.num_pca_snps_used, 0);
        assert_eq!(output.num_qc_samples_used, num_samples);
        assert_eq!(output.num_principal_components_computed, 0);
        assert_eq!(output.final_snp_principal_component_loadings.nrows(), 0);
        assert_eq!(output.final_snp_principal_component_loadings.ncols(), 0);
        assert_eq!(
            output.final_sample_principal_component_scores.nrows(),
            num_samples
        );
        assert_eq!(output.final_sample_principal_component_scores.ncols(), 0);
        assert_eq!(output.final_principal_component_eigenvalues.len(), 0);

        let record = TestResultRecord {
            test_name: "test_pca_zero_snps".to_string(),
            num_features_d: num_snps,
            num_samples_n: num_samples,
            num_pcs_requested_k: k_requested,
            num_pcs_computed: output.num_principal_components_computed,
            success: true, // Assertions above would panic on failure
            outcome_details: "Validated behavior with zero SNPs. All assertions passed."
                .to_string(),
            notes: "Edge case test.".to_string(),
        };
        TEST_RESULTS.lock().unwrap().push(record);
    }

    #[test]
    fn test_pca_zero_samples() {
        let num_snps = 20;
        let num_samples = 0; // Explicit for clarity in logging
        let k_requested = 2;

        let test_data = TestDataAccessor::new_empty(num_snps, num_samples);

        let config = EigenSNPCoreAlgorithmConfig {
            target_num_global_pcs: k_requested,
            ..Default::default()
        };
        let algorithm = EigenSNPCoreAlgorithm::new(config);
        let ld_blocks = vec![LdBlockSpecification {
            user_defined_block_tag: "block1".to_string(),
            pca_snp_ids_in_block: (0..num_snps).map(PcaSnpId).collect(),
        }];

        let snp_metadata = create_dummy_snp_metadata(num_snps);
        let (output, _) = algorithm
            .compute_pca(&test_data, &ld_blocks, &snp_metadata)
            .expect("PCA with 0 samples failed");

        assert_eq!(output.num_qc_samples_used, 0);
        assert_eq!(output.num_pca_snps_used, num_snps);
        assert_eq!(output.num_principal_components_computed, 0);
        assert_eq!(
            output.final_snp_principal_component_loadings.nrows(),
            num_snps
        );
        assert_eq!(output.final_snp_principal_component_loadings.ncols(), 0);
        assert_eq!(output.final_sample_principal_component_scores.nrows(), 0);
        assert_eq!(output.final_sample_principal_component_scores.ncols(), 0);
        assert_eq!(output.final_principal_component_eigenvalues.len(), 0);

        let record = TestResultRecord {
            test_name: "test_pca_zero_samples".to_string(),
            num_features_d: num_snps,
            num_samples_n: num_samples,
            num_pcs_requested_k: k_requested,
            num_pcs_computed: output.num_principal_components_computed,
            success: true, // Assertions above would panic on failure
            outcome_details: "Validated behavior with zero samples. All assertions passed."
                .to_string(),
            notes: "Edge case test.".to_string(),
        };
        TEST_RESULTS.lock().unwrap().push(record);
    }

    #[test]
    fn test_pca_more_components_requested_than_rank_d_gt_n() {
        let mut overall_test_successful = true;
        let mut outcome_details = String::new();
        let mut notes = String::new();
        notes.push_str("Testing k_requested > true rank, with D > N (150x50). ");

        // Phase-specific success flags
        // let mut python_script_phase_ok = true; // Replaced by direct use of get_python_reference_pca
        let mut rust_pca_computation_phase_ok = true;
        let mut rust_eigenvalue_check_phase_ok = true;
        let mut python_eigenvalue_check_phase_ok = true;
        let mut loadings_comparison_phase_ok = true;
        let mut scores_comparison_phase_ok = true;
        let mut eigenvalues_comparison_phase_ok = true;

        let num_samples = 50; // N
        let num_true_rank_snps = 20; // True rank of D
        let num_total_snps = 150; // D (num_features)
        let k_components_requested = 30; // k_requested > num_true_rank_snps

        let mut raw_genos = Array2::<f32>::zeros((num_total_snps, num_samples));
        let mut rng = ChaCha8Rng::seed_from_u64(321);
        for r in 0..num_true_rank_snps {
            for c in 0..num_samples {
                raw_genos[[r, c]] = rng.sample(Uniform::new(0.0, 3.0));
            }
        }
        // Create linear dependencies for SNPs beyond num_true_rank_snps
        for r in num_true_rank_snps..num_total_snps {
            let source_row_idx = r % num_true_rank_snps; // pick a source row from the true rank block
            let factor = rng.sample(Uniform::new(0.3, 0.7));
            let noise: f32 = rng.sample(Uniform::new(-0.01, 0.01)); // Small noise
            for c in 0..num_samples {
                raw_genos[[r, c]] = raw_genos[[source_row_idx, c]] * factor + noise;
            }
        }

        let standardized_genos = standardize_features_across_samples(raw_genos.clone());
        let test_data = TestDataAccessor::new(standardized_genos.clone());

        let config = EigenSNPCoreAlgorithmConfig {
            target_num_global_pcs: k_components_requested,
            components_per_ld_block: num_total_snps.min(num_samples),
            random_seed: 321,
            subset_factor_for_local_basis_learning: 1.0,
            min_subset_size_for_local_basis_learning: num_samples.max(1), // Ensure at least 1
            max_subset_size_for_local_basis_learning: num_samples.max(10), // Ensure at least 10
            ..Default::default()
        };
        let algorithm = EigenSNPCoreAlgorithm::new(config);
        let ld_blocks = vec![LdBlockSpecification {
            user_defined_block_tag: "block1".to_string(),
            pca_snp_ids_in_block: (0..num_total_snps).map(PcaSnpId).collect(),
        }];

        let snp_metadata = create_dummy_snp_metadata(num_total_snps);
        let rust_output_result_tuple = algorithm.compute_pca(&test_data, &ld_blocks, &snp_metadata);

        let rust_output = match rust_output_result_tuple {
            Ok((output, _)) => {
                outcome_details.push_str("Rust PCA computation: SUCCESS. ");
                output
            }
            Err(e) => {
                rust_pca_computation_phase_ok = false;
                overall_test_successful = false;
                outcome_details.push_str(&format!("Rust PCA computation: FAILED. Error: {}. ", e));
                // Create a dummy output to allow logging and avoid panics before logging
                EigenSNPCoreOutput {
                    // Now directly in scope due to the import change
                    final_snp_principal_component_loadings: Array2::zeros((0, 0)),
                    final_sample_principal_component_scores: Array2::zeros((0, 0)),
                    final_principal_component_eigenvalues: Array1::zeros(0),
                    num_principal_components_computed: 0,
                    num_pca_snps_used: num_total_snps, // Ensure num_total_snps is in scope
                    num_qc_samples_used: num_samples,  // Ensure num_samples is in scope
                }
            }
        };
        let effective_k_rust = rust_output.num_principal_components_computed;

        let mut py_loadings_k_x_d: Array2<f32> = Array2::zeros((0, 0)); // D x K after transpose
        let mut py_scores_n_x_k: Array2<f32> = Array2::zeros((0, 0));
        let mut py_eigenvalues_k: Array1<f64> = Array1::zeros(0);
        let mut effective_k_py = 0;
        let mut python_script_phase_ok = true; // Assume success, set to false on error

        match get_python_reference_pca(
            &standardized_genos,
            k_components_requested,
            "pca_low_rank_D_gt_N_py_ref",
        ) {
            Ok((loadings_from_py, scores_from_py, eigenvalues_from_py)) => {
                py_loadings_k_x_d = loadings_from_py; // This is K x D from pca.py
                py_scores_n_x_k = scores_from_py;
                py_eigenvalues_k = eigenvalues_from_py;
                effective_k_py = py_eigenvalues_k.len();
                outcome_details.push_str(&format!(
                    "Python script execution: SUCCESS. Rust k_eff: {}, Py k_eff: {}. ",
                    effective_k_rust, effective_k_py
                ));
            }
            Err(e) => {
                python_script_phase_ok = false;
                outcome_details
                    .push_str(&format!("Python script execution: FAILED. Error: {}. ", e));
                // py_loadings, py_scores, py_eigenvalues remain as zeros, effective_k_py is 0
            }
        }
        overall_test_successful &= python_script_phase_ok;

        // Rust Eigenvalue Check Phase
        if overall_test_successful && rust_pca_computation_phase_ok {
            // Only if Rust PCA ran
            if effective_k_rust > num_true_rank_snps {
                let mut all_rust_eigenvalues_small = true;
                for i in num_true_rank_snps..effective_k_rust {
                    if rust_output.final_principal_component_eigenvalues[i] > 1e-3 {
                        all_rust_eigenvalues_small = false;
                        rust_eigenvalue_check_phase_ok = false;
                        outcome_details.push_str(&format!("Rust Eigenvalue Check: FAILED. PC {} ({}) beyond true rank ({}) is too large ({}). ",
                            i, rust_output.final_principal_component_eigenvalues[i], num_true_rank_snps, 1e-3));
                        break;
                    }
                }
                if all_rust_eigenvalues_small {
                    outcome_details.push_str(
                        "Rust Eigenvalue Check: SUCCESS (eigenvalues beyond true rank are small). ",
                    );
                }
            } else {
                outcome_details.push_str(
                    "Rust Eigenvalue Check: SKIPPED (k_eff_rust <= num_true_rank_snps). ",
                );
            }
        } else if rust_pca_computation_phase_ok {
            // If Rust PCA ran but overall test failed before this
            outcome_details.push_str("Rust Eigenvalue Check: SKIPPED (prior failure). ");
        }
        overall_test_successful &= rust_eigenvalue_check_phase_ok;

        // Python Eigenvalue Check Phase
        if overall_test_successful && python_script_phase_ok {
            // Only if Python script ran and parsed
            if effective_k_py > 0 && effective_k_py > num_true_rank_snps {
                let mut all_py_eigenvalues_small = true;
                for i in num_true_rank_snps..effective_k_py {
                    if py_eigenvalues_k.get(i).map_or(false, |&val| val > 1e-3) {
                        all_py_eigenvalues_small = false;
                        python_eigenvalue_check_phase_ok = false;
                        outcome_details.push_str(&format!("Python Eigenvalue Check: FAILED. PC {} ({}) beyond true rank ({}) is too large ({}). ",
                            i, py_eigenvalues_k.get(i).unwrap_or(&0.0), num_true_rank_snps, 1e-3));
                        break;
                    }
                }
                if all_py_eigenvalues_small {
                    outcome_details.push_str("Python Eigenvalue Check: SUCCESS (eigenvalues beyond true rank are small). ");
                }
            } else {
                outcome_details.push_str("Python Eigenvalue Check: SKIPPED (k_eff_py <= num_true_rank_snps or k_eff_py is 0). ");
            }
        } else if python_script_phase_ok {
            // If python script ran but overall test failed
            outcome_details.push_str("Python Eigenvalue Check: SKIPPED (prior failure). ");
        }
        overall_test_successful &= python_eigenvalue_check_phase_ok;

        let py_loadings_d_x_k = py_loadings_k_x_d.t().into_owned();

        let artifact_dir = "target/test_artifacts/pca_low_rank_D_gt_N";
        if rust_output.num_principal_components_computed > 0 {
            // Save only if there's something to save
            save_matrix_to_tsv(
                &rust_output.final_snp_principal_component_loadings.view(),
                artifact_dir,
                "rust_loadings.tsv",
            )
            .expect("Failed to save rust_loadings.tsv");
            save_matrix_to_tsv(
                &rust_output.final_sample_principal_component_scores.view(),
                artifact_dir,
                "rust_scores.tsv",
            )
            .expect("Failed to save rust_scores.tsv");
            save_vector_to_tsv(
                &rust_output.final_principal_component_eigenvalues.view(),
                artifact_dir,
                "rust_eigenvalues.tsv",
            )
            .expect("Failed to save rust_eigenvalues.tsv");
        }
        if effective_k_py > 0 {
            // Save Python results only if they exist
            save_matrix_to_tsv(
                &py_loadings_d_x_k.view(),
                artifact_dir,
                "python_loadings.tsv",
            )
            .expect("Failed to save python_loadings.tsv");
            save_matrix_to_tsv(&py_scores_n_x_k.view(), artifact_dir, "python_scores.tsv")
                .expect("Failed to save python_scores.tsv");
            save_vector_to_tsv(
                &py_eigenvalues_k.view(),
                artifact_dir,
                "python_eigenvalues.tsv",
            )
            .expect("Failed to save python_eigenvalues.tsv");
        }

        let record = TestResultRecord {
            test_name: "test_pca_more_components_requested_than_rank_D_gt_N".to_string(),
            num_features_d: num_total_snps,
            num_samples_n: num_samples,
            num_pcs_requested_k: k_components_requested,
            num_pcs_computed: effective_k_rust,
            success: overall_test_successful, // Use the aggregated success status
            outcome_details: outcome_details.clone(),
            notes,
        };
        TEST_RESULTS.lock().unwrap().push(record);

        // Detailed comparisons only if all preceding critical phases were successful
        if overall_test_successful {
            let num_pcs_to_compare = effective_k_rust
                .min(effective_k_py)
                .min(num_true_rank_snps + 2)
                .min(k_components_requested);

            if num_pcs_to_compare > 0 {
                // Loadings Comparison
                let loadings_comparison_result = std::panic::catch_unwind(|| {
                    assert_f32_arrays_are_close_with_sign_flips(
                        rust_output
                            .final_snp_principal_component_loadings
                            .slice(s![.., 0..num_pcs_to_compare]),
                        py_loadings_d_x_k.slice(s![.., 0..num_pcs_to_compare]),
                        1.5f32,
                        "SNP Loadings (Low-Rank D>N)",
                    );
                });
                if loadings_comparison_result.is_err() {
                    loadings_comparison_phase_ok = false;
                    outcome_details
                        .push_str("Loadings Comparison: FAILED. Details captured by assert. ");
                } else {
                    outcome_details.push_str("Loadings Comparison: SUCCESS. ");
                }
                overall_test_successful &= loadings_comparison_phase_ok;

                // Scores Comparison
                if overall_test_successful {
                    // Proceed only if previous was ok
                    let scores_comparison_result = std::panic::catch_unwind(|| {
                        assert_f32_arrays_are_close_with_sign_flips(
                            rust_output
                                .final_sample_principal_component_scores
                                .slice(s![.., 0..num_pcs_to_compare]),
                            py_scores_n_x_k.slice(s![.., 0..num_pcs_to_compare]),
                            DEFAULT_FLOAT_TOLERANCE_F32 * 10.0,
                            "Sample Scores (Low-Rank D>N)",
                        );
                    });
                    if scores_comparison_result.is_err() {
                        scores_comparison_phase_ok = false;
                        outcome_details
                            .push_str("Scores Comparison: FAILED. Details captured by assert. ");
                    } else {
                        outcome_details.push_str("Scores Comparison: SUCCESS. ");
                    }
                    overall_test_successful &= scores_comparison_phase_ok;
                }

                // Eigenvalues Comparison
                if overall_test_successful {
                    // Proceed only if previous was ok
                    let eigenvalues_comparison_result = std::panic::catch_unwind(|| {
                        assert_f64_arrays_are_close(
                            rust_output
                                .final_principal_component_eigenvalues
                                .slice(s![0..num_pcs_to_compare]),
                            py_eigenvalues_k.slice(s![0..num_pcs_to_compare]),
                            DEFAULT_FLOAT_TOLERANCE_F64 * 10.0,
                            "Eigenvalues (Low-Rank D>N)",
                        );
                    });
                    if eigenvalues_comparison_result.is_err() {
                        eigenvalues_comparison_phase_ok = false;
                        outcome_details.push_str(
                            "Eigenvalues Comparison: FAILED. Details captured by assert. ",
                        );
                    } else {
                        outcome_details.push_str("Eigenvalues Comparison: SUCCESS. ");
                    }
                    overall_test_successful &= eigenvalues_comparison_phase_ok;
                }
            } else {
                // num_pcs_to_compare is 0
                outcome_details
                    .push_str("Detailed Comparisons: SKIPPED (num_pcs_to_compare is 0). ");
                if effective_k_rust != effective_k_py {
                    // If one is 0 and other is not
                    overall_test_successful = false; // This is a failure if we expected components
                    outcome_details.push_str(&format!(
                        "Effective k mismatch (Rust: {}, Py: {}), leading to 0 comparable PCs. ",
                        effective_k_rust, effective_k_py
                    ));
                }
            }
            // Update the success status in the record again if detailed comparisons failed
            if !overall_test_successful {
                TEST_RESULTS
                    .lock()
                    .unwrap()
                    .last_mut()
                    .map(|rec| rec.success = false);
                TEST_RESULTS
                    .lock()
                    .unwrap()
                    .last_mut()
                    .map(|rec| rec.outcome_details = outcome_details.clone());
            }
        } else {
            // overall_test_successful was false before detailed comparisons
            outcome_details
                .push_str("Detailed Comparisons: SKIPPED (due to prior phase failures). ");
            TEST_RESULTS
                .lock()
                .unwrap()
                .last_mut()
                .map(|rec| rec.outcome_details = outcome_details.clone());
        }
        assert!(
            overall_test_successful,
            "Test 'test_pca_more_components_requested_than_rank_D_gt_N' failed. Details: {}",
            outcome_details
        );
    }
}

// Helper function for Pearson Correlation
pub fn pearson_correlation(v1: ArrayView1<f32>, v2: ArrayView1<f32>) -> Option<f32> {
    if v1.len() != v2.len() || v1.is_empty() {
        return None;
    }
    let _n = v1.len() as f32;
    let mean1 = v1.mean().unwrap_or(0.0);
    let mean2 = v2.mean().unwrap_or(0.0);
    let mut cov = 0.0;
    let mut std_dev1_sq = 0.0;
    let mut std_dev2_sq = 0.0;
    for i in 0..v1.len() {
        let d1 = v1[i] - mean1;
        let d2 = v2[i] - mean2;
        cov += d1 * d2;
        std_dev1_sq += d1 * d1;
        std_dev2_sq += d2 * d2;
    }
    if std_dev1_sq <= 1e-6 || std_dev2_sq <= 1e-6 {
        // Avoid division by zero or near-zero
        // If one vector is constant, correlation is undefined or can be taken as 0
        // For PCA components, they shouldn't be constant if eigenvalues are non-zero.
        // If both are constant and identical, correlation is 1.
        if (std_dev1_sq - std_dev2_sq).abs() < 1e-6 && cov.abs() < 1e-6 {
            let mut all_v1_same = true;
            let mut all_v2_same = true;
            if v1.len() > 1 {
                for i in 1..v1.len() {
                    if (v1[i] - v1[0]).abs() > 1e-6 {
                        all_v1_same = false;
                        break;
                    }
                    if (v2[i] - v2[0]).abs() > 1e-6 {
                        all_v2_same = false;
                        break;
                    }
                }
            }
            if all_v1_same && all_v2_same && (v1[0] - v2[0]).abs() < 1e-6 {
                return Some(1.0);
            }
        }
        return None;
    }
    Some(cov / (std_dev1_sq.sqrt() * std_dev2_sq.sqrt()))
}

// Helper function for PC correlation tests
pub fn run_pc_correlation_with_truth_set_test(
    test_name_str: &str,
    num_snps: usize,     // D
    num_samples: usize,  // N
    k_components: usize, // k
    seed: u64,
) {
    let test_name = test_name_str.to_string();
    let mut test_successful = true;
    let mut outcome_details = String::new();
    let mut notes = format!(
        "Matrix D_snps x N_samples: {}x{}, k_requested: {}. ",
        num_snps, num_samples, k_components
    );

    let mut rng = ChaCha8Rng::seed_from_u64(seed);
    let raw_genos = Array2::random_using((num_snps, num_samples), Uniform::new(0.0, 3.0), &mut rng);
    let standardized_genos_snps_x_samples = standardize_features_across_samples(raw_genos.clone());

    let artifact_dir_suffix = format!(
        "pc_correlation_{}x{}_k{}",
        num_snps, num_samples, k_components
    );
    let artifact_dir = Path::new("target/test_artifacts").join(artifact_dir_suffix);
    if let Err(e) = fs::create_dir_all(&artifact_dir) {
        notes.push_str(&format!("Failed to create artifact dir: {}. ", e));
    }

    // Get Truth PCs from pca.py by calling the new helper function
    let mut py_loadings_d_x_k: Array2<f32> = Array2::zeros((0, 0)); // D x K
    let mut py_scores_n_x_k: Array2<f32> = Array2::zeros((0, 0)); // N x K
                                                                  // let mut py_eigenvalues_k: Array1<f64> = Array1::zeros(0); // K
    let mut effective_k_py = 0;

    let python_pca_result = get_python_reference_pca(
        &standardized_genos_snps_x_samples,
        k_components,
        &format!(
            "pc_correlation_{}x{}_k{}_py_ref",
            num_snps, num_samples, k_components
        ),
    );

    match python_pca_result {
        Ok((loadings_k_x_d_from_py, scores_from_py, _eigenvalues_from_py)) => {
            // pca.py returns loadings as K x D, scores as N x K, eigenvalues as K
            // We need D x K for loadings to match Rust output.
            py_loadings_d_x_k = loadings_k_x_d_from_py.t().into_owned();
            py_scores_n_x_k = scores_from_py;
            // py_eigenvalues_k = _eigenvalues_from_py; // Not directly used for correlation here
            effective_k_py = py_loadings_d_x_k.ncols(); // D x K, so ncols is K

            save_matrix_to_tsv(
                &py_loadings_d_x_k.view(),
                artifact_dir.to_str().unwrap_or("."),
                "python_loadings.tsv",
            )
            .unwrap_or_default();
            save_matrix_to_tsv(
                &py_scores_n_x_k.view(),
                artifact_dir.to_str().unwrap_or("."),
                "python_scores.tsv",
            )
            .unwrap_or_default();
            outcome_details.push_str(&format!(
                "Python PCA successful. effective_k_py: {}. ",
                effective_k_py
            ));
        }
        Err(e) => {
            test_successful = false;
            outcome_details.push_str(&format!("Python reference PCA failed: {}. ", e));
            // effective_k_py remains 0, arrays remain empty.
        }
    }

    // Run eigensnp
    let test_data_accessor = TestDataAccessor::new(standardized_genos_snps_x_samples.clone());
    let config = EigenSNPCoreAlgorithmConfig {
        target_num_global_pcs: k_components,
        random_seed: seed,
        subset_factor_for_local_basis_learning: 0.5,
        min_subset_size_for_local_basis_learning: (num_samples / 4).max(1).min(num_samples.max(1)),
        max_subset_size_for_local_basis_learning: (num_samples / 2).max(10).min(num_samples.max(1)),
        components_per_ld_block: 10
            .min(num_snps.min((num_samples / 2).max(10).min(num_samples.max(1)))),
        ..Default::default()
    };
    let algorithm = EigenSNPCoreAlgorithm::new(config);
    let ld_blocks = vec![LdBlockSpecification {
        user_defined_block_tag: "block1".to_string(),
        pca_snp_ids_in_block: (0..num_snps).map(PcaSnpId).collect(),
    }];

    let mut rust_pcs_computed = 0;
    let snp_metadata = create_dummy_snp_metadata(num_snps);
    match algorithm.compute_pca(&test_data_accessor, &ld_blocks, &snp_metadata) {
        Ok((rust_result, _)) => {
            rust_pcs_computed = rust_result.num_principal_components_computed;
            save_matrix_to_tsv(
                &rust_result.final_snp_principal_component_loadings.view(),
                artifact_dir.to_str().unwrap_or("."),
                "rust_loadings.tsv",
            )
            .unwrap_or_default();
            save_matrix_to_tsv(
                &rust_result.final_sample_principal_component_scores.view(),
                artifact_dir.to_str().unwrap_or("."),
                "rust_scores.tsv",
            )
            .unwrap_or_default();

            if test_successful {
                // Only compare if Python part was also successful
                let k_to_compare = rust_result
                    .num_principal_components_computed
                    .min(effective_k_py);
                if k_to_compare == 0 {
                    outcome_details
                        .push_str("No components to compare (Rust or Python computed 0 PCs). ");
                    if rust_result.num_principal_components_computed != effective_k_py {
                        // If one is 0 and other is not
                        test_successful = false;
                        outcome_details.push_str(&format!(
                            "Mismatch in computed k (Rust: {}, Py: {}). ",
                            rust_result.num_principal_components_computed, effective_k_py
                        ));
                    }
                } else {
                    let mut min_loading_abs_corr = 1.0f32;
                    let mut min_score_abs_corr = 1.0f32;
                    let mut correlations_summary = String::new();

                    for pc_idx in 0..k_to_compare {
                        let rust_loading_col = rust_result
                            .final_snp_principal_component_loadings
                            .column(pc_idx);
                        let py_loading_col = py_loadings_d_x_k.column(pc_idx);
                        let loading_corr =
                            pearson_correlation(rust_loading_col.view(), py_loading_col.view())
                                .map_or(0.0, |c| c.abs());
                        if loading_corr < min_loading_abs_corr {
                            min_loading_abs_corr = loading_corr;
                        }
                        correlations_summary
                            .push_str(&format!("PC{}_Load_absR={:.4}; ", pc_idx, loading_corr));
                        if loading_corr < 0.95 {
                            test_successful = false;
                            outcome_details.push_str(&format!(
                                "Low loading correlation for PC {}: {:.4}. ",
                                pc_idx, loading_corr
                            ));
                        }

                        let rust_score_col = rust_result
                            .final_sample_principal_component_scores
                            .column(pc_idx);
                        let py_score_col = py_scores_n_x_k.column(pc_idx);
                        let score_corr =
                            pearson_correlation(rust_score_col.view(), py_score_col.view())
                                .map_or(0.0, |c| c.abs());
                        if score_corr < min_score_abs_corr {
                            min_score_abs_corr = score_corr;
                        }
                        correlations_summary
                            .push_str(&format!("PC{}_Score_absR={:.4}; ", pc_idx, score_corr));
                        if score_corr < 0.95 {
                            test_successful = false;
                            outcome_details.push_str(&format!(
                                "Low score correlation for PC {}: {:.4}. ",
                                pc_idx, score_corr
                            ));
                        }
                    }
                    outcome_details.push_str(&format!(
                        "Compared {} PCs. Min loading_absR={:.4}, Min score_absR={:.4}. Full: {}. ",
                        k_to_compare,
                        min_loading_abs_corr,
                        min_score_abs_corr,
                        correlations_summary.trim_end_matches("; ")
                    ));
                }
            }
        }
        Err(e) => {
            test_successful = false;
            outcome_details.push_str(&format!("Rust PCA computation failed: {}. ", e));
        }
    }

    let record = TestResultRecord {
        test_name,
        num_features_d: num_snps,
        num_samples_n: num_samples,
        num_pcs_requested_k: k_components,
        num_pcs_computed: rust_pcs_computed,
        success: test_successful,
        outcome_details: outcome_details.clone(),
        notes,
    };
    TEST_RESULTS.lock().unwrap().push(record);

    assert!(
        test_successful,
        "Test '{}' failed. Check TSV. Details: {}",
        test_name_str, outcome_details
    );
}

#[test]
fn test_pc_correlation_with_truth_set_large_1000x200() {
    run_pc_correlation_with_truth_set_test(
        "test_pc_correlation_with_truth_set_large_1000x200",
        1000,   // num_snps (D)
        200,    // num_samples (N)
        10,     // k_components
        202401, // seed
    );
}

#[test]
fn test_pc_correlation_structured_1000snps_200samples_5truepcs() {
    // 1. Define parameters
    let num_snps = 1000; // D
    let num_samples = 200; // N
    let k_components_to_request = 10; // k_request
    let seed = 202408;
    let num_true_pcs = 5; // K_true
    let signal_strength = 5.0;
    let noise_std_dev = 1.0;

    // 2. Initialize TestResultRecord variables
    let test_name = "test_pc_correlation_structured_1000snps_200samples_5truepcs".to_string();
    let mut test_successful = true;
    let mut outcome_details = String::new();
    let mut notes = format!(
        "Structured Data Test: D_snps={}, N_samples={}, k_requested={}, k_true={}. ",
        num_snps, num_samples, k_components_to_request, num_true_pcs
    );

    // 3. Generate structured data
    let structured_standardized_genos_snps_x_samples = generate_structured_data(
        num_snps,
        num_samples,
        num_true_pcs,
        signal_strength,
        noise_std_dev,
        seed,
    );

    // 4. Set up an artifact directory
    let artifact_dir_suffix = format!(
        "pc_corr_structured_{}x{}_k{}_true{}",
        num_snps, num_samples, k_components_to_request, num_true_pcs
    );
    let artifact_dir = Path::new("target/test_artifacts").join(artifact_dir_suffix);
    if let Err(e) = fs::create_dir_all(&artifact_dir) {
        notes.push_str(&format!("Failed to create artifact dir: {}. ", e));
        // Depending on policy, might set test_successful = false here or let subsequent ops fail
    }

    // 5. Python Reference PCA
    let mut py_loadings_d_x_k: Array2<f32> = Array2::zeros((0, 0)); // D x K
    let mut py_scores_n_x_k: Array2<f32> = Array2::zeros((0, 0)); // N x K
    let mut _py_eigenvalues_k: Array1<f64> = Array1::zeros(0); // K
    let mut effective_k_py = 0;

    let python_pca_prefix = format!(
        "pc_corr_structured_{}x{}_k{}_true{}_py_ref",
        num_snps, num_samples, k_components_to_request, num_true_pcs
    );
    match get_python_reference_pca(
        &structured_standardized_genos_snps_x_samples,
        k_components_to_request,
        &python_pca_prefix,
    ) {
        Ok((loadings_k_x_d_py, scores_n_x_k_py, eigenvalues_k_py)) => {
            // pca.py returns Loadings: KxC, Scores: RxC, Eigenvalues: Vector (C is num components, R is num samples, K is num features)
            // For our Rust code, Loadings are D_snps x K_components.
            // pca.py output for loadings is K_components x D_snps. So we need to transpose it.
            py_loadings_d_x_k = loadings_k_x_d_py.t().into_owned(); // D x K
            py_scores_n_x_k = scores_n_x_k_py; // N x K
            _py_eigenvalues_k = eigenvalues_k_py; // K
            effective_k_py = py_loadings_d_x_k.ncols(); // Number of components computed by Python
            outcome_details.push_str(&format!(
                "Python PCA successful. effective_k_py: {}. ",
                effective_k_py
            ));
            save_matrix_to_tsv(
                &py_loadings_d_x_k.view(),
                artifact_dir.to_str().unwrap_or("."),
                "python_loadings.tsv",
            )
            .unwrap_or_default();
            save_matrix_to_tsv(
                &py_scores_n_x_k.view(),
                artifact_dir.to_str().unwrap_or("."),
                "python_scores.tsv",
            )
            .unwrap_or_default();
            save_vector_to_tsv(
                &_py_eigenvalues_k.view(),
                artifact_dir.to_str().unwrap_or("."),
                "python_eigenvalues.tsv",
            )
            .unwrap_or_default();
        }
        Err(e) => {
            test_successful = false;
            outcome_details.push_str(&format!("Python reference PCA failed: {}. ", e));
            // effective_k_py remains 0, arrays remain empty.
        }
    }

    // 6. eigensnp Execution
    let test_data_accessor =
        TestDataAccessor::new(structured_standardized_genos_snps_x_samples.clone());

    // Correctly calculate these parameters based on num_samples and num_snps
    let min_subset_size = (num_samples / 4).max(1).min(num_samples.max(1));
    let max_subset_size = (num_samples / 2).max(10).min(num_samples.max(1));
    let components_per_block = 10.min(num_snps.min(max_subset_size));

    let config = EigenSNPCoreAlgorithmConfig {
        target_num_global_pcs: k_components_to_request,
        random_seed: seed,
        subset_factor_for_local_basis_learning: 0.5, // As per issue
        min_subset_size_for_local_basis_learning: min_subset_size,
        max_subset_size_for_local_basis_learning: max_subset_size,
        components_per_ld_block: components_per_block,
        ..Default::default()
    };

    let algorithm = EigenSNPCoreAlgorithm::new(config);
    let ld_blocks = vec![LdBlockSpecification {
        user_defined_block_tag: "block_structured".to_string(),
        pca_snp_ids_in_block: (0..num_snps).map(PcaSnpId).collect(),
    }];

    let mut rust_pcs_computed = 0;
    let snp_metadata = create_dummy_snp_metadata(num_snps);
    match algorithm.compute_pca(&test_data_accessor, &ld_blocks, &snp_metadata) {
        Ok((rust_result, _)) => {
            rust_pcs_computed = rust_result.num_principal_components_computed;
            outcome_details.push_str(&format!(
                "eigensnp PCA successful. rust_pcs_computed: {}. ",
                rust_pcs_computed
            ));
            save_matrix_to_tsv(
                &rust_result.final_snp_principal_component_loadings.view(),
                artifact_dir.to_str().unwrap_or("."),
                "rust_loadings.tsv",
            )
            .unwrap_or_default();
            save_matrix_to_tsv(
                &rust_result.final_sample_principal_component_scores.view(),
                artifact_dir.to_str().unwrap_or("."),
                "rust_scores.tsv",
            )
            .unwrap_or_default();
            save_vector_to_tsv(
                &rust_result.final_principal_component_eigenvalues.view(),
                artifact_dir.to_str().unwrap_or("."),
                "rust_eigenvalues.tsv",
            )
            .unwrap_or_default();

            // 7. Comparison and Assertions
            if test_successful {
                // Only compare if Python part was also successful and Rust part is successful
                // Determine k_to_compare: min of Rust computed, Python computed, and slightly more than true PCs for detailed check
                let k_to_compare = rust_pcs_computed
                    .min(effective_k_py)
                    .min(num_true_pcs + 2)
                    .min(k_components_to_request);

                outcome_details.push_str(&format!(
                    "Comparing up to {} PCs. True PCs: {}. ",
                    k_to_compare, num_true_pcs
                ));

                if k_to_compare == 0 {
                    outcome_details.push_str(
                        "No components to compare (Rust or Python computed 0 relevant PCs). ",
                    );
                    if rust_pcs_computed != effective_k_py
                        && (rust_pcs_computed == 0 || effective_k_py == 0)
                    {
                        test_successful = false; // If one is 0 and other is not, it's a failure if we expected components
                        outcome_details.push_str(&format!(
                            "Mismatch in computed k (Rust: {}, Py: {}). ",
                            rust_pcs_computed, effective_k_py
                        ));
                    }
                } else {
                    let mut min_true_loading_abs_corr = 1.0f32;
                    let mut min_true_score_abs_corr = 1.0f32;
                    let mut correlations_summary = String::new();

                    for pc_idx in 0..k_to_compare {
                        let rust_loading_col = rust_result
                            .final_snp_principal_component_loadings
                            .column(pc_idx);
                        let py_loading_col = py_loadings_d_x_k.column(pc_idx);
                        let loading_corr_opt =
                            pearson_correlation(rust_loading_col.view(), py_loading_col.view());
                        let loading_abs_corr = loading_corr_opt.map_or(0.0, |c| c.abs());

                        correlations_summary
                            .push_str(&format!("PC{}_Load_absR={:.4}; ", pc_idx, loading_abs_corr));

                        let rust_score_col = rust_result
                            .final_sample_principal_component_scores
                            .column(pc_idx);
                        let py_score_col = py_scores_n_x_k.column(pc_idx);
                        let score_corr_opt =
                            pearson_correlation(rust_score_col.view(), py_score_col.view());
                        let score_abs_corr = score_corr_opt.map_or(0.0, |c| c.abs());

                        correlations_summary
                            .push_str(&format!("PC{}_Score_absR={:.4}; ", pc_idx, score_abs_corr));

                        if pc_idx < num_true_pcs {
                            // Stricter check for true components
                            if loading_abs_corr < min_true_loading_abs_corr {
                                min_true_loading_abs_corr = loading_abs_corr;
                            }
                            if score_abs_corr < min_true_score_abs_corr {
                                min_true_score_abs_corr = score_abs_corr;
                            }

                            if loading_abs_corr < 0.98 {
                                // High threshold for true PCs
                                test_successful = false;
                                outcome_details.push_str(&format!(
                                    "Low loading correlation for true PC {}: {:.4}. ",
                                    pc_idx, loading_abs_corr
                                ));
                            }
                            if score_abs_corr < 0.98 {
                                // High threshold for true PCs
                                test_successful = false;
                                outcome_details.push_str(&format!(
                                    "Low score correlation for true PC {}: {:.4}. ",
                                    pc_idx, score_abs_corr
                                ));
                            }
                        } else {
                            // More lenient for PCs beyond num_true_pcs (noise or mixed)
                            // Could have a more lenient threshold here if needed, e.g. > 0.8 or just log
                            if loading_abs_corr < 0.70 {
                                // Not necessarily a failure, but good to note if unexpected.
                                notes.push_str(&format!(
                                    "Note: Loading correlation for non-true PC {} is {:.4}. ",
                                    pc_idx, loading_abs_corr
                                ));
                            }
                            if score_abs_corr < 0.70 {
                                notes.push_str(&format!(
                                    "Note: Score correlation for non-true PC {} is {:.4}. ",
                                    pc_idx, score_abs_corr
                                ));
                            }
                        }
                    }
                    outcome_details.push_str(&format!(
                        "For first {} true PCs: Min_Load_absR={:.4}, Min_Score_absR={:.4}. Full_Corr_Summary: {}. ",
                        num_true_pcs.min(k_to_compare), // Ensure we don't claim more true PCs than compared
                        min_true_loading_abs_corr,
                        min_true_score_abs_corr,
                        correlations_summary.trim_end_matches("; ")
                    ));
                }
            }
        }
        Err(e) => {
            test_successful = false;
            outcome_details.push_str(&format!("eigensnp PCA computation failed: {}. ", e));
            // rust_pcs_computed remains 0, which is fine for logging
        }
    }

    // 8. Logging
    let record = TestResultRecord {
        test_name: test_name.clone(),
        num_features_d: num_snps,
        num_samples_n: num_samples,
        num_pcs_requested_k: k_components_to_request,
        num_pcs_computed: rust_pcs_computed, // Rust's computed PCs
        success: test_successful,
        outcome_details: outcome_details.clone(),
        notes,
    };
    TEST_RESULTS.lock().unwrap().push(record);

    // 9. Final Assertion
    assert!(
        test_successful,
        "Test '{}' failed. Check artifacts in '{}'. Details: {}",
        test_name,
        artifact_dir.display(),
        outcome_details
    );
}

// Helper function for generic large matrix execution tests
pub fn run_generic_large_matrix_test(
    test_name_str: &str,
    num_snps: usize,     // D
    num_samples: usize,  // N
    k_components: usize, // k
    seed: u64,
    // Optional: allow passing a custom config modifier
    config_modifier: Option<fn(EigenSNPCoreAlgorithmConfig) -> EigenSNPCoreAlgorithmConfig>,
) {
    let mut outcome_details = String::new(); // Added this line
    let test_name = test_name_str.to_string();
    let mut test_successful = true;
    let mut notes = format!(
        "Matrix D_snps x N_samples: {}x{}, k_requested: {}. ",
        num_snps, num_samples, k_components
    );

    let artifact_dir_suffix = format!(
        "generic_large_matrix_{}x{}_k{}",
        num_snps, num_samples, k_components
    );
    let artifact_dir = Path::new("target/test_artifacts").join(artifact_dir_suffix);
    if let Err(e) = fs::create_dir_all(&artifact_dir) {
        notes.push_str(&format!("Failed to create artifact dir: {}. ", e));
    }

    let mut rng = ChaCha8Rng::seed_from_u64(seed);
    let raw_genos = Array2::random_using((num_snps, num_samples), Uniform::new(0.0, 3.0), &mut rng);
    let standardized_genos = standardize_features_across_samples(raw_genos);

    let test_data_accessor = TestDataAccessor::new(standardized_genos);

    let mut base_config = EigenSNPCoreAlgorithmConfig {
        target_num_global_pcs: k_components,
        random_seed: seed,
        ..Default::default()
    };

    if let Some(modifier) = config_modifier {
        base_config = modifier(base_config);
        notes.push_str("Custom config modifier applied. ");
    }

    let algorithm = EigenSNPCoreAlgorithm::new(base_config);
    let ld_blocks = vec![LdBlockSpecification {
        user_defined_block_tag: "block_generic".to_string(),
        pca_snp_ids_in_block: (0..num_snps).map(PcaSnpId).collect(),
    }];

    let mut rust_pcs_computed = 0;

    let snp_metadata = create_dummy_snp_metadata(num_snps);
    match algorithm.compute_pca(&test_data_accessor, &ld_blocks, &snp_metadata) {
        Ok((output, _)) => {
            rust_pcs_computed = output.num_principal_components_computed;
            write!(
                &mut outcome_details,
                "eigensnp successful. Computed {} PCs. First eigenvalue: {:.4}. ",
                output.num_principal_components_computed,
                output
                    .final_principal_component_eigenvalues
                    .get(0)
                    .unwrap_or(&0.0)
            )
            .unwrap_or_default();
            if rust_pcs_computed == 0 && k_components > 0 {
                test_successful = false;
                // Use write! for appending, though push_str is also fine if not formatting.
                write!(
                    &mut outcome_details,
                    "Warning: 0 PCs computed when k_requested > 0. "
                )
                .unwrap_or_default();
            }
            if rust_pcs_computed > k_components {
                test_successful = false;
                write!(
                    &mut outcome_details,
                    "Warning: More PCs computed ({}) than requested ({}). ",
                    rust_pcs_computed, k_components
                )
                .unwrap_or_default();
            }
            // Save outputs
            save_matrix_to_tsv(
                &output.final_snp_principal_component_loadings.view(),
                artifact_dir.to_str().unwrap_or("."),
                "rust_loadings.tsv",
            )
            .unwrap_or_default();
            save_matrix_to_tsv(
                &output.final_sample_principal_component_scores.view(),
                artifact_dir.to_str().unwrap_or("."),
                "rust_scores.tsv",
            )
            .unwrap_or_default();
            save_vector_to_tsv(
                &output.final_principal_component_eigenvalues.view(),
                artifact_dir.to_str().unwrap_or("."),
                "rust_eigenvalues.tsv",
            )
            .unwrap_or_default();
        }
        Err(e) => {
            test_successful = false;
            write!(
                &mut outcome_details,
                "eigensnp PCA computation failed: {}. ",
                e
            )
            .unwrap_or_default();
        }
    }

    let record = TestResultRecord {
        test_name,
        num_features_d: num_snps,
        num_samples_n: num_samples,
        num_pcs_requested_k: k_components,
        num_pcs_computed: rust_pcs_computed,
        success: test_successful,
        outcome_details: outcome_details.clone(),
        notes,
    };
    TEST_RESULTS.lock().unwrap().push(record);

    assert!(
        test_successful,
        "Test '{}' failed. Check TSV. Details: {}",
        test_name_str, outcome_details
    );
}

#[test]
fn test_large_matrix_2000x200_k10() {
    run_generic_large_matrix_test(
        "test_large_matrix_2000x200_k10",
        2000,   // num_snps
        200,    // num_samples
        10,     // k_components
        202405, // seed
        None,
    );
}

#[test]
fn test_large_matrix_5000x500_k20() {
    run_generic_large_matrix_test(
        "test_large_matrix_5000x500_k20",
        5000,   // num_snps
        500,    // num_samples
        20,     // k_components
        202406, // seed
        None,
    );
}

#[test]
fn test_large_matrix_1000x100_k5_blocksize_variation() {
    run_generic_large_matrix_test(
        "test_large_matrix_1000x100_k5_blocksize_variation",
        1000,   // num_snps
        100,    // num_samples
        5,      // k_components
        202407, // seed
        Some(|mut cfg: EigenSNPCoreAlgorithmConfig| {
            cfg.components_per_ld_block = 20; // Example variation
            cfg.subset_factor_for_local_basis_learning = 0.8;
            cfg
        }),
    );
}

// Helper function for sample projection accuracy tests
pub fn run_sample_projection_accuracy_test(
    test_name_str: &str,
    num_snps: usize,          // D
    num_samples_total: usize, // N_total
    num_samples_train: usize, // N_train
    k_components: usize,      // k
    seed: u64,
) {
    let test_name = test_name_str.to_string();
    let mut test_successful = true;
    let mut outcome_details: String;
    let num_samples_test = num_samples_total - num_samples_train;
    let mut notes = format!(
        "Matrix D_snps x N_total_samples (N_train_samples / N_test_samples): {}x{} ({} / {}), k_requested: {}. ",
        num_snps, num_samples_total, num_samples_train, num_samples_test, k_components
    );

    assert!(num_samples_train > 0, "num_samples_train must be > 0");
    assert!(
        num_samples_total > num_samples_train,
        "num_samples_total must be > num_samples_train"
    );

    let artifact_dir_suffix = format!(
        "sample_projection_{}x{}_k{}",
        num_snps, num_samples_train, k_components
    );
    let artifact_dir = Path::new("target/test_artifacts").join(artifact_dir_suffix);
    if let Err(e) = fs::create_dir_all(&artifact_dir) {
        notes.push_str(&format!("Failed to create artifact dir: {}. ", e));
    }

    let mut rng = ChaCha8Rng::seed_from_u64(seed);
    let raw_genos_total = Array2::random_using(
        (num_snps, num_samples_total),
        Uniform::new(0.0, 3.0),
        &mut rng,
    );
    let standardized_genos_total_snps_x_samples =
        standardize_features_across_samples(raw_genos_total);

    let train_data_snps_x_samples = standardized_genos_total_snps_x_samples
        .slice(s![.., 0..num_samples_train])
        .to_owned();
    let test_data_snps_x_samples = standardized_genos_total_snps_x_samples
        .slice(s![.., num_samples_train..])
        .to_owned();

    // Run eigensnp on Training Data
    let test_data_accessor_train = TestDataAccessor::new(train_data_snps_x_samples.clone());
    let config_train = EigenSNPCoreAlgorithmConfig {
        target_num_global_pcs: k_components,
        random_seed: seed,
        // Default other params or make them configurable if needed for these tests
        ..Default::default()
    };
    let algorithm_train = EigenSNPCoreAlgorithm::new(config_train);
    let ld_blocks_train = vec![LdBlockSpecification {
        user_defined_block_tag: "block_train".to_string(),
        pca_snp_ids_in_block: (0..num_snps).map(PcaSnpId).collect(),
    }];

    let mut rust_pca_output_option: Option<EigenSNPCoreOutput> = None; // Now directly in scope
    let mut k_eff_rust = 0;

    let snp_metadata = create_dummy_snp_metadata(num_snps);
    match algorithm_train.compute_pca(&test_data_accessor_train, &ld_blocks_train, &snp_metadata) {
        Ok((output_struct, _)) => {
            k_eff_rust = output_struct.num_principal_components_computed;
            save_matrix_to_tsv(
                &output_struct.final_snp_principal_component_loadings.view(),
                artifact_dir.to_str().unwrap_or("."),
                "rust_train_loadings.tsv",
            )
            .unwrap_or_default();
            save_matrix_to_tsv(
                &output_struct.final_sample_principal_component_scores.view(),
                artifact_dir.to_str().unwrap_or("."),
                "rust_train_scores.tsv",
            )
            .unwrap_or_default();
            rust_pca_output_option = Some(output_struct);
            outcome_details = format!(
                "eigensnp on train data successful. k_eff_rust: {}. ",
                k_eff_rust
            );
        }
        Err(e) => {
            test_successful = false;
            outcome_details = format!("eigensnp on train data failed: {}. ", e);
        }
    }

    // Project Test Samples using Rust loadings
    let mut s_projected_option: Option<Array2<f32>> = None;
    if let Some(ref rust_pca_output) = rust_pca_output_option {
        if k_eff_rust > 0 {
            let loadings_l = &rust_pca_output.final_snp_principal_component_loadings; // D x k_eff_rust
            if test_data_snps_x_samples.nrows() == loadings_l.nrows() {
                // D must match
                // (N_test x D) dot (D x k_eff_rust) = N_test x k_eff_rust
                let projected_scores = test_data_snps_x_samples.t().dot(loadings_l);
                save_matrix_to_tsv(
                    &projected_scores.view(),
                    artifact_dir.to_str().unwrap_or("."),
                    "rust_projected_test_scores.tsv",
                )
                .unwrap_or_default();
                s_projected_option = Some(projected_scores);
            } else {
                test_successful = false;
                outcome_details.push_str(&format!("Dimension mismatch for projection: test_data_snps_x_samples.nrows() ({}) != loadings_l.nrows() ({}). ",
                    test_data_snps_x_samples.nrows(), loadings_l.nrows()));
            }
        } else {
            outcome_details.push_str("Rust PCA computed 0 components, skipping projection. ");
        }
    }

    // Get "Truth" Scores for Test Samples using pca.py on total data
    let mut py_test_scores_ref_option: Option<Array2<f32>> = None;

    if test_successful {
        // Only proceed if eigensnp part was okay so far
        let python_total_data_prefix = format!(
            "sample_projection_{}x{}_k{}_py_total_ref",
            num_snps, num_samples_total, k_components
        );
        match get_python_reference_pca(
            &standardized_genos_total_snps_x_samples,
            k_components, // Use original k_components for full data PCA
            &python_total_data_prefix,
        ) {
            Ok((_py_loadings_total_k_x_d, py_scores_total_n_x_k, _py_eigenvalues_total)) => {
                // _py_loadings_total is K x D
                // py_scores_total_n_x_k is N_total x K
                let k_py_total = _py_loadings_total_k_x_d.nrows(); // Kx D, so nrows is K
                if py_scores_total_n_x_k.nrows() == num_samples_total
                    && py_scores_total_n_x_k.ncols() >= k_components.min(k_py_total)
                {
                    // Extract test sample scores: from row num_samples_train onwards
                    // Ensure k_eff_rust is used for slicing columns to match projected scores dimensions
                    let num_cols_to_slice = k_eff_rust.min(py_scores_total_n_x_k.ncols());
                    if num_cols_to_slice > 0 {
                        let py_test_scores_ref = py_scores_total_n_x_k
                            .slice(s![num_samples_train.., 0..num_cols_to_slice])
                            .to_owned();
                        save_matrix_to_tsv(
                            &py_test_scores_ref.view(),
                            artifact_dir.to_str().unwrap_or("."),
                            "python_ref_test_scores.tsv",
                        )
                        .unwrap_or_default();
                        py_test_scores_ref_option = Some(py_test_scores_ref);
                        outcome_details.push_str(&format!("Python on total data successful. k_py_total: {}. Sliced to {} cols for comparison. ", k_py_total, num_cols_to_slice));
                    } else {
                        outcome_details.push_str(
                            "Python on total data: 0 relevant components to slice for comparison. ",
                        );
                        // This might be a test failure if k_eff_rust or k_py_total was expected to be > 0
                        if k_eff_rust > 0 {
                            // If Rust produced PCs but Python didn't produce comparable ones
                            test_successful = false;
                            outcome_details.push_str("Mismatch: Rust produced PCs but Python reference had 0 comparable PCs. ");
                        }
                    }
                } else {
                    test_successful = false;
                    outcome_details.push_str(&format!(
                        "Python (total data) scores dimensions mismatch. Expected N_total x >=k_eff_py ({}x{}), Got {}x{}. ",
                        num_samples_total, k_components.min(k_py_total),
                        py_scores_total_n_x_k.nrows(), py_scores_total_n_x_k.ncols()
                    ));
                }
            }
            Err(e) => {
                test_successful = false;
                outcome_details.push_str(&format!(
                    "Python reference PCA on total data failed: {}. ",
                    e
                ));
            }
        }
    }
    // Compare Projected Scores with Reference Scores
    if test_successful && s_projected_option.is_some() && py_test_scores_ref_option.is_some() {
        let s_projected = s_projected_option.as_ref().unwrap();
        let py_test_scores_ref = py_test_scores_ref_option.as_ref().unwrap();

        let k_compare = k_eff_rust.min(py_test_scores_ref.ncols());
        outcome_details.push_str(&format!("Comparing {} PCs for projection. ", k_compare));

        if k_compare == 0 {
            if k_eff_rust != py_test_scores_ref.ncols() {
                // If one is 0 and other is not (and k_compare became 0)
                test_successful = false;
                outcome_details.push_str(&format!(
                    "Mismatch in comparable k (Rust_eff_k: {}, Py_ref_k: {}). ",
                    k_eff_rust,
                    py_test_scores_ref.ncols()
                ));
            } else {
                // Both are 0
                outcome_details.push_str("Both Rust and Py_ref have 0 PCs to compare. ");
            }
        } else {
            let mut min_abs_corr = 1.0f32;
            let mut max_mse = 0.0f32;
            let mut correlations_summary = String::new();
            let mut mses_summary = String::new();

            for pc_idx in 0..k_compare {
                let projected_col = s_projected.column(pc_idx);
                let ref_col = py_test_scores_ref.column(pc_idx);

                // Correlation
                let abs_corr = pearson_correlation(projected_col.view(), ref_col.view())
                    .map_or(0.0, |c| c.abs());
                if abs_corr < min_abs_corr {
                    min_abs_corr = abs_corr;
                }
                correlations_summary.push_str(&format!("PC{}_absR={:.4}; ", pc_idx, abs_corr));
                if abs_corr < 0.95 {
                    test_successful = false;
                    outcome_details.push_str(&format!(
                        "Low projection correlation for PC {}: {:.4}. ",
                        pc_idx, abs_corr
                    ));
                }

                // MSE
                let mse = (projected_col.to_owned() - ref_col.to_owned())
                    .mapv(|x| x * x)
                    .mean()
                    .unwrap_or(f32::MAX);
                if mse > max_mse {
                    max_mse = mse;
                }
                mses_summary.push_str(&format!("PC{}_MSE={:.4e}; ", pc_idx, mse));
                if mse > 0.1 {
                    // Threshold for MSE, might need adjustment
                    test_successful = false;
                    outcome_details.push_str(&format!(
                        "High projection MSE for PC {}: {:.4e}. ",
                        pc_idx, mse
                    ));
                }
            }
            outcome_details.push_str(&format!(
                "Min_abs_correlation: {:.4}, Max_MSE: {:.4e}. Correlations: {}. MSEs: {}. ",
                min_abs_corr,
                max_mse,
                correlations_summary.trim_end_matches("; "),
                mses_summary.trim_end_matches("; ")
            ));
        }
    } else if test_successful {
        // If previous steps were successful but options are None
        test_successful = false; // Should not happen if logic is correct
        outcome_details.push_str("Comparison skipped due to missing projected or reference scores despite earlier success. ");
    }

    let record = TestResultRecord {
        test_name,
        num_features_d: num_snps,
        num_samples_n: num_samples_total, // Log total N for context
        num_pcs_requested_k: k_components,
        num_pcs_computed: k_eff_rust, // Rust's computed PCs on training set
        success: test_successful,
        outcome_details: outcome_details.clone(),
        notes,
    };
    TEST_RESULTS.lock().unwrap().push(record);

    assert!(
        test_successful,
        "Test '{}' failed. Check TSV. Details: {}",
        test_name_str, outcome_details
    );
}

#[test]
fn test_sample_projection_accuracy_large_config1() {
    run_sample_projection_accuracy_test(
        "test_sample_projection_accuracy_large_config1",
        1000,   // num_snps (D)
        250,    // num_samples_total
        200,    // num_samples_train
        10,     // k_components
        202403, // seed
    );
}

#[test]
fn test_sample_projection_accuracy_large_config2() {
    run_sample_projection_accuracy_test(
        "test_sample_projection_accuracy_large_config2",
        2000,   // num_snps (D)
        400,    // num_samples_total
        300,    // num_samples_train
        15,     // k_components
        202404, // seed
    );
}

#[test]
fn test_pc_correlation_with_truth_set_large_2000x300() {
    run_pc_correlation_with_truth_set_test(
        "test_pc_correlation_with_truth_set_large_2000x300",
        2000,   // num_snps (D)
        300,    // num_samples (N)
        15,     // k_components
        202402, // seed
    );
}

// Original tests for reorder utils
// use efficient_pca::eigensnp::{reorder_array_owned, reorder_columns_owned}; // Removed

// use ndarray::{Array1, Array2, arr2}; // Removed

#[test]
fn test_reorder_array_basic() {
    let original = Array1::from(vec![10, 20, 30, 40]);
    let order = vec![2, 0, 3, 1];
    let expected = Array1::from(vec![30, 10, 40, 20]);
    let reordered = reorder_array_owned(&original, &order);
    assert_eq!(reordered, expected);
}

#[test]
fn test_reorder_array_empty_array_empty_order() {
    let original = Array1::<i32>::from(vec![]);
    let order_empty = vec![];
    let expected = Array1::<i32>::from(vec![]);
    let reordered_empty_order = reorder_array_owned(&original, &order_empty);
    assert_eq!(reordered_empty_order, expected);
}

#[test]
#[should_panic]
fn test_reorder_array_select_from_zero_elements_panics() {
    let original = Array1::<i32>::from(vec![]);
    let order = vec![0];
    reorder_array_owned(&original, &order);
}

#[test]
fn test_reorder_array_empty_order() {
    let original = Array1::from(vec![10, 20, 30]);
    let order = vec![];
    let expected = Array1::<i32>::from(vec![]);
    let reordered = reorder_array_owned(&original, &order);
    assert_eq!(reordered, expected);
}

#[test]
fn test_reorder_array_repeated_indices() {
    let original = Array1::from(vec![10, 20, 30]);
    let order = vec![0, 1, 0, 2, 1, 1];
    let expected = Array1::from(vec![10, 20, 10, 30, 20, 20]);
    let reordered = reorder_array_owned(&original, &order);
    assert_eq!(reordered, expected);
}

#[test]
fn test_reorder_columns_basic() {
    let original = arr2(&[[1, 2, 3], [4, 5, 6]]);
    let order = vec![2, 0, 1];
    let expected = arr2(&[[3, 1, 2], [6, 4, 5]]);
    let reordered = reorder_columns_owned(&original, &order);
    assert_eq!(reordered, expected);
}

// Function to run refinement improvement tests
#[allow(clippy::too_many_arguments)] // Allow many arguments for this test helper
fn run_refinement_improvement_test<F>(
    test_name: &str,
    standardized_structured_data: &Array2<f32>,
    ld_block_specs: &[LdBlockSpecification],
    k_components_to_request: usize,
    python_reference_output: &(Array2<f32>, Array2<f32>, Array1<f64>), // (ref_loadings_k_x_d, ref_scores_n_x_k, ref_eigenvalues_k)
    pass_count_less_refined: usize,
    pass_count_more_refined: usize,
    metric_evaluator: F,
    metric_name: &str,
    seed: u64,
) -> Result<(), String>
where
    F: Fn(
        &EigenSNPCoreOutput,
        &(Array2<f32>, Array2<f32>, Array1<f64>), // Python ref: (loadings_k_x_d, scores_n_x_k, eigenvalues_k)
        &str,                                     // Context string
    ) -> f64, // Metric score (higher is better)
{
    let full_test_name = format!("{}_{}", test_name, metric_name);
    let artifact_dir_name = full_test_name.replace(|c: char| !c.is_alphanumeric() && c != '_', "_"); // Sanitize for dir name
    let artifact_dir = Path::new("target/test_artifacts").join(artifact_dir_name);
    fs::create_dir_all(&artifact_dir).map_err(|e| {
        format!(
            "Failed to create artifact directory '{}': {}",
            artifact_dir.display(),
            e
        )
    })?;

    let mut outcome_details = String::new();
    let mut notes = format!("Seed: {}. ", seed);
    let mut overall_test_success = true;

    // Save Python reference outputs
    save_matrix_to_tsv(
        &python_reference_output.0.view(),
        artifact_dir.to_str().unwrap(),
        "python_ref_loadings_k_x_d.tsv",
    )
    .map_err(|e| format!("Failed to save python_ref_loadings.tsv: {}", e))?;
    save_matrix_to_tsv(
        &python_reference_output.1.view(),
        artifact_dir.to_str().unwrap(),
        "python_ref_scores_n_x_k.tsv",
    )
    .map_err(|e| format!("Failed to save python_ref_scores.tsv: {}", e))?;
    save_vector_to_tsv(
        &python_reference_output.2.view(),
        artifact_dir.to_str().unwrap(),
        "python_ref_eigenvalues_k.tsv",
    )
    .map_err(|e| format!("Failed to save python_ref_eigenvalues.tsv: {}", e))?;

    // Config A (less refined)
    let config_a = EigenSNPCoreAlgorithmConfig {
        target_num_global_pcs: k_components_to_request,
        refine_pass_count: pass_count_less_refined,
        random_seed: seed,
        // Placeholder: Use a default_eigensnp_config_for_refinement_tests helper in future
        subset_factor_for_local_basis_learning: 0.5,
        min_subset_size_for_local_basis_learning: (standardized_structured_data.ncols() / 4)
            .max(1)
            .min(standardized_structured_data.ncols().max(1)),
        max_subset_size_for_local_basis_learning: (standardized_structured_data.ncols() / 2)
            .max(10)
            .min(standardized_structured_data.ncols().max(1)),
        components_per_ld_block: 10.min(
            standardized_structured_data.nrows().min(
                (standardized_structured_data.ncols() / 2)
                    .max(10)
                    .min(standardized_structured_data.ncols().max(1)),
            ),
        ),
        ..Default::default()
    };

    // Config B (more refined)
    let config_b = EigenSNPCoreAlgorithmConfig {
        refine_pass_count: pass_count_more_refined,
        ..config_a.clone() // All other parameters identical to config_a
    };

    let test_data_accessor = TestDataAccessor::new(standardized_structured_data.clone());
    let algorithm_a = EigenSNPCoreAlgorithm::new(config_a);
    let algorithm_b = EigenSNPCoreAlgorithm::new(config_b);

    let snp_metadata = create_dummy_snp_metadata(standardized_structured_data.nrows());

    // Run EigenSnp A
    let output_a = match algorithm_a.compute_pca(&test_data_accessor, ld_block_specs, &snp_metadata)
    {
        Ok((out, _)) => {
            writeln!(
                outcome_details,
                "EigenSnp (Less Refined, {} passes): SUCCESS.",
                pass_count_less_refined
            )
            .unwrap_or_default();
            save_matrix_to_tsv(
                &out.final_snp_principal_component_loadings.view(),
                artifact_dir.to_str().unwrap(),
                "eigensnp_less_refined_loadings.tsv",
            )
            .unwrap_or_default();
            save_matrix_to_tsv(
                &out.final_sample_principal_component_scores.view(),
                artifact_dir.to_str().unwrap(),
                "eigensnp_less_refined_scores.tsv",
            )
            .unwrap_or_default();
            save_vector_to_tsv(
                &out.final_principal_component_eigenvalues.view(),
                artifact_dir.to_str().unwrap(),
                "eigensnp_less_refined_eigenvalues.tsv",
            )
            .unwrap_or_default();
            out
        }
        Err(e) => {
            writeln!(
                outcome_details,
                "EigenSnp (Less Refined, {} passes): FAILED. Error: {}",
                pass_count_less_refined, e
            )
            .unwrap_or_default();
            notes.push_str(&format!("EigenSnp A failed: {}. ", e));
            overall_test_success = false;
            EigenSNPCoreOutput {
                final_snp_principal_component_loadings: Array2::zeros((0, 0)),
                final_sample_principal_component_scores: Array2::zeros((0, 0)),
                final_principal_component_eigenvalues: Array1::zeros(0),
                num_principal_components_computed: 0,
                num_pca_snps_used: standardized_structured_data.nrows(),
                num_qc_samples_used: standardized_structured_data.ncols(),
            }
        }
    };

    // Run EigenSnp B
    let output_b = match algorithm_b.compute_pca(&test_data_accessor, ld_block_specs, &snp_metadata)
    {
        Ok((out, _)) => {
            writeln!(
                outcome_details,
                "EigenSnp (More Refined, {} passes): SUCCESS.",
                pass_count_more_refined
            )
            .unwrap_or_default();
            save_matrix_to_tsv(
                &out.final_snp_principal_component_loadings.view(),
                artifact_dir.to_str().unwrap(),
                "eigensnp_more_refined_loadings.tsv",
            )
            .unwrap_or_default();
            save_matrix_to_tsv(
                &out.final_sample_principal_component_scores.view(),
                artifact_dir.to_str().unwrap(),
                "eigensnp_more_refined_scores.tsv",
            )
            .unwrap_or_default();
            save_vector_to_tsv(
                &out.final_principal_component_eigenvalues.view(),
                artifact_dir.to_str().unwrap(),
                "eigensnp_more_refined_eigenvalues.tsv",
            )
            .unwrap_or_default();
            out
        }
        Err(e) => {
            writeln!(
                outcome_details,
                "EigenSnp (More Refined, {} passes): FAILED. Error: {}",
                pass_count_more_refined, e
            )
            .unwrap_or_default();
            notes.push_str(&format!("EigenSnp B failed: {}. ", e));
            overall_test_success = false;
            EigenSNPCoreOutput {
                final_snp_principal_component_loadings: Array2::zeros((0, 0)),
                final_sample_principal_component_scores: Array2::zeros((0, 0)),
                final_principal_component_eigenvalues: Array1::zeros(0),
                num_principal_components_computed: 0,
                num_pca_snps_used: standardized_structured_data.nrows(),
                num_qc_samples_used: standardized_structured_data.ncols(),
            }
        }
    };

    let num_pcs_computed_for_log =
        if overall_test_success || output_b.num_principal_components_computed > 0 {
            output_b.num_principal_components_computed
        } else {
            output_a.num_principal_components_computed
        };

    // Evaluate Metrics (only if both runs were notionally successful, even if they produced empty output)
    let score_a = if !notes.contains("EigenSnp A failed") {
        metric_evaluator(&output_a, python_reference_output, "LessRefined_vs_Ref")
    } else {
        f64::NAN // Indicate failure to compute metric
    };
    let score_b = if !notes.contains("EigenSnp B failed") {
        metric_evaluator(&output_b, python_reference_output, "MoreRefined_vs_Ref")
    } else {
        f64::NAN
    };

    writeln!(
        outcome_details,
        "{}: Less Refined ({} passes) score = {:.6e}, More Refined ({} passes) score = {:.6e}.",
        metric_name, pass_count_less_refined, score_a, pass_count_more_refined, score_b
    )
    .unwrap_or_default();

    let tolerance = 1e-9;
    let improvement_observed = score_b >= score_a - tolerance;

    if score_a.is_nan() || score_b.is_nan() {
        writeln!(
            outcome_details,
            "Improvement check SKIPPED due to metric computation failure for one or both runs."
        )
        .unwrap_or_default();
        overall_test_success = false; // If scores couldn't be computed, it's a failure.
    } else if improvement_observed {
        writeln!(
            outcome_details,
            "Improvement (or non-degradation within tolerance) OBSERVED."
        )
        .unwrap_or_default();
    } else {
        writeln!(
            outcome_details,
            "Improvement NOT OBSERVED. Score B ({:.6e}) < Score A ({:.6e}) - tolerance.",
            score_b, score_a
        )
        .unwrap_or_default();
        overall_test_success = false; // This is the primary assertion failure
    }

    let record = TestResultRecord {
        test_name: full_test_name,
        num_features_d: standardized_structured_data.nrows(),
        num_samples_n: standardized_structured_data.ncols(),
        num_pcs_requested_k: k_components_to_request,
        num_pcs_computed: num_pcs_computed_for_log,
        success: overall_test_success,
        outcome_details: outcome_details.clone(), // Clone since it's used again in assert
        notes,
    };
    TEST_RESULTS.lock().unwrap().push(record);

    if !overall_test_success {
        return Err(format!(
            "Test '{}' failed. Details: {}",
            test_name, outcome_details
        ));
    }

    Ok(())
}

/// Evaluates the average absolute Pearson correlation of PC scores between eigensnp output and a reference.
pub fn evaluate_pc_score_correlation(
    eigensnp_output: &EigenSNPCoreOutput,
    reference_output: &(Array2<f32>, Array2<f32>, Array1<f64>), // (ref_loadings_k_x_d, ref_scores_n_x_k, ref_eigenvalues_k)
    _context: &str, // Context string, ignored for this evaluator
) -> f64 {
    let eigensnp_scores = &eigensnp_output.final_sample_principal_component_scores; // N x K_eigensnp
    let ref_scores = &reference_output.1; // N x K_ref (reference_output.1 is scores_n_x_k)

    let k_compare = eigensnp_scores.ncols().min(ref_scores.ncols());

    if k_compare == 0 {
        return 0.0; // No components to compare
    }

    let mut total_abs_correlation = 0.0;

    for pc_idx in 0..k_compare {
        let eigensnp_score_col = eigensnp_scores.column(pc_idx);
        let ref_score_col = ref_scores.column(pc_idx);

        // pearson_correlation is expected to be available in the same module or made public
        let corr =
            pearson_correlation(eigensnp_score_col.view(), ref_score_col.view()).unwrap_or(0.0);
        let abs_corr = corr.abs();
        total_abs_correlation += abs_corr as f64; // Ensure f64 for sum
    }

    total_abs_correlation / (k_compare as f64) // Average absolute correlation
}

/// Evaluates the average absolute Pearson correlation of SNP loadings between eigensnp output and a reference.
pub fn evaluate_snp_loading_correlation(
    eigensnp_output: &EigenSNPCoreOutput,
    reference_output: &(Array2<f32>, Array2<f32>, Array1<f64>), // (ref_loadings_k_x_d, ref_scores_n_x_k, ref_eigenvalues_k)
    _context: &str, // Context string, ignored for this evaluator
) -> f64 {
    let eigensnp_loadings = &eigensnp_output.final_snp_principal_component_loadings; // D_snps x K_eigensnp
    let ref_loadings_k_x_d = &reference_output.0; // K_ref x D_snps from Python reference

    // Number of principal components to compare.
    // eigensnp_loadings.ncols() is K_eigensnp.
    // ref_loadings_k_x_d.nrows() is K_ref (number of components in reference).
    let k_compare = eigensnp_loadings.ncols().min(ref_loadings_k_x_d.nrows());

    if k_compare == 0 {
        return 0.0; // No components to compare
    }

    // Ensure the number of SNPs (dimension D) matches.
    // eigensnp_loadings.nrows() is D_snps.
    // ref_loadings_k_x_d.ncols() is D_snps.
    if eigensnp_loadings.nrows() != ref_loadings_k_x_d.ncols() && k_compare > 0 {
        // This case should ideally not happen if data preprocessing is correct,
        // but it's a safeguard.
        eprintln!(
            "SNP dimension mismatch in evaluate_snp_loading_correlation: eigensnp D={}, ref D={}",
            eigensnp_loadings.nrows(),
            ref_loadings_k_x_d.ncols()
        );
        return f64::NAN; // Or handle as an error, returning NaN for metric seems appropriate
    }

    let mut total_abs_correlation = 0.0;

    for pc_idx in 0..k_compare {
        // eigensnp_loading_col is a view of a column from D_snps x K_eigensnp matrix (length D_snps)
        let eigensnp_loading_col = eigensnp_loadings.column(pc_idx);

        // ref_loadings_k_x_d is K_ref x D_snps. We need the pc_idx-th row,
        // which represents the loadings for that PC across D_snps.
        // This row view is 1 x D_snps, but its .view() will be compatible with ArrayView1 of length D_snps.
        let ref_loading_vector_for_pc = ref_loadings_k_x_d.row(pc_idx);

        let corr = pearson_correlation(
            eigensnp_loading_col.view(),
            ref_loading_vector_for_pc.view(),
        )
        .unwrap_or(0.0);
        let abs_corr = corr.abs();
        total_abs_correlation += abs_corr as f64;
    }

    total_abs_correlation / (k_compare as f64) // Average absolute correlation
}

/// Evaluates the accuracy of eigenvalues based on mean squared relative error.
/// Returns -mean_squared_relative_error (higher is better).
pub fn evaluate_eigenvalue_accuracy(
    eigensnp_output: &EigenSNPCoreOutput,
    reference_output: &(Array2<f32>, Array2<f32>, Array1<f64>), // (ref_loadings_k_x_d, ref_scores_n_x_k, ref_eigenvalues_k)
    _context: &str, // Context string, ignored for this evaluator
) -> f64 {
    let eigensnp_eigenvalues = &eigensnp_output.final_principal_component_eigenvalues; // Array1<f64>
    let ref_eigenvalues = &reference_output.2; // Array1<f64>

    let k_compare = eigensnp_eigenvalues.len().min(ref_eigenvalues.len());

    if k_compare == 0 {
        // No components to compare. If any side expected components, this is bad.
        // If both expected 0, then 0 error is fine.
        // For a general metric, if k_requested > 0 and k_compare = 0, it should be a large penalty.
        // However, run_refinement_improvement_test's assertion is score_b >= score_a - tol.
        // If both are 0, it passes. If one is 0 and other is positive (less error), it passes.
        // If score_a is 0 (no error) and score_b is -ve (error), it might fail if -ve < 0 - tol.
        // Let's return 0.0 if no components, implying perfect accuracy for "nothing".
        // The overall test success will depend on whether PCs were expected.
        return 0.0;
    }

    let mut total_squared_relative_error = 0.0;

    for i in 0..k_compare {
        let e_eigensnp = eigensnp_eigenvalues[i];
        let e_ref = ref_eigenvalues[i];
        let squared_relative_error: f64;

        if e_ref.abs() < 1e-9 {
            if e_eigensnp.abs() < 1e-9 {
                squared_relative_error = 0.0; // Both are zero, no error
            } else {
                // Reference is zero, but eigensnp is not. Large error.
                // Capping to avoid extreme values from dominating if e_eigensnp is also small but non-zero.
                // A fixed large error penalty might be better than e_eigensnp.powi(2) if e_eigensnp can be large.
                // For now, using 1.0 as the error, so 1.0 when squared.
                // The prompt suggests 1.0e12, which is very large. Let's use a moderately large number.
                // Using (e_eigensnp / (1e-9)).powi(2) could be an alternative if e_ref is truly tiny.
                // Let's cap the error term's contribution to total_squared_relative_error. Max value of 1.0 for this term.
                // squared_relative_error = (e_eigensnp.abs() / (e_eigensnp.abs().max(1e-9))).powi(2); //This is not ideal
                // Let's use a large fixed value if e_ref is ~0 but e_eigensnp is not.
                squared_relative_error = 1.0e6; // Large penalty if ref is zero and eigensnp is not.
            }
        } else {
            let relative_error = (e_eigensnp - e_ref) / e_ref;
            squared_relative_error = relative_error.powi(2);
        }
        // Cap individual squared_relative_error to prevent extreme values from dominating.
        total_squared_relative_error += squared_relative_error.min(1.0e12); // Cap at 1.0e12
    }

    let mean_squared_relative_error = total_squared_relative_error / (k_compare as f64);
    -mean_squared_relative_error // Higher is better, so negate MSRE
}

#[test]
fn test_refinement_score_correlation() {
    // 1. Setup Data
    let d_total_snps = 5000;
    let n_samples = 500;
    let k_true_components = 10;
    let signal_strength = 3.0;
    let noise_std_dev = 1.0;
    let seed = 20241001;

    let standardized_structured_data = generate_structured_data(
        d_total_snps,
        n_samples,
        k_true_components,
        signal_strength,
        noise_std_dev,
        seed,
    );

    // 2. Get Python Reference
    let k_components_to_request_pca = k_true_components + 5;
    let python_reference_output_result = get_python_reference_pca(
        &standardized_structured_data,
        k_components_to_request_pca,
        "refinement_score_corr_py_ref",
    );

    let python_reference_output = match python_reference_output_result {
        Ok(output) => output,
        Err(e) => {
            panic!(
                "Failed to get Python reference PCA for test_refinement_score_correlation: {}",
                e
            );
        }
    };
    // python_reference_output is (loadings_k_x_d, scores_n_x_k, eigenvalues_k)
    // We need to ensure the dimensions are what evaluate_pc_score_correlation expects.
    // The get_python_reference_pca already returns KxN for loadings, NxC for scores, C for eigenvalues.
    // This is consistent with what parse_pca_py_output provides.
    // However, the metric evaluator expects ref_loadings_k_x_d.
    // The current python_reference_output.0 is K_py x D_snps. This is fine.

    // 3. LD Blocks
    let ld_block_specs = vec![LdBlockSpecification {
        user_defined_block_tag: "full_block".to_string(),
        pca_snp_ids_in_block: (0..d_total_snps).map(PcaSnpId).collect(),
    }];

    // 4. Run Test
    let result = run_refinement_improvement_test(
        "refinement_score_correlation",
        &standardized_structured_data,
        &ld_block_specs,
        k_true_components, // k_components_to_request for eigensnp
        &python_reference_output,
        1, // pass_count_less_refined
        2, // pass_count_more_refined
        evaluate_pc_score_correlation,
        "PCScoreCorrelation",
        seed, // Use the same seed for eigensnp runs
    );

    if let Err(e) = result {
        panic!("test_refinement_score_correlation failed: {}", e);
    }
}

#[test]
fn test_refinement_loading_correlation() {
    // 1. Setup Data
    let d_total_snps = 5000;
    let n_samples = 500;
    let k_true_components = 10;
    let signal_strength = 3.0;
    let noise_std_dev = 1.0;
    let seed = 20241002; // Different seed as suggested

    let standardized_structured_data = generate_structured_data(
        d_total_snps,
        n_samples,
        k_true_components,
        signal_strength,
        noise_std_dev,
        seed,
    );

    // 2. Get Python Reference
    let k_components_to_request_pca = k_true_components + 5;
    let python_reference_output_result = get_python_reference_pca(
        &standardized_structured_data,
        k_components_to_request_pca,
        "refinement_loading_corr_py_ref", // Unique artifact prefix
    );

    let python_reference_output = match python_reference_output_result {
        Ok(output) => output,
        Err(e) => {
            panic!(
                "Failed to get Python reference PCA for test_refinement_loading_correlation: {}",
                e
            );
        }
    };
    // python_reference_output is (loadings_k_x_d, scores_n_x_k, eigenvalues_k)
    // evaluate_snp_loading_correlation expects reference_output.0 to be K_ref x D_snps,
    // which is what get_python_reference_pca provides via parse_pca_py_output.

    // 3. LD Blocks
    let ld_block_specs = vec![LdBlockSpecification {
        user_defined_block_tag: "full_block".to_string(),
        pca_snp_ids_in_block: (0..d_total_snps).map(PcaSnpId).collect(),
    }];

    // 4. Run Test
    let result = run_refinement_improvement_test(
        "refinement_loading_correlation",
        &standardized_structured_data,
        &ld_block_specs,
        k_true_components, // k_components_to_request for eigensnp
        &python_reference_output,
        1, // pass_count_less_refined
        2, // pass_count_more_refined
        evaluate_snp_loading_correlation,
        "SNPLoadingCorrelation",
        seed, // Use the same seed for eigensnp runs for this specific test
    );

    if let Err(e) = result {
        panic!("test_refinement_loading_correlation failed: {}", e);
    }
}

#[test]
fn test_refinement_eigenvalue_accuracy() {
    // 1. Setup Data
    let d_total_snps = 5000;
    let n_samples = 500;
    let k_true_components = 10;
    let signal_strength = 3.0;
    let noise_std_dev = 1.0;
    let seed = 20241003; // New seed

    let standardized_structured_data = generate_structured_data(
        d_total_snps,
        n_samples,
        k_true_components,
        signal_strength,
        noise_std_dev,
        seed,
    );

    // 2. Get Python Reference
    let k_components_to_request_pca = k_true_components + 5;
    let python_reference_output_result = get_python_reference_pca(
        &standardized_structured_data,
        k_components_to_request_pca,
        "refinement_eigenvalue_acc_py_ref", // Unique artifact prefix
    );

    let python_reference_output = match python_reference_output_result {
        Ok(output) => output,
        Err(e) => {
            panic!(
                "Failed to get Python reference PCA for test_refinement_eigenvalue_accuracy: {}",
                e
            );
        }
    };
    // python_reference_output.2 is Array1<f64> for eigenvalues.

    // 3. LD Blocks
    let ld_block_specs = vec![LdBlockSpecification {
        user_defined_block_tag: "full_block".to_string(),
        pca_snp_ids_in_block: (0..d_total_snps).map(PcaSnpId).collect(),
    }];

    // 4. Run Test
    let result = run_refinement_improvement_test(
        "refinement_eigenvalue_accuracy",
        &standardized_structured_data,
        &ld_block_specs,
        k_true_components, // k_components_to_request for eigensnp
        &python_reference_output,
        1, // pass_count_less_refined
        2, // pass_count_more_refined
        evaluate_eigenvalue_accuracy,
        "EigenvalueAccuracy",
        seed, // Use the same seed for eigensnp runs for this specific test
    );

    if let Err(e) = result {
        panic!("test_refinement_eigenvalue_accuracy failed: {}", e);
    }
}

// Define the QualityThresholds struct
struct QualityThresholds {
    min_score_correlation: f64,
    min_loading_correlation: f64,
    max_neg_eigenvalue_accuracy: f64, // Higher is better, so this is a minimum acceptable value
}

#[test]
fn test_min_passes_for_quality_convergence() {
    let test_logging_name = "test_min_passes_for_quality_convergence";

    // 1. Data Generation
    let d_total_snps = 5000;
    let n_samples = 500;
    let k_true_components = 10;
    let signal_strength = 3.0;
    let noise_std_dev = 1.0;
    let seed = 20241005;

    let standardized_structured_data = generate_structured_data(
        d_total_snps,
        n_samples,
        k_true_components,
        signal_strength,
        noise_std_dev,
        seed,
    );

    // 2. Artifact Directory
    let artifact_dir = Path::new("target/test_artifacts").join(test_logging_name);
    fs::create_dir_all(&artifact_dir).unwrap_or_else(|e| {
        panic!(
            "Failed to create artifact directory '{}': {}",
            artifact_dir.display(),
            e
        )
    });

    // 3. Python Reference PCA
    let k_components_to_request_pca = k_true_components + 5;
    let python_reference_output_result = get_python_reference_pca(
        &standardized_structured_data,
        k_components_to_request_pca,
        &format!("{}_py_ref", test_logging_name),
    );

    let python_reference_output = match python_reference_output_result {
        Ok(output) => output,
        Err(e) => {
            panic!(
                "Failed to get Python reference PCA for {}: {}",
                test_logging_name, e
            );
        }
    };

    // 4. Quality Thresholds
    let thresholds = QualityThresholds {
        min_score_correlation: 0.998,
        min_loading_correlation: 0.995,
        max_neg_eigenvalue_accuracy: -0.01, // Corresponds to an MSRE of 0.01. Higher is better.
    };

    // 5. LD Blocks
    let ld_block_specs = vec![LdBlockSpecification {
        user_defined_block_tag: "full_block".to_string(),
        pca_snp_ids_in_block: (0..d_total_snps).map(PcaSnpId).collect(),
    }];

    let snp_metadata = create_dummy_snp_metadata(d_total_snps);

    // 6. Looping and Evaluation
    let max_passes_to_test = 5;
    let mut min_passes_found: i32 = -1;
    let mut overall_outcome_details = String::new();
    writeln!(overall_outcome_details, "Test: {}", test_logging_name).unwrap_or_default();
    let mut num_pcs_computed_at_convergence = 0;

    for current_pass_count in 1..=max_passes_to_test {
        writeln!(
            overall_outcome_details,
            "\n--- Evaluating with {} refinement pass(es) ---",
            current_pass_count
        )
        .unwrap_or_default();

        let config = EigenSNPCoreAlgorithmConfig {
            target_num_global_pcs: k_true_components,
            refine_pass_count: current_pass_count,
            random_seed: seed,
            subset_factor_for_local_basis_learning: 0.5,
            min_subset_size_for_local_basis_learning: (n_samples / 4).max(1).min(n_samples.max(1)),
            max_subset_size_for_local_basis_learning: (n_samples / 2).max(10).min(n_samples.max(1)),
            components_per_ld_block: 10
                .min(d_total_snps.min((n_samples / 2).max(10).min(n_samples.max(1)))),
            ..Default::default()
        };

        let test_data_accessor = TestDataAccessor::new(standardized_structured_data.clone());
        let algorithm = EigenSNPCoreAlgorithm::new(config);

        match algorithm.compute_pca(&test_data_accessor, &ld_block_specs, &snp_metadata) {
            Ok((eigensnp_output_current_pass, _)) => {
                // This variable will store the PC count from the pass that *first* meets criteria,
                // or the last successful one if criteria are never met.
                // If min_passes_found is already set, we don't update num_pcs_computed_at_convergence.
                if min_passes_found == -1 {
                    num_pcs_computed_at_convergence =
                        eigensnp_output_current_pass.num_principal_components_computed;
                }

                let pass_artifact_dir_name = format!("eigensnp_pass_{}", current_pass_count);
                let pass_artifact_dir = artifact_dir.join(pass_artifact_dir_name);
                fs::create_dir_all(&pass_artifact_dir)
                    .unwrap_or_else(|e| eprintln!("Failed to create pass artifact dir: {}", e));

                save_matrix_to_tsv(
                    &eigensnp_output_current_pass
                        .final_snp_principal_component_loadings
                        .view(),
                    pass_artifact_dir.to_str().unwrap_or("."),
                    "loadings.tsv",
                )
                .unwrap_or_default();
                save_matrix_to_tsv(
                    &eigensnp_output_current_pass
                        .final_sample_principal_component_scores
                        .view(),
                    pass_artifact_dir.to_str().unwrap_or("."),
                    "scores.tsv",
                )
                .unwrap_or_default();
                save_vector_to_tsv(
                    &eigensnp_output_current_pass
                        .final_principal_component_eigenvalues
                        .view(),
                    pass_artifact_dir.to_str().unwrap_or("."),
                    "eigenvalues.tsv",
                )
                .unwrap_or_default();

                let score_corr = evaluate_pc_score_correlation(
                    &eigensnp_output_current_pass,
                    &python_reference_output,
                    "ScoreCorr",
                );
                let loading_corr = evaluate_snp_loading_correlation(
                    &eigensnp_output_current_pass,
                    &python_reference_output,
                    "LoadingCorr",
                );
                let eigen_acc = evaluate_eigenvalue_accuracy(
                    &eigensnp_output_current_pass,
                    &python_reference_output,
                    "EigenAcc",
                );

                writeln!(
                    overall_outcome_details,
                    "  PC Score Correlation: {:.6e}",
                    score_corr
                )
                .unwrap_or_default();
                writeln!(
                    overall_outcome_details,
                    "  SNP Loading Correlation: {:.6e}",
                    loading_corr
                )
                .unwrap_or_default();
                writeln!(
                    overall_outcome_details,
                    "  Eigenvalue Accuracy (-MSRE): {:.6e}",
                    eigen_acc
                )
                .unwrap_or_default();

                if min_passes_found == -1 {
                    // Only set if not already found
                    if score_corr >= thresholds.min_score_correlation
                        && loading_corr >= thresholds.min_loading_correlation
                        && eigen_acc >= thresholds.max_neg_eigenvalue_accuracy
                    {
                        min_passes_found = current_pass_count as i32;
                        // num_pcs_computed_at_convergence is already set from this successful pass.
                        writeln!(overall_outcome_details, "  SUCCESS: All quality thresholds MET at {} pass(es). PCs in this run: {}.", current_pass_count, num_pcs_computed_at_convergence).unwrap_or_default();
                    } else {
                        writeln!(
                            overall_outcome_details,
                            "  INFO: Quality thresholds NOT MET at {} pass(es).",
                            current_pass_count
                        )
                        .unwrap_or_default();
                    }
                } else {
                    // min_passes_found != -1 (i.e., convergence already met)
                    writeln!(overall_outcome_details, "  INFO: Thresholds previously met at {} passes. Current pass {} metrics recorded.", min_passes_found, current_pass_count).unwrap_or_default();
                    if !(score_corr >= thresholds.min_score_correlation
                        && loading_corr >= thresholds.min_loading_correlation
                        && eigen_acc >= thresholds.max_neg_eigenvalue_accuracy)
                    {
                        writeln!(overall_outcome_details, "  WARNING: Quality REGRESSED at {} passes after prior convergence at {} passes.", current_pass_count, min_passes_found).unwrap_or_default();
                    }
                }
            }
            Err(e) => {
                writeln!(
                    overall_outcome_details,
                    "  FAILURE: EigenSnp compute_pca failed for {} pass(es): {}",
                    current_pass_count, e
                )
                .unwrap_or_default();
                if min_passes_found != -1 {
                    writeln!(overall_outcome_details, "  WARNING: PCA computation FAILED at {} passes after prior convergence at {} passes.", current_pass_count, min_passes_found).unwrap_or_default();
                }
                // If this pass fails, num_pcs_computed_at_convergence should ideally hold the value from the *actual* converging pass,
                // or from the last successful pass if convergence was never met.
                // Since num_pcs_computed_at_convergence is only updated if min_passes_found is -1 (i.e., before convergence is met),
                // it will correctly hold the PC count of the *first* converging pass if convergence happens.
                // If convergence never happens, it holds PC count of last successful run. If all fail, it's 0.
            }
        }
    }

    if min_passes_found == -1 {
        writeln!(
            overall_outcome_details,
            "\n--- High quality NOT ACHIEVED within {} passes. ---",
            max_passes_to_test
        )
        .unwrap_or_default();
        // If no convergence, num_pcs_computed_at_convergence is from the last successful run, or 0 if all failed.
    } else {
        writeln!(
            overall_outcome_details,
            "\n--- Minimum passes for convergence: {}. PCs computed in that run: {} ---",
            min_passes_found, num_pcs_computed_at_convergence
        )
        .unwrap_or_default();
    }

    // 7. Logging & Assertion
    let expected_max_passes_for_convergence = 2;
    let success = min_passes_found != -1 && min_passes_found <= expected_max_passes_for_convergence;

    let record = TestResultRecord {
        test_name: test_logging_name.to_string(),
        num_features_d: d_total_snps,
        num_samples_n: n_samples,
        num_pcs_requested_k: k_true_components,
        num_pcs_computed: num_pcs_computed_at_convergence,
        success,
        outcome_details: overall_outcome_details.clone(),
        notes: format!(
            "Min passes found for convergence: {}. Expected <= {}. Thresholds: ScoreCor >= {:.3}, LoadCor >= {:.3}, EigAcc (-MSRE) >= {:.3e}",
            min_passes_found, expected_max_passes_for_convergence,
            thresholds.min_score_correlation, thresholds.min_loading_correlation, thresholds.max_neg_eigenvalue_accuracy
        ),
    };
    TEST_RESULTS.lock().unwrap().push(record);

    assert!(
        success,
        "Minimum passes for quality convergence test failed. Min passes found: {}. Expected <= {}. Details:\n{}",
        min_passes_found, expected_max_passes_for_convergence, overall_outcome_details
    );
}

#[test]
fn test_refinement_projection_accuracy() {
    let test_logging_name = "test_refinement_projection_accuracy";

    // 1. Data Generation & Splitting
    let d_total_snps = 5000;
    let n_samples_total = 600;
    let k_true_components = 10;
    let n_samples_train = 500;
    let n_samples_test = n_samples_total - n_samples_train;
    let signal_strength = 3.0;
    let noise_std_dev = 1.0;
    let seed = 20241004;

    let structured_standardized_data_total = generate_structured_data(
        d_total_snps,
        n_samples_total,
        k_true_components,
        signal_strength,
        noise_std_dev,
        seed,
    );

    let train_data = structured_standardized_data_total
        .slice(s![.., 0..n_samples_train])
        .to_owned();
    let test_data_snps_x_samples = structured_standardized_data_total
        .slice(s![.., n_samples_train..])
        .to_owned();

    // 2. Artifact Directory
    let artifact_dir = Path::new("target/test_artifacts").join(test_logging_name);
    fs::create_dir_all(&artifact_dir).unwrap_or_else(|e| {
        panic!(
            "Failed to create artifact directory '{}': {}",
            artifact_dir.display(),
            e
        )
    });

    // 3. Reference Projected Scores (from Python on total data)
    let k_components_to_request_pca = k_true_components + 5;
    let python_total_pca_result = get_python_reference_pca(
        &structured_standardized_data_total,
        k_components_to_request_pca,
        &format!("{}_py_ref_total", test_logging_name),
    );

    let (_py_total_loadings_k_x_d, py_total_scores_n_x_k, _py_total_eigenvalues_k) =
        match python_total_pca_result {
            Ok(output) => output,
            Err(e) => {
                panic!("Failed to get Python reference PCA on total data: {}", e);
            }
        };

    // Ensure py_total_scores_n_x_k has enough rows for the test set slice
    if py_total_scores_n_x_k.nrows() < n_samples_total {
        panic!(
            "Python reference scores have fewer rows ({}) than n_samples_total ({}). Cannot slice test set scores.",
            py_total_scores_n_x_k.nrows(), n_samples_total
        );
    }
    // Ensure py_total_scores_n_x_k has columns before trying to slice
    if py_total_scores_n_x_k.ncols() == 0 && k_components_to_request_pca > 0 {
        // This case might indicate an issue with Python PCA if components were expected
        // For the purpose of this test, if no components, ref_projected_scores_for_test_set will have 0 columns
        // which should be handled gracefully by calculate_avg_abs_correlation.
        eprintln!("Warning: Python reference PCA on total data resulted in 0 components.");
    }

    let ref_projected_scores_for_test_set = py_total_scores_n_x_k
        .slice(s![n_samples_train.., ..])
        .to_owned();
    save_matrix_to_tsv(
        &ref_projected_scores_for_test_set.view(),
        artifact_dir.to_str().unwrap(),
        "python_ref_projected_test_scores.tsv",
    )
    .expect("Failed to save python_ref_projected_test_scores.tsv");

    // 4. Helper Closure for Eigensnp & Projection
    let run_eigensnp_and_project = |pass_count: usize,
                                    run_tag: &str|
     -> Result<(Array2<f32>, EigenSNPCoreOutput), String> {
        // Config (using some defaults as in previous tests)
        let config = EigenSNPCoreAlgorithmConfig {
            target_num_global_pcs: k_true_components, // Requesting k_true_components
            refine_pass_count: pass_count,
            random_seed: seed,
            subset_factor_for_local_basis_learning: 0.5,
            min_subset_size_for_local_basis_learning: (n_samples_train / 4)
                .max(1)
                .min(n_samples_train.max(1)),
            max_subset_size_for_local_basis_learning: (n_samples_train / 2)
                .max(10)
                .min(n_samples_train.max(1)),
            components_per_ld_block: 10
                .min(d_total_snps.min((n_samples_train / 2).max(10).min(n_samples_train.max(1)))),
            ..Default::default()
        };

        let test_data_accessor_train = TestDataAccessor::new(train_data.clone()); // Clone train_data for accessor
        let ld_block_specs_train = vec![LdBlockSpecification {
            user_defined_block_tag: "full_block_train".to_string(),
            pca_snp_ids_in_block: (0..d_total_snps).map(PcaSnpId).collect(),
        }];

        let algorithm = EigenSNPCoreAlgorithm::new(config);
        let snp_metadata = create_dummy_snp_metadata(d_total_snps);
        match algorithm.compute_pca(
            &test_data_accessor_train,
            &ld_block_specs_train,
            &snp_metadata,
        ) {
            Ok((eigensnp_train_output_struct, _)) => {
                save_matrix_to_tsv(
                    &eigensnp_train_output_struct
                        .final_snp_principal_component_loadings
                        .view(),
                    artifact_dir.to_str().unwrap(),
                    &format!("eigensnp_train_loadings_{}.tsv", run_tag),
                )
                .map_err(|e| format!("Failed to save train_loadings for {}: {}", run_tag, e))?;

                save_matrix_to_tsv(
                    &eigensnp_train_output_struct
                        .final_sample_principal_component_scores
                        .view(),
                    artifact_dir.to_str().unwrap(),
                    &format!("eigensnp_train_scores_{}.tsv", run_tag),
                )
                .map_err(|e| format!("Failed to save train_scores for {}: {}", run_tag, e))?;

                // Projection: (N_test x D_snps) dot (D_snps x K_eigensnp) -> N_test x K_eigensnp
                let projected_scores = if eigensnp_train_output_struct
                    .final_snp_principal_component_loadings
                    .ncols()
                    > 0
                {
                    test_data_snps_x_samples
                        .t()
                        .dot(&eigensnp_train_output_struct.final_snp_principal_component_loadings)
                } else {
                    // If no loadings (K_eigensnp = 0), projected scores matrix should have n_samples_test rows and 0 columns.
                    Array2::zeros((n_samples_test, 0))
                };

                save_matrix_to_tsv(
                    &projected_scores.view(),
                    artifact_dir.to_str().unwrap(),
                    &format!("eigensnp_projected_test_scores_{}.tsv", run_tag),
                )
                .map_err(|e| format!("Failed to save projected_scores for {}: {}", run_tag, e))?;

                Ok((projected_scores, eigensnp_train_output_struct))
            }
            Err(e) => Err(format!(
                "EigenSnp compute_pca failed for {}: {}",
                run_tag, e
            )),
        }
    };

    // 5. Run for Pass Count A & B
    let (projected_scores_a, _output_a) =
        run_eigensnp_and_project(1, "pass1") // _output_a is EigenSNPCoreOutput if needed later
            .unwrap_or_else(|e| panic!("Eigensnp run/projection A (pass1) failed: {}", e));

    let (projected_scores_b, _output_b) =
        run_eigensnp_and_project(2, "pass2") // _output_b is EigenSNPCoreOutput
            .unwrap_or_else(|e| panic!("Eigensnp run/projection B (pass2) failed: {}", e));

    // 6. Calculate Correlations
    let calculate_avg_abs_correlation =
        |projected_scores: &Array2<f32>, ref_scores: &Array2<f32>, k_compare_max: usize| -> f64 {
            let k_compare = projected_scores
                .ncols()
                .min(ref_scores.ncols())
                .min(k_compare_max);
            if k_compare == 0 {
                return 0.0;
            }
            let mut total_abs_corr = 0.0;
            for i in 0..k_compare {
                let proj_col = projected_scores.column(i);
                let ref_col = ref_scores.column(i);
                total_abs_corr += pearson_correlation(proj_col.view(), ref_col.view())
                    .map_or(0.0, |c| c.abs() as f64);
            }
            total_abs_corr / (k_compare as f64)
        };

    let corr_a = calculate_avg_abs_correlation(
        &projected_scores_a,
        &ref_projected_scores_for_test_set,
        k_true_components,
    );
    let corr_b = calculate_avg_abs_correlation(
        &projected_scores_b,
        &ref_projected_scores_for_test_set,
        k_true_components,
    );

    // 7. Logging & Assertion
    let success = corr_b >= corr_a - 1e-9;
    let mut outcome_details = String::new();
    writeln!(outcome_details, "Projection Accuracy Test Results:").unwrap_or_default();
    writeln!(outcome_details, "  Correlation A (1 pass): {:.6e}", corr_a).unwrap_or_default();
    writeln!(
        outcome_details,
        "  Correlation B (2 passes): {:.6e}",
        corr_b
    )
    .unwrap_or_default();
    if success {
        writeln!(
            outcome_details,
            "  Improvement OBSERVED or non-degradation within tolerance."
        )
        .unwrap_or_default();
    } else {
        writeln!(
            outcome_details,
            "  Improvement NOT OBSERVED. Corr_B < Corr_A - tolerance."
        )
        .unwrap_or_default();
    }

    let num_pcs_computed_log = _output_b
        .num_principal_components_computed
        .max(_output_a.num_principal_components_computed);

    let record = TestResultRecord {
        test_name: test_logging_name.to_string(),
        num_features_d: d_total_snps,
        num_samples_n: n_samples_total, // Log total samples used in generating data
        num_pcs_requested_k: k_true_components, // PCs requested from eigensnp on train set
        num_pcs_computed: num_pcs_computed_log, // Max PCs computed by eigensnp runs
        success,
        outcome_details: outcome_details.clone(),
        notes: format!(
            "Train_N={}, Test_N={}, Seed={}. Python_Ref_PCs_Requested={}. Corr_A={:.4e}, Corr_B={:.4e}.",
            n_samples_train, n_samples_test, seed, k_components_to_request_pca, corr_a, corr_b
        ),
    };
    TEST_RESULTS.lock().unwrap().push(record);

    assert!(
        success,
        "Projection accuracy did not improve with more refinement. Corr_A: {:.6e}, Corr_B: {:.6e}. Details: {}",
        corr_a, corr_b, outcome_details
    );
}

#[test]
fn test_reorder_columns_empty_matrix_variants() {
    let original_0_rows = Array2::<i32>::zeros((0, 3));
    let order = vec![1, 0, 2];
    let expected_0_rows = Array2::<i32>::zeros((0, 3));
    let reordered_0_rows = reorder_columns_owned(&original_0_rows, &order);
    assert_eq!(reordered_0_rows, expected_0_rows);

    let original_0_cols = Array2::<i32>::zeros((2, 0));
    let order_empty = vec![];
    let expected_0_cols_empty_order = Array2::<i32>::zeros((2, 0));
    let reordered_empty_order = reorder_columns_owned(&original_0_cols, &order_empty);
    assert_eq!(reordered_empty_order, expected_0_cols_empty_order);
}

#[test]
#[should_panic]
fn test_reorder_columns_select_from_zero_cols_panics() {
    let original_0_cols = Array2::<i32>::zeros((2, 0));
    let order_for_0_cols = vec![0];
    reorder_columns_owned(&original_0_cols, &order_for_0_cols);
}

#[test]
fn test_reorder_columns_empty_order() {
    let original = arr2(&[[1, 2, 3], [4, 5, 6]]);
    let order = vec![];
    let expected = Array2::<i32>::zeros((2, 0));
    let reordered = reorder_columns_owned(&original, &order);
    assert_eq!(reordered, expected);
}

#[test]
fn test_reorder_columns_repeated_indices() {
    let original = arr2(&[[1, 2], [3, 4]]);
    let order = vec![0, 1, 0, 0];
    let expected = arr2(&[[1, 2, 1, 1], [3, 4, 3, 3]]);
    let reordered = reorder_columns_owned(&original, &order);
    assert_eq!(reordered, expected);
}

use std::sync::Arc;

// Helper function to create dummy SNP metadata for tests
fn create_dummy_snp_metadata(num_snps: usize) -> Vec<PcaSnpMetadata> {
    (0..num_snps)
        .map(|i| PcaSnpMetadata {
            id: Arc::new(format!("snp_{}", i)),
            chr: Arc::new("chr1".to_string()),
            pos: i as u64 * 1000 + 100000, // Simple position calculation
        })
        .collect()
}