| 1 |
/* This file is part of the Gudhi Library - https://gudhi.inria.fr/ - which is released under MIT. |
| 2 |
* See file LICENSE or go to https://gudhi.inria.fr/licensing/ for full license details. |
| 3 |
* Author(s): Clément Maria |
| 4 |
* |
| 5 |
* Copyright (C) 2014 Inria |
| 6 |
* |
| 7 |
* Modification(s): |
| 8 |
* - YYYY/MM Author: Description of the modification |
| 9 |
*/ |
| 10 |
|
| 11 |
#include <gudhi/Rips_complex.h> |
| 12 |
#include <gudhi/distance_functions.h> |
| 13 |
#include <gudhi/Simplex_tree.h> |
| 14 |
#include <gudhi/Persistent_cohomology.h> |
| 15 |
#include <gudhi/Persistent_cohomology/Multi_field.h> |
| 16 |
#include <gudhi/Hasse_complex.h> |
| 17 |
#include <gudhi/Points_off_io.h> |
| 18 |
|
| 19 |
#include <chrono> |
| 20 |
#include <string> |
| 21 |
#include <vector> |
| 22 |
|
| 23 |
// Types definition |
| 24 |
using Simplex_tree = Gudhi::Simplex_tree<Gudhi::Simplex_tree_options_fast_persistence>; |
| 25 |
using Filtration_value = Simplex_tree::Filtration_value; |
| 26 |
using Rips_complex = Gudhi::rips_complex::Rips_complex<Filtration_value>; |
| 27 |
using Field_Zp = Gudhi::persistent_cohomology::Field_Zp; |
| 28 |
using Multi_field = Gudhi::persistent_cohomology::Multi_field; |
| 29 |
using Point = std::vector<double>; |
| 30 |
using Points_off_reader = Gudhi::Points_off_reader<Point>; |
| 31 |
|
| 32 |
/* Compute the persistent homology of the complex cpx with coefficients in Z/pZ. */ |
| 33 |
template< typename FilteredComplex> |
| 34 |
void timing_persistence(FilteredComplex & cpx |
| 35 |
, int p); |
| 36 |
|
| 37 |
/* Compute multi-field persistent homology of the complex cpx with coefficients in |
| 38 |
* Z/rZ for all prime number r in [p;q].*/ |
| 39 |
template< typename FilteredComplex> |
| 40 |
void timing_persistence(FilteredComplex & cpx |
| 41 |
, int p |
| 42 |
, int q); |
| 43 |
|
| 44 |
/* Timings for the computation of persistent homology with different |
| 45 |
* representations of a Rips complex and different coefficient fields. The |
| 46 |
* Rips complex is built on a set of 10000 points sampling a Klein bottle embedded |
| 47 |
* in dimension 5. |
| 48 |
* We represent complexes with a simplex tree and |
| 49 |
* with a Hasse diagram. The Hasse diagram represents explicitly all |
| 50 |
* codimension 1 incidence relations in the complex, and hence leads to |
| 51 |
* a faster computation of persistence because boundaries are precomputed. |
| 52 |
* However, the simplex tree may be constructed directly from a point cloud and |
| 53 |
* is more compact. |
| 54 |
* We compute persistent homology with coefficient fields Z/2Z and Z/1223Z. |
| 55 |
* We present also timings for the computation of multi-field persistent |
| 56 |
* homology in all fields Z/rZ for r prime between 2 and 1223. |
| 57 |
*/ |
| 58 |
int main(int argc, char * argv[]) { |
| 59 |
std::chrono::time_point<std::chrono::system_clock> start, end; |
| 60 |
int elapsed_sec; |
| 61 |
{ |
| 62 |
|
| 63 |
std::string off_file_points = "Kl.off"; |
| 64 |
Filtration_value threshold = 0.27; |
| 65 |
int dim_max = 3; |
| 66 |
int p = 2; |
| 67 |
int q = 1223; |
| 68 |
|
| 69 |
// Extract the points from the file off_file_points |
| 70 |
Points_off_reader off_reader(off_file_points); |
| 71 |
|
| 72 |
// Compute the proximity graph of the points |
| 73 |
start = std::chrono::system_clock::now(); |
| 74 |
Rips_complex rips_complex_from_file(off_reader.get_point_cloud(), threshold, Gudhi::Euclidean_distance()); |
| 75 |
end = std::chrono::system_clock::now(); |
| 76 |
elapsed_sec = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count(); |
| 77 |
std::clog << "Compute Rips graph in " << elapsed_sec << " ms.\n"; |
| 78 |
|
| 79 |
// Construct the Rips complex in a Simplex Tree |
| 80 |
Simplex_tree st; |
| 81 |
start = std::chrono::system_clock::now(); |
| 82 |
|
| 83 |
// insert the proximity graph in the simplex tree |
| 84 |
// expand the graph until dimension dim_max |
| 85 |
rips_complex_from_file.create_complex(st, dim_max); |
| 86 |
|
| 87 |
end = std::chrono::system_clock::now(); |
| 88 |
elapsed_sec = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count(); |
| 89 |
std::clog << "Compute Rips complex in " << elapsed_sec << " ms.\n"; |
| 90 |
std::clog << " - dimension = " << st.dimension() << std::endl; |
| 91 |
std::clog << " - number of simplices = " << st.num_simplices() << std::endl; |
| 92 |
|
| 93 |
// Sort the simplices in the order of the filtration |
| 94 |
start = std::chrono::system_clock::now(); |
| 95 |
st.initialize_filtration(); |
| 96 |
end = std::chrono::system_clock::now(); |
| 97 |
elapsed_sec = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count(); |
| 98 |
std::clog << "Order the simplices of the filtration in " << elapsed_sec << " ms.\n"; |
| 99 |
|
| 100 |
// Copy the keys inside the simplices |
| 101 |
start = std::chrono::system_clock::now(); |
| 102 |
{ |
| 103 |
int count = 0; |
| 104 |
for (auto sh : st.filtration_simplex_range()) |
| 105 |
st.assign_key(sh, count++); |
| 106 |
} |
| 107 |
end = std::chrono::system_clock::now(); |
| 108 |
elapsed_sec = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count(); |
| 109 |
std::clog << "Copied the keys inside the simplices in " << elapsed_sec << " ms.\n"; |
| 110 |
|
| 111 |
// Convert the simplex tree into a hasse diagram |
| 112 |
start = std::chrono::system_clock::now(); |
| 113 |
Gudhi::Hasse_complex<> hcpx(st); |
| 114 |
end = std::chrono::system_clock::now(); |
| 115 |
elapsed_sec = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count(); |
| 116 |
std::clog << "Convert the simplex tree into a Hasse diagram in " << elapsed_sec << " ms.\n"; |
| 117 |
|
| 118 |
|
| 119 |
std::clog << "Timings when using a simplex tree: \n"; |
| 120 |
timing_persistence(st, p); |
| 121 |
timing_persistence(st, q); |
| 122 |
timing_persistence(st, p, q); |
| 123 |
|
| 124 |
std::clog << "Timings when using a Hasse complex: \n"; |
| 125 |
timing_persistence(hcpx, p); |
| 126 |
timing_persistence(hcpx, q); |
| 127 |
timing_persistence(hcpx, p, q); |
| 128 |
|
| 129 |
start = std::chrono::system_clock::now(); |
| 130 |
} |
| 131 |
end = std::chrono::system_clock::now(); |
| 132 |
elapsed_sec = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count(); |
| 133 |
std::clog << "Running the complex destructors in " << elapsed_sec << " ms.\n"; |
| 134 |
return 0; |
| 135 |
} |
| 136 |
|
| 137 |
template< typename FilteredComplex> |
| 138 |
void |
| 139 |
timing_persistence(FilteredComplex & cpx |
| 140 |
, int p) { |
| 141 |
std::chrono::time_point<std::chrono::system_clock> start, end; |
| 142 |
int elapsed_sec; |
| 143 |
{ |
| 144 |
start = std::chrono::system_clock::now(); |
| 145 |
Gudhi::persistent_cohomology::Persistent_cohomology< FilteredComplex, Field_Zp > pcoh(cpx); |
| 146 |
end = std::chrono::system_clock::now(); |
| 147 |
elapsed_sec = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count(); |
| 148 |
std::clog << " Initialize pcoh in " << elapsed_sec << " ms.\n"; |
| 149 |
// initializes the coefficient field for homology |
| 150 |
start = std::chrono::system_clock::now(); |
| 151 |
pcoh.init_coefficients(p); |
| 152 |
end = std::chrono::system_clock::now(); |
| 153 |
elapsed_sec = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count(); |
| 154 |
std::clog << " Initialize the coefficient field in " << elapsed_sec << " ms.\n"; |
| 155 |
|
| 156 |
start = std::chrono::system_clock::now(); |
| 157 |
|
| 158 |
pcoh.compute_persistent_cohomology(INFINITY); |
| 159 |
|
| 160 |
end = std::chrono::system_clock::now(); |
| 161 |
elapsed_sec = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count(); |
| 162 |
std::clog << " Compute persistent homology in Z/" << p << "Z in " << elapsed_sec << " ms.\n"; |
| 163 |
start = std::chrono::system_clock::now(); |
| 164 |
} |
| 165 |
end = std::chrono::system_clock::now(); |
| 166 |
elapsed_sec = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count(); |
| 167 |
std::clog << " Run the persistence destructors in " << elapsed_sec << " ms.\n"; |
| 168 |
} |
| 169 |
|
| 170 |
template< typename FilteredComplex> |
| 171 |
void |
| 172 |
timing_persistence(FilteredComplex & cpx |
| 173 |
, int p |
| 174 |
, int q) { |
| 175 |
std::chrono::time_point<std::chrono::system_clock> start, end; |
| 176 |
int elapsed_sec; |
| 177 |
{ |
| 178 |
start = std::chrono::system_clock::now(); |
| 179 |
Gudhi::persistent_cohomology::Persistent_cohomology< FilteredComplex, Multi_field > pcoh(cpx); |
| 180 |
end = std::chrono::system_clock::now(); |
| 181 |
elapsed_sec = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count(); |
| 182 |
std::clog << " Initialize pcoh in " << elapsed_sec << " ms.\n"; |
| 183 |
// initializes the coefficient field for homology |
| 184 |
start = std::chrono::system_clock::now(); |
| 185 |
pcoh.init_coefficients(p, q); |
| 186 |
end = std::chrono::system_clock::now(); |
| 187 |
elapsed_sec = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count(); |
| 188 |
std::clog << " Initialize the coefficient field in " << elapsed_sec << " ms.\n"; |
| 189 |
// compute persistent homology, disgarding persistent features of life shorter than min_persistence |
| 190 |
|
| 191 |
start = std::chrono::system_clock::now(); |
| 192 |
|
| 193 |
pcoh.compute_persistent_cohomology(INFINITY); |
| 194 |
|
| 195 |
end = std::chrono::system_clock::now(); |
| 196 |
elapsed_sec = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count(); |
| 197 |
std::clog << " Compute multi-field persistent homology in all coefficient fields Z/pZ " |
| 198 |
<< "with p in [" << p << ";" << q << "] in " << elapsed_sec << " ms.\n"; |
| 199 |
start = std::chrono::system_clock::now(); |
| 200 |
} |
| 201 |
end = std::chrono::system_clock::now(); |
| 202 |
elapsed_sec = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count(); |
| 203 |
std::clog << " Run the persistence destructors in " << elapsed_sec << " ms.\n"; |
| 204 |
} |