def read(filename): """This will read any VTK file! It will figure out what reader to use then wrap the VTK object for use in ``vtki`` """ def legacy(filename): reader = vtk.vtkDataSetReader() reader.SetFileName(filename) reader.Update() return reader.GetOutputDataObject(0) ext = os.path.splitext(filename)[1] if ext.lower() in '.vtk': # Use a legacy reader and wrap the result return wrap(legacy(filename)) else: # From the extension, decide which reader to use if ext.lower() in '.vti': # ImageData return vtki.UniformGrid(filename) elif ext.lower() in '.vtr': # RectilinearGrid return vtki.RectilinearGrid(filename) elif ext.lower() in '.vtu': # UnstructuredGrid return vtki.UnstructuredGrid(filename) elif ext.lower() in '.ply': # PolyData return vtki.PolyData(filename) elif ext.lower() in '.vts': # UnstructuredGrid return vtki.StructuredGrid(filename) else: # Attempt to use the legacy reader... try: return wrap(legacy(filename)) except: pass raise IOError("This file was not able to be automatically read by vtki.")
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_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_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_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 numpy_to_texture(image): """Convert a NumPy image array to a vtk.vtkTexture""" if not isinstance(image, np.ndarray): raise TypeError('Unknown input type ({})'.format(type(image))) if image.ndim != 3 or image.shape[2] != 3: raise AssertionError('Input image must be nn by nm by RGB') grid = vtki.UniformGrid((image.shape[1], image.shape[0], 1)) grid.point_arrays['Image'] = np.flip(image.swapaxes(0, 1), axis=1).reshape( (-1, 3), order='F') grid.set_active_scalar('Image') return image_to_texture(grid)
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 read(filename, attrs=None): """This will read any VTK file! It will figure out what reader to use then wrap the VTK object for use in ``vtki``. Parameters ---------- attrs : dict, optional A dictionary of attributes to call on the reader. Keys of dictionary are the attribute/method names and values are the arguments passed to those calls. If you do not have any attributes to call, pass ``None`` as the value. """ filename = os.path.abspath(os.path.expanduser(filename)) ext = get_ext(filename) # From the extension, decide which reader to use if attrs is not None: reader = get_reader(filename) return standard_reader_routine(reader, filename, attrs=attrs) elif ext in '.vti': # ImageData return vtki.UniformGrid(filename) elif ext in '.vtr': # RectilinearGrid return vtki.RectilinearGrid(filename) elif ext in '.vtu': # UnstructuredGrid return vtki.UnstructuredGrid(filename) elif ext in ['.ply', '.obj', '.stl']: # PolyData return vtki.PolyData(filename) elif ext in '.vts': # StructuredGrid return vtki.StructuredGrid(filename) elif ext in ['.vtm', '.vtmb']: return vtki.MultiBlock(filename) elif ext in ['.e', '.exo']: return read_exodus(filename) elif ext in ['.vtk']: # Attempt to use the legacy reader... return read_legacy(filename) else: # Attempt find a reader in the readers mapping try: reader = get_reader(filename) return standard_reader_routine(reader, filename) except KeyError: pass raise IOError("This file was not able to be automatically read by vtki.")
def test_grid_points(): """Test the points mehtods on UniformGrid and inearGrid""" 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))
# Take a 3D NumPy array of data values that holds some spatial data where each # axis corresponds to the XYZ cartesian axes. This example will create a # :class:`vtki.UniformGrid` object that will hold the spatial reference for a # 3D grid which a 3D NumPy array of values can be plotted against. import vtki import numpy as np ################################################################################ # Create the 3D NumPy array of spatially referenced data # This is spatially referenced such that the grid is 20 by 5 by 10 (nx by ny by nz) values = np.linspace(0, 10, 1000).reshape((20, 5, 10)) values.shape # Create the spatial reference grid = vtki.UniformGrid() # Set the grid dimensions: shape + 1 because we want to inject our values on the CELL data grid.dimensions = np.array(values.shape) + 1 # Edit the spatial reference grid.origin = (100, 33, 55.6) # The bottom left corner of the data set grid.spacing = (1, 5, 2) # These are the cell sizes along each axis # Add the data values to the cell data grid.cell_arrays['values'] = values.flatten(order='F') # Flatten the array! # Now plot the grid! grid.plot(show_edges=True) ################################################################################
def load_uniform(): """ Loads a sample uniform grid """ return vtki.UniformGrid(uniformfile)