-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_arrayHelpers.py
More file actions
244 lines (202 loc) · 9.59 KB
/
test_arrayHelpers.py
File metadata and controls
244 lines (202 loc) · 9.59 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
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright 2023-2024 TotalEnergies.
# SPDX-FileContributor: Paloma Martinez
# SPDX-License-Identifier: Apache 2.0
# ruff: noqa: E402 # disable Module level import not at top of file
# mypy: disable-error-code="operator, attr-defined"
import pytest
from typing import Tuple
import numpy as np
import numpy.typing as npt
import vtkmodules.util.numpy_support as vnp
import pandas as pd # type: ignore[import-untyped]
from vtkmodules.vtkCommonCore import vtkDoubleArray
from vtkmodules.vtkCommonDataModel import vtkDataSet, vtkMultiBlockDataSet, vtkPolyData
from geos.mesh.utils import arrayHelpers
@pytest.mark.parametrize( "onpoints, expected", [ ( True, {
'GLOBAL_IDS_POINTS': 1,
'collocated_nodes': 2,
'PointAttribute': 3
} ), ( False, {
'CELL_MARKERS': 1,
'PERM': 3,
'PORO': 1,
'FAULT': 1,
'GLOBAL_IDS_CELLS': 1,
'CellAttribute': 3
} ) ] )
def test_getAttributeFromMultiBlockDataSet( dataSetTest: vtkMultiBlockDataSet, onpoints: bool,
expected: dict[ str, int ] ) -> None:
"""Test getting attribute list as dict from multiblock."""
multiBlockTest: vtkMultiBlockDataSet = dataSetTest( "multiblock" )
attributes: dict[ str, int ] = arrayHelpers.getAttributesFromMultiBlockDataSet( multiBlockTest, onpoints )
assert attributes == expected
@pytest.mark.parametrize( "onpoints, expected", [ ( True, {
'GLOBAL_IDS_POINTS': 1,
'PointAttribute': 3,
} ), ( False, {
'CELL_MARKERS': 1,
'PERM': 3,
'PORO': 1,
'FAULT': 1,
'GLOBAL_IDS_CELLS': 1,
'CellAttribute': 3
} ) ] )
def test_getAttributesFromDataSet( dataSetTest: vtkDataSet, onpoints: bool, expected: dict[ str, int ] ) -> None:
"""Test getting attribute list as dict from dataset."""
vtkDataSetTest: vtkDataSet = dataSetTest( "dataset" )
attributes: dict[ str, int ] = arrayHelpers.getAttributesFromDataSet( vtkDataSetTest, onpoints )
assert attributes == expected
@pytest.mark.parametrize( "attributeName, onpoints, expected", [
( "PORO", False, 1 ),
( "PORO", True, 0 ),
] )
def test_isAttributeInObjectMultiBlockDataSet( dataSetTest: vtkMultiBlockDataSet, attributeName: str, onpoints: bool,
expected: dict[ str, int ] ) -> None:
"""Test presence of attribute in a multiblock."""
multiBlockDataset: vtkMultiBlockDataSet = dataSetTest( "multiblock" )
obtained: bool = arrayHelpers.isAttributeInObjectMultiBlockDataSet( multiBlockDataset, attributeName, onpoints )
assert obtained == expected
@pytest.mark.parametrize( "attributeName, onpoints, expected", [
( "PORO", False, 1 ),
( "PORO", True, 0 ),
] )
def test_isAttributeInObjectDataSet( dataSetTest: vtkDataSet, attributeName: str, onpoints: bool,
expected: bool ) -> None:
"""Test presence of attribute in a dataset."""
vtkDataset: vtkDataSet = dataSetTest( "dataset" )
obtained: bool = arrayHelpers.isAttributeInObjectDataSet( vtkDataset, attributeName, onpoints )
assert obtained == expected
@pytest.mark.parametrize( "attributeName, onpoints, expected", [
( "PORO", False, False ),
( "GLOBAL_IDS_POINTS", True, True ),
] )
def test_isAttributeGlobal(
dataSetTest: vtkMultiBlockDataSet,
attributeName: str,
onpoints: bool,
expected: bool,
) -> None:
"""Test if the attribute is global or partial."""
multiBlockDataset: vtkMultiBlockDataSet = dataSetTest( "multiblock" )
obtained: bool = arrayHelpers.isAttributeGlobal( multiBlockDataset, attributeName, onpoints )
assert obtained == expected
@pytest.mark.parametrize( "arrayExpected, onpoints", [
( "PORO", False ),
( "PERM", False ),
( "PointAttribute", True ),
],
indirect=[ "arrayExpected" ] )
def test_getArrayInObject( request: pytest.FixtureRequest, arrayExpected: npt.NDArray[ np.float64 ],
dataSetTest: vtkDataSet, onpoints: bool ) -> None:
"""Test getting numpy array of an attribute from dataset."""
vtkDataSetTest: vtkDataSet = dataSetTest( "dataset" )
params = request.node.callspec.params
attributeName: str = params[ "arrayExpected" ]
obtained: npt.NDArray[ np.float64 ] = arrayHelpers.getArrayInObject( vtkDataSetTest, attributeName, onpoints )
expected: npt.NDArray[ np.float64 ] = arrayExpected
assert ( obtained == expected ).all()
@pytest.mark.parametrize( "attributeName, vtkDataType, onPoints", [
( "CellAttribute", 11, False ),
( "PointAttribute", 11, True ),
( "collocated_nodes", 12, True ),
] )
def test_getVtkArrayTypeInMultiBlock( dataSetTest: vtkMultiBlockDataSet, attributeName: str, vtkDataType: int,
onPoints: bool ) -> None:
"""Test getting the type of the vtk array of an attribute from multiBlockDataSet."""
multiBlockDataSet: vtkMultiBlockDataSet = dataSetTest( "multiblock" )
vtkDataTypeTest: int = arrayHelpers.getVtkArrayTypeInMultiBlock( multiBlockDataSet, attributeName, onPoints )
assert ( vtkDataTypeTest == vtkDataType )
@pytest.mark.parametrize( "attributeName, onPoints", [
( "CellAttribute", False ),
( "PointAttribute", True ),
] )
def test_getVtkArrayTypeInObject( dataSetTest: vtkDataSet, attributeName: str, onPoints: bool ) -> None:
"""Test getting the type of the vtk array of an attribute from dataset."""
vtkDataSetTest: vtkDataSet = dataSetTest( "dataset" )
obtained: int = arrayHelpers.getVtkArrayTypeInObject( vtkDataSetTest, attributeName, onPoints )
expected: int = 11
assert ( obtained == expected )
@pytest.mark.parametrize( "arrayExpected, onpoints", [
( "PORO", False ),
( "PointAttribute", True ),
],
indirect=[ "arrayExpected" ] )
def test_getVtkArrayInObject( request: pytest.FixtureRequest, arrayExpected: npt.NDArray[ np.float64 ],
dataSetTest: vtkDataSet, onpoints: bool ) -> None:
"""Test getting Vtk Array from a dataset."""
vtkDataSetTest: vtkDataSet = dataSetTest( "dataset" )
params = request.node.callspec.params
attributeName: str = params[ 'arrayExpected' ]
obtained: vtkDoubleArray = arrayHelpers.getVtkArrayInObject( vtkDataSetTest, attributeName, onpoints )
obtained_as_np: npt.NDArray[ np.float64 ] = vnp.vtk_to_numpy( obtained )
assert ( obtained_as_np == arrayExpected ).all()
@pytest.mark.parametrize( "attributeName, onpoints, expected", [
( "PORO", False, 1 ),
( "PERM", False, 3 ),
( "PointAttribute", True, 3 ),
] )
def test_getNumberOfComponentsDataSet(
dataSetTest: vtkDataSet,
attributeName: str,
onpoints: bool,
expected: int,
) -> None:
"""Test getting the number of components of an attribute from a dataset."""
vtkDataSetTest: vtkDataSet = dataSetTest( "dataset" )
obtained: int = arrayHelpers.getNumberOfComponentsDataSet( vtkDataSetTest, attributeName, onpoints )
assert obtained == expected
@pytest.mark.parametrize( "attributeName, onpoints, expected", [
( "PORO", False, 1 ),
( "PERM", False, 3 ),
( "PointAttribute", True, 3 ),
] )
def test_getNumberOfComponentsMultiBlock(
dataSetTest: vtkMultiBlockDataSet,
attributeName: str,
onpoints: bool,
expected: int,
) -> None:
"""Test getting the number of components of an attribute from a multiblock."""
vtkMultiBlockDataSetTest: vtkMultiBlockDataSet = dataSetTest( "multiblock" )
obtained: int = arrayHelpers.getNumberOfComponentsMultiBlock( vtkMultiBlockDataSetTest, attributeName, onpoints )
assert obtained == expected
@pytest.mark.parametrize( "attributeName, onpoints, expected", [
( "PERM", False, ( "AX1", "AX2", "AX3" ) ),
( "PORO", False, () ),
] )
def test_getComponentNamesDataSet( dataSetTest: vtkDataSet, attributeName: str, onpoints: bool,
expected: tuple[ str, ...] ) -> None:
"""Test getting the component names of an attribute from a dataset."""
vtkDataSetTest: vtkDataSet = dataSetTest( "dataset" )
obtained: tuple[ str, ...] = arrayHelpers.getComponentNamesDataSet( vtkDataSetTest, attributeName, onpoints )
assert obtained == expected
@pytest.mark.parametrize( "attributeName, onpoints, expected", [
( "PERM", False, ( "AX1", "AX2", "AX3" ) ),
( "PORO", False, () ),
] )
def test_getComponentNamesMultiBlock(
dataSetTest: vtkMultiBlockDataSet,
attributeName: str,
onpoints: bool,
expected: tuple[ str, ...],
) -> None:
"""Test getting the component names of an attribute from a multiblock."""
vtkMultiBlockDataSetTest: vtkMultiBlockDataSet = dataSetTest( "multiblock" )
obtained: tuple[ str, ...] = arrayHelpers.getComponentNamesMultiBlock( vtkMultiBlockDataSetTest, attributeName,
onpoints )
assert obtained == expected
@pytest.mark.parametrize( "attributeNames, expected_columns", [
( ( "CellAttribute1", ), ( "CellAttribute1_0", "CellAttribute1_1", "CellAttribute1_2" ) ),
( (
"CellAttribute1",
"CellAttribute2",
), ( "CellAttribute2", "CellAttribute1_0", "CellAttribute1_1", "CellAttribute1_2" ) ),
] )
def test_getAttributeValuesAsDF( dataSetTest: vtkPolyData, attributeNames: Tuple[ str, ...],
expected_columns: Tuple[ str, ...] ) -> None:
"""Test getting an attribute from a polydata as a dataframe."""
polydataset: vtkPolyData = dataSetTest( "polydata" )
data: pd.DataFrame = arrayHelpers.getAttributeValuesAsDF( polydataset, attributeNames )
obtained_columns = data.columns.values.tolist()
assert obtained_columns == list( expected_columns )