complex valued function with "QM-standard" color convention?

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

complex valued function with "QM-standard" color convention?

vasileg
This post was updated on .
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

complex valued function with "QM-standard" color convention?

vasileg

Dear All,

I want to visualize a function of two real variables with complex values, i.e.
z = f(x,y) is complex with x, y real. Quite similar to the example on the  atomic orbital

 http://code.enthought.com/projects/mayavi/docs/development/html/mayavi/auto/example_atomic_orbital.html

I want to plot a surface with height abs(z)**2; the color of the points on the surface
should depend on the angle(z). In the example of the atomic orbital, the colormap='hsv'
is used. I wasn't able to find out how the colormaps (particularly hsv), is defined.
However, in the standard book of visualization in quantum mechanics (Bernd Thaller), the convention
is that a map is defined with the help of the HLS color system (hue-lightness-saturation):
- the hue of the color is given by angle(z)
- the lightness is given by absolute value of z (0 is white)
- the saturation is always maximal.

In case of complex functions depending on only one variable, I was able to implement the same idea:
just define the hsv_colors with this convention (well, in 1D is the lightness also maximal)
and convert them to rgb_colors that we feed to the matplotlib routines, see below.

How should I implement this with mayavi2?

Examples to try:
g(x) = x*exp(-x**2) for x in [-1,1] should show two colors
f(x,y) = g(x)*g(y) for x,y in [-1,1] should show two colors and no jumps, since white appears at x=0 or y=0 and the lightness vary smoothly (the RGBA has jumps...)

Thank  you,
Vasile


from numpy import pi, empty, array
from matplotlib.colors import hsv_to_rgb
from matplotlib.collections import LineCollection
from matplotlib.pyplot import gca, plot

def phaseplot(grid, phase, modulus, ax=None, label="", color="k", alpha=1.0, linestylep="solid", linestyle="solid", linewidthp=1, linewidth=1, marker="None"):
    """Plot the modulus complex valued function $f:R -> C$ together with its phase in a color coded fashion.
    @param grid: The grid nodes of the real domain R
    @param phase: The phase of the complex domain result f(grid)
    @param modulus: The modulus of the complex domain result f(grid)
    """
    # Color mapping
    hsv_colors = empty((1, len(grid), 3))
    hsv_colors[:, :, 0] = 0.5*(pi+phase)/pi
    hsv_colors[:, :, 1] = 1.0
    hsv_colors[:, :, 2] = 1.0
    rgb_colors = hsv_to_rgb(hsv_colors)

    # Put all the vertical line into a collection
    segments = [ array([[node,0], [node,value]]) for node, value in zip(grid, modulus) ]
    line_segments = LineCollection(segments)

    # Set some properties of the lines
    rgb_colors = line_segments.to_rgba(rgb_colors)
    line_segments.set_color(rgb_colors[0])
    line_segments.set_linestyle(linestylep)
    line_segments.set_linewidth(linewidthp)


--
View this message in context: http://enthought-dev.117412.n3.nabble.com/complex-valued-function-with-QM-standard-color-convention-tp3038765p3038765.html
Sent from the Enthought Dev mailing list archive at Nabble.com.
_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev
Reply | Threaded
Open this post in threaded view
|

Re: complex valued function with "QM-standard" color convention?

Gael Varoquaux
On Wed, Jun 08, 2011 at 12:04:38PM +0000, Gradinaru  Vasile Catrinel wrote:
> is that a map is defined with the help of the HLS color system (hue-lightness-saturation):
> - the hue of the color is given by angle(z)
> - the lightness is given by absolute value of z (0 is white)
> - the saturation is always maximal.

> In case of complex functions depending on only one variable, I was able
> to implement the same idea: just define the hsv_colors with this
> convention (well, in 1D is the lightness also maximal) and convert them
> to rgb_colors that we feed to the matplotlib routines, see below.

> How should I implement this with mayavi2?

Having a 2D colormap is not currently supported in Mayavi. If you want to
define your own 1D colormap (that can then be called 'LUT' for Look Up
Table), you can insert a pre-computed table, as can be seen in the
following example:
http://code.enthought.com/projects/mayavi/docs/development/html/mayavi/auto/example_custom_colormap.html

