VTKVolume#

Download this notebook from GitHub (right-click to download).


import panel as pn
pn.extension('vtk')

The VTKVolume pane renders 3d volumetric data defined on regular grids. It may be constructed from a 3D NumPy array or a vtkVolume. The pane provides a number of interactive control which can be set either through callbacks from Python or Javascript callbacks.

Parameters:#

For layout and styling related parameters see the customization user guide.

  • max_data_size (Number): Maximum data size (in MB) of the data array allowed to be passed through the websockets without subsampling. If the data exceeded this size data are downsampled using scipy if installed or by taking 1 sample on N (N choosen to have an array size smaller than max_data_size) in each dimension

  • object (ndarray or object): Can be a 3d numpy array or an instance ofvtkImageData class

  • origin (3-tuple): Origin of the volume in the scene. By default value is (0,0,0)

  • spacing (3-tuple): Define the distance between 2 adjacent voxels in the 3 dimensions. By default the value is (1,1,1)

  • render_background (hexadecimal color string): Define the background color of the 3D rendering. By default value is ‘#52576e’

  • camera (dict): A dictionary reflecting the current state of the VTK camera

  • controller_expanded (bool): A boolean to expand/collapse the volume controller panel in the view. If True the controller is expanded else it is collapsed

  • orientation_widget (bool): A boolean to activate/deactivate the orientation widget in the 3D pane. This widget is clickable and allows to rotate the scene in one of the orthographic projections.

  • colormap (string): Name of the colormap used to transform pixel value in color. All allowed colormaps are referenced in PRESET_CMAPS in panel.pane.vtk.enums. By default the value is ‘erdc_rainbow_bright’

  • rescale (boolean): If set to True the colormap is rescale beween min and max value of the non transparent pixels else the full range of the data are used. By default the value is True

  • display_volume (boolean): If set to True, the 3D respresentation of the volume is displayed using ray casting. By default the value is True

  • display_slices (boolean): If set to true, the orthgonal slices in the three (X, Y, Z) directions are displayed. Postition of each slice can be controlled using slice_(i,j,k) parameters. By default the value is False

  • mapper (dict): Parameter to store on python side information about the color mapper set through the javascript widget in the 3d view. The format is {low, high, palette}.

Volume Rendering options

  • sampling (Number): Parameter to adjust the distance between samples used for rendering. The lower the value is the more precise is the representation but it is more computationnaly intensive. By default the value is 0.4. The value bust be in (0, 1)

  • edge_gradient (Number): Parameter to adjust the opacity of the volume based on the gradient between voxels. By default the value is 0.4. The value bust be in (0, 1)

  • interpolation (String): Interpolation type for sampling a volume. nearest interpolation will snap to the closest voxel, linear will perform trilinear interpolation to compute a scalar value from surrounding voxels and fast_linear under WebGL 1 will perform bilinear interpolation on X and Y but use nearest for Z. This is slightly faster than full linear at the cost of no Z axis linear interpolation. By default the value is ‘fast_linear’. The options are among ‘fast_linear’, ‘linear’, ‘nearest’

Lighting options (affect only the volume rendering, not the slices)

  • shadow (boolean): If set to false then the mapper for the volume will not perform shading computations, it is the same as setting ambient=1, diffuse=0, specular=0. By default the value is True

  • ambient (Number): Value to control the ambient lighting. It is the light an object gives even in the absence of strong light. It is constant in all directions. By default the value is 0.2

  • diffuse (Number): Value to control the diffuse Lighting. It relies on both the light direction and the object surface normal. By default the value is 0.7

  • specular (Number): Value to control specular lighting. It is the light reflects back toward the camera when hitting the object. By default the value is 0.3

  • specular_power (Number): Specular power refers to how much light is reflected in a mirror like fashion, rather than scattered randomly in a diffuse manner. By default the value is 8.0

Slices options

  • slice_i (Integer): parameter to control the position of the slice normal to the X direction. By default the the value is computed to be in the middle of the data

  • slice_j (Integer): parameter to control the position of the slice normal to the Y direction. By default the the value is computed to be in the middle of the data

  • slice_k (Integer): parameter to control the position of the slice normal to the Z direction. By default the the value is computed to be in the middle of the data

  • nan_opacity (Number): parameter to control the opacity of NaN values in a slice. By default the the value is 1. The value must be in (0, 1)


The simplest way to create a VTKVolume pane is to use a 3d numpy array. The spacing is used to produced a rectangular parallelepiped instead of a cube.

import numpy as np

