In [1]:
%%javascript
IPython.load_extensions('calico-document-tools');



In [1]:
!date


Fri Feb 27 21:03:52 MST 2015

In [2]:
from pyqtgraph.Qt import QtCore, QtGui
import pyqtgraph.opengl as gl
import pyqtgraph as pg
import numpy as np


Vendor:  Continuum Analytics, Inc.
Package: mkl
Message: trial mode expires in 30 days

Objective: propagating plane wave visualization

To do

  • DONE 2/27/15 - Propagating e-field
    • TRIED 2/27/15, ARROWS DON'T SEEM TO WORK IN 3D, JUST 2D. include some propagating arrows
  • DONE 2/27/15 - propagating h-field
  • TRIED 2/27/15, DOESN'T LOOK GOOD - semi-transparent plane in x-y through which plane wave propagates
  • DONE 2/27/15 - Add more visible axes
  • DONE 2/27/15 - What is np.vstack doing?
  • TRIED 2/25/15, NO FACILITY TO DO THIS - Fill to zero?
  • DONE 2/28/15 - If not, make my own vertical lines to zero?
  • TRIED 2/28/15, NO DOCUMENTATION INDICATING HOW TO DO THIS - Add labels (E, H, z)
  • DONE 2/28/15, DOESN'T LOOK GREAT IF NOT LINEAR POLARIZATION - Change efield function to set arbitrary polarization state
  • Add ability to change propagation velocity

How to get docstrings for a class definition


In [ ]:
help(pg.opengl.GLLinePlotItem)
help(pg.opengl.GLGridItem)
help(pg.QtGui.QGraphicsRectItem)

Figure out what makeARGB is doing


In [13]:
image_shape = (4,4)
uniform_values = np.ones(image_shape) * 255
uniform_image = pg.makeARGB(uniform_values)
print uniform_values
print uniform_image


[[ 255.  255.  255.  255.]
 [ 255.  255.  255.  255.]
 [ 255.  255.  255.  255.]
 [ 255.  255.  255.  255.]]
(array([[[255, 255, 255, 255],
        [255, 255, 255, 255],
        [255, 255, 255, 255],
        [255, 255, 255, 255]],

       [[255, 255, 255, 255],
        [255, 255, 255, 255],
        [255, 255, 255, 255],
        [255, 255, 255, 255]],

       [[255, 255, 255, 255],
        [255, 255, 255, 255],
        [255, 255, 255, 255],
        [255, 255, 255, 255]],

       [[255, 255, 255, 255],
        [255, 255, 255, 255],
        [255, 255, 255, 255],
        [255, 255, 255, 255]]], dtype=uint8), False)

In [1]:
from pyqtgraph.Qt import QtCore, QtGui
import pyqtgraph.opengl as gl
import pyqtgraph as pg
import numpy as np

app = QtGui.QApplication([])
w = gl.GLViewWidget()
w.opts['distance'] = 200
w.show()
w.setWindowTitle('pyqtgraph example: GLImageItem')

## create volume data set to slice three images from
shape = (100,100,70)
data = np.random.normal(size=shape)
#data += pg.gaussianFilter(np.random.normal(size=shape), (15,15,15))*15

## slice out three planes, convert to RGBA for OpenGL texture
levels = (-0.08, 0.08)
tex1 = pg.makeRGBA(data[shape[0]/2], levels=levels)[0]       # yz plane
tex2 = pg.makeRGBA(data[:,shape[1]/2], levels=levels)[0]     # xz plane
tex3 = pg.makeRGBA(data[:,:,shape[2]/2], levels=levels)[0]   # xy plane
#tex1[:,:,3] = 128
tex2[:,:,3] = 128
#tex3[:,:,3] = 128

## Create three image items from textures, add to view
v1 = gl.GLImageItem(tex1)
v1.translate(-shape[1]/2, -shape[2]/2, 0)
v1.rotate(90, 0,0,1)
v1.rotate(-90, 0,1,0)
#w.addItem(v1)
v2 = gl.GLImageItem(tex1)
v2.translate(-shape[0]/2, -shape[2]/2, 0)
v2.rotate(-90, 1,0,0)
w.addItem(v2)
v3 = gl.GLImageItem(tex3)
v3.translate(-shape[0]/2, -shape[1]/2, 0)
#w.addItem(v3)

ax = gl.GLAxisItem()
w.addItem(ax)

## Start Qt event loop unless running in interactive mode.
if __name__ == '__main__':
    import sys
    if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
        QtGui.QApplication.instance().exec_()


Vendor:  Continuum Analytics, Inc.
Package: mkl
Message: trial mode expires in 29 days

In [4]:
print shape[0], shape[1], shape[2]


100 100 70

In [10]:
print len(data[shape[0]/2]), len(data[:,shape[1]/2])


100 100

In [11]:
shape = (5,4,3)
data = np.random.normal(size=shape)
print data


