Friday, December 28, 2007

Far Cry levels

After finishing far cry I was on the look out for more levels and mods. (Of course I'm drooling with anticipation for far cry 2). I found that Matto's mods/maps were amazing. I played Matto3 and had a lot of fun. Matto has injected a lot of wry humor into the scenery. Also, if you are the kind that investigates every corner of the map, you may receive unexpected "rewards" !

I found that filefront has a bunch of far cry mods and files and a terrible page layout. Here are the single player maps and mods. Always be careful with mods!

Now, I gotta try Matto 4!

Friday, December 21, 2007

undefined method `auto_complete_for'

From here we see that auto_complete_for has been implemented as a plugin for Rails 2.0. That was irritating, but relatively painless...

ruby script\plugin install auto_complete

Upgrading RRiki to Rails 2.0

Ok, in a moment of distraction I upgraded my Rriki setup to rails 2.0. Boom. It stopped working. Even the front page wouldn't work, giving me a 500 error. I looked through the log and found that it was some cookie thing and it was fixed by adding the line

config.action_controller.session = { :session_key => "_rriki_session", :secret => "some secret phrase of at least 30 characters" }

to the environment.rb file

I tried it again. Firefox would just not load, saying localhost was taking too long to respond. I went into panic mode trying out different things, untill I tried to load google and cnn. Neither worked. I restarted Firefox and got things back. But now I got a

undefined method `extract_options_from_args!'

error, even on the first page. I started to mess mess with my find syntax but decided that shouldn't be the problem. After looking at the error message closely I realised that even though I thought I had no strange (i.e. foreign, not written by me) code in my app that would potentially mess up when rails was updated, I did. I had paginate in my vendors folder. From this page I found that the fix to this is to replace the line

options = extract_options_from_args!(args)


options = args.extract_options!

Then I found out that pagination had been deprecated, nay, tossed out of 2.0.
So I got rid of the pagination plugin and write some elementary pagination into the controllers, so that NEXT time, its only MY code I'll have to worry about.

Yes, I LOVE upgrading, don't you :) ?

Inkscape, vista and windows explorer

On vista, I've had this problem that windows explorer will suddenly crash. It restarts itself, but I have to reopen all the folders I've had open. Other people have had this problem. One condition that causes the crash is if I write a new .svg file to a folder that is open.

This page suggests to turn off the DEP, but that looks risky to me.
This page suggests turning off search indexing, but my indexing is off.

Sunday, December 16, 2007

HTML tables

Table formatting tips from Finland.

Opensource note taking software

Jarnal allows you to write notes on top of a pdf background (you can also use it as just a note taker). If you have a tablet pc bully for you, other wise you can type in notes on top of the pdf. It would be neat if you could embed this in a browser, say to use it from within Rriki.

Notelab is meant for tablet pc users to take notes on a digital note pad.

Sunday, November 11, 2007

SVG with matlab

Sometimes you need to use MATLAB. There is however no reason your figures should suck. Use Juerg Schwizer's script for exporting figures into SVG! Then use inkscape!

Saturday, November 10, 2007

Blender saga Part III

I've abandoned blender for my 3D modeling and am using Alibre free edition, but I found this thread which may help other people interested in using blender to create models for three D printing

Wednesday, October 24, 2007

Beamer on MikTeX

Need xcolor and pgf for the new beamer to run. MikTeX kept failing on pgf install. Led to discovery of problem with MikTeX 2.6 and LZMA compressed package archive files. Had to update just the miktex-bin, then update the rest to get the latest package manager. Remember, don't have package manager and updater running at the same time - they access some same files and can lock each other out on windows.

MikTeX on vista

MikTeX works fine on vista, except on occasion if I don't run package manager as root I get a com surrogate error. I just start package manager as root (run as administrator) to get around this problem.

Monday, October 22, 2007


t = mlab.Title()
t.text = 'Blah'
t.text_actor.height = .1

#see all traits using

Saturday, October 20, 2007

Odd error with mlab and lighting - TVTK

Is there a time delay to the lighting manager being instantiated when a figure is set up?

In ipython the following lines work fine

fig = mlab.figure(browser=False)

But this doesn't

fig = mlab.figure(browser=False);fig.renwin.light_manager.lights[0].move_to(0,-45)

In the noninteractive mode (i.e. running a script as opposed to typing in commands in the shell) even the first version does not work. I tried inserting a delay (time.sleep()) but that does not help

l = light_manager.CameraLight(fig.renwin)

Figure properties - TVTK

fig.renwin.off_screen_rendering = True #allows you to minimize the window and keep drawing/saving to file

.light_mode = 'vtk' or 'raymond'

light[0] seems to be the important one. azimuth , elevation, with 0 elevation being right over head

The rendering properties (size etc.) are controlled from

.aa_frames =

.size = array([width, height]) OR
.set_size(width, height)

fig.renwin.show_axes = True #turns on the little axis orientation indicator

Some important properties - TVTK

(For how to access object properties see this post)

fig = mlab.figure(browser = False)
s = mlab.Surf(x,y,z)


.representation =
  • 'w' wireframe
  • 's' surface
.opacity = 0...1
.interpolation =
  • 'flat'
  • 'gouraud'
  • 'phong'
.ambient = 0...1 #uniform (direction independent) lighting on surface
.color = [r,g,b]
.specular = 0...1
.specular_color = [r,g,b]
.specular_power =0...1

Save figure to bitmap - TVTK'tada.png', size = (200,200)) 
#save the figure to png with stated size

