Skip to content

Disease Model

Climate-informed disease prediction and projection model.

This class implements an end-to-end pipeline for modeling climate-sensitive diseases (e.g., dengue, malaria) using historical climate data, lagged features, and hybrid machine learning approaches.

The pipeline includes
  1. Data ingestion (disease + climate)
  2. Climate–disease data merging
  3. Lagged feature optimisation (Bayesian search)
  4. Model training (stacked learning framework)
  5. Prediction and evaluation
  6. Climate-based future projections (via DiseaseProjection)

Parameters

str

District identifier (must match supported naming convention, e.g., 'IND_Pune_MAHARASHTRA').

str or Path :

Path to the disease dataset file. Supported formats include: CSV (.csv), Excel (.xlsx), and Parquet (.parquet).

str, default="Count" :

Column name representing disease case counts.

int, default=42 :

Random seed for reproducibility across the pipeline.

str, optional :

Name of the disease (e.g., "Dengue", "Malaria"). Used for reporting and visualization.

Attributes

pandas.DataFrame :

Raw disease dataset.

pandas.DataFrame :

Historical climate data.

pandas.DataFrame :

Climate projection data (CMIP6).

pandas.DataFrame :

Combined dataset with aligned disease and climate variables.

pandas.DataFrame :

Training subset after domain-aware split.

pandas.DataFrame :

Testing subset.

dict

Results from lag optimisation process.

dict

Best-performing lag configuration.

dict

Trained models (base, residual, calibration).

dict

Execution time tracking for each pipeline stage.

Notes

  • The modeling framework uses
    • Base model: captures main signal (e.g., XGBoost)
    • Residual model: models unexplained variation (e.g., Random Forest)
    • Calibration model: adjusts predictions (e.g., isotonic regression)
Source code in climaid\climaid_model.py
 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