[[[ 0.45984547  1.36035537  0.17877769]
  [-0.74233363 -1.4027535  -0.31127744]
  [ 0.15139385 -1.53737929 -0.44762799]
  [-0.07928505 -0.55425195 -0.4397145 ]]

 [[-1.18939939  2.14264077 -0.24350655]
  [ 1.74703657  0.02382973 -1.21494055]
  [-1.05530072 -0.61658412  0.11202   ]
  [-1.64019854  0.90568988  2.00091668]]

 [[ 0.68254619 -0.26610311 -1.7318612 ]
  [ 0.802056   -0.65997896  1.60191216]
  [-0.45561828  0.75331113  1.04526321]
  [-0.26396584 -0.83466144 -0.09280454]]

 [[-0.52378385 -0.67985562  0.51557659]
  [ 0.19575697  0.47967583  1.60270125]
  [-0.83338379 -0.35544256  0.30715218]
  [-0.70449796 -0.11221931  0.41522148]]

 [[ 0.0137461   0.39272416 -0.17842813]
  [-0.04211752  2.73380214  1.19733147]
  [-1.95301056 -1.76086652  0.62690548]
  [ 0.81274395  0.63919669 -1.07803292]]]

In [12]:
print data[shape[0]/2]


[[ 0.68254619 -0.26610311 -1.7318612 ]
 [ 0.802056   -0.65997896  1.60191216]
 [-0.45561828  0.75331113  1.04526321]
 [-0.26396584 -0.83466144 -0.09280454]]

In [13]:
print data[:,shape[1]/2]


[[ 0.15139385 -1.53737929 -0.44762799]
 [-1.05530072 -0.61658412  0.11202   ]
 [-0.45561828  0.75331113  1.04526321]
 [-0.83338379 -0.35544256  0.30715218]
 [-1.95301056 -1.76086652  0.62690548]]

In [14]:
print data[:,:,shape[2]/2]


[[ 1.36035537 -1.4027535  -1.53737929 -0.55425195]
 [ 2.14264077  0.02382973 -0.61658412  0.90568988]
 [-0.26610311 -0.65997896  0.75331113 -0.83466144]
 [-0.67985562  0.47967583 -0.35544256 -0.11221931]
 [ 0.39272416  2.73380214 -1.76086652  0.63919669]]

In [20]:
tex = pg.makeRGBA(data[shape[2]/2])[0]
print tex


[[[  0   0   0 255]
  [  2   2   2 255]
  [  0   0   0 255]]

 [[  1   1   1 255]
  [  0   0   0 255]
  [  0   0   0 255]]

 [[  0   0   0 255]
  [  0   0   0 255]
  [  0   0   0 255]]

 [[  0   0   0 255]
  [  0   0   0 255]
  [  2   2   2 255]]]

In [4]:
image_shape = (3,5)
uniform_values = np.ones(image_shape) * 255
uniform_image = pg.makeARGB(uniform_values)[0]
uniform_image[:,:,3] = 128
print uniform_image


[[[255 255 255 128]
  [255 255 255 128]
  [255 255 255 128]
  [255 255 255 128]
  [255 255 255 128]]

 [[255 255 255 128]
  [255 255 255 128]
  [255 255 255 128]
  [255 255 255 128]
  [255 255 255 128]]

 [[255 255 255 128]
  [255 255 255 128]
  [255 255 255 128]
  [255 255 255 128]
  [255 255 255 128]]]

Make a semi-transparent rectangle (image)


In [1]:
from pyqtgraph.Qt import QtCore, QtGui
import pyqtgraph.opengl as gl
import pyqtgraph as pg
import numpy as np

app = QtGui.QApplication([])
w = gl.GLViewWidget()
w.opts['distance'] = 20
w.show()
w.setWindowTitle('pyqtgraph example: GLImageItem')

## create volume data set to slice three images from
shape = (100,100,70)
data = np.random.normal(size=shape)
#data += pg.gaussianFilter(np.random.normal(size=shape), (15,15,15))*15

## make images
image_shape = (6,6)
uniform_values = np.ones(image_shape) * 255
uniform_image = pg.makeARGB(uniform_values)[0]
uniform_image[:,:,1] = 128
uniform_image_transparent = pg.makeARGB(uniform_values)[0]
uniform_image_transparent[:,:,3] = 128

## Create image items from textures, add to view
v2 = gl.GLImageItem(uniform_image)
v2.translate(-image_shape[0]/2, -image_shape[1]/2, 0)
v2.rotate(90, 1,0,0)
v2.translate(0, -2, 0)
w.addItem(v2)
v1 = gl.GLImageItem(uniform_image_transparent)
v1.translate(-image_shape[0]/2, -image_shape[1]/2, 0)
v1.rotate(90, 1,0,0)
w.addItem(v1)

ax = gl.GLAxisItem()
w.addItem(ax)

## Start Qt event loop unless running in interactive mode.
if __name__ == '__main__':
    import sys
    if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
        QtGui.QApplication.instance().exec_()


Vendor:  Continuum Analytics, Inc.
Package: mkl
Message: trial mode expires in 30 days

What is np.vstack.transpose() doing?


