def test_clip_box(): for i, dataset in enumerate(DATASETS): clp = dataset.clip_box(invert=True) assert clp is not None assert isinstance(clp, pyvista.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) assert result.n_cells with pytest.raises(ValueError): dataset.clip_box(bounds=( 5, 6, )) # allow Sequence but not Iterable bounds with pytest.raises(TypeError): dataset.clip_box(bounds={5, 6, 7}) # Test with a poly data box mesh = examples.load_airplane() box = pyvista.Cube(center=(0.9e3, 0.2e3, mesh.center[2]), x_length=500, y_length=500, z_length=500) box.rotate_z(33) result = mesh.clip_box(box, invert=False) assert result.n_cells result = mesh.clip_box(box, invert=True) assert result.n_cells with pytest.raises(ValueError): dataset.clip_box(bounds=pyvista.Sphere())
def test_add_background_image_subplots(): pl = pyvista.Plotter(shape=(2, 2)) pl.add_background_image(examples.mapfile, scale=1, as_global=False) pl.add_mesh(examples.load_airplane()) pl.subplot(1, 1) pl.add_background_image(examples.mapfile, scale=1, as_global=False) pl.add_mesh(examples.load_airplane()) pl.remove_background_image() # should error out as there's no background with pytest.raises(RuntimeError): pl.remove_background_image() pl.add_background_image(examples.mapfile, scale=1, as_global=False) pl.show()
def test_plot_eye_dome_lighting(): mesh = examples.load_airplane() mesh.plot(off_screen=OFF_SCREEN, eye_dome_lighting=True) p = pyvista.Plotter(off_screen=OFF_SCREEN) p.add_mesh(mesh) p.enable_eye_dome_lighting() p.show()
def test_multi_block_init_list(): data = [ex.load_rectilinear(), ex.load_airplane()] multi = pyvista.MultiBlock(data) assert isinstance(multi, pyvista.MultiBlock) assert multi.n_blocks == 2 assert isinstance(multi.GetBlock(0), pyvista.RectilinearGrid) assert isinstance(multi.GetBlock(1), pyvista.PolyData)
def test_plot_depth_peeling(): mesh = examples.load_airplane() p = pyvista.Plotter() p.add_mesh(mesh) p.enable_depth_peeling() p.disable_depth_peeling() p.show(before_close_callback=verify_cache_image)
def test_multi_slice_index(): multi = pyvista.MultiBlock() # Add examples multi.append(ex.load_ant()) multi.append(ex.load_sphere()) multi.append(ex.load_uniform()) multi.append(ex.load_airplane()) multi.append(ex.load_globe()) # Now check everything sub = multi[0:3] assert len(sub) == 3 for i in range(3): assert id(sub[i]) == id(multi[i]) assert sub.get_block_name(i) == multi.get_block_name(i) sub = multi[0:-1] assert len(sub) == len(multi) == multi.n_blocks for i in range(multi.n_blocks): assert id(sub[i]) == id(multi[i]) assert sub.get_block_name(i) == multi.get_block_name(i) sub = multi[0:-1:2] assert len(sub) == 3 for i in range(3): j = i * 2 assert id(sub[i]) == id(multi[j]) assert sub.get_block_name(i) == multi.get_block_name(j)
def test_rotations_should_match_by_a_360_degree_difference(): mesh = examples.load_airplane() point = np.random.random(3) - 0.5 angle = (np.random.random() - 0.5) * 360.0 vector = np.random.random(3) - 0.5 # Rotate about x axis. rot1 = mesh.copy() rot2 = mesh.copy() rot1.rotate_x(angle=angle, point=point) rot2.rotate_x(angle=angle - 360.0, point=point) assert np.allclose(rot1.points, rot2.points) # Rotate about y axis. rot1 = mesh.copy() rot2 = mesh.copy() rot1.rotate_y(angle=angle, point=point) rot2.rotate_y(angle=angle - 360.0, point=point) assert np.allclose(rot1.points, rot2.points) # Rotate about z axis. rot1 = mesh.copy() rot2 = mesh.copy() rot1.rotate_z(angle=angle, point=point) rot2.rotate_z(angle=angle - 360.0, point=point) assert np.allclose(rot1.points, rot2.points) # Rotate about custom vector. rot1 = mesh.copy() rot2 = mesh.copy() rot1.rotate_vector(vector=vector, angle=angle, point=point) rot2.rotate_vector(vector=vector, angle=angle - 360.0, point=point) assert np.allclose(rot1.points, rot2.points)
def test_flip_z(): mesh = examples.load_airplane() flip_z1 = mesh.copy() flip_z2 = mesh.copy() flip_z1.flip_z(point=(0, 0, 0)) flip_z2.points[:, 2] *= -1.0 assert np.allclose(flip_z1.points, flip_z2.points)
def test_plot_depth_peeling(): mesh = examples.load_airplane() p = pyvista.Plotter(off_screen=OFF_SCREEN) p.add_mesh(mesh) p.enable_depth_peeling() p.disable_depth_peeling() p.show()
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 test_multi_block_keys(): data = dict() data['grid'] = ex.load_rectilinear() data['poly'] = ex.load_airplane() multi = pyvista.MultiBlock(data) assert len(multi.keys()) == 2 assert 'grid' in multi.keys() assert 'poly' in multi.keys()
def datasets(): return [ examples.load_uniform(), # UniformGrid examples.load_rectilinear(), # RectilinearGrid examples.load_hexbeam(), # UnstructuredGrid examples.load_airplane(), # PolyData examples.load_structured(), # StructuredGrid ]
def test_multi_block_length(): multi = pyvista.MultiBlock() # Add examples multi.append(ex.load_ant()) multi.append(ex.load_sphere()) multi.append(ex.load_uniform()) multi.append(ex.load_airplane()) multi.append(None) assert multi.length
def test_scale(): mesh = examples.load_airplane() xyz = np.random.random(3) scale1 = mesh.copy() scale2 = mesh.copy() scale1.scale(xyz) scale2.points *= xyz assert np.allclose(scale1.points, scale2.points)
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
def test_contour_errors(uniform): with pytest.raises(TypeError): uniform.contour(scalars='Spatial Cell Data') with pytest.raises(TypeError): uniform.contour(isosurfaces=pyvista.PolyData()) uniform = examples.load_airplane() with pytest.raises(ValueError): uniform.contour() with pytest.raises(ValueError): uniform.contour(method='invalid method')
def test_multi_block_plot(): multi = pyvista.MultiBlock() multi.append(examples.load_rectilinear()) uni = examples.load_uniform() arr = np.random.rand(uni.n_cells) uni._add_cell_array(arr, 'Random Data') multi.append(uni) # And now add a data set without the desired array and a NULL component multi[3] = examples.load_airplane() multi.plot(scalars='Random Data', off_screen=OFF_SCREEN, multi_colors=True)
def test_export_color(tmpdir): filename = str(tmpdir.mkdir("tmpdir").join('scene-color')) data = ex.load_airplane() # Create the scene plotter = pyvista.Plotter(off_screen=OFF_SCREEN) plotter.add_mesh(data, color='yellow') plotter.export_vtkjs(filename) cpos_out = plotter.show() # Export must be called before showing! plotter.close() # Now make sure the file is there assert os.path.isfile(f'{filename}.vtkjs')
def test_multi_block_repr(): multi = pyvista.MultiBlock() # Add examples multi.append(ex.load_ant()) multi.append(ex.load_sphere()) multi.append(ex.load_uniform()) multi.append(ex.load_airplane()) multi.append(None) # Now check everything assert multi.n_blocks == 5 assert multi._repr_html_() is not None
def test_multi_block_init_dict(): data = dict() data['grid'] = ex.load_rectilinear() data['poly'] = ex.load_airplane() multi = pyvista.MultiBlock(data) assert isinstance(multi, pyvista.MultiBlock) assert multi.n_blocks == 2 # Note that disctionaries do not maintain order assert isinstance(multi[0], (pyvista.RectilinearGrid, pyvista.PolyData)) assert multi.get_block_name(0) in ['grid', 'poly'] assert isinstance(multi[1], (pyvista.RectilinearGrid, pyvista.PolyData)) assert multi.get_block_name(1) in ['grid', 'poly']
def test_clip_box(): for i, dataset in enumerate(datasets): clp = dataset.clip_box(invert=True) assert clp is not None assert isinstance(clp, pyvista.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_multi_block_plot(): multi = pyvista.MultiBlock() multi.append(examples.load_rectilinear()) uni = examples.load_uniform() arr = np.random.rand(uni.n_cells) uni.cell_arrays.append(arr, 'Random Data') multi.append(uni) # And now add a data set without the desired array and a NULL component multi[3] = examples.load_airplane() with pytest.raises(ValueError): # The scalars are not available in all datasets so raises ValueError multi.plot(scalars='Random Data', multi_colors=True) multi.plot(multi_colors=True, before_close_callback=verify_cache_image)
def test_combine_filter(): multi = pyvista.MultiBlock() # Add examples multi.append(ex.load_ant()) multi.append(ex.load_sphere()) multi.append(ex.load_uniform()) multi.append(ex.load_airplane()) multi.append(ex.load_globe()) # Now check everything assert multi.n_blocks == 5 # Now apply the geometry filter to combine a plethora of data blocks geom = multi.combine() assert isinstance(geom, pyvista.UnstructuredGrid)
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=pyvista.PolyData()) dataset = examples.load_airplane() with pytest.raises(AssertionError): result = dataset.contour()
def test_multi_block_copy(): multi = pyvista.MultiBlock() # Add examples multi.append(ex.load_ant()) multi.append(ex.load_sphere()) multi.append(ex.load_uniform()) multi.append(ex.load_airplane()) multi.append(ex.load_globe()) # Now check everything newobj = multi.copy() assert multi.n_blocks == 5 == newobj.n_blocks assert id(multi[0]) != id(newobj[0]) assert id(multi[-1]) != id(newobj[-1]) return
def test_extract_geometry(): multi = pyvista.MultiBlock() # Add examples multi.append(ex.load_ant()) multi.append(ex.load_sphere()) multi.append(ex.load_uniform()) nested = pyvista.MultiBlock() nested.append(ex.load_airplane()) nested.append(ex.load_globe()) multi.append(nested) # Now check everything assert multi.n_blocks == 4 # Now apply the geometry filter to combine a plethora of data blocks geom = multi.extract_geometry() assert isinstance(geom, pyvista.PolyData)
def test_multi_block_list_index(): multi = pyvista.MultiBlock() # Add examples multi.append(ex.load_ant()) multi.append(ex.load_sphere()) multi.append(ex.load_uniform()) multi.append(ex.load_airplane()) multi.append(ex.load_globe()) # Now check everything indices = [0, 3, 4] sub = multi[indices] assert len(sub) == len(indices) for i, j in enumerate(indices): assert id(sub[i]) == id(multi[j]) assert sub.get_block_name(i) == multi.get_block_name(j)
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_multi_block_negative_index(): multi = pyvista.MultiBlock() # Add examples multi.append(ex.load_ant()) multi.append(ex.load_sphere()) multi.append(ex.load_uniform()) multi.append(ex.load_airplane()) multi.append(ex.load_globe()) # Now check everything assert id(multi[-1]) == id(multi[4]) assert id(multi[-2]) == id(multi[3]) assert id(multi[-3]) == id(multi[2]) assert id(multi[-4]) == id(multi[1]) assert id(multi[-5]) == id(multi[0]) with pytest.raises(IndexError): foo = multi[-6] return
def test_multi_block_io(extension, binary, tmpdir): filename = str(tmpdir.mkdir("tmpdir").join('tmp.%s' % extension)) multi = pyvista.MultiBlock() # Add examples multi.append(ex.load_ant()) multi.append(ex.load_sphere()) multi.append(ex.load_uniform()) multi.append(ex.load_airplane()) multi.append(ex.load_globe()) # Now check everything assert multi.n_blocks == 5 # Save it out multi.save(filename, binary) foo = pyvista.MultiBlock(filename) assert foo.n_blocks == multi.n_blocks foo = pyvista.read(filename) assert foo.n_blocks == multi.n_blocks