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=',')
 Constructs MatrixIO object.
 
template<typename T >
txeo::Matrix< T > read_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< T > read_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.
 

Detailed Description

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

Definition at line 17 of file MatrixIO.h.

Constructor & Destructor Documentation

◆ MatrixIO()

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

Constructs MatrixIO object.

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

Definition at line 25 of file MatrixIO.h.

26 : _path(std::move(path)), _separator(separator) {};

Member Function Documentation

◆ 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:17

◆ 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):
"input.tsv", '\t', true
);
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:103

Definition at line 103 of file MatrixIO.h.

104 {
107 return resp;
108 };
txeo::Matrix< T > read_text_file(bool has_header=false) const
Returns a matrix with elements read from a text file.

◆ 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
A class representing a matrix, derived from Tensor.
Definition Matrix.h:42
The shape of a tensor is an ordered collection of dimensions of mathematical vector spaces.
Definition TensorShape.h:30
Here is the caller graph for this function:

◆ 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});
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:125

Definition at line 125 of file MatrixIO.h.

126 {
129 }
void write_text_file(const txeo::Matrix< T > &matrix) const
Writes a matrix to a text file.
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});
// Writes: 0.000,4567.800,9.100,234.567

Definition at line 149 of file MatrixIO.h.

Here is the call graph for this function:

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