tsai_train 0.1.2

Training loop, callbacks, metrics, and checkpointing for tsai-rs
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
//! Callback system for training hooks.

use std::collections::HashMap;
use std::path::PathBuf;

use crate::error::Result;

/// Context passed to callbacks containing training state.
pub struct CallbackContext {
    /// Current epoch (0-indexed).
    pub epoch: usize,
    /// Total number of epochs.
    pub n_epochs: usize,
    /// Current batch (0-indexed).
    pub batch: usize,
    /// Total number of batches in epoch.
    pub n_batches: usize,
    /// Current learning rate.
    pub lr: f64,
    /// Current training loss.
    pub train_loss: Option<f32>,
    /// Current validation loss.
    pub valid_loss: Option<f32>,
    /// Current metrics.
    pub metrics: HashMap<String, f32>,
    /// Whether to stop training.
    pub stop_training: bool,
    /// Whether to skip this batch.
    pub skip_batch: bool,
}

impl CallbackContext {
    /// Create a new callback context.
    pub fn new(n_epochs: usize, n_batches: usize) -> Self {
        Self {
            epoch: 0,
            n_epochs,
            batch: 0,
            n_batches,
            lr: 0.0,
            train_loss: None,
            valid_loss: None,
            metrics: HashMap::new(),
            stop_training: false,
            skip_batch: false,
        }
    }

    /// Get progress as a fraction (0.0 to 1.0).
    pub fn progress(&self) -> f32 {
        let total_batches = self.n_epochs * self.n_batches;
        let current = self.epoch * self.n_batches + self.batch;
        current as f32 / total_batches as f32
    }
}

/// Trait for training callbacks.
///
/// Callbacks allow customization of the training loop at various points.
pub trait Callback: Send + Sync {
    /// Called before training starts.
    fn before_fit(&mut self, _ctx: &mut CallbackContext) -> Result<()> {
        Ok(())
    }

    /// Called after training completes.
    fn after_fit(&mut self, _ctx: &mut CallbackContext) -> Result<()> {
        Ok(())
    }

    /// Called before each epoch.
    fn before_epoch(&mut self, _ctx: &mut CallbackContext) -> Result<()> {
        Ok(())
    }

    /// Called after each epoch.
    fn after_epoch(&mut self, _ctx: &mut CallbackContext) -> Result<()> {
        Ok(())
    }

    /// Called before each training batch.
    fn before_batch(&mut self, _ctx: &mut CallbackContext) -> Result<()> {
        Ok(())
    }

    /// Called after each training batch.
    fn after_batch(&mut self, _ctx: &mut CallbackContext) -> Result<()> {
        Ok(())
    }

    /// Called before validation.
    fn before_validate(&mut self, _ctx: &mut CallbackContext) -> Result<()> {
        Ok(())
    }

    /// Called after validation.
    fn after_validate(&mut self, _ctx: &mut CallbackContext) -> Result<()> {
        Ok(())
    }

    /// Get the callback name.
    fn name(&self) -> &str {
        std::any::type_name::<Self>()
    }
}

/// A list of callbacks.
#[derive(Default)]
pub struct CallbackList {
    callbacks: Vec<Box<dyn Callback>>,
}

impl CallbackList {
    /// Create a new empty callback list.
    pub fn new() -> Self {
        Self {
            callbacks: Vec::new(),
        }
    }

    /// Add a callback.
    pub fn add<C: Callback + 'static>(&mut self, callback: C) {
        self.callbacks.push(Box::new(callback));
    }

    /// Call before_fit on all callbacks.
    pub fn before_fit(&mut self, ctx: &mut CallbackContext) -> Result<()> {
        for cb in &mut self.callbacks {
            cb.before_fit(ctx)?;
        }
        Ok(())
    }

    /// Call after_fit on all callbacks.
    pub fn after_fit(&mut self, ctx: &mut CallbackContext) -> Result<()> {
        for cb in &mut self.callbacks {
            cb.after_fit(ctx)?;
        }
        Ok(())
    }

    /// Call before_epoch on all callbacks.
    pub fn before_epoch(&mut self, ctx: &mut CallbackContext) -> Result<()> {
        for cb in &mut self.callbacks {
            cb.before_epoch(ctx)?;
        }
        Ok(())
    }

    /// Call after_epoch on all callbacks.
    pub fn after_epoch(&mut self, ctx: &mut CallbackContext) -> Result<()> {
        for cb in &mut self.callbacks {
            cb.after_epoch(ctx)?;
        }
        Ok(())
    }

    /// Call before_batch on all callbacks.
    pub fn before_batch(&mut self, ctx: &mut CallbackContext) -> Result<()> {
        for cb in &mut self.callbacks {
            cb.before_batch(ctx)?;
        }
        Ok(())
    }

    /// Call after_batch on all callbacks.
    pub fn after_batch(&mut self, ctx: &mut CallbackContext) -> Result<()> {
        for cb in &mut self.callbacks {
            cb.after_batch(ctx)?;
        }
        Ok(())
    }

    /// Call before_validate on all callbacks.
    pub fn before_validate(&mut self, ctx: &mut CallbackContext) -> Result<()> {
        for cb in &mut self.callbacks {
            cb.before_validate(ctx)?;
        }
        Ok(())
    }

    /// Call after_validate on all callbacks.
    pub fn after_validate(&mut self, ctx: &mut CallbackContext) -> Result<()> {
        for cb in &mut self.callbacks {
            cb.after_validate(ctx)?;
        }
        Ok(())
    }
}

/// Progress bar callback for displaying training progress.
pub struct ProgressCallback {
    /// Whether to show batch-level progress (reserved for future use).
    #[allow(dead_code)]
    show_batch: bool,
}

impl ProgressCallback {
    /// Create a new progress callback.
    pub fn new(show_batch: bool) -> Self {
        Self { show_batch }
    }
}

impl Callback for ProgressCallback {
    fn before_fit(&mut self, ctx: &mut CallbackContext) -> Result<()> {
        tracing::info!("Starting training for {} epochs", ctx.n_epochs);
        Ok(())
    }

    fn after_epoch(&mut self, ctx: &mut CallbackContext) -> Result<()> {
        let train_loss = ctx.train_loss.map(|l| format!("{:.4}", l)).unwrap_or_default();
        let valid_loss = ctx.valid_loss.map(|l| format!("{:.4}", l)).unwrap_or_default();

        tracing::info!(
            "Epoch {}/{}: train_loss={}, valid_loss={}, lr={:.6}",
            ctx.epoch + 1,
            ctx.n_epochs,
            train_loss,
            valid_loss,
            ctx.lr
        );

        for (name, value) in &ctx.metrics {
            tracing::info!("  {}: {:.4}", name, value);
        }

        Ok(())
    }

    fn after_fit(&mut self, _ctx: &mut CallbackContext) -> Result<()> {
        tracing::info!("Training completed");
        Ok(())
    }

    fn name(&self) -> &str {
        "ProgressCallback"
    }
}

/// Early stopping callback.
pub struct EarlyStoppingCallback {
    patience: usize,
    min_delta: f32,
    best_loss: f32,
    counter: usize,
    mode: EarlyStoppingMode,
}

/// Mode for early stopping.
pub enum EarlyStoppingMode {
    /// Stop when validation loss stops decreasing.
    Min,
    /// Stop when validation metric stops increasing.
    Max,
}

impl EarlyStoppingCallback {
    /// Create a new early stopping callback.
    pub fn new(patience: usize, min_delta: f32, mode: EarlyStoppingMode) -> Self {
        let best_loss = match mode {
            EarlyStoppingMode::Min => f32::INFINITY,
            EarlyStoppingMode::Max => f32::NEG_INFINITY,
        };

        Self {
            patience,
            min_delta,
            best_loss,
            counter: 0,
            mode,
        }
    }
}

impl Callback for EarlyStoppingCallback {
    fn after_epoch(&mut self, ctx: &mut CallbackContext) -> Result<()> {
        let current = ctx.valid_loss.unwrap_or(f32::INFINITY);

        let improved = match self.mode {
            EarlyStoppingMode::Min => current < self.best_loss - self.min_delta,
            EarlyStoppingMode::Max => current > self.best_loss + self.min_delta,
        };

        if improved {
            self.best_loss = current;
            self.counter = 0;
        } else {
            self.counter += 1;
            if self.counter >= self.patience {
                tracing::info!(
                    "Early stopping triggered after {} epochs without improvement",
                    self.patience
                );
                ctx.stop_training = true;
            }
        }

        Ok(())
    }

    fn name(&self) -> &str {
        "EarlyStoppingCallback"
    }
}

/// Mode for model saving.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SaveModelMode {
    /// Save when validation loss improves (lower is better).
    Min,
    /// Save when validation metric improves (higher is better).
    Max,
    /// Save after every epoch.
    Every,
}

/// Callback for saving model checkpoints.
///
/// This callback saves model state after each epoch when the monitored
/// metric improves. It creates checkpoint files in the specified directory.
///
/// # Example
///
/// ```rust,ignore
/// use tsai_train::callback::{SaveModelCallback, SaveModelMode};
///
/// let callback = SaveModelCallback::new("./checkpoints", SaveModelMode::Min)
///     .with_metric("valid_loss");
/// ```
pub struct SaveModelCallback {
    /// Directory to save checkpoints.
    save_dir: PathBuf,
    /// Mode for determining improvement.
    mode: SaveModelMode,
    /// Best metric value seen so far.
    best_value: f32,
    /// Metric to monitor (default: validation loss).
    metric_name: Option<String>,
    /// Whether to save only the best model or all.
    save_best_only: bool,
    /// Filename prefix for checkpoints.
    filename_prefix: String,
    /// Epoch of the best model.
    best_epoch: usize,
}

impl SaveModelCallback {
    /// Create a new save model callback.
    ///
    /// # Arguments
    ///
    /// * `save_dir` - Directory to save checkpoints
    /// * `mode` - When to save (min loss, max metric, or every epoch)
    pub fn new<P: Into<PathBuf>>(save_dir: P, mode: SaveModelMode) -> Self {
        let best_value = match mode {
            SaveModelMode::Min => f32::INFINITY,
            SaveModelMode::Max => f32::NEG_INFINITY,
            SaveModelMode::Every => 0.0,
        };

        Self {
            save_dir: save_dir.into(),
            mode,
            best_value,
            metric_name: None,
            save_best_only: true,
            filename_prefix: "checkpoint".to_string(),
            best_epoch: 0,
        }
    }

