Skip to content

Commit

Permalink
SBCK added & tests
Browse files Browse the repository at this point in the history
  • Loading branch information
coxipi committed Aug 1, 2024
1 parent 45ec1b6 commit 47cd132
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 83 deletions.
11 changes: 3 additions & 8 deletions src/xsdba/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,18 @@
units,
utils,
)

# , adjustment
# from . import adjustment, base, detrending, measures, processing, properties, utils
from .adjustment import *
from .base import Grouper
from .options import set_options
from .processing import stack_variables, unstack_variables

# from .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 –
# The ISI-MIP approach. Earth System Dynamics, 4(2), 219–236. https://doi.org/10.5194/esd-4-219-2013
# If SBCK is installed, create adjustment classes wrapping SBCK's algorithms.
# if hasattr(adjustment, "_generate_SBCK_classes"):
# for cls in adjustment._generate_SBCK_classes():
# adjustment.__dict__[cls.__name__] = cls
if hasattr(adjustment, "_generate_SBCK_classes"):
for cls in adjustment._generate_SBCK_classes():
adjustment.__dict__[cls.__name__] = cls

__author__ = """Trevor James Smith"""
__email__ = "smith.trevorj@ouranos.ca"
Expand Down
150 changes: 75 additions & 75 deletions tests/test_adjustment.py
Original file line number Diff line number Diff line change
Expand Up @@ -856,78 +856,78 @@ def test_real_data(self, open_dataset, use_dask, group, window, period_dim):
p.load()


# class TestSBCKutils:
# @pytest.mark.slow
# @pytest.mark.parametrize(
# "method",
# [m for m in dir(adjustment) if m.startswith("SBCK_")],
# )
# @pytest.mark.parametrize("use_dask", [True]) # do we gain testing both?
# def test_sbck(self, method, use_dask, random):
# SBCK = pytest.importorskip("SBCK", minversion="0.4.0")

# n = 10 * 365
# m = 2 # A dummy dimension to test vectorization.
# ref_y = norm.rvs(loc=10, scale=1, size=(m, n), random_state=random)
# ref_x = norm.rvs(loc=3, scale=2, size=(m, n), random_state=random)
# hist_x = norm.rvs(loc=11, scale=1.2, size=(m, n), random_state=random)
# hist_y = norm.rvs(loc=4, scale=2.2, size=(m, n), random_state=random)
# sim_x = norm.rvs(loc=12, scale=2, size=(m, n), random_state=random)
# sim_y = norm.rvs(loc=3, scale=1.8, size=(m, n), random_state=random)

# ref = xr.Dataset(
# {
# "tasmin": xr.DataArray(
# ref_x, dims=("lon", "time"), attrs={"units": "degC"}
# ),
# "tasmax": xr.DataArray(
# ref_y, dims=("lon", "time"), attrs={"units": "degC"}
# ),
# }
# )
# ref["time"] = xr.cftime_range("1990-01-01", periods=n, calendar="noleap")

# hist = xr.Dataset(
# {
# "tasmin": xr.DataArray(
# hist_x, dims=("lon", "time"), attrs={"units": "degC"}
# ),
# "tasmax": xr.DataArray(
# hist_y, dims=("lon", "time"), attrs={"units": "degC"}
# ),
# }
# )
# hist["time"] = ref["time"]

# sim = xr.Dataset(
# {
# "tasmin": xr.DataArray(
# sim_x, dims=("lon", "time"), attrs={"units": "degC"}
# ),
# "tasmax": xr.DataArray(
# sim_y, dims=("lon", "time"), attrs={"units": "degC"}
# ),
# }
# )
# sim["time"] = xr.cftime_range("2090-01-01", periods=n, calendar="noleap")

# if use_dask:
# ref = ref.chunk({"lon": 1})
# hist = hist.chunk({"lon": 1})
# sim = sim.chunk({"lon": 1})

# if "TSMBC" in method:
# kws = {"lag": 1}
# elif "MBCn" in method:
# kws = {"metric": SBCK.metrics.energy}
# else:
# kws = {}

# scen = getattr(adjustment, method).adjust(
# stack_variables(ref),
# stack_variables(hist),
# stack_variables(sim),
# multi_dim="multivar",
# **kws,
# )
# unstack_variables(scen).load()
class TestSBCKutils:
@pytest.mark.slow
@pytest.mark.parametrize(
"method",
[m for m in dir(adjustment) if m.startswith("SBCK_")],
)
@pytest.mark.parametrize("use_dask", [True]) # do we gain testing both?
def test_sbck(self, method, use_dask, random):
SBCK = pytest.importorskip("SBCK", minversion="0.4.0")

n = 10 * 365
m = 2 # A dummy dimension to test vectorization.
ref_y = norm.rvs(loc=10, scale=1, size=(m, n), random_state=random)
ref_x = norm.rvs(loc=3, scale=2, size=(m, n), random_state=random)
hist_x = norm.rvs(loc=11, scale=1.2, size=(m, n), random_state=random)
hist_y = norm.rvs(loc=4, scale=2.2, size=(m, n), random_state=random)
sim_x = norm.rvs(loc=12, scale=2, size=(m, n), random_state=random)
sim_y = norm.rvs(loc=3, scale=1.8, size=(m, n), random_state=random)

ref = xr.Dataset(
{
"tasmin": xr.DataArray(
ref_x, dims=("lon", "time"), attrs={"units": "degC"}
),
"tasmax": xr.DataArray(
ref_y, dims=("lon", "time"), attrs={"units": "degC"}
),
}
)
ref["time"] = xr.cftime_range("1990-01-01", periods=n, calendar="noleap")

hist = xr.Dataset(
{
"tasmin": xr.DataArray(
hist_x, dims=("lon", "time"), attrs={"units": "degC"}
),
"tasmax": xr.DataArray(
hist_y, dims=("lon", "time"), attrs={"units": "degC"}
),
}
)
hist["time"] = ref["time"]

sim = xr.Dataset(
{
"tasmin": xr.DataArray(
sim_x, dims=("lon", "time"), attrs={"units": "degC"}
),
"tasmax": xr.DataArray(
sim_y, dims=("lon", "time"), attrs={"units": "degC"}
),
}
)
sim["time"] = xr.cftime_range("2090-01-01", periods=n, calendar="noleap")

if use_dask:
ref = ref.chunk({"lon": 1})
hist = hist.chunk({"lon": 1})
sim = sim.chunk({"lon": 1})

if "TSMBC" in method:
kws = {"lag": 1}
elif "MBCn" in method:
kws = {"metric": SBCK.metrics.energy}
else:
kws = {}

scen = getattr(adjustment, method).adjust(
stack_variables(ref),
stack_variables(hist),
stack_variables(sim),
multi_dim="multivar",
**kws,
)
unstack_variables(scen).load()

0 comments on commit 47cd132

Please sign in to comment.