def main(): import os,sys import glob import json import numpy as np import matplotlib matplotlib.use('Agg') import os import json import numpy as np import nibabel as nib os.environ['ETS_TOOLKIT'] = 'qt4' os.environ['QT_API'] = 'pyqt5' import pyvista from pyvista.utilities import xvfb pyvista.OFF_SCREEN=True xvfb.start_xvfb() from mne.viz import set_3d_backend set_3d_backend('pyvista') # grab config with open('config.json','r') as config_f: config = json.load(config_f) # set up paths and variables freesurfer='./output' cortexmap='./cortexmap' colormap = config['colormap'] min_percentile = config['min_percentile'] max_percentile = config['max_percentile'] threshold = np.float(config['threshold']) measureFiles = np.unique([ f.split('h.')[1] for f in os.listdir('./'+cortexmap+'/func') if f.split('h.')[1] != 'goodvertex.func.gii' ]) surface = config['surface'] hemi=["lh","rh"] #### set up other inputs #### # set outdir outdir = 'images' # generate output directory if not already there if os.path.isdir(outdir): print("directory exits") else: print("making output directory") os.mkdir(outdir) for h in hemi: for i in measureFiles: print('generating image for %s.%s' %(h,i)) if surface == 'midthickness': generateMeasureOverlayImage(freesurfer,h,'midthickness.very_inflated',i,colormap,min_percentile,max_percentile,threshold,outdir,'cortexmap') else: generateMeasureOverlayImage(freesurfer,h,surface,i,colormap,min_percentile,max_percentile,threshold,outdir,'cortexmap')
def test_viz(): Lx = 1.0 Ly = 0.1 _nel = 30 gmsh_model, tdim = mesh_bar_gmshapi("bar", Lx, Ly, 1/_nel, 2) mesh, mts = gmsh_model_to_mesh( gmsh_model, cell_data=False, facet_data=True, gdim=2) element_u = ufl.VectorElement("Lagrange", mesh.ufl_cell(), degree=1, dim=2) V_u = dolfinx.fem.FunctionSpace(mesh, element_u) element_alpha = ufl.FiniteElement("Lagrange", mesh.ufl_cell(), degree=1) V_alpha = dolfinx.fem.FunctionSpace(mesh, element_alpha) def scal_2D(x): return np.sin(np.pi * x[0] * 3.0) def vect_2D(x): vals = np.zeros((2, x.shape[1])) vals[0] = np.sin(x[1]) vals[1] = 0.1 * x[0] return vals # Define the state u = Function(V_u, name="Displacement") u.interpolate(vect_2D) alpha = Function(V_alpha, name="Damage") alpha.interpolate(scal_2D) xvfb.start_xvfb(wait=0.05) pyvista.OFF_SCREEN = True plotter = pyvista.Plotter( title="Test Viz", window_size=[1600, 600], shape=(1, 2), ) _plt = plot_scalar(alpha, plotter, subplot=(0, 0)) logging.critical('plotted scalar') _plt = plot_vector(u, plotter, subplot=(0, 1)) logging.critical('plotted vector') _plt.screenshot(f"./output/test_viz/test_viz_MPI{comm.size}-.png") if not pyvista.OFF_SCREEN: plotter.show()
from pathlib import Path import dolfinx.plot gmsh_model, tdim = mesh_bar_gmshapi("bar", 1, 0.1, 0.01, 2, msh_file="output/bar.msh") mesh, mts = gmsh_to_dolfin(gmsh_model, tdim, prune_z=True) Path("output").mkdir(parents=True, exist_ok=True) with XDMFFile(MPI.COMM_WORLD, "output/bar.xdmf", "w") as ofile: ofile.write_mesh_meshtags(mesh, mts) import pyvista from pyvista.utilities import xvfb xvfb.start_xvfb(wait=0.05) pyvista.OFF_SCREEN = True plotter = pyvista.Plotter(title="Bar mesh") topology, cell_types = dolfinx.plot.create_vtk_topology( mesh, mesh.topology.dim) grid = pyvista.UnstructuredGrid(topology, cell_types, mesh.geometry.x) # plotter.subplot(0, 0) actor_1 = plotter.add_mesh(grid, show_edges=True) plotter.view_xy() if not pyvista.OFF_SCREEN: plotter.show() figure = plotter.screenshot("output/bar.png")
import dolfinx.plot import numpy as np import ufl from mpi4py import MPI try: import pyvista except ModuleNotFoundError: print("pyvista is required for this demo") exit(0) # If environment variable PYVISTA_OFF_SCREEN is set to true save a png # otherwise create interactive plot if pyvista.OFF_SCREEN: from pyvista.utilities.xvfb import start_xvfb start_xvfb(wait=0.1) # Set some global options for all plots transparent = False figsize = 800 pyvista.rcParams["background"] = [0.5, 0.5, 0.5] # Plotting a 3D dolfinx.Function with pyvista # =========================================== # Interpolate a simple scalar function in 3D def int_u(x): return x[0] + 3 * x[1] + 5 * x[2]
# Save solution in XDMF format with XDMFFile(MPI.COMM_WORLD, "poisson.xdmf", "w") as file: file.write_mesh(mesh) file.write_function(uh) # Update ghost entries and plot uh.vector.ghostUpdate(addv=PETSc.InsertMode.INSERT, mode=PETSc.ScatterMode.FORWARD) try: import pyvista topology, cell_types = plot.create_vtk_topology(mesh, mesh.topology.dim) grid = pyvista.UnstructuredGrid(topology, cell_types, mesh.geometry.x) grid.point_arrays["u"] = uh.compute_point_values().real grid.set_active_scalars("u") plotter = pyvista.Plotter() plotter.add_mesh(grid, show_edges=True) warped = grid.warp_by_scalar() plotter.add_mesh(warped) # If pyvista environment variable is set to off-screen (static) plotting save png if pyvista.OFF_SCREEN: from pyvista.utilities.xvfb import start_xvfb start_xvfb(wait=0) plotter.screenshot("uh.png") else: plotter.show() except ModuleNotFoundError: print("pyvista is required to visualise the solution")