    /// Set the metric name to monitor.
    ///
    /// If not set, uses validation loss.
    #[must_use]
    pub fn with_metric(mut self, name: &str) -> Self {
        self.metric_name = Some(name.to_string());
        self
    }

    /// Set whether to save only the best model.
    ///
    /// If false, saves a checkpoint after every epoch.
    #[must_use]
    pub fn save_best_only(mut self, value: bool) -> Self {
        self.save_best_only = value;
        self
    }

    /// Set the filename prefix for checkpoints.
    #[must_use]
    pub fn with_prefix(mut self, prefix: &str) -> Self {
        self.filename_prefix = prefix.to_string();
        self
    }

    /// Get the path to the best checkpoint.
    pub fn best_checkpoint_path(&self) -> PathBuf {
        self.save_dir.join(format!("{}_best.json", self.filename_prefix))
    }

    /// Get the path to a specific epoch's checkpoint.
    pub fn epoch_checkpoint_path(&self, epoch: usize) -> PathBuf {
        self.save_dir
            .join(format!("{}_epoch_{}.json", self.filename_prefix, epoch))
    }

    /// Get the epoch of the best model.
    pub fn best_epoch(&self) -> usize {
        self.best_epoch
    }

    /// Get the best metric value.
    pub fn best_value(&self) -> f32 {
        self.best_value
    }

    fn get_current_value(&self, ctx: &CallbackContext) -> Option<f32> {
        if let Some(ref metric_name) = self.metric_name {
            ctx.metrics.get(metric_name).copied()
        } else {
            ctx.valid_loss
        }
    }

    fn should_save(&self, current: f32) -> bool {
        match self.mode {
            SaveModelMode::Min => current < self.best_value,
            SaveModelMode::Max => current > self.best_value,
            SaveModelMode::Every => true,
        }
    }

    fn save_checkpoint(&self, ctx: &CallbackContext, is_best: bool) -> Result<()> {
        // Create save directory if it doesn't exist
        std::fs::create_dir_all(&self.save_dir).map_err(|e| {
            crate::error::TrainError::CheckpointError(format!(
                "Failed to create checkpoint directory: {}",
                e
            ))
        })?;

        // Create checkpoint metadata
        let checkpoint = CheckpointMetadata {
            epoch: ctx.epoch,
            train_loss: ctx.train_loss,
            valid_loss: ctx.valid_loss,
            metrics: ctx.metrics.clone(),
            is_best,
        };

        // Save epoch checkpoint
        let epoch_path = self.epoch_checkpoint_path(ctx.epoch);
        let json = serde_json::to_string_pretty(&checkpoint).map_err(|e| {
            crate::error::TrainError::SerializationError(format!(
                "Failed to serialize checkpoint: {}",
                e
            ))
        })?;
        std::fs::write(&epoch_path, json).map_err(|e| {
            crate::error::TrainError::CheckpointError(format!("Failed to write checkpoint: {}", e))
        })?;

        // If this is the best, also save as best checkpoint
        if is_best {
            let best_path = self.best_checkpoint_path();
            std::fs::copy(&epoch_path, &best_path).map_err(|e| {
                crate::error::TrainError::CheckpointError(format!(
                    "Failed to copy best checkpoint: {}",
                    e
                ))
            })?;
        }

        Ok(())
    }
}

/// Metadata stored in checkpoint files.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct CheckpointMetadata {
    /// Epoch number.
    pub epoch: usize,
    /// Training loss at checkpoint.
    pub train_loss: Option<f32>,
    /// Validation loss at checkpoint.
    pub valid_loss: Option<f32>,
    /// Metrics at checkpoint.
    pub metrics: HashMap<String, f32>,
    /// Whether this was the best checkpoint.
    pub is_best: bool,
}

impl Callback for SaveModelCallback {
    fn before_fit(&mut self, _ctx: &mut CallbackContext) -> Result<()> {
        // Create the save directory at the start of training
        std::fs::create_dir_all(&self.save_dir).map_err(|e| {
            crate::error::TrainError::CheckpointError(format!(
                "Failed to create checkpoint directory: {}",
                e
            ))
        })?;
        tracing::info!("Checkpoints will be saved to: {:?}", self.save_dir);
        Ok(())
    }

    fn after_epoch(&mut self, ctx: &mut CallbackContext) -> Result<()> {
        let Some(current) = self.get_current_value(ctx) else {
            return Ok(());
        };

        let is_best = self.should_save(current);

        if is_best {
            self.best_value = current;
            self.best_epoch = ctx.epoch;
        }

        // Save checkpoint based on settings
        if !self.save_best_only || is_best {
            self.save_checkpoint(ctx, is_best)?;

            if is_best {
                let metric_display = self
                    .metric_name
                    .as_deref()
                    .unwrap_or("valid_loss");
                tracing::info!(
                    "Epoch {}: {} improved to {:.4}, saving checkpoint",
                    ctx.epoch + 1,
                    metric_display,
                    current
                );
            }
        }

        Ok(())
    }

    fn after_fit(&mut self, _ctx: &mut CallbackContext) -> Result<()> {
        tracing::info!(
            "Best model from epoch {} with value {:.4}",
            self.best_epoch + 1,
            self.best_value
        );
        Ok(())
    }

    fn name(&self) -> &str {
        "SaveModelCallback"
    }
}

/// Configuration for gradient clipping.
#[derive(Debug, Clone, Copy)]
pub enum GradientClipMode {
    /// Clip gradients by value: all gradients are clipped to [-value, value].
    Value(f32),
    /// Clip gradients by norm: if total norm exceeds max_norm, scale all gradients.
    Norm(f32),
}

/// Callback for gradient clipping during training.
///
/// Gradient clipping helps prevent exploding gradients, which can cause
/// unstable training. This is especially useful for RNNs and transformers.
///
/// # Example
///
/// ```rust,ignore
/// use tsai_train::callback::{GradientClipCallback, GradientClipMode};
///
/// // Clip gradient norms to max 1.0
/// let callback = GradientClipCallback::new(GradientClipMode::Norm(1.0));
///
/// // Or clip individual gradient values
/// let callback = GradientClipCallback::new(GradientClipMode::Value(0.5));
/// ```
pub struct GradientClipCallback {
    mode: GradientClipMode,
    clip_count: usize,
    total_batches: usize,
}

impl GradientClipCallback {
    /// Create a new gradient clipping callback.
    pub fn new(mode: GradientClipMode) -> Self {
        Self {
            mode,
            clip_count: 0,
            total_batches: 0,
        }
    }

    /// Create a gradient clipping callback with norm clipping.
    pub fn by_norm(max_norm: f32) -> Self {
        Self::new(GradientClipMode::Norm(max_norm))
    }

    /// Create a gradient clipping callback with value clipping.
    pub fn by_value(max_value: f32) -> Self {
        Self::new(GradientClipMode::Value(max_value))
    }

    /// Get the clipping mode.
    pub fn mode(&self) -> GradientClipMode {
        self.mode
    }

    /// Get the max norm/value for clipping.
    pub fn clip_value(&self) -> f32 {
        match self.mode {
            GradientClipMode::Value(v) => v,
            GradientClipMode::Norm(n) => n,
        }
    }
}

impl Callback for GradientClipCallback {
    fn before_fit(&mut self, _ctx: &mut CallbackContext) -> Result<()> {
        self.clip_count = 0;
        self.total_batches = 0;
        Ok(())
    }

    fn after_batch(&mut self, _ctx: &mut CallbackContext) -> Result<()> {
        // Gradient clipping would be applied by the learner
        // This callback just tracks statistics
        self.total_batches += 1;
        Ok(())
    }

    fn after_fit(&mut self, _ctx: &mut CallbackContext) -> Result<()> {
        if self.total_batches > 0 {
            let clip_rate = self.clip_count as f32 / self.total_batches as f32 * 100.0;
            if clip_rate > 0.0 {
                tracing::info!(
                    "Gradient clipping was applied in {:.1}% of batches",
                    clip_rate
                );
            }
        }
        Ok(())
    }

    fn name(&self) -> &str {
        "GradientClipCallback"
    }
}

/// Callback for logging training history.
///
/// Records all training metrics for later analysis or visualization.
#[derive(Default)]
pub struct HistoryCallback {
    train_losses: Vec<f32>,
    valid_losses: Vec<f32>,
    learning_rates: Vec<f64>,
    metrics_history: HashMap<String, Vec<f32>>,
}

impl HistoryCallback {
    /// Create a new history callback.
    pub fn new() -> Self {
        Self::default()
    }

    /// Get the training loss history.
    pub fn train_losses(&self) -> &[f32] {
        &self.train_losses
    }

    /// Get the validation loss history.
    pub fn valid_losses(&self) -> &[f32] {
        &self.valid_losses
    }

    /// Get the learning rate history.
    pub fn learning_rates(&self) -> &[f64] {
        &self.learning_rates
    }

    /// Get the history for a specific metric.
    pub fn metric_history(&self, name: &str) -> Option<&[f32]> {
        self.metrics_history.get(name).map(|v| v.as_slice())
    }

    /// Get all metric names.
    pub fn metric_names(&self) -> Vec<&str> {
        self.metrics_history.keys().map(|s| s.as_str()).collect()
    }

    /// Get the best epoch based on validation loss (minimum).
    pub fn best_epoch(&self) -> Option<usize> {
        self.valid_losses
            .iter()
            .enumerate()
            .min_by(|(_, a), (_, b)| a.partial_cmp(b).unwrap())
            .map(|(i, _)| i)
    }
}

impl Callback for HistoryCallback {
    fn after_epoch(&mut self, ctx: &mut CallbackContext) -> Result<()> {
        if let Some(loss) = ctx.train_loss {
            self.train_losses.push(loss);
        }
        if let Some(loss) = ctx.valid_loss {
            self.valid_losses.push(loss);
        }
        self.learning_rates.push(ctx.lr);

        for (name, &value) in &ctx.metrics {
            self.metrics_history
                .entry(name.clone())
                .or_default()
                .push(value);
        }

        Ok(())
    }

    fn name(&self) -> &str {
        "HistoryCallback"
    }
}

