drought¶
Composite Drought Index (CDI) monitoring and forecasting. Entry point:
DroughtUseCase.
Use case¶
use_case ¶
DroughtUseCase ¶
Bases: BaseUseCase
Drought risk module — wraps the drought-monitoring package via cdi_runner.py. Implements BaseUseCase and registers itself in core.runner.MODULE_MAP.
Source code in drought/use_case.py
fetch_data ¶
Extract bbox and year range from AnalysisConfig. GEE authentication and data fetching happen inside preprocess().
Source code in drought/use_case.py
preprocess ¶
Fetch ERA5-Land + MODIS from GEE and compute CDI time-series / annual maps.
Delegates to a module-level joblib-cached function so self is never
part of the cache key (joblib >= 1.4 broke ignore=["self"] on methods).
Coordinates are rounded to 6 d.p. before hashing to prevent float-jitter
cache misses for semantically identical AOIs.
Source code in drought/use_case.py
run_model ¶
Run DroughtModel (LSTM or drought_monitoring statistical forecast), build GeoJSON from the spatial CDI map, and package as AnalysisOutput.
Source code in drought/use_case.py
run ¶
Single AOI pipeline — dict config for internal parallel use.
Source code in drought/use_case.py
run_date_ranges ¶
Not supported — drought is single-area only. See UseCaseInfo.single_area_only.
Source code in drought/use_case.py
run_multi_regions ¶
Not supported — drought is single-area only. See UseCaseInfo.single_area_only.
Source code in drought/use_case.py
Features¶
features ¶
DroughtSequenceDataset ¶
Bases: Dataset
PyTorch dataset yielding (input_window, target_sequence) pairs.
Source code in drought/features.py
build_features ¶
Enrich CDI DataFrame with lag features, rolling statistics, and seasonal sine/cosine encodings. Rows with NaN (warm-up period) are dropped.
Source code in drought/features.py
prepare_datasets ¶
prepare_datasets(feat_df: DataFrame, seq_len: int = SEQ_LEN, horizon: int = FORECAST_H, holdout_months: int = HOLDOUT_MONTHS) -> tuple[DroughtSequenceDataset, DroughtSequenceDataset, StandardScaler, torch.Tensor]
Scale features, split train / test, and package into PyTorch datasets.
Source code in drought/features.py
Model¶
model ¶
DroughtLSTM ¶
DroughtLSTM(n_features: int = len(INPUT_COLS), hidden: int = HIDDEN, layers: int = LAYERS, horizon: int = FORECAST_H)
Bases: Module
2-layer LSTM with dropout. Final hidden state → linear head → FORECAST_H outputs. All six forecast steps are predicted simultaneously (multi-output regression).
Source code in drought/model.py
DroughtModel ¶
Orchestrates the drought ML pipeline.
model_type (from config, default 'lstm'): 'lstm' — train DroughtLSTM + MC Dropout forecast (existing) 'drought_monitoring' — use drought_monitoring package's built-in forecast
Designed to be called from DroughtUseCase.run_model().
train_lstm ¶
train_lstm(train_ds: DroughtSequenceDataset, test_ds: DroughtSequenceDataset, device: device | None = None) -> tuple[DroughtLSTM, dict]
Train with early stopping (patience = PATIENCE epochs on val MSE). Best weights are restored before returning.
Source code in drought/model.py
evaluate_lstm ¶
evaluate_lstm(model: DroughtLSTM, test_ds: DroughtSequenceDataset, device: device | None = None) -> dict
1-step-ahead evaluation on the held-out test set.
Source code in drought/model.py
forecast_with_uncertainty ¶
forecast_with_uncertainty(model: DroughtLSTM, last_seq: Tensor, future_dates: DatetimeIndex, n_samples: int = 500, device: device | None = None) -> dict
MC Dropout forecast: enable dropout at inference and run n_samples stochastic forward passes. The spread of the resulting distribution is the model's epistemic (model) uncertainty — it does NOT capture data or structural uncertainty.
Source code in drought/model.py
run_kmeans_typology ¶
Cluster pixel CDI trajectories into drought typologies using KMeans. Clusters are ranked by ascending mean CDI (most drought-prone = cluster 0).
Source code in drought/model.py
drought_severity_stats ¶
Summarise the most recent CDI raster into a mean value and the percentage
of valid pixels falling in each of the 8 standard drought/wet severity
bands (see _classify_cdi_value in cdi_runner.py for the same cutoffs).
Source code in drought/model.py
CDI pipeline¶
cdi_runner ¶
run_cdi_pipeline ¶
Full CDI pipeline.
Source code in drought/cdi_runner.py
build_drought_charts ¶
Build chart data payloads for the frontend.
Source code in drought/cdi_runner.py
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 | |
compute_spatial_uncertainty ¶
Two spatial uncertainty metrics derived from the annual CDI dataset.
Source code in drought/cdi_runner.py
export_cdi_cog ¶
Export CDI maps to Cloud Optimised GeoTIFFs. Called only at the save step.