class DiseaseModel:
    """
    Climate-informed disease prediction and projection model.

    This class implements an end-to-end pipeline for modeling climate-sensitive
    diseases (e.g., dengue, malaria) using historical climate data, lagged features,
    and hybrid machine learning approaches.

    The pipeline includes:
        1. Data ingestion (disease + climate)
        2. Climate–disease data merging
        3. Lagged feature optimisation (Bayesian search)
        4. Model training (stacked learning framework)
        5. Prediction and evaluation
        6. Climate-based future projections (via DiseaseProjection)

    Parameters
    ----------

    district : str
        District identifier (must match supported naming convention, e.g.,
        'IND_Pune_MAHARASHTRA').

    disease_file : str or Path :
        Path to the disease dataset file. Supported formats include:
        CSV (.csv), Excel (.xlsx), and Parquet (.parquet).

    target_col : str, default="Count" : 
        Column name representing disease case counts.

    random_state : int, default=42 : 
        Random seed for reproducibility across the pipeline.

    disease_name : str, optional :
        Name of the disease (e.g., "Dengue", "Malaria").
        Used for reporting and visualization.

    Attributes
    ----------

    df_disease : pandas.DataFrame :
        Raw disease dataset.

    df_climate_hist : pandas.DataFrame :
        Historical climate data.

    df_climate_proj : pandas.DataFrame :
        Climate projection data (CMIP6).

    df_merged : pandas.DataFrame :
        Combined dataset with aligned disease and climate variables.

    train_df : pandas.DataFrame :
        Training subset after domain-aware split.

    test_df : pandas.DataFrame :
        Testing subset.

    lag_search_results : dict
        Results from lag optimisation process.

    best_config : dict
        Best-performing lag configuration.

    final_models : dict
        Trained models (base, residual, calibration).

    runtime : dict
        Execution time tracking for each pipeline stage.

    Notes
    -----

    - The modeling framework uses
        - Base model: captures main signal (e.g., XGBoost)
        - Residual model: models unexplained variation (e.g., Random Forest)
        - Calibration model: adjusts predictions (e.g., isotonic regression)

    """

    def __init__(
        self,
        district: str,
        disease_file: str,
        target_col: str = "Count",
        random_state: int = 42,
        disease_name: str | None = None,
        weather_file: str | None = None,        
        projection_file: str | None = None,
    ):
        """
        Initialize the DiseaseModel pipeline.

        This automatically:
        1. Loads disease data
        2. Fetches climate data (historical + projections)
        3. Merges datasets for modeling

        Parameters
        ----------

        district : str
            District identifier.

        disease_file : str
            Path to disease dataset.

        target_col : str, optional
            Target variable column.

        random_state : int, optional
            Seed for reproducibility.

        disease_name : str, optional
            Name of disease.

        weather_file : path, optional
            Used in ClimAID Global Mode.

        projection_file : path, optional
            Used in ClimAID Global Mode.
        """

        # -----------------------------
        # Core configuration
        # -----------------------------
        self.random_state = random_state
        self._set_global_seed()
        self.district = district
        self.disease_file = Path(disease_file)
        self.target_col = target_col
        self.disease_name = disease_name
        self.weather_file = weather_file
        self.projection_file = projection_file

        # -----------------------------
        # Load disease data
        # -----------------------------
        self.df_disease = self._load_disease_data()

        # -----------------------------
        # Load climate data
        # -----------------------------
        self.weather_file = weather_file
        self.projection_file = projection_file

        clim = ClimateData(
            weather_file=self.weather_file,
            projection_file=self.projection_file
        )

        self.df_climate_hist = clim.get_historical(district=district)
        self.df_climate_proj = clim.get_projection(district=district)

        # -----------------------------
        # Merge datasets
        # -----------------------------
        self.df_merged = self._merge_data()

        # -----------------------------
        # Model placeholders
        # -----------------------------
        self.train_df = None
        self.test_df = None
        self.lag_search_results = None
        self.best_config = None
        self.final_models = {}

        # -----------------------------
        # Runtime tracking
        # -----------------------------
        self.runtime = {
            "lag_optimization_seconds": None,
            "training_seconds": None,
            "prediction_seconds": None,
            "report_generation_seconds": None,
            "total_pipeline_seconds": None,
        }

        self._pipeline_start_time = None

    # --------------------------------------------------
    # Global Random Seed
    # --------------------------------------------------
    def _set_global_seed(self):
        """Set global random seeds for full pipeline reproducibility."""
        if self.random_state is None:
            return  # Do not override global RNG if user did not specify seed

        np.random.seed(self.random_state)
        random.seed(self.random_state)
        os.environ["PYTHONHASHSEED"] = str(self.random_state)

    # --------------------------------------------------
    # Runtime correcter
    # --------------------------------------------------
    def _update_total_runtime(self):
        """Compute corrected total runtime excluding idle time."""

        components = [
            self.runtime.get("lag_optimization_seconds"),
            self.runtime.get("training_seconds"),
            self.runtime.get("prediction_seconds"),
            self.runtime.get("report_generation_seconds"),
        ]

        # Remove None values
        valid_times = [t for t in components if t is not None]

        self.runtime["Corrected_total_pipeline_seconds"] = sum(valid_times)

    # --------------------------------------------------
    # Load disease data
    # --------------------------------------------------
    def _load_disease_data(self):

        from difflib import get_close_matches

        suffix = self.disease_file.suffix.lower()

        if suffix == ".csv":
            df = pd.read_csv(self.disease_file)

        elif suffix in (".xlsx", ".xls"):
            df = pd.read_excel(self.disease_file)

        else:
            raise ValueError(f"Unsupported file format: {suffix}")

        df = df.copy()
        original_cols = df.columns.tolist()

        # standardize for matching
        df.columns = df.columns.str.strip().str.lower()

        # =====================================================
        # TARGETS
        # =====================================================
        targets = {
            "time": ["date", "time", "datetime"],
            "Count": ["cases", "case", "count"]
        }

        rename_dict = {}
        mapping_log = {}

        # =====================================================
        # MATCHING
        # =====================================================
        for col in df.columns:
            for target, aliases in targets.items():

                # exact match
                if col in aliases:
                    rename_dict[col] = target
                    mapping_log[col] = (target, "exact")
                    break

                # fuzzy match
                match = get_close_matches(col, aliases, n=1, cutoff=0.9)
                if match:
                    rename_dict[col] = target
                    mapping_log[col] = (target, "fuzzy")
                    break

        df = df.rename(columns=rename_dict)

        # =====================================================
        # VALIDATION REPORT
        # =====================================================
        print("\n--- COLUMN STANDARDIZATION REPORT ---")
        print("Original columns:", original_cols)

        print("\nMappings:")
        for k, v in mapping_log.items():
            print(f"{k} -> {v[0]} ({v[1]})")

        # =====================================================
        # REQUIRED CHECK
        # =====================================================
        required = ["time", "Count"]

        print("\n--- REQUIRED COLUMN CHECK ---")
        for col in required:
            if col not in df.columns:
                raise ValueError(
                    f"Missing required column: {col}\n"
                    f"Supported date columns: date, time, datetime\n"
                    f"Supported case columns: cases, case, count"
                )
            else:
                print(f"{col}: OK")

        df["time"] = pd.to_datetime(df["time"], errors="coerce")
        df["Year"] = df["time"].dt.year
        df["Month"] = df["time"].dt.month

        # dropping nan values based on cases
        df = df.dropna(subset=['Count']).reset_index(drop = True)

        if df.empty:
            raise ValueError(
                "The disease data loaded is empty. "
                "The file may be corrupted"
            )

        return df

    # --------------------------------------------------
    # Merge climate + disease
    # --------------------------------------------------
    def _merge_data(self):
        from difflib import get_close_matches
        from climaid.utils import _map_columns
        climate = self.df_climate_hist.copy()

        climate = _map_columns(climate)

        # Removed (from DiseaseModel)
        # # =====================================================
        # # STANDARDIZE COLUMN NAMES
        # # =====================================================
        # original_cols = climate.columns.tolist()
        # climate.columns = climate.columns.str.strip().str.lower()

        # # canonical names
        # standard_vars = {
        #     "mean_Rain": ["rain", "rainfall", "precipitation", "pr"],
        #     "mean_temperature": ["temp", "temperature", "tas"],
        #     "mean_SH": ["humidity", "specific_humidity", "huss"],
        #     "Nino_anomaly": ["nino", "enso", "nino34"],
        #     "time": ["date", "datetime"],
        #     "Dist_States" : ['diststates', 'dist_states', 'dist_state', 'DistStates']
        # }

        # rename_dict = {}
        # unmatched_cols = []
        # mapping_log = []

        # for col in climate.columns:
        #     matched = False

        #     for standard, aliases in standard_vars.items():
        #         # exact match
        #         if col == standard.lower() or col in aliases:
        #             rename_dict[col] = standard
        #             mapping_log.append((col, standard, "exact"))
        #             matched = True
        #             break

        #         # fuzzy match
        #         matches = get_close_matches(col, aliases, n=1, cutoff=0.8)
        #         if matches:
        #             rename_dict[col] = standard
        #             mapping_log.append((col, standard, "fuzzy"))
        #             matched = True
        #             break

        #     if not matched:
        #         unmatched_cols.append(col)

        # climate = climate.rename(columns=rename_dict)

        # # =====================================================
        # # VALIDATION REPORT
        # # =====================================================
        # print("\n--- COLUMN MAPPING REPORT ---")
        # print("Original columns:", original_cols)

        # print("\nMapped columns:")
        # for old, new, mtype in mapping_log:
        #     print(f"{old} -> {new} ({mtype})")

        # if unmatched_cols:
        #     print("\nUnmapped columns:")
        #     for col in unmatched_cols:
        #         print(f"- {col}")

        # =====================================================
        # CHECK REQUIRED COLUMNS
        # =====================================================
        required_cols = ["time"]

        print("\n--- REQUIRED COLUMN CHECK ---")
        for col in required_cols:
            if col not in climate.columns:
                raise ValueError(f"Missing required column: {col}")
            else:
                print(f"{col}: OK")

        # =====================================================
        # TIME HANDLING
        # =====================================================
        climate["time"] = pd.to_datetime(climate["time"], errors="coerce")

        invalid_time = climate["time"].isna().sum()
        print(f"\nInvalid time entries: {invalid_time}")

        climate["Year"] = climate["time"].dt.year.astype("Int16")
        climate["Month"] = climate["time"].dt.month.astype("Int8")

        # =====================================================
        # NUMERIC VARIABLES
        # =====================================================
        climate_vars = ["mean_Rain", "mean_temperature", "mean_SH", "Nino_anomaly"]

        # print("\n--- NUMERIC CONVERSION REPORT ---")

        # available_vars = []

        for col in climate_vars:
            climate[col] = pd.to_numeric(climate[col], errors="coerce")

        # =====================================================
        # CLEAN VALUES
        # =====================================================
        if "mean_Rain" in climate.columns:
            climate["mean_Rain"] = climate["mean_Rain"].clip(lower=0)

        if "mean_SH" in climate.columns:
            climate["mean_SH"] = climate["mean_SH"].clip(lower=0)

        # # =====================================================
        # # SUMMARY REPORT
        # # =====================================================
        # print("\n--- FINAL DATA SUMMARY ---")
        # print("Shape:", climate.shape)

        # for col in available_vars:
        #     missing_pct = climate[col].isna().mean() * 100
        #     print(f"{col}: missing {missing_pct:.2f}%")

        # =====================================================
        # get the annual averages
        # =====================================================
        vars = ["mean_Rain", "mean_temperature", "mean_SH"]
        annual = (
            climate.groupby("Year")[vars]
            .mean()
            .reset_index()
        )

        for col in vars:
            annual[f"YA_{col}"] = annual[col]
            annual.drop(columns=col, inplace=True)

        climate = climate.merge(annual, on="Year", how="left")

        # =====================================================
        # get the rolling monthly averages
        # =====================================================
        for col in vars:
            climate[f"MA_{col}"] = climate[col].rolling(window=120, min_periods=12).mean()

        # =====================================================
        # The climate data will have one additional year i.e., previous to the year from when the disease data has been recorded
        # =====================================================
        year_den = self.df_disease['Year'].min()
        year_clim_desired_min = year_den - 1
        year_clim_desired = self.df_disease['Year'].max() 

        climate = climate[
                        climate["Year"].between(year_clim_desired_min, year_clim_desired)
                    ].copy()

        print('The rolling monthly and yearly averages are calculated. Now, merging the disease and the climate data... ')

        climate = climate.drop_duplicates(subset=["Year", "Month"]).reset_index(drop = True)

        # =====================================================
        # Merging + Returning
        # =====================================================

        merged = pd.merge(
            self.df_disease,
            climate[
                [
                    "Year", "Month",
                    "mean_temperature", "mean_Rain",
                    "mean_SH", "Nino_anomaly", 
                    "MA_mean_temperature" , "MA_mean_Rain", "MA_mean_SH",
                    "YA_mean_temperature" , "YA_mean_Rain", "YA_mean_SH",
                ]
            ],
            on=["Year", "Month"],
            how="right"
        )

        merged["time"] = pd.to_datetime(
            dict(year=merged["Year"], month=merged["Month"], day=1)
        )

        # merged = merged.dropna(subset=['Count']).reset_index(drop=True)

        if merged.empty:
            raise ValueError(
                "Merged DataFrame is empty. This likely indicates a column/value mismatch "
                "between disease and climate data (e.g., Year/Month alignment or missing columns). \n"
                "Please verify that both datasets share overlapping Year and Month values"
            )
        else:
            print('The climate-disease data has been merged, now performing train-test split....')

        return merged

    # --------------------------------------------------
    # train/test split splitting
    # --------------------------------------------------
    def _train_test_split(
        self,
        train_year: int | None = None,
        test_year: int | None = None,
        drop_2020: bool = True,
    ):
        """
        Perform a time-aware train/test split for disease modeling.

        This method splits the merged dataset into training and testing subsets
        based on temporal ordering, ensuring no data leakage from future observations.

        Parameters
        ----------

        train_year : int or None, optional
            Last year to include in the training dataset.
            If None, the split is determined automatically.

        test_year : int or None, optional
            First year to include in the test dataset.
            If None, the split is determined automatically.

        drop_2020 : bool, default=True
            Whether to exclude data from the year 2020.
            This is useful to remove anomalies due to COVID-19 disruptions
            in disease reporting and transmission patterns.

        Returns
        -------

        None:
            Updates internal attributes:

            - self.train_df : pandas.DataFrame
                Training dataset

            - self.test_df : pandas.DataFrame
                Testing dataset

        Notes
        -----

        - The split preserves temporal ordering (no random shuffling).
        - Designed for time-series epidemiological data.
        - Avoids leakage by ensuring test data strictly follows training data.
        - If both `train_year` and `test_year` are provided, they must be consistent.

        Warning
        -------

        This is an internal method and is not intended for direct use.

        """

        if hasattr(self, "df") and self.df is not None:
            df = self.df.copy()
        else:
            df = self.df_merged.copy()

        if drop_2020:
            df = df[df["Year"] != 2020]

        # -------------------------------
        # SPLIT LOGIC
        # -------------------------------
        if train_year is None and test_year is None:
            train_df = df[df["Year"] < 2020].copy()
            test_df  = df[df["Year"] > 2020].copy()

        elif train_year is None:
            test_year = int(test_year)
            train_df = df[df["Year"] < test_year].copy()
            test_df  = df[df["Year"] >= test_year].copy()

        elif test_year is None:
            train_year = int(train_year)
            train_df = df[df["Year"] <= train_year].copy()
            test_df  = df[df["Year"] > train_year].copy()

        else:
            train_year = int(train_year)
            test_year  = int(test_year)

            if train_year >= test_year:
                raise ValueError("train_year must be earlier than test_year")

            train_df = df[df["Year"] <= train_year].copy()
            test_df  = df[df["Year"] >= test_year].copy()

        # Dropping NaN values from both train_df and test_df
        train_df = train_df.dropna(subset = ['Count']).reset_index(drop = True)

        test_df = test_df.dropna(subset = ['Count']).reset_index(drop = True)

        # -------------------------------
        # STORE INTERNALLY (KEY FIX)
        # -------------------------------
        self.train_df = train_df
        self.test_df  = test_df
        self.train_year = train_year
        self.test_year = test_year

        # # Optional debug
        # print("Split applied:")
        # print("Train years:", sorted(train_df["Year"].unique()))
        # print("Test years :", sorted(test_df["Year"].unique()))

        return train_df, test_df

    # --------------------------------------------------
    # Detect outbreaks
    # --------------------------------------------------
    def detect_historical_outbreaks(
        self,
        method: str = "zscore",
        window: int = 12,
        threshold: float = 2.0,
        date_col: str = "time",
    ):
        """
        Detect historical outbreak periods using anomaly-based thresholds.

        This method identifies unusually high disease incidence relative to
        a historical baseline using statistical anomaly detection techniques.

        Parameters
        ----------

        method : str, default="zscore" :
            Method used for anomaly detection. Supported options:

            - "zscore" :
                Computes standardized anomalies relative to rolling mean and
                standard deviation.

            - "percentile" :
                Flags observations exceeding a specified percentile threshold.

        window : int, default=12 : 
            Rolling window size (in time steps, typically months) used to compute
            baseline statistics such as mean and standard deviation.

        threshold : float, default=2.0 :
            Threshold for outbreak detection:

            - For "zscore":
                Number of standard deviations above the rolling mean.

            - For "percentile":
                Percentile cutoff (e.g., 0.9 for 90th percentile).

        date_col : str, default="time" : 
            Column name representing temporal ordering of the data.

        Returns
        -------

        df : pandas.DataFrame 
            DataFrame with additional columns :

            - anomaly_score : float
                - Computed anomaly metric (z-score or percentile-based)
            - outbreak_flag : int
                - Binary indicator (1 = outbreak, 0 = normal)

        Notes
        -----

        - The method assumes time-series structure in the dataset.
        - Rolling statistics are computed using past observations only
        (no future leakage).
        - Suitable for identifying epidemic spikes in climate-sensitive diseases.

        """

        df = self.df_merged.copy()
        df = df.sort_values(date_col)

        y = df[self.target_col]

        rolling_mean = y.rolling(window=window, min_periods=3).mean()
        rolling_std = y.rolling(window=window, min_periods=3).std()

        zscore = (y - rolling_mean) / (rolling_std + 1e-8)

        df["historical_outbreak_flag"] = zscore > threshold
        df["zscore"] = zscore

        self.df_outbreaks_hist = df

        return df[df["historical_outbreak_flag"]]

    @track_time("lag_optimization_seconds")
    def optimize_lags(
        self,
        base_models=("xgb", "rf"),
        residual_models=("rf", "extratrees"),
        correction_models=("ridge", "gbr", "isotonic"),
        n_trials=30,
        debug=False,
        n_jobs=-1,
        pruning_strategy="percentile",
        top_k=50,
        sh_range=range(0, 4),
        temp_range=range(0, 4),
        rain_range=range(0, 4),
        elnino_range=range(0, 13),
        percentile=90,
        scalar=None,
    ):
        """
        Optimize climate lag structures and model configurations using Bayesian search.

        This method identifies the optimal combination of lagged climate variables
        (e.g., temperature, rainfall, humidity, ENSO indices) along with the best
        model architecture using a hybrid AutoML framework.

        The optimization jointly searches over:
            - Climate lag configurations
            - Base models (signal learning)
            - Residual models (error correction)
            - Calibration models (prediction adjustment)

        Parameters
        ----------

        base_models : tuple of str, default=("xgb", "rf") :
            Models used to capture the primary disease–climate relationship.

            - Supported options include 
                - "xgb" : XGBoost
                - "rf" : Random Forest

        residual_models : tuple of str, default=("rf", "extratrees")
            Models used to capture residual patterns not explained by base models.

        correction_models : tuple of str, default=("ridge", "gbr", "isotonic")
            Models used for calibration or bias correction of predictions.

        n_trials : int, default=30
            Number of optimization iterations (Bayesian search trials).

        debug : bool, default=False
            If True, enables verbose logging and debugging output.

        n_jobs : int, default=-1
            Number of parallel jobs:
            - -1 uses all available CPU cores

        pruning_strategy : str, default="percentile"
            Strategy for pruning poor-performing configurations during search.

            - Options:
                - "percentile" : keeps top-performing configurations
                - "threshold" : uses fixed cutoff

        top_k : int, default=50
            Number of top configurations retained after pruning.

        sh_range : iterable, default=range(0, 4)
            Lag range for specific humidity (months).

        temp_range : iterable, default=range(0, 4)
            Lag range for temperature.

        rain_range : iterable, default=range(0, 4)
            Lag range for rainfall.

        elnino_range : iterable, default=range(0, 13)
            Lag range for ENSO (El Niño) index.

        percentile : int, default=90
            Percentile threshold used for pruning or model selection.

        scalar : object, optional
            Optional feature scaling object (e.g., StandardScaler).

        Returns
        -------

        tuple :
            (feature_metadata, lag_search_results, best_config)

            - feature_metadata : dict
                Information about generated lagged features

            - lag_search_results : pandas.DataFrame
                Performance of all evaluated configurations

            - best_config : dict
                Best-performing configuration (lags + model combination)

        Notes
        -----
        - Uses Bayesian optimization for efficient hyperparameter search.
        - Supports parallel evaluation of configurations.
        - Designed for climate-sensitive disease systems with delayed effects.
        - Lag ranges should reflect domain knowledge (e.g., incubation periods).

        """

        import numpy as np
        import pandas as pd
        import time
        from joblib import Parallel, delayed
        from sklearn.metrics import r2_score, root_mean_squared_error

        from sklearn.preprocessing import (
            StandardScaler,
            MinMaxScaler,
            MaxAbsScaler,
            RobustScaler,
            QuantileTransformer,
            PowerTransformer,
            Normalizer,
        )

        # --------------------------------------------------
        # Scaler registry
        # --------------------------------------------------
        scaler_dict = {
            "standard": StandardScaler(),
            "minmax": MinMaxScaler(),
            "maxabs": MaxAbsScaler(),
            "robust": RobustScaler(),
            "quantile": QuantileTransformer(output_distribution="normal"),
            "power": PowerTransformer(),
            "normalize": Normalizer(),
            None: None,
        }

        if scalar not in scaler_dict:
            raise ValueError(
                f"Invalid scaler '{scalar}'. Choose from {list(scaler_dict.keys())}"
            )

        scaler_obj = scaler_dict[scalar]

        # --------------------------------------------------
        # Load dataset
        # --------------------------------------------------
        if hasattr(self, "df_merged") and self.df_merged is not None:
            df = self.df_merged.copy()
        else:
            raise RuntimeError("Merged dataset not available.")

        # --------------------------------------------------
        # Lag definitions
        # --------------------------------------------------
        lag_defs = {
            "mean_SH": sh_range,
            "mean_temperature": temp_range,
            "mean_Rain": rain_range,
            "Nino_anomaly": elnino_range,
        }

        # --------------------------------------------------
        # Generate lag features
        # --------------------------------------------------
        for var, lags in lag_defs.items():
            for lag in lags:
                df[f"{var}_lag{lag}"] = df[var].shift(lag)

        df["Year"] = pd.to_numeric(df["Year"], errors="coerce")

        # --------------------------------------------------
        # ENSO interaction terms
        # --------------------------------------------------
        climate_vars = ["mean_SH", "mean_temperature", "mean_Rain"]

        for clim in climate_vars:
            for lag_c in lag_defs[clim]:
                for lag_n in lag_defs["Nino_anomaly"]:

                    clim_col = f"{clim}_lag{lag_c}"
                    nino_col = f"Nino_anomaly_lag{lag_n}"

                    inter_col = f"{clim}_lag{lag_c}_x_Nino_anomaly_lag{lag_n}"
                    df[inter_col] = df[clim_col] * df[nino_col]

        # --------------------------------------------------
        # Final dataframe with engineered features
        # --------------------------------------------------
        self.df = df.dropna().reset_index(drop=True)

        # --------------------------------------------------
        # Train test split
        # --------------------------------------------------
        self._train_test_split(
            train_year=getattr(self, "train_year", None),
            test_year=getattr(self, "test_year", None)
        )

        print('Initial Train Test Split completed...')
        print('Now performing lag optimisation....')

        # --------------------------------------------------
        # Fit scaler
        # --------------------------------------------------
        if scaler_obj is not None:

            numeric_cols = [
                c for c in self.train_df.select_dtypes(include=[np.number]).columns
                if c != "Year"
            ]

            if len(numeric_cols) > 0:

                scaler_obj.fit(self.train_df[numeric_cols])

                self.train_df.loc[:, numeric_cols] = scaler_obj.transform(
                    self.train_df[numeric_cols]
                )

                self.test_df.loc[:, numeric_cols] = scaler_obj.transform(
                    self.test_df[numeric_cols]
                )

        # --------------------------------------------------
        # Feature grid
        # --------------------------------------------------
        if debug:
            all_features = [[
                "mean_SH_lag1",
                "mean_temperature_lag2",
                "mean_Rain_lag2",
                "Nino_anomaly_lag12",
                "Year",
                "MA_mean_temperature","MA_mean_Rain","MA_mean_SH",
                "YA_mean_temperature","YA_mean_Rain","YA_mean_SH",
            ]]
        else:

            all_features = [
                [
                    f"mean_SH_lag{rh}",
                    f"mean_temperature_lag{t}",
                    f"mean_Rain_lag{r}",
                    f"Nino_anomaly_lag{n}",

                    f"mean_SH_lag{rh}_x_Nino_anomaly_lag{n}",
                    f"mean_temperature_lag{t}_x_Nino_anomaly_lag{n}",
                    f"mean_Rain_lag{r}_x_Nino_anomaly_lag{n}",

                    "Year",
                    "MA_mean_temperature","MA_mean_Rain","MA_mean_SH",
                    "YA_mean_temperature","YA_mean_Rain","YA_mean_SH",
                ]

                for rh in lag_defs["mean_SH"]
                for t in lag_defs["mean_temperature"]
                for r in lag_defs["mean_Rain"]
                for n in lag_defs["Nino_anomaly"]
            ]

        # --------------------------------------------------
        # Config grid
        # --------------------------------------------------
        configs = [
            (feats, base, res, corr)
            for feats in all_features
            for base in base_models if is_model_available(base)
            for res in residual_models if is_model_available(res)
            for corr in correction_models if is_model_available(corr)
        ]

        if len(configs) == 0:
            raise RuntimeError("No valid model configurations available.")

        # --------------------------------------------------
        # Precompute feature matrices
        # --------------------------------------------------
        feature_pool = sorted({f for feats,_,_,_ in configs for f in feats})

        X_train_full = self.train_df[feature_pool].to_numpy()
        X_test_full = self.test_df[feature_pool].to_numpy()

        y_train = self.train_df[self.target_col].to_numpy()
        y_test = self.test_df[self.target_col].to_numpy()

        feature_index = {f: i for i, f in enumerate(feature_pool)}

        # --------------------------------------------------
        # Stage 1: Base screening
        # --------------------------------------------------
        def _evaluate_base_screen(idx, feats, base_model):

            try:

                seed = (
                    self.random_state + idx
                    if self.random_state is not None else None
                )

                if seed is not None:
                    np.random.seed(seed)

                cols = [feature_index[f] for f in feats]

                X_train = X_train_full[:, cols]
                X_test = X_test_full[:, cols]

                base_defaults = DEFAULT_PARAMS.get(base_model, {}).copy()

                if seed is not None and "random_state" in str(MODEL_REGISTRY[base_model]):
                    base_defaults["random_state"] = seed

                if "n_jobs" in str(MODEL_REGISTRY[base_model]):
                    base_defaults["n_jobs"] = 1

                model = MODEL_REGISTRY[base_model](**base_defaults)

                model.fit(X_train, y_train)

                preds = model.predict(X_test)

                base_rmse = root_mean_squared_error(y_test, preds)

                return {"idx": idx, "base_rmse": base_rmse}

            except Exception:

                return {"idx": idx, "base_rmse": np.inf}

        base_scores = Parallel(n_jobs=n_jobs)(
            delayed(_evaluate_base_screen)(i, feats, base)
            for i,(feats,base,_,_) in enumerate(configs)
        )

        base_df = pd.DataFrame(base_scores)

        # --------------------------------------------------
        # Hierarchical pruning
        # --------------------------------------------------
        if pruning_strategy is None:
            selected_indices = base_df["idx"].tolist()

        elif pruning_strategy == "top_k":

            k = min(top_k, len(base_df))
            selected_indices = base_df.nsmallest(k, "base_rmse")["idx"].tolist()

        elif pruning_strategy == "percentile":

            valid_scores = base_df["base_rmse"].replace(np.inf, np.nan).dropna()

            if len(valid_scores)==0:
                selected_indices = base_df["idx"].tolist()
            else:
                threshold = np.percentile(valid_scores, percentile)

                selected_indices = base_df[
                    base_df["base_rmse"] <= threshold
                ]["idx"].tolist()
        else:
            raise ValueError("pruning_strategy must be {'top_k','percentile',None}")

        if len(selected_indices)==0:
            selected_indices = base_df.nsmallest(10, "base_rmse")["idx"].tolist()

        pruned_configs = [configs[i] for i in selected_indices]

        # --------------------------------------------------
        # Stage 2: Full stacked optimization
        # --------------------------------------------------
        results = Parallel(n_jobs=n_jobs)(
            delayed(_evaluate_configuration)(
                feats,
                base,
                res,
                corr,
                self.train_df,
                self.test_df,
                self.target_col,
                n_trials,
                (
                    self.random_state + idx
                    if self.random_state is not None else None
                ),
            )
            for idx,(feats,base,res,corr) in enumerate(pruned_configs)
        )

        self.lag_search_results = pd.DataFrame(results)

        self.best_config = (
            self.lag_search_results.sort_values("rmse", ascending=True).iloc[0]
        )

        self.scaler = scaler_obj

        self.scaled_columns = [
            c for c in self.train_df.select_dtypes(include=[np.number]).columns
            if c != "Year"
        ]

        if self._pipeline_start_time is None:
            self._pipeline_start_time = time.perf_counter()

        # --------------------------------------------------
        # Store feature metadata
        # --------------------------------------------------
        lag_map = {}
        interaction_map = []

        for feat in self.best_config["features"]:

            if "_lag" in feat and "_x_" not in feat:

                base, lag = feat.split("_lag")
                lag = int(lag)

                lag_map.setdefault(base, set()).add(lag)

            if "_x_" in feat:

                left, right = feat.split("_x_")

                # fix shorthand naming if it occurs
                if right.startswith("nino_"):
                    right = right.replace("nino_", "Nino_anomaly_")

                interaction_map.append((left, right))

        self.feature_metadata = {
            "lags": {k: sorted(v) for k, v in lag_map.items()},
            "interactions": interaction_map,
            "scaled_columns": self.scaled_columns
        }

        return self.feature_metadata, self.lag_search_results, self.best_config

    # --------------------------------------------------
    # Train final triple-stacked model
    # -------------------------------------------------
    @track_time("training_seconds")
    def train_final_model(self):
        """
        Train the final disease prediction model using the optimized feature set.

        This method fits the final model after preprocessing, feature engineering,
        and lag optimization steps have been completed. It typically uses the
        best-performing configuration identified during model selection (e.g.,
        stacked learning framework or tuned estimator).

        The trained model is stored internally and used for subsequent prediction
        and projection tasks.

        Parameters
        ----------

        None

        Returns
        -------

        self : DiseaseModel
            Returns the instance with the trained model stored internally.

        Notes
        -----

        - Assumes that data ingestion and preprocessing have already been performed.
        - Requires optimized lagged features to be available.
        - May internally split data into training and validation sets.
        - Supports integration with ensemble or stacked models if configured.
        """

        if self.best_config is None:
            raise RuntimeError("Run optimize_lags() first.")

        import numpy as np
        import pandas as pd
        from sklearn.metrics import r2_score, root_mean_squared_error

        feats = self.best_config["features"]
        X_train = self.train_df[feats]
        y_train = self.train_df[self.target_col]
        X_test = self.test_df[feats]
        y_test = self.test_df[self.target_col]

        # ======================================================
        # BASE MODEL
        # ======================================================
        base_model_name = self.best_config["base_model"]
        base_params = self.best_config["base_params"].copy()

        if self.random_state is not None and "random_state" in str(MODEL_REGISTRY[base_model_name]):
            base_params["random_state"] = self.random_state

        self.base = MODEL_REGISTRY[base_model_name](**base_params)
        self.base.fit(X_train, y_train)

        y_base_train = self.base.predict(X_train)
        y_base_test = self.base.predict(X_test)

        # ======================================================
        # RESIDUAL STAGE
        # ======================================================
        res_model_name = self.best_config["residual_model"]

        if res_model_name in [None, "none", "base_only"]:
            self.res = None
            y_res_train = np.zeros_like(y_base_train)
            y_res_test = np.zeros_like(y_base_test)
        else:
            res_params = self.best_config["residual_params"].copy()

            if self.random_state is not None and "random_state" in str(MODEL_REGISTRY[res_model_name]):
                res_params["random_state"] = self.random_state

            self.res = MODEL_REGISTRY[res_model_name](**res_params)
            self.res.fit(X_train, y_train - y_base_train)

            y_res_train = self.res.predict(X_train)
            y_res_test = self.res.predict(X_test)

        # ======================================================
        # CORRECTION STAGE
        # ======================================================
        corr_model_name = self.best_config["correction_model"]

        raw_train_preds = y_base_train + y_res_train
        raw_test_preds = y_base_test + y_res_test

        # RMSE baseline (instead of R2)
        baseline_rmse = root_mean_squared_error(y_test, raw_test_preds)

        if corr_model_name in [None, "none", "base_only"]:
            self.corr = None
            final_test = raw_test_preds

        elif corr_model_name == "isotonic":
            from sklearn.isotonic import IsotonicRegression

            iso = IsotonicRegression(out_of_bounds="clip")
            iso.fit(raw_train_preds, y_train)
            iso_test = iso.predict(raw_test_preds)

            iso_rmse = root_mean_squared_error(y_test, iso_test)

            # RMSE-based guard
            if iso_rmse < baseline_rmse:
                self.corr = iso
                final_test = iso_test
            else:
                self.corr = None
                final_test = raw_test_preds

        else:
            corr_params = self.best_config["correction_params"].copy()

            if self.random_state is not None and "random_state" in str(MODEL_REGISTRY[corr_model_name]):
                corr_params["random_state"] = self.random_state

            self.corr = MODEL_REGISTRY[corr_model_name](**corr_params)

            X_corr_train = np.asarray(raw_train_preds).reshape(-1, 1)
            X_corr_test = np.asarray(raw_test_preds).reshape(-1, 1)

            self.corr.fit(X_corr_train, y_train)
            corr_test = self.corr.predict(X_corr_test)

            corr_rmse = root_mean_squared_error(y_test, corr_test)

            # RMSE-based safety check
            if corr_rmse < baseline_rmse:
                final_test = corr_test
            else:
                self.corr = None
                final_test = raw_test_preds

        # ======================================================
        # METRICS (FINAL)
        # ======================================================
        self.rmse = root_mean_squared_error(y_test, final_test)
        self.r2 = r2_score(y_test, final_test)

        # ======================================================
        # Prediction DataFrame
        # ======================================================
        self.dpred = pd.DataFrame({
            "time": self.test_df["time"],
            "Actual": y_test,
            "Predicted": final_test,
        }).reset_index(drop=True)

        # ======================================================
        # Uncertainty (Gaussian approx)
        # ======================================================
        self.dpred["Predicted_lower"] = (
            self.dpred["Predicted"] - 1.96 * self.rmse
        ).clip(lower=0)

        self.dpred["Predicted_upper"] = (
            self.dpred["Predicted"] + 1.96 * self.rmse
        )

        # ======================================================
        # Return
        # ======================================================
        return {
            "test_r2": self.r2,
            "test_rmse": self.rmse,
            "predictions": self.dpred,
            "model_info": {
                "base_model": self.best_config["base_model"],
                "residual_model": self.best_config["residual_model"],
                "correction_model": self.best_config["correction_model"],
                "features": self.best_config["features"],
            },
            "data_summary": {
                "train_size": len(self.train_df),
                "test_size": len(self.test_df),
                "train_period": str(self.train_df["time"].min()),
                "test_period": str(self.test_df["time"].max()),
            }
        }

    # --------------------------------------------------
    # AUTO BUILD REPORT ARTIFACTS 
    # --------------------------------------------------
    @track_time("report_generation_seconds")
    def build_report_artifacts(self, projection_summary, tidy_df):
        """
        Construct standardized reporting artifacts from model outputs.

        This method generates a structured collection of artifacts required for
        downstream reporting and visualization. It transforms model outputs,
        including projections and evaluation summaries, into a consistent format
        that can be used by the reporting layer (e.g., dashboards, plots, or exports).

        Users are not expected to manually assemble these artifacts. This function
        ensures that all required components are created automatically and in a
        reproducible format.

        Parameters
        ----------

        projection_summary : dict or pandas.DataFrame
            Summary of model projections, typically produced by the projection
            pipeline (e.g., `DiseaseProjection`). This may include predicted values,
            confidence intervals, and temporal aggregation.

        Returns
        -------

        artifacts : ReportArtifacts
            - A structured object containing all elements required for reporting,
            such as:
                - Processed projection data
                - Evaluation metrics (if available)
                - Metadata (e.g., model configuration, time range)
                - Visualization-ready datasets

        Notes
        -----

        - This method is part of the reporting pipeline and is typically called
        after model training and projection steps.
        - Ensures consistency between modeling outputs and reporting interfaces.
        - Designed to support reproducible research workflows.
        """

        from climaid.reporting import ReportArtifacts

        if self.best_config is None:
            raise RuntimeError("Run optimize_lags() first.")

        if not hasattr(self, "r2"):
            raise RuntimeError("Run train_final_model() first.")

        # ---------------------------
        # Metrics
        # ---------------------------
        metrics = {
            "test_r2": np.round(self.r2,2), 
            "test_rmse": np.round(self.rmse, 2),
        }

        # ---------------------------
        # Selected lags (auto-extract)
        # ---------------------------
        selected_lags = {}
        interaction_lags = []

        for f in self.best_config['features']:

            # --------------------------
            # Interaction terms
            # --------------------------
            if "_x_" in f:

                left, right = f.split("_x_")

                if "_lag" in left and "_lag" in right:

                    var1, lag1 = left.split("_lag")
                    var2, lag2 = right.split("_lag")

                    interaction_lags.append({
                        "var1": var1,
                        "lag1": int(lag1),
                        "var2": var2,
                        "lag2": int(lag2)
                    })

            # --------------------------
            # Single lag terms
            # --------------------------
            elif "_lag" in f:

                var, lag = f.split("_lag")

                selected_lags[var] = int(lag)

        # ---------------------------
        # Feature importance 
        # ---------------------------
        importance = {}
        if hasattr(self.base, "feature_importances_"):
            importance = dict(
                zip(self.best_config["features"], self.base.feature_importances_)
            )

        # ---------------------------
        # Model metadata
        # ---------------------------
        model_info = {
            "base_model": self.best_config["base_model"],
            "residual_model": self.best_config["residual_model"],
            "correction_model": self.best_config["correction_model"],
            "stacking_pipeline": "Base → Residual → Correction",
            "n_features": len(self.best_config["features"]),
        }

        # ---------------------------
        # Data summary
        # ---------------------------
        # Ensure datetime safety (prevents malformed tokens in reports)
        train_time = pd.to_datetime(self.train_df["time"], errors="coerce")
        test_time = pd.to_datetime(self.test_df["time"], errors="coerce")

        # Drop invalid timestamps if any
        train_time = train_time.dropna()
        test_time = test_time.dropna()

        # Format clean ISO dates for reporting (LLM-friendly)
        if not train_time.empty:
            train_start = train_time.min().strftime("%d-%m-%Y")
            train_end = train_time.max().strftime("%d-%m-%Y")
        else:
            train_start, train_end = "Unknown", "Unknown"

        if not test_time.empty:
            test_start = test_time.min().strftime("%d-%m-%Y")
            test_end = test_time.max().strftime("%d-%m-%Y")
        else:
            test_start, test_end = "Unknown", "Unknown"

        data_summary = {
            "train_size": int(len(self.train_df)),
            "test_size": int(len(self.test_df)),
            "train_period": f"{train_start} to {train_end}",
            "test_period": f"{test_start} to {test_end}",
        }

        # ---------------------------
        # Create artifacts (AUTO)
        # ---------------------------
        artifacts = ReportArtifacts(
            district=self.district,
            disease_name=self.disease_name,
            date_range=f"{train_start} to {test_end}",
            metrics=metrics,
            selected_lags=selected_lags,
            interaction_lags = interaction_lags,
            features=self.best_config["features"],
            importance=importance,
            projection_summary=projection_summary,
            runtime=self.runtime,
            model_info=model_info,
            data_summary=data_summary,
            download_data=tidy_df,
        )

        return artifacts

    @track_time("prediction_seconds")
    def predict(self, X: pd.DataFrame) -> np.ndarray:
        """
        Generate disease case predictions using the trained stacked model.

        This method applies the full prediction pipeline consisting of:

        1. **Base model** – Generates initial predictions from input features.
        2. **Residual model** – Learns and predicts residual errors from the base model.
        3. **Correction step** – Combines base predictions and residual corrections
        to produce final refined predictions.

        The method assumes that the model has already been trained using
        `train_final_model`.

        Parameters
        ----------

        X : pandas.DataFrame :
            Input feature matrix containing the same variables used during training,
            including climate variables and any engineered lagged features.

        Returns
        -------

        predictions : numpy.ndarray
            Array of predicted disease case counts (or risk scores), aligned with
            the input samples in `X`.

        Notes
        -----

        - Input features must match the training schema (column names and preprocessing).
        - If lagged features were used during training, they must be present in `X`.
        - This method uses the internally stored trained models and does not retrain.
        """

        # -----------------------
        # Safety checks
        # -----------------------
        if not hasattr(self, "base"):
            raise RuntimeError(
                "diseaseModel has not been trained. "
                "Call train_final_model() before predict()."
            )

        # Ensure correct feature order
        X = X[self.best_config["features"]]

        # -----------------------
        # Base model prediction
        # -----------------------
        base_pred = self.base.predict(X)

        # -----------------------
        # Residual correction
        # -----------------------
        if hasattr(self, "res") and self.res is not None:
            resid_pred = self.res.predict(X)
            combined_pred = base_pred + resid_pred
        else:
            combined_pred = base_pred

        # -----------------------
        # Calibration correction
        # -----------------------
        if hasattr(self, "cor") and self.corr is not None:
            final_pred = self.corr.predict(combined_pred)
            # upperbound = final_pred + 1.96 * self.rmse
            # lowerbound = final_pred - 1.96 * self.rmse
        else:
            final_pred = combined_pred
            # upperbound = final_pred + 1.96 * self.rmse
            # lowerbound = final_pred - 1.96 * self.rmse

        return final_pred     

    # --------------------------------------------------
    # Reporting
    # --------------------------------------------------
    @track_time("report_generation_seconds")
    def generate_report(self, 
                        projection_summary = None, 
                        llm_client=None, 
                        tidy_df=None, 
                        style="detailed", 
                        open_browser = False, 
                        save_copy=False, 
                        output_dir = None):
        """
        Generate a comprehensive disease projection report.

        This method provides a fully automated reporting pipeline that transforms
        model outputs into a structured, human-readable report. It integrates
        projections, optional language model summarization, and formatted outputs
        for visualization or sharing.

        Users are not required to manually construct intermediate artifacts—this
        method handles all necessary steps internally, including artifact creation,
        formatting, and optional rendering.

        Parameters
        ----------

        projection_summary : dict or pandas.DataFrame, optional
            Precomputed projection results. If not provided, the method may internally
            generate projections using the trained model.

        llm_client : object, optional
            Language model client used to generate narrative summaries or insights.
            If provided, the report may include AI-generated interpretations of trends.

        tidy_df : pandas.DataFrame
            DataFrame for exporting to an excel file. 

        style : {"detailed", "summary", "policy"}, default="detailed"
            - Level of detail in the generated report:
                - "detailed": Includes full analysis, metrics, and explanations.
                - "summary": Provides a concise overview of key findings.
                - "policy": Provides a comprehensive policy report. 

        open_browser : bool, default=False
            If True, automatically opens the generated report in a web browser.

        save_copy : bool, default=False
            If True, saves a copy of the report to disk.

        output_dir : str, optional
            Directory where the report will be saved if `save_copy=True`.
            If not specified, a default output location is used.

        Returns
        -------

        report : str or pathlib.Path
            Path to the generated report file or rendered output, depending on configuration.

        Notes
        -----

        - This method combines multiple pipeline stages:
            1. Projection (if not provided)
            2. Artifact construction
            3. Report formatting and rendering
        - Designed for end-to-end usability with minimal user intervention.
        - Supports integration with LLMs for enhanced interpretability.
        """

        import time
        from climaid.reporting import DiseaseReporter, open_report_in_browser

        if projection_summary is None:
            projection_summary = {
                "mode": "historical_only",
                "note": "No climate projection summary provided. Report based on historical model performance only."
            }

        # Auto-build artifacts internally
        artifacts = self.build_report_artifacts(projection_summary, tidy_df)

        # Create reporter
        reporter = DiseaseReporter(llm_client=llm_client)
        report = reporter.generate(artifacts, style=style)

        if self._pipeline_start_time is not None:
            self.runtime["total_pipeline_seconds"] = (
                time.perf_counter() - self._pipeline_start_time
            )

        if open_browser:

            from climaid.reporting import open_report_in_browser

            title = f"ClimAID {self.disease_name} Risk Intelligence Report"

            report_path = open_report_in_browser(
                report_text=report,
                artifacts=artifacts,
                title=title,
                save_copy=save_copy,
                output_dir="climaid_outputs/reports"
            )

        # Generate report
        return report

    # --------------------------------------------------
    # PLOTTING FUNCTION
    # --------------------------------------------------
    def plot_historical_predictions(
            self,
            actual_color='#006d77',
            prediction_color='#e29578',
            hatch_color = '#ffddd2',
            figsize=(12, 5),
            hatch = '.',
            save=False,
            alpha = 0.4, 
            path='path',
            theme='ticks',
            dpi=500,
        ):
            """
            Plot observed vs predicted disease cases over time.

            This method visualizes the model's performance by comparing historical
            observed values with predicted values. It helps assess model fit,
            identify temporal patterns, and detect systematic deviations.

            The plot may optionally include shaded or hatched regions to highlight
            differences between observed and predicted values.

            Parameters
            ----------

            actual_color : str, default='#006d77'
                Color used for plotting observed (true) disease cases.

            prediction_color : str, default='#e29578'
                Color used for plotting model predictions.

            hatch_color : str, default='#ffddd2'
                Color used for shaded or hatched regions representing prediction error
                or uncertainty.

            figsize : tuple of int, default=(12, 5)
                Size of the figure in inches (width, height).

            hatch : str, default='.'
                Matplotlib hatch pattern used to highlight differences between
                observed and predicted values.

            save : bool, default=False
                If True, saves the plot to disk.

            alpha : float, default=0.4
                Transparency level for shaded or hatched regions.

            path : str, default='path'
                File path where the plot will be saved if `save=True`.

            theme : str, default='ticks'
                Seaborn or matplotlib style theme applied to the plot.

            dpi : int, default=500
                Resolution of the saved figure in dots per inch.

            Returns
            -------

            - None
            - Displays the plot and optionally saves it to disk.

            Notes
            -----

            - Requires the model to be trained and predictions to be available.
            - Assumes temporal alignment between observed and predicted values.
            - Useful for diagnostic evaluation of model performance.
            """

            import matplotlib.pyplot as plt
            import seaborn as sns
            import matplotlib.dates as mdates

            if "Actual" not in self.dpred.columns:
                raise KeyError("Dataframe must contain 'Actual' column.")

            sns.set_theme(style=theme, font_scale=1.2)
            fig, ax = plt.subplots(figsize=figsize)
            ax.set_facecolor('#f8f9fa')

            ax.plot(self.dpred['time'], self.dpred['Actual'], label='Actual cases',
                    marker='8', color=actual_color, linewidth=3)

            ax.plot(self.dpred['time'], self.dpred['Predicted'], label='Predicted cases',
                    marker='o', color=prediction_color, linewidth=3)

            ax.fill_between(
                self.dpred['time'],
                self.dpred['Predicted_lower'],
                self.dpred['Predicted_upper'],
                hatch=hatch,
                linewidth=0.0,
                color=hatch_color,
                alpha=alpha
            )

            ax.legend(loc='upper left', ncol=2, frameon=False)
            ax.set_ylabel('Cases', weight='demi')
            ax.grid(alpha=0.2, linestyle='--', color='#6c757d')

            ax.set_xlabel("Month-Year", weight='demi')
            ax.xaxis.set_major_locator(mdates.MonthLocator(interval=1))
            ax.xaxis.set_major_formatter(mdates.DateFormatter('%b %y'))
            plt.xticks(fontsize=10)

            plt.text(
                0.715, 0.9675,
                f"$R^2$ = {self.r2:.3f}; RMSE = {self.rmse:.2f}",
                transform=plt.gca().transAxes,
                fontsize=14,
                verticalalignment='top'
            )

            fig.autofmt_xdate(rotation=90)
            plt.tight_layout()

            if save:
                plt.savefig(path, dpi=dpi)

            plt.show()

    # --------------------------------------------------
    # Time Summary
    # --------------------------------------------------
    def print_runtime_summary(self):
        """
        Prints a clean runtime summary for the full pipeline.
        """
        if not hasattr(self, "runtime") or not self.runtime:
            print("Runtime information not available.")
            return

        print("\n==============================")
        print("⏱ ClimAID Pipeline Runtime Summary")
        print("==============================")

        total = 0.0
        for k, v in self.runtime.items():
            if isinstance(v, (int, float)):
                print(f"{k.replace('_', ' ').title()}: {v:.2f} seconds")
                total += v

        if total > 0:
            print("------------------------------")
            print(f"Total Computation Time: {total:.2f} seconds")
            print("==============================\n")

