XGBoost Forecaster API
XGBoost Substation Forecaster
This package implements an XGBoost-based model to forecast power flows at NGED primary substations using numerical weather prediction (NWP) forecasts. It implements the BaseForecaster protocol defined in ml_core.
Features
- Unified ML Interface: Implements the
BaseForecasterprotocol, allowing seamless integration with the Dagster orchestration pipeline. - Multi-NWP Support: Ingests forecasts from multiple NWP providers simultaneously. Secondary NWP features are prefixed with their model name (e.g.,
gfs_temperature_2m), and all NWPs are joined using a 3-hour availability delay. - Dynamic Seasonal Lags: Prevents lookahead bias by calculating autoregressive lags dynamically based on the forecast lead time. The model always uses the most recent available historical data for a given lead time (e.g.,
lag_days = max(1, ceil(lead_time_days / 7)) * 7). - Rigorous Backtesting: Supports simulating real-time inference via the
collapse_lead_timesparameter. When enabled, it filters NWP data to keep only the latest available forecast for each valid time, enforcing the 3-hour availability delay. - H3-based Weather Matching: Automatically matches substation coordinates to H3 resolution 5 cells used in the weather data.
- Ensemble Averaging: Averages weather variables across ensemble members for robust feature engineering.
- Temporal Features: Includes cyclical temporal features (sine/cosine for hour and day of year) and day of week.
- Long-Range Horizon Handling: Supports 14-day (336h) forecasts at 30-minute resolution. The
lead_time_hoursis passed as a feature to the XGBoost model, allowing it to learn the decay in NWP skill over time. - Physical Wind Logic: Wind speed and direction are interpolated using Cartesian
uandvcomponents instead of circular interpolation. This avoids "phantom high wind" artifacts during rapid direction shifts and ensures physical correctness.
Installation
This package is part of the uv workspace. Install all dependencies from the root:
uv sync
Usage
This package is intended to be used as part of the Dagster pipeline. The XGBoostForecaster class handles the full lifecycle of the model, including training and inference.
xgboost_forecaster.config
Classes
XGBoostHyperparameters
Bases: BaseModel
Hyperparameters for the XGBoost model.
Source code in packages/xgboost_forecaster/src/xgboost_forecaster/config.py
4 5 6 7 8 9 10 | |
xgboost_forecaster.data
Data loading and preprocessing for XGBoost forecasting.
Classes
DataConfig
dataclass
Configuration for data loading and preprocessing.
Source code in packages/xgboost_forecaster/src/xgboost_forecaster/data.py
27 28 29 30 31 32 33 34 35 36 | |
Functions
construct_historical_weather(start_date, end_date, h3_indices, config=None)
Construct a continuous historical weather timeseries by stitching NWP runs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
start_date
|
date
|
Start date for the timeseries. |
required |
end_date
|
date
|
End date for the timeseries. |
required |
h3_indices
|
list[int]
|
List of H3 indices to filter for. |
required |
config
|
DataConfig | None
|
Data configuration. |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame[Nwp]
|
A Patito DataFrame containing the stitched NWP data. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If no NWP files are found in the date range. |
Source code in packages/xgboost_forecaster/src/xgboost_forecaster/data.py
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 | |
get_substation_metadata(config=None)
Load substation metadata and filter for those with available power data.
Source code in packages/xgboost_forecaster/src/xgboost_forecaster/data.py
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | |
load_nwp_run(init_time, h3_indices, config=None)
Load a single NWP forecast run.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
init_time
|
datetime
|
The initialization time of the NWP run. |
required |
h3_indices
|
list[int]
|
List of H3 indices to filter for. |
required |
config
|
DataConfig | None
|
Data configuration. |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame[Nwp]
|
A Patito DataFrame containing the NWP data. |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If the NWP file for the given init_time does not exist. |
Source code in packages/xgboost_forecaster/src/xgboost_forecaster/data.py
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 | |
process_nwp_data(nwp, h3_indices)
Process NWP data: lead-time filtering and 30m interpolation for all members.
Note: Accumulated variables (e.g., precipitation, radiation) are already de-accumulated by Dynamical.org prior to download, and should not be differenced.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
nwp
|
LazyFrame
|
Raw NWP data. |
required |
h3_indices
|
list[int]
|
List of H3 indices to filter for. |
required |
Returns:
| Type | Description |
|---|---|
LazyFrame
|
Processed NWP data. |
Source code in packages/xgboost_forecaster/src/xgboost_forecaster/data.py
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 | |
xgboost_forecaster.features
Feature engineering for XGBoost forecasting.
Classes
Functions
add_autoregressive_lags(df, flows_30m, telemetry_delay_hours=24)
Add autoregressive lags to the feature matrix.
This function calculates the required lag dynamically to strictly prevent lookahead bias, ensuring that the model only uses power flow data that would have been available at the time the forecast was made.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
LazyFrame
|
The input LazyFrame (schema: SubstationFeatures). |
required |
flows_30m
|
LazyFrame
|
Historical power flows downsampled to 30m. |
required |
telemetry_delay_hours
|
int
|
Delay in hours for telemetry availability. |
24
|
Returns:
| Type | Description |
|---|---|
LazyFrame
|
LazyFrame with added lag features (schema: SubstationFeatures). |
Source code in packages/xgboost_forecaster/src/xgboost_forecaster/features.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | |
add_time_features(df)
Add lead_time_hours and nwp_init_hour features.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
LazyFrame
|
The input LazyFrame (schema: SubstationFeatures). |
required |
Returns:
| Type | Description |
|---|---|
LazyFrame
|
LazyFrame with added time features (schema: SubstationFeatures). |
Source code in packages/xgboost_forecaster/src/xgboost_forecaster/features.py
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 | |
add_weather_features(weather, history=None)
Add lags and trends to weather data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
weather
|
LazyFrame
|
Current weather forecast (schema: ProcessedNwp). |
required |
history
|
LazyFrame | None
|
Historical weather data (optional, used for lags). |
None
|
Returns:
| Type | Description |
|---|---|
LazyFrame
|
LazyFrame with added weather features (schema: ProcessedNwp). |
Source code in packages/xgboost_forecaster/src/xgboost_forecaster/features.py
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 | |
xgboost_forecaster.model
XGBoost implementation of the Forecaster interface.
Classes
XGBoostForecaster
Bases: BaseForecaster
XGBoost implementation of the Forecaster interface.
This class handles the full lifecycle of an XGBoost model: training and production inference.
Source code in packages/xgboost_forecaster/src/xgboost_forecaster/model.py
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 | |
Functions
__init__(model=None)
Initialize the forecaster.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
XGBRegressor | None
|
An optional pre-trained XGBoost model. |
None
|
Source code in packages/xgboost_forecaster/src/xgboost_forecaster/model.py
47 48 49 50 51 52 53 54 55 | |
log_model(model_name)
Log the model to MLflow.
Source code in packages/xgboost_forecaster/src/xgboost_forecaster/model.py
73 74 75 76 77 78 79 80 81 82 83 84 | |
predict(substation_metadata, inference_params, flows_30m, nwps=None, collapse_lead_times=False)
Execute the inference logic.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
substation_metadata
|
DataFrame[SubstationMetadata]
|
The substation metadata containing h3 mapping. |
required |
inference_params
|
InferenceParams
|
Parameters for inference. |
required |
flows_30m
|
LazyFrame
|
Historical power flow data downsampled to 30m (for lags). |
required |
nwps
|
Mapping[NwpModel, LazyFrame] | None
|
A dictionary of weather forecast lazyframes. |
None
|
collapse_lead_times
|
bool
|
Whether to collapse lead times to simulate real-time inference by keeping only the latest available NWP forecast for each valid time. |
False
|
Returns:
| Type | Description |
|---|---|
DataFrame[PowerForecast]
|
A Patito DataFrame containing the predictions. |
Source code in packages/xgboost_forecaster/src/xgboost_forecaster/model.py
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 | |
train(config, flows_30m, substation_metadata, nwps=None)
Train the XGBoost model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
ModelConfig
|
The model configuration object. |
required |
flows_30m
|
LazyFrame
|
Historical power flow data downsampled to 30m. |
required |
substation_metadata
|
DataFrame[SubstationMetadata]
|
The substation metadata containing h3 mapping. |
required |
nwps
|
Mapping[NwpModel, LazyFrame] | None
|
A dictionary of weather forecast dataframes. |
None
|
Returns:
| Type | Description |
|---|---|
XGBoostForecaster
|
The trained XGBoostForecaster instance. |
Source code in packages/xgboost_forecaster/src/xgboost_forecaster/model.py
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 | |
Functions
xgboost_forecaster.scaling
Classes
xgboost_forecaster.types
Classes
EnsembleSelection
Bases: str, Enum
Selection method for weather ensemble members.
Source code in packages/xgboost_forecaster/src/xgboost_forecaster/types.py
4 5 6 7 8 9 | |