Skip to content

Commit

Permalink
Merge pull request #23 from martinvonk/dev
Browse files Browse the repository at this point in the history
Update main to v0.3.2
  • Loading branch information
martinvonk authored Feb 23, 2023
2 parents ef8c6dc + 62a4e20 commit a35ccc4
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 29 deletions.
15 changes: 15 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ jobs:
python: "3.9"
os: ubuntu-latest
toxenv: lint
- name: Codacy Coverage Report
python: "3.9"
os: ubuntu-latest
toxenv: coverage

name: ${{ matrix.name }}
env:
Expand All @@ -55,16 +59,27 @@ jobs:
PYTEST_ADDOPTS: "--color=yes"
steps:
- uses: actions/checkout@v3

- name: Set up Python ${{ matrix.python }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python }}
check-latest: true

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install tox
- name: Setup tox environment
run: tox -e ${{ matrix.toxenv }} --notest

- name: Test
run: tox -e ${{ matrix.toxenv }} --skip-pkg-install

- name: Run codacy-coverage-reporter
if: ${{ matrix.toxenv == 'coverage' && github.repository == 'martinvonk/spei' && success() }}
uses: codacy/codacy-coverage-reporter-action@master
with:
project-token: ${{ secrets.CODACY_PROJECT_TOKEN }}
coverage-reports: coverage.xml
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@
/.mypy_cache
/.tox
/.pytest_cache
/htmlcov
/htmlcov
coverage.xml
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
![PyPI](https://img.shields.io/pypi/v/spei?style=flat-square) ![PyPi Supported Python Versions](https://img.shields.io/pypi/pyversions/spei?style=flat-square) ![Code Size](https://img.shields.io/github/languages/code-size/martinvonk/spei?style=flat-square) ![PyPi Downloads](https://img.shields.io/pypi/dm/spei?style=flat-square) ![License](https://img.shields.io/pypi/l/spei?style=flat-square)

![Tests](https://img.shields.io/github/actions/workflow/status/martinvonk/spei/tests.yml?style=flat-square)
![CodacyGrade](https://img.shields.io/codacy/grade/908b566912314666b84e1add22ea7d66?style=flat-square)
![CodacyCoverage](https://img.shields.io/codacy/coverage/908b566912314666b84e1add22ea7d66?style=flat-square)
![MyPy](https://img.shields.io/badge/%20type_checker-mypy-%231674b1?style=flat-square)
![Black](https://img.shields.io/badge/code%20style-black-black?style=flat-square)

Expand Down Expand Up @@ -32,11 +34,11 @@ To get the development version download the GitHub code to your computer. Use cd
`pip install -e .`

## Literature
1. B. Lloyd-Hughes and M.A. Saunders (2002) - A Drought Climatology for Europe. DOI: 10.1002/joc.846
2. S.M. Vicente-Serrano, S. Beguería and J.I. López-Moreno (2010) - A Multi-scalar drought index sensitive to global warming: The Standardized Precipitation Evapotranspiration Index. DOI: 10.1175/2009JCLI2909.1
3. J.P.Bloomfield and B.P. Marchant, B. P. (2013) - Analysis of groundwater drought building on the standardised precipitation index approach. DOI: 10.5194/hess-17-4769-2013
4. A. Babre, A. Kalvāns, Z. Avotniece, I. Retiķe, J. Bikše, K.P.M. Jemeljanova, A. Zelenkevičs and A. Dēliņa (2022) - The use of predefined drought indices for the assessment of groundwater drought episodes in the Baltic States over the period 1989–2018. DOI: 10.1016/j.ejrh.2022.101049
5. E. Tijdeman, K. Stahl and L.M. Tallaksen (2020) - Drought characteristics derived based on the Standardized Streamflow Index: A large sample comparison for parametric and nonparametric methods. DOI: 10.1029/2019WR026315
1. B. Lloyd-Hughes and M.A. Saunders (2002) - A Drought Climatology for Europe. DOI: 10.1002/joc.846
2. S.M. Vicente-Serrano, S. Beguería and J.I. López-Moreno (2010) - A Multi-scalar drought index sensitive to global warming: The Standardized Precipitation Evapotranspiration Index. DOI: 10.1175/2009JCLI2909.1
3. J.P.Bloomfield and B.P. Marchant, B. P. (2013) - Analysis of groundwater drought building on the standardised precipitation index approach. DOI: 10.5194/hess-17-4769-2013
4. A. Babre, A. Kalvāns, Z. Avotniece, I. Retiķe, J. Bikše, K.P.M. Jemeljanova, A. Zelenkevičs and A. Dēliņa (2022) - The use of predefined drought indices for the assessment of groundwater drought episodes in the Baltic States over the period 1989–2018. DOI: 10.1016/j.ejrh.2022.101049
5. E. Tijdeman, K. Stahl and L.M. Tallaksen (2020) - Drought characteristics derived based on the Standardized Streamflow Index: A large sample comparison for parametric and nonparametric methods. DOI: 10.1029/2019WR026315

Note that the method for calculating the drought indices does not come from these articles and SciPy is used for deriving the distribution. However the literature is helpful as a reference to understand the context and application of drought indices.

Expand Down
11 changes: 10 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ linting = ["flake8"]
formatting = ["black[jupyter]", "isort"]
typing = ["mypy", "pandas-stubs"]
pytesting = ["pytest>=7", "pytest-cov", "pytest-sugar"]
dev = ["spei[linting,formatting,typing,pytesting]"]
coveraging = ["coverage"]
dev = ["spei[linting,formatting,typing,pytesting,coveraging]"]

[tool.setuptools.dynamic]
version = { attr = "spei._version.__version__" }
Expand Down Expand Up @@ -86,4 +87,12 @@ legacy_tox_ini = """
extras = linting
commands =
flake8 src --max-line-length=88
[testenv:coverage]
description = get coverage report xml
basepython = 3.9
extras = coveraging,pytesting
commands =
coverage run -m pytest tests
coverage xml
"""
6 changes: 4 additions & 2 deletions src/spei/_typing.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# flake8: noqa
from typing import Any, Union

from matplotlib.axes import Axes
from matplotlib.axes import Axes as mplAxes
from numpy import float64
from numpy.typing import NDArray
from scipy.stats._continuous_distns import rv_continuous

ContinuousDist = Union[Any, rv_continuous]
Axes = Union[Any, Axes]
Axes = Union[Any, mplAxes]
NDArrayAxes = NDArray[Axes]
NDArrayFloat = NDArray[float64]
2 changes: 1 addition & 1 deletion src/spei/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.3.1"
__version__ = "0.3.2"
8 changes: 4 additions & 4 deletions src/spei/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from pandas import Series
from scipy.stats import gaussian_kde

from ._typing import Axes, ContinuousDist, NDArray, float64
from ._typing import Axes, ContinuousDist, NDArrayAxes
from .utils import dist_test, validate_index, validate_series


Expand Down Expand Up @@ -40,9 +40,9 @@ def si(

nmin = -bound
nmax = bound
droughts = si.to_numpy(dtype=float64, copy=True)
droughts = si.to_numpy(dtype=float, copy=True)
droughts[droughts > 0] = 0
nodroughts = si.to_numpy(dtype=float64, copy=True)
nodroughts = si.to_numpy(dtype=float, copy=True)
nodroughts[nodroughts < 0] = 0

x, y = meshgrid(si.index, linspace(nmin, nmax, 100))
Expand All @@ -64,7 +64,7 @@ def dist(
cmap: Optional[str] = None,
figsize: tuple = (8, 10),
legend: bool = True,
) -> NDArray[Axes]:
) -> NDArrayAxes:
"""Plot the (cumulative) histogram and scipy fitted distribution
for the time series on a monthly basis.
Expand Down
14 changes: 7 additions & 7 deletions src/spei/si.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from pandas import DatetimeIndex, Series
from scipy.stats import fisk, gamma, genextreme, norm

from ._typing import ContinuousDist, NDArray, float64
from ._typing import ContinuousDist, NDArrayFloat
from .utils import validate_index, validate_series


Expand Down Expand Up @@ -42,7 +42,7 @@ def compute_si_ppf(
series = validate_series(series)
index = validate_index(series.index)

si = Series(index=index, dtype=float64)
si = Series(index=index, dtype=float)
for month in range(1, 13):
data = series[index.month == month].sort_values()
if not sgi:
Expand All @@ -58,16 +58,16 @@ def compute_si_ppf(


def compute_cdf(
data: Union[Series, NDArray[float64]], dist: ContinuousDist
) -> NDArray[float64]:
data: Union[Series, NDArrayFloat], dist: ContinuousDist
) -> NDArrayFloat:
*pars, loc, scale = dist.fit(data, scale=std(data))
cdf = dist.cdf(data, pars, loc=loc, scale=scale)
return cdf


def compute_cdf_probzero(
data: Union[Series, NDArray[float64]], dist: ContinuousDist
) -> NDArray[float64]:
data: Union[Series, NDArrayFloat], dist: ContinuousDist
) -> NDArrayFloat:
p0 = (data == 0.0).sum() / len(data)
*pars, loc, scale = dist.fit(data[data != 0.0], scale=std(data))
cdf_sub = dist.cdf(data, pars, loc=loc, scale=scale)
Expand All @@ -76,7 +76,7 @@ def compute_cdf_probzero(
return cdf


def compute_cdf_nsf(data: Union[Series, NDArray[float64]]) -> NDArray[float64]:
def compute_cdf_nsf(data: Union[Series, NDArrayFloat]) -> NDArrayFloat:
"""Normal Scores Transform"""
n = data.size
cdf = linspace(1 / (2 * n), 1 - 1 / (2 * n), n)
Expand Down
10 changes: 5 additions & 5 deletions src/spei/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
pearson3,
)

from ._typing import ContinuousDist, NDArray, float64
from ._typing import ContinuousDist, NDArrayFloat


def validate_series(series: Series) -> Series:
Expand Down Expand Up @@ -53,7 +53,7 @@ def validate_index(index: Index) -> DatetimeIndex:


def dist_test(
series: Union[Series, NDArray[float64]],
series: Union[Series, NDArrayFloat],
dist: ContinuousDist,
N: int = 100,
alpha: float = 0.05,
Expand All @@ -66,7 +66,7 @@ def dist_test(
Parameters
----------
data : Union[Series, NDArray[float64]]
data : Union[Series, NDArray[float]]
pandas Series or numpy array of floats of observations of random
variables
dist: scipy.stats.rv_continuous
Expand Down Expand Up @@ -100,7 +100,7 @@ def dist_test(


def dists_test(
series: Union[Series, NDArray[float64]],
series: Union[Series, NDArrayFloat],
distributions: Optional[List[ContinuousDist]] = None,
N: int = 100,
alpha: float = 0.05,
Expand All @@ -113,7 +113,7 @@ def dists_test(
Parameters
----------
series : Union[Series, NDArray[float64]]
series : Union[Series, NDArray[float]]
pandas Series with observations of random variables
distributions : list of scipy.stats.rv_continuous, optional
A list of (can be) any continuous distribution from the scipy.stats
Expand Down
2 changes: 0 additions & 2 deletions tests/test_dist_utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from numpy import random

from spei.utils import dists_test

from .fixtures import head
Expand Down
1 change: 0 additions & 1 deletion tests/test_plots.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import matplotlib as mpl
import pytest
from pandas import Series
from scipy.stats import pearson3

Expand Down

0 comments on commit a35ccc4

Please sign in to comment.