detect_historical_outbreaks(method='zscore', window=12, threshold=2.0, date_col='time')

Detect historical outbreak periods using anomaly-based thresholds.

This method identifies unusually high disease incidence relative to a historical baseline using statistical anomaly detection techniques.

Parameters

str, default="zscore" :

Method used for anomaly detection. Supported options:

  • "zscore" : Computes standardized anomalies relative to rolling mean and standard deviation.

  • "percentile" : Flags observations exceeding a specified percentile threshold.

int, default=12 :

Rolling window size (in time steps, typically months) used to compute baseline statistics such as mean and standard deviation.

float, default=2.0 :

Threshold for outbreak detection:

  • For "zscore": Number of standard deviations above the rolling mean.

  • For "percentile": Percentile cutoff (e.g., 0.9 for 90th percentile).

str, default="time" :

Column name representing temporal ordering of the data.

Returns

pandas.DataFrame

DataFrame with additional columns :

  • anomaly_score : float
    • Computed anomaly metric (z-score or percentile-based)
  • outbreak_flag : int
    • Binary indicator (1 = outbreak, 0 = normal)

Notes

  • The method assumes time-series structure in the dataset.
  • Rolling statistics are computed using past observations only (no future leakage).
  • Suitable for identifying epidemic spikes in climate-sensitive diseases.