/// Callback for mixed precision training.
///
/// Tracks loss scaling factor and overflow events for automatic
/// mixed precision (AMP) training.
pub struct MixedPrecisionCallback {
    initial_scale: f32,
    current_scale: f32,
    growth_factor: f32,
    backoff_factor: f32,
    growth_interval: usize,
    batches_since_rescale: usize,
    overflow_count: usize,
}

impl MixedPrecisionCallback {
    /// Create a new mixed precision callback.
    pub fn new(initial_scale: f32) -> Self {
        Self {
            initial_scale,
            current_scale: initial_scale,
            growth_factor: 2.0,
            backoff_factor: 0.5,
            growth_interval: 2000,
            batches_since_rescale: 0,
            overflow_count: 0,
        }
    }

    /// Get the current loss scale.
    pub fn current_scale(&self) -> f32 {
        self.current_scale
    }

    /// Report an overflow (nan/inf in gradients).
    pub fn report_overflow(&mut self) {
        self.overflow_count += 1;
        self.current_scale *= self.backoff_factor;
        self.batches_since_rescale = 0;
    }
}

impl Callback for MixedPrecisionCallback {
    fn before_fit(&mut self, _ctx: &mut CallbackContext) -> Result<()> {
        self.current_scale = self.initial_scale;
        self.batches_since_rescale = 0;
        self.overflow_count = 0;
        Ok(())
    }

    fn after_batch(&mut self, _ctx: &mut CallbackContext) -> Result<()> {
        self.batches_since_rescale += 1;

        // Try to grow scale periodically if no overflows
        if self.batches_since_rescale >= self.growth_interval {
            self.current_scale *= self.growth_factor;
            self.batches_since_rescale = 0;
        }

        Ok(())
    }

    fn after_fit(&mut self, _ctx: &mut CallbackContext) -> Result<()> {
        if self.overflow_count > 0 {
            tracing::info!(
                "Mixed precision: {} overflow events, final scale = {:.0}",
                self.overflow_count,
                self.current_scale
            );
        }
        Ok(())
    }

    fn name(&self) -> &str {
        "MixedPrecisionCallback"
    }
}

/// Callback for terminating training after a certain number of batches.
///
/// Useful for debugging or quick sanity checks.
pub struct TerminateOnNanCallback {
    nan_count: usize,
}

impl TerminateOnNanCallback {
    /// Create a new terminate on NaN callback.
    pub fn new() -> Self {
        Self { nan_count: 0 }
    }
}

impl Default for TerminateOnNanCallback {
    fn default() -> Self {
        Self::new()
    }
}

impl Callback for TerminateOnNanCallback {
    fn after_batch(&mut self, ctx: &mut CallbackContext) -> Result<()> {
        if let Some(loss) = ctx.train_loss {
            if loss.is_nan() || loss.is_infinite() {
                self.nan_count += 1;
                tracing::error!("NaN/Inf detected in training loss at batch {}", ctx.batch);
                ctx.stop_training = true;
            }
        }
        Ok(())
    }

    fn name(&self) -> &str {
        "TerminateOnNanCallback"
    }
}

/// Callback for displaying ASCII training graphs.
///
/// Shows training and validation loss curves in the terminal after each epoch.
/// Useful for quick visual feedback on training progress without external tools.
///
/// # Example
///
/// ```rust,ignore
/// use tsai_train::callback::ShowGraphCallback;
///
/// // Create with default settings (40x10 graph)
/// let callback = ShowGraphCallback::new();
///
/// // Or customize the display
/// let callback = ShowGraphCallback::new()
///     .with_width(60)
///     .with_height(15)
///     .with_metrics(vec!["accuracy"]);
/// ```
pub struct ShowGraphCallback {
    /// Training losses to plot.
    train_losses: Vec<f32>,
    /// Validation losses to plot.
    valid_losses: Vec<f32>,
    /// Additional metrics to track.
    metrics_history: HashMap<String, Vec<f32>>,
    /// Names of additional metrics to display.
    metric_names: Vec<String>,
    /// Graph width in characters.
    width: usize,
    /// Graph height in characters.
    height: usize,
    /// Whether to show after each epoch.
    show_per_epoch: bool,
}

impl Default for ShowGraphCallback {
    fn default() -> Self {
        Self {
            train_losses: Vec::new(),
            valid_losses: Vec::new(),
            metrics_history: HashMap::new(),
            metric_names: Vec::new(),
            width: 50,
            height: 10,
            show_per_epoch: true,
        }
    }
}

impl ShowGraphCallback {
    /// Create a new show graph callback with default settings.
    pub fn new() -> Self {
        Self::default()
    }

    /// Set the graph width in characters.
    #[must_use]
    pub fn with_width(mut self, width: usize) -> Self {
        self.width = width.max(20);
        self
    }

    /// Set the graph height in characters.
    #[must_use]
    pub fn with_height(mut self, height: usize) -> Self {
        self.height = height.max(5);
        self
    }

    /// Add metrics to display in addition to loss.
    #[must_use]
    pub fn with_metrics(mut self, names: Vec<&str>) -> Self {
        self.metric_names = names.into_iter().map(|s| s.to_string()).collect();
        self
    }

    /// Set whether to show the graph after each epoch.
    #[must_use]
    pub fn show_per_epoch(mut self, show: bool) -> Self {
        self.show_per_epoch = show;
        self
    }

    /// Render an ASCII graph for a set of values.
    fn render_graph(&self, label: &str, values: &[f32], color_start: &str, color_end: &str) -> String {
        if values.is_empty() {
            return String::new();
        }

        let mut output = String::new();

        // Find min/max for scaling
        let min_val = values.iter().cloned().fold(f32::INFINITY, f32::min);
        let max_val = values.iter().cloned().fold(f32::NEG_INFINITY, f32::max);
        let range = (max_val - min_val).max(1e-6);

        // Create graph header
        output.push_str(&format!("┌─ {} ", label));
        let header_remaining = self.width.saturating_sub(label.len() + 4);
        output.push_str(&"".repeat(header_remaining));
        output.push_str("\n");

        // Create the plot area
        let mut grid = vec![vec![' '; self.width]; self.height];

        // Map values to grid positions
        let step = values.len() as f32 / self.width as f32;
        for col in 0..self.width {
            let idx = (col as f32 * step) as usize;
            if idx < values.len() {
                let val = values[idx];
                let normalized = (val - min_val) / range;
                let row = ((1.0 - normalized) * (self.height - 1) as f32) as usize;
                let row = row.min(self.height - 1);
                grid[row][col] = '';
            }
        }

        // Render grid with axis labels
        for (i, row) in grid.iter().enumerate() {
            // Y-axis label
            if i == 0 {
                output.push_str(&format!("{:>6.3} ", max_val));
            } else if i == self.height - 1 {
                output.push_str(&format!("{:>6.3} ", min_val));
            } else {
                output.push_str("");
            }

            // Plot data with color
            output.push_str(color_start);
            for &ch in row {
                output.push(ch);
            }
            output.push_str(color_end);
            output.push_str("\n");
        }

        // Bottom border with epoch labels
        output.push_str("└───────");
        output.push_str(&"".repeat(self.width));
        output.push_str("\n");

        // X-axis label
        output.push_str(&format!("        Epochs: 1 → {}\n", values.len()));

        output
    }

    /// Display the training graphs.
    fn display_graphs(&self) {
        let mut output = String::new();
        output.push_str("\n╔══════════════════════════════════════════════════════════════╗\n");
        output.push_str("║                    Training Progress                         ║\n");
        output.push_str("╚══════════════════════════════════════════════════════════════╝\n\n");

        // Show training loss
        if !self.train_losses.is_empty() {
            output.push_str(&self.render_graph("Train Loss", &self.train_losses, "\x1b[33m", "\x1b[0m"));
            output.push('\n');
        }

        // Show validation loss
        if !self.valid_losses.is_empty() {
            output.push_str(&self.render_graph("Valid Loss", &self.valid_losses, "\x1b[36m", "\x1b[0m"));
            output.push('\n');
        }

        // Show additional metrics
        for name in &self.metric_names {
            if let Some(values) = self.metrics_history.get(name) {
                if !values.is_empty() {
                    output.push_str(&self.render_graph(name, values, "\x1b[32m", "\x1b[0m"));
                    output.push('\n');
                }
            }
        }

        // Current values summary
        output.push_str("Current Values:\n");
        if let Some(train) = self.train_losses.last() {
            output.push_str(&format!("  Train Loss: {:.4}\n", train));
        }
        if let Some(valid) = self.valid_losses.last() {
            output.push_str(&format!("  Valid Loss: {:.4}\n", valid));
        }
        for name in &self.metric_names {
            if let Some(values) = self.metrics_history.get(name) {
                if let Some(val) = values.last() {
                    output.push_str(&format!("  {}: {:.4}\n", name, val));
                }
            }
        }

        // Print to stdout
        print!("{}", output);
    }
}

impl Callback for ShowGraphCallback {
    fn before_fit(&mut self, _ctx: &mut CallbackContext) -> Result<()> {
        self.train_losses.clear();
        self.valid_losses.clear();
        self.metrics_history.clear();
        tracing::info!("ShowGraph enabled - will display training curves");
        Ok(())
    }

    fn after_epoch(&mut self, ctx: &mut CallbackContext) -> Result<()> {
        // Record losses
        if let Some(loss) = ctx.train_loss {
            self.train_losses.push(loss);
        }
        if let Some(loss) = ctx.valid_loss {
            self.valid_losses.push(loss);
        }

        // Record tracked metrics
        for name in &self.metric_names {
            if let Some(&value) = ctx.metrics.get(name) {
                self.metrics_history
                    .entry(name.clone())
                    .or_default()
                    .push(value);
            }
        }

        // Display if enabled
        if self.show_per_epoch {
            self.display_graphs();
        }

        Ok(())
    }

    fn after_fit(&mut self, _ctx: &mut CallbackContext) -> Result<()> {
        // Always show final graph
        if !self.show_per_epoch && (!self.train_losses.is_empty() || !self.valid_losses.is_empty()) {
            self.display_graphs();
        }
        Ok(())
    }

    fn name(&self) -> &str {
        "ShowGraphCallback"
    }
}

