Skip to content

Commit

Permalink
Correct template types and handle empty data into Calculator objects
Browse files Browse the repository at this point in the history
  • Loading branch information
aeslaughter committed Jun 2, 2021
1 parent 5edb540 commit 0f90558
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 66 deletions.
44 changes: 10 additions & 34 deletions modules/stochastic_tools/include/reporters/StatisticsReporter.h
Expand Up @@ -91,12 +91,16 @@ template <typename InType, typename OutType>
void
ReporterStatisticsContext<InType, OutType>::finalize()
{
this->_state.value().first = _calc_ptr->compute(_data, _data_mode == REPORTER_MODE_DISTRIBUTED);
ReporterContext<std::pair<OutType, std::vector<OutType>>>::finalize();
if (_data_mode == REPORTER_MODE_DISTRIBUTED || this->processor_id() == 0)
{
this->_state.value().first = _calc_ptr->compute(_data, _data_mode == REPORTER_MODE_DISTRIBUTED);

if (_ci_calc_ptr)
this->_state.value().second =
_ci_calc_ptr->compute(_data, _data_mode == REPORTER_MODE_DISTRIBUTED);
}

if (_ci_calc_ptr)
this->_state.value().second =
_ci_calc_ptr->compute(_data, _data_mode == REPORTER_MODE_DISTRIBUTED);
ReporterContext<std::pair<OutType, std::vector<OutType>>>::finalize();
}

template <typename InType, typename OutType>
Expand Down Expand Up @@ -147,34 +151,6 @@ class StatisticsReporter : public GeneralReporter
*
* @param r_name ReporterName of the data from which the statistics will be computed
*/
template <typename InType, typename OutType, typename StatType>
template <typename InType, typename OutType>
void declareValueHelper(const ReporterName & r_name);
};

template <typename InType, typename OutType, typename StatType>
void
StatisticsReporter::declareValueHelper(const ReporterName & r_name)
{
const auto & mode = _fe_problem.getReporterData().getReporterMode(r_name);
const auto & data = getReporterValueByName<InType>(r_name);
for (const auto & item : _compute_stats)
{
const std::string s_name =
r_name.getObjectName() + "_" + r_name.getValueName() + "_" + item.name();
if (_ci_method.isValid())
declareValueByName<std::pair<OutType, std::vector<OutType>>,
ReporterStatisticsContext<InType, OutType>>(s_name,
REPORTER_MODE_ROOT,
data,
mode,
item,
_ci_method,
_ci_levels,
_ci_replicates,
_ci_seed);
else
declareValueByName<std::pair<OutType, std::vector<OutType>>,
ReporterStatisticsContext<InType, OutType>>(
s_name, REPORTER_MODE_ROOT, data, mode, item);
}
}
Expand Up @@ -89,7 +89,7 @@ class BiasCorrectedAccelerated : public BootstrapCalculator<InType, OutType>

private:
// Compute the acceleration, see Efron and Tibshirani (2003), Ch. 14, Eq. 14.15, p 186.
OutType acceleration(const InType &, const bool) const;
Real acceleration(const InType &, const bool) const;
};

template <typename InType, typename OutType>
Expand Down
39 changes: 36 additions & 3 deletions modules/stochastic_tools/src/reporters/StatisticsReporter.C
Expand Up @@ -91,9 +91,9 @@ StatisticsReporter::StatisticsReporter(const InputParameters & parameters)
for (const auto & r_name : reporter_names)
{
if (hasReporterValueByName<std::vector<Real>>(r_name))
declareValueHelper<std::vector<Real>, Real, Real>(r_name);
declareValueHelper<std::vector<Real>, Real>(r_name);
else if (hasReporterValueByName<std::vector<int>>(r_name))
declareValueHelper<std::vector<int>, Real, Real>(r_name);
declareValueHelper<std::vector<int>, Real>(r_name);
else
unsupported_types.emplace_back(r_name.getCombinedName());
}
Expand All @@ -117,7 +117,7 @@ StatisticsReporter::StatisticsReporter(const InputParameters & parameters)
for (const auto & vec_name : vpp_vectors)
{
ReporterName r_name(vpp_name, vec_name);
declareValueHelper<std::vector<Real>, Real, Real>(r_name);
declareValueHelper<std::vector<Real>, Real>(r_name);
}
}
}
Expand All @@ -139,3 +139,36 @@ StatisticsReporter::store(nlohmann::json & json) const
{"replicates", _ci_replicates},
{"seed", _ci_seed}};
}

