04 Jan

Mayavi2: using from ipython

Recently Prabhu and I have been ironing the library aspect of Mayavi2 (library as opposed to application). One of the usecases we are interested in, is interative use, via for instance ipython, a la pylab.

Most people think of Mayavi as a big and powerful application, maybe a bit clunky to script and to get to interact with other bits of code. With the recent additions you can use mayavi just as you would use matplotlib, to complete matplotlib’s 3D plotting.

3D plotting from ipython

As Mayavi relies on wxPython (technical details, yes I know you don’t care), to use it with ipython, you have to start ipython with iptyhon -wthread. Using ipython from svn you can start ipython with both the -pylab and the -wthread options to use both pylab and mayavi for 2D and 3D plotting (beware of namespace mangling, don’t use from module import *).

The matlab/pylab-like interface to Mayavi is found in enthought.mayavi.tools.mlab (this will most probably change to enthought.mayavi.mlab, or to something else shorter), import this module to have familiar functions. Documentation is a bit missing for now, (you can grab some kind of a embryo at https://svn.enthought.com/enthought/attachment/wiki/MayaVi/mlab.pdf?format=raw ), so I’ll just show an example:

from enthought.mayavi.toosl import mlab as M
from numpy import *
f = lambda x, y: sin(x + y) + sin(2*x - y ) + cos(3*x + 4*y)
x, y = mgrid[-7:7.05:0.1, -5:5.05:0.05]
M.surf(x, y, f)
M.axes()
M.title('Demoing mlab.surf')

demoing_mlab_1.jpg

Apart from the surf command, the different commands used have equivalents in pylab. surf is inspired from matlab: let us continue pylab’s work here. OK, the keyword arguments are not exactly the same, and not all pylab features are available through the mayavi/mlab interface. But the good news is that the objects created are VTK objects, even though it is a bit hidden by this simplified interface. This means that there is the power of VTK, and that you can always modify the resulting objects to fine tune properties that you cannot (yet) tweak with keyword arguments.

Modifying the plot from the GUI

OK, as you see, we can control Mayavi without all the fuss of the user interface. We get a really simple window that does not get in our way. But is this still Mayavi? This clunky UI was convenient to interact with the visualization. I can pop the pipeline up using M.show_engine(). Once I have the pipeline I can easily double click on any of the items I want to modify, and I get the usual Mayavi dialogs that are so convenient when trying to tweak a scene, for instance to modify the colormap:

demoing_mlab_2.jpg

This is still work in progress, but mlab is completely useable for real work (I use it whenever I want to make a figure in 3D). Beware that the API is not cast into stone (that’s a good moment to make remarks) and that it might change. Documentation is also lacking. Don’t worry it will come pretty soon, but I also have a thesis to defend :->.

15 Responses to “Mayavi2: using from ipython”

  1. Gaël Varoquaux » Blog Archive » Adding simple customisation to Mayavi2 Says:

    [...] on people wanting to use Mayavi2, we have been working on decoupling the two. As I showed in a previous post, Mayavi2 can now fully be used without Envisage. But this is in the development version, and some [...]

  2. Cristobal Says:

    Hi, I’ve installed mayavi2 in ubuntu hardy but doesn’t work fine.
    I’ve created your example in a .py file, but when i put python superficie.py then opens mayavi2 but appears these two messages:
    Exception
    In /usr/lib/python2.5/site-packages/enthought/mayavi/core/pipeline_base.py:113
    AttributeError: ‘DecoratedScene’ object has no attribute ‘add_widgets’ (in add_actors)

    and the other one is

    Current object is not active, please select an active object

    i have no idea because it happens one, only i know that chaco2 works me fine
    Have you got an idea because i have got this problem?

    Thanks

  3. anton Says:

    hi
    I wanted to know how to use the package in ubuntu mlab
    I mayavi2 but I did not recognize mlab
    Help

  4. gael Says:

    This blog post is terribly old. This is no longer the recommended way of doing this. All you need is to change the import:

    from enthought.mayavi import mlab as M

    Also, there is an extensive documentation on line, please see:
    http://code.enthought.com/projects/mayavi/docs/development/html/mayavi/mlab.html#simple-scripting-with-mlab

  5. gael Says:

    Also, Anthon, make sure that you have a reasonably recent mayavi install. The one in jaunty is good, but if you are using intrepid or older, you will need to use the packages available at:
    https://launchpad.net/~gael-varoquaux/+archive/ppa

  6. anton Says:

    I tried this code
    but I get this:

    biblio@biblio-desktop:~$ ipython -wthread
    Python 2.5.2 (r252:60911, Jul 31 2008, 17:28:52)
    Type “copyright”, “credits” or “license” for more information.

    IPython 0.8.1 — An enhanced Interactive Python.
    ? -> Introduction to IPython’s features.
    %magic -> Information about IPython’s ‘magic’ % functions.
    help -> Python’s own help system.
    object? -> Details about ‘object’. ?object also works, ?? prints more.

    In [1]: from enthought.mayavi.tools import mlab as M
    Set Envisage to use the workbench UI: True
    Set Envisage to use the workbench UI: True

    In [2]: from numpy import *

    In [3]: f=lambda x,y: sin(x+y)+sin(2*x-y)+cos(3*x+4*y)

    In [4]: x,y=mgrid[-7.:7.05:0.1, -5.:5.05:0.05]

    In [5]: M.axes()

    (python:9639): Gtk-CRITICAL **: gtk_widget_set_colormap: assertion `!GTK_WIDGET_REALIZED (widget)’ failed
    Warning: invalid value encountered in divide
    Out[5]:

    In [6]: M.title(’anton’)
    Out[6]:

    In [7]: M.show_engine()
    —————————————————————————
    Traceback (most recent call last)

    /home/biblioteca/ in ()

    : ‘module’ object has no attribute ’show_engine’

    In [8]:
    KeyboardInterrupt - Press to continue.
    KeyboardInterrupt - Press to continue.

  7. gael Says:

    Same issue: the ’show_engine’ has been phased out a while ago (I hadn’t realized we had actually suppressed it, but it has been depreciated for more than a year). Use ’show_pipeline()’.

    You should trust the documentation much more than this blog post, it is totally obsolete.

  8. anton Says:

    hi
    I tried but I read all this packaging. deb
    but the page giving me these are in many parts
    also assumes that mayavi2 because I already have this in my sinaptic
    but I do not agree to import mlab when I run code in python

  9. gael Says:

    I don’t really understand completely you last post. I’ll try to reply to what I understand.
    To use the PPA, you need to add the relevant line to your sources, as explained on the page. Don’t try to install the different Debian packages by hand, it will be a loss of time, and changes are you will get it wrong. Just add the url to the list of repositories in synpatic, refresh and upgrade mayavi2 using synaptic.

    I don’t get the sentence about importing mlab.

  10. anton Says:

    grasias

    for your answer
    I wrote what came out this

    In [8]: show_pipeline ()
    —————————————————————————
    Traceback (most recent call last)

    /home/biblioteca/ in ()

    : name ’show_pipeline’ is not defined

    In [9]:

  11. gael Says:

    M.show_pipeline(), if you have imported mlab as M.

  12. anton Says:

    what happens is that when I run this code Pyhton:
    from pylab import *
    from enthought.mayavi import mlab

    I get:

    Traceback (most recent call last):
    File “/home/biblioteca/Escritorio/eje.py”, line 2, in
    from enthought.mayavi import mlab
    ImportError: cannot import name mlab

    help

  13. anton Says:

    I get:

    In [10]: M.show_pipeline ()
    —————————————————————————
    Traceback (most recent call last)

    /home/biblioteca/ in ()

    : ‘module’ object has no attribute ’show_pipeline’

  14. gael Says:

    What is your version of mayavi2? I suspect 2.2.1 or something like this. Please upgrade to 3.x. It is really easy: just a question of adding the PPA to your sources in synaptic.

    We cannot support too old versions of mayavi2, especially since they are not well-documented compared to the newest ones, so we have to answer questions all the time.

    Ubuntu main packages had fallen behind with mayavi2 version, which is a pitty, but it has been fixed: the new version in Jaunty is just fine, and the Ubuntu and Debian packagers are tracking things closely.

  15. anton Says:

    hi
    I am a student of the academic professional school of computer scienceUNMSM
    and I am very interested in my projects to make mayavi Undergraduate
    also
    I have
    ubuntu 8.04
    mayavi2 2.0.2a1-4ubuntu2 2.0.2a1-4ubuntu2
    python 2.5

    I can do to correctly use mayavi2
    I appreciate your help

Leave a Reply