Skip to content

Commit

Permalink
Merge pull request #1390 from janosimas/b4.0.0-release-candidate-4
Browse files Browse the repository at this point in the history
fix analysis operations
  • Loading branch information
janosimas authored Nov 28, 2017
2 parents 713efdc + 76fe06d commit 8fc6093
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 17 deletions.
18 changes: 4 additions & 14 deletions src/terrama2/core/utility/TimeUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,21 +147,14 @@ double terrama2::core::TimeUtils::convertTimeString(const std::string& time, std
throw terrama2::InitializationException() << terrama2::ErrorDescription(msg);
}

bool isNegative = false;
if(timeStr.front() == '-')
{
isNegative = true;
timeStr.erase(0, 1);
}

// split the input time string in numeric value and unit
std::string numberStr;
std::string unitStr;
std::partition_copy(std::begin(timeStr),
std::end(timeStr),
std::back_inserter(numberStr),
std::back_inserter(unitStr),
[](const char& ch) {return std::isdigit(ch);} );
std::back_inserter(numberStr),
[](const char& ch) {return std::isalpha(ch);} );

// if no unit was given, use defaultUnit
if(unitStr.empty()) unitStr = defaultUnit;
Expand Down Expand Up @@ -203,18 +196,15 @@ double terrama2::core::TimeUtils::convertTimeString(const std::string& time, std
}
else
{
QString msg(QObject::tr("Could not find any known unit of measure in the given string: %1.").arg(QString::fromStdString(time)));
QString msg(QObject::tr("Could not find any known unit of measure in the given string: %1.").arg(QString::fromStdString(timeStr)));
TERRAMA2_LOG_ERROR() << msg;
throw terrama2::InvalidArgumentException() << terrama2::ErrorDescription(msg);
}

if(unitName != "SECOND")
result = unitsManager.getConversion("SECOND", unitName) * result;

if(isNegative)
return -result;
else
return result;
return result;
}


Expand Down
35 changes: 32 additions & 3 deletions src/terrama2/services/analysis/core/utility/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,29 @@ double terrama2::services::analysis::core::getValue(terrama2::core::Synchronized
return value;
}

/*!
Calculates the variance of the elements of a container
source: http://roth.cs.kuleuven.be/w-ess/index.php/Accurate_variance_and_mean_calculations_in_C%2B%2B11
*/
template <typename T, typename Container>
T internal_variance(const Container& xs)
{
// begin(xs) will point to the first element in xs
// end(xs) will point to the last element in xs
// the distance between them gives the number of elements
size_t N = end(xs) - begin(xs);
// first pass through all data (hidden in accumulate):
T m = std::accumulate(begin(xs), end(xs), T(0)) / N;
T s2 = 0;
// second pass through all data:
for(auto x : xs) {
s2 += (x - m) * (x - m);
}
return s2 / (N-1);
}

void terrama2::services::analysis::core::calculateStatistics(std::vector<double>& values, OperatorCache& cache)
{
if(values.empty())
Expand All @@ -279,11 +302,17 @@ void terrama2::services::analysis::core::calculateStatistics(std::vector<double>
cache.min = min(acc);
cache.max = max(acc);
cache.median = median(acc);
cache.variance = variance(acc);
cache.standardDeviation = std::sqrt(variance(acc));
//============================================
// WARNING
//
// The boost function returned a different then expected value
// check internal_variance
//
// cache.variance = variance(acc);
cache.variance = internal_variance<double>(values);
cache.standardDeviation = std::sqrt(cache.variance);
}


double terrama2::services::analysis::core::getOperationResult(OperatorCache& cache, StatisticOperation statisticOperation)
{
switch(statisticOperation)
Expand Down

0 comments on commit 8fc6093

Please sign in to comment.