Source code in climaid\climaid_model.py
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
def detect_historical_outbreaks(
    self,
    method: str = "zscore",
    window: int = 12,
    threshold: float = 2.0,
    date_col: str = "time",
):
    """
    Detect historical outbreak periods using anomaly-based thresholds.

    This method identifies unusually high disease incidence relative to
    a historical baseline using statistical anomaly detection techniques.

    Parameters
    ----------

    method : str, default="zscore" :
        Method used for anomaly detection. Supported options:

        - "zscore" :
            Computes standardized anomalies relative to rolling mean and
            standard deviation.

        - "percentile" :
            Flags observations exceeding a specified percentile threshold.

    window : int, default=12 : 
        Rolling window size (in time steps, typically months) used to compute
        baseline statistics such as mean and standard deviation.

    threshold : float, default=2.0 :
        Threshold for outbreak detection:

        - For "zscore":
            Number of standard deviations above the rolling mean.

        - For "percentile":
            Percentile cutoff (e.g., 0.9 for 90th percentile).

    date_col : str, default="time" : 
        Column name representing temporal ordering of the data.

    Returns
    -------

    df : pandas.DataFrame 
        DataFrame with additional columns :

        - anomaly_score : float
            - Computed anomaly metric (z-score or percentile-based)
        - outbreak_flag : int
            - Binary indicator (1 = outbreak, 0 = normal)

    Notes
    -----

    - The method assumes time-series structure in the dataset.
    - Rolling statistics are computed using past observations only
    (no future leakage).
    - Suitable for identifying epidemic spikes in climate-sensitive diseases.

    """

    df = self.df_merged.copy()
    df = df.sort_values(date_col)

    y = df[self.target_col]

    rolling_mean = y.rolling(window=window, min_periods=3).mean()
    rolling_std = y.rolling(window=window, min_periods=3).std()

    zscore = (y - rolling_mean) / (rolling_std + 1e-8)

    df["historical_outbreak_flag"] = zscore > threshold
    df["zscore"] = zscore

    self.df_outbreaks_hist = df

    return df[df["historical_outbreak_flag"]]

