27 #ifndef TOMOGRAPHER_MHRW_VALUEHIST_TASKS_H 28 #define TOMOGRAPHER_MHRW_VALUEHIST_TASKS_H 36 #include <boost/math/constants/constants.hpp> 49 namespace ValueHistogramTasks {
52 namespace tomo_internal {
53 template<
typename CDataBaseType,
bool UseBinningAnalysis>
54 struct histogram_types {
57 typedef HistogramType MHRWStatsCollectorResultType;
58 typedef typename HistogramType::Params HistogramParams;
60 template<
typename CDataBaseType>
61 struct histogram_types<CDataBaseType, true> {
64 typename CDataBaseType::ValueCalculator,
typename CDataBaseType::CountIntType,
65 typename CDataBaseType::CountRealType, Eigen::Dynamic, Eigen::Dynamic
66 > BinningMHRWStatsCollectorParams;
68 typedef typename BinningMHRWStatsCollectorParams::Result MHRWStatsCollectorResultType;
69 typedef typename BinningMHRWStatsCollectorParams::HistogramType HistogramType;
70 typedef typename BinningMHRWStatsCollectorParams::HistogramParams HistogramParams;
87 template<
typename CDataBaseType_,
typename LoggerType_>
90 AveragedHistogram<UniformBinsHistogram<typename CDataBaseType_::HistogramType::Scalar,
91 typename CDataBaseType_::CountRealType>,
92 typename CDataBaseType_::CountRealType>
95 typedef CDataBaseType_ CDataBaseType;
96 typedef typename CDataBaseType::ValueCalculator ValueCalculator;
97 typedef typename CDataBaseType::CountRealType CountRealType;
98 typedef typename CDataBaseType::CountIntType CountIntType;
99 typedef typename CDataBaseType::StepRealType StepRealType;
100 typedef LoggerType_ LoggerType;
102 typedef typename CDataBaseType::HistogramType HistogramType;
103 typedef typename CDataBaseType::HistogramParams HistogramParams;
107 typedef HistogramType MHRWStatsCollectorResultType;
126 : Base(), histogram()
130 template<
typename BaseType,
typename NormalizedHistogramTypeRef>
131 RunTaskResult(BaseType&& b, NormalizedHistogramTypeRef&& histogram_)
132 : Base(std::forward<BaseType>(b)), histogram(histogram_)
136 const NormalizedHistogramType histogram;
143 : _finalized(
false), _finalhistogram(HistogramParams()),
144 _collected_runtaskresults(),
145 _llogger(
"MHRWTasks::ValueHistogramTasks::ResultsCollectorSimple", logger_)
151 for (
std::size_t j = 0; j < _collected_runtaskresults.size(); ++j) {
152 if (_collected_runtaskresults[j] != NULL) {
153 delete _collected_runtaskresults[j];
158 inline bool isFinalized()
const {
return _finalized; }
160 inline FinalHistogramType finalHistogram()
const {
161 tomographer_assert(isFinalized() &&
"You may only call finalHistogram() after the runs have been finalized.");
162 return _finalhistogram;
166 tomographer_assert(isFinalized() &&
"You may only call numTasks() after the runs have been finalized.");
167 return _collected_runtaskresults.size();
170 inline const RunTaskResultList & collectedRunTaskResults()
const {
171 tomographer_assert(isFinalized() &&
"You may only call collectedRunTaskResults() after the runs have been finalized.");
172 return _collected_runtaskresults;
176 tomographer_assert(isFinalized() &&
"You may only call collectedRunTaskResult(std::size_t) after the runs have been finalized.");
177 tomographer_assert(task_no < _collected_runtaskresults.size());
178 return _collected_runtaskresults[task_no];
181 template<
typename RealType =
double>
184 stream <<
"Value" << sep <<
"Counts" << sep <<
"Error" << linesep
186 for (
int kk = 0; kk < _finalhistogram.bins.size(); ++kk) {
187 stream << RealType(_finalhistogram.params.binLowerValue(kk)) << sep
188 << RealType(_finalhistogram.bins(kk)) << sep
189 << RealType(_finalhistogram.delta(kk)) << linesep;
195 FinalHistogramType _finalhistogram;
197 RunTaskResultList _collected_runtaskresults;
205 template<
typename Cnt,
typename CData>
206 inline void init(Cnt num_total_runs, Cnt ,
const CData * pcdata)
208 tomographer_assert(!isFinalized() &&
"init() called after results have been finalized!");
210 _collected_runtaskresults.
resize(num_total_runs, NULL);
211 _finalhistogram.
reset(pcdata->histogram_params);
213 template<
typename Cnt,
typename TaskResultType,
typename CData>
214 inline void collectResult(Cnt task_no, TaskResultType&& taskresult,
const CData * )
216 tomographer_assert(!isFinalized() &&
"collectResult() called after results have been finalized!");
220 str <<
"Got task result. Histogram is:\n" << taskresult.stats_collector_result.prettyPrint();
223 NormalizedHistogramType thishistogram = taskresult.stats_collector_result;
226 thishistogram.
bins /= normalization;
227 thishistogram.
off_chart /= normalization;
230 _collected_runtaskresults[task_no]
233 template<
typename Cnt,
typename CData>
234 inline void runsFinished(Cnt,
const CData *)
236 tomographer_assert(!isFinalized() &&
"runsFinished() called after results have been finalized!");
255 template<
typename CDataBaseType_,
typename LoggerType_>
258 UniformBinsHistogram<typename CDataBaseType_::HistogramType::Scalar,
259 typename CDataBaseType_::CountRealType>
262 typedef CDataBaseType_ CDataBaseType;
263 typedef typename CDataBaseType::ValueCalculator ValueCalculator;
264 typedef typename CDataBaseType::CountRealType CountRealType;
265 typedef typename CDataBaseType::CountIntType CountIntType;
266 typedef typename CDataBaseType::StepRealType StepRealType;
267 typedef LoggerType_ LoggerType;
269 typedef typename tomo_internal::histogram_types<CDataBaseType_,true>::BinningMHRWStatsCollectorParams
270 BinningMHRWStatsCollectorParams;
272 typedef typename BinningMHRWStatsCollectorParams::BinningAnalysisParamsType BinningAnalysisParamsType;
274 typedef typename BinningMHRWStatsCollectorParams::Result MHRWStatsCollectorResultType;
276 typedef typename CDataBaseType::HistogramType HistogramType;
277 typedef typename CDataBaseType::HistogramParams HistogramParams;
298 : _finalized(false), _finalhistogram(), _simplefinalhistogram(),
299 _collected_runtaskresults(),
300 _llogger(
"MHRWTasks::ValueHistogramTasks::ResultsCollectorWithBinningAnalysis", logger_)
306 for (
std::size_t j = 0; j < _collected_runtaskresults.size(); ++j) {
307 if (_collected_runtaskresults[j] != NULL) {
308 delete _collected_runtaskresults[j];
313 inline bool isFinalized()
const {
return _finalized; }
315 inline FinalHistogramType finalHistogram()
const {
316 tomographer_assert(isFinalized() &&
"You may only call finalHistogram() after the runs have been finalized.");
317 return _finalhistogram;
320 inline SimpleFinalHistogramType simpleFinalHistogram()
const {
321 tomographer_assert(isFinalized() &&
"You may only call simpleFinalHistogram() after the runs have been finalized.");
322 return _simplefinalhistogram;
326 tomographer_assert(isFinalized() &&
"You may only call numTasks() after the runs have been finalized.");
327 return _collected_runtaskresults.size();
330 inline const RunTaskResultList & collectedRunTaskResults()
const {
331 tomographer_assert(isFinalized() &&
"You may only call collectedRunTaskResults() after the runs have been finalized.");
332 return _collected_runtaskresults;
335 inline const RunTaskResult * collectedRunTaskResult(
std::size_t task_no)
const {
336 tomographer_assert(isFinalized() &&
"You may only call collectedRunTaskResult(std::size_t) after the runs have been finalized.");
337 tomographer_assert(task_no < _collected_runtaskresults.size());
338 return _collected_runtaskresults[task_no];
342 template<
typename RealType =
double>
345 stream <<
"Value" << sep <<
"Counts" << sep <<
"Error" << sep <<
"SimpleError" << linesep
347 for (
int kk = 0; kk < _finalhistogram.bins.size(); ++kk) {
348 stream << RealType(_finalhistogram.params.binLowerValue(kk)) << sep
349 << RealType(_finalhistogram.bins(kk)) << sep
350 << RealType(_finalhistogram.delta(kk)) << sep
351 << RealType(_simplefinalhistogram.delta(kk)) << linesep;
357 FinalHistogramType _finalhistogram;
358 SimpleFinalHistogramType _simplefinalhistogram;
360 RunTaskResultList _collected_runtaskresults;
367 template<
typename Cnt,
typename CData>
368 inline void init(Cnt num_total_runs, Cnt ,
const CData * pcdata)
370 tomographer_assert(!isFinalized() &&
"init() called after results have been finalized!");
372 _collected_runtaskresults.
resize(num_total_runs, NULL);
373 _finalhistogram.
reset(pcdata->histogram_params);
374 _simplefinalhistogram.
reset(pcdata->histogram_params);
377 template<
typename Cnt,
typename TaskResultType,
typename CData>
378 inline void collectResult(Cnt task_no, TaskResultType && taskresult,
const CData *)
380 tomographer_assert(!isFinalized() &&
"collectResult() called after results have been finalized!");
384 auto stats_coll_result = taskresult.stats_collector_result;
387 str <<
"(). Got task result. Histogram (w/ error bars from binning analysis):\n" 388 << stats_coll_result.hist.prettyPrint();
391 if ((stats_coll_result.converged_status !=
392 Eigen::ArrayXi::Constant(stats_coll_result.hist.numBins(), BinningAnalysisParamsType::CONVERGED)).any()) {
394 str <<
"Error bars have not converged! The error bars at different binning levels are:\n" 395 << stats_coll_result.error_levels <<
"\n" 396 <<
"\t-> convergence analysis: \n";
397 for (
std::size_t k = 0; k < stats_coll_result.hist.numBins(); ++k) {
398 str <<
"\t val[" <<
std::setw(3) << k <<
"] = " 399 <<
std::setw(12) << stats_coll_result.hist.bins(k)
400 <<
" +- " <<
std::setw(12) << stats_coll_result.hist.delta(k);
401 if (stats_coll_result.converged_status(k) == BinningAnalysisParamsType::CONVERGED) {
402 str <<
" [CONVERGED]";
403 }
else if (stats_coll_result.converged_status(k) == BinningAnalysisParamsType::NOT_CONVERGED) {
404 str <<
" [NOT CONVERGED]";
405 }
else if (stats_coll_result.converged_status(k) == BinningAnalysisParamsType::UNKNOWN_CONVERGENCE) {
408 str <<
" [UNKNOWN CONVERGENCE STATUS: " << stats_coll_result.converged_status(k) <<
"]";
419 logger.debug(
"added histogram.");
424 str <<
"Simple histogram is:\n";
425 histogramPrettyPrint<SimpleNormalizedHistogramType>(str, stats_coll_result.hist);
427 _simplefinalhistogram.
addHistogram(stats_coll_result.hist);
429 _collected_runtaskresults[task_no] =
new RunTaskResult(
std::move(taskresult));
431 logger.debug(
"done.");
434 template<
typename Cnt,
typename CData>
435 inline void runsFinished(Cnt,
const CData *)
437 tomographer_assert(!isFinalized() &&
"runs_finished() called after results have been finalized!");
455 namespace tomo_internal {
456 template<
typename CDataBaseType,
typename LoggerType,
bool UseBinningAnalysis>
457 struct ResultsCollectorTypeHelper {
460 template<
typename CDataBaseType,
typename LoggerType>
461 struct ResultsCollectorTypeHelper<CDataBaseType, LoggerType, true> {
474 template<
typename ValueCalculator_,
bool UseBinningAnalysis_ =
true,
475 typename CountIntType_ = int,
typename StepRealType_ = double,
476 typename CountRealType_ =
double>
486 typedef ValueCalculator_ ValueCalculator;
487 typedef CountRealType_ CountRealType;
489 static constexpr
bool UseBinningAnalysis = UseBinningAnalysis_;
492 MHRWStatsCollectorResultType;
499 TOMOGRAPHER_ENABLED_IF(!UseBinningAnalysis)
500 CDataBase(
const ValueCalculator & valcalc_, HistogramParams histogram_params_,
501 MHRWParamsType p,
int base_seed = 0)
502 : Base(
std::move(p), base_seed), valcalc(valcalc_), histogram_params(histogram_params_),
507 TOMOGRAPHER_ENABLED_IF(UseBinningAnalysis)
508 CDataBase(
const ValueCalculator & valcalc_, HistogramParams histogram_params_,
int binning_num_levels_,
509 MHRWParamsType p,
int base_seed = 0)
510 : Base(
std::move(p), base_seed), valcalc(valcalc_), histogram_params(histogram_params_),
511 binningNumLevels(binning_num_levels_)
515 const ValueCalculator valcalc;
516 const HistogramParams histogram_params;
520 template<
typename LoggerType, TOMOGRAPHER_ENABLED_IF_TMPL(!UseBinningAnalysis)>
522 createStatsCollector(LoggerType & logger)
const 531 template<
typename LoggerType, TOMOGRAPHER_ENABLED_IF_TMPL(UseBinningAnalysis)>
533 typename tomo_internal::histogram_types<CDataBase, true>::BinningMHRWStatsCollectorParams,
535 createStatsCollector(LoggerType & logger)
const 537 typedef typename tomo_internal::histogram_types<CDataBase, true>::BinningMHRWStatsCollectorParams
538 BinningMHRWStatsCollectorParams;
543 binningNumLevels.value,
549 template<
typename LoggerType>
552 #ifndef TOMOGRAPHER_PARSED_BY_DOXYGEN 554 tomo_internal::ResultsCollectorTypeHelper<CDataBase<ValueCalculator,UseBinningAnalysis,CountIntType,StepRealType,CountRealType>,
555 LoggerType, UseBinningAnalysis>::type
561 typedef typename Type::RunTaskResult RunTaskResultType;
565 template<
typename ValueCalculator_,
bool UseBinningAnalysis_,
566 typename CountIntType_,
typename StepRealType_,
567 typename CountRealType_>
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.
CountIntType_ CountIntType
Type used to count the number of iterations.
Base namespace for the Tomographer project.
Results collector, if no binning analysis is being used.
Helper to get the results collector type.
UniformBinsHistogram< typename HistogramType::Scalar, CountRealType > SimpleNormalizedHistogramType
The "simple" histogram, as if without binning analysis.
StepRealType_ StepRealType
Type used to specify the step size.
Multiprocessing tasks interface (see Task Manager/Dispatcher Interfaces) for parallel Metropolis-Hast...
void finalize()
Finalize the averaging procedure.
#define TOMO_ORIGIN
Use this as argument for a Tomographer::Logger::LocalLogger constructor .
LocalLogger< LocalLogger< BaseLoggerType > > subLogger(const std::string &new_prefix)
Create a sub-logger.
Data needed to be accessible to the working code.
Traits-like class for ValueHistogramWithBinningMHRWStatsCollector.
constant data for our MH random walk tasks with value histogram stats collector
Collect a histogram of values from a MH random walk, with binning analysis.
AveragedHistogram< HistogramType, CountRealType > FinalHistogramType
The final histogram, properly averaged.
Some C++ utilities, with a tad of C++11 tricks.
Managing the need for specific overrides to operator new() for some types (especially Eigen types) ...
void addHistogram(const HistogramType &histogram)
Add a new histogram in the data series.
void reset(const Params ¶ms_)
Resets the data and sets new params.
Results collector, if no binning analysis is being used.
Definitions for MHRWStatsCollector Interface's.
Stores information about the result of a task run.
Utilities for logging messages.