12 #ifndef MLPACK_CORE_DATA_SAVE_IMPL_HPP 13 #define MLPACK_CORE_DATA_SAVE_IMPL_HPP 20 #include <cereal/archives/xml.hpp> 21 #include <cereal/archives/json.hpp> 22 #include <cereal/archives/binary.hpp> 28 bool Save(
const std::string& filename,
29 const arma::Col<eT>& vec,
31 arma::file_type inputSaveType)
34 return Save(filename, vec, fatal,
false, inputSaveType);
38 bool Save(
const std::string& filename,
39 const arma::Row<eT>& rowvec,
41 arma::file_type inputSaveType)
43 return Save(filename, rowvec, fatal,
true, inputSaveType);
47 bool Save(
const std::string& filename,
48 const arma::Mat<eT>& matrix,
51 arma::file_type inputSaveType)
55 arma::file_type saveType = inputSaveType;
56 std::string stringType =
"";
58 if (inputSaveType == arma::auto_detect)
62 if (saveType == arma::file_type_unknown)
65 Log::Fatal <<
"Could not detect type of file '" << filename <<
"' for " 66 <<
"writing. Save failed." << std::endl;
68 Log::Warn <<
"Could not detect type of file '" << filename <<
"' for " 69 <<
"writing. Save failed." << std::endl;
79 #ifdef _WIN32 // Always open in binary mode on Windows. 80 stream.open(filename.c_str(), std::fstream::out | std::fstream::binary);
82 stream.open(filename.c_str(), std::fstream::out);
84 if (!stream.is_open())
88 Log::Fatal <<
"Cannot open file '" << filename <<
"' for writing. " 89 <<
"Save failed." << std::endl;
91 Log::Warn <<
"Cannot open file '" << filename <<
"' for writing; save " 92 <<
"failed." << std::endl;
98 Log::Info <<
"Saving " << stringType <<
" to '" << filename <<
"'." 104 arma::Mat<eT> tmp = trans(matrix);
108 const bool success = (saveType == arma::hdf5_binary) ?
109 tmp.quiet_save(filename, saveType) :
110 tmp.quiet_save(stream, saveType);
112 const bool success = tmp.quiet_save(stream, saveType);
118 Log::Fatal <<
"Save to '" << filename <<
"' failed." << std::endl;
120 Log::Warn <<
"Save to '" << filename <<
"' failed." << std::endl;
129 const bool success = (saveType == arma::hdf5_binary) ?
130 matrix.quiet_save(filename, saveType) :
131 matrix.quiet_save(stream, saveType);
133 const bool success = matrix.quiet_save(stream, saveType);
139 Log::Fatal <<
"Save to '" << filename <<
"' failed." << std::endl;
141 Log::Warn <<
"Save to '" << filename <<
"' failed." << std::endl;
154 template<
typename eT>
155 bool Save(
const std::string& filename,
156 const arma::SpMat<eT>& matrix,
163 std::string extension = Extension(filename);
168 Log::Fatal <<
"No extension given with filename '" << filename <<
"'; " 169 <<
"type unknown. Save failed." << std::endl;
171 Log::Warn <<
"No extension given with filename '" << filename <<
"'; " 172 <<
"type unknown. Save failed." << std::endl;
179 #ifdef _WIN32 // Always open in binary mode on Windows. 180 stream.open(filename.c_str(), std::fstream::out | std::fstream::binary);
182 stream.open(filename.c_str(), std::fstream::out);
184 if (!stream.is_open())
188 Log::Fatal <<
"Cannot open file '" << filename <<
"' for writing. " 189 <<
"Save failed." << std::endl;
191 Log::Warn <<
"Cannot open file '" << filename <<
"' for writing; save " 192 <<
"failed." << std::endl;
197 bool unknownType =
false;
198 arma::file_type saveType;
199 std::string stringType;
201 if (extension ==
"txt" || extension ==
"tsv")
203 saveType = arma::coord_ascii;
204 stringType =
"raw ASCII formatted data";
206 else if (extension ==
"bin")
208 saveType = arma::arma_binary;
209 stringType =
"Armadillo binary formatted data";
214 saveType = arma::raw_binary;
223 Log::Fatal <<
"Unable to determine format to save to from filename '" 224 << filename <<
"'. Save failed." << std::endl;
226 Log::Warn <<
"Unable to determine format to save to from filename '" 227 << filename <<
"'. Save failed." << std::endl;
233 Log::Info <<
"Saving " << stringType <<
" to '" << filename <<
"'." 236 arma::SpMat<eT> tmp = matrix;
244 const bool success = tmp.quiet_save(stream, saveType);
249 Log::Fatal <<
"Save to '" << filename <<
"' failed." << std::endl;
251 Log::Warn <<
"Save to '" << filename <<
"' failed." << std::endl;
264 bool Save(
const std::string& filename,
265 const std::string& name,
270 if (f == format::autodetect)
272 std::string extension = Extension(filename);
274 if (extension ==
"xml")
276 else if (extension ==
"bin")
278 else if (extension ==
"json")
283 Log::Fatal <<
"Unable to detect type of '" << filename <<
"'; incorrect" 284 <<
" extension? (allowed: xml/bin/json)" << std::endl;
286 Log::Warn <<
"Unable to detect type of '" << filename <<
"'; save " 287 <<
"failed. Incorrect extension? (allowed: xml/bin/json)" 297 if (f == format::binary)
298 ofs.open(filename, std::ofstream::out | std::ofstream::binary);
300 ofs.open(filename, std::ofstream::out);
302 ofs.open(filename, std::ofstream::out);
308 Log::Fatal <<
"Unable to open file '" << filename <<
"' to save object '" 309 << name <<
"'." << std::endl;
311 Log::Warn <<
"Unable to open file '" << filename <<
"' to save object '" 312 << name <<
"'." << std::endl;
319 if (f == format::xml)
321 cereal::XMLOutputArchive ar(ofs);
322 ar(cereal::make_nvp(name.c_str(), t));
324 else if (f == format::json)
326 cereal::JSONOutputArchive ar(ofs);
327 ar(cereal::make_nvp(name.c_str(), t));
329 else if (f == format::binary)
331 cereal::BinaryOutputArchive ar(ofs);
332 ar(cereal::make_nvp(name.c_str(), t));
337 catch (cereal::Exception& e)
356 template<
typename eT>
357 bool Save(
const std::string& filename,
358 arma::Mat<eT>& matrix,
362 arma::Mat<unsigned char> tmpMatrix =
363 arma::conv_to<arma::Mat<unsigned char>>::from(matrix);
366 return SaveImage(filename, tmpMatrix, info, fatal);
370 template<
typename eT>
371 bool Save(
const std::vector<std::string>& files,
372 arma::Mat<eT>& matrix,
376 if (files.size() == 0)
380 Log::Fatal <<
"Save(): vector of image files is empty; nothing to save." 385 Log::Warn <<
"Save(): vector of image files is empty; nothing to save." 392 arma::Mat<unsigned char> img;
395 for (
size_t i = 0; i < files.size() ; ++i)
397 arma::Mat<eT> colImg(matrix.colptr(i), matrix.n_rows, 1,
399 status &=
Save(files[i], colImg, info, fatal);
static void Start(const std::string &name)
Start the given timer.
Definition: timers.cpp:28
static MLPACK_EXPORT util::PrefixedOutStream Fatal
Prints fatal messages prefixed with [FATAL], then terminates the program.
Definition: log.hpp:90
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: cv.hpp:1
Implements meta-data of images required by data::Load and data::Save for loading and saving images in...
Definition: image_info.hpp:36
bool SaveImage(const std::string &filename, arma::Mat< unsigned char > &image, ImageInfo &info, const bool fatal=false)
Helper function to save files.
Definition: save_image.cpp:130
std::string GetStringType(const arma::file_type &type)
Given a file type, return a logical name corresponding to that file type.
Definition: detect_file_type.cpp:30
static MLPACK_EXPORT util::PrefixedOutStream Warn
Prints warning messages prefixed with [WARN ].
Definition: log.hpp:87
static void Stop(const std::string &name)
Stop the given timer.
Definition: timers.cpp:36
static MLPACK_EXPORT util::PrefixedOutStream Info
Prints informational messages if –verbose is specified, prefixed with [INFO ].
Definition: log.hpp:84
format
Define the formats we can read through cereal.
Definition: format.hpp:20
bool Save(const std::string &filename, const arma::Mat< eT > &matrix, const bool fatal=false, bool transpose=true, arma::file_type inputSaveType=arma::auto_detect)
Saves a matrix to file, guessing the filetype from the extension.
Definition: save_impl.hpp:47
arma::file_type DetectFromExtension(const std::string &filename)
Return the type based only on the extension.
Definition: detect_file_type.cpp:310