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 (*).