/// Transform schedule type for controlling when augmentations are applied.
#[derive(Debug, Clone)]
pub enum TransformSchedule {
    /// Fixed probability throughout training.
    Constant(f32),
    /// Linear warmup from 0 to max probability over n epochs.
    LinearWarmup {
        /// Max probability to reach.
        max_p: f32,
        /// Number of warmup epochs.
        warmup_epochs: usize,
    },
    /// Linear cooldown from max to 0 over last n epochs.
    LinearCooldown {
        /// Starting probability.
        max_p: f32,
        /// Number of cooldown epochs.
        cooldown_epochs: usize,
    },
    /// Cosine annealing between min and max probability.
    CosineAnnealing {
        /// Minimum probability.
        min_p: f32,
        /// Maximum probability.
        max_p: f32,
    },
    /// Step-wise schedule: probability changes at specific epochs.
    Step {
        /// List of (epoch, probability) pairs.
        schedule: Vec<(usize, f32)>,
    },
    /// Start augmentation only after a certain epoch.
    DelayedStart {
        /// Probability after start.
        p: f32,
        /// Epoch to start at.
        start_epoch: usize,
    },
}

/// Callback for scheduling transform/augmentation probabilities during training.
///
/// This callback adjusts the probability of data augmentation transforms
/// based on the current epoch, following a specified schedule.
///
/// # Example
///
/// ```rust,ignore
/// use tsai_train::callback::{TransformSchedulerCallback, TransformSchedule};
///
/// // Start at 0 probability and warm up to 0.8 over 5 epochs
/// let callback = TransformSchedulerCallback::new("GaussianNoise", TransformSchedule::LinearWarmup {
///     max_p: 0.8,
///     warmup_epochs: 5,
/// });
///
/// // Or use delayed start
/// let callback = TransformSchedulerCallback::new("CutOut", TransformSchedule::DelayedStart {
///     p: 0.5,
///     start_epoch: 10,
/// });
/// ```
pub struct TransformSchedulerCallback {
    /// Name of the transform being scheduled.
    transform_name: String,
    /// The schedule to follow.
    schedule: TransformSchedule,
    /// Current probability value.
    current_p: f32,
    /// Whether this callback has been triggered (reserved for future use).
    #[allow(dead_code)]
    is_active: bool,
}

impl TransformSchedulerCallback {
    /// Create a new transform scheduler callback.
    pub fn new(transform_name: &str, schedule: TransformSchedule) -> Self {
        let initial_p = match &schedule {
            TransformSchedule::Constant(p) => *p,
            TransformSchedule::LinearWarmup { .. } => 0.0,
            TransformSchedule::LinearCooldown { max_p, .. } => *max_p,
            TransformSchedule::CosineAnnealing { min_p, max_p } => (*min_p + *max_p) / 2.0,
            TransformSchedule::Step { schedule } => schedule.first().map(|(_, p)| *p).unwrap_or(0.5),
            TransformSchedule::DelayedStart { .. } => 0.0,
        };

        Self {
            transform_name: transform_name.to_string(),
            schedule,
            current_p: initial_p,
            is_active: true,
        }
    }

    /// Get the current probability value.
    pub fn current_probability(&self) -> f32 {
        self.current_p
    }

    /// Get the transform name.
    pub fn transform_name(&self) -> &str {
        &self.transform_name
    }

    /// Compute the probability for the given epoch.
    fn compute_probability(&self, epoch: usize, n_epochs: usize) -> f32 {
        match &self.schedule {
            TransformSchedule::Constant(p) => *p,

            TransformSchedule::LinearWarmup { max_p, warmup_epochs } => {
                if epoch >= *warmup_epochs {
                    *max_p
                } else {
                    *max_p * (epoch as f32 / *warmup_epochs as f32)
                }
            }

            TransformSchedule::LinearCooldown { max_p, cooldown_epochs } => {
                let start_cooldown = n_epochs.saturating_sub(*cooldown_epochs);
                if epoch < start_cooldown {
                    *max_p
                } else {
                    let progress = (epoch - start_cooldown) as f32 / *cooldown_epochs as f32;
                    *max_p * (1.0 - progress)
                }
            }

            TransformSchedule::CosineAnnealing { min_p, max_p } => {
                if n_epochs <= 1 {
                    (*min_p + *max_p) / 2.0
                } else {
                    let progress = epoch as f32 / (n_epochs - 1) as f32;
                    let cosine = (1.0 + (progress * std::f32::consts::PI).cos()) / 2.0;
                    *min_p + (*max_p - *min_p) * cosine
                }
            }

            TransformSchedule::Step { schedule } => {
                let mut current_p = schedule.first().map(|(_, p)| *p).unwrap_or(0.5);
                for &(step_epoch, p) in schedule {
                    if epoch >= step_epoch {
                        current_p = p;
                    } else {
                        break;
                    }
                }
                current_p
            }

            TransformSchedule::DelayedStart { p, start_epoch } => {
                if epoch >= *start_epoch {
                    *p
                } else {
                    0.0
                }
            }
        }
    }
}

impl Callback for TransformSchedulerCallback {
    fn before_fit(&mut self, ctx: &mut CallbackContext) -> Result<()> {
        self.current_p = self.compute_probability(0, ctx.n_epochs);
        tracing::info!(
            "TransformScheduler: {} starting with p={:.3}",
            self.transform_name,
            self.current_p
        );
        Ok(())
    }

    fn before_epoch(&mut self, ctx: &mut CallbackContext) -> Result<()> {
        self.current_p = self.compute_probability(ctx.epoch, ctx.n_epochs);
        tracing::debug!(
            "TransformScheduler: {} epoch {} p={:.3}",
            self.transform_name,
            ctx.epoch + 1,
            self.current_p
        );
        Ok(())
    }

    fn after_fit(&mut self, _ctx: &mut CallbackContext) -> Result<()> {
        tracing::info!(
            "TransformScheduler: {} finished with final p={:.3}",
            self.transform_name,
            self.current_p
        );
        Ok(())
    }

    fn name(&self) -> &str {
        "TransformSchedulerCallback"
    }
}

/// Strategy for computing per-sample weights.
#[derive(Debug, Clone)]
pub enum WeightStrategy {
    /// Equal weights for all samples.
    Uniform,
    /// Inverse frequency weighting (minority classes get higher weight).
    InverseFrequency {
        /// Class counts (or will be computed).
        class_counts: Option<Vec<usize>>,
    },
    /// Effective number weighting (handles long-tail distributions).
    EffectiveNumber {
        /// Beta parameter (typically 0.99 or 0.999).
        beta: f64,
        /// Class counts.
        class_counts: Option<Vec<usize>>,
    },
    /// Custom weights per sample.
    Custom(Vec<f32>),
    /// Curriculum learning: weight by sample difficulty.
    Curriculum {
        /// Current loss per sample.
        sample_losses: Vec<f32>,
        /// Weight easy samples more initially.
        easy_first: bool,
    },
}

/// Callback for weighted per-sample loss computation.
///
/// Applies per-sample weights during training to handle:
/// - Class imbalance
/// - Sample importance
/// - Curriculum learning
/// - Hard example mining
///
/// The weights are applied to the loss before reduction, effectively
/// scaling the gradient contribution of each sample.
///
/// # Example
///
/// ```rust,ignore
/// use tsai_train::callback::{WeightedPerSampleLossCallback, WeightStrategy};
///
/// // Use inverse frequency weighting for imbalanced classes
/// let callback = WeightedPerSampleLossCallback::new(WeightStrategy::InverseFrequency {
///     class_counts: Some(vec![1000, 100, 50]),
/// });
/// ```
pub struct WeightedPerSampleLossCallback {
    strategy: WeightStrategy,
    weights: Vec<f32>,
    total_samples: usize,
    num_classes: Option<usize>,
}

impl WeightedPerSampleLossCallback {
    /// Create a new weighted per-sample loss callback.
    pub fn new(strategy: WeightStrategy) -> Self {
        Self {
            strategy,
            weights: Vec::new(),
            total_samples: 0,
            num_classes: None,
        }
    }

    /// Create with inverse frequency weighting.
    pub fn inverse_frequency(class_counts: Vec<usize>) -> Self {
        Self::new(WeightStrategy::InverseFrequency {
            class_counts: Some(class_counts),
        })
    }

    /// Create with effective number weighting.
    pub fn effective_number(beta: f64, class_counts: Vec<usize>) -> Self {
        Self::new(WeightStrategy::EffectiveNumber {
            beta,
            class_counts: Some(class_counts),
        })
    }

    /// Create with custom weights.
    pub fn custom(weights: Vec<f32>) -> Self {
        Self::new(WeightStrategy::Custom(weights))
    }

    /// Set the number of classes (for computing class weights).
    #[must_use]
    pub fn with_num_classes(mut self, num_classes: usize) -> Self {
        self.num_classes = Some(num_classes);
        self
    }

    /// Get current weights.
    pub fn weights(&self) -> &[f32] {
        &self.weights
    }

    /// Get weight for a specific sample.
    pub fn get_weight(&self, sample_idx: usize) -> f32 {
        self.weights.get(sample_idx).copied().unwrap_or(1.0)
    }

    /// Get weights for a batch of samples.
    pub fn get_batch_weights(&self, indices: &[usize]) -> Vec<f32> {
        indices.iter().map(|&i| self.get_weight(i)).collect()
    }

    /// Compute inverse frequency weights.
    fn compute_inverse_frequency_weights(class_counts: &[usize]) -> Vec<f32> {
        let total: usize = class_counts.iter().sum();
        let n_classes = class_counts.len();

        class_counts
            .iter()
            .map(|&count| {
                if count > 0 {
                    total as f32 / (n_classes as f32 * count as f32)
                } else {
                    1.0
                }
            })
            .collect()
    }

    /// Compute effective number weights.
    ///
    /// Uses the formula: (1 - beta^n) / (1 - beta)
    /// This better handles long-tail distributions than simple inverse frequency.
    fn compute_effective_number_weights(beta: f64, class_counts: &[usize]) -> Vec<f32> {
        let effective_nums: Vec<f64> = class_counts
            .iter()
            .map(|&n| {
                if n == 0 {
                    1.0
                } else {
                    (1.0 - beta.powi(n as i32)) / (1.0 - beta)
                }
            })
            .collect();

        let total: f64 = effective_nums.iter().sum();
        let n_classes = class_counts.len();

        effective_nums
            .iter()
            .map(|&eff| (total / (n_classes as f64 * eff)) as f32)
            .collect()
    }

