Commit 5d3bf0a3 authored by Konstantinos Chatzilygeroudis's avatar Konstantinos Chatzilygeroudis
Browse files

A few fixes in save/load

parent 13fddac7
......@@ -428,7 +428,7 @@ namespace limbo {
/// save the parameters and the data for the GP to the archive (text or binary)
template <typename A>
void save(A& archive)
void save(const A& archive)
{
if (_kernel_function.h_params_size() > 0) {
archive.save(_kernel_function.h_params(), "kernel_params");
......@@ -446,13 +446,13 @@ namespace limbo {
template <typename A>
void load(const std::string& directory, bool recompute = true)
{
A archive(directory, recompute);
load(archive);
A archive(directory);
load(archive, recompute);
}
/// load the parameters and the data for the GP from the archive (text or binary)
template <typename A>
void load(A& archive, bool recompute = true)
void load(const A& archive, bool recompute = true)
{
_samples.clear();
archive.load(_samples, "samples");
......
......@@ -17,7 +17,7 @@ namespace limbo {
BinaryArchive(const std::string& dir_name) : _dir_name(dir_name) {}
/// write an Eigen::Matrix*
void save(const Eigen::MatrixXd& v, const std::string& object_name)
void save(const Eigen::MatrixXd& v, const std::string& object_name) const
{
_create_directory();
......@@ -28,7 +28,7 @@ namespace limbo {
/// write a vector of Eigen::Vector*
template <typename T>
void save(const std::vector<T>& v, const std::string& object_name)
void save(const std::vector<T>& v, const std::string& object_name) const
{
_create_directory();
......@@ -47,7 +47,7 @@ namespace limbo {
/// load an Eigen matrix (or vector)
template <typename M>
void load(M& m, const std::string& object_name)
void load(M& m, const std::string& object_name) const
{
std::ifstream in(fname(object_name).c_str(), std::ios::in | std::ios::binary);
_read_binary(in, m);
......@@ -56,7 +56,7 @@ namespace limbo {
/// load a vector of Eigen::Vector*
template <typename V>
void load(std::vector<V>& m_list, const std::string& object_name)
void load(std::vector<V>& m_list, const std::string& object_name) const
{
m_list.clear();
......@@ -81,16 +81,15 @@ namespace limbo {
protected:
std::string _dir_name;
Eigen::IOFormat _fmt;
void _create_directory()
void _create_directory() const
{
boost::filesystem::path my_path(_dir_name);
boost::filesystem::create_directory(my_path);
}
template <class Matrix, class Stream>
void _write_binary(Stream& out, const Matrix& matrix)
void _write_binary(Stream& out, const Matrix& matrix) const
{
typename Matrix::Index rows = matrix.rows(), cols = matrix.cols();
out.write((char*)(&rows), sizeof(typename Matrix::Index));
......@@ -99,7 +98,7 @@ namespace limbo {
}
template <class Matrix, class Stream>
void _read_binary(Stream& in, Matrix& matrix)
void _read_binary(Stream& in, Matrix& matrix) const
{
typename Matrix::Index rows = 0, cols = 0;
in.read((char*)(&rows), sizeof(typename Matrix::Index));
......
......@@ -18,7 +18,7 @@ namespace limbo {
_fmt(Eigen::FullPrecision, Eigen::DontAlignCols, " ", "\n", "", "") {}
/// write an Eigen::Matrix*
void save(const Eigen::MatrixXd& v, const std::string& object_name)
void save(const Eigen::MatrixXd& v, const std::string& object_name) const
{
_create_directory();
std::ofstream ofs(fname(object_name).c_str());
......@@ -27,7 +27,7 @@ namespace limbo {
/// write a vector of Eigen::Vector*
template <typename T>
void save(const std::vector<T>& v, const std::string& object_name)
void save(const std::vector<T>& v, const std::string& object_name) const
{
_create_directory();
std::ofstream ofs(fname(object_name).c_str());
......@@ -38,7 +38,7 @@ namespace limbo {
/// load an Eigen matrix (or vector)
template <typename M>
void load(M& m, const std::string& object_name)
void load(M& m, const std::string& object_name) const
{
auto values = _load(object_name);
m.resize(values.size(), values[0].size());
......@@ -49,7 +49,7 @@ namespace limbo {
/// load a vector of Eigen::Vector*
template <typename V>
void load(std::vector<V>& m_list, const std::string& object_name)
void load(std::vector<V>& m_list, const std::string& object_name) const
{
m_list.clear();
auto values = _load(object_name);
......@@ -72,13 +72,13 @@ namespace limbo {
std::string _dir_name;
Eigen::IOFormat _fmt;
void _create_directory()
void _create_directory() const
{
boost::filesystem::path my_path(_dir_name);
boost::filesystem::create_directory(my_path);
}
std::vector<std::vector<double>> _load(const std::string& object_name)
std::vector<std::vector<double>> _load(const std::string& object_name) const
{
std::ifstream ifs(fname(object_name).c_str());
assert(ifs.good() && "file not found");
......
......@@ -106,11 +106,12 @@ void test_gp(const std::string& name, bool optimize_hp = true)
// attempt to save
Archive a1(name);
gp.save(a1);
// We can also save like this
// gp.template save<Archive>(name);
// attempt to load
// attempt to load -- use only the name
GP gp2(3, 1);
Archive a2(name);
gp2.load(a2);
gp2.template load<Archive>(name);
BOOST_CHECK_EQUAL(gp.nb_samples(), gp2.nb_samples());
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment