PETLS
sheaf_simplex_tree.hpp
Go to the documentation of this file.
1 #ifndef SST_H
2 #define SST_H
3 // wrap gudhi simplex tree with extra data
5 
6 
7 #include <gudhi/Simplex_tree.h>
8 #include <gudhi/Rips_complex.h>
9 #include <gudhi/distance_functions.h>
10 #include <vector>
11 #include "Eigen/SparseCore"
12 #include <unordered_map>
13 #include <cmath>
14 #include <iostream>
15 #include <algorithm>
16 
17 namespace petls{
18  using Simplex_tree = Gudhi::Simplex_tree<Gudhi::Simplex_tree_options_default>;
20  using Distance_matrix = std::vector<std::vector<Filtration_value>>;
22  public:
24  std::unordered_map<Simplex_tree::Simplex_key, std::vector<float>> extra_data;
25  std::function<float(Simplex_tree::Simplex_handle, Simplex_tree::Simplex_handle, sheaf_simplex_tree&)> restriction;
27  std::unordered_map<Simplex_tree::Simplex_key, std::vector<float>> _extra_data,
28  std::function<float(Simplex_tree::Simplex_handle, Simplex_tree::Simplex_handle, sheaf_simplex_tree&)> _restriction){
29  st = Simplex_tree(_st);
30  extra_data = _extra_data;
31  restriction = _restriction;
32  }
33  int coface_index(Simplex_tree::Simplex_handle simplex, Simplex_tree::Simplex_handle coface){
34  // get iterator for vertices of simplex and coface
35  Simplex_tree::Simplex_vertex_range vertex_range_simplex = this->st.simplex_vertex_range(simplex);
36  Simplex_tree::Simplex_vertex_range vertex_range_coface = this->st.simplex_vertex_range(coface);
37 
38  // convert to vector
39  std::vector<Simplex_tree::Vertex_handle> vec_simplex(vertex_range_simplex.begin(), vertex_range_simplex.end());
40  std::vector<Simplex_tree::Vertex_handle> vec_coface(vertex_range_coface.begin(), vertex_range_coface.end());
41 
42  // simplex_vertex_range returns simplices in reverse order
43  std::reverse(vec_simplex.begin(), vec_simplex.end());
44  std::reverse(vec_coface.begin(), vec_coface.end());
45 
46  // loop until no match
47  for (int i = 0; i < (int) vec_simplex.size(); i++){
48  if (vec_simplex[i] != vec_coface[i])
49  return i;
50  }
51  return vec_coface.size() - 1; // missing vertex is in last position
52  }
53 
54  std::vector<FilteredBoundaryMatrix<float>> apply_restriction_function(){
55  // std::function<float(Simplex_tree::Simplex_handle, Simplex_tree::Simplex_handle, sheaf_simplex_tree&)> f){
56  int complex_dim = this->st.dimension();
57  std::vector<size_t> dims = this->st.num_simplices_by_dimension();
58 
59  // intermediate sparse matrix storage as (row, col, coefficient)
60  // this is later reindexed then given to Eigen to create a SparseMatrixFloat (MatrixXf)
61  std::vector<std::vector<std::tuple<int64_t,int64_t,float>>> boundaries_triples;
62  std::vector<std::vector<std::pair<int,double>>> filtrations;
63 
64  std::vector<SparseMatrixFloat> coboundary_matrices;
65  std::vector<std::vector<double>> reindexed_filtrations;
66 
67  // Wrapper to keep explicit boundary matrices with the filtration of each simplex
68  std::vector<FilteredBoundaryMatrix<float>> coboundaries;
69 
70  // Initialize vectors for filtrations and boundary matrices
71  for (int dim = 0; dim < (int) complex_dim; dim++){
72  filtrations.push_back({});
73  boundaries_triples.push_back({});
74  }
75  filtrations.push_back({}); //for top dim
76 
77  // loop over all simplices
78 
79  for (auto sh : st.filtration_simplex_range()){
80  int dim = st.dimension(sh);
81  filtrations[dim].push_back(std::make_pair<int, float>(st.key(sh),st.filtration(sh)));
82  if (dim == complex_dim){ // zero coboundary from top dimension
83  continue;
84  }
85 
86  // loop over all codimension-1 cofaces
87  for (Simplex_tree::Simplex_handle c : st.cofaces_simplex_range(sh, 1)){
88 
89  float sign = (float) std::pow(-1.0, (double) (this->coface_index(sh, c) % 2));
90 
91  // apply the restriction function
92  float coeff = sign*restriction(sh, c, *this);
93 
94  // store as (row, col, coefficient)
95  boundaries_triples[dim].push_back(std::tuple<int64_t,int64_t,float>(st.key(c),st.key(sh),coeff));
96  }
97  }
98  reindex_boundaries_map(boundaries_triples,coboundary_matrices,filtrations, reindexed_filtrations);
99 
100  // convert from reindexed (row, col, coeff) to FilteredBoundaryMatrix (wraps Eigen::MatrixXf)
101  for (int dim = 0; dim < (int) complex_dim; dim++){
102  SparseMatrixFloat d = coboundary_matrices[dim];
103  std::vector<double> filt_domain = reindexed_filtrations[dim];
104  std::vector<double> filt_range = reindexed_filtrations[dim+1];
105  FilteredBoundaryMatrix<float> fbm(coboundary_matrices[dim],filt_domain, filt_range);
106  coboundaries.push_back(fbm);
107  }
108  return coboundaries;
109  }
110  private:
111  void reindex_boundaries_map(std::vector<std::vector<std::tuple<int64_t,int64_t,float>>> &boundaries_triples,std::vector<SparseMatrixFloat> &reindexed_boundaries,
112  std::vector<std::vector<std::pair<int,double>>> &filtrations, std::vector<std::vector<double>> &reindexed_filtrations){
113  std::vector<std::set<int64_t>> indices_of_actual_simplices_set(boundaries_triples.size()+1);
114  std::vector<std::vector<int>> indices_of_actual_simplices_vector(boundaries_triples.size()+1);
115 
116  std::vector<std::unordered_map<int64_t,int>> index_maps(filtrations.size());
117 
118  for (int i = 0; i < (int) filtrations.size(); i++){
119  // int dim = i+1;
120  std::vector<std::pair<int,double>> filtration_pairs_dim = filtrations[i]; // starts with dim 1 simplices
121 
122  // sort by filtration then by simplex number
123  std::sort(filtration_pairs_dim.begin(), filtration_pairs_dim.end(), [](auto &left, auto &right){
124  return (left.first < right.first) || (left.first == right.first && left.second < right.second);
125  });
126 
127  // build mappings
128  std::unordered_map<int64_t, int> temp_map(filtration_pairs_dim.size());
129  std::vector<double> filtrations_dim;
130  for (int j = 0; j < (int) filtration_pairs_dim.size(); j++){
131  temp_map[filtration_pairs_dim[j].first] = j;
132  filtrations_dim.push_back(filtration_pairs_dim[j].second);
133  }
134  reindexed_filtrations.push_back(filtrations_dim);
135  index_maps[i] = temp_map;
136  }
137 
138  for (int i = 0; i < (int) boundaries_triples.size(); i++){
139  int dim = i + 1;
140  std::vector<Eigen::Triplet<float>> tripletList;
141  std::vector<std::tuple<int64_t, int64_t, float>> boundary_triples = boundaries_triples[i];
142  for (int j = 0; j < (int) boundary_triples.size(); j++){
143  int row = index_maps[dim][std::get<0>(boundary_triples[j])];
144  int col = index_maps[dim-1][std::get<1>(boundary_triples[j])];
145  float coeff = std::get<2>(boundary_triples[j]);
146  boundary_triples[j] = std::make_tuple(row, col, coeff);
147  tripletList.push_back(Eigen::Triplet<float>(row, col, coeff));
148  }
149  // boundaries_triples[i] = boundary_triples;
150  SparseMatrixFloat boundary(index_maps[dim].size(), index_maps[dim-1].size());
151 
152  boundary.setFromTriplets(tripletList.begin(), tripletList.end());
153  reindexed_boundaries.push_back(boundary);
154  }
155 
156  }
157 
158  };
159 
160  petls::sheaf_simplex_tree rips_sheaf_simplex_tree(std::vector<std::vector<float>> points, Filtration_value max_length,
161  std::function<float(Simplex_tree::Simplex_handle, Simplex_tree::Simplex_handle, sheaf_simplex_tree&)> restriction){
162  using Rips_complex = Gudhi::rips_complex::Rips_complex<Filtration_value>;
163 
164  // This is an example of a function that constructs a sheaf simplex tree
165 
166 
167  // Important: We must wrap the Gudhi Rips_complex rips in our final sheaf_simplex_tree sst
168  // BEFORE calling rips.create_complex(sst.st, dim_max).
169  // This is because of how Gudhi copies certain information.
170 
171  // Declare a rips complex with the given point cloud,
172  // but do NOT call rips.create_complex yet.
173  Rips_complex rips(points,max_length,Gudhi::Euclidean_distance());
174 
175  // Declare but do NOT give a value to a Simplex_tree
177  int dim_max = 3;
178 
179  // Extra data is essentially a dictionary with keys of type Simplex_key and values of vector<float>
180  std::unordered_map<petls::Simplex_tree::Simplex_key, std::vector<float>> extra_data;
181 
182  // Construct sst as wrapping the declared simplex tree and extra data
183  petls::sheaf_simplex_tree sst(st, extra_data, restriction);
184 
185  // Store the rips complex in the simplex tree that has been wrapped.
186  rips.create_complex(sst.st, dim_max);
187 
188  // Assuming that Gudhi's rips complex preserves the order of points
189  int counter = 0;
190 
191  // Assign vertex keys and add (x,y,z) coordinates to extra_data
192  for (Simplex_tree::Vertex_handle v : sst.st.complex_vertex_range()){
193  Simplex_tree::Simplex_handle as_sh = sst.st.find({v});
194  sst.st.assign_key(as_sh, counter);
195  sst.extra_data[sst.st.key(as_sh)] = {points[counter][0],
196  points[counter][1],
197  points[counter][2]};
198  counter++;
199  }
200  // Assign the dimension >= 1 simplex keys
201  for (Simplex_tree::Simplex_handle sh : sst.st.filtration_simplex_range()){
202  if (sst.st.dimension(sh) == 0)
203  continue;
204  sst.st.assign_key(sh, counter++);
205  }
206  return sst;
207  }
208 
209 
210 
211 
212 }
213 
214 #endif
Definition: FilteredBoundaryMatrix.hpp:15
Definition: sheaf_simplex_tree.hpp:21
int coface_index(Simplex_tree::Simplex_handle simplex, Simplex_tree::Simplex_handle coface)
Definition: sheaf_simplex_tree.hpp:33
Simplex_tree st
Definition: sheaf_simplex_tree.hpp:23
sheaf_simplex_tree(Simplex_tree _st, std::unordered_map< Simplex_tree::Simplex_key, std::vector< float >> _extra_data, std::function< float(Simplex_tree::Simplex_handle, Simplex_tree::Simplex_handle, sheaf_simplex_tree &)> _restriction)
Definition: sheaf_simplex_tree.hpp:26
std::unordered_map< Simplex_tree::Simplex_key, std::vector< float > > extra_data
Definition: sheaf_simplex_tree.hpp:24
std::function< float(Simplex_tree::Simplex_handle, Simplex_tree::Simplex_handle, sheaf_simplex_tree &)> restriction
Definition: sheaf_simplex_tree.hpp:25
std::vector< FilteredBoundaryMatrix< float > > apply_restriction_function()
Definition: sheaf_simplex_tree.hpp:54
Definition: Complex.cpp:26
Gudhi::Simplex_tree< Gudhi::Simplex_tree_options_default > Simplex_tree
Definition: sheaf_simplex_tree.hpp:18
std::vector< std::vector< Filtration_value > > Distance_matrix
Definition: sheaf_simplex_tree.hpp:20
Simplex_tree::Filtration_value Filtration_value
Definition: sheaf_simplex_tree.hpp:19
petls::sheaf_simplex_tree rips_sheaf_simplex_tree(std::vector< std::vector< float >> points, Filtration_value max_length, std::function< float(Simplex_tree::Simplex_handle, Simplex_tree::Simplex_handle, sheaf_simplex_tree &)> restriction)
Definition: sheaf_simplex_tree.hpp:160
Eigen::SparseMatrix< float, Eigen::ColMajor > SparseMatrixFloat
Definition: typedefs.hpp:21