From 53818204ba2f914c89e21deb6bfe7cb4a0122718 Mon Sep 17 00:00:00 2001 From: Vincent Rouvreau Date: Wed, 30 Mar 2022 09:22:09 +0200 Subject: Remove this useless line --- src/Alpha_complex/include/gudhi/Alpha_complex_3d.h | 2 -- 1 file changed, 2 deletions(-) (limited to 'src') diff --git a/src/Alpha_complex/include/gudhi/Alpha_complex_3d.h b/src/Alpha_complex/include/gudhi/Alpha_complex_3d.h index df5c630e..b3dbc9bb 100644 --- a/src/Alpha_complex/include/gudhi/Alpha_complex_3d.h +++ b/src/Alpha_complex/include/gudhi/Alpha_complex_3d.h @@ -57,8 +57,6 @@ namespace Gudhi { namespace alpha_complex { -thread_local double RELATIVE_PRECISION_OF_TO_DOUBLE = 0.00001; - // Value_from_iterator returns the filtration value from an iterator on alpha shapes values // // FAST SAFE EXACT -- cgit v1.2.3 From 495f3d9b51f5ed48151118fedcc61f5067f04d51 Mon Sep 17 00:00:00 2001 From: Vincent Rouvreau Date: Wed, 30 Mar 2022 12:22:56 +0200 Subject: get/set float_relative_precision and its documentation --- src/python/doc/alpha_complex_user.rst | 3 ++- src/python/gudhi/alpha_complex.pyx | 28 ++++++++++++++++++++++++++++ src/python/include/Alpha_complex_interface.h | 10 ++++++++++ 3 files changed, 40 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/python/doc/alpha_complex_user.rst b/src/python/doc/alpha_complex_user.rst index cfd22742..b060c86e 100644 --- a/src/python/doc/alpha_complex_user.rst +++ b/src/python/doc/alpha_complex_user.rst @@ -27,7 +27,8 @@ Remarks If you pass :code:`precision = 'exact'` to :func:`~gudhi.AlphaComplex.__init__`, the filtration values are the exact ones converted to float. This can be very slow. If you pass :code:`precision = 'safe'` (the default), the filtration values are only - guaranteed to have a small multiplicative error compared to the exact value. + guaranteed to have a small multiplicative error compared to the exact value, see + :func:`~gudhi.AlphaComplex.set_float_relative_precision` to modify the precision. A drawback, when computing persistence, is that an empty exact interval [10^12,10^12] may become a non-empty approximate interval [10^12,10^12+10^6]. Using :code:`precision = 'fast'` makes the computations slightly faster, and the combinatorics are still exact, but diff --git a/src/python/gudhi/alpha_complex.pyx b/src/python/gudhi/alpha_complex.pyx index a4888914..9af62b43 100644 --- a/src/python/gudhi/alpha_complex.pyx +++ b/src/python/gudhi/alpha_complex.pyx @@ -31,6 +31,10 @@ cdef extern from "Alpha_complex_interface.h" namespace "Gudhi": Alpha_complex_interface(vector[vector[double]] points, vector[double] weights, bool fast_version, bool exact_version) nogil except + vector[double] get_point(int vertex) nogil except + void create_simplex_tree(Simplex_tree_interface_full_featured* simplex_tree, double max_alpha_square, bool default_filtration_value) nogil except + + @staticmethod + void set_float_relative_precision(double precision) nogil + @staticmethod + double get_float_relative_precision() nogil # AlphaComplex python interface cdef class AlphaComplex: @@ -133,3 +137,27 @@ cdef class AlphaComplex: self.this_ptr.create_simplex_tree(stree_int_ptr, mas, compute_filtration) return stree + + @staticmethod + def set_float_relative_precision(precision): + """ + :param precision: When the AlphaComplex is constructed with :code:`precision = 'safe'` (the default), + one can set the float relative precision of filtration values computed in + :func:`~gudhi.AlphaComplex.create_simplex_tree`. + Default is :code:`1e-5` (cf. :func:`~gudhi.AlphaComplex.get_float_relative_precision`). + For more details, please refer to + `CGAL::Lazy_exact_nt::set_relative_precision_of_to_double `_ + :type precision: float + """ + if precision <=0. or precision >= 1.: + raise ValueError("Relative precision value must be strictly greater than 0 and strictly lower than 1") + Alpha_complex_interface.set_float_relative_precision(precision) + + @staticmethod + def get_float_relative_precision(): + """ + :returns: The float relative precision of filtration values computation in + :func:`~gudhi.AlphaComplex.create_simplex_tree`. + :rtype: float + """ + return Alpha_complex_interface.get_float_relative_precision() diff --git a/src/python/include/Alpha_complex_interface.h b/src/python/include/Alpha_complex_interface.h index 671af4a4..469b91ce 100644 --- a/src/python/include/Alpha_complex_interface.h +++ b/src/python/include/Alpha_complex_interface.h @@ -57,6 +57,16 @@ class Alpha_complex_interface { alpha_ptr_->create_simplex_tree(simplex_tree, max_alpha_square, default_filtration_value); } + static void set_float_relative_precision(double precision) { + // cf. Exact_alpha_complex_dD kernel type in Alpha_complex_factory.h + CGAL::Epeck_d::FT::set_relative_precision_of_to_double(precision); + } + + static double get_float_relative_precision() { + // cf. Exact_alpha_complex_dD kernel type in Alpha_complex_factory.h + return CGAL::Epeck_d::FT::get_relative_precision_of_to_double(); + } + private: std::unique_ptr alpha_ptr_; }; -- cgit v1.2.3 From 37dcedb59cd2a6b70119688ea24c978749d63154 Mon Sep 17 00:00:00 2001 From: Vincent Rouvreau Date: Thu, 31 Mar 2022 16:37:20 +0200 Subject: Test the feature --- src/python/test/test_alpha_complex.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'src') diff --git a/src/python/test/test_alpha_complex.py b/src/python/test/test_alpha_complex.py index f15284f3..f81e6137 100755 --- a/src/python/test/test_alpha_complex.py +++ b/src/python/test/test_alpha_complex.py @@ -286,3 +286,30 @@ def _weighted_doc_example(precision): def test_weighted_doc_example(): for precision in ['fast', 'safe', 'exact']: _weighted_doc_example(precision) + +def test_float_relative_precision(): + assert AlphaComplex.get_float_relative_precision() == 1e-5 + # Must be > 0. + with pytest.raises(ValueError): + AlphaComplex.set_float_relative_precision(0.) + # Must be < 1. + with pytest.raises(ValueError): + AlphaComplex.set_float_relative_precision(1.) + + points = [[1, 1], [7, 0], [4, 6], [9, 6], [0, 14], [2, 19], [9, 17]] + st = AlphaComplex(points=points).create_simplex_tree() + filtrations = list(st.get_filtration()) + + # Get a better precision + AlphaComplex.set_float_relative_precision(1e-15) + assert AlphaComplex.get_float_relative_precision() == 1e-15 + + st = AlphaComplex(points=points).create_simplex_tree() + filtrations_better_resolution = list(st.get_filtration()) + + assert len(filtrations) == len(filtrations_better_resolution) + for idx in range(len(filtrations)): + # check simplex is the same + assert filtrations[idx][0] == filtrations_better_resolution[idx][0] + # check filtration is about the same with a relative precision of the worst case + assert filtrations[idx][1] == pytest.approx(filtrations_better_resolution[idx][1], rel=1e-5) -- cgit v1.2.3 From f4b543bb4891b1c2aa104e45cc5a0d07b56e6977 Mon Sep 17 00:00:00 2001 From: Vincent Rouvreau Date: Thu, 31 Mar 2022 21:15:18 +0200 Subject: code review: no need to nogil for cheap functions. Repeat get_float_relative_precision is only for safe mode --- src/python/gudhi/alpha_complex.pyx | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/python/gudhi/alpha_complex.pyx b/src/python/gudhi/alpha_complex.pyx index 9af62b43..e63796db 100644 --- a/src/python/gudhi/alpha_complex.pyx +++ b/src/python/gudhi/alpha_complex.pyx @@ -32,9 +32,9 @@ cdef extern from "Alpha_complex_interface.h" namespace "Gudhi": vector[double] get_point(int vertex) nogil except + void create_simplex_tree(Simplex_tree_interface_full_featured* simplex_tree, double max_alpha_square, bool default_filtration_value) nogil except + @staticmethod - void set_float_relative_precision(double precision) nogil + void set_float_relative_precision(double precision) @staticmethod - double get_float_relative_precision() nogil + double get_float_relative_precision() # AlphaComplex python interface cdef class AlphaComplex: @@ -157,7 +157,8 @@ cdef class AlphaComplex: def get_float_relative_precision(): """ :returns: The float relative precision of filtration values computation in - :func:`~gudhi.AlphaComplex.create_simplex_tree`. + :func:`~gudhi.AlphaComplex.create_simplex_tree` when the AlphaComplex is constructed with + :code:`precision = 'safe'` (the default). :rtype: float """ return Alpha_complex_interface.get_float_relative_precision() -- cgit v1.2.3 From 25172af7cbe5a3ea3017bed6fde53be471a7e4bf Mon Sep 17 00:00:00 2001 From: Vincent Rouvreau Date: Tue, 5 Apr 2022 13:35:11 +0200 Subject: nogil for float_relative_precision methods --- src/python/gudhi/alpha_complex.pyx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/python/gudhi/alpha_complex.pyx b/src/python/gudhi/alpha_complex.pyx index e63796db..375e1561 100644 --- a/src/python/gudhi/alpha_complex.pyx +++ b/src/python/gudhi/alpha_complex.pyx @@ -32,9 +32,9 @@ cdef extern from "Alpha_complex_interface.h" namespace "Gudhi": vector[double] get_point(int vertex) nogil except + void create_simplex_tree(Simplex_tree_interface_full_featured* simplex_tree, double max_alpha_square, bool default_filtration_value) nogil except + @staticmethod - void set_float_relative_precision(double precision) + void set_float_relative_precision(double precision) nogil @staticmethod - double get_float_relative_precision() + double get_float_relative_precision() nogil # AlphaComplex python interface cdef class AlphaComplex: -- cgit v1.2.3