17 std::fstream file(target_file, std::fstream::out);
24 if(values.rows() != indices.rows()) {
25 throw std::invalid_argument(fmt::format(
"Inconsistent number of rows of values ({}) and indices ({}).",
26 values.rows(), indices.rows()));
28 if(values.cols() != indices.cols()) {
29 throw std::invalid_argument(fmt::format(
"Inconsistent number of columns of values ({}) and indices ({}).",
30 values.rows(), indices.rows()));
33 long last_col = values.cols() - 1;
36 target << values.rows() <<
" " << values.cols() <<
"\n";
38 for(
int row = 0; row < values.rows(); ++row) {
39 for(
int col = 0; col < last_col; ++col) {
40 target << indices.coeff(row, col) <<
":" << values.coeff(row, col) <<
" ";
42 target << indices.coeff(row, last_col) <<
":" << values.coeff(row, last_col) <<
'\n';
47 std::string line_buffer;
50 if(!std::getline(source, line_buffer)) {
51 throw std::runtime_error(
"Error while reading header");
53 std::stringstream parsing(line_buffer);
54 parsing >> rows >> cols;
56 throw std::runtime_error(
"Error while parsing header");
60 throw std::runtime_error(fmt::format(
"Invalid number of rows {} specified.", rows));
64 throw std::runtime_error(fmt::format(
"Invalid number of columns {} specified.", cols));
72 for(; current_row < rows; ++current_row) {
73 if(!std::getline(source, line_buffer)) {
74 throw std::runtime_error(fmt::format(
"Error while reading predictions for instance {}", current_row));
80 THROW_ERROR(
"Got more predictions than expected ({}) for instance {}", cols, current_row);
82 indices.coeffRef(current_row, k) = index;
83 values.coeffRef(current_row, k) = value;
87 THROW_ERROR(
"Expected {} columns, but got only {}", cols, k);
91 if(current_row != rows) {
92 THROW_ERROR(
"Expected {} rows, but got only {}", rows, current_row);
95 return {std::move(indices), std::move(values)};
99 std::fstream stream(source, std::fstream::in);
104 std::fstream file(target, std::fstream::out);
109 target << values.rows() <<
" " << values.cols() <<
"\n";
110 for(
int row = 0; row < values.rows(); ++row) {
115 std::fstream file(target, std::fstream::out);
133 indices.row(0) << 0, 2, 1;
134 indices.row(1) << 1, 31, 2;
135 values.row(0) << 0.5, 1.5, 0.9;
136 values.row(1) << 1.5, 0.9, 0.4;
137 std::string as_text =
139 "0:0.5 2:1.5 1:0.9\n"
140 "1:1.5 31:0.9 2:0.4\n";
143 std::stringstream target;
145 CHECK(target.str() == as_text);
149 std::stringstream source(as_text);
151 CHECK(loaded.first == indices);
152 CHECK(loaded.second == values);
154 SUBCASE(
"load changed whitespace") {
155 std::stringstream source(
"2 3\t\n"
156 "0:0.5 2: 1.5 1:0.9\n"
157 "1:1.5\t31:0.9 2:0.4");
159 CHECK(loaded.first == indices);
160 CHECK(loaded.second == values);
170 std::stringstream target;
171 SUBCASE(
"mismatched rows") {
175 SUBCASE(
"mismatched columns") {
185 std::stringstream source;
186 SUBCASE(
"missing header") {
187 source.str(
"1:2.0 4:1.0");
190 SUBCASE(
"invalid rows") {
191 source.str(
"-5 4\n");
194 SUBCASE(
"invalid columns") {
198 SUBCASE(
"too many columns") {
199 source.str(
"1 2\n1:5.0 2:0.5 3:5.2");
202 SUBCASE(
"too few columns") {
203 source.str(
"1 2\n1:5.0");
206 SUBCASE(
"too few rows") {
207 source.str(
"2 2\n1:5.0 2:0.5");
224 values.row(0) << 0.5, 1.5, 0.9;
225 values.row(1) << 1.5, 0.9, 0.4;
226 std::string as_text =
231 std::stringstream target;
233 CHECK(target.str() == as_text);
building blocks for io procedures that are used by multiple io subsystems
TEST_CASE("save_load_sparse_predictions")
void save_dense_predictions_as_txt(const path &target, const PredictionMatrix &values)
Saves predictions as a dense txt matrix.
void save_dense_predictions_as_npy(const path &target, const PredictionMatrix &values)
Saves predictions as a dense npy file.
std::pair< IndexMatrix, PredictionMatrix > read_sparse_prediction(std::istream &source)
Reads sparse predictions as saved by save_sparse_predictions().
void save_sparse_predictions(const path &target, const PredictionMatrix &values, const IndexMatrix &indices)
Saves sparse predictions as a text file.
std::ostream & write_vector_as_text(std::ostream &stream, const Eigen::Ref< const DenseRealVector > &data)
Writes the given vector as space-separated human-readable numbers.
void parse_sparse_vector_from_text(const char *feature_part, F &&callback)
parses sparse features given in index:value text format.
void save_matrix_to_npy(std::ostream &source, const types::DenseRowMajor< real_t > &)
Saves a matrix to a numpy array.
Main namespace in which all types, classes, and functions are defined.
types::DenseRowMajor< long > IndexMatrix
Matrix used for indices in sparse predictions.
types::DenseRowMajor< real_t > PredictionMatrix
Dense matrix in Row Major format used for predictions.