obsarray 1.0.2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- docs/conf.py +181 -0
- notebook/test_register_err_corr_form.py +45 -0
- obsarray/__init__.py +14 -0
- obsarray/_version.py +21 -0
- obsarray/err_corr.py +298 -0
- obsarray/flag_accessor.py +442 -0
- obsarray/templater/__init__.py +0 -0
- obsarray/templater/dataset_util.py +659 -0
- obsarray/templater/dstemplater.py +133 -0
- obsarray/templater/dswriter.py +113 -0
- obsarray/templater/template_util.py +257 -0
- obsarray/templater/tests/__init__.py +0 -0
- obsarray/templater/tests/test_dataset_util.py +488 -0
- obsarray/templater/tests/test_dswriter.py +31 -0
- obsarray/templater/tests/test_template_util.py +334 -0
- obsarray/test/__init__.py +0 -0
- obsarray/test/test_err_corr_forms.py +251 -0
- obsarray/test/test_flag_accessor.py +373 -0
- obsarray/test/test_unc_accessor.py +745 -0
- obsarray/unc_accessor.py +899 -0
- obsarray/utils.py +31 -0
- obsarray-1.0.2.dist-info/METADATA +57 -0
- obsarray-1.0.2.dist-info/RECORD +26 -0
- obsarray-1.0.2.dist-info/WHEEL +5 -0
- obsarray-1.0.2.dist-info/licenses/LICENSE +165 -0
- obsarray-1.0.2.dist-info/top_level.txt +3 -0
|
@@ -0,0 +1,334 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Tests for TemplateUtil class
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
import unittest
|
|
6
|
+
from unittest.mock import patch, call
|
|
7
|
+
import numpy as np
|
|
8
|
+
import xarray
|
|
9
|
+
from obsarray.templater.template_util import TemplateUtil, create_ds
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
__author__ = "Sam Hunt <sam.hunt@npl.co.uk>"
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class TestTemplateUtil(unittest.TestCase):
|
|
16
|
+
@patch("obsarray.templater.template_util.DatasetUtil")
|
|
17
|
+
def test_add_variables_1var(self, mock_du):
|
|
18
|
+
dataset = xarray.Dataset()
|
|
19
|
+
|
|
20
|
+
size = {"dim1": 25, "dim2": 30, "dim3": 10, "dim4": 15}
|
|
21
|
+
|
|
22
|
+
template = {
|
|
23
|
+
"array_variable": {
|
|
24
|
+
"dim": ["dim1", "dim2"],
|
|
25
|
+
"dtype": np.float32,
|
|
26
|
+
"attributes": {
|
|
27
|
+
"standard_name": "array_variable_std_name",
|
|
28
|
+
"long_name": "array_variable_long_name",
|
|
29
|
+
"units": "units",
|
|
30
|
+
"preferred_symbol": "av",
|
|
31
|
+
},
|
|
32
|
+
"encoding": {"dtype": np.uint16, "scale_factor": 1.0, "offset": 0.0},
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
# run method
|
|
37
|
+
dataset = TemplateUtil.add_variables(dataset, template, size)
|
|
38
|
+
|
|
39
|
+
# test results
|
|
40
|
+
mock_du.return_value.create_variable.assert_called_once_with(
|
|
41
|
+
[25, 30],
|
|
42
|
+
dim_names=["dim1", "dim2"],
|
|
43
|
+
dtype=np.float32,
|
|
44
|
+
attributes={
|
|
45
|
+
"standard_name": "array_variable_std_name",
|
|
46
|
+
"long_name": "array_variable_long_name",
|
|
47
|
+
"units": "units",
|
|
48
|
+
"preferred_symbol": "av",
|
|
49
|
+
},
|
|
50
|
+
)
|
|
51
|
+
mock_du.return_value.add_encoding.assert_called_once_with(
|
|
52
|
+
mock_du.return_value.create_variable.return_value,
|
|
53
|
+
dtype=np.uint16,
|
|
54
|
+
scale_factor=1.0,
|
|
55
|
+
offset=0.0,
|
|
56
|
+
)
|
|
57
|
+
|
|
58
|
+
@patch("obsarray.templater.template_util.DatasetUtil")
|
|
59
|
+
def test_add_variables_1uncvar(self, mock_du):
|
|
60
|
+
dataset = xarray.Dataset()
|
|
61
|
+
|
|
62
|
+
size = {"dim1": 25, "dim2": 30, "dim3": 10, "dim4": 15}
|
|
63
|
+
|
|
64
|
+
template = {
|
|
65
|
+
"array_variable": {
|
|
66
|
+
"dim": ["dim1", "dim2"],
|
|
67
|
+
"dtype": np.float32,
|
|
68
|
+
"attributes": {
|
|
69
|
+
"standard_name": "array_variable_std_name",
|
|
70
|
+
"long_name": "array_variable_long_name",
|
|
71
|
+
"units": "units",
|
|
72
|
+
"preferred_symbol": "av",
|
|
73
|
+
"err_corr": "err_corr",
|
|
74
|
+
},
|
|
75
|
+
"encoding": {"dtype": np.uint16, "scale_factor": 1.0, "offset": 0.0},
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
# run method
|
|
80
|
+
dataset = TemplateUtil.add_variables(dataset, template, size)
|
|
81
|
+
|
|
82
|
+
# test results
|
|
83
|
+
mock_du.return_value.create_unc_variable.assert_called_once_with(
|
|
84
|
+
[25, 30],
|
|
85
|
+
dim_names=["dim1", "dim2"],
|
|
86
|
+
dtype=np.float32,
|
|
87
|
+
attributes={
|
|
88
|
+
"standard_name": "array_variable_std_name",
|
|
89
|
+
"long_name": "array_variable_long_name",
|
|
90
|
+
"units": "units",
|
|
91
|
+
"preferred_symbol": "av",
|
|
92
|
+
},
|
|
93
|
+
err_corr="err_corr",
|
|
94
|
+
)
|
|
95
|
+
mock_du.return_value.add_encoding.assert_called_once_with(
|
|
96
|
+
mock_du.return_value.create_unc_variable.return_value,
|
|
97
|
+
dtype=np.uint16,
|
|
98
|
+
scale_factor=1.0,
|
|
99
|
+
offset=0.0,
|
|
100
|
+
)
|
|
101
|
+
|
|
102
|
+
@patch("obsarray.templater.template_util.DatasetUtil")
|
|
103
|
+
def test_add_variables_1flag(self, mock_du):
|
|
104
|
+
dataset = xarray.Dataset()
|
|
105
|
+
|
|
106
|
+
dim_sizes = {"dim1": 25, "dim2": 30, "dim3": 10, "dim4": 15}
|
|
107
|
+
|
|
108
|
+
test_variables = {
|
|
109
|
+
"array_variable": {
|
|
110
|
+
"dim": ["dim1", "dim2"],
|
|
111
|
+
"dtype": "flag",
|
|
112
|
+
"attributes": {
|
|
113
|
+
"standard_name": "array_variable_std_name",
|
|
114
|
+
"long_name": "array_variable_long_name",
|
|
115
|
+
"preferred_symbol": "av",
|
|
116
|
+
"flag_meanings": ["flag1"],
|
|
117
|
+
},
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
# run method
|
|
122
|
+
dataset = TemplateUtil.add_variables(dataset, test_variables, dim_sizes)
|
|
123
|
+
|
|
124
|
+
# test results
|
|
125
|
+
mock_du.return_value.create_flags_variable.assert_called_once_with(
|
|
126
|
+
[25, 30],
|
|
127
|
+
dim_names=["dim1", "dim2"],
|
|
128
|
+
meanings=["flag1"],
|
|
129
|
+
attributes={
|
|
130
|
+
"standard_name": "array_variable_std_name",
|
|
131
|
+
"long_name": "array_variable_long_name",
|
|
132
|
+
"preferred_symbol": "av",
|
|
133
|
+
},
|
|
134
|
+
)
|
|
135
|
+
|
|
136
|
+
@patch("obsarray.templater.template_util.DatasetUtil")
|
|
137
|
+
def test_add_variables_2var(self, mock_du):
|
|
138
|
+
dataset = xarray.Dataset()
|
|
139
|
+
|
|
140
|
+
size = {"dim1": 25, "dim2": 30, "dim3": 10, "dim4": 15}
|
|
141
|
+
|
|
142
|
+
template = {
|
|
143
|
+
"array_variable1": {
|
|
144
|
+
"dim": ["dim1", "dim2"],
|
|
145
|
+
"dtype": np.float32,
|
|
146
|
+
"attributes": {
|
|
147
|
+
"standard_name": "array_variable_std_name1",
|
|
148
|
+
"long_name": "array_variable_long_name1",
|
|
149
|
+
"units": "units",
|
|
150
|
+
"preferred_symbol": "av",
|
|
151
|
+
},
|
|
152
|
+
"encoding": {"dtype": np.uint16, "scale_factor": 1.0, "offset": 0.0},
|
|
153
|
+
},
|
|
154
|
+
"array_variable2": {
|
|
155
|
+
"dim": ["dim3", "dim4"],
|
|
156
|
+
"dtype": np.float32,
|
|
157
|
+
"attributes": {
|
|
158
|
+
"standard_name": "array_variable_std_name2",
|
|
159
|
+
"long_name": "array_variable_long_name2",
|
|
160
|
+
"units": "units",
|
|
161
|
+
"preferred_symbol": "av",
|
|
162
|
+
},
|
|
163
|
+
"encoding": {"dtype": np.uint16, "scale_factor": 1.0, "offset": 0.0},
|
|
164
|
+
},
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
# run method
|
|
168
|
+
dataset = TemplateUtil.add_variables(dataset, template, size)
|
|
169
|
+
|
|
170
|
+
# test results
|
|
171
|
+
# define expected calls to DatasetUtil methods
|
|
172
|
+
calls = [
|
|
173
|
+
call(
|
|
174
|
+
[25, 30],
|
|
175
|
+
dim_names=["dim1", "dim2"],
|
|
176
|
+
dtype=np.float32,
|
|
177
|
+
attributes={
|
|
178
|
+
"standard_name": "array_variable_std_name1",
|
|
179
|
+
"long_name": "array_variable_long_name1",
|
|
180
|
+
"units": "units",
|
|
181
|
+
"preferred_symbol": "av",
|
|
182
|
+
},
|
|
183
|
+
),
|
|
184
|
+
call(
|
|
185
|
+
[10, 15],
|
|
186
|
+
dim_names=["dim3", "dim4"],
|
|
187
|
+
dtype=np.float32,
|
|
188
|
+
attributes={
|
|
189
|
+
"standard_name": "array_variable_std_name2",
|
|
190
|
+
"long_name": "array_variable_long_name2",
|
|
191
|
+
"units": "units",
|
|
192
|
+
"preferred_symbol": "av",
|
|
193
|
+
},
|
|
194
|
+
),
|
|
195
|
+
]
|
|
196
|
+
|
|
197
|
+
mock_du.return_value.create_variable.assert_has_calls(calls, any_order=True)
|
|
198
|
+
|
|
199
|
+
def test_add_variables_1var_ds(self):
|
|
200
|
+
dataset = xarray.Dataset()
|
|
201
|
+
|
|
202
|
+
size = {"dim1": 25, "dim2": 30, "dim3": 10, "dim4": 15}
|
|
203
|
+
|
|
204
|
+
template = {
|
|
205
|
+
"array_variable": {
|
|
206
|
+
"dim": ["dim1", "dim2"],
|
|
207
|
+
"dtype": np.float32,
|
|
208
|
+
"attributes": {
|
|
209
|
+
"standard_name": "array_variable_std_name",
|
|
210
|
+
"long_name": "array_variable_long_name",
|
|
211
|
+
"units": "units",
|
|
212
|
+
"preferred_symbol": "av",
|
|
213
|
+
},
|
|
214
|
+
"encoding": {"dtype": np.uint16, "scale_factor": 1.0, "offset": 0.0},
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
# run method
|
|
219
|
+
dataset = TemplateUtil.add_variables(dataset, template, size)
|
|
220
|
+
|
|
221
|
+
# assert dataset with variables
|
|
222
|
+
self.assertEqual(type(dataset), xarray.Dataset)
|
|
223
|
+
self.assertEqual(type(dataset["array_variable"]), xarray.DataArray)
|
|
224
|
+
|
|
225
|
+
def test__return_variable_shape(self):
|
|
226
|
+
size = {"dim1": 25, "dim2": 30, "dim3": 10, "dim4": 15}
|
|
227
|
+
dim_names = ["dim2", "dim1"]
|
|
228
|
+
|
|
229
|
+
dim_sizes = TemplateUtil._return_variable_shape(dim_names=dim_names, size=size)
|
|
230
|
+
|
|
231
|
+
self.assertCountEqual([30, 25], dim_sizes)
|
|
232
|
+
|
|
233
|
+
def test__check_variable_definition_bad_name(self):
|
|
234
|
+
test_variable_name = 23
|
|
235
|
+
test_variable_attrs = {
|
|
236
|
+
"dim": ["dim1", "dim2"],
|
|
237
|
+
"dtype": np.float32,
|
|
238
|
+
"attributes": {
|
|
239
|
+
"standard_name": "array_variable_std_name",
|
|
240
|
+
"long_name": "array_variable_long_name",
|
|
241
|
+
"units": "units",
|
|
242
|
+
"preferred_symbol": "av",
|
|
243
|
+
},
|
|
244
|
+
"encoding": {"dtype": np.uint16, "scale_factor": 1.0, "offset": 0.0},
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
self.assertRaises(
|
|
248
|
+
TypeError,
|
|
249
|
+
TemplateUtil._check_variable_definition,
|
|
250
|
+
test_variable_name,
|
|
251
|
+
test_variable_attrs,
|
|
252
|
+
)
|
|
253
|
+
|
|
254
|
+
def test_add_metadata(self):
|
|
255
|
+
dataset = xarray.Dataset()
|
|
256
|
+
|
|
257
|
+
test_metadata = {"metadata1": "value"}
|
|
258
|
+
|
|
259
|
+
TemplateUtil.add_metadata(dataset, test_metadata)
|
|
260
|
+
|
|
261
|
+
self.assertEqual("value", dataset.attrs["metadata1"])
|
|
262
|
+
|
|
263
|
+
def test_create_template_dataset(self):
|
|
264
|
+
dim_sizes = {"dim1": 25, "dim2": 30, "dim3": 10, "dim4": 15}
|
|
265
|
+
|
|
266
|
+
test_variables = {
|
|
267
|
+
"array_variable": {
|
|
268
|
+
"dim": ["dim1", "dim2"],
|
|
269
|
+
"dtype": np.float32,
|
|
270
|
+
"attributes": {
|
|
271
|
+
"standard_name": "array_variable_std_name",
|
|
272
|
+
"long_name": "array_variable_long_name",
|
|
273
|
+
"units": "units",
|
|
274
|
+
"preferred_symbol": "av",
|
|
275
|
+
},
|
|
276
|
+
"encoding": {"dtype": np.uint16, "scale_factor": 1.0, "offset": 0.0},
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
test_metadata = {"metadata1": "value"}
|
|
281
|
+
|
|
282
|
+
ds = create_ds(test_variables, dim_sizes, test_metadata)
|
|
283
|
+
|
|
284
|
+
self.assertEqual(type(ds), xarray.Dataset)
|
|
285
|
+
self.assertEqual(type(ds["array_variable"]), xarray.DataArray)
|
|
286
|
+
self.assertEqual("value", ds.attrs["metadata1"])
|
|
287
|
+
|
|
288
|
+
def test_create_template_dataset_withunc(self):
|
|
289
|
+
dim_sizes = {"dim1": 25}
|
|
290
|
+
|
|
291
|
+
test_variables = {
|
|
292
|
+
"array_variable": {
|
|
293
|
+
"dim": ["dim1"],
|
|
294
|
+
"dtype": np.float32,
|
|
295
|
+
"attributes": {
|
|
296
|
+
"standard_name": "array_variable_std_name",
|
|
297
|
+
"long_name": "array_variable_long_name",
|
|
298
|
+
"units": "units",
|
|
299
|
+
"preferred_symbol": "av",
|
|
300
|
+
"unc_comps": ["u_array_variable"],
|
|
301
|
+
},
|
|
302
|
+
"encoding": {"dtype": np.uint16, "scale_factor": 1.0, "offset": 0.0},
|
|
303
|
+
},
|
|
304
|
+
"u_array_variable": {
|
|
305
|
+
"dim": ["dim1"],
|
|
306
|
+
"dtype": np.float32,
|
|
307
|
+
"attributes": {
|
|
308
|
+
"err_corr": [
|
|
309
|
+
{
|
|
310
|
+
"dim": ["dim1"],
|
|
311
|
+
"form": "rectangle_absolute",
|
|
312
|
+
"params": [1, 2],
|
|
313
|
+
"units": ["m", "m"],
|
|
314
|
+
},
|
|
315
|
+
],
|
|
316
|
+
"standard_name": "array_variable_std_name",
|
|
317
|
+
"long_name": "array_variable_long_name",
|
|
318
|
+
"units": "units",
|
|
319
|
+
"preferred_symbol": "av",
|
|
320
|
+
},
|
|
321
|
+
},
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
test_metadata = {"metadata1": "value"}
|
|
325
|
+
|
|
326
|
+
ds = create_ds(test_variables, dim_sizes, test_metadata)
|
|
327
|
+
|
|
328
|
+
self.assertEqual(type(ds), xarray.Dataset)
|
|
329
|
+
self.assertEqual(type(ds["array_variable"]), xarray.DataArray)
|
|
330
|
+
self.assertEqual("value", ds.attrs["metadata1"])
|
|
331
|
+
|
|
332
|
+
|
|
333
|
+
if __name__ == "__main__":
|
|
334
|
+
unittest.main()
|
|
File without changes
|
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
"""test_err_corr_forms - tests for obsarray.err_corr_forms"""
|
|
2
|
+
|
|
3
|
+
import unittest
|
|
4
|
+
import numpy as np
|
|
5
|
+
from obsarray.err_corr import (
|
|
6
|
+
BaseErrCorrForm,
|
|
7
|
+
ErrCorrForms,
|
|
8
|
+
RandomCorrelation,
|
|
9
|
+
SystematicCorrelation,
|
|
10
|
+
ErrCorrMatrixCorrelation,
|
|
11
|
+
)
|
|
12
|
+
from obsarray.test.test_unc_accessor import create_ds
|
|
13
|
+
|
|
14
|
+
__author__ = "Sam Hunt <sam.hunt@npl.co.uk>"
|
|
15
|
+
__all__ = []
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class TestErrCorrForms(unittest.TestCase):
|
|
19
|
+
def setUp(self):
|
|
20
|
+
class ErrCorrFormDef(BaseErrCorrForm):
|
|
21
|
+
def form(self):
|
|
22
|
+
pass
|
|
23
|
+
|
|
24
|
+
self.ErrCorrFormDef = ErrCorrFormDef
|
|
25
|
+
|
|
26
|
+
def test___setitem__(self):
|
|
27
|
+
err_corr_forms = ErrCorrForms()
|
|
28
|
+
err_corr_forms["test"] = self.ErrCorrFormDef
|
|
29
|
+
|
|
30
|
+
self.assertCountEqual(list(err_corr_forms._forms.keys()), ["test"])
|
|
31
|
+
|
|
32
|
+
def test___setitem___not_valid_cls(self):
|
|
33
|
+
class InvalidErrCorrFormDef:
|
|
34
|
+
pass
|
|
35
|
+
|
|
36
|
+
err_corr_forms = ErrCorrForms()
|
|
37
|
+
self.assertRaises(
|
|
38
|
+
TypeError, err_corr_forms.__setitem__, "test", InvalidErrCorrFormDef
|
|
39
|
+
)
|
|
40
|
+
|
|
41
|
+
def test___getitem__(self):
|
|
42
|
+
err_corr_forms = ErrCorrForms()
|
|
43
|
+
err_corr_forms._forms["test"] = "test"
|
|
44
|
+
self.assertEqual("test", err_corr_forms["test"])
|
|
45
|
+
|
|
46
|
+
def test___delitem__(self):
|
|
47
|
+
err_corr_forms = ErrCorrForms()
|
|
48
|
+
err_corr_forms._forms["test"] = "test"
|
|
49
|
+
|
|
50
|
+
del err_corr_forms._forms["test"]
|
|
51
|
+
|
|
52
|
+
self.assertTrue("test" not in err_corr_forms._forms)
|
|
53
|
+
|
|
54
|
+
def test_keys(self):
|
|
55
|
+
err_corr_forms = ErrCorrForms()
|
|
56
|
+
err_corr_forms._forms["test"] = "test"
|
|
57
|
+
|
|
58
|
+
self.assertCountEqual(["test"], err_corr_forms.keys())
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
class TestBaseErrCorrForm(unittest.TestCase):
|
|
62
|
+
def setUp(self) -> None:
|
|
63
|
+
self.ds = create_ds()
|
|
64
|
+
|
|
65
|
+
class BasicErrCorrForm(BaseErrCorrForm):
|
|
66
|
+
form = "basic"
|
|
67
|
+
|
|
68
|
+
def build_matrix(self, sli):
|
|
69
|
+
full_matrix = np.arange(144).reshape((12, 12))
|
|
70
|
+
return self.slice_errcorr_matrix(full_matrix, (2, 2, 3), sli)
|
|
71
|
+
|
|
72
|
+
self.BasicErrCorrForm = BasicErrCorrForm
|
|
73
|
+
|
|
74
|
+
def test_get_varshape_errcorr(self):
|
|
75
|
+
basicerrcorr = self.BasicErrCorrForm(
|
|
76
|
+
self.ds, "u_ran_temperature", ["x", "y", "time"], [], []
|
|
77
|
+
)
|
|
78
|
+
shape = basicerrcorr.get_varshape_errcorr()
|
|
79
|
+
np.testing.assert_equal(shape, (2, 2, 3))
|
|
80
|
+
|
|
81
|
+
def test_slice_full_cov_full(self):
|
|
82
|
+
basicerrcorr = self.BasicErrCorrForm(
|
|
83
|
+
self.ds, "u_ran_temperature", ["x"], [], []
|
|
84
|
+
)
|
|
85
|
+
|
|
86
|
+
full_matrix = np.arange(144).reshape((12, 12))
|
|
87
|
+
slice_matrix = basicerrcorr.build_matrix(
|
|
88
|
+
(slice(None), slice(None), slice(None))
|
|
89
|
+
)
|
|
90
|
+
|
|
91
|
+
np.testing.assert_equal(full_matrix, slice_matrix)
|
|
92
|
+
|
|
93
|
+
def test_get_sliced_dim_sizes_uncvar(self):
|
|
94
|
+
basicerrcorr = self.BasicErrCorrForm(
|
|
95
|
+
self.ds, "u_ran_temperature", ["x"], [], []
|
|
96
|
+
)
|
|
97
|
+
dim_sizes = basicerrcorr.get_sliced_dim_sizes_uncvar(
|
|
98
|
+
(slice(None), 0, slice(0, 2, 1))
|
|
99
|
+
)
|
|
100
|
+
assert dim_sizes == {"x": 2, "time": 2}
|
|
101
|
+
|
|
102
|
+
def test_get_sliced_dim_sizes_errcorr(self):
|
|
103
|
+
basicerrcorr = self.BasicErrCorrForm(
|
|
104
|
+
self.ds, "u_ran_temperature", ["x"], [], []
|
|
105
|
+
)
|
|
106
|
+
dim_sizes = basicerrcorr.get_sliced_dim_sizes_errcorr(
|
|
107
|
+
(slice(None), 0, slice(0, 2, 1))
|
|
108
|
+
)
|
|
109
|
+
assert dim_sizes == {"x": 2}
|
|
110
|
+
|
|
111
|
+
def test_get_sliced_dims_errcorr(self):
|
|
112
|
+
basicerrcorr = self.BasicErrCorrForm(
|
|
113
|
+
self.ds, "u_ran_temperature", ["x"], [], []
|
|
114
|
+
)
|
|
115
|
+
dims = basicerrcorr.get_sliced_dims_errcorr((slice(None), 0, slice(0, 2, 1)))
|
|
116
|
+
assert dims == ["x"]
|
|
117
|
+
|
|
118
|
+
def test_get_sliced_shape_errcorr(self):
|
|
119
|
+
basicerrcorr = self.BasicErrCorrForm(
|
|
120
|
+
self.ds, "u_ran_temperature", ["x"], [], []
|
|
121
|
+
)
|
|
122
|
+
shape = basicerrcorr.get_sliced_shape_errcorr((slice(None), 0, slice(0, 2, 1)))
|
|
123
|
+
assert shape == (2,)
|
|
124
|
+
basicerrcorr = self.BasicErrCorrForm(
|
|
125
|
+
self.ds, "u_ran_temperature", ["x", "time"], [], []
|
|
126
|
+
)
|
|
127
|
+
shape = basicerrcorr.get_sliced_shape_errcorr((slice(None), 0, slice(0, 2, 1)))
|
|
128
|
+
assert shape == (2, 2)
|
|
129
|
+
|
|
130
|
+
def test_slice_errcorr_matrix(self):
|
|
131
|
+
basicerrcorr = self.BasicErrCorrForm(
|
|
132
|
+
self.ds, "u_ran_temperature", ["x", "y", "z"], [], []
|
|
133
|
+
)
|
|
134
|
+
|
|
135
|
+
full_matrix = np.arange(144).reshape((12, 12))
|
|
136
|
+
slice_matrix = basicerrcorr.slice_errcorr_matrix(
|
|
137
|
+
full_matrix, (2, 2, 3), (slice(None), slice(None), 0)
|
|
138
|
+
)
|
|
139
|
+
|
|
140
|
+
exp_slice_matrix = np.array(
|
|
141
|
+
[[0, 3, 6, 9], [36, 39, 42, 45], [72, 75, 78, 81], [108, 111, 114, 117]]
|
|
142
|
+
)
|
|
143
|
+
|
|
144
|
+
np.testing.assert_equal(slice_matrix, exp_slice_matrix)
|
|
145
|
+
|
|
146
|
+
def test_slice_full_cov_slice(self):
|
|
147
|
+
basicerrcorr = self.BasicErrCorrForm(
|
|
148
|
+
self.ds, "u_ran_temperature", ["x", "y", "z"], [], []
|
|
149
|
+
)
|
|
150
|
+
|
|
151
|
+
slice_matrix = basicerrcorr.build_dot_matrix((slice(None), slice(None), 0))
|
|
152
|
+
|
|
153
|
+
exp_slice_matrix = np.array(
|
|
154
|
+
[[0, 3, 6, 9], [36, 39, 42, 45], [72, 75, 78, 81], [108, 111, 114, 117]]
|
|
155
|
+
)
|
|
156
|
+
|
|
157
|
+
np.testing.assert_equal(slice_matrix, exp_slice_matrix)
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
class TestRandomUnc(unittest.TestCase):
|
|
161
|
+
def setUp(self) -> None:
|
|
162
|
+
self.ds = create_ds()
|
|
163
|
+
|
|
164
|
+
def test_build_matrix_1stdim(self):
|
|
165
|
+
rc = RandomCorrelation(self.ds, "u_ran_temperature", ["x"], [], [])
|
|
166
|
+
|
|
167
|
+
ecrm = rc.build_dot_matrix((slice(None), slice(None), slice(None)))
|
|
168
|
+
|
|
169
|
+
np.testing.assert_equal(ecrm, np.eye(12))
|
|
170
|
+
|
|
171
|
+
def test_build_matrix_2nddim(self):
|
|
172
|
+
rc = RandomCorrelation(self.ds, "u_ran_temperature", ["y"], [], [])
|
|
173
|
+
|
|
174
|
+
ecrm = rc.build_dot_matrix((slice(None), slice(None), slice(None)))
|
|
175
|
+
|
|
176
|
+
np.testing.assert_equal(ecrm, np.eye(12))
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
class TestSystematicUnc(unittest.TestCase):
|
|
180
|
+
def setUp(self) -> None:
|
|
181
|
+
self.ds = create_ds()
|
|
182
|
+
|
|
183
|
+
def build_matrix_1stdim(self):
|
|
184
|
+
rc = SystematicCorrelation(self.ds, "u_sys_temperature", ["x"], [], [])
|
|
185
|
+
|
|
186
|
+
ecrm = rc.build_dot_matrix((slice(None), slice(None), slice(None)))
|
|
187
|
+
# np.testing.assert_equal(ecrm, np.ones((12, 12)))
|
|
188
|
+
|
|
189
|
+
return ecrm
|
|
190
|
+
|
|
191
|
+
def build_matrix_2nddim(self):
|
|
192
|
+
rc = SystematicCorrelation(self.ds, "u_sys_temperature", ["y"], [], [])
|
|
193
|
+
|
|
194
|
+
ecrm = rc.build_dot_matrix((slice(None), slice(None), slice(None)))
|
|
195
|
+
# np.testing.assert_equal(ecrm, np.ones((12, 12)))
|
|
196
|
+
|
|
197
|
+
return ecrm
|
|
198
|
+
|
|
199
|
+
def build_matrix_3ddim(self):
|
|
200
|
+
rc = SystematicCorrelation(self.ds, "u_sys_temperature", ["time"], [], [])
|
|
201
|
+
|
|
202
|
+
ecrm = rc.build_dot_matrix((slice(None), slice(None), slice(None)))
|
|
203
|
+
# np.testing.assert_equal(ecrm, np.ones((12, 12)))
|
|
204
|
+
|
|
205
|
+
return ecrm
|
|
206
|
+
|
|
207
|
+
def test_build_dot_matrix(self):
|
|
208
|
+
x = self.build_matrix_1stdim()
|
|
209
|
+
y = self.build_matrix_2nddim()
|
|
210
|
+
time = self.build_matrix_3ddim()
|
|
211
|
+
print(x.dot(y), x, y)
|
|
212
|
+
np.testing.assert_equal((x.dot(y)).dot(time), np.ones((12, 12)))
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
class TestErrCorrMatrixCorrelation(unittest.TestCase):
|
|
216
|
+
def setUp(self) -> None:
|
|
217
|
+
self.ds = create_ds()
|
|
218
|
+
|
|
219
|
+
def test_build_matrix_full(self):
|
|
220
|
+
ec = ErrCorrMatrixCorrelation(
|
|
221
|
+
self.ds,
|
|
222
|
+
"u_str_temperature",
|
|
223
|
+
["x", "time"],
|
|
224
|
+
["err_corr_str_temperature"],
|
|
225
|
+
[],
|
|
226
|
+
)
|
|
227
|
+
|
|
228
|
+
ecrm = ec.build_matrix((slice(None), slice(None), slice(None)))
|
|
229
|
+
np.testing.assert_equal(ecrm, np.ones((6, 6)))
|
|
230
|
+
|
|
231
|
+
def test_build_matrix_sliced(self):
|
|
232
|
+
ec = ErrCorrMatrixCorrelation(
|
|
233
|
+
self.ds,
|
|
234
|
+
"u_str_temperature",
|
|
235
|
+
["x", "time"],
|
|
236
|
+
["err_corr_str_temperature"],
|
|
237
|
+
[],
|
|
238
|
+
)
|
|
239
|
+
|
|
240
|
+
ecrm = ec.build_matrix((0, slice(None), slice(None)))
|
|
241
|
+
np.testing.assert_equal(ecrm, np.ones((3, 3)))
|
|
242
|
+
|
|
243
|
+
ecrm = ec.build_matrix((slice(None), 0, slice(None)))
|
|
244
|
+
np.testing.assert_equal(ecrm, np.ones((6, 6)))
|
|
245
|
+
|
|
246
|
+
ecrm = ec.build_matrix((slice(None), slice(None), 0))
|
|
247
|
+
np.testing.assert_equal(ecrm, np.ones((2, 2)))
|
|
248
|
+
|
|
249
|
+
|
|
250
|
+
if __name__ == "main":
|
|
251
|
+
unittest.main()
|