Giter Club home page Giter Club logo

dan-boat / pyesd Goto Github PK

View Code? Open in Web Editor NEW
40.0 5.0 8.0 70.09 MB

Python Package for Empirical Statistical Downscaling. pyESD is under active development and all colaborators are welcomed. The purpose of the package is to downscale any climate variables e.g. precipitation and temperature using predictors from reanalysis datasets (eg. ERA5) to point scale. pyESD adopts many ML and AL as the transfer function.

Home Page: https://dan-boat.github.io/PyESD/

License: MIT License

Python 97.91% Makefile 0.59% Shell 1.49%
machine-learning tensorflow2 sckit-learn precipitation downscaling ensemble-machine-learning deep-learning

pyesd's Introduction

Python Package for Empirical Statistical Downscaling (v1.01) ๐ŸŒฆ๏ธ ๐ŸŒจ๏ธ ๐ŸŒง๏ธ ๐Ÿ”ฅ ๐ŸŒก๏ธ

PyESD is an open-source framework of the Perfect Prognosis approach of statistical downscaling of any climate-related variable such as precipitation, temperature, and wind speed using reanalysis products eg. ERA5 as predictors. The package features all the downscaling cycles including data preprocessing, predictor selection, constructions (eg. using transformers), model selection, training, validation and evaluation, and future prediction. The package serves as the means of downscaling General Circulation Models of future climate to high resolution relevant for climate impact assessment such as droughts, flooding, wildfire risk, and others. The main specialties of the pyESD include:

  • Well designed in an OOP style that considers weather stations as individual objects and all the downscaling routines as attributes. This ensures fewer lines of code that cover the end-to-end downscaling of climate change variable products.
  • PyESD features many machine learning algorithms and predictor selection techniques that can be experimented with toward the selection and design of robust transfer functions that can be coupled with GCM to generate future estimates of climate change
  • And many other functionalities that are highlighted in the paper description of the package (to be submitted).

Documentation ๐Ÿ“˜

The package documentation is accessible at https://dan-boat.github.io/PyESD/

The main component and the work flow of the package are summarised in the modeling outline:

Model outline

Installation ๐Ÿ› ๏ธ

  1. Install the standard version: pip install pyESD from PyPI or git clone [email protected]:Dan-Boat/PyESD.git cd to the folder | pip install .

  2. Install in editable mode: pip install -e pyESD or pip install -e . in the package base folder clone from github The installation might require some dependencies that must be installed if not successful from the distribution from PyPI: cartopy, xarray, sciki-learn, scipy and the other scientific frameworks such as NumPy, pandas, Matplotlib, and seaborn

  3. Alternatively, to ensure the installation in an isolated environment, virtual python environment using conda or virtualenv can be used to create a separate env for the package installation

Examples

The package has been used for downscaling precipitation and temperature for a catchment located in southwestern Germnany. We have also used it for generating future rainfall products for all the synoptic weather stations in Ghana. Their respective control scripts are located in the examples folder. Generally, the control scripts follow the modeling workflow as shown in: Downscaling steps. For instance, the downscaling framework show below can be experimented with to select the robust predictor selection method and emprical transfer function for a specific location and predictand variable. modeling framework

Workflow demonstration: To use the PP-ESD model to downscale climate model, weather station and reanalysis datasets are required. The predictors are loaded in as netCDF files and the predictand as csv file. Let assume that the various predictor variables are stored locally in the era5_datadir directory /home/daniel/ERA5/ and the predictand variable eg. precipitation is stored in station_dir The files should have the same timestamp as the interested predictand variable

  1. import all the required modules
from pyESD.Weatherstation import read_station_csv
from pyESD.standardizer import MonthlyStandardizer, StandardScaling
from pyESD.ESD_utils import store_pickle, store_csv
from pyESD.splitter import KFold
from pyESD.ESD_utils import Dataset
from pyESD.Weatherstation import read_weatherstationnames

import pandas as pd 
  1. Read the datasets
