Cosan  1.0
Data Analytics Library
utils.h File Reference
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <set>
#include <tuple>
#include <math.h>
#include <Eigen/Dense>
#include <gsl/gsl>
#include <cosan/base/CosanBO.h>

Go to the source code of this file.

Namespaces

 Cosan
 

Functions

template<Numeric NumericType>
NumericType Cosan::StringToNum (const std::string &arg, std::size_t *pos=0)
 General string to number conversion function. More...
 
template<typename Matrix >
Matrix load_csv1 (const std::string &path)
 
template<typename Matrix >
void save_csv (const std::string &path, const Matrix &matrix)
 

Function Documentation

◆ load_csv1()

template<typename Matrix >
Matrix load_csv1 ( const std::string &  path)

Definition at line 55 of file utils.h.

55  {
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 }

◆ save_csv()

template<typename Matrix >
void save_csv ( const std::string &  path,
const Matrix &  matrix 
)

Definition at line 76 of file utils.h.

76  {
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 }