forked from xarray-contrib/xskillscore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conftest.py
300 lines (215 loc) · 6.56 KB
/
conftest.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
import numpy as np
import pytest
import xarray as xr
import xskillscore as xs
from xskillscore import Contingency
PERIODS = 12 # effective_p_value produces nans for shorter periods
@pytest.fixture(autouse=True)
def add_standard_imports(doctest_namespace):
"""imports for doctest"""
doctest_namespace["np"] = np
doctest_namespace["xr"] = xr
doctest_namespace["xs"] = xs
# always seed numpy.random to make the examples deterministic
np.random.seed(42)
@pytest.fixture
def times():
return xr.cftime_range(start="2000", periods=PERIODS, freq="D")
@pytest.fixture
def lats():
return np.arange(4)
@pytest.fixture
def lons():
return np.arange(5)
@pytest.fixture
def members():
return np.arange(3)
# o vs. f in probabilistic
@pytest.fixture
def o(times, lats, lons):
"""Observation."""
data = np.random.rand(len(times), len(lats), len(lons))
return xr.DataArray(
data,
coords=[times, lats, lons],
dims=["time", "lat", "lon"],
attrs={"source": "test"},
)
@pytest.fixture
def f_prob(times, lats, lons, members):
"""Probabilistic forecast containing also a member dimension."""
data = np.random.rand(len(members), len(times), len(lats), len(lons))
return xr.DataArray(
data,
coords=[members, times, lats, lons],
dims=["member", "time", "lat", "lon"],
attrs={"source": "test"},
)
@pytest.fixture
def f(f_prob):
"""Deterministic forecast matching observation o."""
return f_prob.isel(member=0, drop=True)
# a vs. b in deterministic
@pytest.fixture
def a(o):
return o
@pytest.fixture
def b(f):
return f
# nan
@pytest.fixture
def a_rand_nan(a):
"""Masked"""
return a.where(a < 0.5)
@pytest.fixture
def b_rand_nan(b):
"""Masked"""
return b.where(b < 0.5)
@pytest.fixture
def a_fixed_nan(a):
"""Masked block"""
a.data[:, 1:3, 1:3] = np.nan
return a
@pytest.fixture
def b_fixed_nan(b):
"""Masked block"""
b.data[:, 1:3, 1:3] = np.nan
return b
# with zeros
@pytest.fixture
def a_with_zeros(a):
"""Zeros"""
return a.where(a < 0.5, 0)
# dask
@pytest.fixture
def a_dask(a):
"""Chunked"""
return a.chunk()
@pytest.fixture
def b_dask(b):
"""Chunked"""
return b.chunk()
@pytest.fixture
def a_rand_nan_dask(a_rand_nan):
"""Chunked"""
return a_rand_nan.chunk()
@pytest.fixture
def b_rand_nan_dask(b_rand_nan):
"""Chunked"""
return b_rand_nan.chunk()
@pytest.fixture
def o_dask(o):
return o.chunk()
@pytest.fixture
def f_prob_dask(f_prob):
return f_prob.chunk()
# 1D time
@pytest.fixture
def a_1d(a):
"""Timeseries of a"""
return a.isel(lon=0, lat=0, drop=True)
@pytest.fixture
def b_1d(b):
"""Timeseries of b"""
return b.isel(lon=0, lat=0, drop=True)
@pytest.fixture
def a_1d_fixed_nan():
time = xr.cftime_range("2000-01-01", "2000-01-03", freq="D")
return xr.DataArray([3, np.nan, 5], dims=["time"], coords=[time])
@pytest.fixture
def b_1d_fixed_nan(a_1d_fixed_nan):
b = a_1d_fixed_nan.copy()
b.values = [7, 2, np.nan]
return b
@pytest.fixture
def a_1d_with_zeros(a_with_zeros):
"""Timeseries of a with zeros"""
return a_with_zeros.isel(lon=0, lat=0, drop=True)
# weights
@pytest.fixture
def weights_cos_lat(a):
"""Weighting array by cosine of the latitude."""
return xr.ones_like(a) * np.abs(np.cos(a.lat))
@pytest.fixture
def weights_linear_time(a):
"""Weighting array by linear (1 -> 0) of the time."""
weights = np.linspace(1, 0, num=len(a.time))
return xr.ones_like(a) * xr.DataArray(weights, dims="time")
@pytest.fixture
def weights_linear_time_1d(weights_linear_time):
"""Timeseries of weights_linear_time"""
return weights_linear_time.isel(lon=0, lat=0, drop=True)
@pytest.fixture
def weights_lonlat(a):
weights = np.cos(np.deg2rad(a.lat))
_, weights = xr.broadcast(a, weights)
return weights.isel(time=0, drop=True)
@pytest.fixture
def weights_time():
time = xr.cftime_range("2000-01-01", "2000-01-03", freq="D")
return xr.DataArray([1, 2, 3], dims=["time"], coords=[time])
@pytest.fixture
def weights_cos_lat_dask(weights_cos_lat):
"""
Weighting array by cosine of the latitude.
"""
return weights_cos_lat.chunk()
@pytest.fixture
def category_edges():
"""Category bin edges between 0 and 1."""
return np.linspace(0, 1, 6)
@pytest.fixture
def forecast_3d_int():
"""Random integer 3D forecast used for testing Contingency."""
times = xr.cftime_range(start="2000", freq="D", periods=10)
lats = np.arange(4)
lons = np.arange(5)
data = np.random.randint(0, 10, size=(len(times), len(lats), len(lons)))
return xr.DataArray(data, coords=[times, lats, lons], dims=["time", "lat", "lon"])
@pytest.fixture
def observation_3d_int(forecast_3d_int):
"""Random integer 3D observation used for testing Contingency."""
b = forecast_3d_int.copy()
b.values = np.random.randint(0, 10, size=(b.shape[0], b.shape[1], b.shape[2]))
return b
@pytest.fixture
def forecast_3d():
"""Random 3D forecast used for testing Contingency."""
times = xr.cftime_range(start="2000", freq="D", periods=10)
lats = np.arange(4)
lons = np.arange(5)
data = np.random.normal(0, 1, size=(len(times), len(lats), len(lons)))
return xr.DataArray(data, coords=[times, lats, lons], dims=["time", "lat", "lon"])
@pytest.fixture
def observation_3d(forecast_3d):
"""Random 3D observation used for testing Contingency."""
b = forecast_3d.copy()
b.values = np.random.normal(0, 1, size=(b.shape[0], b.shape[1], b.shape[2]))
return b
@pytest.fixture
def dichotomous_Contingency_1d():
"""Contingency of fixed, dichotomous 1d forecast and observations."""
observations = xr.DataArray(
np.array(2 * [0] + 2 * [1] + 1 * [0] + 2 * [1]), coords=[("x", np.arange(7))]
)
forecasts = xr.DataArray(
np.array(2 * [0] + 2 * [0] + 1 * [1] + 2 * [1]), coords=[("x", np.arange(7))]
)
category_edges = np.array([-np.inf, 0.5, np.inf])
return Contingency(
observations, forecasts, category_edges, category_edges, dim=["x"]
)
@pytest.fixture
def symmetric_edges():
"""Category bin edges between 0 and 1."""
return np.linspace(-3, 3, 11)
@pytest.fixture
def forecast_1d_long():
"""Forecasts normally distributed around 0."""
s = 100
return xr.DataArray(np.random.normal(size=(s)), coords=[("time", np.arange(s))])
@pytest.fixture
def observation_1d_long():
"""Observations normally distributed around 0."""
s = 100
return xr.DataArray(np.random.normal(size=(s)), coords=[("time", np.arange(s))])