<commit_before>/// HEADER
#include "evaluate_binary_classifier.h"

/// PROJECT
#include <csapex/msg/io.h>
#include <csapex/utility/register_apex_plugin.h>
#include <csapex/param/parameter_factory.h>
#include <csapex/model/node_modifier.h>

CSAPEX_REGISTER_CLASS(csapex::EvaluateBinaryClassifier, csapex::Node)

using namespace csapex;
using namespace csapex::connection_types;


EvaluateBinaryClassifier::EvaluateBinaryClassifier()
{
}

void EvaluateBinaryClassifier::setupParameters(Parameterizable& parameters)
{
    parameters.addParameter(param::ParameterFactory::declareValue("positive class label", 1),
                            positive_class_label_);
    parameters.addParameter(param::ParameterFactory::declareValue("negative class label", 0),
                            negative_class_label_);

}

void EvaluateBinaryClassifier::setup(NodeModifier& node_modifier)
{
    in_ = node_modifier.addInput<ConfusionMatrixMessage>("Confusion Matrix");
}

void EvaluateBinaryClassifier::process()
{
    connection_types::ConfusionMatrixMessage::ConstPtr msg = msg::getMessage<connection_types::ConfusionMatrixMessage>(in_);

    const ConfusionMatrix& cm = msg->confusion;

    if(cm.classes.size() != 2) {
        node_modifier_->setWarning("needs a confusion matrix with exactly 2 classes");
        return;
    }

    node_modifier_->setNoError();

    {
        std::unique_lock<std::recursive_mutex> lock(mutex_);
        metrics_.clear();

        int tp = cm.histogram.at(std::make_pair(positive_class_label_, positive_class_label_));
        int tn = cm.histogram.at(std::make_pair(negative_class_label_, negative_class_label_));
        int fp = cm.histogram.at(std::make_pair(negative_class_label_, positive_class_label_));
        int fn = cm.histogram.at(std::make_pair(positive_class_label_, negative_class_label_));
        int p = tp + fn;
        int n = tn + fp;

        metrics_.push_back(Metric("Sensitivity", "True positive rate, hit rate, recall", 0.0, 1.0,<commit_msg>evaluation: allow more than two classes in a confusion matrix<commit_after>/// HEADER
#include "evaluate_binary_classifier.h"

/// PROJECT
#include <csapex/msg/io.h>
#include <csapex/utility/register_apex_plugin.h>
#include <csapex/param/parameter_factory.h>
#include <csapex/model/node_modifier.h>

CSAPEX_REGISTER_CLASS(csapex::EvaluateBinaryClassifier, csapex::Node)

using namespace csapex;
using namespace csapex::connection_types;


EvaluateBinaryClassifier::EvaluateBinaryClassifier()
{
}

void EvaluateBinaryClassifier::setupParameters(Parameterizable& parameters)
{
    parameters.addParameter(param::ParameterFactory::declareValue("positive class label", 1),
                            positive_class_label_);
    parameters.addParameter(param::ParameterFactory::declareValue("negative class label", 0),
                            negative_class_label_);

}

void EvaluateBinaryClassifier::setup(NodeModifier& node_modifier)
{
    in_ = node_modifier.addInput<ConfusionMatrixMessage>("Confusion Matrix");
}

void EvaluateBinaryClassifier::process()
{
    connection_types::ConfusionMatrixMessage::ConstPtr msg = msg::getMessage<connection_types::ConfusionMatrixMessage>(in_);

    const ConfusionMatrix& cm = msg->confusion;

    if (cm.classes.size() < 2)
    {
        node_modifier_->setWarning("The confusion matrix must have at least 2 classes");
        return;
    }
    else if (cm.classes.size() > 2)
        node_modifier_->setWarning("The confusion matrix should only have 2 classes. Results may be inaccurate.");
    else
        node_modifier_->setNoError();

    {
        std::unique_lock<std::recursive_mutex> lock(mutex_);
        metrics_.clear();

        int tp = cm.histogram.at(std::make_pair(positive_class_label_, positive_class_label_));
        int tn = cm.histogram.at(std::make_pair(negative_class_label_, negative_class_label_));
        int fp = cm.histogram.at(std::make_pair(negative_class_label_, positive_class_label_));
        int fn = cm.histogram.at(std::make_pair(positive_class_label_, negative_class_label_));
        int p = tp + fn;
        int n = tn + fp;

        metrics_.push_back(Metric("Sensitivity", "True positive rate, hit rate, recall", 0.0, 1.0,
{'fraction_non_alphanumeric': 0.11362015688138816, 'fraction_numerical': 0.004278583313525077, 'mean_word_length': 4.493472584856397, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4224732', 'n_tokens_mistral': 1300, 'n_tokens_neox': 1255, 'n_words': 223}