12 #ifndef MLPACK_CORE_DATA_LOAD_NUMERIC_CSV_HPP 13 #define MLPACK_CORE_DATA_LOAD_NUMERIC_CSV_HPP 22 const std::string& token)
24 const size_t N = size_t(token.length());
32 const char* str = token.c_str();
37 if ((N == 3) || (N == 4))
39 const bool neg = (str[0] ==
'-');
40 const bool pos = (str[0] ==
'+');
42 const size_t offset = ((neg || pos) && (N == 4)) ? 1 : 0;
44 const char sigA = str[offset];
45 const char sigB = str[offset + 1];
46 const char sigC = str[offset + 2];
48 if (((sigA ==
'i') || (sigA ==
'I')) &&
49 ((sigB ==
'n') || (sigB ==
'N')) &&
50 ((sigC ==
'f') || (sigC ==
'F')))
52 val = neg ? -(std::numeric_limits<eT>
53 ::infinity()) : std::numeric_limits<eT>::infinity();
56 else if (((sigA ==
'n') || (sigA ==
'N')) &&
57 ((sigB ==
'a') || (sigB ==
'A')) &&
58 ((sigC ==
'n') || (sigC ==
'N')))
60 val = std::numeric_limits<eT>::quiet_NaN();
65 char* endptr =
nullptr;
70 if (std::is_floating_point<eT>::value)
72 val = eT(std::strtod(str, &endptr));
74 else if (std::is_integral<eT>::value)
76 if (std::is_signed<eT>::value)
77 val = eT(std::strtoll(str, &endptr, 10));
85 val = eT(std::strtoull(str, &endptr, 10));
102 template<
typename eT>
105 bool loadOkay = f.good();
107 std::pair<size_t, size_t> mat_size = GetMatrixSize<true>(f);
108 x.zeros(mat_size.first, mat_size.second);
111 std::string lineString;
112 std::stringstream lineStream;
118 std::getline(f, lineString);
120 if (lineString.size() == 0)
124 lineStream.str(lineString);
128 while (lineStream.good())
131 std::getline(lineStream, token,
',');
137 if (ConvertToken<eT>(tmpVal, token))
139 x.at(row, col) = tmpVal;
145 Log::Warn <<
"Failed to convert token " << token <<
", at row " << row
146 <<
", column " << col <<
" of matrix!";
161 while (lineStream.good())
163 std::getline(lineStream, token, delim);
static util::PrefixedOutStream Warn
Linear algebra utility functions, generally performed on matrices or vectors.
bool ConvertToken(eT &val, const std::string &token)
Converts the given string token to assigned datatype and assigns this value to the given address...
bool LoadNumericCSV(arma::Mat< eT > &x, std::fstream &f)
Returns a bool value showing whether data was loaded successfully or not.
constexpr auto data(Container const &container) noexcept -> decltype(container.data())
void NumericMatSize(std::stringstream &lineStream, size_t &col, const char delim)
Calculate the number of columns in each row and assign the value to the col.