Showing posts with label Mayavi2. Show all posts
Showing posts with label Mayavi2. Show all posts

Sunday, June 10, 2012

Success! - First Mayavi Plot

Recently, I started Python from a fresh re-install of Python(x, y).  Confident I had everything, including Mayavi, installed correctly.  I tried to re-run some examples I found online.  This time I did not receive any error messages regarding that Mayavi could not be recognized.  However, I was/am still having problems.

I decided to try to open Mayavi from the Start Menu in Windows under the Python(x, y) folder structure.  It worked!  That is, Mayavi opened as a separate console/program.  I found Run Python Script under the File heading.  I tried to run one of my previous files, and this time it really did work!!

This is the spherical harmonic example using the mesh() function (although the example is a little different, maybe I got it from somewhere else; can't remember) from the Mayavi website:
http://docs.enthought.com/mayavi/mayavi/auto/examples.html
http://docs.enthought.com/mayavi/mayavi/index.html
http://docs.enthought.com/mayavi/mayavi/auto/mlab_helper_functions.html#
http://docs.enthought.com/mayavi/mayavi/auto/mlab_helper_functions.html#mesh

Edit:  I found the example, it is the provided demo - http://docs.enthought.com/mayavi/mayavi/mlab.html#simple-scripting-with-mlab

from numpy import *
from enthought.mayavi import mlab
# Create the data.
dphi, dtheta = pi/250.0, pi/250.0
[phi,theta] = mgrid[0:pi+dphi*1.5:dphi,0:2*pi+dtheta*1.5:dtheta]
m0 = 4; m1 = 3; m2 = 2; m3 = 3; m4 = 6; m5 = 2; m6 = 6; m7 = 4;
r = sin(m0*phi)**m1 + cos(m2*phi)**m3 + sin(m4*theta)**m5 + cos(m6*theta)**m7
x = r*sin(phi)*cos(theta)
y = r*cos(phi)
z = r*sin(phi)*sin(theta);
# View it.
f = mlab.figure()
s = mlab.mesh(x, y, z)

My screenshots:




However, I could not get the flow() function example to work, :(, which I would like to use for 3D streamline plots.

import numpy
import enthought
from mayavi.mlab import *

def test_flow():
    x, y, z = numpy.mgrid[0:5, 0:5, 0:5]
    r = numpy.sqrt(x**2 + y**2 + z**4)
    u = y*numpy.sin(r)/r
    v = -x*numpy.sin(r)/r
    w = numpy.zeros_like(z)
    obj = flow(u, v, w)
    return obj

Edit: I got the test flow to work.  Just remove the def and return and unindent the rest.  (However, I still have very little grasp on how MayaVi works as I am having great difficulty in trying to produce streamlines from a velocity vector field.  Concept seems simple enough but...  I also still cannot get MayaVi to work from Spyder.  Very frustrating!!!!)

import numpy
import enthought
from mayavi.mlab import *

x, y, z = numpy.mgrid[0:5, 0:5, 0:5]
r = numpy.sqrt(x**2 + y**2 + z**4)
u = y*numpy.sin(r)/r
v = -x*numpy.sin(r)/r
w = numpy.zeros_like(z)
obj = flow(u, v, w)

I did get another 3D plot example I found to work which also uses the mesh() function.

http://www.sam.math.ethz.ch/~hheumann/Tutorial/html/mayavi2_tips.html

from numpy import mgrid, real, conj, ones, zeros
from numpy.fft.fftpack import fft2
from numpy.fft.helper import fftshift
from enthought.mayavi import mlab

# A mesh grid
X,Y = mgrid[-100:100, -100:100]

# The initial function: a 2D unit step
Z = zeros((200,200))
Z[0:6,0:6] = 0.3*ones((6,6))

# The fourier transform: a 2D sinc(x,y)
W = fftshift(fft2(Z))
W = real(conj(W)*W)

# Display the data with mayavi
# Plot the original function
#mlab.mesh(X, Y, Z)
# Plot the fourier transformed function
mlab.mesh(X, Y, W)
mlab.savefig("mayavi_fft_plot.png")


Saturday, June 9, 2012

Getting Python to work in Windows - Python 2.7.2, Python(x, y) 2.7.2.3, Sypder2, and Mayavi2

Ok, whew, I finally got a good grasp on installing Python in Windows 7.  I had it installed first (through Python(x,y) also apparently) but was having issues with getting Mayavi to work (or so I thought then, could have been ok I just didn't understand the structure until I got it to work now; not sure if I really understand, but whatever, it works).

So, I decided to uninstall everything again, and then re-install because I really had forgotten how I did it in the first place.  After some googling and browsing, I knew I wanted to use and install Spyder because I had it before.  So I went to the site http://packages.python.org/spyder/installation.html then found a link to https://code.google.com/p/spyderlib/.  However, I kept seeing Windows - Python(x,y).  So read and browsed a little more to find that Python(x,y) is a one stop for all source.  It not only installs Python, Spyder, IPython, and libraries such as Numpy, Scipy, Matplotlib, etc., but it also installs many other packages such as the IDLE simple IDE/editor, Mayavi2, etc. (all are options of course).  Python(x, y) also is integrated with or as (I don't yet understand) a Qt framework (again, not so sure what Qt is/does; like Java app development maybe?).

Qt links:
http://qt.nokia.com/
http://qt.nokia.com/products

Here is a nice schematic displaying the integration of Python(x, y) from http://code.google.com/p/pythonxy/wiki/Welcome


Python(x, y) home page link: http://code.google.com/p/pythonxy/

Python(x, y) installer download page: http://code.google.com/p/pythonxy/wiki/Downloads

The latest Python(x, y) version is 2.7.2.3 and simply download and run the installer .exe file: Python(x,y)-2.7.2.3.exe



I kept everything default under the installation except I added the SymPy library/package under the Python expansion.



Notice all the packages that come with Python(x, y):


Important note!  The Mayavi website says to make sure ETS (short for Enthought Software?) is checked for the installation of Mayavi.

http://docs.enthought.com/mayavi/mayavi/installation.html

Python(x, y) installs with it Python 2.7.2


Although, there have been releases of Python 3.x (3.2.3 as the latest stable) and a more recent 2.7.x as 2.7.3.  It seems that packages aren't caught up to 3.x yet, so it may be best to use 2.7.x for now?  See these links for more:

http://www.python.org/
http://www.python.org/download/releases/2.7.3/
http://wiki.python.org/moin/Python2orPython3

After installation, I feel more comfortable and confident that Python and packages such as Mayavi are installed properly.  I was getting an error before that Mayavi couldn't be found.

This is proof!
Spyder is installed too.