27 #ifndef TOMOGRAPHER_MHRWSTATSCOLLECTORS_H 28 #define TOMOGRAPHER_MHRWSTATSCOLLECTORS_H 35 #include <type_traits> 86 template<
typename... MHRWStatsCollectors>
94 static constexpr
int NumStatColl =
sizeof...(MHRWStatsCollectors);
102 : statscollectors(statscollectors_...)
108 inline const typename std::tuple_element<I, MHRWStatsCollectorsTupleType>::type & getStatsCollector()
const 110 return std::get<I>(statscollectors);
118 std::get<I>(statscollectors).init();
131 std::get<I>(statscollectors).thermalizingDone();
132 thermalizingDone<I+1>();
144 std::get<I>(statscollectors).done();
155 template<
typename CountIntType,
typename Po
intType,
typename FnValueType,
typename MHRandomWalk,
int I = 0>
157 CountIntType k,
bool is_thermalizing,
bool is_live_iter,
bool accepted,
158 double a,
const PointType & newpt, FnValueType newptval,
159 const PointType & curpt, FnValueType curptval,
163 std::get<I>(statscollectors).rawMove(
164 k, is_thermalizing, is_live_iter, accepted, a,
165 newpt, newptval, curpt, curptval, rw
167 rawMove<CountIntType, PointType, FnValueType, MHRandomWalk, I+1>(
168 k, is_thermalizing, is_live_iter, accepted, a,
169 newpt, newptval, curpt, curptval, rw
172 template<
typename CountIntType,
typename Po
intType,
typename FnValueType,
typename MHRandomWalk,
int I = 0>
174 CountIntType,
bool,
bool,
bool,
double,
const PointType &, FnValueType,
183 template<
typename CountIntType,
typename Po
intType,
typename FnValueType,
typename MHRandomWalk,
int I = 0>
185 CountIntType k, CountIntType n,
const PointType & curpt, FnValueType curptval,
MHRandomWalk & rw
188 std::get<I>(statscollectors).processSample(k, n, curpt, curptval, rw);
189 processSample<CountIntType, PointType, FnValueType, MHRandomWalk, I+1>(k, n, curpt, curptval, rw);
192 template<
typename CountIntType,
typename Po
intType,
typename FnValueType,
typename MHRandomWalk,
int I = 0>
194 CountIntType, CountIntType,
const PointType &, FnValueType,
MHRandomWalk &
234 template<
typename ValueCalculator_,
263 HistogramType _histogram;
269 ValueCalculator _vcalc;
271 LoggerType & _logger;
276 const ValueCalculator & vcalc,
278 : _histogram(histogram_params),
323 template<
bool Pr
intHistogram = true>
326 if (PrintHistogram) {
329 _logger.longdebug(
"ValueHistogramMHRWStatsCollector",
330 "Done walking & collecting stats. Here's the histogram:\n" 331 + _histogram.prettyPrint());
337 template<
typename CountIntType,
typename Po
intType,
typename LLHValueType,
typename MHRandomWalk>
338 void rawMove(CountIntType k,
bool ,
bool ,
bool ,
339 double ,
const PointType & , LLHValueType ,
342 _logger.longdebug(
"ValueHistogramMHRWStatsCollector", [&](
std::ostream & stream) {
343 stream <<
"rawMove(): k=" << k;
348 template<
typename CountIntType,
typename Po
intType,
typename LLHValueType,
typename MHRandomWalk>
352 ValueType val = _vcalc.getValue(curpt);
354 _logger.longdebug(
"ValueHistogramMHRWStatsCollector", [&](
std::ostream & stream) {
355 stream <<
"in processSample(): " 356 <<
"k=" << k <<
", n=" << n <<
", val=" << val
357 <<
" [with ValueType=" <<
typeid(ValueType).name() <<
"]" ;
360 return _histogram.record(val);
388 template<
typename ValueCalculator_,
389 typename CountIntType_ = int,
390 typename CountRealAvgType_ = double,
391 int NumTrackValues_ = Eigen::Dynamic,
392 int NumLevels_ = Eigen::Dynamic
405 static constexpr
int NumTrackValues = NumTrackValues_;
407 static constexpr
int NumLevels = NumLevels_;
438 typename BinningAnalysisParamsType::BinSumSqArray
443 : hist(), error_levels(), converged_status()
448 template<
typename BinningAnalysisType>
449 Result(HistogramParams p,
const BinningAnalysisType & b)
451 error_levels(b.numTrackValues(), b.numLevels()+1),
452 converged_status(
Eigen::ArrayXi::Constant(b.numTrackValues(), BinningAnalysisType::UNKNOWN_CONVERGENCE))
454 tomographer_assert(converged_status.rows() == b.numTrackValues() && converged_status.cols() == 1);
469 for (
int k = 0; k < converged_status.size(); ++k) {
470 str <<
"\tval[" <<
std::setw(3) << k <<
"] = " 473 if (converged_status(k) == BinningAnalysisParamsType::CONVERGED) {
474 str <<
" [CONVERGED]";
475 }
else if (converged_status(k) == BinningAnalysisParamsType::NOT_CONVERGED) {
476 str <<
" [NOT CONVERGED]";
477 }
else if (converged_status(k) == BinningAnalysisParamsType::UNKNOWN_CONVERGENCE) {
480 str <<
" [UNKNOWN CONVERGENCE STATUS: " << converged_status(k) <<
"]";
490 dumpConvergenceAnalysis(ss);
505 template<
typename Params,
510 typename Params::ValueCalculator,
512 typename Params::BaseHistogramType
514 BinningAnalysis<typename Params::BinningAnalysisParamsType, LoggerType_>,
515 typename Params::Result
544 static constexpr
int NumTrackValuesCTime = Params::NumTrackValues;
546 static constexpr
int NumLevelsCTime = Params::NumLevels;
564 BinningAnalysisType binning_analysis;
573 const ValueCalculator & vcalc,
575 LoggerType & logger_)
576 : value_histogram(histogram_params, vcalc, logger_),
577 binning_analysis(histogram_params.num_bins, num_levels, logger_),
579 result(histogram_params, binning_analysis)
581 logger.longdebug(
"ValueHistogramWithBinningMHRWStatsCollector",
"constructor()");
590 inline const BinningAnalysisType & getBinningAnalysis()
const 592 return binning_analysis;
611 value_histogram.
init();
621 logger.longdebug(
"ValueHistogramWithBinningMHRWStatsCollector::done()",
"finishing up ...");
623 value_histogram.template done<false>();
625 const BaseHistogramType & h = value_histogram.
histogram();
626 result.hist.params = h.params;
627 CountRealAvgType normalization = h.bins.sum() + h.off_chart;
629 result.hist.bins = h.bins.template cast<CountRealAvgType>() / normalization;
630 result.error_levels = binning_analysis.
calcErrorLevels(result.hist.bins);
631 result.hist.delta = result.error_levels.col(binning_analysis.
numLevels()).
template cast<CountRealAvgType>();
632 result.hist.off_chart = h.off_chart / normalization;
636 logger.debug(
"ValueHistogramWithBinningMHRWStatsCollector", [&,
this](
std::ostream & str) {
637 str <<
"Binning analysis: bin sqmeans at different binning levels are:\n" 639 <<
"\t-> so the error bars at different binning levels are:\n" 640 << result.error_levels <<
"\n" 641 <<
"\t-> convergence analysis: \n";
642 result.dumpConvergenceAnalysis(str);
643 str <<
"\t... and just for you, here is the final histogram:\n" << result.hist.prettyPrint() <<
"\n";
648 template<
typename CountIntType2,
typename Po
intType,
typename LLHValueType,
typename MHRandomWalk>
649 inline void rawMove(CountIntType2 k,
bool is_thermalizing,
bool is_live_iter,
bool accepted,
650 double a,
const PointType & newpt, LLHValueType newptval,
651 const PointType & curpt, LLHValueType curptval,
MHRandomWalk & mh)
653 value_histogram.
rawMove(k, is_thermalizing, is_live_iter, accepted, a, newpt, newptval, curpt, curptval, mh);
657 template<
typename CountIntType2,
typename Po
intType,
typename LLHValueType,
typename MHRandomWalk>
658 inline void processSample(CountIntType2 k, CountIntType2 n,
const PointType & curpt,
683 template<
typename MHRWStatsCollector_>
686 typedef MHRWStatsCollector_ MHRWStatsCollector;
688 static constexpr
bool CanProvideStatus =
false;
700 template<
typename MHRWStatsCollector_>
708 template<
typename... Args>
713 static constexpr
int NumStatColl = MHRWStatsCollector::NumStatColl;
715 static constexpr
bool CanProvideStatus =
true;
717 template<int I = 0, typename std::enable_if<(I < NumStatColl), bool>::type dummy =
true>
720 typedef typename std::tuple_element<I, typename MHRWStatsCollector::MHRWStatsCollectorsTupleType>::type
727 + getStatus<I+1>(stats);
730 template<int I = 0, typename std::enable_if<(I == NumStatColl), bool>::type dummy =
true>
739 template<
typename... Args>
741 template<
typename... Args>
749 template<
typename ValueCalculator_,
750 typename LoggerType_,
751 typename HistogramType_
757 static constexpr
bool CanProvideStatus =
true;
759 static inline std::string getStatus(
const MHRWStatsCollector * stats)
761 const int maxbarwidth = 50;
765 return "Histogram: " + histogramShortBar<HistogramType>(stats->
histogram(),
true, maxbarwidth);
769 template<
typename ValueCalculator_,
770 typename LoggerType_,
771 typename HistogramType_
780 template<
typename Params_,
787 static constexpr
bool CanProvideStatus =
true;
789 static inline std::string getStatus(
const MHRWStatsCollector * stats)
791 const int maxbarwidth = 50;
794 const BaseHistogramType & histogram = stats->
histogram();
799 typedef typename BinningAnalysisType::ValueType ValueType;
800 const auto& binning_analysis = stats->getBinningAnalysis();
802 binmeans = histogram.bins.template cast<ValueType>() /
803 (ValueType)(histogram.bins.sum() + histogram.off_chart);
805 auto error_levels = binning_analysis.calcErrorLevels(binmeans);
806 auto conv_status = binning_analysis.determineErrorConvergence(error_levels);
812 if (conv_status(k) == BinningAnalysisType::CONVERGED) {
814 }
else if (conv_status(k) == BinningAnalysisType::NOT_CONVERGED) {
821 return tomo_internal::histogram_short_bar_fmt<BaseHistogramType>(histogram,
"", maxbarwidth)
822 +
Tools::fmts(
" err: (cnvg/?/fail) %d/%d/%d", n_cnvg, n_unknown, n_not_cnvg);
826 template<
typename Params_,
void dumpConvergenceAnalysis(std::ostream &str) const
Dump values, error bars and convergence status in human-readable form into ostream.
A StatsCollector which builds a histogram of values calculated with a ValueCalculator for each data s...
A Metropolis-Hastings Random Walk.
Result type of the corresponding ValueHistogramWithBinningMHRWStatsCollector.
Params::BaseHistogramType BaseHistogramType
See ValueHistogramWithBinningMHRWStatsCollectorParams::BaseHistogramType .
Result(HistogramParams p, const BinningAnalysisType &b)
Constructor which initializes the fields from the histogram and binning analysis type.
void done()
Finalize the data collection. Part of the MHRWStatsCollector Interface.
std::string dumpConvergenceAnalysis() const
Dump values, error bars and convergence status in human-readable form as string.
BinningAnalysis< BinningAnalysisParamsType, LoggerType > BinningAnalysisType
The corresponding BinningAnalysis type for this value histogram stats collector.
void done()
Part of the MHRWStatsCollector Interface.
Base namespace for the Tomographer project.
CountRealAvgType_ CountRealAvgType
Type used to store the averages of the histogram bins.
ValueHistogramMHRWStatsCollector(HistogramParams histogram_params, const ValueCalculator &vcalc, LoggerType &logger)
Simple constructor, initializes with the given values.
ValueCalculator::ValueType ValueType
The type to use to represent a calculated distance.
Eigen::ArrayXi converged_status
Information of convergence status of the error bars (see e.g. BinningAnalysisParamsType::CONVERGED) ...
Group template parameters for BinningAnalysis.
BinSumSqArray calcErrorLevels(const Eigen::ArrayBase< Derived > &means) const
Calculate the error bars of samples at different binning levels.
Eigen::ArrayXi determineErrorConvergence(const Eigen::Ref< const BinSumSqArray > &error_levels) const
Attempt to determine if the error bars have converged.
HistogramType::Params HistogramParams
The corresponding histogram params type.
Params::HistogramParams HistogramParams
See ValueHistogramWithBinningMHRWStatsCollectorParams::HistogramParams .
void processNewValues(const Eigen::DenseBase< Derived > &vals)
Process new raw samples.
ValueCalculator_ ValueCalculator
The type which calculates the interesting value. Should be of type interface ValueCalculator Interfac...
const Tools::StaticOrDynamic< int,(NumLevelsCTime==Eigen::Dynamic), NumLevelsCTime > numLevels
The number of levels in the binning analysis.
void thermalizingDone()
Part of the MHRWStatsCollector Interface. No-op.
Logger that discards all messages.
HistogramType hist
Histogram, already with error bars.
void rawMove(CountIntType2 k, bool is_thermalizing, bool is_live_iter, bool accepted, double a, const PointType &newpt, LLHValueType newptval, const PointType &curpt, LLHValueType curptval, MHRandomWalk &mh)
Part of the MHRWStatsCollector Interface. No-op.
UniformBinsHistogram< typename ValueCalculator::ValueType, CountIntType > BaseHistogramType
The Base Histogram Type.
const ResultType & getResult() const
Get the histogram data collected. This method is needed for Resultable Interface compliance.
Result()
Simple default constructor (e.g. to use as std::vector<Result>).
HistogramType::Params HistogramParams
Structure which holds the parameters of the histogram we're recording.
static constexpr int NumStatColl
The number of stats collectors we are tracking.
HistogramType_ ResultType
Required for compliance with Resultable Interface type.
BinningAnalysisParamsType::BinSumSqArray error_levels
Detailed error bars for all binning levels.
Traits-like class for ValueHistogramWithBinningMHRWStatsCollector.
std::size_t processSample(CountIntType k, CountIntType n, const PointType &curpt, LLHValueType, MHRandomWalk &)
Part of the MHRWStatsCollector Interface. Records the sample in the histogram.
Collect a histogram of values from a MH random walk, with binning analysis.
Params::ValueType ValueType
See ValueHistogramWithBinningMHRWStatsCollectorParams::ValueType .
const HistogramType & histogram() const
Get the histogram data collected so far. See HistogramType.
Params::BinningAnalysisParamsType BinningAnalysisParamsType
See ValueHistogramWithBinningMHRWStatsCollectorParams::BinningAnalysisParamsType .
Params::ValueCalculator ValueCalculator
See ValueHistogramWithBinningMHRWStatsCollectorParams::ValueCalculator .
void thermalizingDone()
Part of the MHRWStatsCollector Interface. No-op.
void rawMove(CountIntType k, bool, bool, bool, double, const PointType &, LLHValueType, const PointType &, LLHValueType, MHRandomWalk &)
Part of the MHRWStatsCollector Interface. No-op.
Params::CountRealAvgType CountRealAvgType
See ValueHistogramWithBinningMHRWStatsCollectorParams::CountRealAvgType .
const ResultType & getResult() const
Get the final histogram data. This method is needed for Resultable Interface compliance.
Some C++ utilities, with a tad of C++11 tricks.
HistogramType_ HistogramType
The type of the histogram. Usually a UniformBinsHistogram with ValueType range type.
static std::string getStatus(const MHRWStatsCollector *)
Prepare a string which reports the status of the given stats collector.
void init()
Part of the MHRWStatsCollector Interface. Initializes the histogram to zeros.
Template, specializable class to get status reports from stats collectors.
Params::CountIntType CountIntType
See ValueHistogramWithBinningMHRWStatsCollectorParams::CountIntType .
BinningAnalysisParams< ValueType, NumTrackValues, NumLevels, false, CountIntType > BinningAnalysisParamsType
The relevant BinningAnalysis parameters for us.
ValueCalculator_ ValueCalculator
The type of the ValueCalculator Interface which calculates the value of which we're collecting a hist...
void processSample(CountIntType2 k, CountIntType2 n, const PointType &curpt, LLHValueType curptval, MHRandomWalk &mh)
Part of the MHRWStatsCollector Interface. Records the sample in the histogram.
LoggerType_ LoggerType
Somewhere where this object may log what it's doing.
Definitions for Histogram Types.
UniformBinsHistogramWithErrorBars< typename ValueCalculator::ValueType, CountRealAvgType > HistogramType
The Final Histogram Type (with error bars).
Long Debug logging level.
Params::Result ResultType
See ValueHistogramWithBinningMHRWStatsCollectorParams::Result .
Binning Analysis in a Metropolis-Hastings random walk.
void init()
Part of the MHRWStatsCollector Interface. Initializes the histogram to zeros.
const BaseHistogramType & histogram() const
Get the histogram data collected so far. See BaseHistogramType .
ValueHistogramMHRWStatsCollector< ValueCalculator, LoggerType, BaseHistogramType > ValueHistogramMHRWStatsCollectorType
This is the natural ValueHistogramMHRWStatsCollector type on which we're adding error bars...
Utilities for logging messages.
auto getBinSqmeans() const
Get the raw average of the squared values observed, for each binning level.
ValueCalculator::ValueType ValueType
The type of a value calculated by the ValueCalculator Interface.
A simple MHRWStatsCollector interface which combines several stats collectors.
MultipleMHRWStatsCollectors TrivialMHRWStatsCollector
Trivial, NO-OP stats collector.
CountIntType_ CountIntType
Type used to count the number of hits in each bin.