Contracts API
Contracts
Defines the "data contracts": the schemas defining the precise shape of each data source, and the semantics.
Dependency Isolation
This package is designed to be extremely lightweight. It defines the shape of the data using Patito and Polars, but it does not contain any ML-specific logic or heavy dependencies like MLflow. This ensures that any component in the system (e.g., a data ingestion script or a dashboard) can import these schemas without bringing in the entire ML stack.
Key Data Contracts
SubstationFeatures: The final joined dataset ready for ML model training and inference. It enforces strict validation for critical features, including the dynamically calculatedlatest_available_weekly_power_lag(which prevents lookahead bias) and theensemble_memberfield.PowerForecast: The schema for deterministic ensemble forecasts generated by the ML models. It includes fields for tracking the model name, initialization times (both power forecast and underlying NWP), and the predicted power flow.ProcessedNwp: Weather data after ensemble selection and interpolation.ModelConfig: Configuration schema for ML models, defining hyperparameters, feature selection, and critical forecasting parameters such as:required_lookback_days: The amount of historical data required for dynamic autoregressive lags (e.g., 14 days).nwp_availability_delay_hours: The delay before an NWP forecast becomes available for inference (e.g., 3 hours), used to prevent lookahead bias during backtesting.
Design principals
- Naming of columns: Prefer snake_case, except for acronyms or SI units. For example, capitalise "DER" (the acronym of distributed energy resource) and use upper case for "MW" (megawatts).
- Semantic checks: Checking that a value is within range should be fairly generous. The aim is to catch physically impossible values, rather than possible-but-unlikely values.
contracts.data_schemas
Data schemas for the NGED substation forecast project.
Classes
H3GridWeights
Bases: Model
Schema for the pre-computed H3 grid weights.
This contract defines the mapping between H3 hexagons and a regular latitude/longitude grid.
It is used to ensure type safety when passing spatial mapping data from generic geospatial
utilities (like packages/geo) to dataset-specific ingestion pipelines (like packages/dynamical_data).
Source code in packages/contracts/src/contracts/data_schemas.py
433 434 435 436 437 438 439 440 441 442 443 444 445 446 | |
InferenceParams
Bases: BaseModel
Parameters for ML model inference.
Source code in packages/contracts/src/contracts/data_schemas.py
229 230 231 232 233 234 235 236 237 | |
MissingCorePowerVariablesError
Bases: ValueError
Raised when a substation CSV lacks both MW and MVA data.
Source code in packages/contracts/src/contracts/data_schemas.py
25 26 27 28 | |
Nwp
Bases: Model
Weather data schema for NWP forecasts.
Source code in packages/contracts/src/contracts/data_schemas.py
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 | |
Functions
validate(dataframe, columns=None, allow_missing_columns=False, allow_superfluous_columns=False, drop_superfluous_columns=False)
classmethod
Validate the given dataframe, ensuring no nulls from second step onwards.
Source code in packages/contracts/src/contracts/data_schemas.py
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 | |
NwpColumns
Centralized constants for NWP column names.
Used to prevent typos and ensure consistency across feature engineering and model training.
Source code in packages/contracts/src/contracts/data_schemas.py
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 | |
PowerForecast
Bases: Model
Forecast data schema for deterministic ensemble forecasts.
Source code in packages/contracts/src/contracts/data_schemas.py
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 | |
ProcessedNwp
Bases: Model
Weather data after ensemble selection and interpolation.
Note: Accumulated variables (e.g., precipitation, radiation) are already de-accumulated by Dynamical.org prior to download, and should not be differenced.
Clever Optimization: To save memory, weather variables are scaled to a 0-255 range (uint8) before being saved to disk. The scaling formula is: uint8_value = round(((physical_value - buffered_min) / buffered_range) * 255).
When loaded, they are cast to Float32 but retain the 0-255 scale.
Source code in packages/contracts/src/contracts/data_schemas.py
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 | |
ScalingParams
Bases: Model
Schema for weather variable scaling parameters.
Used when scaling between physical units (e.g. degrees C) and their unsigned 8-bit integer (uint8) representations. uint8 represents integers in the range [0, 255].
Source code in packages/contracts/src/contracts/data_schemas.py
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 | |
SimplifiedSubstationPowerFlows
Bases: Model
Standardized, single-column representation of power flows.
This model is used after the best available power column (MW or MVA) has been selected and renamed to 'MW_or_MVA'.
Source code in packages/contracts/src/contracts/data_schemas.py
103 104 105 106 107 108 109 110 111 112 | |
SubstationFeatures
Bases: Model
Final joined dataset ready for XGBoost.
Weather features are kept in their physical units (e.g., degrees Celsius, m/s) to ensure precision during interpolation and feature engineering.
Source code in packages/contracts/src/contracts/data_schemas.py
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 | |
SubstationLocations
Bases: Model
The data structure of the raw substation location data from NGED.
Source code in packages/contracts/src/contracts/data_schemas.py
127 128 129 130 131 132 133 134 135 136 137 138 139 140 | |
SubstationLocationsWithH3
Bases: SubstationLocations
Substation locations including their H3 index.
Source code in packages/contracts/src/contracts/data_schemas.py
143 144 145 146 | |
SubstationMetadata
Bases: Model
Metadata for a substation, joining location data with live telemetry info.
Source code in packages/contracts/src/contracts/data_schemas.py
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 | |
SubstationPowerFlows
Bases: Model
Source code in packages/contracts/src/contracts/data_schemas.py
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 | |
Functions
validate(dataframe, columns=None, allow_missing_columns=False, allow_superfluous_columns=False, drop_superfluous_columns=False)
classmethod
Validate the given dataframe, ensuring either MW or MVA is present and has data.
NOTE: Fully null DataFrames are allowed to handle edge cases where: 1. An entire partition's data was cleaned and all values marked as stuck/insane 2. Ingestion failed completely for a partition (empty DataFrame after filtering)
In these cases, the validation passes through to the parent class which allows null values for the columns. This prevents pipeline crashes from legitimate empty data scenarios. The downstream model training logic will need to handle fully null target variables by either skipping training or using fallback strategies.
Source code in packages/contracts/src/contracts/data_schemas.py
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 | |
SubstationTargetMap
Bases: Model
Maps substations to their primary power column and stores their peak capacity.
This model is used to determine whether to use MW or MVA as the target variable for a given substation, and provides the peak capacity for scaling and validation.
Source code in packages/contracts/src/contracts/data_schemas.py
115 116 117 118 119 120 121 122 123 124 | |
contracts.hydra_schemas
Hydra configuration schemas for the NGED substation forecast project.
Classes
DataSplitConfig
Bases: BaseModel
Configuration for temporal data splitting.
Source code in packages/contracts/src/contracts/hydra_schemas.py
10 11 12 13 14 15 16 | |
ModelConfig
Bases: BaseModel
Configuration for the ML model.
Source code in packages/contracts/src/contracts/hydra_schemas.py
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 | |
ModelFeaturesConfig
Bases: BaseModel
Configuration for model features.
Source code in packages/contracts/src/contracts/hydra_schemas.py
26 27 28 29 30 | |
NwpModel
Bases: str, Enum
Available NWP datasets.
Source code in packages/contracts/src/contracts/hydra_schemas.py
19 20 21 22 23 | |
TrainingConfig
Bases: BaseModel
Root configuration object for model training and evaluation.
Source code in packages/contracts/src/contracts/hydra_schemas.py
63 64 65 66 67 | |
contracts.settings
Classes
DataQualitySettings
Bases: BaseSettings
Settings for data quality thresholds in substation flow processing.
These thresholds are used to identify problematic telemetry data:
- stuck_std_threshold: When the rolling standard deviation falls below this value
(across a 48-period/24-hour window), the sensor is likely stuck. We replace such
values with null to preserve the temporal grid. A value of 0.01 MW was chosen
because substations with normal operation typically have much higher variability.
-
max_mw_threshold: Active power above this value is considered physically unrealistic for primary substations in the NGED portfolio. A threshold of 100.0 MW was chosen because typical primary substations operate in the tens of MW range, and values exceeding 100 MW are extremely rare anomalies. -
min_mw_threshold: Active power below this value is potentially erroneous (negative values can occur at times of high renewable generation). A threshold of -20.0 MW was chosen to allow for reverse power flow during high renewable generation periods while still catching implausible extreme negative values.
Centralizing these in Settings allows them to be configurable per environment (dev/staging/prod) while preventing logic drift between asset checks and data cleaning steps. All code that references these thresholds should import them from here, not define them locally.
Source code in packages/contracts/src/contracts/settings.py
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 | |
Settings
Bases: BaseSettings
Configuration settings for the NGED substation forecast project.
Source code in packages/contracts/src/contracts/settings.py
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 | |
Functions
validate_url(v)
classmethod
Validate that the S3 bucket URL is a valid URL.
Source code in packages/contracts/src/contracts/settings.py
121 122 123 124 125 126 | |
Functions
find_project_root()
Find the project root by looking for uv.lock.
Source code in packages/contracts/src/contracts/settings.py
9 10 11 12 13 14 15 | |