Added ability to iterate over ZigzagPersistence cycles in Python dev
authorDmitriy Morozov <dmitriy@mrzv.org>
Tue, 14 Sep 2010 11:12:33 -0700
branchdev
changeset 230 6a222607714c
parent 229 ed3905be234f
child 231 04a4629979da
Added ability to iterate over ZigzagPersistence cycles in Python
bindings/python/zigzag-persistence.cpp
doc/python/zigzag-persistence.rst
include/topology/zigzag-persistence.h
--- a/bindings/python/zigzag-persistence.cpp	Fri Sep 10 21:21:27 2010 -0700
+++ b/bindings/python/zigzag-persistence.cpp	Tue Sep 14 11:12:33 2010 -0700
@@ -10,6 +10,9 @@
 #include "optional.h"
 namespace dp = dionysus::python;
 
+#include <sstream>
+#include <string>
+
 
 // ZigzagPersistence
 bp::tuple                           zzp_add(dp::ZZPersistence& zzp, bp::object bdry, dp::BirthID birth)
@@ -33,6 +36,10 @@
     return zzp.remove(s, birth); 
 }
 
+bool                                zzp_is_alive(dp::ZZPersistence& zzp, const dp::ZZPersistence::ZNode& zn)
+{
+    return zzp.is_alive(zn);
+}
 
 // ImageZigzagPersistence
 bp::tuple                           izzp_add(dp::IZZPersistence& izzp, bp::object bdry, bool subcomplex, dp::BirthID birth)
@@ -64,11 +71,30 @@
     return si->order;
 }
 
+template<class T>
+std::string                         si_repr(T& si)
+{
+    std::ostringstream out; out << "SimplexIndex <" << si->order << ">";
+    return out.str();
+}
+
+// ZNode
+dp::ZZPersistence::ZColumn::const_iterator
+znode_zcolumn_begin(dp::ZZPersistence::ZNode& zn)
+{ return zn.z_column.begin(); }
+
+dp::ZZPersistence::ZColumn::const_iterator
+znode_zcolumn_end(dp::ZZPersistence::ZNode& zn)
+{ return zn.z_column.end(); }
+
+
 
 void export_zigzag_persistence()
 {
     bp::class_<dp::ZZPersistence::SimplexIndex>("ZZSimplexIndex")
-        .def("order",           &si_order<dp::ZZPersistence::SimplexIndex>);
+        .def("order",           &si_order<dp::ZZPersistence::SimplexIndex>)
+        .def("__repr__",        &si_repr<dp::ZZPersistence::SimplexIndex>)
+    ;
     
     bp::class_<dp::IZZPersistence::SimplexIndex>("IZZSimplexIndex")
         .def("order",           &si_order<dp::IZZPersistence::SimplexIndex>);
@@ -76,10 +102,17 @@
     bp::class_<dp::ZZPersistence>("ZigzagPersistence")
         .def("add",             &zzp_add)
         .def("remove",          &zzp_remove)
+        .def("is_alive",        &zzp_is_alive)
+        .def("__iter__",        bp::range(&dp::ZZPersistence::begin, &dp::ZZPersistence::end))
     ;
     
     bp::class_<dp::IZZPersistence>("ImageZigzagPersistence")
         .def("add",             &izzp_add)
         .def("remove",          &izzp_remove)
     ;
+
+    bp::class_<dp::ZZPersistence::ZNode>("ZNode", bp::no_init)
+        .add_property("birth",  &dp::ZZPersistence::ZNode::birth)
+        .def("__iter__",        bp::range(&znode_zcolumn_begin, &znode_zcolumn_end))
+    ;
 }
--- a/doc/python/zigzag-persistence.rst	Fri Sep 10 21:21:27 2010 -0700
+++ b/doc/python/zigzag-persistence.rst	Tue Sep 14 11:12:33 2010 -0700
@@ -35,6 +35,29 @@
                   :meth:`~ZigzagPersistence.remove` when the class that just
                   died was born.
 
+    .. method:: is_alive(z)
+
+        Determines whether a given cycle is alive. The input should be an
+        instance of :class:`ZNode`.
+
+    .. method:: __iter__()
+
+        Iterator over elements of type :class:`ZNode`, i.e. the cycles stored in the structure.
+
+.. class:: ZNode
+
+    .. attribute:: birth
+
+        The birth value associated with the cycle. It is passed to
+        :class:`ZigzagPersistence` in method
+        :meth:`~ZigzagPersistence.add`.
+
+    .. method:: __iter__()
+
+        Iterator over the individual nodes (simplices) of the cycle (same
+        indices that are passed to and returned by
+        :meth:`~ZigzagPersistence.add`.
+
 
 Auxilliary functions
 --------------------
--- a/include/topology/zigzag-persistence.h	Fri Sep 10 21:21:27 2010 -0700
+++ b/include/topology/zigzag-persistence.h	Tue Sep 14 11:12:33 2010 -0700
@@ -99,6 +99,7 @@
         ZIndex                          end()                                                       { return z_list.end(); }
 
         bool                            is_alive(ZIndex i) const                                    { return i->low == b_list.end(); }
+        bool                            is_alive(ZNode zn) const                                    { return zn.low == b_list.end(); }
  
     protected:                                        
         // Function: add(s)