Skip to content

Commit

Permalink
update tests to use new functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Zeitsperre committed Nov 7, 2024
1 parent a59ed05 commit acb7baf
Show file tree
Hide file tree
Showing 11 changed files with 149 additions and 95 deletions.
5 changes: 5 additions & 0 deletions src/xsdba/testing/registry.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
cmip3/tas.sresb1.giss_model_e_r.run1.atm.da.nc sha256:e709552beeeccafcfe280759edf5477ae5241c698409ca051b0899c16e92c95e
ERA5/daily_surface_cancities_1990-1993.nc sha256:049d54ace3d229a96cc621189daa3e1a393959ab8d988221cfc7b2acd7ab94b2
NRCANdaily/nrcan_canada_daily_pr_1990.nc sha256:144479ec7a976cfecb6a10762d128a771356093d72caf5f075508ee86d25a1b0
NRCANdaily/nrcan_canada_daily_tasmax_1990.nc sha256:84880205b798740e37a102c7f40e595d7a4fde6e35fb737a1ef68b8dad447526
NRCANdaily/nrcan_canada_daily_tasmin_1990.nc sha256:13d61fc54cdcb4c1617ec777ccbf59575d8fdc24754f914042301bc1b024d7f7
sdba/CanESM2_1950-2100.nc sha256:b41fe603676e70d16c747ec207eb75ec86a39b665de401dcb23b5969ab3e1b32
sdba/adjusted_external.nc sha256:ff325c88eca96844bc85863744e4e08bcdf3d257388255636427ad5e11960d2e
sdba/ahccd_1950-2013.nc sha256:7e9a1f61c1d04ca257b09857a82715f1fa3f0550d77f97b7306d4eaaf0c70239
Expand Down
3 changes: 0 additions & 3 deletions tests/__init__.py

This file was deleted.

1 change: 0 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# noqa: D104
# XC: Many things deactivated, not sure what will be necessary
from __future__ import annotations

