From 85eec1ba750d56b66e3739dc486c6205f49fb31e Mon Sep 17 00:00:00 2001 From: ROUVREAU Vincent Date: Fri, 3 Jul 2020 16:04:45 +0200 Subject: A proposal for simplex_tree reset_filtration (python & C++) --- src/Simplex_tree/include/gudhi/Simplex_tree.h | 30 +++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'src/Simplex_tree/include/gudhi/Simplex_tree.h') diff --git a/src/Simplex_tree/include/gudhi/Simplex_tree.h b/src/Simplex_tree/include/gudhi/Simplex_tree.h index 889dbd00..adc8e801 100644 --- a/src/Simplex_tree/include/gudhi/Simplex_tree.h +++ b/src/Simplex_tree/include/gudhi/Simplex_tree.h @@ -1667,6 +1667,36 @@ class Simplex_tree { return sh; // None of its faces has the same filtration. } + public: + /** \brief This function resets filtration value until a given dimension. + * @param[in] filt_value The new filtration value. + * @param[in] max_dim The maximal dimension. + */ + void reset_filtration(Filtration_value filt_value, int max_dim) { + for (auto& simplex : root_.members()) { + simplex.second.assign_filtration(filt_value); + if (has_children(&simplex) && max_dim > 0) { + rec_reset_filtration(simplex.second.children(), filt_value, (max_dim - 1)); + } + } + clear_filtration(); // Drop the cache. + } + + private: + /** \brief Recursively resets filtration value until a given dimension. + * @param[in] sib Siblings to be parsed. + * @param[in] filt_value The new filtration value. + * @param[in] max_dim The maximal dimension. + */ + void rec_reset_filtration(Siblings * sib, Filtration_value filt_value, int max_dim) { + for (auto& simplex : sib->members()) { + simplex.second.assign_filtration(filt_value); + if (has_children(&simplex) && max_dim > 0) { + rec_reset_filtration(simplex.second.children(), filt_value, (max_dim - 1)); + } + } + } + private: Vertex_handle null_vertex_; /** \brief Total number of simplices in the complex, without the empty simplex.*/ -- cgit v1.2.3 From ddb2118f0af865588d7c14b88171dc04bb27c529 Mon Sep 17 00:00:00 2001 From: ROUVREAU Vincent Date: Tue, 18 Aug 2020 14:38:31 +0200 Subject: reset_filtration from a dimension (instead of 'until') --- src/Simplex_tree/include/gudhi/Simplex_tree.h | 31 ++++++++++++++---------- src/Simplex_tree/test/simplex_tree_unit_test.cpp | 14 +++++++---- src/python/gudhi/simplex_tree.pyx | 17 +++++++------ src/python/test/test_simplex_tree.py | 9 ++++--- 4 files changed, 41 insertions(+), 30 deletions(-) (limited to 'src/Simplex_tree/include/gudhi/Simplex_tree.h') diff --git a/src/Simplex_tree/include/gudhi/Simplex_tree.h b/src/Simplex_tree/include/gudhi/Simplex_tree.h index adc8e801..89b4a5df 100644 --- a/src/Simplex_tree/include/gudhi/Simplex_tree.h +++ b/src/Simplex_tree/include/gudhi/Simplex_tree.h @@ -1668,31 +1668,36 @@ class Simplex_tree { } public: - /** \brief This function resets filtration value until a given dimension. + /** \brief This function resets filtration value from a given dimension. Resets all the Simplex_tree when + * `min_dim = 0`. * @param[in] filt_value The new filtration value. - * @param[in] max_dim The maximal dimension. + * @param[in] min_dim The minimal dimension. */ - void reset_filtration(Filtration_value filt_value, int max_dim) { + void reset_filtration(Filtration_value filt_value, int min_dim) { for (auto& simplex : root_.members()) { - simplex.second.assign_filtration(filt_value); - if (has_children(&simplex) && max_dim > 0) { - rec_reset_filtration(simplex.second.children(), filt_value, (max_dim - 1)); + if (min_dim <= 0) { + simplex.second.assign_filtration(filt_value); + } + if (has_children(&simplex)) { + rec_reset_filtration(simplex.second.children(), filt_value, min_dim); } } clear_filtration(); // Drop the cache. } private: - /** \brief Recursively resets filtration value until a given dimension. + /** \brief Recursively resets filtration value from a given dimension. * @param[in] sib Siblings to be parsed. * @param[in] filt_value The new filtration value. - * @param[in] max_dim The maximal dimension. + * @param[in] min_dim The maximal dimension. */ - void rec_reset_filtration(Siblings * sib, Filtration_value filt_value, int max_dim) { - for (auto& simplex : sib->members()) { - simplex.second.assign_filtration(filt_value); - if (has_children(&simplex) && max_dim > 0) { - rec_reset_filtration(simplex.second.children(), filt_value, (max_dim - 1)); + void rec_reset_filtration(Siblings * sib, Filtration_value filt_value, int min_dim) { + for (auto sh = sib->members().begin(); sh != sib->members().end(); ++sh) { + if (min_dim <= dimension(sh)) { + sh->second.assign_filtration(filt_value); + } + if (has_children(sh)) { + rec_reset_filtration(sh->second.children(), filt_value, min_dim); } } } diff --git a/src/Simplex_tree/test/simplex_tree_unit_test.cpp b/src/Simplex_tree/test/simplex_tree_unit_test.cpp index 9780f5b0..bdd41d34 100644 --- a/src/Simplex_tree/test/simplex_tree_unit_test.cpp +++ b/src/Simplex_tree/test/simplex_tree_unit_test.cpp @@ -965,21 +965,25 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(simplex_tree_reset_filtration, typeST, list_of_tes for (auto vertex : st.simplex_vertex_range(f_simplex)) { std::clog << vertex << ","; } - std::clog << ") - filtration =" << st.filtration(f_simplex) << std::endl; + std::clog << ") - filtration = " << st.filtration(f_simplex); + std::clog << " - dimension = " << st.dimension(f_simplex) << std::endl; + // Guaranteed by construction BOOST_CHECK(st.filtration(f_simplex) >= 2.); } // dimension until 5 even if simplex tree is of dimension 3 to test the limits - for(int dimension = 0; dimension < 6; dimension ++) { + for(int dimension = 5; dimension >= 0; dimension --) { + std::clog << "### reset_filtration - dimension = " << dimension << "\n"; st.reset_filtration(0., dimension); for (auto f_simplex : st.skeleton_simplex_range(3)) { std::clog << "vertex = ("; for (auto vertex : st.simplex_vertex_range(f_simplex)) { std::clog << vertex << ","; } - std::clog << ") - filtration =" << st.filtration(f_simplex) << std::endl; - if (st.dimension(f_simplex) > dimension) - BOOST_CHECK(st.filtration(f_simplex) >= 1.); + std::clog << ") - filtration = " << st.filtration(f_simplex); + std::clog << " - dimension = " << st.dimension(f_simplex) << std::endl; + if (st.dimension(f_simplex) < dimension) + BOOST_CHECK(st.filtration(f_simplex) >= 2.); else BOOST_CHECK(st.filtration(f_simplex) == 0.); } diff --git a/src/python/gudhi/simplex_tree.pyx b/src/python/gudhi/simplex_tree.pyx index b7682693..657d55be 100644 --- a/src/python/gudhi/simplex_tree.pyx +++ b/src/python/gudhi/simplex_tree.pyx @@ -328,7 +328,7 @@ cdef class SimplexTree: return self.get_ptr().prune_above_filtration(filtration) def expansion(self, max_dim): - """Expands the Simplex_tree containing only its one skeleton + """Expands the simplex tree containing only its one skeleton until dimension max_dim. The expanded simplicial complex until dimension :math:`d` @@ -338,7 +338,7 @@ cdef class SimplexTree: The filtration value assigned to a simplex is the maximal filtration value of one of its edges. - The Simplex_tree must contain no simplex of dimension bigger than + The simplex tree must contain no simplex of dimension bigger than 1 when calling the method. :param max_dim: The maximal dimension. @@ -358,15 +358,16 @@ cdef class SimplexTree: """ return self.get_ptr().make_filtration_non_decreasing() - def reset_filtration(self, filtration, max_dim): - """This function resets filtration value until a given dimension. + def reset_filtration(self, filtration, min_dim): + """This function resets filtration value from a given dimension. + Resets all the simplex tree when `min_dim = 0`. :param filtration: New threshold value. :type filtration: float. - :param max_dim: The maximal dimension. + :param max_dim: The minimal dimension. :type max_dim: int. """ - self.get_ptr().reset_filtration(filtration, max_dim) + self.get_ptr().reset_filtration(filtration, min_dim) def extend_filtration(self): """ Extend filtration for computing extended persistence. This function only uses the @@ -376,14 +377,14 @@ cdef class SimplexTree: .. note:: Note that after calling this function, the filtration - values are actually modified within the Simplex_tree. + values are actually modified within the simplex tree. The function :func:`extended_persistence` retrieves the original values. .. note:: Note that this code creates an extra vertex internally, so you should make sure that - the Simplex_tree does not contain a vertex with the largest possible value (i.e., 4294967295). + the simplex tree does not contain a vertex with the largest possible value (i.e., 4294967295). """ self.get_ptr().compute_extended_filtration() diff --git a/src/python/test/test_simplex_tree.py b/src/python/test/test_simplex_tree.py index 6f1d01cc..ac2b59c7 100755 --- a/src/python/test/test_simplex_tree.py +++ b/src/python/test/test_simplex_tree.py @@ -367,15 +367,16 @@ def test_reset_filtration(): assert st.insert([3, 4, 5], 3.) == True assert st.insert([0, 1, 6, 7], 4.) == True + # Guaranteed by construction for simplex in st.get_simplices(): - assert st.filtration(simplex[0]) >= 0. + assert st.filtration(simplex[0]) >= 2. # dimension until 5 even if simplex tree is of dimension 3 to test the limits - for dimension in range(0, 6): + for dimension in range(5, -1, -1): st.reset_filtration(0., dimension) for simplex in st.get_skeleton(3): print(simplex) - if len(simplex[0]) > (dimension + 1): - assert st.filtration(simplex[0]) >= 1. + if len(simplex[0]) < (dimension) + 1: + assert st.filtration(simplex[0]) >= 2. else: assert st.filtration(simplex[0]) == 0. -- cgit v1.2.3 From 12371aa8ec7693f97200c5b3df3aa20add4ad621 Mon Sep 17 00:00:00 2001 From: ROUVREAU Vincent Date: Tue, 25 Aug 2020 08:43:19 +0200 Subject: Code review: min_dim = 0 as default and some doc review --- src/Simplex_tree/include/gudhi/Simplex_tree.h | 9 ++++++--- src/python/gudhi/simplex_tree.pyx | 13 ++++++++----- 2 files changed, 14 insertions(+), 8 deletions(-) (limited to 'src/Simplex_tree/include/gudhi/Simplex_tree.h') diff --git a/src/Simplex_tree/include/gudhi/Simplex_tree.h b/src/Simplex_tree/include/gudhi/Simplex_tree.h index 89b4a5df..9cd51081 100644 --- a/src/Simplex_tree/include/gudhi/Simplex_tree.h +++ b/src/Simplex_tree/include/gudhi/Simplex_tree.h @@ -1670,10 +1670,13 @@ class Simplex_tree { public: /** \brief This function resets filtration value from a given dimension. Resets all the Simplex_tree when * `min_dim = 0`. + * `reset_filtration` may break the filtration property with `min_dim > 0`, and it is the user's responsibility to + * make it a valid filtration (using a large enough `filt_value`, or calling `make_filtration_non_decreasing` + * afterwards for instance). * @param[in] filt_value The new filtration value. - * @param[in] min_dim The minimal dimension. + * @param[in] min_dim The minimal dimension. Default value is 0. */ - void reset_filtration(Filtration_value filt_value, int min_dim) { + void reset_filtration(Filtration_value filt_value, int min_dim = 0) { for (auto& simplex : root_.members()) { if (min_dim <= 0) { simplex.second.assign_filtration(filt_value); @@ -1689,7 +1692,7 @@ class Simplex_tree { /** \brief Recursively resets filtration value from a given dimension. * @param[in] sib Siblings to be parsed. * @param[in] filt_value The new filtration value. - * @param[in] min_dim The maximal dimension. + * @param[in] min_dim The minimal dimension. */ void rec_reset_filtration(Siblings * sib, Filtration_value filt_value, int min_dim) { for (auto sh = sib->members().begin(); sh != sib->members().end(); ++sh) { diff --git a/src/python/gudhi/simplex_tree.pyx b/src/python/gudhi/simplex_tree.pyx index 657d55be..f28990cc 100644 --- a/src/python/gudhi/simplex_tree.pyx +++ b/src/python/gudhi/simplex_tree.pyx @@ -358,14 +358,17 @@ cdef class SimplexTree: """ return self.get_ptr().make_filtration_non_decreasing() - def reset_filtration(self, filtration, min_dim): - """This function resets filtration value from a given dimension. - Resets all the simplex tree when `min_dim = 0`. + def reset_filtration(self, filtration, min_dim = 0): + """This function resets filtration value from a given dimension. Resets all the simplex tree when + `min_dim = 0`. + `reset_filtration` may break the filtration property with `min_dim > 0`, and it is the user's responsibility to + make it a valid filtration (using a large enough `filt_value`, or calling `make_filtration_non_decreasing` + afterwards for instance). :param filtration: New threshold value. :type filtration: float. - :param max_dim: The minimal dimension. - :type max_dim: int. + :param min_dim: The minimal dimension. Default value is 0. + :type min_dim: int. """ self.get_ptr().reset_filtration(filtration, min_dim) -- cgit v1.2.3 From 723252b311a7989ef1d4271b3a812ec7d0be05f2 Mon Sep 17 00:00:00 2001 From: ROUVREAU Vincent Date: Tue, 25 Aug 2020 12:50:22 +0200 Subject: Code review: use minimal depth recursively instead of computing dimension on each loop --- src/Simplex_tree/include/gudhi/Simplex_tree.h | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'src/Simplex_tree/include/gudhi/Simplex_tree.h') diff --git a/src/Simplex_tree/include/gudhi/Simplex_tree.h b/src/Simplex_tree/include/gudhi/Simplex_tree.h index 9cd51081..70ef4c66 100644 --- a/src/Simplex_tree/include/gudhi/Simplex_tree.h +++ b/src/Simplex_tree/include/gudhi/Simplex_tree.h @@ -1682,25 +1682,25 @@ class Simplex_tree { simplex.second.assign_filtration(filt_value); } if (has_children(&simplex)) { - rec_reset_filtration(simplex.second.children(), filt_value, min_dim); + rec_reset_filtration(simplex.second.children(), filt_value, min_dim - 1); } } clear_filtration(); // Drop the cache. } private: - /** \brief Recursively resets filtration value from a given dimension. + /** \brief Recursively resets filtration value when minimal depth <= 0. * @param[in] sib Siblings to be parsed. * @param[in] filt_value The new filtration value. - * @param[in] min_dim The minimal dimension. + * @param[in] min_depth The minimal depth. */ - void rec_reset_filtration(Siblings * sib, Filtration_value filt_value, int min_dim) { + void rec_reset_filtration(Siblings * sib, Filtration_value filt_value, int min_depth) { for (auto sh = sib->members().begin(); sh != sib->members().end(); ++sh) { - if (min_dim <= dimension(sh)) { + if (min_depth <= 0) { sh->second.assign_filtration(filt_value); } if (has_children(sh)) { - rec_reset_filtration(sh->second.children(), filt_value, min_dim); + rec_reset_filtration(sh->second.children(), filt_value, min_depth - 1); } } } -- cgit v1.2.3 From e64dd4039e6dff35322ff01a8aa82d79c77e9c8e Mon Sep 17 00:00:00 2001 From: ROUVREAU Vincent Date: Mon, 28 Sep 2020 09:44:43 +0200 Subject: doc review: improve reset_filtration documentation --- src/Simplex_tree/include/gudhi/Simplex_tree.h | 4 ++-- src/python/gudhi/simplex_tree.pyx | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) (limited to 'src/Simplex_tree/include/gudhi/Simplex_tree.h') diff --git a/src/Simplex_tree/include/gudhi/Simplex_tree.h b/src/Simplex_tree/include/gudhi/Simplex_tree.h index 70ef4c66..5afd0e23 100644 --- a/src/Simplex_tree/include/gudhi/Simplex_tree.h +++ b/src/Simplex_tree/include/gudhi/Simplex_tree.h @@ -1668,8 +1668,8 @@ class Simplex_tree { } public: - /** \brief This function resets filtration value from a given dimension. Resets all the Simplex_tree when - * `min_dim = 0`. + /** \brief This function resets the filtration value of all the simplices of dimension at least min_dim. Resets all + * the Simplex_tree when `min_dim = 0`. * `reset_filtration` may break the filtration property with `min_dim > 0`, and it is the user's responsibility to * make it a valid filtration (using a large enough `filt_value`, or calling `make_filtration_non_decreasing` * afterwards for instance). diff --git a/src/python/gudhi/simplex_tree.pyx b/src/python/gudhi/simplex_tree.pyx index f28990cc..910711a9 100644 --- a/src/python/gudhi/simplex_tree.pyx +++ b/src/python/gudhi/simplex_tree.pyx @@ -359,8 +359,8 @@ cdef class SimplexTree: return self.get_ptr().make_filtration_non_decreasing() def reset_filtration(self, filtration, min_dim = 0): - """This function resets filtration value from a given dimension. Resets all the simplex tree when - `min_dim = 0`. + """This function resets the filtration value of all the simplices of dimension at least min_dim. Resets all the + simplex tree when `min_dim = 0`. `reset_filtration` may break the filtration property with `min_dim > 0`, and it is the user's responsibility to make it a valid filtration (using a large enough `filt_value`, or calling `make_filtration_non_decreasing` afterwards for instance). -- cgit v1.2.3 From 0c843fd01f0cd1ccff92c8ed40e989b3fbf8f1e9 Mon Sep 17 00:00:00 2001 From: ROUVREAU Vincent Date: Mon, 28 Sep 2020 09:56:44 +0200 Subject: code review: Simplify recursivity --- src/Simplex_tree/include/gudhi/Simplex_tree.h | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) (limited to 'src/Simplex_tree/include/gudhi/Simplex_tree.h') diff --git a/src/Simplex_tree/include/gudhi/Simplex_tree.h b/src/Simplex_tree/include/gudhi/Simplex_tree.h index 5afd0e23..85d6c3b0 100644 --- a/src/Simplex_tree/include/gudhi/Simplex_tree.h +++ b/src/Simplex_tree/include/gudhi/Simplex_tree.h @@ -1677,14 +1677,7 @@ class Simplex_tree { * @param[in] min_dim The minimal dimension. Default value is 0. */ void reset_filtration(Filtration_value filt_value, int min_dim = 0) { - for (auto& simplex : root_.members()) { - if (min_dim <= 0) { - simplex.second.assign_filtration(filt_value); - } - if (has_children(&simplex)) { - rec_reset_filtration(simplex.second.children(), filt_value, min_dim - 1); - } - } + rec_reset_filtration(&root_, filt_value, min_dim); clear_filtration(); // Drop the cache. } -- cgit v1.2.3