Thursday, March 11, 2010

Python Polytope Perfect


I am a Common Lisp fan, so never bothered to look at Python in great detail until the need for polytope visulization and Anand's gentle prodding to try NetworkX and Ubigraph tempted me.

Python is so close to Lisp; it was almost like coming home. I picked up the syntax as I went along, and within 2 nights I had put together an interactive shell around the polytope explorer.

Example:

def CalculateHeritage( P, i ):
"""Given the polytope table and the polytope index find its heritage"""
answer = []
parentId = P[i].parentId
while i != parentId:
answer.append( (parentId, P[i].cut_set ) )
i = parentId
parentId = P[i].parentId
return answer

Anyone who has written lambda defuns, or docstrings can see the "inspiration", anyway who cares its good that they have implemented a good subset of Lisp.

Object-oriented programming is also there, ala, CLOS thrown on the existing language.

class Polytope:
"""Facets, Fvector, signature and parent information"""
def __init__(self):
self.facets = []
self.fvec = []
self.signature = ""
self.parent = 0
self.parendId = -1
self.cut_set = []
def dump(self):
print self.signature
print " "
print self.parent
print " "
print self.parentId

But the main advantage of using Python comes here:
def ParseFile( fileName ):
print "Reading file %s" %fileName
if polytope_file_name.find( '.gz' ) != -1:
file = gzip.open( fileName, 'rb' )
lines = file.readlines()
file.close()
else:
f = open( fileName, 'r' )
lines = f.readlines()
f.close()

Just using "import gzip" gave me the ability to compress my data set from 1 Gb to 65 Mb, and still retain the ability to use the data.

Using matplot and NetworkX I got the 4-pane visualizer; left top shows original
polytope, below it the induced cut-set, right top is the graph remainder and right
bottom is the produced polytope graph. You can iterate over the polytopes by specifying which vertices to truncate.

Anand thinks linear cuts will not increase the diameter and the interactive system has confirmed it so far, but the proof is worked on.

Summary: Python is like Lisp, I like Lisp => I like Python.

No comments:

Post a Comment