diff options
author | mcarrier <mcarrier@636b058d-ea47-450e-bf9e-a15bfbe3eedb> | 2018-04-20 15:56:06 +0000 |
---|---|---|
committer | mcarrier <mcarrier@636b058d-ea47-450e-bf9e-a15bfbe3eedb> | 2018-04-20 15:56:06 +0000 |
commit | 7f9e8f11f70e8387ef29c3fa13016121dca79cbe (patch) | |
tree | abd50b927970f7de1cb07f2041e093a22a446753 | |
parent | 905be209a0e62121c125c37e01f4d2eae5aa606d (diff) |
git-svn-id: svn+ssh://scm.gforge.inria.fr/svnroot/gudhi/branches/kernels@3385 636b058d-ea47-450e-bf9e-a15bfbe3eedb
Former-commit-id: 2a83e62e132ce406986efa1456b5f1bca6b93691
19 files changed, 440 insertions, 688 deletions
diff --git a/src/Kernels/doc/COPYRIGHT b/src/Kernels/doc/COPYRIGHT deleted file mode 100644 index 0c36a526..00000000 --- a/src/Kernels/doc/COPYRIGHT +++ /dev/null @@ -1,19 +0,0 @@ -The files of this directory are part of the Gudhi Library. The Gudhi library -(Geometric Understanding in Higher Dimensions) is a generic C++ library for -computational topology. - -Author(s): Mathieu Carrière - -Copyright (C) 2017 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/>. diff --git a/src/Kernels/doc/Intro_kernels.h b/src/Kernels/doc/Intro_kernels.h deleted file mode 100644 index 163690b1..00000000 --- a/src/Kernels/doc/Intro_kernels.h +++ /dev/null @@ -1,108 +0,0 @@ -/* 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) 2017 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 DOC_KERNEL_INTRO_KERNEL_H_ -#define DOC_KERNEL_INTRO_KERNEL_H_ - -namespace Gudhi { - -namespace kernel { - -/** \defgroup kernel Kernels - * - * \author Mathieu Carrière - * - * @{ - * - * Kernels are generalized scalar products. They take the form of functions whose evaluations on pairs of persistence diagrams are equal - * to the scalar products of the images of the diagrams under some feature map into a (generally unknown and infinite dimensional) - * Hilbert space. Kernels are - * very useful to handle any type of data for algorithms that require at least a Hilbert structure, such as Principal Component Analysis - * or Support Vector Machines. In this package, we implement three kernels for persistence diagrams: - * the Persistence Scale Space Kernel (PSSK)---see \cite Reininghaus_Huber_ALL_PSSK, - * the Persistence Weighted Gaussian Kernel (PWGK)---see \cite Kusano_Fukumizu_Hiraoka_PWGK, - * and the Sliced Wasserstein Kernel (SWK)---see \cite pmlr-v70-carriere17a. - * - * \section pwg Persistence Weighted Gaussian Kernel and Persistence Scale Space Kernel - * - * The PWGK is built with Gaussian Kernel Mean Embedding, meaning that each persistence diagram is first - * sent to the Hilbert space of a Gaussian kernel with bandwidth parameter \f$\sigma >0\f$ using a weighted mean embedding \f$\Phi\f$: - * - * \f$ \Phi\,:\,D\,\rightarrow\,\sum_{p\in D}\,w(p)\,{\rm exp}\left(-\frac{\|p-\cdot\|_2^2}{2\sigma^2}\right) \f$, - * - * Usually, the weight function is chosen to be an arctan function of the distance of the point to the diagonal: - * \f$w(p) = {\rm arctan}(C\,|y-x|^\alpha)\f$, for some parameters \f$C,\alpha >0\f$. - * Then, either their scalar product in this space is - * computed (Linear Persistence Weighted Gaussian Kernel): - * - * \f$ LPWGK(D_1,D_2)=\langle\Phi(D_1),\Phi(D_2)\rangle - * \,=\,\sum_{p\in D_1}\,\sum_{q\in D_2}\,w(p)\,w(q)\,{\rm exp}\left(-\frac{\|p-q\|_2^2}{2\sigma^2}\right)\f$, - * - * or a second Gaussian kernel with bandwidth parameter \f$\tau >0\f$ is applied to their distance in this space - * (Gaussian Persistence Weighted Gaussian Kernel): - * - * \f$ GPWGK(D_1,D_2)={\rm exp}\left(-\frac{\|\Phi(D_1)-\Phi(D_2)\|^2}{2\tau^2} \right)\f$, - * where \f$\|\Phi(D_1)-\Phi(D_2)\|^2 = \langle\Phi(D_1)-\Phi(D_2),\Phi(D_1)-\Phi(D_2)\rangle\f$. - * - * It follows that the computation time is \f$O(n^2)\f$ where \f$n\f$ is the number of points - * in the diagrams. This time can be improved by computing approximations of the kernel - * with \f$m\f$ Fourier features \cite Rahimi07randomfeatures. In that case, the computation time becomes \f$O(mn)\f$. - * - * The PSSK is a Linear Persistence Weighted Gaussian Kernel between modified diagrams: - * the symmetric of each point with respect to the diagonal is first added in each diagram, and then the weight function - * is set to be +1 if the point is above the diagonal and -1 otherwise. - * - * \section sw Sliced Wasserstein Kernel - * - * The Sliced Wasserstein Kernel is defined as a Gaussian-like Kernel between persistence diagrams, where the distance used for - * comparison is the Sliced Wasserstein distance \f$SW\f$ between persistence diagrams, defined as the integral of the 1-norm - * between the sorted projections of the diagrams onto all lines passing through the origin: - * - * \f$ SW(D_1,D_2)=\int_{\theta\in\mathbb{S}}\,\|\pi_\theta(D_1\cup\pi_\Delta(D_2))-\pi_\theta(D_2\cup\pi_\Delta(D_1))\|_1{\rm d}\theta\f$, - * - * where \f$\pi_\theta\f$ is the projection onto the line defined with angle \f$\theta\f$ in the unit circle \f$\mathbb{S}\f$, - * and \f$\pi_\Delta\f$ is the projection onto the diagonal. - * The integral can be either computed exactly in \f$O(n^2{\rm log}(n))\f$ time, where \f$n\f$ is the number of points - * in the diagrams, or approximated by sampling \f$m\f$ lines in the circle in \f$O(mn{\rm log}(n))\f$ time. The SWK is then computed as: - * - * \f$ SWK(D_1,D_2) = {\rm exp}\left(-\frac{SW(D_1,D_2)}{2\sigma^2}\right).\f$ - * - * When launching: - * - * \code $> ./BasicEx ../../../../data/persistence_diagram/PD1 ../../../../data/persistence_diagram/PD2 - * \endcode - * - * the program output is: - * - * \include Kernels/kernel.txt - * - * - * \copyright GNU General Public License v3. - * \verbatim Contact: gudhi-users@lists.gforge.inria.fr \endverbatim - */ -/** @} */ // end defgroup kernel - -} // namespace kernel - -} // namespace Gudhi - -#endif // DOC_KERNEL_INTRO_KERNEL_H_ diff --git a/src/Kernels/example/CMakeLists.txt b/src/Kernels/example/CMakeLists.txt deleted file mode 100644 index d8ad4b42..00000000 --- a/src/Kernels/example/CMakeLists.txt +++ /dev/null @@ -1,10 +0,0 @@ -cmake_minimum_required(VERSION 2.6) -project(Kernels_examples) - -add_executable ( BasicEx kernel_basic_example.cpp ) - -if (TBB_FOUND) - target_link_libraries(BasicEx ${TBB_LIBRARIES}) -endif() - -add_test(NAME Kernels_example_basicex COMMAND $<TARGET_FILE:BasicEx> "${CMAKE_SOURCE_DIR}/data/persistence_diagram/PD1" "${CMAKE_SOURCE_DIR}/data/persistence_diagram/PD2")
\ No newline at end of file diff --git a/src/Kernels/example/kernel.txt b/src/Kernels/example/kernel.txt deleted file mode 100644 index 5fb8b504..00000000 --- a/src/Kernels/example/kernel.txt +++ /dev/null @@ -1,8 +0,0 @@ -SWK exact = 0.875446 -SWK approx = 0.875204 -PSSK exact = 0.0218669 -PSSK approx = 0.0213766 -LPWGK exact = 2.57351 -LPWGK approx = 2.49102 -GPWGK exact = 0.98783 -GPWGK approx = 0.987591
\ No newline at end of file diff --git a/src/Kernels/example/kernel_basic_example.cpp b/src/Kernels/example/kernel_basic_example.cpp deleted file mode 100644 index 7ecbe401..00000000 --- a/src/Kernels/example/kernel_basic_example.cpp +++ /dev/null @@ -1,65 +0,0 @@ -/* This file is part of the Gudhi Library. The Gudhi library - * (Geometric Understanding in Higher Dimensions) is a generic C++ - * library for computational topology. - * - * Authors: Mathieu Carrière - * - * Copyright (C) 2017 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/>. - */ - -#include <gudhi/kernel.h> -#include <iostream> -#include <string> -#include <fstream> -#include <sstream> - - -void usage(int nbArgs, char *const progName) { - std::cerr << "Error: Number of arguments (" << nbArgs << ") is not correct\n"; - std::cerr << "Usage: " << progName << " PD1 PD2 \n"; - std::cerr << " i.e.: " << progName << " ../../../../data/persistence_diagram/PD1.pers ../../../../data/persistence_diagram/PD2.pers \n"; - exit(-1); // ----- >> -} - -int main(int argc, char **argv) { - - if (argc != 3) usage(argc, argv[0]); - - double sigma = 2; double tau = 5; - - std::string PDname1(argv[1]); std::string PDname2(argv[2]); - std::vector< std::pair<double, double> > v1, v2; std::string line; double b,d; - - std::ifstream input1(PDname1); - while(std::getline(input1,line)){ - std::stringstream stream(line); stream >> b; stream >> d; v1.push_back(std::pair<double,double>(b,d)); - } - - std::ifstream input2(PDname2); - while(std::getline(input2,line)){ - std::stringstream stream(line); stream >> b; stream >> d; v2.push_back(std::pair<double,double>(b,d)); - } - - std::cout << "SWK exact = " << Gudhi::kernel::sliced_wasserstein_kernel (v1,v2,sigma,-1) << std::endl; - std::cout << "SWK approx = " << Gudhi::kernel::sliced_wasserstein_kernel (v1,v2,sigma) << std::endl; - std::cout << "PSSK exact = " << Gudhi::kernel::persistence_scale_space_kernel (v1,v2,sigma,-1) << std::endl; - std::cout << "PSSK approx = " << Gudhi::kernel::persistence_scale_space_kernel (v1,v2,sigma) << std::endl; - std::cout << "LPWGK exact = " << Gudhi::kernel::linear_persistence_weighted_gaussian_kernel (v1,v2,sigma,Gudhi::kernel::arctan_weight,-1) << std::endl; - std::cout << "LPWGK approx = " << Gudhi::kernel::linear_persistence_weighted_gaussian_kernel (v1,v2,sigma,Gudhi::kernel::arctan_weight) << std::endl; - std::cout << "GPWGK exact = " << Gudhi::kernel::gaussian_persistence_weighted_gaussian_kernel (v1,v2,sigma,tau,Gudhi::kernel::arctan_weight,-1) << std::endl; - std::cout << "GPWGK approx = " << Gudhi::kernel::gaussian_persistence_weighted_gaussian_kernel (v1,v2,sigma,tau,Gudhi::kernel::arctan_weight) << std::endl; - -} diff --git a/src/Kernels/include/gudhi/kernel.h b/src/Kernels/include/gudhi/kernel.h deleted file mode 100644 index 3293cc62..00000000 --- a/src/Kernels/include/gudhi/kernel.h +++ /dev/null @@ -1,365 +0,0 @@ -/* 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 Carrière - * - * Copyright (C) 2018 INRIA (France) - * - * 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 KERNEL_H_ -#define KERNEL_H_ - -#include <cstdlib> -#include <vector> -#include <algorithm> -#include <cmath> -#include <random> -#include <limits> //for numeric_limits<> -#include <utility> //for pair<> - -#include <boost/math/constants/constants.hpp> - - -namespace Gudhi { -namespace kernel { - -using PD = std::vector<std::pair<double,double> >; -double pi = boost::math::constants::pi<double>(); - - - - -// ******************************************************************** -// Utils. -// ******************************************************************** - -bool sortAngle(const std::pair<double, std::pair<int,int> >& p1, const std::pair<double, std::pair<int,int> >& p2){return (p1.first < p2.first);} -bool myComp(const std::pair<int,double> & P1, const std::pair<int,double> & P2){return P1.second < P2.second;} - -double pss_weight(std::pair<double,double> P){ - if(P.second > P.first) return 1; - else return -1; -} - -double arctan_weight(std::pair<double,double> P){ - return atan(P.second - P.first); -} - -// Compute the angle formed by two points of a PD -double compute_angle(const PD & PersDiag, const int & i, const int & j){ - std::pair<double,double> vect; double x1,y1, x2,y2; - x1 = PersDiag[i].first; y1 = PersDiag[i].second; - x2 = PersDiag[j].first; y2 = PersDiag[j].second; - if (y1 - y2 > 0){ - vect.first = y1 - y2; - vect.second = x2 - x1;} - else{ - if(y1 - y2 < 0){ - vect.first = y2 - y1; - vect.second = x1 - x2; - } - else{ - vect.first = 0; - vect.second = abs(x1 - x2);} - } - double norm = std::sqrt(vect.first*vect.first + vect.second*vect.second); - return asin(vect.second/norm); -} - -// Compute the integral of |cos()| between alpha and beta, valid only if alpha is in [-pi,pi] and beta-alpha is in [0,pi] -double compute_int_cos(const double & alpha, const double & beta){ - double res = 0; - if (alpha >= 0 && alpha <= pi){ - if (cos(alpha) >= 0){ - if(pi/2 <= beta){res = 2-sin(alpha)-sin(beta);} - else{res = sin(beta)-sin(alpha);} - } - else{ - if(1.5*pi <= beta){res = 2+sin(alpha)+sin(beta);} - else{res = sin(alpha)-sin(beta);} - } - } - if (alpha >= -pi && alpha <= 0){ - if (cos(alpha) <= 0){ - if(-pi/2 <= beta){res = 2+sin(alpha)+sin(beta);} - else{res = sin(alpha)-sin(beta);} - } - else{ - if(pi/2 <= beta){res = 2-sin(alpha)-sin(beta);} - else{res = sin(beta)-sin(alpha);} - } - } - return res; -} - -double compute_int(const double & theta1, const double & theta2, const int & p, const int & q, const PD & PD1, const PD & PD2){ - double norm = std::sqrt( (PD1[p].first-PD2[q].first)*(PD1[p].first-PD2[q].first) + (PD1[p].second-PD2[q].second)*(PD1[p].second-PD2[q].second) ); - double angle1; - if (PD1[p].first > PD2[q].first) - angle1 = theta1 - asin( (PD1[p].second-PD2[q].second)/norm ); - else - angle1 = theta1 - asin( (PD2[q].second-PD1[p].second)/norm ); - double angle2 = angle1 + theta2 - theta1; - double integral = compute_int_cos(angle1,angle2); - return norm*integral; -} - -template<class Weight = std::function<double (std::pair<double,double>) > > -std::vector<std::pair<double,double> > Fourier_feat(PD D, std::vector<std::pair<double,double> > Z, Weight weight = arctan_weight){ - int m = D.size(); std::vector<std::pair<double,double> > B; int M = Z.size(); - for(int i = 0; i < M; i++){ - double d1 = 0; double d2 = 0; double zx = Z[i].first; double zy = Z[i].second; - for(int j = 0; j < m; j++){ - double x = D[j].first; double y = D[j].second; - d1 += weight(D[j])*cos(x*zx + y*zy); - d2 += weight(D[j])*sin(x*zx + y*zy); - } - B.emplace_back(d1,d2); - } - return B; -} - -std::vector<std::pair<double,double> > random_Fourier(double sigma, int M = 1000){ - std::normal_distribution<double> distrib(0,1); std::vector<std::pair<double,double> > Z; std::random_device rd; - for(int i = 0; i < M; i++){ - std::mt19937 e1(rd()); std::mt19937 e2(rd()); - double zx = distrib(e1); double zy = distrib(e2); - Z.emplace_back(zx/sigma,zy/sigma); - } - return Z; -} - - - - - - - - - - -// ******************************************************************** -// Kernel computation. -// ******************************************************************** - - - - - -/** \brief Computes the Linear Persistence Weighted Gaussian Kernel between two persistence diagrams with random Fourier features. - * \ingroup kernel - * - * @param[in] PD1 first persistence diagram. - * @param[in] PD2 second persistence diagram. - * @param[in] sigma bandwidth parameter of the Gaussian Kernel used for the Kernel Mean Embedding of the diagrams. - * @param[in] weight weight function for the points in the diagrams. - * @param[in] M number of Fourier features (set -1 for exact computation). - * - */ -template<class Weight = std::function<double (std::pair<double,double>) > > -double linear_persistence_weighted_gaussian_kernel(const PD & PD1, const PD & PD2, double sigma, Weight weight = arctan_weight, int M = 1000){ - - if(M == -1){ - int num_pts1 = PD1.size(); int num_pts2 = PD2.size(); double k = 0; - for(int i = 0; i < num_pts1; i++) - for(int j = 0; j < num_pts2; j++) - k += weight(PD1[i])*weight(PD2[j])*exp(-((PD1[i].first-PD2[j].first)*(PD1[i].first-PD2[j].first) + (PD1[i].second-PD2[j].second)*(PD1[i].second-PD2[j].second))/(2*sigma*sigma)); - return k; - } - else{ - std::vector<std::pair<double,double> > Z = random_Fourier(sigma, M); - std::vector<std::pair<double,double> > B1 = Fourier_feat(PD1,Z,weight); - std::vector<std::pair<double,double> > B2 = Fourier_feat(PD2,Z,weight); - double d = 0; for(int i = 0; i < M; i++) d += B1[i].first*B2[i].first + B1[i].second*B2[i].second; - return d/M; - } -} - -/** \brief Computes the Persistence Scale Space Kernel between two persistence diagrams with random Fourier features. - * \ingroup kernel - * - * @param[in] PD1 first persistence diagram. - * @param[in] PD2 second persistence diagram. - * @param[in] sigma bandwidth parameter of the Gaussian Kernel used for the Kernel Mean Embedding of the diagrams. - * @param[in] M number of Fourier features (set -1 for exact computation). - * - */ -double persistence_scale_space_kernel(const PD & PD1, const PD & PD2, double sigma, int M = 1000){ - PD pd1 = PD1; int numpts = PD1.size(); for(int i = 0; i < numpts; i++) pd1.emplace_back(PD1[i].second,PD1[i].first); - PD pd2 = PD2; numpts = PD2.size(); for(int i = 0; i < numpts; i++) pd2.emplace_back(PD2[i].second,PD2[i].first); - return linear_persistence_weighted_gaussian_kernel(pd1, pd2, 2*sqrt(sigma), pss_weight, M) / (2*8*pi*sigma); -} - - -/** \brief Computes the Gaussian Persistence Weighted Gaussian Kernel between two persistence diagrams with random Fourier features. - * \ingroup kernel - * - * @param[in] PD1 first persistence diagram. - * @param[in] PD2 second persistence diagram. - * @param[in] sigma bandwidth parameter of the Gaussian Kernel used for the Kernel Mean Embedding of the diagrams. - * @param[in] tau bandwidth parameter of the Gaussian Kernel used between the embeddings. - * @param[in] weight weight function for the points in the diagrams. - * @param[in] M number of Fourier features (set -1 for exact computation). - * - */ -template<class Weight = std::function<double (std::pair<double,double>) > > -double gaussian_persistence_weighted_gaussian_kernel(const PD & PD1, const PD & PD2, double sigma, double tau, Weight weight = arctan_weight, int M = 1000){ - double k1 = linear_persistence_weighted_gaussian_kernel(PD1,PD1,sigma,weight,M); - double k2 = linear_persistence_weighted_gaussian_kernel(PD2,PD2,sigma,weight,M); - double k3 = linear_persistence_weighted_gaussian_kernel(PD1,PD2,sigma,weight,M); - return exp( - (k1+k2-2*k3) / (2*tau*tau) ); -} - - -/** \brief Computes the Sliced Wasserstein Kernel between two persistence diagrams with sampled directions. - * \ingroup kernel - * - * @param[in] PD1 first persistence diagram. - * @param[in] PD2 second persistence diagram. - * @param[in] sigma bandwidth parameter. - * @param[in] N number of points sampled on the circle (set -1 for exact computation). - * - */ -double sliced_wasserstein_kernel(PD PD1, PD PD2, double sigma, int N = 100){ - - if(N == -1){ - - // Add projections onto diagonal. - int n1, n2; n1 = PD1.size(); n2 = PD2.size(); double max_ordinate = std::numeric_limits<double>::lowest(); - for (int i = 0; i < n2; i++){ - max_ordinate = std::max(max_ordinate, PD2[i].second); - PD1.emplace_back( (PD2[i].first+PD2[i].second)/2, (PD2[i].first+PD2[i].second)/2 ); - } - for (int i = 0; i < n1; i++){ - max_ordinate = std::max(max_ordinate, PD1[i].second); - PD2.emplace_back( (PD1[i].first+PD1[i].second)/2, (PD1[i].first+PD1[i].second)/2 ); - } - int num_pts_dgm = PD1.size(); - - // Slightly perturb the points so that the PDs are in generic positions. - int mag = 0; while(max_ordinate > 10){mag++; max_ordinate/=10;} - double thresh = pow(10,-5+mag); - srand(time(NULL)); - for (int i = 0; i < num_pts_dgm; i++){ - PD1[i].first += thresh*(1.0-2.0*rand()/RAND_MAX); PD1[i].second += thresh*(1.0-2.0*rand()/RAND_MAX); - PD2[i].first += thresh*(1.0-2.0*rand()/RAND_MAX); PD2[i].second += thresh*(1.0-2.0*rand()/RAND_MAX); - } - - // Compute all angles in both PDs. - std::vector<std::pair<double, std::pair<int,int> > > angles1, angles2; - for (int i = 0; i < num_pts_dgm; i++){ - for (int j = i+1; j < num_pts_dgm; j++){ - double theta1 = compute_angle(PD1,i,j); double theta2 = compute_angle(PD2,i,j); - angles1.emplace_back(theta1, std::pair<int,int>(i,j)); - angles2.emplace_back(theta2, std::pair<int,int>(i,j)); - } - } - - // Sort angles. - std::sort(angles1.begin(), angles1.end(), sortAngle); std::sort(angles2.begin(), angles2.end(), sortAngle); - - // Initialize orders of the points of both PDs (given by ordinates when theta = -pi/2). - std::vector<int> orderp1, orderp2; - for (int i = 0; i < num_pts_dgm; i++){ orderp1.push_back(i); orderp2.push_back(i); } - std::sort( orderp1.begin(), orderp1.end(), [=](int i, int j){ if(PD1[i].second != PD1[j].second) return (PD1[i].second < PD1[j].second); else return (PD1[i].first > PD1[j].first); } ); - std::sort( orderp2.begin(), orderp2.end(), [=](int i, int j){ if(PD2[i].second != PD2[j].second) return (PD2[i].second < PD2[j].second); else return (PD2[i].first > PD2[j].first); } ); - - // Find the inverses of the orders. - std::vector<int> order1(num_pts_dgm); std::vector<int> order2(num_pts_dgm); - for(int i = 0; i < num_pts_dgm; i++) for (int j = 0; j < num_pts_dgm; j++) if(orderp1[j] == i){ order1[i] = j; break; } - for(int i = 0; i < num_pts_dgm; i++) for (int j = 0; j < num_pts_dgm; j++) if(orderp2[j] == i){ order2[i] = j; break; } - - // Record all inversions of points in the orders as theta varies along the positive half-disk. - std::vector<std::vector<std::pair<int,double> > > anglePerm1(num_pts_dgm); - std::vector<std::vector<std::pair<int,double> > > anglePerm2(num_pts_dgm); - - int M1 = angles1.size(); - for (int i = 0; i < M1; i++){ - double theta = angles1[i].first; int p = angles1[i].second.first; int q = angles1[i].second.second; - anglePerm1[order1[p]].emplace_back(p,theta); - anglePerm1[order1[q]].emplace_back(q,theta); - int a = order1[p]; int b = order1[q]; order1[p] = b; order1[q] = a; - } - - int M2 = angles2.size(); - for (int i = 0; i < M2; i++){ - double theta = angles2[i].first; int p = angles2[i].second.first; int q = angles2[i].second.second; - anglePerm2[order2[p]].emplace_back(p,theta); - anglePerm2[order2[q]].emplace_back(q,theta); - int a = order2[p]; int b = order2[q]; order2[p] = b; order2[q] = a; - } - - for (int i = 0; i < num_pts_dgm; i++){ - anglePerm1[order1[i]].emplace_back(i,pi/2); - anglePerm2[order2[i]].emplace_back(i,pi/2); - } - - // Compute the SW distance with the list of inversions. - double sw = 0; - for (int i = 0; i < num_pts_dgm; i++){ - std::vector<std::pair<int,double> > U,V; U = anglePerm1[i]; V = anglePerm2[i]; - double theta1, theta2; theta1 = -pi/2; - unsigned int ku, kv; ku = 0; kv = 0; theta2 = std::min(U[ku].second,V[kv].second); - while(theta1 != pi/2){ - if(PD1[U[ku].first].first != PD2[V[kv].first].first || PD1[U[ku].first].second != PD2[V[kv].first].second) - if(theta1 != theta2) - sw += compute_int(theta1, theta2, U[ku].first, V[kv].first, PD1, PD2); - theta1 = theta2; - if ( (theta2 == U[ku].second) && ku < U.size()-1 ) ku++; - if ( (theta2 == V[kv].second) && kv < V.size()-1 ) kv++; - theta2 = std::min(U[ku].second, V[kv].second); - } - } - - return exp( -(sw/pi)/(2*sigma*sigma) ); - - } - - - else{ - double step = pi/N; double sw = 0; - - // Add projections onto diagonal. - int n1, n2; n1 = PD1.size(); n2 = PD2.size(); - for (int i = 0; i < n2; i++) - PD1.emplace_back( (PD2[i].first + PD2[i].second)/2, (PD2[i].first + PD2[i].second)/2 ); - for (int i = 0; i < n1; i++) - PD2.emplace_back( (PD1[i].first + PD1[i].second)/2, (PD1[i].first + PD1[i].second)/2 ); - int n = PD1.size(); - - // Sort and compare all projections. - //#pragma omp parallel for - for (int i = 0; i < N; i++){ - std::vector<std::pair<int,double> > L1, L2; - for (int j = 0; j < n; j++){ - L1.emplace_back( j, PD1[j].first*cos(-pi/2+i*step) + PD1[j].second*sin(-pi/2+i*step) ); - L2.emplace_back( j, PD2[j].first*cos(-pi/2+i*step) + PD2[j].second*sin(-pi/2+i*step) ); - } - std::sort(L1.begin(),L1.end(), myComp); std::sort(L2.begin(),L2.end(), myComp); - double f = 0; for (int j = 0; j < n; j++) f += std::abs(L1[j].second - L2[j].second); - sw += f*step; - } - return exp( -(sw/pi)/(2*sigma*sigma) ); - } -} - - -} // namespace kernel - -} // namespace Gudhi - -#endif //KERNEL_H_ diff --git a/src/Kernels/test/CMakeLists.txt b/src/Kernels/test/CMakeLists.txt deleted file mode 100644 index 95c72a7f..00000000 --- a/src/Kernels/test/CMakeLists.txt +++ /dev/null @@ -1,12 +0,0 @@ -cmake_minimum_required(VERSION 2.6) -project(kernel_tests) - -include(GUDHI_test_coverage) - -add_executable ( kernel_test_unit test_kernel.cpp ) -target_link_libraries(kernel_test_unit ${Boost_UNIT_TEST_FRAMEWORK_LIBRARY}) -if (TBB_FOUND) - target_link_libraries(kernel_test_unit ${TBB_LIBRARIES}) -endif() - -gudhi_add_coverage_test(kernel_test_unit) diff --git a/src/Kernels/test/test_kernel.cpp b/src/Kernels/test/test_kernel.cpp deleted file mode 100644 index db05fd28..00000000 --- a/src/Kernels/test/test_kernel.cpp +++ /dev/null @@ -1,56 +0,0 @@ -/* 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 Carrière - * - * Copyright (C) 2017 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/>. - */ - -#define BOOST_TEST_DYN_LINK -#define BOOST_TEST_MODULE "kernel" - -#include <boost/test/unit_test.hpp> -#include <cmath> // float comparison -#include <limits> -#include <string> -#include <vector> -#include <algorithm> // std::max -#include <gudhi/kernel.h> -#include <gudhi/distance_functions.h> -#include <gudhi/reader_utils.h> - -BOOST_AUTO_TEST_CASE(check_PSS) { - std::vector< std::pair<double, double> > v1, v2; - v1.emplace_back(std::pair<double,double>(0,1)); - v2.emplace_back(std::pair<double,double>(0,2)); - BOOST_CHECK(std::abs(Gudhi::kernel::pssk(v1,v2,1) - Gudhi::kernel::approx_pssk(v1,v2,1)) <= 1e-1); -} - -BOOST_AUTO_TEST_CASE(check_PWG) { - std::vector< std::pair<double, double> > v1, v2; - v1.emplace_back(std::pair<double,double>(0,1)); - v2.emplace_back(std::pair<double,double>(0,2)); - BOOST_CHECK(std::abs(Gudhi::kernel::lpwgk(v1,v2,1) - Gudhi::kernel::approx_lpwgk(v1,v2,1)) <= 1e-1); - BOOST_CHECK(std::abs(Gudhi::kernel::gpwgk(v1,v2,1,1) - Gudhi::kernel::approx_gpwgk(v1,v2,1,1)) <= 1e-1); -} - -BOOST_AUTO_TEST_CASE(check_SW) { - std::vector< std::pair<double, double> > v1, v2; - v2.emplace_back(std::pair<double,double>(0,2)); - BOOST_CHECK(std::abs(Gudhi::kernel::sw(v1,v2) - Gudhi::kernel::approx_sw(v1,v2)) <= 1e-3); - BOOST_CHECK(std::abs(Gudhi::kernel::sw(v1,v2) - 2*std::sqrt(2)/3.1415) <= 1e-3); -} diff --git a/src/Persistence_representations/example/CMakeLists.txt b/src/Persistence_representations/example/CMakeLists.txt index 79d39c4d..89284e38 100644 --- a/src/Persistence_representations/example/CMakeLists.txt +++ b/src/Persistence_representations/example/CMakeLists.txt @@ -37,3 +37,12 @@ add_test(NAME Persistence_weighted_gaussian COMMAND $<TARGET_FILE:Persistence_weighted_gaussian>) install(TARGETS Persistence_weighted_gaussian DESTINATION bin) +add_executable ( Persistence_image persistence_image.cpp ) +add_test(NAME Persistence_image + COMMAND $<TARGET_FILE:Persistence_image>) +install(TARGETS Persistence_image DESTINATION bin) + +add_executable ( Landscape landscape.cpp ) +add_test(NAME Landscape + COMMAND $<TARGET_FILE:Landscape>) +install(TARGETS Landscape DESTINATION bin) diff --git a/src/Persistence_representations/example/landscape.cpp b/src/Persistence_representations/example/landscape.cpp new file mode 100644 index 00000000..5fa84a7c --- /dev/null +++ b/src/Persistence_representations/example/landscape.cpp @@ -0,0 +1,51 @@ +/* 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 (France) + * + * 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/>. + */ + +#include <gudhi/Landscape.h> + +#include <iostream> +#include <vector> +#include <utility> + +using LS = Gudhi::Persistence_representations::Landscape; + +int main(int argc, char** argv) { + + std::vector<std::pair<double, double> > persistence; + + persistence.push_back(std::make_pair(1, 2)); + persistence.push_back(std::make_pair(6, 8)); + persistence.push_back(std::make_pair(0, 4)); + persistence.push_back(std::make_pair(3, 8)); + + int nb_ls = 3; double min_x = 0.0; double max_x = 10.0; int res_x = 100; + + LS ls(persistence, nb_ls, min_x, max_x, res_x); + std::vector<std::vector<double> > L = ls.vectorize(); + + for(int i = 0; i < nb_ls; i++){ + for(int j = 0; j < res_x; j++) std::cout << L[i][j] << " "; + std::cout << std::endl; + } + + return 0; +} diff --git a/src/Persistence_representations/example/persistence_image.cpp b/src/Persistence_representations/example/persistence_image.cpp new file mode 100644 index 00000000..dfa469d4 --- /dev/null +++ b/src/Persistence_representations/example/persistence_image.cpp @@ -0,0 +1,54 @@ +/* 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 (France) + * + * 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/>. + */ + +#include <gudhi/Persistence_image.h> +#include <gudhi/Persistence_weighted_gaussian.h> + +#include <iostream> +#include <vector> +#include <utility> +#include <string> + +using PI = Gudhi::Persistence_representations::Persistence_image; +using Weight = std::function<double (std::pair<double,double>) >; + +int main(int argc, char** argv) { + + std::vector<std::pair<double, double> > persistence; + + persistence.push_back(std::make_pair(1, 2)); + persistence.push_back(std::make_pair(6, 8)); + persistence.push_back(std::make_pair(0, 4)); + persistence.push_back(std::make_pair(3, 8)); + + double min_x = 0.0; double max_x = 10.0; int res_x = 100; double min_y = 0.0; double max_y = 10.0; int res_y = 100; double sigma = 1.0; Weight weight = Gudhi::Persistence_representations::Persistence_weighted_gaussian::linear_weight; + + PI pim(persistence, min_x, max_x, res_x, min_y, max_y, res_y, weight, sigma); + std::vector<std::vector<double> > P = pim.vectorize(); + + for(int i = 0; i < res_y; i++){ + for(int j = 0; j < res_x; j++) std::cout << P[i][j] << " "; + std::cout << std::endl; + } + + return 0; +} diff --git a/src/Persistence_representations/example/persistence_weighted_gaussian.cpp b/src/Persistence_representations/example/persistence_weighted_gaussian.cpp index dea5dab6..234f6323 100644 --- a/src/Persistence_representations/example/persistence_weighted_gaussian.cpp +++ b/src/Persistence_representations/example/persistence_weighted_gaussian.cpp @@ -48,11 +48,11 @@ int main(int argc, char** argv) { double tau = 1; int m = 10000; - PWG PWG1(persistence1, sigma, m, PWG::arctan_weight); - PWG PWG2(persistence2, sigma, m, PWG::arctan_weight); + PWG PWG1(persistence1, sigma, m, PWG::arctan_weight(1,1)); + PWG PWG2(persistence2, sigma, m, PWG::arctan_weight(1,1)); - PWG PWGex1(persistence1, sigma, -1, PWG::arctan_weight); - PWG PWGex2(persistence2, sigma, -1, PWG::arctan_weight); + PWG PWGex1(persistence1, sigma, -1, PWG::arctan_weight(1,1)); + PWG PWGex2(persistence2, sigma, -1, PWG::arctan_weight(1,1)); // Linear PWG diff --git a/src/Persistence_representations/include/gudhi/Landscape.h b/src/Persistence_representations/include/gudhi/Landscape.h new file mode 100644 index 00000000..d6608a57 --- /dev/null +++ b/src/Persistence_representations/include/gudhi/Landscape.h @@ -0,0 +1,103 @@ +/* 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 (France) + * + * 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 LANDSCAPE_H_ +#define LANDSCAPE_H_ + +// gudhi include +#include <gudhi/read_persistence_from_file.h> +#include <gudhi/common_persistence_representations.h> +#include <gudhi/Debug_utils.h> + +// standard include +#include <cmath> +#include <iostream> +#include <vector> +#include <limits> +#include <fstream> +#include <sstream> +#include <algorithm> +#include <string> +#include <utility> +#include <functional> + +using PD = std::vector<std::pair<double,double> >; + +namespace Gudhi { +namespace Persistence_representations { + +/** + * \class Landscape gudhi/Landscape.h + * \brief A class implementing the Landscapes. + * + * \ingroup Persistence_representations + * + * \details + * +**/ + +class Landscape { + + protected: + PD diagram; + int res_x, nb_ls; + double min_x, max_x; + + public: + + /** \brief Landscape constructor. + * \ingroup Landscape + * + */ + Landscape(PD _diagram, int _nb_ls = 5, double _min_x = 0.0, double _max_x = 1.0, int _res_x = 10){diagram = _diagram; nb_ls = _nb_ls; min_x = _min_x; max_x = _max_x; res_x = _res_x;} + + /** \brief Computes the landscape of a diagram. + * \ingroup Landscape + * + */ + std::vector<std::vector<double> > vectorize() { + std::vector<std::vector<double> > ls; for(int i = 0; i < nb_ls; i++) ls.emplace_back(); + int num_pts = diagram.size(); double step = (max_x - min_x)/res_x; + + for(int i = 0; i < res_x; i++){ + double x = min_x + i*step; double t = x / std::sqrt(2); std::vector<double> events; + for(int j = 0; j < num_pts; j++){ + double px = diagram[j].first; double py = diagram[j].second; + if(t >= px && t <= py){ if(t >= (px+py)/2) events.push_back(std::sqrt(2)*(py-t)); else events.push_back(std::sqrt(2)*(t-px)); } + } + + std::sort(events.begin(), events.end(), [](const double & a, const double & b){return a > b;}); int nb_events = events.size(); + for (int j = 0; j < nb_ls; j++){ if(j < nb_events) ls[j].push_back(events[j]); else ls[j].push_back(0); } + } + + return ls; + } + + + + +}; + +} // namespace Landscape +} // namespace Gudhi + +#endif // LANDSCAPE_H_ diff --git a/src/Persistence_representations/include/gudhi/Persistence_image.h b/src/Persistence_representations/include/gudhi/Persistence_image.h new file mode 100644 index 00000000..6c9f75b7 --- /dev/null +++ b/src/Persistence_representations/include/gudhi/Persistence_image.h @@ -0,0 +1,117 @@ +/* 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 (France) + * + * 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 PERSISTENCE_IMAGE_H_ +#define PERSISTENCE_IMAGE_H_ + +// gudhi include +#include <gudhi/read_persistence_from_file.h> +#include <gudhi/common_persistence_representations.h> +#include <gudhi/Debug_utils.h> +#include <gudhi/Persistence_weighted_gaussian.h> + +// standard include +#include <cmath> +#include <iostream> +#include <vector> +#include <limits> +#include <fstream> +#include <sstream> +#include <algorithm> +#include <string> +#include <utility> +#include <functional> + +using PD = std::vector<std::pair<double,double> >; +using Weight = std::function<double (std::pair<double,double>) >; + +namespace Gudhi { +namespace Persistence_representations { + +/** + * \class Persistence_image gudhi/Persistence_image.h + * \brief A class implementing the Persistence Images. + * + * \ingroup Persistence_representations + * + * \details + * +**/ + +class Persistence_image { + + protected: + PD diagram; + int res_x, res_y; + double min_x, max_x, min_y, max_y; + Weight weight; + double sigma; + + public: + + /** \brief Persistence Image constructor. + * \ingroup Persistence_image + * + */ + Persistence_image(PD _diagram, double _min_x = 0.0, double _max_x = 1.0, int _res_x = 10, double _min_y = 0.0, double _max_y = 1.0, int _res_y = 10, + Weight _weight = Gudhi::Persistence_representations::Persistence_weighted_gaussian::arctan_weight(1,1), double _sigma = 1.0){ + diagram = _diagram; min_x = _min_x; max_x = _max_x; res_x = _res_x; min_y = _min_y; max_y = _max_y; res_y = _res_y, weight = _weight; sigma = _sigma; + } + + /** \brief Computes the persistence image of a diagram. + * \ingroup Persistence_image + * + */ + std::vector<std::vector<double> > vectorize() { + std::vector<std::vector<double> > im; for(int i = 0; i < res_y; i++) im.emplace_back(); + double step_x = (max_x - min_x)/res_x; double step_y = (max_y - min_y)/res_y; + + int num_pts = diagram.size(); + + for(int i = 0; i < res_y; i++){ + double y = min_y + i*step_y; + for(int j = 0; j < res_x; j++){ + double x = min_x + j*step_x; + + double pixel_value = 0; + for(int k = 0; k < num_pts; k++){ + double px = diagram[k].first; double py = diagram[k].second; + pixel_value += weight(std::pair<double,double>(px,py)) * std::exp( -((x-px)*(x-px) + (y-(py-px))*(y-(py-px))) / (2*sigma*sigma) ) / (sigma*std::sqrt(2*pi)); + } + im[i].push_back(pixel_value); + + } + } + + return im; + + } + + + + +}; + +} // namespace Persistence_image +} // namespace Gudhi + +#endif // PERSISTENCE_IMAGE_H_ diff --git a/src/Persistence_representations/include/gudhi/Persistence_weighted_gaussian.h b/src/Persistence_representations/include/gudhi/Persistence_weighted_gaussian.h index b30e0273..9a63fccd 100644 --- a/src/Persistence_representations/include/gudhi/Persistence_weighted_gaussian.h +++ b/src/Persistence_representations/include/gudhi/Persistence_weighted_gaussian.h @@ -97,7 +97,7 @@ class Persistence_weighted_gaussian{ * @param[in] _weight weight function for the points in the diagrams. * */ - Persistence_weighted_gaussian(PD _diagram, double _sigma = 1.0, int _approx = 1000, Weight _weight = arctan_weight){diagram = _diagram; sigma = _sigma; approx = _approx; weight = _weight;} + Persistence_weighted_gaussian(PD _diagram, double _sigma = 1.0, int _approx = 1000, Weight _weight = arctan_weight(1,1)){diagram = _diagram; sigma = _sigma; approx = _approx; weight = _weight;} PD get_diagram() const {return this->diagram;} double get_sigma() const {return this->sigma;} @@ -115,16 +115,13 @@ class Persistence_weighted_gaussian{ * @param[in] p point in 2D. * */ - static double pss_weight(std::pair<double,double> p){ - if(p.second > p.first) return 1; - else return -1; - } + static double pss_weight(std::pair<double,double> p) {if(p.second > p.first) return 1; else return -1;} + static double linear_weight(std::pair<double,double> p) {return std::abs(p.second - p.first);} + static double const_weight(std::pair<double,double> p) {return 1;} + static std::function<double (std::pair<double,double>) > arctan_weight(double C, double power) {return [=](std::pair<double,double> p){return C * atan(std::pow(std::abs(p.second - p.first), power));};} - static double arctan_weight(std::pair<double,double> p){ - return atan(p.second - p.first); - } - std::vector<std::pair<double,double> > Fourier_feat(PD diag, std::vector<std::pair<double,double> > z, Weight weight = arctan_weight){ + std::vector<std::pair<double,double> > Fourier_feat(PD diag, std::vector<std::pair<double,double> > z, Weight weight = arctan_weight(1,1)){ int md = diag.size(); std::vector<std::pair<double,double> > b; int mz = z.size(); for(int i = 0; i < mz; i++){ double d1 = 0; double d2 = 0; double zx = z[i].first; double zy = z[i].second; 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 <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 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<std::pair<double, double>>& diag1, - const std::vector<std::pair<double, double>>& diag2, - double sigma, int N) { + + // ******************* + // 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) { - 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<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::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<std::pair<double, double>>& diag1, - const std::vector<std::pair<double, double>>& diag2, - double sigma, int N) { + 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); @@ -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<std::pair<double, double>>& diag1, - const std::vector<std::pair<double, double>>& diag2, - double sigma, int N) { + 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::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<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){ + // **************** + // 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);} @@ -87,22 +86,17 @@ namespace persistence_diagram { 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){ + 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)); matrix.push_back(ps); + 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(); + 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); |