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_,
264 HistogramType _histogram;
270 ValueCalculator _vcalc;
272 LoggerType & _logger;
277 const ValueCalculator & vcalc,
279 : _histogram(histogram_params),
324 template<
bool Pr
intHistogram = true>
327 if (PrintHistogram) {
330 _logger.longdebug(
"ValueHistogramMHRWStatsCollector",
331 "Done walking & collecting stats. Here's the histogram:\n" 332 + _histogram.prettyPrint());
338 template<
typename CountIntType,
typename Po
intType,
typename LLHValueType,
typename MHRandomWalk>
339 void rawMove(CountIntType k,
bool ,
bool ,
bool ,
340 double ,
const PointType & , LLHValueType ,
343 _logger.longdebug(
"ValueHistogramMHRWStatsCollector", [&](
std::ostream & stream) {
344 stream <<
"rawMove(): k=" << k;
349 template<
typename CountIntType,
typename Po
intType,
typename LLHValueType,
typename MHRandomWalk>
353 ValueType val = _vcalc.getValue(curpt);
355 _logger.longdebug(
"ValueHistogramMHRWStatsCollector", [&](
std::ostream & stream) {
356 stream <<
"in processSample(): " 357 <<
"k=" << k <<
", n=" << n <<
", val=" << val
358 <<
" [with ValueType=" <<
typeid(ValueType).name() <<
"]" ;
361 return _histogram.record(val);
377 template<
typename HistogramType_,
typename BinningAnalysisParamsType_>
381 typename BinningAnalysisParamsType_::BinSumSqArray
384 typedef HistogramType_ HistogramType;
385 typedef typename HistogramType::Params HistogramParams;
386 typedef BinningAnalysisParamsType_ BinningAnalysisParamsType;
390 : hist(), error_levels(), converged_status()
395 template<
typename EigenDerived1,
typename EigenDerived2>
399 : hist(hist_), error_levels(error_levels_), converged_status(converged_status_)
404 template<
typename BinningAnalysisType>
407 error_levels(b.numTrackValues(), b.numLevels()+1),
408 converged_status(
Eigen::ArrayXi::Constant(b.numTrackValues(), BinningAnalysisType::UNKNOWN_CONVERGENCE))
410 tomographer_assert(converged_status.rows() == b.numTrackValues() && converged_status.cols() == 1);
436 for (
int k = 0; k < converged_status.size(); ++k) {
437 str <<
"\tval[" <<
std::setw(3) << k <<
"] = " 439 <<
" +- " <<
std::setw(12) << hist.delta(k);
440 if (converged_status(k) == BinningAnalysisParamsType::CONVERGED) {
441 str <<
" [CONVERGED]";
442 }
else if (converged_status(k) == BinningAnalysisParamsType::NOT_CONVERGED) {
443 str <<
" [NOT CONVERGED]";
444 }
else if (converged_status(k) == BinningAnalysisParamsType::UNKNOWN_CONVERGENCE) {
447 str <<
" [UNKNOWN CONVERGENCE STATUS: " << converged_status(k) <<
"]";
457 dumpConvergenceAnalysis(ss);
473 template<
typename ValueCalculator_,
474 typename CountIntType_ = int,
475 typename CountRealAvgType_ = double,
476 int NumTrackValues_ = Eigen::Dynamic,
477 int NumLevels_ = Eigen::Dynamic
490 static constexpr
int NumTrackValues = NumTrackValues_;
492 static constexpr
int NumLevels = NumLevels_;
528 template<
typename Params,
533 typename Params::ValueCalculator,
535 typename Params::BaseHistogramType
537 BinningAnalysis<typename Params::BinningAnalysisParamsType, LoggerType_>,
538 typename Params::Result
567 static constexpr
int NumTrackValuesCTime = Params::NumTrackValues;
569 static constexpr
int NumLevelsCTime = Params::NumLevels;
587 BinningAnalysisType binning_analysis;
596 const ValueCalculator & vcalc,
598 LoggerType & logger_)
599 : value_histogram(histogram_params, vcalc, logger_),
600 binning_analysis(histogram_params.num_bins, num_levels, logger_),
602 result(histogram_params, binning_analysis)
604 logger.longdebug(
"ValueHistogramWithBinningMHRWStatsCollector",
"constructor()");
613 inline const BinningAnalysisType & getBinningAnalysis()
const 615 return binning_analysis;
634 value_histogram.
init();
644 logger.longdebug(
"ValueHistogramWithBinningMHRWStatsCollector::done()",
"finishing up ...");
646 value_histogram.template done<false>();
657 const BaseHistogramType & h = value_histogram.
histogram();
658 result.hist.params = h.params;
659 CountRealAvgType numsamples = h.bins.sum() + h.off_chart;
660 result.hist.bins = h.bins.template cast<CountRealAvgType>() / numsamples;
661 result.error_levels = binning_analysis.
calcErrorLevels(result.hist.bins);
662 result.hist.delta = result.error_levels.col(binning_analysis.
numLevels()).
template cast<CountRealAvgType>();
663 result.hist.off_chart = h.off_chart / numsamples;
667 logger.debug(
"ValueHistogramWithBinningMHRWStatsCollector", [&,
this](
std::ostream & str) {
668 str <<
"Binning analysis: bin sqmeans at different binning levels are:\n" 670 <<
"\t-> so the error bars at different binning levels are:\n" 671 << result.error_levels <<
"\n" 672 <<
"\t-> convergence analysis: \n";
673 result.dumpConvergenceAnalysis(str);
674 str <<
"\t... and just for you, here is the final histogram:\n" << result.hist.prettyPrint() <<
"\n";
679 template<
typename CountIntType2,
typename Po
intType,
typename LLHValueType,
typename MHRandomWalk>
680 inline void rawMove(CountIntType2 k,
bool is_thermalizing,
bool is_live_iter,
bool accepted,
681 double a,
const PointType & newpt, LLHValueType newptval,
682 const PointType & curpt, LLHValueType curptval,
MHRandomWalk & mh)
684 value_histogram.
rawMove(k, is_thermalizing, is_live_iter, accepted, a, newpt, newptval, curpt, curptval, mh);
688 template<
typename CountIntType2,
typename Po
intType,
typename LLHValueType,
typename MHRandomWalk>
689 inline void processSample(CountIntType2 k, CountIntType2 n,
const PointType & curpt,
714 template<
typename MHRWStatsCollector_>
717 typedef MHRWStatsCollector_ MHRWStatsCollector;
719 static constexpr
bool CanProvideStatus =
false;
731 template<
typename MHRWStatsCollector_>
739 template<
typename... Args>
744 static constexpr
int NumStatColl = MHRWStatsCollector::NumStatColl;
746 static constexpr
bool CanProvideStatus =
true;
748 template<int I = 0, typename std::enable_if<(I < NumStatColl), bool>::type dummy =
true>
751 typedef typename std::tuple_element<I, typename MHRWStatsCollector::MHRWStatsCollectorsTupleType>::type
758 + getStatus<I+1>(stats);
761 template<int I = 0, typename std::enable_if<(I == NumStatColl), bool>::type dummy =
true>
770 template<
typename... Args>
772 template<
typename... Args>
780 template<
typename ValueCalculator_,
781 typename LoggerType_,
782 typename HistogramType_
788 static constexpr
bool CanProvideStatus =
true;
790 static inline std::string getStatus(
const MHRWStatsCollector * stats)
792 const int maxbarwidth = 50;
796 return "Histogram: " + histogramShortBar<HistogramType>(stats->
histogram(),
true, maxbarwidth);
800 template<
typename ValueCalculator_,
801 typename LoggerType_,
802 typename HistogramType_
811 template<
typename Params_,
818 static constexpr
bool CanProvideStatus =
true;
820 static inline std::string getStatus(
const MHRWStatsCollector * stats)
822 const int maxbarwidth = 50;
825 const BaseHistogramType & histogram = stats->
histogram();
830 typedef typename BinningAnalysisType::ValueType ValueType;
831 const auto& binning_analysis = stats->getBinningAnalysis();
833 binmeans = histogram.bins.template cast<ValueType>() /
834 (ValueType)(histogram.bins.sum() + histogram.off_chart);
836 auto error_levels = binning_analysis.calcErrorLevels(binmeans);
837 auto conv_status = binning_analysis.determineErrorConvergence(error_levels);
843 if (conv_status(k) == BinningAnalysisType::CONVERGED) {
845 }
else if (conv_status(k) == BinningAnalysisType::NOT_CONVERGED) {
852 return tomo_internal::histogram_short_bar_fmt<BaseHistogramType>(histogram,
"", maxbarwidth)
853 +
Tools::fmts(
" err: (cnvg/?/fail) %d/%d/%d", n_cnvg, n_unknown, n_not_cnvg);
857 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.
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.