1 #ifndef __STAN__IO__STAN_CSV_READER_HPP__
2 #define __STAN__IO__STAN_CSV_READER_HPP__
8 #include <boost/algorithm/string.hpp>
9 #include <boost/lexical_cast.hpp>
49 Eigen::Matrix<std::string, Eigen::Dynamic, 1>
header;
77 while (in.peek() ==
'#') {
78 std::getline(in, line);
81 ss.seekg(std::ios_base::beg);
87 std::getline(ss, line);
88 std::getline(ss, line);
92 std::getline(ss, lhs,
'=');
94 if (lhs.compare(
"") == 0) {
95 }
else if (lhs.compare(
"stan_version_major") == 0) {
97 }
else if (lhs.compare(
"stan_version_minor") == 0) {
99 }
else if (lhs.compare(
"stan_version_patch") == 0) {
101 }
else if (lhs.compare(
"data") == 0) {
103 }
else if (lhs.compare(
"init") == 0) {
104 std::getline(ss, metadata.
init);
105 boost::trim(metadata.
init);
107 }
else if (lhs.compare(
"append_samples") == 0) {
109 }
else if (lhs.compare(
"save_warmup") == 0) {
111 }
else if (lhs.compare(
"seed") == 0) {
114 }
else if (lhs.compare(
"chain_id") == 0) {
116 }
else if (lhs.compare(
"iter") == 0) {
118 }
else if (lhs.compare(
"warmup") == 0) {
120 }
else if (lhs.compare(
"thin") == 0) {
122 }
else if (lhs.compare(
"equal_step_sizes") == 0) {
124 }
else if (lhs.compare(
"nondiag_mass") == 0) {
126 }
else if (lhs.compare(
"leapfrog_steps") == 0) {
128 }
else if (lhs.compare(
"max_treedepth") == 0) {
130 }
else if (lhs.compare(
"epsilon") == 0) {
132 }
else if (lhs.compare(
"epsilon_pm") == 0) {
134 }
else if (lhs.compare(
"delta") == 0) {
135 ss >> metadata.
delta;
136 }
else if (lhs.compare(
"gamma") == 0) {
137 ss >> metadata.
gamma;
139 std::cout <<
"unused option: " << lhs << std::endl;
141 std::getline(ss, line);
143 if (ss.good() ==
true)
148 static bool read_header(std::istream& in, Eigen::Matrix<std::string, Eigen::Dynamic, 1>& header) {
151 if (in.peek() !=
'l')
153 std::getline(in, line);
154 std::stringstream ss(line);
156 header.resize(std::count(line.begin(), line.end(),
',') + 1);
160 std::getline(ss, token,
',');
163 int pos = token.find(
'.');
165 token.replace(pos, 1,
"[");
166 std::replace(token.begin(), token.end(),
'.',
',');
169 header(idx++) = token;
175 std::stringstream ss;
179 if (in.peek() !=
'#' || in.good() ==
false)
181 while (in.peek() ==
'#') {
182 std::getline(in, line);
186 ss.seekg(std::ios_base::beg);
190 ss >> comment >> adaptation.
sampler;
191 std::getline(ss, line);
193 std::replace(adaptation.
sampler.begin(),
196 std::replace(adaptation.
sampler.begin(),
199 boost::trim(adaptation.
sampler);
203 std::getline(ss, line,
'=');
206 std::getline(ss, line);
209 std::getline(ss, line);
210 std::getline(ss, line);
212 int cols = std::count(line.begin(), line.end(),
',') + 1;
216 std::stringstream line_ss;
221 std::getline(line_ss, token,
',');
225 std::getline(ss, line);
233 std::stringstream ss;
239 if (in.peek() ==
'#' || in.good() ==
false)
243 bool comment_line = (in.peek() ==
'#');
244 std::getline(in, line);
247 int current_cols = std::count(line.begin(), line.end(),
',') + 1;
250 }
else if (cols != current_cols) {
251 std::cout <<
"Error: expected " << cols <<
" columns, but found "
252 << current_cols <<
" instead for row " << rows+1 << std::endl;
259 ss.seekg(std::ios_base::beg);
262 samples.resize(rows, cols);
282 std::cout <<
"Warning: non-fatal error reading metadata" << std::endl;
285 std::cout <<
"Error: error reading header" << std::endl;
286 throw std::invalid_argument(
"Error with header of input file in parse");
289 std::cout <<
"Warning: non-fatal error reading adapation data" << std::endl;
292 std::cout <<
"Warning: non-fatal error reading samples" << std::endl;