CARLsim  6.1.0
CARLsim: a GPU-accelerated SNN simulator
ParameterInstances.cpp
Go to the documentation of this file.
1 #include "ParameterInstances.h"
2 #include "Util.h"
3 
4 #include <algorithm>
5 #include <cassert>
6 #include <memory>
7 #include <stdexcept>
8 #include <string>
9 #include <istream>
10 #include <sstream>
11 #include <typeinfo>
12 #include <vector>
13 
14 using namespace std;
15 
16 
17 /********************************************************
18  * Private methods for pointer-to-implementation pattern.
19  ********************************************************/
21  vector< vector< double > > instanceVectors;
22 
23  ParameterInstancesImpl(istream &inputStream) {
24  readCSV(inputStream, instanceVectors);
25  assert(repOK());
26  }
27 
28  void readCSV(istream &inputStream, vector< vector<double> > &instanceVectors) {
29  string strLine;
30  while (getline(inputStream, strLine))
31  instanceVectors.push_back(readCSVLine(strLine));
32  if (!allRowsEqualLength(instanceVectors))
33  throw invalid_argument(string(typeid(*this).name()) + string(": rows are not of equal length."));
34  }
35 
36  vector<double> readCSVLine(const string &strLine) {
37  string doubleString;
38  stringstream strLineStream(strLine);
39 
40  vector<double> lineVector;
41 
42  while (getline(strLineStream, doubleString, ',')) {
43  lineVector.push_back(stringToDouble(doubleString));
44  }
45 
46  return lineVector;
47  }
48 
49  static bool allRowsEqualLength(const vector< vector<double> > &instanceVectors) {
50  if (instanceVectors.size() == 0)
51  return true;
52  const unsigned int numParametersPerInstance = instanceVectors[0].size();
53  for (unsigned int i = 1; i < instanceVectors.size(); i++)
54  if (instanceVectors[i].size() != numParametersPerInstance)
55  return false;
56  return true;
57  }
58 
59  vector<double> getInstance(const unsigned int instance) const {
60  assert(instance < getNumInstances());
61  return instanceVectors[instance];
62  }
63 
64  double getParameter(const unsigned int instance, const unsigned int parameter) const {
65  assert(instance < getNumInstances());
66  assert(parameter < getNumParameters());
67  return instanceVectors[instance][parameter];
68  }
69 
70  unsigned int getNumInstances() const {
71  return instanceVectors.size();
72  }
73 
74  unsigned int getNumParameters() const {
75  if (0 == getNumInstances())
76  return 0;
77  return instanceVectors[0].size();
78  }
79 
80  bool repOK() const {
81  return allRowsEqualLength(instanceVectors);
82  }
83 };
84 
85 
86 /********************************************************
87  * Public Methods
88  ********************************************************/
89 
90 
92  impl(*new ParameterInstancesImpl(inputStream)) {
93  assert(repOK());
94 }
95 
97  delete(&impl);
98 }
99 
100 vector<double> ParameterInstances::getInstance(const unsigned int instance) const {
101  assert(instance < getNumInstances());
102  return impl.getInstance(instance);
103 }
104 
105 double ParameterInstances::getParameter(const unsigned int instance, const unsigned int parameter) const {
106  assert(instance < getNumInstances());
107  assert(parameter < getNumParameters());
108  return impl.getParameter(instance, parameter);
109 }
110 
112  return impl.getNumInstances();
113 }
114 
116  return impl.getNumParameters();
117 }
118 
120  return impl.repOK();
121 }
vector< double > readCSVLine(const string &strLine)
void readCSV(istream &inputStream, vector< vector< double > > &instanceVectors)
std::vector< double > getInstance(const unsigned int instance) const
unsigned int getNumInstances() const
double getParameter(const unsigned int instance, const unsigned int parameter) const
vector< double > getInstance(const unsigned int instance) const
static bool allRowsEqualLength(const vector< vector< double > > &instanceVectors)
unsigned int getNumParameters() const
ParameterInstances(std::istream &inputStream)
double stringToDouble(const string &str)
Definition: Util.cpp:30
double getParameter(const unsigned int instance, const unsigned int parameter) const