UserPreferences

PyroModuleAnalysis


Pyro Module Analysis

This module is designed to give you some hints about performing analysis and creating informational graphs under Linux.

/!\ The dollar sign indicates commands that you enter at the terminal (shell) prompt.

Graphing

There are some graphical tool widgets in Pyro, but to do further analysis we will explore gnuplot. First we will explore using Gnuplot as a stand-alone tool, and then look at an interface to Gnuplot from Python.

Gnuplot

To start Gnuplot:

$ gnuplot

Once inside gnuplot, you can enter commands like:

set data style lines
plot [0:1] [2:4] "data1.txt", "data2.txt"

This will limit the x-coordinates to (0, 1) and y-coordinates between (2, 4) while plotting the two data files.

To create a Postscript file:

set output "file.ps"
set terminal postscript
plot

Control+d will exit. How can you find out more about Gnuplot? Inside gnuplot, type "help" or "help plot", "help set label", etc.

Clustering and Principal Component Analysis

Pyro comes with a cluster and PCA program in pyrobot/tools/cluster. Suppose we have the following files: vectors and names like so:

vectors:

1 0 1 0 1 0 1
1 1 1 1 0 0 0
0 0 0 1 1 1 0
0 1 0 1 0 0 0
0 0 0 0 0 0 0
0 1 0 0 0 0 0

names:

a
b
c
d
e
f

Then we could create a hierarchical tree plot using:

$ cluster vectors names

which produces:

read 6 patterns:  size = 7
minimum distance = 1.000000     ( f )   ( d )
minimum distance = 1.118034     ( d f ) ( e )
minimum distance = 1.598611     ( e d f )       ( b )
minimum distance = 1.713914     ( b e d f )     ( c )
minimum distance = 1.918333     ( c b e d f )   ( a )
Resulting Tree =
_|-----------------> a
 |_________________|---------------> c
                   |_______________|--------------> b
                                   |______________|----------> e
                                                  |__________|---------> d
                                                             |---------> f

However, that is a text-only representation of closeness. It would be nice to have one in graphical form.

$ cluster -g vectors names > data.plot

After creating the "data.plot" file, you can interactively plot it using the TreePlot class in Python:

from pyrobot.tools.cluster.treeplot import *
tp = TreePlot("data.plot")
tp.plot()
tp.hardcopy("output.ps")

And that produces:

http://emergent.brynmawr.edu/~dblank/images/tree.gif

/!\ Remember to check PyroSiteNotes to see if you need to do anything special at your Pyro site.

PCA

To transform a set of points to their Principal Component positions:

$ pca vector names > data.pca

where "data.pca" then contains:

1.8264127979082 0.37831857645255 0.463675663039161 0.552746506107956 -0.0238228008154108 -6.16383953506716e-17 1.30580010880393e-16 a
0.411878075426689 1.402210254595 1.34303124954713 0.178307556090468 0.169222726228872 1.57162575210046e-18 2.42607636492764e-17 b
0.225230537176817 -0.811240419055704 1.44908669490453 0.411036828068069 0.149521450754665 -4.95445435265815e-18 3.3031089173917e-16 c
-0.548645554704592 0.606620156078635 0.977704905302168 0.56760049750164 -0.230049635787567 2.93625689294866e-17 1.26208472304347e-19 d
0 0 0 0 0 0 0 e
-0.355465711371689 0.558022217077771 0.145766568772147 0.682192683282735 0.274991970047355 -1.33396527251454e-17 4.90637333377766e-17 f

If you want to save the Eigenvalues of this particular transformation, provide a -e filename:

$ pca -e eigen.file vector names

If the file doesn't exist, it will store the Eigenvalues there. If the file does exist, it will use those values to define the transform to vectors.

To plot "data.pca" from Python:

from pyrobot.tools.cluster.pcaplot import *
pca = PCAPlot("data.pca")
# or pca = PCAPlot("data.pca", "names") if names aren't in the data.pca file
pca.plot()
pca.hardcopy("output.ps")

http://emergent.brynmawr.edu/~dblank/images/pca.gif

/!\ Note that if you run a script that produces a gnuplot graph and exits, you might want to call python with the "-i" flag so that it keeps the plot open, and doesn't try to delete the temporary files that it creates.

Other pcaplot Options

Keyword Default Value Meaning Example
dimensions 2 2 or 3 dimensions to plot p = PCAPlot("pca.dat", dimensions = 3)
title None title of graph p = PCAPlot("pca.dat", title = "My Plot Title")
datatitle None Name of data p = PCAPlot("pca.dat", datatitle = "Experiment #3")
showpoints 1 Show points? p = PCAPlot("pca.dat", showpoints = 0)
showlabels 1 Show labels? p = PCAPlot("pca.dat", showlabels = 0)
components [0, 1, 2] Which components to plot. First is x axis, second is y. If doing a 3D plot, then third is z. p = PCAPlot("pca.dat", components = [2, 4]) # will plot 3rd and 5th (zero based)

How could you plot the 2nd and 3rd principal components?

Two answers. The first method uses the "components" keyword in PCAPlot:

pca = PCAPlot("pca.dat", components = [1, 2])

The second method is a method of using the Unix tool "cut". In creating the PCA file one would normally:

