7 #include "spdlog/fmt/fmt.h"
10 #include <boost/predef.h>
30 for(
long j = 0; j <
m_K; ++j) {
38 for(
const auto& lbl : labels) {
61 base.TrueNegatives =
m_InstanceCount - base.TruePositives - base.FalsePositives - base.FalseNegatives;
66 return std::make_unique<ConfusionMatrixRecorder>(
num_labels(),
m_K);
93 std::vector<double> weights;
95 std::fill_n(std::back_inserter(weights), k, 1.0 /
static_cast<double>(k));
115 #if BOOST_COMP_GNUC && BOOST_COMP_GNUC <= BOOST_VERSION_NUMBER(10, 0, 0)
116 #pragma GCC push_options
117 #pragma GCC optimize("-O1")
122 for(
long j = 0; j <
m_K; ++j) {
129 long step = std::min(
m_K, (
long)labels.size());
135 #if BOOST_COMP_GNUC && BOOST_COMP_GNUC <= BOOST_VERSION_NUMBER(10, 0, 0)
136 #pragma GCC pop_options
152 double correct = 0.0;
153 for(
long j = 0; j <
m_K; ++j) {
171 m_Name(std::move(name)), m_Metric(metric) {
185 if(
recall(cm) > threshold) {
200 __builtin_unreachable();
205 #define IMPLEMENT_ADD_METRIC(METRIC, SHORTHAND) \
206 void MacroMetricReporter::add_##METRIC(ReductionType reduction, std::string name) { \
207 auto fn = [](const ConfusionMatrix& cm){ return METRIC(cm); }; \
208 add_reduction_helper(std::move(name), "{}" SHORTHAND "@{}", reduction, fn); \
227 name = fmt::format(
"{}F{}@{}",
reduction_name(reduction), beta, m_ConfusionMatrix->get_k());
234 add_reduction(std::move(name), reduction, fn);
265 if(confusion ==
nullptr) {
266 THROW_EXCEPTION(std::invalid_argument,
"ConfusionMatrixRecorder cannot be null");
271 std::vector<metric_t> metric;
274 metric.emplace_back(red.first, 0.0);
290 metric[i].second /= normalize;
294 if(metric[i].second != 0) {
295 metric[i].second = std::numeric_limits<double>::quiet_NaN();
301 metric.emplace_back(name, fn(micro));
307 #ifndef DOCTEST_CONFIG_DISABLE
315 auto vec = std::vector<dismec::label_id_t>{};
316 vec.reserve(init_list.size());
317 for(
const auto& i : init_list) {
327 std::vector<sTrueLabelInfo> true_info;
328 std::vector<sPredLabelInfo> pred_info;
330 target.update(pred_info, true_info);
340 CHECK(pat3.value() == 0.0);
342 CHECK(pat3.value() == 1.0 / 3.0);
344 CHECK(pat3.value() == 3.0 / 6.0);
346 CHECK(pat3.value() == 3.0 / 9.0);
348 CHECK(pat3.value() == 3.0 / 12.0);
350 CHECK(pat3.value() == 6.0 / 15.0);
358 CHECK(aat3.value() == 0.0);
360 CHECK(aat3.value() == 1.0 / 1.0);
362 CHECK(aat3.value() == 2.0 / 2.0);
364 CHECK(aat3.value() == 2.0 / 3.0);
366 CHECK(aat3.value() == 2.0 / 4.0);
368 CHECK(aat3.value() == 3.0 / 5.0);
Strong typedef for an int to signify a label id.
constexpr T to_index() const
! Explicitly convert to an integer.
void update(const pd_info_vec &prediction, const gt_info_vec &labels) override
std::unique_ptr< MetricCollectionInterface > clone() const override
AbandonmentAtK(long num_labels, long k)
void reduce(const MetricCollectionInterface &other) override
ConfusionMatrixRecorder(long num_labels, long k)
std::unique_ptr< MetricCollectionInterface > clone() const override
std::vector< ConfusionMatrix > m_Confusion
void update(const pd_info_vec &prediction, const gt_info_vec &labels) override
ConfusionMatrix get_confusion_matrix(label_id_t label) const
static void process_prediction(const std::vector< label_id_t > &raw_labels, const prediction_t &raw_prediction, std::vector< sTrueLabelInfo > &proc_labels, std::vector< sPredLabelInfo > &proc_pred)
InstanceAveragedMetric(long num_labels)
KahanAccumulator< double > m_Accumulator
void reduce(const MetricCollectionInterface &other) override
void accumulate(double value)
std::unique_ptr< MetricCollectionInterface > clone() const override
void update(const pd_info_vec &prediction, const gt_info_vec &labels) override
std::vector< double > m_Cumulative
InstanceRankedPositives(long num_labels, long k, bool normalize=false)
std::vector< double > m_Weights
std::vector< metric_t > get_values() const override
InstanceWiseMetricReporter(std::string name, const InstanceAveragedMetric *metric)
const InstanceAveragedMetric * m_Metric
void add_coverage(double threshold, std::string name={})
MacroMetricReporter(const ConfusionMatrixRecorder *confusion)
void add_confusion_matrix()
std::vector< metric_t > get_values() const override
const ConfusionMatrixRecorder * m_ConfusionMatrix
void add_reduction(std::string name, ReductionType type, std::function< double(const ConfusionMatrix &)>)
std::vector< std::pair< std::string, reduction_fn > > m_MacroReductions
std::vector< std::pair< std::string, reduction_fn > > m_MicroReductions
void add_reduction_helper(std::string name, const char *pattern, ReductionType type, std::function< double(const ConfusionMatrix &)> fn)
Base class for all metrics that can be calculated during the evaluation phase.
long num_labels() const
Gets the number of labels.
std::vector< sPredLabelInfo > pd_info_vec
std::vector< sTrueLabelInfo > gt_info_vec
MetricCollectionInterface(long num_labels)
std::pair< std::string, double > metric_t
TEST_CASE("precision_at_k")
#define IMPLEMENT_ADD_METRIC(METRIC, SHORTHAND)
std::vector< double > uniform_weights(long k)
Eigen::Matrix< long, 1, Eigen::Dynamic > pred_mat_t
auto make_labels(std::initializer_list< long > init_list)
void update_metric(T &target, std::initializer_list< long > prediction, std::initializer_list< long > labels)
constexpr const char * reduction_name(MacroMetricReporter::ReductionType type)
constexpr double positive_likelihood_ratio(const ConfusionMatrixBase< T > &matrix)
constexpr double matthews(const ConfusionMatrixBase< T > &matrix)
constexpr double diagnostic_odds_ratio(const ConfusionMatrixBase< T > &matrix)
constexpr double negative_predictive_value(const ConfusionMatrixBase< T > &matrix)
constexpr double fowlkes_mallows(const ConfusionMatrixBase< T > &matrix)
constexpr double precision(const ConfusionMatrixBase< T > &matrix)
constexpr double markedness(const ConfusionMatrixBase< T > &matrix)
constexpr double balanced_accuracy(const ConfusionMatrixBase< T > &matrix)
constexpr double informedness(const ConfusionMatrixBase< T > &matrix)
constexpr double f_beta(const ConfusionMatrixBase< T > &matrix, double beta)
constexpr double specificity(const ConfusionMatrixBase< T > &matrix)
constexpr double recall(const ConfusionMatrixBase< T > &matrix)
constexpr double accuracy(const ConfusionMatrixBase< T > &matrix)
constexpr double negative_likelihood_ratio(const ConfusionMatrixBase< T > &matrix)
constexpr auto ssize(const C &c) -> std::common_type_t< std::ptrdiff_t, std::make_signed_t< decltype(c.size())>>
signed size free function. Taken from https://en.cppreference.com/w/cpp/iterator/size
#define ALWAYS_ASSERT_EQUAL(x, y, msg)
#define THROW_EXCEPTION(exception_type,...)