File: test_composite.py

package info (click to toggle)
python-pyvista 0.46.4-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 176,968 kB
  • sloc: python: 94,346; sh: 216; makefile: 70
file content (1546 lines) | stat: -rw-r--r-- 55,683 bytes parent folder | download
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
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
from __future__ import annotations

from collections.abc import Generator
import importlib
import itertools
import pathlib
import re
import weakref

import numpy as np
import pytest
import vtk

import pyvista as pv
from pyvista import ImageData
from pyvista import MultiBlock
from pyvista import PolyData
from pyvista import PyVistaDeprecationWarning
from pyvista import RectilinearGrid
from pyvista import StructuredGrid
from pyvista import examples as ex
from pyvista.core.dataobject import USER_DICT_KEY


def test_multi_block_init_vtk():
    multi = vtk.vtkMultiBlockDataSet()
    multi.SetBlock(0, vtk.vtkRectilinearGrid())
    multi.SetBlock(1, vtk.vtkStructuredGrid())
    multi = MultiBlock(multi)
    assert isinstance(multi, MultiBlock)
    assert multi.n_blocks == 2
    assert isinstance(multi.GetBlock(0), RectilinearGrid)
    assert isinstance(multi.GetBlock(1), StructuredGrid)
    multi = vtk.vtkMultiBlockDataSet()
    multi.SetBlock(0, vtk.vtkRectilinearGrid())
    multi.SetBlock(1, vtk.vtkStructuredGrid())
    multi = MultiBlock(multi, deep=True)
    assert isinstance(multi, MultiBlock)
    assert multi.n_blocks == 2
    assert isinstance(multi.GetBlock(0), RectilinearGrid)
    assert isinstance(multi.GetBlock(1), StructuredGrid)
    # Test nested structure
    multi = vtk.vtkMultiBlockDataSet()
    multi.SetBlock(0, vtk.vtkRectilinearGrid())
    multi.SetBlock(1, vtk.vtkImageData())
    nested = vtk.vtkMultiBlockDataSet()
    nested.SetBlock(0, vtk.vtkUnstructuredGrid())
    nested.SetBlock(1, vtk.vtkStructuredGrid())
    multi.SetBlock(2, nested)
    # Wrap the nested structure
    multi = MultiBlock(multi)
    assert isinstance(multi, MultiBlock)
    assert multi.n_blocks == 3
    assert isinstance(multi.GetBlock(0), RectilinearGrid)
    assert isinstance(multi.GetBlock(1), ImageData)
    assert isinstance(multi.GetBlock(2), MultiBlock)


def test_multi_block_init_dict(rectilinear, airplane):
    data = {'grid': rectilinear, 'poly': airplane}
    multi = MultiBlock(data)
    assert isinstance(multi, MultiBlock)
    assert multi.n_blocks == 2
    # Note that dictionaries do not maintain order
    assert isinstance(multi.GetBlock(0), (RectilinearGrid, PolyData))
    assert multi.get_block_name(0) in ['grid', 'poly']
    assert isinstance(multi.GetBlock(1), (RectilinearGrid, PolyData))
    assert multi.get_block_name(1) in ['grid', 'poly']


def test_multi_block_keys(rectilinear, airplane):
    data = {'grid': rectilinear, 'poly': airplane}
    multi = MultiBlock(data)
    assert len(multi.keys()) == 2
    assert 'grid' in multi.keys()
    assert 'poly' in multi.keys()


def test_multi_block_init_list(rectilinear, airplane):
    data = [rectilinear, airplane]
    multi = MultiBlock(data)
    assert isinstance(multi, MultiBlock)
    assert multi.n_blocks == 2
    assert isinstance(multi.GetBlock(0), RectilinearGrid)
    assert isinstance(multi.GetBlock(1), PolyData)


def test_multi_block_append(ant, sphere, uniform, airplane, rectilinear):
    """This puts all of the example data objects into a a MultiBlock container"""
    multi = MultiBlock()
    # Add and test examples
    datasets = (ant, sphere, uniform, airplane, rectilinear)
    for i, dataset in enumerate(datasets):
        multi.append(dataset)
        assert multi.n_blocks == i + 1
        assert isinstance(multi[i], type(dataset))
    assert multi.bounds is not None
    # Now overwrite a block
    multi[4] = pv.Sphere()
    assert isinstance(multi[4], PolyData)
    multi[4] = vtk.vtkUnstructuredGrid()
    assert isinstance(multi[4], pv.UnstructuredGrid)

    with pytest.raises(ValueError, match='Cannot nest a composite dataset in itself.'):
        multi.append(multi)

    with pytest.raises(TypeError, match='dataset should not be or contain an array'):
        multi.append(vtk.vtkFloatArray())


def test_multi_block_set_get_ers():
    """This puts all of the example data objects into a a MultiBlock container"""
    multi = MultiBlock()
    # Set the number of blocks
    multi.n_blocks = 6
    assert multi.GetNumberOfBlocks() == 6  # Check that VTK side registered it
    assert multi.n_blocks == 6  # Check pyvista side registered it
    # Add data to the MultiBlock
    data = ex.load_rectilinear()
    multi[1] = data
    multi.set_block_name(1, 'rect')
    # Make sure number of blocks is constant
    assert multi.n_blocks == 6
    # Check content
    assert isinstance(multi[1], RectilinearGrid)
    for i in [0, 2, 3, 4, 5]:
        assert multi[i] is None
    # Check the bounds
    assert multi.bounds == data.bounds
    multi[5] = ex.load_uniform()
    multi.set_block_name(5, 'uni')
    multi.set_block_name(5, None)  # Make sure it doesn't get overwritten
    assert isinstance(multi.get(5), ImageData)
    # Test get by name
    assert isinstance(multi['uni'], ImageData)
    assert isinstance(multi['rect'], RectilinearGrid)
    assert isinstance(multi.get('uni'), ImageData)
    assert multi.get('no key') is None
    assert multi.get('no key', default=pv.Sphere()) == pv.Sphere()
    # Test the del operator
    del multi[0]
    assert multi.n_blocks == 5
    # Make sure the rect grid was moved up
    assert isinstance(multi[0], RectilinearGrid)
    assert multi.get_block_name(0) == 'rect'
    assert multi.get_block_name(2) is None
    # test del by name
    del multi['uni']
    assert multi.n_blocks == 4
    # test the pop operator
    pop = multi.pop(0)
    assert isinstance(pop, RectilinearGrid)
    assert multi.n_blocks == 3
    assert all(k is None for k in multi.keys())

    multi['new key'] = pv.Sphere()
    assert multi.n_blocks == 4
    assert multi[3] == pv.Sphere()

    multi['new key'] = pv.Cube()
    assert multi.n_blocks == 4
    assert multi[3] == pv.Cube()

    with pytest.raises(KeyError):
        _ = multi.get_index_by_name('foo')

    with pytest.raises(IndexError):
        multi[4] = ImageData()

    with pytest.raises(KeyError):
        multi['not a key']
    with pytest.raises(TypeError):
        data = multi[[0, 1]]

    with pytest.raises(TypeError):
        multi[1, 'foo'] = data


def test_set_block_name_by_name(ant):
    old_name = 'foo'
    new_name = 'bar'
    multi = pv.MultiBlock({old_name: ant})
    multi.set_block_name(old_name, new_name)
    assert multi.keys() == [new_name]