optimize_lags(base_models=('xgb', 'rf'), residual_models=('rf', 'extratrees'), correction_models=('ridge', 'gbr', 'isotonic'), n_trials=30, debug=False, n_jobs=-1, pruning_strategy='percentile', top_k=50, sh_range=range(0, 4), temp_range=range(0, 4), rain_range=range(0, 4), elnino_range=range(0, 13), percentile=90, scalar=None)

Optimize climate lag structures and model configurations using Bayesian search.

This method identifies the optimal combination of lagged climate variables (e.g., temperature, rainfall, humidity, ENSO indices) along with the best model architecture using a hybrid AutoML framework.

The optimization jointly searches over
  • Climate lag configurations
  • Base models (signal learning)
  • Residual models (error correction)
  • Calibration models (prediction adjustment)

Parameters

tuple of str, default=("xgb", "rf") :

Models used to capture the primary disease–climate relationship.

  • Supported options include
    • "xgb" : XGBoost
    • "rf" : Random Forest
tuple of str, default=("rf", "extratrees")

Models used to capture residual patterns not explained by base models.

tuple of str, default=("ridge", "gbr", "isotonic")

Models used for calibration or bias correction of predictions.

int, default=30

Number of optimization iterations (Bayesian search trials).

bool, default=False

If True, enables verbose logging and debugging output.

int, default=-1

Number of parallel jobs: - -1 uses all available CPU cores

str, default="percentile"

Strategy for pruning poor-performing configurations during search.

  • Options:
    • "percentile" : keeps top-performing configurations
    • "threshold" : uses fixed cutoff
int, default=50

Number of top configurations retained after pruning.

iterable, default=range(0, 4)

Lag range for specific humidity (months).

iterable, default=range(0, 4)

Lag range for temperature.

iterable, default=range(0, 4)

Lag range for rainfall.

iterable, default=range(0, 13)

Lag range for ENSO (El Niño) index.

int, default=90

Percentile threshold used for pruning or model selection.

object, optional

Optional feature scaling object (e.g., StandardScaler).

Returns

tuple

(feature_metadata, lag_search_results, best_config)

  • feature_metadata : dict Information about generated lagged features

  • lag_search_results : pandas.DataFrame Performance of all evaluated configurations

  • best_config : dict Best-performing configuration (lags + model combination)

Notes

  • Uses Bayesian optimization for efficient hyperparameter search.
  • Supports parallel evaluation of configurations.
  • Designed for climate-sensitive disease systems with delayed effects.
  • Lag ranges should reflect domain knowledge (e.g., incubation periods).
