From 1a728400187d89db914e865a979c85c7260b0b02 Mon Sep 17 00:00:00 2001 From: vrouvrea Date: Mon, 21 Nov 2016 16:24:15 +0000 Subject: Add distance matrix rips construction (doc, code, test) git-svn-id: svn+ssh://scm.gforge.inria.fr/svnroot/gudhi/branches/distance_matrix_in_rips_module@1766 636b058d-ea47-450e-bf9e-a15bfbe3eedb Former-commit-id: 6b9bdecbaf0aca12facb023e66bdea71e8e8cd38 --- src/Rips_complex/example/CMakeLists.txt | 23 +++++++ ...mple_one_skeleton_rips_from_distance_matrix.cpp | 68 ++++++++++++++++++++ .../example_one_skeleton_rips_from_points.cpp | 2 - ..._rips_complex_from_csv_distance_matrix_file.cpp | 72 ++++++++++++++++++++++ .../full_skeleton_rips_distance_for_doc.txt | 32 ++++++++++ .../example/one_skeleton_rips_distance_for_doc.txt | 17 +++++ 6 files changed, 212 insertions(+), 2 deletions(-) create mode 100644 src/Rips_complex/example/example_one_skeleton_rips_from_distance_matrix.cpp create mode 100644 src/Rips_complex/example/example_rips_complex_from_csv_distance_matrix_file.cpp create mode 100644 src/Rips_complex/example/full_skeleton_rips_distance_for_doc.txt create mode 100644 src/Rips_complex/example/one_skeleton_rips_distance_for_doc.txt (limited to 'src/Rips_complex/example') diff --git a/src/Rips_complex/example/CMakeLists.txt b/src/Rips_complex/example/CMakeLists.txt index 3962b15e..9ba9d941 100644 --- a/src/Rips_complex/example/CMakeLists.txt +++ b/src/Rips_complex/example/CMakeLists.txt @@ -1,27 +1,50 @@ cmake_minimum_required(VERSION 2.6) project(Rips_complex_examples) +# Point cloud add_executable ( ripsoffreader example_rips_complex_from_off_file.cpp ) target_link_libraries(ripsoffreader ${Boost_SYSTEM_LIBRARY}) add_executable ( oneskeletonripspoints example_one_skeleton_rips_from_points.cpp ) target_link_libraries(oneskeletonripspoints ${Boost_SYSTEM_LIBRARY}) +# Distance matrix +add_executable ( oneskeletonripsdistance example_one_skeleton_rips_from_distance_matrix.cpp ) +target_link_libraries(oneskeletonripsdistance ${Boost_SYSTEM_LIBRARY}) + +add_executable ( ripscsvdistancereader example_rips_complex_from_csv_distance_matrix_file.cpp ) +target_link_libraries(ripscsvdistancereader ${Boost_SYSTEM_LIBRARY}) + if (TBB_FOUND) target_link_libraries(ripsoffreader ${TBB_LIBRARIES}) target_link_libraries(oneskeletonripspoints ${TBB_LIBRARIES}) + target_link_libraries(oneskeletonripsdistance ${TBB_LIBRARIES}) + target_link_libraries(ripscsvdistancereader ${TBB_LIBRARIES}) endif() add_test(oneskeletonripspoints ${CMAKE_CURRENT_BINARY_DIR}/oneskeletonripspoints 12.0) + +add_test(oneskeletonripsdistance ${CMAKE_CURRENT_BINARY_DIR}/oneskeletonripspoints 1.0) + # 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}/) add_test(ripsoffreader_doc_12_1 ${CMAKE_CURRENT_BINARY_DIR}/ripsoffreader alphacomplexdoc.off 12.0 1 ${CMAKE_CURRENT_BINARY_DIR}/ripsoffreader_result_12_1.txt) add_test(ripsoffreader_doc_12_3 ${CMAKE_CURRENT_BINARY_DIR}/ripsoffreader alphacomplexdoc.off 12.0 3 ${CMAKE_CURRENT_BINARY_DIR}/ripsoffreader_result_12_3.txt) + +file(COPY "${CMAKE_SOURCE_DIR}/data/distance_matrix/full_square_distance_matrix.csv" DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/) +add_test(ripscsvdistancereader_doc_1_1 ${CMAKE_CURRENT_BINARY_DIR}/ripscsvdistancereader full_square_distance_matrix.csv 1.0 1 ${CMAKE_CURRENT_BINARY_DIR}/ripscsvreader_result_1_1.txt) +add_test(ripscsvdistancereader_doc_1_3 ${CMAKE_CURRENT_BINARY_DIR}/ripscsvdistancereader full_square_distance_matrix.csv 1.0 3 ${CMAKE_CURRENT_BINARY_DIR}/ripscsvreader_result_1_3.txt) + + if (DIFF_PATH) # Do not forget to copy test results files in current binary dir file(COPY "one_skeleton_rips_points_for_doc.txt" DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/) file(COPY "full_skeleton_rips_points_for_doc.txt" DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/) + file(COPY "one_skeleton_rips_distance_for_doc.txt" DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/) + file(COPY "full_skeleton_rips_distance_for_doc.txt" DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/) add_test(ripsoffreader_doc_12_1_diff_files ${DIFF_PATH} ${CMAKE_CURRENT_BINARY_DIR}/ripsoffreader_result_12_1.txt ${CMAKE_CURRENT_BINARY_DIR}/one_skeleton_rips_points_for_doc.txt) add_test(ripsoffreader_doc_12_3_diff_files ${DIFF_PATH} ${CMAKE_CURRENT_BINARY_DIR}/ripsoffreader_result_12_3.txt ${CMAKE_CURRENT_BINARY_DIR}/full_skeleton_rips_points_for_doc.txt) + add_test(ripscsvreader_doc_1_1_diff_files ${DIFF_PATH} ${CMAKE_CURRENT_BINARY_DIR}/ripscsvreader_result_1_1.txt ${CMAKE_CURRENT_BINARY_DIR}/one_skeleton_rips_distance_for_doc.txt) + add_test(ripscsvreader_doc_1_3_diff_files ${DIFF_PATH} ${CMAKE_CURRENT_BINARY_DIR}/ripscsvreader_result_1_3.txt ${CMAKE_CURRENT_BINARY_DIR}/full_skeleton_rips_distance_for_doc.txt) endif() diff --git a/src/Rips_complex/example/example_one_skeleton_rips_from_distance_matrix.cpp b/src/Rips_complex/example/example_one_skeleton_rips_from_distance_matrix.cpp new file mode 100644 index 00000000..54fa3aa1 --- /dev/null +++ b/src/Rips_complex/example/example_one_skeleton_rips_from_distance_matrix.cpp @@ -0,0 +1,68 @@ +#include +// to construct a simplex_tree from rips complex +#include +#include + +#include +#include +#include // for std::numeric_limits + +void usage(int nbArgs, char * const progName) { + std::cerr << "Error: Number of arguments (" << nbArgs << ") is not correct\n"; + std::cerr << "Usage: " << progName << " threshold\n"; + std::cerr << " i.e.: " << progName << " 12.0\n"; + exit(-1); // ----- >> +} + +int main(int argc, char **argv) { + if (argc != 2) usage(argc, argv[0]); + + double threshold = atof(argv[1]); + + // Type definitions + using Simplex_tree = Gudhi::Simplex_tree; + using Filtration_value = Simplex_tree::Filtration_value; + using Rips_complex = Gudhi::rips_complex::Rips_complex; + using Distance_matrix = std::vector>; + + // User defined distance matrix is: + // | 0 0.94 0.77 0.99 0.11 | + // | 0.94 0 0.26 0.99 0.39 | + // | 0.77 0.26 0 0.28 0.97 | + // | 0.99 0.99 0.28 0 0.30 | + // | 0.11 0.39 0.97 0.30 0 | + + Distance_matrix distances; + distances.push_back({}); + distances.push_back({0.94}); + distances.push_back({0.77, 0.26}); + distances.push_back({0.99, 0.99, 0.28}); + distances.push_back({0.11, 0.39, 0.97, 0.30}); + + // ---------------------------------------------------------------------------- + // Init of a rips complex from points + // ---------------------------------------------------------------------------- + Rips_complex rips_complex_from_points(distances, threshold); + + Simplex_tree simplex; + if (rips_complex_from_points.create_complex(simplex, 1)) { + // ---------------------------------------------------------------------------- + // Display information about the one skeleton rips complex + // ---------------------------------------------------------------------------- + std::cout << "Rips complex is of dimension " << simplex.dimension() << + " - " << simplex.num_simplices() << " simplices - " << + simplex.num_vertices() << " vertices." << std::endl; + + std::cout << "Iterator on rips complex simplices in the filtration order, with [filtration value]:" << + std::endl; + for (auto f_simplex : simplex.filtration_simplex_range()) { + std::cout << " ( "; + for (auto vertex : simplex.simplex_vertex_range(f_simplex)) { + std::cout << vertex << " "; + } + std::cout << ") -> " << "[" << simplex.filtration(f_simplex) << "] "; + std::cout << std::endl; + } + } + return 0; +} diff --git a/src/Rips_complex/example/example_one_skeleton_rips_from_points.cpp b/src/Rips_complex/example/example_one_skeleton_rips_from_points.cpp index 2e63d9a6..26517876 100644 --- a/src/Rips_complex/example/example_one_skeleton_rips_from_points.cpp +++ b/src/Rips_complex/example/example_one_skeleton_rips_from_points.cpp @@ -1,6 +1,4 @@ #include -// to construct Rips_complex from a OFF file of points -#include // to construct a simplex_tree from rips complex #include #include diff --git a/src/Rips_complex/example/example_rips_complex_from_csv_distance_matrix_file.cpp b/src/Rips_complex/example/example_rips_complex_from_csv_distance_matrix_file.cpp new file mode 100644 index 00000000..cfada84a --- /dev/null +++ b/src/Rips_complex/example/example_rips_complex_from_csv_distance_matrix_file.cpp @@ -0,0 +1,72 @@ +#include +// to construct Rips_complex from a OFF file of points +#include +// to construct a simplex_tree from rips complex +#include +#include + +#include +#include + +void usage(int nbArgs, char * const progName) { + std::cerr << "Error: Number of arguments (" << nbArgs << ") is not correct\n"; + std::cerr << "Usage: " << progName << " filename.csv threshold dim_max [ouput_file.txt]\n"; + std::cerr << " i.e.: " << progName << " ../../data/distance_matrix/full_square_distance_matrix.csv 1.0 3\n"; + exit(-1); // ----- >> +} + +int main(int argc, char **argv) { + if ((argc != 4) && (argc != 5)) usage(argc, (argv[0] - 1)); + + std::string csv_file_name(argv[1]); + double threshold = atof(argv[2]); + int dim_max = atoi(argv[3]); + + // Type definitions + using Simplex_tree = Gudhi::Simplex_tree<>; + using Filtration_value = Simplex_tree::Filtration_value; + using Rips_complex = Gudhi::rips_complex::Rips_complex; + using Distance_matrix = std::vector>; + + // ---------------------------------------------------------------------------- + // Init of a rips complex from a distance matrix in a csv file + // Default separator is ';' + // ---------------------------------------------------------------------------- + Distance_matrix distances = read_lower_triangular_matrix_from_csv_file(csv_file_name); + Rips_complex rips_complex_from_file(distances, threshold); + + std::streambuf* streambufffer; + std::ofstream ouput_file_stream; + + if (argc == 5) { + ouput_file_stream.open(std::string(argv[4])); + streambufffer = ouput_file_stream.rdbuf(); + } else { + streambufffer = std::cout.rdbuf(); + } + + Simplex_tree simplex; + if (rips_complex_from_file.create_complex(simplex, dim_max)) { + std::ostream output_stream(streambufffer); + + // ---------------------------------------------------------------------------- + // Display information about the rips complex + // ---------------------------------------------------------------------------- + output_stream << "Rips complex is of dimension " << simplex.dimension() << + " - " << simplex.num_simplices() << " simplices - " << + simplex.num_vertices() << " vertices." << std::endl; + + output_stream << "Iterator on rips complex simplices in the filtration order, with [filtration value]:" << + std::endl; + for (auto f_simplex : simplex.filtration_simplex_range()) { + output_stream << " ( "; + for (auto vertex : simplex.simplex_vertex_range(f_simplex)) { + output_stream << vertex << " "; + } + output_stream << ") -> " << "[" << simplex.filtration(f_simplex) << "] "; + output_stream << std::endl; + } + } + ouput_file_stream.close(); + return 0; +} diff --git a/src/Rips_complex/example/full_skeleton_rips_distance_for_doc.txt b/src/Rips_complex/example/full_skeleton_rips_distance_for_doc.txt new file mode 100644 index 00000000..6744532d --- /dev/null +++ b/src/Rips_complex/example/full_skeleton_rips_distance_for_doc.txt @@ -0,0 +1,32 @@ +Rips complex is of dimension 3 - 30 simplices - 5 vertices. +Iterator on rips complex simplices in the filtration order, with [filtration value]: + ( 0 ) -> [0] + ( 1 ) -> [0] + ( 2 ) -> [0] + ( 3 ) -> [0] + ( 4 ) -> [0] + ( 4 0 ) -> [0.11] + ( 2 1 ) -> [0.26] + ( 3 2 ) -> [0.28] + ( 4 3 ) -> [0.3] + ( 4 1 ) -> [0.39] + ( 2 0 ) -> [0.77] + ( 1 0 ) -> [0.94] + ( 2 1 0 ) -> [0.94] + ( 4 1 0 ) -> [0.94] + ( 4 2 ) -> [0.97] + ( 4 2 0 ) -> [0.97] + ( 4 2 1 ) -> [0.97] + ( 4 2 1 0 ) -> [0.97] + ( 4 3 2 ) -> [0.97] + ( 3 0 ) -> [0.99] + ( 3 1 ) -> [0.99] + ( 3 1 0 ) -> [0.99] + ( 3 2 0 ) -> [0.99] + ( 3 2 1 ) -> [0.99] + ( 3 2 1 0 ) -> [0.99] + ( 4 3 0 ) -> [0.99] + ( 4 3 1 ) -> [0.99] + ( 4 3 1 0 ) -> [0.99] + ( 4 3 2 0 ) -> [0.99] + ( 4 3 2 1 ) -> [0.99] diff --git a/src/Rips_complex/example/one_skeleton_rips_distance_for_doc.txt b/src/Rips_complex/example/one_skeleton_rips_distance_for_doc.txt new file mode 100644 index 00000000..9bb1a7ab --- /dev/null +++ b/src/Rips_complex/example/one_skeleton_rips_distance_for_doc.txt @@ -0,0 +1,17 @@ +Rips complex is of dimension 1 - 15 simplices - 5 vertices. +Iterator on rips complex simplices in the filtration order, with [filtration value]: + ( 0 ) -> [0] + ( 1 ) -> [0] + ( 2 ) -> [0] + ( 3 ) -> [0] + ( 4 ) -> [0] + ( 4 0 ) -> [0.11] + ( 2 1 ) -> [0.26] + ( 3 2 ) -> [0.28] + ( 4 3 ) -> [0.3] + ( 4 1 ) -> [0.39] + ( 2 0 ) -> [0.77] + ( 1 0 ) -> [0.94] + ( 4 2 ) -> [0.97] + ( 3 0 ) -> [0.99] + ( 3 1 ) -> [0.99] -- cgit v1.2.3