view manipulation - TVTK

You have to manipulate the camera which is a
enthought.​pyface.​tvtk.​decorated_scene.​DecoratedScene object

if fig is a figure window handle returned from mlab.figure() rotate scene by .. degrees rotate scene by .. degrees zoom in/out

remember to call fig.renwin.render() to see the changes

Setting surface rendering properties - TVTK

From a hint here

import scipy

x = scipy.arange(-7., 7.05, 0.1)
y = scipy.arange(-5., 5.05, 0.1)

[X,Y] = scipy.meshgrid(x,y)
Z = 3.0*scipy.sin(X*Y+1e-4)/(X*Y+1e-4)

from import mlab
fig = mlab.figure(browser = False)
s = mlab.Surf(X, Y, Z)

act = fig.objects[1].actors[0] #object[1] is the surface, it has one actor, which turns out to be a surface = 'w' #make it a wireframe = 's' #turn it back to a surface!

Friday, October 19, 2007

Plotting irregularly spaced data - MATLAB griddata

From this site:

xmin = min(a(:,1));
ymin = min(a(:,2));
xmax = max(a(:,1));
ymax = max(a(:,2));
xi = xmin : 0.02*(xmax-xmin):xmax;
yi = ymin : 0.02*(ymax-ymin):ymax;
zi = griddata(a(:,1), a(:,2), a(:,3), xi', yi);
surf(xi, yi, zi)

You can do this in matplotlib too, but its not built in. See this post

Thursday, October 18, 2007

Getting mlab/ivtk/mayavi to work

  1. Download and run Enthought Enstaller.
    It pulls of various things from Enthought's website and installs them in the site-pacages etc. Crucially it will put enstaller.exe under \python25\Scripts
  2. Run enstaller.exe
  3. In the GUI tab to repositories and check Mavavi, VTK, enthought.traits, enthought.tvtk, wxPython
  4. click install packages
  5. change ipython to run with wthread commandline option (ipython -wthread)
  6. follow any number of examples from the cookbook for mlab

from import mlab
fig = mlab.figure(browser = False)
mlab.test_surf(fig) #a built in test function for a nice looking surface

t = mlab.Title()#add a title object
t.text = 'Ha'

fig.pop() #remove the last added object (in this case the title) ='tada.png') #save the figure to png

Useful links:

Tuesday, October 16, 2007

Memory leaks in matplotlib

On windows, with Python 2.5 matplotlib has a memory problem using the default tk backend. Say I have to generate 100 figures in succession, dumping each to a file because I want to create and animation from them. Using tk the memory consumption just keeps increasing. The trick is to use the agg backend for such tasks (see this post). The agg backend does not have this memory leak.

Sunday, October 14, 2007

Lucid dreams

I should try out reporting from dreams by training myself to dream lucidly. Hopefully my reports will be a little more interesting than this (xkcd).

Saturday, October 13, 2007

Archery at UMD

At the range today I ran into two fellas from the University of Maryland who are taking the initiative to start a Archery club at the university. About time !

Information about the group can be found at

A note on the bow stringer

The bow stringer I have came with instructions to step on the string with both feet about shoulder width apart. I didn't understand why, especially since some instructions elsewhere (and here) say to stand on the string with one foot. Today I think I figured out why, at least with my bowstringer.

The cup, which covers the upper tip, into which you are trying to slide the bowstring, has a tendency to cover the tip more and more closely, eventually pinching off the notch in the tip into which you need to slide the bowstring.

By standing with your feet apart, you cause the tension in the string to act more perpendicularly to the limb, and so the cup is less likely to slide inwards and cover the notch.

If you stand with only one foot on the stringer, or with feet close together, the string makes a shallow angle with the limb. This pulls the cup more tightly onto the tip, sliding it over the notch.

An additional benefit is that you are causing less tension in the bowstringer.

(Note that the bowstringers in the links do not have a cup that goes on the upper tip - they have a saddle, that you place over the limb. This difference in design probably accounts for the difference in optimum technique.)

Thursday, October 4, 2007

Ditching blender for Alibre free edition

Ok, either I am doing something wrong or Blender really has a problem with stl export. When I export my models to stl I can load the models just fine in Myriad 3D viewer , but when I load them in Zcorp's Zprint the normals are wrong and they show up as wire frames and not solid models. Hyunyoung from the fab-lab suggested I try out Alibre Design Xpress, which is a free version of their CAD software. I got it to work with minimum hassle. The only thing I could not figure out how to do was sketch elipses!

I needed the latest JVM

Saturday, September 29, 2007

Subplot with polar

If you want to do a polar plot in a subplot you will be surprised -
when you do subplot(1,2,1) (say) this sets up the axes in cartesian mode. You need to do

subplot(1,2,1,polar=True) to set the plot in polar mode

Wednesday, September 26, 2007

Monday, September 24, 2007

Setting up SVN repository on a linux machine

Refer to SVN manual at

What worked for me:

repositories created by
svnadmin create /path/to/repo/module

initial import via svn import module file:///path/to/repo/module -m "initial import"
make sure you have the following subdirectories in this initial folder that you import
trunk (all the code goes here)
tags (empty)
branches (empty)

local checkout:
svn checkout file:///path/to/repo/module/trunk module
Tortoise CVS checkout repository URL like:

User management for svn repositories
Initial setup:
groupadd svnusers #add a new group for svnusers
chgrp svnusers repo/ -R #repo is now under svnusers
chmod g=swrx repo/ -R #the directory tree is now writable by group

For each new svnuser with write access:
usermod -a -G svnusers newuser #add newuser to the svnusers group
id newuser #make sure it took

Rescind SVN write permission from a user
gpasswd -d newuser svnusers

Blender saga Part II

I used blender to make some 3D models that want to fabricate. When I went to do that today at the Device Realization lab, we found that the model was only displaying as a wireframe, with the faces not filled in. I checked with the free viewer from Myriad and the models seemed fine. I found this page that lists some free stl viewers.

Saturday, September 22, 2007

Why you should hold the bow with an open grip

Today, while shooting some ends with my new bow, I finally figured out the major reason why archers are told to shoot with an open grip. I read stuff about vibration and shock etc., but I think the real reason has to do with how the string is pulled. As you pull the string you need to pull it perpendicular to the plane of the bow. If you hold the bow in a tight grip you can potentially pull the string to one side, rather than straight back. This will impart a lateral component to your arrow release, causing a lot of fish-tailing. If you use an open grip, no matter how you pull the string the bow will yaw so that the limbs are perpendicular to the pull. This will reduce fish tailing.

Wednesday, September 19, 2007

Use feathers for shooting off the shelf

From what I read, the feathers are softer, so they collapse easily when they hit the shelf and affect arrow direction less.

Monday, September 17, 2007

Backstroke of the west OR Gained in translation

A pirated version of "The revenge of the sith" apparently gained something in translation. This website has a list of screenshots from a Star Wars III DVD bought in china with hilarious closed captioning.

The title itself "Revenge of the Sith" has the interesting translation "Backstroke of the west", leading me to all sorts of conspiracy theories of how in Totalitarian China, the evil empire in Star Wars is called "The West"

Saturday, September 15, 2007

Making a model using blender

Well written tutorials and documentation are found on the wiki.

Some tips:
  • Blender's top "menu bar" can be resized by dragging down, to reveal a preferences menu (Enable auto save on this menu, while you are at it)
  • BUG: Blender 2.44 on windows, boolean operation : union is actually difference and vice versa
  • Copying an object from one file to another (or even the same file). Remember: go into the 'OBJECT' subtree.
  • Export to STL: Remember: blender exports the currently selected object.

Keyboard short cuts:

tab - switch between object and edit mode
a - toggle between select all and select none
b - box select, select a bunch of vertices by dragging
e - extrude
g - grab
m - move object to layer dialog box
s - scale
w - boolean operation menu
shift-k - scissors

Friday, September 14, 2007

Bessel functions

From a hint here, we find that we need scipy.special

import scipy.special as ss

n = 2 #order of bessel function
x = 4 #evaluated at x = 4
y = ss.jn(n,x)

Wednesday, September 12, 2007

Web frameworks with python

I was writing a tutorial for SNAIU when I started to wonder what facilities Python has for writing browser based GUIs. I know that R has Rweb. I have had a great experience with Ruby On Rails, writing RRiki to handle my note taking and reference management needs. So I started to look into what Python has by way of web frameworks.

The python wiki has two relevant pages: web frameworks and web programming. I was attracted to Pylons, because it was the closest to Rails. But for my app what I need is something closer to Rweb, where you can create webpages with links that call actual code and return results on the webpage itself. A browser based GUI!

If anyone knows of any such solution, please tell me. Meanwhile, I think Pylon is the closest I can think off.

2007.11.1 - and then I discover SAGE. It looks like Rweb enhanced. Looks very promising.

Archery quick tips

Some factoids/tips I found useful (and which may not apply to you!)
  1. A toxophilite is a person who is devoted to archery
  2. A right hand bow is held in the left hand and shot by a right eye dominant person
  3. An adult taking up archery should start with a 30# or less bow
  4. For target archery a bow with 30-40# is sufficient
  5. For target archery 75gr points are sufficient
  6. A longer bow is more stable. For a medium height person a 62-66" bow is recommended
  7. Bow poundages are measured at 28" draw
  8. Drop-away arrow rests are meant for compound bows, not recurves, which use at most a plunger.
  9. Outdoor target archery ranges are from 30m to 90m

Resources for begining recurve archery

I took up archery with instruction books, rather than an instructor. Time will tell if that was smart. I read a lot of stuff from websites and some from books. In the UMD library I could not find an archery book more recent than some time in the 1970s ! I guess UMD is not big into archery!

I read the following books:

"Shooting the Bow", Larry C. Whiffen, The Bruce Publishing Co, Milwaukee 1946.
This is an instructional book with charming mid-20th century black and white pictures written in a charming and humorous mid-20th century style. It has hand illustrations for stance, grip etc. It has a one page chapter about correcting errors.

"Archery", Lorraine Pszczola, W.B. Saunders Co, 1976
This is meant as an instructional booklet for college/school archery programmes. Filled with illustrations about stance, and grip etc.

"Archery for beginners", John C. Williams, Contemporary Books, 1976
This was the one I referred to the most. The archers in bellbottoms are a hoot. It also uses the words "sexy" and "groove". A true 70s book. It has some good advice about correcting errors.

My favorite book of these three is Williams' book because the advice on stance, grip and release are very well described and illustrated with photographs. Williams also explains the mechanics behind it all, which makes his writing more aesthetically pleasing foe me.

It helped me to read several books because there were differences in the material and advice, and I picked out those things that struck me as useful, or that helped me. There was contradictory advice about stance and grip, which illustrated to me that there is not ONE way to shoot an arrow!

Mostly it was the illustration and pictures that helped me from the books. A lot of text about archery is available on the web. Unfortunately a lot of the websites don't have pictures, so that's where you have to refer to the books.

Some websites that I find useful are:

Oh, and here are some things I found useful.

Sunday, September 9, 2007

Regular expressions in SciTE

From the SciTE's documentation webpage, you need to check 'Regular expression' in the search dialog box, and then the regex parts of your search will be demarcated by \( and \). For example, to replace all occurences of AUTO_INCREMENT=??? where ??? is some number by nothing, you would set the Find box to be AUTO_INCREMENT=\([0-9]+\) and the Replace box would be untouched.

Thursday, September 6, 2007


I took up archery recently, and I have really gotten into it. I've added some archery related links on the blog.

I started out cheap, since I was interested in it enough to want to buy equipment, but I didn't know if it would be a hobby that would stick.

No-brand fibreglass recurve bow 60", 30-35# pull at 28" -- $50 (esportsonline)

Shooting glove -- $10
Quiver -- $10
Arm guard -- $10
6 carbon express thunderstorm arrows -- $34
Points 75gr and 125gr - $12 total - (all from Basspro)

Total approx $125

The closest basspro shop did not carry cheap recurves (they were all selfbows, and looked very nice and something you would hang over your fireplace and cost something like $500). They carried a lot of compounds, and a few recurves for children (54" high and 9lbs draw). I found more stuff online.

Basspro is fine for most things and esportsonline service is fine.

The no-name recurve bow I got is an OK starter. It's a generic kind of ambidextrous bow that you are likely to find in a number of places. The arrows I got have spines for bows from 30-50#. They are mangled by the bow. The fletching brushes past the riser (Its NOT centershot, because of the ambidextrous design) and two vanes have picked up a black coat from the riser. Those vanes have also been damaged a bit by this brush past.
Other than this, I would say the bow is OK. My shots arn't consistent, and I can't say if that if my form or the bows fault.

If anyone is looking for a decent starter bow I would recommend looking for a center shot one.

Thursday, August 23, 2007

GUIs are evil

Well, not really. Like most things a GUI is neither good nor evil, but a shade of something in between. However, for a while, before I caught myself, I was spending large amounts of time writing GUIs for analysis. Time that would have been better spent writing analysis code and debugging it and documenting it and writing papers as a result of it. I taped a piece of paper in front of my desk that said 'NO GUIs' to remind myself.

Programming a GUIs is a time trade. Creating a GUI trades time now for time later. The catch is that the time later usually goes to an 'end user' rather than you.

When to write a GUI:
  • A GUI for MATLAB or Python is good for code that will be repeatedly used to run analysis by people who have no interest in knowing the underlying code.
  • A GUI is good for tasks that have a strong visual component: this means analysis that is done by displaying data on screen on which you have to indicate things by clicking on them.
  • Your analysis code is all finished and documented and you are regularly running analysis with it and you are getting tired of some repeated typing or repeated actions. Again, first think if you could write a glue script (in Python for example) rather than a GUI.
When to resist the devil's call:
  • The code is for exploratory analysis that may or may not yield results.
    "But I need to see stuff graphically, and I need to click on things to select regions or points", you say. Yes, MATLAB has ginput (look it up) and matplotlib has connect that can connect mouse and keyboard events to a graph. Don't make a full blown GUI that also makes coffee. That is the dark side.
  • The code has no graphical component to it (besides loading data and creating graphs).
  • Your sketch of the GUI has nothing but buttons and text boxes with parameters: replace the GUI with a nice, commented parameter file where the parameters have names. With the time saved write more documentation, take your spouse/SO/friends out or play video games.
  • You are coding the GUI before you have coded the smaller programs the GUI will eventually call. That is the dark side. Write the analysis components first, then decide if it will need a GUI package.
Also see:

Tuesday, August 14, 2007

Plotting packages for python

No time to check them all out now.

Loading matlab binary (.mat) files into python

It happens. You have old data in matlab .mat format or your colleagues insist, for some obscure reason, on using MATLAB. You need and from scipy.

X ='mydata.mat')

X now contains a dictionary whose keys correspond to the variable names saved in the original mat file.

Nested dictionaries seem to be a problem. A kind soul has a short set of methods that converts nested dictionaries here.


Look here (scipy cookbook) for how to handle mat files from recent versions of matlab

Sunday, August 12, 2007

Wikipedia vandalism

I have one data point for how long an act of vandalism stays on wikipedia:

On the page about fictional computers, at 17:23, 17 July 2007 a user from IP added vandalism to the Before 1950s section of the article. It has been there until I removed it on August 12 2007.

So, this particular piece of vandalism lasted 27 days.

Sunday, August 5, 2007

Gravity Pods

I was looking for a 'game' just like this!

Set your initial trajectory so that the projectile winds is way to the target following a iso-potential line. Adjust sources and 'sinks' of gravity to adjust the iso-potential lines so that your projectile hits the target.

Betch'a didn't even think of iso-potential lines as you were busily setting up that rube-goldberg contraption!

Friday, August 3, 2007

Importing pdfs to inkscape

Install ghostscript and gsview. Load the pdf with gsview, goto edit->convert to vector format and choose svg.

Sunday, July 29, 2007


Bloxors is a great puzzle game. Play it between neural recordings, behavioral training sessions, when you take a break from writing that paper and when the reviewers make you mad.

The fun in bloxors comes from the fact that your footprint (how many floor blocks you block occupies) varies with the moves you make. You start 'standing up' and must end, standing up, over the gap, so you fall in.

Step -1
You must aim, therefore, to be 'lying down' next to the gap, with your footprint in one of the four cardinal squares (North, South, East or West), radially, such as:


Step -2
Work backward from such configurations in terms of rolling or tumbling, till you get to your current starting point.

This puzzle has enough degrees of freedom and enough constraints to make it fun.

The teleporter adds some spice, though I think it is an unnecessary complication.

My Codes:

Tuesday, July 17, 2007

Making a poster using Inkscape

Inkscape turned out to be great for making my Vancouver poster. The following things were not so great
  1. Export to pdf is bloated. My poster on export came to 303 MB(!). Using the 'reduce file size' option in acrobat resulted in a 2.3 MB file. Obviously Inkscape is doing something screwy here.
  2. Export to pdf does not honor image clipping. I was using rectangles to mark out a part of an image (an equation screen shot from latex pdf output) and the clip the image. The clipping worked fine in SVG, but when exported to pdf the entire figure was visible, not just the clipped part.
  3. Obviously, I was doing this because Inkscape does not have good equation support. There are two plugins that use pstoedit to convert latex dvi output to svg, but I could not get it to work on my windows machine.
I just learnt about Scribus, and wonder if that would be a better poster making tool

Opensource Workflow

My efforts to shift my work flow to rely more on open source tools has been going relatively smoothly. My current work flow is
  1. Create figures using python and pylab (matplotlib)
  2. Save the figures in svg format from pylab
  3. Import the svg figures into inkscape and annotate them (eqtexsvg to insert latex equations)
  4. Save the figures as pdf
  5. Include them in my latex source and compile with MikTeX
I'm currently using inkscape to make my Vancouver poster. Its better than powerpoint, but I have problems with text formatting e.g. bullets

Saturday, July 14, 2007

Boost Python

I just discovered boost for Python: a wrapper for C code for Python. It looks simple to use, and seems to have good documentation.

Friday, July 13, 2007

Using PCA in MDP

I found mdp on sourceforge. I needed to do PCA for some of my simulations. The mdp.pca function is easy to handle. The input is a m x n matrix, n is the dimension of the space, and m are the observations (rows = observations, cols = dimensions. The doc string is confused as to this point)

The output is a matrix also m x n, but the vectors have been transformed, so that the first column is along the direction with greatest variance etc.

An example is below:

import pylab as m
import matplotlib.axes3d as m3
import mdp

x1 = m.rand(10,3)
x2 = m.rand(10,3) + m.array([[2.,-2.,0.]])
x = m.concatenate((x1,x2))
y = mdp.pca(x)

fig = m.figure()
ax = m3.Axes3D(fig)
ax.scatter3D(x[:10,0].squeeze().T, x[:10,1].squeeze().T, x[:10,2].squeeze().T, marker = '^')
ax.scatter3D(x[10:,0].squeeze().T, x[10:,1].squeeze().T, x[10:,2].squeeze().T, marker = 'o')
m.axes([.7, .8, .2, .2])
m.plot(y[:10,0], y[:10,1],'k^')
m.plot(y[10:,0], y[10:,1],'b.')
m.title('2 D')

m.axes([.7, .6, .2, .2])
m.title('1 D')

Thursday, July 12, 2007

Iterations (i.e. for loops)

xrange is more efficient than range

for z in [0,1,2,3]:
print z

x = range(10)
for z in x:
print z

x = range(1,22,5)
for z in x:
print z

--> 1 6 11 16 21

for q in xrange(100000):
print q

Wednesday, July 11, 2007

Custom colormaps

From an excellent post here , we have a clear explanation of how to make custom colormaps. Under matplotlib there is a colors module which has a method called LinearSegmentedColormap that can create a colormap for your use.

First you need to make a color dictionary which looks like this:
cdict = {
'red' : ((0., 0., 0.), (0.5, 0.25, 0.25), (1., 1., 1.)),
'green': ((0., 1., 1.), (0.7, 0.0, 0.5), (1., 1., 1.)),
'blue' : ((0., 1., 1.), (0.5, 0.0, 0.0), (1., 1., 1.))
For each 'primary' additive color (rgb) you have to specify a tuple (...) which inturn is a collection of triples. Each triple defines a pivot point for that color in your color map.

The triple (0.7, 0.0, 0.5) for instance, means for 'green' when the normalized value is just below 0.7 the green component should be 0.0, and just above 0.7 the green component should be 0.5. This of course creates a discontinuity in the map at 0.7. To make a continuous map all the pivot points should have the same value above and below. So the triple (0.5, 0.25, 0.25) for 'red' means for values just above and below .5 the 'red' component will be .25 .

Values in between pivot points are linearly interpolated by the function matplotlib.colors.LinearSegmentedColormap.

A complete (and ugly) example is given below:

import pylab as m

cdict = {
'red' : ((0., 0., 0.), (0.5, 0.25, 0.25), (1., 1., 1.)),
'green': ((0., 1., 1.), (0.7, 0.0, 0.5), (1., 1., 1.)),
'blue' : ((0., 1., 1.), (0.5, 0.0, 0.0), (1., 1., 1.))
#generate the colormap with 1024 interpolated values
my_cmap = m.matplotlib.colors.LinearSegmentedColormap('my_colormap', cdict, 1024)

#create a gaussian
x = m.arange(0, 50 , 1 , m.Float)
y = x[:,m.NewAxis]
x0 = y0 = x.size // 2
fwhm= x0/1.2
z = m.exp(-4*m.log(2)*((x-x0)**2+(y-y0)**2)/fwhm**2)

pcolormesh(z, cmap = my_cmap)
The api is more for reference, rather than learning.

Tuesday, July 10, 2007

Pretty picture

String formatting

String formatting is weird in Python, but rumor has it that Python 3000 will break backwards compatibility and fix that. Currently (Python 2.5) you do this
x = 'Rabindranath Tagore'
y = 1861
z = 80.25
print '%s was born in %d and lived %2.2f years' %(x,y,z)
Weird, ain't it?

Incidentally, python has no 'num2str' like matlab, so this is how you do it

Initializing arrays in pylab (numpy)

When you initialize an array say using zeros or ones or the array constructor remember that they default to integer type. So if you plan on doing floating point operations always set the type explicitly like
x = zeros(10,'f')
y = array([],'f')

Latex and SVG export

Pylab will render LaTeX equations (yeehah!). But there are glitches when the figures are saved to svg. First, you have to take the BaKoMa fonts (TrueType version of the Computer Modern fonts) located in the "matplotlib/mpl-data" dir (the cm*.ttf files) and install them. Now you can see the glyphs. However, the subscripts and superscripts are messed up (inverted). Which can be fixed in Inkscape, but eats time.

Thanks to Edin Salkovic on the [Matplotlib-users] list

Sorting several arrays locked to one array

Use argsort:
x = [1.,2.,3.]
y = [3.,2.,1.]
print x[y.argsort()]

Iterating through multiple lists/arrays at the same time

Use zip
x = [1,2,3,4]
y = ['a','b','c']
for z in zip(x,y):
print z
(1, 'a')
(2, 'b')
(3, 'c')

Figures : log scale

plot([1, 1e1, 1e2], [1, 1e1, 1e2])
ax = gca()

Figures : plot without showing

Now why would you want to do that? Well, you could want to run a script on a remote machine from a (non graphical) terminal and just save the figure to a file, which you then retrieve. In my case I was making animations of simulations and wanted to do the animation on a remote machine, stitch the images together using mencoder and just retrieve the resultant movie file. At the top of the file add the lines

import matplotlib as mp

This causes the Agg backend to be used.

Thanks to Christopher Barker of NOAA for this.

Indexing or 'Slicing'

In Python, indexing is called slicing. It took me a while to catch on. Slicing an array is the same as MATLAB, except indexes are C indexes (start at 0). index -1 is last element, -2 penultimate etc.


In MATLAB when ever I had to make arrays of inhomogeneous data I would use arrays of structs. In Python 'Lists' can be used for that. Lists are indexed containers for any mixture of data. So for instance, I need to store event times for a series of nodes. The nodes go from 0..N-1 and each node has an array of event times (that can also be a zero length array) In Python we initialize this list of lists as

ras = [[]]*N #empty list with N elements
ras[9] = [2, 4] #10th node now has two events 2 and 4
ras[9].append(4) #and now a third event 4

Reload of module after modification

If you change the code to a module, unlike MATLAB, Python will not use the new code. Even typing 'import' again will not refresh the code. You need to do 'reload(module)'

If you have a module that loads other modules and you are in the interactive session you will need to either put in 'reload's in the importing module or import and then reload it manually in the interactive session

Class variables, instance variables, local variables

Try out the following pieces of code

#Instance variable
class A:
def __init__(self):
self.x = 22

a = A()
print a.x

#Local variable, only accessible inside init and vanishes afterwards
class A:

def __init__(self):
x = 22

a = A()
print a.x

#x is an instance variable
class A:

x = 22
def __init__(self):
return None

a = A()
b = A()
a.x = 15
print a.x, b.x

Objects, dictionaries, shallow and deep copies

objects have their attributes stored in a dictionary accessible by doing objectname.__dict__ use copy module to make deepcopies of objects copy.deepcopy()

Also see .

Index of max,min element of an array

To find the index of the maximum or minimum of an array

x = scipy.array([1,2,3,4,50,6])




Multiple statements on one line

Use semicolons. Like
if num > 99: print "gaga"; x =22; y=34 ; print x + y

for n in range(5): print str(n) ; print str(n**2)

Split statements across multiple lines

Use backslashes '\'. Like

a = \
22 + \

You only need backslashes for multi-line statments that could have been interpreted as a sequence of single line statements. So if you are entering a list of arguments to a function you can break it across multiple lines without back slashes because Python expects an argument list to end with a close parentheses.

Equivalent of switch statements

There is no native switch statement in Python. However the following construct does the job (based on code here:

result = {
'case a': function1,
'case b': function2,
'case c': function3

This is a code dictionary that matches 'value' with the keys and then executes (returns) the respective functions.

Another way is to use a sequence of if and elif statements

New style classes and slots

By typing in 'object' in a class declaration (see below) we create a new style class. (In Python 2.5 default classes are old-style objects). My use for new-style classes was the __slots__ feature. Python normally uses a dictionary __dict__ to store the available attributes of a class. Therefore an object's footprint in memory is the memory required to store the attributes plus memory for the lookup dictionary. In a new-style class we can instruct Python to not use the dictionary, and reserve slots for attributes in the declaration. This reduces memory use by getting rid of __dict__, and is useful if you have large numbers of a class.

class Synapse(object):
"""This stores the parameter data for the synapses as well as routines for file I/O of the parameters"""
__slots__ = ('id','sourceneuron','g_max','E','alpha','beta','T_max','t_pulse')

def __init__(self): = 'A_synapse'
self.sourceneuron = 0 #dummy
self.g_max = 5
self.E = 2 # -0.33 for inhibition
self.alpha = 2.
self.beta = 1.
self.T_max = 1.
self.t_pulse = 1. #for how long does the synaptic drive increase

Essential software

  • Pylab - Nicotine patch for MATLAB junkies. Plot data better than in matlab. Save to SVG. Annotate in Inkscape. Publish in paper.
  • winpdb - Python's weak point is debugging. This helps a bit
  • ipython - Good old interactive command line. With autocompletion! (hit tab) Don't forget to download pyreadline too
  • pygtk - For GUIs. Haven't used it much, but it is nice.

In the begining...

As I was coding in python for my simulation project I tried to maintain a document of what I learned first as a text file, then as a html file. It all got too messy and cumbersome. I needed to organize each snippet of information for quick retrieval. I needed a database. But I was too lazy to setup a database. Then I thought, why not start a blog about it? So here we are. To be precise, we are here (*).