Skip to content

Commit

Permalink
Merge branch 'split_from_xclim' of github.com:Ouranosinc/xsdba into s…
Browse files Browse the repository at this point in the history
…plit_from_xclim
  • Loading branch information
coxipi committed Jul 27, 2024
2 parents fa3f64e + 8dcb0fb commit 9914b03
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 55 deletions.
5 changes: 3 additions & 2 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ Credits
Development Lead
----------------

* Trevor James Smith <smith.trevorj@ouranos.ca> `@Zeitsperre <https://github.com/Zeitsperre>`_
* Éric Dupuis <dupuis.eric@ouranos.ca> `@coxipi <https://github.com/coxipi>`_

Co-Developers
-------------

* Éric Dupuis <[email protected]> `@coxipi <https://github.com/coxipi>`_
* Pascal Bourgault <[email protected]> `@aulemahal <https://github.com/aulemahal>`_
* Trevor James Smith <[email protected]> `@Zeitsperre <https://github.com/Zeitsperre>`_

Contributors
------------
Expand Down
4 changes: 2 additions & 2 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Changelog
`Unreleased <https://github.com/Ouranosinc/xsdba>`_ (latest)
------------------------------------------------------------

Contributors:
Contributors: Éric Dupuis (:user:`coxipi`), Trevor James Smith (:user:`Zeitsperre`).

Changes
^^^^^^^
Expand All @@ -20,7 +20,7 @@ Fixes
`v0.1.0 <https://github.com/Ouranosinc/xsdba/tree/0.1.0>`_
----------------------------------------------------------

Contributors: Trevor James Smith (:user:`Ouranosinc`)
Contributors: Trevor James Smith (:user:`Zeitsperre`)

