forked from pyvista/pyvista
/
test_grid.py
344 lines (265 loc) · 10.3 KB
/
test_grid.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
import os
import vtk
import numpy as np
import pytest
import vtki
from vtki import examples
from vtki.plotting import running_xserver
beam = vtki.UnstructuredGrid(examples.hexbeamfile)
# create structured grid
x = np.arange(-10, 10, 2)
y = np.arange(-10, 10, 2)
z = np.arange(-10, 10, 2)
x, y, z = np.meshgrid(x, y, z)
sgrid = vtki.StructuredGrid(x, y, z)
try:
test_path = os.path.dirname(os.path.abspath(__file__))
test_data_path = os.path.join(test_path, 'test_data')
except:
test_path = '/home/alex/afrl/python/source/vtki/tests'
def test_volume():
assert beam.volume > 0.0
def test_merge():
beamA = vtki.UnstructuredGrid(examples.hexbeamfile)
beamB = beamA.copy()
beamB.points[:, 1] += 1
beamA.Merge(beamB)
@pytest.mark.skipif(not running_xserver(), reason="Requires X11")
def test_struct_example():
# create and plot structured grid
grid = examples.load_structured()
cpos = grid.plot(off_screen=True) # basic plot
assert isinstance(cpos, list)
# Plot mean curvature
cpos_curv = grid.plot_curvature(off_screen=True)
assert isinstance(cpos_curv, list)
def test_init_from_structured():
unstruct_grid = vtki.UnstructuredGrid(sgrid)
assert unstruct_grid.points.shape[0] == x.size
assert np.all(unstruct_grid.celltypes == 12)
def test_init_from_unstructured():
grid = vtki.UnstructuredGrid(beam, deep=True)
grid.points += 1
assert not np.any(grid.points == beam.points)
def test_init_bad_input():
with pytest.raises(Exception):
unstruct_grid = vtki.UnstructuredGrid(np.array(1))
with pytest.raises(Exception):
unstruct_grid = vtki.UnstructuredGrid(np.array(1),
np.array(1),
np.array(1),
'woa')
def test_init_from_arrays():
offset = np.array([0, 9], np.int8)
cells = np.array([8, 0, 1, 2, 3, 4, 5, 6, 7, 8, 8, 9, 10, 11, 12, 13, 14, 15])
cell_type = np.array([vtk.VTK_HEXAHEDRON, vtk.VTK_HEXAHEDRON], np.int32)
cell1 = np.array([[0, 0, 0],
[1, 0, 0],
[1, 1, 0],
[0, 1, 0],
[0, 0, 1],
[1, 0, 1],
[1, 1, 1],
[0, 1, 1]])
cell2 = np.array([[0, 0, 2],
[1, 0, 2],
[1, 1, 2],
[0, 1, 2],
[0, 0, 3],
[1, 0, 3],
[1, 1, 3],
[0, 1, 3]])
points = np.vstack((cell1, cell2)).astype(np.int32)
grid = vtki.UnstructuredGrid(offset, cells, cell_type, points)
assert grid.n_cells == 2
assert np.allclose(grid.offset, offset)
def test_surface_indices():
surf = beam.extract_surface()
surf_ind = surf.point_arrays['vtkOriginalPointIds']
assert np.allclose(surf_ind, beam.surface_indices())
def test_extract_edges():
edges = beam.extract_edges(90)
assert edges.n_points
edges = beam.extract_edges(180)
assert not edges.n_points
@pytest.mark.parametrize('binary', [True, False])
@pytest.mark.parametrize('extension', ['vtu', 'vtk'])
def test_save(extension, binary, tmpdir):
filename = str(tmpdir.mkdir("tmpdir").join('tmp.%s' % extension))
beam.save(filename, binary)
grid = vtki.UnstructuredGrid(filename)
assert grid.cells.shape == beam.cells.shape
assert grid.points.shape == beam.points.shape
def test_init_bad_filename():
filename = os.path.join(test_path, 'test_grid.py')
with pytest.raises(Exception):
grid = vtki.UnstructuredGrid(filename)
with pytest.raises(Exception):
grid = vtki.UnstructuredGrid('not a file')
def test_save_bad_extension():
with pytest.raises(Exception):
grid = vtki.UnstructuredGrid('file.abc')
def test_linear_copy():
# need a grid with quadratic cells
lgrid = beam.linear_copy()
assert np.all(lgrid.celltypes < 20)
def test_extract_cells():
ind = [1, 2, 3]
part_beam = beam.extract_cells(ind)
assert part_beam.n_cells == len(ind)
assert part_beam.n_points < beam.n_points
mask = np.zeros(beam.n_cells, np.bool)
mask[:3] = True
part_beam = beam.extract_cells(mask)
assert part_beam.n_cells == len(ind)
assert part_beam.n_points < beam.n_points
def test_merge():
grid = beam.copy()
grid.points[:, 0] += 1
unmerged = grid.merge(beam, inplace=False, merge_points=False)
grid.merge(beam)
assert grid.n_points > beam.n_points
assert grid.n_points < unmerged.n_points
def test_merge_not_main():
grid = beam.copy()
grid.points[:, 0] += 1
unmerged = grid.merge(beam, inplace=False, merge_points=False,
main_has_priority=False)
grid.merge(beam)
assert grid.n_points > beam.n_points
assert grid.n_points < unmerged.n_points
def test_merge_list():
grid_a = beam.copy()
grid_a.points[:, 0] += 1
grid_b = beam.copy()
grid_b.points[:, 1] += 1
grid_a.merge([beam, grid_b])
assert grid_a.n_points > beam.n_points
def test_init_structured():
xrng = np.arange(-10, 10, 2)
yrng = np.arange(-10, 10, 2)
zrng = np.arange(-10, 10, 2)
x, y, z = np.meshgrid(xrng, yrng, zrng)
grid = vtki.StructuredGrid(x, y, z)
assert np.allclose(sgrid.x, x)
assert np.allclose(sgrid.y, y)
assert np.allclose(sgrid.z, z)
grid_a = vtki.StructuredGrid(grid)
assert np.allclose(grid_a.points, grid.points)
def test_invalid_init_structured():
xrng = np.arange(-10, 10, 2)
yrng = np.arange(-10, 10, 2)
zrng = np.arange(-10, 10, 2)
x, y, z = np.meshgrid(xrng, yrng, zrng)
z = z[:, :, :2]
with pytest.raises(Exception):
grid = vtki.StructuredGrid(x, y, z)
@pytest.mark.parametrize('binary', [True, False])
@pytest.mark.parametrize('extension', ['vts', 'vtk'])
def test_save_structured(extension, binary, tmpdir):
filename = str(tmpdir.mkdir("tmpdir").join('tmp.%s' % extension))
sgrid.save(filename, binary)
grid = vtki.StructuredGrid(filename)
assert grid.x.shape == sgrid.y.shape
assert grid.n_cells
assert grid.points.shape == sgrid.points.shape
def test_load_structured_bad_filename():
with pytest.raises(Exception):
vtki.StructuredGrid('not a file')
filename = os.path.join(test_path, 'test_grid.py')
with pytest.raises(Exception):
grid = vtki.StructuredGrid(filename)
def test_create_rectilinear_grid_from_specs():
xrng = np.arange(-10, 10, 2)
yrng = np.arange(-10, 10, 5)
zrng = np.arange(-10, 10, 1)
grid = vtki.RectilinearGrid(xrng, yrng, zrng)
assert grid.n_cells == 9*3*19
assert grid.n_points == 10*4*20
assert grid.bounds == (-10.0,8.0, -10.0,5.0, -10.0,9.0)
def test_create_rectilinear_grid_from_file():
grid = examples.load_rectilinear()
assert grid.n_cells == 16146
assert grid.n_points == 18144
assert grid.bounds == (-350.0,1350.0, -400.0,1350.0, -850.0,0.0)
assert grid.n_scalars == 1
def test_create_uniform_grid_from_specs():
# create UniformGrid
dims = (10, 10, 10)
grid = vtki.UniformGrid(dims) # Using default spacing and origin
assert grid.dimensions == (10, 10, 10)
assert grid.origin == (0.0, 0.0, 0.0)
assert grid.spacing == (1.0, 1.0, 1.0)
spacing = (2, 1, 5)
grid = vtki.UniformGrid(dims, spacing) # Usign default origin
assert grid.dimensions == (10, 10, 10)
assert grid.origin == (0.0, 0.0, 0.0)
assert grid.spacing == (2.0, 1.0, 5.0)
origin = (10, 35, 50)
grid = vtki.UniformGrid(dims, spacing, origin) # Everything is specified
assert grid.dimensions == (10, 10, 10)
assert grid.origin == (10.0, 35.0, 50.0)
assert grid.spacing == (2.0, 1.0, 5.0)
assert grid.dimensions == (10, 10, 10)
def test_uniform_setters():
grid = vtki.UniformGrid()
grid.dimensions = (10, 10, 10)
assert grid.GetDimensions() == (10, 10, 10)
grid.spacing = (5, 2, 1)
assert grid.GetSpacing() == (5, 2, 1)
grid.origin = (6, 27.7, 19.8)
assert grid.GetOrigin() == (6, 27.7, 19.8)
def test_create_uniform_grid_from_file():
grid = examples.load_uniform()
assert grid.n_cells == 729
assert grid.n_points == 1000
assert grid.bounds == (0.0,9.0, 0.0,9.0, 0.0,9.0)
assert grid.n_scalars == 2
assert grid.dimensions == (10, 10, 10)
@pytest.mark.parametrize('binary', [True, False])
@pytest.mark.parametrize('extension', ['vtr', 'vtk'])
def test_save_rectilinear(extension, binary, tmpdir):
filename = str(tmpdir.mkdir("tmpdir").join('tmp.%s' % extension))
ogrid = examples.load_rectilinear()
ogrid.save(filename, binary)
grid = vtki.RectilinearGrid(filename)
assert grid.n_cells == ogrid.n_cells
assert np.allclose(grid.x, ogrid.x)
assert np.allclose(grid.y, ogrid.y)
assert np.allclose(grid.z, ogrid.z)
assert grid.dimensions == ogrid.dimensions
@pytest.mark.parametrize('binary', [True, False])
@pytest.mark.parametrize('extension', ['vti', 'vtk'])
def test_save_uniform(extension, binary, tmpdir):
filename = str(tmpdir.mkdir("tmpdir").join('tmp.%s' % extension))
ogrid = examples.load_uniform()
ogrid.save(filename, binary)
grid = vtki.UniformGrid(filename)
assert grid.n_cells == ogrid.n_cells
assert grid.origin == ogrid.origin
assert grid.spacing == ogrid.spacing
assert grid.dimensions == ogrid.dimensions
def test_grid_points():
"""Test the points mehtods on UniformGrid and RectilinearGrid"""
points = np.array([[0, 0, 0],
[1, 0, 0],
[1, 1, 0],
[0, 1, 0],
[0, 0, 1],
[1, 0, 1],
[1, 1, 1],
[0, 1, 1]])
grid = vtki.UniformGrid()
grid.points = points
assert grid.dimensions == (2, 2, 2)
assert grid.spacing == (1, 1, 1)
assert grid.origin == (0., 0., 0.)
assert np.allclose(np.unique(grid.points, axis=0), np.unique(points, axis=0))
opts = np.c_[grid.x, grid.y, grid.z]
assert np.allclose(np.unique(opts, axis=0), np.unique(points, axis=0))
# Now test rectilinear grid
del grid
grid = vtki.RectilinearGrid()
grid.points = points
assert grid.dimensions == (2, 2, 2)
assert np.allclose(np.unique(grid.points, axis=0), np.unique(points, axis=0))