| 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, Marc Glisse |
| 4 |
* |
| 5 |
* Copyright (C) 2014 Inria |
| 6 |
* |
| 7 |
* Modification(s): |
| 8 |
* - YYYY/MM Author: Description of the modification |
| 9 |
*/ |
| 10 |
|
| 11 |
#include <gudhi/Simplex_tree.h> |
| 12 |
#include <gudhi/Persistent_cohomology.h> |
| 13 |
#include <gudhi/Rips_complex.h> |
| 14 |
#include <gudhi/Hasse_complex.h> |
| 15 |
#include <gudhi/Points_off_io.h> |
| 16 |
#include <gudhi/distance_functions.h> |
| 17 |
|
| 18 |
#include <boost/program_options.hpp> |
| 19 |
|
| 20 |
#include <string> |
| 21 |
#include <vector> |
| 22 |
|
| 23 |
//////////////////////////////////////////////////////////////// |
| 24 |
// // |
| 25 |
// WARNING: persistence computation itself is not parallel, // |
| 26 |
// and this uses more memory than rips_persistence. // |
| 27 |
// // |
| 28 |
//////////////////////////////////////////////////////////////// |
| 29 |
|
| 30 |
// Types definition |
| 31 |
using Simplex_tree = Gudhi::Simplex_tree<>; |
| 32 |
using Filtration_value = Simplex_tree::Filtration_value; |
| 33 |
using Rips_complex = Gudhi::rips_complex::Rips_complex<Filtration_value>; |
| 34 |
using Field_Zp = Gudhi::persistent_cohomology::Field_Zp; |
| 35 |
using Point = std::vector<double>; |
| 36 |
using Points_off_reader = Gudhi::Points_off_reader<Point>; |
| 37 |
|
| 38 |
void program_options(int argc, char * argv[] |
| 39 |
, std::string & off_file_points |
| 40 |
, std::string & filediag |
| 41 |
, Filtration_value & threshold |
| 42 |
, int & dim_max |
| 43 |
, int & p |
| 44 |
, Filtration_value & min_persistence); |
| 45 |
|
| 46 |
int main(int argc, char * argv[]) { |
| 47 |
std::string off_file_points; |
| 48 |
std::string filediag; |
| 49 |
Filtration_value threshold; |
| 50 |
int dim_max; |
| 51 |
int p; |
| 52 |
Filtration_value min_persistence; |
| 53 |
|
| 54 |
program_options(argc, argv, off_file_points, filediag, threshold, dim_max, p, min_persistence); |
| 55 |
|
| 56 |
Points_off_reader off_reader(off_file_points); |
| 57 |
Rips_complex rips_complex_from_file(off_reader.get_point_cloud(), threshold, Gudhi::Euclidean_distance()); |
| 58 |
|
| 59 |
// Construct the Rips complex in a Simplex Tree |
| 60 |
Simplex_tree& st = *new Simplex_tree; |
| 61 |
rips_complex_from_file.create_complex(st, dim_max); |
| 62 |
|
| 63 |
std::clog << "The complex contains " << st.num_simplices() << " simplices \n"; |
| 64 |
std::clog << " and has dimension " << st.dimension() << " \n"; |
| 65 |
|
| 66 |
// Sort the simplices in the order of the filtration |
| 67 |
st.initialize_filtration(); |
| 68 |
int count = 0; |
| 69 |
for (auto sh : st.filtration_simplex_range()) |
| 70 |
st.assign_key(sh, count++); |
| 71 |
|
| 72 |
// Convert to a more convenient representation. |
| 73 |
Gudhi::Hasse_complex<> hcpx(st); |
| 74 |
|
| 75 |
// Free some space. |
| 76 |
delete &st; |
| 77 |
|
| 78 |
// Compute the persistence diagram of the complex |
| 79 |
Gudhi::persistent_cohomology::Persistent_cohomology< Gudhi::Hasse_complex<>, Field_Zp > pcoh(hcpx); |
| 80 |
// initializes the coefficient field for homology |
| 81 |
pcoh.init_coefficients(p); |
| 82 |
|
| 83 |
pcoh.compute_persistent_cohomology(min_persistence); |
| 84 |
|
| 85 |
// Output the diagram in filediag |
| 86 |
if (filediag.empty()) { |
| 87 |
pcoh.output_diagram(); |
| 88 |
} else { |
| 89 |
std::ofstream out(filediag); |
| 90 |
pcoh.output_diagram(out); |
| 91 |
out.close(); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
void program_options(int argc, char * argv[] |
| 96 |
, std::string & off_file_points |
| 97 |
, std::string & filediag |
| 98 |
, Filtration_value & threshold |
| 99 |
, int & dim_max |
| 100 |
, int & p |
| 101 |
, Filtration_value & min_persistence) { |
| 102 |
namespace po = boost::program_options; |
| 103 |
po::options_description hidden("Hidden options"); |
| 104 |
hidden.add_options() |
| 105 |
("input-file", po::value<std::string>(&off_file_points), |
| 106 |
"Name of file containing a point set. Format is one point per line: X1 ... Xd "); |
| 107 |
|
| 108 |
po::options_description visible("Allowed options", 100); |
| 109 |
visible.add_options() |
| 110 |
("help,h", "produce help message") |
| 111 |
("output-file,o", po::value<std::string>(&filediag)->default_value(std::string()), |
| 112 |
"Name of file in which the persistence diagram is written. Default print in standard output") |
| 113 |
("max-edge-length,r", po::value<Filtration_value>(&threshold)->default_value(0), |
| 114 |
"Maximal length of an edge for the Rips complex construction.") |
| 115 |
("cpx-dimension,d", po::value<int>(&dim_max)->default_value(1), |
| 116 |
"Maximal dimension of the Rips complex we want to compute.") |
| 117 |
("field-charac,p", po::value<int>(&p)->default_value(11), |
| 118 |
"Characteristic p of the coefficient field Z/pZ for computing homology.") |
| 119 |
("min-persistence,m", po::value<Filtration_value>(&min_persistence), |
| 120 |
"Minimal lifetime of homology feature to be recorded. Default is 0. Enter a negative value to see zero length intervals"); |
| 121 |
|
| 122 |
po::positional_options_description pos; |
| 123 |
pos.add("input-file", 1); |
| 124 |
|
| 125 |
po::options_description all; |
| 126 |
all.add(visible).add(hidden); |
| 127 |
|
| 128 |
po::variables_map vm; |
| 129 |
po::store(po::command_line_parser(argc, argv). |
| 130 |
options(all).positional(pos).run(), vm); |
| 131 |
po::notify(vm); |
| 132 |
|
| 133 |
if (vm.count("help") || !vm.count("input-file")) { |
| 134 |
std::clog << std::endl; |
| 135 |
std::clog << "Compute the persistent homology with coefficient field Z/pZ \n"; |
| 136 |
std::clog << "of a Rips complex defined on a set of input points.\n \n"; |
| 137 |
std::clog << "The output diagram contains one bar per line, written with the convention: \n"; |
| 138 |
std::clog << " p dim b d \n"; |
| 139 |
std::clog << "where dim is the dimension of the homological feature,\n"; |
| 140 |
std::clog << "b and d are respectively the birth and death of the feature and \n"; |
| 141 |
std::clog << "p is the characteristic of the field Z/pZ used for homology coefficients." << std::endl << std::endl; |
| 142 |
|
| 143 |
std::clog << "Usage: " << argv[0] << " [options] input-file" << std::endl << std::endl; |
| 144 |
std::clog << visible << std::endl; |
| 145 |
exit(-1); |
| 146 |
} |
| 147 |
} |