-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmodelspec.cpp
More file actions
89 lines (78 loc) · 2.39 KB
/
modelspec.cpp
File metadata and controls
89 lines (78 loc) · 2.39 KB
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
#include "modelspec.hpp"
#include <map>
#include "log.hpp"
#define RATE_VARIABLE "rate"
#define ODDS_VARIABLE "odds"
static std::string loc_indicator(const yy::location& loc) {
std::string ret;
size_t i = 1;
for (; i < loc.begin.column; ++i) {
ret.push_back(' ');
}
for (; i < loc.end.column; ++i) {
ret.push_back('^');
}
return ret;
}
void ModelSpec::from_string(const std::string& str)
{
std::istringstream is(str);
from_stream(is);
}
void ModelSpec::from_stream(std::istream& is)
{
std::string line;
for (size_t i = 1; std::getline(is, line); ++i) {
try {
LOG(debug) << "Parsing line " << i << ": " << line;
parser.parse(line);
parser.location().lines(1);
} catch(const spec_parser::ParseError& e) {
LOG(error) << "Failed to parse model specification (" << e.where << "):";
LOG(error) << e.line;
LOG(error) << loc_indicator(e.where);
LOG(error) << "The error returned was: " << e.what();
throw Exception::ModelSpec::UnrecoverableParseError();
}
}
for (auto &x : parser.regression_exprs) {
if (x.first != RATE_VARIABLE and x.first != ODDS_VARIABLE) {
throw Exception::ModelSpec::InvalidModel(
"declaration of '" + x.first
+ "' is meaningless; please check the model specification");
}
}
rate_expr = parser.regression_exprs[RATE_VARIABLE];
odds_expr = parser.regression_exprs[ODDS_VARIABLE];
variables = parser.random_variables;
}
std::istream& operator>>(std::istream& is, ModelSpec& model_spec)
{
model_spec.from_stream(is);
return is;
}
void log(const std::function<void(const std::string& s)> log_func,
const ModelSpec& model_spec)
{
using namespace spec_parser;
log_func("# Rate expression");
log_func("# ---------------");
log_func("rate = " + show(model_spec.rate_expr));
log_func("");
log_func("# Odds expression");
log_func("# ---------------");
log_func("odds = " + show(model_spec.odds_expr));
log_func("");
log_func("# Coefficient distributions");
log_func("# -------------------------");
std::map<std::string, ModelSpec::Variable> sorted_variables(
model_spec.variables.begin(), model_spec.variables.end());
for (auto &x : sorted_variables) {
auto &v = x.second;
if (v->distribution != nullptr) {
log_func(to_string(*v) + "~" + to_string(*v->distribution));
} else {
log_func(to_string(*v) + "~undefined");
}
}
}