Curves
A library for curves generation.
helpers.hpp
Go to the documentation of this file.
1 /*
2  * @file helpers.hpp
3  * @date Oct 17, 2014
4  * @author Sean Andersson, Peter Fankhauser
5  */
6 
7 #pragma once
8 #include <stdlib.h>
9 #include <vector>
10 #include <iostream>
11 #include <fstream>
12 #include <glog/logging.h>
13 #include <Eigen/Core>
14 
15 namespace curves {
16 
17 template <typename T>
18 static std::string toString(const T& t) {
19  std::ostringstream ss; ss<<t; return ss.str();
20 }
22 static std::vector<std::vector<std::string> > loadCSV(std::string fileName) {
23  // Open file stream and check that it is error free
24  std::ifstream inFileStream( fileName.c_str() );
25  CHECK(inFileStream.good()) << "error opening input file " << fileName;
26  // Create output variable
27  std::vector<std::vector<std::string> > strMatrix;
28  // Loop over lines (rows) of CSV file
29  std::string line;
30  while(std::getline(inFileStream, line))
31  {
32  // Make string stream out of line
33  std::istringstream ss(line);
34  std::vector<std::string> strRow;
35  // Loop over comma separated fields of CSV line
36  std::string field;
37  while (getline(ss, field,',')) {
38  strRow.push_back(field); // add column entry to row
39  }
40  strMatrix.push_back(strRow); // add row to matrix
41  }
42  inFileStream.close();
43  return strMatrix;
44 }
46 static void writeCSV(std::string fileName, const std::vector<std::vector<std::string> >& strMatrix) {
47  CHECK_GE(strMatrix.size(), 1) << "Provided matrix of strings had no entries.";
48  std::ofstream outFileStream;
49  outFileStream.open(fileName.c_str());
50  // Iterate over the rows of the string matrix and write comma-separated fields
51  for (std::vector<std::vector<std::string> >::const_iterator itRow = strMatrix.begin() ; itRow != strMatrix.end(); ++itRow) {
52  const unsigned fields = itRow->size();
53  CHECK_GE(fields, 1) << "String matrix row has no entries.";
54  outFileStream << itRow->at(0);
55  for (unsigned i = 1; i < fields; i++) {
56  outFileStream << "," << itRow->at(i);
57  }
58  outFileStream << std::endl;
59  }
60  outFileStream.close();
61 }
63 static void loadTimeVectorCSV(std::string fileName, std::vector<curves::Time>* outTimes, std::vector<Eigen::VectorXd>* outValues) {
64  // Initialize outputs
65  CHECK_NOTNULL(outTimes);
66  outTimes->clear();
67  CHECK_NOTNULL(outValues);
68  outValues->clear();
69  // Load CSV to matrix of strings
70  std::vector<std::vector<std::string> > temp = loadCSV(fileName);
71  CHECK_GE(temp.size(), 1) << "CSV " << fileName << "was empty.";
72  CHECK_GE(temp.at(0).size(), 2) << "CSV does not have the expected number of fields (atleast time and 1 value).";
73  // Iterate over the rows of the CSV and use comma-separated fields to popular outputs
74  const unsigned vOffset = 1;
75  const unsigned vDim = temp.at(0).size() - vOffset;
76  Eigen::VectorXd tempVec(vDim);
77  for (std::vector<std::vector<std::string> >::iterator it = temp.begin() ; it != temp.end(); ++it) {
78  outTimes->push_back((curves::Time)atof(it->at(1).c_str()));
79  for (unsigned vIdx = 0; vIdx < vDim; vIdx++) {
80  tempVec[vIdx] = atof(it->at(vIdx + vOffset).c_str());
81  }
82  outValues->push_back(tempVec);
83  }
84 }
86 static void writeTimeVectorCSV(std::string fileName, const std::vector<curves::Time>& times, const std::vector<Eigen::VectorXd>& values) {
87  // Check inputs and initialize sizes
88  CHECK_EQ(times.size(), values.size()) << "Length of times and values is not equal.";
89  CHECK_GE(times.size(), 1) << "No entries to write";
90  const unsigned vOffset = 1;
91  const unsigned vDim = values.at(0).rows();
92  // Fill string matrix
93  std::vector<std::vector<std::string> > strMatrix;
94  strMatrix.reserve(times.size());
95  std::vector<std::string> strRow;
96  strRow.reserve(vOffset + vDim);
97  for (unsigned i = 0; i < times.size(); i++) {
98  CHECK_EQ(vDim, values.at(i).rows()) << "all vectors must be of the same dimension.";
99  strRow.clear();
100  strRow.push_back(toString<Time>(times[i]));
101  for (unsigned v = 0; v < vDim; v++) {
102  strRow.push_back(toString<double>(values[i][v]));
103  };
104  strMatrix.push_back(strRow);
105  }
106  // Write
107  writeCSV(fileName, strMatrix);
108 }
110 static void loadTimeTimeVectorCSV(std::string fileName, std::vector<curves::Time>* outTimes0, std::vector<curves::Time>* outTimes1, std::vector<Eigen::VectorXd>* outValues) {
111  // Initialize outputs
112  CHECK_NOTNULL(outTimes0);
113  outTimes0->clear();
114  CHECK_NOTNULL(outTimes1);
115  outTimes1->clear();
116  CHECK_NOTNULL(outValues);
117  outValues->clear();
118  // Load CSV to matrix of strings
119  std::vector<std::vector<std::string> > temp = loadCSV(fileName);
120  CHECK_GE(temp.size(), 1) << "CSV " << fileName << "was empty.";
121  CHECK_GE(temp.at(0).size(), 3) << "CSV does not have the expected number of fields (atleast 2 times and 1 value).";
122  // Iterate over the rows of the CSV and use comma-separated fields to popular outputs
123  const unsigned vOffset = 2;
124  const unsigned vDim = temp.at(0).size() - vOffset;
125  Eigen::VectorXd tempVec(vDim);
126  for (std::vector<std::vector<std::string> >::iterator it = temp.begin() ; it != temp.end(); ++it) {
127  outTimes0->push_back((curves::Time)atof(it->at(1).c_str()));
128  outTimes1->push_back((curves::Time)atof(it->at(2).c_str()));
129  for (unsigned vIdx = 0; vIdx < vDim; vIdx++) {
130  tempVec[vIdx] = atof(it->at(vIdx + vOffset).c_str());
131  }
132  outValues->push_back(tempVec);
133  }
134 }
135 
136 }
static std::string toString(const T &t)
Definition: helpers.hpp:18
static void writeCSV(std::string fileName, const std::vector< std::vector< std::string > > &strMatrix)
Helper function to write &#39;matrix&#39; of strings into CSV file.
Definition: helpers.hpp:46
static void writeTimeVectorCSV(std::string fileName, const std::vector< curves::Time > &times, const std::vector< Eigen::VectorXd > &values)
Helper function to write CSV file formatted in: time, vectorEntry0, vectorEntry1, ...
Definition: helpers.hpp:86
double Time
Definition: Curve.hpp:13
static std::vector< std::vector< std::string > > loadCSV(std::string fileName)
Helper function to read CSV files into &#39;matrix&#39; of strings.
Definition: helpers.hpp:22
static void loadTimeTimeVectorCSV(std::string fileName, std::vector< curves::Time > *outTimes0, std::vector< curves::Time > *outTimes1, std::vector< Eigen::VectorXd > *outValues)
Helper function to read CSV files formatted in: time0, time1, vectorEntry0, vectorEntry1, ...
Definition: helpers.hpp:110
static void loadTimeVectorCSV(std::string fileName, std::vector< curves::Time > *outTimes, std::vector< Eigen::VectorXd > *outValues)
Helper function to read CSV files formatted in: time, vectorEntry0, vectorEntry1, ...
Definition: helpers.hpp:63