Txeo v0.1
A Developer-Friendly TensorFlow C++ Wrapper
Loading...
Searching...
No Matches
txeo::MatrixIO Class Reference

A class to read file data to matrix and to write file data to a matrix. More...

#include <MatrixIO.h>

Collaboration diagram for txeo::MatrixIO:
Collaboration graph

Public Member Functions

 MatrixIO (const std::filesystem::path &path, char separator=',', txeo::Logger &logger=txeo::LoggerConsole::instance())
 Constructs MatrixIO object.
 
std::filesystem::path path () const
 
char separator () const
 
template<typename T >
txeo::Matrix< Tread_text_file (bool has_header=false) const
 Returns a matrix with elements read from a text file.
 
template<typename T >
void write_text_file (const txeo::Matrix< T > &matrix) const
 Writes a matrix to a text file.
 
template<typename T >
requires (std::is_floating_point_v<T>)
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.
 

Static Public Member Functions

template<typename T >
static txeo::Matrix< Tread_textfile (const std::filesystem::path &path, char separator=',', bool has_header=false)
 Returns a matrix with elements read from a text file.
 
template<typename T >
static void write_textfile (const txeo::Matrix< T > &matrix, const std::filesystem::path &path, char separator=',')
 Writes a matrix to a text file.
 
template<typename T >
requires (std::is_floating_point_v<T>)
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.
 
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 file.
 

Detailed Description

A class to read file data to matrix and to write file data to a matrix.

Definition at line 24 of file MatrixIO.h.

Constructor & Destructor Documentation

◆ MatrixIO()

txeo::MatrixIO::MatrixIO ( const std::filesystem::path &  path,
char  separator = ',',
txeo::Logger logger = txeo::LoggerConsole::instance() 
)
inlineexplicit

Constructs MatrixIO object.

Parameters
pathPath to the file
separatorCharacter delimiting each element in a row

Definition at line 32 of file MatrixIO.h.

34 : _path(std::move(path)), _separator(separator), _logger{&logger} {};
std::filesystem::path path() const
Definition MatrixIO.h:36
char separator() const
Definition MatrixIO.h:38

Member Function Documentation

◆ one_hot_encode_text_file()

static txeo::MatrixIO 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() 
)
static

Performs one-hot encoding in all non-numeric columns in a text file and writes the result to a target file.

Parameters
source_pathThe path to the source text file containing the input data.
separatorThe delimiter used in the input file (e.g., ',' for CSV).
has_headerA flag indicating whether the input file has a header row.
target_pathThe path to the target text file where the encoded data will be written.
Returns
A MatrixIO object representing the encoded data file pointing to target_path.
Exceptions
txeo::MatrixIOErrorIf the source and target paths are the same, if the file cannot be opened, or if the input data is inconsistent (e.g., different types in the same column).

Example Usage:

#include "MatrixIO.h"
#include <filesystem>
int main() {
std::filesystem::path source_path = "input.csv";
std::filesystem::path target_path = "output.csv";
char separator = ',';
bool has_header = true;
try
{
std::cout << "One-hot encoding completed successfully. Output
written to: " << target_path << std::endl;
} catch (const txeo::MatrixIOError &e) { std::cerr
<< "Error: " << e.what() << std::endl;
}
return 0;
}
A container for managing training, evaluation, and test data splits.
Definition DataTable.h:24
Exceptions concerning txeo::MatrixIO.
Definition MatrixIO.h:227
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...

◆ path()

std::filesystem::path txeo::MatrixIO::path ( ) const
inline

Definition at line 36 of file MatrixIO.h.

36{ return _path; }
Here is the caller graph for this function:

◆ read_text_file()

template<typename T >
txeo::Matrix< T > txeo::MatrixIO::read_text_file ( bool  has_header = false) const

Returns a matrix with elements read from a text file.

Template Parameters
TData type of the matrix elements
Parameters
has_headerWhether the first line contains column headers
Returns
Matrix<T> Created matrix with data from the file
Exceptions
MatrixIOError
Example (Instance usage):
txeo::MatrixIO io("data.csv");
auto matrix = io.read_text_file<float>(true); // Read with header
std::cout << "Matrix shape: " << matrix.shape();
A class to read file data to matrix and to write file data to a matrix.
Definition MatrixIO.h:24