Hope this helps,

Gaël
_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev
Reply | Threaded
Open this post in threaded view
|

Re: [MayaVi-users] complex valued function with "QM-standard" color convention?

vasileg
Dear Gael,

thank you for your fast answer.
I manipulated the colormap, as you see below, but I am not sure it
does what I wanted, because I do not understand exactly how the LUT works.
I expected a smoother transition in the example below. This does not happens, since
the value v in hsv is constant 1.

We can also compute the color of each computed point.
Is there an efficient way to plot only the points with their associated colors?
This will not look as good as mesh or surf, but it could be a start.  

Another approach would be if
lut: [0,1] x [0,1] x [0,1] x [0,1] -> R x G x B x A
instead of
lut: [0,1]  -> R x G x B x A
    scalar |-> (r,b,g,alpha)
would be available, or even better,
mesh(..., scalar1=?, scalar2=?, scalar3=?, scalar4=?)

Any further ideas?
Thanks,
Vasile

from numpy import linspace, pi, exp, angle, squeeze, zeros
from matplotlib.colors import hsv_to_rgb
from enthought.mayavi import mlab


# 1) create a colormap which do what I think it should do:
def compute_color_map(): # B. Thaller like convention
    k = linspace(-pi, pi, 256, endpoint=True)
    #unitcircle = exp(1.0j*k)
    #hsv_colors = zeros((1, unitcircle.shape[0], 3))
    #hsv_colors[:,:, 0] = 0.5*(pi+angle(unitcircle))/pi
    hsv_colors = zeros((1, k.shape[0], 3))
    hsv_colors[:,:, 0] = 0.5*(pi+k)/pi
    hsv_colors[:,:, 1] = 1.0
    hsv_colors[:,:, 2] = 1.0
    rgb_colors = hsv_to_rgb(hsv_colors)
    color_map = 255*squeeze(rgb_colors)
    return color_map


# 2) define the ploting function
def surfCF(gridx, gridy, phase, modulus, colormap=None):
    """Plot the modulus of a complex valued function $f:R^2 -> C$ together with its phase in a color coded fashion.
    @param grid: The grid nodes of the real domain R
    @param phase: The phase of the complex domain result f(grid)
    @param modulus: The modulus of the complex domain result f(grid)
    @keyword colormap: The colormap to use, if none is given, compute the 'default' QM colormap.
    """

    if colormap is None:
        colormap = compute_color_map()

    mesh = mlab.mesh(gridx, gridy, modulus, scalars=phase, vmin=-pi, vmax=pi)

    # Set the custom color map
    lut = mesh.module_manager.scalar_lut_manager.lut.table.to_array()
    lut[:,0] = colormap[:,0]
    lut[:,1] = colormap[:,1]
    lut[:,2] = colormap[:,2]
    mesh.module_manager.scalar_lut_manager.lut.table = lut
    mlab.draw()
    return mesh


# 3) then test it
if __name__ == "__main__":
    from numpy import real, imag, conj, abs, mgrid
    x, y = mgrid[-2:2:0.05, -2:2:0.05]

    u = x*exp(-0.25*x**2)
    v = y*exp(-0.25*y**2)
    z = u*v

    surfCF(x, y, angle(z), abs(z)**2)

________________________________________
From: Gael Varoquaux [[hidden email]]
Sent: Thursday, June 09, 2011 7:43 AM
To: [hidden email]
Cc: [hidden email]
Subject: Re: [MayaVi-users] [Enthought-Dev] complex valued function with "QM-standard" color convention?

On Wed, Jun 08, 2011 at 12:04:38PM +0000, Gradinaru  Vasile Catrinel wrote:
> is that a map is defined with the help of the HLS color system (hue-lightness-saturation):
> - the hue of the color is given by angle(z)
> - the lightness is given by absolute value of z (0 is white)
> - the saturation is always maximal.

> In case of complex functions depending on only one variable, I was able
> to implement the same idea: just define the hsv_colors with this
> convention (well, in 1D is the lightness also maximal) and convert them
> to rgb_colors that we feed to the matplotlib routines, see below.

> How should I implement this with mayavi2?

