summaryrefslogtreecommitdiff
path: root/src/common/include/gudhi/reader_utils.h
diff options
context:
space:
mode:
authorvrouvrea <vrouvrea@636b058d-ea47-450e-bf9e-a15bfbe3eedb>2017-08-08 12:48:40 +0000
committervrouvrea <vrouvrea@636b058d-ea47-450e-bf9e-a15bfbe3eedb>2017-08-08 12:48:40 +0000
commit1129608c2add6f15538b3e281d75119f0e1bc8b0 (patch)
tree35204fa3c8c195857c76a507d922898fa95cb30f /src/common/include/gudhi/reader_utils.h
parentb8fdc4c7f6c30df5e7a28a0a23e697640f9d67cc (diff)
Fix bug in read_persistence_intervals_in_dimension for only_this_dim
Add unitary tests for read_persistence Add .clang-format to clang format new files git-svn-id: svn+ssh://scm.gforge.inria.fr/svnroot/gudhi/trunk@2601 636b058d-ea47-450e-bf9e-a15bfbe3eedb Former-commit-id: 28e870973f9ebd686d503246202fa6d6a549e9e3
Diffstat (limited to 'src/common/include/gudhi/reader_utils.h')
-rw-r--r--src/common/include/gudhi/reader_utils.h93
1 files changed, 43 insertions, 50 deletions
diff --git a/src/common/include/gudhi/reader_utils.h b/src/common/include/gudhi/reader_utils.h
index f1684d78..bda93f4f 100644
--- a/src/common/include/gudhi/reader_utils.h
+++ b/src/common/include/gudhi/reader_utils.h
@@ -1,5 +1,5 @@
-/* This file is part of the Gudhi Library. The Gudhi library
- * (Geometric Understanding in Higher Dimensions) is a generic C++
+/* This file is part of the Gudhi Library. The Gudhi library
+ * (Geometric Understanding in Higher Dimensions) is a generic C++
* library for computational topology.
*
* Author(s): Clement Maria, Pawel Dlotko, Clement Jamin
@@ -36,6 +36,7 @@
#include <string>
#include <vector>
#include <utility> // for pair
+#include <tuple> // for std::make_tuple
// Keep this file tag for Doxygen to parse the code, otherwise, functions are not documented.
// It is required for global functions and variables.
@@ -52,7 +53,7 @@
* X21 X22 ... X2d<br>
* etc<br>
*/
-inline void read_points(std::string file_name, std::vector< std::vector< double > > & points) {
+inline void read_points(std::string file_name, std::vector<std::vector<double>>& points) {
std::ifstream in_file(file_name.c_str(), std::ios::in);
if (!in_file.is_open()) {
std::cerr << "Unable to open file " << file_name << std::endl;
@@ -62,14 +63,13 @@ inline void read_points(std::string file_name, std::vector< std::vector< double
std::string line;
double x;
while (getline(in_file, line)) {
- std::vector< double > point;
+ std::vector<double> point;
std::istringstream iss(line);
while (iss >> x) {
point.push_back(x);
}
// Check for empty lines
- if (!point.empty())
- points.push_back(point);
+ if (!point.empty()) points.push_back(point);
}
in_file.close();
}
@@ -90,7 +90,7 @@ inline void read_points(std::string file_name, std::vector< std::vector< double
* Every simplex must appear exactly once.
* Simplices of dimension more than 1 are ignored.
*/
-template< typename Graph_t, typename Filtration_value, typename Vertex_handle >
+template <typename Graph_t, typename Filtration_value, typename Vertex_handle>
Graph_t read_graph(std::string file_name) {
std::ifstream in_(file_name.c_str(), std::ios::in);
if (!in_.is_open()) {
@@ -100,10 +100,10 @@ Graph_t read_graph(std::string file_name) {
throw std::invalid_argument(error_str);
}
- typedef std::pair< Vertex_handle, Vertex_handle > Edge_t;
- std::vector< Edge_t > edges;
- std::vector< Filtration_value > edges_fil;
- std::map< Vertex_handle, Filtration_value > vertices;
+ typedef std::pair<Vertex_handle, Vertex_handle> Edge_t;
+ std::vector<Edge_t> edges;
+ std::vector<Filtration_value> edges_fil;
+ std::map<Vertex_handle, Filtration_value> vertices;
std::string line;
int dim;
@@ -113,8 +113,7 @@ Graph_t read_graph(std::string file_name) {
std::istringstream iss(line);
while (iss >> dim) {
switch (dim) {
- case 0:
- {
+ case 0: {
iss >> u;
iss >> fil;
vertices[u] = fil;
@@ -123,8 +122,7 @@ Graph_t read_graph(std::string file_name) {
}
break;
}
- case 1:
- {
+ case 1: {
iss >> u;
iss >> v;
iss >> fil;
@@ -132,16 +130,13 @@ Graph_t read_graph(std::string file_name) {
edges_fil.push_back(fil);
break;
}
- default:
- {
- break;
- }
+ default: { break; }
}
}
}
in_.close();
- if ((size_t) (max_h + 1) != vertices.size()) {
+ if ((size_t)(max_h + 1) != vertices.size()) {
std::cerr << "Error: vertices must be labeled from 0 to n-1 \n";
}
@@ -169,8 +164,8 @@ Graph_t read_graph(std::string file_name) {
* Every simplex must appear exactly once.
* Simplices of dimension more than 1 are ignored.
*/
-template< typename Vertex_handle, typename Filtration_value >
-bool read_simplex(std::istream & in_, std::vector< Vertex_handle > & simplex, Filtration_value & fil) {
+template <typename Vertex_handle, typename Filtration_value>
+bool read_simplex(std::istream& in_, std::vector<Vertex_handle>& simplex, Filtration_value& fil) {
int dim = 0;
if (!(in_ >> dim)) return false;
Vertex_handle v;
@@ -194,8 +189,8 @@ bool read_simplex(std::istream & in_, std::vector< Vertex_handle > & simplex, Fi
* The key of a simplex is its position in the filtration order and also the number of its row in the file.
* Dimi ki1 ki2 ... kiDimi Fili means that the ith simplex in the filtration has dimension Dimi, filtration value
* fil1 and simplices with key ki1 ... kiDimi in its boundary.*/
-template< typename Simplex_key, typename Filtration_value >
-bool read_hasse_simplex(std::istream & in_, std::vector< Simplex_key > & boundary, Filtration_value & fil) {
+template <typename Simplex_key, typename Filtration_value>
+bool read_hasse_simplex(std::istream& in_, std::vector<Simplex_key>& boundary, Filtration_value& fil) {
int dim;
if (!(in_ >> dim)) return false;
if (dim == 0) {
@@ -214,7 +209,7 @@ bool read_hasse_simplex(std::istream & in_, std::vector< Simplex_key > & boundar
/**
* @brief Read a lower triangular distance matrix from a csv file. We assume that the .csv store the whole
* (square) matrix.
- *
+ *
* @author Pawel Dlotko
*
* Square matrix file format:<br>
@@ -231,13 +226,13 @@ bool read_hasse_simplex(std::istream & in_, std::vector< Simplex_key > & boundar
* Dj1;Dj2;...;Dj(j-1);<br>
*
**/
-template< typename Filtration_value >
-std::vector< std::vector< Filtration_value > > read_lower_triangular_matrix_from_csv_file(const std::string& filename,
- const char separator = ';') {
+template <typename Filtration_value>
+std::vector<std::vector<Filtration_value>> read_lower_triangular_matrix_from_csv_file(const std::string& filename,
+ const char separator = ';') {
#ifdef DEBUG_TRACES
std::cout << "Using procedure read_lower_triangular_matrix_from_csv_file \n";
#endif // DEBUG_TRACES
- std::vector< std::vector< Filtration_value > > result;
+ std::vector<std::vector<Filtration_value>> result;
std::ifstream in;
in.open(filename.c_str());
if (!in.is_open()) {
@@ -248,7 +243,7 @@ std::vector< std::vector< Filtration_value > > read_lower_triangular_matrix_from
// the first line is emtpy, so we ignore it:
std::getline(in, line);
- std::vector< Filtration_value > values_in_this_line;
+ std::vector<Filtration_value> values_in_this_line;
result.push_back(values_in_this_line);
int number_of_line = 0;
@@ -256,11 +251,10 @@ std::vector< std::vector< Filtration_value > > read_lower_triangular_matrix_from
// first, read the file line by line to a string:
while (std::getline(in, line)) {
// if line is empty, break
- if (line.size() == 0)
- break;
+ if (line.size() == 0) break;
// if the last element of a string is comma:
- if (line[ line.size() - 1 ] == separator) {
+ if (line[line.size() - 1] == separator) {
// then shrink the string by one
line.pop_back();
}
@@ -273,7 +267,7 @@ std::vector< std::vector< Filtration_value > > read_lower_triangular_matrix_from
// and now read the doubles.
int number_of_entry = 0;
- std::vector< Filtration_value > values_in_this_line;
+ std::vector<Filtration_value> values_in_this_line;
while (iss.good()) {
double entry;
iss >> entry;
@@ -282,7 +276,7 @@ std::vector< std::vector< Filtration_value > > read_lower_triangular_matrix_from
}
++number_of_entry;
}
- if (!values_in_this_line.empty())result.push_back(values_in_this_line);
+ if (!values_in_this_line.empty()) result.push_back(values_in_this_line);
++number_of_line;
}
in.close();
@@ -309,7 +303,6 @@ Note: the function does not check that birth <= death.
**/
template <typename OutputIterator>
void read_persistence_intervals_and_dimension(std::string const& filename, OutputIterator out) {
-
std::ifstream in(filename);
if (!in.is_open()) {
std::string error_str("read_persistence_intervals_and_dimension - Unable to open file ");
@@ -325,13 +318,12 @@ void read_persistence_intervals_and_dimension(std::string const& filename, Outpu
double numbers[4];
int n = sscanf(line.c_str(), "%lf %lf %lf %lf", &numbers[0], &numbers[1], &numbers[2], &numbers[3]);
if (n >= 2) {
- //int field = (n == 4 ? static_cast<int>(numbers[0]) : -1);
int dim = (n >= 3 ? static_cast<int>(numbers[n - 3]) : -1);
*out++ = std::make_tuple(dim, numbers[n - 2], numbers[n - 1]);
}
}
}
-} // read_persistence_diagram_from_file
+}
/**
Reads a file containing persistence intervals.
@@ -340,33 +332,34 @@ The return value is an `std::map<dim, std::vector<std::pair<birth, death>>>`
where `dim` is an `int`, `birth` a `double`, and `death` a `double`.
Note: the function does not check that birth <= death.
**/
-inline std::map<int, std::vector<std::pair<double, double>>> read_persistence_intervals_grouped_by_dimension(std::string const& filename) {
-
+inline std::map<int, std::vector<std::pair<double, double>>> read_persistence_intervals_grouped_by_dimension(
+ std::string const& filename) {
std::map<int, std::vector<std::pair<double, double>>> ret;
read_persistence_intervals_and_dimension(
- filename,
- boost::make_function_output_iterator([&ret](std::tuple<int, double, double> t) { ret[get<0>(t)].push_back(std::make_pair(get<1>(t), get<2>(t))); }));
+ filename, boost::make_function_output_iterator([&ret](std::tuple<int, double, double> t) {
+ ret[get<0>(t)].push_back(std::make_pair(get<1>(t), get<2>(t)));
+ }));
return ret;
-} // read_persistence_diagram_from_file
-
+}
/**
Reads a file containing persistence intervals.
Each line might contain 2, 3 or 4 values: [[field] dimension] birth death
If `only_this_dim` = -1, dimension is ignored and all lines are returned.
-If `only_this_dim` is >= 0, only the lines where dimension = `only_this_dim`
+If `only_this_dim` is >= 0, only the lines where dimension = `only_this_dim`
(or where dimension is not specified) are returned.
The return value is an `std::vector<std::pair<birth, death>>`
where `dim` is an `int`, `birth` a `double`, and `death` a `double`.
Note: the function does not check that birth <= death.
**/
-inline std::vector<std::pair<double, double>> read_persistence_intervals_in_dimension(std::string const& filename, int only_this_dim = -1) {
-
+inline std::vector<std::pair<double, double>> read_persistence_intervals_in_dimension(std::string const& filename,
+ int only_this_dim = -1) {
std::vector<std::pair<double, double>> ret;
read_persistence_intervals_and_dimension(
- filename,
- boost::make_function_output_iterator([&ret](std::tuple<int, double, double> t) { ret.emplace_back(get<1>(t), get<2>(t)); }));
+ filename, boost::make_function_output_iterator([only_this_dim, &ret](std::tuple<int, double, double> t) {
+ if (only_this_dim == get<0>(t) || only_this_dim == -1) ret.emplace_back(get<1>(t), get<2>(t));
+ }));
return ret;
-} // read_persistence_diagram_from_file
+}
#endif // READER_UTILS_H_