diff options
author | vrouvrea <vrouvrea@636b058d-ea47-450e-bf9e-a15bfbe3eedb> | 2016-12-01 23:11:43 +0000 |
---|---|---|
committer | vrouvrea <vrouvrea@636b058d-ea47-450e-bf9e-a15bfbe3eedb> | 2016-12-01 23:11:43 +0000 |
commit | 658e2ad845801f3b2a7a349e499763d7f28a8bc9 (patch) | |
tree | a9434fb7cb1b0799a2f23e12d5d3d1a53229fad7 /src/cython | |
parent | fcfc9de5eb7e309c0ac309f57e26672c31bbc836 (diff) | |
parent | 58c77ecd1c09c1cf116be44c1a7e9d18cd99970b (diff) |
Merge last trunk modifications
add Py test for subsampling
Add choose_n_furthest from off file
git-svn-id: svn+ssh://scm.gforge.inria.fr/svnroot/gudhi/branches/ST_cythonize@1810 636b058d-ea47-450e-bf9e-a15bfbe3eedb
Former-commit-id: a23746a3dd427edc958b375f9f0ac4b4d2901fcb
Diffstat (limited to 'src/cython')
-rw-r--r-- | src/cython/CMakeLists.txt | 2 | ||||
-rw-r--r-- | src/cython/cython/alpha_complex.pyx | 2 | ||||
-rw-r--r-- | src/cython/cython/subsampling.pyx | 40 | ||||
-rw-r--r-- | src/cython/include/Subsampling_interface.h | 22 | ||||
-rwxr-xr-x | src/cython/test/test_subsampling.py | 94 |
5 files changed, 155 insertions, 5 deletions
diff --git a/src/cython/CMakeLists.txt b/src/cython/CMakeLists.txt index c2026682..998908e7 100644 --- a/src/cython/CMakeLists.txt +++ b/src/cython/CMakeLists.txt @@ -47,7 +47,7 @@ if(PYTHON_PATH AND CYTHON_PATH) file(COPY include DESTINATION ${CMAKE_CURRENT_BINARY_DIR}) file(COPY cython DESTINATION ${CMAKE_CURRENT_BINARY_DIR}) file(COPY test DESTINATION ${CMAKE_CURRENT_BINARY_DIR}) - + if (CGAL_FOUND) if (NOT CGAL_VERSION VERSION_LESS 4.8.1) # If CGAL_VERSION >= 4.8.1, include subsampling diff --git a/src/cython/cython/alpha_complex.pyx b/src/cython/cython/alpha_complex.pyx index 56cf925c..6b27594a 100644 --- a/src/cython/cython/alpha_complex.pyx +++ b/src/cython/cython/alpha_complex.pyx @@ -62,7 +62,7 @@ cdef class AlphaComplex: cdef Alpha_complex_interface * thisptr # Fake constructor that does nothing but documenting the constructor - def __init__(self, points=None, off_file=''): + def __init__(self, points=[], off_file=''): """AlphaComplex constructor. :param points: A list of points in d-Dimension. diff --git a/src/cython/cython/subsampling.pyx b/src/cython/cython/subsampling.pyx index e59e0c6a..5ca38099 100644 --- a/src/cython/cython/subsampling.pyx +++ b/src/cython/cython/subsampling.pyx @@ -32,6 +32,42 @@ __license__ = "GPL v3" cdef extern from "Subsampling_interface.h" namespace "Gudhi::subsampling": vector[vector[double]] subsampling_n_farthest_points(vector[vector[double]] points, unsigned nb_points) + vector[vector[double]] subsampling_n_farthest_points(vector[vector[double]] points, unsigned nb_points, unsigned starting_point) + vector[vector[double]] subsampling_n_farthest_points_from_file(string off_file, unsigned nb_points) + vector[vector[double]] subsampling_n_farthest_points_from_file(string off_file, unsigned nb_points, unsigned starting_point) -def choose_n_farthest_points(points, nb_points): - subsampling_n_farthest_points(points, nb_points) +def choose_n_farthest_points(points=[], off_file='', nb_points=0, starting_point = ''): + """Subsample by a greedy strategy of iteratively adding the farthest point + from the current chosen point set to the subsampling. + The iteration starts with the landmark `starting point`. + + :param points: The input point set. + :type points: vector[vector[double]]. + + Or + + :param off_file: An OFF file style name. + :type off_file: string + + :param nb_points: Number of points of the subsample. + :type nb_points: unsigned. + :param starting_point: The iteration starts with the landmark `starting \ + point`,which is the index of the poit to start with. If not set, this \ + index is choosen randomly. + :type starting_point: unsigned. + :returns: The subsamplepoint set. + :rtype: vector[vector[double]] + """ + if off_file is not '': + if os.path.isfile(off_file): + if starting_point is '': + return subsampling_n_farthest_points_from_file(off_file, nb_points) + else: + return subsampling_n_farthest_points_from_file(off_file, nb_points, starting_point) + else: + print("file " + off_file + " not found.") + else: + if starting_point is '': + return subsampling_n_farthest_points(points, nb_points) + else: + return subsampling_n_farthest_points(points, nb_points, starting_point) diff --git a/src/cython/include/Subsampling_interface.h b/src/cython/include/Subsampling_interface.h index bd37a015..12c48012 100644 --- a/src/cython/include/Subsampling_interface.h +++ b/src/cython/include/Subsampling_interface.h @@ -45,12 +45,32 @@ std::vector<std::vector<double>> subsampling_n_farthest_points(std::vector<std:: std::vector<std::vector<double>> landmarks; Subsampling_dynamic_kernel k; choose_n_farthest_points(k, points, nb_points, std::back_inserter(landmarks)); - std::cout << "output " << landmarks.size() << std::endl; + return landmarks; +} + +std::vector<std::vector<double>> subsampling_n_farthest_points(std::vector<std::vector<double>>& points, unsigned nb_points, unsigned starting_point) { + std::vector<Subsampling_point_d> input, output; + for (auto point : points) + input.push_back(Subsampling_point_d(point.size(), point.begin(), point.end())); + std::vector<std::vector<double>> landmarks; + Subsampling_dynamic_kernel k; + choose_n_farthest_points(k, points, nb_points, starting_point, std::back_inserter(landmarks)); return landmarks; } +std::vector<std::vector<double>> subsampling_n_farthest_points_from_file(std::string& off_file, unsigned nb_points) { + Gudhi::Points_off_reader<std::vector<double>> off_reader(off_file); + std::vector<std::vector<double>> points = off_reader.get_point_cloud(); + return subsampling_n_farthest_points(points, nb_points); +} + +std::vector<std::vector<double>> subsampling_n_farthest_points_from_file(std::string& off_file, unsigned nb_points, unsigned starting_point) { + Gudhi::Points_off_reader<std::vector<double>> off_reader(off_file); + std::vector<std::vector<double>> points = off_reader.get_point_cloud(); + return subsampling_n_farthest_points(points, nb_points, starting_point); +} } // namespace subsampling } // namespace Gudhi diff --git a/src/cython/test/test_subsampling.py b/src/cython/test/test_subsampling.py new file mode 100755 index 00000000..e5f2d70a --- /dev/null +++ b/src/cython/test/test_subsampling.py @@ -0,0 +1,94 @@ +import gudhi +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): Vincent Rouvreau + + Copyright (C) 2016 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__ = "Vincent Rouvreau" +__copyright__ = "Copyright (C) 2016 INRIA" +__license__ = "GPL v3" + + +def test_write_off_file_for_tests(): + file = open("n_farthest.off", "w") + file.write("nOFF\n") + file.write("2 7 0 0\n") + file.write("1.0 1.0\n") + file.write("7.0 0.0\n") + file.write("4.0 6.0\n") + file.write("9.0 6.0\n") + file.write("0.0 14.0\n") + file.write("2.0 19.0\n") + file.write("9.0 17.0\n") + file.close() + +def test_simple_choose_n_farthest_points_with_a_starting_point(): + point_set = [[0,1], [0,0], [1,0], [1,1]] + i = 0 + for point in point_set: + # The iteration starts with the given starting point + sub_set = gudhi.choose_n_farthest_points(points = point_set, nb_points = 1, starting_point = i) + assert sub_set[0] == point_set[i] + i = i + 1 + + # The iteration finds then the farthest + sub_set = gudhi.choose_n_farthest_points(points = point_set, nb_points = 2, starting_point = 1) + assert sub_set[1] == point_set[3] + sub_set = gudhi.choose_n_farthest_points(points = point_set, nb_points = 2, starting_point = 3) + assert sub_set[1] == point_set[1] + sub_set = gudhi.choose_n_farthest_points(points = point_set, nb_points = 2, starting_point = 0) + assert sub_set[1] == point_set[2] + sub_set = gudhi.choose_n_farthest_points(points = point_set, nb_points = 2, starting_point = 2) + assert sub_set[1] == point_set[0] + + # Test the limits + assert gudhi.choose_n_farthest_points(points = [], nb_points = 0, starting_point = 0) == [] + assert gudhi.choose_n_farthest_points(points = [], nb_points = 1, starting_point = 0) == [] + assert gudhi.choose_n_farthest_points(points = [], nb_points = 0, starting_point = 1) == [] + assert gudhi.choose_n_farthest_points(points = [], nb_points = 1, starting_point = 1) == [] + + print(os.getcwd()) + # From off file test + for i in range (0, 7): + assert len(gudhi.choose_n_farthest_points(off_file = 'n_farthest.off', nb_points = i, starting_point = i)) == i + +def test_simple_choose_n_farthest_points_randomed(): + point_set = [[0,1], [0,0], [1,0], [1,1]] + + # Test the limits + assert gudhi.choose_n_farthest_points(points = [], nb_points = 0) == [] + assert gudhi.choose_n_farthest_points(points = [], nb_points = 1) == [] + assert gudhi.choose_n_farthest_points(points = point_set, nb_points = 0) == [] + # Go furter than point set on purpose + for iter in range(1,10): + sub_set = gudhi.choose_n_farthest_points(points = point_set, nb_points = iter) + for sub in sub_set: + found = False + for point in point_set: + if point == sub: + found = True + assert found == True + + print(os.getcwd()) + # From off file test + for i in range (0, 7): + assert len(gudhi.choose_n_farthest_points(off_file = 'n_farthest.off', nb_points = i)) == i |