read_persistence_from_file.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 (France)
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 READ_PERSISTENCE_FROM_FILE_H_
24 #define READ_PERSISTENCE_FROM_FILE_H_
25 
26 #include <gudhi/reader_utils.h>
27 
28 #include <iostream>
29 #include <fstream>
30 #include <sstream>
31 #include <vector>
32 #include <algorithm>
33 #include <string>
34 #include <utility>
35 #include <limits> // for std::numeric_limits<>
36 
37 namespace Gudhi {
38 namespace Persistence_representations {
39 
54 std::vector<std::pair<double, double> > read_persistence_intervals_in_one_dimension_from_file(
55  std::string const& filename, int dimension = -1, double what_to_substitute_for_infinite_bar = -1) {
56  bool dbg = false;
57 
58  std::string line;
59  std::vector<std::pair<double, double> > barcode_initial =
60  read_persistence_intervals_in_dimension(filename, (int)dimension);
61  std::vector<std::pair<double, double> > final_barcode;
62  final_barcode.reserve(barcode_initial.size());
63 
64  if (dbg) {
65  std::cerr << "Here are the intervals that we read from the file : \n";
66  for (size_t i = 0; i != barcode_initial.size(); ++i) {
67  std::cout << barcode_initial[i].first << " " << barcode_initial[i].second << std::endl;
68  }
69  getchar();
70  }
71 
72  for (size_t i = 0; i != barcode_initial.size(); ++i) {
73  if (dbg) {
74  std::cout << "COnsidering interval : " << barcode_initial[i].first << " " << barcode_initial[i].second
75  << std::endl;
76  }
77 
78  if (barcode_initial[i].first > barcode_initial[i].second) {
79  // note that in this case barcode_initial[i].second != std::numeric_limits<double>::infinity()
80  if (dbg) std::cout << "Swap and enter \n";
81  // swap them to make sure that birth < death
82  final_barcode.push_back(std::pair<double, double>(barcode_initial[i].second, barcode_initial[i].first));
83  continue;
84  } else {
85  if (barcode_initial[i].second != std::numeric_limits<double>::infinity()) {
86  if (dbg) std::cout << "Simply enters\n";
87  // in this case, due to the previous conditions we know that barcode_initial[i].first <
88  // barcode_initial[i].second, so we put them as they are
89  final_barcode.push_back(std::pair<double, double>(barcode_initial[i].first, barcode_initial[i].second));
90  }
91  }
92 
93  if ((barcode_initial[i].second == std::numeric_limits<double>::infinity()) &&
94  (what_to_substitute_for_infinite_bar != -1)) {
95  if (barcode_initial[i].first < what_to_substitute_for_infinite_bar) // if only birth < death.
96  {
97  final_barcode.push_back(
98  std::pair<double, double>(barcode_initial[i].first, what_to_substitute_for_infinite_bar));
99  }
100  } else {
101  // if the variable what_to_substitute_for_infinite_bar is not set, then we ignore all the infinite bars.
102  }
103  }
104 
105  if (dbg) {
106  std::cerr << "Here are the final bars that we are sending further : \n";
107  for (size_t i = 0; i != final_barcode.size(); ++i) {
108  std::cout << final_barcode[i].first << " " << final_barcode[i].second << std::endl;
109  }
110  std::cerr << "final_barcode.size() : " << final_barcode.size() << std::endl;
111  getchar();
112  }
113 
114  return final_barcode;
115 } // read_persistence_intervals_in_one_dimension_from_file
116 
117 } // namespace Persistence_representations
118 } // namespace Gudhi
119 
120 #endif // READ_PERSISTENCE_FROM_FILE_H_
Definition: SimplicialComplexForAlpha.h:26
std::vector< std::pair< double, double > > read_persistence_intervals_in_dimension(std::string const &filename, int only_this_dim=-1)
Definition: reader_utils.h:357
This file includes common file reader for GUDHI.
GUDHI  Version 2.1.0  - C++ library for Topological Data Analysis (TDA) and Higher Dimensional Geometry Understanding.  - Copyright : GPL v3 Generated on Thu Jun 14 2018 18:07:51 for GUDHI by Doxygen 1.8.13