-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtest_utils.py
257 lines (207 loc) · 7.79 KB
/
test_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# -*- mode: python; coding: utf-8 -*-
#
# Copyright (C) 2023 Benjamin Thomas Schwertfeger
# All rights reserved.
# https://github.com/btschwertfeger
#
"""
Module to to test utility functions for the CMethods package
Data types are ignored for simplicity.
"""
import re
import numpy as np
import pytest
import xarray as xr
from cmethods import adjust
from cmethods.distribution import (
detrended_quantile_mapping,
quantile_delta_mapping,
quantile_mapping,
)
from cmethods.static import MAX_SCALING_FACTOR
from cmethods.utils import (
check_np_types,
ensure_dividable,
ensure_xr_dataarray,
get_adjusted_scaling_factor,
get_pdf,
nan_or_equal,
)
# --------------------------------------------------------------------------
# test for nan values
@pytest.mark.filterwarnings("ignore:Do not call quantile_mapping directly")
def test_quantile_mapping_single_nan() -> None:
obs, simh, simp = list(np.arange(10)), list(np.arange(10)), list(np.arange(10))
obs[0] = np.nan
expected = np.array([0.0, 1.8, 2.7, 3.6, 4.5, 5.4, 6.3, 7.2, 8.1, 9.0])
res = quantile_mapping(obs=obs, simh=simh, simp=simp, n_quantiles=5)
assert np.allclose(res, expected), res
@pytest.mark.filterwarnings("ignore:All-NaN slice encountered")
@pytest.mark.filterwarnings("ignore:Do not call quantile_mapping directly")
def test_quantile_mapping_all_nan() -> None:
obs, simh, simp = (
list(np.full(10, np.nan)),
list(np.arange(10)),
list(np.arange(10)),
)
res = quantile_mapping(obs=obs, simh=simh, simp=simp, n_quantiles=5)
assert np.allclose(res, simp)
@pytest.mark.filterwarnings("ignore:Do not call quantile_delta_mapping directly")
def test_quantile_delta_mapping_single_nan() -> None:
obs, simh, simp = list(np.arange(10)), list(np.arange(10)), list(np.arange(10))
obs[0] = np.nan
expected = np.array([0.0, 1.8, 2.7, 3.6, 4.5, 5.4, 6.3, 7.2, 8.1, 9.0])
res = quantile_delta_mapping(obs=obs, simh=simh, simp=simp, n_quantiles=5)
assert np.allclose(res, expected)
@pytest.mark.filterwarnings("ignore:All-NaN slice encountered")
@pytest.mark.filterwarnings("ignore:Do not call quantile_delta_mapping directly")
def test_quantile_delta_mapping_all_nan() -> None:
obs, simh, simp = (
list(np.full(10, np.nan)),
list(np.arange(10)),
list(np.arange(10)),
)
res = quantile_delta_mapping(obs=obs, simh=simh, simp=simp, n_quantiles=5)
assert np.allclose(res, simp)
# --------------------------------------------------------------------------
# test utils
def test_nan_or_equal() -> None:
assert nan_or_equal(0, 0)
assert nan_or_equal(np.nan, np.nan)
assert not nan_or_equal(0, 1)
def test_get_pdf() -> None:
assert (get_pdf(np.arange(10), [0, 5, 11]) == np.array((5, 5))).all()
def test_get_adjusted_scaling_factor() -> None:
assert get_adjusted_scaling_factor(10, 5) == 5
assert get_adjusted_scaling_factor(10, 11) == 10
assert get_adjusted_scaling_factor(-10, -11) == -10
assert get_adjusted_scaling_factor(-11, -10) == -10
def test_ensure_devidable() -> None:
assert np.array_equal(
ensure_dividable(
np.array((1, 2, 3, 4, 5, 0)),
np.array((0, 1, 0, 2, 3, 0)),
MAX_SCALING_FACTOR,
),
np.array((10, 2, 30, 2, 5 / 3, 0)),
)
# --------------------------------------------------------------------------
# test type checking related functions
# For most of them only one part of the check is tested, since other tests
# are already covering the functionality and correctness of functions using
# valid values.
def test_np_type_check() -> None:
"""
Checks the correctness of the type checking function when the types are
correct. No error should occur.
"""
check_np_types(obs=[], simh=[], simp=[])
def test_xr_type_check() -> None:
"""
Checks the correctness of the type checking function when the types are
correct. No error should occur.
"""
ds: xr.core.dataarray.Dataset = xr.core.dataarray.Dataset()
ensure_xr_dataarray(obs=ds, simh=ds, simp=ds)
def test_type_check_failing() -> None:
"""
Checks the correctness of the type checking function when the inputs do not
have the correct type.
"""
phrase: str = "must be type list, np.ndarray, or np.generic"
with pytest.raises(TypeError, match=f"'obs' {phrase}"):
check_np_types(obs=1, simh=[], simp=[])
with pytest.raises(TypeError, match=f"'simh' {phrase}"):
check_np_types(obs=[], simh=1, simp=[])
with pytest.raises(TypeError, match=f"'simp' {phrase}"):
check_np_types(obs=[], simh=[], simp=1)
@pytest.mark.filterwarnings("ignore:Do not call quantile_mapping directly")
def test_quantile_mapping_type_check_n_quantiles_failing() -> None:
"""n_quantiles must by type int"""
with pytest.raises(TypeError, match="'n_quantiles' must be type int"):
quantile_mapping(obs=[], simh=[], simp=[], n_quantiles="100")
def test_detrended_quantile_mapping_type_check_n_quantiles_failing(
datasets: dict,
) -> None:
"""n_quantiles must by type int"""
with pytest.raises(TypeError, match=re.escape("'n_quantiles' must be type int")):
detrended_quantile_mapping( # type: ignore[attr-defined]
obs=datasets["+"]["obsh"][:, 0, 0],
simh=datasets["+"]["simh"][:, 0, 0],
simp=datasets["+"]["simp"][:, 0, 0],
n_quantiles="100",
)
def test_detrended_quantile_mapping_type_check_simp_failing(datasets: dict) -> None:
"""n_quantiles must by type int"""
with pytest.raises(
TypeError,
match=r"'simp' must be type xarray.core.dataarray.DataArray",
):
detrended_quantile_mapping( # type: ignore[attr-defined]
obs=datasets["+"]["obsh"][:, 0, 0],
simh=datasets["+"]["simh"][:, 0, 0],
simp=[],
n_quantiles=100,
)
@pytest.mark.filterwarnings("ignore:Do not call quantile_delta_mapping directly")
def test_quantile_delta_mapping_type_check_n_quantiles() -> None:
"""n_quantiles must by type int"""
with pytest.raises(TypeError, match="'n_quantiles' must be type int"):
quantile_delta_mapping( # type: ignore[attr-defined]
obs=[],
simh=[],
simp=[],
n_quantiles="100",
)
@pytest.mark.filterwarnings("ignore:Do not call quantile_delta_mapping directly")
def test_quantile_delta_mapping_type_check_n_quantiles_failing() -> None:
"""n_quantiles must by type int"""
with pytest.raises(TypeError, match="'n_quantiles' must be type int"):
quantile_delta_mapping( # type: ignore[attr-defined]
obs=[],
simh=[],
simp=[],
n_quantiles="100",
)
def test_adjust_type_checking_failing() -> None:
"""
Checks for all types that are expected to be passed to the adjust_3d method
"""
# Create the DataArray
data: xr.core.dataarray.DataArray = xr.DataArray(
[10, 20, 30, 40, 50],
dims=["time"],
)
with pytest.raises(
TypeError,
match=r"'obs' must be type 'xarray.core.dataarray.DataArray'.",
):
adjust(
method="linear_scaling",
obs=[],
simh=data,
simp=data,
group="time.month",
)
with pytest.raises(
TypeError,
match=r"'simh' must be type 'xarray.core.dataarray.DataArray'.",
):
adjust(
method="linear_scaling",
obs=data,
simh=[],
simp=data,
group="time.month",
)
with pytest.raises(
TypeError,
match=r"'simp' must be type 'xarray.core.dataarray.DataArray'.",
):
adjust(
method="linear_scaling",
obs=data,
simh=data,
simp=[],
group="time.month",
)