From 8802dc5353020c75012f6ae8c26afefb4c78b161 Mon Sep 17 00:00:00 2001 From: vrouvrea Date: Fri, 6 Feb 2015 14:25:38 +0000 Subject: Add of persistence computation from a simple simplex tree - Fix of infinite persistent (1.#INF under windows) UT git-svn-id: svn+ssh://scm.gforge.inria.fr/svnroot/gudhi/trunk@462 636b058d-ea47-450e-bf9e-a15bfbe3eedb Former-commit-id: f08233c68f60962fa6caa5603b3a8b4a16b2bb76 --- src/Persistent_cohomology/example/CMakeLists.txt | 3 + .../persistence_from_simple_simplex_tree.cpp | 209 +++++++++++++++++++++ .../include/gudhi/Persistent_cohomology.h | 10 +- src/Persistent_cohomology/test/CMakeLists.txt | 2 +- src/Persistent_cohomology/test/README | 11 +- .../test/persistent_cohomology_unit_test.cpp | 5 +- ...persistent_cohomology_unit_test_multi_field.cpp | 2 +- 7 files changed, 236 insertions(+), 6 deletions(-) create mode 100644 src/Persistent_cohomology/example/persistence_from_simple_simplex_tree.cpp diff --git a/src/Persistent_cohomology/example/CMakeLists.txt b/src/Persistent_cohomology/example/CMakeLists.txt index 842f852a..3697a419 100644 --- a/src/Persistent_cohomology/example/CMakeLists.txt +++ b/src/Persistent_cohomology/example/CMakeLists.txt @@ -3,6 +3,9 @@ project(GUDHIExPersCohom) # problem with Visual Studio link on Boost program_options if (NOT MSVC) + add_executable(persistence_from_simple_simplex_tree persistence_from_simple_simplex_tree.cpp) + target_link_libraries(persistence_from_simple_simplex_tree ${Boost_SYSTEM_LIBRARY}) + add_executable(rips_persistence rips_persistence.cpp) target_link_libraries(rips_persistence ${Boost_SYSTEM_LIBRARY} ${Boost_PROGRAM_OPTIONS_LIBRARY}) diff --git a/src/Persistent_cohomology/example/persistence_from_simple_simplex_tree.cpp b/src/Persistent_cohomology/example/persistence_from_simple_simplex_tree.cpp new file mode 100644 index 00000000..ba82e4e6 --- /dev/null +++ b/src/Persistent_cohomology/example/persistence_from_simple_simplex_tree.cpp @@ -0,0 +1,209 @@ +/* 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) 2014 INRIA Sophia Antipolis-Méditerranée (France) + * + * 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 "gudhi/graph_simplicial_complex.h" +#include "gudhi/Simplex_tree.h" +#include "gudhi/Persistent_cohomology.h" + +using namespace Gudhi; +using namespace Gudhi::persistent_cohomology; + +typedef std::vector< Vertex_handle > typeVectorVertex; +typedef std::pair typeSimplex; +typedef std::pair< Simplex_tree<>::Simplex_handle, bool > typePairSimplexBool; +typedef Simplex_tree<> typeST; + +void usage(char * const progName) +{ + std::cerr << "Usage: " << progName << " coeff_field_characteristic[integer > 0] min_persistence[float >= -1.0]\n"; + exit(-1); // ----- >> +} + +int main (int argc, char * const argv[]) +{ + int coeff_field_characteristic=0; + int returnedScanValue = sscanf(argv[1], "%d", &coeff_field_characteristic); + if ((returnedScanValue == EOF) || (coeff_field_characteristic <= 0)) { + std::cerr << "Error: " << argv[1] << " is not correct\n"; + usage(argv[0]); + } + + Filtration_value min_persistence = 0.0; + returnedScanValue = sscanf(argv[2], "%lf", &min_persistence); + if ((returnedScanValue == EOF) || (min_persistence < -1.0)) { + std::cerr << "Error: " << argv[2] << " is not correct\n"; + usage(argv[0]); + } + + // program args management + if (argc != 3) { + std::cerr << "Error: Number of arguments (" << argc << ") is not correct\n"; + usage(argv[0]); + } + + // TEST OF INSERTION + std::cout << "********************************************************************" << std::endl; + std::cout << "TEST OF INSERTION" << std::endl; + typeST st; + + // ++ FIRST + std::cout << " - INSERT (2,1,0)" << std::endl; + typeVectorVertex SimplexVector1; + SimplexVector1.push_back(2); + SimplexVector1.push_back(1); + SimplexVector1.push_back(0); + st.insert_simplex_and_subfaces ( SimplexVector1, 0.3); + + // ++ SECOND + std::cout << " - INSERT 3" << std::endl; + typeVectorVertex SimplexVector2; + SimplexVector2.push_back(3); + st.insert_simplex_and_subfaces ( SimplexVector2, 0.1); + + // ++ THIRD + std::cout << " - INSERT (0,3)" << std::endl; + typeVectorVertex SimplexVector3; + SimplexVector3.push_back(3); + SimplexVector3.push_back(0); + st.insert_simplex_and_subfaces ( SimplexVector3, 0.2); + + // ++ FOURTH + std::cout << " - INSERT (1,0) (already inserted)" << std::endl; + typeVectorVertex SimplexVector4; + SimplexVector4.push_back(1); + SimplexVector4.push_back(0); + st.insert_simplex_and_subfaces ( SimplexVector4, 0.2); + + // ++ FIFTH + std::cout << " - INSERT (3,4,5)" << std::endl; + typeVectorVertex SimplexVector5; + SimplexVector5.push_back(3); + SimplexVector5.push_back(4); + SimplexVector5.push_back(5); + st.insert_simplex_and_subfaces ( SimplexVector5, 0.3); + + // ++ SIXTH + std::cout << " - INSERT (0,1,6,7)" << std::endl; + typeVectorVertex SimplexVector6; + SimplexVector6.push_back(0); + SimplexVector6.push_back(1); + SimplexVector6.push_back(6); + SimplexVector6.push_back(7); + st.insert_simplex_and_subfaces ( SimplexVector6, 0.4); + + // ++ SEVENTH + std::cout << " - INSERT (4,5,8,9)" << std::endl; + typeVectorVertex SimplexVector7; + SimplexVector7.push_back(4); + SimplexVector7.push_back(5); + SimplexVector7.push_back(8); + SimplexVector7.push_back(9); + st.insert_simplex_and_subfaces ( SimplexVector7, 0.4); + + // ++ EIGHTH + std::cout << " - INSERT (9,10,11)" << std::endl; + typeVectorVertex SimplexVector8; + SimplexVector8.push_back(9); + SimplexVector8.push_back(10); + SimplexVector8.push_back(11); + st.insert_simplex_and_subfaces ( SimplexVector8, 0.3); + + // ++ NINETH + std::cout << " - INSERT (2,10,12)" << std::endl; + typeVectorVertex SimplexVector9; + SimplexVector9.push_back(2); + SimplexVector9.push_back(10); + SimplexVector9.push_back(12); + st.insert_simplex_and_subfaces ( SimplexVector9, 0.3); + + // ++ TENTH + std::cout << " - INSERT (11,6)" << std::endl; + typeVectorVertex SimplexVector10; + SimplexVector10.push_back(11); + SimplexVector10.push_back(6); + st.insert_simplex_and_subfaces ( SimplexVector10, 0.2); + + // ++ ELEVENTH + std::cout << " - INSERT (13,14,15)" << std::endl; + typeVectorVertex SimplexVector11; + SimplexVector11.push_back(13); + SimplexVector11.push_back(14); + SimplexVector11.push_back(15); + st.insert_simplex_and_subfaces ( SimplexVector11, 0.25); + + /* Inserted simplex: */ + /* 1 6 */ + /* o---o */ + /* /X\7/ 4 2 */ + /* o---o---o---o o */ + /* 2 0 3\X/8\ 10 /X\ */ + /* o---o---o---o */ + /* 5 9\X/ 12 */ + /* o---o */ + /* 11 6 */ + /* In other words: */ + /* A facet [2,1,0] */ + /* An edge [0,3] */ + /* A facet [3,4,5] */ + /* A cell [0,1,6,7] */ + /* A cell [4,5,8,9] */ + /* A facet [9,10,11] */ + /* An edge [11,6] */ + /* An edge [10,12,2] */ + + st.set_dimension(2); + st.set_filtration(0.4); + + std::cout << "The complex contains " << st.num_simplices() << " simplices - " << st.num_vertices() << " vertices " << std::endl; + std::cout << " - dimension " << st.dimension() << " - filtration " << st.filtration() << std::endl; + std::cout << std::endl << std::endl << "Iterator on Simplices in the filtration, with [filtration value]:" << std::endl; + std::cout << "**************************************************************" << std::endl; + std::cout << "strict graph G { " << std::endl; + + for( auto f_simplex : st.filtration_simplex_range() ) + { + std::cout << " " << "[" << st.filtration(f_simplex) << "] "; + for( auto vertex : st.simplex_vertex_range(f_simplex) ) + { + std::cout << (int)vertex << " -- "; + } + std::cout << ";" << std::endl; + } + + std::cout << "}" << std::endl; + //std::cout << "**************************************************************" << std::endl; + //st.print_hasse(std::cout); + std::cout << "**************************************************************" << std::endl; + + + // Compute the persistence diagram of the complex + persistent_cohomology::Persistent_cohomology< Simplex_tree<>, Field_Zp > pcoh(st); + pcoh.init_coefficients( coeff_field_characteristic ); //initiliazes the coefficient field for homology + + pcoh.compute_persistent_cohomology( min_persistence ); + + // Output the diagram in filediag + pcoh.output_diagram(); + return 0; +} diff --git a/src/Persistent_cohomology/include/gudhi/Persistent_cohomology.h b/src/Persistent_cohomology/include/gudhi/Persistent_cohomology.h index 0f3e689b..c42e4be4 100644 --- a/src/Persistent_cohomology/include/gudhi/Persistent_cohomology.h +++ b/src/Persistent_cohomology/include/gudhi/Persistent_cohomology.h @@ -696,12 +696,20 @@ class Persistent_cohomology { * feature exists in homology with Z/piZ coefficients. */ void output_diagram(std::ostream& ostream = std::cout) { + cmp_intervals_by_length cmp(cpx_); persistent_pairs_.sort(cmp); + bool has_infinity = std::numeric_limits::has_infinity; for (auto pair : persistent_pairs_) { - ostream << get<2>(pair) << " " << cpx_->dimension(get<0>(pair)) << " " + // Special case on windows, inf is "1.#INF" (cf. unitary tests and R package TDA) + if (has_infinity && cpx_->filtration(get<1>(pair)) == std::numeric_limits::infinity()) { + ostream << get<2>(pair) << " " << cpx_->dimension(get<0>(pair)) << " " + << cpx_->filtration(get<0>(pair)) << " inf " << std::endl; + } else { + ostream << get<2>(pair) << " " << cpx_->dimension(get<0>(pair)) << " " << cpx_->filtration(get<0>(pair)) << " " << cpx_->filtration(get<1>(pair)) << " " << std::endl; + } } } diff --git a/src/Persistent_cohomology/test/CMakeLists.txt b/src/Persistent_cohomology/test/CMakeLists.txt index 5c811bfa..9dc19251 100644 --- a/src/Persistent_cohomology/test/CMakeLists.txt +++ b/src/Persistent_cohomology/test/CMakeLists.txt @@ -18,7 +18,7 @@ if(GMPXX_FOUND AND GMP_FOUND) target_link_libraries(persistent_cohomology_unit_test_multi_field ${Boost_SYSTEM_LIBRARY} ${Boost_UNIT_TEST_FRAMEWORK_LIBRARY} ${GMPXX_LIBRARIES} ${GMP_LIBRARIES}) # Unitary tests - add_test(persistent_cohomology_unit_test_multi_field ${CMAKE_CURRENT_BINARY_DIR}/persistent_cohomology_unit_test_multi_field ${CMAKE_SOURCE_DIR}/src/Persistent_cohomology/test/simplex_tree_file_for_unit_test_multi_field.txt) + add_test(persistent_cohomology_unit_test_multi_field ${CMAKE_CURRENT_BINARY_DIR}/persistent_cohomology_unit_test_multi_field ${CMAKE_SOURCE_DIR}/src/Persistent_cohomology/test/simplex_tree_file_for_multi_field_unit_test.txt) endif() if (LCOV_PATH) diff --git a/src/Persistent_cohomology/test/README b/src/Persistent_cohomology/test/README index 56474fa0..ddceac63 100644 --- a/src/Persistent_cohomology/test/README +++ b/src/Persistent_cohomology/test/README @@ -7,6 +7,15 @@ make To launch with details: *********************** -./persistent_cohomology_unit_test --report_level=detailed --log_level=all +SINGLE FIELD +------------ +./persistent_cohomology_unit_test simplex_tree_file_for_unit_test.txt --report_level=detailed --log_level=all ==> echo $? returns 0 in case of success (non-zero otherwise) + +MULTI FIELD +----------- +./persistent_cohomology_unit_test_multi_field simplex_tree_file_for_multi_field_unit_test.txt --report_level=detailed --log_level=all + + ==> echo $? returns 0 in case of success (non-zero otherwise) + diff --git a/src/Persistent_cohomology/test/persistent_cohomology_unit_test.cpp b/src/Persistent_cohomology/test/persistent_cohomology_unit_test.cpp index 2e059b09..1e7a74a7 100644 --- a/src/Persistent_cohomology/test/persistent_cohomology_unit_test.cpp +++ b/src/Persistent_cohomology/test/persistent_cohomology_unit_test.cpp @@ -23,7 +23,7 @@ typedef Simplex_tree<> typeST; std::string test_rips_persistence(int coefficient, int min_persistence) { // Check file name is given as parameter from CMakeLists.txt - BOOST_CHECK(framework::master_test_suite().argc == 2); + BOOST_CHECK(framework::master_test_suite().argc >= 2); const std::string inputFile(framework::master_test_suite().argv[1]); std::ifstream simplex_tree_stream; @@ -84,7 +84,8 @@ void test_rips_persistence_in_dimension(int dimension) { std::cout << "TEST OF RIPS_PERSISTENT_COHOMOLOGY_SINGLE_FIELD DIM=" << dimension << " MIN_PERS=0" << std::endl; std::string str_rips_persistence = test_rips_persistence(dimension, 0); - + std::cout << str_rips_persistence << std::endl; + BOOST_CHECK(str_rips_persistence.find(value0) != std::string::npos); // Check found BOOST_CHECK(str_rips_persistence.find(value1) != std::string::npos); // Check found BOOST_CHECK(str_rips_persistence.find(value2) != std::string::npos); // Check found diff --git a/src/Persistent_cohomology/test/persistent_cohomology_unit_test_multi_field.cpp b/src/Persistent_cohomology/test/persistent_cohomology_unit_test_multi_field.cpp index 196461e2..e88add3a 100644 --- a/src/Persistent_cohomology/test/persistent_cohomology_unit_test_multi_field.cpp +++ b/src/Persistent_cohomology/test/persistent_cohomology_unit_test_multi_field.cpp @@ -24,7 +24,7 @@ typedef Simplex_tree<> typeST; std::string test_rips_persistence(int min_coefficient, int max_coefficient, int min_persistence) { // Check file name is given as parameter from CMakeLists.txt - BOOST_CHECK(framework::master_test_suite().argc == 2); + BOOST_CHECK(framework::master_test_suite().argc >= 2); const std::string inputFile(framework::master_test_suite().argv[1]); std::ifstream simplex_tree_stream; -- cgit v1.2.3