    /// Initialize weights based on strategy.
    fn initialize_weights(&mut self, n_samples: usize) {
        self.total_samples = n_samples;

        self.weights = match &self.strategy {
            WeightStrategy::Uniform => vec![1.0; n_samples],

            WeightStrategy::InverseFrequency { class_counts } => {
                if let Some(counts) = class_counts {
                    Self::compute_inverse_frequency_weights(counts)
                } else {
                    vec![1.0; n_samples]
                }
            }

            WeightStrategy::EffectiveNumber { beta, class_counts } => {
                if let Some(counts) = class_counts {
                    Self::compute_effective_number_weights(*beta, counts)
                } else {
                    vec![1.0; n_samples]
                }
            }

            WeightStrategy::Custom(w) => w.clone(),

            WeightStrategy::Curriculum { sample_losses, easy_first } => {
                // Sort indices by loss and assign weights
                let mut indexed: Vec<(usize, f32)> = sample_losses
                    .iter()
                    .enumerate()
                    .map(|(i, &l)| (i, l))
                    .collect();

                if *easy_first {
                    indexed.sort_by(|a, b| a.1.partial_cmp(&b.1).unwrap());
                } else {
                    indexed.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap());
                }

                // Linear weighting from 1.0 (first) to 0.1 (last)
                let mut weights = vec![0.0; sample_losses.len()];
                for (rank, (orig_idx, _)) in indexed.iter().enumerate() {
                    let w = 1.0 - 0.9 * (rank as f32 / sample_losses.len() as f32);
                    weights[*orig_idx] = w;
                }
                weights
            }
        };

        // Normalize weights to have mean 1.0
        if !self.weights.is_empty() {
            let mean: f32 = self.weights.iter().sum::<f32>() / self.weights.len() as f32;
            if mean > 0.0 {
                for w in &mut self.weights {
                    *w /= mean;
                }
            }
        }
    }

    /// Update weights based on current losses (for curriculum learning).
    pub fn update_curriculum_weights(&mut self, sample_losses: Vec<f32>, easy_first: bool) {
        self.strategy = WeightStrategy::Curriculum {
            sample_losses: sample_losses.clone(),
            easy_first,
        };
        self.initialize_weights(sample_losses.len());
    }
}

impl Callback for WeightedPerSampleLossCallback {
    fn before_fit(&mut self, ctx: &mut CallbackContext) -> Result<()> {
        // Initialize with batch count as approximation
        // In practice, the actual sample count would come from the dataset
        let approx_samples = ctx.n_batches * 32; // Approximate batch size
        if self.weights.is_empty() {
            self.initialize_weights(approx_samples);
        }
        tracing::info!(
            "WeightedPerSampleLoss: initialized with {} weights",
            self.weights.len()
        );
        Ok(())
    }

    fn name(&self) -> &str {
        "WeightedPerSampleLossCallback"
    }
}

/// Strategy for batch subsampling.
#[derive(Debug, Clone)]
pub enum SubsampleStrategy {
    /// Random uniform subsampling.
    Random {
        /// Fraction of batch to keep (0.0 to 1.0).
        fraction: f32,
    },
    /// Hard example mining: keep samples with highest loss.
    HardExamples {
        /// Fraction of hardest samples to keep.
        fraction: f32,
    },
    /// Easy-to-hard curriculum.
    Curriculum {
        /// Starting fraction (more samples).
        start_fraction: f32,
        /// Ending fraction (fewer, harder samples).
        end_fraction: f32,
    },
    /// Stratified subsampling (preserve class distribution).
    Stratified {
        /// Fraction of each class to keep.
        fraction: f32,
    },
}

impl Default for SubsampleStrategy {
    fn default() -> Self {
        Self::Random { fraction: 0.5 }
    }
}

/// Callback for batch subsampling during training.
///
/// Subsampling can be used to:
/// - Reduce training time by using fewer samples per batch
/// - Implement hard example mining (focus on difficult samples)
/// - Apply curriculum learning (easy to hard)
/// - Handle large batch sizes by training on subsets
///
/// # Example
///
/// ```rust,ignore
/// use tsai_train::callback::{BatchSubsamplerCallback, SubsampleStrategy};
///
/// // Keep 50% of each batch randomly
/// let callback = BatchSubsamplerCallback::new(SubsampleStrategy::Random { fraction: 0.5 });
///
/// // Or use hard example mining
/// let callback = BatchSubsamplerCallback::new(SubsampleStrategy::HardExamples { fraction: 0.3 });
/// ```
pub struct BatchSubsamplerCallback {
    strategy: SubsampleStrategy,
    current_fraction: f32,
    samples_kept: usize,
    samples_total: usize,
    batch_losses: Vec<f32>,
    seed: u64,
}

impl BatchSubsamplerCallback {
    /// Create a new batch subsampler callback.
    pub fn new(strategy: SubsampleStrategy) -> Self {
        let initial_fraction = match &strategy {
            SubsampleStrategy::Random { fraction } => *fraction,
            SubsampleStrategy::HardExamples { fraction } => *fraction,
            SubsampleStrategy::Curriculum { start_fraction, .. } => *start_fraction,
            SubsampleStrategy::Stratified { fraction } => *fraction,
        };

        Self {
            strategy,
            current_fraction: initial_fraction,
            samples_kept: 0,
            samples_total: 0,
            batch_losses: Vec::new(),
            seed: 42,
        }
    }

    /// Create with random subsampling.
    pub fn random(fraction: f32) -> Self {
        Self::new(SubsampleStrategy::Random { fraction })
    }

    /// Create with hard example mining.
    pub fn hard_examples(fraction: f32) -> Self {
        Self::new(SubsampleStrategy::HardExamples { fraction })
    }

    /// Create with curriculum learning.
    pub fn curriculum(start_fraction: f32, end_fraction: f32) -> Self {
        Self::new(SubsampleStrategy::Curriculum {
            start_fraction,
            end_fraction,
        })
    }

    /// Set random seed.
    #[must_use]
    pub fn with_seed(mut self, seed: u64) -> Self {
        self.seed = seed;
        self
    }

    /// Get current subsampling fraction.
    pub fn current_fraction(&self) -> f32 {
        self.current_fraction
    }

    /// Get indices to keep for a batch.
    ///
    /// # Arguments
    ///
    /// * `batch_size` - Size of the current batch
    /// * `losses` - Per-sample losses (optional, for hard example mining)
    ///
    /// # Returns
    ///
    /// Vector of indices to keep.
    pub fn get_subsample_indices(&mut self, batch_size: usize, losses: Option<&[f32]>) -> Vec<usize> {
        use rand::prelude::*;
        use rand_chacha::ChaCha8Rng;

        let keep_count = ((batch_size as f32 * self.current_fraction).round() as usize).max(1);

        match &self.strategy {
            SubsampleStrategy::Random { .. } => {
                let mut rng = ChaCha8Rng::seed_from_u64(self.seed);
                self.seed = self.seed.wrapping_add(1);

                let mut indices: Vec<usize> = (0..batch_size).collect();
                indices.shuffle(&mut rng);
                indices.truncate(keep_count);
                indices.sort_unstable();
                indices
            }

            SubsampleStrategy::HardExamples { .. } => {
                if let Some(loss_vals) = losses {
                    // Sort by loss descending, keep highest
                    let mut indexed: Vec<(usize, f32)> = loss_vals
                        .iter()
                        .enumerate()
                        .map(|(i, &l)| (i, l))
                        .collect();
                    indexed.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));

                    let mut indices: Vec<usize> = indexed.iter().take(keep_count).map(|(i, _)| *i).collect();
                    indices.sort_unstable();
                    indices
                } else {
                    // Fallback to random if no losses provided
                    (0..keep_count).collect()
                }
            }

            SubsampleStrategy::Curriculum { .. } => {
                if let Some(loss_vals) = losses {
                    // Sort by loss, direction depends on curriculum phase
                    let mut indexed: Vec<(usize, f32)> = loss_vals
                        .iter()
                        .enumerate()
                        .map(|(i, &l)| (i, l))
                        .collect();

                    // Early training: prefer easy (low loss)
                    // Late training: include harder samples
                    indexed.sort_by(|a, b| a.1.partial_cmp(&b.1).unwrap_or(std::cmp::Ordering::Equal));

                    let mut indices: Vec<usize> = indexed.iter().take(keep_count).map(|(i, _)| *i).collect();
                    indices.sort_unstable();
                    indices
                } else {
                    (0..keep_count).collect()
                }
            }

            SubsampleStrategy::Stratified { .. } => {
                // For stratified, we'd need class labels
                // Fallback to random for now
                let mut rng = ChaCha8Rng::seed_from_u64(self.seed);
                self.seed = self.seed.wrapping_add(1);

                let mut indices: Vec<usize> = (0..batch_size).collect();
                indices.shuffle(&mut rng);
                indices.truncate(keep_count);
                indices.sort_unstable();
                indices
            }
        }
    }

    /// Record batch losses for next iteration's hard example mining.
    pub fn record_batch_losses(&mut self, losses: Vec<f32>) {
        self.batch_losses = losses;
    }

    /// Update curriculum fraction based on training progress.
    fn update_curriculum(&mut self, progress: f32) {
        if let SubsampleStrategy::Curriculum {
            start_fraction,
            end_fraction,
        } = &self.strategy
        {
            // Linear interpolation from start to end fraction
            self.current_fraction = start_fraction + (end_fraction - start_fraction) * progress;
        }
    }
}

impl Callback for BatchSubsamplerCallback {
    fn before_fit(&mut self, _ctx: &mut CallbackContext) -> Result<()> {
        self.samples_kept = 0;
        self.samples_total = 0;

        // Reset to initial fraction
        self.current_fraction = match &self.strategy {
            SubsampleStrategy::Random { fraction } => *fraction,
            SubsampleStrategy::HardExamples { fraction } => *fraction,
            SubsampleStrategy::Curriculum { start_fraction, .. } => *start_fraction,
            SubsampleStrategy::Stratified { fraction } => *fraction,
        };

        tracing::info!(
            "BatchSubsampler: starting with {:.1}% subsampling",
            self.current_fraction * 100.0
        );
        Ok(())
    }

