bindings/python/static-persistence.cpp
author Aravindakshan Babu <akshan@stanford.edu>
Thu, 08 Jul 2010 23:50:39 -0700
branchdev
changeset 217 64a272a34cb2
parent 193 7198c9bf279d
child 232 edbbf84fde9e
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 ).

#include <topology/static-persistence.h>

#include <boost/python.hpp>
#include <boost/python/iterator.hpp>
#include <boost/python/return_internal_reference.hpp>
namespace bp = boost::python;

#include "filtration.h"
#include "static-persistence.h"
#include "chain.h"
namespace dp = dionysus::python;


/* SPersistence */
boost::shared_ptr<dp::SPersistence>     init_from_filtration(bp::object f)
{
    dp::PythonFiltration& sf = bp::extract<dp::PythonFiltration&>(f);
    boost::shared_ptr<dp::SPersistence> p(new dp::SPersistence(sf));
    return p;
}

void                                    pair_simplices(dp::SPersistence& sp)                { sp.pair_simplices(false); }

unsigned                                distance(dp::SPersistence& sp, 
                                                 const dp::SPersistenceIndex& i)            { return sp.iterator_to(i) - sp.begin(); }

/* SPNode */
const dp::SPersistenceNode&             pair(const dp::SPersistenceNode& sn)                { return *sn.pair; }

/* PersistenceSimplexMap */
const dp::SimplexVD&                    psmap_getitem(const dp::PersistenceSimplexMap& psmap, 
                                                      const dp::SPersistenceIndex& i)       { return psmap[i]; }

void export_static_persistence()
{
    bp::class_<dp::SPersistenceNode>("SPNode", bp::no_init)
        .def("pair",            &pair,                      bp::return_internal_reference<1>())
        .add_property("cycle",  &dp::SPersistenceNode::cycle)
        .def("sign",            &dp::SPersistenceNode::sign)
        .def("unpaired",        &dp::SPersistenceNode::unpaired)
    ;

    bp::class_<dp::SPersistence>("StaticPersistence", bp::no_init)
        .def("__init__",        bp::make_constructor(&init_from_filtration))
        
        .def("pair_simplices",  &pair_simplices)
        .def("__call__",        &distance)
        .def("make_simplex_map",&dp::SPersistence::make_simplex_map<dp::PythonFiltration>)

        .def("__iter__",        bp::range<bp::return_internal_reference<1> >(&dp::SPersistence::begin, &dp::SPersistence::end))
        .def("__len__",         &dp::SPersistence::size)
    ;

    bp::class_<dp::PersistenceSimplexMap>("PersistenceSimplexMap", bp::no_init)
        .def("__getitem__",     &psmap_getitem,             bp::return_internal_reference<1>())
    ;
}