Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filter observed values by ParameterProperties. #4678

Merged
merged 9 commits into from
Apr 17, 2020
6 changes: 4 additions & 2 deletions src/shogun/base/SGObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -455,8 +455,10 @@ void SGObject::subscribe(const std::shared_ptr<ParameterObserver>& obs)
rxcpp::subscription subscription =
m_observable_params
->filter([obs](std::shared_ptr<ObservedValue> v) {
return obs->filter(v->get<std::string>("name"));
})
auto p = v->get_params().find(v->get<std::string>("name"));
return obs->observes(v->get<std::string>("name")) &&
obs->observes(p->second->get_properties());
})
.timestamp()
.subscribe(sub);

Expand Down
85 changes: 43 additions & 42 deletions src/shogun/lib/observers/ParameterObserver.cpp
Original file line number Diff line number Diff line change
@@ -1,37 +1,10 @@
/*
* BSD 3-Clause License
*
* Copyright (c) 2017, Shogun-Toolbox e.V. <shogun-team@shogun-toolbox.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* Written (W) 2017 Giovanni De Toni
*
*/
* This software is distributed under BSD 3-clause license (see LICENSE file).
*
* Authors: Giovanni De Toni
*
*/

#include <shogun/lib/RefCount.h>
#include <shogun/lib/observers/ObservedValueTemplated.h>
#include <shogun/lib/observers/ParameterObserver.h>
Expand All @@ -49,32 +22,60 @@ ParameterObserver::ParameterObserver()
"num_observations", &ParameterObserver::get_num_observations);
}

ParameterObserver::ParameterObserver(
std::vector<std::string>& parameters,
std::vector<ParameterProperties>& properties)
: ParameterObserver()
{
m_observed_parameters = parameters;
m_observed_properties = properties;
}

ParameterObserver::ParameterObserver(std::vector<std::string>& parameters)
: ParameterObserver()
{
m_observed_parameters = parameters;
}

ParameterObserver::ParameterObserver(
const std::string& filename, std::vector<std::string>& parameters)
: ParameterObserver(parameters)
std::vector<ParameterProperties>& properties)
: ParameterObserver()
{
m_observed_properties = properties;
}

ParameterObserver::ParameterObserver(
const std::string& filename, std::vector<std::string>& parameters,
std::vector<ParameterProperties>& properties)
: ParameterObserver(parameters, properties)
{
}

ParameterObserver::~ParameterObserver()
{
}

bool ParameterObserver::filter(const std::string& param)
bool ParameterObserver::observes(const std::string& param)
{
// If there are no specified parameters, then watch everything
if (m_observed_parameters.size() == 0)
return true;
return std::find(
m_observed_parameters.begin(), m_observed_parameters.end(),
param) != m_observed_parameters.end() ||
m_observed_parameters.empty();
}

bool ParameterObserver::observes(const AnyParameterProperties& property)
{

for (auto v : m_observed_parameters)
if (v == param)
return true;
return false;
// If there is no specified property, then watch everything
return std::any_of(
m_observed_properties.begin(), m_observed_properties.end(),
[&property](ParameterProperties& p) {
return property.has_property(p) ||
(p == ParameterProperties::ALL &&
property.compare_mask(ParameterProperties::NONE));
}) ||
m_observed_properties.empty();
}

index_t ParameterObserver::get_num_observations() const
Expand Down
63 changes: 28 additions & 35 deletions src/shogun/lib/observers/ParameterObserver.h
Original file line number Diff line number Diff line change
@@ -1,37 +1,9 @@
/*
* BSD 3-Clause License
*
* Copyright (c) 2017, Shogun-Toolbox e.V. <shogun-team@shogun-toolbox.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* Written (W) 2017 Giovanni De Toni
*
*/
* This software is distributed under BSD 3-clause license (see LICENSE file).
*
* Authors: Giovanni De Toni
*
*/
#ifndef SHOGUN_PARAMETEROBSERVER_H
#define SHOGUN_PARAMETEROBSERVER_H

Expand Down Expand Up @@ -64,13 +36,30 @@ namespace shogun
*/
ParameterObserver(std::vector<std::string>& parameters);

/**
* Constructor
* @param parameters list of parameters which we want to watch over
*/
ParameterObserver(std::vector<ParameterProperties>& properties);
geektoni marked this conversation as resolved.
Show resolved Hide resolved

/**
* Constructor
* @param parameters list of parameters which we want to watch over
*/
ParameterObserver(
std::vector<std::string>& parameters,
std::vector<ParameterProperties>& properties);