def test_replace():
    spheres = {f'{i}': pv.Sphere(phi_resolution=i + 3) for i in range(10)}
    multi = MultiBlock(spheres)
    cube = pv.Cube()
    multi.replace(3, cube)
    assert multi.get_block_name(3) == '3'
    assert multi[3] is cube


def test_pop():
    spheres = {f'{i}': pv.Sphere(phi_resolution=i + 3) for i in range(10)}
    multi = MultiBlock(spheres)
    assert multi.pop() == spheres['9']
    assert spheres['9'] not in multi
    assert multi.pop(0) == spheres['0']
    assert spheres['0'] not in multi


def test_del_slice(sphere):
    multi = MultiBlock({f'{i}': sphere for i in range(10)})
    del multi[0:10:2]
    assert len(multi) == 5
    assert all(f'{i}' in multi.keys() for i in range(1, 10, 2))

    multi = MultiBlock({f'{i}': sphere for i in range(10)})
    del multi[5:2:-1]
    assert len(multi) == 7
    assert all(f'{i}' in multi.keys() for i in [0, 1, 2, 6, 7, 8, 9])


def test_slicing_multiple_in_setitem(sphere):
    # equal length
    multi = MultiBlock({f'{i}': sphere for i in range(10)})
    multi[1:3] = [pv.Cube(), pv.Cube()]
    assert multi[1] == pv.Cube()
    assert multi[2] == pv.Cube()
    assert multi.count(pv.Cube()) == 2
    assert len(multi) == 10

    # len(slice) < len(data)
    multi = MultiBlock({f'{i}': sphere for i in range(10)})
    multi[1:3] = [pv.Cube(), pv.Cube(), pv.Cube()]
    assert multi[1] == pv.Cube()
    assert multi[2] == pv.Cube()
    assert multi[3] == pv.Cube()
    assert multi.count(pv.Cube()) == 3
    assert len(multi) == 11

    # len(slice) > len(data)
    multi = MultiBlock({f'{i}': sphere for i in range(10)})
    multi[1:3] = [pv.Cube()]
    assert multi[1] == pv.Cube()
    assert multi.count(pv.Cube()) == 1
    assert len(multi) == 9


@pytest.fixture
def nested_fixture():
    image = pv.ImageData()
    poly = pv.PolyData()
    grid = pv.UnstructuredGrid()
    nested = pv.MultiBlock(dict(image=image, poly=poly))
    multi = pv.MultiBlock(dict(grid=grid))
    nested.insert(1, multi, 'multi')
    return nested


@pytest.mark.parametrize(
    'replace_indices',
    [
        (0,),
        (1, 0),
        (2,),
    ],
)
def test_replace_nested(nested_fixture, replace_indices):
    nested = nested_fixture
    expected_keys = ['image', 'multi', 'poly']
    expected_flat_keys = ['image', 'grid', 'poly']

    nested.replace(replace_indices, None)
    assert nested.get_block(replace_indices) is None
    assert nested.keys() == expected_keys
    assert nested.flatten().keys() == expected_flat_keys


@pytest.mark.parametrize(
    'invalid_indices',
    [
        ((0, 0, 0), 'Invalid indices (0, 0, 0).'),
        ((0, 0), 'Invalid indices (0, 0).'),
    ],
)
def test_replace_nested_invalid_indices(nested_fixture, invalid_indices):
    nested = nested_fixture
    match = re.escape(invalid_indices[1])
    with pytest.raises(IndexError, match=match):
        nested.replace(invalid_indices[0], None)


def test_get_block(nested_fixture):
    index = (1, 0)
    name = 'grid'
    block_by_index = nested_fixture[index[0]].get_block(index[1])
    block_by_nested_index = nested_fixture.get_block(index)
    block_by_name = nested_fixture[index[0]].get_block(name)
    assert block_by_name is block_by_index is block_by_nested_index


def test_reverse(sphere):
    multi = MultiBlock({f'{i}': sphere for i in range(3)})
    multi.append(pv.Cube(), 'cube')
    multi.reverse()
    assert multi[0] == pv.Cube()
    assert np.array_equal(multi.keys(), ['cube', '2', '1', '0'])


def test_insert(sphere):
    multi = MultiBlock({f'{i}': sphere for i in range(3)})
    cube = pv.Cube()
    multi.insert(0, cube)
    assert len(multi) == 4
    assert multi[0] is cube

    # test with negative index and name
    multi.insert(-1, pv.ImageData(), name='uni')
    assert len(multi) == 5
    # inserted before last element
    assert isinstance(multi[-2], pv.ImageData)  # inserted before last element
    assert multi.get_block_name(-2) == 'uni'


def test_extend(sphere, uniform, ant):
    # test with Iterable
    multi = MultiBlock([sphere, ant])
    new_multi = [uniform, uniform]
    multi.extend(new_multi)
    assert len(multi) == 4
    assert multi.count(uniform) == 2

    # test with a MultiBlock
    multi = MultiBlock([sphere, ant])
    new_multi = MultiBlock({'uniform1': uniform, 'uniform2': uniform})
    multi.extend(new_multi)
    assert len(multi) == 4
    assert multi.count(uniform) == 2
    assert multi.keys()[-2] == 'uniform1'
    assert multi.keys()[-1] == 'uniform2'


def test_multi_block_clean(rectilinear, uniform, ant):
    # now test a clean of the null values
    multi = MultiBlock()
    multi.n_blocks = 6
    multi[1] = rectilinear
    multi.set_block_name(1, 'rect')
    multi[2] = PolyData()
    multi.set_block_name(2, 'empty')
    multi[3] = MultiBlock()
    multi.set_block_name(3, 'mempty')
    multi[5] = uniform
    multi.set_block_name(5, 'uni')
    # perform the clean to remove all Null elements
    multi.clean()
    assert multi.n_blocks == 2
    assert multi.GetNumberOfBlocks() == 2
    assert isinstance(multi[0], RectilinearGrid)
    assert isinstance(multi[1], ImageData)
    assert multi.get_block_name(0) == 'rect'
    assert multi.get_block_name(1) == 'uni'
    # Test a nested data struct
    foo = MultiBlock()
    foo.n_blocks = 4
    foo[3] = ant
    assert foo.n_blocks == 4
    multi = MultiBlock()
    multi.n_blocks = 6
    multi[1] = rectilinear
    multi.set_block_name(1, 'rect')
    multi[5] = foo
    multi.set_block_name(5, 'multi')
    # perform the clean to remove all Null elements
    assert multi.n_blocks == 6
    multi.clean()
    assert multi.n_blocks == 2
    assert multi.GetNumberOfBlocks() == 2
    assert isinstance(multi[0], RectilinearGrid)
    assert isinstance(multi[1], MultiBlock)
    assert multi.get_block_name(0) == 'rect'
    assert multi.get_block_name(1) == 'multi'
    assert foo.n_blocks == 1


def test_multi_block_repr(multiblock_all_with_nested_and_none):
    multi = multiblock_all_with_nested_and_none
    assert multi._repr_html_() is not None
    pattern = (
        r'MultiBlock \(0x[0-9a-fA-F]+\)'
        r'\s+N Blocks:\s{3}\d+'
        r'\s+X Bounds:\s{3}[+-]?\d*\.\d{3}e[+-]\d+,\s+[-+]?\d\.\d+e[+-]\d+'
        r'\s+Y Bounds:\s{3}[+-]?\d*\.\d{3}e[+-]\d+,\s+[-+]?\d\.\d+e[+-]\d+'
        r'\s+Z Bounds:\s{3}[+-]?\d*\.\d{3}e[+-]\d+,\s+[-+]?\d\.\d+e[+-]\d+'
    )
    match = re.search(pattern, repr(multi))
    assert repr(multi) == match.string
    assert str(multi) == match.string


