-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ADD: improved test coverage
- Loading branch information
Showing
9 changed files
with
304 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import numpy as np | ||
import pandas as pd | ||
import pytest | ||
|
||
from ilamb3.analysis import bias_analysis | ||
from ilamb3.regions import Regions | ||
from ilamb3.tests.test_compare import generate_test_dset | ||
|
||
|
||
def gen_quantile_dbase(): | ||
df = [] | ||
for r in Regions().regions: | ||
for th in [70, 80]: | ||
df.append( | ||
{ | ||
"variable": "da", | ||
"region": r, | ||
"quantile": th, | ||
"type": "bias", | ||
"value": np.random.rand(1)[0] * 1e-10, | ||
"unit": "kg m-2 s-1", | ||
} | ||
) | ||
return pd.DataFrame(df) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"use_uncertainty,mass_weighting,score", | ||
[ | ||
(True, False, 0.5037343625713414), | ||
(False, False, 0.49809129117395395), | ||
(True, True, 0.6211524133325482), | ||
(False, True, 0.6162697692652096), | ||
], | ||
) | ||
def test_bias_collier2018(use_uncertainty: bool, mass_weighting: bool, score: float): | ||
grid = dict(nlat=10, nlon=20) | ||
ref = generate_test_dset(**grid) | ||
ref["da_bnds"] = generate_test_dset(seed=2, **grid)["da"] * 1e-2 | ||
ref["da"].attrs["bounds"] = "da_bnds" | ||
com = generate_test_dset(seed=3, **grid) | ||
analysis = bias_analysis("da") | ||
df, _, _ = analysis( | ||
ref, | ||
com, | ||
method="Collier2018", | ||
use_uncertainty=use_uncertainty, | ||
mass_weighting=mass_weighting, | ||
) | ||
df = df[df["type"] == "score"] | ||
print(df.iloc[0].value) | ||
assert len(df) == 1 | ||
assert np.allclose(df.iloc[0].value, score) | ||
|
||
|
||
@pytest.mark.skip("incomplete") | ||
@pytest.mark.parametrize( | ||
"use_uncertainty,quantile_threshold,score", | ||
[ | ||
(True, 80, 0.5037343625713414), | ||
(False, 80, 0.49809129117395395), | ||
(False, 70, 0.49809129117395395), | ||
], | ||
) | ||
def test_bias_regionalquantiles( | ||
use_uncertainty: bool, quantile_threshold: bool, score: float | ||
): | ||
grid = dict(nlat=10, nlon=20) | ||
ref = generate_test_dset(**grid) | ||
ref["da_bnds"] = generate_test_dset(seed=2, **grid)["da"] * 1e-2 | ||
ref["da"].attrs["bounds"] = "da_bnds" | ||
com = generate_test_dset(seed=3, **grid) | ||
analysis = bias_analysis("da") | ||
df, _, _ = analysis( | ||
ref, | ||
com, | ||
method="RegionalQuantiles", | ||
use_uncertainty=use_uncertainty, | ||
quantile_dbase=gen_quantile_dbase(), | ||
quantile_threshold=quantile_threshold, | ||
) | ||
df = df[df["type"] == "score"] | ||
print(df.iloc[0].value) | ||
assert len(df) == 1 | ||
assert np.allclose(df.iloc[0].value, score) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import numpy as np | ||
import pandas as pd | ||
import xarray as xr | ||
|
||
import ilamb3.compare as cmp | ||
|
||
|
||
def generate_test_dset(seed: int = 1, ntime=None, nlat=None, nlon=None): | ||
rs = np.random.RandomState(seed) | ||
coords = [] | ||
dims = [] | ||
if ntime is not None: | ||
time = pd.date_range(start="2000-01-15", periods=ntime, freq="30D") | ||
coords.append(time) | ||
dims.append("time") | ||
if nlat is not None: | ||
lat = np.linspace(-90, 90, nlat + 1) | ||
lat = 0.5 * (lat[1:] + lat[:-1]) | ||
coords.append(lat) | ||
dims.append("lat") | ||
if nlon is not None: | ||
lon = np.linspace(-180, 180, nlon + 1) | ||
lon = 0.5 * (lon[1:] + lon[:-1]) | ||
coords.append(lon) | ||
dims.append("lon") | ||
ds = xr.Dataset( | ||
data_vars={ | ||
"da": xr.DataArray( | ||
rs.rand(*[len(c) for c in coords]) * 1e-8, coords=coords, dims=dims | ||
), | ||
} | ||
) | ||
ds["da"].attrs["units"] = "kg m-2 s-1" | ||
return ds | ||
|
||
|
||
def test_nest_spatial_grids(): | ||
ds1 = generate_test_dset(nlat=2, nlon=3) | ||
ds2 = generate_test_dset(nlat=5, nlon=7) | ||
ds1 = ds1.cf.add_bounds("lat") | ||
ds1_, ds2_ = cmp.nest_spatial_grids(ds1, ds2) | ||
assert cmp.is_spatially_aligned(ds1_, ds2_) | ||
assert np.allclose( | ||
(ds1_ - ds2_).pint.dequantify().sum()["da"].values, -6.06395917e-08 | ||
) | ||
dsa, dsb = cmp.pick_grid_aligned(ds1, ds2, ds1_, ds2_) | ||
xr.testing.assert_allclose(dsa, ds1_) | ||
xr.testing.assert_allclose(dsb, ds2_) |
Oops, something went wrong.