Source code in climaid\climaid_model.py
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
@track_time("lag_optimization_seconds")
def optimize_lags(
    self,
    base_models=("xgb", "rf"),
    residual_models=("rf", "extratrees"),
    correction_models=("ridge", "gbr", "isotonic"),
    n_trials=30,
    debug=False,
    n_jobs=-1,
    pruning_strategy="percentile",
    top_k=50,
    sh_range=range(0, 4),
    temp_range=range(0, 4),
    rain_range=range(0, 4),
    elnino_range=range(0, 13),
    percentile=90,
    scalar=None,
):
    """
    Optimize climate lag structures and model configurations using Bayesian search.

    This method identifies the optimal combination of lagged climate variables
    (e.g., temperature, rainfall, humidity, ENSO indices) along with the best
    model architecture using a hybrid AutoML framework.

    The optimization jointly searches over:
        - Climate lag configurations
        - Base models (signal learning)
        - Residual models (error correction)
        - Calibration models (prediction adjustment)

    Parameters
    ----------

    base_models : tuple of str, default=("xgb", "rf") :
        Models used to capture the primary disease–climate relationship.

        - Supported options include 
            - "xgb" : XGBoost
            - "rf" : Random Forest

    residual_models : tuple of str, default=("rf", "extratrees")
        Models used to capture residual patterns not explained by base models.

    correction_models : tuple of str, default=("ridge", "gbr", "isotonic")
        Models used for calibration or bias correction of predictions.

    n_trials : int, default=30
        Number of optimization iterations (Bayesian search trials).

    debug : bool, default=False
        If True, enables verbose logging and debugging output.

    n_jobs : int, default=-1
        Number of parallel jobs:
        - -1 uses all available CPU cores

    pruning_strategy : str, default="percentile"
        Strategy for pruning poor-performing configurations during search.

        - Options:
            - "percentile" : keeps top-performing configurations
            - "threshold" : uses fixed cutoff

    top_k : int, default=50
        Number of top configurations retained after pruning.

    sh_range : iterable, default=range(0, 4)
        Lag range for specific humidity (months).

    temp_range : iterable, default=range(0, 4)
        Lag range for temperature.

    rain_range : iterable, default=range(0, 4)
        Lag range for rainfall.

    elnino_range : iterable, default=range(0, 13)
        Lag range for ENSO (El Niño) index.

    percentile : int, default=90
        Percentile threshold used for pruning or model selection.

    scalar : object, optional
        Optional feature scaling object (e.g., StandardScaler).

    Returns
    -------

    tuple :
        (feature_metadata, lag_search_results, best_config)

        - feature_metadata : dict
            Information about generated lagged features

        - lag_search_results : pandas.DataFrame
            Performance of all evaluated configurations

        - best_config : dict
            Best-performing configuration (lags + model combination)

    Notes
    -----
    - Uses Bayesian optimization for efficient hyperparameter search.
    - Supports parallel evaluation of configurations.
    - Designed for climate-sensitive disease systems with delayed effects.
    - Lag ranges should reflect domain knowledge (e.g., incubation periods).

    """

    import numpy as np
    import pandas as pd
    import time
    from joblib import Parallel, delayed
    from sklearn.metrics import r2_score, root_mean_squared_error

    from sklearn.preprocessing import (
        StandardScaler,
        MinMaxScaler,
        MaxAbsScaler,
        RobustScaler,
        QuantileTransformer,
        PowerTransformer,
        Normalizer,
    )

    # --------------------------------------------------
    # Scaler registry
    # --------------------------------------------------
    scaler_dict = {
        "standard": StandardScaler(),
        "minmax": MinMaxScaler(),
        "maxabs": MaxAbsScaler(),
        "robust": RobustScaler(),
        "quantile": QuantileTransformer(output_distribution="normal"),
        "power": PowerTransformer(),
        "normalize": Normalizer(),
        None: None,
    }

    if scalar not in scaler_dict:
        raise ValueError(
            f"Invalid scaler '{scalar}'. Choose from {list(scaler_dict.keys())}"
        )

    scaler_obj = scaler_dict[scalar]

    # --------------------------------------------------
    # Load dataset
    # --------------------------------------------------
    if hasattr(self, "df_merged") and self.df_merged is not None:
        df = self.df_merged.copy()
    else:
        raise RuntimeError("Merged dataset not available.")

    # --------------------------------------------------
    # Lag definitions
    # --------------------------------------------------
    lag_defs = {
        "mean_SH": sh_range,
        "mean_temperature": temp_range,
        "mean_Rain": rain_range,
        "Nino_anomaly": elnino_range,
    }

    # --------------------------------------------------
    # Generate lag features
    # --------------------------------------------------
    for var, lags in lag_defs.items():
        for lag in lags:
            df[f"{var}_lag{lag}"] = df[var].shift(lag)

    df["Year"] = pd.to_numeric(df["Year"], errors="coerce")

    # --------------------------------------------------
    # ENSO interaction terms
    # --------------------------------------------------
    climate_vars = ["mean_SH", "mean_temperature", "mean_Rain"]

    for clim in climate_vars:
        for lag_c in lag_defs[clim]:
            for lag_n in lag_defs["Nino_anomaly"]:

                clim_col = f"{clim}_lag{lag_c}"
                nino_col = f"Nino_anomaly_lag{lag_n}"

                inter_col = f"{clim}_lag{lag_c}_x_Nino_anomaly_lag{lag_n}"
                df[inter_col] = df[clim_col] * df[nino_col]

    # --------------------------------------------------
    # Final dataframe with engineered features
    # --------------------------------------------------
    self.df = df.dropna().reset_index(drop=True)

    # --------------------------------------------------
    # Train test split
    # --------------------------------------------------
    self._train_test_split(
        train_year=getattr(self, "train_year", None),
        test_year=getattr(self, "test_year", None)
    )

    print('Initial Train Test Split completed...')
    print('Now performing lag optimisation....')

    # --------------------------------------------------
    # Fit scaler
    # --------------------------------------------------
    if scaler_obj is not None:

        numeric_cols = [
            c for c in self.train_df.select_dtypes(include=[np.number]).columns
            if c != "Year"
        ]

        if len(numeric_cols) > 0:

            scaler_obj.fit(self.train_df[numeric_cols])

            self.train_df.loc[:, numeric_cols] = scaler_obj.transform(
                self.train_df[numeric_cols]
            )

            self.test_df.loc[:, numeric_cols] = scaler_obj.transform(
                self.test_df[numeric_cols]
            )

    # --------------------------------------------------
    # Feature grid
    # --------------------------------------------------
    if debug:
        all_features = [[
            "mean_SH_lag1",
            "mean_temperature_lag2",
            "mean_Rain_lag2",
            "Nino_anomaly_lag12",
            "Year",
            "MA_mean_temperature","MA_mean_Rain","MA_mean_SH",
            "YA_mean_temperature","YA_mean_Rain","YA_mean_SH",
        ]]
    else:

        all_features = [
            [
                f"mean_SH_lag{rh}",
                f"mean_temperature_lag{t}",
                f"mean_Rain_lag{r}",
                f"Nino_anomaly_lag{n}",

                f"mean_SH_lag{rh}_x_Nino_anomaly_lag{n}",
                f"mean_temperature_lag{t}_x_Nino_anomaly_lag{n}",
                f"mean_Rain_lag{r}_x_Nino_anomaly_lag{n}",

                "Year",
                "MA_mean_temperature","MA_mean_Rain","MA_mean_SH",
                "YA_mean_temperature","YA_mean_Rain","YA_mean_SH",
            ]

            for rh in lag_defs["mean_SH"]
            for t in lag_defs["mean_temperature"]
            for r in lag_defs["mean_Rain"]
            for n in lag_defs["Nino_anomaly"]
        ]

    # --------------------------------------------------
    # Config grid
    # --------------------------------------------------
    configs = [
        (feats, base, res, corr)
        for feats in all_features
        for base in base_models if is_model_available(base)
        for res in residual_models if is_model_available(res)
        for corr in correction_models if is_model_available(corr)
    ]

    if len(configs) == 0:
        raise RuntimeError("No valid model configurations available.")

    # --------------------------------------------------
    # Precompute feature matrices
    # --------------------------------------------------
    feature_pool = sorted({f for feats,_,_,_ in configs for f in feats})

    X_train_full = self.train_df[feature_pool].to_numpy()
    X_test_full = self.test_df[feature_pool].to_numpy()

    y_train = self.train_df[self.target_col].to_numpy()
    y_test = self.test_df[self.target_col].to_numpy()

    feature_index = {f: i for i, f in enumerate(feature_pool)}

    # --------------------------------------------------
    # Stage 1: Base screening
    # --------------------------------------------------
    def _evaluate_base_screen(idx, feats, base_model):

        try:

            seed = (
                self.random_state + idx
                if self.random_state is not None else None
            )

            if seed is not None:
                np.random.seed(seed)

            cols = [feature_index[f] for f in feats]

            X_train = X_train_full[:, cols]
            X_test = X_test_full[:, cols]

            base_defaults = DEFAULT_PARAMS.get(base_model, {}).copy()

            if seed is not None and "random_state" in str(MODEL_REGISTRY[base_model]):
                base_defaults["random_state"] = seed

            if "n_jobs" in str(MODEL_REGISTRY[base_model]):
                base_defaults["n_jobs"] = 1

            model = MODEL_REGISTRY[base_model](**base_defaults)

            model.fit(X_train, y_train)

            preds = model.predict(X_test)

            base_rmse = root_mean_squared_error(y_test, preds)

            return {"idx": idx, "base_rmse": base_rmse}

        except Exception:

            return {"idx": idx, "base_rmse": np.inf}

    base_scores = Parallel(n_jobs=n_jobs)(
        delayed(_evaluate_base_screen)(i, feats, base)
        for i,(feats,base,_,_) in enumerate(configs)
    )

    base_df = pd.DataFrame(base_scores)

    # --------------------------------------------------
    # Hierarchical pruning
    # --------------------------------------------------
    if pruning_strategy is None:
        selected_indices = base_df["idx"].tolist()

    elif pruning_strategy == "top_k":

        k = min(top_k, len(base_df))
        selected_indices = base_df.nsmallest(k, "base_rmse")["idx"].tolist()

    elif pruning_strategy == "percentile":

        valid_scores = base_df["base_rmse"].replace(np.inf, np.nan).dropna()

        if len(valid_scores)==0:
            selected_indices = base_df["idx"].tolist()
        else:
            threshold = np.percentile(valid_scores, percentile)

            selected_indices = base_df[
                base_df["base_rmse"] <= threshold
            ]["idx"].tolist()
    else:
        raise ValueError("pruning_strategy must be {'top_k','percentile',None}")

    if len(selected_indices)==0:
        selected_indices = base_df.nsmallest(10, "base_rmse")["idx"].tolist()

    pruned_configs = [configs[i] for i in selected_indices]

    # --------------------------------------------------
    # Stage 2: Full stacked optimization
    # --------------------------------------------------
    results = Parallel(n_jobs=n_jobs)(
        delayed(_evaluate_configuration)(
            feats,
            base,
            res,
            corr,
            self.train_df,
            self.test_df,
            self.target_col,
            n_trials,
            (
                self.random_state + idx
                if self.random_state is not None else None
            ),
        )
        for idx,(feats,base,res,corr) in enumerate(pruned_configs)
    )

    self.lag_search_results = pd.DataFrame(results)

    self.best_config = (
        self.lag_search_results.sort_values("rmse", ascending=True).iloc[0]
    )

    self.scaler = scaler_obj

    self.scaled_columns = [
        c for c in self.train_df.select_dtypes(include=[np.number]).columns
        if c != "Year"
    ]

    if self._pipeline_start_time is None:
        self._pipeline_start_time = time.perf_counter()

    # --------------------------------------------------
    # Store feature metadata
    # --------------------------------------------------
    lag_map = {}
    interaction_map = []

    for feat in self.best_config["features"]:

        if "_lag" in feat and "_x_" not in feat:

            base, lag = feat.split("_lag")
            lag = int(lag)

            lag_map.setdefault(base, set()).add(lag)

        if "_x_" in feat:

            left, right = feat.split("_x_")

            # fix shorthand naming if it occurs
            if right.startswith("nino_"):
                right = right.replace("nino_", "Nino_anomaly_")

            interaction_map.append((left, right))

    self.feature_metadata = {
        "lags": {k: sorted(v) for k, v in lag_map.items()},
        "interactions": interaction_map,
        "scaled_columns": self.scaled_columns
    }

    return self.feature_metadata, self.lag_search_results, self.best_config

train_final_model()

Train the final disease prediction model using the optimized feature set.

This method fits the final model after preprocessing, feature engineering, and lag optimization steps have been completed. It typically uses the best-performing configuration identified during model selection (e.g., stacked learning framework or tuned estimator).

The trained model is stored internally and used for subsequent prediction and projection tasks.

Parameters

None

Returns

DiseaseModel

Returns the instance with the trained model stored internally.

Notes

  • Assumes that data ingestion and preprocessing have already been performed.
  • Requires optimized lagged features to be available.
  • May internally split data into training and validation sets.
  • Supports integration with ensemble or stacked models if configured.
Source code in climaid\climaid_model.py
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
@track_time("training_seconds")
def train_final_model(self):
    """
    Train the final disease prediction model using the optimized feature set.

    This method fits the final model after preprocessing, feature engineering,
    and lag optimization steps have been completed. It typically uses the
    best-performing configuration identified during model selection (e.g.,
    stacked learning framework or tuned estimator).

    The trained model is stored internally and used for subsequent prediction
    and projection tasks.

    Parameters
    ----------

    None

    Returns
    -------

    self : DiseaseModel
        Returns the instance with the trained model stored internally.

    Notes
    -----

    - Assumes that data ingestion and preprocessing have already been performed.
    - Requires optimized lagged features to be available.
    - May internally split data into training and validation sets.
    - Supports integration with ensemble or stacked models if configured.
    """

    if self.best_config is None:
        raise RuntimeError("Run optimize_lags() first.")

    import numpy as np
    import pandas as pd
    from sklearn.metrics import r2_score, root_mean_squared_error

    feats = self.best_config["features"]
    X_train = self.train_df[feats]
    y_train = self.train_df[self.target_col]
    X_test = self.test_df[feats]
    y_test = self.test_df[self.target_col]

    # ======================================================
    # BASE MODEL
    # ======================================================
    base_model_name = self.best_config["base_model"]
    base_params = self.best_config["base_params"].copy()

    if self.random_state is not None and "random_state" in str(MODEL_REGISTRY[base_model_name]):
        base_params["random_state"] = self.random_state

    self.base = MODEL_REGISTRY[base_model_name](**base_params)
    self.base.fit(X_train, y_train)

    y_base_train = self.base.predict(X_train)
    y_base_test = self.base.predict(X_test)

    # ======================================================
    # RESIDUAL STAGE
    # ======================================================
    res_model_name = self.best_config["residual_model"]

    if res_model_name in [None, "none", "base_only"]:
        self.res = None
        y_res_train = np.zeros_like(y_base_train)
        y_res_test = np.zeros_like(y_base_test)
    else:
        res_params = self.best_config["residual_params"].copy()

        if self.random_state is not None and "random_state" in str(MODEL_REGISTRY[res_model_name]):
            res_params["random_state"] = self.random_state

        self.res = MODEL_REGISTRY[res_model_name](**res_params)
        self.res.fit(X_train, y_train - y_base_train)

        y_res_train = self.res.predict(X_train)
        y_res_test = self.res.predict(X_test)

    # ======================================================
    # CORRECTION STAGE
    # ======================================================
    corr_model_name = self.best_config["correction_model"]

    raw_train_preds = y_base_train + y_res_train
    raw_test_preds = y_base_test + y_res_test

    # RMSE baseline (instead of R2)
    baseline_rmse = root_mean_squared_error(y_test, raw_test_preds)

    if corr_model_name in [None, "none", "base_only"]:
        self.corr = None
        final_test = raw_test_preds

    elif corr_model_name == "isotonic":
        from sklearn.isotonic import IsotonicRegression

        iso = IsotonicRegression(out_of_bounds="clip")
        iso.fit(raw_train_preds, y_train)
        iso_test = iso.predict(raw_test_preds)

        iso_rmse = root_mean_squared_error(y_test, iso_test)

        # RMSE-based guard
        if iso_rmse < baseline_rmse:
            self.corr = iso
            final_test = iso_test
        else:
            self.corr = None
            final_test = raw_test_preds

    else:
        corr_params = self.best_config["correction_params"].copy()

        if self.random_state is not None and "random_state" in str(MODEL_REGISTRY[corr_model_name]):
            corr_params["random_state"] = self.random_state

        self.corr = MODEL_REGISTRY[corr_model_name](**corr_params)

        X_corr_train = np.asarray(raw_train_preds).reshape(-1, 1)
        X_corr_test = np.asarray(raw_test_preds).reshape(-1, 1)

        self.corr.fit(X_corr_train, y_train)
        corr_test = self.corr.predict(X_corr_test)

        corr_rmse = root_mean_squared_error(y_test, corr_test)

        # RMSE-based safety check
        if corr_rmse < baseline_rmse:
            final_test = corr_test
        else:
            self.corr = None
            final_test = raw_test_preds

    # ======================================================
    # METRICS (FINAL)
    # ======================================================
    self.rmse = root_mean_squared_error(y_test, final_test)
    self.r2 = r2_score(y_test, final_test)

    # ======================================================
    # Prediction DataFrame
    # ======================================================
    self.dpred = pd.DataFrame({
        "time": self.test_df["time"],
        "Actual": y_test,
        "Predicted": final_test,
    }).reset_index(drop=True)

    # ======================================================
    # Uncertainty (Gaussian approx)
    # ======================================================
    self.dpred["Predicted_lower"] = (
        self.dpred["Predicted"] - 1.96 * self.rmse
    ).clip(lower=0)

    self.dpred["Predicted_upper"] = (
        self.dpred["Predicted"] + 1.96 * self.rmse
    )

    # ======================================================
    # Return
    # ======================================================
    return {
        "test_r2": self.r2,
        "test_rmse": self.rmse,
        "predictions": self.dpred,
        "model_info": {
            "base_model": self.best_config["base_model"],
            "residual_model": self.best_config["residual_model"],
            "correction_model": self.best_config["correction_model"],
            "features": self.best_config["features"],
        },
        "data_summary": {
            "train_size": len(self.train_df),
            "test_size": len(self.test_df),
            "train_period": str(self.train_df["time"].min()),
            "test_period": str(self.test_df["time"].max()),
        }
    }