In [6]:
x = np.linspace(0,2,3)
y = np.linspace(10,12,3)
z = np.linspace(20,22,3)
print x, '\n', y, '\n', z, '\n'
pts = np.vstack([x,y,z])
print pts, '\n'
pts = pts.transpose()
print pts


[ 0.  1.  2.] 
[ 10.  11.  12.] 
[ 20.  21.  22.] 

[[  0.   1.   2.]
 [ 10.  11.  12.]
 [ 20.  21.  22.]] 

[[  0.  10.  20.]
 [  1.  11.  21.]
 [  2.  12.  22.]]

Answer: take row vectors x, y, & z and concatenate them as column vectors in a 2D matrix

Make vertical lines


In [8]:
x = np.linspace(0,3,4)
y = np.linspace(10,13,4)
z = np.linspace(20,23,4)
#print x, '\n', y, '\n', z, '\n'
pts = np.vstack([x,y,z])
#print pts, '\n'
pts = pts.transpose()
print pts
print pts.shape


[[  0.  10.  20.]
 [  1.  11.  21.]
 [  2.  12.  22.]
 [  3.  13.  23.]]
(4, 3)

In [15]:
pts2 = np.zeros(shape=(2*pts.shape[0], pts.shape[1]))
print pts2
print pts2.shape
for i in range(pts.shape[0]):
    pts2[2*i,2] = pts[i,2]
    pts2[2*i + 1,:] = pts[i,:]
print pts2


[[ 0.  0.  0.]
 [ 0.  0.  0.]
 [ 0.  0.  0.]
 [ 0.  0.  0.]
 [ 0.  0.  0.]
 [ 0.  0.  0.]
 [ 0.  0.  0.]
 [ 0.  0.  0.]]
(8, 3)
[[  0.   0.  20.]
 [  0.  10.  20.]
 [  0.   0.  21.]
 [  1.  11.  21.]
 [  0.   0.  22.]
 [  2.  12.  22.]
 [  0.   0.  23.]
 [  3.  13.  23.]]

In [18]:
# Function to create new array from old
# where new array is formatted to prepare to
# draw lines perpendicular from z-axis to 
# curve defined by input array
def preptomakelines(pts):
    pts2 = np.zeros(shape=(2*pts.shape[0], pts.shape[1]))
    for i in range(pts.shape[0]):
        pts2[2*i,2] = pts[i,2]
        pts2[2*i + 1,:] = pts[i,:]
    return pts2

pts2 = preptomakelines(pts)
print pts, '\n\n', pts2


[[  0.  10.  20.]
 [  1.  11.  21.]
 [  2.  12.  22.]
 [  3.  13.  23.]] 

[[  0.   0.  20.]
 [  0.  10.  20.]
 [  0.   0.  21.]
 [  1.  11.  21.]
 [  0.   0.  22.]
 [  2.  12.  22.]
 [  0.   0.  23.]
 [  3.  13.  23.]]

Simple coordinate transformation

We want to go from calculation coordinates (x,y,z) to pyqtgraph coordinates (xx,yy,zz). For the electric field the transformation is:

  • x -> zz
  • y -> xx
  • z -> yy

This is the same as rotate -90 degrees about the y axis, then rotate 90 degrees about the z-axis. For the magnetic field the transformation is:

  • x -> xx
  • y -> zz
  • z -> yy

In [25]:
x = np.linspace(0,3,4)
y = np.linspace(10,13,4)
z = np.linspace(20,23,4)
pts = np.vstack([x,y,z])
pts = pts.transpose()
print pts
temp2Darray = [[0, 0, 1],
               [1, 0, 0],
               [0, 1, 0]]
rot_efield_coord = np.array(temp2Darray)
print rot_efield_coord
pts_efield_coord = np.dot(pts, rot_efield_coord)
print pts_efield_coord
temp2Darray = [[1, 0, 0],
               [0, 0, 1],
               [0, 1, 0]]
rot_hfield_coord = np.array(temp2Darray)
print rot_hfield_coord
pts_hfield_coord = np.dot(pts, rot_hfield_coord)
print pts_hfield_coord


[[  0.  10.  20.]
 [  1.  11.  21.]
 [  2.  12.  22.]
 [  3.  13.  23.]]
[[0 0 1]
 [1 0 0]
 [0 1 0]]
[[ 10.  20.   0.]
 [ 11.  21.   1.]
 [ 12.  22.   2.]
 [ 13.  23.   3.]]
[[1 0 0]
 [0 0 1]
 [0 1 0]]
[[  0.  20.  10.]
 [  1.  21.  11.]
 [  2.  22.  12.]
 [  3.  23.  13.]]

In [26]:
print pts
pts = np.dot(pts, rot_efield_coord)
print pts


[[  0.  10.  20.]
 [  1.  11.  21.]
 [  2.  12.  22.]
 [  3.  13.  23.]]
[[ 10.  20.   0.]
 [ 11.  21.   1.]
 [ 12.  22.   2.]
 [ 13.  23.   3.]]

In [ ]: