ML Core API
ml_core
Unified ML model interface and shared utilities for substation forecasting.
Purpose
This package provides the abstract base classes and shared logic required to implement the "Tiny Wrapper" ML pipeline. It prioritizes readability, explicit dependencies, and native framework integrations.
Why this package exists
To enable rapid experimentation with different model architectures (XGBoost, GNNs, etc.), we need a common interface that shields the model developer from the underlying data engineering.
Key Components
model.py: Base classes for forecasters (BaseForecaster). TheBaseForecasterprotocol defines a standard interface for all forecasting models, requiring them to implementtrain()andpredict()methods. This allows Dagster to orchestrate any model uniformly.utils.py: Shared MLOps utilities for training (train_and_log_model) and evaluation (evaluate_and_save_model).features.py: Shared feature engineering logic (e.g., cyclical time features).data.py: Shared data splitting and loading logic.scaling.py: Shared normalization and scaling utilities.
Advanced Forecasting Features
The BaseForecaster protocol supports several advanced forecasting features:
- Multi-NWP Support: Models can ingest forecasts from multiple Numerical Weather Prediction (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.
ml_core.model
Base classes for ML model inference.
Classes
BaseForecaster
Bases: ABC
Abstract base class for all ML model forecasters.
A Forecaster handles the full lifecycle of an ML model: training and
production inference. It handles eager DataFrames and is designed to
be used within Dagster assets or standalone scripts.
Subclasses should override the train and predict methods with explicit,
strictly-typed keyword arguments for the specific data they require.
Source code in packages/ml_core/src/ml_core/model.py
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | |
Functions
log_model(model_name)
abstractmethod
Log the model to MLflow.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_name
|
str
|
The name to register the model under. |
required |
Source code in packages/ml_core/src/ml_core/model.py
75 76 77 78 79 80 81 82 | |
predict(substation_metadata, inference_params, flows_30m, nwps=None, collapse_lead_times=False)
abstractmethod
Generate power forecasts.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
substation_metadata
|
DataFrame[SubstationMetadata]
|
The substation metadata. |
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 dataframes. |
None
|
collapse_lead_times
|
bool
|
Whether to collapse lead times (used in backtesting). |
False
|
Returns:
| Type | Description |
|---|---|
DataFrame[PowerForecast]
|
A Patito DataFrame containing the model's predictions. |
Source code in packages/ml_core/src/ml_core/model.py
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | |
train(config, flows_30m, substation_metadata, nwps=None)
abstractmethod
Train the model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
ModelConfig
|
Model configuration object. |
required |
flows_30m
|
LazyFrame
|
Historical power flow data downsampled to 30m. |
required |
substation_metadata
|
DataFrame[SubstationMetadata]
|
The substation metadata. |
required |
nwps
|
Mapping[NwpModel, LazyFrame] | None
|
A dictionary of weather forecast dataframes. |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
The trained native model object (e.g., XGBRegressor). |
Source code in packages/ml_core/src/ml_core/model.py
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | |
ml_core.utils
Classes
Functions
evaluate_and_save_model(context, model_name, forecaster, config, **kwargs)
Universal utility to handle temporal slicing, inference, and storage.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context
|
Union[AssetExecutionContext, OpExecutionContext]
|
Dagster execution context (Asset or Op). |
required |
model_name
|
str
|
Name of the model. |
required |
forecaster
|
An object with a |
required | |
config
|
TrainingConfig
|
Training configuration object. |
required |
**kwargs
|
Input LazyFrames to be temporally sliced and collected. |
{}
|
Returns:
| Type | Description |
|---|---|
|
A Polars DataFrame containing the predictions. |
Source code in packages/ml_core/src/ml_core/utils.py
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 | |
train_and_log_model(context, model_name, trainer, config, **kwargs)
Universal utility to handle temporal slicing and MLflow logging for training.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context
|
Union[AssetExecutionContext, OpExecutionContext]
|
Dagster execution context (Asset or Op). |
required |
model_name
|
str
|
Name of the model (for MLflow run name). |
required |
trainer
|
An object with a |
required | |
config
|
TrainingConfig
|
Training configuration object. |
required |
**kwargs
|
Input LazyFrames to be temporally sliced. |
{}
|
Returns:
| Type | Description |
|---|---|
|
The trained model object. |
Source code in packages/ml_core/src/ml_core/utils.py
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 | |
ml_core.data
Shared data processing logic for ML models.
Classes
Functions
calculate_target_map(flows)
Calculate the target map (power_col and peak_capacity) for each substation.
This function analyzes historical power flows to determine whether MW or MVA is the more reliable target variable (based on data availability) and calculates the peak capacity for normalization.
POTENTIAL DATA LEAKAGE: The 90-day dead sensor rule uses the entire history, introducing temporal leakage, but the user has consciously accepted this to simplify the global decision.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
flows
|
LazyFrame[SubstationPowerFlows] | DataFrame[SubstationPowerFlows]
|
Historical power flow data (LazyFrame or DataFrame). |
required |
Returns:
| Type | Description |
|---|---|
DataFrame[SubstationTargetMap]
|
A Patito DataFrame containing the target map for each substation. |
Source code in packages/ml_core/src/ml_core/data.py
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | |
downsample_power_flows(flows, target_map)
Downsample power flows to 30m using period-ending semantics.
We assume that NWP data represents the average (or accumulated) value for the
period ending at valid_time. For example, a weather forecast for 10:00
describes the weather from 09:00 to 10:00.
To align our targets with these features, we downsample power flows using
closed="right", label="right". This ensures that power readings from
09:30 to 10:00 are aggregated and labeled as 10:00.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
flows
|
DataFrame[SubstationPowerFlows] | LazyFrame[SubstationPowerFlows]
|
Historical power flow data. |
required |
target_map
|
DataFrame[SubstationTargetMap] | LazyFrame[SubstationTargetMap]
|
Map of substation_number to target_col (MW or MVA). |
required |
Returns:
| Type | Description |
|---|---|
LazyFrame[SimplifiedSubstationPowerFlows]
|
Downsampled power flows. |
Source code in packages/ml_core/src/ml_core/data.py
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 | |
ml_core.features
Shared feature engineering logic for ML models.
Functions
add_cyclical_temporal_features(df, time_col='valid_time')
Add cyclical and standard temporal features.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
T
|
DataFrame or LazyFrame with a time column. |
required |
time_col
|
str
|
Name of the time column. |
'valid_time'
|
Returns:
| Type | Description |
|---|---|
T
|
DataFrame or LazyFrame with added temporal features (hour_sin, hour_cos, |
T
|
day_of_year_sin, day_of_year_cos, day_of_week). |
Source code in packages/ml_core/src/ml_core/features.py
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | |
ml_core.scaling
Shared scaling and normalization utilities for ML models.
Classes
Functions
uint8_to_physical_unit(params)
Convert uint8 columns back to physical units (Float32).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
DataFrame[ScalingParams]
|
Patito DataFrame with scaling parameters. |
required |
Returns:
| Type | Description |
|---|---|
list[Expr]
|
List of Polars expressions for the conversion. |
Source code in packages/ml_core/src/ml_core/scaling.py
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | |