Skip to main content

River low-flow and drought forecasting with TorchRiver

Project description

TorchRiver

TorchRiver is a Python library for river low-flow and drought early warning. It provides a notebook-friendly workflow for loading river data, training forecasting models, predicting low-flow risk, saving .triver models, and launching an Arabic RTL Gradio interface.

The default architecture is river_cnn_lstm:

Conv1D -> ReLU -> MaxPool1D -> LSTM -> Dense -> probability/logit

Available architectures:

Name Description
river_cnn_lstm Conv1D + LSTM hybrid (default, recommended)
cnn_lstm Alias for river_cnn_lstm
lstm Pure LSTM recurrent model
gru GRU recurrent model
cnn Pure convolutional model
mlp Multi-layer perceptron (fully connected)
transformer Transformer encoder model
custom Bring your own nn.Module

Repository: https://github.com/torchriver/torchriver

Installation

pip install torchriver

In Google Colab:

%pip install -q torchriver

TorchRiver Quick Start

Open the main full notebook:

A smaller compact notebook is also included:

Quick start in Python

from torchriver import RiverProject

project = RiverProject(title="TorchRiver", lang="ar", direction="rtl")

project.make_synthetic(
    days=240,
    station_codes=["R001", "R002", "R003"],
    station_names={"R001": "River One", "R002": "River Two", "R003": "River Three"},
)

project.clean()
project.use_model(
    "river_cnn_lstm",
    horizons=[7, 14, 30],
    lookback=30,
    device="auto",
    window_mode="per_station",
    balance_classes=True,
)
project.train(epochs=2, batch_size=512, lr=0.001, seed=42)
print(project.predict_all(horizon=14))
project.save_model("river_warning_model.triver", include_recent_data=True)

Kaggle example

import os, getpass
from torchriver import RiverProject

KAGGLE_URL = "https://www.kaggle.com/datasets/assemelqirsh/elbe-river-data-lake"
os.environ["KAGGLE_USERNAME"] = "YOUR_KAGGLE_USERNAME"
os.environ["KAGGLE_KEY"] = getpass.getpass("Paste Kaggle API key/token hidden: ")

project = RiverProject(title="TorchRiver", lang="ar", direction="rtl")
project.load_kaggle(KAGGLE_URL, nrows=200_000, fallback_synthetic=False)
project.clean()
project.use_model("river_cnn_lstm", horizons=[7, 14, 30], lookback=30, window_mode="per_station", balance_classes=True)
project.train(epochs=2, batch_size=512, lr=0.001)
print(project.predict_all(horizon=14))

TorchRiver never stores Kaggle API keys. Use KAGGLE_USERNAME and KAGGLE_KEY, Kaggle's standard ~/.kaggle/kaggle.json, or a hidden prompt.

Notebooks

All notebooks install TorchRiver from PyPI with %pip install -q torchriver.

Notebook Purpose GitHub Colab
notebooks/TorchRiver_Quick_Start.ipynb Main full quick-start runner https://github.com/torchriver/torchriver/blob/main/notebooks/TorchRiver_Quick_Start.ipynb https://colab.research.google.com/github/torchriver/torchriver/blob/main/notebooks/TorchRiver_Quick_Start.ipynb
notebooks/TorchRiver_Quick_Start_small.ipynb Small compact quick start https://github.com/torchriver/torchriver/blob/main/notebooks/TorchRiver_Quick_Start_small.ipynb https://colab.research.google.com/github/torchriver/torchriver/blob/main/notebooks/TorchRiver_Quick_Start_small.ipynb
notebooks/02_quick_start_synthetic_train_predict.ipynb Synthetic training and prediction https://github.com/torchriver/torchriver/blob/main/notebooks/02_quick_start_synthetic_train_predict.ipynb https://colab.research.google.com/github/torchriver/torchriver/blob/main/notebooks/02_quick_start_synthetic_train_predict.ipynb
notebooks/03_kaggle_url_train_river.ipynb Kaggle URL training https://github.com/torchriver/torchriver/blob/main/notebooks/03_kaggle_url_train_river.ipynb https://colab.research.google.com/github/torchriver/torchriver/blob/main/notebooks/03_kaggle_url_train_river.ipynb
notebooks/08_save_load_triver.ipynb Save/load .triver models https://github.com/torchriver/torchriver/blob/main/notebooks/08_save_load_triver.ipynb https://colab.research.google.com/github/torchriver/torchriver/blob/main/notebooks/08_save_load_triver.ipynb
notebooks/09_gradio_rtl_arabic.ipynb Arabic RTL Gradio UI https://github.com/torchriver/torchriver/blob/main/notebooks/09_gradio_rtl_arabic.ipynb https://colab.research.google.com/github/torchriver/torchriver/blob/main/notebooks/09_gradio_rtl_arabic.ipynb

