1 #include <topology/cohomology-persistence.h>
2 #include <topology/rips.h>
4 #include <geometry/l2distance.h>
5 #include <geometry/distances.h>
7 #include <utilities/containers.h> // for BackInsertFunctor
8 #include <utilities/property-maps.h>
9 #include <utilities/timer.h>
10 #include <utilities/log.h>
11 #include <utilities/counter.h>
12 #include <utilities/memory.h>
16 #include <boost/tuple/tuple.hpp>
17 #include <boost/program_options.hpp>
18 #include <boost/progress.hpp>
22 typedef PairwiseDistances<PointContainer, L2Distance> PairDistances;
23 typedef PairDistances::DistanceType DistanceType;
24 typedef PairDistances::IndexType Vertex;
26 typedef boost::tuple<Dimension, DistanceType> BirthInfo;
27 typedef CohomologyPersistence<BirthInfo, Wrapper<unsigned> > Persistence;
28 typedef Persistence::SimplexIndex Index;
29 typedef Persistence::Death Death;
30 typedef Persistence::CocyclePtr CocyclePtr;
32 typedef Rips<PairDistances, Simplex<Vertex, Index> > Generator;
33 typedef Generator::Simplex Smplx;
34 typedef std::vector<Smplx> SimplexVector;
35 typedef SimplexVector::const_iterator SV_const_iterator;
37 typedef std::map<Smplx, Index, Smplx::VertexComparison> Complex;
39 #include "output.h" // for output_*()
41 void program_options(int argc, char* argv[], std::string& infilename, Dimension& skeleton, DistanceType& max_distance, ZpField::Element& prime, std::string& boundary_name, std::string& cocycle_prefix, std::string& vertices_name, std::string& diagram_name);
43 int main(int argc, char* argv[])
46 rlog::RLogInit(argc, argv);
48 stderrLog.subscribeTo( RLOG_CHANNEL("error") ); 52 DistanceType max_distance;
53 ZpField::Element prime;
54 std::string infilename, boundary_name, cocycle_prefix, vertices_name, diagram_name;
56 program_options(argc, argv, infilename, skeleton, max_distance, prime, boundary_name, cocycle_prefix, vertices_name, diagram_name);
57 std::ofstream bdry_out(boundary_name.c_str());
58 std::ofstream vertices_out(vertices_name.c_str());
59 std::ofstream diagram_out(diagram_name.c_str());
60 std::cout << "Boundary matrix: " << boundary_name << std::endl;
61 std::cout << "Cocycles: " << cocycle_prefix << "*.ccl" << std::endl;
62 std::cout << "Vertices: " << vertices_name << std::endl;
63 std::cout << "Diagram: " << diagram_name << std::endl;
65 Timer total_timer; total_timer.start();
66 PointContainer points;
67 read_points(infilename, points);
69 PairDistances distances(points);
70 Generator rips(distances);
71 Generator::Evaluator size(distances);
72 Generator::Comparison cmp(distances);
75 Timer rips_timer; rips_timer.start();
76 rips.generate(skeleton, max_distance, make_push_back_functor(v));
77 std::sort(v.begin(), v.end(), Smplx::VertexComparison());
79 std::vector<unsigned> index_in_v(v.size());
80 for (unsigned idx = 0; idx < v.size(); ++idx)
81 index_in_v[idx] = idx;
82 std::sort(index_in_v.begin(), index_in_v.end(), IndirectIndexComparison<SimplexVector, Generator::Comparison>(v, cmp));
84 BinarySearchMap<Smplx, SimplexVector::iterator, Smplx::VertexComparison> map_of_v(v.begin(), v.end());
87 std::cout << "Simplex vector generated, size: " << v.size() << std::endl;
89 output_boundary_matrix(bdry_out, v, Smplx::VertexComparison());
90 output_vertex_indices(vertices_out, v);
92 Timer persistence_timer; persistence_timer.start();
95 boost::progress_display show_progress(v.size());
98 Counter::CounterType max_element_count = 0;
99 unsigned max_memory = 0;
105 int max_uordblks = 0;
106 int max_fordblks = 0;
109 for (unsigned j = 0; j < index_in_v.size(); ++j)
111 SimplexVector::const_iterator cur = v.begin() + index_in_v[j];
112 std::vector<Index> boundary;
113 for (Smplx::BoundaryIterator bcur = cur->boundary_begin(); bcur != cur->boundary_end(); ++bcur)
114 boundary.push_back(map_of_v[*bcur]->data());
116 Index idx; Death d; CocyclePtr ccl;
117 bool store = cur->dimension() < skeleton;
118 boost::tie(idx, d, ccl) = p.add(boundary.begin(), boundary.end(), boost::make_tuple(cur->dimension(), size(*cur)), store, index_in_v[j]);
122 map_of_v[*cur]->data() = idx;
124 if (d && (size(*cur) - d->get<1>()) > 0)
126 AssertMsg(d->get<0>() == cur->dimension() - 1, "Dimensions must match");
127 diagram_out << (cur->dimension() - 1) << " " << d->get<1>() << " " << size(*cur) << std::endl;
132 max_element_count = std::max(max_element_count, cCohomologyElementCount->count);
135 // output infinte persistence pairs
136 for (Persistence::CocycleIndex cur = p.begin(); cur != p.end(); ++cur)
137 diagram_out << cur->birth.get<0>() << " " << cur->birth.get<1>() << " inf" << std::endl;
138 persistence_timer.stop();
141 // p.show_cocycles();
142 // Output alive cocycles of dimension 1
143 if (!cocycle_prefix.empty())
146 for (Persistence::Cocycles::const_iterator cur = p.begin(); cur != p.end(); ++cur)
148 if (cur->birth.get<0>() != 1) continue;
149 output_cocycle(cocycle_prefix, i, v, cur->birth, cur->zcolumn, prime);
150 // std::cout << "Cocycle of dimension: " << cur->birth.get<0>() << " born at " << cur->birth.get<1>() << std::endl;
155 rips_timer.check("Rips timer"); 156 persistence_timer.check("Persistence timer"); 157 total_timer.check("Total timer"); 160 std::cout << "Max element count: " << max_element_count << std::endl;
164 void program_options(int argc, char* argv[], std::string& infilename, Dimension& skeleton, DistanceType& max_distance, ZpField::Element& prime, std::string& boundary_name, std::string& cocycle_prefix, std::string& vertices_name, std::string& diagram_name)
166 namespace po = boost::program_options;
168 po::options_description hidden("Hidden options"); 170 ("input-file", po::value<std::string>(&infilename), "Point set whose Rips zigzag we want to compute"); 172 po::options_description visible("Allowed options", 100); 173 visible.add_options()
174 ("help,h", "produce help message") 175 ("skeleton-dimsnion,s", po::value<Dimension>(&skeleton)->default_value(2), "Dimension of the Rips complex we want to compute") 176 ("prime,p", po::value<ZpField::Element>(&prime)->default_value(11), "Prime p for the field F_p") 177 ("max-distance,m", po::value<DistanceType>(&max_distance)->default_value(Infinity), "Maximum value for the Rips complex construction") 178 ("boundary,b", po::value<std::string>(&boundary_name), "Filename where to output the boundary matrix") 179 ("cocycle,c", po::value<std::string>(&cocycle_prefix), "Prefix of the filename where to output the 1-dimensional cocycles") 180 ("vertices,v", po::value<std::string>(&vertices_name), "Filename where to output the simplex-vertex mapping") 181 ("diagram,d", po::value<std::string>(&diagram_name), "Filename where to output the persistence diagram"); 183 std::vector<std::string> log_channels;
184 visible.add_options()
185 ("log,l", po::value< std::vector<std::string> >(&log_channels), "log channels to turn on (info, debug, etc)"); 188 po::positional_options_description pos;
189 pos.add("input-file", 1); 191 po::options_description all; all.add(visible).add(hidden);
193 po::variables_map vm;
194 po::store(po::command_line_parser(argc, argv).
195 options(all).positional(pos).run(), vm);
199 for (std::vector<std::string>::const_iterator cur = log_channels.begin(); cur != log_channels.end(); ++cur)
200 stderrLog.subscribeTo( RLOG_CHANNEL(cur->c_str()) );
203 if (vm.count("help") || !vm.count("input-file")) 205 std::cout << "Usage: " << argv[0] << " [options] input-file" << std::endl;
206 std::cout << visible << std::endl;