-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_arrayModifiers.py
More file actions
587 lines (520 loc) · 28.3 KB
/
test_arrayModifiers.py
File metadata and controls
587 lines (520 loc) · 28.3 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
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
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright 2023-2024 TotalEnergies.
# SPDX-FileContributor: Paloma Martinez, Romain Baville
# SPDX-License-Identifier: Apache 2.0
# ruff: noqa: E402 # disable Module level import not at top of file
# mypy: disable-error-code="operator"
import pytest
from typing import Union, Any
import numpy as np
import numpy.typing as npt
import vtkmodules.util.numpy_support as vnp
from vtkmodules.vtkCommonCore import vtkDataArray
from vtkmodules.vtkCommonDataModel import ( vtkDataSet, vtkMultiBlockDataSet, vtkPointData, vtkCellData )
from geos.mesh.utils.multiblockHelpers import getBlockElementIndexesFlatten
from vtk import ( # type: ignore[import-untyped]
VTK_UNSIGNED_CHAR, VTK_UNSIGNED_SHORT, VTK_UNSIGNED_INT, VTK_UNSIGNED_LONG_LONG, VTK_CHAR, VTK_SIGNED_CHAR,
VTK_SHORT, VTK_INT, VTK_LONG_LONG, VTK_ID_TYPE, VTK_FLOAT, VTK_DOUBLE,
)
# Information :
# https://github.com/Kitware/VTK/blob/master/Wrapping/Python/vtkmodules/util/numpy_support.py
# https://github.com/Kitware/VTK/blob/master/Wrapping/Python/vtkmodules/util/vtkConstants.py
# vtk array type int numpy type
# VTK_CHAR = 2 = np.int8
# VTK_SIGNED_CHAR = 15 = np.int8
# VTK_SHORT = 4 = np.int16
# VTK_INT = 6 = np.int32
# VTK_BIT = 1 = np.uint8
# VTK_UNSIGNED_CHAR = 3 = np.uint8
# VTK_UNSIGNED_SHORT = 5 = np.uint16
# VTK_UNSIGNED_INT = 7 = np.uint32
# VTK_UNSIGNED_LONG_LONG = 17 = np.uint64
# VTK_LONG = 8 = LONG_TYPE_CODE ( int32 | int64 )
# VTK_UNSIGNED_LONG = 9 = ULONG_TYPE_CODE ( uint32 | uint64 )
# VTK_FLOAT = 10 = np.float32
# VTK_DOUBLE = 11 = np.float64
# VTK_ID_TYPE = 12 = ID_TYPE_CODE ( int32 | int64 )
# vtk array type int IdType numpy type
# VTK_LONG_LONG = 16 = 2 = np.int64
from geos.mesh.utils import arrayModifiers
@pytest.mark.parametrize(
"idBlock, attributeName, nbComponentsTest, componentNamesTest, onPoints, listValues, listValuesTest, vtkDataTypeTest",
[
# Test fill an attribute on point and on cell.
( 3, "PointAttribute", 3,
( "AX1", "AX2", "AX3" ), True, None, [ np.float64(
np.nan ), np.float64( np.nan ), np.float64( np.nan ) ], VTK_DOUBLE ),
( 3, "CellAttribute", 3,
( "AX1", "AX2", "AX3" ), False, None, [ np.float64(
np.nan ), np.float64( np.nan ), np.float64( np.nan ) ], VTK_DOUBLE ),
# Test fill attributes with different number of component with or without component names.
( 3, "PORO", 1, (), False, None, [ np.float32( np.nan ) ], VTK_FLOAT ),
( 1, "collocated_nodes", 2, ( None, None ), True, None, [ np.int64( -1 ), np.int64( -1 ) ], VTK_ID_TYPE ),
# Test fill an attribute with different type of value.
( 3, "FAULT", 1, (), False, None, [ np.int32( -1 ) ], VTK_INT ),
( 3, "FAULT", 1, (), False, [ 4 ], [ np.int32( 4 ) ], VTK_INT ),
( 3, "PORO", 1, (), False, [ 4 ], [ np.float32( 4 ) ], VTK_FLOAT ),
( 1, "collocated_nodes", 2, ( None, None ), True, [ 4, 4 ], [ np.int64( 4 ), np.int64( 4 ) ], VTK_ID_TYPE ),
( 3, "CellAttribute", 3, ( "AX1", "AX2", "AX3" ), False, [ 4, 4, 4 ],
[ np.float64( 4 ), np.float64( 4 ), np.float64( 4 ) ], VTK_DOUBLE ),
] )
def test_fillPartialAttributes(
dataSetTest: vtkMultiBlockDataSet,
idBlock: int,
attributeName: str,
nbComponentsTest: int,
componentNamesTest: tuple[ str, ...],
onPoints: bool,
listValues: Union[ list[ Any ], None ],
listValuesTest: list[ Any ],
vtkDataTypeTest: int,
) -> None:
"""Test filling a partial attribute from a multiblock with values."""
multiBlockDataSetTest: vtkMultiBlockDataSet = dataSetTest( "multiblock" )
# Fill the attribute in the multiBlockDataSet.
assert arrayModifiers.fillPartialAttributes( multiBlockDataSetTest,
attributeName,
onPoints=onPoints,
listValues=listValues )
# Get the dataSet where the attribute has been filled.
dataSet: vtkDataSet = vtkDataSet.SafeDownCast( multiBlockDataSetTest.GetDataSet( idBlock ) )
# Get the filled attribute.
data: Union[ vtkPointData, vtkCellData ]
nbElements: int
if onPoints:
nbElements = dataSet.GetNumberOfPoints()
data = dataSet.GetPointData()
else:
nbElements = dataSet.GetNumberOfCells()
data = dataSet.GetCellData()
attributeFilled: vtkDataArray = data.GetArray( attributeName )
# Test the number of components and their names if multiple.
nbComponentsFilled: int = attributeFilled.GetNumberOfComponents()
assert nbComponentsFilled == nbComponentsTest
if nbComponentsTest > 1:
componentNamesFilled: tuple[ str, ...] = tuple(
attributeFilled.GetComponentName( i ) for i in range( nbComponentsFilled ) )
assert componentNamesFilled == componentNamesTest
# Test values and their types.
## Create the constant array test from the value.
npArrayTest: npt.NDArray[ Any ]
if nbComponentsTest > 1:
npArrayTest = np.array( [ listValuesTest for _ in range( nbElements ) ] )
else:
npArrayTest = np.array( [ listValuesTest[ 0 ] for _ in range( nbElements ) ] )
npArrayFilled: npt.NDArray[ Any ] = vnp.vtk_to_numpy( attributeFilled )
assert npArrayFilled.dtype == npArrayTest.dtype
if listValues is None and vtkDataTypeTest in ( VTK_FLOAT, VTK_DOUBLE ):
assert np.isnan( npArrayFilled ).all()
else:
assert ( npArrayFilled == npArrayTest ).all()
vtkDataTypeFilled: int = attributeFilled.GetDataType()
assert vtkDataTypeFilled == vtkDataTypeTest
@pytest.mark.parametrize( "multiBlockDataSetName", [ "multiblock" ] )
def test_FillAllPartialAttributes(
dataSetTest: vtkMultiBlockDataSet,
multiBlockDataSetName: str,
) -> None:
"""Test to fill all the partial attributes of a vtkMultiBlockDataSet with a value."""
multiBlockDataSetTest: vtkMultiBlockDataSet = dataSetTest( multiBlockDataSetName )
assert arrayModifiers.fillAllPartialAttributes( multiBlockDataSetTest )
elementaryBlockIndexes: list[ int ] = getBlockElementIndexesFlatten( multiBlockDataSetTest )
for blockIndex in elementaryBlockIndexes:
dataSet: vtkDataSet = vtkDataSet.SafeDownCast( multiBlockDataSetTest.GetDataSet( blockIndex ) )
attributeExist: int
for attributeNameOnPoint in [ "PointAttribute", "collocated_nodes" ]:
attributeExist = dataSet.GetPointData().HasArray( attributeNameOnPoint )
assert attributeExist == 1
for attributeNameOnCell in [ "CELL_MARKERS", "CellAttribute", "FAULT", "PERM", "PORO" ]:
attributeExist = dataSet.GetCellData().HasArray( attributeNameOnCell )
assert attributeExist == 1
@pytest.mark.parametrize( "attributeName, dataType, expectedDatatypeArray", [
( "test_double", VTK_DOUBLE, "vtkDoubleArray" ),
( "test_float", VTK_FLOAT, "vtkFloatArray" ),
( "test_int", VTK_INT, "vtkIntArray" ),
( "test_unsigned_int", VTK_UNSIGNED_INT, "vtkUnsignedIntArray" ),
( "test_char", VTK_CHAR, "vtkCharArray" ),
] )
def test_createEmptyAttribute(
attributeName: str,
dataType: int,
expectedDatatypeArray: vtkDataArray,
) -> None:
"""Test empty attribute creation."""
componentNames: tuple[ str, str, str ] = ( "d1", "d2", "d3" )
newAttr: vtkDataArray = arrayModifiers.createEmptyAttribute( attributeName, componentNames, dataType )
assert newAttr.GetNumberOfComponents() == len( componentNames )
for ax in range( 3 ):
assert newAttr.GetComponentName( ax ) == componentNames[ ax ]
assert newAttr.IsA( str( expectedDatatypeArray ) )
@pytest.mark.parametrize(
"attributeName, onPoints",
[
# Test to create a new attribute on points and on cells.
( "newAttribute", False ),
( "newAttribute", True ),
# Test to create a new attribute when an attribute with the same name already exist on the opposite piece.
( "PORO", True ), # Partial attribute on cells already exist.
( "GLOBAL_IDS_CELLS", True ), # Global attribute on cells already exist.
] )
def test_createConstantAttributeMultiBlock(
dataSetTest: vtkMultiBlockDataSet,
attributeName: str,
onPoints: bool,
) -> None:
"""Test creation of constant attribute in multiblock dataset."""
multiBlockDataSetTest: vtkMultiBlockDataSet = dataSetTest( "multiblock" )
values: list[ float ] = [ np.nan ]
assert arrayModifiers.createConstantAttributeMultiBlock( multiBlockDataSetTest,
values,
attributeName,
onPoints=onPoints )
elementaryBlockIndexes: list[ int ] = getBlockElementIndexesFlatten( multiBlockDataSetTest )
for blockIndex in elementaryBlockIndexes:
dataSet: vtkDataSet = vtkDataSet.SafeDownCast( multiBlockDataSetTest.GetDataSet( blockIndex ) )
data: Union[ vtkPointData, vtkCellData ]
data = dataSet.GetPointData() if onPoints else dataSet.GetCellData()
attributeWellCreated: int = data.HasArray( attributeName )
assert attributeWellCreated == 1
@pytest.mark.parametrize(
"listValues, componentNames, componentNamesTest, onPoints, vtkDataType, vtkDataTypeTest, attributeName",
[
# Test attribute names.
## Test with an attributeName already existing on opposite piece.
( [ np.float64( 42 ) ], (), (), True, VTK_DOUBLE, VTK_DOUBLE, "CellAttribute" ),
( [ np.float64( 42 ) ], (), (), False, VTK_DOUBLE, VTK_DOUBLE, "PointAttribute" ),
## Test with a new attributeName on cells and on points.
( [ np.float32( 42 ) ], (), (), True, VTK_FLOAT, VTK_FLOAT, "newAttribute" ),
( [ np.float32( 42 ) ], (), (), False, VTK_FLOAT, VTK_FLOAT, "newAttribute" ),
# Test the number of components and their names.
( [ np.float32( 42 ) ], ( "X" ), (), True, VTK_FLOAT, VTK_FLOAT, "newAttribute" ),
( [ np.float32( 42 ), np.float32( 42 ) ], ( "X", "Y" ),
( "X", "Y" ), True, VTK_FLOAT, VTK_FLOAT, "newAttribute" ),
( [ np.float32( 42 ), np.float32( 42 ) ], ( "X", "Y", "Z" ),
( "X", "Y" ), True, VTK_FLOAT, VTK_FLOAT, "newAttribute" ),
( [ np.float32( 42 ), np.float32( 42 ) ], (),
( "Component0", "Component1" ), True, VTK_FLOAT, VTK_FLOAT, "newAttribute" ),
# Test the type of the values.
## With numpy scalar type.
( [ np.int8( 42 ) ], (), (), True, None, VTK_SIGNED_CHAR, "newAttribute" ),
( [ np.int8( 42 ) ], (), (), True, VTK_SIGNED_CHAR, VTK_SIGNED_CHAR, "newAttribute" ),
( [ np.int16( 42 ) ], (), (), True, None, VTK_SHORT, "newAttribute" ),
( [ np.int16( 42 ) ], (), (), True, VTK_SHORT, VTK_SHORT, "newAttribute" ),
( [ np.int32( 42 ) ], (), (), True, None, VTK_INT, "newAttribute" ),
( [ np.int32( 42 ) ], (), (), True, VTK_INT, VTK_INT, "newAttribute" ),
( [ np.int64( 42 ) ], (), (), True, None, VTK_LONG_LONG, "newAttribute" ),
( [ np.int64( 42 ) ], (), (), True, VTK_LONG_LONG, VTK_LONG_LONG, "newAttribute" ),
( [ np.uint8( 42 ) ], (), (), True, None, VTK_UNSIGNED_CHAR, "newAttribute" ),
( [ np.uint8( 42 ) ], (), (), True, VTK_UNSIGNED_CHAR, VTK_UNSIGNED_CHAR, "newAttribute" ),
( [ np.uint16( 42 ) ], (), (), True, None, VTK_UNSIGNED_SHORT, "newAttribute" ),
( [ np.uint16( 42 ) ], (), (), True, VTK_UNSIGNED_SHORT, VTK_UNSIGNED_SHORT, "newAttribute" ),
( [ np.uint32( 42 ) ], (), (), True, None, VTK_UNSIGNED_INT, "newAttribute" ),
( [ np.uint32( 42 ) ], (), (), True, VTK_UNSIGNED_INT, VTK_UNSIGNED_INT, "newAttribute" ),
( [ np.uint64( 42 ) ], (), (), True, None, VTK_UNSIGNED_LONG_LONG, "newAttribute" ),
( [ np.uint64( 42 ) ], (), (), True, VTK_UNSIGNED_LONG_LONG, VTK_UNSIGNED_LONG_LONG, "newAttribute" ),
( [ np.float32( 42 ) ], (), (), True, None, VTK_FLOAT, "newAttribute" ),
( [ np.float64( 42 ) ], (), (), True, None, VTK_DOUBLE, "newAttribute" ),
( [ np.float64( 42 ) ], (), (), True, VTK_DOUBLE, VTK_DOUBLE, "newAttribute" ),
## With python scalar type.
( [ 42 ], (), (), True, None, VTK_LONG_LONG, "newAttribute" ),
( [ 42 ], (), (), True, VTK_LONG_LONG, VTK_LONG_LONG, "newAttribute" ),
( [ 42. ], (), (), True, None, VTK_DOUBLE, "newAttribute" ),
( [ 42. ], (), (), True, VTK_DOUBLE, VTK_DOUBLE, "newAttribute" ),
] )
def test_createConstantAttributeDataSet(
dataSetTest: vtkDataSet,
listValues: list[ Any ],
componentNames: tuple[ str, ...],
componentNamesTest: tuple[ str, ...],
onPoints: bool,
vtkDataType: Union[ int, Any ],
vtkDataTypeTest: int,
attributeName: str,
) -> None:
"""Test constant attribute creation in dataset."""
dataSet: vtkDataSet = dataSetTest( "dataset" )
# Create the new constant attribute in the dataSet.
assert arrayModifiers.createConstantAttributeDataSet( dataSet, listValues, attributeName, componentNames, onPoints,
vtkDataType )
# Get the created attribute.
data: Union[ vtkPointData, vtkCellData ]
nbElements: int
if onPoints:
data = dataSet.GetPointData()
nbElements = dataSet.GetNumberOfPoints()
else:
data = dataSet.GetCellData()
nbElements = dataSet.GetNumberOfCells()
attributeCreated: vtkDataArray = data.GetArray( attributeName )
# Test the number of components and their names if multiple.
nbComponentsTest: int = len( listValues )
nbComponentsCreated: int = attributeCreated.GetNumberOfComponents()
assert nbComponentsCreated == nbComponentsTest
if nbComponentsTest > 1:
componentNamesCreated: tuple[ str, ...] = tuple(
attributeCreated.GetComponentName( i ) for i in range( nbComponentsCreated ) )
assert componentNamesCreated, componentNamesTest
# Test values and their types.
## Create the constant array test from values in the list values.
npArrayTest: npt.NDArray[ Any ]
if len( listValues ) > 1:
npArrayTest = np.array( [ listValues for _ in range( nbElements ) ] )
else:
npArrayTest = np.array( [ listValues[ 0 ] for _ in range( nbElements ) ] )
npArrayCreated: npt.NDArray[ Any ] = vnp.vtk_to_numpy( attributeCreated )
assert npArrayCreated.dtype == npArrayTest.dtype
assert ( npArrayCreated == npArrayTest ).all()
vtkDataTypeCreated: int = attributeCreated.GetDataType()
assert vtkDataTypeCreated == vtkDataTypeTest
@pytest.mark.parametrize(
"componentNames, componentNamesTest, onPoints, vtkDataType, vtkDataTypeTest, valueType, attributeName",
[
# Test attribute names.
## Test with an attributeName already existing on opposite piece.
( (), (), True, VTK_DOUBLE, VTK_DOUBLE, "float64", "CellAttribute" ),
( (), (), False, VTK_DOUBLE, VTK_DOUBLE, "float64", "PointAttribute" ),
## Test with a new attributeName on cells and on points.
( (), (), True, VTK_FLOAT, VTK_FLOAT, "float32", "newAttribute" ),
( (), (), False, VTK_FLOAT, VTK_FLOAT, "float32", "newAttribute" ),
# Test the number of components and their names.
( ( "X" ), (), True, VTK_FLOAT, VTK_FLOAT, "float32", "newAttribute" ),
( ( "X", "Y" ), ( "X", "Y" ), True, VTK_FLOAT, VTK_FLOAT, "float32", "newAttribute" ),
( ( "X", "Y", "Z" ), ( "X", "Y" ), True, VTK_FLOAT, VTK_FLOAT, "float32", "newAttribute" ),
( (), ( "Component0", "Component1" ), True, VTK_FLOAT, VTK_FLOAT, "float32", "newAttribute" ),
# Test the type of the values.
## With numpy scalar type.
( (), (), True, None, VTK_SIGNED_CHAR, "int8", "newAttribute" ),
( (), (), True, VTK_SIGNED_CHAR, VTK_SIGNED_CHAR, "int8", "newAttribute" ),
( (), (), True, None, VTK_SHORT, "int16", "newAttribute" ),
( (), (), True, VTK_SHORT, VTK_SHORT, "int16", "newAttribute" ),
( (), (), True, None, VTK_INT, "int32", "newAttribute" ),
( (), (), True, VTK_INT, VTK_INT, "int32", "newAttribute" ),
( (), (), True, None, VTK_LONG_LONG, "int64", "newAttribute" ),
( (), (), True, VTK_LONG_LONG, VTK_LONG_LONG, "int64", "newAttribute" ),
( (), (), True, None, VTK_UNSIGNED_CHAR, "uint8", "newAttribute" ),
( (), (), True, VTK_UNSIGNED_CHAR, VTK_UNSIGNED_CHAR, "uint8", "newAttribute" ),
( (), (), True, None, VTK_UNSIGNED_SHORT, "uint16", "newAttribute" ),
( (), (), True, VTK_UNSIGNED_SHORT, VTK_UNSIGNED_SHORT, "uint16", "newAttribute" ),
( (), (), True, None, VTK_UNSIGNED_INT, "uint32", "newAttribute" ),
( (), (), True, VTK_UNSIGNED_INT, VTK_UNSIGNED_INT, "uint32", "newAttribute" ),
( (), (), True, None, VTK_UNSIGNED_LONG_LONG, "uint64", "newAttribute" ),
( (), (), True, VTK_UNSIGNED_LONG_LONG, VTK_UNSIGNED_LONG_LONG, "uint64", "newAttribute" ),
( (), (), True, None, VTK_FLOAT, "float32", "newAttribute" ),
( (), (), True, None, VTK_DOUBLE, "float64", "newAttribute" ),
( (), (), True, VTK_DOUBLE, VTK_DOUBLE, "float64", "newAttribute" ),
## With python scalar type.
( (), (), True, None, VTK_LONG_LONG, "int", "newAttribute" ),
( (), (), True, VTK_LONG_LONG, VTK_LONG_LONG, "int", "newAttribute" ),
( (), (), True, None, VTK_DOUBLE, "float", "newAttribute" ),
( (), (), True, VTK_DOUBLE, VTK_DOUBLE, "float", "newAttribute" ),
] )
def test_createAttribute(
dataSetTest: vtkDataSet,
getArrayWithSpeTypeValue: npt.NDArray[ Any ],
componentNames: tuple[ str, ...],
componentNamesTest: tuple[ str, ...],
onPoints: bool,
vtkDataType: int,
vtkDataTypeTest: int,
valueType: str,
attributeName: str,
) -> None:
"""Test creation of dataset in dataset from given array."""
dataSet: vtkDataSet = dataSetTest( "dataset" )
# Get a array with random values of a given type.
nbElements: int = dataSet.GetNumberOfPoints() if onPoints else dataSet.GetNumberOfCells()
nbComponentsTest: int = 1 if len( componentNamesTest ) == 0 else len( componentNamesTest )
npArrayTest: npt.NDArray[ Any ] = getArrayWithSpeTypeValue( nbComponentsTest, nbElements, valueType )
# Create the new attribute in the dataSet.
assert arrayModifiers.createAttribute( dataSet, npArrayTest, attributeName, componentNames, onPoints, vtkDataType )
# Get the created attribute.
data: Union[ vtkPointData, vtkCellData ]
data = dataSet.GetPointData() if onPoints else dataSet.GetCellData()
attributeCreated: vtkDataArray = data.GetArray( attributeName )
# Test the number of components and their names if multiple.
nbComponentsCreated: int = attributeCreated.GetNumberOfComponents()
assert nbComponentsCreated == nbComponentsTest
if nbComponentsTest > 1:
componentsNamesCreated: tuple[ str, ...] = tuple(
attributeCreated.GetComponentName( i ) for i in range( nbComponentsCreated ) )
assert componentsNamesCreated == componentNamesTest
# Test values and their types.
npArrayCreated: npt.NDArray[ Any ] = vnp.vtk_to_numpy( attributeCreated )
assert npArrayCreated.dtype == npArrayTest.dtype
assert ( npArrayCreated == npArrayTest ).all()
vtkDataTypeCreated: int = attributeCreated.GetDataType()
assert vtkDataTypeCreated == vtkDataTypeTest
@pytest.mark.parametrize(
"attributeNameFrom, attributeNameTo, onPoints",
[
# Test with global attributes.
( "GLOBAL_IDS_POINTS", "GLOBAL_IDS_POINTS_To", True ),
( "GLOBAL_IDS_CELLS", 'GLOBAL_IDS_CELLS_To', False ),
# Test with partial attributes.
( "CellAttribute", "CellAttributeTo", False ),
( "PointAttribute", "PointAttributeTo", True ),
] )
def test_copyAttribute(
dataSetTest: vtkMultiBlockDataSet,
attributeNameFrom: str,
attributeNameTo: str,
onPoints: bool,
) -> None:
"""Test copy of cell attribute from one multiblock to another."""
multiBlockDataSetFrom: vtkMultiBlockDataSet = dataSetTest( "multiblock" )
multiBlockDataSetTo: vtkMultiBlockDataSet = dataSetTest( "emptymultiblock" )
# Copy the attribute from the multiBlockDataSetFrom to the multiBlockDataSetTo.
assert arrayModifiers.copyAttribute( multiBlockDataSetFrom, multiBlockDataSetTo, attributeNameFrom, attributeNameTo,
onPoints )
# Parse the two multiBlockDataSet and test if the attribute has been copied.
elementaryBlockIndexes: list[ int ] = getBlockElementIndexesFlatten( multiBlockDataSetFrom )
for blockIndex in elementaryBlockIndexes:
dataSetFrom: vtkDataSet = vtkDataSet.SafeDownCast( multiBlockDataSetFrom.GetDataSet( blockIndex ) )
dataSetTo: vtkDataSet = vtkDataSet.SafeDownCast( multiBlockDataSetTo.GetDataSet( blockIndex ) )
dataFrom: Union[ vtkPointData, vtkCellData ]
dataTo: Union[ vtkPointData, vtkCellData ]
if onPoints:
dataFrom = dataSetFrom.GetPointData()
dataTo = dataSetTo.GetPointData()
else:
dataFrom = dataSetFrom.GetCellData()
dataTo = dataSetTo.GetCellData()
attributeExistTest: int = dataFrom.HasArray( attributeNameFrom )
attributeExistCopied: int = dataTo.HasArray( attributeNameTo )
assert attributeExistCopied == attributeExistTest
@pytest.mark.parametrize( "attributeNameFrom, attributeNameTo, onPoints", [
( "CellAttribute", "CellAttributeTo", False ),
( "PointAttribute", "PointAttributeTo", True ),
] )
def test_copyAttributeDataSet(
dataSetTest: vtkDataSet,
attributeNameFrom: str,
attributeNameTo: str,
onPoints: bool,
) -> None:
"""Test copy of an attribute from one dataset to another."""
dataSetFrom: vtkDataSet = dataSetTest( "dataset" )
dataSetTo: vtkDataSet = dataSetTest( "emptydataset" )
# Copy the attribute from the dataSetFrom to the dataSetTo.
assert arrayModifiers.copyAttributeDataSet( dataSetFrom, dataSetTo, attributeNameFrom, attributeNameTo, onPoints )
# Get the tested attribute and its copy.
dataFrom: Union[ vtkPointData, vtkCellData ]
dataTo: Union[ vtkPointData, vtkCellData ]
if onPoints:
dataFrom = dataSetFrom.GetPointData()
dataTo = dataSetTo.GetPointData()
else:
dataFrom = dataSetFrom.GetCellData()
dataTo = dataSetTo.GetCellData()
attributeTest: vtkDataArray = dataFrom.GetArray( attributeNameFrom )
attributeCopied: vtkDataArray = dataTo.GetArray( attributeNameTo )
# Test the number of components and their names if multiple.
nbComponentsTest: int = attributeTest.GetNumberOfComponents()
nbComponentsCopied: int = attributeCopied.GetNumberOfComponents()
assert nbComponentsCopied == nbComponentsTest
if nbComponentsTest > 1:
componentsNamesTest: tuple[ str, ...] = tuple(
attributeTest.GetComponentName( i ) for i in range( nbComponentsTest ) )
componentsNamesCopied: tuple[ str, ...] = tuple(
attributeCopied.GetComponentName( i ) for i in range( nbComponentsCopied ) )
assert componentsNamesCopied == componentsNamesTest
# Test values and their types.
npArrayTest: npt.NDArray[ Any ] = vnp.vtk_to_numpy( attributeTest )
npArrayCopied: npt.NDArray[ Any ] = vnp.vtk_to_numpy( attributeCopied )
assert npArrayCopied.dtype == npArrayTest.dtype
assert ( npArrayCopied == npArrayTest ).all()
vtkDataTypeTest: int = attributeTest.GetDataType()
vtkDataTypeCopied: int = attributeCopied.GetDataType()
assert vtkDataTypeCopied == vtkDataTypeTest
@pytest.mark.parametrize( "meshFromName, meshToName, attributeName, onPoints, defaultValueTest", [
( "fracture", "emptyFracture", "collocated_nodes", True, [ -1, -1 ] ),
( "multiblock", "emptyFracture", "FAULT", False, -1 ),
( "multiblock", "emptymultiblock", "FAULT", False, -1 ),
( "dataset", "emptymultiblock", "FAULT", False, -1 ),
( "dataset", "emptydataset", "FAULT", False, -1 ),
] )
def test_transferAttributeWithElementMap(
dataSetTest: Any,
getElementMap: dict[ int, npt.NDArray[ np.int64 ] ],
meshFromName: str,
meshToName: str,
attributeName: str,
onPoints: bool,
defaultValueTest: Any,
) -> None:
"""Test to transfer attributes from the source mesh to the final mesh using a map of points/cells."""
meshFrom: Union[ vtkMultiBlockDataSet, vtkDataSet ] = dataSetTest( meshFromName )
if isinstance( meshFrom, vtkMultiBlockDataSet ):
arrayModifiers.fillAllPartialAttributes( meshFrom )
meshTo: Union[ vtkMultiBlockDataSet, vtkDataSet ] = dataSetTest( meshToName )
elementMap: dict[ int, npt.NDArray[ np.int64 ] ] = getElementMap( meshFromName, meshToName, onPoints )
assert arrayModifiers.transferAttributeWithElementMap( meshFrom, meshTo, elementMap, attributeName, onPoints )
for flatIdDataSetTo in elementMap:
dataTo: Union[ vtkPointData, vtkCellData ]
if isinstance( meshTo, vtkDataSet ):
dataTo = meshTo.GetPointData() if onPoints else meshTo.GetCellData()
elif isinstance( meshTo, vtkMultiBlockDataSet ):
dataSetTo: vtkDataSet = vtkDataSet.SafeDownCast( meshTo.GetDataSet( flatIdDataSetTo ) )
dataTo = dataSetTo.GetPointData() if onPoints else dataSetTo.GetCellData()
arrayTo: npt.NDArray[ Any ] = vnp.vtk_to_numpy( dataTo.GetArray( attributeName ) )
for idElementTo in range( len( arrayTo ) ):
idElementFrom: int = int( elementMap[ flatIdDataSetTo ][ idElementTo ][ 1 ] )
if idElementFrom == -1:
assert arrayTo[ idElementTo ] == defaultValueTest
else:
dataFrom: Union[ vtkPointData, vtkCellData ]
if isinstance( meshFrom, vtkDataSet ):
dataFrom = meshFrom.GetPointData() if onPoints else meshFrom.GetCellData()
elif isinstance( meshFrom, vtkMultiBlockDataSet ):
flatIdDataSetFrom: int = int( elementMap[ flatIdDataSetTo ][ idElementTo ][ 0 ] )
dataSetFrom: vtkDataSet = vtkDataSet.SafeDownCast( meshFrom.GetDataSet( flatIdDataSetFrom ) )
dataFrom = dataSetFrom.GetPointData() if onPoints else dataSetFrom.GetCellData()
arrayFrom: npt.NDArray[ Any ] = vnp.vtk_to_numpy( dataFrom.GetArray( attributeName ) )
assert np.all( arrayTo[ idElementTo ] == arrayFrom[ idElementFrom ] )
@pytest.mark.parametrize( "attributeName, onPoints", [
( "CellAttribute", False ),
( "PointAttribute", True ),
] )
def test_renameAttributeMultiblock(
dataSetTest: vtkMultiBlockDataSet,
attributeName: str,
onPoints: bool,
) -> None:
"""Test renaming attribute in a multiblock dataset."""
vtkMultiBlockDataSetTest: vtkMultiBlockDataSet = dataSetTest( "multiblock" )
newAttributeName: str = "new" + attributeName
arrayModifiers.renameAttribute(
vtkMultiBlockDataSetTest,
attributeName,
newAttributeName,
onPoints,
)
block: vtkDataSet = vtkDataSet.SafeDownCast( vtkMultiBlockDataSetTest.GetDataSet( 1 ) )
data: Union[ vtkPointData, vtkCellData ]
if onPoints:
data = block.GetPointData()
assert data.HasArray( attributeName ) == 0
assert data.HasArray( newAttributeName ) == 1
else:
data = block.GetCellData()
assert data.HasArray( attributeName ) == 0
assert data.HasArray( newAttributeName ) == 1
@pytest.mark.parametrize( "attributeName, onPoints", [ ( "CellAttribute", False ), ( "PointAttribute", True ) ] )
def test_renameAttributeDataSet(
dataSetTest: vtkDataSet,
attributeName: str,
onPoints: bool,
) -> None:
"""Test renaming an attribute in a dataset."""
vtkDataSetTest: vtkDataSet = dataSetTest( "dataset" )
newAttributeName: str = "new" + attributeName
arrayModifiers.renameAttribute( object=vtkDataSetTest,
attributeName=attributeName,
newAttributeName=newAttributeName,
onPoints=onPoints )
if onPoints:
assert vtkDataSetTest.GetPointData().HasArray( attributeName ) == 0
assert vtkDataSetTest.GetPointData().HasArray( newAttributeName ) == 1
else:
assert vtkDataSetTest.GetCellData().HasArray( attributeName ) == 0
assert vtkDataSetTest.GetCellData().HasArray( newAttributeName ) == 1