A curated list of open technology projects to sustain a stable climate, energy supply, biodiversity and natural resources.

LDCast

A precipitation nowcasting model based on a latent diffusion model.
https://github.com/meteoswiss/ldcast

Category: Natural Resources
Sub Category: Water Supply and Quality

Last synced: about 20 hours ago
JSON representation

Repository metadata

Latent diffusion for generative precipitation nowcasting

README.md

LDCast is a precipitation nowcasting model based on a latent diffusion model (LDM, used by e.g. Stable Diffusion).

This repository contains the code for using LDCast to make predictions and the code used to generate the analysis in the LDCast paper (a preprint is available at https://arxiv.org/abs/2304.12891).

A GPU is recommended for both using and training LDCast, although you may be able to generate some samples with a CPU and enough patience.

Installation

It is recommended you install the code in its own virtual environment (created with e.g. pyenv or conda).

Clone the repository, then, in the main directory, run

$ pip install -e .

This should automatically install the required packages (which might take some minutes). In the paper, we used PyTorch 11.2 but are not aware of any problems with newer versions.

If you don't want the requirements to be installed (e.g. if you installed them manually with conda), use:

$ pip install --no-dependencies -e .

Using LDCast

Pretrained models

The pretrained models are available at the Zenodo repository https://doi.org/10.5281/zenodo.7780914. Unzip the file ldcast-models.zip. The default is to unzip it to the models directory, but you can also use another location.

Producing predictions

The easiest way to produce predictions is to use the ldcast.forecast.Forecast class, which will set up all models and data transformations and is callable with a past precipitation array.

from ldcast import forecast

fc = forecast.Forecast(
    ldm_weights_fn=ldm_weights_fn, autoenc_weights_fn=autoenc_weights_fn
)
R_pred = fc(R_past)

Here, ldm_weights_fn is the path to the LDM weights and autoenc_weights_fn is the path to the autoencoder weights. R_past is a NumPy array of precipitation rates with shape (timesteps, height, width) where timesteps must be 4 and height and width must be divisible by 32.

Ensemble predictions

If want to process multiple cases at once and/or generate several ensemble members, there is the ldcast.forecast.ForecastDistributed class. The usage is similar to the Forecast class, for example:

from ldcast import forecast

fc = forecast.ForecastDistributed(
    ldm_weights_fn=ldm_weights_fn, autoenc_weights_fn=autoenc_weights_fn
)
R_pred = fc(R_past, ensemble_members=32)

Here, R_past should be of shape (cases, timesteps, height, width) where cases is the number of cases you want to process. For each case, ensemble_members predictions are produced (this is the last axis of R_pred). ForecastDistributed automatically distributes the workload to multiple GPUs if you have them.

Demo

For a practical example, you can run the demo in the scripts directory. First download the ldcast-demo-20210622.zip file from the Zenodo repository, then unzip it in the data directory. Then run

$ python forecast_demo.py

A sample output can be found in the file ldcast-demo-video-20210622.zip in the data repository. See the function forecast_demo in forecast_demo.py see how the Forecast class works. To run an ensemble mean of 8 members using the ForecastDistributed class, you can use:

$ python forecast_demo.py --ensemble-members=8

The demo for a single ensemble member runs in a couple of minutes on our system using one V100 GPU; with a CPU around 10 minutes or more would be expected. A progress bar will show the status of the generation.

Training

Training data

The preprocessed training data, needed to rerun the LDCast training, can be found at the Zenodo repository. Unzip the ldcast-datasets.zip file to the data directory.

Training the autoencoder

In the scripts directory, run

$ python train_autoenc.py --model_dir="../models/autoenc_train"

to run the training of the autoencoder with the default parameters. The training checkpoints will be saved in the ../models/autoenc_train directory (feel free to change this).

It has been reported that this training may encounter a condition where the loss goes to nan. If this happens, try restarting from the latest checkpoint:

$ python train_autoenc.py --model_dir="../models/autoenc_train" --ckpt_path="../models/autoenc_train/<checkpoint_file>"

where <checkpoint_file> should be the latest checkpoint in the ../models/autoenc_train/ directory.

Training the diffusion model

In the scripts directory, run

$ python train_genforecast.py --model_dir="../models/genforecast_train"

to run the training of the diffusion model with the default parameters, or

$ python train_genforecast.py --model_dir="../models/genforecast_train" --config=<path_to_config_file>

to run the training with different parameters. Some config files can be found in the config directory. The training checkpoints will be saved in the ../models/genforecast_train directory (again, this can be changed freely).

Evaluation

You can find scripts for evaluating models in the scripts directory:

  • eval_genforecast.py to evaluate LDCast
  • eval_dgmr.py to evaluate DGMR (requires tensorflow installation and the DGMR model from https://github.com/deepmind/deepmind-research/tree/master/nowcasting placed in the models/dgmr directory)
  • eval_pysteps.py to evaluate PySTEPS (requires pysteps installation)
  • metrics.py to produce metrics from the evaluation results produced with the functions in scripts above
  • plot_genforecast.py to make plots from the results generated

Owner metadata


GitHub Events

Total
Last Year

Committers metadata

Last synced: 7 days ago

Total Commits: 24
Total Committers: 2
Avg Commits per committer: 12.0
Development Distribution Score (DDS): 0.458

Commits in past year: 24
Committers in past year: 2
Avg Commits per committer in past year: 12.0
Development Distribution Score (DDS) in past year: 0.458

Name Email Commits
Jussi Leinonen j****n@m****h 13
Jussi Leinonen j****n@g****m 11

Committer domains:


Issue and Pull Request metadata

Last synced: 2 days ago

Total issues: 23
Total pull requests: 0
Average time to close issues: 14 days
Average time to close pull requests: N/A
Total issue authors: 12
Total pull request authors: 0
Average comments per issue: 2.17
Average comments per pull request: 0
Merged pull request: 0
Bot issues: 0
Bot pull requests: 0

Past year issues: 1
Past year pull requests: 0
Past year average time to close issues: N/A
Past year average time to close pull requests: N/A
Past year issue authors: 1
Past year pull request authors: 0
Past year average comments per issue: 1.0
Past year average comments per pull request: 0
Past year merged pull request: 0
Past year bot issues: 0
Past year bot pull requests: 0

More stats: https://issues.ecosyste.ms/repositories/lookup?url=https://github.com/meteoswiss/ldcast

Top Issue Authors

  • nathzi1505 (6)
  • bugsuse (4)
  • tomasvanoyen (3)
  • DaDaDa-FH (2)
  • caglarkucuk (1)
  • jacksd233 (1)
  • jichongxing2023 (1)
  • wang6501sfx (1)
  • clearlyzero (1)
  • ajikmr (1)
  • M4verrick (1)
  • byphilipp (1)

Top Pull Request Authors


Top Issue Labels

Top Pull Request Labels


Dependencies

setup.py pypi
  • dask *
  • einops *
  • fire *
  • h5py *
  • matplotlib *
  • netCDF4 *
  • numba *
  • numpy *
  • omegaconf *
  • pyshp *
  • pytorch-lightning *
  • scipy *
  • torch *
  • tqdm *

Score: 5.568344503761097