/usr/local/pyrobot/tools/cluster/pca vectors names > data.pca 

To strip the first column of numbers from that, do this instead:

pca vectors names > data.pca | cut -d" " -f2- > data.pca

where:

-d" "    means use the space as a delimeter
-f2-     means use fields 2 through the end of the fields

If you want to rearrange the components, or pick different ones:

-f2,4    the 2nd and the 4th
-f5,2    the 5th and the 2nd
-f2,3,1  the 2nd, the 3rd and the 1st (for 3D plots, see below)

3D PCA Plots

When you create a PCAPlot you can also add the "dimension" keyword:

pca = PCAPlot("data.pca", "names", title = "Sample PCA Plot", dimensions = 3)
pca.plot()

and you will see the first 3 principal components, rather than just the first two.

You can also rotate that plot interactively:

for view in range(35,70,5):
   pca.gp('set view 60, %d' % view)
   pca.gp.replot()
   time.sleep(1.0)

or directly:

pca.gp('set view 60,50')

Labels in Gnuplot

To put an artibtrary label on a gnuplot plot graph you use the syntax:

set label "My Label" at 10,20

where 10,20 is the X,Y coordinate and "My Label" is any text. You can enter that directly in gnuplot, or if using gnuplot from inside Python, something like:

pca = PCAPlot("data.pca", "names", title = "Sample PCA Plot")
pca.gp('set label "%s" at %f,%f' % ("My Label", 10, 20))
pca.plot()

The PCAPlot and TreePlot make calls to Gnuplot from Python. For more details on the interface see PyroGnuplot.

OpenOffice.org

OpenOffice also offers some nice charts and graphs.

Tracing Paths

If you are using the Stage simulator, there is a tool in Pyro to help in analyzing paths and other spatial data.

First, you will need an image file (gif, jpg, ppm, etc) of the Stage world. You can use the Linux command import like:

$ import world.gif

and then click in a corner of the Stage window and drag to the opposite corner. Let the mouse button up, and the import program will capture the image of the screen in the box you have outlined. If you mess up, just enter the command and try again.

Another way to create an image file of a Stage world is to use the Linux command xv, and then select the capture option.

Once you have an image file of the world, next you will need a data file. You can plot either:

x y theta

or

x y theta stateOrSymbol

Put the data in a file, such as path.dat, one data point per line. You can collect this data by attaching a truth device to the robot and then on every step you can use the getPose command to retrieve the robot's current position and heading. x and y are given in Stage coordinates: (0,0) is in the bottom of the left-hand corner, and going more positive as you go up and to the right, in meters. theta has 0 to the right, increasing counter clockwise, with 180 pointing to the left.

If you use -s 1, you can also use the additional 4th column which is a state number or a symbol string. The state number will be mapped to a symbol, a different one for each state. Or, if you use double quotes around it, it will appear as a literal label at that x,y position.

If you use -s 0, then you will not plot the labels (states or labels) but will place a dot and a line facing in the theta direction. If you adjust --length, you can get a good approximation of a line.

Note that when colorized (using the -c flag) the labels or lines will be colored as follows:

Flag Meaning
-c 0 no color, lines or labels appear in black
-c 1 lines or labels appear in red
-c A same labels are automatically mapped to same colors, for lines or labels

Now, you are ready to plot some data. You need only run:

$ python $PYROBOT/tools/trace.py DATAFILE IMAGEFILE

The IMAGEFILE is that which you captured above from the Stage simulator (above). The DATAFILE is that described above. There are two sample files that come with Pyro, so you can practice:

$ python $PYROBOT/tools/trace.py $PYROBOT/tools/sample-trace.dat $PYROBOT/tools/sample-trace-background.gif -o output.gif -c A

This will create a new image file called ouput.gif which combines the path data and the world image into one image. If you were to view this image using a program like xv, or if you add the -v flag, you would see the following:

http://bubo.brynmawr.edu/~dblank/images/sample-trace.gif

Here is the help for this program:

Help:
-c --color      [0|1|A] B&W, color, or Automatic
-s --symbols    [0|1] Show lines or symbols
-i --interval   <INT> Frequency data should be displayed
-l --length     <INT> Line width (use with -s 0)
-r --resolution <REAL> Value given in stage world file
-o --outfile    <FILENAME> Output filename (.gif, .jpg, .ppm)
-h --help       This help message
-w --window     Show data interactively in Tkinter graphics window
-v --view       Open xview after creating an output file

Pyro Modules Table of Contents

Modules

  1. PyroModuleIntroduction

  2. PyroModuleObjectOverview

  3. PyroModulePythonIntro

  4. PyroModuleDirectControl

  5. PyroModuleSequencingControl

  6. PyroModuleBehaviorBasedControl

  7. PyroModuleReinforcementLearning

  8. PyroModuleNeuralNetworks

  9. PyroModuleEvolutionaryAlgorithms

  10. PyroModuleComputerVision

  11. PyroModuleMapping

  12. PyroModuleMultirobot

  13. FurtherReading

Additional Resources

  1. PyroIndex

  2. PyroAdvancedTopics

  3. PyroUserManual

  4. [WWW]Pyro Tutorial Movies

Reference: PyroSiteNotes