11#ifndef READER_UTILS_H_
12#define READER_UTILS_H_
15#include <gudhi/Debug_utils.h>
17#if BOOST_VERSION < 106600
18# include <boost/function_output_iterator.hpp>
20# include <boost/iterator/function_output_iterator.hpp>
22#include <boost/graph/adjacency_list.hpp>
50inline void read_points(std::string file_name, std::vector<std::vector<double>>& points) {
51 std::ifstream in_file(file_name.c_str(), std::ios::in);
52 if (!in_file.is_open()) {
53 std::cerr <<
"Unable to open file " << file_name << std::endl;
59 while (getline(in_file, line)) {
60 std::vector<double> point;
61 std::istringstream iss(line);
66 if (!point.empty()) points.push_back(point);
87template <
typename Graph_t,
typename Filtration_value,
typename Vertex_handle>
89 std::ifstream in_(file_name.c_str(), std::ios::in);
91 std::string error_str(
"read_graph - Unable to open file ");
92 error_str.append(file_name);
93 std::cerr << error_str << std::endl;
94 throw std::invalid_argument(error_str);
97 typedef std::pair<Vertex_handle, Vertex_handle> Edge_t;
98 std::vector<Edge_t> edges;
99 std::vector<Filtration_value> edges_fil;
100 std::map<Vertex_handle, Filtration_value> vertices;
104 Vertex_handle u, v, max_h = -1;
106 while (getline(in_, line)) {
107 std::istringstream iss(line);
123 edges.push_back(Edge_t(u, v));
124 edges_fil.push_back(fil);
133 if ((
size_t)(max_h + 1) != vertices.size()) {
134 std::cerr <<
"Error: vertices must be labeled from 0 to n-1 \n";
137 Graph_t skel_graph(edges.begin(), edges.end(), edges_fil.begin(), vertices.size());
138 auto vertex_prop = boost::get(vertex_filtration_t(), skel_graph);
140 typename boost::graph_traits<Graph_t>::vertex_iterator vi, vi_end;
141 auto v_it = vertices.begin();
142 for (std::tie(vi, vi_end) = boost::vertices(skel_graph); vi != vi_end; ++vi, ++v_it) {
143 boost::put(vertex_prop, *vi, v_it->second);
161template <
typename Vertex_handle,
typename Filtration_value>
164 if (!(in_ >> dim))
return false;
166 for (
int i = 0; i < dim + 1; ++i) {
167 if (!(in_ >> v))
return false;
168 simplex.push_back(v);
170 if (!(in_ >> fil))
return false;
171 in_.ignore((std::numeric_limits<std::streamsize>::max)(),
'\n');
186template <
typename Simplex_key,
typename Filtration_value>
189 if (!(in_ >> dim))
return false;
195 for (
int i = 0; i < dim + 1; ++i) {
197 boundary.push_back(key);
223template <
typename Filtration_value>
225 const char separator =
';') {
227 std::clog <<
"Using procedure read_lower_triangular_matrix_from_csv_file \n";
229 std::vector<std::vector<Filtration_value>> result;
231 in.open(filename.c_str());
239 std::getline(in, line);
240 std::vector<Filtration_value> values_in_this_line;
241 result.push_back(values_in_this_line);
243 int number_of_line = 0;
246 while (std::getline(in, line)) {
248 if (line.size() == 0)
break;
251 if (line[line.size() - 1] == separator) {
257 std::replace(line.begin(), line.end(), separator,
' ');
260 std::istringstream iss(line);
263 int number_of_entry = 0;
264 std::vector<Filtration_value> values_in_this_line;
268 if (number_of_entry <= number_of_line) {
269 values_in_this_line.push_back(entry);
273 if (!values_in_this_line.empty()) result.push_back(values_in_this_line);
279 std::clog <<
"Here is the matrix we read : \n";
280 for (
size_t i = 0; i != result.size(); ++i) {
281 for (
size_t j = 0; j != result[i].size(); ++j) {
282 std::clog << result[i][j] <<
" ";
284 std::clog << std::endl;
298template <
typename OutputIterator>
301 std::clog <<
"read_persistence_intervals_and_dimension - " << filename << std::endl;
303 std::ifstream in(filename);
305 std::string error_str(
"read_persistence_intervals_and_dimension - Unable to open file ");
306 error_str.append(filename);
307 std::cerr << error_str << std::endl;
308 throw std::invalid_argument(error_str);
314 if (line.length() != 0 && line[0] !=
'#') {
316 int n = sscanf(line.c_str(),
"%lf %lf %lf %lf", &numbers[0], &numbers[1], &numbers[2], &numbers[3]);
318 std::clog <<
"[" << n <<
"] = ";
319 for (
int i = 0; i < n; i++) {
320 std::clog << numbers[i] <<
",";
322 std::clog << std::endl;
325 int dim = (n >= 3 ?
static_cast<int>(numbers[n - 3]) : -1);
326 *out++ = std::make_tuple(dim, numbers[n - 2], numbers[n - 1]);
340 std::string
const& filename) {
341 std::map<int, std::vector<std::pair<double, double>>> ret;
343 filename, boost::make_function_output_iterator([&ret](std::tuple<int, double, double> t) {
344 ret[get<0>(t)].push_back(std::make_pair(get<1>(t), get<2>(t)));
360 int only_this_dim = -1) {
361 std::vector<std::pair<double, double>> ret;
363 filename, boost::make_function_output_iterator([only_this_dim, &ret](std::tuple<int, double, double> t) {
364 if (only_this_dim == get<0>(t) || only_this_dim == -1) ret.emplace_back(get<1>(t), get<2>(t));
Graph simplicial complex methods.
bool read_hasse_simplex(std::istream &in_, std::vector< Simplex_key > &boundary, Filtration_value &fil)
Read a hasse simplex from a file.
Definition: reader_utils.h:187
Graph_t read_graph(std::string file_name)
Read a graph from a file.
Definition: reader_utils.h:88
std::vector< std::pair< double, double > > read_persistence_intervals_in_dimension(std::string const &filename, int only_this_dim=-1)
Definition: reader_utils.h:359
void read_persistence_intervals_and_dimension(std::string const &filename, OutputIterator out)
Definition: reader_utils.h:299
bool read_simplex(std::istream &in_, std::vector< Vertex_handle > &simplex, Filtration_value &fil)
Read a face from a file.
Definition: reader_utils.h:162
std::vector< std::vector< Filtration_value > > read_lower_triangular_matrix_from_csv_file(const std::string &filename, const char separator=';')
Read a lower triangular distance matrix from a csv file. We assume that the .csv store the whole (squ...
Definition: reader_utils.h:224
void read_points(std::string file_name, std::vector< std::vector< double > > &points)
Read a set of points to turn it into a vector< vector<double> > by filling points.
Definition: reader_utils.h:50
std::map< int, std::vector< std::pair< double, double > > > read_persistence_intervals_grouped_by_dimension(std::string const &filename)
Definition: reader_utils.h:339
Value type for a filtration function on a cell complex.
Definition: FiltrationValue.h:20