    fn before_epoch(&mut self, ctx: &mut CallbackContext) -> Result<()> {
        let progress = ctx.epoch as f32 / ctx.n_epochs.max(1) as f32;
        self.update_curriculum(progress);
        Ok(())
    }

    fn after_fit(&mut self, _ctx: &mut CallbackContext) -> Result<()> {
        if self.samples_total > 0 {
            let keep_rate = self.samples_kept as f32 / self.samples_total as f32 * 100.0;
            tracing::info!(
                "BatchSubsampler: kept {:.1}% of samples ({}/{})",
                keep_rate,
                self.samples_kept,
                self.samples_total
            );
        }
        Ok(())
    }

    fn name(&self) -> &str {
        "BatchSubsamplerCallback"
    }
}

/// Tracking mode for prediction dynamics.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PredictionTrackingMode {
    /// Track all samples.
    All,
    /// Track only samples that change predictions.
    ChangesOnly,
    /// Track a random subset of samples.
    Subset(usize),
}

impl Default for PredictionTrackingMode {
    fn default() -> Self {
        Self::All
    }
}

/// Per-sample prediction history.
#[derive(Debug, Clone, Default)]
pub struct SamplePredictionHistory {
    /// Sample index.
    pub sample_idx: usize,
    /// True label.
    pub true_label: i64,
    /// Predicted labels per epoch.
    pub predictions: Vec<i64>,
    /// Confidence scores per epoch.
    pub confidences: Vec<f32>,
    /// Loss values per epoch.
    pub losses: Vec<f32>,
}

impl SamplePredictionHistory {
    /// Create a new sample history.
    pub fn new(sample_idx: usize, true_label: i64) -> Self {
        Self {
            sample_idx,
            true_label,
            predictions: Vec::new(),
            confidences: Vec::new(),
            losses: Vec::new(),
        }
    }

    /// Add an epoch's prediction data.
    pub fn add_epoch(&mut self, prediction: i64, confidence: f32, loss: f32) {
        self.predictions.push(prediction);
        self.confidences.push(confidence);
        self.losses.push(loss);
    }

    /// Check if this sample was ever correctly predicted.
    pub fn ever_correct(&self) -> bool {
        self.predictions.iter().any(|&p| p == self.true_label)
    }

    /// Check if this sample was always correctly predicted.
    pub fn always_correct(&self) -> bool {
        !self.predictions.is_empty() && self.predictions.iter().all(|&p| p == self.true_label)
    }

    /// Count how many times the prediction changed.
    pub fn flip_count(&self) -> usize {
        if self.predictions.len() < 2 {
            return 0;
        }
        self.predictions
            .windows(2)
            .filter(|w| w[0] != w[1])
            .count()
    }

    /// Get the epoch when this sample was first correctly predicted.
    pub fn first_correct_epoch(&self) -> Option<usize> {
        self.predictions
            .iter()
            .enumerate()
            .find(|(_, &p)| p == self.true_label)
            .map(|(i, _)| i)
    }

    /// Get the epoch when this sample was last incorrectly predicted.
    pub fn last_incorrect_epoch(&self) -> Option<usize> {
        self.predictions
            .iter()
            .enumerate()
            .rev()
            .find(|(_, &p)| p != self.true_label)
            .map(|(i, _)| i)
    }

    /// Check if this sample "regressed" (was correct then became incorrect).
    pub fn has_regression(&self) -> bool {
        let mut was_correct = false;
        for &p in &self.predictions {
            if p == self.true_label {
                was_correct = true;
            } else if was_correct {
                return true;
            }
        }
        false
    }

    /// Calculate stability score (1.0 = stable, 0.0 = unstable).
    pub fn stability(&self) -> f32 {
        if self.predictions.len() < 2 {
            return 1.0;
        }
        let flips = self.flip_count();
        1.0 - (flips as f32 / (self.predictions.len() - 1) as f32)
    }
}

/// Callback for tracking prediction dynamics during training.
///
/// This callback monitors how predictions change across epochs,
/// helping identify:
/// - Samples the model struggles with
/// - Unstable predictions that flip-flop
/// - Learning curves for individual samples
/// - Potential mislabeled samples
///
/// # Example
///
/// ```rust,ignore
/// use tsai_train::callback::{PredictionDynamicsCallback, PredictionTrackingMode};
///
/// // Track all samples
/// let callback = PredictionDynamicsCallback::new(PredictionTrackingMode::All);
///
/// // Or track only changing predictions
/// let callback = PredictionDynamicsCallback::new(PredictionTrackingMode::ChangesOnly);
///
/// // After training, analyze the results
/// let unstable = callback.get_unstable_samples(0.5);
/// let never_correct = callback.get_never_correct_samples();
/// ```
pub struct PredictionDynamicsCallback {
    /// Tracking mode for which samples to monitor.
    #[allow(dead_code)]
    mode: PredictionTrackingMode,
    /// Per-sample prediction histories.
    histories: HashMap<usize, SamplePredictionHistory>,
    /// True labels for all tracked samples.
    true_labels: HashMap<usize, i64>,
    /// Current epoch predictions buffer.
    current_predictions: Vec<(usize, i64, f32, f32)>, // (idx, pred, conf, loss)
    /// Number of epochs tracked.
    epochs_tracked: usize,
    /// Whether to log summary each epoch.
    log_per_epoch: bool,
}

impl PredictionDynamicsCallback {
    /// Create a new prediction dynamics callback.
    pub fn new(mode: PredictionTrackingMode) -> Self {
        Self {
            mode,
            histories: HashMap::new(),
            true_labels: HashMap::new(),
            current_predictions: Vec::new(),
            epochs_tracked: 0,
            log_per_epoch: false,
        }
    }

    /// Enable per-epoch logging.
    #[must_use]
    pub fn with_logging(mut self, enabled: bool) -> Self {
        self.log_per_epoch = enabled;
        self
    }

    /// Set true labels for samples.
    ///
    /// Must be called before training starts.
    pub fn set_true_labels(&mut self, labels: HashMap<usize, i64>) {
        self.true_labels = labels;
    }

    /// Record predictions for the current epoch.
    ///
    /// Call this after each validation pass with the predictions.
    pub fn record_predictions(
        &mut self,
        sample_indices: &[usize],
        predictions: &[i64],
        confidences: &[f32],
        losses: &[f32],
    ) {
        for i in 0..sample_indices.len() {
            let idx = sample_indices[i];
            let pred = predictions.get(i).copied().unwrap_or(0);
            let conf = confidences.get(i).copied().unwrap_or(0.0);
            let loss = losses.get(i).copied().unwrap_or(0.0);
            self.current_predictions.push((idx, pred, conf, loss));
        }
    }

    /// Commit current epoch's predictions to history.
    fn commit_epoch(&mut self) {
        for (idx, pred, conf, loss) in self.current_predictions.drain(..) {
            let true_label = self.true_labels.get(&idx).copied().unwrap_or(-1);

            let history = self.histories.entry(idx).or_insert_with(|| {
                SamplePredictionHistory::new(idx, true_label)
            });

            // Update true label if not set
            if history.true_label == -1 && true_label != -1 {
                history.true_label = true_label;
            }

            history.add_epoch(pred, conf, loss);
        }
        self.epochs_tracked += 1;
    }

    /// Get samples that are never correctly predicted.
    pub fn get_never_correct_samples(&self) -> Vec<usize> {
        self.histories
            .values()
            .filter(|h| !h.predictions.is_empty() && !h.ever_correct())
            .map(|h| h.sample_idx)
            .collect()
    }

    /// Get samples with unstable predictions.
    ///
    /// # Arguments
    ///
    /// * `stability_threshold` - Samples with stability below this are returned.
    pub fn get_unstable_samples(&self, stability_threshold: f32) -> Vec<(usize, f32)> {
        self.histories
            .values()
            .filter(|h| h.stability() < stability_threshold)
            .map(|h| (h.sample_idx, h.stability()))
            .collect()
    }

    /// Get samples that show regression (were correct, then incorrect).
    pub fn get_regression_samples(&self) -> Vec<usize> {
        self.histories
            .values()
            .filter(|h| h.has_regression())
            .map(|h| h.sample_idx)
            .collect()
    }

    /// Get samples that are always correct.
    pub fn get_always_correct_samples(&self) -> Vec<usize> {
        self.histories
            .values()
            .filter(|h| h.always_correct())
            .map(|h| h.sample_idx)
            .collect()
    }

    /// Get the full history for a sample.
    pub fn get_sample_history(&self, sample_idx: usize) -> Option<&SamplePredictionHistory> {
        self.histories.get(&sample_idx)
    }

    /// Get average confidence over epochs for each sample.
    pub fn get_average_confidences(&self) -> HashMap<usize, f32> {
        self.histories
            .iter()
            .map(|(idx, h)| {
                let avg = if h.confidences.is_empty() {
                    0.0
                } else {
                    h.confidences.iter().sum::<f32>() / h.confidences.len() as f32
                };
                (*idx, avg)
            })
            .collect()
    }

    /// Get learning difficulty score for samples.
    ///
    /// Score based on: first correct epoch, stability, and final accuracy.
    /// Higher score = more difficult sample.
    pub fn get_difficulty_scores(&self) -> HashMap<usize, f32> {
        let max_epochs = self.epochs_tracked as f32;

        self.histories
            .iter()
            .map(|(idx, h)| {
                let first_correct_score = h
                    .first_correct_epoch()
                    .map(|e| e as f32 / max_epochs.max(1.0))
                    .unwrap_or(1.0);

                let stability_score = 1.0 - h.stability();

                let final_correct = h
                    .predictions
                    .last()
                    .map(|&p| if p == h.true_label { 0.0 } else { 0.5 })
                    .unwrap_or(0.5);

                let score = (first_correct_score + stability_score + final_correct) / 3.0;
                (*idx, score)
            })
            .collect()
    }

