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/copy.hpp> 52 #include <boost/iostreams/filter/newline.hpp> 53 #include <boost/iostreams/filtering_stream.hpp> 54 #include <boost/lexical_cast.hpp> 55 #include <boost/type_traits.hpp> 85 template<
typename T,
typename Stream>
89 bool scientific =
true,
90 unsigned int fieldwidth = 0
93 throw(std::invalid_argument(
"[exportCSV (1)] Stream cannot be opened for writing."));
98 out.setf(std::ios_base::scientific);
99 std::streamsize ss = out.precision();
103 typename T::const_iterator it = data.begin();
104 for (; it != data.end(); ++it) {
105 SHARK_CHECK(it->begin() != it->end(),
"[exportCSV (1)] record must not be empty");
106 for (std::size_t i=0; i<(*it).size()-1; i++) {
107 out << std::setw(fieldwidth) << (*it)(i) << separator;
109 out << std::setw(fieldwidth) << (*it)((*it).size()-1) << std::endl;
118 template<
typename T,
typename U,
typename Stream>
119 void exportCSV_labeled(
const T &input,
124 bool scientific =
true,
125 unsigned int fieldwidth = 0,
126 typename boost::enable_if<
127 boost::is_arithmetic<
typename boost::range_value<U>::type>
132 throw(std::invalid_argument(
"[exportCSV (2)] Stream cannot be opened for writing."));
137 out.setf(std::ios_base::scientific);
138 std::streamsize ss = out.precision();
141 typename T::const_iterator iti = input.begin();
142 typename U::const_iterator itl = labels.begin();
145 for (; iti != input.end(); ++iti, ++itl) {
146 SHARK_CHECK(iti->begin() != iti->end(),
"[exportCSV (2)] record must not be empty");
148 out << *itl << separator;
149 for (std::size_t i=0; i<(*iti).size()-1; i++) {
150 out << std::setw(fieldwidth) << (*iti)(i) << separator;
153 out << std::setw(fieldwidth) << (*iti)((*iti).size()-1) << std::endl;
155 out << std::setw(fieldwidth) << (*iti)((*iti).size()-1) << separator << *itl << std::endl;
162 template<
typename T,
typename U,
typename Stream>
163 void exportCSV_labeled(
169 bool scientific =
true,
170 unsigned int fieldwidth = 0,
171 typename boost::disable_if<
172 boost::is_arithmetic<
typename boost::range_value<U>::type>
177 throw(std::invalid_argument(
"[exportCSV (2)] Stream cannot be opened for writing."));
182 out.setf(std::ios_base::scientific);
183 std::streamsize ss = out.precision();
186 typename T::const_iterator iti = input.begin();
187 typename U::const_iterator itl = labels.begin();
189 for (; iti != input.end(); ++iti, ++itl) {
190 SHARK_CHECK(iti->begin() != iti->end(),
"[exportCSV (2)] record must not be empty");
192 for (std::size_t j = 0; j < itl->size(); j++) out << std::setw(fieldwidth) << (*itl)(j) << separator;
194 for (std::size_t i=0; i<(*iti).size()-1; i++) {
195 out << std::setw(fieldwidth) << (*iti)(i) << separator;
198 out << std::setw(fieldwidth) << (*iti)((*iti).size()-1) << std::endl;
200 out << std::setw(fieldwidth) << (*iti)((*iti).size()-1);
201 for (std::size_t j = 0; j < itl->size(); j++) out << std::setw(fieldwidth) << separator << (*itl)(j);
222 std::string
const& contents,
223 char separator =
',',
237 std::string
const& contents,
238 char separator =
',',
252 std::string
const& contents,
253 char separator =
',',
267 std::string
const& contents,
268 char separator =
',',
282 std::string
const& contents,
283 char separator =
',',
297 std::string
const& contents,
298 char separator =
',',
313 std::string
const& contents,
315 char separator =
',',
330 std::string
const& contents,
332 char separator =
',',
349 std::string
const& contents,
351 std::size_t numberOfOutputs = 1,
352 char separator =
',',
368 std::string
const& contents,
370 std::size_t numberOfOutputs = 1,
371 char separator =
',',
390 char separator =
',',
393 std::size_t titleLines = 0
395 std::ifstream stream(fn.c_str());
396 stream.unsetf(std::ios::skipws);
398 for(std::size_t i=0; i < titleLines; ++i)
401 std::istream_iterator<char> streamBegin(stream);
402 std::string contents(
404 std::istream_iterator<char>()
423 char separator =
',',
427 std::ifstream stream(fn.c_str());
428 stream.unsetf(std::ios::skipws);
429 std::istream_iterator<char> streamBegin(stream);
430 std::string contents(
432 std::istream_iterator<char>()
452 std::size_t numberOfOutputs = 1,
453 char separator =
',',
457 std::ifstream stream(fn.c_str());
458 stream.unsetf(std::ios::skipws);
459 std::istream_iterator<char> streamBegin(stream);
460 std::string contents(
462 std::istream_iterator<char>()
465 csvStringToData(data,contents,lp, numberOfOutputs, separator,comment,maximumBatchSize);
475 template<
typename Type>
479 char separator =
',',
481 unsigned int width = 0
483 std::ofstream ofs(fn.c_str());
496 template<
typename InputType,
typename LabelType>
501 char separator =
',',
503 unsigned int width = 0
505 std::ofstream ofs(fn.c_str());
513 #endif // SHARK_ML_CSV_H