summaryrefslogtreecommitdiff
path: root/src/cython
diff options
context:
space:
mode:
authorvrouvrea <vrouvrea@636b058d-ea47-450e-bf9e-a15bfbe3eedb>2016-12-03 14:46:37 +0000
committervrouvrea <vrouvrea@636b058d-ea47-450e-bf9e-a15bfbe3eedb>2016-12-03 14:46:37 +0000
commit0c85e54d44a95aa7aff3f6d51a587287ce4a88d6 (patch)
treef0f5e815ba819f710757551765a8fce95d286bb8 /src/cython
parent9a3373a0db722c75994826d44b4cfbe1b7b5aeb0 (diff)
sparsify point set cythonization
unitary tested git-svn-id: svn+ssh://scm.gforge.inria.fr/svnroot/gudhi/branches/ST_cythonize@1816 636b058d-ea47-450e-bf9e-a15bfbe3eedb Former-commit-id: 170f14ffcf009ebe1169df90a4e088441eaad13f
Diffstat (limited to 'src/cython')
-rw-r--r--src/cython/cython/subsampling.pyx30
-rw-r--r--src/cython/include/Subsampling_interface.h31
-rwxr-xr-xsrc/cython/test/test_subsampling.py22
3 files changed, 72 insertions, 11 deletions
diff --git a/src/cython/cython/subsampling.pyx b/src/cython/cython/subsampling.pyx
index c71f5810..05c0232f 100644
--- a/src/cython/cython/subsampling.pyx
+++ b/src/cython/cython/subsampling.pyx
@@ -37,6 +37,8 @@ cdef extern from "Subsampling_interface.h" namespace "Gudhi::subsampling":
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)
+ vector[vector[double]] subsampling_sparsify_points(vector[vector[double]] points, double min_squared_dist)
+ vector[vector[double]] subsampling_sparsify_points_from_file(string off_file, double min_squared_dist)
def choose_n_farthest_points(points=[], off_file='', nb_points=0, starting_point = ''):
"""Subsample by a greedy strategy of iteratively adding the farthest point
@@ -57,7 +59,7 @@ def choose_n_farthest_points(points=[], off_file='', nb_points=0, starting_point
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.
+ :returns: The subsample point set.
:rtype: vector[vector[double]]
"""
if off_file is not '':
@@ -87,7 +89,7 @@ def pick_n_random_points(points=[], off_file='', nb_points=0):
:param nb_points: Number of points of the subsample.
:type nb_points: unsigned.
- :returns: The subsamplepoint set.
+ :returns: The subsample point set.
:rtype: vector[vector[double]]
"""
if off_file is not '':
@@ -97,3 +99,27 @@ def pick_n_random_points(points=[], off_file='', nb_points=0):
print("file " + off_file + " not found.")
else:
return subsampling_n_random_points(points, nb_points)
+
+def sparsify_point_set(points=[], off_file='', min_squared_dist=0.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 min_squared_dist: Number of points of the subsample.
+ :type min_squared_dist: unsigned.
+ :returns: The subsample point set.
+ :rtype: vector[vector[double]]
+ """
+ if off_file is not '':
+ if os.path.isfile(off_file):
+ return subsampling_sparsify_points_from_file(off_file, min_squared_dist)
+ else:
+ print("file " + off_file + " not found.")
+ else:
+ return subsampling_sparsify_points(points, min_squared_dist)
diff --git a/src/cython/include/Subsampling_interface.h b/src/cython/include/Subsampling_interface.h
index 8ef4fea1..9340cd86 100644
--- a/src/cython/include/Subsampling_interface.h
+++ b/src/cython/include/Subsampling_interface.h
@@ -24,6 +24,8 @@
#define SUBSAMPLING_INTERFACE_H
#include <gudhi/choose_n_farthest_points.h>
+#include <gudhi/pick_n_random_points.h>
+#include <gudhi/sparsify_point_set.h>
#include <gudhi/Points_off_io.h>
#include <CGAL/Epick_d.h>
@@ -40,9 +42,6 @@ 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)
- 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, std::back_inserter(landmarks));
@@ -51,9 +50,6 @@ std::vector<std::vector<double>> subsampling_n_farthest_points(std::vector<std::
}
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));
@@ -75,9 +71,6 @@ std::vector<std::vector<double>> subsampling_n_farthest_points_from_file(std::st
// ------ 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));
@@ -90,6 +83,26 @@ std::vector<std::vector<double>> subsampling_n_random_points_from_file(std::stri
return subsampling_n_random_points(points, nb_points);
}
+// ------ sparsify_point_set ------
+std::vector<std::vector<double>> subsampling_sparsify_points(std::vector<std::vector<double>>& points, double min_squared_dist) {
+ std::vector<Subsampling_point_d> input, output;
+ for (auto point : points)
+ input.push_back(Subsampling_point_d(point.size(), point.begin(), point.end()));
+ Subsampling_dynamic_kernel k;
+ sparsify_point_set(k, input, min_squared_dist, std::back_inserter(output));
+
+ std::vector<std::vector<double>> landmarks;
+ for (auto point : output)
+ landmarks.push_back(std::vector<double>(point.cartesian_begin(), point.cartesian_end()));
+ return landmarks;
+}
+
+std::vector<std::vector<double>> subsampling_sparsify_points_from_file(std::string& off_file, double min_squared_dist) {
+ Gudhi::Points_off_reader<std::vector<double>> off_reader(off_file);
+ std::vector<std::vector<double>> points = off_reader.get_point_cloud();
+ return subsampling_sparsify_points(points, min_squared_dist);
+}
+
} // namespace subsampling
diff --git a/src/cython/test/test_subsampling.py b/src/cython/test/test_subsampling.py
index 2dc12a89..2caf4ddb 100755
--- a/src/cython/test/test_subsampling.py
+++ b/src/cython/test/test_subsampling.py
@@ -109,3 +109,25 @@ def test_simple_pick_n_random_points():
found = True
# Check each sub set point is existing in the point set
assert found == True
+
+ # From off file test
+ for i in range (0, 7):
+ assert len(gudhi.pick_n_random_points(off_file = 'subsample.off', nb_points = i)) == i
+
+def test_simple_sparsify_points():
+ point_set = [[0,1], [0,0], [1,0], [1,1]]
+ # Test the limits
+ # assert gudhi.sparsify_point_set(points = [], min_squared_dist = 0.0) == []
+ # assert gudhi.sparsify_point_set(points = [], min_squared_dist = 10.0) == []
+ assert gudhi.sparsify_point_set(points = point_set, min_squared_dist = 0.0) == point_set
+ assert gudhi.sparsify_point_set(points = point_set, min_squared_dist = 1.0) == point_set
+ assert gudhi.sparsify_point_set(points = point_set, min_squared_dist = 2.0) == [[0,1], [1,0]]
+ assert gudhi.sparsify_point_set(points = point_set, min_squared_dist = 2.01) == [[0,1]]
+
+ assert len(gudhi.sparsify_point_set(off_file = 'subsample.off', min_squared_dist = 0.0)) == 7
+ assert len(gudhi.sparsify_point_set(off_file = 'subsample.off', min_squared_dist = 30.0)) == 5
+ assert len(gudhi.sparsify_point_set(off_file = 'subsample.off', min_squared_dist = 40.0)) == 4
+ assert len(gudhi.sparsify_point_set(off_file = 'subsample.off', min_squared_dist = 90.0)) == 3
+ assert len(gudhi.sparsify_point_set(off_file = 'subsample.off', min_squared_dist = 100.0)) == 2
+ assert len(gudhi.sparsify_point_set(off_file = 'subsample.off', min_squared_dist = 325.0)) == 2
+ assert len(gudhi.sparsify_point_set(off_file = 'subsample.off', min_squared_dist = 325.01)) == 1