13 #ifndef PERSISTENCE_INTERVALS_H_
14 #define PERSISTENCE_INTERVALS_H_
17 #include <gudhi/read_persistence_from_file.h>
31 namespace Persistence_representations {
47 Persistence_intervals(
const char* filename,
unsigned dimension = std::numeric_limits<unsigned>::max());
60 double min_ = std::numeric_limits<int>::max();
61 double max_ = -std::numeric_limits<int>::max();
62 for (
size_t i = 0; i != this->intervals.size(); ++i) {
63 if (this->intervals[i].first < min_) min_ = this->intervals[i].first;
64 if (this->intervals[i].second > max_) max_ = this->intervals[i].second;
66 return std::make_pair(min_, max_);
73 double min_ = std::numeric_limits<int>::max();
74 double max_ = -std::numeric_limits<int>::max();
75 for (
size_t i = 0; i != this->intervals.size(); ++i) {
76 if (this->intervals[i].second < min_) min_ = this->intervals[i].second;
77 if (this->intervals[i].second > max_) max_ = this->intervals[i].second;
79 return std::make_pair(min_, max_);
92 std::vector<std::pair<double, double> >
dominant_intervals(
size_t where_to_cut = 100)
const;
124 size_t number_of_bins = 10)
const;
139 std::vector<double>
k_n_n(
size_t k,
size_t where_to_cut = 10)
const;
145 for (
size_t i = 0; i != intervals.intervals.size(); ++i) {
146 out << intervals.intervals[i].first <<
" " << intervals.intervals[i].second << std::endl;
154 void plot(
const char* filename,
double min_x = std::numeric_limits<double>::max(),
155 double max_x = std::numeric_limits<double>::max(),
double min_y = std::numeric_limits<double>::max(),
156 double max_y = std::numeric_limits<double>::max())
const {
160 std::stringstream gnuplot_script;
161 gnuplot_script << filename <<
"_GnuplotScript";
163 out.open(gnuplot_script.str().c_str());
165 std::pair<double, double> min_max_values = this->
get_x_range();
166 if (min_x == max_x) {
167 out <<
"set xrange [" << min_max_values.first - 0.1 * (min_max_values.second - min_max_values.first) <<
" : "
168 << min_max_values.second + 0.1 * (min_max_values.second - min_max_values.first) <<
" ]" << std::endl;
169 out <<
"set yrange [" << min_max_values.first - 0.1 * (min_max_values.second - min_max_values.first) <<
" : "
170 << min_max_values.second + 0.1 * (min_max_values.second - min_max_values.first) <<
" ]" << std::endl;
172 out <<
"set xrange [" << min_x <<
" : " << max_x <<
" ]" << std::endl;
173 out <<
"set yrange [" << min_y <<
" : " << max_y <<
" ]" << std::endl;
175 out <<
"plot '-' using 1:2 notitle \"" << filename <<
"\", \\" << std::endl;
176 out <<
" '-' using 1:2 notitle with lp" << std::endl;
177 for (
size_t i = 0; i != this->intervals.size(); ++i) {
178 out << this->intervals[i].first <<
" " << this->intervals[i].second << std::endl;
180 out <<
"EOF" << std::endl;
181 out << min_max_values.first - 0.1 * (min_max_values.second - min_max_values.first) <<
" "
182 << min_max_values.first - 0.1 * (min_max_values.second - min_max_values.first) << std::endl;
183 out << min_max_values.second + 0.1 * (min_max_values.second - min_max_values.first) <<
" "
184 << min_max_values.second + 0.1 * (min_max_values.second - min_max_values.first) << std::endl;
188 std::clog <<
"To visualize, install gnuplot and type the command: gnuplot -persist -e \"load \'"
189 << gnuplot_script.str().c_str() <<
"\'\"" << std::endl;
195 size_t size()
const {
return this->intervals.size(); }
201 inline std::pair<double, double>
operator[](
size_t i)
const {
202 if (i >= this->intervals.size())
throw(
"Index out of range! Operator [], one_d_gaussians class\n");
203 return this->intervals[i];
226 std::vector<double>
vectorize(
int number_of_function)
const {
238 std::vector<std::pair<double, double> > output_for_visualization() {
return this->intervals; }
241 void set_up_numbers_of_functions_for_vectorization_and_projections_to_reals() {
243 this->number_of_functions_for_vectorization = this->intervals.size();
244 this->number_of_functions_for_projections_to_reals = 1;
247 std::vector<std::pair<double, double> > intervals;
248 size_t number_of_functions_for_vectorization;
249 size_t number_of_functions_for_projections_to_reals;
253 if (dimension == std::numeric_limits<unsigned>::max()) {
254 this->intervals = read_persistence_intervals_in_one_dimension_from_file(filename);
256 this->intervals = read_persistence_intervals_in_one_dimension_from_file(filename, dimension);
258 this->set_up_numbers_of_functions_for_vectorization_and_projections_to_reals();
262 : intervals(intervals_) {
263 this->set_up_numbers_of_functions_for_vectorization_and_projections_to_reals();
267 std::vector<double> result(this->intervals.size());
268 for (
size_t i = 0; i != this->intervals.size(); ++i) {
269 result[i] = this->intervals[i].second - this->intervals[i].first;
271 std::sort(result.begin(), result.end(), std::greater<double>());
273 result.resize(std::min(where_to_cut, result.size()));
277 bool compare(
const std::pair<size_t, double>& first,
const std::pair<size_t, double>& second) {
278 return first.second > second.second;
283 std::vector<std::pair<size_t, double> > position_length_vector(this->intervals.size());
284 for (
size_t i = 0; i != this->intervals.size(); ++i) {
285 position_length_vector[i] = std::make_pair(i, this->intervals[i].second - this->intervals[i].first);
288 std::sort(position_length_vector.begin(), position_length_vector.end(), compare);
290 std::vector<std::pair<double, double> > result;
291 result.reserve(std::min(where_to_cut, position_length_vector.size()));
293 for (
size_t i = 0; i != std::min(where_to_cut, position_length_vector.size()); ++i) {
294 result.push_back(this->intervals[position_length_vector[i].first]);
296 std::clog <<
"Position : " << position_length_vector[i].first <<
" length : " << position_length_vector[i].second
306 if (dbg) std::clog <<
"this->intervals.size() : " << this->intervals.size() << std::endl;
308 double lengthOfLongest = 0;
309 for (
size_t i = 0; i != this->intervals.size(); ++i) {
310 if ((this->intervals[i].second - this->intervals[i].first) > lengthOfLongest) {
311 lengthOfLongest = this->intervals[i].second - this->intervals[i].first;
316 std::clog <<
"lengthOfLongest : " << lengthOfLongest << std::endl;
320 std::vector<size_t> result(number_of_bins + 1, 0);
323 for (
size_t i = 0; i != this->intervals.size(); ++i) {
325 double relative_length_of_this_interval = (this->intervals[i].second - this->intervals[i].first) / lengthOfLongest;
328 size_t position = (size_t)(relative_length_of_this_interval * number_of_bins);
333 std::clog <<
"i : " << i << std::endl;
334 std::clog <<
"Interval : [" << this->intervals[i].first <<
" , " << this->intervals[i].second <<
" ] \n";
335 std::clog <<
"relative_length_of_this_interval : " << relative_length_of_this_interval << std::endl;
336 std::clog <<
"position : " << position << std::endl;
341 result[number_of_bins-1]+=result[number_of_bins];
342 result.resize(number_of_bins);
345 for (
size_t i = 0; i != result.size(); ++i) std::clog << result[i] << std::endl;
352 std::vector<size_t> result(histogram.size());
355 for (
size_t i = 0; i != histogram.size(); ++i) {
363 size_t number_of_bins)
const {
366 std::vector<double> result(number_of_bins);
367 std::fill(result.begin(), result.end(), 0);
369 for (
size_t i = 0; i != this->intervals.size(); ++i) {
371 std::clog <<
"Interval : " << this->intervals[i].first <<
" , " << this->intervals[i].second << std::endl;
375 if (this->intervals[i].first < x_min) beginIt = 0;
376 if (this->intervals[i].first >= x_max) beginIt = result.size();
377 if ((this->intervals[i].first > x_min) && (this->intervals[i].first < x_max)) {
378 beginIt = number_of_bins * (this->intervals[i].first - x_min) / (x_max - x_min);
382 if (this->intervals[i].second < x_min) endIt = 0;
383 if (this->intervals[i].second >= x_max) endIt = result.size();
384 if ((this->intervals[i].second > x_min) && (this->intervals[i].second < x_max)) {
385 endIt = number_of_bins * (this->intervals[i].second - x_min) / (x_max - x_min);
388 if (beginIt > endIt) {
393 std::clog <<
"beginIt : " << beginIt << std::endl;
394 std::clog <<
"endIt : " << endIt << std::endl;
397 for (
size_t pos = beginIt; pos != endIt; ++pos) {
398 result[pos] += ((x_max - x_min) /
static_cast<double>(number_of_bins)) *
399 (this->intervals[i].second - this->intervals[i].first);
402 std::clog <<
"Result at this stage \n";
403 for (
size_t aa = 0; aa != result.size(); ++aa) {
404 std::clog << result[aa] <<
" ";
406 std::clog << std::endl;
413 size_t number_of_bins)
const {
415 std::vector<double> result(intsOfBars.size());
417 for (
size_t i = 0; i != intsOfBars.size(); ++i) {
418 sum += intsOfBars[i];
424 template <
typename T>
425 bool compare_first_element_of_pair(
const std::pair<T, bool>& f,
const std::pair<T, bool>& s) {
426 return (f.first < s.first);
430 std::vector<std::pair<double, bool> > places_where_pbs_change(2 * this->intervals.size());
432 for (
size_t i = 0; i != this->intervals.size(); ++i) {
433 places_where_pbs_change[2 * i] = std::make_pair(this->intervals[i].first,
true);
434 places_where_pbs_change[2 * i + 1] = std::make_pair(this->intervals[i].second,
false);
437 std::sort(places_where_pbs_change.begin(), places_where_pbs_change.end(), compare_first_element_of_pair<double>);
439 std::vector<std::pair<double, size_t> > pbns(places_where_pbs_change.size());
440 for (
size_t i = 0; i != places_where_pbs_change.size(); ++i) {
441 if (places_where_pbs_change[i].second ==
true) {
446 pbns[i] = std::make_pair(places_where_pbs_change[i].first, pbn);
451 inline double compute_euclidean_distance(
const std::pair<double, double>& f,
const std::pair<double, double>& s) {
452 return sqrt((f.first - s.first) * (f.first - s.first) + (f.second - s.second) * (f.second - s.second));
458 std::clog <<
"Here are the intervals : \n";
459 for (
size_t i = 0; i != this->intervals.size(); ++i) {
460 std::clog <<
"[ " << this->intervals[i].first <<
" , " << this->intervals[i].second <<
"] \n";
465 std::vector<double> result;
468 std::vector<std::vector<double> > distances(this->intervals.size());
469 for (
size_t i = 0; i != this->intervals.size(); ++i) {
470 std::vector<double> aa(this->intervals.size());
471 std::fill(aa.begin(), aa.end(), 0);
474 std::vector<double> distances_from_diagonal(this->intervals.size());
475 std::fill(distances_from_diagonal.begin(), distances_from_diagonal.end(), 0);
477 for (
size_t i = 0; i != this->intervals.size(); ++i) {
478 std::vector<double> distancesFromI;
479 for (
size_t j = i + 1; j != this->intervals.size(); ++j) {
480 distancesFromI.push_back(compute_euclidean_distance(this->intervals[i], this->intervals[j]));
483 double distanceToDiagonal = compute_euclidean_distance(
484 this->intervals[i], std::make_pair(0.5 * (this->intervals[i].first + this->intervals[i].second),
485 0.5 * (this->intervals[i].first + this->intervals[i].second)));
486 distances_from_diagonal[i] = distanceToDiagonal;
489 std::clog <<
"Here are the distances form the point : [" << this->intervals[i].first <<
" , "
490 << this->intervals[i].second <<
"] in the diagram \n";
491 for (
size_t aa = 0; aa != distancesFromI.size(); ++aa) {
492 std::clog <<
"To : " << i + aa <<
" : " << distancesFromI[aa] <<
" ";
494 std::clog << std::endl;
499 for (
size_t j = i + 1; j != this->intervals.size(); ++j) {
500 distances[i][j] = distancesFromI[j - i - 1];
501 distances[j][i] = distancesFromI[j - i - 1];
505 std::clog <<
"Here is the distance matrix : \n";
506 for (
size_t i = 0; i != distances.size(); ++i) {
507 for (
size_t j = 0; j != distances.size(); ++j) {
508 std::clog << distances[i][j] <<
" ";
510 std::clog << std::endl;
512 std::clog << std::endl << std::endl <<
"And here are the distances to the diagonal : " << std::endl;
513 for (
size_t i = 0; i != distances_from_diagonal.size(); ++i) {
514 std::clog << distances_from_diagonal[i] <<
" ";
516 std::clog << std::endl << std::endl;
520 for (
size_t i = 0; i != this->intervals.size(); ++i) {
521 std::vector<double> distancesFromI = distances[i];
522 distancesFromI.push_back(distances_from_diagonal[i]);
525 std::sort(distancesFromI.begin(), distancesFromI.end(), std::greater<double>());
527 if (k > distancesFromI.size()) {
529 std::clog <<
"There are not enough neighbors in your set. We set the result to plus infty \n";
531 result.push_back(std::numeric_limits<double>::max());
533 if (distances_from_diagonal[i] > distancesFromI[k]) {
535 std::clog <<
"The k-th n.n. is on a diagonal. Therefore we set up a distance to diagonal \n";
537 result.push_back(distances_from_diagonal[i]);
539 result.push_back(distancesFromI[k]);
543 std::sort(result.begin(), result.end(), std::greater<double>());
544 result.resize(std::min(result.size(), where_to_cut));
552 for (
size_t i = 0; i != this->intervals.size(); ++i) {
554 (this->intervals[i].second - this->intervals[i].first) * (this->intervals[i].second - this->intervals[i].first);
Definition: Persistence_intervals.h:37
std::vector< std::pair< double, size_t > > compute_persistent_betti_numbers() const
Definition: Persistence_intervals.h:429
double project_to_R(int number_of_function) const
Definition: Persistence_intervals.h:549
std::vector< double > cumulative_characteristic_function_of_diagram(double x_min, double x_max, size_t number_of_bins=10) const
Definition: Persistence_intervals.h:412
size_t number_of_projections_to_R() const
Definition: Persistence_intervals.h:220
std::vector< double > characteristic_function_of_diagram(double x_min, double x_max, size_t number_of_bins=10) const
Definition: Persistence_intervals.h:362
std::vector< std::pair< double, double > > dominant_intervals(size_t where_to_cut=100) const
Definition: Persistence_intervals.h:281
std::pair< double, double > get_y_range() const
Definition: Persistence_intervals.h:72
std::vector< size_t > cumulative_histogram_of_lengths(size_t number_of_bins=10) const
Definition: Persistence_intervals.h:350
void plot(const char *filename, double min_x=std::numeric_limits< double >::max(), double max_x=std::numeric_limits< double >::max(), double min_y=std::numeric_limits< double >::max(), double max_y=std::numeric_limits< double >::max()) const
Definition: Persistence_intervals.h:154
std::vector< double > length_of_dominant_intervals(size_t where_to_cut=100) const
Definition: Persistence_intervals.h:266
std::vector< size_t > histogram_of_lengths(size_t number_of_bins=10) const
Definition: Persistence_intervals.h:303
size_t number_of_vectorize_functions() const
Definition: Persistence_intervals.h:233
size_t size() const
Definition: Persistence_intervals.h:195
std::vector< double > k_n_n(size_t k, size_t where_to_cut=10) const
Definition: Persistence_intervals.h:455
friend std::ostream & operator<<(std::ostream &out, const Persistence_intervals &intervals)
Definition: Persistence_intervals.h:144
std::pair< double, double > get_x_range() const
Definition: Persistence_intervals.h:59
std::pair< double, double > operator[](size_t i) const
Definition: Persistence_intervals.h:201
std::vector< double > vectorize(int number_of_function) const
Definition: Persistence_intervals.h:226
Persistence_intervals(const char *filename, unsigned dimension=std::numeric_limits< unsigned >::max())
Definition: Persistence_intervals.h:252
Gudhi namespace.
Definition: SimplicialComplexForAlpha.h:14