Having a 2D colormap is not currently supported in Mayavi. If you want to
define your own 1D colormap (that can then be called 'LUT' for Look Up
Table), you can insert a pre-computed table, as can be seen in the
following example:
http://code.enthought.com/projects/mayavi/docs/development/html/mayavi/auto/example_custom_colormap.html

Hope this helps,

Gaël

------------------------------------------------------------------------------
EditLive Enterprise is the world's most technically advanced content
authoring tool. Experience the power of Track Changes, Inline Image
Editing and ensure content is compliant with Accessibility Checking.
http://p.sf.net/sfu/ephox-dev2dev
_______________________________________________
MayaVi-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/mayavi-users
_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev
Reply | Threaded
Open this post in threaded view
|

Re: [MayaVi-users] complex valued function with "QM-standard" color convention?

Gael Varoquaux
Hi,

I had a quick look at your code, and it seems jst fine. It even seems to
run fine, at least the Mayavi part. The 'angle(z)' variable that you are
passing in is only made of 0 and pi values, thus I am not surprised that
the mesh shows only two colors. Maybe that's where the problem lies?

Cheers,

Gael

On Thu, Jun 09, 2011 at 03:00:36PM +0000, Gradinaru  Vasile Catrinel wrote:
>     from numpy import real, imag, conj, abs, mgrid
>     x, y = mgrid[-2:2:0.05, -2:2:0.05]

>     u = x*exp(-0.25*x**2)
>     v = y*exp(-0.25*y**2)
>     z = u*v

>     surfCF(x, y, angle(z), abs(z)**2)

_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev
Reply | Threaded
Open this post in threaded view
|

Re: [MayaVi-users] complex valued function with "QM-standard" color convention?

vasileg
Hi,

having only 0 and pi is just what I wanted: a simple example allows me to find faster the bugs...
Since the v in the hsv scheme is constant (hue-saturation-value) we see the jump.
Having the v set by the (squared) absolute value of z=f(x,y) would allow the points near to 0 to be darker
and maybe to not allow the eyes to realize too much of the jump.

Do you know if there is an efficient way to plot only the points with their associated colors
(each point comes with its own color)?
   
Vasile
________________________________________
From: Gael Varoquaux [[hidden email]]
Sent: Friday, June 10, 2011 9:01 AM
To: Gradinaru  Vasile Catrinel
Cc: [hidden email]; [hidden email]
Subject: Re:  [MayaVi-users] [Enthought-Dev] complex valued function with "QM-standard" color convention?

Hi,

I had a quick look at your code, and it seems jst fine. It even seems to
run fine, at least the Mayavi part. The 'angle(z)' variable that you are
passing in is only made of 0 and pi values, thus I am not surprised that
the mesh shows only two colors. Maybe that's where the problem lies?

Cheers,

Gael

On Thu, Jun 09, 2011 at 03:00:36PM +0000, Gradinaru  Vasile Catrinel wrote:
>     from numpy import real, imag, conj, abs, mgrid
>     x, y = mgrid[-2:2:0.05, -2:2:0.05]

>     u = x*exp(-0.25*x**2)
>     v = y*exp(-0.25*y**2)
>     z = u*v

>     surfCF(x, y, angle(z), abs(z)**2)

_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev
Reply | Threaded
Open this post in threaded view
|

Re: [MayaVi-users] complex valued function with "QM-standard" color convention?

vasileg
Hi,

I still not have a solution for plotting the complex valued functions with mayavi.
It is not clear to me whether there is an issue of VTK itself or of a not-yet-implemented
function in mayavi. However, I was able to do exactly what I wanted using matplotlib;
unfortunately, this has to stay 2D. I was hoping to be able to do something similar
for 3-dimensional functions in mayavi...

Here is the exercise: given the function f(x,y) = (-1.+(x+1.j*y)**3)**2
we have to plot z = f(x,y) for x an y between -2 and 2. As z is complex, we follow the
convention of Thaller (quantum mechanics): the color of each point is first computed in the
hsv system, where h (hue) is given by the angle of z, s is always 1 and v (value) is related to the
absolute value of z (by the stereographic projection) such that a point of absolute value 0 is completely dark.
After that, the hsv codes are traduced into the rgb system and then the points with their own colors are ploted with the imshow function of matplotlib.
The proposed function to plot is nice, because we'll recognize 3 dark points: it has 3 zeros. The phase spectrum appears 2 times around each dark point, meaning that each of the zeros has multiplicity 2.

