How to use the pyvista.MultiBlock function in pyvista

To help you get started, we’ve selected a few pyvista examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github pyvista / pyvista / tests / test_composite.py View on Github external
def test_multi_block_init_vtk():
    multi = vtk.vtkMultiBlockDataSet()
    multi.SetBlock(0, vtk.vtkRectilinearGrid())
    multi.SetBlock(1, vtk.vtkStructuredGrid())
    multi = pyvista.MultiBlock(multi)
    assert isinstance(multi, pyvista.MultiBlock)
    assert multi.n_blocks == 2
    assert isinstance(multi.GetBlock(0), pyvista.RectilinearGrid)
    assert isinstance(multi.GetBlock(1), pyvista.StructuredGrid)
    multi = vtk.vtkMultiBlockDataSet()
    multi.SetBlock(0, vtk.vtkRectilinearGrid())
    multi.SetBlock(1, vtk.vtkStructuredGrid())
    multi = pyvista.MultiBlock(multi, deep=True)
    assert isinstance(multi, pyvista.MultiBlock)
    assert multi.n_blocks == 2
    assert isinstance(multi.GetBlock(0), pyvista.RectilinearGrid)
    assert isinstance(multi.GetBlock(1), pyvista.StructuredGrid)
    # Test nested structure
    multi = vtk.vtkMultiBlockDataSet()
    multi.SetBlock(0, vtk.vtkRectilinearGrid())
    multi.SetBlock(1, vtk.vtkImageData())
    nested = vtk.vtkMultiBlockDataSet()
    nested.SetBlock(0, vtk.vtkUnstructuredGrid())
    nested.SetBlock(1, vtk.vtkStructuredGrid())
    multi.SetBlock(2, nested)
    # Wrap the nested structure
    multi = pyvista.MultiBlock(multi)
    assert isinstance(multi, pyvista.MultiBlock)
    assert multi.n_blocks == 3
github pyvista / pyvista / tests / test_composite.py View on Github external
multi.SetBlock(1, vtk.vtkStructuredGrid())
    multi = pyvista.MultiBlock(multi, deep=True)
    assert isinstance(multi, pyvista.MultiBlock)
    assert multi.n_blocks == 2
    assert isinstance(multi.GetBlock(0), pyvista.RectilinearGrid)
    assert isinstance(multi.GetBlock(1), pyvista.StructuredGrid)
    # Test nested structure
    multi = vtk.vtkMultiBlockDataSet()
    multi.SetBlock(0, vtk.vtkRectilinearGrid())
    multi.SetBlock(1, vtk.vtkImageData())
    nested = vtk.vtkMultiBlockDataSet()
    nested.SetBlock(0, vtk.vtkUnstructuredGrid())
    nested.SetBlock(1, vtk.vtkStructuredGrid())
    multi.SetBlock(2, nested)
    # Wrap the nested structure
    multi = pyvista.MultiBlock(multi)
    assert isinstance(multi, pyvista.MultiBlock)
    assert multi.n_blocks == 3
    assert isinstance(multi.GetBlock(0), pyvista.RectilinearGrid)
    assert isinstance(multi.GetBlock(1), pyvista.UniformGrid)
    assert isinstance(multi.GetBlock(2), pyvista.MultiBlock)
github pyvista / pyvista / tests / test_composite.py View on Github external
def test_multi_io_erros(tmpdir):
    fdir = tmpdir.mkdir("tmpdir")
    multi = pyvista.MultiBlock()
    # Check saving with bad extension
    bad_ext_name = str(fdir.join('tmp.%s' % 'npy'))
    with pytest.raises(Exception):
        multi.save(bad_ext_name)
    arr = np.random.rand(10, 10)
    np.save(bad_ext_name, arr)
    # Load non existing file
    with pytest.raises(Exception):
        _ = pyvista.MultiBlock('foo.vtm')
    # Load bad extension
    with pytest.raises(IOError):
        _ = pyvista.MultiBlock(bad_ext_name)
github pyvista / pyvista / tests / test_composite.py View on Github external
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
github pyvista / pyvista / tests / test_plotting.py View on Github external
def test_volume_rendering():
    # Really just making sure no errors are thrown
    vol = examples.load_uniform()
    vol.plot(off_screen=OFF_SCREEN, volume=True, opacity='linear')

    plotter = pyvista.Plotter(off_screen=OFF_SCREEN)
    plotter.add_volume(vol, opacity='sigmoid', cmap='jet', n_colors=15)
    plotter.show()

    # Now test MultiBlock rendering
    data = pyvista.MultiBlock(dict(a=examples.load_uniform(),
                                   b=examples.load_uniform(),
                                   c=examples.load_uniform(),
                                   d=examples.load_uniform(),))
    data['a'].rename_array('Spatial Point Data', 'a')
    data['b'].rename_array('Spatial Point Data', 'b')
    data['c'].rename_array('Spatial Point Data', 'c')
    data['d'].rename_array('Spatial Point Data', 'd')
    data.plot(off_screen=OFF_SCREEN, volume=True, multi_colors=True, )
github pyvista / pyvista / tests / test_composite.py View on Github external
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)
github pyvista / pyvista / pyvista / examples / downloads.py View on Github external
def download_bolt_nut():
    blocks = pyvista.MultiBlock()
    blocks['bolt'] =  _download_and_read('bolt.slc')
    blocks['nut'] = _download_and_read('nut.slc')
    return blocks
github pyvista / pyvista / pyvista / examples / downloads.py View on Github external
def download_tetra_dc_mesh():
    """Download two meshes defining an electrical inverse problem. This contains
    a high resolution forward modeled mesh and a coarse inverse modeled mesh
    """
    local_path, _ = _download_file('dc-inversion.zip')
    filename = os.path.join(local_path, 'mesh-forward.vtu')
    fwd = pyvista.read(filename)
    fwd.set_active_scalar('Resistivity(log10)-fwd')
    filename = os.path.join(local_path, 'mesh-inverse.vtu')
    inv = pyvista.read(filename)
    inv.set_active_scalar('Resistivity(log10)')
    return pyvista.MultiBlock({'forward':fwd, 'inverse':inv})
github pyvista / pyvista / pyvista / core / filters.py View on Github external
def split_bodies(dataset, label=False):
        """Find, label, and split connected bodies/volumes.

        This splits different connected bodies into blocks in a MultiBlock dataset.

        Parameters
        ----------
        label : bool
            A flag on whether to keep the ID arrays given by the
            ``connectivity`` filter.

        """
        # Get the connectivity and label different bodies
        labeled = DataSetFilters.connectivity(dataset)
        classifier = labeled.cell_arrays['RegionId']
        bodies = pyvista.MultiBlock()
        for vid in np.unique(classifier):
            # Now extract it:
            b = labeled.threshold([vid-0.5, vid+0.5], scalars='RegionId')
            if not label:
                # strange behavior:
                # must use this method rather than deleting from the point_arrays
                # or else object is collected.
                b._remove_array(CELL_DATA_FIELD, 'RegionId')
                b._remove_array(POINT_DATA_FIELD, 'RegionId')
            bodies.append(b)

        return bodies
github pyvista / pyvista / pyvista / core / filters.py View on Github external
try:
                ax = axes[axis]
            except KeyError:
                raise RuntimeError('Axis ({}) not understood'.format(axis))
        # get the locations along that axis
        if bounds is None:
            bounds = dataset.bounds
        if center is None:
            center = dataset.center
        if tolerance is None:
            tolerance = (bounds[ax*2+1] - bounds[ax*2]) * 0.01
        rng = np.linspace(bounds[ax*2]+tolerance, bounds[ax*2+1]-tolerance, n)
        center = list(center)
        # Make each of the slices
        output = pyvista.MultiBlock()
        if isinstance(dataset, pyvista.MultiBlock):
            for i in range(dataset.n_blocks):
                output[i] = dataset[i].slice_along_axis(n=n, axis=axis,
                    tolerance=tolerance, generate_triangles=generate_triangles,
                    contour=contour, bounds=bounds, center=center)
            return output
        for i in range(n):
            center[ax] = rng[i]
            slc = DataSetFilters.slice(dataset, normal=axis, origin=center,
                    generate_triangles=generate_triangles, contour=contour)
            output[i, 'slice%.2d'%i] = slc
        return output