Skip to content

Commit 1e56603

Browse files
leoglonzZhennanShi1
andcommitted
Add persistent state caching (Fixes #31)
Co-authored-by: ZhennanShi1 <[email protected]>
1 parent a09163f commit 1e56603

File tree

13 files changed

+465
-335
lines changed

13 files changed

+465
-335
lines changed

.github/PULL_REQUEST_TEMPLATE.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55

66
## Description
77

8-
Fixes # (issue number)
9-
108
<!-- Describe how you addressed the bug/feature request, what choices you made and why. Changes can be listed as bullet point;
119
1210
- ...

.github/workflows/lint.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ jobs:
1212
strategy:
1313
matrix:
1414
os: [ubuntu-latest]
15-
python-version: ['3.12', '3.13']
15+
python-version: ['3.9', '3.12', '3.13']
1616

1717
steps:
1818
- name: Checkout code
1919
uses: actions/[email protected]
2020

2121
- name: Install uv + Python
22-
uses: astral-sh/setup-uv@v6.7.0
22+
uses: astral-sh/setup-uv@v7.1.2
2323
with:
2424
python-version: ${{ matrix.python-version }}
2525
enable-cache: true

.github/workflows/pytest.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ jobs:
1212
strategy:
1313
matrix:
1414
os: [ubuntu-latest]
15-
python-version: ['3.12', '3.13']
15+
python-version: ['3.9', '3.12', '3.13']
1616

1717
steps:
1818
- name: Checkout code
1919
uses: actions/[email protected]
20-
20+
2121
- name: Install uv + Python
22-
uses: astral-sh/setup-uv@v6.7.0
22+
uses: astral-sh/setup-uv@v7.1.2
2323
with:
2424
python-version: ${{ matrix.python-version }}
2525
enable-cache: true

.pre-commit-config.yaml

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,27 @@
11
repos:
22
- repo: https://github.com/pre-commit/pre-commit-hooks
3-
rev: v4.4.0
3+
rev: v6.0.0
44
hooks:
55
- id: trailing-whitespace
66
- id: end-of-file-fixer
77
- id: check-yaml
8-
exclude: "mkdocs.*\\.yml$" # Exclude mkdocs YAML files
8+
exclude: "mkdocs.*\\.yml$"
99
- id: debug-statements
10+
- id: check-toml
1011

1112
- repo: https://github.com/astral-sh/ruff-pre-commit
12-
rev: v0.1.5 # Use whatever version you're currently using
13+
rev: v0.14.2
1314
hooks:
14-
- id: ruff
15-
args: [--fix]
16-
exclude: "hooks/.*\\.py$" # Exclude the hooks directory from ruff
15+
- id: ruff-check
16+
types_or: [python, pyi, jupyter]
17+
args: [--fix, --config, pyproject.toml]
1718
- id: ruff-format
19+
types_or: [python, pyi, jupyter]
20+
args: [--config, pyproject.toml]
21+
22+
- repo: https://github.com/kynan/nbstripout
23+
rev: 0.8.1
24+
hooks:
25+
- id: nbstripout
26+
types: [jupyter]
27+
language_version: python3

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# HydroDL2: Differentiable Hydrological Models
22

3-
[![Python](https://img.shields.io/badge/python-3.12%20%7C%203.13-blue)](https://www.python.org/downloads/)
4-
[![PyTorch](https://img.shields.io/badge/PyTorch-2.8.0-EE4C2C?logo=pytorch)](https://pytorch.org/)
3+
[![Python](https://img.shields.io/badge/python-3.9%20%7C%203.12%20%7C%203.13-blue)](https://www.python.org/downloads/)
4+
[![PyTorch](https://img.shields.io/badge/PyTorch-2.9.0-EE4C2C?logo=pytorch)](https://pytorch.org/)
55

66
[![Build](https://github.com/mhpi/hydrodl2/actions/workflows/pytest.yaml/badge.svg?branch=master)](https://github.com/mhpi/hydrodl2/actions/workflows/pytest.yaml/)
77
[![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff)
@@ -39,15 +39,15 @@ Optionally, add flag `-e` to install in editable mode.
3939
```text
4040
.
4141
├── src/
42-
| └── hydrodl2/
42+
| └── hydrodl2/
4343
│ ├── api/ # Main API
44-
│ | ├── __init__.py
44+
│ | ├── __init__.py
4545
│ | └── methods.py # Methods exposed to end-users
4646
| ├── core/ # Methods used internally
4747
│ ├── models/ # Shared models directory
4848
│ | └── hbv/ # HBV models
4949
| └── modules/ # Augmentations for δMG models
50-
└── docs/
50+
└── docs/
5151
```
5252

5353
</br>

docs/CONTRIBUTING.md

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,40 @@
1-
# Contributing to *hydroDL2*
1+
# Contributing to *HydroDL2*
22

33
Thank you for considering contributing to this project! Whether it's fixing a bug, improving documentation, or adding a new feature, we welcome contributions.
44

5-
We have a minimal set of standards we would ask you to consider to speed up the review process.
5+
There is a minimal set of standards we would ask you to consider to speed up the review process.
66

77
## 🧭 How to Contribute
88

99
1. **Fork the repository**
10-
- If you have not already done so, create a fork of the hydroDL2 repo (master branch) and make changes to this copy.
10+
- If you have not already done so, create a fork of the `hydrodl2` repo (master branch) and make changes to this copy.
1111

1212
2. **Lint & test your code**
13-
- If you have not already, install development packages for hydroDL2.
13+
- Make sure development packages for HydroDL2 are installed. This can be done by flagging dev packages during pip install like `uv pip install "./hydrodl2[dev]`.
1414

15-
- When your changes are ready, run
15+
- Once your changes are complete, run the following in your Python environement:
1616

1717
```bash
1818
cd ./hydrodl2
19-
20-
isort .
21-
22-
ruff check .
2319

2420
pytest tests
21+
22+
pre-commit install
23+
24+
git add .
25+
26+
git commit -m 'your commit message'
2527
```
2628

27-
- If there ruff or pytest report any errors, please try to correct these if possible. (We can also help in the next step).
29+
Upon committing, pre-commit will run a series of checks according to `.pre-commit-config.yaml` lint and format your code. This will block your commit if changes are made or requested. If manual changes are required, you will be notified. If only automatic changes are made, simply perform the git add and commit once more to push your code.
30+
31+
Note: if pytest does not work, try `python -m pytest`.
32+
33+
- If ruff or pytest report any errors, please try to correct these if possible. Otherwise, do `pre-commit uninstall` to proceed with committing your code and we can help in the next step.
2834

2935
3. **Make a pull request (PR)**
30-
- When you are ready make a PR to the hydroDL2 repository master branch.
36+
- When you are ready, make a PR of your fork to the HydroDL2 repository master branch.
3137

32-
- In the PR description, make sure to include enough detail so that we can understand the changes made and rationale if necessary.
38+
- In the PR description, include enough detail so that we can understand the changes made and rationale if necessary.
3339

34-
- If hydroDL2 master branch has new commits not included in your forked version, we would ask you to merge these new changes into your fork before we accept the PR. We can assist with this if necessary.
40+
- If the HydroDL2 master branch has new commits not included in your forked version, we would ask you to merge these new changes into your fork before we accept the PR. We can assist with this if necessary.

docs/adding_models_and_modules.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
# Adding Models and Modules to *hydroDL2*
1+
# Adding Models and Modules to *HydroDL2*
22

33
We illustrate this with a hydrology model HBV 1.2.
44

55
- In the `models/` directory, create a folder for the model type if it does not already exist, using only lowercase.
66
- e.g., we should create `models/hbv/`, or confirm it exists.
77

88
- Within `models/<your_model>/`, create a `.py` file for the model taking the name that will be exposed to users, converting to lowercase.
9-
- e.g., HBV 1.2 goes to `hbv1_2.py`.
9+
- e.g., HBV 1.2 goes to `hbv_1_2.py`.
1010

1111
- The model file should only contain one model class. If you want to place multiple variants of your model in the same file, make sure to add a variant specification to your config and indicate
1212
with the `ver_name` flag when loading your model with `load_model()`. Otherwise, the first model listed in your file will be loaded by default.

docs/index.md

Lines changed: 0 additions & 17 deletions
This file was deleted.

pyproject.toml

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,13 @@ authors = [
1717
maintainers = [
1818
{ name = "Leo Lonzarich", email = "[email protected]" },
1919
]
20-
requires-python = ">=3.12.0"
20+
requires-python = ">=3.9.0"
2121
dynamic = ["version"]
2222
dependencies = [
23-
"numpy>=2.3.3",
24-
"pre-commit>=4.3.0",
25-
"sourcedefender>=15.0.14",
26-
"torch>=2.8.0",
27-
"uv>=0.8.22",
23+
"numpy",
24+
"sourcedefender",
25+
"torch",
26+
"uv",
2827
]
2928
classifiers = [
3029
"Programming Language :: Python :: 3",
@@ -34,9 +33,10 @@ classifiers = [
3433

3534
[project.optional-dependencies]
3635
dev = [
37-
"ipywidgets>=8.1.7",
38-
"pytest>=8.4.2",
39-
"ruff>=0.13.2",
36+
"ipywidgets",
37+
"pre-commit",
38+
"pytest",
39+
"ruff",
4040
]
4141

4242
[project.urls]

src/hydrodl2/core/calc/utils.py

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,10 @@ def change_param_range(param: torch.Tensor, bounds: list[float]) -> torch.Tensor
1818
1919
Returns
2020
-------
21-
out
21+
torch.Tensor
2222
The parameter with the specified bounds.
2323
"""
24-
out = param * (bounds[1] - bounds[0]) + bounds[0]
25-
return out
24+
return param * (bounds[1] - bounds[0]) + bounds[0]
2625

2726

2827
def param_bounds_2d(
@@ -49,19 +48,12 @@ def param_bounds_2d(
4948
5049
Returns
5150
-------
52-
out : torch.Tensor
51+
torch.Tensor
5352
The 3D parameter array.
5453
"""
5554
out_temp = (
5655
params[:, num * nmul : (num + 1) * nmul] * (bounds[1] - bounds[0]) + bounds[0]
5756
)
58-
out = (
59-
out_temp.unsqueeze(0)
60-
.repeat(ndays, 1, 1)
61-
.reshape(
62-
ndays,
63-
params.shape[0],
64-
nmul,
65-
)
57+
return (
58+
out_temp.unsqueeze(0).repeat(ndays, 1, 1).reshape(ndays, params.shape[0], nmul)
6659
)
67-
return out

0 commit comments

Comments
 (0)