def test_multi_block_repr_bounds():
    empty_poly = pv.PolyData().extract_cells(0)
    poly_x_bounds = repr(empty_poly).splitlines()[3]
    poly_y_bounds = repr(empty_poly).splitlines()[4]
    poly_z_bounds = repr(empty_poly).splitlines()[5]

    empty_multiblock = pv.MultiBlock([empty_poly])
    multi_x_bounds = repr(empty_multiblock).splitlines()[2]
    multi_y_bounds = repr(empty_multiblock).splitlines()[3]
    multi_z_bounds = repr(empty_multiblock).splitlines()[4]

    assert multi_x_bounds == poly_x_bounds
    assert multi_y_bounds == poly_y_bounds
    assert multi_z_bounds == poly_z_bounds


@pytest.mark.xfail(importlib.util.find_spec("paraview"),
                   reason="paraview provides inconsistent vtk")
def test_multi_block_eq(multiblock_all_with_nested_and_none):
    multi = multiblock_all_with_nested_and_none
    other = multi.copy()

    assert multi is not other
    assert multi == other

    assert pv.MultiBlock() == pv.MultiBlock()

    other[0] = pv.Sphere()
    assert multi != other

    other = multi.copy()
    other.set_block_name(0, 'not matching')
    assert multi != other

    other = multi.copy()
    other.append(pv.Sphere())
    assert multi != other


@pytest.mark.parametrize('binary', [True, False])
@pytest.mark.parametrize('extension', pv.core.composite.MultiBlock._WRITERS)
@pytest.mark.parametrize('use_pathlib', [True, False])
def test_multi_block_io(
    extension, binary, tmpdir, use_pathlib, multiblock_all_with_nested_and_none
):
    filename = str(tmpdir.mkdir('tmpdir').join(f'tmp.{extension}'))
    if use_pathlib:
        pathlib.Path(filename)

    # Use non-nested multiblock with no None types for vtkhdf case
    # these cases are tested separately
    multi = (
        pv.MultiBlock([pv.PolyData(), pv.UnstructuredGrid()])
        if extension == '.vtkhdf'
        else multiblock_all_with_nested_and_none
    )

    # Save it out
    if extension == '.vtkhdf' and binary is False:
        match = '.vtkhdf files can only be written in binary format'
        with pytest.raises(ValueError, match=match):
            multi.save(filename, binary=binary)
        return
    multi.save(filename, binary=binary)

    foo = MultiBlock(filename)
    assert foo.n_blocks == multi.n_blocks
    foo = pv.read(filename)
    assert foo.n_blocks == multi.n_blocks


@pytest.mark.needs_vtk_version(at_least=(9, 4, 0), less_than=(9, 5))
def test_multi_block_hdf_invalid_block_types_vtk94(tmpdir):
    filename = tmpdir / 'multi.vtkhdf'

    multi = pv.MultiBlock([pv.MultiBlock()])
    match = (
        'Nested MultiBlocks are not supported by the .vtkhdf format in VTK 9.4.\n'
        'Upgrade to VTK>=9.5 for this functionality.'
    )
    with pytest.raises(TypeError, match=match):
        multi.save(filename)

    multi = pv.MultiBlock([None])
    match = (
        'Saving None blocks is not supported by the .vtkhdf format in VTK 9.4.\n'
        'Upgrade to VTK>=9.5 for this functionality.'
    )
    with pytest.raises(TypeError, match=match):
        multi.save(filename)


@pytest.mark.needs_vtk_version(at_least=(9, 4, 0))
def test_multi_block_hdf_invalid_block_type(tmpdir):
    filename = tmpdir / 'multi.vtkhdf'

    multi = pv.MultiBlock([pv.ImageData()])
    match = (
        "Block at index [0] with name 'Block-00' has type 'ImageData' which cannot be saved to "
        "the .vtkhdf format.\nSupported types are: ['PolyData', 'UnstructuredGrid', 'NoneType', "
        "'MultiBlock', 'PartitionedDataSet']."
    )
    with pytest.raises(TypeError, match=re.escape(match)):
        multi.save(filename)


@pytest.mark.needs_vtk_version(at_least=(9, 5, 0))
def test_multi_block_hdf_invalid_nested_block(tmpdir):
    filename = tmpdir / 'multi.vtkhdf'

    multi = pv.MultiBlock([pv.MultiBlock({'nested_block': pv.PointSet()})])
    match = (
        "Block at index [0][0] with name 'nested_block' has type 'PointSet' which cannot be saved "
        'to the .vtkhdf format.'
    )
    with pytest.raises(TypeError, match=re.escape(match)):
        multi.save(filename)


@pytest.mark.parametrize('binary', [True, False])
@pytest.mark.parametrize('extension', ['vtm', 'vtmb'])
def test_ensight_multi_block_io(extension, binary, tmpdir):
    filename = str(tmpdir.mkdir('tmpdir').join(f'tmp.{extension}'))
    # multi = ex.load_bfs()  # .case file
    multi = ex.download_backward_facing_step()  # .case file
    # Now check everything
    assert multi.n_blocks == 4
    array_names = ['v2', 'nut', 'k', 'nuTilda', 'p', 'omega', 'f', 'epsilon', 'U']
    for block in multi:
        assert block.array_names == array_names
    # Save it out
    multi.save(filename, binary=binary)
    foo = MultiBlock(filename)
    assert foo.n_blocks == multi.n_blocks
    for block in foo:
        assert block.array_names == array_names
    foo = pv.read(filename)
    assert foo.n_blocks == multi.n_blocks
    for block in foo:
        assert block.array_names == array_names


def test_invalid_arg():
    with pytest.raises(TypeError):
        pv.MultiBlock(np.empty(10))
    with pytest.raises(ValueError):  # noqa: PT011
        pv.MultiBlock(np.empty(10), np.empty(10))


def test_multi_io_erros(tmpdir):
    fdir = tmpdir.mkdir('tmpdir')
    multi = MultiBlock()
    # Check saving with bad extension
    bad_ext_name = str(fdir.join('tmp.npy'))
    with pytest.raises(ValueError):  # noqa: PT011
        multi.save(bad_ext_name)
    arr = np.random.default_rng().random((10, 10))
    np.save(bad_ext_name, arr)
    # Load non existing file
    with pytest.raises(FileNotFoundError):
        _ = MultiBlock('foo.vtm')
    # Load bad extension
    with pytest.raises(IOError):  # noqa: PT011
        _ = MultiBlock(bad_ext_name)


def test_extract_geometry(multiblock_all_with_nested_and_none):
    geom = multiblock_all_with_nested_and_none.extract_geometry()
    assert isinstance(geom, PolyData)


def test_combine_filter(multiblock_all_with_nested_and_none):
    geom = multiblock_all_with_nested_and_none.combine()
    assert isinstance(geom, pv.UnstructuredGrid)


