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>
90 void print_hist_short_bar_with_accept_info(
std::ostream & str,
int dig_w,
std::size_t j,
const HistogramType & hist,
91 double acceptance_ratio,
int columns)
134 template<
typename CDataBaseType_,
typename LoggerType_>
137 AveragedHistogram<UniformBinsHistogram<typename CDataBaseType_::HistogramType::Scalar,
138 typename CDataBaseType_::CountRealType>,
139 typename CDataBaseType_::CountRealType>
197 : Base(), histogram()
201 template<
typename BaseType,
typename ScaledHistogramTypeRef>
202 RunTaskResult(BaseType&& b, ScaledHistogramTypeRef&& histogram_)
203 : Base(std::forward<BaseType>(b)), histogram(histogram_)
226 : _finalized(false), _finalhistogram(HistogramParams()),
227 _collected_runtaskresults(),
228 _llogger(
"MHRWTasks::ValueHistogramTasks::ResultsCollectorSimple", logger_)
234 for (
std::size_t j = 0; j < _collected_runtaskresults.size(); ++j) {
235 if (_collected_runtaskresults[j] != NULL) {
236 delete _collected_runtaskresults[j];
260 tomographer_assert(isFinalized() &&
"You may only call finalHistogram() after the runs have been finalized.");
261 return _finalhistogram;
270 tomographer_assert(isFinalized() &&
"You may only call numTasks() after the runs have been finalized.");
271 return _collected_runtaskresults.size();
283 tomographer_assert(isFinalized() &&
"You may only call collectedRunTaskResults() after the runs have been finalized.");
284 return _collected_runtaskresults;
299 tomographer_assert(isFinalized() &&
"You may only call collectedRunTaskResult(std::size_t) after the runs have been finalized.");
301 return _collected_runtaskresults[task_no];
316 template<
typename RealType =
double>
320 stream <<
"Value" << sep <<
"Counts" << sep <<
"Error" << linesep
322 for (
int kk = 0; kk < _finalhistogram.bins.size(); ++kk) {
323 stream << RealType(_finalhistogram.params.binLowerValue(kk)) << sep
324 << RealType(_finalhistogram.bins(kk)) << sep
325 << RealType(_finalhistogram.delta(kk)) << linesep;
344 int max_width = 0,
bool print_histogram =
true)
348 const auto& res = *
this;
350 const RunTaskResultList & collresults = res.collectedRunTaskResults();
351 const FinalHistogramType finalhistogram = res.finalHistogram();
356 cdata.printBasicCDataMHRWInfo(str);
359 tomo_internal::print_hist_short_bar_with_accept_info(str, dig_w, j, collresults[j]->histogram,
360 collresults[j]->acceptance_ratio, h.
columns());
365 if (print_histogram) {
379 FinalHistogramType _finalhistogram;
381 RunTaskResultList _collected_runtaskresults;
390 template<
typename Cnt,
typename CData>
391 inline void init(Cnt num_total_runs, Cnt ,
const CData * pcdata)
393 tomographer_assert(!isFinalized() &&
"init() called after results have been finalized!");
395 _collected_runtaskresults.
resize(num_total_runs, NULL);
396 _finalhistogram.
reset(pcdata->histogram_params);
399 template<
typename Cnt,
typename TaskResultType,
typename CData>
400 inline void collectResult(Cnt task_no, TaskResultType&& taskresult,
const CData * )
402 tomographer_assert(!isFinalized() &&
"collectResult() called after results have been finalized!");
406 str <<
"Got task result. Histogram is:\n" << taskresult.stats_collector_result.prettyPrint();
416 ScaledHistogramType thishistogram = taskresult.stats_collector_result;
419 thishistogram.
bins /= numsamples;
423 _collected_runtaskresults[task_no]
432 template<
typename Cnt,
typename CData>
435 tomographer_assert(!isFinalized() &&
"runsFinished() called after results have been finalized!");
478 template<
typename CDataBaseType_,
typename LoggerType_>
481 UniformBinsHistogram<typename CDataBaseType_::HistogramType::Scalar,
482 typename CDataBaseType_::CountRealType>
503 typedef typename tomo_internal::histogram_types<CDataBaseType_,true>::BinningMHRWStatsCollectorParams
563 : _finalized(false), _finalhistogram(), _simplefinalhistogram(),
564 _collected_runtaskresults(),
565 _llogger(
"MHRWTasks::ValueHistogramTasks::ResultsCollectorWithBinningAnalysis", logger_)
571 for (
std::size_t j = 0; j < _collected_runtaskresults.size(); ++j) {
572 if (_collected_runtaskresults[j] != NULL) {
573 delete _collected_runtaskresults[j];
597 tomographer_assert(isFinalized() &&
"You may only call finalHistogram() after the runs have been finalized.");
598 return _finalhistogram;
614 tomographer_assert(isFinalized() &&
"You may only call simpleFinalHistogram() after the runs have been finalized.");
615 return _simplefinalhistogram;
624 tomographer_assert(isFinalized() &&
"You may only call numTasks() after the runs have been finalized.");
625 return _collected_runtaskresults.size();
637 tomographer_assert(isFinalized() &&
"You may only call collectedRunTaskResults() after the runs have been finalized.");
638 return _collected_runtaskresults;
653 tomographer_assert(isFinalized() &&
"You may only call collectedRunTaskResult(std::size_t) after the runs have been finalized.");
655 return _collected_runtaskresults[task_no];
672 template<
typename RealType =
double>
677 stream <<
"Value" << sep <<
"Counts" << sep <<
"Error" << sep <<
"SimpleError" << linesep
679 for (
int kk = 0; kk < _finalhistogram.bins.size(); ++kk) {
680 stream << RealType(_finalhistogram.params.binLowerValue(kk)) << sep
681 << RealType(_finalhistogram.bins(kk)) << sep
682 << RealType(_finalhistogram.delta(kk)) << sep
683 << RealType(_simplefinalhistogram.delta(kk)) << linesep;
701 int max_width = 0,
bool print_histogram =
true)
705 const auto& res = *
this;
708 const RunTaskResultList & collresults = res.collectedRunTaskResults();
709 const FinalHistogramType finalhistogram = res.finalHistogram();
714 cdata.printBasicCDataMHRWInfo(str);
717 const auto& stats_coll_result = collresults[j]->stats_collector_result;
719 tomo_internal::print_hist_short_bar_with_accept_info(str, dig_w, j, stats_coll_result.hist,
720 collresults[j]->acceptance_ratio, h.
columns());
723 const auto nbins = stats_coll_result.converged_status.
size();
724 const auto n_conv = stats_coll_result.converged_status
725 .cwiseEqual(BinningAnalysisParamsType::CONVERGED).count();
726 Eigen::ArrayXi unkn_arr = (stats_coll_result.converged_status
727 .cwiseEqual(BinningAnalysisParamsType::UNKNOWN_CONVERGENCE))
728 .
template cast<int>();
730 const auto n_unknown = unkn_arr.count();
731 const auto n_unknown_followingotherunknown
732 = unkn_arr.segment(0,nbins-1).cwiseProduct(unkn_arr.segment(1,nbins-1)).count();
733 const auto n_unknown_isolated = n_unknown - n_unknown_followingotherunknown;
734 const auto n_notconv = stats_coll_result.converged_status
735 .cwiseEqual(BinningAnalysisParamsType::NOT_CONVERGED).count();
736 str <<
" error bars: " << n_conv <<
" converged / " 737 << n_unknown <<
" maybe (" << n_unknown_isolated <<
" isolated) / " 738 << n_notconv <<
" not converged\n";
743 if (print_histogram) {
756 FinalHistogramType _finalhistogram;
757 SimpleFinalHistogramType _simplefinalhistogram;
759 RunTaskResultList _collected_runtaskresults;
767 template<
typename Cnt,
typename CData>
768 inline void init(Cnt num_total_runs, Cnt ,
const CData * pcdata)
770 tomographer_assert(!isFinalized() &&
"init() called after results have been finalized!");
772 _collected_runtaskresults.
resize(num_total_runs, NULL);
773 _finalhistogram.
reset(pcdata->histogram_params);
774 _simplefinalhistogram.
reset(pcdata->histogram_params);
778 template<
typename Cnt,
typename TaskResultType,
typename CData>
779 inline void collectResult(Cnt task_no, TaskResultType && taskresult,
const CData *)
781 tomographer_assert(!isFinalized() &&
"collectResult() called after results have been finalized!");
785 auto stats_coll_result = taskresult.stats_collector_result;
788 str <<
"(). Got task result. Histogram (w/ error bars from binning analysis):\n" 789 << stats_coll_result.hist.prettyPrint();
792 if ((stats_coll_result.converged_status !=
793 Eigen::ArrayXi::Constant(stats_coll_result.hist.numBins(), BinningAnalysisParamsType::CONVERGED)).any()) {
795 str <<
"Error bars have not converged! The error bars at different binning levels are:\n" 796 << stats_coll_result.error_levels <<
"\n" 797 <<
"\t-> convergence analysis: \n";
798 for (
std::size_t k = 0; k < stats_coll_result.hist.numBins(); ++k) {
799 str <<
"\t val[" <<
std::setw(3) << k <<
"] = " 800 <<
std::setw(12) << stats_coll_result.hist.bins(k)
801 <<
" +- " <<
std::setw(12) << stats_coll_result.hist.delta(k);
802 if (stats_coll_result.converged_status(k) == BinningAnalysisParamsType::CONVERGED) {
803 str <<
" [CONVERGED]";
804 }
else if (stats_coll_result.converged_status(k) == BinningAnalysisParamsType::NOT_CONVERGED) {
805 str <<
" [NOT CONVERGED]";
806 }
else if (stats_coll_result.converged_status(k) == BinningAnalysisParamsType::UNKNOWN_CONVERGENCE) {
809 str <<
" [UNKNOWN CONVERGENCE STATUS: " << stats_coll_result.converged_status(k) <<
"]";
820 logger.debug(
"added histogram.");
825 str <<
"Simple histogram is:\n";
826 histogramPrettyPrint<SimpleScaledHistogramType>(str, stats_coll_result.hist);
828 _simplefinalhistogram.
addHistogram(stats_coll_result.hist);
830 _collected_runtaskresults[task_no] =
new RunTaskResult(
std::move(taskresult));
832 logger.debug(
"done.");
836 template<
typename Cnt,
typename CData>
839 tomographer_assert(!isFinalized() &&
"runs_finished() called after results have been finalized!");
857 namespace tomo_internal {
858 template<
typename CDataBaseType,
typename LoggerType,
bool UseBinningAnalysis>
859 struct ResultsCollectorTypeHelper {
862 template<
typename CDataBaseType,
typename LoggerType>
863 struct ResultsCollectorTypeHelper<CDataBaseType, LoggerType, true> {
938 template<
typename ValueCalculator_,
bool UseBinningAnalysis_ =
true,
939 typename IterCountIntType_ = int,
typename StepRealType_ = double,
940 typename CountRealType_ = double,
typename HistCountIntType_ = IterCountIntType_>
962 static constexpr
bool UseBinningAnalysis = UseBinningAnalysis_;
1001 TOMOGRAPHER_ENABLED_IF(!UseBinningAnalysis)
1002 CDataBase(const ValueCalculator & valcalc_, HistogramParams histogram_params_,
1003 MHRWParamsType p,
int base_seed = 0)
1004 : Base(
std::move(p), base_seed), valcalc(valcalc_), histogram_params(histogram_params_),
1010 TOMOGRAPHER_ENABLED_IF(UseBinningAnalysis)
1011 CDataBase(const ValueCalculator & valcalc_, HistogramParams histogram_params_,
int binning_num_levels_,
1012 MHRWParamsType p,
int base_seed = 0)
1013 : Base(
std::move(p), base_seed), valcalc(valcalc_), histogram_params(histogram_params_),
1014 binningNumLevels(binning_num_levels_)
1031 template<
typename LoggerType, TOMOGRAPHER_ENABLED_IF_TMPL(!UseBinningAnalysis)>
1047 template<
typename LoggerType, TOMOGRAPHER_ENABLED_IF_TMPL(UseBinningAnalysis)>
1049 typename tomo_internal::histogram_types<CDataBase, true>::BinningMHRWStatsCollectorParams,
1053 typedef typename tomo_internal::histogram_types<CDataBase, true>::BinningMHRWStatsCollectorParams
1054 BinningMHRWStatsCollectorParams;
1059 binningNumLevels.value,
1079 template<
typename LoggerType>
1082 #ifndef TOMOGRAPHER_PARSED_BY_DOXYGEN 1084 tomo_internal::ResultsCollectorTypeHelper<CDataBase<ValueCalculator,UseBinningAnalysis,IterCountIntType,StepRealType,CountRealType,HistCountIntType>,
1085 LoggerType, UseBinningAnalysis>::type
1091 typedef typename Type::RunTaskResult RunTaskResultType;
1095 template<
typename ValueCalculator_,
bool UseBinningAnalysis_,
1096 typename IterCountIntType_,
typename StepRealType_,
1097 typename CountRealType_,
typename HistCountIntType_>
A StatsCollector which builds a histogram of values calculated with a ValueCalculator for each data s...
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.
#define TOMO_STATIC_ASSERT_EXPR(...)
Tool for static assertions without message.
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.
#define tomographer_assert(...)
Assertion test macro.
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.