CEBL  2.1
Lag.cpp
Go to the documentation of this file.
1 #include "Lag.hpp"
2 
3 using namespace cppR;
4 
5 namespace CEBL
6 {
7  //default constructor
9  {
10  n_lags = 0;
11  bufferEmpty=true;
12  plugin_name = "Lag";
13  }
14 
16  std::map<std::string, CEBL::Param> Lag::getParamsList()
17  {
18  std::map<std::string, CEBL::Param> params;
19  CEBL::Param lags("Lags", "How many lags?", n_lags);
20  lags.setMax(500);
21  lags.setMin(0);
22  params["lags"] = lags;
23 
24  return params;
25  }
26 
28  void Lag::setParamsList(std::map<std::string, CEBL::Param> &p)
29  {
30  int new_lags = p["lags"].getInt();
31 
32  if(new_lags != n_lags)
33  {
34  cout << "setting lags to " << new_lags << "\n";
35  n_lags = new_lags;
36  reset();
37  }
38  }
39 
40 
42  ublas::matrix<double> Lag::use(const ublas::matrix<double> &data)
43  {
44  bool debug = false;
45  if(debug)
46  {
47  cout << "using lags: " << endl;
48  cout << "Lag.cpp: data is" << nrow(data) << " by " << ncol(data) << endl;
49  cout << "Num lags is " << n_lags << endl;
50  }
51  if (n_lags > 0) {
52  if (bufferEmpty) {
53  buffer = data;
54  bufferEmpty = false;
55  } else {
56  // cbind (concatentate) new data onto right side of buffer
57  int nRowsBuffer = nrow(buffer);
58  int nColsBuffer = ncol(buffer);
59  //int nRowsData = nrow(data);
60  int nColsData = ncol(data);
61  if(debug)
62  {
63  cout << "Lag.cpp: data is" << nrow(data) << " by " << ncol(data) << endl;
64  cout << "Lag.cpp: buffer was " << nrow(buffer) << " by " << ncol(buffer) << endl;
65  }
66  ublas::matrix<double> keep =
67  submatrix(buffer,0,nRowsBuffer-1,
68  nColsBuffer-n_lags, nColsBuffer-1);
69  buffer = keep;
70  buffer.resize(nRowsBuffer, n_lags+nColsData);
71  nColsBuffer = ncol(buffer);
72  submatrix(buffer, 0, nRowsBuffer-1,
73  nColsBuffer-nColsData, nColsBuffer-1) = data;
74  if(debug)
75  cout << "Lag.cpp: buffer now " << nrow(buffer) << " by " << ncol(buffer) << endl;
76  }
77 
78  // will produce error if n_lags >= ncol(buffer)
79  return cppR::Lag(buffer,n_lags);
80  } else {
81 
82  // n_lags == 0
83  return data;
84 
85  }
86  }
87 
88  map<string, SerializedObject> Lag::save() const
89  {
90  map<string, SerializedObject> ret;
91  ret["n_lags"] = serialize(n_lags);
92 
93  return ret;
94  }
95  void Lag::load(map<string, SerializedObject> objects)
96  {
97  deserialize(objects["n_lags"], n_lags);
98  }
99 
101  void Lag::reset()
102  {
103  buffer.resize(0,0);
104  bufferEmpty = true;
105  }
106 
107 }
108 
109 /*************************************************************/
110 //DYNAMIC LOADING
111 
113 {
114  return new CEBL::Lag;
115 }
116 
117 extern "C" void ObjectDestroy(CEBL::Feature* p)
118 {
119  delete p;
120 }