-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtest_epoch_dataarray_accessor.py
More file actions
337 lines (283 loc) · 10.6 KB
/
test_epoch_dataarray_accessor.py
File metadata and controls
337 lines (283 loc) · 10.6 KB
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
import tempfile
from importlib.metadata import version
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
import pytest
import xarray as xr
from matplotlib.animation import PillowWriter
from matplotlib.container import BarContainer
from packaging.version import Version
import sdf_xarray as sdfxr
import sdf_xarray.plotting as sxp
from sdf_xarray import SDFPreprocess, download
mpl.use("Agg")
# TODO Remove this once the new kwarg options are fully implemented
if Version(version("xarray")) >= Version("2025.8.0"):
xr.set_options(use_new_combine_kwarg_defaults=True)
TEST_FILES_DIR_1D = download.fetch_dataset("test_files_1D")
TEST_FILES_DIR_2D_MW = download.fetch_dataset("test_files_2D_moving_window")
TEST_FILES_DIR_3D = download.fetch_dataset("test_files_3D")
@pytest.fixture
def subplots():
fig, ax = plt.subplots()
yield (fig, ax)
plt.close(fig)
def test_animation_accessor():
array = xr.DataArray(
[1, 2, 3],
dims=["x"],
coords={"x": [0, 1, 2]},
attrs={"long_name": "Test Array", "units": "m"},
)
assert hasattr(array, "epoch")
assert hasattr(array.epoch, "animate")
@pytest.mark.parametrize(
("xrlib", "params"),
[
(
xr,
{
"compat": "no_conflicts",
"join": "outer",
"preprocess": SDFPreprocess(),
},
),
(sdfxr, {}),
],
)
def test_animate_headless(xrlib, params):
with xrlib.open_mfdataset(TEST_FILES_DIR_1D.glob("*.sdf"), **params) as ds:
anim = ds["Derived_Number_Density_electron"].epoch.animate()
# Specify a custom writable temporary directory
with tempfile.TemporaryDirectory() as temp_dir:
temp_file_path = f"{temp_dir}/output.gif"
try:
anim.save(temp_file_path, writer=PillowWriter(fps=2))
except Exception as e:
pytest.fail(f"animate().save() failed in headless mode: {e}")
def test_get_frame_title_no_optional_params():
with xr.open_mfdataset(
TEST_FILES_DIR_1D.glob("*.sdf"),
compat="no_conflicts",
join="outer",
preprocess=SDFPreprocess(),
) as ds:
data = ds["Derived_Number_Density_electron"]
expected_result = "Time = 5.47e-14 [s]"
result = sxp.get_frame_title(data, 0)
assert expected_result == result
def test_get_frame_title_sdf_name():
with xr.open_mfdataset(
TEST_FILES_DIR_1D.glob("*.sdf"),
compat="no_conflicts",
join="outer",
preprocess=SDFPreprocess(),
) as ds:
data = ds["Derived_Number_Density_electron"]
expected_result = "Time = 5.47e-14 [s], 0000.sdf"
result = sxp.get_frame_title(data, 0, display_sdf_name=True)
assert expected_result == result
def test_get_frame_title_custom_title():
with xr.open_mfdataset(
TEST_FILES_DIR_1D.glob("*.sdf"),
compat="no_conflicts",
join="outer",
preprocess=SDFPreprocess(),
) as ds:
data = ds["Derived_Number_Density_electron"]
expected_result = "Test Title, Time = 5.47e-14 [s]"
result = sxp.get_frame_title(data, 0, title_custom="Test Title")
assert expected_result == result
def test_get_frame_title_custom_title_and_sdf_name():
with xr.open_mfdataset(
TEST_FILES_DIR_1D.glob("*.sdf"),
compat="no_conflicts",
join="outer",
preprocess=SDFPreprocess(),
) as ds:
data = ds["Derived_Number_Density_electron"]
expected_result = "Test Title, Time = 5.47e-14 [s], 0000.sdf"
result = sxp.get_frame_title(
data, 0, display_sdf_name=True, title_custom="Test Title"
)
assert expected_result == result
def test_get_frame_title_Z_Grid_mid():
with xr.open_dataset(TEST_FILES_DIR_3D / "0001.sdf") as ds:
data = ds["Derived_Number_Density_Electron"]
expected_result = "Z = 3.91e-07 [m]"
result = sxp.get_frame_title(data, 0, t="Z_Grid_mid")
assert expected_result == result
def test_calculate_window_boundaries_1D():
with xr.open_mfdataset(
TEST_FILES_DIR_2D_MW.glob("*.sdf"),
preprocess=SDFPreprocess(),
combine="nested",
compat="no_conflicts",
join="outer",
) as ds:
data = ds["Derived_Number_Density_electron"][:, :, 50]
expected_result = np.array(
[[0, 1], [0.49, 1.49], [0.99, 1.99], [1.49, 2.49], [1.99, 2.99]]
)
result = sxp.calculate_window_boundaries(data)
assert result == pytest.approx(expected_result, abs=0.1)
def test_calculate_window_boundaries_2D():
with xr.open_mfdataset(
TEST_FILES_DIR_2D_MW.glob("*.sdf"),
preprocess=SDFPreprocess(),
combine="nested",
compat="no_conflicts",
join="outer",
) as ds:
data = ds["Derived_Number_Density_electron"]
expected_result = np.array(
[[0, 1], [0.49, 1.49], [0.99, 1.99], [1.49, 2.49], [1.99, 2.99]]
)
result = sxp.calculate_window_boundaries(data)
assert result == pytest.approx(expected_result, abs=0.1)
def test_calculate_window_boundaries_1D_xlim():
with xr.open_mfdataset(
TEST_FILES_DIR_2D_MW.glob("*.sdf"),
preprocess=SDFPreprocess(),
combine="nested",
compat="no_conflicts",
join="outer",
) as ds:
data = ds["Derived_Number_Density_electron"][:, :, 50]
expected_result = np.array(
[[0.1, 0.9], [0.59, 1.39], [1.09, 1.89], [1.59, 2.39], [2.09, 2.89]]
)
result = sxp.calculate_window_boundaries(data, xlim=(0.1, 0.9))
assert result == pytest.approx(expected_result, abs=0.1)
def test_calculate_window_boundaries_2D_xlim():
with xr.open_mfdataset(
TEST_FILES_DIR_2D_MW.glob("*.sdf"),
preprocess=SDFPreprocess(),
combine="nested",
compat="no_conflicts",
join="outer",
) as ds:
data = ds["Derived_Number_Density_electron"]
expected_result = np.array(
[[0.1, 0.9], [0.59, 1.39], [1.09, 1.89], [1.59, 2.39], [2.09, 2.89]]
)
result = sxp.calculate_window_boundaries(data, xlim=(0.1, 0.9))
assert result == pytest.approx(expected_result, abs=0.1)
def test_compute_global_limits():
with xr.open_mfdataset(
TEST_FILES_DIR_1D.glob("*.sdf"),
compat="no_conflicts",
join="outer",
preprocess=SDFPreprocess(),
) as ds:
result_min, result_max = sxp.compute_global_limits(
ds["Derived_Number_Density_electron"]
)
expected_result_min = 8.07e19
expected_result_max = 1.17e20
assert result_min == pytest.approx(expected_result_min, abs=1e18)
assert result_max == pytest.approx(expected_result_max, abs=1e19)
def test_compute_global_limits_percentile():
with xr.open_mfdataset(
TEST_FILES_DIR_1D.glob("*.sdf"),
compat="no_conflicts",
join="outer",
preprocess=SDFPreprocess(),
) as ds:
result_min, result_max = sxp.compute_global_limits(
ds["Derived_Number_Density_electron"], 40, 45
)
expected_result_min = 9.84e19
expected_result_max = 9.94e19
assert result_min == pytest.approx(expected_result_min, abs=1e18)
assert result_max == pytest.approx(expected_result_max, abs=1e18)
def test_compute_global_limits_NaNs():
with xr.open_mfdataset(
TEST_FILES_DIR_2D_MW.glob("*.sdf"),
preprocess=SDFPreprocess(),
combine="nested",
compat="no_conflicts",
join="outer",
) as ds:
result_min, result_max = sxp.compute_global_limits(
ds["Derived_Number_Density_electron"]
)
expected_result_min = 5.51e-1
expected_result_max = 2.70
assert result_min == pytest.approx(expected_result_min, abs=1e-2)
assert result_max == pytest.approx(expected_result_max, abs=1e-1)
def test_epoch_plot_simple_1d_dataset(subplots):
with xr.open_mfdataset(
TEST_FILES_DIR_1D.glob("*.sdf"),
compat="no_conflicts",
join="outer",
preprocess=SDFPreprocess(),
) as ds:
_, ax = subplots
ds["Derived_Number_Density_electron"].isel(time=0).epoch.plot(ax=ax)
assert len(ax.lines) == 1
assert ax.get_xlabel() == "X [m]"
def test_epoch_plot_simple_2d_dataset(subplots):
with xr.open_mfdataset(
TEST_FILES_DIR_2D_MW.glob("*.sdf"),
preprocess=SDFPreprocess(),
combine="nested",
compat="no_conflicts",
join="outer",
) as ds:
_, ax = subplots
ds["Derived_Number_Density_electron"].isel(time=0).epoch.plot(ax=ax)
assert len(ax.collections) > 0
assert ax.get_xlabel() == "X [m]"
assert ax.get_ylabel() == "Y [m]"
def test_epoch_plot_simple_3d_dataset_slice(subplots):
with xr.open_dataset(TEST_FILES_DIR_3D / "0001.sdf") as ds:
_, ax = subplots
ds["Derived_Number_Density_Electron"].isel(Z_Grid_mid=0).epoch.plot(ax=ax)
assert len(ax.collections) > 0
assert ax.get_xlabel() == "X [m]"
assert ax.get_ylabel() == "Y [m]"
def test_epoch_plot_flips_axis_order_for_2d_data(subplots):
with xr.open_mfdataset(
TEST_FILES_DIR_2D_MW.glob("*.sdf"),
preprocess=SDFPreprocess(),
combine="nested",
compat="no_conflicts",
join="outer",
) as ds:
_, ax = subplots
ds["Derived_Number_Density_electron"].isel(time=0).epoch.plot(ax=ax)
assert ax.get_xlabel() == "X [m]"
assert ax.get_ylabel() == "Y [m]"
def test_epoch_plot_flips_axis_order_for_2d_data_with_additional_params(subplots):
with xr.open_mfdataset(
TEST_FILES_DIR_2D_MW.glob("*.sdf"),
preprocess=SDFPreprocess(),
combine="nested",
compat="no_conflicts",
join="outer",
) as ds:
_, ax = subplots
ds["Derived_Number_Density_electron"].isel(time=0).epoch.plot(
ax=ax,
xlim=(0.5, 1.0),
ylim=(0.0, 0.5),
)
assert ax.get_xlabel() == "X [m]"
assert ax.get_ylabel() == "Y [m]"
assert ax.get_xlim() == pytest.approx((0.5, 1.0), abs=1e-2)
assert ax.get_ylim() == pytest.approx((0.0, 0.5), abs=1e-2)
def test_epoch_plot_flips_axis_order_for_2d_data_but_not_when_time_dim_present(
subplots,
):
with xr.open_mfdataset(
TEST_FILES_DIR_2D_MW.glob("*.sdf"),
preprocess=SDFPreprocess(),
combine="nested",
compat="no_conflicts",
join="outer",
) as ds:
_, ax = subplots
plot = ds["Derived_Number_Density_electron"].epoch.plot(ax=ax)
assert type(plot[2]) is BarContainer