[mlpack-git] master: Remove SaveRestoreUtility. An ignominious end to an ambitious idea. (efbddf2)
gitdub at big.cc.gt.atl.ga.us
gitdub at big.cc.gt.atl.ga.us
Mon Jul 13 04:04:28 EDT 2015
Repository : https://github.com/mlpack/mlpack
On branch : master
Link : https://github.com/mlpack/mlpack/compare/8b2ca720828224607c70d2b539c43aecf8f4ec32...b4659b668021db631b3c8a48e3d735b513706fdc
>---------------------------------------------------------------
commit efbddf2b98a2314c39eed4a750c0b5ee388c8a93
Author: Ryan Curtin <ryan at ratml.org>
Date: Sat Jul 11 12:07:59 2015 +0000
Remove SaveRestoreUtility.
An ignominious end to an ambitious idea.
>---------------------------------------------------------------
efbddf2b98a2314c39eed4a750c0b5ee388c8a93
src/mlpack/core/util/save_restore_utility.cpp | 127 --------------
src/mlpack/core/util/save_restore_utility.hpp | 173 ------------------
src/mlpack/core/util/save_restore_utility_impl.hpp | 193 ---------------------
3 files changed, 493 deletions(-)
diff --git a/src/mlpack/core/util/save_restore_utility.cpp b/src/mlpack/core/util/save_restore_utility.cpp
deleted file mode 100644
index 7eeaf6d..0000000
--- a/src/mlpack/core/util/save_restore_utility.cpp
+++ /dev/null
@@ -1,127 +0,0 @@
-/**
- * @file save_restore_utility.cpp
- * @author Neil Slagle
- * @author Michael Fox
- *
- * The SaveRestoreUtility provides helper functions in saving and
- * restoring models. The current output file type is XML.
- */
-#include <mlpack/core.hpp>
-
-using namespace mlpack;
-using namespace mlpack::util;
-
-bool SaveRestoreUtility::ReadFile(const std::string& filename)
-{
- xmlDocPtr xmlDocTree = NULL;
- if (NULL == (xmlDocTree = xmlReadFile(filename.c_str(), NULL, 0)))
- {
- Log::Fatal << "Could not load XML file '" << filename << "'!" << std::endl;
- }
-
- xmlNodePtr root = xmlDocGetRootElement(xmlDocTree);
- ReadFile(root->children);
- xmlFreeDoc(xmlDocTree);
- return true;
-}
-
-void SaveRestoreUtility::ReadFile(xmlNode* n)
-{
- parameters.clear();
- xmlNodePtr current = NULL;
- for (current = n; current; current = current->next)
- {
- if (current->type == XML_ELEMENT_NODE)
- {
- xmlChar* content = xmlNodeGetContent(current);
- if(xmlChildElementCount(current) == 0)
- {
- parameters[(const char*) current->name] = (const char*) content;
- }
- else
- {
- children[(const char*) current->name].ReadFile(current->children);
- }
- xmlFree(content);
- }
- }
-}
-
-bool SaveRestoreUtility::WriteFile(const std::string& filename)
-{
- bool success = false;
- xmlDocPtr xmlDocTree = xmlNewDoc(BAD_CAST "1.0");
- xmlNodePtr root = xmlNewNode(NULL, BAD_CAST "root");
- xmlDocSetRootElement(xmlDocTree, root);
- WriteFile(root);
-
- // Actually save the file.
- success = (xmlSaveFormatFileEnc(filename.c_str(), xmlDocTree, "UTF-8", 1) !=
- -1);
- xmlFreeDoc(xmlDocTree);
- return success;
-}
-
-void SaveRestoreUtility::WriteFile(xmlNode* n)
-{
- for (std::map<std::string, std::string>::reverse_iterator it =
- parameters.rbegin(); it != parameters.rend(); ++it)
- {
- xmlNewChild(n, NULL, BAD_CAST(*it).first.c_str(),
- BAD_CAST(*it).second.c_str());
- }
- xmlNodePtr child;
- for (std::map<std::string, SaveRestoreUtility>::iterator it =
- children.begin(); it != children.end(); ++it)
- {
- child = xmlNewChild(n, NULL, BAD_CAST(*it).first.c_str(), NULL);
- it->second.WriteFile(child);
- }
-}
-
-std::string SaveRestoreUtility::LoadParameter(std::string& str,
- const std::string& name) const
-{
- std::map<std::string, std::string>::const_iterator it = parameters.find(name);
- if (it != parameters.end())
- {
- return str = (*it).second;
- }
- else
- {
- Log::Fatal << "LoadParameter(): node '" << name << "' not found.\n";
- }
- return "";
-}
-
-char SaveRestoreUtility::LoadParameter(char c, const std::string& name) const
-{
- std::map<std::string, std::string>::const_iterator it = parameters.find(name);
- if (it != parameters.end())
- {
- int temp;
- std::string value = (*it).second;
- std::istringstream input (value);
- input >> temp;
- return c = (char) temp;
- }
- else
- {
- Log::Fatal << "LoadParameter(): node '" << name << "' not found.\n";
- }
- return 0;
-}
-
-void SaveRestoreUtility::SaveParameter(const char c, const std::string& name)
-{
- int temp = (int) c;
- std::ostringstream output;
- output << temp;
- parameters[name] = output.str();
-}
-
-void SaveRestoreUtility::AddChild(SaveRestoreUtility& mn, const std::string&
- name)
-{
- children[name] = mn;
-}
diff --git a/src/mlpack/core/util/save_restore_utility.hpp b/src/mlpack/core/util/save_restore_utility.hpp
deleted file mode 100644
index 9536437..0000000
--- a/src/mlpack/core/util/save_restore_utility.hpp
+++ /dev/null
@@ -1,173 +0,0 @@
-/**
- * @file save_restore_utility.hpp
- * @author Neil Slagle
- *
- * The SaveRestoreUtility provides helper functions in saving and
- * restoring models. The current output file type is XML.
- *
- * @experimental
- */
-#ifndef __MLPACK_CORE_UTIL_SAVE_RESTORE_UTILITY_HPP
-#define __MLPACK_CORE_UTIL_SAVE_RESTORE_UTILITY_HPP
-
-#include <mlpack/prereqs.hpp>
-#include <list>
-#include <map>
-#include <sstream>
-#include <string>
-
-#include <libxml/parser.h>
-#include <libxml/tree.h>
-
-#include <boost/tokenizer.hpp>
-
-namespace mlpack {
-namespace util {
-
-class SaveRestoreUtility
-{
- private:
- /**
- * parameters contains a list of names and parameters in string form.
- */
- std::map<std::string, std::string> parameters;
-
- /**
- * children contains a list of names in string format and child
- * models in the model hierarchy in SaveRestoreUtility format
- */
- std::map<std::string, SaveRestoreUtility> children;
-
- /**
- * RecurseOnNodes performs a depth first search of the XML tree.
- */
- void RecurseOnNodes(xmlNode* n);
-
- public:
-
- SaveRestoreUtility() {}
- ~SaveRestoreUtility() { parameters.clear(); }
-
- /**
- * ReadFile reads an XML tree from a file.
- */
- bool ReadFile(const std::string& filename);
-
- /**
- * WriteFile writes the XML tree to a file.
- */
- bool WriteFile(const std::string& filename);
-
- /**
- * LoadParameter loads a parameter from the parameters map. This overload is
- * not called for Armadillo objects (via the enable_if).
- */
- template<typename T>
- T& LoadParameter(T& t,
- const std::string& name,
- const typename boost::enable_if_c<
- (!arma::is_arma_type<T>::value &&
- !arma::is_arma_sparse_type<T>::value)
- >::type* junk = 0) const;
-
- /**
- * LoadParameter loads a parameter from the parameters map.
- */
- template<typename T>
- std::vector<T>& LoadParameter(std::vector<T>& v, const std::string& name)
- const;
-
- /**
- * LoadParameter loads a character from the parameters map.
- */
- char LoadParameter(char c, const std::string& name) const;
-
- /**
- * LoadParameter loads a string from the parameters map.
- */
- std::string LoadParameter(std::string& str, const std::string& name) const;
-
- /**
- * LoadParameter loads an Armadillo matrix from the parameters map.
- */
- template<typename eT>
- arma::Mat<eT>& LoadParameter(arma::Mat<eT>& matrix, const std::string& name)
- const;
-
- /**
- * LoadParameter loads an Armadillo sparse matrix from the parameters map.
- */
- template<typename eT>
- arma::SpMat<eT>& LoadParameter(arma::SpMat<eT>& matrix,
- const std::string& name) const;
-
- /**
- * SaveParameter saves a dense Armadillo object to the parameters map.
- */
- template<typename eT, typename T1>
- void SaveParameter(const arma::Base<eT, T1>& t, const std::string& name);
-
- /**
- * SaveParameter saves a sparse Armadillo object to the parameters map.
- */
- template<typename eT, typename T1>
- void SaveParameter(const arma::SpBase<eT, T1>& t, const std::string& name);
-
- /**
- * SaveParameter saves a parameter to the parameters map. This is not called
- * for Armadillo objects, via the enable_if.
- */
- template<typename T>
- void SaveParameter(const T& t,
- const std::string& name,
- const typename boost::enable_if_c<
- (!arma::is_arma_type<T>::value &&
- !arma::is_arma_sparse_type<T>::value)
- >::type* junk = 0);
-
- /**
- * SaveParameter saves a parameter to the parameters map.
- */
- template<typename T>
- void SaveParameter(const std::vector<T>& v, const std::string& name);
-
- /**
- * SaveParameter saves a character to the parameters map.
- */
- void SaveParameter(const char c, const std::string& name);
-
- /**
- * SaveSubModel saves a SaveRestoreUtility to the children map.
- */
- void AddChild(SaveRestoreUtility& mn, const std::string& name);
-
- /**
- * Return the children.
- */
- const std::map<std::string, SaveRestoreUtility> Children() const { return
- children; }
-
- /**
- * Return modifiable copy of the children.
- */
- std::map<std::string, SaveRestoreUtility> Children() { return children; }
-
- private:
- /**
- * WriteFile creates XML tree recursively.
- */
- void WriteFile(xmlNode* n);
-
- /**
- * ReadFile reads an XML tree recursively.
- */
- void ReadFile(xmlNode* n);
-};
-
-} /* namespace util */
-} /* namespace mlpack */
-
-// Include implementation.
-#include "save_restore_utility_impl.hpp"
-
-#endif
diff --git a/src/mlpack/core/util/save_restore_utility_impl.hpp b/src/mlpack/core/util/save_restore_utility_impl.hpp
deleted file mode 100644
index 44a5d95..0000000
--- a/src/mlpack/core/util/save_restore_utility_impl.hpp
+++ /dev/null
@@ -1,193 +0,0 @@
-/**
- * @file save_restore_utility_impl.hpp
- * @author Neil Slagle
- *
- * The SaveRestoreUtility provides helper functions in saving and
- * restoring models. The current output file type is XML.
- */
-#ifndef __MLPACK_CORE_UTIL_SAVE_RESTORE_UTILITY_IMPL_HPP
-#define __MLPACK_CORE_UTIL_SAVE_RESTORE_UTILITY_IMPL_HPP
-
-// In case it hasn't been included already.
-#include "save_restore_utility.hpp"
-#include "log.hpp"
-
-namespace mlpack {
-namespace util {
-
-template<typename T>
-std::vector<T>& SaveRestoreUtility::LoadParameter(std::vector<T>& v,
- const std::string& name) const
-{
- std::map<std::string, std::string>::const_iterator it = parameters.find(name);
- if (it != parameters.end())
- {
- v.clear();
- std::string value = (*it).second;
- boost::char_separator<char> sep (",");
- boost::tokenizer<boost::char_separator<char> > tok (value, sep);
- std::list<std::list<double> > rows;
- for (boost::tokenizer<boost::char_separator<char> >::iterator
- tokIt = tok.begin(); tokIt != tok.end(); ++tokIt)
- {
- T t;
- std::istringstream iss(*tokIt);
- iss >> t;
- v.push_back(t);
- }
- }
- else
- {
- Log::Fatal << "LoadParameter(): node '" << name << "' not found.\n";
- }
- return v;
-}
-
-// Load Armadillo matrices specially, in order to preserve precision. This
-// catches dense objects.
-template<typename eT>
-arma::Mat<eT>& SaveRestoreUtility::LoadParameter(
- arma::Mat<eT>& t,
- const std::string& name) const
-{
- std::map<std::string, std::string>::const_iterator it = parameters.find(name);
- if (it != parameters.end())
- {
- std::string value = (*it).second;
- std::istringstream input(value);
-
- std::string err; // Store a possible error message.
- if (!arma::diskio::load_csv_ascii(t, input, err))
- {
- Log::Fatal << "LoadParameter(): error while loading node '" << name
- << "': " << err << ".\n";
- }
- }
- else
- {
- Log::Fatal << "LoadParameter(): node '" << name << "' not found.\n";
- }
- return t;
-}
-
-// Load Armadillo matrices specially, in order to preserve precision. This
-// catches sparse objects.
-template<typename eT>
-arma::SpMat<eT>& SaveRestoreUtility::LoadParameter(
- arma::SpMat<eT>& t,
- const std::string& name) const
-{
- std::map<std::string, std::string>::const_iterator it = parameters.find(name);
- if (it != parameters.end())
- {
- std::string value = (*it).second;
- std::istringstream input(value);
-
- std::string err; // Store a possible error message.
- if (!arma::diskio::load_coord_ascii(t, input, err))
- {
- Log::Fatal << "LoadParameter(): error while loading node '" << name
- << "': " << err << ".\n";
- }
- }
- else
- {
- Log::Fatal << "LoadParameter(): node '" << name << "' not found.\n";
- }
- return t;
-}
-
-template<typename T>
-T& SaveRestoreUtility::LoadParameter(
- T& t,
- const std::string& name,
- const typename boost::enable_if_c<(!arma::is_arma_type<T>::value &&
- !arma::is_arma_sparse_type<T>::value)
- >::type* /* junk */) const
-{
- std::map<std::string, std::string>::const_iterator it = parameters.find(name);
- if (it != parameters.end())
- {
- std::string value = (*it).second;
- std::istringstream input(value);
- input >> t;
- return t;
- }
- else
- {
- Log::Fatal << "LoadParameter(): node '" << name << "' not found.\n";
- }
- return t;
-}
-
-// Print Armadillo matrices specially, in order to preserve precision. This
-// catches dense objects.
-template<typename eT, typename T1>
-void SaveRestoreUtility::SaveParameter(
- const arma::Base<eT, T1>& t,
- const std::string& name)
-{
- // Create a matrix to give to save_csv_ascii(). This may incur a copy,
- // depending on the compiler's intelligence. But the disk bandwidth is going
- // to be the main slowdown anyway...
- arma::Mat<eT> temp(t.get_ref());
-
- // Use save_csv_ascii(). This is *slightly* imprecise and it may be better to
- // store this raw. But this is readable...
- std::ostringstream output;
- arma::diskio::save_csv_ascii(temp, output);
- parameters[name] = output.str();
-}
-
-// Print sparse Armadillo matrices specially, in order to preserve precision.
-// This catches sparse objects.
-template<typename eT, typename T1>
-void SaveRestoreUtility::SaveParameter(
- const arma::SpBase<eT, T1>& t,
- const std::string& name)
-{
- // Create a matrix to give to save_coord_ascii(). This may incur a copy,
- // depending on the compiler's intelligence. But the disk bandwidth is going
- // to be the main slowdown anyway...
- arma::SpMat<eT> temp(t.get_ref());
-
- // Use save_coord_ascii(). This is *slightly* imprecise and it may be better
- // to store this raw. But this is readable...
- std::ostringstream output;
- arma::diskio::save_coord_ascii(temp, output);
- parameters[name] = output.str();
-}
-
-template<typename T>
-void SaveRestoreUtility::SaveParameter(
- const T& t,
- const std::string& name,
- const typename boost::enable_if_c<(!arma::is_arma_type<T>::value &&
- !arma::is_arma_sparse_type<T>::value)
- >::type* /* junk */)
-{
- std::ostringstream output;
- // Manually increase precision to solve #313 for now, until we have a way to
- // store this as an actual binary number.
- output << std::setprecision(15) << t;
- parameters[name] = output.str();
-}
-
-template<typename T>
-void SaveRestoreUtility::SaveParameter(const std::vector<T>& t,
- const std::string& name)
-{
- std::ostringstream output;
- for (size_t index = 0; index < t.size(); ++index)
- {
- output << t[index] << ",";
- }
- std::string vectorAsStr = output.str();
- vectorAsStr.erase(vectorAsStr.length() - 1);
- parameters[name] = vectorAsStr;
-}
-
-}; // namespace util
-}; // namespace mlpack
-
-#endif
More information about the mlpack-git
mailing list