From 2c353d0c3db1c5c8fc11cb28ea65c90c559766a1 Mon Sep 17 00:00:00 2001 From: glisse Date: Mon, 10 Jul 2017 15:05:46 +0000 Subject: Cubical: use +inf instead of UINT_MAX as filtration value for non-simplices. git-svn-id: svn+ssh://scm.gforge.inria.fr/svnroot/gudhi/trunk@2595 636b058d-ea47-450e-bf9e-a15bfbe3eedb Former-commit-id: c578be16d9559b6d271cdb63be3a58a015735ab7 --- .../include/gudhi/Bitmap_cubical_complex.h | 27 +++++++++++----------- src/cython/test/test_cubical_complex.py | 4 ++-- 2 files changed, 16 insertions(+), 15 deletions(-) (limited to 'src') diff --git a/src/Bitmap_cubical_complex/include/gudhi/Bitmap_cubical_complex.h b/src/Bitmap_cubical_complex/include/gudhi/Bitmap_cubical_complex.h index 5a87b9b8..f395de65 100644 --- a/src/Bitmap_cubical_complex/include/gudhi/Bitmap_cubical_complex.h +++ b/src/Bitmap_cubical_complex/include/gudhi/Bitmap_cubical_complex.h @@ -97,7 +97,7 @@ class Bitmap_cubical_complex : public T { * with filtration on top dimensional cells. **/ Bitmap_cubical_complex(const std::vector& dimensions, - const std::vector& top_dimensional_cells) : + const std::vector& top_dimensional_cells) : T(dimensions, top_dimensional_cells), key_associated_to_simplex(this->total_number_of_cells + 1) { for (size_t i = 0; i != this->total_number_of_cells; ++i) { @@ -111,13 +111,13 @@ class Bitmap_cubical_complex : public T { /** * Constructor that requires vector of elements of type unsigned, which gives number of top dimensional cells - * in the following directions and vector of element of a type T::filtration_type + * in the following directions and vector of element of a type Filtration_value * with filtration on top dimensional cells. The last parameter of the constructor is a vector of boolean of a length * equal to the dimension of cubical complex. * If the position i on this vector is true, then we impose periodic boundary conditions in this direction. **/ Bitmap_cubical_complex(const std::vector& dimensions, - const std::vector& top_dimensional_cells, + const std::vector& top_dimensional_cells, std::vector< bool > directions_in_which_periodic_b_cond_are_to_be_imposed) : T(dimensions, top_dimensional_cells, directions_in_which_periodic_b_cond_are_to_be_imposed), key_associated_to_simplex(this->total_number_of_cells + 1) { @@ -170,20 +170,20 @@ class Bitmap_cubical_complex : public T { if (globalDbg) { std::cerr << "unsigned dimension(const Simplex_handle& sh)\n"; } - if (sh != std::numeric_limits::max()) return this->get_dimension_of_a_cell(sh); + if (sh != null_simplex()) return this->get_dimension_of_a_cell(sh); return -1; } /** * Return the filtration of a cell pointed by the Simplex_handle. **/ - typename T::filtration_type filtration(Simplex_handle sh) { + Filtration_value filtration(Simplex_handle sh) { if (globalDbg) { - std::cerr << "T::filtration_type filtration(const Simplex_handle& sh)\n"; + std::cerr << "Filtration_value filtration(const Simplex_handle& sh)\n"; } // Returns the filtration value of a simplex. - if (sh != std::numeric_limits::max()) return this->data[sh]; - return std::numeric_limits::max(); + if (sh != null_simplex()) return this->data[sh]; + return std::numeric_limits::infinity(); } /** @@ -203,7 +203,7 @@ class Bitmap_cubical_complex : public T { if (globalDbg) { std::cerr << "Simplex_key key(const Simplex_handle& sh)\n"; } - if (sh != std::numeric_limits::max()) { + if (sh != null_simplex()) { return this->key_associated_to_simplex[sh]; } return this->null_key(); @@ -216,7 +216,7 @@ class Bitmap_cubical_complex : public T { if (globalDbg) { std::cerr << "Simplex_handle simplex(Simplex_key key)\n"; } - if (key != std::numeric_limits::max()) { + if (key != null_key()) { return this->simplex_associated_to_key[ key ]; } return null_simplex(); @@ -229,7 +229,7 @@ class Bitmap_cubical_complex : public T { if (globalDbg) { std::cerr << "void assign_key(Simplex_handle& sh, Simplex_key key)\n"; } - if (key == std::numeric_limits::max()) return; + if (key == null_key()) return; this->key_associated_to_simplex[sh] = key; this->simplex_associated_to_key[key] = sh; } @@ -566,8 +566,9 @@ class is_before_in_filtration { bool operator()(const typename Bitmap_cubical_complex::Simplex_handle& sh1, const typename Bitmap_cubical_complex::Simplex_handle& sh2) const { // Not using st_->filtration(sh1) because it uselessly tests for null_simplex. - typename T::filtration_type fil1 = CC_->data[sh1]; - typename T::filtration_type fil2 = CC_->data[sh2]; + typedef typename T::filtration_type Filtration_value; + Filtration_value fil1 = CC_->data[sh1]; + Filtration_value fil2 = CC_->data[sh2]; if (fil1 != fil2) { return fil1 < fil2; } diff --git a/src/cython/test/test_cubical_complex.py b/src/cython/test/test_cubical_complex.py index 2e281ee4..9a365823 100755 --- a/src/cython/test/test_cubical_complex.py +++ b/src/cython/test/test_cubical_complex.py @@ -67,7 +67,7 @@ def test_dimension_constructor(): top_dimensional_cells = [1,2,3,4,5,6,7,8,9]) assert cub.__is_defined() == True assert cub.__is_persistence_defined() == False - assert cub.persistence() == [(1, (0.0, 100.0)), (0, (0.0, 1.8446744073709552e+19))] + assert cub.persistence() == [(1, (0.0, 100.0)), (0, (0.0, float('inf')))] assert cub.__is_persistence_defined() == True assert cub.betti_numbers() == [1, 0] assert cub.persistent_betti_numbers(0, 1000) == [0, 0] @@ -80,7 +80,7 @@ def test_dimension_constructor(): cub = CubicalComplex(perseus_file='CubicalOneSphere.txt') assert cub.__is_defined() == True assert cub.__is_persistence_defined() == False - assert cub.persistence() == [(1, (0.0, 100.0)), (0, (0.0, 1.8446744073709552e+19))] + assert cub.persistence() == [(1, (0.0, 100.0)), (0, (0.0, float('inf')))] assert cub.__is_persistence_defined() == True assert cub.betti_numbers() == [1, 0, 0] assert cub.persistent_betti_numbers(0, 1000) == [1, 0, 0] -- cgit v1.2.3 From b8fdc4c7f6c30df5e7a28a0a23e697640f9d67cc Mon Sep 17 00:00:00 2001 From: glisse Date: Mon, 10 Jul 2017 18:58:12 +0000 Subject: simplex / complex confusion git-svn-id: svn+ssh://scm.gforge.inria.fr/svnroot/gudhi/trunk@2596 636b058d-ea47-450e-bf9e-a15bfbe3eedb Former-commit-id: 77c093029d3e2518260ce59705110df3fbfe11e3 --- src/cython/cython/simplex_tree.pyx | 48 ++++++++++++++--------------- src/cython/include/Simplex_tree_interface.h | 8 ++--- 2 files changed, 28 insertions(+), 28 deletions(-) (limited to 'src') diff --git a/src/cython/cython/simplex_tree.pyx b/src/cython/cython/simplex_tree.pyx index 9d40a8b5..2acdac3c 100644 --- a/src/cython/cython/simplex_tree.pyx +++ b/src/cython/cython/simplex_tree.pyx @@ -183,10 +183,10 @@ cdef class SimplexTree: :returns: true if the simplex was found, false otherwise. :rtype: bool """ - cdef vector[int] complex + cdef vector[int] csimplex for i in simplex: - complex.push_back(i) - return self.thisptr.find_simplex(complex) + csimplex.push_back(i) + return self.thisptr.find_simplex(csimplex) def insert(self, simplex, filtration=0.0): """This function inserts the given N-simplex and its subfaces with the @@ -200,10 +200,10 @@ cdef class SimplexTree: :returns: true if the simplex was found, false otherwise. :rtype: bool """ - cdef vector[int] complex + cdef vector[int] csimplex for i in simplex: - complex.push_back(i) - return self.thisptr.insert_simplex_and_subfaces(complex, + csimplex.push_back(i) + return self.thisptr.insert_simplex_and_subfaces(csimplex, filtration) def get_filtration(self): @@ -232,35 +232,35 @@ cdef class SimplexTree: :returns: The (simplices of the) skeleton of a maximum dimension. :rtype: list of tuples(simplex, filtration) """ - cdef vector[pair[vector[int], double]] skeletons \ + cdef vector[pair[vector[int], double]] skeleton \ = self.thisptr.get_skeleton(dimension) ct = [] - for filtered_complex in skeletons: + for filtered_simplex in skeleton: v = [] - for vertex in filtered_complex.first: + for vertex in filtered_simplex.first: v.append(vertex) - ct.append((v, filtered_complex.second)) + ct.append((v, filtered_simplex.second)) return ct def get_star(self, simplex): - """This function returns the stars of a given N-simplex. + """This function returns the star of a given N-simplex. :param simplex: The N-simplex, represented by a list of vertex. :type simplex: list of int. :returns: The (simplices of the) star of a simplex. :rtype: list of tuples(simplex, filtration) """ - cdef vector[int] complex + cdef vector[int] csimplex for i in simplex: - complex.push_back(i) - cdef vector[pair[vector[int], double]] stars \ - = self.thisptr.get_star(complex) + csimplex.push_back(i) + cdef vector[pair[vector[int], double]] star \ + = self.thisptr.get_star(csimplex) ct = [] - for filtered_complex in stars: + for filtered_simplex in star: v = [] - for vertex in filtered_complex.first: + for vertex in filtered_simplex.first: v.append(vertex) - ct.append((v, filtered_complex.second)) + ct.append((v, filtered_simplex.second)) return ct def get_cofaces(self, simplex, codimension): @@ -275,17 +275,17 @@ cdef class SimplexTree: :returns: The (simplices of the) cofaces of a simplex :rtype: list of tuples(simplex, filtration) """ - cdef vector[int] complex + cdef vector[int] csimplex for i in simplex: - complex.push_back(i) + csimplex.push_back(i) cdef vector[pair[vector[int], double]] cofaces \ - = self.thisptr.get_cofaces(complex, codimension) + = self.thisptr.get_cofaces(csimplex, codimension) ct = [] - for filtered_complex in cofaces: + for filtered_simplex in cofaces: v = [] - for vertex in filtered_complex.first: + for vertex in filtered_simplex.first: v.append(vertex) - ct.append((v, filtered_complex.second)) + ct.append((v, filtered_simplex.second)) return ct def remove_maximal_simplex(self, simplex): diff --git a/src/cython/include/Simplex_tree_interface.h b/src/cython/include/Simplex_tree_interface.h index 45ce1916..09e7e992 100644 --- a/src/cython/include/Simplex_tree_interface.h +++ b/src/cython/include/Simplex_tree_interface.h @@ -70,14 +70,14 @@ class Simplex_tree_interface : public Simplex_tree { } // Do not interface this function, only used in strong witness interface for complex creation - bool insert_simplex(const std::vector& complex, Filtration_value filtration = 0) { - Insertion_result result = Base::insert_simplex(complex, filtration); + bool insert_simplex(const std::vector& simplex, Filtration_value filtration = 0) { + Insertion_result result = Base::insert_simplex(simplex, filtration); return (result.second); } // Do not interface this function, only used in strong witness interface for complex creation - bool insert_simplex_and_subfaces(const std::vector& complex, Filtration_value filtration = 0) { - Insertion_result result = Base::insert_simplex_and_subfaces(complex, filtration); + bool insert_simplex_and_subfaces(const std::vector& simplex, Filtration_value filtration = 0) { + Insertion_result result = Base::insert_simplex_and_subfaces(simplex, filtration); return (result.second); } -- cgit v1.2.3 From 1129608c2add6f15538b3e281d75119f0e1bc8b0 Mon Sep 17 00:00:00 2001 From: vrouvrea Date: Tue, 8 Aug 2017 12:48:40 +0000 Subject: Fix bug in read_persistence_intervals_in_dimension for only_this_dim Add unitary tests for read_persistence Add .clang-format to clang format new files git-svn-id: svn+ssh://scm.gforge.inria.fr/svnroot/gudhi/trunk@2601 636b058d-ea47-450e-bf9e-a15bfbe3eedb Former-commit-id: 28e870973f9ebd686d503246202fa6d6a549e9e3 --- src/.clang-format | 90 ++++++ src/common/include/gudhi/reader_utils.h | 93 +++--- src/common/test/CMakeLists.txt | 7 + src/common/test/README | 2 +- .../test/persistence_intervals_with_dimension.pers | 5 + .../test/persistence_intervals_with_field.pers | 4 + .../persistence_intervals_without_dimension.pers | 7 + .../test/test_persistence_intervals_reader.cpp | 321 +++++++++++++++++++++ 8 files changed, 478 insertions(+), 51 deletions(-) create mode 100644 src/.clang-format create mode 100644 src/common/test/persistence_intervals_with_dimension.pers create mode 100644 src/common/test/persistence_intervals_with_field.pers create mode 100644 src/common/test/persistence_intervals_without_dimension.pers create mode 100644 src/common/test/test_persistence_intervals_reader.cpp (limited to 'src') diff --git a/src/.clang-format b/src/.clang-format new file mode 100644 index 00000000..dd4590c8 --- /dev/null +++ b/src/.clang-format @@ -0,0 +1,90 @@ +--- +Language: Cpp +# BasedOnStyle: Google +AccessModifierOffset: -1 +AlignAfterOpenBracket: Align +AlignConsecutiveAssignments: false +AlignConsecutiveDeclarations: false +AlignEscapedNewlinesLeft: true +AlignOperands: true +AlignTrailingComments: true +AllowAllParametersOfDeclarationOnNextLine: true +AllowShortBlocksOnASingleLine: false +AllowShortCaseLabelsOnASingleLine: false +AllowShortFunctionsOnASingleLine: All +AllowShortIfStatementsOnASingleLine: true +AllowShortLoopsOnASingleLine: true +AlwaysBreakAfterDefinitionReturnType: None +AlwaysBreakAfterReturnType: None +AlwaysBreakBeforeMultilineStrings: true +AlwaysBreakTemplateDeclarations: true +BinPackArguments: true +BinPackParameters: true +BraceWrapping: + AfterClass: false + AfterControlStatement: false + AfterEnum: false + AfterFunction: false + AfterNamespace: false + AfterObjCDeclaration: false + AfterStruct: false + AfterUnion: false + BeforeCatch: false + BeforeElse: false + IndentBraces: false +BreakBeforeBinaryOperators: None +BreakBeforeBraces: Attach +BreakBeforeTernaryOperators: true +BreakConstructorInitializersBeforeComma: false +ColumnLimit: 120 +CommentPragmas: '^ IWYU pragma:' +ConstructorInitializerAllOnOneLineOrOnePerLine: true +ConstructorInitializerIndentWidth: 4 +ContinuationIndentWidth: 4 +Cpp11BracedListStyle: true +DerivePointerAlignment: true +DisableFormat: false +ExperimentalAutoDetectBinPacking: false +ForEachMacros: [ foreach, Q_FOREACH, BOOST_FOREACH ] +IncludeCategories: + - Regex: '^<.*\.h>' + Priority: 1 + - Regex: '^<.*' + Priority: 2 + - Regex: '.*' + Priority: 3 +IndentCaseLabels: true +IndentWidth: 2 +IndentWrappedFunctionNames: false +KeepEmptyLinesAtTheStartOfBlocks: false +MacroBlockBegin: '' +MacroBlockEnd: '' +MaxEmptyLinesToKeep: 1 +NamespaceIndentation: None +ObjCBlockIndentWidth: 2 +ObjCSpaceAfterProperty: false +ObjCSpaceBeforeProtocolList: false +PenaltyBreakBeforeFirstCallParameter: 1 +PenaltyBreakComment: 300 +PenaltyBreakFirstLessLess: 120 +PenaltyBreakString: 1000 +PenaltyExcessCharacter: 1000000 +PenaltyReturnTypeOnItsOwnLine: 200 +PointerAlignment: Left +ReflowComments: true +SortIncludes: false +SpaceAfterCStyleCast: false +SpaceBeforeAssignmentOperators: true +SpaceBeforeParens: ControlStatements +SpaceInEmptyParentheses: false +SpacesBeforeTrailingComments: 2 +SpacesInAngles: false +SpacesInContainerLiterals: true +SpacesInCStyleCastParentheses: false +SpacesInParentheses: false +SpacesInSquareBrackets: false +Standard: Auto +TabWidth: 8 +UseTab: Never +... + diff --git a/src/common/include/gudhi/reader_utils.h b/src/common/include/gudhi/reader_utils.h index f1684d78..bda93f4f 100644 --- a/src/common/include/gudhi/reader_utils.h +++ b/src/common/include/gudhi/reader_utils.h @@ -1,5 +1,5 @@ -/* This file is part of the Gudhi Library. The Gudhi library - * (Geometric Understanding in Higher Dimensions) is a generic C++ +/* This file is part of the Gudhi Library. The Gudhi library + * (Geometric Understanding in Higher Dimensions) is a generic C++ * library for computational topology. * * Author(s): Clement Maria, Pawel Dlotko, Clement Jamin @@ -36,6 +36,7 @@ #include #include #include // for pair +#include // for std::make_tuple // Keep this file tag for Doxygen to parse the code, otherwise, functions are not documented. // It is required for global functions and variables. @@ -52,7 +53,7 @@ * X21 X22 ... X2d
* etc
*/ -inline void read_points(std::string file_name, std::vector< std::vector< double > > & points) { +inline void read_points(std::string file_name, std::vector>& points) { std::ifstream in_file(file_name.c_str(), std::ios::in); if (!in_file.is_open()) { std::cerr << "Unable to open file " << file_name << std::endl; @@ -62,14 +63,13 @@ inline void read_points(std::string file_name, std::vector< std::vector< double std::string line; double x; while (getline(in_file, line)) { - std::vector< double > point; + std::vector point; std::istringstream iss(line); while (iss >> x) { point.push_back(x); } // Check for empty lines - if (!point.empty()) - points.push_back(point); + if (!point.empty()) points.push_back(point); } in_file.close(); } @@ -90,7 +90,7 @@ inline void read_points(std::string file_name, std::vector< std::vector< double * Every simplex must appear exactly once. * Simplices of dimension more than 1 are ignored. */ -template< typename Graph_t, typename Filtration_value, typename Vertex_handle > +template Graph_t read_graph(std::string file_name) { std::ifstream in_(file_name.c_str(), std::ios::in); if (!in_.is_open()) { @@ -100,10 +100,10 @@ Graph_t read_graph(std::string file_name) { throw std::invalid_argument(error_str); } - typedef std::pair< Vertex_handle, Vertex_handle > Edge_t; - std::vector< Edge_t > edges; - std::vector< Filtration_value > edges_fil; - std::map< Vertex_handle, Filtration_value > vertices; + typedef std::pair Edge_t; + std::vector edges; + std::vector edges_fil; + std::map vertices; std::string line; int dim; @@ -113,8 +113,7 @@ Graph_t read_graph(std::string file_name) { std::istringstream iss(line); while (iss >> dim) { switch (dim) { - case 0: - { + case 0: { iss >> u; iss >> fil; vertices[u] = fil; @@ -123,8 +122,7 @@ Graph_t read_graph(std::string file_name) { } break; } - case 1: - { + case 1: { iss >> u; iss >> v; iss >> fil; @@ -132,16 +130,13 @@ Graph_t read_graph(std::string file_name) { edges_fil.push_back(fil); break; } - default: - { - break; - } + default: { break; } } } } in_.close(); - if ((size_t) (max_h + 1) != vertices.size()) { + if ((size_t)(max_h + 1) != vertices.size()) { std::cerr << "Error: vertices must be labeled from 0 to n-1 \n"; } @@ -169,8 +164,8 @@ Graph_t read_graph(std::string file_name) { * Every simplex must appear exactly once. * Simplices of dimension more than 1 are ignored. */ -template< typename Vertex_handle, typename Filtration_value > -bool read_simplex(std::istream & in_, std::vector< Vertex_handle > & simplex, Filtration_value & fil) { +template +bool read_simplex(std::istream& in_, std::vector& simplex, Filtration_value& fil) { int dim = 0; if (!(in_ >> dim)) return false; Vertex_handle v; @@ -194,8 +189,8 @@ bool read_simplex(std::istream & in_, std::vector< Vertex_handle > & simplex, Fi * The key of a simplex is its position in the filtration order and also the number of its row in the file. * Dimi ki1 ki2 ... kiDimi Fili means that the ith simplex in the filtration has dimension Dimi, filtration value * fil1 and simplices with key ki1 ... kiDimi in its boundary.*/ -template< typename Simplex_key, typename Filtration_value > -bool read_hasse_simplex(std::istream & in_, std::vector< Simplex_key > & boundary, Filtration_value & fil) { +template +bool read_hasse_simplex(std::istream& in_, std::vector& boundary, Filtration_value& fil) { int dim; if (!(in_ >> dim)) return false; if (dim == 0) { @@ -214,7 +209,7 @@ bool read_hasse_simplex(std::istream & in_, std::vector< Simplex_key > & boundar /** * @brief Read a lower triangular distance matrix from a csv file. We assume that the .csv store the whole * (square) matrix. - * + * * @author Pawel Dlotko * * Square matrix file format:
@@ -231,13 +226,13 @@ bool read_hasse_simplex(std::istream & in_, std::vector< Simplex_key > & boundar * Dj1;Dj2;...;Dj(j-1);
* **/ -template< typename Filtration_value > -std::vector< std::vector< Filtration_value > > read_lower_triangular_matrix_from_csv_file(const std::string& filename, - const char separator = ';') { +template +std::vector> read_lower_triangular_matrix_from_csv_file(const std::string& filename, + const char separator = ';') { #ifdef DEBUG_TRACES std::cout << "Using procedure read_lower_triangular_matrix_from_csv_file \n"; #endif // DEBUG_TRACES - std::vector< std::vector< Filtration_value > > result; + std::vector> result; std::ifstream in; in.open(filename.c_str()); if (!in.is_open()) { @@ -248,7 +243,7 @@ std::vector< std::vector< Filtration_value > > read_lower_triangular_matrix_from // the first line is emtpy, so we ignore it: std::getline(in, line); - std::vector< Filtration_value > values_in_this_line; + std::vector values_in_this_line; result.push_back(values_in_this_line); int number_of_line = 0; @@ -256,11 +251,10 @@ std::vector< std::vector< Filtration_value > > read_lower_triangular_matrix_from // first, read the file line by line to a string: while (std::getline(in, line)) { // if line is empty, break - if (line.size() == 0) - break; + if (line.size() == 0) break; // if the last element of a string is comma: - if (line[ line.size() - 1 ] == separator) { + if (line[line.size() - 1] == separator) { // then shrink the string by one line.pop_back(); } @@ -273,7 +267,7 @@ std::vector< std::vector< Filtration_value > > read_lower_triangular_matrix_from // and now read the doubles. int number_of_entry = 0; - std::vector< Filtration_value > values_in_this_line; + std::vector values_in_this_line; while (iss.good()) { double entry; iss >> entry; @@ -282,7 +276,7 @@ std::vector< std::vector< Filtration_value > > read_lower_triangular_matrix_from } ++number_of_entry; } - if (!values_in_this_line.empty())result.push_back(values_in_this_line); + if (!values_in_this_line.empty()) result.push_back(values_in_this_line); ++number_of_line; } in.close(); @@ -309,7 +303,6 @@ Note: the function does not check that birth <= death. **/ template void read_persistence_intervals_and_dimension(std::string const& filename, OutputIterator out) { - std::ifstream in(filename); if (!in.is_open()) { std::string error_str("read_persistence_intervals_and_dimension - Unable to open file "); @@ -325,13 +318,12 @@ void read_persistence_intervals_and_dimension(std::string const& filename, Outpu double numbers[4]; int n = sscanf(line.c_str(), "%lf %lf %lf %lf", &numbers[0], &numbers[1], &numbers[2], &numbers[3]); if (n >= 2) { - //int field = (n == 4 ? static_cast(numbers[0]) : -1); int dim = (n >= 3 ? static_cast(numbers[n - 3]) : -1); *out++ = std::make_tuple(dim, numbers[n - 2], numbers[n - 1]); } } } -} // read_persistence_diagram_from_file +} /** Reads a file containing persistence intervals. @@ -340,33 +332,34 @@ The return value is an `std::map>>` where `dim` is an `int`, `birth` a `double`, and `death` a `double`. Note: the function does not check that birth <= death. **/ -inline std::map>> read_persistence_intervals_grouped_by_dimension(std::string const& filename) { - +inline std::map>> read_persistence_intervals_grouped_by_dimension( + std::string const& filename) { std::map>> ret; read_persistence_intervals_and_dimension( - filename, - boost::make_function_output_iterator([&ret](std::tuple t) { ret[get<0>(t)].push_back(std::make_pair(get<1>(t), get<2>(t))); })); + filename, boost::make_function_output_iterator([&ret](std::tuple t) { + ret[get<0>(t)].push_back(std::make_pair(get<1>(t), get<2>(t))); + })); return ret; -} // read_persistence_diagram_from_file - +} /** Reads a file containing persistence intervals. Each line might contain 2, 3 or 4 values: [[field] dimension] birth death If `only_this_dim` = -1, dimension is ignored and all lines are returned. -If `only_this_dim` is >= 0, only the lines where dimension = `only_this_dim` +If `only_this_dim` is >= 0, only the lines where dimension = `only_this_dim` (or where dimension is not specified) are returned. The return value is an `std::vector>` where `dim` is an `int`, `birth` a `double`, and `death` a `double`. Note: the function does not check that birth <= death. **/ -inline std::vector> read_persistence_intervals_in_dimension(std::string const& filename, int only_this_dim = -1) { - +inline std::vector> read_persistence_intervals_in_dimension(std::string const& filename, + int only_this_dim = -1) { std::vector> ret; read_persistence_intervals_and_dimension( - filename, - boost::make_function_output_iterator([&ret](std::tuple t) { ret.emplace_back(get<1>(t), get<2>(t)); })); + filename, boost::make_function_output_iterator([only_this_dim, &ret](std::tuple t) { + if (only_this_dim == get<0>(t) || only_this_dim == -1) ret.emplace_back(get<1>(t), get<2>(t)); + })); return ret; -} // read_persistence_diagram_from_file +} #endif // READER_UTILS_H_ diff --git a/src/common/test/CMakeLists.txt b/src/common/test/CMakeLists.txt index 5aa426d7..de3e765a 100644 --- a/src/common/test/CMakeLists.txt +++ b/src/common/test/CMakeLists.txt @@ -9,10 +9,17 @@ target_link_libraries(Common_test_points_off_reader ${Boost_UNIT_TEST_FRAMEWORK_ add_executable ( Common_test_distance_matrix_reader test_distance_matrix_reader.cpp ) target_link_libraries(Common_test_distance_matrix_reader ${Boost_UNIT_TEST_FRAMEWORK_LIBRARY}) +add_executable ( Common_test_persistence_intervals_reader test_persistence_intervals_reader.cpp ) +target_link_libraries(Common_test_persistence_intervals_reader ${Boost_UNIT_TEST_FRAMEWORK_LIBRARY}) + # Do not forget to copy test files in current binary dir file(COPY "${CMAKE_SOURCE_DIR}/data/points/alphacomplexdoc.off" DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/) file(COPY "${CMAKE_SOURCE_DIR}/data/distance_matrix/lower_triangular_distance_matrix.csv" DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/) file(COPY "${CMAKE_SOURCE_DIR}/data/distance_matrix/full_square_distance_matrix.csv" DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/) +file(COPY "${CMAKE_SOURCE_DIR}/src/common/test/persistence_intervals_with_dimension.pers" DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/) +file(COPY "${CMAKE_SOURCE_DIR}/src/common/test/persistence_intervals_with_field.pers" DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/) +file(COPY "${CMAKE_SOURCE_DIR}/src/common/test/persistence_intervals_without_dimension.pers" DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/) gudhi_add_coverage_test(Common_test_points_off_reader) gudhi_add_coverage_test(Common_test_distance_matrix_reader) +gudhi_add_coverage_test(Common_test_persistence_intervals_reader) diff --git a/src/common/test/README b/src/common/test/README index f2a7eb5a..a8e6efe9 100644 --- a/src/common/test/README +++ b/src/common/test/README @@ -7,7 +7,7 @@ make To launch with details: *********************** -./dtoffrw_UT --report_level=detailed --log_level=all +./Common_test_points_off_reader --report_level=detailed --log_level=all ==> echo $? returns 0 in case of success (non-zero otherwise) diff --git a/src/common/test/persistence_intervals_with_dimension.pers b/src/common/test/persistence_intervals_with_dimension.pers new file mode 100644 index 00000000..406748c8 --- /dev/null +++ b/src/common/test/persistence_intervals_with_dimension.pers @@ -0,0 +1,5 @@ +# Simple persistence diagram with dimension +0 2.7 3.7 +1 9.6 14. +3 34.2 34.974 +1 3. inf diff --git a/src/common/test/persistence_intervals_with_field.pers b/src/common/test/persistence_intervals_with_field.pers new file mode 100644 index 00000000..41dd9f1e --- /dev/null +++ b/src/common/test/persistence_intervals_with_field.pers @@ -0,0 +1,4 @@ +3 0 2.7 3.7 +3 1 9.6 14. +3 3 34.2 34.974 +3 1 3. inf diff --git a/src/common/test/persistence_intervals_without_dimension.pers b/src/common/test/persistence_intervals_without_dimension.pers new file mode 100644 index 00000000..76fa27f3 --- /dev/null +++ b/src/common/test/persistence_intervals_without_dimension.pers @@ -0,0 +1,7 @@ +# Simple persistence diagram without dimension +2.7 3.7 +9.6 14. +# Another comment +34.2 34.974 +3. inf +# End of file diff --git a/src/common/test/test_persistence_intervals_reader.cpp b/src/common/test/test_persistence_intervals_reader.cpp new file mode 100644 index 00000000..a06fff1e --- /dev/null +++ b/src/common/test/test_persistence_intervals_reader.cpp @@ -0,0 +1,321 @@ +/* This file is part of the Gudhi Library. The Gudhi library + * (Geometric Understanding in Higher Dimensions) is a generic C++ + * library for computational topology. + * + * Author(s): Vincent Rouvreau + * + * Copyright (C) 2017 INRIA + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include + +#include +#include +#include // for pair +#include +#include // for inf + +#define BOOST_TEST_DYN_LINK +#define BOOST_TEST_MODULE "persistence_intervals_reader" +#include + +using Persistence_intervals_by_dimension = std::map>>; +using Persistence_intervals = std::vector>; +// Test files with only 2 parameters (persistence birth and death) per line in file +BOOST_AUTO_TEST_CASE( persistence_intervals_without_dimension ) +{ + Persistence_intervals_by_dimension expected_intervals_by_dimension; + expected_intervals_by_dimension[-1].push_back(std::make_pair(2.7, 3.7)); + expected_intervals_by_dimension[-1].push_back(std::make_pair(9.6, 14.)); + expected_intervals_by_dimension[-1].push_back(std::make_pair(34.2, 34.974)); + expected_intervals_by_dimension[-1].push_back(std::make_pair(3., std::numeric_limits::infinity())); + + Persistence_intervals_by_dimension persistence_intervals_by_dimension = + read_persistence_intervals_grouped_by_dimension("persistence_intervals_without_dimension.pers"); + + std::cout << "\nread_persistence_intervals_grouped_by_dimension - expected\n"; + for (auto map_iter : expected_intervals_by_dimension) { + std::cout << "key=" << map_iter.first; + for (auto vec_iter : map_iter.second) + std::cout << " [" << vec_iter.first << " ," << vec_iter.second << "] "; + } + + std::cout << "\nread_persistence_intervals_grouped_by_dimension - read\n"; + for (auto map_iter : persistence_intervals_by_dimension) { + std::cout << "key=" << map_iter.first; + for (auto vec_iter : map_iter.second) + std::cout << " [" << vec_iter.first << " ," << vec_iter.second << "] "; + } + + BOOST_CHECK(persistence_intervals_by_dimension == expected_intervals_by_dimension); + + Persistence_intervals expected_intervals_in_dimension; + expected_intervals_in_dimension.push_back(std::make_pair(2.7, 3.7)); + expected_intervals_in_dimension.push_back(std::make_pair(9.6, 14.)); + expected_intervals_in_dimension.push_back(std::make_pair(34.2, 34.974)); + expected_intervals_in_dimension.push_back(std::make_pair(3., std::numeric_limits::infinity())); + + Persistence_intervals persistence_intervals_in_dimension = + read_persistence_intervals_in_dimension("persistence_intervals_without_dimension.pers"); + + std::cout << "\nread_persistence_intervals_in_dimension - expected\n"; + for (auto vec_iter : expected_intervals_in_dimension) + std::cout << " [" << vec_iter.first << " ," << vec_iter.second << "] "; + + std::cout << "\nread_persistence_intervals_in_dimension - read\n"; + for (auto vec_iter : expected_intervals_in_dimension) + std::cout << " [" << vec_iter.first << " ," << vec_iter.second << "] "; + + BOOST_CHECK(persistence_intervals_in_dimension == expected_intervals_in_dimension); + + expected_intervals_in_dimension.clear(); + persistence_intervals_in_dimension = + read_persistence_intervals_in_dimension("persistence_intervals_without_dimension.pers", 0); + BOOST_CHECK(persistence_intervals_in_dimension == expected_intervals_in_dimension); + + expected_intervals_in_dimension.clear(); + persistence_intervals_in_dimension = + read_persistence_intervals_in_dimension("persistence_intervals_without_dimension.pers", 1); + BOOST_CHECK(persistence_intervals_in_dimension == expected_intervals_in_dimension); + + expected_intervals_in_dimension.clear(); + persistence_intervals_in_dimension = + read_persistence_intervals_in_dimension("persistence_intervals_without_dimension.pers", 2); + BOOST_CHECK(persistence_intervals_in_dimension == expected_intervals_in_dimension); + + expected_intervals_in_dimension.clear(); + persistence_intervals_in_dimension = + read_persistence_intervals_in_dimension("persistence_intervals_without_dimension.pers", 3); + BOOST_CHECK(persistence_intervals_in_dimension == expected_intervals_in_dimension); + +} +// Test files with 3 parameters (dimension birth death) per line in file +BOOST_AUTO_TEST_CASE( persistence_intervals_with_dimension ) +{ + Persistence_intervals_by_dimension expected_intervals_by_dimension; + expected_intervals_by_dimension[0].push_back(std::make_pair(2.7, 3.7)); + expected_intervals_by_dimension[1].push_back(std::make_pair(9.6, 14.)); + expected_intervals_by_dimension[3].push_back(std::make_pair(34.2, 34.974)); + expected_intervals_by_dimension[1].push_back(std::make_pair(3., std::numeric_limits::infinity())); + + Persistence_intervals_by_dimension persistence_intervals_by_dimension = + read_persistence_intervals_grouped_by_dimension("persistence_intervals_with_dimension.pers"); + + std::cout << "\nread_persistence_intervals_grouped_by_dimension - expected\n"; + for (auto map_iter : expected_intervals_by_dimension) { + std::cout << "key=" << map_iter.first; + for (auto vec_iter : map_iter.second) + std::cout << " [" << vec_iter.first << " ," << vec_iter.second << "] "; + } + + std::cout << "\nread_persistence_intervals_grouped_by_dimension - read\n"; + for (auto map_iter : persistence_intervals_by_dimension) { + std::cout << "key=" << map_iter.first; + for (auto vec_iter : map_iter.second) + std::cout << " [" << vec_iter.first << " ," << vec_iter.second << "] "; + } + + BOOST_CHECK(persistence_intervals_by_dimension == expected_intervals_by_dimension); + + Persistence_intervals expected_intervals_in_dimension; + expected_intervals_in_dimension.push_back(std::make_pair(2.7, 3.7)); + expected_intervals_in_dimension.push_back(std::make_pair(9.6, 14.)); + expected_intervals_in_dimension.push_back(std::make_pair(34.2, 34.974)); + expected_intervals_in_dimension.push_back(std::make_pair(3., std::numeric_limits::infinity())); + + Persistence_intervals persistence_intervals_in_dimension = + read_persistence_intervals_in_dimension("persistence_intervals_with_dimension.pers"); + + std::cout << "\nread_persistence_intervals_in_dimension - expected\n"; + for (auto vec_iter : expected_intervals_in_dimension) + std::cout << " [" << vec_iter.first << " ," << vec_iter.second << "] "; + + std::cout << "\nread_persistence_intervals_in_dimension - read\n"; + for (auto vec_iter : persistence_intervals_in_dimension) + std::cout << " [" << vec_iter.first << " ," << vec_iter.second << "] "; + + BOOST_CHECK(persistence_intervals_in_dimension == expected_intervals_in_dimension); + + expected_intervals_in_dimension.clear(); + expected_intervals_in_dimension.push_back(std::make_pair(2.7, 3.7)); + persistence_intervals_in_dimension = + read_persistence_intervals_in_dimension("persistence_intervals_with_dimension.pers", 0); + + std::cout << "\nread_persistence_intervals_in_dimension 0 - expected\n"; + for (auto vec_iter : expected_intervals_in_dimension) + std::cout << " [" << vec_iter.first << " ," << vec_iter.second << "] "; + + std::cout << "\nread_persistence_intervals_in_dimension 0 - read\n"; + for (auto vec_iter : persistence_intervals_in_dimension) + std::cout << " [" << vec_iter.first << " ," << vec_iter.second << "] "; + + BOOST_CHECK(persistence_intervals_in_dimension == expected_intervals_in_dimension); + + expected_intervals_in_dimension.clear(); + expected_intervals_in_dimension.push_back(std::make_pair(9.6, 14.)); + expected_intervals_in_dimension.push_back(std::make_pair(3., std::numeric_limits::infinity())); + persistence_intervals_in_dimension = + read_persistence_intervals_in_dimension("persistence_intervals_with_dimension.pers", 1); + + std::cout << "\nread_persistence_intervals_in_dimension 1 - expected\n"; + for (auto vec_iter : expected_intervals_in_dimension) + std::cout << " [" << vec_iter.first << " ," << vec_iter.second << "] "; + + std::cout << "\nread_persistence_intervals_in_dimension 1 - read\n"; + for (auto vec_iter : persistence_intervals_in_dimension) + std::cout << " [" << vec_iter.first << " ," << vec_iter.second << "] "; + + BOOST_CHECK(persistence_intervals_in_dimension == expected_intervals_in_dimension); + + expected_intervals_in_dimension.clear(); + persistence_intervals_in_dimension = + read_persistence_intervals_in_dimension("persistence_intervals_with_dimension.pers", 2); + + std::cout << "\nread_persistence_intervals_in_dimension 2 - expected\n"; + for (auto vec_iter : expected_intervals_in_dimension) + std::cout << " [" << vec_iter.first << " ," << vec_iter.second << "] "; + + std::cout << "\nread_persistence_intervals_in_dimension 2 - read\n"; + for (auto vec_iter : persistence_intervals_in_dimension) + std::cout << " [" << vec_iter.first << " ," << vec_iter.second << "] "; + + BOOST_CHECK(persistence_intervals_in_dimension == expected_intervals_in_dimension); + + expected_intervals_in_dimension.clear(); + expected_intervals_in_dimension.push_back(std::make_pair(34.2, 34.974)); + persistence_intervals_in_dimension = + read_persistence_intervals_in_dimension("persistence_intervals_with_dimension.pers", 3); + + std::cout << "\nread_persistence_intervals_in_dimension 3 - expected\n"; + for (auto vec_iter : expected_intervals_in_dimension) + std::cout << " [" << vec_iter.first << " ," << vec_iter.second << "] "; + + std::cout << "\nread_persistence_intervals_in_dimension 3 - read\n"; + for (auto vec_iter : persistence_intervals_in_dimension) + std::cout << " [" << vec_iter.first << " ," << vec_iter.second << "] "; + + BOOST_CHECK(persistence_intervals_in_dimension == expected_intervals_in_dimension); + +} + +// Test files with 4 parameters (field dimension birth death) per line in file +BOOST_AUTO_TEST_CASE( persistence_intervals_with_field ) +{ + Persistence_intervals_by_dimension expected_intervals_by_dimension; + expected_intervals_by_dimension[0].push_back(std::make_pair(2.7, 3.7)); + expected_intervals_by_dimension[1].push_back(std::make_pair(9.6, 14.)); + expected_intervals_by_dimension[3].push_back(std::make_pair(34.2, 34.974)); + expected_intervals_by_dimension[1].push_back(std::make_pair(3., std::numeric_limits::infinity())); + + Persistence_intervals_by_dimension persistence_intervals_by_dimension = + read_persistence_intervals_grouped_by_dimension("persistence_intervals_with_field.pers"); + + std::cout << "\nread_persistence_intervals_grouped_by_dimension - expected\n"; + for (auto map_iter : expected_intervals_by_dimension) { + std::cout << "key=" << map_iter.first; + for (auto vec_iter : map_iter.second) + std::cout << " [" << vec_iter.first << " ," << vec_iter.second << "] "; + } + + std::cout << "\nread_persistence_intervals_grouped_by_dimension - read\n"; + for (auto map_iter : persistence_intervals_by_dimension) { + std::cout << "key=" << map_iter.first; + for (auto vec_iter : map_iter.second) + std::cout << " [" << vec_iter.first << " ," << vec_iter.second << "] "; + } + + BOOST_CHECK(persistence_intervals_by_dimension == expected_intervals_by_dimension); + + Persistence_intervals expected_intervals_in_dimension; + expected_intervals_in_dimension.push_back(std::make_pair(2.7, 3.7)); + expected_intervals_in_dimension.push_back(std::make_pair(9.6, 14.)); + expected_intervals_in_dimension.push_back(std::make_pair(34.2, 34.974)); + expected_intervals_in_dimension.push_back(std::make_pair(3., std::numeric_limits::infinity())); + + Persistence_intervals persistence_intervals_in_dimension = + read_persistence_intervals_in_dimension("persistence_intervals_with_field.pers"); + + std::cout << "\nread_persistence_intervals_in_dimension - expected\n"; + for (auto vec_iter : expected_intervals_in_dimension) + std::cout << " [" << vec_iter.first << " ," << vec_iter.second << "] "; + + std::cout << "\nread_persistence_intervals_in_dimension - read\n"; + for (auto vec_iter : persistence_intervals_in_dimension) + std::cout << " [" << vec_iter.first << " ," << vec_iter.second << "] "; + + BOOST_CHECK(persistence_intervals_in_dimension == expected_intervals_in_dimension); + + expected_intervals_in_dimension.clear(); + expected_intervals_in_dimension.push_back(std::make_pair(2.7, 3.7)); + persistence_intervals_in_dimension = + read_persistence_intervals_in_dimension("persistence_intervals_with_field.pers", 0); + + std::cout << "\nread_persistence_intervals_in_dimension 0 - expected\n"; + for (auto vec_iter : expected_intervals_in_dimension) + std::cout << " [" << vec_iter.first << " ," << vec_iter.second << "] "; + + std::cout << "\nread_persistence_intervals_in_dimension 0 - read\n"; + for (auto vec_iter : persistence_intervals_in_dimension) + std::cout << " [" << vec_iter.first << " ," << vec_iter.second << "] "; + + BOOST_CHECK(persistence_intervals_in_dimension == expected_intervals_in_dimension); + + expected_intervals_in_dimension.clear(); + expected_intervals_in_dimension.push_back(std::make_pair(9.6, 14.)); + expected_intervals_in_dimension.push_back(std::make_pair(3., std::numeric_limits::infinity())); + persistence_intervals_in_dimension = + read_persistence_intervals_in_dimension("persistence_intervals_with_field.pers", 1); + + std::cout << "\nread_persistence_intervals_in_dimension 1 - expected\n"; + for (auto vec_iter : expected_intervals_in_dimension) + std::cout << " [" << vec_iter.first << " ," << vec_iter.second << "] "; + + std::cout << "\nread_persistence_intervals_in_dimension 1 - read\n"; + for (auto vec_iter : persistence_intervals_in_dimension) + std::cout << " [" << vec_iter.first << " ," << vec_iter.second << "] "; + + BOOST_CHECK(persistence_intervals_in_dimension == expected_intervals_in_dimension); + + expected_intervals_in_dimension.clear(); + persistence_intervals_in_dimension = + read_persistence_intervals_in_dimension("persistence_intervals_with_field.pers", 2); + + std::cout << "\nread_persistence_intervals_in_dimension 2 - expected\n"; + for (auto vec_iter : expected_intervals_in_dimension) + std::cout << " [" << vec_iter.first << " ," << vec_iter.second << "] "; + + std::cout << "\nread_persistence_intervals_in_dimension 2 - read\n"; + for (auto vec_iter : persistence_intervals_in_dimension) + std::cout << " [" << vec_iter.first << " ," << vec_iter.second << "] "; + + BOOST_CHECK(persistence_intervals_in_dimension == expected_intervals_in_dimension); + + expected_intervals_in_dimension.clear(); + expected_intervals_in_dimension.push_back(std::make_pair(34.2, 34.974)); + persistence_intervals_in_dimension = + read_persistence_intervals_in_dimension("persistence_intervals_with_field.pers", 3); + + std::cout << "\nread_persistence_intervals_in_dimension 3 - expected\n"; + for (auto vec_iter : expected_intervals_in_dimension) + std::cout << " [" << vec_iter.first << " ," << vec_iter.second << "] "; + + std::cout << "\nread_persistence_intervals_in_dimension 3 - read\n"; + for (auto vec_iter : persistence_intervals_in_dimension) + std::cout << " [" << vec_iter.first << " ," << vec_iter.second << "] "; + + BOOST_CHECK(persistence_intervals_in_dimension == expected_intervals_in_dimension); + +} -- cgit v1.2.3