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:
- GitHub: https://github.com/torchriver/torchriver/blob/main/notebooks/TorchRiver_Quick_Start.ipynb
- Colab: https://colab.research.google.com/github/torchriver/torchriver/blob/main/notebooks/TorchRiver_Quick_Start.ipynb
A smaller compact notebook is also included:
- GitHub: https://github.com/torchriver/torchriver/blob/main/notebooks/TorchRiver_Quick_Start_small.ipynb
- Colab: https://colab.research.google.com/github/torchriver/torchriver/blob/main/notebooks/TorchRiver_Quick_Start_small.ipynb
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.
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
- Repository: https://github.com/torchriver/torchriver
- Notebooks: https://github.com/torchriver/torchriver/blob/main/notebooks/
- Quick Start Notebook: https://github.com/torchriver/torchriver/blob/main/notebooks/TorchRiver_Quick_Start.ipynb
- Small Quick Start Notebook: https://github.com/torchriver/torchriver/blob/main/notebooks/TorchRiver_Quick_Start_small.ipynb
- Examples: https://github.com/torchriver/torchriver/tree/main/examples
- Docs: https://github.com/torchriver/torchriver/tree/main/docs
- Tests: https://github.com/torchriver/torchriver/tree/main/tests
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7d9e72f0a859b9f08320b039db98819cbd189cce5e946ea65bfb1ec914aa7f1f
|
|
| MD5 |
38ecabcde85c5da106f7a32cfd00d7a8
|
|
| BLAKE2b-256 |
7bfce3b4fc9f23f2da8a8efa019b9c2cea2bd3ae162b0bd2cbdde281116aa0e2
|
Provenance
The following attestation bundles were made for torchriver-2.0.1.tar.gz:
Publisher:
publish.yml on torchriver/torchriver-lib
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
torchriver-2.0.1.tar.gz -
Subject digest:
7d9e72f0a859b9f08320b039db98819cbd189cce5e946ea65bfb1ec914aa7f1f - Sigstore transparency entry: 2127260031
- Sigstore integration time:
-
Permalink:
torchriver/torchriver-lib@6891d7880fbdeb2b11c0b69d3a9529333f90bed8 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/torchriver
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@6891d7880fbdeb2b11c0b69d3a9529333f90bed8 -
Trigger Event:
workflow_dispatch
-
Statement type:
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
754a0a63f8b2c8ca544cd62037976d1757378aaabafa91fa203b87fc9a999019
|
|
| MD5 |
757b458c6ecd99ccb914055cda8408eb
|
|
| BLAKE2b-256 |
53738d033ea3aefde311b3fc672efb4d4a53a1a84405abed36f9b7bd66f2fd2e
|
Provenance
The following attestation bundles were made for torchriver-2.0.1-py3-none-any.whl:
Publisher:
publish.yml on torchriver/torchriver-lib
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
torchriver-2.0.1-py3-none-any.whl -
Subject digest:
754a0a63f8b2c8ca544cd62037976d1757378aaabafa91fa203b87fc9a999019 - Sigstore transparency entry: 2127260236
- Sigstore integration time:
-
Permalink:
torchriver/torchriver-lib@6891d7880fbdeb2b11c0b69d3a9529333f90bed8 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/torchriver
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@6891d7880fbdeb2b11c0b69d3a9529333f90bed8 -
Trigger Event:
workflow_dispatch
-
Statement type: