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 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- to $SAGE_ROOT.

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


# 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

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
cd Wrapping/Python
sage --python 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.


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


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


# 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

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
import numpy
# A simple 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)
# Export a x3d file for online 3D viewing.
# FreeWRL ( works great!

a.x3d viewed from freewrl standalone application

The worksheet of sage notebook is published at

Leave a Reply

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