read_persistence_from_file.h
1 /* This file is part of the Gudhi Library - https://gudhi.inria.fr/ - which is released under MIT.
2  * See file LICENSE or go to https://gudhi.inria.fr/licensing/ for full license details.
3  * Author(s): Pawel Dlotko
4  *
5  * Copyright (C) 2016 Inria
6  *
7  * Modification(s):
8  * - YYYY/MM Author: Description of the modification
9  */
10 
11 #ifndef READ_PERSISTENCE_FROM_FILE_H_
12 #define READ_PERSISTENCE_FROM_FILE_H_
13 
14 #include <gudhi/reader_utils.h>
15 
16 #include <iostream>
17 #include <fstream>
18 #include <sstream>
19 #include <vector>
20 #include <algorithm>
21 #include <string>
22 #include <utility>
23 #include <limits> // for std::numeric_limits<>
24 
25 namespace Gudhi {
26 namespace Persistence_representations {
27 
42 std::vector<std::pair<double, double> > read_persistence_intervals_in_one_dimension_from_file(
43  std::string const& filename, int dimension = -1, double what_to_substitute_for_infinite_bar = -1) {
44  bool dbg = false;
45 
46  std::string line;
47  std::vector<std::pair<double, double> > barcode_initial =
48  read_persistence_intervals_in_dimension(filename, static_cast<int>(dimension));
49  std::vector<std::pair<double, double> > final_barcode;
50  final_barcode.reserve(barcode_initial.size());
51 
52  if (dbg) {
53  std::cerr << "Here are the intervals that we read from the file : \n";
54  for (size_t i = 0; i != barcode_initial.size(); ++i) {
55  std::cout << barcode_initial[i].first << " " << barcode_initial[i].second << std::endl;
56  }
57  getchar();
58  }
59 
60  for (size_t i = 0; i != barcode_initial.size(); ++i) {
61  if (dbg) {
62  std::cout << "COnsidering interval : " << barcode_initial[i].first << " " << barcode_initial[i].second
63  << std::endl;
64  }
65 
66  if (barcode_initial[i].first > barcode_initial[i].second) {
67  // note that in this case barcode_initial[i].second != std::numeric_limits<double>::infinity()
68  if (dbg) std::cout << "Swap and enter \n";
69  // swap them to make sure that birth < death
70  final_barcode.push_back(std::pair<double, double>(barcode_initial[i].second, barcode_initial[i].first));
71  continue;
72  } else {
73  if (barcode_initial[i].second != std::numeric_limits<double>::infinity()) {
74  if (dbg) std::cout << "Simply enters\n";
75  // in this case, due to the previous conditions we know that barcode_initial[i].first <
76  // barcode_initial[i].second, so we put them as they are
77  final_barcode.push_back(std::pair<double, double>(barcode_initial[i].first, barcode_initial[i].second));
78  }
79  }
80 
81  if ((barcode_initial[i].second == std::numeric_limits<double>::infinity()) &&
82  (what_to_substitute_for_infinite_bar != -1)) {
83  if (barcode_initial[i].first < what_to_substitute_for_infinite_bar) {
84  // if only birth < death.
85  final_barcode.push_back(
86  std::pair<double, double>(barcode_initial[i].first, what_to_substitute_for_infinite_bar));
87  }
88  } else {
89  // if the variable what_to_substitute_for_infinite_bar is not set, then we ignore all the infinite bars.
90  }
91  }
92 
93  if (dbg) {
94  std::cerr << "Here are the final bars that we are sending further : \n";
95  for (size_t i = 0; i != final_barcode.size(); ++i) {
96  std::cout << final_barcode[i].first << " " << final_barcode[i].second << std::endl;
97  }
98  std::cerr << "final_barcode.size() : " << final_barcode.size() << std::endl;
99  getchar();
100  }
101 
102  return final_barcode;
103 } // read_persistence_intervals_in_one_dimension_from_file
104 
105 } // namespace Persistence_representations
106 } // namespace Gudhi
107 
108 #endif // READ_PERSISTENCE_FROM_FILE_H_
Gudhi::read_persistence_intervals_in_dimension
std::vector< std::pair< double, double > > read_persistence_intervals_in_dimension(std::string const &filename, int only_this_dim=-1)
Definition: reader_utils.h:355
reader_utils.h
This file includes common file reader for GUDHI.
GUDHI  Version 3.1.0  - C++ library for Topological Data Analysis (TDA) and Higher Dimensional Geometry Understanding.  - Copyright : MIT Generated on Mon Feb 3 2020 20:32:13 for GUDHI by Doxygen 1.8.16