28 #ifndef TOMOGRAPHER_MHRW_VALUEHIST_TASKS_H 29 #define TOMOGRAPHER_MHRW_VALUEHIST_TASKS_H 40 #include <boost/math/constants/constants.hpp> 53 namespace ValueHistogramTasks {
56 namespace tomo_internal {
57 template<
typename CDataBaseType,
bool UseBinningAnalysis>
58 struct histogram_types {
60 typename CDataBaseType::HistCountIntType> HistogramType;
62 typedef HistogramType MHRWStatsCollectorResultType;
65 template<
typename CDataBaseType>
66 struct histogram_types<CDataBaseType, true> {
69 typename CDataBaseType::ValueCalculator,
typename CDataBaseType::HistCountIntType,
70 typename CDataBaseType::CountRealType, Eigen::Dynamic, Eigen::Dynamic
71 > BinningMHRWStatsCollectorParams;
73 typedef typename BinningMHRWStatsCollectorParams::Result MHRWStatsCollectorResultType;
74 typedef typename BinningMHRWStatsCollectorParams::HistogramType HistogramType;
75 typedef typename BinningMHRWStatsCollectorParams::HistogramParams HistogramParams;
87 namespace tomo_internal {
88 template<
typename HistogramType>
89 void print_hist_short_bar_with_accept_info(
std::ostream & str,
int dig_w,
int j,
const HistogramType & hist,
90 double acceptance_ratio,
int columns)
133 template<
typename CDataBaseType_,
typename LoggerType_>
136 AveragedHistogram<UniformBinsHistogram<typename CDataBaseType_::HistogramType::Scalar,
137 typename CDataBaseType_::CountRealType>,
138 typename CDataBaseType_::CountRealType>
196 : Base(), histogram()
200 template<
typename BaseType,
typename ScaledHistogramTypeRef>
201 RunTaskResult(BaseType&& b, ScaledHistogramTypeRef&& histogram_)
202 : Base(std::forward<BaseType>(b)), histogram(histogram_)
225 : _finalized(false), _finalhistogram(HistogramParams()),
226 _collected_runtaskresults(),
227 _llogger(
"MHRWTasks::ValueHistogramTasks::ResultsCollectorSimple", logger_)
233 for (
std::size_t j = 0; j < _collected_runtaskresults.size(); ++j) {
234 if (_collected_runtaskresults[j] != NULL) {
235 delete _collected_runtaskresults[j];
259 tomographer_assert(isFinalized() &&
"You may only call finalHistogram() after the runs have been finalized.");
260 return _finalhistogram;
269 tomographer_assert(isFinalized() &&
"You may only call numTasks() after the runs have been finalized.");
270 return _collected_runtaskresults.size();
282 tomographer_assert(isFinalized() &&
"You may only call collectedRunTaskResults() after the runs have been finalized.");
283 return _collected_runtaskresults;
298 tomographer_assert(isFinalized() &&
"You may only call collectedRunTaskResult(std::size_t) after the runs have been finalized.");
299 tomographer_assert(task_no < _collected_runtaskresults.size());
300 return _collected_runtaskresults[task_no];
315 template<
typename RealType =
double>
319 stream <<
"Value" << sep <<
"Counts" << sep <<
"Error" << linesep
321 for (
int kk = 0; kk < _finalhistogram.bins.size(); ++kk) {
322 stream << RealType(_finalhistogram.params.binLowerValue(kk)) << sep
323 << RealType(_finalhistogram.bins(kk)) << sep
324 << RealType(_finalhistogram.delta(kk)) << linesep;
343 int max_width = 0,
bool print_histogram =
true)
347 const auto& res = *
this;
349 const RunTaskResultList & collresults = res.collectedRunTaskResults();
350 const FinalHistogramType finalhistogram = res.finalHistogram();
355 cdata.printBasicCDataMHRWInfo(str);
358 tomo_internal::print_hist_short_bar_with_accept_info(str, dig_w, j, collresults[j]->histogram,
359 collresults[j]->acceptance_ratio, h.
columns());
364 if (print_histogram) {
378 FinalHistogramType _finalhistogram;
380 RunTaskResultList _collected_runtaskresults;
389 template<
typename Cnt,
typename CData>
390 inline void init(Cnt num_total_runs, Cnt ,
const CData * pcdata)
392 tomographer_assert(!isFinalized() &&
"init() called after results have been finalized!");
394 _collected_runtaskresults.
resize(num_total_runs, NULL);
395 _finalhistogram.
reset(pcdata->histogram_params);
398 template<
typename Cnt,
typename TaskResultType,
typename CData>
399 inline void collectResult(Cnt task_no, TaskResultType&& taskresult,
const CData * )
401 tomographer_assert(!isFinalized() &&
"collectResult() called after results have been finalized!");
405 str <<
"Got task result. Histogram is:\n" << taskresult.stats_collector_result.prettyPrint();
415 ScaledHistogramType thishistogram = taskresult.stats_collector_result;
418 thishistogram.
bins /= numsamples;
422 _collected_runtaskresults[task_no]
431 template<
typename Cnt,
typename CData>
434 tomographer_assert(!isFinalized() &&
"runsFinished() called after results have been finalized!");
477 template<
typename CDataBaseType_,
typename LoggerType_>
480 UniformBinsHistogram<typename CDataBaseType_::HistogramType::Scalar,
481 typename CDataBaseType_::CountRealType>
502 typedef typename tomo_internal::histogram_types<CDataBaseType_,true>::BinningMHRWStatsCollectorParams
562 : _finalized(false), _finalhistogram(), _simplefinalhistogram(),
563 _collected_runtaskresults(),
564 _llogger(
"MHRWTasks::ValueHistogramTasks::ResultsCollectorWithBinningAnalysis", logger_)
570 for (
std::size_t j = 0; j < _collected_runtaskresults.size(); ++j) {
571 if (_collected_runtaskresults[j] != NULL) {
572 delete _collected_runtaskresults[j];
596 tomographer_assert(isFinalized() &&
"You may only call finalHistogram() after the runs have been finalized.");
597 return _finalhistogram;
613 tomographer_assert(isFinalized() &&
"You may only call simpleFinalHistogram() after the runs have been finalized.");
614 return _simplefinalhistogram;
623 tomographer_assert(isFinalized() &&
"You may only call numTasks() after the runs have been finalized.");
624 return _collected_runtaskresults.size();
636 tomographer_assert(isFinalized() &&
"You may only call collectedRunTaskResults() after the runs have been finalized.");
637 return _collected_runtaskresults;
652 tomographer_assert(isFinalized() &&
"You may only call collectedRunTaskResult(std::size_t) after the runs have been finalized.");
653 tomographer_assert(task_no < _collected_runtaskresults.size());
654 return _collected_runtaskresults[task_no];
671 template<
typename RealType =
double>
676 stream <<
"Value" << sep <<
"Counts" << sep <<
"Error" << sep <<
"SimpleError" << linesep
678 for (
int kk = 0; kk < _finalhistogram.bins.size(); ++kk) {
679 stream << RealType(_finalhistogram.params.binLowerValue(kk)) << sep
680 << RealType(_finalhistogram.bins(kk)) << sep
681 << RealType(_finalhistogram.delta(kk)) << sep
682 << RealType(_simplefinalhistogram.delta(kk)) << linesep;
700 int max_width = 0,
bool print_histogram =
true)
704 const auto& res = *
this;
707 const RunTaskResultList & collresults = res.collectedRunTaskResults();
708 const FinalHistogramType finalhistogram = res.finalHistogram();
713 cdata.printBasicCDataMHRWInfo(str);
716 const auto& stats_coll_result = collresults[j]->stats_collector_result;
718 tomo_internal::print_hist_short_bar_with_accept_info(str, dig_w, j, stats_coll_result.hist,
719 collresults[j]->acceptance_ratio, h.
columns());
722 const int nbins = stats_coll_result.converged_status.
size();
723 const int n_conv = stats_coll_result.converged_status
724 .cwiseEqual(BinningAnalysisParamsType::CONVERGED).count();
725 Eigen::ArrayXi unkn_arr = (stats_coll_result.converged_status
726 .cwiseEqual(BinningAnalysisParamsType::UNKNOWN_CONVERGENCE))
727 .
template cast<int>();
729 const int n_unknown = unkn_arr.count();
730 const int n_unknown_followingotherunknown
731 = unkn_arr.segment(0,nbins-1).cwiseProduct(unkn_arr.segment(1,nbins-1)).count();
732 const int n_unknown_isolated = n_unknown - n_unknown_followingotherunknown;
733 const int n_notconv = stats_coll_result.converged_status
734 .cwiseEqual(BinningAnalysisParamsType::NOT_CONVERGED).count();
735 str <<
" error bars: " << n_conv <<
" converged / " 736 << n_unknown <<
" maybe (" << n_unknown_isolated <<
" isolated) / " 737 << n_notconv <<
" not converged\n";
742 if (print_histogram) {
755 FinalHistogramType _finalhistogram;
756 SimpleFinalHistogramType _simplefinalhistogram;
758 RunTaskResultList _collected_runtaskresults;
766 template<
typename Cnt,
typename CData>
767 inline void init(Cnt num_total_runs, Cnt ,
const CData * pcdata)
769 tomographer_assert(!isFinalized() &&
"init() called after results have been finalized!");
771 _collected_runtaskresults.
resize(num_total_runs, NULL);
772 _finalhistogram.
reset(pcdata->histogram_params);
773 _simplefinalhistogram.
reset(pcdata->histogram_params);
777 template<
typename Cnt,
typename TaskResultType,
typename CData>
778 inline void collectResult(Cnt task_no, TaskResultType && taskresult,
const CData *)
780 tomographer_assert(!isFinalized() &&
"collectResult() called after results have been finalized!");
784 auto stats_coll_result = taskresult.stats_collector_result;
787 str <<
"(). Got task result. Histogram (w/ error bars from binning analysis):\n" 788 << stats_coll_result.hist.prettyPrint();
791 if ((stats_coll_result.converged_status !=
792 Eigen::ArrayXi::Constant(stats_coll_result.hist.numBins(), BinningAnalysisParamsType::CONVERGED)).any()) {
794 str <<
"Error bars have not converged! The error bars at different binning levels are:\n" 795 << stats_coll_result.error_levels <<
"\n" 796 <<
"\t-> convergence analysis: \n";
797 for (
std::size_t k = 0; k < stats_coll_result.hist.numBins(); ++k) {
798 str <<
"\t val[" <<
std::setw(3) << k <<
"] = " 799 <<
std::setw(12) << stats_coll_result.hist.bins(k)
800 <<
" +- " <<
std::setw(12) << stats_coll_result.hist.delta(k);
801 if (stats_coll_result.converged_status(k) == BinningAnalysisParamsType::CONVERGED) {
802 str <<
" [CONVERGED]";
803 }
else if (stats_coll_result.converged_status(k) == BinningAnalysisParamsType::NOT_CONVERGED) {
804 str <<
" [NOT CONVERGED]";
805 }
else if (stats_coll_result.converged_status(k) == BinningAnalysisParamsType::UNKNOWN_CONVERGENCE) {
808 str <<
" [UNKNOWN CONVERGENCE STATUS: " << stats_coll_result.converged_status(k) <<
"]";
819 logger.debug(
"added histogram.");
824 str <<
"Simple histogram is:\n";
825 histogramPrettyPrint<SimpleScaledHistogramType>(str, stats_coll_result.hist);
827 _simplefinalhistogram.
addHistogram(stats_coll_result.hist);
829 _collected_runtaskresults[task_no] =
new RunTaskResult(
std::move(taskresult));
831 logger.debug(
"done.");
835 template<
typename Cnt,
typename CData>
838 tomographer_assert(!isFinalized() &&
"runs_finished() called after results have been finalized!");
856 namespace tomo_internal {
857 template<
typename CDataBaseType,
typename LoggerType,
bool UseBinningAnalysis>
858 struct ResultsCollectorTypeHelper {
861 template<
typename CDataBaseType,
typename LoggerType>
862 struct ResultsCollectorTypeHelper<CDataBaseType, LoggerType, true> {
937 template<
typename ValueCalculator_,
bool UseBinningAnalysis_ =
true,
938 typename IterCountIntType_ = int,
typename StepRealType_ = double,
939 typename CountRealType_ = double,
typename HistCountIntType_ = IterCountIntType_>
961 static constexpr
bool UseBinningAnalysis = UseBinningAnalysis_;
1000 TOMOGRAPHER_ENABLED_IF(!UseBinningAnalysis)
1001 CDataBase(const ValueCalculator & valcalc_, HistogramParams histogram_params_,
1002 MHRWParamsType p,
int base_seed = 0)
1003 : Base(
std::move(p), base_seed), valcalc(valcalc_), histogram_params(histogram_params_),
1009 TOMOGRAPHER_ENABLED_IF(UseBinningAnalysis)
1010 CDataBase(const ValueCalculator & valcalc_, HistogramParams histogram_params_,
int binning_num_levels_,
1011 MHRWParamsType p,
int base_seed = 0)
1012 : Base(
std::move(p), base_seed), valcalc(valcalc_), histogram_params(histogram_params_),
1013 binningNumLevels(binning_num_levels_)
1030 template<
typename LoggerType, TOMOGRAPHER_ENABLED_IF_TMPL(!UseBinningAnalysis)>
1046 template<
typename LoggerType, TOMOGRAPHER_ENABLED_IF_TMPL(UseBinningAnalysis)>
1048 typename tomo_internal::histogram_types<CDataBase, true>::BinningMHRWStatsCollectorParams,
1052 typedef typename tomo_internal::histogram_types<CDataBase, true>::BinningMHRWStatsCollectorParams
1053 BinningMHRWStatsCollectorParams;
1058 binningNumLevels.value,
1078 template<
typename LoggerType>
1081 #ifndef TOMOGRAPHER_PARSED_BY_DOXYGEN 1083 tomo_internal::ResultsCollectorTypeHelper<CDataBase<ValueCalculator,UseBinningAnalysis,IterCountIntType,StepRealType,CountRealType,HistCountIntType>,
1084 LoggerType, UseBinningAnalysis>::type
1090 typedef typename Type::RunTaskResult RunTaskResultType;
1094 template<
typename ValueCalculator_,
bool UseBinningAnalysis_,
1095 typename IterCountIntType_,
typename StepRealType_,
1096 typename CountRealType_,
typename HistCountIntType_>
A StatsCollector which builds a histogram of values calculated with a ValueCalculator for each data s...
#define TOMO_STATIC_ASSERT_EXPR(...)
Tool for static assertions without message.
const RunTaskResultList & collectedRunTaskResults() const
Return a list of the resulting report of each run task.
void addHistogram(const HistogramType &histogram)
Add a new histogram in the data series.
CDataBaseType::StepRealType StepRealType
The real type which serves to describe the step size of the random walk (typically double) ...
std::size_t numTasks() const
Return the number of tasks that were run.
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.
void collectResult(Cnt task_no, TaskResultType &&taskresult, const CData *)
In compliance with ResultsCollector Interface.
Results collector, if binning analysis is being used.
LoggerType_ LoggerType
The logger type we can send messages to.
LoggerType_ LoggerType
The logger type we can send messages to.
HistogramType MHRWStatsCollectorResultType
The result type of our ValueHistogramMHRWStatsCollector.
AveragedHistogram< ScaledHistogramType, CountRealType > FinalHistogramType
tomo_internal::histogram_types< CDataBaseType_, true >::BinningMHRWStatsCollectorParams BinningMHRWStatsCollectorParams
The parameters for the relevant ValueHistogramWithBinningMHRWStatsCollector.
Helper to get the results collector type.
const Tools::StoreIfEnabled< int, UseBinningAnalysis > binningNumLevels
The number of levels in the binning analysis (only if we are using a binning analysis) ...
const double MHRWAcceptanceRatioRecommendedMin
Minimal recommended acceptance ratio.
MHRWParams< IterCountIntType, StepRealType > MHRWParamsType
Type for the parameters of the random walk.
ValueCalculator_ ValueCalculator
The class which calculates the value we are collecting a histogram of (ValueCalculator Interface comp...
tomo_internal::histogram_types< CDataBase, UseBinningAnalysis >::MHRWStatsCollectorResultType MHRWStatsCollectorResultType
The result type of our stats collector.
ValueHistogramWithBinningMHRWStatsCollector< typename tomo_internal::histogram_types< CDataBase, true >::BinningMHRWStatsCollectorParams, LoggerType > createStatsCollector(LoggerType &logger) const
Create the stats collector (with binning analysis)
MHRWTasks::CDataBase< IterCountIntType_, StepRealType_ > Base
The MHRWTasks::CDataBase base class.
Multiprocessing tasks interface (see Multiprocessing Task Interfaces) for parallel Metropolis-Hasting...
#define TOMO_ORIGIN
Use this as argument for a Tomographer::Logger::LocalLogger constructor .
std::vector< RunTaskResult * > RunTaskResultList
A list which will store the result of each task.
void runsFinished(Cnt, const CData *)
In compliance with ResultsCollector Interface — finalizes the results.
void init(Cnt num_total_runs, Cnt, const CData *pcdata)
In compliance with ResultsCollector Interface.
FinalHistogramType finalHistogram() const
The final histogram, with error bars.
BinningMHRWStatsCollectorParams::Result MHRWStatsCollectorResultType
The result type of our stats collector.
LocalLogger< LocalLogger< BaseLoggerType > > subLogger(const std::string &new_prefix)
Create a sub-logger.
CDataBaseType_ CDataBaseType
The structure which hold the constant shared data describing what we have to do.
Data needed to be accessible to the working code.
CDataBaseType::IterCountIntType IterCountIntType
The integer type which serves to count the number of iterations (see MHRWParams)
const HistogramParams histogram_params
The parameters of the histogram that we are collecting.
ValueHistogramMHRWStatsCollector< ValueCalculator, LoggerType, HistogramType > createStatsCollector(LoggerType &logger) const
Create the stats collector (without binning analysis)
Traits-like class for ValueHistogramWithBinningMHRWStatsCollector.
MHRandomWalkTaskResult< MHRWStatsCollectorResultType, IterCountIntType, StepRealType > RunTaskResult
Stores information about the result of a task run.
void printFinalReport(std::ostream &str, const CDataBaseType &cdata, int max_width=0, bool print_histogram=true)
Produce a final, human-readable report of the whole procedure.
void histogramPrettyPrint(std::ostream &str, const HistogramType &histogram, int max_width=0)
pretty-print the given histogram.
Constant data structure for MH random walk tasks with a value histogram stats collector.
Collect a histogram of values from a MH random walk, with binning analysis.
const double MHRWAcceptanceRatioRecommendedMax
Maximal recommended acceptance ratio.
CDataBaseType::ValueCalculator ValueCalculator
The class which calculates the value we are collecting a histogram of (ValueCalculator Interface comp...
const ValueCalculator valcalc
The value calculator instance.
CDataBaseType::ValueCalculator ValueCalculator
The class which calculates the value we are collecting a histogram of (ValueCalculator Interface comp...
Base::StepRealType StepRealType
The real type which serves to describe the step size of the random walk (typically double) ...
ResultsCollectorSimple(LoggerType &logger_)
Constructor.
ResultsCollectorWithBinningAnalysis(LoggerType &logger_)
Constructor.
AveragedHistogram< HistogramType, CountRealType > FinalHistogramType
The final histogram, properly averaged.
void printFinalReport(std::ostream &str, const CDataBaseType &cdata, int max_width=0, bool print_histogram=true)
Produce a final, human-readable report of the whole procedure.
Some C++ utilities, with a tad of C++11 tricks.
IterCountIntType_ IterCountIntType
Type used to count the number of iterations.
CDataBaseType::HistogramParams HistogramParams
The parameters type used to describe our histogram range and number of bins.
CDataBaseType::CountRealType CountRealType
The real type which serves to average histogram counts (typically double)
tomo_internal::histogram_types< CDataBase, UseBinningAnalysis >::HistogramParams HistogramParams
The appropriate parameters type for the histogram reported by the task.
void reset(const Params ¶ms_)
Resets the data and sets new params.
void init(Cnt num_total_runs, Cnt, const CData *pcdata)
In compliance with ResultsCollector Interface.
tomo_internal::histogram_types< CDataBase, UseBinningAnalysis >::HistogramType HistogramType
The histogram type reported by the task.
StepRealType_ StepRealType
Type used to specify the step size.
CDataBaseType::HistogramType HistogramType
The histogram type corresponding to the result of a task.
void collectResult(Cnt task_no, TaskResultType &&taskresult, const CData *)
In compliance with ResultsCollector Interface.
Managing the need for specific overrides to operator new() for some types (especially Eigen types) ...
HistCountIntType_ HistCountIntType
The integer counting type in our underlying raw histogram type.
UniformBinsHistogram< typename HistogramType::Scalar, CountRealType > ScaledHistogramType
The type of the histogram resulting from a single task, but scaled so that each bin value corresponds...
CDataBaseType::StepRealType StepRealType
The real type which serves to describe the step size of the random walk (typically double) ...
const RunTaskResultList & collectedRunTaskResults() const
Return a list of the resulting report of each run task.
CDataBaseType::HistogramParams HistogramParams
The histogram parameters type reported by each task.
CDataBaseType::IterCountIntType IterCountIntType
The integer type which serves to count the number of iterations (see MHRWParams)
BinningMHRWStatsCollectorParams::BinningAnalysisParamsType BinningAnalysisParamsType
The parameters for the corresponding BinningAnalysis type.
void printHistogramCsv(std::ostream &stream, std::string sep="\, std::string linesep="\", int precision=10)
Produce a comma-separated-value (CSV) representation of the final histogram.
UniformBinsHistogram< typename HistogramType::Scalar, CountRealType > SimpleScaledHistogramType
The "simple" histogram, as if without binning analysis.
std::size_t numTasks() const
Return the number of tasks that were run.
void printHistogramCsv(std::ostream &stream, std::string sep="\, std::string linesep="\", int precision=10)
Produce a comma-separated-value (CSV) representation of the final histogram.
Results collector, if no binning analysis is being used.
Definitions for MHRWStatsCollector Interface's.
const RunTaskResult * collectedRunTaskResult(std::size_t task_no) const
Return a list of the resulting report of one specific run task.
#define streamstr(tokens)
Utility macro to format stream tokens to a std::string.
CDataBaseType::CountRealType CountRealType
The real type which serves to average histogram counts (typically double)
void runsFinished(Cnt, const CData *)
In compliance with ResultsCollector Interface.
AveragedHistogram< SimpleScaledHistogramType, double > SimpleFinalHistogramType
Properly averaged "simple" histogram, with naive statistical standard deviation error bars from the s...
bool isFinalized() const
Returns TRUE after all runs have finished and results processed.
bool isFinalized() const
Returns TRUE after all runs have finished and results processed.
T setprecision(T... args)
const RunTaskResult * collectedRunTaskResult(std::size_t task_no) const
Return a list of the resulting report of one specific run task.
Stores information about the result of a task run.
const ScaledHistogramType histogram
The resulting histogram.
Base::IterCountIntType IterCountIntType
The integer type which serves to count the number of iterations (see MHRWParams)
CDataBaseType_ CDataBaseType
The structure which hold the constant shared data describing what we have to do.
CountRealType_ CountRealType
The real type which serves to average histogram counts (typically double)
CDataBaseType::HistogramType HistogramType
The histogram type reported by each task.
SimpleFinalHistogramType simpleFinalHistogram() const
The final histogram, with naive error bars ignoring the binning analysis.
Utilities for logging messages.
void finalize()
Finalize the averaging procedure.
FinalHistogramType finalHistogram() const
The final histogram, with all the error bars combined.