Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validation fixes #120

Merged
merged 11 commits into from
Oct 25, 2021
12 changes: 6 additions & 6 deletions dodola/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,24 +327,24 @@ def correct_wetday_frequency(x, out, process):
@click.argument("x", required=True)
@click.option("--variable", "-v", required=True)
@click.option(
"--data_type",
"-data",
"--data-type",
"-d",
required=True,
type=click.Choice(["cmip6", "bias_corrected", "downscaled"], case_sensitive=False),
help="Which data type to validate",
)
@click.option(
"--time_period",
"-time",
"--time-period",
"-t",
required=True,
type=click.Choice(["historical", "future"], case_sensitive=False),
help="Which time period to validate",
)
def validate_dataset(x, variable, data_type, time_period):
"""Validate a dataset"""
services.validate_dataset(
services.validate(
str(x),
variable=str(variable),
var=str(variable),
data_type=str(data_type),
time_period=str(time_period),
)
8 changes: 3 additions & 5 deletions dodola/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -485,19 +485,17 @@ def validate_dataset(ds, var, data_type, time_period="future"):
"""
Validate a Dataset. Valid for CMIP6, bias corrected and downscaled.

Raises AssertionError when validation fails.

Parameters
----------
ds : xr.Dataset
variable : {"tasmax", "tasmin", "dtr", "pr"}
var : {"tasmax", "tasmin", "dtr", "pr"}
Variable in Dataset to validate.
data_type : {"cmip6", "bias_corrected", "downscaled"}
Type of data output to validate.
time_period : {"historical", "future"}
Time period of data that will be validated.

Returns
-------
xr.Dataset
"""

# validation for all variables
Expand Down
2 changes: 1 addition & 1 deletion dodola/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ def validate(x, var, data_type, time_period):
"""

ds = storage.read(x)
validate_dataset(ds)
validate_dataset(ds, var, data_type, time_period)


@log_service
Expand Down
2 changes: 2 additions & 0 deletions dodola/tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"correct-wetday-frequency",
"train-aiqpd",
"apply-aiqpd",
"validate-dataset",
],
ids=(
"--help",
Expand All @@ -29,6 +30,7 @@
"correct-wetday-frequency --help",
"train-aiqpd --help",
"apply-aiqpd --help",
"validate-dataset --help",
),
)
def test_cli_helpflags(subcmd):
Expand Down
38 changes: 18 additions & 20 deletions dodola/tests/test_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,17 @@ def _modeloutputfactory(

np.random.seed(0)
time = xr.cftime_range(start=start_time, end=end_time, calendar="noleap")
data = 15 + 8 * np.random.randn(len(time))
# make sure that test data range is reasonable for the variable being tested
if variable_name == "tasmax" or variable_name == "tasmin":
low_val = 160
high_val = 340
elif variable_name == "dtr":
low_val = 1
high_val = 40
elif variable_name == "pr":
low_val = 0.01
high_val = 1900
data = np.random.randint(low_val, high_val, len(time)).astype(np.float64)

out = xr.Dataset(
{variable_name: (["time", "lon", "lat"], data[:, np.newaxis, np.newaxis])},
Expand Down Expand Up @@ -862,23 +872,13 @@ def test_downscale(domain_file, method, var):
np.testing.assert_almost_equal(downscaled_ds.values, downscaled_test.values)


@pytest.mark.parametrize(
"variable",
[
pytest.param("tasmax"),
pytest.param("tasmin"),
pytest.param("dtr"),
pytest.param("pr"),
],
"data_type",
[pytest.param("cmip6"), pytest.param("bias_corrected"), pytest.param("downscaled")],
"time_period",
[pytest.param("historical"), pytest.param("future")],
)
def validate_dataset(variable, data_type, time_period):
"""Tests that validate dataset passes for fake output data"""
@pytest.mark.parametrize("variable", ["tasmax", "tasmin", "dtr", "pr"])
@pytest.mark.parametrize("data_type", ["cmip6", "bias_corrected", "downscaled"])
@pytest.mark.parametrize("time_period", ["historical", "future"])
def test_validation(variable, data_type, time_period):
"""Tests that validate passes for fake output data"""
# Setup input data
if data_type == "bias_corrected" or "downscaled":
if data_type == "bias_corrected" or data_type == "downscaled":
if time_period == "historical":
start_time = "1950-01-01"
end_time = "2014-12-31"
Expand All @@ -902,6 +902,4 @@ def validate_dataset(variable, data_type, time_period):
in_url = "memory://test_validate/an/input/path.zarr"
repository.write(in_url, ds)

# read in test data and validate
ds_test = repository.read(in_url)
validate(ds_test, variable, data_type, time_period)
validate(in_url, variable, data_type, time_period)