28 #ifndef TOMOGRAPHER_MHRWSTATSCOLLECTORS_H 29 #define TOMOGRAPHER_MHRWSTATSCOLLECTORS_H 36 #include <type_traits> 87 template<
typename... MHRWStatsCollectors>
95 static constexpr
int NumStatColl =
sizeof...(MHRWStatsCollectors);
103 : statscollectors(statscollectors_...)
109 inline const typename std::tuple_element<I, MHRWStatsCollectorsTupleType>::type & getStatsCollector()
const 111 return std::get<I>(statscollectors);
119 std::get<I>(statscollectors).init();
132 std::get<I>(statscollectors).thermalizingDone();
133 thermalizingDone<I+1>();
145 std::get<I>(statscollectors).done();
156 template<
typename CountIntType,
typename Po
intType,
typename FnValueType,
typename MHRandomWalk,
int I = 0>
158 CountIntType k,
bool is_thermalizing,
bool is_live_iter,
bool accepted,
159 double a,
const PointType & newpt, FnValueType newptval,
160 const PointType & curpt, FnValueType curptval,
164 std::get<I>(statscollectors).rawMove(
165 k, is_thermalizing, is_live_iter, accepted, a,
166 newpt, newptval, curpt, curptval, rw
168 rawMove<CountIntType, PointType, FnValueType, MHRandomWalk, I+1>(
169 k, is_thermalizing, is_live_iter, accepted, a,
170 newpt, newptval, curpt, curptval, rw
173 template<
typename CountIntType,
typename Po
intType,
typename FnValueType,
typename MHRandomWalk,
int I = 0>
175 CountIntType,
bool,
bool,
bool,
double,
const PointType &, FnValueType,
184 template<
typename CountIntType,
typename Po
intType,
typename FnValueType,
typename MHRandomWalk,
int I = 0>
186 CountIntType k, CountIntType n,
const PointType & curpt, FnValueType curptval,
MHRandomWalk & rw
189 std::get<I>(statscollectors).processSample(k, n, curpt, curptval, rw);
190 processSample<CountIntType, PointType, FnValueType, MHRandomWalk, I+1>(k, n, curpt, curptval, rw);
193 template<
typename CountIntType,
typename Po
intType,
typename FnValueType,
typename MHRandomWalk,
int I = 0>
195 CountIntType, CountIntType,
const PointType &, FnValueType,
MHRandomWalk &
235 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);
376 template<
typename HistogramType_,
typename BinningAnalysisParamsType_>
380 typename BinningAnalysisParamsType_::BinSumSqArray
383 typedef HistogramType_ HistogramType;
384 typedef typename HistogramType::Params HistogramParams;
385 typedef BinningAnalysisParamsType_ BinningAnalysisParamsType;
389 : hist(), error_levels(), converged_status()
394 template<
typename EigenDerived1,
typename EigenDerived2>
398 : hist(hist_), error_levels(error_levels_), converged_status(converged_status_)
403 template<
typename BinningAnalysisType>
406 error_levels(b.numTrackValues(), b.numLevels()+1),
407 converged_status(
Eigen::ArrayXi::Constant(b.numTrackValues(), BinningAnalysisType::UNKNOWN_CONVERGENCE))
409 tomographer_assert(converged_status.rows() == b.numTrackValues() && converged_status.cols() == 1);
435 for (
int k = 0; k < converged_status.size(); ++k) {
436 str <<
"\tval[" <<
std::setw(3) << k <<
"] = " 438 <<
" +- " <<
std::setw(12) << hist.delta(k);
439 if (converged_status(k) == BinningAnalysisParamsType::CONVERGED) {
440 str <<
" [CONVERGED]";
441 }
else if (converged_status(k) == BinningAnalysisParamsType::NOT_CONVERGED) {
442 str <<
" [NOT CONVERGED]";
443 }
else if (converged_status(k) == BinningAnalysisParamsType::UNKNOWN_CONVERGENCE) {
446 str <<
" [UNKNOWN CONVERGENCE STATUS: " << converged_status(k) <<
"]";
456 dumpConvergenceAnalysis(ss);
472 template<
typename ValueCalculator_,
473 typename CountIntType_ = int,
474 typename CountRealAvgType_ = double,
475 int NumTrackValues_ = Eigen::Dynamic,
476 int NumLevels_ = Eigen::Dynamic
489 static constexpr
int NumTrackValues = NumTrackValues_;
491 static constexpr
int NumLevels = NumLevels_;
527 template<
typename Params,
532 typename Params::ValueCalculator,
534 typename Params::BaseHistogramType
536 BinningAnalysis<typename Params::BinningAnalysisParamsType, LoggerType_>,
537 typename Params::Result
566 static constexpr
int NumTrackValuesCTime = Params::NumTrackValues;
568 static constexpr
int NumLevelsCTime = Params::NumLevels;
586 BinningAnalysisType binning_analysis;
595 const ValueCalculator & vcalc,
597 LoggerType & logger_)
598 : value_histogram(histogram_params, vcalc, logger_),
599 binning_analysis((int)histogram_params.num_bins, num_levels, logger_),
601 result(histogram_params, binning_analysis)
603 logger.longdebug(
"ValueHistogramWithBinningMHRWStatsCollector",
"constructor()");
612 inline const BinningAnalysisType & getBinningAnalysis()
const 614 return binning_analysis;
633 value_histogram.
init();
643 logger.longdebug(
"ValueHistogramWithBinningMHRWStatsCollector::done()",
"finishing up ...");
645 value_histogram.template done<false>();
656 const BaseHistogramType & h = value_histogram.
histogram();
657 result.hist.params = h.params;
658 CountRealAvgType numsamples = h.bins.sum() + h.off_chart;
659 result.hist.bins = h.bins.template cast<CountRealAvgType>() / numsamples;
660 result.error_levels = binning_analysis.
calcErrorLevels(result.hist.bins);
661 result.hist.delta = result.error_levels.col(binning_analysis.
numLevels()).
template cast<CountRealAvgType>();
662 result.hist.off_chart = h.off_chart / numsamples;
666 logger.debug(
"ValueHistogramWithBinningMHRWStatsCollector", [&,
this](
std::ostream & str) {
667 str <<
"Binning analysis: bin sqmeans at different binning levels are:\n" 669 <<
"\t-> so the error bars at different binning levels are:\n" 670 << result.error_levels <<
"\n" 671 <<
"\t-> convergence analysis: \n";
672 result.dumpConvergenceAnalysis(str);
673 str <<
"\t... and just for you, here is the final histogram:\n" << result.hist.prettyPrint() <<
"\n";
678 template<
typename CountIntType2,
typename Po
intType,
typename LLHValueType,
typename MHRandomWalk>
679 inline void rawMove(CountIntType2 k,
bool is_thermalizing,
bool is_live_iter,
bool accepted,
680 double a,
const PointType & newpt, LLHValueType newptval,
681 const PointType & curpt, LLHValueType curptval,
MHRandomWalk & mh)
683 value_histogram.
rawMove(k, is_thermalizing, is_live_iter, accepted, a, newpt, newptval, curpt, curptval, mh);
687 template<
typename CountIntType2,
typename Po
intType,
typename LLHValueType,
typename MHRandomWalk>
688 inline void processSample(CountIntType2 k, CountIntType2 n,
const PointType & curpt,
713 template<
typename MHRWStatsCollector_>
716 typedef MHRWStatsCollector_ MHRWStatsCollector;
718 static constexpr
bool CanProvideStatus =
false;
730 template<
typename MHRWStatsCollector_>
738 template<
typename... Args>
743 static constexpr
int NumStatColl = MHRWStatsCollector::NumStatColl;
745 static constexpr
bool CanProvideStatus =
true;
747 template<int I = 0, typename std::enable_if<(I < NumStatColl), bool>::type dummy =
true>
750 typedef typename std::tuple_element<I, typename MHRWStatsCollector::MHRWStatsCollectorsTupleType>::type
757 + getStatus<I+1>(stats);
760 template<int I = 0, typename std::enable_if<(I == NumStatColl), bool>::type dummy =
true>
769 template<
typename... Args>
771 template<
typename... Args>
779 template<
typename ValueCalculator_,
780 typename LoggerType_,
781 typename HistogramType_
787 static constexpr
bool CanProvideStatus =
true;
789 static inline std::string getStatus(
const MHRWStatsCollector * stats)
791 const int maxbarwidth = 50;
795 return "Histogram: " + histogramShortBar<HistogramType>(stats->
histogram(),
true, maxbarwidth);
799 template<
typename ValueCalculator_,
800 typename LoggerType_,
801 typename HistogramType_
810 template<
typename Params_,
817 static constexpr
bool CanProvideStatus =
true;
819 static inline std::string getStatus(
const MHRWStatsCollector * stats)
821 const int maxbarwidth = 50;
824 const BaseHistogramType & histogram = stats->
histogram();
829 typedef typename BinningAnalysisType::ValueType ValueType;
830 const auto& binning_analysis = stats->getBinningAnalysis();
832 binmeans = histogram.bins.template cast<ValueType>() /
833 (ValueType)(histogram.bins.sum() + histogram.off_chart);
835 auto error_levels = binning_analysis.calcErrorLevels(binmeans);
836 auto conv_status = binning_analysis.determineErrorConvergence(error_levels);
842 if (conv_status(k) == BinningAnalysisType::CONVERGED) {
844 }
else if (conv_status(k) == BinningAnalysisType::NOT_CONVERGED) {
851 return tomo_internal::histogram_short_bar_fmt<BaseHistogramType>(histogram,
"", maxbarwidth)
852 +
Tools::fmts(
" err: (cnvg/?/fail) %d/%d/%d", n_cnvg, n_unknown, n_not_cnvg);
856 template<
typename Params_,
A StatsCollector which builds a histogram of values calculated with a ValueCalculator for each data s...
A Metropolis-Hastings Random Walk.
Params::BaseHistogramType BaseHistogramType
See ValueHistogramWithBinningMHRWStatsCollectorParams::BaseHistogramType .
void done()
Finalize the data collection. Part of the MHRWStatsCollector Interface.
BinningAnalysis< BinningAnalysisParamsType, LoggerType > BinningAnalysisType
The corresponding BinningAnalysis type for this value histogram stats collector.
auto getBinSqmeans() const
Get the raw average of the squared values observed, for each binning level.
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.
const BaseHistogramType & histogram() const
Get the histogram data collected so far. See BaseHistogramType .
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.
Eigen::ArrayXi determineErrorConvergence(const Eigen::Ref< const BinSumSqArray > &error_levels) const
Attempt to determine if the error bars have converged.
Result type of a ValueHistogramWithBinningMHRWStatsCollector.
HistogramType::Params HistogramParams
The corresponding histogram params type.
ValueHistogramWithBinningMHRWStatsCollectorResult(HistogramParams p, const BinningAnalysisType &b)
Constructor which initializes the fields from the histogram and binning analysis 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.
HistogramType::Params HistogramParams
Structure which holds the parameters of the histogram we're recording.
ValueHistogramWithBinningMHRWStatsCollectorResult(const HistogramType &hist_, const Eigen::DenseBase< EigenDerived1 > &error_levels_, const Eigen::DenseBase< EigenDerived2 > &converged_status_)
Simple constructor with direct initialization of fields.
static constexpr int NumStatColl
The number of stats collectors we are tracking.
HistogramType_ ResultType
Required for compliance with Resultable Interface type.
const ResultType & getResult() const
Get the histogram data collected. This method is needed for Resultable Interface compliance.
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.
BinSumSqArray calcErrorLevels(const Eigen::ArrayBase< Derived > &means) const
Calculate the error bars of samples at different binning levels.
const HistogramType & histogram() const
Get the histogram data collected so far. See HistogramType.
Params::ValueType ValueType
See ValueHistogramWithBinningMHRWStatsCollectorParams::ValueType .
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.
ValueHistogramWithBinningMHRWStatsCollectorResult< HistogramType, BinningAnalysisParamsType > Result
Result type of the corresponding ValueHistogramWithBinningMHRWStatsCollector.
Params::CountRealAvgType CountRealAvgType
See ValueHistogramWithBinningMHRWStatsCollectorParams::CountRealAvgType .
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.
std::string dumpConvergenceAnalysis() const
Dump values, error bars and convergence status in human-readable form as string.
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.
void dumpConvergenceAnalysis(std::ostream &str) const
Dump values, error bars and convergence status in human-readable form into ostream.
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.
ValueHistogramWithBinningMHRWStatsCollectorResult()
Simple default constructor (e.g. to use as std::vector<Result>).
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 ResultType & getResult() const
Get the final histogram data. This method is needed for Resultable Interface compliance.
#define tomographer_assert(...)
Assertion test macro.
BinningAnalysisParamsType::BinSumSqArray error_levels
Detailed error bars for all binning levels.
ValueHistogramMHRWStatsCollector< ValueCalculator, LoggerType, BaseHistogramType > ValueHistogramMHRWStatsCollectorType
This is the natural ValueHistogramMHRWStatsCollector type on which we're adding error bars...
Utilities for logging messages.
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.