12 #ifndef PLSSVM_DETAIL_IO_SCALING_FACTORS_PARSING_HPP_
13 #define PLSSVM_DETAIL_IO_SCALING_FACTORS_PARSING_HPP_
28 #include <string_view>
56 template <
typename real_type,
typename factors_type>
70 std::vector<real_type> scale_to_interval = detail::split_as<real_type>(reader.
line(1));
71 if (scale_to_interval.size() != 2) {
72 throw invalid_file_format_exception{ fmt::format(
"The interval to which the data points should be scaled must exactly have two values, but {} were given!", scale_to_interval.size()) };
74 if (scale_to_interval[0] >= scale_to_interval[1]) {
75 throw invalid_file_format_exception{ fmt::format(
"Inconsistent scaling interval specification: lower ({}) must be less than upper ({})!", scale_to_interval[0], scale_to_interval[1]) };
79 std::exception_ptr parallel_exception;
80 std::vector<factors_type> scaling_factors(reader.
num_lines() - 2);
81 #pragma omp parallel default(none) shared(parallel_exception, scaling_factors, reader)
84 for (
typename std::vector<factors_type>::size_type i = 0; i < scaling_factors.size(); ++i) {
87 const std::string_view line = reader.
line(i + 2);
88 const std::vector<real_type> values = detail::split_as<real_type>(line);
90 if (values.size() != 3) {
94 const auto feature =
static_cast<decltype(scaling_factors[i].feature)
>(values[0]);
99 scaling_factors[i].feature = feature - 1;
100 scaling_factors[i].lower = values[1];
101 scaling_factors[i].upper = values[2];
102 }
catch (
const std::exception &) {
106 if (!parallel_exception) {
107 parallel_exception = std::current_exception();
114 if (parallel_exception) {
115 std::rethrow_exception(parallel_exception);
118 return std::make_tuple(std::make_pair(scale_to_interval[0], scale_to_interval[1]), std::move(scaling_factors));
138 template <
typename real_type,
typename factors_type>
139 inline void write_scaling_factors(
const std::string &filename,
const std::pair<real_type, real_type> &scaling_interval,
const std::vector<factors_type> &scaling_factors) {
140 PLSSVM_ASSERT(scaling_interval.first < scaling_interval.second,
"Illegal interval specification: lower ({}) < upper ({}).", scaling_interval.first, scaling_interval.second);
143 fmt::ostream out = fmt::output_file(filename);
150 out.print(
"{} {}\n", scaling_interval.first, scaling_interval.second);
152 for (
const factors_type &f : scaling_factors) {
153 out.print(
"{} {} {}\n", f.feature + 1, f.lower, f.upper);
Implements a custom assert macro PLSSVM_ASSERT.
#define PLSSVM_ASSERT(cond, msg,...)
Defines the PLSSVM_ASSERT macro if PLSSVM_ASSERT_ENABLED is defined.
Definition: assert.hpp:74
The plssvm::detail::file_reader class is responsible for reading a file and splitting it into its lin...
Definition: file_reader.hpp:42
std::string_view line(typename std::vector< std::string_view >::size_type pos) const
Return the pos line of the parsed file.
bool is_open() const noexcept
Checks whether this file_reader is currently associated with a file.
std::vector< std::string_view >::size_type num_lines() const noexcept
Return the number of parsed lines (where all empty lines or lines starting with a comment are ignored...
Defines universal utility functions.
Implements custom exception classes derived from std::runtime_error including source location informa...
Implements a file reader class responsible for reading the input file and parsing it into lines.
Namespace containing implementation details for the IO related functions. Should not directly be used...
Definition: core.hpp:44
std::tuple< std::pair< real_type, real_type >, std::vector< factors_type > > parse_scaling_factors(const file_reader &reader)
Read the scaling interval and factors stored using LIBSVM's file format from the file filename.
Definition: scaling_factors_parsing.hpp:57
void write_scaling_factors(const std::string &filename, const std::pair< real_type, real_type > &scaling_interval, const std::vector< factors_type > &scaling_factors)
Write the scaling_interval and scaling_factors to a file for later usage in scaling another data set ...
Definition: scaling_factors_parsing.hpp:139
std::string_view trim(std::string_view str) noexcept
Returns a new std::string_view equal to str where all leading and trailing whitespaces are removed.
std::string current_date_time()
Return the current date time in the format "YYYY-MM-DD hh:mm:ss".
Implements a conversion function from a string to an arithmetic type.
Implements utility functions for string manipulation and querying.