data_matrix = np.zeros([75, 75, 75], dtype=np.uint8)
data_matrix[0:35, 0:35, 0:35] = 50
data_matrix[25:55, 25:55, 25:55] = 100
data_matrix[45:74, 45:74, 45:74] = 150

pn.pane.VTKVolume(data_matrix, sizing_mode='stretch_width', height=400, spacing=(3,2,1), interpolation='nearest', edge_gradient=0, sampling=0)

Alternatively the pane maybe constructed from a vtkImageData object. This type of object can be construct directly with vtk or pyvista module:

import pyvista as pv
from pyvista import examples

# Download a volumetric dataset
vol = examples.download_head()
volume = pn.panel(vol, sizing_mode='stretch_both', height=400, display_slices=True)
volume
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
Cell In [3], line 1
----> 1 import pyvista as pv
      2 from pyvista import examples
      4 # Download a volumetric dataset

File /usr/share/miniconda3/envs/test-environment/lib/python3.9/site-packages/pyvista/__init__.py:12
     10 # Load default theme.  Must be loaded first.
     11 from pyvista._version import __version__
---> 12 from pyvista.plotting import *
     13 from pyvista.utilities import *
     14 from pyvista.core import *

File /usr/share/miniconda3/envs/test-environment/lib/python3.9/site-packages/pyvista/plotting/__init__.py:7
      4 from .colors import (color_char_to_word, get_cmap_safe, hex_to_rgb, hexcolors,
      5                      string_to_rgb, PARAVIEW_BACKGROUND)
      6 from .export_vtkjs import export_plotter_vtkjs, get_vtkjs_url
----> 7 from .helpers import plot, plot_arrows, plot_compare_four, plot_itk
      8 from .plotting import BasePlotter, Plotter, close_all
      9 from .renderer import CameraPosition, Renderer, scale_point

File /usr/share/miniconda3/envs/test-environment/lib/python3.9/site-packages/pyvista/plotting/helpers.py:7
      4 import scooby
      6 import pyvista
----> 7 from pyvista.utilities import is_pyvista_dataset
      8 from .plotting import Plotter
     11 def plot(var_item, off_screen=None, full_screen=None, screenshot=None,
     12          interactive=True, cpos=None, window_size=None,
     13          show_bounds=False, show_axes=None, notebook=None, background=None,
   (...)
     17          theme=None, hidden_line_removal=None, anti_aliasing=None,
     18          zoom=None, **kwargs):

File /usr/share/miniconda3/envs/test-environment/lib/python3.9/site-packages/pyvista/utilities/__init__.py:2
      1 """Utilities routines."""
----> 2 from .errors import (GPUInfo, Observer, Report,
      3                      assert_empty_kwargs, get_gpu_info, send_errors_to_logging,
      4                      set_error_output_file, check_valid_vector, VtkErrorCatcher)
      5 from .features import *
      6 from .fileio import *

File /usr/share/miniconda3/envs/test-environment/lib/python3.9/site-packages/pyvista/utilities/errors.py:12
      9 import scooby
     11 import pyvista
---> 12 from pyvista import _vtk
     14 import contextlib
     15 import collections

File /usr/share/miniconda3/envs/test-environment/lib/python3.9/site-packages/pyvista/_vtk.py:42
     38 from vtkmodules.vtkRenderingLabel import (vtkPointSetToLabelHierarchy,
     39                                           vtkLabelPlacementMapper)
     40 from vtkmodules.vtkRenderingVolume import (vtkFixedPointVolumeRayCastMapper,
     41                                            vtkGPUVolumeRayCastMapper)
---> 42 from vtkmodules.vtkRenderingVolumeOpenGL2 import (vtkOpenGLGPUVolumeRayCastMapper,
     43                                                   vtkSmartVolumeMapper)
     44 from vtkmodules.vtkRenderingOpenGL2 import (vtkOpenGLHardwareSelector,
     45                                             vtkRenderStepsPass,
     46                                             vtkEDLShading,
   (...)
     51                                             vtkRenderPassCollection,
     52                                             vtkOpenGLTexture)
     53 from vtkmodules.vtkIOInfovis import vtkDelimitedTextReader

ImportError: libEGL.so.1: cannot open shared object file: No such file or directory

Controls#

The VTKVolume pane exposes a number of options which can be changed from both Python and Javascript try out the effect of these parameters interactively:

pn.Row(volume.controls(jslink=True), volume)
This web page was generated from a Jupyter notebook and not all interactivity will work on this website. Right click to download and run locally for full Python-backed interactivity.

Download this notebook from GitHub (right-click to download).