◆ read_textfile()

template<typename T >
static txeo::Matrix< T > txeo::MatrixIO::read_textfile ( const std::filesystem::path &  path,
char  separator = ',',
bool  has_header = false 
)
inlinestatic

Returns a matrix with elements read from a text file.

Template Parameters
TData type of matrix elements
Parameters
pathFile path to read from
separatorColumn separator character
has_headerWhether to skip first line as header
Returns
Matrix<T> Created matrix
Example (One-time read):
auto data = txeo::MatrixIO::read_textfile<float>(
"input.tsv", '\t', true
);

Definition at line 115 of file MatrixIO.h.

116 {
118 Matrix<T> resp{io.read_text_file<T>(has_header)};
119 return resp;
120 };
Here is the call graph for this function:

◆ separator()

char txeo::MatrixIO::separator ( ) const
inline

Definition at line 38 of file MatrixIO.h.

38{ return _separator; }
Here is the caller graph for this function:

◆ write_text_file() [1/2]

template<typename T >
void txeo::MatrixIO::write_text_file ( const txeo::Matrix< T > &  matrix) const

Writes a matrix to a text file.

Template Parameters
TData type of the matrix elements
Parameters
matrixMatrix to write to file
Exceptions
MatrixIOError
Example (Instance usage):
txeo::Matrix<int> data(txeo::TensorShape({2, 3}), {1, 2, 3, 4, 5, 6});
txeo::MatrixIO io("output.csv");
io.write_text_file(data); // Writes as CSV
The shape of a tensor is an ordered collection of dimensions of mathematical vector spaces.
Definition TensorShape.h:31

◆ write_text_file() [2/2]

template<typename T >
requires (std::is_floating_point_v<T>)
void txeo::MatrixIO::write_text_file ( const txeo::Matrix< T > &  matrix,
size_t  precision 
) const

Writes a floating-point matrix with specified precision to a text file.

Template Parameters
TFloating-point type (float/double)
Parameters
matrixMatrix to write
precisionNumber of decimal places to write
Exceptions
MatrixIOError
Example (Precision control):
txeo::Matrix<double> values(txeo::TensorShape({1, 3}), {1.2345, 2.3456, 3.4567});
txeo::MatrixIO io("results.csv");
io.write_text_file(values, 2); // Writes 1.23,2.35,3.46

◆ write_textfile() [1/2]

template<typename T >
static void txeo::MatrixIO::write_textfile ( const txeo::Matrix< T > &  matrix,
const std::filesystem::path &  path,
char  separator = ',' 
)
inlinestatic

Writes a matrix to a text file.

Template Parameters
TData type of matrix elements
Parameters
matrixMatrix to write
pathOutput file path
separatorColumn separator
Example (One-time write):
txeo::Matrix<int> matrix(txeo::TensorShape({3, 2}), {1, 2, 3, 4, 5, 6});
txeo::MatrixIO::write_textfile(matrix, "matrix.csv");
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

Definition at line 137 of file MatrixIO.h.

138 {
140 io.write_text_file(matrix);
141 }
Here is the call graph for this function:

◆ write_textfile() [2/2]

template<typename T >
requires (std::is_floating_point_v<T>)
static void txeo::MatrixIO::write_textfile ( const txeo::Matrix< T > &  matrix,
size_t  precision,
const std::filesystem::path &  path,
char  separator = ',' 
)
inlinestatic

Writes a floating-point matrix with specified precision to a text file.

Template Parameters
TFloating-point type (float/double)
Parameters
matrixMatrix to write
precisionDecimal places to display
pathOutput file path
separatorColumn delimiter
Example (Scientific notation):
txeo::Matrix<double> results(txeo::TensorShape({2, 2}), {0.000123, 4567.8, 9.1, 234.567});
txeo::MatrixIO::write_textfile(results, 3, "science.csv");
// Writes: 0.000,4567.800,9.100,234.567

Definition at line 161 of file MatrixIO.h.

162 {
164 io.write_text_file(matrix, precision);
165 };
Here is the call graph for this function:

The documentation for this class was generated from the following file: