nnlib
GPU-accelerated, C/C++ neural network library.
Functions
read.cpp File Reference

Source file defining methods that can be used when reading from files. More...

#include "read.h"
#include "printing.h"
#include <cmath>
#include <fstream>
#include <iostream>
#include <thread>
#include <utility>

Functions

std::vector< std::string > split (const std::string &str, const std::string &delim)
 Splits a string given the delimiter. More...
 
bool fileExists (const std::string &filepath)
 Check if a file exists with the provided filepath. More...
 
std::vector< std::string > readFile (const std::string &filepath)
 Read file from a filepath. More...
 
void threadCSVJob (const std::vector< std::string > &lines, const std::string &delim, Tensor &result, int id, int numThreads)
 A method that is executed by every thread during readCSV(). More...
 
sTensor readCSV (const std::string &filepath, const std::string &delim, int numThreads)
 Read a csv file from a path. More...
 

Detailed Description

Source file defining methods that can be used when reading from files.

Author
Jan Warchocki
Date
06 March 2022

Function Documentation

◆ fileExists()

bool fileExists ( const std::string &  filepath)

Check if a file exists with the provided filepath.

Parameters
filepathThe filepath of the file to check.
Returns
True if the file exists, false otherwise.

◆ readCSV()

sTensor readCSV ( const std::string &  filepath,
const std::string &  delim = ",",
int  numThreads = 1 
)

Read a csv file from a path.

The method will first read all lines from the file and then divide the lines over numThreads threads to split them into values. The values are then written into a matrix that is then returned.

Parameters
filepathThe path to the csv file.
delimThe delimiter to use when splitting data.
numThreadsThe number of threads to use when splitting data.
Returns
Matrix of data that was read from the csv file.
Examples
MNIST, and Titanic.

◆ readFile()

std::vector<std::string> readFile ( const std::string &  filepath)

Read file from a filepath.

Will attempt to read file with the provided filepath or in the parent directory of the filepath.

The method can only read text files and will not work on binary files.

Parameters
filepathThe path to the file.
Returns
The lines read from the file.

◆ split()

std::vector<std::string> split ( const std::string &  str,
const std::string &  delim 
)

Splits a string given the delimiter.

Parameters
strThe string to split.
delimThe delimiter to split on.
Returns
A vector of strings, corresponding to splitting str by delim.

◆ threadCSVJob()

void threadCSVJob ( const std::vector< std::string > &  lines,
const std::string &  delim,
Tensor result,
int  id,
int  numThreads 
)

A method that is executed by every thread during readCSV().

Each thread takes some part of the lines of the file, splits them by the delimiter, converts the values to float and saves those values into a result matrix.

Parameters
linesThe lines as read from the file.
delimThe delimiter to use when splitting lines.
resultA result matrix where the threads should store the results.
idThe id of the thread.
numThreadsThe total number of threads launched.