@pytest.mark.parametrize('inplace', [True, False])
def test_transform_filter(ant, sphere, airplane, tetbeam, inplace):
    # Set up
    multi = pv.MultiBlock([ant, sphere])
    nested = pv.MultiBlock([airplane, tetbeam])
    nested.append(None)
    multi.append(nested)
    multi.append(None)
    for i, _ in enumerate(multi):
        multi.set_block_name(i, str(i))

    NUMBER = 42
    transform = pv.Transform().translate(NUMBER, NUMBER, NUMBER)
    bounds_before = np.array(multi.bounds)
    n_blocks_before = multi.n_blocks
    keys_before = multi.keys()

    # Do test
    output = multi.transform(
        transform,
        inplace=inplace,
        transform_all_input_vectors=False,
        progress_bar=False,
    )
    bounds_after = np.array(output.bounds)
    n_blocks_after = output.n_blocks
    keys_after = output.keys()

    assert (output is multi) == inplace
    for block_in, block_out in zip(multi, output):
        assert (block_in is block_out) == inplace or (block_in is None)
    assert np.allclose(bounds_before + NUMBER, bounds_after)
    assert n_blocks_before == n_blocks_after
    assert keys_before == keys_after


@pytest.mark.parametrize('deep', [True, False])
def test_multi_block_copy(deep, multiblock_all_with_nested_and_none):
    multi = multiblock_all_with_nested_and_none
    multi_copy = multi.copy(deep=deep)
    assert multi.n_blocks == multi_copy.n_blocks
    for i in range(multi_copy.n_blocks):
        block = multi_copy.GetBlock(i)
        assert pv.is_pyvista_dataset(block) or block is None
        assert (multi[i] is multi_copy[i]) != deep or (multi[i] is None)


@pytest.mark.parametrize('recursive', [True, False])
def test_multi_block_shallow_copy(recursive, multiblock_all_with_nested_and_none):
    multi = multiblock_all_with_nested_and_none
    multi_copy = MultiBlock()
    multi_copy.shallow_copy(multi, recursive=recursive)
    assert multi.n_blocks == multi_copy.n_blocks
    for i, block in enumerate(multi_copy):
        assert pv.is_pyvista_dataset(block) or block is None
        if isinstance(multi[i], MultiBlock):
            assert (multi[i] is block) != recursive
        else:
            assert block is multi[i]


def test_multi_block_negative_index(ant, sphere, uniform, airplane, tetbeam):
    multi = pv.MultiBlock([ant, sphere, uniform, airplane, tetbeam])
    # Now check everything
    assert id(multi[-1]) == id(multi[4])
    assert id(multi[-2]) == id(multi[3])
    assert id(multi[-3]) == id(multi[2])
    assert id(multi[-4]) == id(multi[1])
    assert id(multi[-5]) == id(multi[0])
    with pytest.raises(IndexError):
        _ = multi[-6]

    multi[-1] = ant
    assert multi[4] == ant
    multi[-5] = tetbeam
    assert multi[0] == tetbeam

    with pytest.raises(IndexError):
        multi[-6] = uniform


def test_multi_slice_index(ant, sphere, uniform, airplane, tetbeam):
    multi = pv.MultiBlock([ant, sphere, uniform, airplane, tetbeam])
    # Now check everything
    sub = multi[0:3]
    assert len(sub) == 3
    for i in range(len(sub)):
        assert sub[i] is multi[i]
        assert sub.get_block_name(i) == multi.get_block_name(i)
    sub = multi[0:-1]
    assert len(sub) + 1 == len(multi)
    for i in range(len(sub)):
        assert sub[i] is multi[i]
        assert sub.get_block_name(i) == multi.get_block_name(i)
    sub = multi[0:-1:2]
    assert len(sub) == 2
    for i in range(len(sub)):
        j = i * 2
        assert sub[i] is multi[j]
        assert sub.get_block_name(i) == multi.get_block_name(j)

    sub = [airplane, tetbeam]
    multi[0:2] = sub
    assert multi[0] is airplane
    assert multi[1] is tetbeam


def test_slice_defaults(multiblock_all_with_nested_and_none):
    multi = multiblock_all_with_nested_and_none
    assert multi[:] == multi[0 : len(multi)]


def test_slice_negatives(multiblock_all_with_nested_and_none):
    multi = multiblock_all_with_nested_and_none
    test_multi = pv.MultiBlock({key: multi[key] for key in multi.keys()[::-1]})
    assert multi[::-1] == test_multi

    test_multi = pv.MultiBlock({key: multi[key] for key in multi.keys()[-2:]})
    assert multi[-2:] == test_multi

    test_multi = pv.MultiBlock({key: multi[key] for key in multi.keys()[:-1]})
    assert multi[:-1] == test_multi

    test_multi = pv.MultiBlock({key: multi[key] for key in multi.keys()[-1:-4:-2]})
    assert multi[-1:-4:-2] == test_multi


def test_multi_block_volume(ant, airplane, sphere, uniform):
    multi = pv.MultiBlock([ant, sphere, uniform, airplane, None])
    vols = ant.volume + sphere.volume + uniform.volume + airplane.volume
    assert multi.volume == pytest.approx(vols)


def test_multi_block_length(multiblock_all_with_nested_and_none):
    multi = multiblock_all_with_nested_and_none
    assert multi.length == pv.Box(bounds=multi.bounds).length


def test_multi_block_save_lines(tmpdir):
    radius = 1
    xr = np.random.default_rng().random(10)
    yr = np.random.default_rng().random(10)
    x = radius * np.sin(yr) * np.cos(xr)
    y = radius * np.sin(yr) * np.sin(xr)
    z = radius * np.cos(yr)
    xyz = np.stack((x, y, z), axis=1)

    poly = pv.lines_from_points(xyz, close=False)
    blocks = pv.MultiBlock()
    for _ in range(2):
        blocks.append(poly)

    path = tmpdir.mkdir('tmpdir')
    line_filename = str(path.join('lines.vtk'))
    block_filename = str(path.join('blocks.vtmb'))
    poly.save(line_filename)
    blocks.save(block_filename)

    poly_load = pv.read(line_filename)
    assert np.allclose(poly_load.points, poly.points)

    blocks_load = pv.read(block_filename)
    assert np.allclose(blocks_load[0].points, blocks[0].points)


def test_multi_block_data_range():
    # Create ambiguous point and cell data
    volume = pv.ImageData(dimensions=(10, 10, 10))
    point_data_value = 99
    cell_data_value = 42
    volume.point_data['data'] = np.ones((volume.n_points,)) * point_data_value
    volume.cell_data['data'] = np.ones((volume.n_cells,)) * cell_data_value

    # Create multiblock
    a = volume.slice_along_axis(5, 'x')
    with pytest.raises(KeyError):
        a.get_data_range('foo')
    mi, ma = a.get_data_range(volume.active_scalars_name, preference='point')
    assert mi == point_data_value
    assert ma == point_data_value

    mi, ma = a.get_data_range(volume.active_scalars_name, preference='cell')
    assert mi == cell_data_value
    assert ma == cell_data_value

    # Test on a nested MultiBlock
    b = volume.slice_along_axis(5, 'y')
    slices = pv.MultiBlock([a, b])
    with pytest.raises(KeyError):
        slices.get_data_range('foo')
    mi, ma = slices.get_data_range(volume.active_scalars_name)
    assert mi is not None
    assert ma is not None


def test_multiblock_ref():
    # can't use fixtures here as we need to remove all references for
    # garbage collection
    sphere = pv.Sphere()
    cube = pv.Cube()

    block = MultiBlock([sphere, cube])
    block[0]['a_new_var'] = np.zeros(block[0].n_points)
    assert 'a_new_var' in block[0].array_names

    assert sphere is block[0]
    assert cube is block[1]

    wref_sphere = weakref.ref(sphere)
    wref_cube = weakref.ref(cube)

    # verify reference remains
    assert wref_sphere() is sphere
    del sphere
    assert wref_sphere() is not None

    # verify __delitem__ works and removes reference
    del block[0]
    assert wref_sphere() is None

    # verify reference remains
    assert wref_cube() is cube

    # verify the __setitem__(index, None) edge case
    del cube
    block[0] = None
    assert wref_cube() is None


