1 #ifndef STAN_IO_STAN_CSV_READER_HPP 2 #define STAN_IO_STAN_CSV_READER_HPP 4 #include <boost/algorithm/string.hpp> 5 #include <boost/lexical_cast.hpp> 37 : stan_version_major(0), stan_version_minor(0), stan_version_patch(0),
38 model(
""), data(
""), init(
""),
39 chain_id(1), seed(0), random_seed(false),
40 num_samples(0), num_warmup(0), save_warmup(false), thin(0),
41 append_samples(false),
42 algorithm(
""), engine(
"") {}
50 : step_size(0), metric(0, 0) {}
58 : warmup(0), sampling(0) {}
63 Eigen::Matrix<std::string, Eigen::Dynamic, 1>
header;
84 while (in.peek() ==
'#') {
85 std::getline(in, line);
88 ss.seekg(std::ios_base::beg);
98 std::getline(ss, lhs);
100 size_t equal = lhs.find(
"=");
101 if (equal != std::string::npos) {
102 name = lhs.substr(0, equal);
104 value = lhs.substr(equal + 1, lhs.size());
106 boost::replace_first(value,
" (Default)",
"");
108 if (lhs.compare(
" data") == 0) {
110 std::getline(ss, lhs);
112 size_t equal = lhs.find(
"=");
113 if (equal != std::string::npos) {
114 name = lhs.substr(0, equal);
116 value = lhs.substr(equal + 2, lhs.size());
117 boost::replace_first(value,
" (Default)",
"");
120 if (name.compare(
"file") == 0)
121 metadata.
data = value;
127 if (name.compare(
"stan_version_major") == 0) {
129 }
else if (name.compare(
"stan_version_minor") == 0) {
131 }
else if (name.compare(
"stan_version_patch") == 0) {
133 }
else if (name.compare(
"model") == 0) {
135 }
else if (name.compare(
"num_samples") == 0) {
137 }
else if (name.compare(
"num_warmup") == 0) {
139 }
else if (name.compare(
"save_warmup") == 0) {
141 }
else if (name.compare(
"thin") == 0) {
142 metadata.
thin = boost::lexical_cast<
int>(
value);
143 }
else if (name.compare(
"chain_id") == 0) {
145 }
else if (name.compare(
"init") == 0) {
148 }
else if (name.compare(
"seed") == 0) {
149 metadata.
seed = boost::lexical_cast<
unsigned int>(
value);
151 }
else if (name.compare(
"append_samples") == 0) {
153 }
else if (name.compare(
"algorithm") == 0) {
155 }
else if (name.compare(
"engine") == 0) {
159 if (ss.good() ==
true)
167 Eigen::Matrix<std::string, Eigen::Dynamic, 1>&
header,
171 if (in.peek() !=
'l')
174 std::getline(in, line);
175 std::stringstream
ss(line);
177 header.resize(
std::count(line.begin(), line.end(),
',') + 1);
181 std::getline(ss, token,
',');
184 int pos = token.find(
'.');
186 token.replace(pos, 1,
"[");
187 std::replace(token.begin(), token.end(),
'.',
',');
198 std::stringstream
ss;
202 if (in.peek() !=
'#' || in.good() ==
false)
205 while (in.peek() ==
'#') {
206 std::getline(in, line);
210 ss.seekg(std::ios_base::beg);
218 std::getline(ss, line);
221 std::getline(ss, line,
'=');
226 std::getline(ss, line);
227 std::getline(ss, line);
228 std::getline(ss, line);
230 int rows = lines - 3;
232 adaptation.
metric.resize(rows, cols);
235 std::stringstream line_ss;
241 std::getline(line_ss, token,
',');
245 std::getline(ss, line);
256 std::stringstream
ss;
262 if (in.peek() ==
'#' || in.good() ==
false)
266 bool comment_line = (in.peek() ==
'#');
267 bool empty_line = (in.peek() ==
'\n');
269 std::getline(in, line);
277 if (line.find(
"(Warm-up)") != std::string::npos) {
279 int right = line.find(
" seconds");
281 += boost::lexical_cast<
double>(line.substr(left, right - left));
282 }
else if (line.find(
"(Sampling)") != std::string::npos) {
284 int right = line.find(
" seconds");
286 += boost::lexical_cast<
double>(line.substr(left, right - left));
290 int current_cols =
std::count(line.begin(), line.end(),
',') + 1;
293 }
else if (cols != current_cols) {
295 *out <<
"Error: expected " << cols <<
" columns, but found " 296 << current_cols <<
" instead for row " << rows + 1
306 ss.seekg(std::ios_base::beg);
309 samples.resize(rows, cols);
311 std::getline(ss, line);
312 std::stringstream
ls(line);
314 std::getline(ls, line,
',');
316 samples(
row,
col) = boost::lexical_cast<
double>(
line);
332 if (!read_metadata(in, data.
metadata, out)) {
334 *out <<
"Warning: non-fatal error reading metadata" <<
std::endl;
337 if (!read_header(in, data.
header, out)) {
339 *out <<
"Error: error reading header" <<
std::endl;
341 (
"Error with header of input file in parse");
344 if (!read_adaptation(in, data.
adaptation, out)) {
346 *out <<
"Warning: non-fatal error reading adapation data" 355 *out <<
"Warning: non-fatal error reading samples" <<
std::endl;
constexpr auto const & right(const_AssnsIter< L, R, D, Dir > const &a, const_AssnsIter< L, R, D, Dir > const &b)
static bool read_metadata(std::istream &in, stan_csv_metadata &metadata, std::ostream *out)
static bool read_adaptation(std::istream &in, stan_csv_adaptation &adaptation, std::ostream *out)
bool equal(T *first, T *second)
stan_csv_adaptation adaptation
const XML_Char int const XML_Char * value
void invalid_argument(const char *function, const char *name, const T &y, const char *msg1, const char *msg2)
constexpr auto const & left(const_AssnsIter< L, R, D, Dir > const &a, const_AssnsIter< L, R, D, Dir > const &b)
static bool read_header(std::istream &in, Eigen::Matrix< std::string, Eigen::Dynamic, 1 > &header, std::ostream *out)
static stan_csv parse(std::istream &in, std::ostream *out)
stan_csv_metadata metadata
::xsd::cxx::tree::token< char, normalized_string > token
static bool read_samples(std::istream &in, Eigen::MatrixXd &samples, stan_csv_timing &timing, std::ostream *out)
Eigen::Matrix< std::string, Eigen::Dynamic, 1 > header