bindings/python/dionysus/__init__.py
author Aravindakshan Babu <akshan@stanford.edu>
Thu, 08 Jul 2010 23:50:39 -0700
branchdev
changeset 217 64a272a34cb2
parent 181 1ee6edc17cb6
child 218 4f70a473e34b
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    _dionysus   import *
from    distances   import l2, ExplicitDistances, points_file
from    zigzag      import *


def init_with_none(self, iter, data = None):        # convenience: data defaults to None
    self._cpp_init_(iter, data)

def repr_with_data(self):
    str = self._cpp_repr_()
    if type(self.data) == float:
        str += ' %f' % self.data
    return str

Simplex._cpp_init_ =    Simplex.__init__
Simplex.__init__   =    init_with_none
Simplex._cpp_repr_ =    Simplex.__repr__
Simplex.__repr__   =    repr_with_data


def data_cmp(s1, s2):
    return cmp(s1.data, s2.data)

def data_dim_cmp(s1,s2):
    return cmp(s1.dimension(), s2.dimension()) or data_cmp(s1,s2)

def vertex_dim_cmp(s1, s2):
    return cmp(s1.dimension(), s2.dimension()) or vertex_cmp(s1, s2)

# TBD: Port this into C++
def point_iterator( point ):

    yield point.x
    yield point.y
    if not point.data is None:
        yield point.data

Point.__iter__ = point_iterator