From 2b694f9beae0e5fa78ae5b8923e7f2905c58777f Mon Sep 17 00:00:00 2001 From: Hind Montassif Date: Wed, 5 May 2021 13:58:30 +0200 Subject: Add __init__ files at every level in modules Remove last modules level and add sample type argument Rename num_points to n_samples --- src/python/CMakeLists.txt | 7 +-- ...plex_from_generated_points_on_sphere_example.py | 7 +-- src/python/gudhi/datasets/__init__.py | 0 src/python/gudhi/datasets/generators/__init__.py | 0 src/python/gudhi/datasets/generators/points.cc | 68 ++++++++++++++++++++++ .../gudhi/datasets/generators/points/__init__.py | 0 .../gudhi/datasets/generators/points/sphere.cc | 61 ------------------- 7 files changed, 74 insertions(+), 69 deletions(-) create mode 100644 src/python/gudhi/datasets/__init__.py create mode 100644 src/python/gudhi/datasets/generators/__init__.py create mode 100644 src/python/gudhi/datasets/generators/points.cc delete mode 100644 src/python/gudhi/datasets/generators/points/__init__.py delete mode 100644 src/python/gudhi/datasets/generators/points/sphere.cc (limited to 'src/python') diff --git a/src/python/CMakeLists.txt b/src/python/CMakeLists.txt index ef9dc3ab..8dd4ea5d 100644 --- a/src/python/CMakeLists.txt +++ b/src/python/CMakeLists.txt @@ -46,7 +46,7 @@ if(PYTHONINTERP_FOUND) 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', ") - set(GUDHI_PYTHON_MODULES_EXTRA "${GUDHI_PYTHON_MODULES_EXTRA}'datasets/generators/points', ") + set(GUDHI_PYTHON_MODULES_EXTRA "${GUDHI_PYTHON_MODULES_EXTRA}'datasets', ") endif() if(CYTHON_FOUND) set(GUDHI_PYTHON_MODULES "${GUDHI_PYTHON_MODULES}'off_reader', ") @@ -152,7 +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}'datasets/generators/points/sphere', ") + set(GUDHI_PYBIND11_MODULES "${GUDHI_PYBIND11_MODULES}'datasets/generators/points', ") set(GUDHI_PYBIND11_MODULES "${GUDHI_PYBIND11_MODULES}'bottleneck', ") set(GUDHI_CYTHON_MODULES "${GUDHI_CYTHON_MODULES}'nerve_gic', ") endif () @@ -264,8 +264,7 @@ if(PYTHONINTERP_FOUND) file(COPY "gudhi/weighted_rips_complex.py" DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/gudhi") file(COPY "gudhi/dtm_rips_complex.py" DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/gudhi") file(COPY "gudhi/hera/__init__.py" DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/gudhi/hera") - file(COPY "gudhi/datasets/generators/points/__init__.py" DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/gudhi/datasets/generators/points") - + file(COPY "gudhi/datasets" DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/gudhi" FILES_MATCHING PATTERN "*.py") # Some files for pip package file(COPY "introduction.rst" DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/") diff --git a/src/python/example/alpha_complex_from_generated_points_on_sphere_example.py b/src/python/example/alpha_complex_from_generated_points_on_sphere_example.py index 2b023bbe..e73584d3 100644 --- a/src/python/example/alpha_complex_from_generated_points_on_sphere_example.py +++ b/src/python/example/alpha_complex_from_generated_points_on_sphere_example.py @@ -1,6 +1,6 @@ #!/usr/bin/env python -from gudhi.datasets.generators.points import sphere +from gudhi.datasets.generators import points from gudhi import AlphaComplex @@ -22,11 +22,10 @@ print("#####################################################################") print("AlphaComplex creation from generated points on sphere") -# Generate a circle: 50 points; dim 2; radius 1 -points = sphere.generate_random_points(50, 2, 1) +gen_points = points.sphere(n_samples = 50, dim = 2, radius = 1, sample = "random") # Create an alpha complex -alpha_complex = AlphaComplex(points=points) +alpha_complex = AlphaComplex(points = gen_points) simplex_tree = alpha_complex.create_simplex_tree() result_str = 'Alpha complex is of dimension ' + repr(simplex_tree.dimension()) + ' - ' + \ diff --git a/src/python/gudhi/datasets/__init__.py b/src/python/gudhi/datasets/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/src/python/gudhi/datasets/generators/__init__.py b/src/python/gudhi/datasets/generators/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/src/python/gudhi/datasets/generators/points.cc b/src/python/gudhi/datasets/generators/points.cc new file mode 100644 index 00000000..f02c7d73 --- /dev/null +++ b/src/python/gudhi/datasets/generators/points.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 +#include + +#include +#include + +#include + +namespace py = pybind11; + + +typedef CGAL::Epick_d< CGAL::Dynamic_dimension_tag > Kern; + +py::array_t generate_points_on_sphere(size_t n_samples, int dim, double radius, std::string sample) { + + if (sample != "random") { + throw pybind11::value_error("sample type is not supported"); + } + + py::array_t points({n_samples, (size_t)dim}); + + py::buffer_info buf = points.request(); + double *ptr = static_cast(buf.ptr); + + GUDHI_CHECK(n_samples == buf.shape[0], "Py array first dimension not matching n_samples on sphere"); + GUDHI_CHECK(dim == buf.shape[1], "Py array second dimension not matching the ambient space dimension"); + + + py::gil_scoped_release release; + auto points_generated = Gudhi::generate_points_on_sphere_d(n_samples, dim, radius); + + for (size_t i = 0; i < n_samples; i++) + for (int j = 0; j < dim; j++) + ptr[i*dim+j] = points_generated[i][j]; + + return points; +} + +PYBIND11_MODULE(points, m) { + m.attr("__license__") = "LGPL v3"; + m.def("sphere", &generate_points_on_sphere, + py::arg("n_samples"), py::arg("dim"), + py::arg("radius") = 1, py::arg("sample") = "random", + R"pbdoc( + Generate random i.i.d. points uniformly on a (d-1)-sphere in R^d + + :param n_samples: The number of points to be generated. + :type n_samples: integer + :param dim: The ambient dimension d. + :type dim: integer + :param radius: The radius. + :type radius: float + :param sample: The sample type. + :type sample: string + :rtype: numpy array of float + :returns: the generated points on a sphere. + )pbdoc"); +} diff --git a/src/python/gudhi/datasets/generators/points/__init__.py b/src/python/gudhi/datasets/generators/points/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/src/python/gudhi/datasets/generators/points/sphere.cc b/src/python/gudhi/datasets/generators/points/sphere.cc deleted file mode 100644 index 79392ef0..00000000 --- a/src/python/gudhi/datasets/generators/points/sphere.cc +++ /dev/null @@ -1,61 +0,0 @@ -/* 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 -#include - -#include -#include - -#include - -namespace py = pybind11; - - -typedef CGAL::Epick_d< CGAL::Dynamic_dimension_tag > Kern; - -py::array_t generate_points_on_sphere(size_t num_points, int dim, double radius) { - - py::array_t points({num_points, (size_t)dim}); - - py::buffer_info buf = points.request(); - double *ptr = static_cast(buf.ptr); - - GUDHI_CHECK(num_points == buf.shape[0], "Py array first dimension not matching num_points on sphere"); - GUDHI_CHECK(dim == buf.shape[1], "Py array second dimension not matching the ambient space dimension"); - - - py::gil_scoped_release release; - auto points_generated = Gudhi::generate_points_on_sphere_d(num_points, dim, radius); - - for (size_t i = 0; i < num_points; i++) - for (int j = 0; j < dim; j++) - ptr[i*dim+j] = points_generated[i][j]; - - return points; -} - -PYBIND11_MODULE(sphere, m) { - m.attr("__license__") = "LGPL v3"; - m.def("generate_random_points", &generate_points_on_sphere, - py::arg("num_points"), py::arg("dim"), py::arg("radius") = 1, - R"pbdoc( - Generate random i.i.d. points uniformly on a (d-1)-sphere in R^d - - :param num_points: The number of points to be generated. - :type num_points: unsigned integer - :param dim: The dimension. - :type dim: integer - :param radius: The radius. - :type radius: float - :rtype: numpy array of float - :returns: the generated points on a sphere. - )pbdoc"); -} -- cgit v1.2.3