-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathtest_stats.py
475 lines (353 loc) · 16.4 KB
/
test_stats.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
from abc import ABCMeta
import numpy as np
import xarray as xr
import pytest
from geocat.comp.stats import eofunc, eofunc_eofs, eofunc_pcs, eofunc_ts, pearson_r
class BaseEOFTestClass(metaclass=ABCMeta):
_sample_data_eof = []
# _sample_data[ 0 ]
_sample_data_eof.append([[[0, 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]]])
# _sample_data[ 1 ]
_sample_data_eof.append(np.arange(64, dtype='double').reshape((4, 4, 4)))
# _sample_data[ 2 ]
tmp_data = np.asarray([
0, 1, -99, -99, 4, -99, 6, -99, 8, 9, 10, -99, 12, -99, 14, 15, 16, -99,
18, -99, 20, 21, 22, -99, 24, 25, 26, 27, 28, -99, 30, -99, 32, 33, 34,
35, 36, -99, 38, 39, 40, -99, 42, -99, 44, 45, 46, -99, 48, 49, 50, 51,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63
],
dtype='double').reshape((4, 4, 4))
_sample_data_eof.append(tmp_data)
# _sample_data[ 3 ]
tmp_data = np.asarray([
0, 1, -99, -99, 4, -99, 6, -99, 8, 9, 10, -99, 12, -99, 14, 15, 16, -99,
18, -99, 20, 21, 22, -99, 24, 25, 26, 27, 28, -99, 30, -99, 32, 33, 34,
35, 36, -99, 38, 39, 40, -99, 42, -99, 44, 45, 46, -99, 48, 49, 50, 51,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63
],
dtype='double').reshape((4, 4, 4))
tmp_data[tmp_data == -99] = np.nan
_sample_data_eof.append(tmp_data)
# _sample_data[ 4 ]
_sample_data_eof.append(np.arange(64, dtype='int64').reshape((4, 4, 4)))
_num_attrs = 4
expected_output = np.full((1, 4, 4), 0.25)
expected_eigen_val_time_dim_2 = 26.66666
expected_eigen_val_time_dim_1 = 426.66666
expected_eigen_val_time_dim_0 = 6826.66667
class Test_eof(BaseEOFTestClass):
def test_eof_00(self) -> None:
data = self._sample_data_eof[0]
results = eofunc_eofs(data, neofs=1, time_dim=2)
eof = results.data
attrs = results.attrs
np.testing.assert_equal(self.expected_output.shape, results.shape)
np.testing.assert_array_almost_equal(
np.linalg.norm(self.expected_output), np.linalg.norm(eof), 5)
np.testing.assert_array_almost_equal(self.expected_output, abs(eof), 5)
np.testing.assert_equal(self._num_attrs, len(attrs))
np.testing.assert_almost_equal(self.expected_eigen_val_time_dim_2,
attrs['eigenvalues'].values[0], 5)
def test_eof_deprecated(self) -> None:
data = self._sample_data_eof[0]
results = eofunc(data, neval=1)
eof = results.data
attrs = results.attrs
np.testing.assert_equal(self.expected_output.shape, results.shape)
np.testing.assert_array_almost_equal(
np.linalg.norm(self.expected_output), np.linalg.norm(eof), 5)
np.testing.assert_array_almost_equal(self.expected_output, abs(eof), 5)
np.testing.assert_equal(self._num_attrs, len(attrs))
np.testing.assert_almost_equal(self.expected_eigen_val_time_dim_2,
attrs['eigenvalues'].values[0], 5)
def test_eof_01(self) -> None:
data = self._sample_data_eof[1]
results = eofunc_eofs(data, neofs=1, time_dim=2)
eof = results.data
attrs = results.attrs
np.testing.assert_equal(self.expected_output.shape, results.shape)
np.testing.assert_array_almost_equal(
np.linalg.norm(self.expected_output), np.linalg.norm(eof), 5)
np.testing.assert_array_almost_equal(self.expected_output, abs(eof), 5)
np.testing.assert_equal(self._num_attrs, len(attrs))
np.testing.assert_almost_equal(self.expected_eigen_val_time_dim_2,
attrs['eigenvalues'].values[0], 5)
def test_eof_02(self) -> None:
data = self._sample_data_eof[1]
results = eofunc_eofs(data, neofs=1, time_dim=2)
eof = results.data
attrs = results.attrs
np.testing.assert_equal(self.expected_output.shape, results.shape)
np.testing.assert_array_almost_equal(
np.linalg.norm(self.expected_output), np.linalg.norm(eof), 5)
np.testing.assert_array_almost_equal(self.expected_output, abs(eof), 5)
np.testing.assert_equal(self._num_attrs, len(attrs))
np.testing.assert_almost_equal(self.expected_eigen_val_time_dim_2,
attrs['eigenvalues'].values[0], 5)
def test_eof_14(self) -> None:
data = self._sample_data_eof[4]
results = eofunc_eofs(data, neofs=1, time_dim=2)
eof = results.data
attrs = results.attrs
np.testing.assert_equal(self.expected_output.shape, results.shape)
np.testing.assert_array_almost_equal(
np.linalg.norm(self.expected_output), np.linalg.norm(eof), 5)
np.testing.assert_array_almost_equal(self.expected_output, abs(eof), 5)
np.testing.assert_equal(self._num_attrs, len(attrs))
np.testing.assert_almost_equal(self.expected_eigen_val_time_dim_2,
attrs['eigenvalues'].values[0], 5)
def test_eof_15(self) -> None:
data = np.asarray(self._sample_data_eof[0])
data = np.transpose(data, axes=(2, 1, 0))
dims = [f"dim_{i}" for i in range(data.ndim)]
dims[0] = 'time'
data = xr.DataArray(data,
dims=dims,
attrs={
"prop1": "prop1",
"prop2": 2
})
results = eofunc_eofs(data, neofs=1)
eof = results.data
attrs = results.attrs
np.testing.assert_equal(self.expected_output.shape, results.shape)
np.testing.assert_array_almost_equal(
np.linalg.norm(self.expected_output), np.linalg.norm(eof), 5)
np.testing.assert_array_almost_equal(self.expected_output, abs(eof), 5)
np.testing.assert_equal(self._num_attrs, len(attrs))
np.testing.assert_almost_equal(self.expected_eigen_val_time_dim_2,
attrs['eigenvalues'].values[0], 5)
np.testing.assert_equal(False, ("prop1" in attrs))
np.testing.assert_equal(False, ("prop2" in attrs))
# TODO: Maybe revisited to add time_dim support for Xarray in addition to numpy inputs
# def test_eof_15_time_dim(self) -> None:
#
# data = np.asarray(self._sample_data_eof[0])
#
# dims = [f"dim_{i}" for i in range(data.ndim)]
# dims[2] = 'time'
#
# data = xr.DataArray(
# data,
# dims=dims,
# attrs={"prop1": "prop1",
# "prop2": 2,
# }
# )
#
# results = eofunc_eofs(data, num_eofs=1, time_dim=2)
# eof = results.data
# attrs = results.attrs
#
# np.testing.assert_equal(self.expected_output.shape, results.shape)
#
# np.testing.assert_array_almost_equal(np.linalg.norm(self.expected_output), np.linalg.norm(eof), 5)
#
# np.testing.assert_array_almost_equal(self.expected_output, abs(eof), 5)
#
# np.testing.assert_equal(self._num_attrs + 2, len(attrs))
#
# # self.assertAlmostEqual(5.33333, attrs['eval_transpose'][0], 4)
# # self.assertAlmostEqual(100.0, attrs['pcvar'][0], 1)
# self.assertAlmostEqual(26.66666, attrs['eigenvalues'].values[0], 4)
# # self.assertEqual("covariance", attrs['matrix'])
# # self.assertEqual("transpose", attrs['method'])
# self.assertFalse("prop1" in attrs)
# self.assertFalse("prop2" in attrs)
def test_eof_16(self) -> None:
data = np.asarray(self._sample_data_eof[0])
data = np.transpose(data, axes=(2, 1, 0))
dims = [f"dim_{i}" for i in range(data.ndim)]
dims[0] = 'time'
data = xr.DataArray(data,
dims=dims,
attrs={
"prop1": "prop1",
"prop2": 2,
})
results = eofunc_eofs(data, 1, meta=True)
eof = results.data
attrs = results.attrs
np.testing.assert_equal(self.expected_output.shape, results.shape)
np.testing.assert_array_almost_equal(
np.linalg.norm(self.expected_output), np.linalg.norm(eof), 5)
np.testing.assert_array_almost_equal(self.expected_output, abs(eof), 5)
np.testing.assert_equal(self._num_attrs + 2, len(attrs))
np.testing.assert_almost_equal(self.expected_eigen_val_time_dim_2,
attrs['eigenvalues'].values[0], 5)
np.testing.assert_equal(True, ("prop1" in attrs))
np.testing.assert_equal(True, ("prop2" in attrs))
np.testing.assert_equal("prop1", attrs["prop1"])
np.testing.assert_equal(2, attrs["prop2"])
def test_eof_n_01(self) -> None:
data = self._sample_data_eof[1]
results = eofunc_eofs(data, neofs=1, time_dim=1)
eof = results.data
attrs = results.attrs
np.testing.assert_equal(self.expected_output.shape, results.shape)
np.testing.assert_array_almost_equal(
np.linalg.norm(self.expected_output), np.linalg.norm(eof), 5)
np.testing.assert_array_almost_equal(self.expected_output, abs(eof), 5)
np.testing.assert_equal(self._num_attrs, len(attrs))
np.testing.assert_almost_equal(self.expected_eigen_val_time_dim_1,
attrs['eigenvalues'].values[0], 5)
def test_eof_n_03(self) -> None:
data = self._sample_data_eof[1]
results = eofunc_eofs(data, 1, time_dim=0)
eof = results.data
attrs = results.attrs
np.testing.assert_equal(self.expected_output.shape, results.shape)
np.testing.assert_array_almost_equal(
np.linalg.norm(self.expected_output), np.linalg.norm(eof), 5)
np.testing.assert_array_almost_equal(self.expected_output, abs(eof), 5)
np.testing.assert_equal(self._num_attrs, len(attrs))
np.testing.assert_almost_equal(self.expected_eigen_val_time_dim_0,
attrs['eigenvalues'].values[0], 5)
def test_eof_n_03_1(self) -> None:
data = self._sample_data_eof[1]
results = eofunc_eofs(data, 1, time_dim=0)
eof = results.data
attrs = results.attrs
np.testing.assert_equal(self.expected_output.shape, results.shape)
np.testing.assert_array_almost_equal(
np.linalg.norm(self.expected_output), np.linalg.norm(eof), 5)
np.testing.assert_array_almost_equal(self.expected_output, abs(eof), 5)
np.testing.assert_equal(self._num_attrs, len(attrs))
np.testing.assert_almost_equal(self.expected_eigen_val_time_dim_0,
attrs['eigenvalues'].values[0], 5)
class Test_eof_ts(BaseEOFTestClass):
@pytest.fixture(scope="class")
def _nc_ds(self):
try:
return xr.open_dataset("eofunc_dataset.nc")
except Exception:
return xr.open_dataset("test/eofunc_dataset.nc")
def test_01(self, _nc_ds) -> None:
sst = _nc_ds.sst
expected_tsout = _nc_ds.tsout
actual_tsout = eofunc_pcs(sst, npcs=5)
np.testing.assert_equal(actual_tsout.shape, expected_tsout.shape)
np.testing.assert_array_almost_equal(actual_tsout, expected_tsout.data,
3)
def test_01_deprecated(self, _nc_ds) -> None:
sst = _nc_ds.sst
evec = _nc_ds.evec
expected_tsout = _nc_ds.tsout
actual_tsout = eofunc_ts(sst, evec, time_dim=0)
np.testing.assert_equal(actual_tsout.shape, expected_tsout.shape)
np.testing.assert_array_almost_equal(actual_tsout, expected_tsout.data,
3)
def test_02(self, _nc_ds) -> None:
sst = _nc_ds.sst
expected_tsout = _nc_ds.tsout
actual_tsout = eofunc_pcs(sst, npcs=5, meta=True)
np.testing.assert_equal(actual_tsout.shape, expected_tsout.shape)
np.testing.assert_array_almost_equal(actual_tsout, expected_tsout.data,
3)
np.testing.assert_equal(actual_tsout.coords["time"].data,
sst.coords["time"].data)
class Test_pearson_r:
# Coordinates
times = xr.cftime_range(start='2022-08-01', end='2022-08-05', freq='D')
lats = np.linspace(start=-45, stop=45, num=3, dtype='float32')
lons = np.linspace(start=-180, stop=180, num=4, dtype='float32')
# Create data variables
x, y, z = np.meshgrid(lons, lats, times)
np.random.seed(0)
a = np.random.random_sample((len(lats), len(lons), len(times)))
b = np.power(a, 2)
weights = np.cos(np.deg2rad(y))
ds = xr.Dataset(data_vars={
'a': (('lat', 'lon', 'time'), a),
'b': (('lat', 'lon', 'time'), b),
'weights': (('lat', 'lon', 'time'), weights)
},
coords={
'lat': lats,
'lon': lons,
'time': times
},
attrs={'description': 'Test data'})
unweighted_r = 0.963472086
unweighted_r_skipnan = 0.96383798
weighted_r = 0.963209755
weighted_r_lat = [
[0.995454445, 0.998450821, 0.99863877, 0.978765291, 0.982350092],
[0.99999275, 0.995778831, 0.998994355, 0.991634937, 0.999868279],
[0.991344899, 0.998632079, 0.99801552, 0.968517489, 0.985215828],
[0.997034735, 0.99834464, 0.987382522, 0.99646236, 0.989222738]
]
# Testing numpy inputs
def test_np_inputs(self) -> None:
a = self.a
b = self.b
result = pearson_r(a, b)
assert np.allclose(self.unweighted_r, result)
def test_np_inputs_weighted(self) -> None:
a = self.a
b = self.b
w = self.weights
result = pearson_r(a, b, weights=w)
assert np.allclose(self.weighted_r, result)
def test_np_inputs_warn(self) -> None:
a = self.a
b = self.b
with pytest.warns(UserWarning):
pearson_r(a, b, dim='lat', axis=0)
def test_np_inputs_across_lats(self) -> None:
a = self.a
b = self.b
w = self.weights
result = pearson_r(a, b, weights=w, axis=0)
assert np.allclose(self.weighted_r_lat, result)
def test_np_inputs_skipna(self) -> None:
# deep copy to prevent adding nans to the test data for other tests
a = self.a.copy()
a[0] = np.nan
b = self.b
result = pearson_r(a, b, skipna=True)
assert np.allclose(self.unweighted_r_skipnan, result)
# Testing xarray inputs
def test_xr_inputs(self) -> None:
a = self.ds.a
b = self.ds.b
result = pearson_r(a, b)
assert np.allclose(self.unweighted_r, result)
def test_xr_inputs_weighted(self) -> None:
a = self.ds.a
b = self.ds.b
w = self.ds.weights
result = pearson_r(a, b, weights=w)
assert np.allclose(self.weighted_r, result)
def test_xr_inputs_warn(self) -> None:
a = self.ds.a
b = self.ds.b
with pytest.warns(UserWarning):
pearson_r(a, b, dim='lat', axis=0)
def test_xr_inputs_across_lats(self) -> None:
a = self.ds.a
b = self.ds.b
w = self.ds.weights[:, 0, 0]
result = pearson_r(a, b, weights=w, dim='lat')
assert np.allclose(self.weighted_r_lat, result)
def test_xr_inputs_skipna(self) -> None:
# deep copy to prevent adding nans to the test data for other tests
a = self.ds.a.copy(deep=True)
a[0] = np.nan
b = self.ds.b
result = pearson_r(a, b, skipna=True)
assert np.allclose(self.unweighted_r_skipnan, result)
def test_keep_attrs(self) -> None:
a = self.ds.a
b = self.ds.b
a.attrs.update({'Description': 'Test Data'})
b.attrs.update({'2nd Description': 'Dummy Data'})
result = pearson_r(a, b, keep_attrs=True)
assert result.attrs == a.attrs