Introducing pyvista-xarray for 3D visualization

I’ve recently been working on pyvista-xarray to create a DataArray accessor that builds a direct line of interoperability between xarray and VTK/PyVista for 3D visualization.

This package is still in its early stages and I’d love to solicit as much feedback as possible from the xarray community about the types of data and ways in which you all would like to see this package improve to address 3D visualization needs.

Additionally, this package provides a DataSet IO backend for reading VTK files with xarray’s open_dataset function.

Please open an issue for any feedback you have or any new features you’d like to see!

Examples

Check out the README of the repository for examples, usage, and installation instructions: GitHub - pyvista/pyvista-xarray: PyVista DataArray accessors for xarray

import pvxarray
import rioxarray

da = rioxarray.open_rasterio("Elevation.tif")
da = da.rio.reproject("EPSG:3857")

# Grab the mesh object for use with PyVista
mesh = da.pyvista.mesh(x="x", y="y")

# Warp top and plot in 3D
mesh.warp_by_scalar().plot()

Or open a VTK data file with xarray

import xarray as xr
import pyvista as pv

ds = xr.open_dataset("knee.vti", engine="pyvista")

# Generate PyVista mesh object
mesh = ds["SLCImage"].pyvista.mesh(x="x", y="y", z="z")

# Slice and plot
mesh.slice_orthogonal().plot()

(would add screenshot but I’m too new of a user :frowning_face:)

9 Likes

@banesullivan , very cool! If you run this on a JupyterHub you need to have xvfb running, right?
So a JupyterHub users need to ask their JH admin to install xvfb in the container running JupyterLab, right?

1 Like

@rsignell, that is correct! Usually, this is as simple as having the following startup script:

#!/bin/sh
set -x
sudo apt-get update && sudo apt-get install libgl1-mesa-glx xvfb -y
export DISPLAY=:99.0
export PYVISTA_OFF_SCREEN=True
which Xvfb
Xvfb :99 -screen 0 1024x768x24 > /dev/null 2>&1 &
# give xvfb some time to start
sleep 3
set +x

We also have an OSMesa VTK wheel for PyVista to workaround xvfb/virtual frame buffers: https://github.com/pyvista/pyvista-wheels/raw/main/vtk-osmesa-9.1.0-cp39-cp39-linux_x86_64.whl

There is a Dockerfile in PyVista that demonstrates how to set this up: pyvista/jupyter.Dockerfile at main · pyvista/pyvista · GitHub

We usually have to apt install the following:

apt-get update \
 && apt-get install  -yq --no-install-recommends \
    libfontconfig1 \
    libxrender1 \
    libosmesa6

And then set the following ENV variable for PyVista for using ipyvtklink: https://github.com/Kitware/ipyvtklink

export PYVISTA_USE_IPYVTK=true

We should probably get this configuration into pangeo-docker-images.

2 Likes