Changes
^^^^^^^
Expand Down
8 changes: 4 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ build-backend = "flit_core.buildapi"
[project]
name = "xsdba"
authors = [
{name = "Éric Dupuis", email = "[email protected]"},
{name = "Trevor James Smith", email = "[email protected]"}
]
maintainers = [
{name = "Éric Dupuis", email = "[email protected]"},
{name = "Trevor James Smith", email = "[email protected]"}
]
readme = {file = "README.rst", content-type = "text/x-rst"}
Expand All @@ -24,7 +26,6 @@ classifiers = [
"Operating System :: OS Independent",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3 :: Only",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
Expand Down Expand Up @@ -93,7 +94,6 @@ xsdba = "xsdba.cli:app"

[tool.black]
target-version = [
"py38",
"py39",
"py310",
"py311",
Expand Down Expand Up @@ -210,7 +210,7 @@ exclude = [

[tool.isort]
profile = "black"
py_version = 38
py_version = 39

[tool.mypy]
files = "."
Expand Down Expand Up @@ -273,7 +273,7 @@ usefixtures = "xdoctest_namespace"
[tool.ruff]
src = ["xsdba"]
line-length = 150
target-version = "py38"
target-version = "py39"
exclude = [
".eggs",
".git",
Expand Down
2 changes: 1 addition & 1 deletion src/xsdba/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

from __future__ import annotations

from . import base, utils, units, detrending, processing
from . import base, detrending, processing, units, utils

# , adjustment
# from . import adjustment, base, detrending, measures, processing, properties, utils
Expand Down
26 changes: 14 additions & 12 deletions src/xsdba/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,23 @@
from __future__ import annotations

import datetime as pydt
import itertools
from collections.abc import Sequence
from inspect import _empty, signature
from typing import Any, Callable, NewType, TypeVar
from pint import Quantity


import itertools

import cftime
import dask.array as dsk
import jsonpickle
import numpy as np
import pandas as pd
import xarray as xr
from boltons.funcutils import wraps
import pandas as pd
from pint import Quantity

from xsdba.options import OPTIONS, SDBA_ENCODE_CF


# XC:
# XC:
#: Type annotation for strings representing full dates (YYYY-MM-DD), may include time.
DateStr = NewType("DateStr", str)

Expand All @@ -35,6 +32,7 @@
#: Type annotation for thresholds and other not-exactly-a-variable quantities
Quantified = TypeVar("Quantified", xr.DataArray, str, Quantity)


# ## Base class for the sdba module
class Parametrizable(dict):
"""Helper base class resembling a dictionary.
Expand Down Expand Up @@ -117,7 +115,8 @@ def set_dataset(self, ds: xr.Dataset) -> None:
self.ds.attrs[self._attribute] = jsonpickle.encode(self)


# XC
# XC


def copy_all_attrs(ds: xr.Dataset | xr.DataArray, ref: xr.Dataset | xr.DataArray):
"""Copy all attributes of ds to ref, including attributes of shared coordinates, and variables in the case of Datasets."""
Expand All @@ -128,6 +127,7 @@ def copy_all_attrs(ds: xr.Dataset | xr.DataArray, ref: xr.Dataset | xr.DataArray
if name in others:
var.attrs.update(ref[name].attrs)


# XC put here to avoid circular import
def uses_dask(*das: xr.DataArray | xr.Dataset) -> bool:
r"""Evaluate whether dask is installed and array is loaded as a dask array.
Expand All @@ -153,7 +153,8 @@ def uses_dask(*das: xr.DataArray | xr.Dataset) -> bool:
return True
return False

# XC

# XC
# Maximum day of year in each calendar.
max_doy = {
"standard": 366,
Expand All @@ -167,7 +168,8 @@ def uses_dask(*das: xr.DataArray | xr.Dataset) -> bool:
"360_day": 360,
}

# XC

# XC
def parse_offset(freq: str) -> tuple[int, str, bool, str | None]:
"""Parse an offset string.
Expand Down Expand Up @@ -208,6 +210,7 @@ def parse_offset(freq: str) -> tuple[int, str, bool, str | None]:
anchor = None
return mult, base, start, anchor


# XC put here to avoid circular import
def get_calendar(obj: Any, dim: str = "time") -> str:
"""Return the calendar of an object.
Expand Down Expand Up @@ -251,7 +254,7 @@ def get_calendar(obj: Any, dim: str = "time") -> str:
raise ValueError(f"Calendar could not be inferred from object of type {type(obj)}.")


# XC
# XC
def gen_call_string(funcname: str, *args, **kwargs) -> str:
r"""Generate a signature string for use in the history attribute.
Expand Down Expand Up @@ -294,7 +297,6 @@ def gen_call_string(funcname: str, *args, **kwargs) -> str:
return f"{funcname}({', '.join(elements)})"



class Grouper(Parametrizable):
"""Grouper inherited class for parameterizable classes."""

Expand Down
2 changes: 1 addition & 1 deletion src/xsdba/detrending.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""
"""# noqa: SS01
Detrending Objects Utilities
============================
"""
Expand Down
15 changes: 10 additions & 5 deletions src/xsdba/formatting.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
"""
Formatting Utilities
Formatting Utilities
===================================
"""

from __future__ import annotations

import datetime as dt
import itertools
from inspect import signature

import xarray as xr
from boltons.funcutils import wraps
from inspect import signature
import itertools


# XC
def merge_attributes(
Expand Down Expand Up @@ -63,6 +66,7 @@ def merge_attributes(
return merged_attr[: -len(new_line)] # Remove the last added new_line
return merged_attr


# XC
def update_history(
hist_str: str,
Expand Down Expand Up @@ -115,7 +119,8 @@ def update_history(
)
return merged_history

# XC

# XC
def update_xsdba_history(func: Callable):
"""Decorator that auto-generates and fills the history attribute.
Expand Down Expand Up @@ -199,4 +204,4 @@ def gen_call_string(funcname: str, *args, **kwargs) -> str:

elements.append(rep)

return f"{funcname}({', '.join(elements)})"
return f"{funcname}({', '.join(elements)})"
24 changes: 9 additions & 15 deletions src/xsdba/processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,17 @@
import numpy as np
import xarray as xr
from xarray.core.utils import get_temp_dimname

from xsdba.base import get_calendar, max_doy, parse_offset, uses_dask
from xsdba.formatting import update_xsdba_history
# from xclim.core.units import convert_units_to, infer_context, units

from ._processing import _adapt_freq, _normalize, _reordering
from .base import Grouper
from .nbutils import _escore
from .units import check_units, convert_units_to, harmonize_units
from .utils import ADDITIVE, copy_all_attrs
from .units import check_units, harmonize_units, convert_units_to

# from xclim.core.units import convert_units_to, infer_context, units


__all__ = [
Expand Down Expand Up @@ -227,9 +229,7 @@ def jitter(
jitter_dist = np.random.uniform(
low=jitter_min, high=jitter_lower, size=x.shape
)
out = out.where(
~((x < jitter_lower) & notnull), jitter_dist.astype(x.dtype)
)
out = out.where(~((x < jitter_lower) & notnull), jitter_dist.astype(x.dtype))
if upper is not None:
if maximum is None:
raise ValueError("If 'upper' is given, so must 'maximum'.")
Expand All @@ -243,16 +243,14 @@ def jitter(
jitter_dist = np.random.uniform(
low=jitter_upper, high=jitter_max, size=x.shape
)
out = out.where(
~((x >= jitter_upper) & notnull), jitter_dist.astype(x.dtype)
)
out = out.where(~((x >= jitter_upper) & notnull), jitter_dist.astype(x.dtype))

copy_all_attrs(out, x) # copy attrs and same units
return out


@update_xsdba_history
@harmonize_units(["data","norm"])
@harmonize_units(["data", "norm"])
def normalize(
data: xr.DataArray,
norm: xr.DataArray | None = None,
Expand Down Expand Up @@ -577,9 +575,7 @@ def to_additive_space(
# with units.context(infer_context(data.attrs.get("standard_name"))):
lower_bound_array = np.array(lower_bound).astype(float)
if upper_bound is not None:
upper_bound_array = np.array(upper_bound).astype(
float
)
upper_bound_array = np.array(upper_bound).astype(float)

with xr.set_options(keep_attrs=True), np.errstate(divide="ignore"):
if trans == "log":
Expand Down Expand Up @@ -698,9 +694,7 @@ def from_additive_space(
# FIXME: convert_units_to is causing issues since it can't handle all variations of Quantified here
lower_bound_array = np.array(lower_bound).astype(float)
if trans == "logit":
upper_bound_array = np.array(upper_bound).astype(
float
)
upper_bound_array = np.array(upper_bound).astype(float)
else:
raise ValueError(
"Parameters missing. Either all parameters are given as attributes of data, "
Expand Down
5 changes: 3 additions & 2 deletions src/xsdba/units.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""
"""# noqa: SS01
Units Handling Submodule
========================
"""
Expand Down Expand Up @@ -115,6 +115,7 @@ def str2pint(val: str) -> pint.Quantity:


def extract_units(arg):
"""Extract units from a string, DataArray, or scalar."""
if not (isinstance(arg, (str, xr.DataArray)) or np.isscalar(arg)):
print(arg)
raise TypeError(f"Argument must be a str, DataArray, or scalar. Got {type(arg)}")
Expand All @@ -129,7 +130,7 @@ def extract_units(arg):


def check_units(args_to_check):
"""Check that units are compatible with dimensions, otherwise raise a `ValidationError`."""
"""Decorator to check that all arguments have the same units (or no units)."""
# if no units are present (DataArray without units attribute or float), then no check is performed
# if units are present, then check is performed
# in mixed cases, an error is raised
Expand Down
3 changes: 2 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@
from filelock import FileLock
from packaging.version import Version

from xsdba.testing import TESTDATA_BRANCH, test_timelonlatseries, test_timeseries
from xsdba.testing import TESTDATA_BRANCH
from xsdba.testing import open_dataset as _open_dataset
from xsdba.testing import test_timelonlatseries, test_timeseries

# import xclim
# from xclim import __version__ as __xclim_version__
Expand Down
Loading

0 comments on commit 9914b03

Please sign in to comment.