Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_table_init(tmpdir):
"""Save some delimited text to a file and read it"""
filename = str(tmpdir.mkdir("tmpdir").join('tmp.%s' % 'csv'))
nr, nc = 50, 3
arrays = np.random.rand(nr, nc)
# Create from 2D array
table = pyvista.Table(arrays)
assert table.n_rows == nr
assert table.n_columns == nc
assert table.n_arrays == nc
assert len(table.row_arrays) == nc
for i in range(nc):
assert np.allclose(arrays[:,i], table[i])
# create from dictionary
array_dict = {}
for i in range(nc):
array_dict['foo{}'.format(i)] = arrays[:, i].copy()
table = pyvista.Table(array_dict)
assert table.n_rows == nr
assert table.n_columns == nc
def test_table_repr():
nr, nc = 50, 3
arrays = np.random.rand(nr, nc)
table = pyvista.Table(arrays)
text = table._repr_html_()
assert isinstance(text, str)
text = table.__repr__()
assert isinstance(text, str)
text = table.__str__()
assert isinstance(text, str)
# Create from 2D array
table = pyvista.Table(arrays)
assert table.n_rows == nr
assert table.n_columns == nc
assert table.n_arrays == nc
assert len(table.row_arrays) == nc
for i in range(nc):
assert np.allclose(arrays[:,i], table[i])
# create from dictionary
array_dict = {}
for i in range(nc):
array_dict['foo{}'.format(i)] = arrays[:, i].copy()
table = pyvista.Table(array_dict)
assert table.n_rows == nr
assert table.n_columns == nc
assert len(table.row_arrays) == nc
for i in range(nc):
assert np.allclose(arrays[:,i], table['foo{}'.format(i)])
dataset = examples.load_hexbeam()
array_dict = dataset.point_arrays
table = pyvista.Table(array_dict)
assert table.n_rows == dataset.n_points
assert table.n_columns == len(array_dict)
assert len(table.row_arrays) == len(array_dict)
for name in table.keys():
assert np.allclose(dataset[name], table[name])
def test_table_row_uint8():
n = 50
table = pyvista.Table()
arr = np.zeros(n, np.uint8)
table.row_arrays['arr'] = arr
arr[:] = np.arange(n)
assert np.allclose(table.row_arrays['arr'], np.arange(n))
def test_table_row_np_bool():
n = 50
table = pyvista.Table()
bool_arr = np.zeros(n, np.bool)
table.row_arrays['bool_arr'] = bool_arr
bool_arr[:] = True
assert table.row_arrays['bool_arr'].all()
assert table._row_array('bool_arr').all()
assert table._row_array('bool_arr').dtype == np.bool
def test_fail_plot_table():
"""Make sure tables cannot be plotted"""
table = pyvista.Table(np.random.rand(50, 3))
with pytest.raises(TypeError):
pyvista.plot(table)
with pytest.raises(TypeError):
plotter = pyvista.Plotter(off_screen=OFF_SCREEN)
plotter.add_mesh(table)
This example will demonstrate how to reshape an input table as though it were a 2D array.
This filter will take a `vtkTable` object and reshape it. This filter essentially treats `vtkTable`s as 2D matrices and reshapes them using `numpy.reshape` in a C contiguous manner. Unfortunately, data fields will be renamed arbitrarily because VTK data arrays require a name.
This example demos :class:`PVGeo.filters.ReshapeTable`
"""
import numpy as np
import pyvista as pv
import PVGeo
from PVGeo.filters import ReshapeTable
###############################################################################
# Create some input table
t0 = pv.Table()
# Populate the tables
arrs = [None, None, None]
n = 400
ncols = 2
nrows = int(n * len(arrs) / ncols)
titles = ('Array 0', 'Array 1', 'Array 2')
arrs[0] = np.random.random(n)
arrs[1] = np.random.random(n)
arrs[2] = np.random.random(n)
t0[titles[0]] = arrs[0]
t0[titles[1]] = arrs[1]
t0[titles[2]] = arrs[2]
###############################################################################
# Use the filter to reshape the table
def table_to_data_frame(table):
"""Converts a vtkTable to a pandas DataFrame"""
if not isinstance(table, vtk.vtkTable):
raise _helpers.PVGeoError('Input is not a vtkTable')
if not isinstance(table, pv.Table):
table = pv.Table(table)
return table.to_pandas()
"""Wrap any given VTK data object to its appropriate PyVista data object.
Other formats that are supported include:
* 2D :class:`numpy.ndarray` of XYZ vertices
* 3D :class:`numpy.ndarray` representing a volume. Values will be scalars.
"""
wrappers = {
'vtkUnstructuredGrid': pyvista.UnstructuredGrid,
'vtkRectilinearGrid': pyvista.RectilinearGrid,
'vtkStructuredGrid': pyvista.StructuredGrid,
'vtkPolyData': pyvista.PolyData,
'vtkImageData': pyvista.UniformGrid,
'vtkStructuredPoints': pyvista.UniformGrid,
'vtkMultiBlockDataSet': pyvista.MultiBlock,
'vtkTable': pyvista.Table,
# 'vtkParametricSpline': pyvista.Spline,
}
# Otherwise, we assume a VTK data object was passed
if hasattr(vtkdataset, 'GetClassName'):
key = vtkdataset.GetClassName()
elif vtkdataset is None:
return None
elif isinstance(vtkdataset, np.ndarray):
if vtkdataset.ndim == 1 and vtkdataset.shape[0] == 3:
return pyvista.PolyData(vtkdataset)
if vtkdataset.ndim > 1 and vtkdataset.ndim < 3 and vtkdataset.shape[1] == 3:
return pyvista.PolyData(vtkdataset)
elif vtkdataset.ndim == 3:
mesh = pyvista.UniformGrid(vtkdataset.shape)
mesh['values'] = vtkdataset.ravel(order='F')
mesh.active_scalars_name = 'values'
This example will demonstrate how to to merge to `vtkTable` objects with the
same number of rows into a single `vtkTable`.
This example demos :class:`PVGeo.filters.CombineTables`
Please note that this example only works on version of PyVista>=0.22.0
"""
import numpy as np
import pyvista as pv
import PVGeo
from PVGeo.filters import CombineTables
###############################################################################
# Create some input tables
t0 = pv.Table()
t1 = pv.Table()
# Populate the tables
n = 100
titles = ('Array 0', 'Array 1', 'Array 2')
arr0 = np.random.random(n) # Table 0
arr1 = np.random.random(n) # Table 0
t0[titles[0]] = arr0
t0[titles[1]] = arr1
arr2 = np.random.random(n) # Table 1
t1[titles[2]] = arr2
arrs = [arr0, arr1, arr2]
###############################################################################
print(t0)
###############################################################################