summaryrefslogtreecommitdiff
path: root/src/cython
diff options
context:
space:
mode:
authorpdlotko <pdlotko@636b058d-ea47-450e-bf9e-a15bfbe3eedb>2017-08-22 13:09:08 +0000
committerpdlotko <pdlotko@636b058d-ea47-450e-bf9e-a15bfbe3eedb>2017-08-22 13:09:08 +0000
commit40db1989840aa9e485233354853ad09239272ad7 (patch)
tree0ccf6a7779943babf7b496e8f442fe3162f760dd /src/cython
parente2c0166fcbf17e91cf5ed7b6159f80d64b49cc0b (diff)
First version of persistence diagrams cytonization that compiles (and create strange behavior)
git-svn-id: svn+ssh://scm.gforge.inria.fr/svnroot/gudhi/branches/persistence_representation_integration@2615 636b058d-ea47-450e-bf9e-a15bfbe3eedb Former-commit-id: 0a2a8ea77c654968e3be476c118f9cd3db0e9570
Diffstat (limited to 'src/cython')
-rw-r--r--src/cython/cython/persistence_representations_diagrams.pyx185
-rwxr-xr-xsrc/cython/example/persistence_representations_diagrams_example.py107
-rw-r--r--src/cython/gudhi.pyx.in1
-rw-r--r--src/cython/include/persistence_representations_diagrams.h123
4 files changed, 416 insertions, 0 deletions
diff --git a/src/cython/cython/persistence_representations_diagrams.pyx b/src/cython/cython/persistence_representations_diagrams.pyx
new file mode 100644
index 00000000..45ab4828
--- /dev/null
+++ b/src/cython/cython/persistence_representations_diagrams.pyx
@@ -0,0 +1,185 @@
+from cython cimport numeric
+from libcpp.vector cimport vector #use similar line if you need string, pair, etc....
+from libcpp.utility cimport pair
+from libcpp cimport bool
+import os
+
+"""This file is part of the Gudhi Library. The Gudhi library
+ (Geometric Understanding in Higher Dimensions) is a generic C++
+ library for computational topology.
+
+ Author(s): Pawel Dlotko
+
+ Copyright (C) 2017 Swansea University
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+"""
+
+__author__ = "Pawel Dlotko"
+__copyright__ = "Copyright (C) 2017 Swansea University"
+__license__ = "GPL v3"
+
+
+
+
+
+
+"""
+This is a promisse that there will be a class in this file with the following function signature. Something like C++ predeclaration.
+According to Vincent, most of the tutorials in cython suggest to separate pre-declaration below with the definition of the method. Hovewer it seems to create problems, that is why we keep them both here.
+"""
+
+cdef extern from "persistence_representations_diagrams.h" namespace "Gudhi::Persistence_representations":
+ cdef cppclass Persistence_intervals_interface "Gudhi::Persistence_representations::Persistence_intervals_interface":
+ Persistence_intervals_interface()
+ Persistence_intervals_interface(const char* , unsigned )
+ Persistence_intervals_interface(const vector[pair[double, double]] intervals)
+ pair[double, double] get_x_range_interface() const
+ pair[double, double] get_y_range_interface() const
+ vector[double] length_of_dominant_intervals_interface(size_t where_to_cut) const
+ vector[pair[double, double] ] dominant_intervals_interface(size_t where_to_cut) const
+ vector[size_t] histogram_of_lengths_interface(size_t number_of_bins ) const
+ vector[size_t] cumulative_histogram_of_lengths_interface(size_t number_of_bins) const
+ vector[double] characteristic_function_of_diagram_interface(double x_min, double x_max, size_t number_of_bins) const
+ vector[double] cumulative_characteristic_function_of_diagram_interface(double x_min, double x_max, size_t number_of_bins) const
+ vector[pair[double, size_t] ] compute_persistent_betti_numbers_interface() const
+ double project_to_R_interface(int number_of_function) const
+ size_t number_of_projections_to_R_interface() const
+ vector[double] vectorize_interface(int number_of_function) const
+ size_t number_of_vectorize_functions_interface() const
+
+"""
+make sure that here we call the functions from the intermediate .h file, with dummy names, so that later below we can use the same names of the functions as in C++ version.
+Over here I need to list all the functions that will be used in the file. So there should be a list of constructors, methors, etc.
+to separate the function, use newline. Put there only C++ signature
+"""
+
+
+
+#convention for python class is PersistenceDiagrams instead of Persistence_diagrams
+#for methods it is def num_simplices(self).
+cdef class PersistenceIntervals:
+ """
+ Persistence interals is a standard representation of persistent homology. This file provide implementation of a number of operations on persistence diagrams.
+ """
+
+ cdef Persistence_intervals_interface * thisptr
+
+ #do we need a fake constructor here, as in case of bitmaps??
+ #We do need it so that we have a doc for python becaus ethe documentation only read from __init__, it do not read from __cinit__
+ #__ means private memeber
+ def __init__(self, vector_of_intervals=None, dimension = None, file_with_intervals=''):
+ """Persistence interals is a standard representation of persistent homology. This file provide implementation of a number of operations on persistence diagrams.
+
+ :param dimensions: A vector of birth-death pairs.
+
+ Or
+
+ :param Gudhi style file togethr with a dimension of birth-death pairs to consider.
+ """
+
+
+ # The real cython constructor
+ def __cinit__(self, vector_of_intervals=None, dimension = None, file_with_intervals=''):
+ if (vector_of_intervals is None) and (file_with_intervals is not ''):
+ self.thisptr = new Persistence_intervals_interface(file_with_intervals, dimension)
+ elif (file_with_intervals is not '') and (vector_of_intervals is not None):
+ if os.path.isfile(file_with_intervals):
+ self.thisptr = new Persistence_intervals_interface(str.encode(file_with_intervals))
+ else:
+ print("file " + file_with_intervals + " not found.")
+ else:
+ print("Persistence interals can be constructed from vector of birth-death pairs, vector_of_intervals or a Gudhi-style file.")
+
+
+
+ def __dealloc__(self):
+ if self.thisptr != NULL:
+ del self.thisptr
+
+
+ #from here on this is my try. Do we need to specify the returned type??
+ #no, we do not.
+ def get_x_range(self):
+ if self.thisptr != NULL:
+ return self.thisptr.get_x_range_interface()
+
+ def get_y_range(self):
+ if self.thisptr != NULL:
+ return self.thisptr.get_y_range_interface()
+
+ def length_of_dominant_intervals(self,where_to_cut):
+ if (self.thisptr != NULL) and (where_to_cut is not None):
+ return self.thisptr.length_of_dominant_intervals_interface(where_to_cut)
+ else:
+ if (self.thisptr != NULL):
+ return self.thisptr.dominant_intervals_interface(100)#default argument
+
+ def dominant_intervals(self,where_to_cut):
+ if (self.thisptr != NULL) and (where_to_cut is not None):
+ return self.thisptr.dominant_intervals_interface(where_to_cut)
+ else:
+ if (self.thisptr != NULL):
+ return self.thisptr.dominant_intervals_interface(100)#default argument
+
+ def histogram_of_lengths(self,number_of_bins):
+ if (self.thisptr != NULL) and (number_of_bins is not None):
+ return self.thisptr.histogram_of_lengths_interface(number_of_bins)
+ else:
+ if (self.thisptr != NULL):
+ return self.thisptr.dominant_intervals_interface(100) #default argument
+
+ def cumulative_histogram_of_lengths(self,number_of_bins):
+ if (self.thisptr != NULL) and (number_of_bins is not None):
+ return self.thisptr.cumulative_histogram_of_lengths_interface(number_of_bins)
+ else:
+ if (self.thisptr != NULL):
+ return self.thisptr.cumulative_histogram_of_lengths_interface(10)#default argument
+
+ def characteristic_function_of_diagram(self,x_min,x_max,number_of_bins):
+ if (self.thisptr != NULL) and ( x_min is not None ) and ( x_max is not None ) and ( number_of_bins is not None ):
+ return self.thisptr.characteristic_function_of_diagram_interface( x_min , x_max , number_of_bins )
+ else:
+ if (self.thisptr != NULL) and ( x_min is not None ) and ( x_max is not None ):
+ return self.thisptr.characteristic_function_of_diagram_interface( x_min , x_max ,10 ) #default argument
+
+ def cumulative_characteristic_function_of_diagram(self,x_min,x_max,number_of_bins):
+ if (self.thisptr != NULL) and ( x_min is not None ) and ( x_max is not None ) and ( number_of_bins is not None ):
+ return self.thisptr.cumulative_characteristic_function_of_diagram_interface( x_min , x_max , number_of_bins )
+ else:
+ if (self.thisptr != NULL ) and ( x_min is not None ) and ( x_max is not None ):
+ return self.thisptr.cumulative_characteristic_function_of_diagram_interface( x_min , x_max , 10) #default argument
+
+ def compute_persistent_betti_numbers(self):
+ if self.thisptr != NULL:
+ return self.thisptr.compute_persistent_betti_numbers_interface()
+
+ def project_to_R(self,number_of_function):
+ if (self.thisptr != NULL) and (number_of_function is not None):
+ return self.thisptr.project_to_R_interface(number_of_function)
+
+ def number_of_projections_to_R(self):
+ if self.thisptr != NULL:
+ return self.thisptr.number_of_projections_to_R_interface()
+
+ def vectorize(self,number_of_function):
+ if (self.thisptr != NULL) and ( number_of_function is not None ):
+ return self.thisptr.vectorize_interface(number_of_function)
+
+ def number_of_vectorize_functions(self):
+ if (self.thisptr != NULL):
+ return self.thisptr.number_of_vectorize_functions_interface()
+
+
+
diff --git a/src/cython/example/persistence_representations_diagrams_example.py b/src/cython/example/persistence_representations_diagrams_example.py
new file mode 100755
index 00000000..62dc84d7
--- /dev/null
+++ b/src/cython/example/persistence_representations_diagrams_example.py
@@ -0,0 +1,107 @@
+#!/usr/bin/env python
+
+import gudhi
+import argparse
+
+"""This file is part of the Gudhi Library. The Gudhi library
+ (Geometric Understanding in Higher Dimensions) is a generic C++
+ library for computational topology.
+
+ Author(s): Pawel Dlotko
+
+ Copyright (C) 2017 Swansea University
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+"""
+
+__author__ = "Pawel Dlotko"
+__copyright__ = "Copyright (C) 2017 Swansea University"
+__license__ = "GPL v3"
+
+print("#####################################################################")
+print("Persistence representations diagrams example")
+
+
+
+
+
+
+
+
+
+
+
+parser = argparse.ArgumentParser(description='Statistics od persistence diagrams from file ',
+ epilog='Example: '
+ 'example/persistence_representations_diagrams_example.py '
+ '-f file_with_diagram -d 1')
+parser.add_argument("-f", "--file", type=str, required=True)
+parser.add_argument("-d", "--dimension", type=int, default=0)
+
+args = parser.parse_args()
+
+print "Here are the parameters of the program: ",args.file," , " ,args.dimension
+
+
+p = gudhi.PersistenceIntervals(args.file,args.dimension);
+min_max_ = p.get_x_range();
+print( "Birth-death range : ", min_max_)
+"""
+std::vector<double> dominant_ten_intervals_length = p.length_of_dominant_intervals(10);
+std::cout << "Length of ten dominant intervals : " << std::endl;
+for (size_t i = 0; i != dominant_ten_intervals_length.size(); ++i) {
+std::cout << dominant_ten_intervals_length[i] << std::endl;
+}
+
+std::vector<std::pair<double, double> > ten_dominant_intervals = p.dominant_intervals(10);
+std::cout << "Here are the dominant intervals : " << std::endl;
+for (size_t i = 0; i != ten_dominant_intervals.size(); ++i) {
+std::cout << "( " << ten_dominant_intervals[i].first << "," << ten_dominant_intervals[i].second << std::endl;
+}
+
+std::vector<size_t> histogram = p.histogram_of_lengths(10);
+std::cout << "Here is the histogram of barcode's length : " << std::endl;
+for (size_t i = 0; i != histogram.size(); ++i) {
+std::cout << histogram[i] << " ";
+}
+std::cout << std::endl;
+
+std::vector<size_t> cumulative_histogram = p.cumulative_histogram_of_lengths(10);
+std::cout << "Cumulative histogram : " << std::endl;
+for (size_t i = 0; i != cumulative_histogram.size(); ++i) {
+std::cout << cumulative_histogram[i] << " ";
+}
+std::cout << std::endl;
+
+std::vector<double> char_funct_diag = p.characteristic_function_of_diagram(min_max_.first, min_max_.second);
+std::cout << "Characteristic function of diagram : " << std::endl;
+for (size_t i = 0; i != char_funct_diag.size(); ++i) {
+std::cout << char_funct_diag[i] << " ";
+}
+std::cout << std::endl;
+
+std::vector<double> cumul_char_funct_diag =
+ p.cumulative_characteristic_function_of_diagram(min_max_.first, min_max_.second);
+std::cout << "Cumulative characteristic function of diagram : " << std::endl;
+for (size_t i = 0; i != cumul_char_funct_diag.size(); ++i) {
+std::cout << cumul_char_funct_diag[i] << " ";
+}
+std::cout << std::endl;
+
+std::cout << "Persistence Betti numbers \n";
+std::vector<std::pair<double, size_t> > pbns = p.compute_persistent_betti_numbers();
+for (size_t i = 0; i != pbns.size(); ++i) {
+std::cout << pbns[i].first << " " << pbns[i].second << std::endl;
+}
+"""
diff --git a/src/cython/gudhi.pyx.in b/src/cython/gudhi.pyx.in
index 34d7c3b5..6bb6dce9 100644
--- a/src/cython/gudhi.pyx.in
+++ b/src/cython/gudhi.pyx.in
@@ -32,6 +32,7 @@ include "cython/periodic_cubical_complex.pyx"
include "cython/persistence_graphical_tools.py"
include "cython/witness_complex.pyx"
include "cython/strong_witness_complex.pyx"
+include "cython/persistence_representations_diagrams.pyx"
@GUDHI_CYTHON_ALPHA_COMPLEX@
@GUDHI_CYTHON_EUCLIDEAN_WITNESS_COMPLEX@
@GUDHI_CYTHON_SUBSAMPLING@
diff --git a/src/cython/include/persistence_representations_diagrams.h b/src/cython/include/persistence_representations_diagrams.h
new file mode 100644
index 00000000..aed2304c
--- /dev/null
+++ b/src/cython/include/persistence_representations_diagrams.h
@@ -0,0 +1,123 @@
+/* This file is part of the Gudhi Library. The Gudhi library
+ * (Geometric Understanding in Higher Dimensions) is a generic C++
+ * library for computational topology.
+ *
+ * Author(s): Pawel Dlotko
+ *
+ * Copyright (C) 2017 Swansea University
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef INCLUDE_PERSISTENCE_REPRESENTATIONS_DIAGRAMS_
+#define INCLUDE_PERSISTENCE_REPRESENTATIONS_DIAGRAMS_
+
+#include <gudhi/Persistence_intervals.h>
+
+#include <iostream>
+#include <vector>
+#include <string>
+
+namespace Gudhi {
+
+//But if we want to have the same names of classes in C++ and cyton side we ned this interface, because othervise we will have a name conflict. And we want to have the same names on the
+//C++ and python side for various reasonc (clarity, documentantions etc.).
+//If the C++ class we inherid from are template class, we are inherid from concretization, for instance Persistence_intervals<double>.
+//Also in this class, we create an interface functions that will be used in the python side. That will allow to have the same name of the functions in the C++ and python side.
+
+namespace Persistence_representations {
+
+class Persistence_intervals_interface : public Persistence_intervals
+{
+ public:
+ Persistence_intervals_interface(const char* filename, unsigned dimension = std::numeric_limits<unsigned>::max())
+ : Persistence_intervals(filename, dimension) {
+ }
+
+ Persistence_intervals_interface(const std::vector<std::pair<double, double> >& intervals)
+ : Persistence_intervals(intervals) {
+ }
+
+ std::pair<double, double> get_x_range_interface() const
+ {
+ return this->get_x_range();
+ }
+
+ std::pair<double, double> get_y_range_interface() const
+ {
+ return this->get_y_range();
+ }
+
+ std::vector<double> length_of_dominant_intervals_interface(size_t where_to_cut = 100) const
+ {
+ return this->length_of_dominant_intervals(where_to_cut);
+ }
+
+ std::vector<std::pair<double, double> > dominant_intervals_interface(size_t where_to_cut = 100) const
+ {
+ return this->dominant_intervals(where_to_cut);
+ }
+
+ std::vector<size_t> histogram_of_lengths_interface(size_t number_of_bins = 10) const
+ {
+ return this->histogram_of_lengths(number_of_bins);
+ }
+
+ std::vector<size_t> cumulative_histogram_of_lengths_interface(size_t number_of_bins = 10) const
+ {
+ return this->cumulative_histogram_of_lengths(number_of_bins);
+ }
+
+ std::vector<double> characteristic_function_of_diagram_interface(double x_min, double x_max, size_t number_of_bins = 10) const
+ {
+ return this->characteristic_function_of_diagram(x_min,x_max,number_of_bins);
+ }
+
+ std::vector<double> cumulative_characteristic_function_of_diagram_interface(double x_min, double x_max, size_t number_of_bins = 10) const
+ {
+ return this->cumulative_characteristic_function_of_diagram(x_min,x_max,number_of_bins);
+ }
+
+ std::vector<std::pair<double, size_t> > compute_persistent_betti_numbers_interface() const
+ {
+ return this->compute_persistent_betti_numbers();
+ }
+
+ double project_to_R_interface(int number_of_function) const
+ {
+ return this->project_to_R(number_of_function);
+ }
+
+ size_t number_of_projections_to_R_interface() const
+ {
+ return this->number_of_projections_to_R();
+ }
+
+ std::vector<double> vectorize_interface(int number_of_function) const
+ {
+ return this->vectorize(number_of_function);
+ }
+
+ size_t number_of_vectorize_functions_interface() const
+ {
+ return this->number_of_vectorize_functions();
+ }
+};
+
+} // namespace Persistence_representations
+
+} // namespace Gudhi
+
+#endif // INCLUDE_PERSISTENCE_REPRESENTATIONS_DIAGRAMS_
+