| 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): Vincent Rouvreau |
| 4 |
* |
| 5 |
* Copyright (C) 2014 Inria |
| 6 |
* |
| 7 |
* Modification(s): |
| 8 |
* - YYYY/MM Author: Description of the modification |
| 9 |
*/ |
| 10 |
|
| 11 |
#include <gudhi/reader_utils.h> |
| 12 |
#include <gudhi/graph_simplicial_complex.h> |
| 13 |
#include <gudhi/distance_functions.h> |
| 14 |
#include <gudhi/Simplex_tree.h> |
| 15 |
#include <gudhi/Persistent_cohomology.h> |
| 16 |
|
| 17 |
#include <boost/program_options.hpp> |
| 18 |
|
| 19 |
#include <string> |
| 20 |
|
| 21 |
using namespace Gudhi; |
| 22 |
using namespace Gudhi::persistent_cohomology; |
| 23 |
|
| 24 |
typedef double Filtration_value; |
| 25 |
|
| 26 |
void program_options(int argc, char * argv[] |
| 27 |
, std::string & simplex_tree_file |
| 28 |
, std::string & output_file |
| 29 |
, int & p |
| 30 |
, Filtration_value & min_persistence); |
| 31 |
|
| 32 |
int main(int argc, char * argv[]) { |
| 33 |
std::string simplex_tree_file; |
| 34 |
std::string output_file; |
| 35 |
int p; |
| 36 |
Filtration_value min_persistence; |
| 37 |
|
| 38 |
program_options(argc, argv, simplex_tree_file, output_file, p, min_persistence); |
| 39 |
|
| 40 |
std::clog << "Simplex_tree from file=" << simplex_tree_file.c_str() << " - output_file=" << output_file.c_str() |
| 41 |
<< std::endl; |
| 42 |
std::clog << " - p=" << p << " - min_persistence=" << min_persistence << std::endl; |
| 43 |
|
| 44 |
// Read the list of simplices from a file. |
| 45 |
Simplex_tree<> simplex_tree; |
| 46 |
|
| 47 |
std::ifstream simplex_tree_stream(simplex_tree_file); |
| 48 |
simplex_tree_stream >> simplex_tree; |
| 49 |
|
| 50 |
std::clog << "The complex contains " << simplex_tree.num_simplices() << " simplices" << std::endl; |
| 51 |
std::clog << " - dimension " << simplex_tree.dimension() << std::endl; |
| 52 |
|
| 53 |
/* |
| 54 |
std::clog << std::endl << std::endl << "Iterator on Simplices in the filtration, with [filtration value]:" << std::endl; |
| 55 |
for( auto f_simplex : simplex_tree.filtration_simplex_range() ) |
| 56 |
{ std::clog << " " << "[" << simplex_tree.filtration(f_simplex) << "] "; |
| 57 |
for( auto vertex : simplex_tree.simplex_vertex_range(f_simplex) ) |
| 58 |
{ std::clog << vertex << " "; } |
| 59 |
std::clog << std::endl; |
| 60 |
}*/ |
| 61 |
|
| 62 |
// Compute the persistence diagram of the complex |
| 63 |
Persistent_cohomology< Simplex_tree<>, Field_Zp > pcoh(simplex_tree); |
| 64 |
// initializes the coefficient field for homology |
| 65 |
pcoh.init_coefficients(p); |
| 66 |
|
| 67 |
pcoh.compute_persistent_cohomology(min_persistence); |
| 68 |
|
| 69 |
// Output the diagram in output_file |
| 70 |
if (output_file.empty()) { |
| 71 |
pcoh.output_diagram(); |
| 72 |
} else { |
| 73 |
std::ofstream out(output_file); |
| 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 & simplex_tree_file |
| 83 |
, std::string & output_file |
| 84 |
, int & p |
| 85 |
, Filtration_value & min_persistence) { |
| 86 |
namespace po = boost::program_options; |
| 87 |
po::options_description hidden("Hidden options"); |
| 88 |
hidden.add_options() |
| 89 |
("input-file", po::value<std::string>(&simplex_tree_file), |
| 90 |
"Name of file containing a simplex set. Format is one simplex per line (cf. reader_utils.h - read_simplex): Dim1 X11 X12 ... X1d Fil1 "); |
| 91 |
|
| 92 |
po::options_description visible("Allowed options", 100); |
| 93 |
visible.add_options() |
| 94 |
("help,h", "produce help message") |
| 95 |
("output-file,o", po::value<std::string>(&output_file)->default_value(std::string()), |
| 96 |
"Name of file in which the persistence diagram is written. Default print in standard output") |
| 97 |
("field-charac,p", po::value<int>(&p)->default_value(11), |
| 98 |
"Characteristic p of the coefficient field Z/pZ for computing homology.") |
| 99 |
("min-persistence,m", po::value<Filtration_value>(&min_persistence), |
| 100 |
"Minimal lifetime of homology feature to be recorded. Default is 0"); |
| 101 |
|
| 102 |
po::positional_options_description pos; |
| 103 |
pos.add("input-file", 1); |
| 104 |
|
| 105 |
po::options_description all; |
| 106 |
all.add(visible).add(hidden); |
| 107 |
|
| 108 |
po::variables_map vm; |
| 109 |
po::store(po::command_line_parser(argc, argv). |
| 110 |
options(all).positional(pos).run(), vm); |
| 111 |
po::notify(vm); |
| 112 |
|
| 113 |
if (vm.count("help") || !vm.count("input-file")) { |
| 114 |
std::clog << std::endl; |
| 115 |
std::clog << "Compute the persistent homology with coefficient field Z/pZ \n"; |
| 116 |
std::clog << "of a Rips complex defined on a set of input points.\n \n"; |
| 117 |
std::clog << "The output diagram contains one bar per line, written with the convention: \n"; |
| 118 |
std::clog << " p dim b d \n"; |
| 119 |
std::clog << "where dim is the dimension of the homological feature,\n"; |
| 120 |
std::clog << "b and d are respectively the birth and death of the feature and \n"; |
| 121 |
std::clog << "p is the characteristic of the field Z/pZ used for homology coefficients." << std::endl << std::endl; |
| 122 |
|
| 123 |
std::clog << "Usage: " << argv[0] << " [options] input-file" << std::endl << std::endl; |
| 124 |
std::clog << visible << std::endl; |
| 125 |
exit(-1); |
| 126 |
} |
| 127 |
} |