Refactor mean calculation into static helper method

This commit is contained in:
Magne Sjaastad committed 2026-02-12 14:25:48 +01:00
1 parent 1ec53cc3f2
commit 8f365ea340
3 files changed
+71 -16

No files matched your search

@@ -164,22 +164,7 @@ void RigStatisticsMath::calculateStatisticsCurves( const std::vector<double>& va
*p90 = HUGE_VAL;
}
// Calculate mean separately
std::vector<double> validValues = values;
validValues.erase( std::remove_if( validValues.begin(),
validValues.end(),
[]( double x ) { return !RiaStatisticsTools::isValidNumber( x ); } ),
validValues.end() );
if ( !validValues.empty() )
{
double valueSum = std::accumulate( validValues.begin(), validValues.end(), 0.0 );
*mean = valueSum / validValues.size();
}
else
{
*mean = HUGE_VAL;
}
*mean = calculateMean( values );
}
//--------------------------------------------------------------------------------------------------
@@ -430,6 +415,26 @@ std::expected<std::vector<double>, std::string>
return resultValues;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
double RigStatisticsMath::calculateMean( const std::vector<double>& values )
{
std::vector<double> validValues = values;
validValues.erase( std::remove_if( validValues.begin(),
validValues.end(),
[]( double x ) { return !RiaStatisticsTools::isValidNumber( x ); } ),
validValues.end() );
if ( !validValues.empty() )
{
double valueSum = std::accumulate( validValues.begin(), validValues.end(), 0.0 );
return valueSum / validValues.size();
}
return HUGE_VAL;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
@@ -54,6 +54,8 @@ public:
static std::expected<std::vector<double>, std::string> calculateInterpolatedPercentiles( const std::vector<double>& inputValues,
const std::vector<double>& percentiles,
PercentileStyle percentileStyle );
static double calculateMean( const std::vector<double>& values );
};
//==================================================================================================
@@ -300,6 +300,54 @@ TEST( RigStatisticsMath, calculateStatisticsCurves )
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
TEST( RigStatisticsMath, calculateMean )
{
// Empty vector should return HUGE_VAL
{
std::vector<double> values;
EXPECT_DOUBLE_EQ( HUGE_VAL, RigStatisticsMath::calculateMean( values ) );
}
// Single value
{
std::vector<double> values{ 42.0 };
EXPECT_DOUBLE_EQ( 42.0, RigStatisticsMath::calculateMean( values ) );
}
// Multiple valid values
{
std::vector<double> values{ 1.0, 2.0, 3.0, 4.0, 5.0 };
EXPECT_DOUBLE_EQ( 3.0, RigStatisticsMath::calculateMean( values ) );
}
// Values with HUGE_VAL entries that should be filtered out
{
std::vector<double> values{ HUGE_VAL, 10.0, 20.0, -HUGE_VAL, 30.0, HUGE_VAL };
EXPECT_DOUBLE_EQ( 20.0, RigStatisticsMath::calculateMean( values ) );
}
// All invalid values should return HUGE_VAL
{
std::vector<double> values{ HUGE_VAL, -HUGE_VAL, HUGE_VAL };
EXPECT_DOUBLE_EQ( HUGE_VAL, RigStatisticsMath::calculateMean( values ) );
}
// Negative values
{
std::vector<double> values{ -10.0, -20.0, -30.0 };
EXPECT_DOUBLE_EQ( -20.0, RigStatisticsMath::calculateMean( values ) );
}
// Mixed positive and negative values summing to zero
{
std::vector<double> values{ -5.0, 5.0 };
EXPECT_DOUBLE_EQ( 0.0, RigStatisticsMath::calculateMean( values ) );
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------