vec_fixture() { a = vec::continuous(u, "a", 1).desc(); b = vec::continuous(u, "b", 2).desc(); a0 = a(0); a1 = a(1); b0 = b(0); ds.initialize({a, b}); dense_matrix<> values; values.resize(3, 3); values << 0, 1, 2, 3, 4, 1, 6, 7, 8; ds.insert(values, 1.0); values.resize(3, 1); values << 3, 2, 1; ds.insert(values, 2.0); values.resize(3, 2); values << 2, 3, 1, 0, 8, 9; ds.insert(values, 3.0); values.resize(3, 0); ds.insert(values, 0.5); }
void save(const std::vector<std::string>& filenames, const text_dataset_format<Arg>& format, const real_sequence_dataset<Arg, T>& ds) { // Check the arguments if (!format.all_sequences_continuous()) { throw std::domain_error( "The format contains sequence(s) that are not continuous-valued" ); } if (filenames.size() != ds.size()) { throw std::invalid_argument("The number of filenames and samples differ"); } std::size_t row = 0; for (const auto& sample : ds.samples(format.sequences)) { // Open the file std::ofstream out(filenames[row]); if (!out) { throw std::runtime_error("Cannot open the file " + filenames[row]); } ++row; // Output dummy rows for (std::size_t i = 0; i < format.skip_rows; ++i) { out << std::endl; } // Output the data std::string separator = format.separator.empty() ? " " : format.separator; const real_matrix<T>& data = sample.first; for (std::size_t t = 0; t < data.cols(); ++t) { for (std::size_t i = 0; i < format.skip_cols; ++i) { out << "0" << separator; } for (std::size_t i = 0; i < data.rows(); ++i) { if (i > 0) { out << separator; } if (ismissing(data(i, t))) { out << format.missing; } else { out << data(i, t); } } out << std::endl; } } }
void load(const std::vector<std::string>& filenames, const text_dataset_format<Arg>& format, real_sequence_dataset<Arg, T>& ds) { // initialize the dataset if (!format.all_sequences_continuous()) { throw std::domain_error( "The format contains sequence(s) that are not continuous-valued" ); } ds.initialize(format.sequences, filenames.size()); std::size_t num_dims = ds.num_cols(); for (const std::string& filename : filenames) { // open the file std::ifstream in(filename); if (!in) { throw std::runtime_error("Cannot open the file " + filename); } // read the table line-by-line, storing the values for each time step std::vector<T> values; std::string line; std::size_t line_number = 0; std::size_t t = 0; std::vector<const char*> tokens; while (std::getline(in, line)) { if (format.tokenize(num_dims, line, line_number, tokens)) { ++t; for (std::size_t i = 0; i < num_dims; ++i) { const char* token = tokens[i + format.skip_cols]; if (token == format.missing) { values.push_back(missing<T>::value); } else { values.push_back(parse_string<T>(token)); } } } } // the values are already in the right format (column-major) ds.insert(Eigen::Map<real_matrix<T> >(values.data(), num_dims, t), T(1)); } }