28 #ifndef TOMOGRAPHER_MHRW_VALUEHIST_TOOLS_H 29 #define TOMOGRAPHER_MHRW_VALUEHIST_TOOLS_H 35 #include <boost/math/constants/constants.hpp> 36 #include <boost/serialization/serialization.hpp> 37 #include <boost/serialization/base_object.hpp> 59 namespace ValueHistogramTools {
70 template<
typename RawHistogramType_,
typename ScaledHistogramType_>
73 typedef RawHistogramType_ RawHistogramType;
74 typedef ScaledHistogramType_ ScaledHistogramType;
78 histogram(raw_histogram.params)
80 typedef typename ScaledHistogramType::CountType CountRealType;
81 CountRealType ncounts = raw_histogram.totalCounts();
82 histogram.load(raw_histogram.bins.template cast<CountRealType>() / ncounts,
83 raw_histogram.off_chart / ncounts);
86 RawHistogramType raw_histogram;
88 ScaledHistogramType histogram;
99 namespace tomo_internal {
103 template<
typename CDataBaseType,
bool UseBinningAnalysis>
104 struct valuehist_types
106 typedef Histogram<
typename CDataBaseType::ValueCalculator::ValueType,
107 typename CDataBaseType::HistCountIntType> HistogramType;
112 typedef HistogramType ValueStatsCollectorResultType;
114 typedef typename CDataBaseType::CountRealType CountRealType;
119 MHRWStatsResultsBaseType;
127 template<
typename CDataBaseType>
128 struct valuehist_types<CDataBaseType, true>
132 typedef typename CDataBaseType::CountRealType CountRealType;
136 typename CDataBaseType::ValueCalculator,
137 typename CDataBaseType::HistCountIntType,
141 > BinningMHRWStatsCollectorParams;
143 typedef typename BinningMHRWStatsCollectorParams::Result ValueStatsCollectorResultType;
144 typedef typename BinningMHRWStatsCollectorParams::HistogramType HistogramType;
145 typedef typename BinningMHRWStatsCollectorParams::HistogramParams
HistogramParams;
148 typedef ValueStatsCollectorResultType MHRWStatsResultsBaseType;
233 template<
typename ValueCalculator_,
234 bool UseBinningAnalysis_ =
true,
236 typename RngSeedType_ = std::mt19937::result_type,
237 typename IterCountIntType_ = int,
238 typename CountRealType_ = double,
239 typename HistCountIntType_ = IterCountIntType_>
267 static constexpr
bool UseBinningAnalysis = UseBinningAnalysis_;
321 TOMOGRAPHER_ENABLED_IF(!UseBinningAnalysis)
323 MHRWParamsType p, RngSeedType base_seed = 0)
324 : Base(
std::move(p), base_seed), valcalc(valcalc_), histogram_params(histogram_params_),
329 TOMOGRAPHER_ENABLED_IF(!UseBinningAnalysis)
331 MHRWParamsType p,
std::vector<RngSeedType> seeds)
332 : Base(
std::move(p),
std::move(seeds)), valcalc(valcalc_), histogram_params(histogram_params_),
338 TOMOGRAPHER_ENABLED_IF(UseBinningAnalysis)
341 :
Base(
std::move(p), base_seed), valcalc(valcalc_), histogram_params(histogram_params_),
342 binningNumLevels(binning_num_levels_)
346 TOMOGRAPHER_ENABLED_IF(UseBinningAnalysis)
349 :
Base(
std::move(p),
std::move(seeds)), valcalc(valcalc_), histogram_params(histogram_params_),
350 binningNumLevels(binning_num_levels_)
391 template<
typename LoggerType, TOMOGRAPHER_ENABLED_IF_TMPL(!UseBinningAnalysis)>
410 template<
typename LoggerType, TOMOGRAPHER_ENABLED_IF_TMPL(UseBinningAnalysis)>
412 typename tomo_internal::valuehist_types<CDataBase, true>::BinningMHRWStatsCollectorParams,
416 typedef typename tomo_internal::valuehist_types<CDataBase, true>::BinningMHRWStatsCollectorParams
417 BinningMHRWStatsCollectorParams;
422 binningNumLevels.value,
440 template<
typename TaskResultType>
443 return AggregatedHistogramType::aggregate(
446 [](
const TaskResultType * task_result)
452 return task_result->stats_results.histogram;
458 friend boost::serialization::access;
459 template<
typename Archive,
461 void serialize(Archive & a,
const unsigned int version)
463 a & boost::serialization::base_object<Base>(*this);
464 ValueCalculator2 & valcalc_ref = valcalc;
466 a & histogram_params;
467 maybe_serialize_binning(a, version);
469 template<
typename Archive, TOMOGRAPHER_ENABLED_IF_TMPL(UseBinningAnalysis)>
470 void maybe_serialize_binning(Archive & a,
const unsigned int )
472 a & binningNumLevels.value;
474 template<
typename Archive, TOMOGRAPHER_ENABLED_IF_TMPL(!UseBinningAnalysis)>
475 void maybe_serialize_binning(Archive &,
const unsigned int) { }
479 template<
typename ValueCalculator_,
bool UseBinningAnalysis_,
480 typename MHWalkerParams_,
typename RngSeedType_,
481 typename IterCountIntType_,
typename CountRealType_,
482 typename HistCountIntType_>
483 constexpr
bool CDataBase<ValueCalculator_,UseBinningAnalysis_,MHWalkerParams_,RngSeedType_,
484 IterCountIntType_,CountRealType_,HistCountIntType_>::UseBinningAnalysis;
504 namespace tomo_internal {
506 template<
typename StatsResultsType,
typename =
void>
507 struct maybe_show_error_summary_helper {
508 static inline void print(
std::ostream & ,
const StatsResultsType & ) { }
510 template<
typename StatsResultsType>
511 struct maybe_show_error_summary_helper<
513 typename std::enable_if<
515 decltype(((StatsResultsType*)NULL)->errorBarConvergenceSummary()),
521 static inline void print(
std::ostream & stream,
const StatsResultsType & stats_results)
523 stream <<
" error bars: " << stats_results.errorBarConvergenceSummary() <<
"\n";
526 template<
typename StatsResultsType>
527 inline void maybe_show_error_summary(
std::ostream & stream,
const StatsResultsType & stats_results)
529 maybe_show_error_summary_helper<StatsResultsType>::print(stream, stats_results) ;
533 template<
typename TaskResultType>
535 const TaskResultType * task_result,
int columns)
537 const auto acceptance_ratio = task_result->acceptance_ratio;
540 task_result->stats_results.histogram,
545 stream <<
" *** Accept ratio out of recommended bounds [" 547 <<
"] ! Adapt step size ***\n";
549 maybe_show_error_summary(stream, task_result->stats_results);
573 template<
typename CDataBaseType,
typename TaskResultType,
typename AggregatedHistogramType>
576 const AggregatedHistogramType & aggregated_histogram,
577 int max_width = 0,
bool print_histogram =
true)
586 cdata.printBasicCDataMHRWInfo(stream);
590 tomo_internal::print_hist_short_bar_summary(stream, dig_w, j, task_results[j], (
int)h.
columns());
595 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.