    /// Get summary statistics.
    pub fn summary(&self) -> PredictionDynamicsSummary {
        let total = self.histories.len();
        let never_correct = self.get_never_correct_samples().len();
        let always_correct = self.get_always_correct_samples().len();
        let regression = self.get_regression_samples().len();
        let unstable = self.get_unstable_samples(0.7).len();

        let avg_stability = if total > 0 {
            self.histories.values().map(|h| h.stability()).sum::<f32>() / total as f32
        } else {
            0.0
        };

        PredictionDynamicsSummary {
            total_samples: total,
            never_correct,
            always_correct,
            regression_count: regression,
            unstable_count: unstable,
            average_stability: avg_stability,
            epochs_tracked: self.epochs_tracked,
        }
    }
}

/// Summary of prediction dynamics.
#[derive(Debug, Clone)]
pub struct PredictionDynamicsSummary {
    /// Total samples tracked.
    pub total_samples: usize,
    /// Samples never correctly predicted.
    pub never_correct: usize,
    /// Samples always correctly predicted.
    pub always_correct: usize,
    /// Samples with regression (correct → incorrect).
    pub regression_count: usize,
    /// Samples with unstable predictions.
    pub unstable_count: usize,
    /// Average stability score.
    pub average_stability: f32,
    /// Number of epochs tracked.
    pub epochs_tracked: usize,
}

impl std::fmt::Display for PredictionDynamicsSummary {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        writeln!(f, "=== Prediction Dynamics Summary ===")?;
        writeln!(f, "Epochs tracked: {}", self.epochs_tracked)?;
        writeln!(f, "Total samples: {}", self.total_samples)?;
        writeln!(f, "Always correct: {} ({:.1}%)",
            self.always_correct,
            100.0 * self.always_correct as f32 / self.total_samples.max(1) as f32
        )?;
        writeln!(f, "Never correct: {} ({:.1}%)",
            self.never_correct,
            100.0 * self.never_correct as f32 / self.total_samples.max(1) as f32
        )?;
        writeln!(f, "Regressions: {} ({:.1}%)",
            self.regression_count,
            100.0 * self.regression_count as f32 / self.total_samples.max(1) as f32
        )?;
        writeln!(f, "Unstable: {} ({:.1}%)",
            self.unstable_count,
            100.0 * self.unstable_count as f32 / self.total_samples.max(1) as f32
        )?;
        writeln!(f, "Average stability: {:.3}", self.average_stability)?;
        Ok(())
    }
}

impl Callback for PredictionDynamicsCallback {
    fn before_fit(&mut self, _ctx: &mut CallbackContext) -> Result<()> {
        self.histories.clear();
        self.current_predictions.clear();
        self.epochs_tracked = 0;
        tracing::info!("PredictionDynamics: tracking enabled");
        Ok(())
    }

    fn after_epoch(&mut self, ctx: &mut CallbackContext) -> Result<()> {
        // Commit predictions recorded during this epoch
        self.commit_epoch();

        if self.log_per_epoch && !self.histories.is_empty() {
            let summary = self.summary();
            tracing::info!(
                "Epoch {}: {} samples tracked, {:.1}% always correct, {:.1}% never correct",
                ctx.epoch + 1,
                summary.total_samples,
                100.0 * summary.always_correct as f32 / summary.total_samples.max(1) as f32,
                100.0 * summary.never_correct as f32 / summary.total_samples.max(1) as f32
            );
        }

        Ok(())
    }

    fn after_fit(&mut self, _ctx: &mut CallbackContext) -> Result<()> {
        let summary = self.summary();
        tracing::info!("\n{}", summary);
        Ok(())
    }

    fn name(&self) -> &str {
        "PredictionDynamicsCallback"
    }
}

/// Configuration for pseudo-label filtering.
#[derive(Debug, Clone)]
pub enum PseudoLabelFilter {
    /// Keep all pseudo-labels.
    All,
    /// Keep only high-confidence predictions.
    ConfidenceThreshold(f32),
    /// Keep top-k percent most confident predictions.
    TopK {
        /// Fraction of samples to keep (0.0 to 1.0).
        fraction: f32,
    },
    /// Class-balanced top-k (equal samples per class).
    ClassBalancedTopK {
        /// Number of samples per class.
        samples_per_class: usize,
    },
}

impl Default for PseudoLabelFilter {
    fn default() -> Self {
        Self::ConfidenceThreshold(0.9)
    }
}

/// Noise injection mode for student training.
#[derive(Debug, Clone)]
pub enum NoiseInjection {
    /// No noise (standard training).
    None,
    /// Dropout on input data.
    InputDropout(f32),
    /// Stochastic depth (layer dropout).
    StochasticDepth(f32),
    /// RandAugment-style augmentation.
    RandAugment {
        /// Number of augmentation operations.
        n_ops: usize,
        /// Magnitude of augmentations.
        magnitude: f32,
    },
    /// Gaussian noise on inputs.
    GaussianNoise(f32),
    /// Combined noise sources.
    Combined(Vec<NoiseInjection>),
}

impl Default for NoiseInjection {
    fn default() -> Self {
        Self::InputDropout(0.5)
    }
}

/// Pseudo-label entry for an unlabeled sample.
#[derive(Debug, Clone)]
pub struct PseudoLabel {
    /// Sample index.
    pub sample_idx: usize,
    /// Assigned pseudo-label.
    pub label: i64,
    /// Confidence score (probability).
    pub confidence: f32,
    /// Teacher iteration that generated this label.
    pub teacher_iteration: usize,
}

/// Callback for Noisy Student semi-supervised learning.
///
/// Implements the Noisy Student Training algorithm:
/// 1. Train a teacher on labeled data
/// 2. Generate pseudo-labels for unlabeled data
/// 3. Train a (larger) student on labeled + pseudo-labeled data with noise
/// 4. Student becomes new teacher, repeat
///
/// Key features:
/// - Pseudo-label filtering by confidence or top-k
/// - Noise injection during student training
/// - Progressive refinement through iterations
/// - Support for class-balanced sampling
///
/// # Example
///
/// ```rust,ignore
/// use tsai_train::callback::{NoisyStudentCallback, PseudoLabelFilter, NoiseInjection};
///
/// let callback = NoisyStudentCallback::new()
///     .with_filter(PseudoLabelFilter::ConfidenceThreshold(0.9))
///     .with_noise(NoiseInjection::InputDropout(0.5))
///     .with_iterations(3);
///
/// // After teacher training, generate pseudo-labels
/// callback.generate_pseudo_labels(&teacher_predictions, &teacher_confidences);
///
/// // Then train student with the callback attached
/// ```
pub struct NoisyStudentCallback {
    /// Pseudo-label filtering strategy.
    filter: PseudoLabelFilter,
    /// Noise injection mode.
    noise: NoiseInjection,
    /// Number of teacher-student iterations.
    n_iterations: usize,
    /// Current iteration (0 = first teacher).
    current_iteration: usize,
    /// Pseudo-labels for unlabeled data.
    pseudo_labels: Vec<PseudoLabel>,
    /// Number of labeled samples.
    n_labeled: usize,
    /// Number of unlabeled samples.
    n_unlabeled: usize,
    /// Whether we're in student phase (with noise).
    is_student_phase: bool,
    /// Temperature for soft labels (1.0 = hard labels).
    temperature: f32,
    /// Minimum confidence to include pseudo-label.
    min_confidence: f32,
    /// Statistics tracking.
    stats: NoisyStudentStats,
}

/// Statistics for Noisy Student training.
#[derive(Debug, Clone, Default)]
pub struct NoisyStudentStats {
    /// Pseudo-labels generated per iteration.
    pub labels_per_iteration: Vec<usize>,
    /// Average confidence per iteration.
    pub avg_confidence_per_iteration: Vec<f32>,
    /// Accuracy on validation set per iteration.
    pub validation_accuracy: Vec<f32>,
    /// Distribution of pseudo-labels per class.
    pub class_distribution: HashMap<i64, usize>,
}

impl NoisyStudentCallback {
    /// Create a new Noisy Student callback.
    pub fn new() -> Self {
        Self {
            filter: PseudoLabelFilter::default(),
            noise: NoiseInjection::default(),
            n_iterations: 3,
            current_iteration: 0,
            pseudo_labels: Vec::new(),
            n_labeled: 0,
            n_unlabeled: 0,
            is_student_phase: false,
            temperature: 1.0,
            min_confidence: 0.5,
            stats: NoisyStudentStats::default(),
        }
    }

    /// Set the pseudo-label filtering strategy.
    #[must_use]
    pub fn with_filter(mut self, filter: PseudoLabelFilter) -> Self {
        self.filter = filter;
        self
    }

    /// Set the noise injection mode.
    #[must_use]
    pub fn with_noise(mut self, noise: NoiseInjection) -> Self {
        self.noise = noise;
        self
    }

    /// Set the number of teacher-student iterations.
    #[must_use]
    pub fn with_iterations(mut self, n: usize) -> Self {
        self.n_iterations = n;
        self
    }

    /// Set temperature for soft labels.
    #[must_use]
    pub fn with_temperature(mut self, t: f32) -> Self {
        self.temperature = t.max(0.1);
        self
    }

    /// Set minimum confidence threshold.
    #[must_use]
    pub fn with_min_confidence(mut self, conf: f32) -> Self {
        self.min_confidence = conf.clamp(0.0, 1.0);
        self
    }

    /// Set the number of labeled/unlabeled samples.
    pub fn set_data_counts(&mut self, n_labeled: usize, n_unlabeled: usize) {
        self.n_labeled = n_labeled;
        self.n_unlabeled = n_unlabeled;
    }

