23 #ifndef READER_UTILS_H_ 24 #define READER_UTILS_H_ 26 #include <gudhi/graph_simplicial_complex.h> 27 #include <gudhi/Debug_utils.h> 29 #include <boost/function_output_iterator.hpp> 30 #include <boost/graph/adjacency_list.hpp> 58 inline void read_points(std::string file_name, std::vector<std::vector<double>>& points) {
59 std::ifstream in_file(file_name.c_str(), std::ios::in);
60 if (!in_file.is_open()) {
61 std::cerr <<
"Unable to open file " << file_name << std::endl;
67 while (getline(in_file, line)) {
68 std::vector<double> point;
69 std::istringstream iss(line);
74 if (!point.empty()) points.push_back(point);
95 template <
typename Graph_t,
typename Filtration_value,
typename Vertex_handle>
97 std::ifstream in_(file_name.c_str(), std::ios::in);
99 std::string error_str(
"read_graph - Unable to open file ");
100 error_str.append(file_name);
101 std::cerr << error_str << std::endl;
102 throw std::invalid_argument(error_str);
105 typedef std::pair<Vertex_handle, Vertex_handle> Edge_t;
106 std::vector<Edge_t> edges;
107 std::vector<Filtration_value> edges_fil;
108 std::map<Vertex_handle, Filtration_value> vertices;
112 Vertex_handle u, v, max_h = -1;
114 while (getline(in_, line)) {
115 std::istringstream iss(line);
131 edges.push_back(Edge_t(u, v));
132 edges_fil.push_back(fil);
141 if ((
size_t)(max_h + 1) != vertices.size()) {
142 std::cerr <<
"Error: vertices must be labeled from 0 to n-1 \n";
145 Graph_t skel_graph(edges.begin(), edges.end(), edges_fil.begin(), vertices.size());
146 auto vertex_prop = boost::get(vertex_filtration_t(), skel_graph);
148 typename boost::graph_traits<Graph_t>::vertex_iterator vi, vi_end;
149 auto v_it = vertices.begin();
150 for (std::tie(vi, vi_end) = boost::vertices(skel_graph); vi != vi_end; ++vi, ++v_it) {
151 boost::put(vertex_prop, *vi, v_it->second);
169 template <
typename Vertex_handle,
typename Filtration_value>
172 if (!(in_ >> dim))
return false;
174 for (
int i = 0; i < dim + 1; ++i) {
176 simplex.push_back(v);
179 in_.ignore((std::numeric_limits<std::streamsize>::max)(),
'\n');
194 template <
typename Simplex_key,
typename Filtration_value>
197 if (!(in_ >> dim))
return false;
203 for (
int i = 0; i < dim + 1; ++i) {
205 boundary.push_back(key);
231 template <
typename Filtration_value>
233 const char separator =
';') {
235 std::cout <<
"Using procedure read_lower_triangular_matrix_from_csv_file \n";
236 #endif // DEBUG_TRACES 237 std::vector<std::vector<Filtration_value>> result;
239 in.open(filename.c_str());
247 std::getline(in, line);
248 std::vector<Filtration_value> values_in_this_line;
249 result.push_back(values_in_this_line);
251 int number_of_line = 0;
254 while (std::getline(in, line)) {
256 if (line.size() == 0)
break;
259 if (line[line.size() - 1] == separator) {
265 std::replace(line.begin(), line.end(), separator,
' ');
268 std::istringstream iss(line);
271 int number_of_entry = 0;
272 std::vector<Filtration_value> values_in_this_line;
276 if (number_of_entry <= number_of_line) {
277 values_in_this_line.push_back(entry);
281 if (!values_in_this_line.empty()) result.push_back(values_in_this_line);
287 std::cerr <<
"Here is the matrix we read : \n";
288 for (
size_t i = 0; i != result.size(); ++i) {
289 for (
size_t j = 0; j != result[i].size(); ++j) {
290 std::cerr << result[i][j] <<
" ";
292 std::cerr << std::endl;
294 #endif // DEBUG_TRACES 306 template <
typename OutputIterator>
308 std::ifstream in(filename);
310 std::string error_str(
"read_persistence_intervals_and_dimension - Unable to open file ");
311 error_str.append(filename);
312 std::cerr << error_str << std::endl;
313 throw std::invalid_argument(error_str);
319 if (line.length() != 0 && line[0] !=
'#') {
321 int n = sscanf(line.c_str(),
"%lf %lf %lf %lf", &numbers[0], &numbers[1], &numbers[2], &numbers[3]);
323 int dim = (n >= 3 ?
static_cast<int>(numbers[n - 3]) : -1);
324 *out++ = std::make_tuple(dim, numbers[n - 2], numbers[n - 1]);
338 std::string
const& filename) {
339 std::map<int, std::vector<std::pair<double, double>>> ret;
341 filename, boost::make_function_output_iterator([&ret](std::tuple<int, double, double> t) {
342 ret[get<0>(t)].push_back(std::make_pair(get<1>(t), get<2>(t)));
358 int only_this_dim = -1) {
359 std::vector<std::pair<double, double>> ret;
361 filename, boost::make_function_output_iterator([only_this_dim, &ret](std::tuple<int, double, double> t) {
362 if (only_this_dim == get<0>(t) || only_this_dim == -1) ret.emplace_back(get<1>(t), get<2>(t));
369 #endif // READER_UTILS_H_ Graph_t read_graph(std::string file_name)
Read a graph from a file.
Definition: reader_utils.h:96
std::map< int, std::vector< std::pair< double, double > > > read_persistence_intervals_grouped_by_dimension(std::string const &filename)
Definition: reader_utils.h:337
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:232
Definition: SimplicialComplexForAlpha.h:26
void read_persistence_intervals_and_dimension(std::string const &filename, OutputIterator out)
Definition: reader_utils.h:307
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:58
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
Value type for a filtration function on a cell complex.
Definition: FiltrationValue.h:32
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:195
bool read_simplex(std::istream &in_, std::vector< Vertex_handle > &simplex, Filtration_value &fil)
Read a face from a file.
Definition: reader_utils.h:170