def test_set_active_scalars(multiblock_all):
    for block in multiblock_all:
        block.clear_data()
        block.point_data['data'] = range(block.n_points)
        block.point_data['point_data_a'] = range(block.n_points)
        block.point_data['point_data_b'] = range(block.n_points)

        block.cell_data['data'] = range(block.n_cells)
        block.cell_data['cell_data_a'] = range(block.n_cells)
        block.cell_data['cell_data_b'] = range(block.n_cells)

    # test none
    multiblock_all.set_active_scalars(None)
    for block in multiblock_all:
        assert block.point_data.active_scalars_name is None
        assert block.cell_data.active_scalars_name is None

    # test set point_data
    active_scalars_name = 'point_data_a'
    multiblock_all.set_active_scalars(active_scalars_name)
    for block in multiblock_all:
        assert block.point_data.active_scalars_name == active_scalars_name

    # test set point_data
    active_scalars_name = 'cell_data_a'
    multiblock_all.set_active_scalars(active_scalars_name)
    for block in multiblock_all:
        assert block.cell_data.active_scalars_name == active_scalars_name

    # test set point_data
    multiblock_all.set_active_scalars(None)
    active_scalars_name = 'data'
    multiblock_all.set_active_scalars(active_scalars_name, preference='point')
    for block in multiblock_all:
        assert block.point_data.active_scalars_name == active_scalars_name
        assert block.cell_data.active_scalars_name is None

    multiblock_all.set_active_scalars(None)
    active_scalars_name = 'data'
    multiblock_all.set_active_scalars(active_scalars_name, preference='cell')
    for block in multiblock_all:
        assert block.point_data.active_scalars_name is None
        assert block.cell_data.active_scalars_name == active_scalars_name

    # test partial
    multiblock_all[0].clear_data()
    multiblock_all.set_active_scalars(None)
    with pytest.raises(KeyError, match='does not exist'):
        multiblock_all.set_active_scalars('point_data_a')
    multiblock_all.set_active_scalars('point_data_a', allow_missing=True)
    assert multiblock_all[1].point_data.active_scalars_name == 'point_data_a'

    with pytest.raises(KeyError, match='is missing from all'):
        multiblock_all.set_active_scalars('does not exist', allow_missing=True)


def test_set_active_scalars_multi(multiblock_poly):
    multiblock_poly.set_active_scalars(None)

    block = multiblock_poly[0]
    block.point_data.set_array(range(block.n_points), 'data')
    block.cell_data.set_array(range(block.n_cells), 'data')

    block = multiblock_poly[1]
    block.point_data.set_array(range(block.n_points), 'data')

    multiblock_poly.set_active_scalars('data', preference='point', allow_missing=True)
    for block in multiblock_poly:
        if 'data' in block.point_data:
            assert block.point_data.active_scalars_name == 'data'
        else:
            assert block.point_data.active_scalars_name is None

    multiblock_poly.set_active_scalars('data', preference='cell', allow_missing=True)
    for block in multiblock_poly:
        if 'data' in block.cell_data:
            assert block.cell_data.active_scalars_name == 'data'
        else:
            assert block.cell_data.active_scalars_name is None


def test_set_active_scalars_components(multiblock_poly):
    multiblock_poly[0].point_data['data'] = range(multiblock_poly[0].n_points)
    multiblock_poly[1].point_data['data'] = range(multiblock_poly[1].n_points)
    multiblock_poly[2].point_data['data'] = range(multiblock_poly[2].n_points)

    multiblock_poly.set_active_scalars(None)
    multiblock_poly.set_active_scalars('data')
    for block in multiblock_poly:
        assert block.point_data.active_scalars_name == 'data'

    data = np.zeros((multiblock_poly[2].n_points, 3))
    multiblock_poly[2].point_data['data'] = data
    with pytest.raises(ValueError, match='Inconsistent dimensions'):
        multiblock_poly.set_active_scalars('data')

    data = np.arange(multiblock_poly[2].n_points, dtype=np.complex128)
    multiblock_poly[2].point_data['data'] = data
    with pytest.raises(ValueError, match='Inconsistent complex and real'):
        multiblock_poly.set_active_scalars('data')


def test_set_active_multi_multi(multiblock_poly):
    multi_multi = MultiBlock([multiblock_poly, multiblock_poly])
    with pytest.raises(KeyError, match='missing from all'):
        multi_multi.set_active_scalars('does-not-exist', allow_missing=True)

    multi_multi.set_active_scalars('multi-comp', allow_missing=True)


def test_set_active_scalars_mixed(multiblock_poly):
    for block in multiblock_poly:
        block.clear_data()
        block.point_data.set_array(range(block.n_points), 'data')
        block.cell_data.set_array(range(block.n_cells), 'data')

    # remove data from the last block
    del multiblock_poly[-1].point_data['data']
    del multiblock_poly[-1].cell_data['data']

    multiblock_poly.set_active_scalars('data', preference='cell', allow_missing=True)

    for block in multiblock_poly:
        if 'data' in block.cell_data:
            assert block.cell_data.active_scalars_name == 'data'

    multiblock_poly.set_active_scalars('data', preference='point', allow_missing=True)

    for block in multiblock_poly:
        if 'data' in block.point_data:
            assert block.point_data.active_scalars_name == 'data'


def test_as_polydata_blocks(multiblock_all_with_nested_and_none):
    multi = multiblock_all_with_nested_and_none
    if pv.vtk_version_info >= (9, 1, 0):
        multi.append(pv.PointSet([0.0, 0.0, 1.0]))  # missing pointset
    assert not multi.is_all_polydata
    # Get a polydata block for copy test
    poly_index = 3
    poly = multi[poly_index]
    assert isinstance(poly, pv.PolyData)

    dataset_a = multi.as_polydata_blocks()
    assert dataset_a[poly_index] is poly
    if pv.vtk_version_info >= (9, 1, 0):
        assert dataset_a[-1].n_points == 1
    assert not multi.is_all_polydata
    assert dataset_a.is_all_polydata

    # Test shallow copy
    dataset_copy = multi.as_polydata_blocks(copy=True)
    assert dataset_copy[poly_index] is not poly
    copied_points = dataset_copy[poly_index].points
    expected_points = poly.points
    assert np.shares_memory(copied_points, expected_points)

    # verify nested works
    nested_mblock = pv.MultiBlock([multi, multi])
    assert not nested_mblock.is_all_polydata
    dataset_b = nested_mblock.as_polydata_blocks()
    assert dataset_b.is_all_polydata


def test_as_unstructured_grid_blocks(multiblock_all_with_nested_and_none):
    multi = multiblock_all_with_nested_and_none
    if pv.vtk_version_info >= (9, 1, 0):
        multi.append(pv.PointSet([0.0, 0.0, 1.0]))  # missing pointset
    # Get a UnstructuredGrid block for copy test
    grid_index = 2
    grid = multi[grid_index]
    assert isinstance(grid, pv.UnstructuredGrid)

    new_multi = multi.as_unstructured_grid_blocks()
    assert all(isinstance(block, pv.UnstructuredGrid) for block in new_multi.recursive_iterator())
    assert new_multi[grid_index] is grid

    # Test shallow copy
    dataset_copy = multi.as_unstructured_grid_blocks(copy=True)
    assert dataset_copy[grid_index] is not grid
    copied_points = dataset_copy[grid_index].points
    expected_points = grid.points
    assert np.shares_memory(copied_points, expected_points)


