Differentiable river routing in PyTorch
Project description
DiffRoute
Differentiable and scalable LTI routing operator integrated into PyTorch
Overview
This repository contains the code for the diffroute model presented in the paper:
Differentiable river routing for end-to-end learning of hydrological processes at diverse scales, ESS Open Archive . May 27, 2025.
diffroute provides a differentiable and GPU-accelerated formulation of classical Linear Time Invariant (LTI) river routing models.
Every component is written in PyTorch, which keeps routing differentiable and easy to integrate in deep learning workflows.
For advanced use-cases, we recommend using diffroute through the companion module diffhydro,
which layers additional data structures and utilities to integrate DiffRoute into more complex differentiable hydrological pipelines, with minimal dependency.
However, DiffRoute can also be used as a standalone component as showcased in this documentation.
Please note that diffroute is in an early development stage and thus subject to changes.
Nevertheless, the explanations included in this documentation are limited to API and structural components that are unlikely to change in the short to middle term.
Key Features
- GPU Acceleration: Formulating LTI River Routing operations as 1D Convolution layers allow for efficient GPU execution.
- Differentiable: DiffRoute is integrated to Pytorch, which allows for efficient gradient computations through Automatic Differentiation.
- Scalable: Computations scale to very large river graphs (up to millions of river channels) using staged computations.
- Generality: DiffRoute allows to formulate any LTI River Routing scheme. Currenlty implemented routing schemes include the Muskingum, Linear Diffusive Wave, Linear Storage, Nash Cascade and Pure Lag schemes. Custom schemes can easily be added as shown in this example.
- Batching: Accepts batched runoff tensors with shape
[batch, catchments, time]and routes them efficiently. This simplifies batched training of parameters and inference of ensemble predictions. - Composability: Integration to Pytorch Automatic Differentiation framework also aims to combine
diffroutewith other learnable components. The companion modulediffhydroaims to simplify the assembly of more complex hydrological pipelines. - Pure Python: DiffRoute is written in pure python. As such it is easily hackable for users interested in experimenting with variations.
Installation
You can install diffroute from pip repository:
pip install diffroute
Or install the latest version from GitHub:
pip install git+https://github.com/TristHas/DiffRoute.git
Or clone and install locally:
git clone https://github.com/TristHas/DiffRoute.git
cd DiffRoute; pip install .
Dependencies
The package depends on:
torch>=2.0networkxpandastqdm
These are automatically installed when using pip.
Quickstart
import numpy as np
import pandas as pd
import networkx as nx
import torch
from diffroute import RivTree, LTIRouter
b = 2 # Batch (or ensemble size)
n = 20 # Number of channels
T = 100 # Number of time steps
device = "cuda:0" # GPU device to use
G = nx.gn_graph(n) # Toy example tree with a unique outlet.
# Define per-node routing parameters. For a Linear Storage scheme, only one parameter "tau"
params = pd.DataFrame({"tau": np.random.rand(n)}, index=G.nodes)
# River Tree data structure
riv_tree = RivTree(G, params=params, irf_fn="linear_storage").to(device)
# Generate random input runoff
runoff = torch.rand(b, n, T, device=device)
# Instantiate the routing model with desired parameters
router = LTIRouter(max_delay=48, dt=1)
# Compute output discharges from input graph and runoffs
discharge = router(runoff, riv_tree)
Documentation
TODO
Publications and Citation
The motivation for, mathematical derivations behind, and illustrative use-cases of DiffRoute are presented in the paper "Differentiable river routing for end-to-end learning of hydrological processes".
"Differentiable river routing for end-to-end learning of hydrological processes at diverse scales"
ESS Open Archive . May 27, 2025. DOI: 10.22541/essoar.174835108.87664030/v1
If you use diffroute in your academic work, please cite the above reference.
The original code used for the experiment in the paper at the time of submission can be found in a standalone repository.
However, We recommend using diffroute through the companion package diffhydro, which layers additional data structures and utilities to ease the use of DiffRoute, with minimal dependency.
The original experiments of the paper have been included as example notebooks within the diffhydro package.
License
This project is licensed under the terms of the MIT license.
See the LICENSE file for details.
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 diffroute-0.1.0.tar.gz.
File metadata
- Download URL: diffroute-0.1.0.tar.gz
- Upload date:
- Size: 58.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7298be5bb9d3499757069c897f3aa4c7ddabd6972ec3918f9cc92b630364b68a
|
|
| MD5 |
14041ef771806044d687d42d362e7039
|
|
| BLAKE2b-256 |
d3b925441ed8143a303f159def0899af38a28167cb708e82eac73b42bc0af373
|
File details
Details for the file diffroute-0.1.0-py3-none-any.whl.
File metadata
- Download URL: diffroute-0.1.0-py3-none-any.whl
- Upload date:
- Size: 93.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3ad64bbca685f51e4a25d38fbed639bddd0962f377e4fff8dfffaf85230b2973
|
|
| MD5 |
8b154a80e6489b399e004e414e5d3af4
|
|
| BLAKE2b-256 |
63f480206c6ed9391f64bd4470f0ade29690ffaba739934b0a68ac6e58412c8e
|