summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorvrouvrea <vrouvrea@636b058d-ea47-450e-bf9e-a15bfbe3eedb>2016-12-02 13:26:47 +0000
committervrouvrea <vrouvrea@636b058d-ea47-450e-bf9e-a15bfbe3eedb>2016-12-02 13:26:47 +0000
commit9a3373a0db722c75994826d44b4cfbe1b7b5aeb0 (patch)
treec83cabae516d675fd7a27a673da1913835b1dcd5 /src
parent658e2ad845801f3b2a7a349e499763d7f28a8bc9 (diff)
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
Diffstat (limited to 'src')
-rw-r--r--src/Subsampling/include/gudhi/pick_n_random_points.h4
-rw-r--r--src/cython/cython/subsampling.pyx26
-rw-r--r--src/cython/include/Subsampling_interface.h20
-rwxr-xr-xsrc/cython/test/test_subsampling.py31
4 files changed, 73 insertions, 8 deletions
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<int> 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<std::vector<double>> subsampling_n_farthest_points(std::vector<std::vector<double>>& points, unsigned nb_points) {
std::vector<Subsampling_point_d> input, output;
for (auto point : points)
@@ -71,6 +72,25 @@ std::vector<std::vector<double>> subsampling_n_farthest_points_from_file(std::st
std::vector<std::vector<double>> points = off_reader.get_point_cloud();
return subsampling_n_farthest_points(points, nb_points, starting_point);
}
+
+// ------ pick_n_random_points ------
+std::vector<std::vector<double>> subsampling_n_random_points(std::vector<std::vector<double>>& points, unsigned nb_points) {
+ 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;
+ pick_n_random_points(points, nb_points, std::back_inserter(landmarks));
+
+ return landmarks;
+}
+
+std::vector<std::vector<double>> subsampling_n_random_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_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