28 #ifndef TOMOGRAPHER_MHRW_VALUEHIST_TOOLS_H 29 #define TOMOGRAPHER_MHRW_VALUEHIST_TOOLS_H 41 #include <boost/math/constants/constants.hpp> 57 namespace ValueHistogramTools {
68 template<
typename RawHistogramType_,
typename ScaledHistogramType_>
71 typedef RawHistogramType_ RawHistogramType;
72 typedef ScaledHistogramType_ ScaledHistogramType;
76 histogram(raw_histogram.params)
78 typedef typename ScaledHistogramType::CountType CountRealType;
79 CountRealType ncounts = raw_histogram.totalCounts();
80 histogram.load(raw_histogram.bins.template cast<CountRealType>() / ncounts,
81 raw_histogram.off_chart / ncounts);
84 RawHistogramType raw_histogram;
86 ScaledHistogramType histogram;
97 namespace tomo_internal {
101 template<
typename CDataBaseType,
bool UseBinningAnalysis>
102 struct valuehist_types
104 typedef Histogram<
typename CDataBaseType::ValueCalculator::ValueType,
105 typename CDataBaseType::HistCountIntType> HistogramType;
110 typedef HistogramType ValueStatsCollectorResultType;
112 typedef typename CDataBaseType::CountRealType CountRealType;
117 MHRWStatsResultsBaseType;
125 template<
typename CDataBaseType>
126 struct valuehist_types<CDataBaseType, true>
130 typedef typename CDataBaseType::CountRealType CountRealType;
134 typename CDataBaseType::ValueCalculator,
135 typename CDataBaseType::HistCountIntType,
139 > BinningMHRWStatsCollectorParams;
141 typedef typename BinningMHRWStatsCollectorParams::Result ValueStatsCollectorResultType;
142 typedef typename BinningMHRWStatsCollectorParams::HistogramType HistogramType;
143 typedef typename BinningMHRWStatsCollectorParams::HistogramParams
HistogramParams;
146 typedef ValueStatsCollectorResultType MHRWStatsResultsBaseType;
228 template<
typename ValueCalculator_,
229 bool UseBinningAnalysis_ =
true,
231 typename RngSeedType_ = std::mt19937::result_type,
232 typename IterCountIntType_ = int,
233 typename CountRealType_ = double,
234 typename HistCountIntType_ = IterCountIntType_>
262 static constexpr
bool UseBinningAnalysis = UseBinningAnalysis_;
316 TOMOGRAPHER_ENABLED_IF(!UseBinningAnalysis)
318 MHRWParamsType p, RngSeedType base_seed = 0)
319 : Base(
std::move(p), base_seed), valcalc(valcalc_), histogram_params(histogram_params_),
325 TOMOGRAPHER_ENABLED_IF(UseBinningAnalysis)
327 MHRWParamsType p, RngSeedType base_seed = 0)
328 : Base(
std::move(p), base_seed), valcalc(valcalc_), histogram_params(histogram_params_),
329 binningNumLevels(binning_num_levels_)
349 template<
typename LoggerType, TOMOGRAPHER_ENABLED_IF_TMPL(!UseBinningAnalysis)>
368 template<
typename LoggerType, TOMOGRAPHER_ENABLED_IF_TMPL(UseBinningAnalysis)>
370 typename tomo_internal::valuehist_types<CDataBase, true>::BinningMHRWStatsCollectorParams,
374 typedef typename tomo_internal::valuehist_types<CDataBase, true>::BinningMHRWStatsCollectorParams
375 BinningMHRWStatsCollectorParams;
380 binningNumLevels.value,
398 template<
typename TaskResultType>
401 return AggregatedHistogramType::aggregate(
404 [](
const TaskResultType * task_result)
410 return task_result->stats_results.histogram;
417 template<
typename ValueCalculator_,
bool UseBinningAnalysis_,
418 typename MHWalkerParams_,
typename RngSeedType_,
419 typename IterCountIntType_,
typename CountRealType_,
420 typename HistCountIntType_>
421 constexpr
bool CDataBase<ValueCalculator_,UseBinningAnalysis_,MHWalkerParams_,RngSeedType_,
422 IterCountIntType_,CountRealType_,HistCountIntType_>::UseBinningAnalysis;
442 namespace tomo_internal {
444 template<
typename StatsResultsType,
typename =
void>
445 struct maybe_show_error_summary_helper {
446 static inline void print(
std::ostream & ,
const StatsResultsType & ) { }
448 template<
typename StatsResultsType>
449 struct maybe_show_error_summary_helper<
451 typename std::enable_if<
453 decltype(((StatsResultsType*)NULL)->errorBarConvergenceSummary()),
459 static inline void print(
std::ostream & stream,
const StatsResultsType & stats_results)
461 stream <<
" error bars: " << stats_results.errorBarConvergenceSummary() <<
"\n";
464 template<
typename StatsResultsType>
465 inline void maybe_show_error_summary(
std::ostream & stream,
const StatsResultsType & stats_results)
467 maybe_show_error_summary_helper<StatsResultsType>::print(stream, stats_results) ;
471 template<
typename TaskResultType>
473 const TaskResultType * task_result,
int columns)
475 const auto acceptance_ratio = task_result->acceptance_ratio;
478 task_result->stats_results.histogram,
483 stream <<
" *** Accept ratio out of recommended bounds [" 485 <<
"] ! Adapt step size ***\n";
487 maybe_show_error_summary(stream, task_result->stats_results);
511 template<
typename CDataBaseType,
typename TaskResultType,
typename AggregatedHistogramType>
514 const AggregatedHistogramType & aggregated_histogram,
515 int max_width = 0,
bool print_histogram =
true)
524 cdata.printBasicCDataMHRWInfo(stream);
528 tomo_internal::print_hist_short_bar_summary(stream, dig_w, j, task_results[j], (
int)h.
columns());
533 if (print_histogram) {
A StatsCollector which builds a histogram of values calculated with a ValueCalculator for each data s...
The parameters of a Histogram.
Base namespace for the Tomographer project.
void histogramShortBarWithInfo(std::ostream &str, std::string head, const HistogramType &hist, std::string tail, bool log_scale=true, int full_max_width=0)
Format the histogram as a one-line bar, with some surrounding info.
An MHWalkerParams type which just stores a step size.
constexpr const double MHRWAcceptanceRatioRecommendedMax
Maximal recommended acceptance ratio.
Multiprocessing tasks interface (see Multiprocessing Task Interfaces) for parallel Metropolis-Hasting...
constexpr const double MHRWAcceptanceRatioRecommendedMin
Minimal recommended acceptance ratio.
Data needed to be accessible to the working code.
Traits-like class for ValueHistogramWithBinningMHRWStatsCollector.
void histogramPrettyPrint(std::ostream &str, const HistogramType &histogram, int max_width=0)
pretty-print the given histogram.
Collect a histogram of values from a MH random walk, with binning analysis.
RngSeedType_ RngSeedType
Type used to specify the seed of the random number generator.
Some C++ utilities, with a tad of C++11 tricks.
HistogramType_ HistogramType
The histogram type corresponding to the result of a task.
Managing the need for specific overrides to operator new() for some types (especially Eigen types) ...
VarValueDecoder< T >::RetType value(const Var &var)
Access the value of the given variable, as a C++ type.
Definitions for MHRWStatsCollector Interface's.
IterCountIntType_ IterCountIntType
Type used to count the number of iterations.
#define streamstr(tokens)
Utility macro to format stream tokens to a std::string.
MHWalkerParams_ MHWalkerParams
Type used to specify the step size.
Histogram aggregator, if each individual histogram already has error bars.
Histogram aggregator, if each histogram doesn't have error bars.
Utilities for logging messages.