From 7f9e8f11f70e8387ef29c3fa13016121dca79cbe Mon Sep 17 00:00:00 2001 From: mcarrier Date: Fri, 20 Apr 2018 15:56:06 +0000 Subject: git-svn-id: svn+ssh://scm.gforge.inria.fr/svnroot/gudhi/branches/kernels@3385 636b058d-ea47-450e-bf9e-a15bfbe3eedb Former-commit-id: 2a83e62e132ce406986efa1456b5f1bca6b93691 --- src/cython/cython/kernels.pyx | 16 +++++---- src/cython/cython/vectors.pyx | 65 ++++++++++++++++++++++++++++++++++ src/cython/gudhi.pyx.in | 1 + src/cython/include/Kernels_interface.h | 46 +++++++++++------------- 4 files changed, 96 insertions(+), 32 deletions(-) create mode 100644 src/cython/cython/vectors.pyx (limited to 'src/cython') diff --git a/src/cython/cython/kernels.pyx b/src/cython/cython/kernels.pyx index 466917b1..0cb296ec 100644 --- a/src/cython/cython/kernels.pyx +++ b/src/cython/cython/kernels.pyx @@ -34,8 +34,8 @@ cdef extern from "Kernels_interface.h" namespace "Gudhi::persistence_diagram": 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) - vector[vector[double]] pwg_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): """ @@ -65,7 +65,7 @@ def sliced_wasserstein_matrix(diagrams_1, diagrams_2, sigma = 1, N = 100): """ return sw_matrix(diagrams_1, diagrams_2, sigma, N) -def persistence_weighted_gaussian(diagram_1, diagram_2, sigma = 1, N = 100): +def persistence_weighted_gaussian(diagram_1, diagram_2, sigma = 1, N = 100, C = 1, p = 1): """ :param diagram_1: The first diagram. @@ -74,12 +74,14 @@ def persistence_weighted_gaussian(diagram_1, diagram_2, sigma = 1, N = 100): :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) + return pwg(diagram_1, diagram_2, sigma, N, C, p) -def persistence_weighted_gaussian_matrix(diagrams_1, diagrams_2, sigma = 1, N = 100): +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. @@ -88,10 +90,12 @@ def persistence_weighted_gaussian_matrix(diagrams_1, diagrams_2, sigma = 1, N = :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) + return pwg_matrix(diagrams_1, diagrams_2, sigma, N, C, p) def persistence_scale_space(diagram_1, diagram_2, sigma = 1, N = 100): """ 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 . +""" + +__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 7f42968d..3ff68085 100644 --- a/src/cython/gudhi.pyx.in +++ b/src/cython/gudhi.pyx.in @@ -37,6 +37,7 @@ 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 index 1742d016..0da28245 100644 --- a/src/cython/include/Kernels_interface.h +++ b/src/cython/include/Kernels_interface.h @@ -34,25 +34,24 @@ namespace Gudhi { namespace persistence_diagram { - double sw(const std::vector>& diag1, - const std::vector>& diag2, - double sigma, int N) { + + // ******************* + // Kernel evaluations. + // ******************* + + double sw(const std::vector>& diag1, const std::vector>& 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>& diag1, - const std::vector>& diag2, - double sigma, int N) { - Gudhi::Persistence_representations::Persistence_weighted_gaussian pwg1(diag1, sigma, N, Gudhi::Persistence_representations::Persistence_weighted_gaussian::arctan_weight); - Gudhi::Persistence_representations::Persistence_weighted_gaussian pwg2(diag2, sigma, N, Gudhi::Persistence_representations::Persistence_weighted_gaussian::arctan_weight); + double pwg(const std::vector>& diag1, const std::vector>& diag2, double sigma, int N, double C, double p) { + Gudhi::Persistence_representations::Persistence_weighted_gaussian pwg1(diag1, sigma, N, Gudhi::Persistence_representations::Persistence_weighted_gaussian::arctan_weight(C,p)); + Gudhi::Persistence_representations::Persistence_weighted_gaussian pwg2(diag2, sigma, N, Gudhi::Persistence_representations::Persistence_weighted_gaussian::arctan_weight(C,p)); return pwg1.compute_scalar_product(pwg2); } - double pss(const std::vector>& diag1, - const std::vector>& diag2, - double sigma, int N) { + double pss(const std::vector>& diag1, const std::vector>& diag2, double sigma, int N) { std::vector> pd1 = diag1; int numpts = diag1.size(); for(int i = 0; i < numpts; i++) pd1.emplace_back(diag1[i].second,diag1[i].first); std::vector> pd2 = diag2; numpts = diag2.size(); for(int i = 0; i < numpts; i++) pd2.emplace_back(diag2[i].second,diag2[i].first); @@ -62,9 +61,7 @@ namespace persistence_diagram { return pwg1.compute_scalar_product (pwg2) / (16*Gudhi::Persistence_representations::pi*sigma); } - double pss_sym(const std::vector>& diag1, - const std::vector>& diag2, - double sigma, int N) { + double pss_sym(const std::vector>& diag1, const std::vector>& diag2, double sigma, int N) { Gudhi::Persistence_representations::Persistence_weighted_gaussian pwg1(diag1, 2*std::sqrt(sigma), N, Gudhi::Persistence_representations::Persistence_weighted_gaussian::pss_weight); Gudhi::Persistence_representations::Persistence_weighted_gaussian pwg2(diag2, 2*std::sqrt(sigma), N, Gudhi::Persistence_representations::Persistence_weighted_gaussian::pss_weight); @@ -72,9 +69,11 @@ namespace persistence_diagram { } - std::vector > sw_matrix(const std::vector > >& s1, - const std::vector > >& s2, - double sigma, int N){ + // **************** + // Kernel matrices. + // **************** + + std::vector > sw_matrix(const std::vector > >& s1, const std::vector > >& s2, double sigma, int N){ std::vector > matrix; std::vector 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);} @@ -87,22 +86,17 @@ namespace persistence_diagram { return matrix; } - std::vector > pwg_matrix(const std::vector > >& s1, - const std::vector > >& s2, - double sigma, int N){ + std::vector > pwg_matrix(const std::vector > >& s1, const std::vector > >& s2, double sigma, int N, double C, double p){ std::vector > 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 ps; for(int j = 0; j < num_diag_2; j++) ps.push_back(pwg(s1[i], s2[j], sigma, N)); matrix.push_back(ps); + std::vector 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 > pss_matrix(const std::vector > >& s1, - const std::vector > >& s2, - double sigma, int N){ - std::vector > > ss1, ss2; - std::vector > matrix; int num_diag_1 = s1.size(); int num_diag_2 = s2.size(); + std::vector > pss_matrix(const std::vector > >& s1, const std::vector > >& s2, double sigma, int N){ + std::vector > > ss1, ss2; std::vector > matrix; int num_diag_1 = s1.size(); int num_diag_2 = s2.size(); for(int i = 0; i < num_diag_1; i++){ std::vector> 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); -- cgit v1.2.3