Loading...
Searching...
No Matches
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 * - 2025/06 Hannah Schreiber: Various small bug fixes (missing `inline`s, `DEBUG_TRACES`s etc.)
9 * - YYYY/MM Author: Description of the modification
10 */
11
12#ifndef READ_PERSISTENCE_FROM_FILE_H_
13#define READ_PERSISTENCE_FROM_FILE_H_
14
15#ifdef DEBUG_TRACES
16#include <iostream> // std::clog
17#endif
18#include <utility> // std::pair
19#include <limits> // for std::numeric_limits
20#include <vector>
21#include <string>
22
23#include <gudhi/reader_utils.h>
24#include <gudhi/Debug_utils.h>
25
26namespace Gudhi {
27namespace Persistence_representations {
28
44inline std::vector<std::pair<double, double> > read_persistence_intervals_in_one_dimension_from_file(
45 std::string const& filename,
46 int dimension = -1,
47 double what_to_substitute_for_infinite_bar = -1)
48{
49 using Bar = std::pair<double, double>;
50 using Barcode = std::vector<Bar>;
51
52 // TODO: `when what_to_substitute_for_infinite_bar` != -1, make changes directly in `barcode_initial` to avoid
53 // allocating space?
54 // `final_barcode` makes sense when there are a lot of infinite bars which gets ignored (other
55 // solution would be to swap those bars to the end and end with a resize(). The now unused space won't be freed,
56 // but this do not differ from `final_barcode.reserve(barcode_initial.size())` used below).
57 Barcode barcode_initial = read_persistence_intervals_in_dimension(filename, dimension);
58 Barcode final_barcode;
59 final_barcode.reserve(barcode_initial.size());
60
61#ifdef DEBUG_TRACES
62 std::clog << "Here are the intervals that we read from the file : \n";
63 for (std::size_t i = 0; i != barcode_initial.size(); ++i) {
64 std::clog << barcode_initial[i].first << " " << barcode_initial[i].second << std::endl;
65 }
66#endif
67
68 for (std::size_t i = 0; i != barcode_initial.size(); ++i) {
69#ifdef DEBUG_TRACES
70 std::clog << "Considering interval : " << barcode_initial[i].first << " " << barcode_initial[i].second << std::endl;
71#endif
72
73 if (barcode_initial[i].first > barcode_initial[i].second) {
74 // note that in this case barcode_initial[i].second != std::numeric_limits<double>::infinity()
75#ifdef DEBUG_TRACES
76 std::clog << "Swap and enter \n";
77#endif
78 // swap them to make sure that birth < death
79 final_barcode.push_back(Bar(barcode_initial[i].second, barcode_initial[i].first));
80 } else {
81 if (barcode_initial[i].second != std::numeric_limits<double>::infinity()) {
82#ifdef DEBUG_TRACES
83 std::clog << "Simply enters\n";
84#endif
85 // in this case, due to the previous conditions we know that
86 // barcode_initial[i].first < barcode_initial[i].second, so we put them as they are
87 final_barcode.push_back(Bar(barcode_initial[i].first, barcode_initial[i].second));
88 } else if (what_to_substitute_for_infinite_bar != -1){
89 if (barcode_initial[i].first < what_to_substitute_for_infinite_bar) {
90 // if only birth < death.
91 final_barcode.push_back(Bar(barcode_initial[i].first, what_to_substitute_for_infinite_bar));
92 }
93 }
94 // if the variable what_to_substitute_for_infinite_bar is not set, then we ignore all the infinite bars.
95 }
96 }
97
98#ifdef DEBUG_TRACES
99 std::clog << "Here are the final bars that we are sending further : \n";
100 for (std::size_t i = 0; i != final_barcode.size(); ++i) {
101 std::clog << final_barcode[i].first << " " << final_barcode[i].second << std::endl;
102 }
103 std::clog << "final_barcode.size() : " << final_barcode.size() << std::endl;
104#endif
105
106 return final_barcode;
107} // read_persistence_intervals_in_one_dimension_from_file
108
109} // namespace Persistence_representations
110} // namespace Gudhi
111
112#endif // READ_PERSISTENCE_FROM_FILE_H_
std::vector< std::pair< double, double > > read_persistence_intervals_in_one_dimension_from_file(std::string const &filename, int dimension=-1, double what_to_substitute_for_infinite_bar=-1)
Definition read_persistence_from_file.h:44
Gudhi namespace.
Definition SimplicialComplexForAlpha.h:14
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
This file includes common file reader for GUDHI.