    /// Generate pseudo-labels from teacher predictions.
    ///
    /// # Arguments
    ///
    /// * `sample_indices` - Indices of unlabeled samples
    /// * `predictions` - Predicted class labels
    /// * `confidences` - Confidence scores (max probability)
    pub fn generate_pseudo_labels(
        &mut self,
        sample_indices: &[usize],
        predictions: &[i64],
        confidences: &[f32],
    ) {
        // Create pseudo-label candidates
        let mut candidates: Vec<PseudoLabel> = sample_indices
            .iter()
            .zip(predictions.iter())
            .zip(confidences.iter())
            .map(|((&idx, &label), &conf)| PseudoLabel {
                sample_idx: idx,
                label,
                confidence: conf,
                teacher_iteration: self.current_iteration,
            })
            .filter(|pl| pl.confidence >= self.min_confidence)
            .collect();

        // Apply filtering strategy
        self.pseudo_labels = match &self.filter {
            PseudoLabelFilter::All => candidates,

            PseudoLabelFilter::ConfidenceThreshold(thresh) => {
                candidates.retain(|pl| pl.confidence >= *thresh);
                candidates
            }

            PseudoLabelFilter::TopK { fraction } => {
                candidates.sort_by(|a, b| b.confidence.partial_cmp(&a.confidence).unwrap());
                let keep = ((candidates.len() as f32 * fraction).round() as usize).max(1);
                candidates.truncate(keep);
                candidates
            }

            PseudoLabelFilter::ClassBalancedTopK { samples_per_class } => {
                // Group by class
                let mut by_class: HashMap<i64, Vec<PseudoLabel>> = HashMap::new();
                for pl in candidates {
                    by_class.entry(pl.label).or_default().push(pl);
                }

                // Take top-k from each class
                let mut result = Vec::new();
                for (_, mut class_samples) in by_class {
                    class_samples.sort_by(|a, b| b.confidence.partial_cmp(&a.confidence).unwrap());
                    class_samples.truncate(*samples_per_class);
                    result.extend(class_samples);
                }
                result
            }
        };

        // Update statistics
        self.stats.labels_per_iteration.push(self.pseudo_labels.len());

        let avg_conf = if self.pseudo_labels.is_empty() {
            0.0
        } else {
            self.pseudo_labels.iter().map(|pl| pl.confidence).sum::<f32>()
                / self.pseudo_labels.len() as f32
        };
        self.stats.avg_confidence_per_iteration.push(avg_conf);

        // Update class distribution
        self.stats.class_distribution.clear();
        for pl in &self.pseudo_labels {
            *self.stats.class_distribution.entry(pl.label).or_default() += 1;
        }

        tracing::info!(
            "NoisyStudent: generated {} pseudo-labels (avg conf: {:.3})",
            self.pseudo_labels.len(),
            avg_conf
        );
    }

    /// Get pseudo-labels for training.
    pub fn get_pseudo_labels(&self) -> &[PseudoLabel] {
        &self.pseudo_labels
    }

    /// Get pseudo-label for a specific sample.
    pub fn get_sample_label(&self, sample_idx: usize) -> Option<&PseudoLabel> {
        self.pseudo_labels.iter().find(|pl| pl.sample_idx == sample_idx)
    }

    /// Check if we should apply noise (student phase).
    pub fn should_apply_noise(&self) -> bool {
        self.is_student_phase
    }

    /// Get the noise injection configuration.
    pub fn noise_config(&self) -> &NoiseInjection {
        &self.noise
    }

    /// Advance to the next iteration.
    pub fn next_iteration(&mut self) {
        self.current_iteration += 1;
        self.is_student_phase = true;
        tracing::info!(
            "NoisyStudent: starting iteration {} of {}",
            self.current_iteration + 1,
            self.n_iterations
        );
    }

    /// Check if we've completed all iterations.
    pub fn is_complete(&self) -> bool {
        self.current_iteration >= self.n_iterations
    }

    /// Get current iteration.
    pub fn current_iteration(&self) -> usize {
        self.current_iteration
    }

    /// Get training statistics.
    pub fn stats(&self) -> &NoisyStudentStats {
        &self.stats
    }

    /// Record validation accuracy for this iteration.
    pub fn record_validation_accuracy(&mut self, accuracy: f32) {
        self.stats.validation_accuracy.push(accuracy);
    }

    /// Get a sample weight based on whether it's labeled or pseudo-labeled.
    pub fn get_sample_weight(&self, sample_idx: usize, is_labeled: bool) -> f32 {
        if is_labeled {
            1.0
        } else if let Some(pl) = self.get_sample_label(sample_idx) {
            // Weight by confidence
            pl.confidence
        } else {
            0.0 // Not in pseudo-label set
        }
    }

    /// Prepare combined dataset indices (labeled + pseudo-labeled).
    pub fn get_combined_indices(&self, labeled_indices: &[usize]) -> Vec<(usize, bool)> {
        let mut combined: Vec<(usize, bool)> = labeled_indices
            .iter()
            .map(|&idx| (idx, true))
            .collect();

        for pl in &self.pseudo_labels {
            combined.push((pl.sample_idx, false));
        }

        combined
    }

    /// Summary of current state.
    pub fn summary(&self) -> String {
        let mut output = String::new();
        output.push_str("=== Noisy Student Summary ===\n");
        output.push_str(&format!("Iteration: {} / {}\n", self.current_iteration + 1, self.n_iterations));
        output.push_str(&format!("Labeled samples: {}\n", self.n_labeled));
        output.push_str(&format!("Pseudo-labeled: {}\n", self.pseudo_labels.len()));
        output.push_str(&format!("Student phase: {}\n", self.is_student_phase));

        if !self.stats.class_distribution.is_empty() {
            output.push_str("\nClass distribution:\n");
            let mut sorted: Vec<_> = self.stats.class_distribution.iter().collect();
            sorted.sort_by_key(|(k, _)| *k);
            for (class, count) in sorted {
                output.push_str(&format!("  Class {}: {}\n", class, count));
            }
        }

        output
    }
}

impl Default for NoisyStudentCallback {
    fn default() -> Self {
        Self::new()
    }
}

impl Callback for NoisyStudentCallback {
    fn before_fit(&mut self, ctx: &mut CallbackContext) -> Result<()> {
        tracing::info!(
            "NoisyStudent: starting iteration {} with {} epochs",
            self.current_iteration + 1,
            ctx.n_epochs
        );

        if self.is_student_phase {
            tracing::info!(
                "NoisyStudent: student phase with {} pseudo-labels",
                self.pseudo_labels.len()
            );
        }

        Ok(())
    }

    fn after_fit(&mut self, ctx: &mut CallbackContext) -> Result<()> {
        if let Some(valid_loss) = ctx.valid_loss {
            tracing::info!(
                "NoisyStudent iteration {} complete: valid_loss = {:.4}",
                self.current_iteration + 1,
                valid_loss
            );
        }

        // Record accuracy if available
        if let Some(&acc) = ctx.metrics.get("accuracy") {
            self.record_validation_accuracy(acc);
        }

        Ok(())
    }

    fn before_batch(&mut self, _ctx: &mut CallbackContext) -> Result<()> {
        // Noise injection would be applied by the learner based on should_apply_noise()
        Ok(())
    }

    fn name(&self) -> &str {
        "NoisyStudentCallback"
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_callback_context() {
        let ctx = CallbackContext::new(10, 100);
        assert_eq!(ctx.epoch, 0);
        assert_eq!(ctx.n_epochs, 10);
        assert_eq!(ctx.progress(), 0.0);
    }

    #[test]
    fn test_callback_list() {
        let mut list = CallbackList::new();
        list.add(ProgressCallback::new(false));
        // Would add more callbacks here
    }

    #[test]
    fn test_gradient_clip_callback() {
        let clip = GradientClipCallback::by_norm(1.0);
        assert_eq!(clip.clip_value(), 1.0);

        let clip = GradientClipCallback::by_value(0.5);
        assert_eq!(clip.clip_value(), 0.5);
    }

    #[test]
    fn test_history_callback() {
        let mut history = HistoryCallback::new();
        let mut ctx = CallbackContext::new(10, 100);

        ctx.train_loss = Some(0.5);
        ctx.valid_loss = Some(0.4);
        ctx.lr = 0.001;
        history.after_epoch(&mut ctx).unwrap();

        assert_eq!(history.train_losses(), &[0.5]);
        assert_eq!(history.valid_losses(), &[0.4]);
        assert_eq!(history.learning_rates(), &[0.001]);
    }

    #[test]
    fn test_show_graph_callback_config() {
        let callback = ShowGraphCallback::new()
            .with_width(60)
            .with_height(15)
            .with_metrics(vec!["accuracy", "f1"]);

        assert_eq!(callback.width, 60);
        assert_eq!(callback.height, 15);
        assert_eq!(callback.metric_names, vec!["accuracy", "f1"]);
    }

    #[test]
    fn test_show_graph_callback_render() {
        let mut callback = ShowGraphCallback::new()
            .with_width(30)
            .with_height(5)
            .show_per_epoch(false);

        let mut ctx = CallbackContext::new(10, 100);

        // Simulate several epochs
        for i in 0..5 {
            ctx.train_loss = Some(1.0 - i as f32 * 0.1);
            ctx.valid_loss = Some(0.9 - i as f32 * 0.08);
            callback.after_epoch(&mut ctx).unwrap();
        }

        assert_eq!(callback.train_losses.len(), 5);
        assert_eq!(callback.valid_losses.len(), 5);
    }

    #[test]
    fn test_transform_scheduler_constant() {
        let callback = TransformSchedulerCallback::new(
            "TestTransform",
            TransformSchedule::Constant(0.8),
        );
        assert_eq!(callback.current_probability(), 0.8);
    }

    #[test]
    fn test_transform_scheduler_linear_warmup() {
        let mut callback = TransformSchedulerCallback::new(
            "TestTransform",
            TransformSchedule::LinearWarmup {
                max_p: 1.0,
                warmup_epochs: 5,
            },
        );

        let mut ctx = CallbackContext::new(10, 100);

        // Start at 0
        callback.before_fit(&mut ctx).unwrap();
        assert_eq!(callback.current_probability(), 0.0);

        // Epoch 2: should be 0.4 (2/5 * 1.0)
        ctx.epoch = 2;
        callback.before_epoch(&mut ctx).unwrap();
        assert!((callback.current_probability() - 0.4).abs() < 0.01);

        // Epoch 5+: should be at max
        ctx.epoch = 5;
        callback.before_epoch(&mut ctx).unwrap();
        assert_eq!(callback.current_probability(), 1.0);
    }

    #[test]
    fn test_transform_scheduler_delayed_start() {
        let mut callback = TransformSchedulerCallback::new(
            "TestTransform",
            TransformSchedule::DelayedStart {
                p: 0.7,
                start_epoch: 5,
            },
        );

        let mut ctx = CallbackContext::new(10, 100);

        // Before start_epoch
        ctx.epoch = 3;
        callback.before_epoch(&mut ctx).unwrap();
        assert_eq!(callback.current_probability(), 0.0);

        // At start_epoch
        ctx.epoch = 5;
        callback.before_epoch(&mut ctx).unwrap();
        assert_eq!(callback.current_probability(), 0.7);
    }
}