hydro_correct¶
The hydro_correct module provides interactive correction algorithms for hydrological forecasting, implementing five-point quadratic smoothing and cubic spline interpolation based on Zhang Silong's 2006 paper.
Classes¶
HydrographCorrector¶
1 2 3 4 5 6 7 8 | |
A class for interactive correction of flood hydrographs using five-point quadratic smoothing and cubic spline interpolation.
Methods:
five_point_smooth¶
1 2 3 4 5 6 7 8 9 10 11 12 | |
cubic_spline_interpolation¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | |
apply_correction¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | |
Functions¶
apply_smooth_correction¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | |
apply_water_balance_correction¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | |
calculate_water_balance_metrics¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | |
validate_correction_quality¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | |
API Reference¶
Author: Wenyu Ouyang Date: 2025-01-17 LastEditTime: 2025-10-28 08:22:40 LastEditors: Wenyu Ouyang Description: FilePath: \hydroutils\hydroutils\hydro_correct.py Copyright (c) 2023-2026 Wenyu Ouyang. All rights reserved.
HydrographCorrector
¶
Interactive corrector for flood hydrograph.
This class implements a combination of five-point quadratic smoothing and cubic spline interpolation based on Zhang Silong's 2006 paper "Research on Interactive Correction Technology for Flood Forecasting Process".
The corrector provides methods for
- Five-point quadratic smoothing
- Cubic spline interpolation
- Combined correction process
Attributes:
| Name | Type | Description |
|---|---|---|
time_points |
ndarray
|
Array of time points. |
discharge_original |
ndarray
|
Original discharge values. |
n_points |
int
|
Number of data points. |
smoothing_matrix |
csr_matrix
|
Pre-computed smoothing matrix. |
Source code in hydroutils/hydro_correct.py
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 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 | |
__init__(time_points, discharge_values)
¶
Initialize the hydrograph corrector.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
time_points
|
ndarray
|
Array of time points. |
required |
discharge_values
|
ndarray
|
Array of discharge values. |
required |
Note
The smoothing matrix is pre-computed during initialization for efficiency. This matrix is used in the five-point quadratic smoothing process.
Source code in hydroutils/hydro_correct.py
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | |
apply_correction(modified_discharge, smoothing_enabled=True, interpolation_enabled=True)
¶
Apply the complete correction algorithm.
Applies a combination of five-point quadratic smoothing and cubic spline interpolation to the discharge data. Both steps can be enabled/disabled independently.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
modified_discharge
|
ndarray
|
Modified discharge data. |
required |
smoothing_enabled
|
bool
|
Whether to enable five-point smoothing. Defaults to True. |
True
|
interpolation_enabled
|
bool
|
Whether to enable spline interpolation. Defaults to True. |
True
|
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: Corrected discharge data. |
Note
The correction process: 1. Applies five-point smoothing if enabled and n ≥ 5 2. Applies cubic spline interpolation if enabled and n ≥ 3 3. Ensures non-negative values in the result
Source code in hydroutils/hydro_correct.py
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 | |
cubic_spline_interpolation(x, y, x_new=None)
¶
Perform cubic spline interpolation.
Interpolates the given data points using cubic splines with natural boundary conditions. For sequences shorter than 3 points, falls back to linear interpolation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ndarray
|
Original time points. |
required |
y
|
ndarray
|
Original discharge values. |
required |
x_new
|
Optional[ndarray]
|
New time points for interpolation. If None, uses original points. Defaults to None. |
None
|
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: Interpolated discharge values. |
Note
Uses natural spline boundary conditions (zero second derivative at endpoints). For n < 3 points, automatically switches to linear interpolation. Ensures non-negative results for physical consistency.
Source code in hydroutils/hydro_correct.py
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 | |
five_point_smooth(discharge_values)
¶
对径流数据进行五点二次平滑
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
discharge_values
|
ndarray
|
径流值数组 |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
平滑后的径流值数组 |
Source code in hydroutils/hydro_correct.py
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 | |
apply_smooth_correction(original_data, modified_data, discharge_column='gen_discharge', time_column='time')
¶
Apply smoothing correction algorithm based on the paper.
This is the main interface function that supports multiple point modifications using five-point quadratic smoothing and cubic spline interpolation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
original_data
|
DataFrame
|
Original data before any modifications. |
required |
modified_data
|
DataFrame
|
Data after user modifications. |
required |
discharge_column
|
str
|
Name of discharge column. Defaults to "gen_discharge". |
'gen_discharge'
|
time_column
|
str
|
Name of time column. Defaults to "time". |
'time'
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
pd.DataFrame: Data with smoothing correction applied. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If required columns are missing from the data. |
Note
The correction process: 1. Validates input data 2. Creates HydrographCorrector instance 3. Applies smoothing and interpolation 4. Returns corrected data
Source code in hydroutils/hydro_correct.py
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 | |
apply_water_balance_correction(original_data, modified_data, discharge_column='gen_discharge', time_column='time', net_rain_column='net_rain')
¶
Apply water balance correction to discharge data.
Performs volume correction by calculating and applying a water balance coefficient to maintain consistency between net rainfall and discharge volumes.
The correction process
- Calculate total net rainfall volume
- Calculate total discharge volume
- Compute and apply volume correction factor
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
original_data
|
DataFrame
|
Original data before any modifications. |
required |
modified_data
|
DataFrame
|
Data after user modifications. |
required |
discharge_column
|
str
|
Name of discharge column. Defaults to "gen_discharge". |
'gen_discharge'
|
time_column
|
str
|
Name of time column. Defaults to "time". |
'time'
|
net_rain_column
|
str
|
Name of net rainfall column. Defaults to "net_rain". |
'net_rain'
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
pd.DataFrame: Data with water balance correction applied. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If required columns are missing from the data. |
Note
- Assumes discharge and net_rain are in the same units (e.g., both in mm)
- If net_rain_column is missing, returns the input data unchanged
- Ensures all discharge values remain non-negative
Source code in hydroutils/hydro_correct.py
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 | |
calculate_water_balance_metrics(data, net_rain_column='net_rain', discharge_column='gen_discharge')
¶
Calculate water balance and discharge statistics metrics.
Computes various metrics to assess water balance and discharge characteristics
- Total net rainfall
- Total discharge volume
- Water balance error (%)
- Basic discharge statistics (mean, max, min, std)
- Peak timing information
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataFrame
|
Input data containing discharge and optionally net rainfall. |
required |
net_rain_column
|
str
|
Name of net rainfall column. Defaults to "net_rain". |
'net_rain'
|
discharge_column
|
str
|
Name of discharge column. Defaults to "gen_discharge". |
'gen_discharge'
|
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict
|
Dictionary containing the following metrics: - total_net_rain (float): Total net rainfall (if available) - total_discharge (float): Total discharge volume - balance_error_percent (float): Water balance error percentage - discharge_stats (dict): Dictionary containing: - mean (float): Mean discharge - max (float): Maximum discharge - min (float): Minimum discharge - std (float): Standard deviation - peak_time_index: Index of peak discharge |
Note
- Assumes discharge and net_rain are in the same units
- Water balance error is only calculated if net rainfall data is available
- All statistics are computed ignoring any NaN values
Source code in hydroutils/hydro_correct.py
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 | |
validate_correction_quality(original_data, corrected_data, discharge_column='gen_discharge')
¶
Validate the quality of hydrograph correction.
Computes various metrics to assess how well the correction preserves important characteristics of the hydrograph while improving its quality.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
original_data
|
DataFrame
|
Original data before correction. |
required |
corrected_data
|
DataFrame
|
Data after correction. |
required |
discharge_column
|
str
|
Name of discharge column. Defaults to "gen_discharge". |
'gen_discharge'
|
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict
|
Dictionary containing quality metrics: - mse (float): Mean squared error - rmse (float): Root mean squared error - mae (float): Mean absolute error - relative_error_percent (float): Mean relative error percentage - peak_preservation_ratio (float): Ratio of corrected to original peak - original_peak (float): Original peak discharge value - corrected_peak (float): Corrected peak discharge value |
Raises:
| Type | Description |
|---|---|
ValueError
|
If discharge_column is missing from either dataset. |
Note
- All error metrics are computed between original and corrected values
- Peak preservation ratio should ideally be close to 1.0
- Relative error uses a small epsilon (1e-8) to avoid division by zero
Source code in hydroutils/hydro_correct.py
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 | |