16#include <tbb/parallel_for.h>
20#if __has_include(<CGAL/version.h>)
21# define GUDHI_GIC_USE_CGAL 1
22# include <gudhi/Bottleneck.h>
23#elif __has_include(<hera/bottleneck.h>)
24# define GUDHI_GIC_USE_HERA 1
25# include <hera/bottleneck.h>
28#include <gudhi/Debug_utils.h>
31#include <gudhi/Simplex_tree.h>
32#include <gudhi/Rips_complex.h>
33#include <gudhi/Points_off_io.h>
35#include <gudhi/Persistent_cohomology.h>
37#include <boost/config.hpp>
38#include <boost/graph/graph_traits.hpp>
39#include <boost/graph/adjacency_list.hpp>
40#include <boost/graph/connected_components.hpp>
41#include <boost/graph/dijkstra_shortest_paths.hpp>
42#include <boost/graph/subgraph.hpp>
43#include <boost/graph/graph_utility.hpp>
58namespace cover_complex {
63using Persistence_diagram = std::vector<std::pair<double, double> >;
64using 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> > > >;
67using Vertex_t = boost::graph_traits<Graph>::vertex_descriptor;
68using Index_map = boost::property_map<Graph, boost::vertex_index_t>::type;
69using Weight_map = boost::property_map<Graph, boost::edge_weight_t>::type;
91template <
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);
148 double GetUniform() {
149 thread_local std::default_random_engine re;
150 std::uniform_real_distribution<double> Dist(0, 1);
155 void SampleWithoutReplacement(
int populationSize,
int sampleSize, std::vector<int>& samples) {
159 while (m < sampleSize) {
161 if ((populationSize - t) * u >= sampleSize - m) {
200 rate_constant = constant;
223 n = point_cloud.size(); data_dimension = point_cloud[0].size();
224 point_cloud_name =
"matrix"; cover.resize(n);
225 for(
int i = 0; i < n; i++){
226 boost::add_vertex(one_skeleton_OFF);
227 vertices.push_back(boost::add_vertex(one_skeleton));
229 this->point_cloud = point_cloud;
238 point_cloud_name = off_file_name;
239 std::ifstream input(off_file_name);
243 while (comment ==
'#') {
244 std::getline(input, line);
245 if (!line.empty() && !all_of(line.begin(), line.end(), (int (*)(
int))isspace))
246 comment = line[line.find_first_not_of(
' ')];
248 if (strcmp((
char*)line.c_str(),
"nOFF") == 0) {
250 while (comment ==
'#') {
251 std::getline(input, line);
252 if (!line.empty() && !all_of(line.begin(), line.end(), (int (*)(
int))isspace))
253 comment = line[line.find_first_not_of(
' ')];
255 std::stringstream stream(line);
256 stream >> data_dimension;
262 int numedges, numfaces, i, dim;
263 while (comment ==
'#') {
264 std::getline(input, line);
265 if (!line.empty() && !all_of(line.begin(), line.end(), (int (*)(
int))isspace))
266 comment = line[line.find_first_not_of(
' ')];
268 std::stringstream stream(line);
275 std::getline(input, line);
276 if (!line.empty() && line[line.find_first_not_of(
' ')] !=
'#' &&
277 !all_of(line.begin(), line.end(), (int (*)(
int))isspace)) {
278 std::stringstream iss(line);
279 std::vector<double> point;
280 point.assign(std::istream_iterator<double>(iss), std::istream_iterator<double>());
281 point_cloud.emplace_back(point.begin(), point.begin() + data_dimension);
282 boost::add_vertex(one_skeleton_OFF);
283 vertices.push_back(boost::add_vertex(one_skeleton));
284 cover.emplace_back();
290 while (i < numfaces) {
291 std::getline(input, line);
292 if (!line.empty() && line[line.find_first_not_of(
' ')] !=
'#' &&
293 !all_of(line.begin(), line.end(), (int (*)(
int))isspace)) {
294 std::vector<int> simplex;
295 std::stringstream iss(line);
296 simplex.assign(std::istream_iterator<int>(iss), std::istream_iterator<int>());
298 for (
int j = 1; j <= dim; j++)
299 for (
int k = j + 1; k <= dim; k++)
300 boost::add_edge(vertices[simplex[j]], vertices[simplex[k]], one_skeleton_OFF);
305 return input.is_open();
321 remove_edges(one_skeleton);
323 std::ifstream input(graph_file_name);
326 while (std::getline(input, line)) {
327 std::stringstream stream(line);
329 while (stream >> neighb) boost::add_edge(vertices[source], vertices[neighb], one_skeleton);
338 remove_edges(one_skeleton);
339 if (num_edges(one_skeleton_OFF))
340 one_skeleton = one_skeleton_OFF;
342 std::cerr <<
"No triangulation read in OFF file!" << std::endl;
352 template <
typename Distance>
354 remove_edges(one_skeleton);
355 if (distances.size() == 0) compute_pairwise_distances(distance);
356 for (
int i = 0; i < n; i++) {
357 for (
int j = i + 1; j < n; j++) {
358 if (distances[i][j] <= threshold) {
359 boost::add_edge(vertices[i], vertices[j], one_skeleton);
360 boost::put(boost::edge_weight, one_skeleton, boost::edge(vertices[i], vertices[j], one_skeleton).first,
368 void set_graph_weights() {
369 Index_map index = boost::get(boost::vertex_index, one_skeleton);
370 Weight_map weight = boost::get(boost::edge_weight, one_skeleton);
371 boost::graph_traits<Graph>::edge_iterator ei, ei_end;
372 for (boost::tie(ei, ei_end) = boost::edges(one_skeleton); ei != ei_end; ++ei)
373 boost::put(weight, *ei,
374 distances[index[boost::source(*ei, one_skeleton)]][index[boost::target(*ei, one_skeleton)]]);
384 n = distance_matrix.size(); data_dimension = 0; point_cloud_name =
"matrix";
385 cover.resize(n); point_cloud.resize(n);
386 for(
int i = 0; i < n; i++){
387 boost::add_vertex(one_skeleton_OFF);
388 vertices.push_back(boost::add_vertex(one_skeleton));
390 distances = distance_matrix;
396 template <
typename Distance>
397 void compute_pairwise_distances(Distance ref_distance) {
399 std::vector<double> zeros(n);
400 for (
int i = 0; i < n; i++) distances.push_back(zeros);
401 std::string distance = point_cloud_name +
"_dist";
402 std::ifstream input(distance, std::ios::out | std::ios::binary);
405 if (verbose) std::clog <<
"Reading distances..." << std::endl;
406 for (
int i = 0; i < n; i++) {
407 for (
int j = i; j < n; j++) {
408 input.read((
char*)&d, 8);
415 if (verbose) std::clog <<
"Computing distances..." << std::endl;
417 std::ofstream output(distance, std::ios::out | std::ios::binary);
418 for (
int i = 0; i < n; i++) {
419 int state = (int)floor(100 * (i * 1.0 + 1) / n) % 10;
420 if (state == 0 && verbose) std::clog <<
"\r" << state <<
"%" << std::flush;
421 for (
int j = i; j < n; j++) {
422 double dis = ref_distance(point_cloud[i], point_cloud[j]);
423 distances[i][j] = dis;
424 distances[j][i] = dis;
425 output.write((
char*)&dis, 8);
429 if (verbose) std::clog << std::endl;
443 template <
typename Distance>
445 int m = floor(n / std::exp((1 + rate_power) * std::log(std::log(n) / std::log(rate_constant))));
446 m = (std::min)(m, n - 1);
449 if (verbose) std::clog << n <<
" points in R^" << data_dimension << std::endl;
450 if (verbose) std::clog <<
"Subsampling " << m <<
" points" << std::endl;
452 if (distances.size() == 0) compute_pairwise_distances(distance);
455 std::mutex deltamutex;
456 tbb::parallel_for(0, N, [&](
int i){
457 std::vector<int> samples(m);
458 SampleWithoutReplacement(n, m, samples);
459 double hausdorff_dist = 0;
460 for (
int j = 0; j < n; j++) {
461 double mj = distances[j][samples[0]];
462 for (
int k = 1; k < m; k++) mj = (std::min)(mj, distances[j][samples[k]]);
463 hausdorff_dist = (std::max)(hausdorff_dist, mj);
466 delta += hausdorff_dist / N;
470 for (
int i = 0; i < N; i++) {
471 std::vector<int> samples(m);
472 SampleWithoutReplacement(n, m, samples);
473 double hausdorff_dist = 0;
474 for (
int j = 0; j < n; j++) {
475 double mj = distances[j][samples[0]];
476 for (
int k = 1; k < m; k++) mj = (std::min)(mj, distances[j][samples[k]]);
477 hausdorff_dist = (std::max)(hausdorff_dist, mj);
479 delta += hausdorff_dist / N;
483 if (verbose) std::clog <<
"delta = " << delta << std::endl;
500 std::ifstream input(func_file_name);
503 while (std::getline(input, line)) {
504 std::stringstream stream(line);
509 functional_cover =
true;
510 cover_name = func_file_name;
520 if(point_cloud[0].size() > 0){
521 for (
int i = 0; i < n; i++) func.push_back(point_cloud[i][k]);
522 functional_cover =
true;
523 cover_name =
"coordinate " + std::to_string(k);
526 std::cerr <<
"Only pairwise distances provided---cannot access " << k <<
"th coordinate; returning null vector instead" << std::endl;
527 for (
int i = 0; i < n; i++) func.push_back(0.0);
528 functional_cover =
true;
539 template <
class InputRange>
541 for (
int i = 0; i < n; i++) func.push_back(function[i]);
542 functional_cover =
true;
558 if (!functional_cover) {
559 std::cerr <<
"Cover needs to come from the preimages of a function." << std::endl;
562 if (type !=
"Nerve" && type !=
"GIC") {
563 std::cerr <<
"Type of complex needs to be specified." << std::endl;
568 Index_map index = boost::get(boost::vertex_index, one_skeleton);
571 boost::graph_traits<Graph>::edge_iterator ei, ei_end;
572 for (boost::tie(ei, ei_end) = boost::edges(one_skeleton); ei != ei_end; ++ei)
573 reso = (std::max)(reso, std::abs(func[index[boost::source(*ei, one_skeleton)]] -
574 func[index[boost::target(*ei, one_skeleton)]]));
575 if (verbose) std::clog <<
"resolution = " << reso << std::endl;
576 resolution_double = reso;
579 if (type ==
"Nerve") {
580 boost::graph_traits<Graph>::edge_iterator ei, ei_end;
581 for (boost::tie(ei, ei_end) = boost::edges(one_skeleton); ei != ei_end; ++ei)
582 reso = (std::max)(reso, std::abs(func[index[boost::source(*ei, one_skeleton)]] -
583 func[index[boost::target(*ei, one_skeleton)]]) /
585 if (verbose) std::clog <<
"resolution = " << reso << std::endl;
586 resolution_double = reso;
617 if (resolution_double == -1 && resolution_int == -1) {
618 std::cerr <<
"Number and/or length of intervals not specified" << std::endl;
622 std::cerr <<
"Gain not specified" << std::endl;
627 double minf = (std::numeric_limits<float>::max)();
628 double maxf = std::numeric_limits<float>::lowest();
629 for (
int i = 0; i < n; i++) {
630 minf = (std::min)(minf, func[i]);
631 maxf = (std::max)(maxf, func[i]);
633 if (verbose) std::clog <<
"Min function value = " << minf <<
" and Max function value = " << maxf << std::endl;
636 std::vector<std::pair<double, double> > intervals;
639 if (resolution_double == -1) {
640 double incr = (maxf - minf) / resolution_int;
642 double alpha = (incr * gain) / (2 - 2 * gain);
643 double y = minf + incr + alpha;
644 std::pair<double, double> interm(x, y);
645 intervals.push_back(interm);
646 for (
int i = 1; i < resolution_int - 1; i++) {
647 x = minf + i * incr - alpha;
648 y = minf + (i + 1) * incr + alpha;
649 std::pair<double, double> inter(x, y);
650 intervals.push_back(inter);
652 x = minf + (resolution_int - 1) * incr - alpha;
654 std::pair<double, double> interM(x, y);
655 intervals.push_back(interM);
656 res = intervals.size();
658 for (
int i = 0; i < res; i++)
659 std::clog <<
"Interval " << i <<
" = [" << intervals[i].first <<
", " << intervals[i].second <<
"]"
663 if (resolution_int == -1) {
665 double y = x + resolution_double;
666 while (y <= maxf && maxf - (y - gain * resolution_double) >= resolution_double) {
667 std::pair<double, double> inter(x, y);
668 intervals.push_back(inter);
669 x = y - gain * resolution_double;
670 y = x + resolution_double;
672 std::pair<double, double> interM(x, maxf);
673 intervals.push_back(interM);
674 res = intervals.size();
676 for (
int i = 0; i < res; i++)
677 std::clog <<
"Interval " << i <<
" = [" << intervals[i].first <<
", " << intervals[i].second <<
"]"
682 double y = x + resolution_double;
684 while (count < resolution_int && y <= maxf && maxf - (y - gain * resolution_double) >= resolution_double) {
685 std::pair<double, double> inter(x, y);
686 intervals.push_back(inter);
688 x = y - gain * resolution_double;
689 y = x + resolution_double;
691 res = intervals.size();
693 for (
int i = 0; i < res; i++)
694 std::clog <<
"Interval " << i <<
" = [" << intervals[i].first <<
", " << intervals[i].second <<
"]"
701 std::vector<int> points(n);
702 for (
int i = 0; i < n; i++) points[i] = i;
703 std::sort(points.begin(), points.end(), [
this](
int p1,
int p2){return (this->func[p1] < this->func[p2]);});
707 Index_map index = boost::get(boost::vertex_index, one_skeleton);
708 std::map<int, std::vector<int> > preimages;
709 std::map<int, double> funcstd;
711 if (verbose) std::clog <<
"Computing preimages..." << std::endl;
712 for (
int i = 0; i < res; i++) {
714 std::pair<double, double> inter1 = intervals[i];
720 std::pair<double, double> inter3 = intervals[i - 1];
721 while (func[points[tmp]] < inter3.second && tmp != n) {
722 preimages[i].push_back(points[tmp]);
730 std::pair<double, double> inter2 = intervals[i + 1];
731 while (func[points[tmp]] < inter2.first && tmp != n) {
732 preimages[i].push_back(points[tmp]);
737 while (func[points[tmp]] < inter1.second && tmp != n) {
738 preimages[i].push_back(points[tmp]);
743 std::pair<double, double> inter3 = intervals[i - 1];
744 while (func[points[tmp]] < inter3.second && tmp != n) {
745 preimages[i].push_back(points[tmp]);
749 preimages[i].push_back(points[tmp]);
756 funcstd[i] = 0.5 * (u + v);
760 if (verbose) std::clog <<
"Computing connected components (parallelized)..." << std::endl;
761 std::mutex covermutex, idmutex;
762 tbb::parallel_for(0, res, [&](
int i){
764 Graph G = one_skeleton.create_subgraph();
765 int num = preimages[i].size();
766 std::vector<int> component(num);
767 for (
int j = 0; j < num; j++) boost::add_vertex(index[vertices[preimages[i][j]]], G);
768 boost::connected_components(G, &component[0]);
772 for (
int j = 0; j < num; j++) {
774 if (component[j] > max) max = component[j];
777 int identifier = ((i + component[j])*(i + component[j]) + 3 * i + component[j]) / 2;
781 cover[preimages[i][j]].push_back(identifier);
782 cover_back[identifier].push_back(preimages[i][j]);
783 cover_fct[identifier] = i;
784 cover_std[identifier] = funcstd[i];
785 cover_color[identifier].second += func_color[preimages[i][j]];
786 cover_color[identifier].first += 1;
796 if (verbose) std::clog <<
"Computing connected components..." << std::endl;
797 for (
int i = 0; i < res; i++) {
799 Graph G = one_skeleton.create_subgraph();
800 int num = preimages[i].size();
801 std::vector<int> component(num);
802 for (
int j = 0; j < num; j++) boost::add_vertex(index[vertices[preimages[i][j]]], G);
803 boost::connected_components(G, &component[0]);
807 for (
int j = 0; j < num; j++) {
809 if (component[j] > max) max = component[j];
812 int identifier = (std::pow(i + component[j], 2) + 3 * i + component[j]) / 2;
815 cover[preimages[i][j]].push_back(identifier);
816 cover_back[identifier].push_back(preimages[i][j]);
817 cover_fct[identifier] = i;
818 cover_std[identifier] = funcstd[i];
819 cover_color[identifier].second += func_color[preimages[i][j]];
820 cover_color[identifier].first += 1;
828 maximal_dim =
id - 1;
829 for (std::map<
int, std::pair<int, double> >::iterator iit = cover_color.begin(); iit != cover_color.end(); iit++)
830 iit->second.second /= iit->second.first;
843 std::vector<int> cov_elts, cov_number;
844 std::ifstream input(cover_file_name);
846 while (std::getline(input, line)) {
848 std::stringstream stream(line);
849 while (stream >> cov) {
850 cov_elts.push_back(cov);
851 cov_number.push_back(cov);
852 cover_fct[cov] = cov;
853 cover_color[cov].second += func_color[i];
854 cover_color[cov].first++;
855 cover_back[cov].push_back(i);
861 std::sort(cov_number.begin(), cov_number.end());
862 std::vector<int>::iterator it = std::unique(cov_number.begin(), cov_number.end());
863 cov_number.resize(std::distance(cov_number.begin(), it));
865 maximal_dim = cov_number.size() - 1;
866 for (
int i = 0; i <= maximal_dim; i++) cover_color[i].second /= cover_color[i].first;
867 cover_name = cover_file_name;
877 template <
typename Distance>
879 voronoi_subsamples.resize(m);
880 SampleWithoutReplacement(n, m, voronoi_subsamples);
881 if (distances.size() == 0) compute_pairwise_distances(distance);
883 Weight_map weight = boost::get(boost::edge_weight, one_skeleton);
884 Index_map index = boost::get(boost::vertex_index, one_skeleton);
885 std::vector<double> mindist(n);
886 for (
int j = 0; j < n; j++) mindist[j] = (std::numeric_limits<double>::max)();
890 if (verbose) std::clog <<
"Computing geodesic distances (parallelized)..." << std::endl;
891 std::mutex coverMutex; std::mutex mindistMutex;
892 tbb::parallel_for(0, m, [&](
int i){
893 int seed = voronoi_subsamples[i];
894 std::vector<double> dmap(n);
895 boost::dijkstra_shortest_paths(
896 one_skeleton, vertices[seed],
897 boost::weight_map(weight).distance_map(boost::make_iterator_property_map(dmap.begin(), index)));
899 coverMutex.lock(); mindistMutex.lock();
900 for (
int j = 0; j < n; j++)
901 if (mindist[j] > dmap[j]) {
902 mindist[j] = dmap[j];
903 if (cover[j].size() == 0)
904 cover[j].push_back(i);
908 coverMutex.unlock(); mindistMutex.unlock();
911 for (
int i = 0; i < m; i++) {
912 if (verbose) std::clog <<
"Computing geodesic distances to seed " << i <<
"..." << std::endl;
913 int seed = voronoi_subsamples[i];
914 std::vector<double> dmap(n);
915 boost::dijkstra_shortest_paths(
916 one_skeleton, vertices[seed],
917 boost::weight_map(weight).distance_map(boost::make_iterator_property_map(dmap.begin(), index)));
919 for (
int j = 0; j < n; j++)
920 if (mindist[j] > dmap[j]) {
921 mindist[j] = dmap[j];
922 if (cover[j].size() == 0)
923 cover[j].push_back(i);
930 for (
int i = 0; i < n; i++) {
931 cover_back[cover[i][0]].push_back(i);
932 cover_color[cover[i][0]].second += func_color[i];
933 cover_color[cover[i][0]].first++;
935 for (
int i = 0; i < m; i++) cover_color[i].second /= cover_color[i].first;
937 cover_name =
"Voronoi";
947 const std::vector<int>&
subpopulation(
int c) {
return cover_back[name2idinv[c]]; }
962 std::ifstream input(color_file_name);
965 while (std::getline(input, line)) {
966 std::stringstream stream(line);
968 func_color.push_back(f);
971 color_name = color_file_name;
981 if(point_cloud[0].size() > 0){
982 for (
int i = 0; i < n; i++) func_color.push_back(point_cloud[i][k]);
983 color_name =
"coordinate ";
984 color_name.append(std::to_string(k));
987 std::cerr <<
"Only pairwise distances provided---cannot access " << k <<
"th coordinate; returning null vector instead" << std::endl;
988 for (
int i = 0; i < n; i++) func.push_back(0.0);
989 functional_cover =
true;
1001 for (
unsigned int i = 0; i < color.size(); i++) func_color.push_back(color[i]);
1010 std::string mapp = point_cloud_name +
"_sc.dot";
1011 std::ofstream graphic(mapp);
1013 double maxv = std::numeric_limits<double>::lowest();
1014 double minv = (std::numeric_limits<double>::max)();
1015 for (std::map<
int, std::pair<int, double> >::iterator iit = cover_color.begin(); iit != cover_color.end(); iit++) {
1016 maxv = (std::max)(maxv, iit->second.second);
1017 minv = (std::min)(minv, iit->second.second);
1021 std::vector<int> nodes;
1024 graphic <<
"graph GIC {" << std::endl;
1026 for (std::map<
int, std::pair<int, double> >::iterator iit = cover_color.begin(); iit != cover_color.end(); iit++) {
1027 if (iit->second.first > mask) {
1028 nodes.push_back(iit->first);
1029 name2id[iit->first] = id;
1030 name2idinv[id] = iit->first;
1032 graphic << name2id[iit->first] <<
"[shape=circle fontcolor=black color=black label=\"" << name2id[iit->first]
1033 <<
":" << iit->second.first <<
"\" style=filled fillcolor=\""
1034 << (1 - (maxv - iit->second.second) / (maxv - minv)) * 0.6 <<
", 1, 1\"]" << std::endl;
1039 int num_simplices = simplices.size();
1040 for (
int i = 0; i < num_simplices; i++)
1041 if (simplices[i].size() == 2) {
1042 if (cover_color[simplices[i][0]].first > mask && cover_color[simplices[i][1]].first > mask) {
1043 graphic <<
" " << name2id[simplices[i][0]] <<
" -- " << name2id[simplices[i][1]] <<
" [weight=15];"
1050 std::clog << mapp <<
" file generated. It can be visualized with e.g. neato." << std::endl;
1058 int num_simplices = simplices.size();
1060 std::string mapp = point_cloud_name +
"_sc.txt";
1061 std::ofstream graphic(mapp);
1063 for (
int i = 0; i < num_simplices; i++)
1064 if (simplices[i].size() == 2)
1065 if (cover_color[simplices[i][0]].first > mask && cover_color[simplices[i][1]].first > mask) num_edges++;
1067 graphic << point_cloud_name << std::endl;
1068 graphic << cover_name << std::endl;
1069 graphic << color_name << std::endl;
1070 graphic << resolution_double <<
" " << gain << std::endl;
1071 graphic << cover_color.size() <<
" " << num_edges << std::endl;
1074 for (std::map<
int, std::pair<int, double> >::iterator iit = cover_color.begin(); iit != cover_color.end(); iit++) {
1075 graphic <<
id <<
" " << iit->second.second <<
" " << iit->second.first << std::endl;
1076 name2id[iit->first] = id;
1077 name2idinv[id] = iit->first;
1081 for (
int i = 0; i < num_simplices; i++)
1082 if (simplices[i].size() == 2)
1083 if (cover_color[simplices[i][0]].first > mask && cover_color[simplices[i][1]].first > mask)
1084 graphic << name2id[simplices[i][0]] <<
" " << name2id[simplices[i][1]] << std::endl;
1087 <<
" generated. It can be visualized with e.g. python KeplerMapperVisuFromTxtFile.py and firefox."
1097 assert(cover_name ==
"Voronoi");
1099 int m = voronoi_subsamples.size();
1102 std::vector<std::vector<int> > edges, faces;
1103 int numsimplices = simplices.size();
1105 std::string mapp = point_cloud_name +
"_sc.off";
1106 std::ofstream graphic(mapp);
1108 graphic <<
"OFF" << std::endl;
1109 for (
int i = 0; i < numsimplices; i++) {
1110 if (simplices[i].size() == 2) {
1112 edges.push_back(simplices[i]);
1114 if (simplices[i].size() == 3) {
1116 faces.push_back(simplices[i]);
1119 graphic << m <<
" " << numedges + numfaces << std::endl;
1120 for (
int i = 0; i < m; i++) {
1121 if (data_dimension <= 3) {
1122 for (
int j = 0; j < data_dimension; j++) graphic << point_cloud[voronoi_subsamples[i]][j] <<
" ";
1123 for (
int j = data_dimension; j < 3; j++) graphic << 0 <<
" ";
1124 graphic << std::endl;
1126 for (
int j = 0; j < 3; j++) graphic << point_cloud[voronoi_subsamples[i]][j] <<
" ";
1129 for (
int i = 0; i < numedges; i++) graphic << 2 <<
" " << edges[i][0] <<
" " << edges[i][1] << std::endl;
1130 for (
int i = 0; i < numfaces; i++)
1131 graphic << 3 <<
" " << faces[i][0] <<
" " << faces[i][1] <<
" " << faces[i][2] << std::endl;
1133 std::clog << mapp <<
" generated. It can be visualized with e.g. geomview." << std::endl;
1148 double maxf = std::numeric_limits<double>::lowest();
1149 double minf = (std::numeric_limits<double>::max)();
1150 for (std::map<int, double>::iterator it = cover_std.begin(); it != cover_std.end(); it++) {
1151 maxf = (std::max)(maxf, it->second);
1152 minf = (std::min)(minf, it->second);
1156 for (
auto const& simplex : simplices) {
1157 std::vector<int> splx = simplex;
1162 for (std::map<int, double>::iterator it = cover_std.begin(); it != cover_std.end(); it++) {
1163 int vertex = it->first;
float val = it->second;
1164 int vert[] = {vertex};
int edge[] = {vertex, -2};
1178 for (
int i = 0; i < max_dim; i++) {
1180 int num_bars = bars.size();
if(i == 0) num_bars -= 1;
1181 if(verbose) std::clog << num_bars <<
" interval(s) in dimension " << i <<
":" << std::endl;
1182 for (
int j = 0; j < num_bars; j++) {
1183 double birth = bars[j].first;
1184 double death = bars[j].second;
1185 if (i == 0 && std::isinf(death))
continue;
1187 birth = minf + (birth + 2) * (maxf - minf);
1189 birth = minf + (2 - birth) * (maxf - minf);
1191 death = minf + (death + 2) * (maxf - minf);
1193 death = minf + (2 - death) * (maxf - minf);
1194 PD.push_back(std::pair<double, double>(birth, death));
1195 if (verbose) std::clog <<
" [" << birth <<
", " << death <<
"]" << std::endl;
1208 unsigned int sz = distribution.size();
1210 for (
unsigned int i = 0; i < N - sz; i++) {
1211 if (verbose) std::clog <<
"Computing " << i <<
"th bootstrap, bottleneck distance = ";
1213 Cover_complex Cboot; Cboot.n = this->n; Cboot.data_dimension = this->data_dimension; Cboot.type = this->type; Cboot.functional_cover =
true;
1215 std::vector<int> boot(this->n);
1216 for (
int j = 0; j < this->n; j++) {
1217 double u = GetUniform();
1218 int id = std::floor(u * (this->n)); boot[j] = id;
1219 Cboot.point_cloud.push_back(this->point_cloud[
id]); Cboot.cover.emplace_back(); Cboot.func.push_back(this->func[
id]);
1220 boost::add_vertex(Cboot.one_skeleton_OFF); Cboot.vertices.push_back(boost::add_vertex(Cboot.one_skeleton));
1224 for (
int j = 0; j < n; j++) {
1225 std::vector<double> dist(n);
1226 for (
int k = 0; k < n; k++) dist[k] = distances[boot[j]][boot[k]];
1227 Cboot.distances.push_back(dist);
1236#ifdef GUDHI_GIC_USE_CGAL
1238#elif defined GUDHI_GIC_USE_HERA
1239 double db = hera::bottleneckDistExact(this->PD, Cboot.PD);
1242 throw std::logic_error(
"This function requires CGAL or Hera for the bottleneck distance.");
1244 if (verbose) std::clog << db << std::endl;
1245 distribution.push_back(db);
1248 std::sort(distribution.begin(), distribution.end());
1259 unsigned int N = distribution.size();
1260 double d = distribution[std::floor(alpha * N)];
1261 if (verbose) std::clog <<
"Distance corresponding to confidence " << alpha <<
" is " << d << std::endl;
1272 unsigned int N = distribution.size();
1274 for (
unsigned int i = 0; i < N; i++)
1275 if (distribution[i] >= d){ level = i * 1.0 / N;
break; }
1276 if (verbose) std::clog <<
"Confidence level of distance " << d <<
" is " << level << std::endl;
1286 double distancemin = (std::numeric_limits<double>::max)();
int N = PD.size();
1287 for (
int i = 0; i < N; i++) distancemin = (std::min)(distancemin, 0.5 * std::abs(PD[i].second - PD[i].first));
1289 if (verbose) std::clog <<
"p value = " << p_value << std::endl;
1303 template <
typename SimplicialComplex>
1305 unsigned int dimension = 0;
1306 for (
auto const& simplex : simplices) {
1307 int numvert = simplex.size();
1308 double filt = std::numeric_limits<double>::lowest();
1309 for (
int i = 0; i < numvert; i++) filt = (std::max)(cover_color[simplex[i]].second, filt);
1310 complex.insert_simplex_and_subfaces(simplex, filt);
1311 if (dimension < simplex.size() - 1) dimension = simplex.size() - 1;
1319 if (type !=
"Nerve" && type !=
"GIC") {
1320 std::cerr <<
"Type of complex needs to be specified." << std::endl;
1324 if (type ==
"Nerve") {
1325 for(
int i = 0; i < n; i++) simplices.push_back(cover[i]);
1326 std::sort(simplices.begin(), simplices.end());
1327 std::vector<std::vector<int> >::iterator it = std::unique(simplices.begin(), simplices.end());
1328 simplices.resize(std::distance(simplices.begin(), it));
1331 if (type ==
"GIC") {
1332 Index_map index = boost::get(boost::vertex_index, one_skeleton);
1334 if (functional_cover) {
1339 throw std::invalid_argument(
1340 "the output of this function is correct ONLY if the cover is minimal, i.e. the gain is less than 0.5.");
1343 boost::graph_traits<Graph>::edge_iterator ei, ei_end;
1344 for (boost::tie(ei, ei_end) = boost::edges(one_skeleton); ei != ei_end; ++ei) {
1345 int nums = cover[index[boost::source(*ei, one_skeleton)]].size();
1346 for (
int i = 0; i < nums; i++) {
1347 int vs = cover[index[boost::source(*ei, one_skeleton)]][i];
1348 int numt = cover[index[boost::target(*ei, one_skeleton)]].size();
1349 for (
int j = 0; j < numt; j++) {
1350 int vt = cover[index[boost::target(*ei, one_skeleton)]][j];
1351 if (cover_fct[vs] == cover_fct[vt] + 1 || cover_fct[vt] == cover_fct[vs] + 1) {
1352 std::vector<int> edge(2);
1353 edge[0] = (std::min)(vs, vt);
1354 edge[1] = (std::max)(vs, vt);
1355 simplices.push_back(edge);
1362 std::sort(simplices.begin(), simplices.end());
1363 std::vector<std::vector<int> >::iterator it = std::unique(simplices.begin(), simplices.end());
1364 simplices.resize(std::distance(simplices.begin(), it));
1369 boost::graph_traits<Graph>::edge_iterator ei, ei_end;
1370 for (boost::tie(ei, ei_end) = boost::edges(one_skeleton); ei != ei_end; ++ei)
1371 if (!(cover[index[boost::target(*ei, one_skeleton)]].size() == 1 &&
1372 cover[index[boost::target(*ei, one_skeleton)]] == cover[index[boost::source(*ei, one_skeleton)]])) {
1373 std::vector<int> edge(2);
1374 edge[0] = index[boost::source(*ei, one_skeleton)];
1375 edge[1] = index[boost::target(*ei, one_skeleton)];
1388 std::vector<int> simplx;
1390 unsigned int sz = cover[vertex].size();
1391 for (
unsigned int i = 0; i < sz; i++) {
1392 simplx.push_back(cover[vertex][i]);
1395 std::sort(simplx.begin(), simplx.end());
1396 std::vector<int>::iterator it = std::unique(simplx.begin(), simplx.end());
1397 simplx.resize(std::distance(simplx.begin(), it));
1398 simplices.push_back(simplx);
1401 std::sort(simplices.begin(), simplices.end());
1402 std::vector<std::vector<int> >::iterator it = std::unique(simplices.begin(), simplices.end());
1403 simplices.resize(std::distance(simplices.begin(), it));
Compute the Euclidean distance between two Points given by a range of coordinates....
Definition: distance_functions.h:32
Options::Filtration_value Filtration_value
Type for the value of the filtration function.
Definition: Simplex_tree.h:88
void assign_filtration(Simplex_handle sh, Filtration_value fv)
Sets the filtration value of a simplex.
Definition: Simplex_tree.h:548
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:1401
Simplex_vertex_range simplex_vertex_range(Simplex_handle sh) const
Returns a range over the vertices of a simplex.
Definition: Simplex_tree.h:284
bool has_children(SimplexHandle sh) const
Returns true if the node in the simplex tree pointed by sh has children.
Definition: Simplex_tree.h:630
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:643
void expansion(int max_dim)
Expands the Simplex_tree containing only its one skeleton until dimension max_dim.
Definition: Simplex_tree.h:1185
int dimension(Simplex_handle sh)
Returns the dimension of a simplex.
Definition: Simplex_tree.h:602
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:811
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:558
Complex_simplex_range complex_simplex_range()
Returns a range over the simplices of the simplicial complex.
Definition: Simplex_tree.h:239
Cover complex data structure.
Definition: GIC.h:92
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:498
double set_automatic_resolution()
Computes the optimal length of intervals (i.e. the smallest interval length avoiding discretization a...
Definition: GIC.h:557
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:878
void set_resolution_with_interval_number(int reso)
Sets a number of intervals from a value stored in memory.
Definition: GIC.h:604
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:212
Persistence_diagram compute_PD()
Computes the extended persistence diagram of the complex.
Definition: GIC.h:1144
double compute_distance_from_confidence_level(double alpha)
Computes the bottleneck distance threshold corresponding to a specific confidence level.
Definition: GIC.h:1258
void set_graph_from_rips(double threshold, Distance distance)
Creates a graph G from a Rips complex.
Definition: GIC.h:353
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:840
void set_graph_from_file(const std::string &graph_file_name)
Creates a graph G from a file containing the edges.
Definition: GIC.h:320
void create_complex(SimplicialComplex &complex)
Creates the simplicial complex.
Definition: GIC.h:1304
void set_type(const std::string &t)
Specifies whether the type of the output simplicial complex.
Definition: GIC.h:181
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:383
void find_simplices()
Computes the simplices of the simplicial complex.
Definition: GIC.h:1318
void set_function_from_range(InputRange const &function)
Creates the function f from a vector stored in memory.
Definition: GIC.h:540
void set_cover_from_function()
Creates a cover C from the preimages of the function f.
Definition: GIC.h:616
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:960
void write_info()
Creates a .txt file called SC.txt describing the 1-skeleton, which can then be plotted with e....
Definition: GIC.h:1057
void plot_OFF()
Creates a .off file called SC.off for 3D visualization, which contains the 2-skeleton of the GIC....
Definition: GIC.h:1096
void plot_DOT()
Creates a .dot file called SC.dot for neato (part of the graphviz package) once the simplicial comple...
Definition: GIC.h:1009
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:1000
void set_subsampling(double constant, double power)
Sets the constants used to subsample the data set. These constants are explained in .
Definition: GIC.h:199
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:222
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—s...
Definition: GIC.h:444
void set_resolution_with_interval_length(double reso)
Sets a length of intervals from a value stored in memory.
Definition: GIC.h:598
const std::vector< int > & subpopulation(int c)
Returns the data subset corresponding to a specific node of the created complex.
Definition: GIC.h:947
void set_gain(double g=0.3)
Sets a gain from a value stored in memory (default value 0.3).
Definition: GIC.h:610
void set_function_from_coordinate(int k)
Creates the function f from the k-th coordinate of the point cloud P.
Definition: GIC.h:519
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:980
void compute_distribution(unsigned int N=100)
Computes bootstrapped distances distribution.
Definition: GIC.h:1207
double compute_confidence_level_from_distance(double d)
Computes the confidence level of a specific bottleneck distance threshold.
Definition: GIC.h:1271
void set_graph_from_OFF()
Creates a graph G from the triangulation given by the input .OFF file.
Definition: GIC.h:337
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:1285
bool read_point_cloud(const std::string &off_file_name)
Reads and stores the input point cloud from .(n)OFF file.
Definition: GIC.h:237
void set_verbose(bool verb=false)
Specifies whether the program should display information or not.
Definition: GIC.h:189
Computes the persistent cohomology of a filtered complex.
Definition: Persistent_cohomology.h:52
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
void compute_persistent_cohomology(Filtration_value min_interval_length=0)
Compute the persistent homology of the filtered simplicial complex.
Definition: Persistent_cohomology.h:172
void init_coefficients(int charac)
Initializes the coefficient field.
Definition: Persistent_cohomology.h:156
Rips complex data structure.
Definition: Rips_complex.h:45
Global distance functions.
Graph simplicial complex methods.
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:116
This file includes common file reader for GUDHI.
Value type for a filtration function on a cell complex.
Definition: FiltrationValue.h:20