bindings/python/filtration.cpp
author Aravindakshan Babu <akshan@stanford.edu>
Thu, 08 Jul 2010 23:50:39 -0700
branchdev
changeset 217 64a272a34cb2
parent 181 1ee6edc17cb6
child 259 84c100980206
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/filtration.h>

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


#include "simplex.h"
#include "filtration.h"      // defines PythonFiltration
#include "utils.h"           // defines PythonCmp
namespace dp = dionysus::python;

boost::shared_ptr<dp::PythonFiltration>     init_from_iterator(bp::object iter, bp::object cmp)
{
    typedef     dp::PythonFiltration::Simplex   Smplx;
    boost::shared_ptr<dp::PythonFiltration>     p(new dp::PythonFiltration(bp::stl_input_iterator<Smplx>(iter), 
                                                                           bp::stl_input_iterator<Smplx>(),
                                                                           dp::PythonCmp(cmp)));
    return p;
}

void                                        filtration_sort(dp::PythonFiltration& f, bp::object cmp)
{ f.sort(dp::PythonCmp(cmp)); }

const dp::PythonFiltration::Simplex&        f_getitem(const dp::PythonFiltration& f, int i)
{ 
    if (i >= 0)
        return f.simplex(f.begin() + i); 
    else
        return f.simplex(f.end() + i);
}

unsigned                                    f_call(const dp::PythonFiltration& f, const dp::PythonFiltration::Simplex& s)
{ return f.find(s) - f.begin(); }


void export_filtration()
{
    bp::class_<dp::PythonFiltration>("Filtration")
        .def("__init__",        bp::make_constructor(&init_from_iterator))

        .def("append",          &dp::PythonFiltration::push_back)
        .def("sort",            &filtration_sort)

        .def("__getitem__",     &f_getitem,      bp::return_internal_reference<1>())
        .def("__call__",        &f_call)
        .def("__iter__",        bp::range<bp::return_internal_reference<1> >(&dp::PythonFiltration::begin, &dp::PythonFiltration::end))
        .def("__len__",         &dp::PythonFiltration::size)
    ;
}