From 9a3373a0db722c75994826d44b4cfbe1b7b5aeb0 Mon Sep 17 00:00:00 2001 From: vrouvrea Date: Fri, 2 Dec 2016 13:26:47 +0000 Subject: pick_n_random_points cythonization git-svn-id: svn+ssh://scm.gforge.inria.fr/svnroot/gudhi/branches/ST_cythonize@1814 636b058d-ea47-450e-bf9e-a15bfbe3eedb Former-commit-id: 696c9b39cb05e69e2d9c2a3ac3f5400c7e518691 --- .../include/gudhi/pick_n_random_points.h | 4 ++- src/cython/cython/subsampling.pyx | 26 ++++++++++++++++++ src/cython/include/Subsampling_interface.h | 20 ++++++++++++++ src/cython/test/test_subsampling.py | 31 +++++++++++++++++----- 4 files changed, 73 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/Subsampling/include/gudhi/pick_n_random_points.h b/src/Subsampling/include/gudhi/pick_n_random_points.h index e89b2b2d..f0e3f1f1 100644 --- a/src/Subsampling/include/gudhi/pick_n_random_points.h +++ b/src/Subsampling/include/gudhi/pick_n_random_points.h @@ -57,7 +57,9 @@ void pick_n_random_points(Point_container const &points, #endif std::size_t nbP = boost::size(points); - assert(nbP >= final_size); + if (final_size > nbP) + final_size = nbP; + std::vector landmarks(nbP); std::iota(landmarks.begin(), landmarks.end(), 0); diff --git a/src/cython/cython/subsampling.pyx b/src/cython/cython/subsampling.pyx index 5ca38099..c71f5810 100644 --- a/src/cython/cython/subsampling.pyx +++ b/src/cython/cython/subsampling.pyx @@ -35,6 +35,8 @@ cdef extern from "Subsampling_interface.h" namespace "Gudhi::subsampling": 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) + vector[vector[double]] subsampling_n_random_points(vector[vector[double]] points, unsigned nb_points) + vector[vector[double]] subsampling_n_random_points_from_file(string off_file, unsigned 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 @@ -71,3 +73,27 @@ def choose_n_farthest_points(points=[], off_file='', nb_points=0, starting_point return subsampling_n_farthest_points(points, nb_points) else: return subsampling_n_farthest_points(points, nb_points, starting_point) + +def pick_n_random_points(points=[], off_file='', nb_points=0): + """Subsample a point set by picking random vertices. + + :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. + :returns: The subsamplepoint set. + :rtype: vector[vector[double]] + """ + if off_file is not '': + if os.path.isfile(off_file): + return subsampling_n_random_points_from_file(off_file, nb_points) + else: + print("file " + off_file + " not found.") + else: + return subsampling_n_random_points(points, nb_points) diff --git a/src/cython/include/Subsampling_interface.h b/src/cython/include/Subsampling_interface.h index 12c48012..8ef4fea1 100644 --- a/src/cython/include/Subsampling_interface.h +++ b/src/cython/include/Subsampling_interface.h @@ -38,6 +38,7 @@ using Subsampling_dynamic_kernel = CGAL::Epick_d< CGAL::Dynamic_dimension_tag >; using Subsampling_point_d = Subsampling_dynamic_kernel::Point_d; using Subsampling_ft = Subsampling_dynamic_kernel::FT; +// ------ choose_n_farthest_points ------ std::vector> subsampling_n_farthest_points(std::vector>& points, unsigned nb_points) { std::vector input, output; for (auto point : points) @@ -71,6 +72,25 @@ std::vector> subsampling_n_farthest_points_from_file(std::st std::vector> points = off_reader.get_point_cloud(); return subsampling_n_farthest_points(points, nb_points, starting_point); } + +// ------ pick_n_random_points ------ +std::vector> subsampling_n_random_points(std::vector>& points, unsigned nb_points) { + std::vector input, output; + for (auto point : points) + input.push_back(Subsampling_point_d(point.size(), point.begin(), point.end())); + std::vector> landmarks; + pick_n_random_points(points, nb_points, std::back_inserter(landmarks)); + + return landmarks; +} + +std::vector> subsampling_n_random_points_from_file(std::string& off_file, unsigned nb_points) { + Gudhi::Points_off_reader> off_reader(off_file); + std::vector> points = off_reader.get_point_cloud(); + return subsampling_n_random_points(points, nb_points); +} + + } // namespace subsampling } // namespace Gudhi diff --git a/src/cython/test/test_subsampling.py b/src/cython/test/test_subsampling.py index e5f2d70a..2dc12a89 100755 --- a/src/cython/test/test_subsampling.py +++ b/src/cython/test/test_subsampling.py @@ -1,5 +1,4 @@ import gudhi -import os """This file is part of the Gudhi Library. The Gudhi library (Geometric Understanding in Higher Dimensions) is a generic C++ @@ -29,7 +28,7 @@ __license__ = "GPL v3" def test_write_off_file_for_tests(): - file = open("n_farthest.off", "w") + file = open("subsample.off", "w") file.write("nOFF\n") file.write("2 7 0 0\n") file.write("1.0 1.0\n") @@ -66,18 +65,17 @@ def test_simple_choose_n_farthest_points_with_a_starting_point(): 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 + assert len(gudhi.choose_n_farthest_points(off_file = 'subsample.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) @@ -86,9 +84,28 @@ def test_simple_choose_n_farthest_points_randomed(): for point in point_set: if point == sub: found = True + # Check each sub set point is existing in the point set 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 + assert len(gudhi.choose_n_farthest_points(off_file = 'subsample.off', nb_points = i)) == i + +def test_simple_pick_n_random_points(): + point_set = [[0,1], [0,0], [1,0], [1,1]] + # Test the limits + assert gudhi.pick_n_random_points(points = [], nb_points = 0) == [] + assert gudhi.pick_n_random_points(points = [], nb_points = 1) == [] + assert gudhi.pick_n_random_points(points = point_set, nb_points = 0) == [] + + # Go furter than point set on purpose + for iter in range(1,10): + sub_set = gudhi.pick_n_random_points(points = point_set, nb_points = iter) + print(5) + for sub in sub_set: + found = False + for point in point_set: + if point == sub: + found = True + # Check each sub set point is existing in the point set + assert found == True -- cgit v1.2.3