See notebooks/00_INDEX.ipynb for the full notebook list:

https://github.com/torchriver/torchriver/blob/main/notebooks/00_INDEX.ipynb

Examples

pip install torchriver
python examples/01_train_synthetic.py
python examples/02_train_kaggle_url.py

See examples/README.md for all script examples:

https://github.com/torchriver/torchriver/blob/main/examples/README.md

Compatible data format

Default columns:

date
station_code
station
discharge_m3s
low_flow_q10_threshold_m3s
sdi_30
precipitation_mm
temperature_c
ndwi
target_lowflow

Custom schemas are supported through mapping:

project.load_file(
    "my_river_data.csv",
    mapping={
        "date": "day",
        "station": "gauge_id",
        "station_name": "gauge_name",
        "discharge": "Q_m3s",
        "low_flow_threshold": "Q10",
        "target": "low_flow_flag",
    },
)

If a threshold is missing, TorchRiver computes Q10 per station. If the target is missing, it creates:

target_lowflow = discharge_m3s < low_flow_q10_threshold_m3s

Prediction

project.predict(station="501060", horizon=14)
project.predict_all(horizon=14)
project.diagnose_predictions(horizon=14)

Prediction uses the last lookback days for the selected station. TorchRiver does not reuse the same last rows for every station.

Gradio API

project.launch_river_style(share=True, api=True, direction="rtl")

Client usage:

from gradio_client import Client

client = Client("GRADIO_URL")
client.predict("501060 - DRESDEN", "+14 days", api_name="/predict")

Save/load

project.save_model("river_warning_model.triver", include_recent_data=True)

from torchriver import load_river_model
loaded = load_river_model("river_warning_model.triver")
loaded.predict(station="501060", horizon=14)

Tests

pytest

Repository folders

docs/       Documentation
examples/   Runnable Python examples
notebooks/  Colab and Jupyter notebooks
data/       Small sample datasets
tests/      Pytest tests

Links

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

torchriver-2.0.1.tar.gz (26.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

torchriver-2.0.1-py3-none-any.whl (27.8 kB view details)

Uploaded Python 3

File details

Details for the file torchriver-2.0.1.tar.gz.

File metadata

  • Download URL: torchriver-2.0.1.tar.gz
  • Upload date:
  • Size: 26.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for torchriver-2.0.1.tar.gz
Algorithm Hash digest
SHA256 7d9e72f0a859b9f08320b039db98819cbd189cce5e946ea65bfb1ec914aa7f1f
MD5 38ecabcde85c5da106f7a32cfd00d7a8
BLAKE2b-256 7bfce3b4fc9f23f2da8a8efa019b9c2cea2bd3ae162b0bd2cbdde281116aa0e2

See more details on using hashes here.

Provenance

The following attestation bundles were made for torchriver-2.0.1.tar.gz:

Publisher: publish.yml on torchriver/torchriver-lib

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file torchriver-2.0.1-py3-none-any.whl.

File metadata

  • Download URL: torchriver-2.0.1-py3-none-any.whl
  • Upload date:
  • Size: 27.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for torchriver-2.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 754a0a63f8b2c8ca544cd62037976d1757378aaabafa91fa203b87fc9a999019
MD5 757b458c6ecd99ccb914055cda8408eb
BLAKE2b-256 53738d033ea3aefde311b3fc672efb4d4a53a1a84405abed36f9b7bd66f2fd2e

See more details on using hashes here.

Provenance

The following attestation bundles were made for torchriver-2.0.1-py3-none-any.whl:

Publisher: publish.yml on torchriver/torchriver-lib

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page