-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtest_against_collect.py
223 lines (164 loc) · 8.1 KB
/
test_against_collect.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
import pytest
import numpy.testing as npt
from xbout import open_boutdataset, collect as new_collect
from .utils_for_tests import create_bout_ds, create_bout_ds_list, METADATA_VARS
boutdata = pytest.importorskip("boutdata", reason="boutdata is not available")
old_collect = boutdata.collect
class TestAccuracyAgainstOldCollect:
def test_single_file(self, tmp_path_factory):
# Create temp directory for files
test_dir = tmp_path_factory.mktemp("test_data")
# Generate some test data
generated_ds = create_bout_ds(syn_data_type="linear")
generated_ds.to_netcdf(test_dir.joinpath("BOUT.dmp.0.nc"))
var = "n"
expected = old_collect(var, path=test_dir, xguards=True, yguards=False)
# Test against new standard - open_boutdataset
with pytest.warns(UserWarning):
ds = open_boutdataset(test_dir.joinpath("BOUT.dmp.0.nc"))
actual = ds[var].values
assert expected.shape == actual.shape
npt.assert_equal(actual, expected)
# Test against backwards compatible collect function
actual = new_collect(var, path=test_dir, xguards=True, yguards=False)
assert expected.shape == actual.shape
npt.assert_equal(actual, expected)
def test_multiple_files_along_x(self, tmp_path_factory):
# Create temp directory for files
test_dir = tmp_path_factory.mktemp("test_data")
# Generate some test data
ds_list, file_list = create_bout_ds_list(
"BOUT.dmp", nxpe=3, nype=1, syn_data_type="linear"
)
for temp_ds, file_name in zip(ds_list, file_list):
temp_ds.to_netcdf(test_dir.joinpath(file_name))
var = "n"
expected = old_collect(var, path=test_dir, prefix="BOUT.dmp", xguards=True)
# Test against new standard - open_boutdataset
with pytest.warns(UserWarning):
ds = open_boutdataset(test_dir.joinpath("BOUT.dmp.*.nc"))
actual = ds[var].values
assert expected.shape == actual.shape
npt.assert_equal(actual, expected)
# Test against backwards compatible collect function
actual = new_collect(var, path=test_dir, prefix="BOUT.dmp", xguards=True)
assert expected.shape == actual.shape
npt.assert_equal(actual, expected)
def test_multiple_files_along_y(self, tmp_path_factory):
# Create temp directory for files
test_dir = tmp_path_factory.mktemp("test_data")
# Generate some test data
ds_list, file_list = create_bout_ds_list(
"BOUT.dmp", nxpe=1, nype=3, syn_data_type="linear"
)
for temp_ds, file_name in zip(ds_list, file_list):
temp_ds.to_netcdf(test_dir.joinpath(file_name))
var = "n"
expected = old_collect(var, path=test_dir, prefix="BOUT.dmp", xguards=True)
# Test against new standard - .open_boutdataset
with pytest.warns(UserWarning):
ds = open_boutdataset(test_dir.joinpath("BOUT.dmp.*.nc"))
actual = ds[var].values
assert expected.shape == actual.shape
npt.assert_equal(actual, expected)
# Test against backwards compatible collect function
actual = new_collect(var, path=test_dir, prefix="BOUT.dmp", xguards=True)
assert expected.shape == actual.shape
npt.assert_equal(actual, expected)
def test_multiple_files_along_xy(self, tmp_path_factory):
# Create temp directory for files
test_dir = tmp_path_factory.mktemp("test_data")
# Generate some test data
ds_list, file_list = create_bout_ds_list(
"BOUT.dmp", nxpe=3, nype=3, syn_data_type="linear"
)
for temp_ds, file_name in zip(ds_list, file_list):
temp_ds.to_netcdf(test_dir.joinpath(file_name))
var = "n"
expected = old_collect(var, path=test_dir, prefix="BOUT.dmp", xguards=True)
# Test against new standard - .open_boutdataset
with pytest.warns(UserWarning):
ds = open_boutdataset(test_dir.joinpath("BOUT.dmp.*.nc"))
actual = ds[var].values
assert expected.shape == actual.shape
npt.assert_equal(actual, expected)
# Test against backwards compatible collect function
actual = new_collect(var, path=test_dir, prefix="BOUT.dmp", xguards=True)
assert expected.shape == actual.shape
npt.assert_equal(actual, expected)
def test_metadata(self, tmp_path_factory):
# Create temp directory for files
test_dir = tmp_path_factory.mktemp("test_data")
# Generate some test data
generated_ds = create_bout_ds(syn_data_type="linear")
generated_ds.to_netcdf(test_dir.joinpath("BOUT.dmp.0.nc"))
with pytest.warns(UserWarning):
ds = open_boutdataset(test_dir.joinpath("BOUT.dmp.*.nc"))
for v in METADATA_VARS:
expected = old_collect(v, path=test_dir)
# Check metadata against new standard - open_boutdataset
actual = ds.bout.metadata[v]
npt.assert_equal(actual, expected)
# Check against backwards compatible collect function
actual = new_collect(v, path=test_dir)
npt.assert_equal(actual, expected)
def test_new_collect_indexing_int(self, tmp_path_factory):
# Create temp directory for files
test_dir = tmp_path_factory.mktemp("test_data")
# Generate some test data
ds_list, file_list = create_bout_ds_list(
"BOUT.dmp", nxpe=3, nype=3, syn_data_type="linear"
)
for temp_ds, file_name in zip(ds_list, file_list):
temp_ds.to_netcdf(test_dir.joinpath(file_name))
var = "n"
indexers = ["tind", "xind", "yind", "zind"]
ind_arg = 0
for kwarg in indexers:
# Extracting a the first index of each dimension for comparison
expected = old_collect(var, path=test_dir, **{kwarg: ind_arg})
# Test against backwards compatible collect function
actual = new_collect(var, path=test_dir, **{kwarg: ind_arg})
assert expected.shape == actual.shape
npt.assert_equal(actual, expected)
def test_new_collect_indexing_list(self, tmp_path_factory):
# Create temp directory for files
test_dir = tmp_path_factory.mktemp("test_data")
# Generate some test data
ds_list, file_list = create_bout_ds_list(
"BOUT.dmp", nxpe=3, nype=3, syn_data_type="linear"
)
for temp_ds, file_name in zip(ds_list, file_list):
temp_ds.to_netcdf(test_dir.joinpath(file_name))
var = "n"
indexers = ["tind", "xind", "yind", "zind"]
ind_arg = [0, 4]
for kwarg in indexers:
# Extracting a the first index of each dimension for comparison
expected = old_collect(var, path=test_dir, **{kwarg: ind_arg})
# Test against backwards compatible collect function
actual = new_collect(var, path=test_dir, **{kwarg: ind_arg})
assert expected.shape == actual.shape
npt.assert_equal(actual, expected)
def test_new_collect_indexing_slice(self, tmp_path_factory):
# Create temp directory for files
test_dir = tmp_path_factory.mktemp("test_data")
# Generate some test data
ds_list, file_list = create_bout_ds_list(
"BOUT.dmp", nxpe=3, nype=3, syn_data_type="linear"
)
for temp_ds, file_name in zip(ds_list, file_list):
temp_ds.to_netcdf(test_dir.joinpath(file_name))
var = "n"
indexers = ["tind", "xind", "yind", "zind"]
ind_list = [slice(0, 4, 2), slice(0, 4)]
for kwarg in indexers:
for ind_arg in ind_list:
# Extracting a the first index of each dimension for comparison
expected = old_collect(var, path=test_dir, **{kwarg: ind_arg})
# Test against backwards compatible collect function
actual = new_collect(var, path=test_dir, **{kwarg: ind_arg})
assert expected.shape == actual.shape
npt.assert_equal(actual, expected)
@pytest.mark.skip
class test_speed_against_old_collect: ...