42 #ifndef SHARK_DATA_CSV_H 43 #define SHARK_DATA_CSV_H 74 template<
typename T,
typename Stream>
78 bool scientific =
true,
79 unsigned int fieldwidth = 0
85 out.setf(std::ios_base::scientific);
86 std::streamsize ss = out.precision();
90 typename T::const_iterator it = data.begin();
91 for (; it != data.end(); ++it) {
93 for (std::size_t i=0; i<(*it).size()-1; i++) {
94 out << std::setw(fieldwidth) << (*it)(i) << separator;
96 out << std::setw(fieldwidth) << (*it)((*it).size()-1) << std::endl;
105 template<
typename T,
typename U,
typename Stream>
106 void exportCSV_labeled(
const T &input,
111 bool scientific =
true,
112 unsigned int fieldwidth = 0,
113 typename boost::enable_if<
114 std::is_arithmetic<
typename boost::range_value<U>::type>
120 out.setf(std::ios_base::scientific);
121 std::streamsize ss = out.precision();
124 typename T::const_iterator iti = input.begin();
125 typename U::const_iterator itl = labels.begin();
128 for (; iti != input.end(); ++iti, ++itl) {
131 out << *itl << separator;
132 for (std::size_t i=0; i<(*iti).size()-1; i++) {
133 out << std::setw(fieldwidth) << (*iti)(i) << separator;
136 out << std::setw(fieldwidth) << (*iti)((*iti).size()-1) << std::endl;
138 out << std::setw(fieldwidth) << (*iti)((*iti).size()-1) << separator << *itl << std::endl;
145 template<
typename T,
typename U,
typename Stream>
146 void exportCSV_labeled(
152 bool scientific =
true,
153 unsigned int fieldwidth = 0,
154 typename boost::disable_if<
155 std::is_arithmetic<
typename boost::range_value<U>::type>
162 out.setf(std::ios_base::scientific);
163 std::streamsize ss = out.precision();
166 typename T::const_iterator iti = input.begin();
167 typename U::const_iterator itl = labels.begin();
169 for (; iti != input.end(); ++iti, ++itl) {
170 SHARK_RUNTIME_CHECK(iti->begin() != iti->end(),
"[exportCSV (2)] record must not be empty");
172 for (std::size_t j = 0; j < itl->size(); j++)
173 out << std::setw(fieldwidth) << (*itl)(j) << separator;
175 for (std::size_t i=0; i<(*iti).size()-1; i++) {
176 out << std::setw(fieldwidth) << (*iti)(i) << separator;
179 out << std::setw(fieldwidth) << (*iti)((*iti).size()-1) << std::endl;
181 out << std::setw(fieldwidth) << (*iti)((*iti).size()-1);
182 for (std::size_t j = 0; j < itl->size(); j++)
183 out << std::setw(fieldwidth) << separator << (*itl)(j);
204 std::string
const& contents,
205 char separator =
',',
219 std::string
const& contents,
220 char separator =
',',
234 std::string
const& contents,
235 char separator =
',',
249 std::string
const& contents,
250 char separator =
',',
264 std::string
const& contents,
265 char separator =
',',
279 std::string
const& contents,
280 char separator =
',',
295 std::string
const& contents,
297 char separator =
',',
312 std::string
const& contents,
314 char separator =
',',
331 std::string
const& contents,
333 std::size_t numberOfOutputs = 1,
334 char separator =
',',
350 std::string
const& contents,
352 std::size_t numberOfOutputs = 1,
353 char separator =
',',
372 char separator =
',',
375 std::size_t titleLines = 0
377 std::ifstream stream(fn.c_str());
380 stream.unsetf(std::ios::skipws);
382 for(std::size_t i=0; i < titleLines; ++i)
383 stream.ignore(std::numeric_limits<std::streamsize>::max(),
'\n');
385 std::istream_iterator<char> streamBegin(stream);
386 std::string contents(
388 std::istream_iterator<char>()
407 char separator =
',',
411 std::ifstream stream(fn.c_str());
414 stream.unsetf(std::ios::skipws);
415 std::istream_iterator<char> streamBegin(stream);
416 std::string contents(
418 std::istream_iterator<char>()
435 LabeledData<blas::vector<T>, blas::vector<T> >& data,
438 std::size_t numberOfOutputs = 1,
439 char separator =
',',
443 std::ifstream stream(fn.c_str());
446 stream.unsetf(std::ios::skipws);
447 std::istream_iterator<char> streamBegin(stream);
448 std::string contents(
450 std::istream_iterator<char>()
453 csvStringToData(data,contents,lp, numberOfOutputs, separator,comment,maximumBatchSize);
463 template<
typename Type>
467 char separator =
',',
469 unsigned int width = 0
471 std::ofstream ofs(fn.c_str());
485 template<
typename InputType,
typename LabelType>
490 char separator =
',',
492 unsigned int width = 0
494 std::ofstream ofs(fn.c_str());
503 #endif // SHARK_ML_CSV_H