/
test_utilities.py
588 lines (484 loc) · 19.3 KB
/
test_utilities.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
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
""" test pyvista.utilities """
import warnings
import pathlib
import os
import shutil
import numpy as np
import pytest
import unittest.mock as mock
import vtk
import pyvista
from pyvista import examples as ex
from pyvista.utilities import (
check_valid_vector,
errors,
fileio,
GPUInfo,
helpers,
Observer,
cells,
transformations
)
def test_createvectorpolydata_error():
orig = np.random.random((3, 1))
vec = np.random.random((3, 1))
with pytest.raises(ValueError):
helpers.vector_poly_data(orig, vec)
def test_createvectorpolydata_1D():
orig = np.random.random(3)
vec = np.random.random(3)
vdata = helpers.vector_poly_data(orig, vec)
assert np.any(vdata.points)
assert np.any(vdata.point_arrays['vectors'])
def test_createvectorpolydata():
orig = np.random.random((100, 3))
vec = np.random.random((100, 3))
vdata = helpers.vector_poly_data(orig, vec)
assert np.any(vdata.points)
assert np.any(vdata.point_arrays['vectors'])
@pytest.mark.parametrize('use_pathlib', [True, False])
def test_read(tmpdir, use_pathlib):
fnames = (ex.antfile, ex.planefile, ex.hexbeamfile, ex.spherefile,
ex.uniformfile, ex.rectfile)
if use_pathlib:
fnames = [pathlib.Path(fname) for fname in fnames]
types = (pyvista.PolyData, pyvista.PolyData, pyvista.UnstructuredGrid,
pyvista.PolyData, pyvista.UniformGrid, pyvista.RectilinearGrid)
for i, filename in enumerate(fnames):
obj = fileio.read(filename)
assert isinstance(obj, types[i])
# Now test the standard_reader_routine
for i, filename in enumerate(fnames):
# Pass attrs to for the standard_reader_routine to be used
obj = fileio.read(filename, attrs={'DebugOn': None})
assert isinstance(obj, types[i])
# this is also tested for each mesh types init from file tests
filename = str(tmpdir.mkdir("tmpdir").join(f'tmp.npy'))
arr = np.random.rand(10, 10)
np.save(filename, arr)
with pytest.raises(IOError):
_ = pyvista.read(filename)
# read non existing file
with pytest.raises(IOError):
_ = pyvista.read('this_file_totally_does_not_exist.vtk')
# Now test reading lists of files as multi blocks
multi = pyvista.read(fnames)
assert isinstance(multi, pyvista.MultiBlock)
assert multi.n_blocks == len(fnames)
nested = [ex.planefile,
[ex.hexbeamfile, ex.uniformfile]]
multi = pyvista.read(nested)
assert isinstance(multi, pyvista.MultiBlock)
assert multi.n_blocks == 2
assert isinstance(multi[1], pyvista.MultiBlock)
assert multi[1].n_blocks == 2
def test_read_force_ext(tmpdir):
fnames = (ex.antfile, ex.planefile, ex.hexbeamfile, ex.spherefile,
ex.uniformfile, ex.rectfile)
types = (pyvista.PolyData, pyvista.PolyData, pyvista.UnstructuredGrid,
pyvista.PolyData, pyvista.UniformGrid, pyvista.RectilinearGrid)
dummy_extension = '.dummy'
for fname, type in zip(fnames, types):
root, original_ext = os.path.splitext(fname)
_, name = os.path.split(root)
new_fname = tmpdir / name + '.' + dummy_extension
shutil.copy(fname, new_fname)
data = fileio.read(new_fname, force_ext=original_ext)
assert isinstance(data, type)
def test_read_force_ext_wrong_extension(tmpdir):
# try to read a .vtu file as .vts
# vtkXMLStructuredGridReader throws a VTK error about the validity of the XML file
# the returned dataset is empty
fname = tmpdir / 'airplane.vtu'
ex.load_airplane().cast_to_unstructured_grid().save(fname)
with warnings.catch_warnings():
warnings.simplefilter("ignore")
data = fileio.read(fname, force_ext='.vts')
assert data.n_points == 0
# try to read a .ply file as .vtm
# vtkXMLMultiBlockDataReader throws a VTK error about the validity of the XML file
# the returned dataset is empty
fname = ex.planefile
with warnings.catch_warnings():
warnings.simplefilter("ignore")
data = fileio.read(fname, force_ext='.vtm')
assert len(data) == 0
@mock.patch('pyvista.utilities.fileio.standard_reader_routine')
def test_read_legacy(srr_mock):
srr_mock.return_value = pyvista.read(ex.planefile)
pyvista.read_legacy('legacy.vtk')
args, kwargs = srr_mock.call_args
reader = args[0]
assert isinstance(reader, vtk.vtkDataSetReader)
assert reader.GetFileName().endswith('legacy.vtk')
# check error is raised when no data returned
srr_mock.reset_mock()
srr_mock.return_value = None
with pytest.raises(RuntimeError):
pyvista.read_legacy('legacy.vtk')
@mock.patch('pyvista.utilities.fileio.read_legacy')
def test_pyvista_read_legacy(read_legacy_mock):
# check that reading a file with extension .vtk calls `read_legacy`
# use the globefile as a dummy because pv.read() checks for the existence of the file
pyvista.read(ex.globefile)
args, kwargs = read_legacy_mock.call_args
filename = args[0]
assert filename == ex.globefile
@mock.patch('pyvista.utilities.fileio.read_exodus')
def test_pyvista_read_exodus(read_exodus_mock):
# check that reading a file with extension .e calls `read_exodus`
# use the globefile as a dummy because pv.read() checks for the existence of the file
pyvista.read(ex.globefile, force_ext='.e')
args, kwargs = read_exodus_mock.call_args
filename = args[0]
assert filename == ex.globefile
@pytest.mark.parametrize('auto_detect', (True, False))
@mock.patch('pyvista.utilities.fileio.standard_reader_routine')
def test_read_plot3d(srr_mock, auto_detect):
# with grid only
pyvista.read_plot3d(filename='grid.in', auto_detect=auto_detect)
srr_mock.assert_called_once()
args, kwargs = srr_mock.call_args
reader = args[0]
assert isinstance(reader, vtk.vtkMultiBlockPLOT3DReader)
assert reader.GetFileName().endswith('grid.in')
assert kwargs['filename'] is None
assert kwargs['attrs'] == {'SetAutoDetectFormat': auto_detect}
# with grid and q
srr_mock.reset_mock()
pyvista.read_plot3d(filename='grid.in', q_filenames='q1.save',
auto_detect=auto_detect)
args, kwargs = srr_mock.call_args
reader = args[0]
assert isinstance(reader, vtk.vtkMultiBlockPLOT3DReader)
assert reader.GetFileName().endswith('grid.in')
assert args[0].GetQFileName().endswith('q1.save')
assert kwargs['filename'] is None
assert kwargs['attrs'] == {'SetAutoDetectFormat': auto_detect}
def test_get_array():
grid = pyvista.UnstructuredGrid(ex.hexbeamfile)
# add array to both point/cell data with same name
carr = np.random.rand(grid.n_cells)
grid.cell_arrays.append(carr, 'test_data')
parr = np.random.rand(grid.n_points)
grid.point_arrays.append(parr, 'test_data')
# add other data
oarr = np.random.rand(grid.n_points)
grid.point_arrays.append(oarr, 'other')
farr = np.random.rand(grid.n_points * grid.n_cells)
grid.field_arrays.append(farr, 'field_data')
assert np.allclose(carr, helpers.get_array(grid, 'test_data', preference='cell'))
assert np.allclose(parr, helpers.get_array(grid, 'test_data', preference='point'))
assert np.allclose(oarr, helpers.get_array(grid, 'other'))
assert helpers.get_array(grid, 'foo') is None
assert helpers.get_array(grid, 'test_data', preference='field') is None
assert np.allclose(farr, helpers.get_array(grid, 'field_data', preference='field'))
def test_is_inside_bounds():
data = ex.load_uniform()
bnds = data.bounds
assert helpers.is_inside_bounds((0.5, 0.5, 0.5), bnds)
assert not helpers.is_inside_bounds((12, 5, 5), bnds)
assert not helpers.is_inside_bounds((5, 12, 5), bnds)
assert not helpers.is_inside_bounds((5, 5, 12), bnds)
assert not helpers.is_inside_bounds((12, 12, 12), bnds)
def test_get_sg_image_scraper():
scraper = pyvista._get_sg_image_scraper()
assert isinstance(scraper, pyvista.Scraper)
assert callable(scraper)
def test_voxelize():
mesh = pyvista.PolyData(ex.load_uniform().points)
vox = pyvista.voxelize(mesh, 0.5)
assert vox.n_cells
def test_voxelize_non_uniform_desnity():
mesh = pyvista.PolyData(ex.load_uniform().points)
vox = pyvista.voxelize(mesh, [0.5, 0.3, 0.2])
assert vox.n_cells
def test_voxelize_throws_when_density_is_not_length_3():
with pytest.raises(ValueError) as e:
mesh = pyvista.PolyData(ex.load_uniform().points)
vox = pyvista.voxelize(mesh, [0.5, 0.3])
assert "not enough values to unpack" in str(e.value)
def test_report():
report = pyvista.Report(gpu=True)
assert report is not None
report = pyvista.Report(gpu=False)
assert report is not None
def test_line_segments_from_points():
points = np.array([[0, 0, 0], [1, 0, 0], [1, 0, 0], [1, 1, 0]])
poly = pyvista.line_segments_from_points(points)
assert poly.n_cells == 2
assert poly.n_points == 4
cells = poly.lines
assert np.allclose(cells[:3], [2, 0, 1])
assert np.allclose(cells[3:], [2, 2, 3])
def test_lines_from_points():
points = np.array([[0, 0, 0], [1, 0, 0], [1, 1, 0]])
poly = pyvista.lines_from_points(points)
assert poly.n_cells == 2
assert poly.n_points == 3
cells = poly.lines
assert np.allclose(cells[:3], [2, 0, 1])
assert np.allclose(cells[3:], [2, 1, 2])
def test_grid_from_sph_coords():
x = np.arange(0.0, 360.0, 40.0) # longitude
y = np.arange(0.0, 181.0, 60.0) # colatitude
z = [1] # elevation (radius)
g = pyvista.grid_from_sph_coords(x, y, z)
assert g.n_cells == 24
assert g.n_points == 36
assert np.allclose(
g.bounds,
[
-0.8137976813493738,
0.8660254037844387,
-0.8528685319524434,
0.8528685319524433,
-1.0,
1.0,
],
)
assert np.allclose(g.points[1], [0.8660254037844386, 0.0, 0.5])
z = np.linspace(10, 30, 3)
g = pyvista.grid_from_sph_coords(x, y, z)
assert g.n_cells == 48
assert g.n_points == 108
assert np.allclose(g.points[0], [0.0, 0.0, 10.0])
def test_transform_vectors_sph_to_cart():
lon = np.arange(0.0, 360.0, 40.0) # longitude
lat = np.arange(0.0, 181.0, 60.0) # colatitude
lev = [1] # elevation (radius)
u, v = np.meshgrid(lon, lat, indexing="ij")
w = u ** 2 - v ** 2
uu, vv, ww = pyvista.transform_vectors_sph_to_cart(lon, lat, lev, u, v, w)
assert np.allclose(
[uu[-1, -1], vv[-1, -1], ww[-1, -1]],
[67.80403533828323, 360.8359915416445, -70000.0],
)
def test_vtkmatrix_to_from_array():
rng = np.random.default_rng()
array3x3 = rng.integers(0, 10, size=(3, 3))
matrix = pyvista.vtkmatrix_from_array(array3x3)
assert isinstance(matrix, vtk.vtkMatrix3x3)
for i in range(3):
for j in range(3):
assert matrix.GetElement(i, j) == array3x3[i, j]
array = pyvista.array_from_vtkmatrix(matrix)
assert isinstance(array, np.ndarray)
assert array.shape == (3, 3)
for i in range(3):
for j in range(3):
assert array[i, j] == matrix.GetElement(i, j)
array4x4 = rng.integers(0, 10, size=(4, 4))
matrix = pyvista.vtkmatrix_from_array(array4x4)
assert isinstance(matrix, vtk.vtkMatrix4x4)
for i in range(4):
for j in range(4):
assert matrix.GetElement(i, j) == array4x4[i, j]
array = pyvista.array_from_vtkmatrix(matrix)
assert isinstance(array, np.ndarray)
assert array.shape == (4, 4)
for i in range(4):
for j in range(4):
assert array[i, j] == matrix.GetElement(i, j)
# invalid cases
with pytest.raises(ValueError):
matrix = pyvista.vtkmatrix_from_array(np.arange(3 * 4).reshape(3, 4))
with pytest.raises(TypeError):
invalid = vtk.vtkTransform()
array = pyvista.array_from_vtkmatrix(invalid)
def test_assert_empty_kwargs():
kwargs = {}
assert errors.assert_empty_kwargs(**kwargs)
with pytest.raises(TypeError):
kwargs = {"foo":6}
errors.assert_empty_kwargs(**kwargs)
with pytest.raises(TypeError):
kwargs = {"foo":6, "goo":"bad"}
errors.assert_empty_kwargs(**kwargs)
def test_convert_id_list():
ids = np.array([4, 5, 8])
id_list = vtk.vtkIdList()
id_list.SetNumberOfIds(len(ids))
for i, v in enumerate(ids):
id_list.SetId(i, v)
converted = helpers.vtk_id_list_to_array(id_list)
assert np.allclose(converted, ids)
def test_progress_monitor():
mesh = pyvista.Sphere()
ugrid = mesh.delaunay_3d(progress_bar=True)
assert isinstance(ugrid, pyvista.UnstructuredGrid)
def test_observer():
msg = "KIND: In PATH, line 0\nfoo (ADDRESS): ALERT"
obs = Observer()
ret = obs.parse_message("foo")
assert ret[3] == "foo"
ret = obs.parse_message(msg)
assert ret[3] == "ALERT"
for kind in ["WARNING", "ERROR"]:
obs.log_message(kind, "foo")
obs(obj=None, event=None, message=msg)
assert obs.has_event_occurred()
assert obs.get_message() == "ALERT"
assert obs.get_message(etc=True) == msg
alg = vtk.vtkSphereSource()
alg.GetExecutive()
obs.observe(alg)
with pytest.raises(RuntimeError, match="algorithm"):
obs.observe(alg)
def test_gpuinfo():
gpuinfo = GPUInfo()
_repr = gpuinfo.__repr__()
_repr_html = gpuinfo._repr_html_()
assert isinstance(_repr, str) and len(_repr) > 1
assert isinstance(_repr_html, str) and len(_repr_html) > 1
# test corrupted internal infos
gpuinfo._gpu_info = 'foo'
for func_name in ['renderer', 'version', 'vendor']:
with pytest.raises(RuntimeError, match=func_name):
getattr(gpuinfo, func_name)()
def test_check_valid_vector():
with pytest.raises(TypeError, match="length three"):
check_valid_vector([0, 1])
check_valid_vector([0, 1, 2])
def test_cells_dict_utils():
# No pyvista object
with pytest.raises(ValueError):
cells.get_mixed_cells(None)
with pytest.raises(ValueError):
cells.get_mixed_cells(np.zeros(shape=[3, 3]))
cells_arr = np.array([3, 0, 1, 2, 3, 3, 4, 5])
cells_types = np.array([vtk.VTK_TRIANGLE] * 2)
assert np.all(cells.generate_cell_offsets(cells_arr, cells_types)
== cells.generate_cell_offsets(cells_arr, cells_types))
# Non-integer type
with pytest.raises(ValueError):
cells.generate_cell_offsets(cells_arr, cells_types.astype(np.float32))
with pytest.raises(ValueError):
cells.generate_cell_offsets_loop(cells_arr, cells_types.astype(np.float32))
# Inconsistency of cell array lengths
with pytest.raises(ValueError):
cells.generate_cell_offsets(np.array(cells_arr.tolist() + [6]), cells_types)
with pytest.raises(ValueError):
cells.generate_cell_offsets_loop(np.array(cells_arr.tolist() + [6]), cells_types)
with pytest.raises(ValueError):
cells.generate_cell_offsets(cells_arr, np.array(cells_types.tolist() + [vtk.VTK_TRIANGLE]))
with pytest.raises(ValueError):
cells.generate_cell_offsets_loop(cells_arr, np.array(cells_types.tolist() + [vtk.VTK_TRIANGLE]))
# Unknown cell type
np.all(cells.generate_cell_offsets(cells_arr, cells_types) ==
cells.generate_cell_offsets(cells_arr, np.array([255, 255])))
def test_apply_transformation_to_points():
mesh = ex.load_airplane()
points = mesh.points
points_orig = points.copy()
# identity 3 x 3
tf = np.eye(3)
points_new = transformations.apply_transformation_to_points(tf, points, inplace=False)
assert points_new == pytest.approx(points)
# identity 4 x 4
tf = np.eye(4)
points_new = transformations.apply_transformation_to_points(tf, points, inplace=False)
assert points_new == pytest.approx(points)
# scale in-place
tf = np.eye(4) * 2
tf[3, 3] = 1
r = transformations.apply_transformation_to_points(tf, points, inplace=True)
assert r is None
assert mesh.points == pytest.approx(2 * points_orig)
def _generate_vtk_err():
"""Simple operation which generates a VTK error."""
x, y, z = np.meshgrid(
np.arange(-10, 10, 0.5), np.arange(-10, 10, 0.5), np.arange(-10, 10, 0.5)
)
mesh = pyvista.StructuredGrid(x, y, z)
x2, y2, z2 = np.meshgrid(
np.arange(-1, 1, 0.5), np.arange(-1, 1, 0.5), np.arange(-1, 1, 0.5)
)
mesh2 = pyvista.StructuredGrid(x2, y2, z2)
alg = vtk.vtkStreamTracer()
obs = pyvista.Observer()
obs.observe(alg)
alg.SetInputDataObject(mesh)
alg.SetSourceData(mesh2)
alg.Update()
def test_vtk_error_catcher():
# raise_errors: False
error_catcher = pyvista.utilities.errors.VtkErrorCatcher()
with error_catcher:
_generate_vtk_err()
_generate_vtk_err()
assert len(error_catcher.events) == 2
# raise_errors: False, no error
error_catcher = pyvista.utilities.errors.VtkErrorCatcher()
with error_catcher:
pass
# raise_errors: True
error_catcher = pyvista.utilities.errors.VtkErrorCatcher(raise_errors=True)
with pytest.raises(RuntimeError):
with error_catcher:
_generate_vtk_err()
assert len(error_catcher.events) == 1
# raise_errors: True, no error
error_catcher = pyvista.utilities.errors.VtkErrorCatcher(raise_errors=True)
with error_catcher:
pass
def test_axis_angle_rotation():
# rotate cube corners around body diagonal
points = np.array([
[1, 0, 0],
[0, 1, 0],
[0, 0, 1],
])
axis = [1, 1, 1]
# no-op case
angle = 360
trans = transformations.axis_angle_rotation(axis, angle)
actual = transformations.apply_transformation_to_points(trans, points)
assert np.array_equal(actual, points)
# default origin
angle = np.radians(120)
expected = points[[1, 2, 0], :]
trans = transformations.axis_angle_rotation(axis, angle, deg=False)
actual = transformations.apply_transformation_to_points(trans, points)
assert np.allclose(actual, expected)
# non-default origin
p0 = [-2, -3, 4]
points += p0
expected += p0
trans = transformations.axis_angle_rotation(axis, angle, point=p0, deg=False)
actual = transformations.apply_transformation_to_points(trans, points)
assert np.allclose(actual, expected)
# invalid cases
with pytest.raises(ValueError):
transformations.axis_angle_rotation([1, 0, 0, 0], angle)
with pytest.raises(ValueError):
transformations.axis_angle_rotation(axis, angle, point=[1, 0, 0, 0])
with pytest.raises(ValueError):
transformations.axis_angle_rotation([0, 0, 0], angle)
def test_reflection():
# reflect points of a square across a diagonal
points = np.array([
[ 1, 1, 0],
[-1, 1, 0],
[-1, -1, 0],
[ 1, -1, 0],
])
normal = [1, 1, 0]
# default origin
expected = points[[2, 1, 0, 3], :]
trans = transformations.reflection(normal)
actual = transformations.apply_transformation_to_points(trans, points)
assert np.allclose(actual, expected)
# non-default origin
p0 = [1, 1, 0]
expected += 2 * np.array(p0)
trans = transformations.reflection(normal, point=p0)
actual = transformations.apply_transformation_to_points(trans, points)
assert np.allclose(actual, expected)
# invalid cases
with pytest.raises(ValueError):
transformations.reflection([1, 0, 0, 0])
with pytest.raises(ValueError):
transformations.reflection(normal, point=[1, 0, 0, 0])
with pytest.raises(ValueError):
transformations.reflection([0, 0, 0])