Calibration
The calibration module provides tools for model calibration, global sensitivity analysis, and uncertainty quantification using PEST++ via pyemu.
Installation
Calibration dependencies are optional and must be installed separately:
Make sure the PEST++ executables (pestpp-glm, pestpp-ies, pestpp-sen) are on the system PATH.
Overview
The calibration workflow is driven by a single YAML configuration file (alongside the main MOBIDIC configuration). The PestSetup class orchestrates all steps: generating PEST++ template and instruction files, running the forward model, and parsing results.
Currently supported PEST++ tools:
| Tool | Method | Use case |
|---|---|---|
glm |
Gauss-Levenberg-Marquardt | Gradient-based calibration |
ies |
Iterative Ensemble Smoother | Ensemble-based calibration and uncertainty |
sen |
Sensitivity analysis | Global sensitivity analysis |
Calibration setup
Orchestrator for PEST++ calibration of MOBIDICpy.
Creates a complete PEST++ working directory with all necessary files and provides methods to execute calibration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
calib_config
|
CalibrationConfig | str | Path
|
Calibration configuration (CalibrationConfig object or path to YAML). |
required |
base_path
|
Path | None
|
Base directory for resolving relative paths in the config. |
None
|
Source code in mobidic/calibration/pest_setup.py
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 | |
working_dir
property
Path to the PEST++ working directory.
load_results()
Load results from a completed PEST++ run.
Returns:
| Type | Description |
|---|---|
'CalibrationResults'
|
CalibrationResults parsed from PEST++ output files. |
Source code in mobidic/calibration/pest_setup.py
run(num_workers=None, start_manager=True)
Execute PEST++ with parallel workers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_workers
|
int | None
|
Override number of workers (default: from config or os.cpu_count()). |
None
|
start_manager
|
bool
|
If True (default), start the manager on this machine (local mode). If False, start agents only for cluster mode. |
True
|
Returns:
| Type | Description |
|---|---|
'CalibrationResults'
|
CalibrationResults with parsed output. |
Source code in mobidic/calibration/pest_setup.py
setup()
Create complete PEST++ working directory with all files.
Returns:
| Type | Description |
|---|---|
Path
|
Path to the working directory. |
Source code in mobidic/calibration/pest_setup.py
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 | |
Calibration results
Container for parsed PEST++ calibration results.
Provides access to: - Optimal parameter values - Objective function history - Residuals - Parameter sensitivities (GLM/SEN) - Ensemble statistics (IES)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
master_dir
|
Path
|
Path to the PEST++ master directory. |
required |
calib_config
|
CalibrationConfig
|
Calibration configuration. |
required |
Source code in mobidic/calibration/results.py
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 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 | |
pst
property
Load the PEST control file.
from_pest_output(master_dir, calib_config)
classmethod
Create CalibrationResults from completed PEST++ output.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
master_dir
|
Path
|
Path to PEST++ master directory. |
required |
calib_config
|
CalibrationConfig
|
Calibration configuration. |
required |
Returns:
| Type | Description |
|---|---|
CalibrationResults
|
CalibrationResults object. |
Source code in mobidic/calibration/results.py
get_ensemble_results()
Get IES ensemble results (prior and posterior).
Returns:
| Type | Description |
|---|---|
dict | None
|
Dict with ‘prior_parameters’, ‘posterior_parameters’, |
dict | None
|
‘prior_observations’, ‘posterior_observations’ DataFrames, |
dict | None
|
or None if not available. |
Source code in mobidic/calibration/results.py
get_objective_function_history()
Get objective function values across iterations.
Returns a DataFrame with at minimum iteration and phi columns.
The source file and meaning of phi depend on the PEST++ tool:
glm: readscalibration.iobj(CSV);phi=total_phi.ies: readscalibration.phi.actual.csv;phi= mean across ensemble members. Extra columnsstdand one column per member are also included.- other tools: reads
calibration.rec;phi= total phi extracted from the record file.
Returns:
| Type | Description |
|---|---|
DataFrame | None
|
DataFrame with iteration number and phi (objective function value), |
DataFrame | None
|
or None if the expected file is not found. |
Source code in mobidic/calibration/results.py
get_optimal_parameters()
Get the optimal parameter values from the calibration.
Returns:
| Type | Description |
|---|---|
dict[str, float]
|
Dict mapping parameter name to optimal value. |
Source code in mobidic/calibration/results.py
get_parameter_sensitivities()
Get parameter sensitivities.
sen: reads{case}.msn(Morris sensitivity); returns all columns (par_name, n_samples, sen_mean, sen_mean_abs, sen_std_dev).glm: loads the Jacobian (.jcb/.jco) via pyemu and computes composite sensitivity (column-wise L2 norm).
Returns:
| Type | Description |
|---|---|
DataFrame | None
|
DataFrame with sensitivity information, or None if not available. |
Source code in mobidic/calibration/results.py
get_residuals()
Get observation residuals (simulated - observed) from the final iteration.
Returns:
| Type | Description |
|---|---|
DataFrame | None
|
DataFrame with obs_name, observed, simulated, residual, weight columns, |
DataFrame | None
|
or None if .rei file not found. |
Source code in mobidic/calibration/results.py
Configuration
Bases: BaseModel
Complete calibration configuration for PEST++ integration.
Source code in mobidic/calibration/config.py
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 | |
check_calibration_within_simulation()
Ensure calibration_period is contained within simulation_period.
If simulation_period is not set, it defaults to calibration_period at runtime. But if both are set, calibration must be within simulation.
Source code in mobidic/calibration/config.py
check_observation_names_unique()
Ensure all observation group names are unique.
Source code in mobidic/calibration/config.py
check_parameter_names_unique()
Ensure all parameter names are unique.
Source code in mobidic/calibration/config.py
set_case_name_default()
Default case_name to ‘sensitivity’ for pestpp-sen, ‘calibration’ otherwise.
Source code in mobidic/calibration/config.py
Bases: BaseModel
A single parameter to be calibrated by PEST++.
Source code in mobidic/calibration/config.py
check_bounds()
Validate that lower_bound < upper_bound and initial_value is within bounds.
Source code in mobidic/calibration/config.py
check_name_no_spaces(v)
classmethod
PEST++ parameter names cannot contain spaces.
Bases: BaseModel
An observation group (e.g., discharge at a specific gauging station).
Source code in mobidic/calibration/config.py
check_name_no_spaces(v)
classmethod
PEST++ observation names cannot contain spaces.
check_weight_non_negative(v)
classmethod
Bases: BaseModel
Configuration for a derived metric used as pseudo-observation.
Source code in mobidic/calibration/config.py
check_metric_name(v)
classmethod
Validate metric name is supported.
Source code in mobidic/calibration/config.py
check_weight_non_negative(v)
classmethod
Bases: BaseModel
A date range with start and end dates.
Used for both calibration_period and simulation_period.
Source code in mobidic/calibration/config.py
check_date_format(v)
classmethod
Validate that date strings are parseable as YYYY-MM-DD or YYYY-MM-DD HH:MM:SS.
Source code in mobidic/calibration/config.py
check_start_before_end()
Validate that start_date < end_date.
Source code in mobidic/calibration/config.py
Load and validate calibration configuration from YAML file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config_path
|
str | Path
|
Path to the calibration YAML file. |
required |
Returns:
| Type | Description |
|---|---|
CalibrationConfig
|
Validated CalibrationConfig object. |
Source code in mobidic/calibration/config.py
Observations
Load observed data from CSV file for an observation group.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obs_group
|
ObservationGroup
|
Observation group configuration. |
required |
base_path
|
Path
|
Base directory for resolving relative file paths. |
required |
start_date
|
str | Timestamp | None
|
Optional start date to filter observations. |
None
|
end_date
|
str | Timestamp | None
|
Optional end date to filter observations. |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame with ‘time’ (datetime) and ‘value’ columns, sorted by time. |
Source code in mobidic/calibration/observation.py
Align observed data to simulation time steps using nearest-neighbor matching.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obs_df
|
DataFrame
|
DataFrame with ‘time’ and ‘value’ columns. |
required |
sim_times
|
list | ndarray | DatetimeIndex
|
Simulation time stamps. |
required |
tolerance_seconds
|
float | None
|
Maximum allowed time difference in seconds for matching. If None, uses half the minimum simulation time step. |
None
|
Returns:
| Type | Description |
|---|---|
tuple[ndarray, ndarray, ndarray]
|
Tuple of (sim_indices, obs_values, obs_times): - sim_indices: Indices into sim_times where observations match. - obs_values: Observed values at matched times. - obs_times: Observed times at matched times. |
Source code in mobidic/calibration/observation.py
Performance metrics
Nash-Sutcliffe Efficiency.
NSE = 1 - sum((sim - obs)^2) / sum((obs - mean(obs))^2) Perfect score: 1.0
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
simulated
|
ndarray
|
Simulated values. |
required |
observed
|
ndarray
|
Observed values. |
required |
Returns:
| Type | Description |
|---|---|
float
|
NSE value (range: -inf to 1.0). |
Source code in mobidic/calibration/metrics.py
NSE on log-transformed flows (emphasizes low flows).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
simulated
|
ndarray
|
Simulated values. |
required |
observed
|
ndarray
|
Observed values. |
required |
eps
|
float
|
Small constant to avoid log(0). |
1e-06
|
Returns:
| Type | Description |
|---|---|
float
|
NSE of log-transformed values. |
Source code in mobidic/calibration/metrics.py
Kling-Gupta Efficiency.
KGE = 1 - sqrt((r - 1)^2 + (alpha - 1)^2 + (beta - 1)^2) where r = correlation, alpha = std(sim)/std(obs), beta = mean(sim)/mean(obs) Perfect score: 1.0
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
simulated
|
ndarray
|
Simulated values. |
required |
observed
|
ndarray
|
Observed values. |
required |
Returns:
| Type | Description |
|---|---|
float
|
KGE value (range: -inf to 1.0). |
Source code in mobidic/calibration/metrics.py
Percent bias as a fraction (not percentage).
pbias = sum(sim - obs) / sum(obs) Perfect score: 0.0
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
simulated
|
ndarray
|
Simulated values. |
required |
observed
|
ndarray
|
Observed values. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Percent bias as fraction (e.g., 0.05 = 5% overestimation). |
Source code in mobidic/calibration/metrics.py
Root Mean Square Error.
Perfect score: 0.0
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
simulated
|
ndarray
|
Simulated values. |
required |
observed
|
ndarray
|
Observed values. |
required |
Returns:
| Type | Description |
|---|---|
float
|
RMSE value. |
Source code in mobidic/calibration/metrics.py
Relative peak error.
peak_error = (max(sim) - max(obs)) / max(obs) Perfect score: 0.0
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
simulated
|
ndarray
|
Simulated values. |
required |
observed
|
ndarray
|
Observed values. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Relative peak error. |
Source code in mobidic/calibration/metrics.py
Compute multiple metrics for a sim/obs pair.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
simulated
|
ndarray
|
Simulated values. |
required |
observed
|
ndarray
|
Observed values. |
required |
metric_names
|
list[str]
|
List of metric names to compute. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, float]
|
Dict mapping metric name to computed value. |
Source code in mobidic/calibration/metrics.py
Quick import
from mobidic.calibration import (
# Setup and results
PestSetup,
CalibrationResults,
# Configuration
CalibrationConfig,
CalibrationParameter,
CalibrationPeriod,
MetricConfig,
ObservationGroup,
load_calibration_config,
# Observations
load_observations,
align_observations_to_simulation,
# Metrics
nse,
nse_log,
kge,
pbias,
rmse,
peak_error,
compute_metrics,
)
Workflow example
from pathlib import Path
from mobidic.calibration import PestSetup, load_calibration_config
# Load configuration
calib_config = load_calibration_config("Arno.calibration.yaml")
# Set up PEST++ files
pest = PestSetup(calib_config)
working_dir = pest.setup()
# Run calibration
results = pest.run()
# Extract results
optimal = results.get_optimal_parameters()
phi = results.get_objective_function_history()
sens = results.get_parameter_sensitivities()
See Examples for complete working scripts.