Skip to content

Commit

Permalink
refactor testing utilities to not rely on xclim, name pooch class gosset
Browse files Browse the repository at this point in the history
  • Loading branch information
Zeitsperre committed Nov 7, 2024
1 parent 04d5243 commit a59ed05
Show file tree
Hide file tree
Showing 9 changed files with 410 additions and 155 deletions.
12 changes: 6 additions & 6 deletions src/xsdba/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,23 @@
import importlib.util
import warnings

from . import adjustment, base, detrending, processing, testing, units, utils
from xsdba import adjustment, base, detrending, processing, units, utils

xclim_installed = importlib.util.find_spec("xclim") is not None
if not xclim_installed:
warnings.warn(
"Sub-modules `properties` and `measures` depend on `xclim`. Run `pip install xsdba['extras']` to install it."
)
else:
from . import (
from xsdba import (
measures,
properties,
)

from .adjustment import *
from .base import Grouper
from .options import set_options
from .processing import stack_variables, unstack_variables
from xsdba.adjustment import *
from xsdba.base import Grouper
from xsdba.options import set_options
from xsdba.processing import stack_variables, unstack_variables

# TODO: ISIMIP ? Used for precip freq adjustment in biasCorrection.R
# Hempel, S., Frieler, K., Warszawski, L., Schewe, J., & Piontek, F. (2013). A trend-preserving bias correction –
Expand Down
6 changes: 6 additions & 0 deletions src/xsdba/testing/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
"""Testing utilities for xsdba."""

from __future__ import annotations

from xsdba.testing import utils
from xsdba.testing.helpers import *
51 changes: 7 additions & 44 deletions src/xsdba/testing.py → src/xsdba/testing/helpers.py
Original file line number Diff line number Diff line change
@@ -1,52 +1,22 @@
"""Testing utilities for xsdba."""
"""Helper functions for testing purposes."""

from __future__ import annotations

import collections
import hashlib
import logging
import os
import warnings
from pathlib import Path
from urllib.error import HTTPError, URLError
from urllib.parse import urljoin, urlparse
from urllib.request import urlopen, urlretrieve

import numpy as np
import pandas as pd
import xarray as xr
from platformdirs import user_cache_dir
from scipy.stats import gamma
from xarray import open_dataset as _open_dataset

from xsdba.utils import equally_spaced_nodes

__all__ = ["nancov", "test_timelonlatseries", "test_timeseries"]

# keeping xclim-testdata for now, since it's still this on gitHub
_default_cache_dir = Path(user_cache_dir("xclim-testdata"))

# XC
TESTDATA_BRANCH = os.getenv("XCLIM_TESTDATA_BRANCH", "main")
"""Sets the branch of Ouranosinc/xclim-testdata to use when fetching testing datasets.
Notes
-----
When running tests locally, this can be set for both `pytest` and `tox` by exporting the variable:
.. code-block:: console
$ export XCLIM_TESTDATA_BRANCH="my_testing_branch"
or setting the variable at runtime:
.. code-block:: console
$ env XCLIM_TESTDATA_BRANCH="my_testing_branch" pytest
"""

logger = logging.getLogger("xsdba")
__all__ = [
"test_cannon_2015_dist",
"test_cannon_2015_rvs",
"test_timelonlatseries",
"test_timeseries",
]


def test_cannon_2015_dist(): # noqa: D103
Expand Down Expand Up @@ -116,10 +86,3 @@ def test_timeseries(
return da.to_dataset()
else:
return da


# XC
def nancov(X):
"""Drop observations with NaNs from Numpy's cov."""
X_na = np.isnan(X).any(axis=0)
return np.cov(X[:, ~X_na])
4 changes: 4 additions & 0 deletions src/xsdba/testing/registry.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
sdba/CanESM2_1950-2100.nc sha256:b41fe603676e70d16c747ec207eb75ec86a39b665de401dcb23b5969ab3e1b32
sdba/adjusted_external.nc sha256:ff325c88eca96844bc85863744e4e08bcdf3d257388255636427ad5e11960d2e
sdba/ahccd_1950-2013.nc sha256:7e9a1f61c1d04ca257b09857a82715f1fa3f0550d77f97b7306d4eaaf0c70239
sdba/nrcan_1950-2013.nc sha256:4ce2dcfdac09b028db0f3e348272a496d796c36d4f3c4a412ebcca11449b7237
Loading

0 comments on commit a59ed05

Please sign in to comment.