PSSK.h
1 /* This file is part of the Gudhi Library. The Gudhi library
2  * (Geometric Understanding in Higher Dimensions) is a generic C++
3  * library for computational topology.
4  *
5  * Author(s): Pawel Dlotko
6  *
7  * Copyright (C) 2016 Inria
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <http://www.gnu.org/licenses/>.
21  */
22 
23 #ifndef PSSK_H_
24 #define PSSK_H_
25 
26 // gudhi include
27 #include <gudhi/Persistence_heat_maps.h>
28 
29 #include <limits>
30 #include <utility>
31 #include <vector>
32 
33 namespace Gudhi {
34 namespace Persistence_representations {
35 
46 class PSSK : public Persistence_heat_maps<constant_scaling_function> {
47  public:
49 
50  PSSK(const std::vector<std::pair<double, double> >& interval,
51  std::vector<std::vector<double> > filter = create_Gaussian_filter(5, 1), size_t number_of_pixels = 1000,
52  double min_ = -1, double max_ = -1)
54  this->construct(interval, filter, number_of_pixels, min_, max_);
55  }
56 
57  PSSK(const char* filename, std::vector<std::vector<double> > filter = create_Gaussian_filter(5, 1),
58  size_t number_of_pixels = 1000, double min_ = -1, double max_ = -1,
59  unsigned dimension = std::numeric_limits<unsigned>::max())
61  std::vector<std::pair<double, double> > intervals_;
62  if (dimension == std::numeric_limits<unsigned>::max()) {
63  intervals_ = read_persistence_intervals_in_one_dimension_from_file(filename);
64  } else {
65  intervals_ = read_persistence_intervals_in_one_dimension_from_file(filename, dimension);
66  }
67  this->construct(intervals_, filter, number_of_pixels, min_, max_);
68  }
69 
70  protected:
71  void construct(const std::vector<std::pair<double, double> >& intervals_,
72  std::vector<std::vector<double> > filter = create_Gaussian_filter(5, 1),
73  size_t number_of_pixels = 1000, double min_ = -1, double max_ = -1);
74 };
75 
76 // if min_ == max_, then the program is requested to set up the values itself based on persistence intervals
77 void PSSK::construct(const std::vector<std::pair<double, double> >& intervals_,
78  std::vector<std::vector<double> > filter, size_t number_of_pixels, double min_, double max_) {
79  bool dbg = false;
80  if (dbg) {
81  std::cerr << "Entering construct procedure \n";
82  getchar();
83  }
84 
85  if (min_ == max_) {
86  // in this case, we want the program to set up the min_ and max_ values by itself.
87  min_ = std::numeric_limits<int>::max();
88  max_ = -std::numeric_limits<int>::max();
89 
90  for (size_t i = 0; i != intervals_.size(); ++i) {
91  if (intervals_[i].first < min_) min_ = intervals_[i].first;
92  if (intervals_[i].second > max_) max_ = intervals_[i].second;
93  }
94  // now we have the structure filled in, and moreover we know min_ and max_ values of the interval, so we know the
95  // range.
96 
97  // add some more space:
98  min_ -= fabs(max_ - min_) / 100;
99  max_ += fabs(max_ - min_) / 100;
100  }
101 
102  if (dbg) {
103  std::cerr << "min_ : " << min_ << std::endl;
104  std::cerr << "max_ : " << max_ << std::endl;
105  std::cerr << "number_of_pixels : " << number_of_pixels << std::endl;
106  getchar();
107  }
108 
109  this->min_ = min_;
110  this->max_ = max_;
111 
112  // initialization of the structure heat_map
113  std::vector<std::vector<double> > heat_map_;
114  for (size_t i = 0; i != number_of_pixels; ++i) {
115  std::vector<double> v(number_of_pixels, 0);
116  heat_map_.push_back(v);
117  }
118  this->heat_map = heat_map_;
119 
120  if (dbg) std::cerr << "Done creating of the heat map, now we will fill in the structure \n";
121 
122  for (size_t pt_nr = 0; pt_nr != intervals_.size(); ++pt_nr) {
123  // compute the value of intervals_[pt_nr] in the grid:
124  int x_grid =
125  static_cast<int>((intervals_[pt_nr].first - this->min_) / (this->max_ - this->min_) * number_of_pixels);
126  int y_grid =
127  static_cast<int>((intervals_[pt_nr].second - this->min_) / (this->max_ - this->min_) * number_of_pixels);
128 
129  if (dbg) {
130  std::cerr << "point : " << intervals_[pt_nr].first << " , " << intervals_[pt_nr].second << std::endl;
131  std::cerr << "x_grid : " << x_grid << std::endl;
132  std::cerr << "y_grid : " << y_grid << std::endl;
133  }
134 
135  // x_grid and y_grid gives a center of the kernel. We want to have its lower left corner. To get this, we need to
136  // shift x_grid and y_grid by a grid diameter.
137  x_grid -= filter.size() / 2;
138  y_grid -= filter.size() / 2;
139  // note that the numbers x_grid and y_grid may be negative.
140 
141  if (dbg) {
142  std::cerr << "After shift : \n";
143  std::cerr << "x_grid : " << x_grid << std::endl;
144  std::cerr << "y_grid : " << y_grid << std::endl;
145  std::cerr << "filter.size() : " << filter.size() << std::endl;
146  getchar();
147  }
148 
149  for (size_t i = 0; i != filter.size(); ++i) {
150  for (size_t j = 0; j != filter.size(); ++j) {
151  // if the point (x_grid+i,y_grid+j) is the correct point in the grid.
152  if (((x_grid + i) >= 0) && (x_grid + i < this->heat_map.size()) && ((y_grid + j) >= 0) &&
153  (y_grid + j < this->heat_map.size())) {
154  if (dbg) {
155  std::cerr << y_grid + j << " " << x_grid + i << std::endl;
156  }
157  this->heat_map[y_grid + j][x_grid + i] += filter[i][j];
158  this->heat_map[x_grid + i][y_grid + j] += -filter[i][j];
159  }
160  }
161  }
162  }
163 } // construct
164 
165 } // namespace Persistence_representations
166 } // namespace Gudhi
167 
168 #endif // PSSK_H_
A class implementing persistence heat maps.
Definition: Persistence_heat_maps.h:187
Definition: SimplicialComplexForAlpha.h:26
GUDHI  Version 2.2.0  - C++ library for Topological Data Analysis (TDA) and Higher Dimensional Geometry Understanding.  - Copyright : GPL v3 Generated on Thu Jun 14 2018 15:00:54 for GUDHI by Doxygen 1.8.13