17#include <tbb/parallel_for.h>
21#if __has_include(<CGAL/version.h>)
22# define GUDHI_GIC_USE_CGAL 1
23# include <gudhi/Bottleneck.h>
24#elif __has_include(<hera/bottleneck.h>)
25# define GUDHI_GIC_USE_HERA 1
29# include <type_traits>
31using ssize_t = std::make_signed_t<std::size_t>;
33# include <hera/bottleneck.h>
36#include <gudhi/Debug_utils.h>
39#include <gudhi/Simplex_tree.h>
40#include <gudhi/Rips_complex.h>
41#include <gudhi/Points_off_io.h>
43#include <gudhi/Persistent_cohomology.h>
44#include <gudhi/random.h>
46#include <boost/config.hpp>
47#include <boost/graph/graph_traits.hpp>
48#include <boost/graph/adjacency_list.hpp>
49#include <boost/graph/connected_components.hpp>
50#include <boost/graph/dijkstra_shortest_paths.hpp>
51#include <boost/graph/subgraph.hpp>
52#include <boost/graph/graph_utility.hpp>
66namespace cover_complex {
68using Simplex_tree = Gudhi::Simplex_tree<>;
70using Rips_complex = Gudhi::rips_complex::Rips_complex<Filtration_value>;
71using Persistence_diagram = std::vector<std::pair<double, double> >;
72using Graph = boost::subgraph<
73 boost::adjacency_list<boost::setS, boost::vecS, boost::undirectedS, boost::no_property,
74 boost::property<boost::edge_index_t, int, boost::property<boost::edge_weight_t, double> > > >;
75using Vertex_t = boost::graph_traits<Graph>::vertex_descriptor;
76using Index_map = boost::property_map<Graph, boost::vertex_index_t>::type;
77using Weight_map = boost::property_map<Graph, boost::edge_weight_t>::type;
99template <
typename Po
int>
102 bool verbose =
false;
105 std::vector<Point> point_cloud;
106 std::vector<std::vector<double> > distances;
111 std::vector<double> func;
112 std::vector<double> func_color;
113 bool functional_cover =
false;
115 Graph one_skeleton_OFF;
117 std::vector<Vertex_t> vertices;
119 std::vector<std::vector<int> > simplices;
120 std::vector<int> voronoi_subsamples;
122 Persistence_diagram PD;
123 std::vector<double> distribution;
125 std::vector<std::vector<int> >
127 std::map<int, std::vector<int> >
129 std::map<int, double> cover_std;
133 std::map<int, std::pair<int, double> >
136 int resolution_int = -1;
137 double resolution_double = -1;
139 double rate_constant = 10;
140 double rate_power = 0.001;
143 std::map<int, int> name2id, name2idinv;
145 std::string cover_name;
146 std::string point_cloud_name;
147 std::string color_name;
150 void remove_edges(Graph& G) {
151 boost::graph_traits<Graph>::edge_iterator ei, ei_end;
152 for (boost::tie(ei, ei_end) = boost::edges(G); ei != ei_end; ++ei) boost::remove_edge(*ei, G);
156 void SampleWithoutReplacement(
int populationSize,
int sampleSize, std::vector<int>& samples) {
160 while (m < sampleSize) {
162 if ((populationSize - t) * u >= sampleSize - m) {
201 rate_constant = constant;
224 this->num_points = point_cloud.size(); data_dimension = point_cloud[0].size();
225 point_cloud_name =
"cloud"; cover.resize(this->num_points);
226 for(
int i = 0; i < this->num_points; i++){
227 boost::add_vertex(one_skeleton_OFF);
228 vertices.push_back(boost::add_vertex(one_skeleton));
230 this->point_cloud = point_cloud;
239 point_cloud_name = off_file_name;
240 std::ifstream input(off_file_name);
244 while (comment ==
'#') {
245 std::getline(input, line);
246 if (!line.empty() && !all_of(line.begin(), line.end(), (int (*)(
int))isspace))
247 comment = line[line.find_first_not_of(
' ')];
249 if (strcmp((
char*)line.c_str(),
"nOFF") == 0) {
251 while (comment ==
'#') {
252 std::getline(input, line);
253 if (!line.empty() && !all_of(line.begin(), line.end(), (int (*)(
int))isspace))
254 comment = line[line.find_first_not_of(
' ')];
256 std::stringstream stream(line);
257 stream >> data_dimension;
263 int numedges, numfaces, i, dim;
264 while (comment ==
'#') {
265 std::getline(input, line);
266 if (!line.empty() && !all_of(line.begin(), line.end(), (int (*)(
int))isspace))
267 comment = line[line.find_first_not_of(
' ')];
269 std::stringstream stream(line);
270 stream >> this->num_points;
275 while (i < this->num_points) {
276 std::getline(input, line);
277 if (!line.empty() && line[line.find_first_not_of(
' ')] !=
'#' &&
278 !all_of(line.begin(), line.end(), (int (*)(
int))isspace)) {
279 std::stringstream iss(line);
280 std::vector<double> point;
281 point.assign(std::istream_iterator<double>(iss), std::istream_iterator<double>());
282 point_cloud.emplace_back(point.begin(), point.begin() + data_dimension);
283 boost::add_vertex(one_skeleton_OFF);
284 vertices.push_back(boost::add_vertex(one_skeleton));
285 cover.emplace_back();
291 while (i < numfaces) {
292 std::getline(input, line);
293 if (!line.empty() && line[line.find_first_not_of(
' ')] !=
'#' &&
294 !all_of(line.begin(), line.end(), (int (*)(
int))isspace)) {
295 std::vector<int> simplex;
296 std::stringstream iss(line);
297 simplex.assign(std::istream_iterator<int>(iss), std::istream_iterator<int>());
299 for (
int j = 1; j <= dim; j++)
300 for (
int k = j + 1; k <= dim; k++)
301 boost::add_edge(vertices[simplex[j]], vertices[simplex[k]], one_skeleton_OFF);
306 return input.is_open();
322 remove_edges(one_skeleton);
324 std::ifstream input(graph_file_name);
327 while (std::getline(input, line)) {
328 std::stringstream stream(line);
330 while (stream >> neighb) boost::add_edge(vertices[source], vertices[neighb], one_skeleton);
339 remove_edges(one_skeleton);
340 if (num_edges(one_skeleton_OFF))
341 one_skeleton = one_skeleton_OFF;
343 std::cerr <<
"No triangulation read in OFF file!" << std::endl;
353 template <
typename Distance>
355 remove_edges(one_skeleton);
356 if (distances.size() == 0) compute_pairwise_distances(distance);
357 for (
int i = 0; i < this->num_points; i++) {
358 for (
int j = i + 1; j < this->num_points; j++) {
359 if (distances[i][j] <= threshold) {
360 boost::add_edge(vertices[i], vertices[j], one_skeleton);
361 boost::put(boost::edge_weight, one_skeleton, boost::edge(vertices[i], vertices[j], one_skeleton).first,
369 void set_graph_weights() {
370 Index_map index = boost::get(boost::vertex_index, one_skeleton);
371 Weight_map weight = boost::get(boost::edge_weight, one_skeleton);
372 boost::graph_traits<Graph>::edge_iterator ei, ei_end;
373 for (boost::tie(ei, ei_end) = boost::edges(one_skeleton); ei != ei_end; ++ei)
374 boost::put(weight, *ei,
375 distances[index[boost::source(*ei, one_skeleton)]][index[boost::target(*ei, one_skeleton)]]);
385 this->num_points = distance_matrix.size(); data_dimension = 0; point_cloud_name =
"matrix";
386 cover.resize(this->num_points); point_cloud.resize(this->num_points);
387 for(
int i = 0; i < this->num_points; i++){
388 boost::add_vertex(one_skeleton_OFF);
389 vertices.push_back(boost::add_vertex(one_skeleton));
391 distances = distance_matrix;
397 template <
typename Distance>
398 void compute_pairwise_distances(Distance ref_distance) {
399 std::vector<double> zeros(this->num_points);
400 for (
int i = 0; i < this->num_points; i++) distances.push_back(zeros);
401 if (verbose) std::clog <<
"Computing distances..." << std::endl;
402 for (
int i = 0; i < this->num_points; i++) {
403 int state = 100 * (i + 1) / this->num_points;
404 if (verbose && state % 10 == 0) std::clog <<
"\r" << state <<
"%" << std::flush;
405 for (
int j = i; j < this->num_points; j++) {
406 double dis = ref_distance(point_cloud[i], point_cloud[j]);
407 distances[i][j] = dis;
408 distances[j][i] = dis;
411 if (verbose) std::clog << std::endl;
424 template <
typename Distance>
426 int m = floor(this->num_points / std::exp((1 + rate_power) * std::log(std::log(this->num_points) / std::log(rate_constant))));
427 m = (std::min)(m, this->num_points - 1);
430 if (verbose) std::clog << this->num_points <<
" points in R^" << data_dimension << std::endl;
431 if (verbose) std::clog <<
"Subsampling " << m <<
" points" << std::endl;
433 if (distances.size() == 0) compute_pairwise_distances(distance);
436 std::mutex deltamutex;
437 tbb::parallel_for(0, N, [&](
int i){
438 std::vector<int> samples(m);
439 SampleWithoutReplacement(this->num_points, m, samples);
440 double hausdorff_dist = 0;
441 for (
int j = 0; j < this->num_points; j++) {
442 double mj = distances[j][samples[0]];
443 for (
int k = 1; k < m; k++) mj = (std::min)(mj, distances[j][samples[k]]);
444 hausdorff_dist = (std::max)(hausdorff_dist, mj);
447 delta += hausdorff_dist / N;
451 for (
int i = 0; i < N; i++) {
452 std::vector<int> samples(m);
453 SampleWithoutReplacement(this->num_points, m, samples);
454 double hausdorff_dist = 0;
455 for (
int j = 0; j < this->num_points; j++) {
456 double mj = distances[j][samples[0]];
457 for (
int k = 1; k < m; k++) mj = (std::min)(mj, distances[j][samples[k]]);
458 hausdorff_dist = (std::max)(hausdorff_dist, mj);
460 delta += hausdorff_dist / N;
464 if (verbose) std::clog <<
"delta = " << delta << std::endl;
480 std::ifstream input(func_file_name);
483 while (std::getline(input, line)) {
484 std::stringstream stream(line);
488 functional_cover =
true;
489 cover_name = func_file_name;
499 if(point_cloud[0].size() > 0){
500 for (
int i = 0; i < this->num_points; i++) func.push_back(point_cloud[i][k]);
501 functional_cover =
true;
502 cover_name =
"coordinate " + std::to_string(k);
505 std::cerr <<
"Only pairwise distances provided---cannot access " << k <<
"th coordinate; returning null vector instead" << std::endl;
506 for (
int i = 0; i < this->num_points; i++) func.push_back(0.0);
507 functional_cover =
true;
518 template <
class InputRange>
520 for (
int i = 0; i < this->num_points; i++) func.push_back(function[i]);
521 functional_cover =
true;
537 if (!functional_cover) {
538 std::cerr <<
"Cover needs to come from the preimages of a function." << std::endl;
541 if (type !=
"Nerve" && type !=
"GIC") {
542 std::cerr <<
"Type of complex needs to be specified." << std::endl;
547 Index_map index = boost::get(boost::vertex_index, one_skeleton);
550 boost::graph_traits<Graph>::edge_iterator ei, ei_end;
551 for (boost::tie(ei, ei_end) = boost::edges(one_skeleton); ei != ei_end; ++ei)
552 reso = (std::max)(reso, std::abs(func[index[boost::source(*ei, one_skeleton)]] -
553 func[index[boost::target(*ei, one_skeleton)]]));
554 if (verbose) std::clog <<
"resolution = " << reso << std::endl;
555 resolution_double = reso;
558 if (type ==
"Nerve") {
559 boost::graph_traits<Graph>::edge_iterator ei, ei_end;
560 for (boost::tie(ei, ei_end) = boost::edges(one_skeleton); ei != ei_end; ++ei)
561 reso = (std::max)(reso, std::abs(func[index[boost::source(*ei, one_skeleton)]] -
562 func[index[boost::target(*ei, one_skeleton)]]) /
564 if (verbose) std::clog <<
"resolution = " << reso << std::endl;
565 resolution_double = reso;
596 if (resolution_double == -1 && resolution_int == -1) {
597 std::cerr <<
"Number and/or length of intervals not specified" << std::endl;
601 std::cerr <<
"Gain not specified" << std::endl;
606 double minf = (std::numeric_limits<float>::max)();
607 double maxf = std::numeric_limits<float>::lowest();
608 for (
int i = 0; i < this->num_points; i++) {
609 minf = (std::min)(minf, func[i]);
610 maxf = (std::max)(maxf, func[i]);
612 if (verbose) std::clog <<
"Min function value = " << minf <<
" and Max function value = " << maxf << std::endl;
615 std::vector<std::pair<double, double> > intervals;
618 if (resolution_double == -1) {
619 double incr = (maxf - minf) / resolution_int;
621 double alpha = (incr * gain) / (2 - 2 * gain);
622 double y = minf + incr + alpha;
623 std::pair<double, double> interm(x, y);
624 intervals.push_back(interm);
625 for (
int i = 1; i < resolution_int - 1; i++) {
626 x = minf + i * incr - alpha;
627 y = minf + (i + 1) * incr + alpha;
628 std::pair<double, double> inter(x, y);
629 intervals.push_back(inter);
631 x = minf + (resolution_int - 1) * incr - alpha;
633 std::pair<double, double> interM(x, y);
634 intervals.push_back(interM);
635 res = intervals.size();
637 for (
int i = 0; i < res; i++)
638 std::clog <<
"Interval " << i <<
" = [" << intervals[i].first <<
", " << intervals[i].second <<
"]"
642 if (resolution_int == -1) {
644 double y = x + resolution_double;
645 while (y <= maxf && maxf - (y - gain * resolution_double) >= resolution_double) {
646 std::pair<double, double> inter(x, y);
647 intervals.push_back(inter);
648 x = y - gain * resolution_double;
649 y = x + resolution_double;
651 std::pair<double, double> interM(x, maxf);
652 intervals.push_back(interM);
653 res = intervals.size();
655 for (
int i = 0; i < res; i++)
656 std::clog <<
"Interval " << i <<
" = [" << intervals[i].first <<
", " << intervals[i].second <<
"]"
661 double y = x + resolution_double;
663 while (count < resolution_int && y <= maxf && maxf - (y - gain * resolution_double) >= resolution_double) {
664 std::pair<double, double> inter(x, y);
665 intervals.push_back(inter);
667 x = y - gain * resolution_double;
668 y = x + resolution_double;
670 res = intervals.size();
672 for (
int i = 0; i < res; i++)
673 std::clog <<
"Interval " << i <<
" = [" << intervals[i].first <<
", " << intervals[i].second <<
"]"
680 std::vector<int> points(this->num_points);
681 for (
int i = 0; i < this->num_points; i++) points[i] = i;
682 std::sort(points.begin(), points.end(), [
this](
int p1,
int p2){return (this->func[p1] < this->func[p2]);});
686 Index_map index = boost::get(boost::vertex_index, one_skeleton);
687 std::map<int, std::vector<int> > preimages;
688 std::map<int, double> funcstd;
690 if (verbose) std::clog <<
"Computing preimages..." << std::endl;
691 for (
int i = 0; i < res; i++) {
693 std::pair<double, double> inter1 = intervals[i];
699 std::pair<double, double> inter3 = intervals[i - 1];
700 while (func[points[tmp]] < inter3.second && tmp != this->num_points) {
701 preimages[i].push_back(points[tmp]);
709 std::pair<double, double> inter2 = intervals[i + 1];
710 while (func[points[tmp]] < inter2.first && tmp != this->num_points) {
711 preimages[i].push_back(points[tmp]);
716 while (func[points[tmp]] < inter1.second && tmp != this->num_points) {
717 preimages[i].push_back(points[tmp]);
722 std::pair<double, double> inter3 = intervals[i - 1];
723 while (func[points[tmp]] < inter3.second && tmp != this->num_points) {
724 preimages[i].push_back(points[tmp]);
727 while (tmp != this->num_points) {
728 preimages[i].push_back(points[tmp]);
735 funcstd[i] = 0.5 * (u + v);
739 if (verbose) std::clog <<
"Computing connected components (parallelized)..." << std::endl;
740 std::mutex covermutex, idmutex;
741 tbb::parallel_for(0, res, [&](
int i){
743 Graph G = one_skeleton.create_subgraph();
744 int num = preimages[i].size();
745 std::vector<int> component(num);
746 for (
int j = 0; j < num; j++) boost::add_vertex(index[vertices[preimages[i][j]]], G);
747 boost::connected_components(G, &component[0]);
751 for (
int j = 0; j < num; j++) {
753 if (component[j] > max) max = component[j];
756 int identifier = ((i + component[j])*(i + component[j]) + 3 * i + component[j]) / 2;
760 cover[preimages[i][j]].push_back(identifier);
761 cover_back[identifier].push_back(preimages[i][j]);
762 cover_fct[identifier] = i;
763 cover_std[identifier] = funcstd[i];
764 cover_color[identifier].second += func_color[preimages[i][j]];
765 cover_color[identifier].first += 1;
775 if (verbose) std::clog <<
"Computing connected components..." << std::endl;
776 for (
int i = 0; i < res; i++) {
778 Graph G = one_skeleton.create_subgraph();
779 int num = preimages[i].size();
780 std::vector<int> component(num);
781 for (
int j = 0; j < num; j++) boost::add_vertex(index[vertices[preimages[i][j]]], G);
782 boost::connected_components(G, &component[0]);
786 for (
int j = 0; j < num; j++) {
788 if (component[j] > max) max = component[j];
791 int identifier = (std::pow(i + component[j], 2) + 3 * i + component[j]) / 2;
794 cover[preimages[i][j]].push_back(identifier);
795 cover_back[identifier].push_back(preimages[i][j]);
796 cover_fct[identifier] = i;
797 cover_std[identifier] = funcstd[i];
798 cover_color[identifier].second += func_color[preimages[i][j]];
799 cover_color[identifier].first += 1;
807 maximal_dim =
id - 1;
808 for (std::map<
int, std::pair<int, double> >::iterator iit = cover_color.begin(); iit != cover_color.end(); iit++)
809 iit->second.second /= iit->second.first;
822 std::vector<int> cov_elts, cov_number;
823 std::ifstream input(cover_file_name);
825 while (std::getline(input, line)) {
827 std::stringstream stream(line);
828 while (stream >> cov) {
829 cov_elts.push_back(cov);
830 cov_number.push_back(cov);
831 cover_fct[cov] = cov;
832 cover_color[cov].second += func_color[i];
833 cover_color[cov].first++;
834 cover_back[cov].push_back(i);
840 std::sort(cov_number.begin(), cov_number.end());
841 std::vector<int>::iterator it = std::unique(cov_number.begin(), cov_number.end());
842 cov_number.resize(std::distance(cov_number.begin(), it));
844 maximal_dim = cov_number.size() - 1;
845 for (
int i = 0; i <= maximal_dim; i++) cover_color[i].second /= cover_color[i].first;
846 cover_name = cover_file_name;
856 template <
class AssignmentRange>
858 std::vector<int> cov_elts, cov_number;
859 for(
int i=0; i < static_cast<int>(assignments.size()); i++){
861 for (
int cov : assignments[i]){
862 cov_elts.push_back(cov);
863 cov_number.push_back(cov);
864 cover_fct[cov] = cov;
865 auto& cc = cover_color[cov];
866 cc.second += func_color[i];
868 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;
888 template <
typename Distance>
890 voronoi_subsamples.resize(m);
891 SampleWithoutReplacement(this->num_points, m, voronoi_subsamples);
892 if (distances.size() == 0) compute_pairwise_distances(distance);
894 Weight_map weight = boost::get(boost::edge_weight, one_skeleton);
895 Index_map index = boost::get(boost::vertex_index, one_skeleton);
896 std::vector<double> mindist(this->num_points);
897 for (
int j = 0; j < this->num_points; j++) mindist[j] = (std::numeric_limits<double>::max)();
901 if (verbose) std::clog <<
"Computing geodesic distances (parallelized)..." << std::endl;
902 std::mutex coverMutex; std::mutex mindistMutex;
903 tbb::parallel_for(0, m, [&](
int i){
904 int seed = voronoi_subsamples[i];
905 std::vector<double> dmap(this->num_points);
906 boost::dijkstra_shortest_paths(
907 one_skeleton, vertices[seed],
908 boost::weight_map(weight).distance_map(boost::make_iterator_property_map(dmap.begin(), index)));
910 coverMutex.lock(); mindistMutex.lock();
911 for (
int j = 0; j < this->num_points; j++)
912 if (mindist[j] > dmap[j]) {
913 mindist[j] = dmap[j];
914 if (cover[j].size() == 0)
915 cover[j].push_back(i);
919 coverMutex.unlock(); mindistMutex.unlock();
922 for (
int i = 0; i < m; i++) {
923 if (verbose) std::clog <<
"Computing geodesic distances to seed " << i <<
"..." << std::endl;
924 int seed = voronoi_subsamples[i];
925 std::vector<double> dmap(this->num_points);
926 boost::dijkstra_shortest_paths(
927 one_skeleton, vertices[seed],
928 boost::weight_map(weight).distance_map(boost::make_iterator_property_map(dmap.begin(), index)));
930 for (
int j = 0; j < this->num_points; j++)
931 if (mindist[j] > dmap[j]) {
932 mindist[j] = dmap[j];
933 if (cover[j].size() == 0)
934 cover[j].push_back(i);
941 for (
int i = 0; i < this->num_points; i++) {
942 cover_back[cover[i][0]].push_back(i);
943 cover_color[cover[i][0]].second += func_color[i];
944 cover_color[cover[i][0]].first++;
946 for (
int i = 0; i < m; i++) cover_color[i].second /= cover_color[i].first;
948 cover_name =
"Voronoi";
968 double subcolor(
int c) {
return cover_color[c].second; }
982 std::ifstream input(color_file_name);
985 while (std::getline(input, line)) {
986 std::stringstream stream(line);
988 func_color.push_back(f);
990 color_name = color_file_name;
1000 if(point_cloud[0].size() > 0){
1001 for (
int i = 0; i < this->num_points; i++) func_color.push_back(point_cloud[i][k]);
1002 color_name =
"coordinate ";
1003 color_name.append(std::to_string(k));
1006 std::cerr <<
"Only pairwise distances provided---cannot access " << k <<
"th coordinate; returning null vector instead" << std::endl;
1007 for (
int i = 0; i < this->num_points; i++) func.push_back(0.0);
1008 functional_cover =
true;
1009 cover_name =
"null";
1020 for (
unsigned int i = 0; i < color.size(); i++) func_color.push_back(color[i]);
1029 std::string mapp = point_cloud_name +
"_sc.dot";
1030 std::ofstream graphic(mapp);
1032 double maxv = std::numeric_limits<double>::lowest();
1033 double minv = (std::numeric_limits<double>::max)();
1034 for (std::map<
int, std::pair<int, double> >::iterator iit = cover_color.begin(); iit != cover_color.end(); iit++) {
1035 maxv = (std::max)(maxv, iit->second.second);
1036 minv = (std::min)(minv, iit->second.second);
1039 std::vector<int> nodes;
1042 graphic <<
"graph GIC {" << std::endl;
1044 for (std::map<
int, std::pair<int, double> >::iterator iit = cover_color.begin(); iit != cover_color.end(); iit++) {
1045 if (iit->second.first > mask) {
1046 nodes.push_back(iit->first);
1047 name2id[iit->first] = id;
1048 name2idinv[id] = iit->first;
1050 graphic << name2id[iit->first] <<
"[shape=circle fontcolor=black color=black label=\"" << name2id[iit->first]
1051 <<
":" << iit->second.first <<
"\" style=filled fillcolor=\""
1052 << (1 - (maxv - iit->second.second) / (maxv - minv)) * 0.6 <<
", 1, 1\"]" << std::endl;
1055 int num_simplices = simplices.size();
1056 for (
int i = 0; i < num_simplices; i++)
1057 if (simplices[i].size() == 2) {
1058 if (cover_color[simplices[i][0]].first > mask && cover_color[simplices[i][1]].first > mask) {
1059 graphic <<
" " << name2id[simplices[i][0]] <<
" -- " << name2id[simplices[i][1]] <<
" [weight=15];"
1065 std::clog << mapp <<
" file generated. It can be visualized with e.g. neato." << std::endl;
1073 int num_simplices = simplices.size();
1075 std::string mapp = point_cloud_name +
"_sc.txt";
1076 std::ofstream graphic(mapp);
1078 for (
int i = 0; i < num_simplices; i++)
1079 if (simplices[i].size() == 2)
1080 if (cover_color[simplices[i][0]].first > mask && cover_color[simplices[i][1]].first > mask) num_edges++;
1082 graphic << point_cloud_name << std::endl;
1083 graphic << cover_name << std::endl;
1084 graphic << color_name << std::endl;
1085 graphic << resolution_double <<
" " << gain << std::endl;
1086 graphic << cover_color.size() <<
" " << num_edges << std::endl;
1089 for (std::map<
int, std::pair<int, double> >::iterator iit = cover_color.begin(); iit != cover_color.end(); iit++) {
1090 graphic <<
id <<
" " << iit->second.second <<
" " << iit->second.first << std::endl;
1091 name2id[iit->first] = id;
1092 name2idinv[id] = iit->first;
1096 for (
int i = 0; i < num_simplices; i++)
1097 if (simplices[i].size() == 2)
1098 if (cover_color[simplices[i][0]].first > mask && cover_color[simplices[i][1]].first > mask)
1099 graphic << name2id[simplices[i][0]] <<
" " << name2id[simplices[i][1]] << std::endl;
1102 <<
" generated. It can be visualized with e.g. python KeplerMapperVisuFromTxtFile.py and firefox."
1112 assert(cover_name ==
"Voronoi");
1114 int m = voronoi_subsamples.size();
1117 std::vector<std::vector<int> > edges, faces;
1118 int numsimplices = simplices.size();
1120 std::string mapp = point_cloud_name +
"_sc.off";
1121 std::ofstream graphic(mapp);
1123 graphic <<
"OFF" << std::endl;
1124 for (
int i = 0; i < numsimplices; i++) {
1125 if (simplices[i].size() == 2) {
1127 edges.push_back(simplices[i]);
1129 if (simplices[i].size() == 3) {
1131 faces.push_back(simplices[i]);
1134 graphic << m <<
" " << numedges + numfaces << std::endl;
1135 for (
int i = 0; i < m; i++) {
1136 if (data_dimension <= 3) {
1137 for (
int j = 0; j < data_dimension; j++) graphic << point_cloud[voronoi_subsamples[i]][j] <<
" ";
1138 for (
int j = data_dimension; j < 3; j++) graphic << 0 <<
" ";
1139 graphic << std::endl;
1141 for (
int j = 0; j < 3; j++) graphic << point_cloud[voronoi_subsamples[i]][j] <<
" ";
1144 for (
int i = 0; i < numedges; i++) graphic << 2 <<
" " << edges[i][0] <<
" " << edges[i][1] << std::endl;
1145 for (
int i = 0; i < numfaces; i++)
1146 graphic << 3 <<
" " << faces[i][0] <<
" " << faces[i][1] <<
" " << faces[i][2] << std::endl;
1148 std::clog << mapp <<
" generated. It can be visualized with e.g. geomview." << std::endl;
1163 double maxf = std::numeric_limits<double>::lowest();
1164 double minf = (std::numeric_limits<double>::max)();
1165 for (std::map<int, double>::iterator it = cover_std.begin(); it != cover_std.end(); it++) {
1166 maxf = (std::max)(maxf, it->second);
1167 minf = (std::min)(minf, it->second);
1171 for (
auto const& simplex : simplices) {
1172 std::vector<int> splx = simplex;
1177 for (std::map<int, double>::iterator it = cover_std.begin(); it != cover_std.end(); it++) {
1178 int vertex = it->first;
float val = it->second;
1179 int vert[] = {vertex};
int edge[] = {vertex, -2};
1192 int max_dim = st.dimension();
1193 for (
int i = 0; i < max_dim; i++) {
1195 int num_bars = bars.size();
if(i == 0) num_bars -= 1;
1196 if(verbose) std::clog << num_bars <<
" interval(s) in dimension " << i <<
":" << std::endl;
1197 for (
int j = 0; j < num_bars; j++) {
1198 double birth = bars[j].first;
1199 double death = bars[j].second;
1200 if (i == 0 && std::isinf(death))
continue;
1202 birth = minf + (birth + 2) * (maxf - minf);
1204 birth = minf + (2 - birth) * (maxf - minf);
1206 death = minf + (death + 2) * (maxf - minf);
1208 death = minf + (2 - death) * (maxf - minf);
1209 PD.push_back(std::pair<double, double>(birth, death));
1210 if (verbose) std::clog <<
" [" << birth <<
", " << death <<
"]" << std::endl;
1223 unsigned int sz = distribution.size();
1225 for (
unsigned int i = 0; i < N - sz; i++) {
1226 if (verbose) std::clog <<
"Computing " << i <<
"th bootstrap, bottleneck distance = ";
1228 Cover_complex Cboot; Cboot.num_points = this->num_points; Cboot.data_dimension = this->data_dimension; Cboot.type = this->type; Cboot.functional_cover =
true;
1230 std::vector<int> boot(this->num_points);
1231 for (
int j = 0; j < this->num_points; j++) {
1233 int id = std::floor(u * (this->num_points)); boot[j] = id;
1234 Cboot.point_cloud.push_back(this->point_cloud[
id]); Cboot.cover.emplace_back(); Cboot.func.push_back(this->func[
id]);
1235 boost::add_vertex(Cboot.one_skeleton_OFF); Cboot.vertices.push_back(boost::add_vertex(Cboot.one_skeleton));
1239 for (
int j = 0; j < this->num_points; j++) {
1240 std::vector<double> dist(this->num_points);
1241 for (
int k = 0; k < this->num_points; k++) dist[k] = distances[boot[j]][boot[k]];
1242 Cboot.distances.push_back(dist);
1251#ifdef GUDHI_GIC_USE_CGAL
1253#elif defined GUDHI_GIC_USE_HERA
1254 double db = hera::bottleneckDistExact(this->PD, Cboot.PD);
1257 throw std::logic_error(
"This function requires CGAL or Hera for the bottleneck distance.");
1259 if (verbose) std::clog << db << std::endl;
1260 distribution.push_back(db);
1263 std::sort(distribution.begin(), distribution.end());
1274 unsigned int N = distribution.size();
1275 double d = distribution[std::floor(alpha * N)];
1276 if (verbose) std::clog <<
"Distance corresponding to confidence " << alpha <<
" is " << d << std::endl;
1287 unsigned int N = distribution.size();
1289 for (
unsigned int i = 0; i < N; i++)
1290 if (distribution[i] >= d){ level = i * 1.0 / N;
break; }
1291 if (verbose) std::clog <<
"Confidence level of distance " << d <<
" is " << level << std::endl;
1301 double distancemin = (std::numeric_limits<double>::max)();
int N = PD.size();
1302 for (
int i = 0; i < N; i++) distancemin = (std::min)(distancemin, 0.5 * std::abs(PD[i].second - PD[i].first));
1304 if (verbose) std::clog <<
"p value = " << p_value << std::endl;
1318 template <
typename SimplicialComplex>
1320 unsigned int dimension = 0;
1321 for (
auto const& simplex : simplices) {
1322 int numvert = simplex.size();
1323 double filt = std::numeric_limits<double>::lowest();
1324 for (
int i = 0; i < numvert; i++) filt = (std::max)(cover_color[simplex[i]].second, filt);
1325 complex.insert_simplex_and_subfaces(simplex, filt);
1326 if (dimension < simplex.size() - 1) dimension = simplex.size() - 1;
1334 if (type !=
"Nerve" && type !=
"GIC") {
1335 std::cerr <<
"Type of complex needs to be specified." << std::endl;
1339 if (type ==
"Nerve") {
1341 std::sort(simplices.begin(), simplices.end());
1342 std::vector<std::vector<int> >::iterator it = std::unique(simplices.begin(), simplices.end());
1343 simplices.resize(std::distance(simplices.begin(), it));
1346 if (type ==
"GIC") {
1347 Index_map index = boost::get(boost::vertex_index, one_skeleton);
1349 if (functional_cover) {
1354 throw std::invalid_argument(
1355 "the output of this function is correct ONLY if the cover is minimal, i.e. the gain is less than 0.5.");
1358 boost::graph_traits<Graph>::edge_iterator ei, ei_end;
1359 for (boost::tie(ei, ei_end) = boost::edges(one_skeleton); ei != ei_end; ++ei) {
1360 int nums = cover[index[boost::source(*ei, one_skeleton)]].size();
1361 for (
int i = 0; i < nums; i++) {
1362 int vs = cover[index[boost::source(*ei, one_skeleton)]][i];
1363 int numt = cover[index[boost::target(*ei, one_skeleton)]].size();
1364 for (
int j = 0; j < numt; j++) {
1365 int vt = cover[index[boost::target(*ei, one_skeleton)]][j];
1366 if (cover_fct[vs] == cover_fct[vt] + 1 || cover_fct[vt] == cover_fct[vs] + 1) {
1367 std::vector<int> edge(2);
1368 edge[0] = (std::min)(vs, vt);
1369 edge[1] = (std::max)(vs, vt);
1370 simplices.push_back(edge);
1377 std::sort(simplices.begin(), simplices.end());
1378 std::vector<std::vector<int> >::iterator it = std::unique(simplices.begin(), simplices.end());
1379 simplices.resize(std::distance(simplices.begin(), it));
1384 boost::graph_traits<Graph>::edge_iterator ei, ei_end;
1385 for (boost::tie(ei, ei_end) = boost::edges(one_skeleton); ei != ei_end; ++ei)
1386 if (!(cover[index[boost::target(*ei, one_skeleton)]].size() == 1 &&
1387 cover[index[boost::target(*ei, one_skeleton)]] == cover[index[boost::source(*ei, one_skeleton)]])) {
1388 std::vector<int> edge(2);
1389 edge[0] = index[boost::source(*ei, one_skeleton)];
1390 edge[1] = index[boost::target(*ei, one_skeleton)];
1403 std::vector<int> simplx;
1405 unsigned int sz = cover[vertex].size();
1406 for (
unsigned int i = 0; i < sz; i++) {
1407 simplx.push_back(cover[vertex][i]);
1410 std::sort(simplx.begin(), simplx.end());
1411 std::vector<int>::iterator it = std::unique(simplx.begin(), simplx.end());
1412 simplx.resize(std::distance(simplx.begin(), it));
1413 simplices.push_back(simplx);
1416 std::sort(simplices.begin(), simplices.end());
1417 std::vector<std::vector<int> >::iterator it = std::unique(simplices.begin(), simplices.end());
1418 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:109
Complex_simplex_range complex_simplex_range() const
Returns a range over the simplices of the simplicial complex.
Definition Simplex_tree.h:348
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:2374
Simplex_vertex_range simplex_vertex_range(Simplex_handle sh) const
Returns a range over the vertices of a simplex.
Definition Simplex_tree.h:418
std::pair< Simplex_handle, bool > insert_simplex_and_subfaces(const InputVertexRange &n_simplex, const Filtration_value &filtration=Filtration_value())
Inserts a N-simplex and all his subfaces, from a N-simplex represented by a range of Vertex_handles,...
Definition Simplex_tree.h:1240
bool has_children(SimplexHandle sh) const
Returns true if the node in the simplex tree pointed by the given simplex handle has children.
Definition Simplex_tree.h:961
void expansion(int max_dimension)
Expands the Simplex_tree containing only its one skeleton until dimension max_dim.
Definition Simplex_tree.h:1834
void assign_filtration(Simplex_handle sh, const Filtration_value &fv)
Sets the filtration value of a simplex.
Definition Simplex_tree.h:792
Simplex_handle find(const InputVertexRange &s) const
Given a range of Vertex_handles, returns the Simplex_handle of the simplex in the simplicial complex ...
Definition Simplex_tree.h:986
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:802
Cover complex data structure.
Definition GIC.h:100
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:479
double set_automatic_resolution()
Computes the optimal length of intervals (i.e. the smallest interval length avoiding discretization a...
Definition GIC.h:536
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:889
void set_resolution_with_interval_number(int reso)
Sets a number of intervals from a value stored in memory.
Definition GIC.h:583
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:213
Persistence_diagram compute_PD()
Computes the extended persistence diagram of the complex.
Definition GIC.h:1159
double compute_distance_from_confidence_level(double alpha)
Computes the bottleneck distance threshold corresponding to a specific confidence level.
Definition GIC.h:1273
void set_graph_from_rips(double threshold, Distance distance)
Creates a graph G from a Rips complex.
Definition GIC.h:354
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:819
void set_graph_from_file(const std::string &graph_file_name)
Creates a graph G from a file containing the edges.
Definition GIC.h:321
void set_cover_from_range(AssignmentRange const &assignments)
Creates the cover C from a vector of assignments stored in memory. The assignments,...
Definition GIC.h:857
void create_complex(SimplicialComplex &complex)
Creates the simplicial complex.
Definition GIC.h:1319
void set_type(const std::string &t)
Specifies whether the type of the output simplicial complex.
Definition GIC.h:182
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:384
void find_simplices()
Computes the simplices of the simplicial complex.
Definition GIC.h:1333
void set_function_from_range(InputRange const &function)
Creates the function f from a vector stored in memory.
Definition GIC.h:519
void set_cover_from_function()
Creates a cover C from the preimages of the function f.
Definition GIC.h:595
double subcolor(int c)
Returns the mean color corresponding to a specific node of the created complex.
Definition GIC.h:968
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:981
void write_info()
Creates a .txt file called SC.txt describing the 1-skeleton, which can then be plotted with e....
Definition GIC.h:1072
void plot_OFF()
Creates a .off file called SC.off for 3D visualization, which contains the 2-skeleton of the GIC....
Definition GIC.h:1111
void plot_DOT()
Creates a .dot file called SC.dot for neato (part of the graphviz package) once the simplicial comple...
Definition GIC.h:1028
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:1019
void set_subsampling(double constant, double power)
Sets the constants used to subsample the data set. These constants are explained in carriere17c.
Definition GIC.h:200
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:223
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:425
void set_resolution_with_interval_length(double reso)
Sets a length of intervals from a value stored in memory.
Definition GIC.h:577
const std::vector< int > & subpopulation(int c)
Returns the data subset corresponding to a specific node of the created complex.
Definition GIC.h:958
void set_gain(double g=0.3)
Sets a gain from a value stored in memory (default value 0.3).
Definition GIC.h:589
void set_function_from_coordinate(int k)
Creates the function f from the k-th coordinate of the point cloud P.
Definition GIC.h:498
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:999
void compute_distribution(unsigned int N=100)
Computes bootstrapped distances distribution.
Definition GIC.h:1222
double compute_confidence_level_from_distance(double d)
Computes the confidence level of a specific bottleneck distance threshold.
Definition GIC.h:1286
void set_graph_from_OFF()
Creates a graph G from the triangulation given by the input .OFF file.
Definition GIC.h:338
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:1300
bool read_point_cloud(const std::string &off_file_name)
Reads and stores the input point cloud from .(n)OFF file.
Definition GIC.h:238
void set_verbose(bool verb=false)
Specifies whether the program should display information or not.
Definition GIC.h:190
Computes the persistent cohomology of a filtered complex.
Definition Persistent_cohomology.h:59
std::vector< std::pair< Filtration_value, Filtration_value > > intervals_in_dimension(int dimension)
Returns persistence intervals for a given dimension.
Definition Persistent_cohomology.h:708
void compute_persistent_cohomology(Filtration_value min_interval_length=0)
Compute the persistent homology of the filtered simplicial complex.
Definition Persistent_cohomology.h:173
void init_coefficients(int charac)
Initializes the coefficient field.
Definition Persistent_cohomology.h:157
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
Type get_uniform(const Type &min, const Type &max, CustomRandomGenerator &&rng=get_default_random())
Generates a random number in the range [min, max].
Definition random.h:117
Gudhi namespace.
Definition SimplicialComplexForAlpha.h:14
This file includes common file reader for GUDHI.