42 #ifndef SHARK_DATA_CSV_H 43 #define SHARK_DATA_CSV_H 48 #include <boost/algorithm/string.hpp> 49 #include <boost/algorithm/string/trim.hpp> 50 #include <boost/format.hpp> 51 #include <boost/iostreams/filter/newline.hpp> 52 #include <boost/lexical_cast.hpp> 53 #include <boost/type_traits.hpp> 83 template<
typename T,
typename Stream>
87 bool scientific =
true,
88 unsigned int fieldwidth = 0
91 throw(std::invalid_argument(
"[exportCSV (1)] Stream cannot be opened for writing."));
96 out.setf(std::ios_base::scientific);
97 std::streamsize ss = out.precision();
101 typename T::const_iterator it = data.begin();
102 for (; it != data.end(); ++it) {
103 SHARK_CHECK(it->begin() != it->end(),
"[exportCSV (1)] record must not be empty");
104 for (std::size_t i=0; i<(*it).size()-1; i++) {
105 out << std::setw(fieldwidth) << (*it)(i) << separator;
107 out << std::setw(fieldwidth) << (*it)((*it).size()-1) << std::endl;
116 template<
typename T,
typename U,
typename Stream>
117 void exportCSV_labeled(
const T &input,
122 bool scientific =
true,
123 unsigned int fieldwidth = 0,
124 typename boost::enable_if<
125 boost::is_arithmetic<
typename boost::range_value<U>::type>
130 throw(std::invalid_argument(
"[exportCSV (2)] Stream cannot be opened for writing."));
135 out.setf(std::ios_base::scientific);
136 std::streamsize ss = out.precision();
139 typename T::const_iterator iti = input.begin();
140 typename U::const_iterator itl = labels.begin();
143 for (; iti != input.end(); ++iti, ++itl) {
144 SHARK_CHECK(iti->begin() != iti->end(),
"[exportCSV (2)] record must not be empty");
146 out << *itl << separator;
147 for (std::size_t i=0; i<(*iti).size()-1; i++) {
148 out << std::setw(fieldwidth) << (*iti)(i) << separator;
151 out << std::setw(fieldwidth) << (*iti)((*iti).size()-1) << std::endl;
153 out << std::setw(fieldwidth) << (*iti)((*iti).size()-1) << separator << *itl << std::endl;
160 template<
typename T,
typename U,
typename Stream>
161 void exportCSV_labeled(
167 bool scientific =
true,
168 unsigned int fieldwidth = 0,
169 typename boost::disable_if<
170 boost::is_arithmetic<
typename boost::range_value<U>::type>
175 throw(std::invalid_argument(
"[exportCSV (2)] Stream cannot be opened for writing."));
180 out.setf(std::ios_base::scientific);
181 std::streamsize ss = out.precision();
184 typename T::const_iterator iti = input.begin();
185 typename U::const_iterator itl = labels.begin();
187 for (; iti != input.end(); ++iti, ++itl) {
188 SHARK_CHECK(iti->begin() != iti->end(),
"[exportCSV (2)] record must not be empty");
190 for (std::size_t j = 0; j < itl->size(); j++) out << std::setw(fieldwidth) << (*itl)(j) << separator;
192 for (std::size_t i=0; i<(*iti).size()-1; i++) {
193 out << std::setw(fieldwidth) << (*iti)(i) << separator;
196 out << std::setw(fieldwidth) << (*iti)((*iti).size()-1) << std::endl;
198 out << std::setw(fieldwidth) << (*iti)((*iti).size()-1);
199 for (std::size_t j = 0; j < itl->size(); j++) out << std::setw(fieldwidth) << separator << (*itl)(j);
220 std::string
const& contents,
221 char separator =
',',
235 std::string
const& contents,
236 char separator =
',',
250 std::string
const& contents,
251 char separator =
',',
265 std::string
const& contents,
266 char separator =
',',
280 std::string
const& contents,
281 char separator =
',',
295 std::string
const& contents,
296 char separator =
',',
311 std::string
const& contents,
313 char separator =
',',
328 std::string
const& contents,
330 char separator =
',',
347 std::string
const& contents,
349 std::size_t numberOfOutputs = 1,
350 char separator =
',',
366 std::string
const& contents,
368 std::size_t numberOfOutputs = 1,
369 char separator =
',',
388 char separator =
',',
391 std::size_t titleLines = 0
393 std::ifstream stream(fn.c_str());
394 stream.unsetf(std::ios::skipws);
396 for(std::size_t i=0; i < titleLines; ++i)
399 std::istream_iterator<char> streamBegin(stream);
400 std::string contents(
402 std::istream_iterator<char>()
421 char separator =
',',
425 std::ifstream stream(fn.c_str());
426 stream.unsetf(std::ios::skipws);
427 std::istream_iterator<char> streamBegin(stream);
428 std::string contents(
430 std::istream_iterator<char>()
450 std::size_t numberOfOutputs = 1,
451 char separator =
',',
455 std::ifstream stream(fn.c_str());
456 stream.unsetf(std::ios::skipws);
457 std::istream_iterator<char> streamBegin(stream);
458 std::string contents(
460 std::istream_iterator<char>()
463 csvStringToData(data,contents,lp, numberOfOutputs, separator,comment,maximumBatchSize);
473 template<
typename Type>
477 char separator =
',',
479 unsigned int width = 0
481 std::ofstream ofs(fn.c_str());
494 template<
typename InputType,
typename LabelType>
499 char separator =
',',
501 unsigned int width = 0
503 std::ofstream ofs(fn.c_str());
511 #endif // SHARK_ML_CSV_H