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_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)
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_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)
def test_table_pandas(): nr, nc = 50, 3 arrays = np.random.rand(nr, nc) df = pd.DataFrame() for i in range(nc): df['foo{}'.format(i)] = arrays[:, i].copy() table = pyvista.Table(df) assert table.n_rows == nr assert table.n_columns == nc for i in range(nc): assert np.allclose(table.row_arrays['foo{}'.format(i)], arrays[:, i]) assert df.equals(table.to_pandas())
def test_scalars_dict_update(): mesh = examples.load_uniform() n = len(mesh.point_arrays) arrays = { 'foo': np.arange(mesh.n_points), 'rand': np.random.random(mesh.n_points) } mesh.point_arrays.update(arrays) assert 'foo' in mesh.array_names assert 'rand' in mesh.array_names assert len(mesh.point_arrays) == n + 2 # Test update from Table table = pyvista.Table(arrays) mesh = examples.load_uniform() mesh.point_arrays.update(table) assert 'foo' in mesh.array_names assert 'rand' in mesh.array_names assert len(mesh.point_arrays) == n + 2
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] ###############################################################################
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)
def test_table_iter(): nr, nc = 50, 3 arrays = np.random.rand(nr, nc) table = pyvista.Table(arrays) for i, array in enumerate(table): assert np.allclose(array, arrays[:, i])
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]) with pytest.raises(AssertionError): pyvista.Table(np.random.rand(100)) with pytest.raises(AssertionError): pyvista.Table(np.random.rand(100, 2, 3)) # 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]) # Create from vtkTable object h = '\t'.join(['a{}'.format(i) for i in range(nc)]) np.savetxt(filename, arrays, delimiter='\t', header=h, comments='') reader = vtk.vtkDelimitedTextReader() reader.SetFileName(filename) reader.DetectNumericColumnsOn() reader.SetFieldDelimiterCharacters('\t') reader.SetHaveHeaders(True) reader.Update() # Test init table = pyvista.Table(reader.GetOutput(), deep=True) assert isinstance(table, vtk.vtkTable) assert isinstance(table, pyvista.Table) table = pyvista.Table(reader.GetOutput(), deep=False) assert isinstance(table, vtk.vtkTable) assert isinstance(table, pyvista.Table) # Test wrap table = pyvista.wrap(reader.GetOutput()) assert isinstance(table, vtk.vtkTable) assert isinstance(table, pyvista.Table) 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[i]) with pytest.raises(TypeError): pyvista.Table("foo") return