_train_test_split(train_year=None, test_year=None, drop_2020=True)

Perform a time-aware train/test split for disease modeling.

This method splits the merged dataset into training and testing subsets based on temporal ordering, ensuring no data leakage from future observations.

Parameters

int or None, optional

Last year to include in the training dataset. If None, the split is determined automatically.

int or None, optional

First year to include in the test dataset. If None, the split is determined automatically.

bool, default=True

Whether to exclude data from the year 2020. This is useful to remove anomalies due to COVID-19 disruptions in disease reporting and transmission patterns.

Returns

None

Updates internal attributes:

  • self.train_df : pandas.DataFrame Training dataset

  • self.test_df : pandas.DataFrame Testing dataset

Notes

  • The split preserves temporal ordering (no random shuffling).
  • Designed for time-series epidemiological data.
  • Avoids leakage by ensuring test data strictly follows training data.
  • If both train_year and test_year are provided, they must be consistent.

Warning

This is an internal method and is not intended for direct use.

Source code in climaid\climaid_model.py
 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
def _train_test_split(
    self,
    train_year: int | None = None,
    test_year: int | None = None,
    drop_2020: bool = True,
):
    """
    Perform a time-aware train/test split for disease modeling.

    This method splits the merged dataset into training and testing subsets
    based on temporal ordering, ensuring no data leakage from future observations.

    Parameters
    ----------

    train_year : int or None, optional
        Last year to include in the training dataset.
        If None, the split is determined automatically.

    test_year : int or None, optional
        First year to include in the test dataset.
        If None, the split is determined automatically.

    drop_2020 : bool, default=True
        Whether to exclude data from the year 2020.
        This is useful to remove anomalies due to COVID-19 disruptions
        in disease reporting and transmission patterns.

    Returns
    -------

    None:
        Updates internal attributes:

        - self.train_df : pandas.DataFrame
            Training dataset

        - self.test_df : pandas.DataFrame
            Testing dataset

    Notes
    -----

    - The split preserves temporal ordering (no random shuffling).
    - Designed for time-series epidemiological data.
    - Avoids leakage by ensuring test data strictly follows training data.
    - If both `train_year` and `test_year` are provided, they must be consistent.

    Warning
    -------

    This is an internal method and is not intended for direct use.

    """

    if hasattr(self, "df") and self.df is not None:
        df = self.df.copy()
    else:
        df = self.df_merged.copy()

    if drop_2020:
        df = df[df["Year"] != 2020]

    # -------------------------------
    # SPLIT LOGIC
    # -------------------------------
    if train_year is None and test_year is None:
        train_df = df[df["Year"] < 2020].copy()
        test_df  = df[df["Year"] > 2020].copy()

    elif train_year is None:
        test_year = int(test_year)
        train_df = df[df["Year"] < test_year].copy()
        test_df  = df[df["Year"] >= test_year].copy()

    elif test_year is None:
        train_year = int(train_year)
        train_df = df[df["Year"] <= train_year].copy()
        test_df  = df[df["Year"] > train_year].copy()

    else:
        train_year = int(train_year)
        test_year  = int(test_year)

        if train_year >= test_year:
            raise ValueError("train_year must be earlier than test_year")

        train_df = df[df["Year"] <= train_year].copy()
        test_df  = df[df["Year"] >= test_year].copy()

    # Dropping NaN values from both train_df and test_df
    train_df = train_df.dropna(subset = ['Count']).reset_index(drop = True)

    test_df = test_df.dropna(subset = ['Count']).reset_index(drop = True)

    # -------------------------------
    # STORE INTERNALLY (KEY FIX)
    # -------------------------------
    self.train_df = train_df
    self.test_df  = test_df
    self.train_year = train_year
    self.test_year = test_year

    # # Optional debug
    # print("Split applied:")
    # print("Train years:", sorted(train_df["Year"].unique()))
    # print("Test years :", sorted(test_df["Year"].unique()))

    return train_df, test_df

build_report_artifacts(projection_summary, tidy_df)

Construct standardized reporting artifacts from model outputs.

This method generates a structured collection of artifacts required for downstream reporting and visualization. It transforms model outputs, including projections and evaluation summaries, into a consistent format that can be used by the reporting layer (e.g., dashboards, plots, or exports).

Users are not expected to manually assemble these artifacts. This function ensures that all required components are created automatically and in a reproducible format.

Parameters

dict or pandas.DataFrame

Summary of model projections, typically produced by the projection pipeline (e.g., DiseaseProjection). This may include predicted values, confidence intervals, and temporal aggregation.

Returns

ReportArtifacts
  • A structured object containing all elements required for reporting, such as:
    • Processed projection data
    • Evaluation metrics (if available)
    • Metadata (e.g., model configuration, time range)
    • Visualization-ready datasets

Notes

  • This method is part of the reporting pipeline and is typically called after model training and projection steps.
  • Ensures consistency between modeling outputs and reporting interfaces.
  • Designed to support reproducible research workflows.
Source code in climaid\climaid_model.py
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
@track_time("report_generation_seconds")
def build_report_artifacts(self, projection_summary, tidy_df):
    """
    Construct standardized reporting artifacts from model outputs.

    This method generates a structured collection of artifacts required for
    downstream reporting and visualization. It transforms model outputs,
    including projections and evaluation summaries, into a consistent format
    that can be used by the reporting layer (e.g., dashboards, plots, or exports).

    Users are not expected to manually assemble these artifacts. This function
    ensures that all required components are created automatically and in a
    reproducible format.

    Parameters
    ----------

    projection_summary : dict or pandas.DataFrame
        Summary of model projections, typically produced by the projection
        pipeline (e.g., `DiseaseProjection`). This may include predicted values,
        confidence intervals, and temporal aggregation.

    Returns
    -------

    artifacts : ReportArtifacts
        - A structured object containing all elements required for reporting,
        such as:
            - Processed projection data
            - Evaluation metrics (if available)
            - Metadata (e.g., model configuration, time range)
            - Visualization-ready datasets

    Notes
    -----

    - This method is part of the reporting pipeline and is typically called
    after model training and projection steps.
    - Ensures consistency between modeling outputs and reporting interfaces.
    - Designed to support reproducible research workflows.
    """

    from climaid.reporting import ReportArtifacts

    if self.best_config is None:
        raise RuntimeError("Run optimize_lags() first.")

    if not hasattr(self, "r2"):
        raise RuntimeError("Run train_final_model() first.")

    # ---------------------------
    # Metrics
    # ---------------------------
    metrics = {
        "test_r2": np.round(self.r2,2), 
        "test_rmse": np.round(self.rmse, 2),
    }

    # ---------------------------
    # Selected lags (auto-extract)
    # ---------------------------
    selected_lags = {}
    interaction_lags = []

    for f in self.best_config['features']:

        # --------------------------
        # Interaction terms
        # --------------------------
        if "_x_" in f:

            left, right = f.split("_x_")

            if "_lag" in left and "_lag" in right:

                var1, lag1 = left.split("_lag")
                var2, lag2 = right.split("_lag")

                interaction_lags.append({
                    "var1": var1,
                    "lag1": int(lag1),
                    "var2": var2,
                    "lag2": int(lag2)
                })

        # --------------------------
        # Single lag terms
        # --------------------------
        elif "_lag" in f:

            var, lag = f.split("_lag")

            selected_lags[var] = int(lag)

    # ---------------------------
    # Feature importance 
    # ---------------------------
    importance = {}
    if hasattr(self.base, "feature_importances_"):
        importance = dict(
            zip(self.best_config["features"], self.base.feature_importances_)
        )

    # ---------------------------
    # Model metadata
    # ---------------------------
    model_info = {
        "base_model": self.best_config["base_model"],
        "residual_model": self.best_config["residual_model"],
        "correction_model": self.best_config["correction_model"],
        "stacking_pipeline": "Base → Residual → Correction",
        "n_features": len(self.best_config["features"]),
    }

    # ---------------------------
    # Data summary
    # ---------------------------
    # Ensure datetime safety (prevents malformed tokens in reports)
    train_time = pd.to_datetime(self.train_df["time"], errors="coerce")
    test_time = pd.to_datetime(self.test_df["time"], errors="coerce")

    # Drop invalid timestamps if any
    train_time = train_time.dropna()
    test_time = test_time.dropna()

    # Format clean ISO dates for reporting (LLM-friendly)
    if not train_time.empty:
        train_start = train_time.min().strftime("%d-%m-%Y")
        train_end = train_time.max().strftime("%d-%m-%Y")
    else:
        train_start, train_end = "Unknown", "Unknown"

    if not test_time.empty:
        test_start = test_time.min().strftime("%d-%m-%Y")
        test_end = test_time.max().strftime("%d-%m-%Y")
    else:
        test_start, test_end = "Unknown", "Unknown"

    data_summary = {
        "train_size": int(len(self.train_df)),
        "test_size": int(len(self.test_df)),
        "train_period": f"{train_start} to {train_end}",
        "test_period": f"{test_start} to {test_end}",
    }

    # ---------------------------
    # Create artifacts (AUTO)
    # ---------------------------
    artifacts = ReportArtifacts(
        district=self.district,
        disease_name=self.disease_name,
        date_range=f"{train_start} to {test_end}",
        metrics=metrics,
        selected_lags=selected_lags,
        interaction_lags = interaction_lags,
        features=self.best_config["features"],
        importance=importance,
        projection_summary=projection_summary,
        runtime=self.runtime,
        model_info=model_info,
        data_summary=data_summary,
        download_data=tidy_df,
    )

    return artifacts

predict(X)

Generate disease case predictions using the trained stacked model.

This method applies the full prediction pipeline consisting of:

  1. Base model – Generates initial predictions from input features.
  2. Residual model – Learns and predicts residual errors from the base model.
  3. Correction step – Combines base predictions and residual corrections to produce final refined predictions.

The method assumes that the model has already been trained using train_final_model.

Parameters

pandas.DataFrame :

Input feature matrix containing the same variables used during training, including climate variables and any engineered lagged features.

Returns

numpy.ndarray

Array of predicted disease case counts (or risk scores), aligned with the input samples in X.

