Skip to content

Commit

Permalink
updates after review
Browse files Browse the repository at this point in the history
  • Loading branch information
Nir-Az committed Dec 8, 2020
1 parent f98b2ac commit b993d6a
Show file tree
Hide file tree
Showing 10 changed files with 214 additions and 31 deletions.
2 changes: 1 addition & 1 deletion common/model-views.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3173,7 +3173,7 @@ namespace rs2
if (_reflectivity)
{
_reflectivity->reset_history();
_stabilized_reflectivity.reset();
_stabilized_reflectivity.clear();
}
}
}
Expand Down
58 changes: 36 additions & 22 deletions common/utilities/number/stabilized-value.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,23 @@

#include <string>
#include <atomic>
#include <mutex>

namespace utilities {
namespace number {

// The stabilized value implement a history based stable value.
// It supply a way to define and get values with a required stabilization.
// When the user adds a value, the value is inserted into the history
// When the user call "get" the last stable value is returned
// Inputs: Template <value_type>
// required history size
// required stabilization percentage
template<typename T> class stabilized_value
// The stabilized value implement a history based stable value.
// It supply a way to define and get values with a required stabilization.
// When the user adds a value, the value is inserted into the history
// When the user call "get" the last stable value is returned
// Inputs: Template <value_type>
// required history size
// required stabilization percentage as a fraction , range [0-1]
// The stabilized_value class is thread safe.
template < typename T > class stabilized_value
{
public:
stabilized_value( size_t history_size, float stabilize_percentage )
stabilized_value( size_t history_size, float stabilize_percentage = 0.75f )
: _history_size( history_size )
, _last_stable_value( 0 )
, _stabilize_percentage( stabilize_percentage )
Expand All @@ -29,27 +31,34 @@ namespace number {
+ std::to_string( stabilize_percentage ) );
}

stabilized_value() = delete;
stabilized_value(const stabilized_value &) = delete;


void add( T val )
{
_recalc_stable_val = true;

const std::lock_guard< std::mutex > lock( _mutex );
if( _values.empty() )
{
_values.push_back( val );
_last_stable_value = val;
return;
}

_recalc_stable_val = true;

_values.push_back( val );
if( _values.size() > _history_size )
_values.pop_front();
}

T get()
T get() const
{
std::lock_guard< std::mutex > lock( _mutex );

if( _values.empty() )
{
throw std::runtime_error( "No stable value exist, history is empty" );
throw std::runtime_error( "history is empty; no stable value" );
}

if( _recalc_stable_val )
Expand All @@ -58,10 +67,7 @@ namespace number {
std::pair< T, int > most_stable_value = { 0, 0 };
for( T val : _values )
{
if( values_count_map.find( val ) != values_count_map.end() )
values_count_map[val]++;
else
values_count_map[val] = 1;
++values_count_map[val];

if( most_stable_value.second < values_count_map[val] )
{
Expand All @@ -83,21 +89,29 @@ namespace number {
return _last_stable_value;
}

void reset()
void clear()
{
const std::lock_guard< std::mutex > lock( _mutex );

_values.clear();
_last_stable_value = 0.0f;
_recalc_stable_val = false;
}

bool empty()
{
const std::lock_guard< std::mutex > lock( _mutex );
return _values.empty();
}

private:
std::deque< T > _values;
const size_t _history_size;
T _last_stable_value;
mutable T _last_stable_value;
const float _stabilize_percentage;
std::atomic_bool _recalc_stable_val = { true };
mutable std::atomic_bool _recalc_stable_val = { true };
mutable std::mutex _mutex;
};


} // namespace value_stabilizer
} // namespace number
} // namespace utilities
2 changes: 1 addition & 1 deletion unit-tests/utilities/number/stabilized/test-60-percent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@


#include "../../../test.h"
#include "../common/utilities/number/stabilized-value.h"
#include <../common/utilities/number/stabilized-value.h>

using namespace utilities::number;
// Test group description:
Expand Down
70 changes: 70 additions & 0 deletions unit-tests/utilities/number/stabilized/test-empty.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// License: Apache 2.0. See LICENSE file in root directory.
// Copyright(c) 2020 Intel Corporation. All Rights Reserved.

//#cmake:add-file ../../../../common/utilities/number/stabilized-value.h


#include "../../../test.h"
#include <../common/utilities/number/stabilized-value.h>

using namespace utilities::number;

// Test group description:
// * This tests group verifies stabilized_value class.
//
// Current test description:
// * Verify that empty function works as expected
TEST_CASE( "empty test", "[stabilized value]" )
{
try
{
stabilized_value< float > stab_value(5, 1.0f);
CHECK(stab_value.empty());
CHECK_NOTHROW(stab_value.add(1.0f));
CHECK_FALSE(stab_value.empty());
stab_value.clear();
CHECK(stab_value.empty());
}
catch(const std::exception &e)
{
FAIL(" Exception caught: " << e.what());
}
}

TEST_CASE("stable value sanity - 40%", "[stabilized value]")
{
try
{
stabilized_value< float > stab_value(5, 0.4f);
CHECK_NOTHROW(stab_value.add(1.0f));
CHECK_NOTHROW(stab_value.add(1.0f));
CHECK_NOTHROW(stab_value.add(1.0f));
CHECK_NOTHROW(stab_value.add(1.0f));
CHECK_NOTHROW(stab_value.add(1.0f));

CHECK(1.0f == stab_value.get());
}
catch (const std::exception &e)
{
FAIL("Exception caught: " << e.what());
}
}

TEST_CASE("stable value sanity - 25%", "[stabilized value]")
{
try
{
stabilized_value< float > stab_value(5, 0.25f);
CHECK_NOTHROW(stab_value.add(1.0f));
CHECK_NOTHROW(stab_value.add(1.0f));
CHECK_NOTHROW(stab_value.add(1.0f));
CHECK_NOTHROW(stab_value.add(1.0f));
CHECK_NOTHROW(stab_value.add(1.0f));

CHECK(1.0f == stab_value.get());
}
catch (const std::exception &e)
{
FAIL("Exception caught: " << e.what());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@


#include "../../../test.h"
#include "../common/utilities/number/stabilized-value.h"
#include <../common/utilities/number/stabilized-value.h>

using namespace utilities::number;

Expand Down
57 changes: 57 additions & 0 deletions unit-tests/utilities/number/stabilized/test-multi-threading.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// License: Apache 2.0. See LICENSE file in root directory.
// Copyright(c) 2020 Intel Corporation. All Rights Reserved.

//#cmake:add-file ../../../../common/utilities/number/stabilized-value.h

#include <thread>
#include <chrono>
#include "../../../test.h"
#include <../common/utilities/number/stabilized-value.h>

using namespace utilities::number;
// Test group description:
// * This tests group verifies stabilized_value class.
//
// Current test description:
// * Verify that the stabilized_value class is thread safe

TEST_CASE( "multi-threading", "[stabilized value]" )
{
try
{
std::atomic<float> inserted_val_1 = { 20.0f };
std::atomic<float> inserted_val_2 = { 55.0f };
std::vector<float> values_vec;
stabilized_value< float > stab_value( 10, 0.6f );

// Fill history with values (> 60% is 'inserted_val_1')
std::thread first( [&]() {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
for (int i = 0; i < 1000; i++)
{
if (i%3)
stab_value.add(inserted_val_1);
else
stab_value.add(inserted_val_2);
}
} );

// Sample stabilized value
std::thread second( [&]() {
while (stab_value.empty());
for (int i = 0; i < 1000; i++)
values_vec.push_back(stab_value.get());
} );

if (first.joinable()) first.join();
if (second.joinable()) second.join();

// Verify that all samples show the > 60% number inserted 'inserted_val_1'
for (auto val : values_vec)
CHECK(val == inserted_val_1);
}
catch( const std::exception & e )
{
FAIL( "Exception caught: " << e.what() );
}
}
42 changes: 42 additions & 0 deletions unit-tests/utilities/number/stabilized/test-not-full.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// License: Apache 2.0. See LICENSE file in root directory.
// Copyright(c) 2020 Intel Corporation. All Rights Reserved.

//#cmake:add-file ../../../../common/utilities/number/stabilized-value.h


#include "../../../test.h"
#include <../common/utilities/number/stabilized-value.h>

using namespace utilities::number;

// Test group description:
// * This tests group verifies stabilized_value class.
//
// Current test description:
// * Verify if history is not the logic works as expected and the percentage is calculated from the history current size
TEST_CASE( "not full history", "[stabilized value]" )
{
try
{
stabilized_value< float > stab_value(30, 0.6f);
CHECK_NOTHROW(stab_value.add(76.0f));
CHECK_NOTHROW(stab_value.add(76.0f));
CHECK_NOTHROW(stab_value.add(76.0f));
CHECK_NOTHROW(stab_value.add(76.0f));
CHECK(76.0f == stab_value.get());

CHECK_NOTHROW(stab_value.add(45.0f));
CHECK_NOTHROW(stab_value.add(45.0f));
CHECK_NOTHROW(stab_value.add(45.0f));
CHECK_NOTHROW(stab_value.add(45.0f));
CHECK(76.0f == stab_value.get());

CHECK_NOTHROW(stab_value.add(45.0f));
CHECK_NOTHROW(stab_value.add(45.0f)); // The stable value should change now (4 * 76.0 + 6 * 45.0 (total 10 values))
CHECK(45.0f == stab_value.get());
}
catch(const std::exception &e)
{
FAIL("Exception caught: " << e.what());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@


#include "../../../test.h"
#include "../common/utilities/number/stabilized-value.h"
#include <../common/utilities/number/stabilized-value.h>

using namespace utilities::number;

Expand All @@ -30,7 +30,7 @@ TEST_CASE( "stable value sanity - 100%", "[stabilized value]" )
}
catch(const std::exception &e)
{
FAIL(" Exception caught: " << e.what());
FAIL("Exception caught: " << e.what());
}
}

Expand All @@ -49,7 +49,7 @@ TEST_CASE("stable value sanity - 40%", "[stabilized value]")
}
catch (const std::exception &e)
{
FAIL(" Exception caught: " << e.what());
FAIL("Exception caught: " << e.what());
}
}

Expand All @@ -68,6 +68,6 @@ TEST_CASE("stable value sanity - 25%", "[stabilized value]")
}
catch (const std::exception &e)
{
FAIL(" Exception caught: " << e.what());
FAIL("Exception caught: " << e.what());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@


#include "../../../test.h"
#include "../common/utilities/number/stabilized-value.h"
#include <../common/utilities/number/stabilized-value.h>

using namespace utilities::number;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@


#include "../../../test.h"
#include "../common/utilities/number/stabilized-value.h"
#include <../common/utilities/number/stabilized-value.h>

using namespace utilities::number;

Expand Down

0 comments on commit b993d6a

Please sign in to comment.