Debian Codegraph @

src/Persistent_cohomology/example/rips_multifield_persistence.cpp

raw
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/Points_off_io.h>
17
18 #include <boost/program_options.hpp>
19
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 Multi_field = Gudhi::persistent_cohomology::Multi_field;
28 using Persistent_cohomology = Gudhi::persistent_cohomology::Persistent_cohomology<Simplex_tree, Multi_field >;
29 using Point = std::vector<double>;
30 using Points_off_reader = Gudhi::Points_off_reader<Point>;
31
32 void program_options(int argc, char * argv[]
33 , std::string & off_file_points
34 , std::string & filediag
35 , Filtration_value & threshold
36 , int & dim_max
37 , int & min_p
38 , int & max_p
39 , Filtration_value & min_persistence);
40
41 int main(int argc, char * argv[]) {
42 std::string off_file_points;
43 std::string filediag;
44 Filtration_value threshold;
45 int dim_max;
46 int min_p;
47 int max_p;
48 Filtration_value min_persistence;
49
50 program_options(argc, argv, off_file_points, filediag, threshold, dim_max, min_p, max_p, min_persistence);
51
52 Points_off_reader off_reader(off_file_points);
53 Rips_complex rips_complex_from_file(off_reader.get_point_cloud(), threshold, Gudhi::Euclidean_distance());
54
55 // Construct the Rips complex in a Simplex Tree
56 Simplex_tree simplex_tree;
57
58 rips_complex_from_file.create_complex(simplex_tree, dim_max);
59 std::clog << "The complex contains " << simplex_tree.num_simplices() << " simplices \n";
60 std::clog << " and has dimension " << simplex_tree.dimension() << " \n";
61
62 // Compute the persistence diagram of the complex
63 Persistent_cohomology pcoh(simplex_tree);
64 // initializes the coefficient field for homology
65 pcoh.init_coefficients(min_p, max_p);
66
67 pcoh.compute_persistent_cohomology(min_persistence);
68
69 // Output the diagram in filediag
70 if (filediag.empty()) {
71 pcoh.output_diagram();
72 } else {
73 std::ofstream out(filediag);
74 pcoh.output_diagram(out);
75 out.close();
76 }
77
78 return 0;
79 }
80
81 void program_options(int argc, char * argv[]
82 , std::string & off_file_points
83 , std::string & filediag
84 , Filtration_value & threshold
85 , int & dim_max
86 , int & min_p
87 , int & max_p
88 , Filtration_value & min_persistence) {
89 namespace po = boost::program_options;
90 po::options_description hidden("Hidden options");
91 hidden.add_options()
92 ("input-file", po::value<std::string>(&off_file_points),
93 "Name of an OFF file containing a point set.\n");
94
95 po::options_description visible("Allowed options");
96 visible.add_options()
97 ("help,h", "produce help message")
98 ("output-file,o", po::value<std::string>(&filediag)->default_value(std::string()),
99 "Name of file in which the persistence diagram is written. Default print in standard output")
100 ("max-edge-length,r", po::value<Filtration_value>(&threshold)->default_value(0),
101 "Maximal length of an edge for the Rips complex construction.")
102 ("cpx-dimension,d", po::value<int>(&dim_max)->default_value(1),
103 "Maximal dimension of the Rips complex we want to compute.")
104 ("min-field-charac,p", po::value<int>(&min_p)->default_value(2),
105 "Minimal characteristic p of the coefficient field Z/pZ.")
106 ("max-field-charac,q", po::value<int>(&max_p)->default_value(1223),
107 "Maximal characteristic q of the coefficient field Z/pZ.")
108 ("min-persistence,m", po::value<Filtration_value>(&min_persistence),
109 "Minimal lifetime of homology feature to be recorded. Default is 0");
110
111 po::positional_options_description pos;
112 pos.add("input-file", 1);
113
114 po::options_description all;
115 all.add(visible).add(hidden);
116
117 po::variables_map vm;
118 po::store(po::command_line_parser(argc, argv).
119 options(all).positional(pos).run(), vm);
120 po::notify(vm);
121
122 if (vm.count("help") || !vm.count("input-file")) {
123 std::clog << std::endl;
124 std::clog << "Compute the persistent homology with various coefficient fields \n";
125 std::clog << "of a Rips complex defined on a set of input points. The coefficient \n";
126 std::clog << "fields are all the Z/rZ for a prime number r contained in the \n";
127 std::clog << "specified range [p,q]\n \n";
128 std::clog << "The output diagram contains one bar per line, written with the convention: \n";
129 std::clog << " p1*...*pr dim b d \n";
130 std::clog << "where dim is the dimension of the homological feature,\n";
131 std::clog << "b and d are respectively the birth and death of the feature and \n";
132 std::clog << "p1*...*pr is the product of prime numbers pi such that the homology \n";
133 std::clog << "feature exists in homology with Z/piZ coefficients." << std::endl << std::endl;
134
135 std::clog << "Usage: " << argv[0] << " [options] input-file" << std::endl << std::endl;
136 std::clog << visible << std::endl;
137 exit(-1);
138 }
139 }