Breakpoint Moduleđź”—
Breakpoint detection strategies for water‑timeseries.
This module provides a hierarchy of breakpoint detection methods for analyzing water‑timeseries data. It includes:
BreakpointMethod– abstract base class with shared helpers.SimpleBreakpoint– a fast rolling‑window statistical detector.BeastBreakpoint– a Bayesian RBEAST‑based detector.NRTBreakpoint– a Near‑Real‑Time breakpoint detector with custom logic.
Each concrete class implements calculate_break for a single lake and
inherits calculate_breaks_batch from the base class. The classes can be
used directly in Python code or indirectly through the water-timeseries
CLI.
Exampleđź”—
from water_timeseries.breakpoint import SimpleBreakpoint breakpoint = SimpleBreakpoint(kwargs_break=dict(window=3, method="median", threshold=-0.25))
breakpoint.calculate_break(dataset) # Returns DataFrame with breakpoint infođź”—
BeastBreakpoint
đź”—
Bases: BreakpointMethod
Bayesian RBEAST-based breakpoint detector.
This method uses the RBEAST library to detect breakpoints in water‑timeseries data using Bayesian change‑point detection. It identifies points where the statistical properties of the time series change significantly.
Parametersđź”—
kwargs_break : dict, optional
Configuration dictionary for RBEAST priors. Common keys include:
- trendMaxOrder : int, default 0
Maximum order of the trend component.
- trendMinSepDist : int, default 1
Minimum separation distance between change points.
break_threshold : float, optional
Probability threshold for detecting a break point. Default is 0.5.
Attributesđź”—
breakpoint_columns : list List of column names in the output DataFrame.
Source code in src/water_timeseries/breakpoint.py
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 | |
calculate_break(dataset, object_id)
đź”—
Calculate breakpoints for a single lake object using RBEAST.
Parametersđź”—
dataset : LakeDataset Dataset containing lake water‑area data. object_id : str Unique identifier (geohash) for the lake object.
Returnsđź”—
pd.DataFrame
DataFrame containing breakpoint information with columns defined in
self.breakpoint_columns plus calculated temporal statistics.
Source code in src/water_timeseries/breakpoint.py
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 | |
BreakpointMethod
đź”—
Base class for breakpoint detection methods.
Parametersđź”—
method_name : str
Short identifier stored in the break_method column of the output
DataFrames. Sub‑classes pass values such as "simple" or "rbeast".
Source code in src/water_timeseries/breakpoint.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 | |
calculate_break(dataset)
đź”—
Calculate breakpoints for a single object.
Sub‑classes must implement the actual detection algorithm and return a
pandas.DataFrame containing at least the columns defined in
self.breakpoint_columns.
Parametersđź”—
dataset : LakeDataset Dataset containing lake water‑area data.
Returnsđź”—
pd.DataFrame DataFrame containing breakpoint information.
Source code in src/water_timeseries/breakpoint.py
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 | |
calculate_breaks_batch(dataset, progress_bar=False)
đź”—
Run calculate_break for every lake in dataset.
Parametersđź”—
dataset : LakeDataset
Dataset providing both raw and normalized water‑area arrays.
progress_bar : bool, optional
Show a tqdm progress bar when True. Default is False.
Returnsđź”—
pd.DataFrame Concatenated results from all lakes in the dataset.
Source code in src/water_timeseries/breakpoint.py
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 | |
get_first_break_date(df, column='water')
đź”—
Placeholder implementation for the abstract base.
Concrete subclasses override this method. The default returns a
(None, None, None) tuple so that calling code can safely handle the lack of
a breakpoint.
Parametersđź”—
df : pd.DataFrame DataFrame with a datetime-like index and a water column. column : str, optional Column name to evaluate. Defaults to "water".
Returnsđź”—
tuple (first_break_date, previous_date, after_date) - All values are None for the default implementation.
Source code in src/water_timeseries/breakpoint.py
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | |
NRTBreakpoint
đź”—
Bases: BreakpointMethod
Near‑Real‑Time (NRT) breakpoint detector.
This method implements custom logic for detecting breakpoints in water‑timeseries data. It follows the same interface as other breakpoint methods but uses internal logic that is distinct from the SimpleBreakpoint and BeastBreakpoint classes.
The NRT method uses AutoARIMA to predict the expected water extent and compares it against the observed value. It also calculates historical statistics and assigns a drainage confidence level based on three criteria.
Parametersđź”—
kwargs_break : dict, optional Configuration dictionary for NRT-specific parameters. Default is an empty dict.
Attributesđź”—
breakpoint_columns : list List of column names in the output DataFrame. output_columns : list List of column names in the output DataFrame, including normalized values (0-1 scale) and their absolute equivalents (scaled by max area). output_columns_base : list Subset of output columns for handling NaN entries.
Notesđź”—
The output includes both normalized values (0-1 range) and absolute values.
Absolute values are computed by multiplying normalized values with the scaling
factor (max area per id_geohash): absolute = normalized * max_area_data.
Examplesđź”—
from water_timeseries.breakpoint import NRTBreakpoint from water_timeseries.dataset import DWDataset bp = NRTBreakpoint() dataset = DWDataset(xr.open_dataset("data.zarr")) result = bp.calculate_break(dataset, analysis_date="2024-07")
Source code in src/water_timeseries/breakpoint.py
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 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 590 591 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 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 | |
calculate_break(dataset, analysis_date, data_aggregation_period='all', object_id=None, keep_nans=False)
đź”—
Calculate breakpoints for a single lake object using NRT logic.
This method implements the NRT-specific breakpoint detection and returns a DataFrame with breakpoint information following the same structure as other breakpoint methods.
Parametersđź”—
dataset : LakeDataset Dataset containing lake water‑area data. object_id : str | Optional[str] Unique identifier (geohash) for the lake object. analysis_date : str or pd.Timestamp The date for which to perform the NRT breakpoint analysis. data_aggregation_period : str, optional The period of data to consider for the analysis (e.g., "all", "monthly") process_nans : bool, optional Set True if you want to return historical water stats Returns
pd.DataFrame
DataFrame containing breakpoint information with columns defined in
self.breakpoint_columns plus calculated temporal statistics.
Source code in src/water_timeseries/breakpoint.py
704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 | |
predict_nrt_arima(ds_in, id_geohash, min_length=3, water_column='water')
đź”—
summary
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ds_in
|
Dataset
|
description |
required |
id_geohash
|
str
|
description |
required |
min_length
|
int
|
Minimum length of the time series. |
3
|
water_column
|
str
|
Name of the water column in the dataset. |
'water'
|
Returns:
| Type | Description |
|---|---|
Series
|
pd.Series: description |
Source code in src/water_timeseries/breakpoint.py
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 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 | |
SimpleBreakpoint
đź”—
Bases: BreakpointMethod
Fast rolling‑window statistical breakpoint detector.
This method detects breakpoints by comparing current water values against rolling window statistics (mean, median, or max). A breakpoint is identified when values fall below a threshold in both a primary and secondary window for consecutive time points, which helps reduce false positives.
Parametersđź”—
kwargs_break : dict, optional
Configuration dictionary with the following keys:
- window : int, default 3
Size of the primary rolling window.
- method : str, default "median"
Rolling statistic to use: "mean", "median", or "max".
- threshold : float, default -0.25
Threshold for detecting a break (values below this indicate a break).
Attributesđź”—
breakpoint_columns : list List of column names in the output DataFrame.
Source code in src/water_timeseries/breakpoint.py
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 | |
calculate_break(dataset, object_id)
đź”—
Calculate breakpoints for a single lake object.
Parametersđź”—
dataset : LakeDataset Dataset containing lake water‑area data. object_id : str Unique identifier (geohash) for the lake object.
Returnsđź”—
pd.DataFrame
DataFrame containing breakpoint information with columns defined in
self.breakpoint_columns plus calculated temporal statistics.
Source code in src/water_timeseries/breakpoint.py
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 | |
get_first_break_date(df, column='water')
đź”—
Find the first break date and the immediately preceding index value.
The detection uses a dual‑window approach: a primary rolling window (kwargs_break['window']) and a secondary window that is window+2. A break is detected when the current value falls below BOTH window calculations for consecutive points, reducing false positives.
Parametersđź”—
df : pd.DataFrame DataFrame with a datetime‑like index and a water column. column : str, optional Column name to evaluate. Defaults to "water".
Returnsđź”—
tuple (first_break_date, previous_date, after_date) where each element is a pandas Timestamp or None if no break was found.
Source code in src/water_timeseries/breakpoint.py
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 | |