summaryrefslogtreecommitdiff
path: root/src/python
diff options
context:
space:
mode:
authorHind <hind.montassif@gmail.com>2021-04-22 17:08:17 +0200
committerHind <hind.montassif@gmail.com>2021-04-22 17:08:17 +0200
commit45917ecf17acacfede909994d7b3a78fc18355da (patch)
tree66f800f9ca2ad023f5c9ccd418aac6b8ab5d6dc5 /src/python
parent73fa5b763a53179444304ccbe0583b616403bb0a (diff)
Add random points generator on sphere in python, with an example
Diffstat (limited to 'src/python')
-rw-r--r--src/python/CMakeLists.txt6
-rw-r--r--src/python/example/alpha_complex_from_generated_points_example.py52
-rw-r--r--src/python/gudhi/random_point_generators.cc68
3 files changed, 126 insertions, 0 deletions
diff --git a/src/python/CMakeLists.txt b/src/python/CMakeLists.txt
index 73303a24..8baf0f02 100644
--- a/src/python/CMakeLists.txt
+++ b/src/python/CMakeLists.txt
@@ -43,6 +43,7 @@ endfunction( add_gudhi_debug_info )
if(PYTHONINTERP_FOUND)
if(PYBIND11_FOUND)
add_gudhi_debug_info("Pybind11 version ${PYBIND11_VERSION}")
+ set(GUDHI_PYTHON_MODULES "${GUDHI_PYTHON_MODULES}'random_point_generators', ")
set(GUDHI_PYTHON_MODULES "${GUDHI_PYTHON_MODULES}'bottleneck', ")
set(GUDHI_PYTHON_MODULES_EXTRA "${GUDHI_PYTHON_MODULES_EXTRA}'hera', ")
set(GUDHI_PYTHON_MODULES_EXTRA "${GUDHI_PYTHON_MODULES_EXTRA}'clustering', ")
@@ -151,6 +152,7 @@ if(PYTHONINTERP_FOUND)
set(GUDHI_PYBIND11_MODULES "${GUDHI_PYBIND11_MODULES}'hera/wasserstein', ")
set(GUDHI_PYBIND11_MODULES "${GUDHI_PYBIND11_MODULES}'hera/bottleneck', ")
if (NOT CGAL_VERSION VERSION_LESS 4.11.0)
+ set(GUDHI_PYBIND11_MODULES "${GUDHI_PYBIND11_MODULES}'random_point_generators', ")
set(GUDHI_PYBIND11_MODULES "${GUDHI_PYBIND11_MODULES}'bottleneck', ")
set(GUDHI_CYTHON_MODULES "${GUDHI_CYTHON_MODULES}'nerve_gic', ")
endif ()
@@ -425,6 +427,10 @@ if(PYTHONINTERP_FOUND)
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
COMMAND ${CMAKE_COMMAND} -E env "${GUDHI_PYTHON_PATH_ENV}"
${PYTHON_EXECUTABLE} "${CMAKE_CURRENT_SOURCE_DIR}/example/alpha_complex_from_points_example.py")
+ add_test(NAME alpha_complex_from_generated_points_example_py_test
+ WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
+ COMMAND ${CMAKE_COMMAND} -E env "${GUDHI_PYTHON_PATH_ENV}"
+ ${PYTHON_EXECUTABLE} "${CMAKE_CURRENT_SOURCE_DIR}/example/alpha_complex_from_generated_points_example.py")
add_test(NAME alpha_complex_diagram_persistence_from_off_file_example_py_test
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
COMMAND ${CMAKE_COMMAND} -E env "${GUDHI_PYTHON_PATH_ENV}"
diff --git a/src/python/example/alpha_complex_from_generated_points_example.py b/src/python/example/alpha_complex_from_generated_points_example.py
new file mode 100644
index 00000000..7a07ed42
--- /dev/null
+++ b/src/python/example/alpha_complex_from_generated_points_example.py
@@ -0,0 +1,52 @@
+#!/usr/bin/env python
+
+from gudhi import random_point_generators
+from gudhi import AlphaComplex, SimplexTree
+from gudhi import plot_persistence_barcode, plot_persistence_diagram
+
+import matplotlib.pyplot as plt
+
+
+""" This file is part of the Gudhi Library - https://gudhi.inria.fr/ - which is released under MIT.
+ See file LICENSE or go to https://gudhi.inria.fr/licensing/ for full license details.
+ Author(s): Hind Montassif
+
+ Copyright (C) 2021 Inria
+
+ Modification(s):
+ - YYYY/MM Author: Description of the modification
+"""
+
+__author__ = "Hind Montassif"
+__copyright__ = "Copyright (C) 2021 Inria"
+__license__ = "MIT"
+
+print("#####################################################################")
+print("AlphaComplex creation from generated points")
+
+
+# Generate a circle: 50 points; dim 2; radius 1
+points = random_point_generators.generate_points_on_sphere_d(50, 2, 1)
+
+# Plot the generated points (to uncomment if wished)
+#plt.scatter(points[:,0], points[:,1])
+#plt.show()
+
+# Create an alpha complex
+alpha_complex = AlphaComplex(points=points)
+simplex_tree = alpha_complex.create_simplex_tree()
+
+result_str = 'Alpha complex is of dimension ' + repr(simplex_tree.dimension()) + ' - ' + \
+ repr(simplex_tree.num_simplices()) + ' simplices - ' + \
+ repr(simplex_tree.num_vertices()) + ' vertices.'
+print(result_str)
+
+
+# Compute the persistence
+diag = simplex_tree.persistence()
+
+# Plot the barcode and diagram (to uncomment if wished)
+#plot_persistence_barcode(diag)
+#plt.show()
+#plot_persistence_diagram(diag)
+#plt.show()
diff --git a/src/python/gudhi/random_point_generators.cc b/src/python/gudhi/random_point_generators.cc
new file mode 100644
index 00000000..39b09a6d
--- /dev/null
+++ b/src/python/gudhi/random_point_generators.cc
@@ -0,0 +1,68 @@
+/* This file is part of the Gudhi Library - https://gudhi.inria.fr/ - which is released under MIT.
+ * See file LICENSE or go to https://gudhi.inria.fr/licensing/ for full license details.
+ * Author(s): Hind Montassif
+ *
+ * Copyright (C) 2021 Inria
+ *
+ * Modification(s):
+ * - YYYY/MM Author: Description of the modification
+ */
+
+#include <pybind11/pybind11.h>
+#include <pybind11/numpy.h>
+
+#include <gudhi/random_point_generators.h>
+
+#include <CGAL/Epick_d.h>
+
+namespace py = pybind11;
+
+
+typedef CGAL::Epick_d< CGAL::Dynamic_dimension_tag > Kern;
+
+template <typename Kernel>
+py::array_t<double> generate_points_on_sphere(py::object num_points, py::object dim, py::object radius) {
+ int npoints = num_points.cast<int>();
+ int d = dim.cast<int>();
+ double rad = radius.cast<double>();
+
+ py::gil_scoped_release release;
+
+ auto points_generated = Gudhi::generate_points_on_sphere_d<Kernel>(npoints, d, rad);
+
+ py::gil_scoped_acquire acquire;
+
+ py::array_t<double> points({npoints, d});
+
+ py::buffer_info buf = points.request();
+
+ double *ptr = static_cast<double *>(buf.ptr);
+
+ assert(npoints == buf.shape[0]);
+ assert(d == buf.shape[1]);
+
+
+ for (size_t i = 0; i < (size_t)npoints; i++)
+ for (size_t j = 0; j < (size_t)d; j++)
+ ptr[i*d+j] = points_generated.at(i).at(j);
+
+ return points;
+}
+
+PYBIND11_MODULE(random_point_generators, m) {
+ m.attr("__license__") = "LGPL v3";
+ m.def("generate_points_on_sphere_d", &generate_points_on_sphere<Kern>,
+ py::arg("num_points"), py::arg("dim"), py::arg("radius"),
+ R"pbdoc(
+ Generate points on a sphere
+
+ :param num_points: The number of points to be generated.
+ :type num_points: integer
+ :param dim: The sphere dimension.
+ :type dim: integer
+ :param radius: The sphere radius.
+ :type radius: float
+ :rtype: numpy array of points
+ :returns: the generated points on a sphere.
+ )pbdoc");
+}