If you run the implementation below (which uses matplotlib), you'll agree, I hope,  that such a visualization  is
more helpful that a classical one, and hence it is worthwhile to find a solution in mayavi, too...

Would it be possible in mayavi to plot only the points, but each with its own color and without modifying their size?

Vasile
----------

from matplotlib.colors import hsv_to_rgb
import numpy as np

# create the grid
x, y = np.mgrid[-2:2:0.01, -2:2:0.05]
z = -1.+(x+1.j*y)**3
z = z**2 # function to be ploted
# funny think in matplotlib: the axes are inversed :(
z = z.transpose(); x = x.transpose(); y = y.transpose()
# go for the hue from the phase
phase = np.angle(-z) # to get exactly Thallers colors
A = 0.5*(phase+np.pi)/np.pi
# go for the value in the hsv scheme
R = abs(z)**2; M = 0.1#R.min() # M is needed in the projection
theta = np.pi - 2*np.arctan(np.sqrt(R/M))

# prepare the points to be ploted
Nx,Ny = z.shape
hsv_colors = np.empty((Nx, Ny,3))
hsv_colors[:, :, 0] = A
hsv_colors[:, :, 1] = 1.
hsv_colors[:, :, 2] = 1.-theta/np.pi
z_rgb = hsv_to_rgb(hsv_colors)
# and plot them
import matplotlib.pyplot as plt
fig = plt.figure()
plt.imshow(z_rgb, origin='lower', extent=[-2,2,-2,2])
plt.xlabel('x')
plt.ylabel('y')
fig = plt.figure()
# look only at the absolute value
plt.imshow(R, origin='lower', extent=[-2,2,-2,2])
plt.xlabel('x')
plt.ylabel('y')
plt.show()


________________________________________
From: Gradinaru  Vasile Catrinel [[hidden email]]
Sent: Friday, June 10, 2011 11:44 AM
To: Gael Varoquaux
Cc: [hidden email]; [hidden email]
Subject: Re: [MayaVi-users] [Enthought-Dev] complex valued function with "QM-standard" color convention?

Hi,

having only 0 and pi is just what I wanted: a simple example allows me to find faster the bugs...
Since the v in the hsv scheme is constant (hue-saturation-value) we see the jump.
Having the v set by the (squared) absolute value of z=f(x,y) would allow the points near to 0 to be darker
and maybe to not allow the eyes to realize too much of the jump.

Do you know if there is an efficient way to plot only the points with their associated colors
(each point comes with its own color)?

Vasile
________________________________________
From: Gael Varoquaux [[hidden email]]
Sent: Friday, June 10, 2011 9:01 AM
To: Gradinaru  Vasile Catrinel
Cc: [hidden email]; [hidden email]
Subject: Re:  [MayaVi-users] [Enthought-Dev] complex valued function with "QM-standard" color convention?

Hi,

I had a quick look at your code, and it seems jst fine. It even seems to
run fine, at least the Mayavi part. The 'angle(z)' variable that you are
passing in is only made of 0 and pi values, thus I am not surprised that
the mesh shows only two colors. Maybe that's where the problem lies?

Cheers,

Gael

On Thu, Jun 09, 2011 at 03:00:36PM +0000, Gradinaru  Vasile Catrinel wrote:
>     from numpy import real, imag, conj, abs, mgrid
>     x, y = mgrid[-2:2:0.05, -2:2:0.05]

>     u = x*exp(-0.25*x**2)
>     v = y*exp(-0.25*y**2)
>     z = u*v

>     surfCF(x, y, angle(z), abs(z)**2)


------------------------------------------------------------------------------
EditLive Enterprise is the world's most technically advanced content
authoring tool. Experience the power of Track Changes, Inline Image
Editing and ensure content is compliant with Accessibility Checking.
http://p.sf.net/sfu/ephox-dev2dev
_______________________________________________
MayaVi-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/mayavi-users
_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev