diff options
Diffstat (limited to 'src/cython')
-rw-r--r-- | src/cython/cython/kernels.pyx | 126 | ||||
-rw-r--r-- | src/cython/cython/vectors.pyx | 65 | ||||
-rw-r--r-- | src/cython/gudhi.pyx.in | 2 | ||||
-rw-r--r-- | src/cython/include/Kernels_interface.h | 125 | ||||
-rw-r--r-- | src/cython/include/Vectors_interface.h | 59 |
5 files changed, 377 insertions, 0 deletions
diff --git a/src/cython/cython/kernels.pyx b/src/cython/cython/kernels.pyx new file mode 100644 index 00000000..0cb296ec --- /dev/null +++ b/src/cython/cython/kernels.pyx @@ -0,0 +1,126 @@ +from cython cimport numeric +from libcpp.vector cimport vector +from libcpp.utility cimport pair +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): Mathieu Carriere + + Copyright (C) 2018 INRIA + + 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__ = "Mathieu Carriere" +__copyright__ = "Copyright (C) 2018 INRIA" +__license__ = "GPL v3" + +cdef extern from "Kernels_interface.h" namespace "Gudhi::persistence_diagram": + double sw (vector[pair[double, double]], vector[pair[double, double]], double, int) + vector[vector[double]] sw_matrix (vector[vector[pair[double, double]]], vector[vector[pair[double, double]]], double, int) + double pss (vector[pair[double, double]], vector[pair[double, double]], double, int) + vector[vector[double]] pss_matrix (vector[vector[pair[double, double]]], vector[vector[pair[double, double]]], double, int) + double pwg (vector[pair[double, double]], vector[pair[double, double]], double, int, double, double) + vector[vector[double]] pwg_matrix (vector[vector[pair[double, double]]], vector[vector[pair[double, double]]], double, int, double, double) + +def sliced_wasserstein(diagram_1, diagram_2, sigma = 1, N = 100): + """ + + :param diagram_1: The first diagram. + :type diagram_1: vector[pair[double, double]] + :param diagram_2: The second diagram. + :type diagram_2: vector[pair[double, double]] + :param sigma: bandwidth of Gaussian + :param N: number of directions + + :returns: the sliced wasserstein kernel. + """ + return sw(diagram_1, diagram_2, sigma, N) + +def sliced_wasserstein_matrix(diagrams_1, diagrams_2, sigma = 1, N = 100): + """ + + :param diagram_1: The first set of diagrams. + :type diagram_1: vector[vector[pair[double, double]]] + :param diagram_2: The second set of diagrams. + :type diagram_2: vector[vector[pair[double, double]]] + :param sigma: bandwidth of Gaussian + :param N: number of directions + + :returns: the sliced wasserstein kernel matrix. + """ + return sw_matrix(diagrams_1, diagrams_2, sigma, N) + +def persistence_weighted_gaussian(diagram_1, diagram_2, sigma = 1, N = 100, C = 1, p = 1): + """ + + :param diagram_1: The first diagram. + :type diagram_1: vector[pair[double, double]] + :param diagram_2: The second diagram. + :type diagram_2: vector[pair[double, double]] + :param sigma: bandwidth of Gaussian + :param N: number of Fourier features + :param C: cost of persistence weight + :param p: power of persistence weight + + :returns: the persistence weighted gaussian kernel. + """ + return pwg(diagram_1, diagram_2, sigma, N, C, p) + +def persistence_weighted_gaussian_matrix(diagrams_1, diagrams_2, sigma = 1, N = 100, C = 1, p = 1): + """ + + :param diagram_1: The first set of diagrams. + :type diagram_1: vector[vector[pair[double, double]]] + :param diagram_2: The second set of diagrams. + :type diagram_2: vector[vector[pair[double, double]]] + :param sigma: bandwidth of Gaussian + :param N: number of Fourier features + :param C: cost of persistence weight + :param p: power of persistence weight + + :returns: the persistence weighted gaussian kernel matrix. + """ + return pwg_matrix(diagrams_1, diagrams_2, sigma, N, C, p) + +def persistence_scale_space(diagram_1, diagram_2, sigma = 1, N = 100): + """ + + :param diagram_1: The first diagram. + :type diagram_1: vector[pair[double, double]] + :param diagram_2: The second diagram. + :type diagram_2: vector[pair[double, double]] + :param sigma: bandwidth of Gaussian + :param N: number of Fourier features + + :returns: the persistence scale space kernel. + """ + return pss(diagram_1, diagram_2, sigma, N) + +def persistence_scale_space_matrix(diagrams_1, diagrams_2, sigma = 1, N = 100): + """ + + :param diagram_1: The first set of diagrams. + :type diagram_1: vector[vector[pair[double, double]]] + :param diagram_2: The second set of diagrams. + :type diagram_2: vector[vector[pair[double, double]]] + :param sigma: bandwidth of Gaussian + :param N: number of Fourier features + + :returns: the persistence scale space kernel matrix. + """ + return pss_matrix(diagrams_1, diagrams_2, sigma, N) diff --git a/src/cython/cython/vectors.pyx b/src/cython/cython/vectors.pyx new file mode 100644 index 00000000..42390ae6 --- /dev/null +++ b/src/cython/cython/vectors.pyx @@ -0,0 +1,65 @@ +from cython cimport numeric +from libcpp.vector cimport vector +from libcpp.utility cimport pair +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): Mathieu Carriere + + Copyright (C) 2018 INRIA + + 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__ = "Mathieu Carriere" +__copyright__ = "Copyright (C) 2018 INRIA" +__license__ = "GPL v3" + +cdef extern from "Vectors_interface.h" namespace "Gudhi::persistence_diagram": + vector[vector[double]] compute_ls (vector[pair[double, double]], int, double, double, int) + vector[vector[double]] compute_pim (vector[pair[double, double]], double, double, int, double, double, int, string, double, double, double) + +def landscape(diagram, nb_ls = 10, min_x = 0.0, max_x = 1.0, res_x = 100): + """ + + :param diagram: The diagram + :type diagram: vector[pair[double, double]] + :param nb_ls: Number of landscapes + :param min_x: Minimum abscissa + :param max_x: Maximum abscissa + :param res_x: Number of samples + + :returns: the landscape + """ + return compute_ls(diagram, nb_ls, min_x, max_x, res_x) + +def persistence_image(diagram, min_x = 0.0, max_x = 1.0, res_x = 10, min_y = 0.0, max_y = 1.0, res_y = 10, weight = "linear", sigma = 1.0, C = 1.0, p = 1.0): + """ + + :param diagram: The diagram + :type diagram: vector[vector[pair[double, double]]] + :param min_x: Minimum abscissa + :param max_x: Maximum abscissa + :param res_x: Number of abscissa pixels + :param min_x: Minimum ordinate + :param max_x: Maximum ordinate + :param res_x: Number of ordinate pixels + :param sigma: bandwidth of Gaussian + + :returns: the persistence image + """ + return compute_pim(diagram, min_x, max_x, res_x, min_y, max_y, res_y, weight, sigma, C, p) diff --git a/src/cython/gudhi.pyx.in b/src/cython/gudhi.pyx.in index b94f2251..3555bbd6 100644 --- a/src/cython/gudhi.pyx.in +++ b/src/cython/gudhi.pyx.in @@ -36,6 +36,8 @@ include '@CMAKE_CURRENT_SOURCE_DIR@/cython/persistence_graphical_tools.py' include '@CMAKE_CURRENT_SOURCE_DIR@/cython/reader_utils.pyx' include '@CMAKE_CURRENT_SOURCE_DIR@/cython/witness_complex.pyx' include '@CMAKE_CURRENT_SOURCE_DIR@/cython/strong_witness_complex.pyx' +include '@CMAKE_CURRENT_SOURCE_DIR@/cython/kernels.pyx' +include '@CMAKE_CURRENT_SOURCE_DIR@/cython/vectors.pyx' @GUDHI_CYTHON_ALPHA_COMPLEX@ @GUDHI_CYTHON_EUCLIDEAN_WITNESS_COMPLEX@ @GUDHI_CYTHON_SUBSAMPLING@ diff --git a/src/cython/include/Kernels_interface.h b/src/cython/include/Kernels_interface.h new file mode 100644 index 00000000..dd46656f --- /dev/null +++ b/src/cython/include/Kernels_interface.h @@ -0,0 +1,125 @@ +/* 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): Mathieu Carriere + * + * Copyright (C) 2018 INRIA + * + * 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_KERNELS_INTERFACE_H_ +#define INCLUDE_KERNELS_INTERFACE_H_ + +#include <gudhi/Sliced_Wasserstein.h> +#include <gudhi/Persistence_weighted_gaussian.h> +#include <gudhi/Weight_functions.h> + +#include <iostream> +#include <vector> +#include <utility> // for std::pair + +namespace Gudhi { + +namespace persistence_diagram { + + + // ******************* + // Kernel evaluations. + // ******************* + + double sw(const std::vector<std::pair<double, double>>& diag1, const std::vector<std::pair<double, double>>& diag2, double sigma, int N) { + Gudhi::Persistence_representations::Sliced_Wasserstein sw1(diag1, sigma, N); + Gudhi::Persistence_representations::Sliced_Wasserstein sw2(diag2, sigma, N); + return sw1.compute_scalar_product(sw2); + } + + double pwg(const std::vector<std::pair<double, double>>& diag1, const std::vector<std::pair<double, double>>& diag2, double sigma, int N, double C, double p) { + Gudhi::Persistence_representations::Persistence_weighted_gaussian pwg1(diag1, sigma, N, Gudhi::Persistence_representations::arctan_weight(C,p)); + Gudhi::Persistence_representations::Persistence_weighted_gaussian pwg2(diag2, sigma, N, Gudhi::Persistence_representations::arctan_weight(C,p)); + return pwg1.compute_scalar_product(pwg2); + } + + double pss(const std::vector<std::pair<double, double>>& diag1, const std::vector<std::pair<double, double>>& diag2, double sigma, int N) { + std::vector<std::pair<double, double>> pd1 = diag1; int numpts = diag1.size(); for(int i = 0; i < numpts; i++) pd1.emplace_back(diag1[i].second,diag1[i].first); + std::vector<std::pair<double, double>> pd2 = diag2; numpts = diag2.size(); for(int i = 0; i < numpts; i++) pd2.emplace_back(diag2[i].second,diag2[i].first); + + Gudhi::Persistence_representations::Persistence_weighted_gaussian pwg1(pd1, 2*std::sqrt(sigma), N, Gudhi::Persistence_representations::pss_weight); + Gudhi::Persistence_representations::Persistence_weighted_gaussian pwg2(pd2, 2*std::sqrt(sigma), N, Gudhi::Persistence_representations::pss_weight); + + return pwg1.compute_scalar_product (pwg2) / (16*Gudhi::Persistence_representations::pi*sigma); + } + + double pss_sym(const std::vector<std::pair<double, double>>& diag1, const std::vector<std::pair<double, double>>& diag2, double sigma, int N) { + Gudhi::Persistence_representations::Persistence_weighted_gaussian pwg1(diag1, 2*std::sqrt(sigma), N, Gudhi::Persistence_representations::pss_weight); + Gudhi::Persistence_representations::Persistence_weighted_gaussian pwg2(diag2, 2*std::sqrt(sigma), N, Gudhi::Persistence_representations::pss_weight); + + return pwg1.compute_scalar_product (pwg2) / (16*Gudhi::Persistence_representations::pi*sigma); + } + + + // **************** + // Kernel matrices. + // **************** + + std::vector<std::vector<double> > sw_matrix(const std::vector<std::vector<std::pair<double, double> > >& s1, const std::vector<std::vector<std::pair<double, double> > >& s2, double sigma, int N){ + std::vector<std::vector<double> > matrix; + std::vector<Gudhi::Persistence_representations::Sliced_Wasserstein> ss1; + int num_diag_1 = s1.size(); for(int i = 0; i < num_diag_1; i++){Gudhi::Persistence_representations::Sliced_Wasserstein sw1(s1[i], sigma, N); ss1.push_back(sw1);} + std::vector<Gudhi::Persistence_representations::Sliced_Wasserstein> ss2; + int num_diag_2 = s2.size(); for(int i = 0; i < num_diag_2; i++){Gudhi::Persistence_representations::Sliced_Wasserstein sw2(s2[i], sigma, N); ss2.push_back(sw2);} + for(int i = 0; i < num_diag_1; i++){ + std::cout << 100.0*i/num_diag_1 << " %" << std::endl; + std::vector<double> ps; for(int j = 0; j < num_diag_2; j++) ps.push_back(ss1[i].compute_scalar_product(ss2[j])); matrix.push_back(ps); + } + return matrix; + } + + std::vector<std::vector<double> > pwg_matrix(const std::vector<std::vector<std::pair<double, double> > >& s1, const std::vector<std::vector<std::pair<double, double> > >& s2, double sigma, int N, double C, double p){ + std::vector<std::vector<double> > matrix; int num_diag_1 = s1.size(); int num_diag_2 = s2.size(); + for(int i = 0; i < num_diag_1; i++){ + std::cout << 100.0*i/num_diag_1 << " %" << std::endl; + std::vector<double> ps; for(int j = 0; j < num_diag_2; j++) ps.push_back(pwg(s1[i], s2[j], sigma, N, C, p)); matrix.push_back(ps); + } + return matrix; + } + + std::vector<std::vector<double> > pss_matrix(const std::vector<std::vector<std::pair<double, double> > >& s1, const std::vector<std::vector<std::pair<double, double> > >& s2, double sigma, int N){ + std::vector<std::vector<std::pair<double, double> > > ss1, ss2; std::vector<std::vector<double> > matrix; int num_diag_1 = s1.size(); int num_diag_2 = s2.size(); + for(int i = 0; i < num_diag_1; i++){ + std::vector<std::pair<double, double>> pd1 = s1[i]; int numpts = s1[i].size(); + for(int j = 0; j < numpts; j++) pd1.emplace_back(s1[i][j].second,s1[i][j].first); + ss1.push_back(pd1); + } + + for(int i = 0; i < num_diag_2; i++){ + std::vector<std::pair<double, double>> pd2 = s2[i]; int numpts = s2[i].size(); + for(int j = 0; j < numpts; j++) pd2.emplace_back(s2[i][j].second,s2[i][j].first); + ss2.push_back(pd2); + } + + for(int i = 0; i < num_diag_1; i++){ + std::cout << 100.0*i/num_diag_1 << " %" << std::endl; + std::vector<double> ps; for(int j = 0; j < num_diag_2; j++) ps.push_back(pss_sym(ss1[i], ss2[j], sigma, N)); matrix.push_back(ps); + } + return matrix; + } + +} // namespace persistence_diagram + +} // namespace Gudhi + + +#endif // INCLUDE_KERNELS_INTERFACE_H_ diff --git a/src/cython/include/Vectors_interface.h b/src/cython/include/Vectors_interface.h new file mode 100644 index 00000000..902ccc10 --- /dev/null +++ b/src/cython/include/Vectors_interface.h @@ -0,0 +1,59 @@ +/* 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): Mathieu Carriere + * + * Copyright (C) 2018 INRIA + * + * 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_VECTORS_INTERFACE_H_ +#define INCLUDE_VECTORS_INTERFACE_H_ + +#include <gudhi/Persistence_landscape_on_grid_exact.h> +#include <gudhi/Persistence_heat_maps_exact.h> +#include <gudhi/Weight_functions.h> + +#include <iostream> +#include <vector> +#include <utility> // for std::pair + +using Weight = std::function<double (std::pair<double,double>) >; + +namespace Gudhi { + +namespace persistence_diagram { + + std::vector<std::vector<double> > compute_ls(const std::vector<std::pair<double, double> >& diag, int nb_ls, double min_x, double max_x, int res_x) { + Gudhi::Persistence_representations::Persistence_landscape_on_grid_exact L(diag, nb_ls, min_x, max_x, res_x); + return L.vectorize(); + } + + std::vector<std::vector<double> > compute_pim(const std::vector<std::pair<double, double> >& diag, double min_x, double max_x, int res_x, double min_y, double max_y, int res_y, std::string weight, double sigma, double C, double p) { + Weight weight_fn; + if(weight.compare("linear") == 0) weight_fn = Gudhi::Persistence_representations::linear_weight; + if(weight.compare("arctan") == 0) weight_fn = Gudhi::Persistence_representations::arctan_weight(C,p); + if(weight.compare("const") == 0) weight_fn = Gudhi::Persistence_representations::const_weight; + Gudhi::Persistence_representations::Persistence_heat_maps_exact P(diag, min_x, max_x, res_x, min_y, max_y, res_y, weight_fn, sigma); + return P.vectorize(); + } + +} // namespace persistence_diagram + +} // namespace Gudhi + + +#endif // INCLUDE_VECTORS_INTERFACE_H_ |