Cosan  1.0
Data Analytics Library
utils.h
Go to the documentation of this file.
1 #ifndef __UTILS_H_INCLUDED__
2 #include <iostream>
3 #include <fstream>
4 #include <string>
5 #include <vector>
6 #include <set>
7 #include <tuple>
8 #include <math.h>
9 #include <Eigen/Dense>
10 #include <gsl/gsl>
11 #define __UTILS_H_INCLUDED__
12 #include <cosan/base/CosanBO.h>
13 namespace Cosan{
14  /**
15  * @brief General string to number conversion function
16  * @details As we allow for user-determined numeric type `NumericType`, the detailed implementation or functions needed maybe slightly
17  * different among each other. For instance, when we are trying to read data from `csv` file. Different data type requires different
18  * string-to-numeric function. For `double`, one is required `std::stod` while 'std::stof' is the candidate function if
19  * `float` is chosen. To take care of this variant before running time, we conside the static-i. The feature
20  * allows us to discard branches of an if statement at compile-time based on a constant expression condition. In the following
21  * code as an example, we define a template function with input requiring `NumericType` as numeric and then the implementation
22  * is decided via `if constexpr` statement.
23  **/
24  template<Numeric NumericType>
25  NumericType StringToNum(const std::string& arg, std::size_t* pos = 0) {
26  static_assert(std::is_arithmetic<NumericType>::value, "NumericType must be numeric");
27  if constexpr (std::is_same_v<NumericType, unsigned long>) {
28  return std::stoul(arg,pos);
29  }
30  else if constexpr (std::is_same_v<NumericType, unsigned long long>){
31  return std::stoull(arg,pos);
32  }
33  else if constexpr (std::is_same_v<NumericType, int>){
34  return std::stoi(arg,pos);
35  }
36  else if constexpr (std::is_same_v<NumericType, long>){
37  return std::stol(arg,pos);
38  }
39  else if constexpr (std::is_same_v<NumericType, long long>){
40  return std::stoll(arg,pos);
41  }
42  else if constexpr (std::is_same_v<NumericType, float>){
43  return std::stof(arg,pos);
44  }
45  else if constexpr (std::is_same_v<NumericType, double>){
46  return std::stod(arg,pos);
47  }
48  else{
49  return std::stold(arg,pos);
50  }
51  }
52 }
53 
54 template<typename Matrix>
55 Matrix load_csv1 (const std::string & path) {
56  std::ifstream indata;
57 
58  indata.open(path);
59  std::string line;
60  std::vector<double> values;
61  gsl::index rows = 0;
62  while (std::getline(indata, line)) {
63  std::stringstream lineStream(line);
64  std::string cell;
65  while (getline(lineStream, cell, ',')) {
66  values.push_back(stod(cell));
67  }
68  ++rows;
69  }
70 
71  return Eigen::Map<const Eigen::Matrix<typename Matrix::Scalar, Matrix::RowsAtCompileTime, Matrix::ColsAtCompileTime, Eigen::RowMajor> >(values.data(), rows, values.size()/rows);
72 }
73 
74 
75 template<typename Matrix>
76 void save_csv(const std::string & path, const Matrix & matrix){
77 // if (path.substr(path.size()-4)!=".csv"){
78 // path.append(".csv");
79 // }
80  std::ofstream file(path,std::ios::out);
81  if (file.is_open()){
82  Eigen::IOFormat csvFmt(Eigen::FullPrecision,0,",");
83  file<<matrix.format(csvFmt);
84  file.close();
85  }
86 }
87 
88 #endif
Cosan
Definition: CosanBO.h:29
NumericType
double NumericType
Definition: onehotencodingTest.cpp:20
save_csv
void save_csv(const std::string &path, const Matrix &matrix)
Definition: utils.h:76
load_csv1
Matrix load_csv1(const std::string &path)
Definition: utils.h:55
CosanBO.h
CosanBO.
Cosan::StringToNum
NumericType StringToNum(const std::string &arg, std::size_t *pos=0)
General string to number conversion function.
Definition: utils.h:25