bindings/python/dionysus/distances.py
author Aravindakshan Babu <akshan@stanford.edu>
Thu, 08 Jul 2010 23:50:39 -0700
branchdev
changeset 217 64a272a34cb2
parent 145 ee096f207dfb
permissions -rw-r--r--
Added extra functionality to Point class( an iterator ) and PersistenceDiagram( dimension property and __len__ func ). persistence-diagram.h: Added a new read-only dimension member and member function to access it. With a new constructor that that takes in an int type to initialize dimension. persistence-diagram.cpp: Added new bp::init constructor. Takes in an integer type to initialize the dimension. Exposed the dimension property. Exposed the size property via a __len__ method. __init__.py: Added an iterator for Point objects. This iterates over the coords and then the data( if present ).

from    math        import sqrt

def l2(p):
    return sqrt(sum((x**2 for x in p)))

# Pairwise distances between the elements of `points` with respect to some `norm`
class PairwiseDistances:
    def __init__(self, points, norm = l2):
        self.points = points
        self.norm = norm

    def __len__(self):
        return len(self.points)

    def __call__(self, p1, p2):
        return self.norm((x - y for (x,y) in zip(self.points[p1], self.points[p2])))

# Caches all distances specified by `distances`
class ExplicitDistances:
    def __init__(self, distances):
        self.len = len(distances)
        self.distances = []
        for i in xrange(self.len): 
            self.distances.append([])
            for j in xrange(self.len):
                self.distances[-1].append(distances(i,j))

    def __len__(self):
        return self.len

    def __call__(self, p1, p2):
        return self.distances[p1][p2]

# Generator of all points in a file `filename` with one point per line
def points_file(filename):
    fd = open(filename)
    for line in fd.xreadlines():
        if line.startswith('#'): continue
        yield map(float, line.strip().split())
    fd.close()