template <typename InType, typename OutType>
void
StatisticsReporter::declareValueHelper(const ReporterName & r_name)
{
const auto & mode = _fe_problem.getReporterData().getReporterMode(r_name);
const auto & data = getReporterValueByName<InType>(r_name);
for (const auto & item : _compute_stats)
{
const std::string s_name =
r_name.getObjectName() + "_" + r_name.getValueName() + "_" + item.name();
if (_ci_method.isValid())
declareValueByName<std::pair<OutType, std::vector<OutType>>,
ReporterStatisticsContext<InType, OutType>>(s_name,
REPORTER_MODE_ROOT,
data,
mode,
item,
_ci_method,
_ci_levels,
_ci_replicates,
_ci_seed);
else
declareValueByName<std::pair<OutType, std::vector<OutType>>,
ReporterStatisticsContext<InType, OutType>>(
s_name, REPORTER_MODE_ROOT, data, mode, item);
}
}

template void
StatisticsReporter::declareValueHelper<std::vector<Real>, Real>(const ReporterName & r_name);
template void
StatisticsReporter::declareValueHelper<std::vector<int>, Real>(const ReporterName & r_name);
6 changes: 3 additions & 3 deletions modules/stochastic_tools/src/utils/BootstrapCalculators.C
Expand Up @@ -189,7 +189,7 @@ BiasCorrectedAccelerated<InType, OutType>::compute(const InType & data,
const Real bias = NormalDistribution::quantile(count / this->_replicates, 0, 1);

// Compute Acceleration, Efron and Tibshirani (2003), Eq. 14.15, p. 186
const Real acc = acceleration(data, is_distributed);
const Real acc = data.empty() ? 0. : acceleration(data, is_distributed);

// Compute intervals, Efron and Tibshirani (2003), Eq. 14.10, p. 185
std::vector<OutType> output;
Expand All @@ -206,7 +206,7 @@ BiasCorrectedAccelerated<InType, OutType>::compute(const InType & data,
}

template <typename InType, typename OutType>
OutType
Real
BiasCorrectedAccelerated<InType, OutType>::acceleration(const InType & data,
const bool is_distributed) const
{
Expand All @@ -226,7 +226,7 @@ BiasCorrectedAccelerated<InType, OutType>::acceleration(const InType & data,
}

// Compute jackknife sum, Ch. 11, Eq. 11.4, p. 141
OutType theta_dot = std::accumulate(theta_i.begin(), theta_i.end(), 0.);
Real theta_dot = std::accumulate(theta_i.begin(), theta_i.end(), 0.);
theta_dot /= count;

// Acceleration, Ch. 14, Eq. 14.15, p. 185
Expand Down
44 changes: 19 additions & 25 deletions modules/stochastic_tools/src/utils/Calculators.C
Expand Up @@ -31,26 +31,23 @@ template <typename InType, typename OutType>
OutType
Mean<InType, OutType>::compute(const InType & data, bool is_distributed) const
{
Real local_count, local_sum;
local_count = data.size();
local_sum = std::accumulate(data.begin(), data.end(), 0.);

auto local_count = data.size();
auto local_sum = std::accumulate(data.begin(), data.end(), 0.);
if (is_distributed)
{
this->_communicator.sum(local_count);
this->_communicator.sum(local_sum);
}

return local_sum / local_count;
return data.empty() ? 0. : local_sum / local_count;
}

// MIN /////////////////////////////////////////////////////////////////////////////////////////////
template <typename InType, typename OutType>
OutType
Min<InType, OutType>::compute(const InType & data, bool is_distributed) const
{
Real local_min;
local_min = *std::min_element(data.begin(), data.end());
auto local_min = data.empty() ? std::numeric_limits<OutType>::max()
: *std::min_element(data.begin(), data.end());
if (is_distributed)
this->_communicator.min(local_min);
return local_min;
Expand All @@ -61,8 +58,8 @@ template <typename InType, typename OutType>
OutType
Max<InType, OutType>::compute(const InType & data, bool is_distributed) const
{
Real local_max;
local_max = *std::max_element(data.begin(), data.end());
auto local_max = data.empty() ? std::numeric_limits<OutType>::min()
: *std::max_element(data.begin(), data.end());
if (is_distributed)
this->_communicator.max(local_max);
return local_max;
Expand All @@ -73,8 +70,7 @@ template <typename InType, typename OutType>
OutType
Sum<InType, OutType>::compute(const InType & data, bool is_distributed) const
{
Real local_sum = 0.;
local_sum = std::accumulate(data.begin(), data.end(), 0.);
auto local_sum = std::accumulate(data.begin(), data.end(), 0.);
if (is_distributed)
this->_communicator.sum(local_sum);
return local_sum;
Expand All @@ -85,17 +81,17 @@ template <typename InType, typename OutType>
OutType
StdDev<InType, OutType>::compute(const InType & data, bool is_distributed) const
{
Real count = data.size();
Real sum = std::accumulate(data.begin(), data.end(), 0.);
auto count = data.size();
auto sum = std::accumulate(data.begin(), data.end(), 0.);

if (is_distributed)
{
this->_communicator.sum(count);
this->_communicator.sum(sum);
}

Real mean = sum / count;
Real sum_of_squares = std::accumulate(
auto mean = sum / count;
auto sum_of_squares = std::accumulate(
data.begin(), data.end(), 0., [&mean](Real running_value, Real current_value) {
return running_value + std::pow(current_value - mean, 2);
});
Expand All @@ -110,7 +106,7 @@ template <typename InType, typename OutType>
OutType
StdErr<InType, OutType>::compute(const InType & data, bool is_distributed) const
{
Real count = data.size();
auto count = data.size();
if (is_distributed)
this->_communicator.sum(count);
return StdDev<InType, OutType>::compute(data, is_distributed) / std::sqrt(count);
Expand All @@ -121,17 +117,15 @@ template <typename InType, typename OutType>
OutType
Ratio<InType, OutType>::compute(const InType & data, bool is_distributed) const
{
Real local_max = std::numeric_limits<Real>::min();
Real local_min = std::numeric_limits<Real>::max();
local_min = *std::min_element(data.begin(), data.end());
local_max = *std::max_element(data.begin(), data.end());

auto local_max = data.empty() ? std::numeric_limits<OutType>::min()
: *std::max_element(data.begin(), data.end());
auto local_min = data.empty() ? std::numeric_limits<OutType>::max()
: *std::min_element(data.begin(), data.end());
if (is_distributed)
{
this->_communicator.min(local_min);
this->_communicator.max(local_max);
}

return local_min != 0. ? local_max / local_min : 0.;
}

Expand All @@ -140,8 +134,8 @@ template <typename InType, typename OutType>
OutType
L2Norm<InType, OutType>::compute(const InType & data, bool is_distributed) const
{
Real local_sum =
std::accumulate(data.begin(), data.end(), 0., [](Real running_value, Real current_value) {
auto local_sum = std::accumulate(
data.begin(), data.end(), 0., [](OutType running_value, OutType current_value) {
return running_value + std::pow(current_value, 2);
});

Expand Down

0 comments on commit 0f90558

Please sign in to comment.