Cleaned up PersistenceDiagram bindings dev
authorDmitriy Morozov <dmitriy@mrzv.org>
Sat Oct 09 13:56:26 2010 -0700 (19 months ago)
branchdev
changeset 236e90653a59fa6
parent 23507b3070cea74
child 23792b59e2e6fb3
Cleaned up PersistenceDiagram bindings
bindings/python/persistence-diagram.cpp
include/topology/persistence-diagram.h
       1 --- a/bindings/python/persistence-diagram.cpp	Mon Oct 04 18:03:35 2010 -0700
       2 +++ b/bindings/python/persistence-diagram.cpp	Sat Oct 09 13:56:26 2010 -0700
       3 @@ -5,7 +5,7 @@
       4  #include<boost/python/init.hpp>
       5  #include<boost/shared_ptr.hpp>
       6  #include<boost/python/stl_iterator.hpp>
       7 -#include <boost/python/def.hpp>
       8 +#include<boost/python/def.hpp>
       9  
      10  
      11  namespace bp = boost::python;
      12 @@ -22,74 +22,52 @@
      13  
      14  namespace dp = dionysus::python;
      15  
      16 +// Easy way out of overloads
      17 +RealType        get_x (const dp::PointD& p)         { return p.x(); }
      18 +RealType        get_y (const dp::PointD& p)         { return p.y(); }
      19 +bp::object      get_data(const dp::PointD& p)       { return p.data(); }
      20  
      21 -template<class PDPoint>
      22 -RealType    get_x_coord( const PDPoint& p ){
      23 -    return p.x( );
      24 +void export_point( )
      25 +{
      26 +    bp::class_<dp::PointD>("Point")
      27 +        .def(                   bp::init<RealType, RealType, bp::optional<dp::Data> >())
      28 +        .add_property("x",      &get_x)
      29 +        .add_property("y",      &get_y)
      30 +        .add_property("data",   &get_data)
      31 +        .def(                   repr(bp::self))
      32 +    ;
      33  }
      34  
      35 -template<class PDPoint>
      36 -RealType    get_y_coord( const PDPoint& p ){
      37 -    return p.y( );
      38 -}
      39  
      40 -template<class PDPoint>
      41 -bp::object    get_data( const PDPoint& p ){
      42 -    return p.data( );
      43 -}
      44 +boost::shared_ptr<dp::PersistenceDiagramD>      init_from_points_sequence(Dimension dimension, bp::object point_sequence)
      45 +{
      46 +    typedef     bp::stl_input_iterator<dp::PointD>  PointIterator;
      47  
      48 -void export_point( ){
      49 +    PointIterator beg = PointIterator(point_sequence), end = PointIterator();
      50 +    boost::shared_ptr<dp::PersistenceDiagramD> p(new dp::PersistenceDiagramD(dimension));
      51  
      52 -    bp::class_<dp::PointD>("Point")
      53 -    .def( bp::init< RealType, RealType, bp::optional<dp::Data>  >( ) )
      54 -    .add_property( "x", &get_x_coord<dp::PointD> )
      55 -    .add_property( "y", &get_y_coord<dp::PointD> )
      56 -    .add_property( "data", &get_data<dp::PointD> )
      57 -    .def( repr(bp::self) )
      58 -    ;
      59 -
      60 -}
      61 -
      62 -
      63 -
      64 -template<class PersistenceDiagram, class Point>
      65 -boost::shared_ptr<PersistenceDiagram>    init_from_points_sequence( Dimension dimension, bp::object point_sequence ){
      66 -
      67 -    typedef bp::stl_input_iterator<Point> PointIterator;
      68 -
      69 -    PointIterator beg = PointIterator( point_sequence ), end = PointIterator( );
      70 -    // The following line is commented out until we can figure out the Evaluator class in make_point
      71 -    //boost::shared_ptr<PersistenceDiagram> p(  new PersistenceDiagram( beg, end );
      72 -    boost::shared_ptr<PersistenceDiagram> p(  new PersistenceDiagram( dimension ) );
      73 -
      74 -    for( PointIterator cur=beg;  cur!=end; cur++ )
      75 -        (*p).push_back( *cur );
      76 +    for(PointIterator cur = beg; cur != end; cur++)
      77 +        (*p).push_back(*cur);
      78      return p;
      79  
      80  }
      81  
      82 +RealType    bottleneck_distnace_adapter(const dp::PersistenceDiagramD& dgm1, const dp::PersistenceDiagramD& dgm2)
      83 +{
      84 +    return bottleneck_distance(dgm1, dgm2);
      85 +}    
      86  
      87 -template<class PersistenceDiagram>
      88 -Dimension get_dimension( PersistenceDiagram dgm ){
      89 -    return dgm.dimension( );
      90 -}
      91 -
      92 -template<class PersistenceDiagram>
      93 -SizeType get_length( PersistenceDiagram dgm ){
      94 -    return dgm.size( );
      95 -}
      96 -
      97 -void export_persistence_diagram( ){
      98 -
      99 +void export_persistence_diagram()
     100 +{
     101      bp::class_<dp::PersistenceDiagramD>("PersistenceDiagram")
     102 -    .def( "__init__", bp::make_constructor(  &init_from_points_sequence< dp::PersistenceDiagramD, dp::PointD > ) )
     103 -    .def( bp::init< Dimension >( ) )
     104 -    .def( "append", &dp::PersistenceDiagramD::push_back )
     105 -    .add_property( "dimension", &get_dimension<dp::PersistenceDiagramD> )
     106 -    .def( repr(bp::self) )
     107 -    .def( "__iter__", bp::range( &dp::PersistenceDiagramD::begin, &dp::PersistenceDiagramD::end ) )
     108 -    .def( "__len__", &get_length<dp::PersistenceDiagramD> )
     109 +        .def("__init__",            bp::make_constructor(&init_from_points_sequence))
     110 +        .def(                       bp::init<Dimension>())
     111 +        .def("append",              &dp::PersistenceDiagramD::push_back)
     112 +        .add_property("dimension",  &dp::PersistenceDiagramD::dimension)
     113 +        .def(                       repr(bp::self))
     114 +        .def("__iter__",            bp::range(&dp::PersistenceDiagramD::begin, &dp::PersistenceDiagramD::end))
     115 +        .def("__len__",             &dp::PersistenceDiagramD::size)
     116      ;
     117  
     118 -    bp::def( "bottleneck_distance", bottleneck_distance<dp::PersistenceDiagramD,dp::PersistenceDiagramD> );
     119 +    bp::def("bottleneck_distance",  &bottleneck_distnace_adapter);
     120  }
     1.1 --- a/include/topology/persistence-diagram.h	Mon Oct 04 18:03:35 2010 -0700
     1.2 +++ b/include/topology/persistence-diagram.h	Sat Oct 09 13:56:26 2010 -0700
     1.3 @@ -113,7 +113,7 @@
     1.4          
     1.5          std::ostream&           operator<<(std::ostream& out) const;
     1.6  
     1.7 -        const Dimension&        dimension( ) const                          { return dimension_; }
     1.8 +        Dimension               dimension() const                           { return dimension_; }
     1.9  
    1.10      private:
    1.11          PointVector             points_;