summaryrefslogtreecommitdiff
path: root/src/Persistent_cohomology/test/persistent_cohomology_unit_test_multi_field.cpp
blob: 3602aa09c95b1e59f43a115d4c68afa1cbb1f7f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <iostream>
#include <string>
#include <algorithm>
#include <utility> // std::pair, std::make_pair
#include <cmath> // float comparison
#include <limits>

#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE "persistent_cohomology_multi_field"
#include <boost/test/unit_test.hpp>

#include <gudhi/graph_simplicial_complex.h>
#include <gudhi/reader_utils.h>
#include <gudhi/Simplex_tree.h>
#include <gudhi/Persistent_cohomology.h>
#include <gudhi/Persistent_cohomology/Multi_field.h>

using namespace Gudhi;
using namespace Gudhi::persistent_cohomology;
using namespace boost::unit_test;

typedef Simplex_tree<> typeST;

std::string test_rips_persistence(int min_coefficient, int max_coefficient, double min_persistence) {
  // file is copied in CMakeLists.txt
  std::ifstream simplex_tree_stream;
  simplex_tree_stream.open("simplex_tree_file_for_multi_field_unit_test.txt");
  typeST st;
  simplex_tree_stream >> st;
  simplex_tree_stream.close();

  // Display the Simplex_tree
  std::clog << "The complex contains " << st.num_simplices() << " simplices" << " - dimension= " << st.dimension()
      << std::endl;

  // Check
  BOOST_CHECK(st.num_simplices() == 58);
  BOOST_CHECK(st.dimension() == 3);

  // Sort the simplices in the order of the filtration
  st.initialize_filtration();

  // Compute the persistence diagram of the complex
  Persistent_cohomology<Simplex_tree<>, Multi_field> pcoh(st);

  pcoh.init_coefficients(min_coefficient, max_coefficient); // initializes the coefficient field for homology
  // Check infinite rips
  pcoh.compute_persistent_cohomology(min_persistence); // Minimal lifetime of homology feature to be recorded.

  std::ostringstream ossRips;
  pcoh.output_diagram(ossRips);

  std::string strRips = ossRips.str();
  return strRips;
}

void test_rips_persistence_in_dimension(int min_dimension, int max_dimension) {
  // there are 2 discontinued ensembles 
  std::string value0("  0 0.25 inf");
  std::string value1("  1 0.4 inf");
  // And a big hole - cut in 2 pieces after 0.3
  std::string value2("  0 0.2 0.3");

  // For dim <= 1 =>
  std::string value3("  1 0.25 inf");
  std::string value4("  2 0.25 inf");
  std::string value5("  1 0.3 inf");
  std::string value6("  2 0.3 inf");
  std::string value7("  2 0.4 inf");

  std::clog << "********************************************************************" << std::endl;
  std::clog << "TEST OF RIPS_PERSISTENT_COHOMOLOGY_MULTI_FIELD MIN_DIM=" << min_dimension << " MAX_DIM=" << max_dimension << " MIN_PERS=0" << std::endl;

  std::string str_rips_persistence = test_rips_persistence(min_dimension, max_dimension, 0.0);
  std::clog << "str_rips_persistence=" << 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

  if ((min_dimension < 2) && (max_dimension < 2)) {
    BOOST_CHECK(str_rips_persistence.find(value3) != std::string::npos); // Check found
    BOOST_CHECK(str_rips_persistence.find(value4) != std::string::npos); // Check found
    BOOST_CHECK(str_rips_persistence.find(value5) != std::string::npos); // Check found
    BOOST_CHECK(str_rips_persistence.find(value6) != std::string::npos); // Check found
    BOOST_CHECK(str_rips_persistence.find(value7) != std::string::npos); // Check found
  } else {
    BOOST_CHECK(str_rips_persistence.find(value3) == std::string::npos); // Check not found
    BOOST_CHECK(str_rips_persistence.find(value4) == std::string::npos); // Check not found
    BOOST_CHECK(str_rips_persistence.find(value5) == std::string::npos); // Check not found
    BOOST_CHECK(str_rips_persistence.find(value6) == std::string::npos); // Check not found
    BOOST_CHECK(str_rips_persistence.find(value7) == std::string::npos); // Check not found
  }

}

BOOST_AUTO_TEST_CASE(rips_persistent_cohomology_multi_field_dim_1_2) {
  test_rips_persistence_in_dimension(0, 1);
}

BOOST_AUTO_TEST_CASE(rips_persistent_cohomology_multi_field_dim_2_3) {
  test_rips_persistence_in_dimension(1, 3);
}

BOOST_AUTO_TEST_CASE(rips_persistent_cohomology_multi_field_dim_1_5) {
  test_rips_persistence_in_dimension(1, 5);
}

// TODO(VR): not working from 6
// std::string str_rips_persistence = test_rips_persistence(6, 0);
// TODO(VR): division by zero
// std::string str_rips_persistence = test_rips_persistence(0, 0);
// TODO(VR): is result OK of :
// test_rips_persistence_in_dimension(3, 4);