-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmodel_frame.cxx
294 lines (276 loc) · 8.14 KB
/
model_frame.cxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
//-----------------------------------------------------------------------------
// model_frame.cxx:
// Implementation for managing variable names, scaling, and formula parsing
// Author: Luke de Oliveira (luke.deoliveira@yale.edu)
//-----------------------------------------------------------------------------
#include "model_frame.hh"
namespace agile
{
//----------------------------------------------------------------------------
model_frame::model_frame(const agile::dataframe &D)
: DF(D), weighting_variable(""), x_set(false), y_set(false), weights_set(false)
{
}
// model_frame(agile::dataframe &&D);
//----------------------------------------------------------------------------
model_frame::model_frame()
: weighting_variable(""), x_set(false), y_set(false), weights_set(false)
{
}
//----------------------------------------------------------------------------
model_frame::~model_frame()
{
}
//----------------------------------------------------------------------------
void model_frame::add_dataset(const agile::dataframe &D)
{
DF.append(D);
}
//----------------------------------------------------------------------------
void model_frame::add_dataset(agile::dataframe &&D)
{
DF.append(std::move(D));
}
//----------------------------------------------------------------------------
void model_frame::model_formula(const std::string &formula)
{
m_formula = formula;
parse_formula(formula);
}
void model_frame::generate(bool verbose)
{
agile::matrix T = eigen_spew(DF);
m_X.resize(DF.rows(), inputs.size());
m_Y.resize(DF.rows(), outputs.size());
unsigned int columns_to_extract = inputs.size();
if (outputs.at(0) != "")
{
columns_to_extract += outputs.size();
}
if (verbose)
{
std::cout << "\nGenerating base model formula..." << std::endl;
}
int idx = 0, ctr = 0;
double pct;
for (auto &name : inputs)
{
m_X.col(idx) = T.col(DF.get_column_idx(name));
++idx;
++ctr;
if (verbose)
{
pct = (double)(ctr) / (double)(columns_to_extract);
agile::progress_bar(pct * 100);
}
}
x_set = true;
idx = 0;
if (outputs.at(0) != "")
{
for (auto &name : outputs)
{
m_Y.col(idx) = T.col(DF.get_column_idx(name));
++idx;
++ctr;
if (verbose)
{
pct = (double)(ctr) / (double)(columns_to_extract);
agile::progress_bar(pct * 100);
}
}
y_set = true;
}
if (weighting_variable != "")
{
m_weighting = T.col(DF.get_column_idx(weighting_variable));
weights_set = true;
}
}
//----------------------------------------------------------------------------
void model_frame::scale(bool verbose, bool scale_outs)
{
if (!x_set)
{
throw std::runtime_error("must load an X into model frame before scaling.");
}
unsigned int columns_to_extract = inputs.size();
int idx = 0;
double pct;
if (verbose)
{
std::cout << "\nScaling model frame..." << std::endl;
}
for (auto &name : inputs)
{
agile::calc_normalization(m_X.col(idx), name, m_scaling);
m_X.col(idx).array() -= m_scaling.mean[name];
m_X.col(idx) /= m_scaling.sd[name];
++idx;
if (verbose)
{
pct = (double)(idx) / (double)(columns_to_extract);
agile::progress_bar(pct * 100);
}
}
if (scale_outs)
{
idx = 0;
columns_to_extract = outputs.size();
for (auto &name : outputs)
{
agile::calc_normalization(m_Y.col(idx), name, m_scaling);
m_Y.col(idx).array() -= m_scaling.mean[name];
m_Y.col(idx) /= m_scaling.sd[name];
++idx;
if (verbose)
{
pct = (double)(idx) / (double)(columns_to_extract);
agile::progress_bar(pct * 100);
}
}
}
if (verbose)
{
std::cout << std::endl;
}
}
//----------------------------------------------------------------------------
void model_frame::load_scaling(const agile::scaling &scale)
{
m_scaling = scale;
int idx = 0;
for (auto &name : inputs)
{
m_X.col(idx).array() -= m_scaling.mean[name];
m_X.col(idx) /= m_scaling.sd[name];
++idx;
}
idx = 0;
}
//----------------------------------------------------------------------------
agile::scaling model_frame::get_scaling()
{
return m_scaling;
}
//----------------------------------------------------------------------------
agile::matrix& model_frame::Y()
{
return m_Y;
}
//----------------------------------------------------------------------------
agile::matrix& model_frame::X()
{
return m_X;
}
//----------------------------------------------------------------------------
agile::vector& model_frame::weighting()
{
if (weighting_variable != "")
{
return m_weighting;
}
else
{
throw std::runtime_error("no weighting variable set.");
}
}
//----------------------------------------------------------------------------
// parses formulas of the form bottom ~ pt + eta | weight
void model_frame::parse_formula(std::string formula)
{
auto pipe = formula.find_first_of("|");
if (pipe != std::string::npos)
{
weighting_variable = agile::no_spaces(formula.substr(pipe + 1));
formula = formula.substr(0, pipe);
exclusions.insert(weighting_variable);
}
formula = agile::no_spaces(formula);
bool parsing = true, wildcard = false;
auto tilde = formula.find_first_of("~");
if (tilde != formula.find_last_of("~"))
{
std::string e("can't specify multiple \'is a function of\'");
e.append(" operators (uses of \'~\') in one formula.");
throw agile::parsing_error(e);
}
auto wildcard_find = formula.find_first_of("*");
if (wildcard_find != formula.find_last_of("*"))
{
std::string e("can't specify multiple \'include all defined");
e.append(" variables\' operators (uses of \'*\') in one formula.");
throw agile::parsing_error(e);
}
if (wildcard_find != std::string::npos)
{
wildcard = true;
std::string::iterator end_pos = std::remove(
formula.begin(), formula.end(), '*');
formula.erase(end_pos, formula.end());
}
auto lhs = formula.substr(0, tilde);
auto rhs = formula.substr(tilde + 1);
auto end = lhs.find_first_of('+');
unsigned long int start = 0;
while(parsing == true)
{
auto new_var = lhs.substr(start, end - start);
outputs.push_back(new_var);
if (wildcard)
{
exclusions.insert(new_var);
}
if (end == std::string::npos) parsing = false;
start = end + 1;
end = lhs.find_first_of('+', start);
}
parsing = true;
start = (rhs[0] == '-' || rhs[0] == '+') ? 1 : 0;
end = rhs.find_first_of("+-", start);
while(parsing == true)
{
auto new_var = rhs.substr(start, end - start);
if (rhs[start - 1] == '-')
{
exclusions.insert(new_var);
}
else
{
if (!wildcard)
{
inputs.push_back(new_var);
}
}
if (end == std::string::npos) parsing = false;
start = end + 1;
end = rhs.find_first_of("+-", start);
}
if (wildcard)
{
for (auto &name : DF.get_column_names())
{
if (exclusions.count(name) == 0)
{
inputs.push_back(name);
}
}
}
x_set = (inputs.size() == 0) ? false : true;
y_set = (outputs.size() == 0) ? false : true;
}
//----------------------------------------------------------------------------
std::vector<std::string> model_frame::get_inputs()
{
return inputs;
}
//----------------------------------------------------------------------------
std::vector<std::string> model_frame::get_outputs()
{
return outputs;
}
//----------------------------------------------------------------------------
parsing_error::parsing_error(const std::string &what)
: std::runtime_error(what)
{}
}