Skip to content

Commit

Permalink
add pymannkendall to dependencies, formatting fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Zeitsperre committed Oct 12, 2023
1 parent 2a1a5eb commit 2b8bc3a
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 13 deletions.
1 change: 1 addition & 0 deletions environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ channels:
dependencies:
- python >=3.9,<3.12
- numpy
- pymannkendall
- scipy
- statsmodels
- xarray
Expand Down
10 changes: 9 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,15 @@
with open("README.rst") as readme_file:
readme = readme_file.read()

requirements = ["numpy", "scipy", "statsmodels", "xarray", "xclim>=0.45.0", "xscen"]
requirements = [
"numpy",
"pymannkendall",
"scipy",
"statsmodels",
"xarray",
"xclim>=0.45.0",
"xscen",
]

dev_requirements = ["pytest", "pytest-cov"]

Expand Down
45 changes: 33 additions & 12 deletions xhydro/indicators.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
"""Module to compute indicators using xclim's build_indicator_module_from_yaml."""
import warnings
from typing import List, Optional

import numpy as np
import pymannkendall
import xarray as xr
import xclim as xc
import xscen as xs
import pymannkendall
import numpy as np
from xclim.core.units import rate2amount

# Special imports from xscen
Expand Down Expand Up @@ -242,19 +243,39 @@ def get_yearly_op(

return out

def _pval_mannkendall_ufunc(da,
test,
outputs):

def _pval_mannkendall_ufunc(da, test, outputs):
da = da[~np.isnan(da)]
pmk = getattr(pymannkendall, test)
mk = pmk(da)
return tuple([getattr(mk, out) for out in outputs])

def mannkendall(da,
outputs: list = ['h', 'p'],
test: str ='original_test'):
try:
return xr.concat(xr.apply_ufunc(_pval_mannkendall_ufunc, da, test, outputs, input_core_dims=[['time'],[], ['dim0']],output_core_dims=np.empty((len(outputs), 0)).tolist(), vectorize=True) , dim='mk').assign_coords(mk=outputs)
except:
return xr.apply_ufunc(_pval_mannkendall_ufunc, da, test, outputs, input_core_dims=[['time'],[], ['dim0']],output_core_dims=np.empty((len(outputs), 0)).tolist(), output_dtypes=[tuple],vectorize=True).assign_coords(mk=outputs)

def mannkendall(da, outputs: Optional[list[str]] = None, test: str = "original_test"):
if outputs is None:
outputs = ["h", "p"]
try:
return xr.concat(
xr.apply_ufunc(
_pval_mannkendall_ufunc,
da,
test,
outputs,
input_core_dims=[["time"], [], ["dim0"]],
output_core_dims=np.empty((len(outputs), 0)).tolist(),
vectorize=True,
),
dim="mk",
).assign_coords(mk=outputs)
# FIXME: Are these the only errors that can be raised?
except (ValueError, TypeError):
return xr.apply_ufunc(
_pval_mannkendall_ufunc,
da,
test,
outputs,
input_core_dims=[["time"], [], ["dim0"]],
output_core_dims=np.empty((len(outputs), 0)).tolist(),
output_dtypes=[tuple],
vectorize=True,
).assign_coords(mk=outputs)

0 comments on commit 2b8bc3a

Please sign in to comment.