Txeo v0.1
A Developer-Friendly TensorFlow C++ Wrapper
Loading...
Searching...
No Matches
MatrixIO.h
Go to the documentation of this file.
1#ifndef MATRIXIO_H
2#define MATRIXIO_H
3#pragma once
4
5#include "txeo/Logger.h"
7#include "txeo/Matrix.h"
8
9#include <cstddef>
10#include <filesystem>
11#include <map>
12#include <stdexcept>
13#include <string>
14#include <type_traits>
15#include <unordered_set>
16#include <utility>
17
18namespace txeo {
19
24class MatrixIO {
25 public:
32 explicit MatrixIO(const std::filesystem::path &path, char separator = ',',
34 : _path(std::move(path)), _separator(separator), _logger{&logger} {};
35
36 [[nodiscard]] std::filesystem::path path() const { return _path; }
37
38 [[nodiscard]] char separator() const { return _separator; }
39
57 template <typename T>
59
75 template <typename T>
77
94 template <typename T>
95 requires(std::is_floating_point_v<T>)
96 void write_text_file(const txeo::Matrix<T> &matrix, size_t precision) const;
97
114 template <typename T>
115 static txeo::Matrix<T> read_textfile(const std::filesystem::path &path, char separator = ',',
116 bool has_header = false) {
118 Matrix<T> resp{io.read_text_file<T>(has_header)};
119 return resp;
120 };
121
136 template <typename T>
137 static void write_textfile(const txeo::Matrix<T> &matrix, const std::filesystem::path &path,
138 char separator = ',') {
140 io.write_text_file(matrix);
141 }
142
159 template <typename T>
160 requires(std::is_floating_point_v<T>)
161 static void write_textfile(const txeo::Matrix<T> &matrix, size_t precision,
162 const std::filesystem::path &path, char separator = ',') {
164 io.write_text_file(matrix, precision);
165 };
166
205 static txeo::MatrixIO
206 one_hot_encode_text_file(const std::filesystem::path &source_path, char separator,
207 bool has_header, const std::filesystem::path &target_path,
209
210 private:
211 std::filesystem::path _path;
212 char _separator;
213 txeo::Logger *_logger{nullptr};
214
215 static std::map<size_t, std::unordered_set<std::string>>
216 build_lookups_map(const std::filesystem::path &source_path, char separator, bool has_header);
217
218 static std::string
219 build_target_header(const std::filesystem::path &source_path, char separator, bool has_header,
220 const std::map<size_t, std::unordered_set<std::string>> &lookups_map);
221};
222
227class MatrixIOError : public std::runtime_error {
228 public:
229 using std::runtime_error::runtime_error;
230};
231
232} // namespace txeo
233
234#endif
A container for managing training, evaluation, and test data splits.
Definition DataTable.h:24
static LoggerConsole & instance()
Access the singleton instance.
Abstract base class for logging subsystems.
Definition Logger.h:45
Exceptions concerning txeo::MatrixIO.
Definition MatrixIO.h:227
A class to read file data to matrix and to write file data to a matrix.
Definition MatrixIO.h:24
MatrixIO(const std::filesystem::path &path, char separator=',', txeo::Logger &logger=txeo::LoggerConsole::instance())
Constructs MatrixIO object.
Definition MatrixIO.h:32
void write_text_file(const txeo::Matrix< T > &matrix, size_t precision) const
Writes a floating-point matrix with specified precision to a text file.
void write_text_file(const txeo::Matrix< T > &matrix) const
Writes a matrix to a text file.
std::filesystem::path path() const
Definition MatrixIO.h:36
static void write_textfile(const txeo::Matrix< T > &matrix, const std::filesystem::path &path, char separator=',')
Writes a matrix to a text file.
Definition MatrixIO.h:137
char separator() const
Definition MatrixIO.h:38
static txeo::MatrixIO one_hot_encode_text_file(const std::filesystem::path &source_path, char separator, bool has_header, const std::filesystem::path &target_path, txeo::Logger &logger=txeo::LoggerConsole::instance())
Performs one-hot encoding in all non-numeric columns in a text file and writes the result to a target...
txeo::Matrix< T > read_text_file(bool has_header=false) const
Returns a matrix with elements read from a text file.
static txeo::Matrix< T > read_textfile(const std::filesystem::path &path, char separator=',', bool has_header=false)
Returns a matrix with elements read from a text file.
Definition MatrixIO.h:115
static void write_textfile(const txeo::Matrix< T > &matrix, size_t precision, const std::filesystem::path &path, char separator=',')
Writes a floating-point matrix with specified precision to a text file.
Definition MatrixIO.h:161