27 #include <tbb/parallel_for.h> 28 #include <tbb/mutex.h> 31 #include <gudhi/Debug_utils.h> 32 #include <gudhi/graph_simplicial_complex.h> 34 #include <gudhi/Simplex_tree.h> 35 #include <gudhi/Rips_complex.h> 36 #include <gudhi/Points_off_io.h> 38 #include <gudhi/Persistent_cohomology.h> 39 #include <gudhi/Bottleneck.h> 41 #include <boost/config.hpp> 42 #include <boost/graph/graph_traits.hpp> 43 #include <boost/graph/adjacency_list.hpp> 44 #include <boost/graph/connected_components.hpp> 45 #include <boost/graph/dijkstra_shortest_paths.hpp> 46 #include <boost/graph/subgraph.hpp> 47 #include <boost/graph/graph_utility.hpp> 62 namespace cover_complex {
67 using Persistence_diagram = std::vector<std::pair<double, double> >;
68 using Graph = boost::subgraph<
69 boost::adjacency_list<boost::setS, boost::vecS, boost::undirectedS, boost::no_property,
70 boost::property<boost::edge_index_t, int, boost::property<boost::edge_weight_t, double> > > >;
71 using Vertex_t = boost::graph_traits<Graph>::vertex_descriptor;
72 using Index_map = boost::property_map<Graph, boost::vertex_index_t>::type;
73 using Weight_map = boost::property_map<Graph, boost::edge_weight_t>::type;
95 template <
typename Po
int>
101 std::vector<Point> point_cloud;
102 std::vector<std::vector<double> > distances;
107 std::vector<double> func;
108 std::vector<double> func_color;
109 bool functional_cover =
false;
111 Graph one_skeleton_OFF;
113 std::vector<Vertex_t> vertices;
115 std::vector<std::vector<int> > simplices;
116 std::vector<int> voronoi_subsamples;
118 Persistence_diagram PD;
119 std::vector<double> distribution;
121 std::vector<std::vector<int> >
123 std::map<int, std::vector<int> >
125 std::map<int, double> cover_std;
129 std::map<int, std::pair<int, double> >
132 int resolution_int = -1;
133 double resolution_double = -1;
135 double rate_constant = 10;
136 double rate_power = 0.001;
139 std::map<int, int> name2id, name2idinv;
141 std::string cover_name;
142 std::string point_cloud_name;
143 std::string color_name;
146 void remove_edges(Graph& G) {
147 boost::graph_traits<Graph>::edge_iterator ei, ei_end;
148 for (boost::tie(ei, ei_end) = boost::edges(G); ei != ei_end; ++ei) boost::remove_edge(*ei, G);
153 #ifndef GUDHI_CAN_USE_CXX11_THREAD_LOCAL 154 std::default_random_engine re;
155 #endif // GUDHI_CAN_USE_CXX11_THREAD_LOCAL 158 double GetUniform() {
161 #ifdef GUDHI_CAN_USE_CXX11_THREAD_LOCAL 162 thread_local std::default_random_engine re;
163 #endif // GUDHI_CAN_USE_CXX11_THREAD_LOCAL 164 std::uniform_real_distribution<double> Dist(0, 1);
169 void SampleWithoutReplacement(
int populationSize,
int sampleSize, std::vector<int>& samples) {
173 while (m < sampleSize) {
175 if ((populationSize - t) * u >= sampleSize - m) {
214 rate_constant = constant;
235 point_cloud_name = off_file_name;
236 std::ifstream input(off_file_name);
240 while (comment ==
'#') {
241 std::getline(input, line);
242 if (!line.empty() && !all_of(line.begin(), line.end(), (int (*)(int))isspace))
243 comment = line[line.find_first_not_of(
' ')];
245 if (strcmp((
char*)line.c_str(),
"nOFF") == 0) {
247 while (comment ==
'#') {
248 std::getline(input, line);
249 if (!line.empty() && !all_of(line.begin(), line.end(), (int (*)(int))isspace))
250 comment = line[line.find_first_not_of(
' ')];
252 std::stringstream stream(line);
253 stream >> data_dimension;
259 int numedges, numfaces, i, dim;
260 while (comment ==
'#') {
261 std::getline(input, line);
262 if (!line.empty() && !all_of(line.begin(), line.end(), (int (*)(int))isspace))
263 comment = line[line.find_first_not_of(
' ')];
265 std::stringstream stream(line);
272 std::getline(input, line);
273 if (!line.empty() && line[line.find_first_not_of(
' ')] !=
'#' &&
274 !all_of(line.begin(), line.end(), (int (*)(int))isspace)) {
275 std::stringstream iss(line);
276 std::vector<double> point;
277 point.assign(std::istream_iterator<double>(iss), std::istream_iterator<double>());
278 point_cloud.emplace_back(point.begin(), point.begin() + data_dimension);
279 boost::add_vertex(one_skeleton_OFF);
280 vertices.push_back(boost::add_vertex(one_skeleton));
281 cover.emplace_back();
287 while (i < numfaces) {
288 std::getline(input, line);
289 if (!line.empty() && line[line.find_first_not_of(
' ')] !=
'#' &&
290 !all_of(line.begin(), line.end(), (int (*)(int))isspace)) {
291 std::vector<int> simplex;
292 std::stringstream iss(line);
293 simplex.assign(std::istream_iterator<int>(iss), std::istream_iterator<int>());
295 for (
int j = 1; j <= dim; j++)
296 for (
int k = j + 1; k <= dim; k++)
297 boost::add_edge(vertices[simplex[j]], vertices[simplex[k]], one_skeleton_OFF);
302 return input.is_open();
318 remove_edges(one_skeleton);
320 std::ifstream input(graph_file_name);
323 while (std::getline(input, line)) {
324 std::stringstream stream(line);
326 while (stream >> neighb) boost::add_edge(vertices[source], vertices[neighb], one_skeleton);
335 remove_edges(one_skeleton);
336 if (num_edges(one_skeleton_OFF))
337 one_skeleton = one_skeleton_OFF;
339 std::cout <<
"No triangulation read in OFF file!" << std::endl;
349 template <
typename Distance>
351 remove_edges(one_skeleton);
352 if (distances.size() == 0) compute_pairwise_distances(distance);
353 for (
int i = 0; i < n; i++) {
354 for (
int j = i + 1; j < n; j++) {
355 if (distances[i][j] <= threshold) {
356 boost::add_edge(vertices[i], vertices[j], one_skeleton);
357 boost::put(boost::edge_weight, one_skeleton, boost::edge(vertices[i], vertices[j], one_skeleton).first,
365 void set_graph_weights() {
366 Index_map index = boost::get(boost::vertex_index, one_skeleton);
367 Weight_map weight = boost::get(boost::edge_weight, one_skeleton);
368 boost::graph_traits<Graph>::edge_iterator ei, ei_end;
369 for (boost::tie(ei, ei_end) = boost::edges(one_skeleton); ei != ei_end; ++ei)
370 boost::put(weight, *ei,
371 distances[index[boost::source(*ei, one_skeleton)]][index[boost::target(*ei, one_skeleton)]]);
377 template <
typename Distance>
378 void compute_pairwise_distances(Distance ref_distance) {
380 std::vector<double> zeros(n);
381 for (
int i = 0; i < n; i++) distances.push_back(zeros);
382 std::string distance = point_cloud_name +
"_dist";
383 std::ifstream input(distance, std::ios::out | std::ios::binary);
386 if (verbose) std::cout <<
"Reading distances..." << std::endl;
387 for (
int i = 0; i < n; i++) {
388 for (
int j = i; j < n; j++) {
389 input.read((
char*)&d, 8);
396 if (verbose) std::cout <<
"Computing distances..." << std::endl;
398 std::ofstream output(distance, std::ios::out | std::ios::binary);
399 for (
int i = 0; i < n; i++) {
400 int state = (int)floor(100 * (i * 1.0 + 1) / n) % 10;
401 if (state == 0 && verbose) std::cout <<
"\r" << state <<
"%" << std::flush;
402 for (
int j = i; j < n; j++) {
403 double dis = ref_distance(point_cloud[i], point_cloud[j]);
404 distances[i][j] = dis;
405 distances[j][i] = dis;
406 output.write((
char*)&dis, 8);
410 if (verbose) std::cout << std::endl;
424 template <
typename Distance>
426 int m = floor(n / std::exp((1 + rate_power) * std::log(std::log(n) / std::log(rate_constant))));
427 m = std::min(m, n - 1);
430 if (verbose) std::cout << n <<
" points in R^" << data_dimension << std::endl;
431 if (verbose) std::cout <<
"Subsampling " << m <<
" points" << std::endl;
433 if (distances.size() == 0) compute_pairwise_distances(distance);
437 #if defined(GUDHI_USE_TBB) && defined(GUDHI_CAN_USE_CXX11_THREAD_LOCAL) 438 tbb::mutex deltamutex;
439 tbb::parallel_for(0, N, [&](
int i){
440 std::vector<int> samples(m);
441 SampleWithoutReplacement(n, m, samples);
442 double hausdorff_dist = 0;
443 for (
int j = 0; j < n; j++) {
444 double mj = distances[j][samples[0]];
445 for (
int k = 1; k < m; k++) mj = std::min(mj, distances[j][samples[k]]);
446 hausdorff_dist = std::max(hausdorff_dist, mj);
449 delta += hausdorff_dist / N;
453 for (
int i = 0; i < N; i++) {
454 std::vector<int> samples(m);
455 SampleWithoutReplacement(n, m, samples);
456 double hausdorff_dist = 0;
457 for (
int j = 0; j < n; j++) {
458 double mj = distances[j][samples[0]];
459 for (
int k = 1; k < m; k++) mj = std::min(mj, distances[j][samples[k]]);
460 hausdorff_dist = std::max(hausdorff_dist, mj);
462 delta += hausdorff_dist / N;
466 if (verbose) std::cout <<
"delta = " << delta << std::endl;
483 std::ifstream input(func_file_name);
486 while (std::getline(input, line)) {
487 std::stringstream stream(line);
492 functional_cover =
true;
493 cover_name = func_file_name;
503 for (
int i = 0; i < n; i++) func.push_back(point_cloud[i][k]);
504 functional_cover =
true;
505 cover_name =
"coordinate " + std::to_string(k);
514 template <
class InputRange>
516 for (
int i = 0; i < n; i++) func.push_back(
function[i]);
517 functional_cover =
true;
533 if (!functional_cover) {
534 std::cout <<
"Cover needs to come from the preimages of a function." << std::endl;
537 if (type !=
"Nerve" && type !=
"GIC") {
538 std::cout <<
"Type of complex needs to be specified." << std::endl;
543 Index_map index = boost::get(boost::vertex_index, one_skeleton);
546 boost::graph_traits<Graph>::edge_iterator ei, ei_end;
547 for (boost::tie(ei, ei_end) = boost::edges(one_skeleton); ei != ei_end; ++ei)
548 reso = std::max(reso, std::abs(func[index[boost::source(*ei, one_skeleton)]] -
549 func[index[boost::target(*ei, one_skeleton)]]));
550 if (verbose) std::cout <<
"resolution = " << reso << std::endl;
551 resolution_double = reso;
554 if (type ==
"Nerve") {
555 boost::graph_traits<Graph>::edge_iterator ei, ei_end;
556 for (boost::tie(ei, ei_end) = boost::edges(one_skeleton); ei != ei_end; ++ei)
557 reso = std::max(reso, std::abs(func[index[boost::source(*ei, one_skeleton)]] -
558 func[index[boost::target(*ei, one_skeleton)]]) /
560 if (verbose) std::cout <<
"resolution = " << reso << std::endl;
561 resolution_double = reso;
592 if (resolution_double == -1 && resolution_int == -1) {
593 std::cout <<
"Number and/or length of intervals not specified" << std::endl;
597 std::cout <<
"Gain not specified" << std::endl;
602 double minf = std::numeric_limits<float>::max();
603 double maxf = std::numeric_limits<float>::lowest();
604 for (
int i = 0; i < n; i++) {
605 minf = std::min(minf, func[i]);
606 maxf = std::max(maxf, func[i]);
608 if (verbose) std::cout <<
"Min function value = " << minf <<
" and Max function value = " << maxf << std::endl;
611 std::vector<std::pair<double, double> > intervals;
614 if (resolution_double == -1) {
615 double incr = (maxf - minf) / resolution_int;
617 double alpha = (incr * gain) / (2 - 2 * gain);
618 double y = minf + incr + alpha;
619 std::pair<double, double> interm(x, y);
620 intervals.push_back(interm);
621 for (
int i = 1; i < resolution_int - 1; i++) {
622 x = minf + i * incr - alpha;
623 y = minf + (i + 1) * incr + alpha;
624 std::pair<double, double> inter(x, y);
625 intervals.push_back(inter);
627 x = minf + (resolution_int - 1) * incr - alpha;
629 std::pair<double, double> interM(x, y);
630 intervals.push_back(interM);
631 res = intervals.size();
633 for (
int i = 0; i < res; i++)
634 std::cout <<
"Interval " << i <<
" = [" << intervals[i].first <<
", " << intervals[i].second <<
"]" 638 if (resolution_int == -1) {
640 double y = x + resolution_double;
641 while (y <= maxf && maxf - (y - gain * resolution_double) >= resolution_double) {
642 std::pair<double, double> inter(x, y);
643 intervals.push_back(inter);
644 x = y - gain * resolution_double;
645 y = x + resolution_double;
647 std::pair<double, double> interM(x, maxf);
648 intervals.push_back(interM);
649 res = intervals.size();
651 for (
int i = 0; i < res; i++)
652 std::cout <<
"Interval " << i <<
" = [" << intervals[i].first <<
", " << intervals[i].second <<
"]" 657 double y = x + resolution_double;
659 while (count < resolution_int && y <= maxf && maxf - (y - gain * resolution_double) >= resolution_double) {
660 std::pair<double, double> inter(x, y);
661 intervals.push_back(inter);
663 x = y - gain * resolution_double;
664 y = x + resolution_double;
666 res = intervals.size();
668 for (
int i = 0; i < res; i++)
669 std::cout <<
"Interval " << i <<
" = [" << intervals[i].first <<
", " << intervals[i].second <<
"]" 676 std::vector<int> points(n);
677 for (
int i = 0; i < n; i++) points[i] = i;
678 std::sort(points.begin(), points.end(), [=](
const int & p1,
const int & p2){
return (this->func[p1] < this->func[p2]);});
682 Index_map index = boost::get(boost::vertex_index, one_skeleton);
683 std::map<int, std::vector<int> > preimages;
684 std::map<int, double> funcstd;
686 if (verbose) std::cout <<
"Computing preimages..." << std::endl;
687 for (
int i = 0; i < res; i++) {
689 std::pair<double, double> inter1 = intervals[i];
695 std::pair<double, double> inter3 = intervals[i - 1];
696 while (func[points[tmp]] < inter3.second && tmp != n) {
697 preimages[i].push_back(points[tmp]);
705 std::pair<double, double> inter2 = intervals[i + 1];
706 while (func[points[tmp]] < inter2.first && tmp != n) {
707 preimages[i].push_back(points[tmp]);
712 while (func[points[tmp]] < inter1.second && tmp != n) {
713 preimages[i].push_back(points[tmp]);
718 std::pair<double, double> inter3 = intervals[i - 1];
719 while (func[points[tmp]] < inter3.second && tmp != n) {
720 preimages[i].push_back(points[tmp]);
724 preimages[i].push_back(points[tmp]);
731 funcstd[i] = 0.5 * (u + v);
735 if (verbose) std::cout <<
"Computing connected components (parallelized)..." << std::endl;
736 tbb::mutex covermutex, idmutex;
737 tbb::parallel_for(0, res, [&](
int i){
739 Graph G = one_skeleton.create_subgraph();
740 int num = preimages[i].size();
741 std::vector<int> component(num);
742 for (
int j = 0; j < num; j++) boost::add_vertex(index[vertices[preimages[i][j]]], G);
743 boost::connected_components(G, &component[0]);
747 for (
int j = 0; j < num; j++) {
749 if (component[j] > max) max = component[j];
752 int identifier = ((i + component[j])*(i + component[j]) + 3 * i + component[j]) / 2;
756 cover[preimages[i][j]].push_back(identifier);
757 cover_back[identifier].push_back(preimages[i][j]);
758 cover_fct[identifier] = i;
759 cover_std[identifier] = funcstd[i];
760 cover_color[identifier].second += func_color[preimages[i][j]];
761 cover_color[identifier].first += 1;
771 if (verbose) std::cout <<
"Computing connected components..." << std::endl;
772 for (
int i = 0; i < res; i++) {
774 Graph G = one_skeleton.create_subgraph();
775 int num = preimages[i].size();
776 std::vector<int> component(num);
777 for (
int j = 0; j < num; j++) boost::add_vertex(index[vertices[preimages[i][j]]], G);
778 boost::connected_components(G, &component[0]);
782 for (
int j = 0; j < num; j++) {
784 if (component[j] > max) max = component[j];
787 int identifier = (std::pow(i + component[j], 2) + 3 * i + component[j]) / 2;
790 cover[preimages[i][j]].push_back(identifier);
791 cover_back[identifier].push_back(preimages[i][j]);
792 cover_fct[identifier] = i;
793 cover_std[identifier] = funcstd[i];
794 cover_color[identifier].second += func_color[preimages[i][j]];
795 cover_color[identifier].first += 1;
803 maximal_dim =
id - 1;
804 for (std::map<
int, std::pair<int, double> >::iterator iit = cover_color.begin(); iit != cover_color.end(); iit++)
805 iit->second.second /= iit->second.first;
818 std::vector<int> cov_elts, cov_number;
819 std::ifstream input(cover_file_name);
821 while (std::getline(input, line)) {
823 std::stringstream stream(line);
824 while (stream >> cov) {
825 cov_elts.push_back(cov);
826 cov_number.push_back(cov);
827 cover_fct[cov] = cov;
828 cover_color[cov].second += func_color[i];
829 cover_color[cov].first++;
830 cover_back[cov].push_back(i);
836 std::sort(cov_number.begin(), cov_number.end());
837 std::vector<int>::iterator it = std::unique(cov_number.begin(), cov_number.end());
838 cov_number.resize(std::distance(cov_number.begin(), it));
840 maximal_dim = cov_number.size() - 1;
841 for (
int i = 0; i <= maximal_dim; i++) cover_color[i].second /= cover_color[i].first;
842 cover_name = cover_file_name;
852 template <
typename Distance>
854 voronoi_subsamples.resize(m);
855 SampleWithoutReplacement(n, m, voronoi_subsamples);
856 if (distances.size() == 0) compute_pairwise_distances(distance);
858 Weight_map weight = boost::get(boost::edge_weight, one_skeleton);
859 Index_map index = boost::get(boost::vertex_index, one_skeleton);
860 std::vector<double> mindist(n);
861 for (
int j = 0; j < n; j++) mindist[j] = std::numeric_limits<double>::max();
865 if (verbose) std::cout <<
"Computing geodesic distances (parallelized)..." << std::endl;
866 tbb::mutex coverMutex; tbb::mutex mindistMutex;
867 tbb::parallel_for(0, m, [&](
int i){
868 int seed = voronoi_subsamples[i];
869 std::vector<double> dmap(n);
870 boost::dijkstra_shortest_paths(
871 one_skeleton, vertices[seed],
872 boost::weight_map(weight).distance_map(boost::make_iterator_property_map(dmap.begin(), index)));
874 coverMutex.lock(); mindistMutex.lock();
875 for (
int j = 0; j < n; j++)
876 if (mindist[j] > dmap[j]) {
877 mindist[j] = dmap[j];
878 if (cover[j].size() == 0)
879 cover[j].push_back(i);
883 coverMutex.unlock(); mindistMutex.unlock();
886 for (
int i = 0; i < m; i++) {
887 if (verbose) std::cout <<
"Computing geodesic distances to seed " << i <<
"..." << std::endl;
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 for (
int j = 0; j < n; j++)
895 if (mindist[j] > dmap[j]) {
896 mindist[j] = dmap[j];
897 if (cover[j].size() == 0)
898 cover[j].push_back(i);
905 for (
int i = 0; i < n; i++) {
906 cover_back[cover[i][0]].push_back(i);
907 cover_color[cover[i][0]].second += func_color[i];
908 cover_color[cover[i][0]].first++;
910 for (
int i = 0; i < m; i++) cover_color[i].second /= cover_color[i].first;
912 cover_name =
"Voronoi";
922 const std::vector<int>&
subpopulation(
int c) {
return cover_back[name2idinv[c]]; }
937 std::ifstream input(color_file_name);
940 while (std::getline(input, line)) {
941 std::stringstream stream(line);
943 func_color.push_back(f);
946 color_name = color_file_name;
956 for (
int i = 0; i < n; i++) func_color.push_back(point_cloud[i][k]);
957 color_name =
"coordinate ";
958 color_name.append(std::to_string(k));
968 for (
unsigned int i = 0; i < color.size(); i++) func_color.push_back(color[i]);
977 std::string mapp = point_cloud_name +
"_sc.dot";
978 std::ofstream graphic(mapp);
980 double maxv = std::numeric_limits<double>::lowest();
981 double minv = std::numeric_limits<double>::max();
982 for (std::map<
int, std::pair<int, double> >::iterator iit = cover_color.begin(); iit != cover_color.end(); iit++) {
983 maxv = std::max(maxv, iit->second.second);
984 minv = std::min(minv, iit->second.second);
988 std::vector<int> nodes;
991 graphic <<
"graph GIC {" << std::endl;
993 for (std::map<
int, std::pair<int, double> >::iterator iit = cover_color.begin(); iit != cover_color.end(); iit++) {
994 if (iit->second.first > mask) {
995 nodes.push_back(iit->first);
996 name2id[iit->first] = id;
997 name2idinv[id] = iit->first;
999 graphic << name2id[iit->first] <<
"[shape=circle fontcolor=black color=black label=\"" << name2id[iit->first]
1000 <<
":" << iit->second.first <<
"\" style=filled fillcolor=\"" 1001 << (1 - (maxv - iit->second.second) / (maxv - minv)) * 0.6 <<
", 1, 1\"]" << std::endl;
1006 int num_simplices = simplices.size();
1007 for (
int i = 0; i < num_simplices; i++)
1008 if (simplices[i].size() == 2) {
1009 if (cover_color[simplices[i][0]].first > mask && cover_color[simplices[i][1]].first > mask) {
1010 graphic <<
" " << name2id[simplices[i][0]] <<
" -- " << name2id[simplices[i][1]] <<
" [weight=15];" 1017 std::cout << mapp <<
" file generated. It can be visualized with e.g. neato." << std::endl;
1025 int num_simplices = simplices.size();
1027 std::string mapp = point_cloud_name +
"_sc.txt";
1028 std::ofstream graphic(mapp);
1030 for (
int i = 0; i < num_simplices; i++)
1031 if (simplices[i].size() == 2)
1032 if (cover_color[simplices[i][0]].first > mask && cover_color[simplices[i][1]].first > mask) num_edges++;
1034 graphic << point_cloud_name << std::endl;
1035 graphic << cover_name << std::endl;
1036 graphic << color_name << std::endl;
1037 graphic << resolution_double <<
" " << gain << std::endl;
1038 graphic << cover_color.size() <<
" " << num_edges << std::endl;
1041 for (std::map<
int, std::pair<int, double> >::iterator iit = cover_color.begin(); iit != cover_color.end(); iit++) {
1042 graphic <<
id <<
" " << iit->second.second <<
" " << iit->second.first << std::endl;
1043 name2id[iit->first] = id;
1044 name2idinv[id] = iit->first;
1048 for (
int i = 0; i < num_simplices; i++)
1049 if (simplices[i].size() == 2)
1050 if (cover_color[simplices[i][0]].first > mask && cover_color[simplices[i][1]].first > mask)
1051 graphic << name2id[simplices[i][0]] <<
" " << name2id[simplices[i][1]] << std::endl;
1054 <<
" generated. It can be visualized with e.g. python KeplerMapperVisuFromTxtFile.py and firefox." 1064 assert(cover_name ==
"Voronoi");
1066 int m = voronoi_subsamples.size();
1069 std::vector<std::vector<int> > edges, faces;
1070 int numsimplices = simplices.size();
1072 std::string mapp = point_cloud_name +
"_sc.off";
1073 std::ofstream graphic(mapp);
1075 graphic <<
"OFF" << std::endl;
1076 for (
int i = 0; i < numsimplices; i++) {
1077 if (simplices[i].size() == 2) {
1079 edges.push_back(simplices[i]);
1081 if (simplices[i].size() == 3) {
1083 faces.push_back(simplices[i]);
1086 graphic << m <<
" " << numedges + numfaces << std::endl;
1087 for (
int i = 0; i < m; i++) {
1088 if (data_dimension <= 3) {
1089 for (
int j = 0; j < data_dimension; j++) graphic << point_cloud[voronoi_subsamples[i]][j] <<
" ";
1090 for (
int j = data_dimension; j < 3; j++) graphic << 0 <<
" ";
1091 graphic << std::endl;
1093 for (
int j = 0; j < 3; j++) graphic << point_cloud[voronoi_subsamples[i]][j] <<
" ";
1096 for (
int i = 0; i < numedges; i++) graphic << 2 <<
" " << edges[i][0] <<
" " << edges[i][1] << std::endl;
1097 for (
int i = 0; i < numfaces; i++)
1098 graphic << 3 <<
" " << faces[i][0] <<
" " << faces[i][1] <<
" " << faces[i][2] << std::endl;
1100 std::cout << mapp <<
" generated. It can be visualized with e.g. geomview." << std::endl;
1115 double maxf = std::numeric_limits<double>::lowest();
1116 double minf = std::numeric_limits<double>::max();
1117 for (std::map<int, double>::iterator it = cover_std.begin(); it != cover_std.end(); it++) {
1118 maxf = std::max(maxf, it->second);
1119 minf = std::min(minf, it->second);
1123 for (
auto const& simplex : simplices) {
1124 std::vector<int> splx = simplex; splx.push_back(-2);
1128 for (std::map<int, double>::iterator it = cover_std.begin(); it != cover_std.end(); it++) {
1129 int vertex = it->first;
float val = it->second;
1130 int vert[] = {vertex};
int edge[] = {vertex, -2};
1142 for (
int i = 0; i < max_dim; i++) {
1144 int num_bars = bars.size();
if(i == 0) num_bars -= 1;
1145 if(verbose) std::cout << num_bars <<
" interval(s) in dimension " << i <<
":" << std::endl;
1146 for (
int j = 0; j < num_bars; j++) {
1147 double birth = bars[j].first;
1148 double death = bars[j].second;
1149 if (i == 0 && std::isinf(death))
continue;
1151 birth = minf + (birth + 2) * (maxf - minf);
1153 birth = minf + (2 - birth) * (maxf - minf);
1155 death = minf + (death + 2) * (maxf - minf);
1157 death = minf + (2 - death) * (maxf - minf);
1158 PD.push_back(std::pair<double, double>(birth, death));
1159 if (verbose) std::cout <<
" [" << birth <<
", " << death <<
"]" << std::endl;
1171 unsigned int sz = distribution.size();
1173 std::cout <<
"Already done!" << std::endl;
1175 for (
unsigned int i = 0; i < N - sz; i++) {
1176 if (verbose) std::cout <<
"Computing " << i <<
"th bootstrap, bottleneck distance = ";
1178 Cover_complex Cboot; Cboot.n = this->n; Cboot.data_dimension = this->data_dimension; Cboot.type = this->type; Cboot.functional_cover =
true;
1180 std::vector<int> boot(this->n);
1181 for (
int j = 0; j < this->n; j++) {
1182 double u = GetUniform();
1183 int id = std::floor(u * (this->n)); boot[j] = id;
1184 Cboot.point_cloud.push_back(this->point_cloud[
id]); Cboot.cover.emplace_back(); Cboot.func.push_back(this->func[
id]);
1185 boost::add_vertex(Cboot.one_skeleton_OFF); Cboot.vertices.push_back(boost::add_vertex(Cboot.one_skeleton));
1189 for (
int j = 0; j < n; j++) {
1190 std::vector<double> dist(n);
1191 for (
int k = 0; k < n; k++) dist[k] = distances[boot[j]][boot[k]];
1192 Cboot.distances.push_back(dist);
1202 if (verbose) std::cout << db << std::endl;
1203 distribution.push_back(db);
1206 std::sort(distribution.begin(), distribution.end());
1217 unsigned int N = distribution.size();
1218 double d = distribution[std::floor(alpha * N)];
1219 if (verbose) std::cout <<
"Distance corresponding to confidence " << alpha <<
" is " << d << std::endl;
1230 unsigned int N = distribution.size();
1232 for (
unsigned int i = 0; i < N; i++)
1233 if (distribution[i] > d){ level = i * 1.0 / N;
break; }
1234 if (verbose) std::cout <<
"Confidence level of distance " << d <<
" is " << level << std::endl;
1244 double distancemin = std::numeric_limits<double>::max();
int N = PD.size();
1245 for (
int i = 0; i < N; i++) distancemin = std::min(distancemin, 0.5 * std::abs(PD[i].second - PD[i].first));
1247 if (verbose) std::cout <<
"p value = " << p_value << std::endl;
1261 template <
typename SimplicialComplex>
1263 unsigned int dimension = 0;
1264 for (
auto const& simplex : simplices) {
1265 int numvert = simplex.size();
1266 double filt = std::numeric_limits<double>::lowest();
1267 for (
int i = 0; i < numvert; i++) filt = std::max(cover_color[simplex[i]].second, filt);
1268 complex.insert_simplex_and_subfaces(simplex, filt);
1269 if (dimension < simplex.size() - 1) dimension = simplex.size() - 1;
1277 if (type !=
"Nerve" && type !=
"GIC") {
1278 std::cout <<
"Type of complex needs to be specified." << std::endl;
1282 if (type ==
"Nerve") {
1283 for(
int i = 0; i < n; i++) simplices.push_back(cover[i]);
1284 std::sort(simplices.begin(), simplices.end());
1285 std::vector<std::vector<int> >::iterator it = std::unique(simplices.begin(), simplices.end());
1286 simplices.resize(std::distance(simplices.begin(), it));
1289 if (type ==
"GIC") {
1290 Index_map index = boost::get(boost::vertex_index, one_skeleton);
1292 if (functional_cover) {
1297 throw std::invalid_argument(
1298 "the output of this function is correct ONLY if the cover is minimal, i.e. the gain is less than 0.5.");
1301 boost::graph_traits<Graph>::edge_iterator ei, ei_end;
1302 for (boost::tie(ei, ei_end) = boost::edges(one_skeleton); ei != ei_end; ++ei) {
1303 int nums = cover[index[boost::source(*ei, one_skeleton)]].size();
1304 for (
int i = 0; i < nums; i++) {
1305 int vs = cover[index[boost::source(*ei, one_skeleton)]][i];
1306 int numt = cover[index[boost::target(*ei, one_skeleton)]].size();
1307 for (
int j = 0; j < numt; j++) {
1308 int vt = cover[index[boost::target(*ei, one_skeleton)]][j];
1309 if (cover_fct[vs] == cover_fct[vt] + 1 || cover_fct[vt] == cover_fct[vs] + 1) {
1310 std::vector<int> edge(2);
1311 edge[0] = std::min(vs, vt);
1312 edge[1] = std::max(vs, vt);
1313 simplices.push_back(edge);
1320 std::sort(simplices.begin(), simplices.end());
1321 std::vector<std::vector<int> >::iterator it = std::unique(simplices.begin(), simplices.end());
1322 simplices.resize(std::distance(simplices.begin(), it));
1327 boost::graph_traits<Graph>::edge_iterator ei, ei_end;
1328 for (boost::tie(ei, ei_end) = boost::edges(one_skeleton); ei != ei_end; ++ei)
1329 if (!(cover[index[boost::target(*ei, one_skeleton)]].size() == 1 &&
1330 cover[index[boost::target(*ei, one_skeleton)]] == cover[index[boost::source(*ei, one_skeleton)]])) {
1331 std::vector<int> edge(2);
1332 edge[0] = index[boost::source(*ei, one_skeleton)];
1333 edge[1] = index[boost::target(*ei, one_skeleton)];
1346 std::vector<int> simplx;
1348 unsigned int sz = cover[vertex].size();
1349 for (
unsigned int i = 0; i < sz; i++) {
1350 simplx.push_back(cover[vertex][i]);
1353 std::sort(simplx.begin(), simplx.end());
1354 std::vector<int>::iterator it = std::unique(simplx.begin(), simplx.end());
1355 simplx.resize(std::distance(simplx.begin(), it));
1356 simplices.push_back(simplx);
1359 std::sort(simplices.begin(), simplices.end());
1360 std::vector<std::vector<int> >::iterator it = std::unique(simplices.begin(), simplices.end());
1361 simplices.resize(std::distance(simplices.begin(), it));
void init_coefficients(int charac)
Initializes the coefficient field.
Definition: Persistent_cohomology.h:168
void set_color_from_vector(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:967
void plot_DOT()
Creates a .dot file called SC.dot for neato (part of the graphviz package) once the simplicial comple...
Definition: GIC.h:976
bool read_point_cloud(const std::string &off_file_name)
Reads and stores the input point cloud.
Definition: GIC.h:234
void expansion(int max_dim)
Expands the Simplex_tree containing only its one skeleton until dimension max_dim.
Definition: Simplex_tree.h:1037
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:226
void set_function_from_range(InputRange const &function)
Creates the function f from a vector stored in memory.
Definition: GIC.h:515
void set_graph_from_file(const std::string &graph_file_name)
Creates a graph G from a file containing the edges.
Definition: GIC.h:317
Computes the persistent cohomology of a filtered complex.
Definition: Persistent_cohomology.h:64
const std::vector< int > & subpopulation(int c)
Returns the data subset corresponding to a specific node of the created complex.
Definition: GIC.h:922
Simplex Tree data structure for representing simplicial complexes.
Definition: Simplex_tree.h:72
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:683
double compute_distance_from_confidence_level(double alpha)
Computes the bottleneck distance threshold corresponding to a specific confidence level...
Definition: GIC.h:1216
void set_type(const std::string &t)
Specifies whether the type of the output simplicial complex.
Definition: GIC.h:195
void find_simplices()
Computes the simplices of the simplicial complex.
Definition: GIC.h:1276
void write_info()
Creates a .txt file called SC.txt describing the 1-skeleton, which can then be plotted with e...
Definition: GIC.h:1024
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:46
void set_function_from_coordinate(int k)
Creates the function f from the k-th coordinate of the point cloud P.
Definition: GIC.h:502
Definition: SimplicialComplexForAlpha.h:26
Rips complex data structure.
Definition: Rips_complex.h:57
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:955
void create_complex(SimplicialComplex &complex)
Creates the simplicial complex.
Definition: GIC.h:1262
void set_graph_from_OFF()
Creates a graph G from the triangulation given by the input .OFF file.
Definition: GIC.h:334
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:481
void set_subsampling(double constant, double power)
Sets the constants used to subsample the data set. These constants are explained in ...
Definition: GIC.h:213
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:853
void set_verbose(bool verb=false)
Specifies whether the program should display information or not.
Definition: GIC.h:203
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:1255
std::vector< std::pair< Filtration_value, Filtration_value > > intervals_in_dimension(int dimension)
Returns persistence intervals for a given dimension.
Definition: Persistent_cohomology.h:706
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:1243
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:517
Value type for a filtration function on a cell complex.
Definition: FiltrationValue.h:32
Complex_simplex_range complex_simplex_range()
Returns a range over the simplices of the simplicial complex.
Definition: Simplex_tree.h:214
void assign_filtration(Simplex_handle sh, Filtration_value fv)
Sets the filtration value of a simplex.
Definition: Simplex_tree.h:421
Simplex_vertex_range simplex_vertex_range(Simplex_handle sh)
Returns a range over the vertices of a simplex.
Definition: Simplex_tree.h:261
Global distance functions.
void set_graph_from_rips(double threshold, Distance distance)
Creates a graph G from a Rips complex.
Definition: GIC.h:350
bool has_children(SimplexHandle sh) const
Returns true if the node in the simplex tree pointed by sh has children.
Definition: Simplex_tree.h:504
void compute_distribution(unsigned int N=100)
Computes bootstrapped distances distribution.
Definition: GIC.h:1170
void plot_OFF()
Creates a .off file called SC.off for 3D visualization, which contains the 2-skeleton of the GIC...
Definition: GIC.h:1063
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:111
int dimension(Simplex_handle sh)
Returns the dimension of a simplex.
Definition: Simplex_tree.h:476
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:425
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:935
double compute_confidence_level_from_distance(double d)
Computes the confidence level of a specific bottleneck distance threshold.
Definition: GIC.h:1229
void set_resolution_with_interval_number(int reso)
Sets a number of intervals from a value stored in memory.
Definition: GIC.h:579
void set_resolution_with_interval_length(double reso)
Sets a length of intervals from a value stored in memory.
Definition: GIC.h:573
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:815
void set_cover_from_function()
Creates a cover C from the preimages of the function f.
Definition: GIC.h:591
Options::Filtration_value Filtration_value
Type for the value of the filtration function.
Definition: Simplex_tree.h:79
void compute_persistent_cohomology(Filtration_value min_interval_length=0)
Compute the persistent homology of the filtered simplicial complex.
Definition: Persistent_cohomology.h:184
Cover complex data structure.
Definition: GIC.h:96
void compute_PD()
Computes the extended persistence diagram of the complex.
Definition: GIC.h:1111
double set_automatic_resolution()
Computes the optimal length of intervals (i.e. the smallest interval length avoiding discretization a...
Definition: GIC.h:532
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:585