install MayaVi2 under sage (v5.0.1) for 2D/3D interactive visulization

According to Wikipedia, “MayaVi is a scientific data visualizer written in Python, which uses VTK and provides a GUI via Tkinter”, while “Sage (previously SAGE, System for Algebra and Geometry Experimentation[3]) is mathematical software with features covering many aspects of mathematics, including algebra, combinatorics, numerical mathematics, number theory, and calculus”. Under the framework of sage, multiple scientific libraries were combined in order to offer a “open source alternative to Magma, Maple, Mathematica, and MATLAB”. From my point of view, although matplotlib under sage can produce high quality figures, it’s 3D scientific visualization capability can be better improved if MayaVi is included in sage as a package. Unfortunately, till version 5.0.1, sage community does not offer an official spkg package to install MayaVi. Users should either install MayaVi outside of sage using system python or install MayaVi under sage using sage’s own python and other libraries. The former is easy and MayaVi community provides good guidance. The latter can integrates the power of both MayaVi and Sage, but is not straightforward. Information scatters over the world wide web and none provides a systematic treatment. In this blog, I summarize how I installed MayaVi2 (v3.4.0) under sage (v5.0.1). I am aware a newer version of MayaVi (v4.2.0) exists, however, I still don’t know why it doesn’t work under sage v5.0.1. Let me know if you manage to get it work. Now, let’s start to install MayaVi2 under Sage.

0. sage v5.0.1 should be already installed on your system. I use $SAGE_ROOT and $SAGE_LOCAL to denote the path of sage root directory and $SAGE_LOCAL=$SAGE_ROOT/local.
1. make sure cmake is installed in your system. I use cmake 2.8.3 under ubuntu 11.04. Cmake package for sage, such as the one on http://sage.math.washington.edu/home/jsp/SPKGS/MayaviOSMesa/ this page, is not useful in my case. Because cmake under sage can’t find paths for X11 related libraries which are located in both /usr/lib and /usr/local/lib. For this reason, I use cmake outside of sage.
2. download mesa-7.2.spkg, vtk-cvs-20090316osmesa.spkg, configobj-4.5.3.spkg, wxPython-2.8.11.0.spkg to $SAGE_ROOT.
3.

# install a local osmesa, this will work according to Ondrej and Prabhu:
./sage -i mesa-7.2.spkg

4.

# install the vtk-cvs-... (this is vtk-5.5.x) with modifications from Prabhu
# with osmesa enabled, see above.
# We use -f -m to keep the installation files in spkg/build,
# so we have VTKDATA and other stuff available
./sage -f -m vtk-cvs-20090316osmesa.spkg

5. The above installation will fail. No panic. This is the step to get out of sage. The error message is about python.h is not found. Go to directory: $SAGE_ROOTspkgbuildvtk-cvs-20090316osmesa, and edit file spkg-install. Basically just to change “2.5” to “2.7”. You may tell the age of this spkg. Under Linux shell, change directory to $SAGE_ROOTspkgbuildvtk-cvs-20090316osmesasrcVTKvtk-build and paste the following command:

export $SAGE_ROOT=/your/path/to/sage-5.0.1
export $SAGE_LOCAL=/your/path/to/sage-5.0.1/local
cmake -DBUILD_SHARED_LIBS:BOOL=ON
        -DVTK_WRAP_PYTHON:BOOL=ON
        -DPYTHON_EXECUTABLE:FILEPATH=$SAGE_LOCAL/bin/python
        -DPYTHON_INCLUDE_PATH:PATH=$SAGE_LOCAL/include/python2.7
        -DPYTHON_LIBRARY:FILEPATH=$SAGE_LOCAL/lib/python2.7/config/libpython2.7.a
        -DVTK_DATA_ROOT:FILEPATH=../VTKData
        -DVTK_USE_GUISUPPORT:BOOL=ON
        -DVTK_USE_DISPLAY:BOOL=ON
        -DVTK_USE_GL2PS:BOOL=ON
        -DVTK_USE_X:BOOL=ON
        -DBUILD_TESTING:BOOL=OFF
        -DCMAKE_INSTALL_PREFIX:PATH=$SAGE_LOCAL
        -DCMAKE_USE_RELATIVE_PATHS:BOOL=OFF
        -DVTK_OPENGL_HAS_OSMESA=ON 
        -DVTK_USE_OFFSCREEN=ON 
        -DOSMESA_INCLUDE_DIR=$SAGE_LOCAL 
        -DOSMESA_LIBRARY=$SAGE_LOCAL/lib/libOSMesa.so 
        -DOPENGL_INCLUDE_DIR=$SAGE_LOCAL/include 
        -DOPENGL_gl_LIBRARY=$SAGE_LOCAL/lib/libGL.so 
        -DOPENGL_glu_LIBRARY=$SAGE_LOCAL/lib/libGLU.so 
      ..

Afterwards, I like to use

ccmake ../

to check all parameters were set properly and all libraries are found. Then I type

make -j2

since I have two CPU cores in my Linux machine, and

make install
CWD=`pwd`
cd Wrapping/Python
sage --python setup.py install
cd $CWD
cp -a $SAGE_ROOT/local/lib/vtk-5.5/libvtk* $SAGE_ROOT/local/lib

You may tell that I was just following the steps in the spkg-install file, just not running it within sage or sage shell.

6.

# install configobj.spkg, needed by mayavi2 (and all other enthought packages)
./sage -i configobj-4.5.3.spkg

7.

# for now install wxPython
./sage -f -m wxPython.2.8.11.0.spkg

8.

# at last
./sage -f -m mayavi-3.1.1.rev23224.spk

The end of installation. Now go to sage notebook webpage, and evaluate the following command:

from enthought.mayavi import mlab as M
M.options.offscreen = True
M.test_mesh()
M.axes()
M.title("Mesh")
M.savefig("mesh.png")

If you should be able to see something like this:

mesh generated by Mayavi2 on sage notebook

Last but not least, you can now generate a x3d file on sage notebook and view it under any brower that has a viewer plugin. In my case I installed freeWRL. You can download the x3d file and view it on your own machine. Here is the source code:

#Test embedding x3d files
from enthought.mayavi import mlab
mlab.options.offscreen = True
mlab.clf()
import numpy
# A simple triangular mesh
mlab.test_triangular_mesh()
# Some nice vectors.
x, y, z = numpy.mgrid[-2:3,-2:3, -2:3]
r = numpy.sqrt(x**2 + y**2 + z**4)
u = y*numpy.sin(r)/(r+0.001)
v = -x*numpy.sin(r)/(r+0.001)
w = numpy.zeros_like(z)
mlab.quiver3d(x,y,z,u,v,w,scale_factor=1)
# Export a x3d file for online 3D viewing.
# FreeWRL (http://freewrl.sourceforge.net) works great!
mlab.savefig("a.x3d")

a.x3d viewed from freewrl standalone application


The worksheet of sage notebook is published at
https://junweihuang.info:8000/home/pub/1/

Leave a Reply

Your email address will not be published. Required fields are marked *