16 #include <tbb/parallel_for.h> 17 #include <tbb/mutex.h> 20 #include <gudhi/Debug_utils.h> 21 #include <gudhi/graph_simplicial_complex.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> 52 #if CGAL_VERSION_NR < 1041101000 53 # error Alpha_complex_3d is only available for CGAL >= 4.11 58 namespace cover_complex {
63 using Persistence_diagram = std::vector<std::pair<double, double> >;
64 using Graph = boost::subgraph<
65 boost::adjacency_list<boost::setS, boost::vecS, boost::undirectedS, boost::no_property,
66 boost::property<boost::edge_index_t, int, boost::property<boost::edge_weight_t, double> > > >;
67 using Vertex_t = boost::graph_traits<Graph>::vertex_descriptor;
68 using Index_map = boost::property_map<Graph, boost::vertex_index_t>::type;
69 using Weight_map = boost::property_map<Graph, boost::edge_weight_t>::type;
91 template <
typename Po
int>
97 std::vector<Point> point_cloud;
98 std::vector<std::vector<double> > distances;
103 std::vector<double> func;
104 std::vector<double> func_color;
105 bool functional_cover =
false;
107 Graph one_skeleton_OFF;
109 std::vector<Vertex_t> vertices;
111 std::vector<std::vector<int> > simplices;
112 std::vector<int> voronoi_subsamples;
114 Persistence_diagram PD;
115 std::vector<double> distribution;
117 std::vector<std::vector<int> >
119 std::map<int, std::vector<int> >
121 std::map<int, double> cover_std;
125 std::map<int, std::pair<int, double> >
128 int resolution_int = -1;
129 double resolution_double = -1;
131 double rate_constant = 10;
132 double rate_power = 0.001;
135 std::map<int, int> name2id, name2idinv;
137 std::string cover_name;
138 std::string point_cloud_name;
139 std::string color_name;
142 void remove_edges(Graph& G) {
143 boost::graph_traits<Graph>::edge_iterator ei, ei_end;
144 for (boost::tie(ei, ei_end) = boost::edges(G); ei != ei_end; ++ei) boost::remove_edge(*ei, G);
149 #ifndef GUDHI_CAN_USE_CXX11_THREAD_LOCAL 150 std::default_random_engine re;
151 #endif // GUDHI_CAN_USE_CXX11_THREAD_LOCAL 154 double GetUniform() {
157 #ifdef GUDHI_CAN_USE_CXX11_THREAD_LOCAL 158 thread_local std::default_random_engine re;
159 #endif // GUDHI_CAN_USE_CXX11_THREAD_LOCAL 160 std::uniform_real_distribution<double> Dist(0, 1);
165 void SampleWithoutReplacement(
int populationSize,
int sampleSize, std::vector<int>& samples) {
169 while (m < sampleSize) {
171 if ((populationSize - t) * u >= sampleSize - m) {
210 rate_constant = constant;
233 n = point_cloud.size(); data_dimension = point_cloud[0].size();
234 point_cloud_name =
"matrix"; cover.resize(n);
235 for(
int i = 0; i < n; i++){
236 boost::add_vertex(one_skeleton_OFF);
237 vertices.push_back(boost::add_vertex(one_skeleton));
239 this->point_cloud = point_cloud;
248 point_cloud_name = off_file_name;
249 std::ifstream input(off_file_name);
253 while (comment ==
'#') {
254 std::getline(input, line);
255 if (!line.empty() && !all_of(line.begin(), line.end(), (int (*)(int))isspace))
256 comment = line[line.find_first_not_of(
' ')];
258 if (strcmp((
char*)line.c_str(),
"nOFF") == 0) {
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);
266 stream >> data_dimension;
272 int numedges, numfaces, i, dim;
273 while (comment ==
'#') {
274 std::getline(input, line);
275 if (!line.empty() && !all_of(line.begin(), line.end(), (int (*)(int))isspace))
276 comment = line[line.find_first_not_of(
' ')];
278 std::stringstream stream(line);
285 std::getline(input, line);
286 if (!line.empty() && line[line.find_first_not_of(
' ')] !=
'#' &&
287 !all_of(line.begin(), line.end(), (int (*)(int))isspace)) {
288 std::stringstream iss(line);
289 std::vector<double> point;
290 point.assign(std::istream_iterator<double>(iss), std::istream_iterator<double>());
291 point_cloud.emplace_back(point.begin(), point.begin() + data_dimension);
292 boost::add_vertex(one_skeleton_OFF);
293 vertices.push_back(boost::add_vertex(one_skeleton));
294 cover.emplace_back();
300 while (i < numfaces) {
301 std::getline(input, line);
302 if (!line.empty() && line[line.find_first_not_of(
' ')] !=
'#' &&
303 !all_of(line.begin(), line.end(), (int (*)(int))isspace)) {
304 std::vector<int> simplex;
305 std::stringstream iss(line);
306 simplex.assign(std::istream_iterator<int>(iss), std::istream_iterator<int>());
308 for (
int j = 1; j <= dim; j++)
309 for (
int k = j + 1; k <= dim; k++)
310 boost::add_edge(vertices[simplex[j]], vertices[simplex[k]], one_skeleton_OFF);
315 return input.is_open();
331 remove_edges(one_skeleton);
333 std::ifstream input(graph_file_name);
336 while (std::getline(input, line)) {
337 std::stringstream stream(line);
339 while (stream >> neighb) boost::add_edge(vertices[source], vertices[neighb], one_skeleton);
348 remove_edges(one_skeleton);
349 if (num_edges(one_skeleton_OFF))
350 one_skeleton = one_skeleton_OFF;
352 std::cout <<
"No triangulation read in OFF file!" << std::endl;
362 template <
typename Distance>
364 remove_edges(one_skeleton);
365 if (distances.size() == 0) compute_pairwise_distances(distance);
366 for (
int i = 0; i < n; i++) {
367 for (
int j = i + 1; j < n; j++) {
368 if (distances[i][j] <= threshold) {
369 boost::add_edge(vertices[i], vertices[j], one_skeleton);
370 boost::put(boost::edge_weight, one_skeleton, boost::edge(vertices[i], vertices[j], one_skeleton).first,
378 void set_graph_weights() {
379 Index_map index = boost::get(boost::vertex_index, one_skeleton);
380 Weight_map weight = boost::get(boost::edge_weight, one_skeleton);
381 boost::graph_traits<Graph>::edge_iterator ei, ei_end;
382 for (boost::tie(ei, ei_end) = boost::edges(one_skeleton); ei != ei_end; ++ei)
383 boost::put(weight, *ei,
384 distances[index[boost::source(*ei, one_skeleton)]][index[boost::target(*ei, one_skeleton)]]);
394 n = distance_matrix.size(); data_dimension = 0; point_cloud_name =
"matrix";
395 cover.resize(n); point_cloud.resize(n);
396 for(
int i = 0; i < n; i++){
397 boost::add_vertex(one_skeleton_OFF);
398 vertices.push_back(boost::add_vertex(one_skeleton));
400 distances = distance_matrix;
406 template <
typename Distance>
407 void compute_pairwise_distances(Distance ref_distance) {
409 std::vector<double> zeros(n);
410 for (
int i = 0; i < n; i++) distances.push_back(zeros);
411 std::string distance = point_cloud_name +
"_dist";
412 std::ifstream input(distance, std::ios::out | std::ios::binary);
415 if (verbose) std::cout <<
"Reading distances..." << std::endl;
416 for (
int i = 0; i < n; i++) {
417 for (
int j = i; j < n; j++) {
418 input.read((
char*)&d, 8);
425 if (verbose) std::cout <<
"Computing distances..." << std::endl;
427 std::ofstream output(distance, std::ios::out | std::ios::binary);
428 for (
int i = 0; i < n; i++) {
429 int state = (int)floor(100 * (i * 1.0 + 1) / n) % 10;
430 if (state == 0 && verbose) std::cout <<
"\r" << state <<
"%" << std::flush;
431 for (
int j = i; j < n; j++) {
432 double dis = ref_distance(point_cloud[i], point_cloud[j]);
433 distances[i][j] = dis;
434 distances[j][i] = dis;
435 output.write((
char*)&dis, 8);
439 if (verbose) std::cout << std::endl;
453 template <
typename Distance>
455 int m = floor(n / std::exp((1 + rate_power) * std::log(std::log(n) / std::log(rate_constant))));
456 m = (std::min)(m, n - 1);
459 if (verbose) std::cout << n <<
" points in R^" << data_dimension << std::endl;
460 if (verbose) std::cout <<
"Subsampling " << m <<
" points" << std::endl;
462 if (distances.size() == 0) compute_pairwise_distances(distance);
466 #if defined(GUDHI_USE_TBB) && defined(GUDHI_CAN_USE_CXX11_THREAD_LOCAL) 467 tbb::mutex deltamutex;
468 tbb::parallel_for(0, N, [&](
int i){
469 std::vector<int> samples(m);
470 SampleWithoutReplacement(n, m, samples);
471 double hausdorff_dist = 0;
472 for (
int j = 0; j < n; j++) {
473 double mj = distances[j][samples[0]];
474 for (
int k = 1; k < m; k++) mj = (std::min)(mj, distances[j][samples[k]]);
475 hausdorff_dist = (std::max)(hausdorff_dist, mj);
478 delta += hausdorff_dist / N;
482 for (
int i = 0; i < N; i++) {
483 std::vector<int> samples(m);
484 SampleWithoutReplacement(n, m, samples);
485 double hausdorff_dist = 0;
486 for (
int j = 0; j < n; j++) {
487 double mj = distances[j][samples[0]];
488 for (
int k = 1; k < m; k++) mj = (std::min)(mj, distances[j][samples[k]]);
489 hausdorff_dist = (std::max)(hausdorff_dist, mj);
491 delta += hausdorff_dist / N;
495 if (verbose) std::cout <<
"delta = " << delta << std::endl;
512 std::ifstream input(func_file_name);
515 while (std::getline(input, line)) {
516 std::stringstream stream(line);
521 functional_cover =
true;
522 cover_name = func_file_name;
532 if(point_cloud[0].size() > 0){
533 for (
int i = 0; i < n; i++) func.push_back(point_cloud[i][k]);
534 functional_cover =
true;
535 cover_name =
"coordinate " + std::to_string(k);
538 std::cout <<
"Only pairwise distances provided---cannot access " << k <<
"th coordinate; returning null vector instead" << std::endl;
539 for (
int i = 0; i < n; i++) func.push_back(0.0);
540 functional_cover =
true;
551 template <
class InputRange>
553 for (
int i = 0; i < n; i++) func.push_back(
function[i]);
554 functional_cover =
true;
570 if (!functional_cover) {
571 std::cout <<
"Cover needs to come from the preimages of a function." << std::endl;
574 if (type !=
"Nerve" && type !=
"GIC") {
575 std::cout <<
"Type of complex needs to be specified." << std::endl;
580 Index_map index = boost::get(boost::vertex_index, one_skeleton);
583 boost::graph_traits<Graph>::edge_iterator ei, ei_end;
584 for (boost::tie(ei, ei_end) = boost::edges(one_skeleton); ei != ei_end; ++ei)
585 reso = (std::max)(reso, std::abs(func[index[boost::source(*ei, one_skeleton)]] -
586 func[index[boost::target(*ei, one_skeleton)]]));
587 if (verbose) std::cout <<
"resolution = " << reso << std::endl;
588 resolution_double = reso;
591 if (type ==
"Nerve") {
592 boost::graph_traits<Graph>::edge_iterator ei, ei_end;
593 for (boost::tie(ei, ei_end) = boost::edges(one_skeleton); ei != ei_end; ++ei)
594 reso = (std::max)(reso, std::abs(func[index[boost::source(*ei, one_skeleton)]] -
595 func[index[boost::target(*ei, one_skeleton)]]) /
597 if (verbose) std::cout <<
"resolution = " << reso << std::endl;
598 resolution_double = reso;
629 if (resolution_double == -1 && resolution_int == -1) {
630 std::cout <<
"Number and/or length of intervals not specified" << std::endl;
634 std::cout <<
"Gain not specified" << std::endl;
639 double minf = (std::numeric_limits<float>::max)();
640 double maxf = std::numeric_limits<float>::lowest();
641 for (
int i = 0; i < n; i++) {
642 minf = (std::min)(minf, func[i]);
643 maxf = (std::max)(maxf, func[i]);
645 if (verbose) std::cout <<
"Min function value = " << minf <<
" and Max function value = " << maxf << std::endl;
648 std::vector<std::pair<double, double> > intervals;
651 if (resolution_double == -1) {
652 double incr = (maxf - minf) / resolution_int;
654 double alpha = (incr * gain) / (2 - 2 * gain);
655 double y = minf + incr + alpha;
656 std::pair<double, double> interm(x, y);
657 intervals.push_back(interm);
658 for (
int i = 1; i < resolution_int - 1; i++) {
659 x = minf + i * incr - alpha;
660 y = minf + (i + 1) * incr + alpha;
661 std::pair<double, double> inter(x, y);
662 intervals.push_back(inter);
664 x = minf + (resolution_int - 1) * incr - alpha;
666 std::pair<double, double> interM(x, y);
667 intervals.push_back(interM);
668 res = intervals.size();
670 for (
int i = 0; i < res; i++)
671 std::cout <<
"Interval " << i <<
" = [" << intervals[i].first <<
", " << intervals[i].second <<
"]" 675 if (resolution_int == -1) {
677 double y = x + resolution_double;
678 while (y <= maxf && maxf - (y - gain * resolution_double) >= resolution_double) {
679 std::pair<double, double> inter(x, y);
680 intervals.push_back(inter);
681 x = y - gain * resolution_double;
682 y = x + resolution_double;
684 std::pair<double, double> interM(x, maxf);
685 intervals.push_back(interM);
686 res = intervals.size();
688 for (
int i = 0; i < res; i++)
689 std::cout <<
"Interval " << i <<
" = [" << intervals[i].first <<
", " << intervals[i].second <<
"]" 694 double y = x + resolution_double;
696 while (count < resolution_int && y <= maxf && maxf - (y - gain * resolution_double) >= resolution_double) {
697 std::pair<double, double> inter(x, y);
698 intervals.push_back(inter);
700 x = y - gain * resolution_double;
701 y = x + resolution_double;
703 res = intervals.size();
705 for (
int i = 0; i < res; i++)
706 std::cout <<
"Interval " << i <<
" = [" << intervals[i].first <<
", " << intervals[i].second <<
"]" 713 std::vector<int> points(n);
714 for (
int i = 0; i < n; i++) points[i] = i;
715 std::sort(points.begin(), points.end(), [=](
const int & p1,
const int & p2){
return (this->func[p1] < this->func[p2]);});
719 Index_map index = boost::get(boost::vertex_index, one_skeleton);
720 std::map<int, std::vector<int> > preimages;
721 std::map<int, double> funcstd;
723 if (verbose) std::cout <<
"Computing preimages..." << std::endl;
724 for (
int i = 0; i < res; i++) {
726 std::pair<double, double> inter1 = intervals[i];
732 std::pair<double, double> inter3 = intervals[i - 1];
733 while (func[points[tmp]] < inter3.second && tmp != n) {
734 preimages[i].push_back(points[tmp]);
742 std::pair<double, double> inter2 = intervals[i + 1];
743 while (func[points[tmp]] < inter2.first && tmp != n) {
744 preimages[i].push_back(points[tmp]);
749 while (func[points[tmp]] < inter1.second && tmp != n) {
750 preimages[i].push_back(points[tmp]);
755 std::pair<double, double> inter3 = intervals[i - 1];
756 while (func[points[tmp]] < inter3.second && tmp != n) {
757 preimages[i].push_back(points[tmp]);
761 preimages[i].push_back(points[tmp]);
768 funcstd[i] = 0.5 * (u + v);
772 if (verbose) std::cout <<
"Computing connected components (parallelized)..." << std::endl;
773 tbb::mutex covermutex, idmutex;
774 tbb::parallel_for(0, res, [&](
int i){
776 Graph G = one_skeleton.create_subgraph();
777 int num = preimages[i].size();
778 std::vector<int> component(num);
779 for (
int j = 0; j < num; j++) boost::add_vertex(index[vertices[preimages[i][j]]], G);
780 boost::connected_components(G, &component[0]);
784 for (
int j = 0; j < num; j++) {
786 if (component[j] > max) max = component[j];
789 int identifier = ((i + component[j])*(i + component[j]) + 3 * i + component[j]) / 2;
793 cover[preimages[i][j]].push_back(identifier);
794 cover_back[identifier].push_back(preimages[i][j]);
795 cover_fct[identifier] = i;
796 cover_std[identifier] = funcstd[i];
797 cover_color[identifier].second += func_color[preimages[i][j]];
798 cover_color[identifier].first += 1;
808 if (verbose) std::cout <<
"Computing connected components..." << std::endl;
809 for (
int i = 0; i < res; i++) {
811 Graph G = one_skeleton.create_subgraph();
812 int num = preimages[i].size();
813 std::vector<int> component(num);
814 for (
int j = 0; j < num; j++) boost::add_vertex(index[vertices[preimages[i][j]]], G);
815 boost::connected_components(G, &component[0]);
819 for (
int j = 0; j < num; j++) {
821 if (component[j] > max) max = component[j];
824 int identifier = (std::pow(i + component[j], 2) + 3 * i + component[j]) / 2;
827 cover[preimages[i][j]].push_back(identifier);
828 cover_back[identifier].push_back(preimages[i][j]);
829 cover_fct[identifier] = i;
830 cover_std[identifier] = funcstd[i];
831 cover_color[identifier].second += func_color[preimages[i][j]];
832 cover_color[identifier].first += 1;
840 maximal_dim =
id - 1;
841 for (std::map<
int, std::pair<int, double> >::iterator iit = cover_color.begin(); iit != cover_color.end(); iit++)
842 iit->second.second /= iit->second.first;
855 std::vector<int> cov_elts, cov_number;
856 std::ifstream input(cover_file_name);
858 while (std::getline(input, line)) {
860 std::stringstream stream(line);
861 while (stream >> cov) {
862 cov_elts.push_back(cov);
863 cov_number.push_back(cov);
864 cover_fct[cov] = cov;
865 cover_color[cov].second += func_color[i];
866 cover_color[cov].first++;
867 cover_back[cov].push_back(i);
873 std::sort(cov_number.begin(), cov_number.end());
874 std::vector<int>::iterator it = std::unique(cov_number.begin(), cov_number.end());
875 cov_number.resize(std::distance(cov_number.begin(), it));
877 maximal_dim = cov_number.size() - 1;
878 for (
int i = 0; i <= maximal_dim; i++) cover_color[i].second /= cover_color[i].first;
879 cover_name = cover_file_name;
889 template <
typename Distance>
891 voronoi_subsamples.resize(m);
892 SampleWithoutReplacement(n, m, voronoi_subsamples);
893 if (distances.size() == 0) compute_pairwise_distances(distance);
895 Weight_map weight = boost::get(boost::edge_weight, one_skeleton);
896 Index_map index = boost::get(boost::vertex_index, one_skeleton);
897 std::vector<double> mindist(n);
898 for (
int j = 0; j < n; j++) mindist[j] = (std::numeric_limits<double>::max)();
902 if (verbose) std::cout <<
"Computing geodesic distances (parallelized)..." << std::endl;
903 tbb::mutex coverMutex; tbb::mutex mindistMutex;
904 tbb::parallel_for(0, m, [&](
int i){
905 int seed = voronoi_subsamples[i];
906 std::vector<double> dmap(n);
907 boost::dijkstra_shortest_paths(
908 one_skeleton, vertices[seed],
909 boost::weight_map(weight).distance_map(boost::make_iterator_property_map(dmap.begin(), index)));
911 coverMutex.lock(); mindistMutex.lock();
912 for (
int j = 0; j < n; j++)
913 if (mindist[j] > dmap[j]) {
914 mindist[j] = dmap[j];
915 if (cover[j].size() == 0)
916 cover[j].push_back(i);
920 coverMutex.unlock(); mindistMutex.unlock();
923 for (
int i = 0; i < m; i++) {
924 if (verbose) std::cout <<
"Computing geodesic distances to seed " << i <<
"..." << std::endl;
925 int seed = voronoi_subsamples[i];
926 std::vector<double> dmap(n);
927 boost::dijkstra_shortest_paths(
928 one_skeleton, vertices[seed],
929 boost::weight_map(weight).distance_map(boost::make_iterator_property_map(dmap.begin(), index)));
931 for (
int j = 0; j < n; j++)
932 if (mindist[j] > dmap[j]) {
933 mindist[j] = dmap[j];
934 if (cover[j].size() == 0)
935 cover[j].push_back(i);
942 for (
int i = 0; i < n; i++) {
943 cover_back[cover[i][0]].push_back(i);
944 cover_color[cover[i][0]].second += func_color[i];
945 cover_color[cover[i][0]].first++;
947 for (
int i = 0; i < m; i++) cover_color[i].second /= cover_color[i].first;
949 cover_name =
"Voronoi";
959 const std::vector<int>&
subpopulation(
int c) {
return cover_back[name2idinv[c]]; }
974 std::ifstream input(color_file_name);
977 while (std::getline(input, line)) {
978 std::stringstream stream(line);
980 func_color.push_back(f);
983 color_name = color_file_name;
993 if(point_cloud[0].size() > 0){
994 for (
int i = 0; i < n; i++) func_color.push_back(point_cloud[i][k]);
995 color_name =
"coordinate ";
996 color_name.append(std::to_string(k));
999 std::cout <<
"Only pairwise distances provided---cannot access " << k <<
"th coordinate; returning null vector instead" << std::endl;
1000 for (
int i = 0; i < n; i++) func.push_back(0.0);
1001 functional_cover =
true;
1002 cover_name =
"null";
1013 for (
unsigned int i = 0; i < color.size(); i++) func_color.push_back(color[i]);
1022 std::string mapp = point_cloud_name +
"_sc.dot";
1023 std::ofstream graphic(mapp);
1025 double maxv = std::numeric_limits<double>::lowest();
1026 double minv = (std::numeric_limits<double>::max)();
1027 for (std::map<
int, std::pair<int, double> >::iterator iit = cover_color.begin(); iit != cover_color.end(); iit++) {
1028 maxv = (std::max)(maxv, iit->second.second);
1029 minv = (std::min)(minv, iit->second.second);
1033 std::vector<int> nodes;
1036 graphic <<
"graph GIC {" << std::endl;
1038 for (std::map<
int, std::pair<int, double> >::iterator iit = cover_color.begin(); iit != cover_color.end(); iit++) {
1039 if (iit->second.first > mask) {
1040 nodes.push_back(iit->first);
1041 name2id[iit->first] = id;
1042 name2idinv[id] = iit->first;
1044 graphic << name2id[iit->first] <<
"[shape=circle fontcolor=black color=black label=\"" << name2id[iit->first]
1045 <<
":" << iit->second.first <<
"\" style=filled fillcolor=\"" 1046 << (1 - (maxv - iit->second.second) / (maxv - minv)) * 0.6 <<
", 1, 1\"]" << std::endl;
1051 int num_simplices = simplices.size();
1052 for (
int i = 0; i < num_simplices; i++)
1053 if (simplices[i].size() == 2) {
1054 if (cover_color[simplices[i][0]].first > mask && cover_color[simplices[i][1]].first > mask) {
1055 graphic <<
" " << name2id[simplices[i][0]] <<
" -- " << name2id[simplices[i][1]] <<
" [weight=15];" 1062 std::cout << mapp <<
" file generated. It can be visualized with e.g. neato." << std::endl;
1070 int num_simplices = simplices.size();
1072 std::string mapp = point_cloud_name +
"_sc.txt";
1073 std::ofstream graphic(mapp);
1075 for (
int i = 0; i < num_simplices; i++)
1076 if (simplices[i].size() == 2)
1077 if (cover_color[simplices[i][0]].first > mask && cover_color[simplices[i][1]].first > mask) num_edges++;
1079 graphic << point_cloud_name << std::endl;
1080 graphic << cover_name << std::endl;
1081 graphic << color_name << std::endl;
1082 graphic << resolution_double <<
" " << gain << std::endl;
1083 graphic << cover_color.size() <<
" " << num_edges << std::endl;
1086 for (std::map<
int, std::pair<int, double> >::iterator iit = cover_color.begin(); iit != cover_color.end(); iit++) {
1087 graphic <<
id <<
" " << iit->second.second <<
" " << iit->second.first << std::endl;
1088 name2id[iit->first] = id;
1089 name2idinv[id] = iit->first;
1093 for (
int i = 0; i < num_simplices; i++)
1094 if (simplices[i].size() == 2)
1095 if (cover_color[simplices[i][0]].first > mask && cover_color[simplices[i][1]].first > mask)
1096 graphic << name2id[simplices[i][0]] <<
" " << name2id[simplices[i][1]] << std::endl;
1099 <<
" generated. It can be visualized with e.g. python KeplerMapperVisuFromTxtFile.py and firefox." 1109 assert(cover_name ==
"Voronoi");
1111 int m = voronoi_subsamples.size();
1114 std::vector<std::vector<int> > edges, faces;
1115 int numsimplices = simplices.size();
1117 std::string mapp = point_cloud_name +
"_sc.off";
1118 std::ofstream graphic(mapp);
1120 graphic <<
"OFF" << std::endl;
1121 for (
int i = 0; i < numsimplices; i++) {
1122 if (simplices[i].size() == 2) {
1124 edges.push_back(simplices[i]);
1126 if (simplices[i].size() == 3) {
1128 faces.push_back(simplices[i]);
1131 graphic << m <<
" " << numedges + numfaces << std::endl;
1132 for (
int i = 0; i < m; i++) {
1133 if (data_dimension <= 3) {
1134 for (
int j = 0; j < data_dimension; j++) graphic << point_cloud[voronoi_subsamples[i]][j] <<
" ";
1135 for (
int j = data_dimension; j < 3; j++) graphic << 0 <<
" ";
1136 graphic << std::endl;
1138 for (
int j = 0; j < 3; j++) graphic << point_cloud[voronoi_subsamples[i]][j] <<
" ";
1141 for (
int i = 0; i < numedges; i++) graphic << 2 <<
" " << edges[i][0] <<
" " << edges[i][1] << std::endl;
1142 for (
int i = 0; i < numfaces; i++)
1143 graphic << 3 <<
" " << faces[i][0] <<
" " << faces[i][1] <<
" " << faces[i][2] << std::endl;
1145 std::cout << mapp <<
" generated. It can be visualized with e.g. geomview." << std::endl;
1160 double maxf = std::numeric_limits<double>::lowest();
1161 double minf = (std::numeric_limits<double>::max)();
1162 for (std::map<int, double>::iterator it = cover_std.begin(); it != cover_std.end(); it++) {
1163 maxf = (std::max)(maxf, it->second);
1164 minf = (std::min)(minf, it->second);
1168 for (
auto const& simplex : simplices) {
1169 std::vector<int> splx = simplex;
1174 for (std::map<int, double>::iterator it = cover_std.begin(); it != cover_std.end(); it++) {
1175 int vertex = it->first;
float val = it->second;
1176 int vert[] = {vertex};
int edge[] = {vertex, -2};
1190 for (
int i = 0; i < max_dim; i++) {
1192 int num_bars = bars.size();
if(i == 0) num_bars -= 1;
1193 if(verbose) std::cout << num_bars <<
" interval(s) in dimension " << i <<
":" << std::endl;
1194 for (
int j = 0; j < num_bars; j++) {
1195 double birth = bars[j].first;
1196 double death = bars[j].second;
1197 if (i == 0 && std::isinf(death))
continue;
1199 birth = minf + (birth + 2) * (maxf - minf);
1201 birth = minf + (2 - birth) * (maxf - minf);
1203 death = minf + (death + 2) * (maxf - minf);
1205 death = minf + (2 - death) * (maxf - minf);
1206 PD.push_back(std::pair<double, double>(birth, death));
1207 if (verbose) std::cout <<
" [" << birth <<
", " << death <<
"]" << std::endl;
1220 unsigned int sz = distribution.size();
1222 std::cout <<
"Already done!" << std::endl;
1224 for (
unsigned int i = 0; i < N - sz; i++) {
1225 if (verbose) std::cout <<
"Computing " << i <<
"th bootstrap, bottleneck distance = ";
1227 Cover_complex Cboot; Cboot.n = this->n; Cboot.data_dimension = this->data_dimension; Cboot.type = this->type; Cboot.functional_cover =
true;
1229 std::vector<int> boot(this->n);
1230 for (
int j = 0; j < this->n; j++) {
1231 double u = GetUniform();
1232 int id = std::floor(u * (this->n)); boot[j] = id;
1233 Cboot.point_cloud.push_back(this->point_cloud[
id]); Cboot.cover.emplace_back(); Cboot.func.push_back(this->func[
id]);
1234 boost::add_vertex(Cboot.one_skeleton_OFF); Cboot.vertices.push_back(boost::add_vertex(Cboot.one_skeleton));
1238 for (
int j = 0; j < n; j++) {
1239 std::vector<double> dist(n);
1240 for (
int k = 0; k < n; k++) dist[k] = distances[boot[j]][boot[k]];
1241 Cboot.distances.push_back(dist);
1251 if (verbose) std::cout << db << std::endl;
1252 distribution.push_back(db);
1255 std::sort(distribution.begin(), distribution.end());
1266 unsigned int N = distribution.size();
1267 double d = distribution[std::floor(alpha * N)];
1268 if (verbose) std::cout <<
"Distance corresponding to confidence " << alpha <<
" is " << d << std::endl;
1279 unsigned int N = distribution.size();
1281 for (
unsigned int i = 0; i < N; i++)
1282 if (distribution[i] >= d){ level = i * 1.0 / N;
break; }
1283 if (verbose) std::cout <<
"Confidence level of distance " << d <<
" is " << level << std::endl;
1293 double distancemin = (std::numeric_limits<double>::max)();
int N = PD.size();
1294 for (
int i = 0; i < N; i++) distancemin = (std::min)(distancemin, 0.5 * std::abs(PD[i].second - PD[i].first));
1296 if (verbose) std::cout <<
"p value = " << p_value << std::endl;
1310 template <
typename SimplicialComplex>
1312 unsigned int dimension = 0;
1313 for (
auto const& simplex : simplices) {
1314 int numvert = simplex.size();
1315 double filt = std::numeric_limits<double>::lowest();
1316 for (
int i = 0; i < numvert; i++) filt = (std::max)(cover_color[simplex[i]].second, filt);
1317 complex.insert_simplex_and_subfaces(simplex, filt);
1318 if (dimension < simplex.size() - 1) dimension = simplex.size() - 1;
1326 if (type !=
"Nerve" && type !=
"GIC") {
1327 std::cout <<
"Type of complex needs to be specified." << std::endl;
1331 if (type ==
"Nerve") {
1332 for(
int i = 0; i < n; i++) simplices.push_back(cover[i]);
1333 std::sort(simplices.begin(), simplices.end());
1334 std::vector<std::vector<int> >::iterator it = std::unique(simplices.begin(), simplices.end());
1335 simplices.resize(std::distance(simplices.begin(), it));
1338 if (type ==
"GIC") {
1339 Index_map index = boost::get(boost::vertex_index, one_skeleton);
1341 if (functional_cover) {
1346 throw std::invalid_argument(
1347 "the output of this function is correct ONLY if the cover is minimal, i.e. the gain is less than 0.5.");
1350 boost::graph_traits<Graph>::edge_iterator ei, ei_end;
1351 for (boost::tie(ei, ei_end) = boost::edges(one_skeleton); ei != ei_end; ++ei) {
1352 int nums = cover[index[boost::source(*ei, one_skeleton)]].size();
1353 for (
int i = 0; i < nums; i++) {
1354 int vs = cover[index[boost::source(*ei, one_skeleton)]][i];
1355 int numt = cover[index[boost::target(*ei, one_skeleton)]].size();
1356 for (
int j = 0; j < numt; j++) {
1357 int vt = cover[index[boost::target(*ei, one_skeleton)]][j];
1358 if (cover_fct[vs] == cover_fct[vt] + 1 || cover_fct[vt] == cover_fct[vs] + 1) {
1359 std::vector<int> edge(2);
1360 edge[0] = (std::min)(vs, vt);
1361 edge[1] = (std::max)(vs, vt);
1362 simplices.push_back(edge);
1369 std::sort(simplices.begin(), simplices.end());
1370 std::vector<std::vector<int> >::iterator it = std::unique(simplices.begin(), simplices.end());
1371 simplices.resize(std::distance(simplices.begin(), it));
1376 boost::graph_traits<Graph>::edge_iterator ei, ei_end;
1377 for (boost::tie(ei, ei_end) = boost::edges(one_skeleton); ei != ei_end; ++ei)
1378 if (!(cover[index[boost::target(*ei, one_skeleton)]].size() == 1 &&
1379 cover[index[boost::target(*ei, one_skeleton)]] == cover[index[boost::source(*ei, one_skeleton)]])) {
1380 std::vector<int> edge(2);
1381 edge[0] = index[boost::source(*ei, one_skeleton)];
1382 edge[1] = index[boost::target(*ei, one_skeleton)];
1395 std::vector<int> simplx;
1397 unsigned int sz = cover[vertex].size();
1398 for (
unsigned int i = 0; i < sz; i++) {
1399 simplx.push_back(cover[vertex][i]);
1402 std::sort(simplx.begin(), simplx.end());
1403 std::vector<int>::iterator it = std::unique(simplx.begin(), simplx.end());
1404 simplx.resize(std::distance(simplx.begin(), it));
1405 simplices.push_back(simplx);
1408 std::sort(simplices.begin(), simplices.end());
1409 std::vector<std::vector<int> >::iterator it = std::unique(simplices.begin(), simplices.end());
1410 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:1021
bool read_point_cloud(const std::string &off_file_name)
Reads and stores the input point cloud from .(n)OFF file.
Definition: GIC.h:247
void expansion(int max_dim)
Expands the Simplex_tree containing only its one skeleton until dimension max_dim.
Definition: Simplex_tree.h:1102
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:222
void set_function_from_range(InputRange const &function)
Creates the function f from a vector stored in memory.
Definition: GIC.h:552
void set_graph_from_file(const std::string &graph_file_name)
Creates a graph G from a file containing the edges.
Definition: GIC.h:330
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:959
Simplex Tree data structure for representing simplicial complexes.
Definition: Simplex_tree.h:60
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:750
double compute_distance_from_confidence_level(double alpha)
Computes the bottleneck distance threshold corresponding to a specific confidence level...
Definition: GIC.h:1265
void set_type(const std::string &t)
Specifies whether the type of the output simplicial complex.
Definition: GIC.h:191
void find_simplices()
Computes the simplices of the simplicial complex.
Definition: GIC.h:1325
void write_info()
Creates a .txt file called SC.txt describing the 1-skeleton, which can then be plotted with e...
Definition: GIC.h:1069
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:498
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:531
Persistence_diagram compute_PD()
Computes the extended persistence diagram of the complex.
Definition: GIC.h:1156
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:992
void create_complex(SimplicialComplex &complex)
Creates the simplicial complex.
Definition: GIC.h:1311
void set_graph_from_OFF()
Creates a graph G from the triangulation given by the input .OFF file.
Definition: GIC.h:347
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:510
void set_subsampling(double constant, double power)
Sets the constants used to subsample the data set. These constants are explained in ...
Definition: GIC.h:209
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:890
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:1012
void set_verbose(bool verb=false)
Specifies whether the program should display information or not.
Definition: GIC.h:199
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:1321
std::vector< std::pair< Filtration_value, Filtration_value > > intervals_in_dimension(int dimension)
Returns persistence intervals for a given dimension.
Definition: Persistent_cohomology.h:693
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:1292
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:584
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:202
void assign_filtration(Simplex_handle sh, Filtration_value fv)
Sets the filtration value of a simplex.
Definition: Simplex_tree.h:488
Simplex_vertex_range simplex_vertex_range(Simplex_handle sh)
Returns a range over the vertices of a simplex.
Definition: Simplex_tree.h:249
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:393
Global distance functions.
void set_graph_from_rips(double threshold, Distance distance)
Creates a graph G from a Rips complex.
Definition: GIC.h:363
bool has_children(SimplexHandle sh) const
Returns true if the node in the simplex tree pointed by sh has children.
Definition: Simplex_tree.h:571
void compute_distribution(unsigned int N=100)
Computes bootstrapped distances distribution.
Definition: GIC.h:1219
void plot_OFF()
Creates a .off file called SC.off for 3D visualization, which contains the 2-skeleton of the GIC...
Definition: GIC.h:1108
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:543
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:454
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:972
double compute_confidence_level_from_distance(double d)
Computes the confidence level of a specific bottleneck distance threshold.
Definition: GIC.h:1278
void set_resolution_with_interval_number(int reso)
Sets a number of intervals from a value stored in memory.
Definition: GIC.h:616
void set_resolution_with_interval_length(double reso)
Sets a length of intervals from a value stored in memory.
Definition: GIC.h:610
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:852
void set_cover_from_function()
Creates a cover C from the preimages of the function f.
Definition: GIC.h:628
Options::Filtration_value Filtration_value
Type for the value of the filtration function.
Definition: Simplex_tree.h:67
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:92
double set_automatic_resolution()
Computes the optimal length of intervals (i.e. the smallest interval length avoiding discretization a...
Definition: GIC.h:569
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:622
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:232