16 #include <tbb/parallel_for.h> 20 #include <gudhi/Debug_utils.h> 23 #include <gudhi/Simplex_tree.h> 24 #include <gudhi/Rips_complex.h> 25 #include <gudhi/Points_off_io.h> 27 #include <gudhi/Persistent_cohomology.h> 28 #include <gudhi/Bottleneck.h> 30 #include <boost/config.hpp> 31 #include <boost/graph/graph_traits.hpp> 32 #include <boost/graph/adjacency_list.hpp> 33 #include <boost/graph/connected_components.hpp> 34 #include <boost/graph/dijkstra_shortest_paths.hpp> 35 #include <boost/graph/subgraph.hpp> 36 #include <boost/graph/graph_utility.hpp> 38 #include <CGAL/version.h> 53 namespace cover_complex {
58 using Persistence_diagram = std::vector<std::pair<double, double> >;
59 using Graph = boost::subgraph<
60 boost::adjacency_list<boost::setS, boost::vecS, boost::undirectedS, boost::no_property,
61 boost::property<boost::edge_index_t, int, boost::property<boost::edge_weight_t, double> > > >;
62 using Vertex_t = boost::graph_traits<Graph>::vertex_descriptor;
63 using Index_map = boost::property_map<Graph, boost::vertex_index_t>::type;
64 using Weight_map = boost::property_map<Graph, boost::edge_weight_t>::type;
86 template <
typename Po
int>
92 std::vector<Point> point_cloud;
93 std::vector<std::vector<double> > distances;
98 std::vector<double> func;
99 std::vector<double> func_color;
100 bool functional_cover =
false;
102 Graph one_skeleton_OFF;
104 std::vector<Vertex_t> vertices;
106 std::vector<std::vector<int> > simplices;
107 std::vector<int> voronoi_subsamples;
109 Persistence_diagram PD;
110 std::vector<double> distribution;
112 std::vector<std::vector<int> >
114 std::map<int, std::vector<int> >
116 std::map<int, double> cover_std;
120 std::map<int, std::pair<int, double> >
123 int resolution_int = -1;
124 double resolution_double = -1;
126 double rate_constant = 10;
127 double rate_power = 0.001;
130 std::map<int, int> name2id, name2idinv;
132 std::string cover_name;
133 std::string point_cloud_name;
134 std::string color_name;
137 void remove_edges(Graph& G) {
138 boost::graph_traits<Graph>::edge_iterator ei, ei_end;
139 for (boost::tie(ei, ei_end) = boost::edges(G); ei != ei_end; ++ei) boost::remove_edge(*ei, G);
143 double GetUniform() {
144 thread_local std::default_random_engine re;
145 std::uniform_real_distribution<double> Dist(0, 1);
150 void SampleWithoutReplacement(
int populationSize,
int sampleSize, std::vector<int>& samples) {
154 while (m < sampleSize) {
156 if ((populationSize - t) * u >= sampleSize - m) {
195 rate_constant = constant;
218 n = point_cloud.size(); data_dimension = point_cloud[0].size();
219 point_cloud_name =
"matrix"; cover.resize(n);
220 for(
int i = 0; i < n; i++){
221 boost::add_vertex(one_skeleton_OFF);
222 vertices.push_back(boost::add_vertex(one_skeleton));
224 this->point_cloud = point_cloud;
233 point_cloud_name = off_file_name;
234 std::ifstream input(off_file_name);
238 while (comment ==
'#') {
239 std::getline(input, line);
240 if (!line.empty() && !all_of(line.begin(), line.end(), (int (*)(int))isspace))
241 comment = line[line.find_first_not_of(
' ')];
243 if (strcmp((
char*)line.c_str(),
"nOFF") == 0) {
245 while (comment ==
'#') {
246 std::getline(input, line);
247 if (!line.empty() && !all_of(line.begin(), line.end(), (int (*)(int))isspace))
248 comment = line[line.find_first_not_of(
' ')];
250 std::stringstream stream(line);
251 stream >> data_dimension;
257 int numedges, numfaces, i, dim;
258 while (comment ==
'#') {
259 std::getline(input, line);
260 if (!line.empty() && !all_of(line.begin(), line.end(), (int (*)(int))isspace))
261 comment = line[line.find_first_not_of(
' ')];
263 std::stringstream stream(line);
270 std::getline(input, line);
271 if (!line.empty() && line[line.find_first_not_of(
' ')] !=
'#' &&
272 !all_of(line.begin(), line.end(), (int (*)(int))isspace)) {
273 std::stringstream iss(line);
274 std::vector<double> point;
275 point.assign(std::istream_iterator<double>(iss), std::istream_iterator<double>());
276 point_cloud.emplace_back(point.begin(), point.begin() + data_dimension);
277 boost::add_vertex(one_skeleton_OFF);
278 vertices.push_back(boost::add_vertex(one_skeleton));
279 cover.emplace_back();
285 while (i < numfaces) {
286 std::getline(input, line);
287 if (!line.empty() && line[line.find_first_not_of(
' ')] !=
'#' &&
288 !all_of(line.begin(), line.end(), (int (*)(int))isspace)) {
289 std::vector<int> simplex;
290 std::stringstream iss(line);
291 simplex.assign(std::istream_iterator<int>(iss), std::istream_iterator<int>());
293 for (
int j = 1; j <= dim; j++)
294 for (
int k = j + 1; k <= dim; k++)
295 boost::add_edge(vertices[simplex[j]], vertices[simplex[k]], one_skeleton_OFF);
300 return input.is_open();
316 remove_edges(one_skeleton);
318 std::ifstream input(graph_file_name);
321 while (std::getline(input, line)) {
322 std::stringstream stream(line);
324 while (stream >> neighb) boost::add_edge(vertices[source], vertices[neighb], one_skeleton);
333 remove_edges(one_skeleton);
334 if (num_edges(one_skeleton_OFF))
335 one_skeleton = one_skeleton_OFF;
337 std::cerr <<
"No triangulation read in OFF file!" << std::endl;
347 template <
typename Distance>
349 remove_edges(one_skeleton);
350 if (distances.size() == 0) compute_pairwise_distances(distance);
351 for (
int i = 0; i < n; i++) {
352 for (
int j = i + 1; j < n; j++) {
353 if (distances[i][j] <= threshold) {
354 boost::add_edge(vertices[i], vertices[j], one_skeleton);
355 boost::put(boost::edge_weight, one_skeleton, boost::edge(vertices[i], vertices[j], one_skeleton).first,
363 void set_graph_weights() {
364 Index_map index = boost::get(boost::vertex_index, one_skeleton);
365 Weight_map weight = boost::get(boost::edge_weight, one_skeleton);
366 boost::graph_traits<Graph>::edge_iterator ei, ei_end;
367 for (boost::tie(ei, ei_end) = boost::edges(one_skeleton); ei != ei_end; ++ei)
368 boost::put(weight, *ei,
369 distances[index[boost::source(*ei, one_skeleton)]][index[boost::target(*ei, one_skeleton)]]);
379 n = distance_matrix.size(); data_dimension = 0; point_cloud_name =
"matrix";
380 cover.resize(n); point_cloud.resize(n);
381 for(
int i = 0; i < n; i++){
382 boost::add_vertex(one_skeleton_OFF);
383 vertices.push_back(boost::add_vertex(one_skeleton));
385 distances = distance_matrix;
391 template <
typename Distance>
392 void compute_pairwise_distances(Distance ref_distance) {
394 std::vector<double> zeros(n);
395 for (
int i = 0; i < n; i++) distances.push_back(zeros);
396 std::string distance = point_cloud_name +
"_dist";
397 std::ifstream input(distance, std::ios::out | std::ios::binary);
400 if (verbose) std::clog <<
"Reading distances..." << std::endl;
401 for (
int i = 0; i < n; i++) {
402 for (
int j = i; j < n; j++) {
403 input.read((
char*)&d, 8);
410 if (verbose) std::clog <<
"Computing distances..." << std::endl;
412 std::ofstream output(distance, std::ios::out | std::ios::binary);
413 for (
int i = 0; i < n; i++) {
414 int state = (int)floor(100 * (i * 1.0 + 1) / n) % 10;
415 if (state == 0 && verbose) std::clog <<
"\r" << state <<
"%" << std::flush;
416 for (
int j = i; j < n; j++) {
417 double dis = ref_distance(point_cloud[i], point_cloud[j]);
418 distances[i][j] = dis;
419 distances[j][i] = dis;
420 output.write((
char*)&dis, 8);
424 if (verbose) std::clog << std::endl;
438 template <
typename Distance>
440 int m = floor(n / std::exp((1 + rate_power) * std::log(std::log(n) / std::log(rate_constant))));
441 m = (std::min)(m, n - 1);
444 if (verbose) std::clog << n <<
" points in R^" << data_dimension << std::endl;
445 if (verbose) std::clog <<
"Subsampling " << m <<
" points" << std::endl;
447 if (distances.size() == 0) compute_pairwise_distances(distance);
450 std::mutex deltamutex;
451 tbb::parallel_for(0, N, [&](
int i){
452 std::vector<int> samples(m);
453 SampleWithoutReplacement(n, m, samples);
454 double hausdorff_dist = 0;
455 for (
int j = 0; j < n; j++) {
456 double mj = distances[j][samples[0]];
457 for (
int k = 1; k < m; k++) mj = (std::min)(mj, distances[j][samples[k]]);
458 hausdorff_dist = (std::max)(hausdorff_dist, mj);
461 delta += hausdorff_dist / N;
465 for (
int i = 0; i < N; i++) {
466 std::vector<int> samples(m);
467 SampleWithoutReplacement(n, m, samples);
468 double hausdorff_dist = 0;
469 for (
int j = 0; j < n; j++) {
470 double mj = distances[j][samples[0]];
471 for (
int k = 1; k < m; k++) mj = (std::min)(mj, distances[j][samples[k]]);
472 hausdorff_dist = (std::max)(hausdorff_dist, mj);
474 delta += hausdorff_dist / N;
478 if (verbose) std::clog <<
"delta = " << delta << std::endl;
495 std::ifstream input(func_file_name);
498 while (std::getline(input, line)) {
499 std::stringstream stream(line);
504 functional_cover =
true;
505 cover_name = func_file_name;
515 if(point_cloud[0].size() > 0){
516 for (
int i = 0; i < n; i++) func.push_back(point_cloud[i][k]);
517 functional_cover =
true;
518 cover_name =
"coordinate " + std::to_string(k);
521 std::cerr <<
"Only pairwise distances provided---cannot access " << k <<
"th coordinate; returning null vector instead" << std::endl;
522 for (
int i = 0; i < n; i++) func.push_back(0.0);
523 functional_cover =
true;
534 template <
class InputRange>
536 for (
int i = 0; i < n; i++) func.push_back(
function[i]);
537 functional_cover =
true;
553 if (!functional_cover) {
554 std::cerr <<
"Cover needs to come from the preimages of a function." << std::endl;
557 if (type !=
"Nerve" && type !=
"GIC") {
558 std::cerr <<
"Type of complex needs to be specified." << std::endl;
563 Index_map index = boost::get(boost::vertex_index, one_skeleton);
566 boost::graph_traits<Graph>::edge_iterator ei, ei_end;
567 for (boost::tie(ei, ei_end) = boost::edges(one_skeleton); ei != ei_end; ++ei)
568 reso = (std::max)(reso, std::abs(func[index[boost::source(*ei, one_skeleton)]] -
569 func[index[boost::target(*ei, one_skeleton)]]));
570 if (verbose) std::clog <<
"resolution = " << reso << std::endl;
571 resolution_double = reso;
574 if (type ==
"Nerve") {
575 boost::graph_traits<Graph>::edge_iterator ei, ei_end;
576 for (boost::tie(ei, ei_end) = boost::edges(one_skeleton); ei != ei_end; ++ei)
577 reso = (std::max)(reso, std::abs(func[index[boost::source(*ei, one_skeleton)]] -
578 func[index[boost::target(*ei, one_skeleton)]]) /
580 if (verbose) std::clog <<
"resolution = " << reso << std::endl;
581 resolution_double = reso;
612 if (resolution_double == -1 && resolution_int == -1) {
613 std::cerr <<
"Number and/or length of intervals not specified" << std::endl;
617 std::cerr <<
"Gain not specified" << std::endl;
622 double minf = (std::numeric_limits<float>::max)();
623 double maxf = std::numeric_limits<float>::lowest();
624 for (
int i = 0; i < n; i++) {
625 minf = (std::min)(minf, func[i]);
626 maxf = (std::max)(maxf, func[i]);
628 if (verbose) std::clog <<
"Min function value = " << minf <<
" and Max function value = " << maxf << std::endl;
631 std::vector<std::pair<double, double> > intervals;
634 if (resolution_double == -1) {
635 double incr = (maxf - minf) / resolution_int;
637 double alpha = (incr * gain) / (2 - 2 * gain);
638 double y = minf + incr + alpha;
639 std::pair<double, double> interm(x, y);
640 intervals.push_back(interm);
641 for (
int i = 1; i < resolution_int - 1; i++) {
642 x = minf + i * incr - alpha;
643 y = minf + (i + 1) * incr + alpha;
644 std::pair<double, double> inter(x, y);
645 intervals.push_back(inter);
647 x = minf + (resolution_int - 1) * incr - alpha;
649 std::pair<double, double> interM(x, y);
650 intervals.push_back(interM);
651 res = intervals.size();
653 for (
int i = 0; i < res; i++)
654 std::clog <<
"Interval " << i <<
" = [" << intervals[i].first <<
", " << intervals[i].second <<
"]" 658 if (resolution_int == -1) {
660 double y = x + resolution_double;
661 while (y <= maxf && maxf - (y - gain * resolution_double) >= resolution_double) {
662 std::pair<double, double> inter(x, y);
663 intervals.push_back(inter);
664 x = y - gain * resolution_double;
665 y = x + resolution_double;
667 std::pair<double, double> interM(x, maxf);
668 intervals.push_back(interM);
669 res = intervals.size();
671 for (
int i = 0; i < res; i++)
672 std::clog <<
"Interval " << i <<
" = [" << intervals[i].first <<
", " << intervals[i].second <<
"]" 677 double y = x + resolution_double;
679 while (count < resolution_int && y <= maxf && maxf - (y - gain * resolution_double) >= resolution_double) {
680 std::pair<double, double> inter(x, y);
681 intervals.push_back(inter);
683 x = y - gain * resolution_double;
684 y = x + resolution_double;
686 res = intervals.size();
688 for (
int i = 0; i < res; i++)
689 std::clog <<
"Interval " << i <<
" = [" << intervals[i].first <<
", " << intervals[i].second <<
"]" 696 std::vector<int> points(n);
697 for (
int i = 0; i < n; i++) points[i] = i;
698 std::sort(points.begin(), points.end(), [
this](
int p1,
int p2){
return (this->func[p1] < this->func[p2]);});
702 Index_map index = boost::get(boost::vertex_index, one_skeleton);
703 std::map<int, std::vector<int> > preimages;
704 std::map<int, double> funcstd;
706 if (verbose) std::clog <<
"Computing preimages..." << std::endl;
707 for (
int i = 0; i < res; i++) {
709 std::pair<double, double> inter1 = intervals[i];
715 std::pair<double, double> inter3 = intervals[i - 1];
716 while (func[points[tmp]] < inter3.second && tmp != n) {
717 preimages[i].push_back(points[tmp]);
725 std::pair<double, double> inter2 = intervals[i + 1];
726 while (func[points[tmp]] < inter2.first && tmp != n) {
727 preimages[i].push_back(points[tmp]);
732 while (func[points[tmp]] < inter1.second && tmp != n) {
733 preimages[i].push_back(points[tmp]);
738 std::pair<double, double> inter3 = intervals[i - 1];
739 while (func[points[tmp]] < inter3.second && tmp != n) {
740 preimages[i].push_back(points[tmp]);
744 preimages[i].push_back(points[tmp]);
751 funcstd[i] = 0.5 * (u + v);
755 if (verbose) std::clog <<
"Computing connected components (parallelized)..." << std::endl;
756 std::mutex covermutex, idmutex;
757 tbb::parallel_for(0, res, [&](
int i){
759 Graph G = one_skeleton.create_subgraph();
760 int num = preimages[i].size();
761 std::vector<int> component(num);
762 for (
int j = 0; j < num; j++) boost::add_vertex(index[vertices[preimages[i][j]]], G);
763 boost::connected_components(G, &component[0]);
767 for (
int j = 0; j < num; j++) {
769 if (component[j] > max) max = component[j];
772 int identifier = ((i + component[j])*(i + component[j]) + 3 * i + component[j]) / 2;
776 cover[preimages[i][j]].push_back(identifier);
777 cover_back[identifier].push_back(preimages[i][j]);
778 cover_fct[identifier] = i;
779 cover_std[identifier] = funcstd[i];
780 cover_color[identifier].second += func_color[preimages[i][j]];
781 cover_color[identifier].first += 1;
791 if (verbose) std::clog <<
"Computing connected components..." << std::endl;
792 for (
int i = 0; i < res; i++) {
794 Graph G = one_skeleton.create_subgraph();
795 int num = preimages[i].size();
796 std::vector<int> component(num);
797 for (
int j = 0; j < num; j++) boost::add_vertex(index[vertices[preimages[i][j]]], G);
798 boost::connected_components(G, &component[0]);
802 for (
int j = 0; j < num; j++) {
804 if (component[j] > max) max = component[j];
807 int identifier = (std::pow(i + component[j], 2) + 3 * i + component[j]) / 2;
810 cover[preimages[i][j]].push_back(identifier);
811 cover_back[identifier].push_back(preimages[i][j]);
812 cover_fct[identifier] = i;
813 cover_std[identifier] = funcstd[i];
814 cover_color[identifier].second += func_color[preimages[i][j]];
815 cover_color[identifier].first += 1;
823 maximal_dim =
id - 1;
824 for (std::map<
int, std::pair<int, double> >::iterator iit = cover_color.begin(); iit != cover_color.end(); iit++)
825 iit->second.second /= iit->second.first;
838 std::vector<int> cov_elts, cov_number;
839 std::ifstream input(cover_file_name);
841 while (std::getline(input, line)) {
843 std::stringstream stream(line);
844 while (stream >> cov) {
845 cov_elts.push_back(cov);
846 cov_number.push_back(cov);
847 cover_fct[cov] = cov;
848 cover_color[cov].second += func_color[i];
849 cover_color[cov].first++;
850 cover_back[cov].push_back(i);
856 std::sort(cov_number.begin(), cov_number.end());
857 std::vector<int>::iterator it = std::unique(cov_number.begin(), cov_number.end());
858 cov_number.resize(std::distance(cov_number.begin(), it));
860 maximal_dim = cov_number.size() - 1;
861 for (
int i = 0; i <= maximal_dim; i++) cover_color[i].second /= cover_color[i].first;
862 cover_name = cover_file_name;
872 template <
typename Distance>
874 voronoi_subsamples.resize(m);
875 SampleWithoutReplacement(n, m, voronoi_subsamples);
876 if (distances.size() == 0) compute_pairwise_distances(distance);
878 Weight_map weight = boost::get(boost::edge_weight, one_skeleton);
879 Index_map index = boost::get(boost::vertex_index, one_skeleton);
880 std::vector<double> mindist(n);
881 for (
int j = 0; j < n; j++) mindist[j] = (std::numeric_limits<double>::max)();
885 if (verbose) std::clog <<
"Computing geodesic distances (parallelized)..." << std::endl;
886 std::mutex coverMutex; std::mutex mindistMutex;
887 tbb::parallel_for(0, m, [&](
int i){
888 int seed = voronoi_subsamples[i];
889 std::vector<double> dmap(n);
890 boost::dijkstra_shortest_paths(
891 one_skeleton, vertices[seed],
892 boost::weight_map(weight).distance_map(boost::make_iterator_property_map(dmap.begin(), index)));
894 coverMutex.lock(); mindistMutex.lock();
895 for (
int j = 0; j < n; j++)
896 if (mindist[j] > dmap[j]) {
897 mindist[j] = dmap[j];
898 if (cover[j].size() == 0)
899 cover[j].push_back(i);
903 coverMutex.unlock(); mindistMutex.unlock();
906 for (
int i = 0; i < m; i++) {
907 if (verbose) std::clog <<
"Computing geodesic distances to seed " << i <<
"..." << std::endl;
908 int seed = voronoi_subsamples[i];
909 std::vector<double> dmap(n);
910 boost::dijkstra_shortest_paths(
911 one_skeleton, vertices[seed],
912 boost::weight_map(weight).distance_map(boost::make_iterator_property_map(dmap.begin(), index)));
914 for (
int j = 0; j < n; j++)
915 if (mindist[j] > dmap[j]) {
916 mindist[j] = dmap[j];
917 if (cover[j].size() == 0)
918 cover[j].push_back(i);
925 for (
int i = 0; i < n; i++) {
926 cover_back[cover[i][0]].push_back(i);
927 cover_color[cover[i][0]].second += func_color[i];
928 cover_color[cover[i][0]].first++;
930 for (
int i = 0; i < m; i++) cover_color[i].second /= cover_color[i].first;
932 cover_name =
"Voronoi";
942 const std::vector<int>&
subpopulation(
int c) {
return cover_back[name2idinv[c]]; }
957 std::ifstream input(color_file_name);
960 while (std::getline(input, line)) {
961 std::stringstream stream(line);
963 func_color.push_back(f);
966 color_name = color_file_name;
976 if(point_cloud[0].size() > 0){
977 for (
int i = 0; i < n; i++) func_color.push_back(point_cloud[i][k]);
978 color_name =
"coordinate ";
979 color_name.append(std::to_string(k));
982 std::cerr <<
"Only pairwise distances provided---cannot access " << k <<
"th coordinate; returning null vector instead" << std::endl;
983 for (
int i = 0; i < n; i++) func.push_back(0.0);
984 functional_cover =
true;
996 for (
unsigned int i = 0; i < color.size(); i++) func_color.push_back(color[i]);
1005 std::string mapp = point_cloud_name +
"_sc.dot";
1006 std::ofstream graphic(mapp);
1008 double maxv = std::numeric_limits<double>::lowest();
1009 double minv = (std::numeric_limits<double>::max)();
1010 for (std::map<
int, std::pair<int, double> >::iterator iit = cover_color.begin(); iit != cover_color.end(); iit++) {
1011 maxv = (std::max)(maxv, iit->second.second);
1012 minv = (std::min)(minv, iit->second.second);
1016 std::vector<int> nodes;
1019 graphic <<
"graph GIC {" << std::endl;
1021 for (std::map<
int, std::pair<int, double> >::iterator iit = cover_color.begin(); iit != cover_color.end(); iit++) {
1022 if (iit->second.first > mask) {
1023 nodes.push_back(iit->first);
1024 name2id[iit->first] = id;
1025 name2idinv[id] = iit->first;
1027 graphic << name2id[iit->first] <<
"[shape=circle fontcolor=black color=black label=\"" << name2id[iit->first]
1028 <<
":" << iit->second.first <<
"\" style=filled fillcolor=\"" 1029 << (1 - (maxv - iit->second.second) / (maxv - minv)) * 0.6 <<
", 1, 1\"]" << std::endl;
1034 int num_simplices = simplices.size();
1035 for (
int i = 0; i < num_simplices; i++)
1036 if (simplices[i].size() == 2) {
1037 if (cover_color[simplices[i][0]].first > mask && cover_color[simplices[i][1]].first > mask) {
1038 graphic <<
" " << name2id[simplices[i][0]] <<
" -- " << name2id[simplices[i][1]] <<
" [weight=15];" 1045 std::clog << mapp <<
" file generated. It can be visualized with e.g. neato." << std::endl;
1053 int num_simplices = simplices.size();
1055 std::string mapp = point_cloud_name +
"_sc.txt";
1056 std::ofstream graphic(mapp);
1058 for (
int i = 0; i < num_simplices; i++)
1059 if (simplices[i].size() == 2)
1060 if (cover_color[simplices[i][0]].first > mask && cover_color[simplices[i][1]].first > mask) num_edges++;
1062 graphic << point_cloud_name << std::endl;
1063 graphic << cover_name << std::endl;
1064 graphic << color_name << std::endl;
1065 graphic << resolution_double <<
" " << gain << std::endl;
1066 graphic << cover_color.size() <<
" " << num_edges << std::endl;
1069 for (std::map<
int, std::pair<int, double> >::iterator iit = cover_color.begin(); iit != cover_color.end(); iit++) {
1070 graphic <<
id <<
" " << iit->second.second <<
" " << iit->second.first << std::endl;
1071 name2id[iit->first] = id;
1072 name2idinv[id] = iit->first;
1076 for (
int i = 0; i < num_simplices; i++)
1077 if (simplices[i].size() == 2)
1078 if (cover_color[simplices[i][0]].first > mask && cover_color[simplices[i][1]].first > mask)
1079 graphic << name2id[simplices[i][0]] <<
" " << name2id[simplices[i][1]] << std::endl;
1082 <<
" generated. It can be visualized with e.g. python KeplerMapperVisuFromTxtFile.py and firefox." 1092 assert(cover_name ==
"Voronoi");
1094 int m = voronoi_subsamples.size();
1097 std::vector<std::vector<int> > edges, faces;
1098 int numsimplices = simplices.size();
1100 std::string mapp = point_cloud_name +
"_sc.off";
1101 std::ofstream graphic(mapp);
1103 graphic <<
"OFF" << std::endl;
1104 for (
int i = 0; i < numsimplices; i++) {
1105 if (simplices[i].size() == 2) {
1107 edges.push_back(simplices[i]);
1109 if (simplices[i].size() == 3) {
1111 faces.push_back(simplices[i]);
1114 graphic << m <<
" " << numedges + numfaces << std::endl;
1115 for (
int i = 0; i < m; i++) {
1116 if (data_dimension <= 3) {
1117 for (
int j = 0; j < data_dimension; j++) graphic << point_cloud[voronoi_subsamples[i]][j] <<
" ";
1118 for (
int j = data_dimension; j < 3; j++) graphic << 0 <<
" ";
1119 graphic << std::endl;
1121 for (
int j = 0; j < 3; j++) graphic << point_cloud[voronoi_subsamples[i]][j] <<
" ";
1124 for (
int i = 0; i < numedges; i++) graphic << 2 <<
" " << edges[i][0] <<
" " << edges[i][1] << std::endl;
1125 for (
int i = 0; i < numfaces; i++)
1126 graphic << 3 <<
" " << faces[i][0] <<
" " << faces[i][1] <<
" " << faces[i][2] << std::endl;
1128 std::clog << mapp <<
" generated. It can be visualized with e.g. geomview." << std::endl;
1143 double maxf = std::numeric_limits<double>::lowest();
1144 double minf = (std::numeric_limits<double>::max)();
1145 for (std::map<int, double>::iterator it = cover_std.begin(); it != cover_std.end(); it++) {
1146 maxf = (std::max)(maxf, it->second);
1147 minf = (std::min)(minf, it->second);
1151 for (
auto const& simplex : simplices) {
1152 std::vector<int> splx = simplex;
1157 for (std::map<int, double>::iterator it = cover_std.begin(); it != cover_std.end(); it++) {
1158 int vertex = it->first;
float val = it->second;
1159 int vert[] = {vertex};
int edge[] = {vertex, -2};
1173 for (
int i = 0; i < max_dim; i++) {
1175 int num_bars = bars.size();
if(i == 0) num_bars -= 1;
1176 if(verbose) std::clog << num_bars <<
" interval(s) in dimension " << i <<
":" << std::endl;
1177 for (
int j = 0; j < num_bars; j++) {
1178 double birth = bars[j].first;
1179 double death = bars[j].second;
1180 if (i == 0 && std::isinf(death))
continue;
1182 birth = minf + (birth + 2) * (maxf - minf);
1184 birth = minf + (2 - birth) * (maxf - minf);
1186 death = minf + (death + 2) * (maxf - minf);
1188 death = minf + (2 - death) * (maxf - minf);
1189 PD.push_back(std::pair<double, double>(birth, death));
1190 if (verbose) std::clog <<
" [" << birth <<
", " << death <<
"]" << std::endl;
1203 unsigned int sz = distribution.size();
1205 for (
unsigned int i = 0; i < N - sz; i++) {
1206 if (verbose) std::clog <<
"Computing " << i <<
"th bootstrap, bottleneck distance = ";
1208 Cover_complex Cboot; Cboot.n = this->n; Cboot.data_dimension = this->data_dimension; Cboot.type = this->type; Cboot.functional_cover =
true;
1210 std::vector<int> boot(this->n);
1211 for (
int j = 0; j < this->n; j++) {
1212 double u = GetUniform();
1213 int id = std::floor(u * (this->n)); boot[j] = id;
1214 Cboot.point_cloud.push_back(this->point_cloud[
id]); Cboot.cover.emplace_back(); Cboot.func.push_back(this->func[
id]);
1215 boost::add_vertex(Cboot.one_skeleton_OFF); Cboot.vertices.push_back(boost::add_vertex(Cboot.one_skeleton));
1219 for (
int j = 0; j < n; j++) {
1220 std::vector<double> dist(n);
1221 for (
int k = 0; k < n; k++) dist[k] = distances[boot[j]][boot[k]];
1222 Cboot.distances.push_back(dist);
1232 if (verbose) std::clog << db << std::endl;
1233 distribution.push_back(db);
1236 std::sort(distribution.begin(), distribution.end());
1247 unsigned int N = distribution.size();
1248 double d = distribution[std::floor(alpha * N)];
1249 if (verbose) std::clog <<
"Distance corresponding to confidence " << alpha <<
" is " << d << std::endl;
1260 unsigned int N = distribution.size();
1262 for (
unsigned int i = 0; i < N; i++)
1263 if (distribution[i] >= d){ level = i * 1.0 / N;
break; }
1264 if (verbose) std::clog <<
"Confidence level of distance " << d <<
" is " << level << std::endl;
1274 double distancemin = (std::numeric_limits<double>::max)();
int N = PD.size();
1275 for (
int i = 0; i < N; i++) distancemin = (std::min)(distancemin, 0.5 * std::abs(PD[i].second - PD[i].first));
1277 if (verbose) std::clog <<
"p value = " << p_value << std::endl;
1291 template <
typename SimplicialComplex>
1293 unsigned int dimension = 0;
1294 for (
auto const& simplex : simplices) {
1295 int numvert = simplex.size();
1296 double filt = std::numeric_limits<double>::lowest();
1297 for (
int i = 0; i < numvert; i++) filt = (std::max)(cover_color[simplex[i]].second, filt);
1298 complex.insert_simplex_and_subfaces(simplex, filt);
1299 if (dimension < simplex.size() - 1) dimension = simplex.size() - 1;
1307 if (type !=
"Nerve" && type !=
"GIC") {
1308 std::cerr <<
"Type of complex needs to be specified." << std::endl;
1312 if (type ==
"Nerve") {
1313 for(
int i = 0; i < n; i++) simplices.push_back(cover[i]);
1314 std::sort(simplices.begin(), simplices.end());
1315 std::vector<std::vector<int> >::iterator it = std::unique(simplices.begin(), simplices.end());
1316 simplices.resize(std::distance(simplices.begin(), it));
1319 if (type ==
"GIC") {
1320 Index_map index = boost::get(boost::vertex_index, one_skeleton);
1322 if (functional_cover) {
1327 throw std::invalid_argument(
1328 "the output of this function is correct ONLY if the cover is minimal, i.e. the gain is less than 0.5.");
1331 boost::graph_traits<Graph>::edge_iterator ei, ei_end;
1332 for (boost::tie(ei, ei_end) = boost::edges(one_skeleton); ei != ei_end; ++ei) {
1333 int nums = cover[index[boost::source(*ei, one_skeleton)]].size();
1334 for (
int i = 0; i < nums; i++) {
1335 int vs = cover[index[boost::source(*ei, one_skeleton)]][i];
1336 int numt = cover[index[boost::target(*ei, one_skeleton)]].size();
1337 for (
int j = 0; j < numt; j++) {
1338 int vt = cover[index[boost::target(*ei, one_skeleton)]][j];
1339 if (cover_fct[vs] == cover_fct[vt] + 1 || cover_fct[vt] == cover_fct[vs] + 1) {
1340 std::vector<int> edge(2);
1341 edge[0] = (std::min)(vs, vt);
1342 edge[1] = (std::max)(vs, vt);
1343 simplices.push_back(edge);
1350 std::sort(simplices.begin(), simplices.end());
1351 std::vector<std::vector<int> >::iterator it = std::unique(simplices.begin(), simplices.end());
1352 simplices.resize(std::distance(simplices.begin(), it));
1357 boost::graph_traits<Graph>::edge_iterator ei, ei_end;
1358 for (boost::tie(ei, ei_end) = boost::edges(one_skeleton); ei != ei_end; ++ei)
1359 if (!(cover[index[boost::target(*ei, one_skeleton)]].size() == 1 &&
1360 cover[index[boost::target(*ei, one_skeleton)]] == cover[index[boost::source(*ei, one_skeleton)]])) {
1361 std::vector<int> edge(2);
1362 edge[0] = index[boost::source(*ei, one_skeleton)];
1363 edge[1] = index[boost::target(*ei, one_skeleton)];
1376 std::vector<int> simplx;
1378 unsigned int sz = cover[vertex].size();
1379 for (
unsigned int i = 0; i < sz; i++) {
1380 simplx.push_back(cover[vertex][i]);
1383 std::sort(simplx.begin(), simplx.end());
1384 std::vector<int>::iterator it = std::unique(simplx.begin(), simplx.end());
1385 simplx.resize(std::distance(simplx.begin(), it));
1386 simplices.push_back(simplx);
1389 std::sort(simplices.begin(), simplices.end());
1390 std::vector<std::vector<int> >::iterator it = std::unique(simplices.begin(), simplices.end());
1391 simplices.resize(std::distance(simplices.begin(), it));
void init_coefficients(int charac)
Initializes the coefficient field.
Definition: Persistent_cohomology.h:156
void plot_DOT()
Creates a .dot file called SC.dot for neato (part of the graphviz package) once the simplicial comple...
Definition: GIC.h:1004
bool read_point_cloud(const std::string &off_file_name)
Reads and stores the input point cloud from .(n)OFF file.
Definition: GIC.h:232
void expansion(int max_dim)
Expands the Simplex_tree containing only its one skeleton until dimension max_dim.
Definition: Simplex_tree.h:1141
void set_mask(int nodemask)
Sets the mask, which is a threshold integer such that nodes in the complex that contain a number of d...
Definition: GIC.h:207
void set_function_from_range(InputRange const &function)
Creates the function f from a vector stored in memory.
Definition: GIC.h:535
void set_graph_from_file(const std::string &graph_file_name)
Creates a graph G from a file containing the edges.
Definition: GIC.h:315
Computes the persistent cohomology of a filtered complex.
Definition: Persistent_cohomology.h:52
const std::vector< int > & subpopulation(int c)
Returns the data subset corresponding to a specific node of the created complex.
Definition: GIC.h:942
Simplex Tree data structure for representing simplicial complexes.
Definition: Simplex_tree.h:75
std::pair< Simplex_handle, bool > insert_simplex_and_subfaces(const InputVertexRange &Nsimplex, Filtration_value filtration=0)
Insert a N-simplex and all his subfaces, from a N-simplex represented by a range of Vertex_handles...
Definition: Simplex_tree.h:781
double compute_distance_from_confidence_level(double alpha)
Computes the bottleneck distance threshold corresponding to a specific confidence level...
Definition: GIC.h:1246
void set_type(const std::string &t)
Specifies whether the type of the output simplicial complex.
Definition: GIC.h:176
void find_simplices()
Computes the simplices of the simplicial complex.
Definition: GIC.h:1306
void write_info()
Creates a .txt file called SC.txt describing the 1-skeleton, which can then be plotted with e...
Definition: GIC.h:1052
static Simplex_handle null_simplex()
Returns a Simplex_handle different from all Simplex_handles associated to the simplices in the simpli...
Definition: Simplex_tree.h:530
Compute the Euclidean distance between two Points given by a range of coordinates. The points are assumed to have the same dimension.
Definition: distance_functions.h:34
void set_function_from_coordinate(int k)
Creates the function f from the k-th coordinate of the point cloud P.
Definition: GIC.h:514
Persistence_diagram compute_PD()
Computes the extended persistence diagram of the complex.
Definition: GIC.h:1139
Definition: SimplicialComplexForAlpha.h:14
Rips complex data structure.
Definition: Rips_complex.h:45
void set_color_from_coordinate(int k=0)
Computes the function used to color the nodes of the simplicial complex from the k-th coordinate...
Definition: GIC.h:975
void create_complex(SimplicialComplex &complex)
Creates the simplicial complex.
Definition: GIC.h:1292
void set_graph_from_OFF()
Creates a graph G from the triangulation given by the input .OFF file.
Definition: GIC.h:332
void set_function_from_file(const std::string &func_file_name)
Creates the function f from a file containing the function values.
Definition: GIC.h:493
void set_subsampling(double constant, double power)
Sets the constants used to subsample the data set. These constants are explained in ...
Definition: GIC.h:194
void set_cover_from_Voronoi(Distance distance, int m=100)
Creates the cover C from the Voronoï cells of a subsampling of the point cloud.
Definition: GIC.h:873
void set_color_from_range(std::vector< double > color)
Computes the function used to color the nodes of the simplicial complex from a vector stored in memor...
Definition: GIC.h:995
void set_verbose(bool verb=false)
Specifies whether the program should display information or not.
Definition: GIC.h:184
bool make_filtration_non_decreasing()
This function ensures that each simplex has a higher filtration value than its faces by increasing th...
Definition: Simplex_tree.h:1357
std::vector< std::pair< Filtration_value, Filtration_value > > intervals_in_dimension(int dimension)
Returns persistence intervals for a given dimension.
Definition: Persistent_cohomology.h:681
double compute_p_value()
Computes the p-value, i.e. the opposite of the confidence level of the largest bottleneck distance pr...
Definition: GIC.h:1273
Simplex_handle find(const InputVertexRange &s)
Given a range of Vertex_handles, returns the Simplex_handle of the simplex in the simplicial complex ...
Definition: Simplex_tree.h:615
Value type for a filtration function on a cell complex.
Definition: FiltrationValue.h:20
Complex_simplex_range complex_simplex_range()
Returns a range over the simplices of the simplicial complex.
Definition: Simplex_tree.h:228
Simplex_vertex_range simplex_vertex_range(Simplex_handle sh) const
Returns a range over the vertices of a simplex.
Definition: Simplex_tree.h:273
void assign_filtration(Simplex_handle sh, Filtration_value fv)
Sets the filtration value of a simplex.
Definition: Simplex_tree.h:520
void set_distances_from_range(const std::vector< std::vector< double > > &distance_matrix)
Reads and stores the distance matrices from vector stored in memory.
Definition: GIC.h:378
Global distance functions.
void set_graph_from_rips(double threshold, Distance distance)
Creates a graph G from a Rips complex.
Definition: GIC.h:348
bool has_children(SimplexHandle sh) const
Returns true if the node in the simplex tree pointed by sh has children.
Definition: Simplex_tree.h:602
void compute_distribution(unsigned int N=100)
Computes bootstrapped distances distribution.
Definition: GIC.h:1202
void plot_OFF()
Creates a .off file called SC.off for 3D visualization, which contains the 2-skeleton of the GIC...
Definition: GIC.h:1091
double bottleneck_distance(const Persistence_diagram1 &diag1, const Persistence_diagram2 &diag2, double e=(std::numeric_limits< double >::min)())
Function to compute the Bottleneck distance between two persistence diagrams.
Definition: Bottleneck.h:112
int dimension(Simplex_handle sh)
Returns the dimension of a simplex.
Definition: Simplex_tree.h:574
double set_graph_from_automatic_rips(Distance distance, int N=100)
Creates a graph G from a Rips complex whose threshold value is automatically tuned with subsampling—...
Definition: GIC.h:439
void set_color_from_file(const std::string &color_file_name)
Computes the function used to color the nodes of the simplicial complex from a file containing the fu...
Definition: GIC.h:955
double compute_confidence_level_from_distance(double d)
Computes the confidence level of a specific bottleneck distance threshold.
Definition: GIC.h:1259
void set_resolution_with_interval_number(int reso)
Sets a number of intervals from a value stored in memory.
Definition: GIC.h:599
void set_resolution_with_interval_length(double reso)
Sets a length of intervals from a value stored in memory.
Definition: GIC.h:593
void set_cover_from_file(const std::string &cover_file_name)
Creates the cover C from a file containing the cover elements of each point (the order has to be the ...
Definition: GIC.h:835
void set_cover_from_function()
Creates a cover C from the preimages of the function f.
Definition: GIC.h:611
Options::Filtration_value Filtration_value
Type for the value of the filtration function.
Definition: Simplex_tree.h:82
void compute_persistent_cohomology(Filtration_value min_interval_length=0)
Compute the persistent homology of the filtered simplicial complex.
Definition: Persistent_cohomology.h:172
Cover complex data structure.
Definition: GIC.h:87
double set_automatic_resolution()
Computes the optimal length of intervals (i.e. the smallest interval length avoiding discretization a...
Definition: GIC.h:552
Graph simplicial complex methods.
This file includes common file reader for GUDHI.
void set_gain(double g=0.3)
Sets a gain from a value stored in memory (default value 0.3).
Definition: GIC.h:605
void set_point_cloud_from_range(const std::vector< std::vector< double > > &point_cloud)
Reads and stores the input point cloud from vector stored in memory.
Definition: GIC.h:217