def test_compute_normals(multiblock_poly):
    for block in multiblock_poly:
        block.clear_data()
        block['point_data'] = range(block.n_points)
    mblock = multiblock_poly._compute_normals(
        cell_normals=False,
        split_vertices=True,
        track_vertices=True,
    )
    for block in mblock:
        assert 'Normals' in block.point_data
        assert 'point_data' in block.point_data
        assert 'pyvistaOriginalPointIds' in block.point_data

    # test non-poly raises
    multiblock_poly.append(pv.UnstructuredGrid())
    with pytest.raises(RuntimeError, match='This multiblock contains non-PolyData'):
        multiblock_poly._compute_normals()


def test_activate_scalars(multiblock_poly):
    for block in multiblock_poly:
        data = np.array(['a'] * block.n_points)
        block.point_data.set_array(data, 'data')


def test_clear_all_data(multiblock_all):
    for block in multiblock_all:
        block.point_data['data'] = range(block.n_points)
        block.cell_data['data'] = range(block.n_cells)
    multiblock_all.append(multiblock_all.copy())
    multiblock_all.clear_all_data()
    for block in multiblock_all:
        if isinstance(block, MultiBlock):
            for subblock in block:
                assert subblock.point_data.keys() == []
                assert subblock.cell_data.keys() == []
        else:
            assert block.point_data.keys() == []
            assert block.cell_data.keys() == []


def test_clear_all_point_data(multiblock_all):
    for block in multiblock_all:
        block.point_data['data'] = range(block.n_points)
        block.cell_data['data'] = range(block.n_cells)
    multiblock_all.append(multiblock_all.copy())
    multiblock_all.clear_all_point_data()
    for block in multiblock_all:
        if isinstance(block, MultiBlock):
            for subblock in block:
                assert subblock.point_data.keys() == []
                assert subblock.cell_data.keys() != []
        else:
            assert block.point_data.keys() == []
            assert block.cell_data.keys() != []


def test_clear_all_cell_data(multiblock_all):
    for block in multiblock_all:
        block.point_data['data'] = range(block.n_points)
        block.cell_data['data'] = range(block.n_cells)
    multiblock_all.append(multiblock_all.copy())
    multiblock_all.clear_all_cell_data()
    for block in multiblock_all:
        if isinstance(block, MultiBlock):
            for subblock in block:
                assert subblock.point_data.keys() != []
                assert subblock.cell_data.keys() == []
        else:
            assert block.point_data.keys() != []
            assert block.cell_data.keys() == []


@pytest.mark.parametrize('container', [pv.MultiBlock, pv.PartitionedDataSet])
def test_multiblock_partitioned_zip(container):
    # Test `__iter__` and `__next__` inheritance
    list_ = [None, None]
    composite = container(list_)
    zipped_container = list(zip(composite, composite))
    zipped_list = list(zip(list_, list_))

    assert len(zipped_container) == len(zipped_list)
    assert len(zipped_container[0]) == len(zipped_list[0])
    for i, j in itertools.product(range(2), repeat=2):
        assert zipped_container[i][j] is zipped_list[i][j] is None


def test_transform_filter_inplace_default_warns(multiblock_poly):
    expected_msg = (
        'The default value of `inplace` for the filter `MultiBlock.transform` '
        'will change in the future.'
    )
    with pytest.warns(PyVistaDeprecationWarning, match=expected_msg):
        _ = multiblock_poly.transform(np.eye(4))


def test_recursive_iterator(multiblock_all_with_nested_and_none):
    # include an empty mesh
    multiblock_all_with_nested_and_none.append(pv.PolyData())

    # Test default does not skip None blocks or empty meshes by default
    iterator = multiblock_all_with_nested_and_none.recursive_iterator()
    assert isinstance(iterator, Generator)
    iterator_list = list(iterator)
    assert None in iterator_list
    assert all(isinstance(item, pv.DataSet) or item is None for item in iterator_list)
    assert any(item.n_points == 0 for item in iterator_list if item is not None)

    # Test skip None blocks
    iterator = multiblock_all_with_nested_and_none.recursive_iterator(skip_none=True)
    assert isinstance(iterator, Generator)
    iterator_list = list(iterator)
    assert None not in iterator_list
    assert all(isinstance(item, pv.DataSet) for item in iterator_list)

    # Test skip empty blocks
    iterator = multiblock_all_with_nested_and_none.recursive_iterator(skip_empty=True)
    assert isinstance(iterator, Generator)
    iterator_list = list(iterator)
    assert all(item.n_points > 0 for item in iterator_list if item is not None)


def test_recursive_iterator_node_type():
    empty = pv.MultiBlock()
    assert len(list(empty.recursive_iterator(node_type='parent'))) == 0

    nested_empty = pv.MultiBlock([empty])
    assert len(list(nested_empty.recursive_iterator(node_type='parent'))) == 1
    assert len(list(nested_empty.recursive_iterator(node_type='parent', skip_empty=True))) == 0

    nested_empty2 = pv.MultiBlock([nested_empty])
    assert len(list(nested_empty2.recursive_iterator(node_type='parent'))) == 2
    assert len(list(nested_empty2.recursive_iterator(node_type='parent', skip_empty=True))) == 1

    match = "Cannot skip None blocks when the node type is 'parent'."
    with pytest.raises(ValueError, match=match):
        empty.recursive_iterator(skip_none=True, node_type='parent')
    match = "Cannot set order when the node type is 'parent'."
    with pytest.raises(TypeError, match=match):
        empty.recursive_iterator(order='nested_first', node_type='parent')


@pytest.mark.parametrize(
    ('node_type', 'expected_types'),
    [('parent', pv.MultiBlock), ('child', (pv.DataSet, type(None)))],
)
def test_recursive_iterator_contents(
    multiblock_all_with_nested_and_none, node_type, expected_types
):
    iterator = multiblock_all_with_nested_and_none.recursive_iterator('ids', node_type=node_type)
    assert all(isinstance(item, tuple) and isinstance(item[0], int) for item in iterator)

    iterator = multiblock_all_with_nested_and_none.recursive_iterator('names', node_type=node_type)
    assert all(isinstance(item, str) for item in iterator)

    iterator = multiblock_all_with_nested_and_none.recursive_iterator(
        'blocks', node_type=node_type
    )
    assert all(isinstance(item, expected_types) for item in iterator)

    iterator = multiblock_all_with_nested_and_none.recursive_iterator('items', node_type=node_type)
    for name, block in iterator:
        assert isinstance(name, str)
        assert isinstance(block, expected_types)

    iterator = multiblock_all_with_nested_and_none.recursive_iterator('all', node_type=node_type)
    for id_, name, block in iterator:
        assert isinstance(id_, tuple)
        assert isinstance(name, str)
        assert isinstance(block, expected_types)


@pytest.mark.parametrize('prepend_names', [True, False])
@pytest.mark.parametrize('separator', ['::', '--'])
def test_recursive_iterator_prepend_names(separator, prepend_names):
    nested = MultiBlock(dict(a=MultiBlock(dict(b=MultiBlock(dict(c=None)), d=None)), e=None))
    expected_names = ['a::b::c', 'a::d', 'e'] if prepend_names else ['c', 'd', 'e']
    expected_names = [name.replace('::', separator) for name in expected_names]

    iterator = nested.recursive_iterator(
        'names', prepend_names=prepend_names, separator=separator, skip_none=False
    )
    names = list(iterator)
    assert names == expected_names

    # Test iterator with flatten method
    name_mode = 'prepend' if prepend_names else 'preserve'
    flattened = nested.flatten(name_mode=name_mode, separator=separator)
    assert flattened.keys() == expected_names