/**
* Constructor
* @param filename name of the generated output file
* @param parameters list of parameters which we want to watch over
* @param properties list of properties which we want to watch over
*/
ParameterObserver(
const std::string& filename, std::vector<std::string>& parameters);
const std::string& filename, std::vector<std::string>& parameters,
std::vector<ParameterProperties>& properties);

/**
* Virtual destructor
*/
Expand All @@ -82,7 +71,9 @@ namespace shogun
* @param param the param name
* @return true if param is found inside of m_parameters list
*/
virtual bool filter(const std::string& param);
virtual bool observes(const std::string& param);

virtual bool observes(const AnyParameterProperties& property);

/**
* Return a single observation from the received ones (not SG_REF).
Expand Down Expand Up @@ -156,6 +147,8 @@ namespace shogun
*/
std::vector<std::string> m_observed_parameters;

std::vector<ParameterProperties> m_observed_properties;

/**
* Observations recorded each time we compute on_next()
*/
Expand Down
69 changes: 32 additions & 37 deletions src/shogun/lib/observers/ParameterObserverCV.cpp
Original file line number Diff line number Diff line change
@@ -1,37 +1,9 @@
/*
* BSD 3-Clause License
*
* Copyright (c) 2017, Shogun-Toolbox e.V. <shogun-team@shogun-toolbox.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* Written (W) 2017 Giovanni De Toni
*
*/
* This software is distributed under BSD 3-clause license (see LICENSE file).
*
* Authors: Giovanni De Toni
*
*/

#include <shogun/classifier/mkl/MKL.h>
#include <shogun/classifier/mkl/MKLMulticlass.h>
Expand All @@ -43,8 +15,32 @@

using namespace shogun;

ParameterObserverCV::ParameterObserverCV(bool verbose)
: ParameterObserver(), m_verbose(verbose)
ParameterObserverCV::ParameterObserverCV() : ParameterObserver()
{
}

ParameterObserverCV::ParameterObserverCV(
std::vector<std::string>& parameters,
std::vector<ParameterProperties>& properties)
: ParameterObserver(parameters, properties)
{
}

ParameterObserverCV::ParameterObserverCV(
const std::string& filename, std::vector<std::string>& parameters,
std::vector<ParameterProperties>& properties)
: ParameterObserver(filename, parameters, properties)
{
}

ParameterObserverCV::ParameterObserverCV(std::vector<std::string>& parameters)
: ParameterObserver(parameters)
{
}

ParameterObserverCV::ParameterObserverCV(
std::vector<ParameterProperties>& properties)
: ParameterObserver(properties)
{
}

Expand All @@ -60,8 +56,7 @@ void ParameterObserverCV::on_next_impl(const shogun::TimedObservedValue& value)
value.first->get(value.first->get<std::string>("name"))->as<CrossValidationStorage>();

/* Print information on screen if enabled*/
if (m_verbose)
print_observed_value(recalled_value);
print_observed_value(recalled_value);
}
catch (ShogunException& e)
{
Expand Down
58 changes: 19 additions & 39 deletions src/shogun/lib/observers/ParameterObserverCV.h
Original file line number Diff line number Diff line change
@@ -1,37 +1,9 @@
/*
* BSD 3-Clause License
*
* Copyright (c) 2017, Shogun-Toolbox e.V. <shogun-team@shogun-toolbox.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* Written (W) 2017 Giovanni De Toni
*
*/
* This software is distributed under BSD 3-clause license (see LICENSE file).
*
* Authors: Giovanni De Toni
*
*/

#ifndef SHOGUN_PARAMETEROBSERVERCV_H
#define SHOGUN_PARAMETEROBSERVERCV_H
Expand All @@ -50,7 +22,20 @@ namespace shogun
{

public:
ParameterObserverCV(bool verbose = false);
ParameterObserverCV();

ParameterObserverCV(std::vector<std::string>& parameters);

ParameterObserverCV(std::vector<ParameterProperties>& properties);

ParameterObserverCV(
std::vector<std::string>& parameters,
std::vector<ParameterProperties>& properties);

ParameterObserverCV(
const std::string& filename, std::vector<std::string>& parameters,
std::vector<ParameterProperties>& properties);

virtual ~ParameterObserverCV();
virtual void on_error(std::exception_ptr ptr);
virtual void on_complete();
Expand Down Expand Up @@ -79,11 +64,6 @@ namespace shogun

protected:
virtual void on_next_impl(const TimedObservedValue& value);

/**
* enable printing of information
*/
bool m_verbose;
};
}

Expand Down