From d08660b4fb6bd60e8c3cbb579b5176b12010e7a5 Mon Sep 17 00:00:00 2001 From: Giovanni De Toni Date: Wed, 12 Jul 2017 10:14:49 +0200 Subject: [PATCH] [ShogunBoard] Add methods to print a list of observed parameters. This enables the user to get a list of parameters which he can observe using a ParameterObserver object. The implementation uses the pimpl idiom to move implementation details out of the header. Other: * Remove HAVE_CXX guard since Shogun's require C++11 now; --- src/shogun/base/SGObject.cpp | 54 +++++++++++++++++++++++++++++------- src/shogun/base/SGObject.h | 13 +++++++++ 2 files changed, 57 insertions(+), 10 deletions(-) diff --git a/src/shogun/base/SGObject.cpp b/src/shogun/base/SGObject.cpp index 2d226a89eac..10b576786b2 100644 --- a/src/shogun/base/SGObject.cpp +++ b/src/shogun/base/SGObject.cpp @@ -31,19 +31,13 @@ #include #include -#ifdef HAVE_CXX11 -#include -#else #include -#endif namespace shogun { -#ifdef HAVE_CXX11 - typedef std::unordered_map ParametersMap; -#else typedef std::map ParametersMap; -#endif + typedef std::map> + ObsParamsList; class CSGObject::Self { @@ -153,7 +147,7 @@ namespace shogun using namespace shogun; -CSGObject::CSGObject() : self() +CSGObject::CSGObject() : self(), param_obs_list() { init(); set_global_objects(); @@ -163,7 +157,8 @@ CSGObject::CSGObject() : self() } CSGObject::CSGObject(const CSGObject& orig) - : self(), io(orig.io), parallel(orig.parallel), version(orig.version) + : self(), param_obs_list(), io(orig.io), parallel(orig.parallel), + version(orig.version) { init(); set_global_objects(); @@ -837,3 +832,42 @@ void CSGObject::observe_scalar( auto tmp = std::make_pair(step, std::make_pair(name, value)); m_subscriber_params->on_next(tmp); } + +class CSGObject::ParameterObserverList +{ +public: + void register_param( + const std::string& name, const std::string& type, + const std::string& description) + { + m_list_obs_params[name] = std::make_pair(type, description); + } + + ObsParamsList get_list() const + { + return m_list_obs_params; + } + +private: + /** List of observable parameters (name, description) */ + ObsParamsList m_list_obs_params; +}; + +void CSGObject::register_observable_param( + const std::string& name, const std::string& type, + const std::string& description) +{ + param_obs_list->register_param(name, type, description); +} + +void CSGObject::list_observable_parameters() +{ + SG_INFO("List of observable parameters of object %s\n", get_name()); + SG_PRINT("------"); + for (auto const& x : param_obs_list->get_list()) + { + SG_PRINT( + "%s [%s]: %s\n", x.first.c_str(), x.second.first.c_str(), + x.second.second.c_str()); + } +} diff --git a/src/shogun/base/SGObject.h b/src/shogun/base/SGObject.h index 57c6ebdeaae..0b7001c1ea9 100644 --- a/src/shogun/base/SGObject.h +++ b/src/shogun/base/SGObject.h @@ -424,6 +424,9 @@ class CSGObject /** Subscribe a parameter observer to watch over params */ void subscribe_to_parameters(ParameterObserverInterface* obs); + /** Print to stdout a list of observable parameters */ + void list_observable_parameters(); + protected: /** Can (optionally) be overridden to pre-initialize some member * variables which are not PARAMETER::ADD'ed. Make sure that at @@ -575,6 +578,9 @@ class CSGObject class Self; Unique self; + class ParameterObserverList; + Unique param_obs_list; + protected: /** Observe the parameter and emits a value using the * observable object @@ -582,6 +588,13 @@ class CSGObject void observe_scalar( const int64_t step, const std::string& name, const Any& value); + /** + * Register a parameter as observable + */ + void register_observable_param( + const std::string& name, const std::string& type, + const std::string& description); + public: /** io */ SGIO* io;