Added code to expose the persistence_diagram class, the bottleneck_distance function and the point class to python. dev
authorAravindakshan Babu <akshan@stanford.edu>
Fri, 02 Jul 2010 04:19:29 -0700
branchdev
changeset 215 e94d521b72bb
parent 208 36ea2751f290
child 216 7aefb2d67203
Added code to expose the persistence_diagram class, the bottleneck_distance function and the point class to python. Most of the commonly used methods for each class have been exported. The constructor for point now requires that a data argument be provided along with x and y coord. This needs to be made optional. The constructor for persistence_diagram could possibly be rewritten as well.
bindings/python/CMakeLists.txt
bindings/python/dionysus.cpp
bindings/python/persistence-diagram.cpp
--- a/bindings/python/CMakeLists.txt	Tue Apr 20 13:58:23 2010 -0700
+++ b/bindings/python/CMakeLists.txt	Fri Jul 02 04:19:29 2010 -0700
@@ -10,6 +10,7 @@
                                                 filtration.cpp
                                                 chain.cpp
                                                 static-persistence.cpp
+                                                persistence-diagram.cpp
                                                 simplex.cpp
                                                 birthid.cpp
                                                 zigzag-persistence.cpp
--- a/bindings/python/dionysus.cpp	Tue Apr 20 13:58:23 2010 -0700
+++ b/bindings/python/dionysus.cpp	Fri Jul 02 04:19:29 2010 -0700
@@ -12,6 +12,8 @@
 void export_birthid();
 void export_zigzag_persistence();
 void export_cohomology_persistence();
+void export_point();
+void export_persistence_diagram();
 
 void export_rips();
 void export_pairwise_distances();
@@ -36,6 +38,8 @@
     export_filtration();
     export_static_persistence();
     export_chain();
+    export_point();
+    export_persistence_diagram();
 
     export_birthid();
     export_zigzag_persistence();
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/bindings/python/persistence-diagram.cpp	Fri Jul 02 04:19:29 2010 -0700
@@ -0,0 +1,90 @@
+#include<topology/persistence-diagram.h>
+#include<utilities/types.h>
+
+#include<boost/python.hpp>
+#include<boost/shared_ptr.hpp>
+#include<boost/python/stl_iterator.hpp>
+#include <boost/python/def.hpp>
+
+
+namespace bp = boost::python;
+
+
+namespace dionysus{
+namespace python{
+
+typedef bp::object Data;
+typedef PDPoint<Data> PointD;
+typedef PersistenceDiagram<Data> PersistenceDiagramD;
+
+} } //namespace dionysus::python
+
+namespace dp = dionysus::python;
+
+// TBD: Make the Data argument optional. Use bp::None ?
+template<class PDPoint>
+boost::shared_ptr< PDPoint >    init_from_coords( bp::object x_obj, bp::object y_obj, dp::Data d ){
+
+    RealType x = bp::extract<RealType>( x_obj ); RealType y = bp::extract<RealType>( y_obj );
+    boost::shared_ptr< PDPoint > p( new PDPoint( x, y, d ) );
+    return p;
+
+}
+
+template<class PDPoint>
+RealType    get_x_coord( const PDPoint& p ){
+    return p.x( );
+}
+
+template<class PDPoint>
+RealType    get_y_coord( const PDPoint& p ){
+    return p.y( );
+}
+
+template<class PDPoint>
+bp::object    get_data( const PDPoint& p ){
+    return p.data( );
+}
+
+void export_point( ){
+
+    bp::class_<dp::PointD>("Point")
+    .def( "__init__", bp::make_constructor( &init_from_coords<dp::PointD> ) )
+    .add_property( "x", &get_x_coord<dp::PointD> )
+    .add_property( "y", &get_y_coord<dp::PointD> )
+    .add_property( "data", &get_data<dp::PointD> )
+    .def( repr(bp::self) )
+    ;
+
+}
+
+
+
+template<class PersistenceDiagramD, class PointD>
+boost::shared_ptr<PersistenceDiagramD>    init_from_points_sequence( bp::object point_sequence ){
+
+    typedef bp::stl_input_iterator<PointD> PointDIterator;
+
+    PointDIterator beg = PointDIterator( point_sequence ), end = PointDIterator( );
+    // The following line is commented out until we can figure out the Evaluator class in make_point
+    //boost::shared_ptr<PersistenceDiagramD> p(  new PersistenceDiagramD( beg, end );
+    boost::shared_ptr<PersistenceDiagramD> p(  new PersistenceDiagramD( ) );
+
+    for( PointDIterator cur=beg;  cur!=end; cur++ )
+        (*p).push_back( *cur );
+    return p;
+
+}
+
+void export_persistence_diagram( ){
+
+    bp::class_<dp::PersistenceDiagramD>("PersistenceDiagram")
+    .def( "__init__", bp::make_constructor(  &init_from_points_sequence< dp::PersistenceDiagramD, dp::PointD > ) )
+    .def( "append", &dp::PersistenceDiagramD::push_back )
+    .add_property( "points", bp::range( &dp::PersistenceDiagramD::begin, &dp::PersistenceDiagramD::end ) )
+    .def( repr(bp::self) )
+    .def( "__sub__", &bottleneck_distance<dp::PersistenceDiagramD,dp::PersistenceDiagramD> )
+    ;
+
+    bp::def( "BottleneckDistance", bottleneck_distance<dp::PersistenceDiagramD,dp::PersistenceDiagramD> );
+}