@pytest.mark.parametrize('nested_ids', [True, False])
def test_recursive_iterator_ids(nested_ids):
    nested = MultiBlock(dict(a=MultiBlock(dict(b=MultiBlock(dict(c=None)), d=None)), e=None))
    expected_ids = [(0, 0, 0), (0, 1), (1,)] if nested_ids else [0, 1, 1]

    iterator = nested.recursive_iterator('ids', nested_ids=nested_ids, skip_none=False)
    ids = list(iterator)
    assert ids == expected_ids


def test_recursive_iterator_raises():
    multi = pv.MultiBlock()

    match = 'Nested ids option only applies when ids are returned.'
    with pytest.raises(ValueError, match=match):
        multi.recursive_iterator('names', nested_ids=True)
    with pytest.raises(ValueError, match=match):
        multi.recursive_iterator('items', nested_ids=True)

    match = 'Prepend names option only applies when names are returned.'
    with pytest.raises(ValueError, match=match):
        multi.recursive_iterator('ids', prepend_names=True)
    with pytest.raises(ValueError, match=match):
        multi.recursive_iterator('blocks', prepend_names=True)

    with pytest.raises(ValueError, match='String separator cannot be empty.'):
        multi.recursive_iterator(separator='')


@pytest.mark.parametrize(
    ('order', 'expected_ids', 'expected_names'),
    [
        ('nested_first', [(1, 0), (0,), (2,)], ['grid', 'image', 'poly']),
        ('nested_last', [(0,), (2,), (1, 0)], ['image', 'poly', 'grid']),
        (None, [(0,), (1, 0), (2,)], ['image', 'grid', 'poly']),
    ],
)
def test_recursive_iterator_order(nested_fixture, order, expected_ids, expected_names):
    # Store instances of each mesh for testing iterator blocks
    expected_meshes = dict(
        image=nested_fixture['image'],
        poly=nested_fixture['poly'],
        grid=nested_fixture['multi']['grid'],
    )

    common_kwargs = dict(skip_empty=False, nested_ids=True, contents='all')
    iterator = nested_fixture.recursive_iterator(order=order, **common_kwargs)
    for i, (ids, name, block) in enumerate(iterator):
        assert ids == expected_ids[i]
        assert name == expected_names[i]
        assert block is expected_meshes[name]


@pytest.mark.parametrize('copy', [True, False, None])
@pytest.mark.parametrize(
    ('field_data_mode', 'separator', 'name_in', 'name_out'),
    [('prepend', '//', 'data', 'Block-00//data'), ('preserve', '::', 'data', 'data')],
)
def test_move_nested_field_data_to_root(copy, field_data_mode, separator, name_in, name_out):
    value = [42]
    multi = pv.MultiBlock()
    multi.field_data[name_in] = value
    root = pv.MultiBlock([multi])
    root.move_nested_field_data_to_root(
        copy=copy, field_data_mode=field_data_mode, separator=separator
    )
    assert root.field_data.keys() == [name_out]
    assert np.array_equal(root.field_data[name_out], value)

    if copy is None:
        assert name_in not in multi.field_data
    else:
        assert name_in in multi.field_data
        data_in = multi.field_data[name_in]
        data_out = root.field_data[name_out]
        assert np.shares_memory(data_in, data_out) == (copy is False)


def _make_nested_multiblock(
    *,
    root_field_data=None,
    root_user_dict=None,
    nested1_field_data=None,
    nested1_user_dict=None,
    nested1_block_name=None,
    nested2_field_data=None,
    nested2_user_dict=None,
    nested2_block_name=None,
):
    nested2 = pv.MultiBlock()
    if nested2_field_data:
        nested2.field_data.update(nested2_field_data)
    if nested2_user_dict:
        nested2.user_dict.update(nested2_user_dict)

    nested1 = pv.MultiBlock([nested2])
    if nested2_block_name:
        nested1.set_block_name(0, nested2_block_name)
    if nested1_field_data:
        nested1.field_data.update(nested1_field_data)
    if nested1_user_dict:
        nested1.user_dict.update(nested1_user_dict)

    root = pv.MultiBlock([nested1])
    if nested1_block_name:
        root.set_block_name(0, nested1_block_name)
    if root_field_data:
        root.field_data.update(root_field_data)
    if root_user_dict:
        root.user_dict.update(root_user_dict)
    return root


def test_move_nested_field_data_to_root_check_duplicate_keys():
    NAME1 = 'name1'
    VALUE1 = 'value1'
    NAME2 = 'name2'
    VALUE2 = 'value2'

    # Test nested field data key overrides root field data key
    root = _make_nested_multiblock(
        root_field_data={NAME1: VALUE1}, nested1_field_data={NAME1: VALUE1}
    )
    match = (
        "The field data array 'name1' from nested MultiBlock at index [0] with name 'Block-00'\n"
        "also exists in the root MultiBlock's field data and cannot be moved.\n"
        "Use `field_data_mode='prepend'` to make the array names unique."
    )
    with pytest.raises(ValueError, match=re.escape(match)):
        root.move_nested_field_data_to_root()
    root.move_nested_field_data_to_root(check_duplicate_keys=False)

    # Test block name key overrides root user dict key
    root = _make_nested_multiblock(
        root_user_dict={NAME1: VALUE1},
        nested1_user_dict={NAME2: VALUE2},
        nested1_block_name=NAME1,
    )
    match = (
        'The root user dict cannot be updated with data from nested MultiBlock '
        "at index [0] with name 'name1'.\n"
        "The key 'name1' already exists in the root user dict and would be overwritten."
    )
    with pytest.raises(ValueError, match=re.escape(match)):
        root.move_nested_field_data_to_root(user_dict_mode='prepend')
    root.move_nested_field_data_to_root(check_duplicate_keys=False)

    # Test nested user dict key overrides root user dict key
    root = _make_nested_multiblock(
        root_user_dict={NAME1: VALUE1}, nested1_user_dict={NAME1: VALUE1}
    )
    match = (
        'The root user dict cannot be updated with data from nested MultiBlock '
        "at index [0] with name 'Block-00'.\n"
        "The key 'name1' already exists in the root user dict and would be overwritten."
    )
    with pytest.raises(ValueError, match=re.escape(match)):
        root.move_nested_field_data_to_root()
    root.move_nested_field_data_to_root(check_duplicate_keys=False)


