diff options
author | vrouvrea <vrouvrea@636b058d-ea47-450e-bf9e-a15bfbe3eedb> | 2017-06-28 05:37:45 +0000 |
---|---|---|
committer | vrouvrea <vrouvrea@636b058d-ea47-450e-bf9e-a15bfbe3eedb> | 2017-06-28 05:37:45 +0000 |
commit | 09925494f2d5010b05f11f418f2c10f99e2e0fdb (patch) | |
tree | 12aa108da3b0cf9bd7046426632b9d2d0d022077 /src/common | |
parent | d53ad3e2b527ec2df82b801a4026ffb77b97427d (diff) | |
parent | cdb39c39a60fe06009168c1ecb1d4ce5de0bed8c (diff) |
Merge of the branch read_persistence_from_file
git-svn-id: svn+ssh://scm.gforge.inria.fr/svnroot/gudhi/trunk@2564 636b058d-ea47-450e-bf9e-a15bfbe3eedb
Former-commit-id: b534b8866e25cf86db1483d141ac217c82b36384
Diffstat (limited to 'src/common')
-rw-r--r-- | src/common/doc/file_formats.h | 54 | ||||
-rw-r--r-- | src/common/include/gudhi/reader_utils.h | 78 |
2 files changed, 130 insertions, 2 deletions
diff --git a/src/common/doc/file_formats.h b/src/common/doc/file_formats.h new file mode 100644 index 00000000..c145b271 --- /dev/null +++ b/src/common/doc/file_formats.h @@ -0,0 +1,54 @@ +/* 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): Clément Jamin +* +* Copyright (C) 2017 INRIA +* +* This program is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program. If not, see <http://www.gnu.org/licenses/>. +*/ + +#ifndef DOC_COMMON_FILE_FORMAT_H_ +#define DOC_COMMON_FILE_FORMAT_H_ + +namespace Gudhi { + +/*! \page fileformats File formats + + \tableofcontents + + \section FileFormatsPers Persistence Diagram + + Such a file, whose extension is usually `.pers`, contains a list of persistence intervals.<br> + Lines starting with `#` are ignored (comments).<br> + Other lines might contain 2, 3 or 4 values (the number of values on each line must be the same for all lines): + \code{.unparsed} + [[field] dimension] birth death + \endcode + + Here is a simple sample file: + \code{.unparsed} + # Beautiful persistence diagram + 2 2.7 3.7 + 2 9.6 14. + 3 34.2 34.974 + 4 3. inf + \endcode + + Other sample files can be found in the `data/persistence_diagram` folder. +*/ +} // namespace Gudhi + +#endif // DOC_COMMON_FILE_FORMAT_H_ diff --git a/src/common/include/gudhi/reader_utils.h b/src/common/include/gudhi/reader_utils.h index 97a87edd..f1684d78 100644 --- a/src/common/include/gudhi/reader_utils.h +++ b/src/common/include/gudhi/reader_utils.h @@ -2,7 +2,7 @@ * (Geometric Understanding in Higher Dimensions) is a generic C++ * library for computational topology. * - * Author(s): Clement Maria, Pawel Dlotko + * Author(s): Clement Maria, Pawel Dlotko, Clement Jamin * * Copyright (C) 2014 INRIA * @@ -24,7 +24,9 @@ #define READER_UTILS_H_ #include <gudhi/graph_simplicial_complex.h> +#include <gudhi/Debug_utils.h> +#include <boost/function_output_iterator.hpp> #include <boost/graph/adjacency_list.hpp> #include <iostream> @@ -92,7 +94,10 @@ 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()) { - std::cerr << "Unable to open file " << file_name << std::endl; + std::string error_str("read_graph - Unable to open file "); + error_str.append(file_name); + std::cerr << error_str << std::endl; + throw std::invalid_argument(error_str); } typedef std::pair< Vertex_handle, Vertex_handle > Edge_t; @@ -295,4 +300,73 @@ std::vector< std::vector< Filtration_value > > read_lower_triangular_matrix_from return result; } // read_lower_triangular_matrix_from_csv_file +/** +Reads a file containing persistence intervals. +Each line might contain 2, 3 or 4 values: [[field] dimension] birth death +The output iterator `out` is used this way: `*out++ = std::make_tuple(dim, birth, death);` +where `dim` is an `int`, `birth` a `double`, and `death` a `double`. +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 "); + error_str.append(filename); + std::cerr << error_str << std::endl; + throw std::invalid_argument(error_str); + } + + while (!in.eof()) { + std::string line; + getline(in, line); + if (line.length() != 0 && line[0] != '#') { + 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. +Each line might contain 2, 3 or 4 values: [[field] dimension] birth death +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) { + + 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))); })); + 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` +(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) { + + 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)); })); + return ret; +} // read_persistence_diagram_from_file + #endif // READER_UTILS_H_ |