Dataset Validation References¶
validation
¶
Dataset validation framework for structural and constraint checking.
This module provides a flexible validation framework for verifying dataset integrity across scenarios and time splits. It's particularly useful when managing multiple scenarios with complex model setups (e.g., 50 weekly simulations) to ensure correct time series selection, topology, constraints, and other structural properties.
The validation framework follows a simple pattern
- Create concrete Validation classes implementing specific checks
- Register validations in a DatasetValidator subclass
- Run validate_dataset() to execute all registered checks
Examples:
Basic constraint validation:
>>> from mesqual.validation import DatasetValidator, ConstraintValidation
>>>
>>> class MyValidator(DatasetValidator):
... def _register_validations(self):
... # Ensure line capacity is within bounds
... self.add_validation(
... ConstraintValidation(
... flag='Line.PeriodConstraints.capacity_up',
... min_value=0,
... max_value=1000,
... object_subset=[101, 102, 103]
... )
... )
>>>
>>> validator = MyValidator()
>>> validator.validate_dataset(dataset)
Scenario-specific validation:
>>> class ScenarioValidator(DatasetValidator):
... def __init__(self, scenario_name: str):
... self.scenario_name = scenario_name
... super().__init__()
...
... def _register_validations(self):
... # Validation logic specific to scenario
... capacity = get_capacity_for_scenario(self.scenario_name)
... self.add_validation(
... ConstraintValidation(
... flag='Line.PeriodConstraints.capacity_up',
... exact_value=capacity
... )
... )
Custom validation logic:
>>> class CustomValidation(Validation):
... def validate(self, dataset: Dataset) -> bool:
... data = dataset.fetch('MyFlag')
... return custom_check(data)
...
... def get_error_message(self, dataset: Dataset) -> str:
... return f"Custom validation failed for {dataset.name}"
Validation
¶
Bases: ABC
Abstract base class for dataset validation logic.
Subclasses must implement the validate() method with custom validation logic. Optionally override get_error_message() and get_success_message() for custom feedback messages.
The validation pattern separates validation logic from error messaging, allowing reusable validators with context-specific messages.
Examples:
>>> class PositivePriceValidation(Validation):
... def validate(self, dataset: Dataset) -> bool:
... prices = dataset.fetch('BiddingZone.Results.market_price')
... return (prices >= 0).all().all()
...
... def get_error_message(self, dataset: Dataset) -> str:
... prices = dataset.fetch('BiddingZone.Results.market_price')
... negative_count = (prices < 0).sum().sum()
... return f"Found {negative_count} negative prices in {dataset.name}"
Source code in submodules/mesqual/mesqual/validation.py
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 | |
validate
abstractmethod
¶
validate(dataset: Dataset) -> bool
Execute validation logic on a dataset.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dataset
|
Dataset
|
The Dataset instance to validate. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if validation passes, False otherwise. |
Source code in submodules/mesqual/mesqual/validation.py
93 94 95 96 97 98 99 100 101 102 103 | |
get_error_message
¶
get_error_message(dataset: Dataset) -> str
Generate error message when validation fails.
Override this method to provide detailed, context-specific error messages that help diagnose validation failures.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dataset
|
Dataset
|
The Dataset instance that failed validation. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Human-readable error message describing the validation failure. |
Source code in submodules/mesqual/mesqual/validation.py
105 106 107 108 109 110 111 112 113 114 115 116 117 | |
get_success_message
¶
get_success_message(dataset: Dataset) -> str
Generate success message when validation passes.
Override this method to provide informative success messages.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dataset
|
Dataset
|
The Dataset instance that passed validation. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Human-readable success message confirming validation passed. |
Source code in submodules/mesqual/mesqual/validation.py
119 120 121 122 123 124 125 126 127 128 129 130 | |
DatasetValidator
¶
Bases: ABC
Manages and executes a collection of dataset validations.
DatasetValidator orchestrates multiple Validation instances, executing them sequentially and providing comprehensive reporting of results. Subclasses implement _register_validations() to define which checks to perform.
The validator automatically runs all registered validations during init, ensuring validations are defined before the validator is used.
Attributes:
| Name | Type | Description |
|---|---|---|
validations |
list[Validation]
|
List of Validation instances to execute. |
Examples:
Basic validator with multiple checks:
>>> class NetworkValidator(DatasetValidator):
... def _register_validations(self):
... # Check line capacities are positive
... self.add_validation(
... ConstraintValidation(
... flag='Line.PeriodConstraints.capacity_up',
... min_value=0
... )
... )
... # Check generators have valid efficiency
... self.add_validation(
... ConstraintValidation(
... flag='Generator.efficiency',
... min_value=0,
... max_value=1
... )
... )
>>>
>>> validator = NetworkValidator()
>>> validator.validate_dataset(my_dataset)
Scenario-aware validator:
>>> class IsolatedNetworkValidator(DatasetValidator):
... def __init__(self, external_borders: list[int]):
... self.external_borders = external_borders
... super().__init__()
...
... def _register_validations(self):
... # Ensure external borders have zero capacity
... self.add_validation(
... ConstraintValidation(
... flag='Line.PeriodConstraints.capacity_up',
... exact_value=0,
... object_subset=self.external_borders
... )
... )
Source code in submodules/mesqual/mesqual/validation.py
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 | |
__init__
¶
__init__()
Initialize validator and register all validations.
Automatically calls _register_validations() to populate the validation list. Subclasses can override init to accept configuration parameters, but must call super().init() after setting any instance attributes needed by _register_validations().
Source code in submodules/mesqual/mesqual/validation.py
188 189 190 191 192 193 194 195 196 197 | |
add_validations
¶
add_validations(validations: Iterable[Validation])
Add multiple validations to the validator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
validations
|
Iterable[Validation]
|
Iterable of Validation instances to register. |
required |
Examples:
>>> validations = [
... ConstraintValidation(flag='price', min_value=0),
... ConstraintValidation(flag='demand', min_value=0)
... ]
>>> self.add_validations(validations)
Source code in submodules/mesqual/mesqual/validation.py
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 | |
add_validation
¶
add_validation(validation: Validation)
Add a single validation to the validator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
validation
|
Validation
|
Validation instance to register. |
required |
Examples:
>>> self.add_validation(
... ConstraintValidation(flag='capacity', min_value=0, max_value=1000)
... )
Source code in submodules/mesqual/mesqual/validation.py
235 236 237 238 239 240 241 242 243 244 245 246 247 | |
validate_dataset
¶
validate_dataset(dataset: Dataset)
Execute all registered validations on a dataset.
Runs each validation sequentially, logging results and producing a summary report. All validations are executed even if some fail, allowing comprehensive diagnosis of issues.
Logging levels:
- Individual validation failures: ERROR
- Individual validation successes: INFO
- Overall summary (all passed): INFO
- Overall summary (some failed): WARNING
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dataset
|
Dataset
|
The Dataset instance to validate. |
required |
Examples:
>>> validator = MyValidator()
>>> for dataset in study.scen.datasets:
... validator.validate_dataset(dataset)
Source code in submodules/mesqual/mesqual/validation.py
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 | |
ConstraintValidation
¶
Bases: Validation
Validates that dataset values satisfy numeric constraints.
ConstraintValidation provides flexible constraint checking for numeric data, supporting min/max bounds, exact values, NA handling, and object subsetting. It's the most commonly used validation for checking model inputs like capacities, efficiencies, prices, and other numeric parameters.
The validation fetches data using the specified flag and applies constraint checks. All checks use pandas vectorized operations for efficiency on large time series.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
flag
|
FlagType
|
The flag to fetch from the dataset (e.g., 'Line.PeriodConstraints.capacity_up'). |
required |
min_value
|
float | None
|
Minimum allowed value (inclusive). If None, no minimum check is performed. |
None
|
max_value
|
float | None
|
Maximum allowed value (inclusive). If None, no maximum check is performed. |
None
|
exact_value
|
float | None
|
Exact required value. If specified, all non-NA values must equal this. Takes precedence over min_value and max_value. |
None
|
isna_ok
|
bool
|
Whether NA/NaN values are acceptable. If False, any NA causes validation to fail. Default is True. |
True
|
object_subset
|
list[int | str] | None
|
Optional list of object IDs to validate. If specified, only checks these objects (e.g., specific line IDs, generator IDs). Uses pandas indexing on the fetched data. |
None
|
Attributes:
| Name | Type | Description |
|---|---|---|
flag |
The dataset flag to validate. |
|
min_value |
Minimum constraint or None. |
|
max_value |
Maximum constraint or None. |
|
exact_value |
Exact value constraint or None. |
|
isna_ok |
Whether NAs are acceptable. |
|
object_subset |
Object IDs to check or None for all objects. |
Examples:
Check RES generator availability between 0 and 1:
>>> validation = ConstraintValidation(
... flag='Generator.availability_factor',
... min_value=0,
... max_value=1,
... object_subset=list_of_all_res_generators,
... isna_ok=False # Availability must be defined
... )
Verify specific borders have zero capacity (isolated network):
>>> validation_up = ConstraintValidation(
... flag='Line.PeriodConstraints.capacity_up',
... exact_value=0,
... object_subset=[1234, 5678] # Specific line IDs
... )
>>> validation_down = ConstraintValidation(
... flag='Line.PeriodConstraints.capacity_down',
... exact_value=0,
... object_subset=[1234, 5678] # Specific line IDs
... )
Notes:
- When exact_value is specified, it takes precedence over min/max constraints
- NA values are allowed by default (isna_ok=True) and are excluded from checks
- object_subset uses pandas indexing, so works with any valid pandas index
- Validation checks all time periods if the flag contains time series data
Source code in submodules/mesqual/mesqual/validation.py
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 | |
validate
¶
validate(dataset: Dataset) -> bool
Execute constraint validation on dataset.
Fetches data for the specified flag and checks it against all configured constraints (min, max, exact, NA). Returns True only if all constraints pass.
The validation logic:
1. Fetch data using self.flag
2. If object_subset specified, filter to those objects
3. Check NA constraint if isna_ok=False
4. Check exact_value constraint if specified (ignoring NAs)
5. Check min_value constraint if specified
6. Check max_value constraint if specified
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dataset
|
Dataset
|
The Dataset instance to validate. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if all constraints are satisfied, False otherwise. |
Source code in submodules/mesqual/mesqual/validation.py
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 | |
get_error_message
¶
get_error_message(dataset: Dataset) -> str
Generate detailed error message describing constraint violation.
Overrides base Validation.get_error_message() to include specific information about which flag, objects, and constraints failed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dataset
|
Dataset
|
The Dataset instance that failed validation. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Error message in format: "ConstraintValidation failed for Dataset X: |
str
|
flag_name for objects [ids] must be constraint_description" |
Source code in submodules/mesqual/mesqual/validation.py
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 | |
get_success_message
¶
get_success_message(dataset: Dataset) -> str
Generate success message confirming constraints were satisfied.
Overrides base Validation.get_success_message() to include specific information about which flag, objects, and constraints passed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dataset
|
Dataset
|
The Dataset instance that passed validation. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Success message in format: "ConstraintValidation successful for Dataset X: |
str
|
flag_name for objects [ids] are valid for constraint_description" |
Source code in submodules/mesqual/mesqual/validation.py
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 | |