ERA5Data = Dataset('ERA5', {
    't2m':os.path.join(era5_datadir, 't2m_monthly.nc'),
    'msl':os.path.join(era5_datadir, 'msl_monthly.nc'),
    'u10':os.path.join(era5_datadir, 'u10_monthly.nc'),
    'v10':os.path.join(era5_datadir, 'v10_monthly.nc'),
    'z250':os.path.join(era5_datadir, 'z250_monthly.nc'),
  1. define potential predictors and radius of predictor construction, time range for model training and evaluation
radius = 100 #km
predictors = ["t2m", "tp","msl", "v10", "u10"]

from1958to2010 = pd.date_range(start="1958-01-01", end="2010-12-31", freq="MS") #training and validation

from2011to2020 = pd.date_range(start="2011-01-01", end="2020-12-31", freq="MS")  # testing trained data 
  1. Read weather stations as objects and apply the downscaling cycle attributes. Note that running the model the first time for a specific location extract the regional means using the define radius and location of the station. The extracted means are stored in a pickel files in the directory called predictor_dir
variable = "Precipitation"
SO = read_station_csv(filename=station_dir, varname=variable)
        
        
# USING ERA5 DATA
# ================


#setting predictors 
SO.set_predictors(variable, predictors, predictordir, radius,)

#setting standardardizer
SO.set_standardizer(variable, standardizer=MonthlyStandardizer(detrending=False,
                                                               scaling=False))

scoring = ["neg_root_mean_squared_error",
            "r2", "neg_mean_absolute_error"]

#setting model
regressor = "RandomForest"
SO.set_model(variable, method=regressor, 
               daterange=from1958to2010,
               predictor_dataset=ERA5Data, 
               cv=KFold(n_splits=10),
               scoring = scoring)


# MODEL TRAINING (1958-2000)
# ==========================


SO.fit(variable, from1958to2010, ERA5Data, fit_predictors=True, predictor_selector=True, 
            selector_method="Recursive" , selector_regressor="ARD",
            cal_relative_importance=False)
   
score_1958to2010, ypred_1958to2010 = SO.cross_validate_and_predict(variable, from1958to2010, ERA5Data)
   
score_2011to2020 = SO.evaluate(variable, from2011to2020, ERA5Data)

ypred_1958to2010 = SO.predict(variable, from1958to2010, ERA5Data)
   
ypred_2011to2020 = SO.predict(variable, from2011to2020, ERA5Data)

ypred_2011to2020.plot()
plt.show()

Package testing

The package is tested using the unittest framework with synthetic generated data. The testing scripts are located in the test folder. Running the various scripts with -v flag (higher level of verbose), would validate the modified version of the package.

Publications

The package description and application paper is currently under review in GCM (Boateng & Mutz 2023) Its application for weather station in Ghana was presented at the AGU22 Link and the paper is under preparation

Citation: Upload on zenodo: https://doi.org/10.5281/zenodo.7748769

Collaborators are welcomed: interms of model application, model improvement, documentation and expansion of the package!

@ Daniel Boateng (linkedin) : University of Tuebingen ๐Ÿ“จ [email protected]

pyesd's People

Contributors

dan-boat avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

pyesd's Issues

Cartopy and Modules Error

Hi Thanks for following up on installation.
I'm now, able to install pyESD
yet, the modules aren't imported.
It says "No Module" error even after Cartopy being installed.
image

image

I'm assuming this is windows compatible!

No Module error : pyESD

Hi even after installing pyESD, I get a "No Modeule" error.
image

Below is my environment

(base) C:\Users>conda create -name pyESD
Collecting package metadata (current_repodata.json): done
Solving environment: failed with repodata from current_repodata.json, will retry with next repodata source.
Collecting package metadata (repodata.json): done
Solving environment: failed

PackagesNotFoundError: The following packages are not available from current channels:

  - pyesd

Current channels:

  - https://conda.anaconda.org/conda-forge/win-64
  - https://conda.anaconda.org/conda-forge/noarch
  - https://repo.anaconda.com/pkgs/main/win-64
  - https://repo.anaconda.com/pkgs/main/noarch
  - https://repo.anaconda.com/pkgs/r/win-64
  - https://repo.anaconda.com/pkgs/r/noarch
  - https://repo.anaconda.com/pkgs/msys2/win-64
  - https://repo.anaconda.com/pkgs/msys2/noarch

To search for alternate channels that may provide the conda package you're
looking for, navigate to

    https://anaconda.org

and use the search bar at the top of the page.



(base) C:\Users\buradagi>pip install pyESD
Defaulting to user installation because normal site-packages is not writeable
Requirement already satisfied: pyESD in c:\users\appdata\roaming\python\python39\site-packages (1.0.1)
Requirement already satisfied: eofs in c:\users\appdata\roaming\python\python39\site-packages (from pyESD) (1.4.0)
Requirement already satisfied: numpy in c:\users\appdata\roaming\python\python39\site-packages (from pyESD) (1.22.4)
Requirement already satisfied: scipy in c:\windows\anaconda3-2021.11\lib\site-packages (from pyESD) (1.7.1)
Requirement already satisfied: pandas in c:\users\appdata\roaming\python\python39\site-packages (from pyESD) (2.0.1)
Requirement already satisfied: sklearn in c:\users\appdata\roaming\python\python39\site-packages (from pyESD) (0.0)
Requirement already satisfied: statsmodels in c:\windows\anaconda3-2021.11\lib\site-packages (from pyESD) (0.12.2)
Requirement already satisfied: xarray in c:\users\appdata\roaming\python\python39\site-packages (from pyESD) (2023.4.2)
Requirement already satisfied: seaborn in c:\windows\anaconda3-2021.11\lib\site-packages (from pyESD) (0.11.2)
Requirement already satisfied: tensorflow in c:\users\appdata\roaming\python\python39\site-packages (from pyESD) (2.12.0)
Requirement already satisfied: scikit-optimize in c:\users\appdata\roaming\python\python39\site-packages (from pyESD) (0.9.0)
Requirement already satisfied: matplotlib in c:\windows\anaconda3-2021.11\lib\site-packages (from pyESD) (3.4.3)
Requirement already satisfied: netCDF4 in c:\users\appdata\roaming\python\python39\site-packages (from pyESD) (1.6.3)
Requirement already satisfied: python-dateutil>=2.7 in c:\windows\anaconda3-2021.11\lib\site-packages (from matplotlib->pyESD) (2.8.2)
Requirement already satisfied: kiwisolver>=1.0.1 in c:\windows\anaconda3-2021.11\lib\site-packages (from matplotlib->pyESD) (1.3.1)
Requirement already satisfied: cycler>=0.10 in c:\windows\anaconda3-2021.11\lib\site-packages (from matplotlib->pyESD) (0.10.0)
Requirement already satisfied: pillow>=6.2.0 in c:\windows\anaconda3-2021.11\lib\site-packages (from matplotlib->pyESD) (8.4.0)
Requirement already satisfied: pyparsing>=2.2.1 in c:\windows\anaconda3-2021.11\lib\site-packages (from matplotlib->pyESD) (3.0.4)
Requirement already satisfied: six in c:\windows\anaconda3-2021.11\lib\site-packages (from cycler>=0.10->matplotlib->pyESD) (1.16.0)
Requirement already satisfied: cftime in c:\users\appdata\roaming\python\python39\site-packages (from netCDF4->pyESD) (1.6.2)
Requirement already satisfied: pytz>=2020.1 in c:\windows\anaconda3-2021.11\lib\site-packages (from pandas->pyESD) (2021.3)
Requirement already satisfied: tzdata>=2022.1 in c:\users\appdata\roaming\python\python39\site-packages (from pandas->pyESD) (2023.3)
Requirement already satisfied: pyaml>=16.9 in c:\users\appdata\roaming\python\python39\site-packages (from scikit-optimize->pyESD) (23.5.9)
Requirement already satisfied: joblib>=0.11 in c:\windows\anaconda3-2021.11\lib\site-packages (from scikit-optimize->pyESD) (1.1.0)
Requirement already satisfied: scikit-learn>=0.20.0 in c:\windows\anaconda3-2021.11\lib\site-packages (from scikit-optimize->pyESD) (0.24.2)
Requirement already satisfied: PyYAML in c:\windows\anaconda3-2021.11\lib\site-packages (from pyaml>=16.9->scikit-optimize->pyESD) (6.0)
Requirement already satisfied: threadpoolctl>=2.0.0 in c:\windows\anaconda3-2021.11\lib\site-packages (from scikit-learn>=0.20.0->scikit-optimize->pyESD) (2.2.0)
Requirement already satisfied: patsy>=0.5 in c:\windows\anaconda3-2021.11\lib\site-packages (from statsmodels->pyESD) (0.5.2)
Requirement already satisfied: tensorflow-intel==2.12.0 in c:\users\appdata\roaming\python\python39\site-packages (from tensorflow->pyESD) (2.12.0)
Requirement already satisfied: packaging in c:\users\appdata\roaming\python\python39\site-packages (from tensorflow-intel==2.12.0->tensorflow->pyESD) (23.1)
Requirement already satisfied: jax>=0.3.15 in c:\users\appdata\roaming\python\python39\site-packages (from tensorflow-intel==2.12.0->tensorflow->pyESD) (0.4.10)
Requirement already satisfied: protobuf!=4.21.0,!=4.21.1,!=4.21.2,!=4.21.3,!=4.21.4,!=4.21.5,<5.0.0dev,>=3.20.3 in c:\users\buradagi\appdata\roaming\python\python39\site-packages (from tensorflow-intel==2.12.0->tensorflow->pyESD) (4.23.2)
Requirement already satisfied: keras<2.13,>=2.12.0 in c:\users\appdata\roaming\python\python39\site-packages (from tensorflow-intel==2.12.0->tensorflow->pyESD) (2.12.0)
Requirement already satisfied: wrapt<1.15,>=1.11.0 in c:\windows\anaconda3-2021.11\lib\site-packages (from tensorflow-intel==2.12.0->tensorflow->pyESD) (1.12.1)
Requirement already satisfied: tensorflow-io-gcs-filesystem>=0.23.1 in c:\users\buradagi\appdata\roaming\python\python39\site-packages (from tensorflow-intel==2.12.0->tensorflow->pyESD) (0.31.0)
Requirement already satisfied: h5py>=2.9.0 in c:\windows\anaconda3-2021.11\lib\site-packages (from tensorflow-intel==2.12.0->tensorflow->pyESD) (3.2.1)
Requirement already satisfied: absl-py>=1.0.0 in c:\users\appdata\roaming\python\python39\site-packages (from tensorflow-intel==2.12.0->tensorflow->pyESD) (1.4.0)
Requirement already satisfied: libclang>=13.0.0 in c:\users\appdata\roaming\python\python39\site-packages (from tensorflow-intel==2.12.0->tensorflow->pyESD) (16.0.0)
Requirement already satisfied: flatbuffers>=2.0 in c:\users\appdata\roaming\python\python39\site-packages (from tensorflow-intel==2.12.0->tensorflow->pyESD) (23.5.26)
Requirement already satisfied: setuptools in c:\windows\anaconda3-2021.11\lib\site-packages (from tensorflow-intel==2.12.0->tensorflow->pyESD) (58.0.4)
Requirement already satisfied: tensorflow-estimator<2.13,>=2.12.0 in c:\users\buradagi\appdata\roaming\python\python39\site-packages (from tensorflow-intel==2.12.0->tensorflow->pyESD) (2.12.0)
Requirement already satisfied: typing-extensions>=3.6.6 in c:\windows\anaconda3-2021.11\lib\site-packages (from tensorflow-intel==2.12.0->tensorflow->pyESD) (3.10.0.2)
Requirement already satisfied: grpcio<2.0,>=1.24.3 in c:\users\appdata\roaming\python\python39\site-packages (from tensorflow-intel==2.12.0->tensorflow->pyESD) (1.54.2)
Requirement already satisfied: astunparse>=1.6.0 in c:\users\appdata\roaming\python\python39\site-packages (from tensorflow-intel==2.12.0->tensorflow->pyESD) (1.6.3)
Requirement already satisfied: opt-einsum>=2.3.2 in c:\users\appdata\roaming\python\python39\site-packages (from tensorflow-intel==2.12.0->tensorflow->pyESD) (3.3.0)
Requirement already satisfied: google-pasta>=0.1.1 in c:\users\appdata\roaming\python\python39\site-packages (from tensorflow-intel==2.12.0->tensorflow->pyESD) (0.2.0)
Requirement already satisfied: gast<=0.4.0,>=0.2.1 in c:\users\appdata\roaming\python\python39\site-packages (from tensorflow-intel==2.12.0->tensorflow->pyESD) (0.4.0)
Requirement already satisfied: tensorboard<2.13,>=2.12 in c:\users\appdata\roaming\python\python39\site-packages (from tensorflow-intel==2.12.0->tensorflow->pyESD) (2.12.3)
Requirement already satisfied: termcolor>=1.1.0 in c:\users\appdata\roaming\python\python39\site-packages (from tensorflow-intel==2.12.0->tensorflow->pyESD) (2.3.0)
Requirement already satisfied: wheel<1.0,>=0.23.0 in c:\windows\anaconda3-2021.11\lib\site-packages (from astunparse>=1.6.0->tensorflow-intel==2.12.0->tensorflow->pyESD) (0.37.0)
Requirement already satisfied: ml-dtypes>=0.1.0 in c:\users\appdata\roaming\python\python39\site-packages (from jax>=0.3.15->tensorflow-intel==2.12.0->tensorflow->pyESD) (0.1.0)
Requirement already satisfied: tensorboard-data-server<0.8.0,>=0.7.0 in c:\users\appdata\roaming\python\python39\site-packages (from tensorboard<2.13,>=2.12->tensorflow-intel==2.12.0->tensorflow->pyESD) (0.7.0)
Requirement already satisfied: requests<3,>=2.21.0 in c:\windows\anaconda3-2021.11\lib\site-packages (from tensorboard<2.13,>=2.12->tensorflow-intel==2.12.0->tensorflow->pyESD) (2.26.0)
Requirement already satisfied: google-auth-oauthlib<1.1,>=0.5 in c:\users\appdata\roaming\python\python39\site-packages (from tensorboard<2.13,>=2.12->tensorflow-intel==2.12.0->tensorflow->pyESD) (1.0.0)
Requirement already satisfied: werkzeug>=1.0.1 in c:\windows\anaconda3-2021.11\lib\site-packages (from tensorboard<2.13,>=2.12->tensorflow-intel==2.12.0->tensorflow->pyESD) (2.0.2)
Requirement already satisfied: markdown>=2.6.8 in c:\users\appdata\roaming\python\python39\site-packages (from tensorboard<2.13,>=2.12->tensorflow-intel==2.12.0->tensorflow->pyESD) (3.4.3)
Requirement already satisfied: google-auth<3,>=1.6.3 in c:\users\appdata\roaming\python\python39\site-packages (from tensorboard<2.13,>=2.12->tensorflow-intel==2.12.0->tensorflow->pyESD) (2.19.0)
Requirement already satisfied: rsa<5,>=3.1.4 in c:\users\appdata\roaming\python\python39\site-packages (from google-auth<3,>=1.6.3->tensorboard<2.13,>=2.12->tensorflow-intel==2.12.0->tensorflow->pyESD) (4.9)
Requirement already satisfied: cachetools<6.0,>=2.0.0 in c:\users\appdata\roaming\python\python39\site-packages (from google-auth<3,>=1.6.3->tensorboard<2.13,>=2.12->tensorflow-intel==2.12.0->tensorflow->pyESD) (5.3.0)
Requirement already satisfied: pyasn1-modules>=0.2.1 in c:\users\appdata\roaming\python\python39\site-packages (from google-auth<3,>=1.6.3->tensorboard<2.13,>=2.12->tensorflow-intel==2.12.0->tensorflow->pyESD) (0.3.0)
Requirement already satisfied: urllib3<2.0 in c:\windows\anaconda3-2021.11\lib\site-packages (from google-auth<3,>=1.6.3->tensorboard<2.13,>=2.12->tensorflow-intel==2.12.0->tensorflow->pyESD) (1.26.7)
Requirement already satisfied: requests-oauthlib>=0.7.0 in c:\users\appdata\roaming\python\python39\site-packages (from google-auth-oauthlib<1.1,>=0.5->tensorboard<2.13,>=2.12->tensorflow-intel==2.12.0->tensorflow->pyESD) (1.3.1)
Requirement already satisfied: importlib-metadata>=4.4 in c:\windows\anaconda3-2021.11\lib\site-packages (from markdown>=2.6.8->tensorboard<2.13,>=2.12->tensorflow-intel==2.12.0->tensorflow->pyESD) (4.8.1)
Requirement already satisfied: zipp>=0.5 in c:\windows\anaconda3-2021.11\lib\site-packages (from importlib-metadata>=4.4->markdown>=2.6.8->tensorboard<2.13,>=2.12->tensorflow-intel==2.12.0->tensorflow->pyESD) (3.6.0)
Requirement already satisfied: pyasn1<0.6.0,>=0.4.6 in c:\users\appdata\roaming\python\python39\site-packages (from pyasn1-modules>=0.2.1->google-auth<3,>=1.6.3->tensorboard<2.13,>=2.12->tensorflow-intel==2.12.0->tensorflow->pyESD) (0.5.0)
Requirement already satisfied: idna<4,>=2.5 in c:\windows\anaconda3-2021.11\lib\site-packages (from requests<3,>=2.21.0->tensorboard<2.13,>=2.12->tensorflow-intel==2.12.0->tensorflow->pyESD) (3.2)
Requirement already satisfied: charset-normalizer~=2.0.0 in c:\windows\anaconda3-2021.11\lib\site-packages (from requests<3,>=2.21.0->tensorboard<2.13,>=2.12->tensorflow-intel==2.12.0->tensorflow->pyESD) (2.0.4)
Requirement already satisfied: certifi>=2017.4.17 in c:\windows\anaconda3-2021.11\lib\site-packages (from requests<3,>=2.21.0->tensorboard<2.13,>=2.12->tensorflow-intel==2.12.0->tensorflow->pyESD) (2021.10.8)
Requirement already satisfied: oauthlib>=3.0.0 in c:\users\appdata\roaming\python\python39\site-packages (from requests-oauthlib>=0.7.0->google-auth-oauthlib<1.1,>=0.5->tensorboard<2.13,>=2.12->tensorflow-intel==2.12.0->tensorflow->pyESD) (3.2.2)

Tried in spyder as well.
Doesn't work there too.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.