from pathlib import Path
Expand Down
28 changes: 15 additions & 13 deletions tests/test_adjustment.py
Original file line number Diff line number Diff line change
Expand Up @@ -632,24 +632,24 @@ def test_mon_u(

@pytest.mark.parametrize("use_dask", [True, False])
@pytest.mark.filterwarnings("ignore::RuntimeWarning")
def test_add_dims(self, use_dask, open_dataset):
def test_add_dims(self, use_dask, gosset):
with set_options(sdba_encode_cf=use_dask):
if use_dask:
chunks = {"location": -1}
else:
chunks = None

dsim = open_dataset(
"sdba/CanESM2_1950-2100.nc",
dsim = xr.open_dataset(
gosset.fetch("sdba/CanESM2_1950-2100.nc"),
chunks=chunks,
drop_variables=["lat", "lon"],
).tasmax
hist = dsim.sel(time=slice("1981", "2010"))
sim = dsim.sel(time=slice("2041", "2070"))

ref = (
open_dataset(
"sdba/ahccd_1950-2013.nc",
xr.open_dataset(
gosset.fetch("sdba/ahccd_1950-2013.nc"),
chunks=chunks,
drop_variables=["lat", "lon"],
)
Expand Down Expand Up @@ -678,16 +678,16 @@ class TestMBCn:
@pytest.mark.parametrize("use_dask", [True, False])
@pytest.mark.parametrize("group, window", [["time", 1], ["time.dayofyear", 31]])
@pytest.mark.parametrize("period_dim", [None, "period"])
def test_simple(self, open_dataset, use_dask, group, window, period_dim):
def test_simple(self, use_dask, group, window, period_dim, gosset):
group, window, period_dim, use_dask = "time", 1, None, False
with set_options(sdba_encode_cf=use_dask):
if use_dask:
chunks = {"location": -1}
else:
chunks = None
ref, dsim = (
open_dataset(
f"sdba/{file}",
xr.open_dataset(
gosset.fetch(f"sdba/{file}"),
chunks=chunks,
drop_variables=["lat", "lon"],
)
Expand Down Expand Up @@ -767,8 +767,10 @@ def _group_assert(ds, dim):

@pytest.mark.parametrize("use_dask", [True, False])
@pytest.mark.parametrize("pcorient", ["full", "simple"])
def test_real_data(self, open_dataset, use_dask, pcorient):
atmosds = open_dataset("ERA5/daily_surface_cancities_1990-1993.nc")
def test_real_data(self, use_dask, pcorient, gosset):
atmosds = xr.open_dataset(
gosset.fetch("ERA5/daily_surface_cancities_1990-1993.nc")
)

ds0 = xr.Dataset(
{"tasmax": atmosds.tasmax, "tasmin": atmosds.tasmin, "tas": atmosds.tas}
Expand Down Expand Up @@ -862,9 +864,9 @@ def gen_testdata(c, s):
).sum()

@pytest.mark.slow
def test_real_data(self, open_dataset):
dsim = open_dataset("sdba/CanESM2_1950-2100.nc") # .chunk()
dref = open_dataset("sdba/ahccd_1950-2013.nc") # .chunk()
def test_real_data(self, gosset):
dsim = xr.open_dataset(gosset.fetch("sdba/CanESM2_1950-2100.nc")) # .chunk()
dref = xr.open_dataset(gosset.fetch("sdba/ahccd_1950-2013.nc")) # .chunk()
ref = dref.sel(time=slice("1950", "2009")).pr
hist = dsim.sel(time=slice("1950", "2009")).pr
# TODO: Do we want to include standard conversions in xsdba tests?
Expand Down
4 changes: 2 additions & 2 deletions tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,8 @@ def func(ds, *, group, lon=None):
func(xr.Dataset(dict(da0=da0)), group="time")

@pytest.mark.parametrize("use_dask", [True, False])
def test_dataarray_cfencode(self, use_dask):
ds = open_dataset("sdba/CanESM2_1950-2100.nc")
def test_dataarray_cfencode(self, use_dask, gosset):
ds = xr.open_dataset(gosset.fetch("sdba/CanESM2_1950-2100.nc"))
if use_dask:
ds = ds.chunk()

Expand Down
6 changes: 3 additions & 3 deletions tests/test_loess.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ def test_loess_nb(d, f, w, n, dx, exp):

@pytest.mark.slow
@pytest.mark.parametrize("use_dask", [True, False])
def test_loess_smoothing(use_dask, open_dataset):
tas = open_dataset(
"cmip3/tas.sresb1.giss_model_e_r.run1.atm.da.nc",
def test_loess_smoothing(use_dask, gosset):
tas = xr.open_dataset(
gosset.fetch("cmip3/tas.sresb1.giss_model_e_r.run1.atm.da.nc"),
chunks={"lat": 1} if use_dask else None,
).tas.isel(lon=0, time=slice(0, 740))
tas = tas.where(tas.time.dt.dayofyear != 360) # Put NaNs
Expand Down
94 changes: 70 additions & 24 deletions tests/test_measures.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,32 @@
from xsdba import measures


def test_bias(open_dataset):
sim = open_dataset("sdba/CanESM2_1950-2100.nc").sel(time="1950-01-01").tasmax
ref = open_dataset("sdba/nrcan_1950-2013.nc").sel(time="1950-01-01").tasmax
def test_bias(gosset):
sim = (
xr.open_dataset(gosset.fetch("sdba/CanESM2_1950-2100.nc"))
.sel(time="1950-01-01")
.tasmax
)
ref = (
xr.open_dataset(gosset.fetch("sdba/nrcan_1950-2013.nc"))
.sel(time="1950-01-01")
.tasmax
)
test = measures.bias(sim, ref).values
np.testing.assert_array_almost_equal(test, [[6.430237, 39.088974, 5.2402344]])


def test_relative_bias(open_dataset):
sim = open_dataset("sdba/CanESM2_1950-2100.nc").sel(time="1950-01-01").tasmax
ref = open_dataset("sdba/nrcan_1950-2013.nc").sel(time="1950-01-01").tasmax
def test_relative_bias(gosset):
sim = (
xr.open_dataset(gosset.fetch("sdba/CanESM2_1950-2100.nc"))
.sel(time="1950-01-01")
.tasmax
)
ref = (
xr.open_dataset(gosset.fetch("sdba/nrcan_1950-2013.nc"))
.sel(time="1950-01-01")
.tasmax
)
test = measures.relative_bias(sim, ref).values
np.testing.assert_array_almost_equal(test, [[0.02366494, 0.16392256, 0.01920133]])

Expand All @@ -32,36 +48,62 @@ def test_circular_bias():
np.testing.assert_array_almost_equal(test, [1, 1, 66, -1, -1, -66])


def test_ratio(open_dataset):
sim = open_dataset("sdba/CanESM2_1950-2100.nc").sel(time="1950-01-01").tasmax
ref = open_dataset("sdba/nrcan_1950-2013.nc").sel(time="1950-01-01").tasmax
def test_ratio(gosset):
sim = (
xr.open_dataset(gosset.fetch("sdba/CanESM2_1950-2100.nc"))
.sel(time="1950-01-01")
.tasmax
)
ref = (
xr.open_dataset(gosset.fetch("sdba/nrcan_1950-2013.nc"))
.sel(time="1950-01-01")
.tasmax
)
test = measures.ratio(sim, ref).values
np.testing.assert_array_almost_equal(test, [[1.023665, 1.1639225, 1.0192013]])


def test_rmse(open_dataset):
def test_rmse(gosset):
sim = (
open_dataset("sdba/CanESM2_1950-2100.nc").sel(time=slice("1950", "1953")).tasmax
xr.open_dataset(gosset.fetch("sdba/CanESM2_1950-2100.nc"))
.sel(time=slice("1950", "1953"))
.tasmax
)
ref = (
xr.open_dataset(gosset.fetch("sdba/nrcan_1950-2013.nc"))
.sel(time=slice("1950", "1953"))
.tasmax
)
ref = open_dataset("sdba/nrcan_1950-2013.nc").sel(time=slice("1950", "1953")).tasmax
test = measures.rmse(sim, ref).values
np.testing.assert_array_almost_equal(test, [5.4499755, 18.124086, 12.387193], 4)


def test_mae(open_dataset):
def test_mae(gosset):
sim = (
open_dataset("sdba/CanESM2_1950-2100.nc").sel(time=slice("1950", "1953")).tasmax
xr.open_dataset(gosset.fetch("sdba/CanESM2_1950-2100.nc"))
.sel(time=slice("1950", "1953"))
.tasmax
)
ref = (
xr.open_dataset(gosset.fetch("sdba/nrcan_1950-2013.nc"))
.sel(time=slice("1950", "1953"))
.tasmax
)
ref = open_dataset("sdba/nrcan_1950-2013.nc").sel(time=slice("1950", "1953")).tasmax
test = measures.mae(sim, ref).values
np.testing.assert_array_almost_equal(test, [4.159672, 14.2148, 9.768536], 4)


def test_annual_cycle_correlation(open_dataset):
def test_annual_cycle_correlation(gosset):
sim = (
open_dataset("sdba/CanESM2_1950-2100.nc").sel(time=slice("1950", "1953")).tasmax
xr.open_dataset(gosset.fetch("sdba/CanESM2_1950-2100.nc"))
.sel(time=slice("1950", "1953"))
.tasmax
)
ref = (
xr.open_dataset(gosset.fetch("sdba/nrcan_1950-2013.nc"))
.sel(time=slice("1950", "1953"))
.tasmax
)
ref = open_dataset("sdba/nrcan_1950-2013.nc").sel(time=slice("1950", "1953")).tasmax
test = (
measures.annual_cycle_correlation(sim, ref, window=31)
.sel(location="Vancouver")
Expand All @@ -71,22 +113,26 @@ def test_annual_cycle_correlation(open_dataset):


@pytest.mark.slow
def test_scorr(open_dataset):
ref = open_dataset("NRCANdaily/nrcan_canada_daily_tasmin_1990.nc").tasmin
sim = open_dataset("NRCANdaily/nrcan_canada_daily_tasmax_1990.nc").tasmax
def test_scorr(gosset):
ref = xr.open_dataset(
gosset.fetch("NRCANdaily/nrcan_canada_daily_tasmin_1990.nc")
).tasmin
sim = xr.open_dataset(
gosset.fetch("NRCANdaily/nrcan_canada_daily_tasmax_1990.nc")
).tasmax
scorr = measures.scorr(sim.isel(lon=slice(0, 50)), ref.isel(lon=slice(0, 50)))

np.testing.assert_allclose(scorr, [97374.2146243])


def test_taylordiagram(open_dataset):
def test_taylordiagram(gosset):
sim = (
open_dataset("sdba/CanESM2_1950-2100.nc")
xr.open_dataset(gosset.fetch("sdba/CanESM2_1950-2100.nc"))
.sel(time=slice("1950", "1953"), location="Amos")
.tasmax
)
ref = (
open_dataset("sdba/nrcan_1950-2013.nc")
xr.open_dataset(gosset.fetch("sdba/nrcan_1950-2013.nc"))
.sel(time=slice("1950", "1953"), location="Amos")
.tasmax
)
Expand Down
7 changes: 5 additions & 2 deletions tests/test_nbutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@
class TestQuantiles:
@pytest.mark.parametrize("uses_dask", [True, False])
def test_quantile(self, uses_dask, gosset):
file = gosset.fetch("sdba/CanESM2_1950-2100.nc")
da = (xr.open_dataset(file).sel(time=slice("1950", "1955")).pr).load()
da = (
xr.open_dataset(gosset.fetch("sdba/CanESM2_1950-2100.nc"))
.sel(time=slice("1950", "1955"))
.pr
).load()
if uses_dask:
da = da.chunk({"location": 1})
else:
Expand Down
7 changes: 4 additions & 3 deletions tests/test_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,13 +279,14 @@ def test_normalize(timeseries, random):
np.testing.assert_allclose(xp, xp2)


def test_stack_variables(open_dataset):
ds1 = open_dataset("sdba/CanESM2_1950-2100.nc")
ds2 = open_dataset("sdba/ahccd_1950-2013.nc")
def test_stack_variables(gosset):
ds1 = xr.open_dataset(gosset.fetch("sdba/CanESM2_1950-2100.nc"))
ds2 = xr.open_dataset(gosset.fetch("sdba/ahccd_1950-2013.nc"))

da1 = stack_variables(ds1)
da2 = stack_variables(ds2)

# FIXME: Thes test for variable order; use a membership test instead
assert list(da1.multivar.values) == ["pr", "tasmax"]
assert da1.multivar.attrs["_standard_name"] == [
"precipitation_flux",
Expand Down
Loading

0 comments on commit acb7baf

Please sign in to comment.