-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtest_load.py
950 lines (833 loc) · 31.8 KB
/
test_load.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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
from pathlib import Path
import pytest
from xarray import concat
from xarray.tests.test_dataset import create_test_data
import xarray.testing as xrt
from natsort import natsorted
from xbout.load import (
_check_filetype,
_expand_wildcards,
_expand_filepaths,
_arrange_for_concatenation,
_trim,
_infer_contains_boundaries,
open_boutdataset,
_BOUT_PER_PROC_VARIABLES,
_BOUT_TIME_DEPENDENT_META_VARS,
)
from xbout.utils import _separate_metadata
from xbout.tests.utils_for_tests import create_bout_ds, METADATA_VARS
def test_check_extensions(tmp_path):
files_dir = tmp_path.joinpath("data")
files_dir.mkdir()
example_nc_file = files_dir.joinpath("example.nc")
example_nc_file.write_text("content_nc")
filetype = _check_filetype(example_nc_file)
assert filetype == "netcdf4"
example_hdf5_file = files_dir.joinpath("example.h5netcdf")
example_hdf5_file.write_text("content_hdf5")
filetype = _check_filetype(example_hdf5_file)
assert filetype == "h5netcdf"
example_invalid_file = files_dir.joinpath("example.txt")
example_hdf5_file.write_text("content_txt")
with pytest.raises(IOError):
filetype = _check_filetype(example_invalid_file)
def test_set_fci_coords(create_example_grid_file_fci, create_example_files_fci):
grid = create_example_grid_file_fci
data = create_example_files_fci
ds = open_boutdataset(data, gridfilepath=grid, geometry="fci")
assert "R" in ds
assert "Z" in ds
class TestPathHandling:
def test_glob_expansion_single(self, tmp_path):
files_dir = tmp_path.joinpath("data")
files_dir.mkdir()
example_file = files_dir.joinpath("example.0.nc")
example_file.write_text("content")
path = example_file
filepaths = _expand_wildcards(path)
assert filepaths[0] == example_file
path = files_dir.joinpath("example.*.nc")
filepaths = _expand_wildcards(path)
assert filepaths[0] == example_file
@pytest.mark.parametrize(
"ii, jj", [(1, 1), (1, 4), (3, 1), (5, 3), (12, 1), (1, 12), (121, 2), (3, 111)]
)
def test_glob_expansion_both(self, tmp_path, ii, jj):
files_dir = tmp_path.joinpath("data")
files_dir.mkdir()
filepaths = []
for i in range(ii):
example_run_dir = files_dir.joinpath("run" + str(i))
example_run_dir.mkdir()
for j in range(jj):
example_file = example_run_dir.joinpath("example." + str(j) + ".nc")
example_file.write_text("content")
filepaths.append(example_file)
expected_filepaths = natsorted(filepaths, key=lambda filepath: str(filepath))
path = files_dir.joinpath("run*/example.*.nc")
actual_filepaths = _expand_wildcards(path)
assert actual_filepaths == expected_filepaths
@pytest.mark.parametrize(
"ii, jj", [(1, 1), (1, 4), (3, 1), (5, 3), (1, 12), (3, 111)]
)
def test_glob_expansion_brackets(self, tmp_path, ii, jj):
files_dir = tmp_path.joinpath("data")
files_dir.mkdir()
filepaths = []
for i in range(ii):
example_run_dir = files_dir.joinpath("run" + str(i))
example_run_dir.mkdir()
for j in range(jj):
example_file = example_run_dir.joinpath("example." + str(j) + ".nc")
example_file.write_text("content")
filepaths.append(example_file)
expected_filepaths = natsorted(filepaths, key=lambda filepath: str(filepath))
path = files_dir.joinpath("run[1-9]/example.*.nc")
actual_filepaths = _expand_wildcards(path)
assert actual_filepaths == expected_filepaths[jj:]
def test_no_files(self, tmp_path):
files_dir = tmp_path.joinpath("data")
files_dir.mkdir()
with pytest.raises(IOError):
path = files_dir.joinpath("run*/example.*.nc")
_expand_filepaths(path)
@pytest.fixture()
def create_filepaths():
return _create_filepaths
def _create_filepaths(nxpe=1, nype=1, nt=1):
filepaths = []
for t in range(nt):
for i in range(nype):
for j in range(nxpe):
file_num = j + nxpe * i
path = "./run{}".format(str(t)) + "/BOUT.dmp.{}.nc".format(
str(file_num)
)
filepaths.append(path)
return filepaths
class TestArrange:
def test_arrange_single(self, create_filepaths):
paths = create_filepaths(nxpe=1, nype=1, nt=1)
expected_path_grid = [[["./run0/BOUT.dmp.0.nc"]]]
actual_path_grid, actual_concat_dims = _arrange_for_concatenation(
paths, nxpe=1, nype=1
)
assert expected_path_grid == actual_path_grid
assert actual_concat_dims == [None, None, None]
def test_arrange_along_x(self, create_filepaths):
paths = create_filepaths(nxpe=3, nype=1, nt=1)
expected_path_grid = [
[["./run0/BOUT.dmp.0.nc", "./run0/BOUT.dmp.1.nc", "./run0/BOUT.dmp.2.nc"]]
]
actual_path_grid, actual_concat_dims = _arrange_for_concatenation(
paths, nxpe=3, nype=1
)
assert expected_path_grid == actual_path_grid
assert actual_concat_dims == [None, None, "x"]
def test_arrange_along_y(self, create_filepaths):
paths = create_filepaths(nxpe=1, nype=3, nt=1)
expected_path_grid = [
[
["./run0/BOUT.dmp.0.nc"],
["./run0/BOUT.dmp.1.nc"],
["./run0/BOUT.dmp.2.nc"],
]
]
actual_path_grid, actual_concat_dims = _arrange_for_concatenation(
paths, nxpe=1, nype=3
)
assert expected_path_grid == actual_path_grid
assert actual_concat_dims == [None, "y", None]
def test_arrange_along_t(self, create_filepaths):
paths = create_filepaths(nxpe=1, nype=1, nt=3)
expected_path_grid = [
[["./run0/BOUT.dmp.0.nc"]],
[["./run1/BOUT.dmp.0.nc"]],
[["./run2/BOUT.dmp.0.nc"]],
]
actual_path_grid, actual_concat_dims = _arrange_for_concatenation(
paths, nxpe=1, nype=1
)
assert expected_path_grid == actual_path_grid
assert actual_concat_dims == ["t", None, None]
def test_arrange_along_xy(self, create_filepaths):
paths = create_filepaths(nxpe=3, nype=2, nt=1)
expected_path_grid = [
[
[
"./run0/BOUT.dmp.0.nc",
"./run0/BOUT.dmp.1.nc",
"./run0/BOUT.dmp.2.nc",
],
[
"./run0/BOUT.dmp.3.nc",
"./run0/BOUT.dmp.4.nc",
"./run0/BOUT.dmp.5.nc",
],
]
]
actual_path_grid, actual_concat_dims = _arrange_for_concatenation(
paths, nxpe=3, nype=2
)
assert expected_path_grid == actual_path_grid
assert actual_concat_dims == [None, "y", "x"]
def test_arrange_along_xt(self, create_filepaths):
paths = create_filepaths(nxpe=3, nype=1, nt=2)
expected_path_grid = [
[["./run0/BOUT.dmp.0.nc", "./run0/BOUT.dmp.1.nc", "./run0/BOUT.dmp.2.nc"]],
[["./run1/BOUT.dmp.0.nc", "./run1/BOUT.dmp.1.nc", "./run1/BOUT.dmp.2.nc"]],
]
actual_path_grid, actual_concat_dims = _arrange_for_concatenation(
paths, nxpe=3, nype=1
)
assert expected_path_grid == actual_path_grid
assert actual_concat_dims == ["t", None, "x"]
def test_arrange_along_xyt(self, create_filepaths):
paths = create_filepaths(nxpe=3, nype=2, nt=2)
expected_path_grid = [
[
[
"./run0/BOUT.dmp.0.nc",
"./run0/BOUT.dmp.1.nc",
"./run0/BOUT.dmp.2.nc",
],
[
"./run0/BOUT.dmp.3.nc",
"./run0/BOUT.dmp.4.nc",
"./run0/BOUT.dmp.5.nc",
],
],
[
[
"./run1/BOUT.dmp.0.nc",
"./run1/BOUT.dmp.1.nc",
"./run1/BOUT.dmp.2.nc",
],
[
"./run1/BOUT.dmp.3.nc",
"./run1/BOUT.dmp.4.nc",
"./run1/BOUT.dmp.5.nc",
],
],
]
actual_path_grid, actual_concat_dims = _arrange_for_concatenation(
paths, nxpe=3, nype=2
)
assert expected_path_grid == actual_path_grid
assert actual_concat_dims == ["t", "y", "x"]
class TestStripMetadata:
def test_strip_metadata(self):
original = create_bout_ds()
assert original["NXPE"] == 1
ds, metadata = _separate_metadata(original)
assert original.drop_vars(
METADATA_VARS + _BOUT_PER_PROC_VARIABLES + _BOUT_TIME_DEPENDENT_META_VARS,
errors="ignore",
).equals(ds)
assert metadata["NXPE"] == 1
# TODO also test loading multiple files which have guard cells
class TestOpen:
def test_single_file(self, tmp_path_factory, bout_xyt_example_files):
path = bout_xyt_example_files(
tmp_path_factory, nxpe=1, nype=1, nt=1, write_to_disk=True
)
with pytest.warns(UserWarning):
actual = open_boutdataset(datapath=path, keep_xboundaries=False)
expected = create_bout_ds()
expected = expected.set_coords(["t_array", "dx", "dy", "dz"]).rename(
t_array="t"
)
xrt.assert_equal(
actual.drop_vars(["x", "y", "z"]).load(),
expected.drop_vars(
METADATA_VARS
+ _BOUT_PER_PROC_VARIABLES
+ _BOUT_TIME_DEPENDENT_META_VARS,
errors="ignore",
),
)
# check creation without writing to disk gives identical result
fake_ds_list = bout_xyt_example_files(None, nxpe=1, nype=1, nt=1)
with pytest.warns(UserWarning):
fake = open_boutdataset(datapath=fake_ds_list, keep_xboundaries=False)
xrt.assert_identical(actual, fake)
def test_squashed_file(self, tmp_path_factory, bout_xyt_example_files):
path = bout_xyt_example_files(
tmp_path_factory, nxpe=4, nype=3, nt=1, squashed=True, write_to_disk=True
)
with pytest.warns(UserWarning):
actual = open_boutdataset(datapath=path, keep_xboundaries=False)
expected = create_bout_ds(lengths=(6, 8, 12, 7))
expected = expected.set_coords(["t_array", "dx", "dy", "dz"]).rename(
t_array="t"
)
xrt.assert_equal(
actual.drop_vars(["x", "y", "z"]).load(),
expected.drop_vars(
METADATA_VARS
+ _BOUT_PER_PROC_VARIABLES
+ _BOUT_TIME_DEPENDENT_META_VARS,
errors="ignore",
),
)
# check creation without writing to disk gives identical result
fake_ds_list = bout_xyt_example_files(None, nxpe=4, nype=3, nt=1, squashed=True)
with pytest.warns(UserWarning):
fake = open_boutdataset(datapath=fake_ds_list, keep_xboundaries=False)
xrt.assert_identical(actual, fake)
@pytest.mark.parametrize(
"keep_xboundaries", [False, pytest.param(True, marks=pytest.mark.long)]
)
@pytest.mark.parametrize(
"keep_yboundaries", [False, pytest.param(True, marks=pytest.mark.long)]
)
def test_squashed_doublenull(
self,
tmp_path_factory,
bout_xyt_example_files,
keep_xboundaries,
keep_yboundaries,
):
path = bout_xyt_example_files(
tmp_path_factory,
nxpe=4,
nype=6,
nt=1,
lengths=(6, 2, 4, 7),
guards={"x": 2, "y": 2},
squashed=True,
topology="lower-disconnected-double-null",
)
with pytest.warns(UserWarning):
ds = open_boutdataset(
datapath=path,
keep_xboundaries=keep_xboundaries,
keep_yboundaries=keep_yboundaries,
)
# bout_xyt_example_files when creating a 'squashed' file just makes it with
# y-size nype*lengths[2]+2*myg, which is 6*4+4=28, so with upper and lower
# boundaries removed, y-size should be 28-4*2=20.
assert ds.sizes["t"] == 6
assert ds.sizes["x"] == 12 if keep_xboundaries else 8
assert ds.sizes["y"] == 32 if keep_yboundaries else 24
assert ds.sizes["z"] == 7
@pytest.mark.parametrize(
"keep_xboundaries", [False, pytest.param(True, marks=pytest.mark.long)]
)
@pytest.mark.parametrize(
"keep_yboundaries", [False, pytest.param(True, marks=pytest.mark.long)]
)
def test_squashed_doublenull_file(
self,
tmp_path_factory,
bout_xyt_example_files,
keep_xboundaries,
keep_yboundaries,
):
path = bout_xyt_example_files(
tmp_path_factory,
nxpe=4,
nype=6,
nt=1,
lengths=(6, 4, 4, 7),
guards={"x": 2, "y": 2},
squashed=True,
write_to_disk=True,
topology="upper-disconnected-double-null",
)
with pytest.warns(UserWarning):
ds = open_boutdataset(
datapath=path,
keep_xboundaries=keep_xboundaries,
keep_yboundaries=keep_yboundaries,
)
# bout_xyt_example_files when creating a 'squashed' file just makes it with
# y-size nype*lengths[2]+2*myg, which is 6*4+4=28, so with upper and lower
# boundaries removed, y-size should be 28-4*2=20.
assert ds.sizes["t"] == 6
assert ds.sizes["x"] == 20 if keep_xboundaries else 16
assert ds.sizes["y"] == 32 if keep_yboundaries else 24
assert ds.sizes["z"] == 7
def test_combine_along_x(self, tmp_path_factory, bout_xyt_example_files):
path = bout_xyt_example_files(
tmp_path_factory,
nxpe=4,
nype=1,
nt=1,
syn_data_type="stepped",
write_to_disk=True,
)
with pytest.warns(UserWarning):
actual = open_boutdataset(datapath=path, keep_xboundaries=False)
bout_ds = create_bout_ds
expected = concat(
[bout_ds(0), bout_ds(1), bout_ds(2), bout_ds(3)],
dim="x",
data_vars="minimal",
)
expected = expected.set_coords(["t_array", "dx", "dy", "dz"]).rename(
t_array="t"
)
xrt.assert_equal(
actual.drop_vars(["x", "y", "z"]).load(),
expected.drop_vars(
METADATA_VARS + _BOUT_PER_PROC_VARIABLES, errors="ignore"
),
)
# check creation without writing to disk gives identical result
fake_ds_list = bout_xyt_example_files(
None, nxpe=4, nype=1, nt=1, syn_data_type="stepped"
)
with pytest.warns(UserWarning):
fake = open_boutdataset(datapath=fake_ds_list, keep_xboundaries=False)
xrt.assert_identical(actual, fake)
def test_combine_along_y(self, tmp_path_factory, bout_xyt_example_files):
path = bout_xyt_example_files(
tmp_path_factory,
nxpe=1,
nype=3,
nt=1,
syn_data_type="stepped",
write_to_disk=True,
)
with pytest.warns(UserWarning):
actual = open_boutdataset(datapath=path, keep_xboundaries=False)
bout_ds = create_bout_ds
expected = concat(
[bout_ds(0), bout_ds(1), bout_ds(2)], dim="y", data_vars="minimal"
)
expected = expected.set_coords(["t_array", "dx", "dy", "dz"]).rename(
t_array="t"
)
xrt.assert_equal(
actual.drop_vars(["x", "y", "z"]).load(),
expected.drop_vars(
METADATA_VARS + _BOUT_PER_PROC_VARIABLES, errors="ignore"
),
)
# check creation without writing to disk gives identical result
fake_ds_list = bout_xyt_example_files(
None, nxpe=1, nype=3, nt=1, syn_data_type="stepped"
)
with pytest.warns(UserWarning):
fake = open_boutdataset(datapath=fake_ds_list, keep_xboundaries=False)
xrt.assert_identical(actual, fake)
@pytest.mark.skip
def test_combine_along_t(self): ...
@pytest.mark.parametrize(
"bout_v5,metric_3D", [(False, False), (True, False), (True, True)]
)
@pytest.mark.parametrize("lengths", [(6, 2, 4, 7), (6, 2, 4, 1)])
def test_combine_along_xy(
self, tmp_path_factory, bout_xyt_example_files, bout_v5, metric_3D, lengths
):
path = bout_xyt_example_files(
tmp_path_factory,
nxpe=4,
nype=3,
nt=1,
lengths=lengths,
syn_data_type="stepped",
write_to_disk=True,
bout_v5=bout_v5,
metric_3D=metric_3D,
)
with pytest.warns(UserWarning):
actual = open_boutdataset(datapath=path, keep_xboundaries=False)
def bout_ds(syn_data_type):
return create_bout_ds(
syn_data_type, bout_v5=bout_v5, metric_3D=metric_3D, lengths=lengths
)
line1 = concat(
[bout_ds(0), bout_ds(1), bout_ds(2), bout_ds(3)],
dim="x",
data_vars="minimal",
)
line2 = concat(
[bout_ds(4), bout_ds(5), bout_ds(6), bout_ds(7)],
dim="x",
data_vars="minimal",
)
line3 = concat(
[bout_ds(8), bout_ds(9), bout_ds(10), bout_ds(11)],
dim="x",
data_vars="minimal",
)
expected = concat([line1, line2, line3], dim="y", data_vars="minimal")
expected = expected.set_coords(["t_array", "dx", "dy", "dz"]).rename(
t_array="t"
)
vars_to_drop = METADATA_VARS + _BOUT_PER_PROC_VARIABLES
xrt.assert_equal(
actual.drop_vars(["x", "y", "z"]).load(),
expected.drop_vars(vars_to_drop, errors="ignore"),
)
# check creation without writing to disk gives identical result
fake_ds_list = bout_xyt_example_files(
None,
nxpe=4,
nype=3,
nt=1,
lengths=lengths,
syn_data_type="stepped",
bout_v5=bout_v5,
metric_3D=metric_3D,
)
with pytest.warns(UserWarning):
fake = open_boutdataset(datapath=fake_ds_list, keep_xboundaries=False)
xrt.assert_identical(actual, fake)
def test_toroidal(self, tmp_path_factory, bout_xyt_example_files):
# actually write these to disk to test the loading fully
path = bout_xyt_example_files(
tmp_path_factory,
nxpe=3,
nype=3,
nt=1,
syn_data_type="stepped",
grid="grid",
write_to_disk=True,
)
actual = open_boutdataset(
datapath=path,
geometry="toroidal",
gridfilepath=path.parent.joinpath("grid.nc"),
)
# check dataset can be saved
save_dir = tmp_path_factory.mktemp("data")
actual.bout.save(save_dir.joinpath("boutdata.nc"))
# check creation without writing to disk gives identical result
fake_ds_list, fake_grid_ds = bout_xyt_example_files(
None,
nxpe=3,
nype=3,
nt=1,
syn_data_type="stepped",
grid="grid",
)
fake = open_boutdataset(
datapath=fake_ds_list, geometry="toroidal", gridfilepath=fake_grid_ds
)
xrt.assert_identical(actual, fake)
def test_salpha(self, tmp_path_factory, bout_xyt_example_files):
path = bout_xyt_example_files(
tmp_path_factory,
nxpe=3,
nype=3,
nt=1,
syn_data_type="stepped",
grid="grid",
write_to_disk=True,
)
actual = open_boutdataset(
datapath=path,
geometry="s-alpha",
gridfilepath=path.parent.joinpath("grid.nc"),
)
# check dataset can be saved
save_dir = tmp_path_factory.mktemp("data")
actual.bout.save(save_dir.joinpath("boutdata.nc"))
# check creation without writing to disk gives identical result
fake_ds_list, fake_grid_ds = bout_xyt_example_files(
None, nxpe=3, nype=3, nt=1, syn_data_type="stepped", grid="grid"
)
fake = open_boutdataset(
datapath=fake_ds_list, geometry="s-alpha", gridfilepath=fake_grid_ds
)
xrt.assert_identical(actual, fake)
def test_drop_vars(self, tmp_path_factory, bout_xyt_example_files):
datapath = bout_xyt_example_files(
tmp_path_factory,
nxpe=4,
nype=1,
nt=1,
syn_data_type="stepped",
write_to_disk=True,
)
with pytest.warns(UserWarning):
ds = open_boutdataset(
datapath=datapath, keep_xboundaries=False, drop_variables=["T"]
)
assert "T" not in ds.keys()
assert "n" in ds.keys()
@pytest.mark.skip
def test_combine_along_tx(self): ...
def test_restarts(self):
datapath = Path(__file__).parent.joinpath(
"data", "restart", "BOUT.restart.*.nc"
)
ds = open_boutdataset(datapath, keep_xboundaries=True, keep_yboundaries=True)
assert "T" in ds
_test_processor_layouts_list = [
# No parallelization
(0, 0, 1, 1, {"x": True, "y": True}, {"x": True, "y": True}),
# 1d parallelization along x:
# Left
(0, 0, 3, 1, {"x": True, "y": True}, {"x": False, "y": True}),
# Middle
(1, 0, 3, 1, {"x": False, "y": True}, {"x": False, "y": True}),
# Right
(2, 0, 3, 1, {"x": False, "y": True}, {"x": True, "y": True}),
# 1d parallelization along y:
# Bottom
(0, 0, 1, 3, {"x": True, "y": True}, {"x": True, "y": False}),
# Middle
(0, 1, 1, 3, {"x": True, "y": False}, {"x": True, "y": False}),
# Top
(0, 2, 1, 3, {"x": True, "y": False}, {"x": True, "y": True}),
# 2d parallelization:
# Bottom left corner
(0, 0, 3, 4, {"x": True, "y": True}, {"x": False, "y": False}),
# Bottom right corner
(2, 0, 3, 4, {"x": False, "y": True}, {"x": True, "y": False}),
# Top left corner
(0, 3, 3, 4, {"x": True, "y": False}, {"x": False, "y": True}),
# Top right corner
(2, 3, 3, 4, {"x": False, "y": False}, {"x": True, "y": True}),
# Centre
(1, 2, 3, 4, {"x": False, "y": False}, {"x": False, "y": False}),
# Left side
(0, 2, 3, 4, {"x": True, "y": False}, {"x": False, "y": False}),
# Right side
(2, 2, 3, 4, {"x": False, "y": False}, {"x": True, "y": False}),
# Bottom side
(1, 0, 3, 4, {"x": False, "y": True}, {"x": False, "y": False}),
# Top side
(1, 3, 3, 4, {"x": False, "y": False}, {"x": False, "y": True}),
]
_test_processor_layouts_doublenull_list = [
# 1d parallelization along y:
# Bottom
(0, 0, 1, 4, {"x": True, "y": True}, {"x": True, "y": False}),
# Lower Middle
(0, 1, 1, 4, {"x": True, "y": False}, {"x": True, "y": True}),
# Upper Middle
(0, 2, 1, 4, {"x": True, "y": True}, {"x": True, "y": False}),
# Top
(0, 3, 1, 4, {"x": True, "y": False}, {"x": True, "y": True}),
# 2d parallelization:
# Bottom left corner
(0, 0, 3, 4, {"x": True, "y": True}, {"x": False, "y": False}),
(1, 0, 3, 4, {"x": False, "y": True}, {"x": False, "y": False}),
# Bottom right corner
(2, 0, 3, 4, {"x": False, "y": True}, {"x": True, "y": False}),
(0, 1, 3, 4, {"x": True, "y": False}, {"x": False, "y": True}),
(1, 1, 3, 4, {"x": False, "y": False}, {"x": False, "y": True}),
(2, 1, 3, 4, {"x": False, "y": False}, {"x": True, "y": True}),
(0, 2, 3, 4, {"x": True, "y": True}, {"x": False, "y": False}),
(1, 2, 3, 4, {"x": False, "y": True}, {"x": False, "y": False}),
(2, 2, 3, 4, {"x": False, "y": True}, {"x": True, "y": False}),
# Top left corner
(0, 3, 3, 4, {"x": True, "y": False}, {"x": False, "y": True}),
(1, 3, 3, 4, {"x": False, "y": False}, {"x": False, "y": True}),
# Top right corner
(2, 3, 3, 4, {"x": False, "y": False}, {"x": True, "y": True}),
]
class TestTrim:
@pytest.mark.parametrize("is_restart", [False, True])
def test_no_trim(self, is_restart):
ds = create_test_data(0)
# Manually add filename - encoding normally added by xr.open_dataset
ds.encoding["source"] = "folder0/BOUT.dmp.0.nc"
actual = _trim(
ds, guards={}, keep_boundaries={}, nxpe=1, nype=1, is_restart=is_restart
)
xrt.assert_equal(actual, ds)
def test_trim_guards(self):
ds = create_test_data(0)
# Manually add filename - encoding normally added by xr.open_dataset
ds.encoding["source"] = "folder0/BOUT.dmp.0.nc"
actual = _trim(
ds, guards={"time": 2}, keep_boundaries={}, nxpe=1, nype=1, is_restart=False
)
selection = {"time": slice(2, -2)}
expected = ds.isel(**selection)
xrt.assert_equal(expected, actual)
@pytest.mark.parametrize(
"xproc, yproc, nxpe, nype, lower_boundaries, upper_boundaries",
_test_processor_layouts_list,
)
def test_infer_boundaries_2d_parallelization(
self, xproc, yproc, nxpe, nype, lower_boundaries, upper_boundaries
):
"""
Numbering scheme for nxpe=3, nype=4
.. code:: text
y 9 10 11
^ 6 7 8
| 3 4 5
| 0 1 2
-----> x
"""
ds = create_test_data(0)
ds["jyseps2_1"] = 0
ds["jyseps1_2"] = 0
ds["PE_XIND"] = xproc
ds["PE_YIND"] = yproc
actual_lower_boundaries, actual_upper_boundaries = _infer_contains_boundaries(
ds, nxpe, nype
)
assert actual_lower_boundaries == lower_boundaries
assert actual_upper_boundaries == upper_boundaries
@pytest.mark.parametrize(
"xproc, yproc, nxpe, nype, lower_boundaries, upper_boundaries",
_test_processor_layouts_doublenull_list,
)
def test_infer_boundaries_2d_parallelization_doublenull(
self, xproc, yproc, nxpe, nype, lower_boundaries, upper_boundaries
):
"""
Numbering scheme for nxpe=3, nype=4
.. code:: text
y 9 10 11
^ 6 7 8
| 3 4 5
| 0 1 2
-----> x
"""
ds = create_test_data(0)
ds["jyseps2_1"] = 3
ds["jyseps1_2"] = 11
ds["ny_inner"] = 8
ds["MYSUB"] = 4
ds["PE_XIND"] = xproc
ds["PE_YIND"] = yproc
actual_lower_boundaries, actual_upper_boundaries = _infer_contains_boundaries(
ds, nxpe, nype
)
assert actual_lower_boundaries == lower_boundaries
assert actual_upper_boundaries == upper_boundaries
@pytest.mark.parametrize(
"xproc, yproc, nxpe, nype, lower_boundaries, upper_boundaries",
_test_processor_layouts_list,
)
def test_infer_boundaries_2d_parallelization_by_filenum(
self, xproc, yproc, nxpe, nype, lower_boundaries, upper_boundaries
):
"""
Numbering scheme for nxpe=3, nype=4
.. code:: text
y 9 10 11
^ 6 7 8
| 3 4 5
| 0 1 2
-----> x
"""
filenum = yproc * nxpe + xproc
ds = create_test_data(0)
ds["jyseps2_1"] = 0
ds["jyseps1_2"] = 0
ds.encoding["source"] = "folder0/BOUT.dmp." + str(filenum) + ".nc"
actual_lower_boundaries, actual_upper_boundaries = _infer_contains_boundaries(
ds, nxpe, nype
)
assert actual_lower_boundaries == lower_boundaries
assert actual_upper_boundaries == upper_boundaries
@pytest.mark.parametrize(
"xproc, yproc, nxpe, nype, lower_boundaries, upper_boundaries",
_test_processor_layouts_doublenull_list,
)
def test_infer_boundaries_2d_parallelization_doublenull_by_filenum(
self, xproc, yproc, nxpe, nype, lower_boundaries, upper_boundaries
):
"""
Numbering scheme for nxpe=3, nype=4
.. code:: text
y 9 10 11
^ 6 7 8
| 3 4 5
| 0 1 2
-----> x
"""
filenum = yproc * nxpe + xproc
ds = create_test_data(0)
ds["jyseps2_1"] = 3
ds["jyseps1_2"] = 11
ds["ny_inner"] = 8
ds["MYSUB"] = 4
ds.encoding["source"] = "folder0/BOUT.dmp." + str(filenum) + ".nc"
actual_lower_boundaries, actual_upper_boundaries = _infer_contains_boundaries(
ds, nxpe, nype
)
assert actual_lower_boundaries == lower_boundaries
assert actual_upper_boundaries == upper_boundaries
@pytest.mark.parametrize("is_restart", [False, True])
def test_keep_xboundaries(self, is_restart):
ds = create_test_data(0)
ds = ds.rename({"dim2": "x"})
# Manually add filename - encoding normally added by xr.open_dataset
ds.encoding["source"] = "folder0/BOUT.dmp.0.nc"
ds["jyseps2_1"] = 8
ds["jyseps1_2"] = 8
actual = _trim(
ds,
guards={"x": 2},
keep_boundaries={"x": True},
nxpe=1,
nype=1,
is_restart=is_restart,
)
expected = ds # Should be unchanged
xrt.assert_equal(expected, actual)
@pytest.mark.parametrize("is_restart", [False, True])
def test_keep_yboundaries(self, is_restart):
ds = create_test_data(0)
ds = ds.rename({"dim2": "y"})
# Manually add filename - encoding normally added by xr.open_dataset
ds.encoding["source"] = "folder0/BOUT.dmp.0.nc"
ds["jyseps2_1"] = 8
ds["jyseps1_2"] = 8
actual = _trim(
ds,
guards={"y": 2},
keep_boundaries={"y": True},
nxpe=1,
nype=1,
is_restart=is_restart,
)
expected = ds # Should be unchanged
xrt.assert_equal(expected, actual)
@pytest.mark.parametrize(
"filenum, lower, upper",
[(0, True, False), (1, False, True), (2, True, False), (3, False, True)],
)
@pytest.mark.parametrize("is_restart", [False, True])
def test_keep_yboundaries_doublenull_by_filenum(
self, filenum, lower, upper, is_restart
):
ds = create_test_data(0)
ds = ds.rename({"dim2": "y"})
# Manually add filename - encoding normally added by xr.open_dataset
ds.encoding["source"] = "folder0/BOUT.dmp." + str(filenum) + ".nc"
ds["jyseps2_1"] = 3
ds["jyseps1_2"] = 11
ds["ny_inner"] = 8
ds["MYSUB"] = 4
actual = _trim(
ds,
guards={"y": 2},
keep_boundaries={"y": True},
nxpe=1,
nype=4,
is_restart=is_restart,
)
expected = ds # Should be unchanged
if not lower:
expected = expected.isel(y=slice(2, None, None))
if not upper:
expected = expected.isel(y=slice(None, -2, None))
xrt.assert_equal(expected, actual)
@pytest.mark.parametrize("is_restart", [False, True])
def test_trim_timing_info(self, is_restart):
ds = create_test_data(0)
from xbout.load import _BOUT_PER_PROC_VARIABLES
# remove a couple of entries from _BOUT_PER_PROC_VARIABLES so we test that _trim
# does not fail if not all of them are present
_BOUT_PER_PROC_VARIABLES = _BOUT_PER_PROC_VARIABLES[:-2]
for v in _BOUT_PER_PROC_VARIABLES:
ds[v] = 42.0
ds = _trim(
ds, guards={}, keep_boundaries={}, nxpe=1, nype=1, is_restart=is_restart
)
expected = create_test_data(0)
xrt.assert_equal(ds, expected)