Merged in Aravind's changes dev
authorDmitriy Morozov <dmitriy@mrzv.org>
Wed, 25 Aug 2010 14:29:54 -0700
branchdev
changeset 222 fdfde7291a0f
parent 214 ee0dca73ea47 (current diff)
parent 221 9ea8d4630907 (diff)
child 224 8f6fb2ad57f7
Merged in Aravind's changes
--- a/bindings/python/CMakeLists.txt	Thu Jul 15 10:20:06 2010 -0700
+++ b/bindings/python/CMakeLists.txt	Wed Aug 25 14:29:54 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	Thu Jul 15 10:20:06 2010 -0700
+++ b/bindings/python/dionysus.cpp	Wed Aug 25 14:29:54 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();
--- a/bindings/python/dionysus/__init__.py	Thu Jul 15 10:20:06 2010 -0700
+++ b/bindings/python/dionysus/__init__.py	Wed Aug 25 14:29:54 2010 -0700
@@ -26,3 +26,13 @@
 
 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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/bindings/python/persistence-diagram.cpp	Wed Aug 25 14:29:54 2010 -0700
@@ -0,0 +1,95 @@
+#include<topology/persistence-diagram.h>
+#include<utilities/types.h>
+
+#include<boost/python.hpp>
+#include<boost/python/init.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;
+
+
+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( bp::init< RealType, RealType, bp::optional<dp::Data>  >( ) )
+    .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 PersistenceDiagram, class Point>
+boost::shared_ptr<PersistenceDiagram>    init_from_points_sequence( Dimension dimension, bp::object point_sequence ){
+
+    typedef bp::stl_input_iterator<Point> PointIterator;
+
+    PointIterator beg = PointIterator( point_sequence ), end = PointIterator( );
+    // The following line is commented out until we can figure out the Evaluator class in make_point
+    //boost::shared_ptr<PersistenceDiagram> p(  new PersistenceDiagram( beg, end );
+    boost::shared_ptr<PersistenceDiagram> p(  new PersistenceDiagram( dimension ) );
+
+    for( PointIterator cur=beg;  cur!=end; cur++ )
+        (*p).push_back( *cur );
+    return p;
+
+}
+
+
+template<class PersistenceDiagram>
+Dimension get_dimension( PersistenceDiagram dgm ){
+    return dgm.dimension( );
+}
+
+template<class PersistenceDiagram>
+SizeType get_length( PersistenceDiagram dgm ){
+    return dgm.size( );
+}
+
+void export_persistence_diagram( ){
+
+    bp::class_<dp::PersistenceDiagramD>("PersistenceDiagram")
+    .def( "__init__", bp::make_constructor(  &init_from_points_sequence< dp::PersistenceDiagramD, dp::PointD > ) )
+    .def( bp::init< Dimension >( ) )
+    .def( "append", &dp::PersistenceDiagramD::push_back )
+    .add_property( "dimension", &get_dimension<dp::PersistenceDiagramD> )
+    .def( repr(bp::self) )
+    .def( "__iter__", bp::range( &dp::PersistenceDiagramD::begin, &dp::PersistenceDiagramD::end ) )
+    .def( "__len__", &get_length<dp::PersistenceDiagramD> )
+    ;
+
+    bp::def( "bottleneck_distance", bottleneck_distance<dp::PersistenceDiagramD,dp::PersistenceDiagramD> );
+}
--- a/doc/python/overview.rst	Thu Jul 15 10:20:06 2010 -0700
+++ b/doc/python/overview.rst	Wed Aug 25 14:29:54 2010 -0700
@@ -21,3 +21,4 @@
     alphashapes.rst
     rips.rst
     zigzag-persistence.rst
+    persistence-diagram.rst
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/doc/python/persistence-diagram.rst	Wed Aug 25 14:29:54 2010 -0700
@@ -0,0 +1,84 @@
+:class:`PersistenceDiagram` class
+==================================
+
+.. class:: PersistenceDiagram
+
+    .. method:: __init__( dimension )
+    
+        Initializes : an empty( no points ) :class:`PersistenceDiagram` object and sets
+        the :attr:`~PersistenceDiagram.dimension` attribute( must be integer ) e.g.::
+    
+            dia = PersistenceDiagram( 1 )
+
+    .. method:: __init__( dimension, point_seq )
+    
+        Initializes :class:`PersistenceDiagram` of specified dimension from the given sequence `seq` of :class:`Point` objects, e.g.::
+    
+            dia = PersistenceDiagram( 1, [Point(1,2)] )
+
+    .. method:: append( p )
+        
+        Adds point `p` to the persistence diagram.
+
+    .. attribute:: dimension
+
+        Dimension of the persistence diagram. Must be an integer. Must be set at initialization.
+
+    .. method:: __iter__( )
+
+        Iterator over the points in the persistence diagram,
+        e.g.::
+            
+            for p in dia: print p
+
+    .. method:: __len__( )
+
+        :returns: The number of points in the diagram.
+
+
+
+Utility functions for persistence diagrams
+--------------------------------------------
+
+
+.. function:: bottleneck_distance(dia1, dia2)
+    
+    Calculates the bottleneck distance between the two persistence diagrams. 
+
+
+
+:class:`Point` class
+======================
+
+.. class:: Point
+
+    .. method:: __init__( x, y [, data])
+    
+        Initializes :class:`Point` with the given real-valued coordinates and
+        optionally real value `data`, e.g.::
+    
+            s = Point( 1, 1.1, 1.11 )
+
+    .. attribute:: x
+        
+        x coordinate of the point
+
+    .. attribute:: y
+        
+        y coordinate of the point
+
+    .. attribute:: data
+        
+        Real value stored in the simplex.
+
+    .. method:: __iter__( )
+
+        Point objects are iterable, returning two or three elements depending on presence of data, e.g.::
+
+            p = Point( 1, 1.1, 1.11 )
+            for i in p:  print p
+
+            1
+            1.1
+            1.11
+
--- a/include/topology/persistence-diagram.h	Thu Jul 15 10:20:06 2010 -0700
+++ b/include/topology/persistence-diagram.h	Wed Aug 25 14:29:54 2010 -0700
@@ -85,6 +85,9 @@
 
                                 PersistenceDiagram()                        {}
 
+                                PersistenceDiagram( Dimension dimension ):  
+                                    dimension_( dimension )                  {}
+
         template<class OtherData>
                                 PersistenceDiagram(const PersistenceDiagram<OtherData>& other);
 
@@ -110,8 +113,11 @@
         
         std::ostream&           operator<<(std::ostream& out) const;
 
+        const Dimension&        dimension( ) const                          { return dimension_; }
+
     private:
         PointVector             points_;
+        Dimension               dimension_;
 	
     private:
 		/* Serialization */