@pytest.mark.parametrize('user_dict_mode', ['preserve', 'prepend', 'flat', 'nested'])
def test_move_nested_field_data_user_dict_mode(user_dict_mode):
    block_name1 = 'level1'
    block_name2 = 'level2'
    root_dict = {'root': 0}
    nested_dict1 = {'nested1': 1}
    nested_dict2 = {'nested2': 2}
    separator = ';'

    expected_user_dict = root_dict.copy()
    if user_dict_mode == 'flat':
        expected_user_dict[block_name1] = nested_dict1
        expected_user_dict[block_name2] = nested_dict2
    elif user_dict_mode == 'prepend':
        expected_user_dict[block_name1] = nested_dict1
        expected_user_dict[block_name1 + separator + block_name2] = nested_dict2
    elif user_dict_mode == 'preserve':
        expected_user_dict.update(nested_dict1)
        expected_user_dict.update(nested_dict2)
    elif user_dict_mode == 'nested':
        nested_dict = nested_dict1.copy()
        nested_dict[block_name2] = nested_dict2
        expected_user_dict[block_name1] = nested_dict

    root = _make_nested_multiblock(
        root_user_dict=root_dict,
        nested1_user_dict=nested_dict1,
        nested1_block_name=block_name1,
        nested2_user_dict=nested_dict2,
        nested2_block_name=block_name2,
    )

    # Test root user dict is updated with nested user dict data
    root.move_nested_field_data_to_root(user_dict_mode=user_dict_mode, separator=separator)
    actual_user_dict = dict(root.user_dict)
    assert actual_user_dict == expected_user_dict
    assert USER_DICT_KEY not in root[0].field_data
    assert root[0].user_dict == {}
    assert USER_DICT_KEY not in root[0][0].field_data
    assert root[0][0].user_dict == {}


def test_flatten(multiblock_all_with_nested_and_none):
    # Add field data
    ROOT_KEY = 'root'
    NESTED_KEY = 'nested'
    root_multi = multiblock_all_with_nested_and_none
    root_multi.field_data[ROOT_KEY] = ['root data']
    nested_multi = multiblock_all_with_nested_and_none[-1]
    nested_multi.field_data[NESTED_KEY] = ['nested data']
    assert isinstance(nested_multi, pv.MultiBlock)

    def assert_field_data_keys(flat_, root_, nested_):
        # Test that input block field data isn't accidentally cleared
        assert flat_.field_data.keys() == [ROOT_KEY, NESTED_KEY]
        assert root_.field_data.keys() == [ROOT_KEY]
        assert nested_.field_data.keys() == [NESTED_KEY]

    root_names = root_multi.keys()[:-1]
    nested_names = nested_multi.keys()
    expected_names = [*root_names, *nested_names]
    expected_n_blocks = len(root_names) + len(nested_names)

    match = (
        "Block at index [6][0] with name 'Block-00' cannot be flattened. Another block \n"
        "with the same name already exists. Use `name_mode='reset'` "
        'or `check_duplicate_keys=False`.'
    )
    with pytest.raises(ValueError, match=re.escape(match)):
        _ = root_multi.flatten()
    flat = root_multi.flatten(name_mode='preserve', check_duplicate_keys=False)
    assert all(isinstance(item, pv.DataSet) or item is None for item in flat)
    assert len(flat) == expected_n_blocks
    assert flat.keys() == expected_names

    # Test field data keys with copy
    assert nested_multi[0] is not flat[0]
    assert_field_data_keys(flat, root_multi, nested_multi)

    flat = root_multi.flatten(name_mode='reset', copy=False)
    expected_names = [f'Block-{i:02}' for i in range(expected_n_blocks)]
    assert flat.keys() == expected_names

    # Test field data keys without copy
    assert nested_multi[0] is flat[0]
    assert_field_data_keys(flat, root_multi, nested_multi)


@pytest.mark.parametrize('copy', [True, False])
def test_flatten_copy(multiblock_all, copy):
    multi_in = multiblock_all
    data_before = np.array([1, 2, 3])
    multi_in.field_data['foo'] = data_before

    multi_out = multiblock_all.flatten(copy=copy)
    assert multi_in is not multi_out
    for block_in, block_out in zip(multi_in, multi_out):
        assert block_in == block_out
        assert (block_in is block_out) == (not copy)

    data_after = multi_out.field_data['foo']
    shares_memory = np.shares_memory(data_after, data_before)
    assert shares_memory == (not copy)


@pytest.mark.parametrize(
    'function', [lambda x: x.cast_to_unstructured_grid(), 'cast_to_unstructured_grid']
)
def test_generic_filter(multiblock_all_with_nested_and_none, function):
    # Include empty mesh
    empty_mesh = pv.PolyData()
    multiblock_all_with_nested_and_none.append(empty_mesh)

    output = multiblock_all_with_nested_and_none.generic_filter(function)
    flat_output = output.flatten(check_duplicate_keys=False)
    # Make sure no `None` blocks were removed
    assert None in flat_output
    # Check output
    for block in flat_output:
        assert isinstance(block, pv.UnstructuredGrid) or block is None


@pytest.mark.parametrize('inplace', [True, False])
def test_generic_filter_inplace(multiblock_all_with_nested_and_none, inplace):
    # Include empty mesh
    input_ = multiblock_all_with_nested_and_none
    empty_mesh = pv.PolyData()
    multiblock_all_with_nested_and_none.append(empty_mesh)
    flat_inputs = multiblock_all_with_nested_and_none.flatten(
        copy=False, check_duplicate_keys=False
    )

    output = multiblock_all_with_nested_and_none.generic_filter(
        'extract_largest',
        inplace=inplace,
    )
    flat_output = output.flatten(copy=False, check_duplicate_keys=False)

    assert flat_inputs.n_blocks == flat_output.n_blocks
    for block_in, block_out in zip(flat_inputs, flat_output):
        assert ((block_in is block_out) == inplace) or block_out is None

    # Test root MultiBlock
    assert (input_ is output) == inplace
    # Test nested MultiBlock container
    assert isinstance(input_[6], pv.MultiBlock)
    assert (input_[6] is output[6]) == inplace


def test_generic_filter_raises(multiblock_all_with_nested_and_none):
    match = (
        "The filter 'resample'\ncould not be applied to the block at index 1 with name "
        "'Block-01' and type RectilinearGrid."
    )
    with pytest.raises(RuntimeError, match=match):
        multiblock_all_with_nested_and_none.generic_filter(
            'resample',
        )
    # Test with nested index
    multi = pv.MultiBlock([multiblock_all_with_nested_and_none])
    match = (
        "The filter 'resample'\ncould not be applied to the nested block at index [0][1] "
        "with name 'Block-01' and type RectilinearGrid."
    )
    with pytest.raises(RuntimeError, match=re.escape(match)):
        multi.generic_filter(
            'resample',
        )
    # Test with invalid kwargs
    match = "The filter '<bound method DataSetFilters.align_xyz of ImageData"
    with pytest.raises(RuntimeError, match=re.escape(match)):
        multiblock_all_with_nested_and_none.generic_filter('align_xyz', foo='bar')
    # Test with function
    match = "The filter '<function test_generic_filter_raises"
    with pytest.raises(RuntimeError, match=match):
        multiblock_all_with_nested_and_none.generic_filter(
            test_generic_filter_raises,
        )


def test_block_types(multiblock_all_with_nested_and_none):
    multi = multiblock_all_with_nested_and_none
    types = {
        type(None),
        pv.RectilinearGrid,
        pv.ImageData,
        pv.PolyData,
        pv.UnstructuredGrid,
        pv.StructuredGrid,
    }
    assert multi.nested_block_types == types
    types.add(pv.MultiBlock)
    assert multi.block_types == types


def test_is_homogeneous_is_heterogeneous(multiblock_all_with_nested_and_none):
    # Empty case
    assert pv.MultiBlock().is_homogeneous is False
    assert pv.MultiBlock().is_heterogeneous is False

    # Heterogeneous case
    heterogeneous = multiblock_all_with_nested_and_none
    assert heterogeneous.is_homogeneous is False
    assert heterogeneous.is_heterogeneous is True

    # Homogeneous case
    homogeneous = multiblock_all_with_nested_and_none.as_polydata_blocks()
    assert homogeneous.is_homogeneous is True
    assert homogeneous.is_heterogeneous is False