Notes

  • Input features must match the training schema (column names and preprocessing).
  • If lagged features were used during training, they must be present in X.
  • This method uses the internally stored trained models and does not retrain.
Source code in climaid\climaid_model.py
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
@track_time("prediction_seconds")
def predict(self, X: pd.DataFrame) -> np.ndarray:
    """
    Generate disease case predictions using the trained stacked model.

    This method applies the full prediction pipeline consisting of:

    1. **Base model** – Generates initial predictions from input features.
    2. **Residual model** – Learns and predicts residual errors from the base model.
    3. **Correction step** – Combines base predictions and residual corrections
    to produce final refined predictions.

    The method assumes that the model has already been trained using
    `train_final_model`.

    Parameters
    ----------

    X : pandas.DataFrame :
        Input feature matrix containing the same variables used during training,
        including climate variables and any engineered lagged features.

    Returns
    -------

    predictions : numpy.ndarray
        Array of predicted disease case counts (or risk scores), aligned with
        the input samples in `X`.

    Notes
    -----

    - Input features must match the training schema (column names and preprocessing).
    - If lagged features were used during training, they must be present in `X`.
    - This method uses the internally stored trained models and does not retrain.
    """

    # -----------------------
    # Safety checks
    # -----------------------
    if not hasattr(self, "base"):
        raise RuntimeError(
            "diseaseModel has not been trained. "
            "Call train_final_model() before predict()."
        )

    # Ensure correct feature order
    X = X[self.best_config["features"]]

    # -----------------------
    # Base model prediction
    # -----------------------
    base_pred = self.base.predict(X)

    # -----------------------
    # Residual correction
    # -----------------------
    if hasattr(self, "res") and self.res is not None:
        resid_pred = self.res.predict(X)
        combined_pred = base_pred + resid_pred
    else:
        combined_pred = base_pred

    # -----------------------
    # Calibration correction
    # -----------------------
    if hasattr(self, "cor") and self.corr is not None:
        final_pred = self.corr.predict(combined_pred)
        # upperbound = final_pred + 1.96 * self.rmse
        # lowerbound = final_pred - 1.96 * self.rmse
    else:
        final_pred = combined_pred
        # upperbound = final_pred + 1.96 * self.rmse
        # lowerbound = final_pred - 1.96 * self.rmse

    return final_pred     

generate_report(projection_summary=None, llm_client=None, tidy_df=None, style='detailed', open_browser=False, save_copy=False, output_dir=None)

Generate a comprehensive disease projection report.

This method provides a fully automated reporting pipeline that transforms model outputs into a structured, human-readable report. It integrates projections, optional language model summarization, and formatted outputs for visualization or sharing.

Users are not required to manually construct intermediate artifacts—this method handles all necessary steps internally, including artifact creation, formatting, and optional rendering.

Parameters

dict or pandas.DataFrame, optional

Precomputed projection results. If not provided, the method may internally generate projections using the trained model.

object, optional

Language model client used to generate narrative summaries or insights. If provided, the report may include AI-generated interpretations of trends.

pandas.DataFrame

DataFrame for exporting to an excel file.

{"detailed", "summary", "policy"}, default="detailed"
  • Level of detail in the generated report:
    • "detailed": Includes full analysis, metrics, and explanations.
    • "summary": Provides a concise overview of key findings.
    • "policy": Provides a comprehensive policy report.
bool, default=False

If True, automatically opens the generated report in a web browser.

bool, default=False

If True, saves a copy of the report to disk.

str, optional

Directory where the report will be saved if save_copy=True. If not specified, a default output location is used.

Returns

str or pathlib.Path

Path to the generated report file or rendered output, depending on configuration.

Notes

  • This method combines multiple pipeline stages:
    1. Projection (if not provided)
    2. Artifact construction
    3. Report formatting and rendering
  • Designed for end-to-end usability with minimal user intervention.
  • Supports integration with LLMs for enhanced interpretability.
Source code in climaid\climaid_model.py
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
@track_time("report_generation_seconds")
def generate_report(self, 
                    projection_summary = None, 
                    llm_client=None, 
                    tidy_df=None, 
                    style="detailed", 
                    open_browser = False, 
                    save_copy=False, 
                    output_dir = None):
    """
    Generate a comprehensive disease projection report.

    This method provides a fully automated reporting pipeline that transforms
    model outputs into a structured, human-readable report. It integrates
    projections, optional language model summarization, and formatted outputs
    for visualization or sharing.

    Users are not required to manually construct intermediate artifacts—this
    method handles all necessary steps internally, including artifact creation,
    formatting, and optional rendering.

    Parameters
    ----------

    projection_summary : dict or pandas.DataFrame, optional
        Precomputed projection results. If not provided, the method may internally
        generate projections using the trained model.

    llm_client : object, optional
        Language model client used to generate narrative summaries or insights.
        If provided, the report may include AI-generated interpretations of trends.

    tidy_df : pandas.DataFrame
        DataFrame for exporting to an excel file. 

    style : {"detailed", "summary", "policy"}, default="detailed"
        - Level of detail in the generated report:
            - "detailed": Includes full analysis, metrics, and explanations.
            - "summary": Provides a concise overview of key findings.
            - "policy": Provides a comprehensive policy report. 

    open_browser : bool, default=False
        If True, automatically opens the generated report in a web browser.

    save_copy : bool, default=False
        If True, saves a copy of the report to disk.

    output_dir : str, optional
        Directory where the report will be saved if `save_copy=True`.
        If not specified, a default output location is used.

    Returns
    -------

    report : str or pathlib.Path
        Path to the generated report file or rendered output, depending on configuration.

    Notes
    -----

    - This method combines multiple pipeline stages:
        1. Projection (if not provided)
        2. Artifact construction
        3. Report formatting and rendering
    - Designed for end-to-end usability with minimal user intervention.
    - Supports integration with LLMs for enhanced interpretability.
    """

    import time
    from climaid.reporting import DiseaseReporter, open_report_in_browser

    if projection_summary is None:
        projection_summary = {
            "mode": "historical_only",
            "note": "No climate projection summary provided. Report based on historical model performance only."
        }

    # Auto-build artifacts internally
    artifacts = self.build_report_artifacts(projection_summary, tidy_df)

    # Create reporter
    reporter = DiseaseReporter(llm_client=llm_client)
    report = reporter.generate(artifacts, style=style)

    if self._pipeline_start_time is not None:
        self.runtime["total_pipeline_seconds"] = (
            time.perf_counter() - self._pipeline_start_time
        )

    if open_browser:

        from climaid.reporting import open_report_in_browser

        title = f"ClimAID {self.disease_name} Risk Intelligence Report"

        report_path = open_report_in_browser(
            report_text=report,
            artifacts=artifacts,
            title=title,
            save_copy=save_copy,
            output_dir="climaid_outputs/reports"
        )

    # Generate report
    return report

plot_historical_predictions(actual_color='#006d77', prediction_color='#e29578', hatch_color='#ffddd2', figsize=(12, 5), hatch='.', save=False, alpha=0.4, path='path', theme='ticks', dpi=500)

Plot observed vs predicted disease cases over time.

This method visualizes the model's performance by comparing historical observed values with predicted values. It helps assess model fit, identify temporal patterns, and detect systematic deviations.

The plot may optionally include shaded or hatched regions to highlight differences between observed and predicted values.

Parameters

str, default='#006d77'

Color used for plotting observed (true) disease cases.

str, default='#e29578'

Color used for plotting model predictions.

str, default='#ffddd2'

Color used for shaded or hatched regions representing prediction error or uncertainty.

tuple of int, default=(12, 5)

Size of the figure in inches (width, height).

str, default='.'

Matplotlib hatch pattern used to highlight differences between observed and predicted values.

bool, default=False

If True, saves the plot to disk.

float, default=0.4

Transparency level for shaded or hatched regions.

str, default='path'

File path where the plot will be saved if save=True.

str, default='ticks'

Seaborn or matplotlib style theme applied to the plot.

int, default=500

Resolution of the saved figure in dots per inch.

Returns

  • None
  • Displays the plot and optionally saves it to disk.

Notes

  • Requires the model to be trained and predictions to be available.
  • Assumes temporal alignment between observed and predicted values.
  • Useful for diagnostic evaluation of model performance.
Source code in climaid\climaid_model.py
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
def plot_historical_predictions(
        self,
        actual_color='#006d77',
        prediction_color='#e29578',
        hatch_color = '#ffddd2',
        figsize=(12, 5),
        hatch = '.',
        save=False,
        alpha = 0.4, 
        path='path',
        theme='ticks',
        dpi=500,
    ):
        """
        Plot observed vs predicted disease cases over time.

        This method visualizes the model's performance by comparing historical
        observed values with predicted values. It helps assess model fit,
        identify temporal patterns, and detect systematic deviations.

        The plot may optionally include shaded or hatched regions to highlight
        differences between observed and predicted values.

        Parameters
        ----------

        actual_color : str, default='#006d77'
            Color used for plotting observed (true) disease cases.

        prediction_color : str, default='#e29578'
            Color used for plotting model predictions.

        hatch_color : str, default='#ffddd2'
            Color used for shaded or hatched regions representing prediction error
            or uncertainty.

        figsize : tuple of int, default=(12, 5)
            Size of the figure in inches (width, height).

        hatch : str, default='.'
            Matplotlib hatch pattern used to highlight differences between
            observed and predicted values.

        save : bool, default=False
            If True, saves the plot to disk.

        alpha : float, default=0.4
            Transparency level for shaded or hatched regions.

        path : str, default='path'
            File path where the plot will be saved if `save=True`.

        theme : str, default='ticks'
            Seaborn or matplotlib style theme applied to the plot.

        dpi : int, default=500
            Resolution of the saved figure in dots per inch.

        Returns
        -------

        - None
        - Displays the plot and optionally saves it to disk.

        Notes
        -----

        - Requires the model to be trained and predictions to be available.
        - Assumes temporal alignment between observed and predicted values.
        - Useful for diagnostic evaluation of model performance.
        """

        import matplotlib.pyplot as plt
        import seaborn as sns
        import matplotlib.dates as mdates

        if "Actual" not in self.dpred.columns:
            raise KeyError("Dataframe must contain 'Actual' column.")

        sns.set_theme(style=theme, font_scale=1.2)
        fig, ax = plt.subplots(figsize=figsize)
        ax.set_facecolor('#f8f9fa')

        ax.plot(self.dpred['time'], self.dpred['Actual'], label='Actual cases',
                marker='8', color=actual_color, linewidth=3)

        ax.plot(self.dpred['time'], self.dpred['Predicted'], label='Predicted cases',
                marker='o', color=prediction_color, linewidth=3)

        ax.fill_between(
            self.dpred['time'],
            self.dpred['Predicted_lower'],
            self.dpred['Predicted_upper'],
            hatch=hatch,
            linewidth=0.0,
            color=hatch_color,
            alpha=alpha
        )

        ax.legend(loc='upper left', ncol=2, frameon=False)
        ax.set_ylabel('Cases', weight='demi')
        ax.grid(alpha=0.2, linestyle='--', color='#6c757d')

        ax.set_xlabel("Month-Year", weight='demi')
        ax.xaxis.set_major_locator(mdates.MonthLocator(interval=1))
        ax.xaxis.set_major_formatter(mdates.DateFormatter('%b %y'))
        plt.xticks(fontsize=10)

        plt.text(
            0.715, 0.9675,
            f"$R^2$ = {self.r2:.3f}; RMSE = {self.rmse:.2f}",
            transform=plt.gca().transAxes,
            fontsize=14,
            verticalalignment='top'
        )

        fig.autofmt_xdate(rotation=90)
        plt.tight_layout()

        if save:
            plt.savefig(path, dpi=dpi)

        plt.show()

print_runtime_summary()

Prints a clean runtime summary for the full pipeline.

Source code in climaid\climaid_model.py
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
def print_runtime_summary(self):
    """
    Prints a clean runtime summary for the full pipeline.
    """
    if not hasattr(self, "runtime") or not self.runtime:
        print("Runtime information not available.")
        return

    print("\n==============================")
    print("⏱ ClimAID Pipeline Runtime Summary")
    print("==============================")

    total = 0.0
    for k, v in self.runtime.items():
        if isinstance(v, (int, float)):
            print(f"{k.replace('_', ' ').title()}: {v:.2f} seconds")
            total += v

    if total > 0:
        print("------------------------------")
        print(f"Total Computation Time: {total:.2f} seconds")
        print("==============================\n")