Skip to main content

Posts

Showing posts from July, 2007

Bloxors

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.

Strategy
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:


or


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:
189493
499707
074355
3005590
291709
958640
448106

Making a poster using Inkscape

Inkscape turned out to be great for making my Vancouver poster. The following things were not so great
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.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.
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
Create figures using python and pylab (matplotlib)Save the figures in svg format from pylabImport the svg figures into inkscape and annotate them (eqtexsvg to insert latex equations)Save the figures as pdfInclude 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

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.axis('scaled')
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.axis('scaled')

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…

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

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

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 linesimport matplotlib as mp
mp.use("Agg") 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.

Lists

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

Split statements across multiple lines

Use backslashes '\'. Like a = \
22 + \
99 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
}[value](parameters) 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):

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 bitipython - Good old interactive command line. With autocompletion! (hit tab) Don't forget to download pyreadline toopygtk - 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 (*).