forked from pyvista/pyvista
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_filters.py
312 lines (261 loc) · 10.6 KB
/
test_filters.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
import numpy as np
import pytest
import vtki
from vtki import examples
datasets = [
examples.load_uniform(), # UniformGrid
examples.load_rectilinear(), # RectilinearGrid
examples.load_hexbeam(), # UnstructuredGrid
examples.load_airplane(), # PolyData
examples.load_structured(), # StructuredGrid
]
normals = ['x', 'y', '-z', (1,1,1), (3.3, 5.4, 0.8)]
def test_clip_filter():
"""This tests the clip filter on all datatypes avaialble filters"""
for i, dataset in enumerate(datasets):
clp = dataset.clip(normal=normals[i], invert=True)
assert clp is not None
assert isinstance(clp, vtki.UnstructuredGrid)
def test_clip_box():
for i, dataset in enumerate(datasets):
clp = dataset.clip_box(invert=True)
assert clp is not None
assert isinstance(clp, vtki.UnstructuredGrid)
dataset = examples.load_airplane()
# test length 3 bounds
result = dataset.clip_box(bounds=(900, 900, 200), invert=False)
dataset = examples.load_uniform()
result = dataset.clip_box(bounds=0.5)
with pytest.raises(AssertionError):
dataset.clip_box(bounds=(5, 6,))
def test_slice_filter():
"""This tests the slice filter on all datatypes avaialble filters"""
for i, dataset in enumerate(datasets):
slc = dataset.slice(normal=normals[i])
assert slc is not None
assert isinstance(slc, vtki.PolyData)
dataset = examples.load_uniform()
with pytest.raises(AssertionError):
dataset.slice(origin=(10, 15, 15))
def test_slice_orthogonal_filter():
"""This tests the slice filter on all datatypes avaialble filters"""
for i, dataset in enumerate(datasets):
slices = dataset.slice_orthogonal()
assert slices is not None
assert isinstance(slices, vtki.MultiBlock)
assert slices.n_blocks == 3
for slc in slices:
assert isinstance(slc, vtki.PolyData)
def test_slice_along_axis():
"""Test the many slices along axis filter """
axii = ['x', 'y', 'z', 'y', 0]
ns = [2, 3, 4, 10, 20, 13]
for i, dataset in enumerate(datasets):
slices = dataset.slice_along_axis(n=ns[i], axis=axii[i])
assert slices is not None
assert isinstance(slices, vtki.MultiBlock)
assert slices.n_blocks == ns[i]
for slc in slices:
assert isinstance(slc, vtki.PolyData)
dataset = examples.load_uniform()
with pytest.raises(RuntimeError):
dataset.slice_along_axis(axis='u')
def test_threshold():
for i, dataset in enumerate(datasets[0:3]):
thresh = dataset.threshold()
assert thresh is not None
assert isinstance(thresh, vtki.UnstructuredGrid)
# Test value ranges
dataset = examples.load_uniform() # UniformGrid
thresh = dataset.threshold(100, invert=False)
assert thresh is not None
assert isinstance(thresh, vtki.UnstructuredGrid)
thresh = dataset.threshold([100, 500], invert=False)
assert thresh is not None
assert isinstance(thresh, vtki.UnstructuredGrid)
thresh = dataset.threshold([100, 500], invert=True)
assert thresh is not None
assert isinstance(thresh, vtki.UnstructuredGrid)
# Now test datasets without arrays
with pytest.raises(AssertionError):
for i, dataset in enumerate(datasets[3:-1]):
thresh = dataset.threshold()
assert thresh is not None
assert isinstance(thresh, vtki.UnstructuredGrid)
dataset = examples.load_uniform()
with pytest.raises(AssertionError):
dataset.threshold([10, 100, 300])
def test_threshold_percent():
percents = [25, 50, [18.0, 85.0], [19.0, 80.0], 0.70]
inverts = [False, True, False, True, False]
# Only test data sets that have arrays
for i, dataset in enumerate(datasets[0:3]):
thresh = dataset.threshold_percent(percent=percents[i], invert=inverts[i])
assert thresh is not None
assert isinstance(thresh, vtki.UnstructuredGrid)
dataset = examples.load_uniform()
result = dataset.threshold_percent(0.75, scalars='Spatial Cell Data')
with pytest.raises(RuntimeError):
result = dataset.threshold_percent(20000)
with pytest.raises(RuntimeError):
result = dataset.threshold_percent(0.0)
def test_outline():
for i, dataset in enumerate(datasets):
outline = dataset.outline()
assert outline is not None
assert isinstance(outline, vtki.PolyData)
def test_outline_corners():
for i, dataset in enumerate(datasets):
outline = dataset.outline_corners()
assert outline is not None
assert isinstance(outline, vtki.PolyData)
def test_extract_geometry():
for i, dataset in enumerate(datasets):
outline = dataset.extract_geometry()
assert outline is not None
assert isinstance(outline, vtki.PolyData)
def test_wireframe():
for i, dataset in enumerate(datasets):
wire = dataset.wireframe()
assert wire is not None
assert isinstance(wire, vtki.PolyData)
def test_contour():
dataset = examples.load_uniform()
iso = dataset.contour()
assert iso is not None
iso = dataset.contour(isosurfaces=[100, 300, 500])
assert iso is not None
with pytest.raises(AssertionError):
result = dataset.contour(scalars='Spatial Cell Data')
with pytest.raises(RuntimeError):
result = dataset.contour(isosurfaces=vtki.PolyData())
dataset = examples.load_airplane()
with pytest.raises(AssertionError):
result = dataset.contour()
def test_elevation():
dataset = examples.load_uniform()
# Test default params
elev = dataset.elevation()
assert 'Elevation' in elev.scalar_names
assert 'Elevation' == elev.active_scalar_name
assert elev.get_data_range() == (dataset.bounds[4], dataset.bounds[5])
# test vector args
c = list(dataset.center)
t = list(c) # cast so it doesnt point to `c`
t[2] = dataset.bounds[-1]
elev = dataset.elevation(low_point=c, high_point=t)
assert 'Elevation' in elev.scalar_names
assert 'Elevation' == elev.active_scalar_name
assert elev.get_data_range() == (dataset.center[2], dataset.bounds[5])
# Test not setting active
elev = dataset.elevation(set_active=False)
assert 'Elevation' in elev.scalar_names
assert 'Elevation' != elev.active_scalar_name
# Set use a range by scalar name
elev = dataset.elevation(scalar_range='Spatial Point Data')
assert 'Elevation' in elev.scalar_names
assert 'Elevation' == elev.active_scalar_name
assert dataset.get_data_range('Spatial Point Data') == (elev.get_data_range('Elevation'))
# Set use a user defined range
elev = dataset.elevation(scalar_range=[1.0, 100.0])
assert 'Elevation' in elev.scalar_names
assert 'Elevation' == elev.active_scalar_name
assert elev.get_data_range('Elevation') == (1.0, 100.0)
# test errors
with pytest.raises(RuntimeError):
elev = dataset.elevation(scalar_range=0.5)
def test_texture_map_to_plane():
dataset = examples.load_airplane()
# Automatically decide plane
out = dataset.texture_map_to_plane(inplace=False)
assert isinstance(out, type(dataset))
# Define the plane explicitly
bnds = dataset.bounds
origin = bnds[0::2]
point_u = (bnds[1], bnds[2], bnds[4])
point_v = (bnds[0], bnds[3], bnds[4])
out = dataset.texture_map_to_plane(origin=origin, point_u=point_u, point_v=point_v)
assert isinstance(out, type(dataset))
assert 'Texture Coordinates' in out.scalar_names
# FINAL: Test in place modifiacation
dataset.texture_map_to_plane(inplace=True)
assert 'Texture Coordinates' in dataset.scalar_names
def test_compute_cell_sizes():
for i, dataset in enumerate(datasets):
result = dataset.compute_cell_sizes()
assert result is not None
assert isinstance(result, type(dataset))
assert 'Area' in result.scalar_names
assert 'Volume' in result.scalar_names
# Test the volume property
grid = vtki.UniformGrid((10,10,10))
volume = float(np.prod(np.array(grid.dimensions) - 1))
assert np.allclose(grid.volume, volume)
def test_cell_centers():
for i, dataset in enumerate(datasets):
result = dataset.cell_centers()
assert result is not None
assert isinstance(result, vtki.PolyData)
def test_glyph():
for i, dataset in enumerate(datasets):
result = dataset.glyph()
assert result is not None
assert isinstance(result, vtki.PolyData)
# Test different options for glyph filter
sphere = vtki.Sphere(radius=3.14)
# make cool swirly pattern
vectors = np.vstack((np.sin(sphere.points[:, 0]),
np.cos(sphere.points[:, 1]),
np.cos(sphere.points[:, 2]))).T
# add and scale
sphere.vectors = vectors*0.3
sphere.point_arrays['foo'] = np.random.rand(sphere.n_points)
sphere.point_arrays['arr'] = np.ones(sphere.n_points)
result = sphere.glyph(scale='arr')
result = sphere.glyph(scale='arr', orient='Normals', factor=0.1)
def test_split_and_connectivity():
# Load a simple example mesh
dataset = examples.load_uniform()
dataset.set_active_scalar('Spatial Cell Data')
threshed = dataset.threshold_percent([0.15, 0.50], invert=True)
bodies = threshed.split_bodies()
volumes = [518.0, 35.0]
assert len(volumes) == bodies.n_blocks
for i, body in enumerate(bodies):
assert np.allclose(body.volume, volumes[i], rtol=0.1)
def test_warp_by_scalar():
data = examples.load_uniform()
warped = data.warp_by_scalar()
assert data.n_points == warped.n_points
def test_cell_data_to_point_data():
data = examples.load_uniform()
foo = data.cell_data_to_point_data()
assert foo.n_scalars == 2
assert len(foo.cell_arrays.keys()) == 0
def test_point_data_to_cell_data():
data = examples.load_uniform()
foo = data.point_data_to_cell_data()
assert foo.n_scalars == 2
assert len(foo.point_arrays.keys()) == 0
def test_triangulate():
data = examples.load_uniform()
tri = data.triangulate()
assert isinstance(tri, vtki.UnstructuredGrid)
assert np.any(tri.cells)
def test_delaunay_3d():
data = examples.load_uniform().threshold_percent(30)
result = data.delaunay_3d()
assert np.any(result.points)
def test_smooth():
data = examples.load_uniform()
vol = data.threshold_percent(30)
surf = vol.extract_geometry()
smooth = surf.smooth()
assert np.any(smooth.points)
def test_resample():
mesh = vtki.Sphere(center=(4.5,4.5,4.5), radius=4.5)
data_to_probe = examples.load_uniform()
result = mesh.sample(data_to_probe)
name = 'Spatial Point Data'
assert name in result.scalar_names
assert isinstance(result, type(mesh))