<commit_before>/*
 * EvaluationMetric.cpp
 *
 *  Created on: 17.03.2015
 *      Author: <NAME> (<EMAIL>)
 */

#include "EvaluationMetric.h"

namespace NetworKit {

EvaluationMetric::EvaluationMetric() : testGraph(nullptr) {
}

EvaluationMetric::EvaluationMetric(const Graph& testGraph) : testGraph(&testGraph) {
}

void EvaluationMetric::setTestGraph(const Graph& newTestGraph) {
  testGraph = &newTestGraph;
}

std::pair<std::vector<double>, std::vector<double>> EvaluationMetric::getCurve(std::vector<LinkPredictor::node_dyad_score_pair> predictions, count numThresholds) {
  if (testGraph == nullptr) {
    throw std::logic_error("Set testGraph first.");
  } else if (predictions.size() == 0) {
    throw std::logic_error("predictions.size() == 0");
  } else if (numThresholds < 2) {
    throw std::invalid_argument("numThresholds < 2: At least 2 thresholds needed for curve.");
  }
  // Don't overshoot with the number of thresholds being greater than the number of predictions + 1.
  if (predictions.size() + 1 < numThresholds || numThresholds == 0) {
    numThresholds = predictions.size() + 1;
  }
  thresholds.clear();
  thresholds.reserve(numThresholds);
  count numPredictions = predictions.size();
  for (index i = 0; i < numThresholds; ++i) {
    // Percentile calculation through nearest rank method.
    thresholds.push_back(std::ceil(numPredictions * (1.0 * i / (numThresholds - 1))));
  }
  // The extraction of statistical measures requires sorted predictions
  LinkPredictor::sortByScore(predictions);
  this->predictions = predictions;
  calculateStatisticalMeasures();
  generatedPoints = generatePoints();
  return generatedPoints;<commit_msg>Fixed numerically instable calculation.<commit_after>/*
 * EvaluationMetric.cpp
 *
 *  Created on: 17.03.2015
 *      Author: <NAME> (<EMAIL>)
 */

#include "EvaluationMetric.h"

namespace NetworKit {

EvaluationMetric::EvaluationMetric() : testGraph(nullptr) {
}

EvaluationMetric::EvaluationMetric(const Graph& testGraph) : testGraph(&testGraph) {
}

void EvaluationMetric::setTestGraph(const Graph& newTestGraph) {
  testGraph = &newTestGraph;
}

std::pair<std::vector<double>, std::vector<double>> EvaluationMetric::getCurve(std::vector<LinkPredictor::node_dyad_score_pair> predictions, count numThresholds) {
  if (testGraph == nullptr) {
    throw std::logic_error("Set testGraph first.");
  } else if (predictions.size() == 0) {
    throw std::logic_error("predictions.size() == 0");
  } else if (numThresholds < 2) {
    throw std::invalid_argument("numThresholds < 2: At least 2 thresholds needed for curve.");
  }
  // Don't overshoot with the number of thresholds being greater than the number of predictions + 1.
  if (predictions.size() + 1 < numThresholds || numThresholds == 0) {
    numThresholds = predictions.size() + 1;
  }
  std::set<index> thresholdSet;
  count numPredictions = predictions.size();
  for (index i = 0; i < numThresholds; ++i) {
    // Percentile calculation through nearest rank method.
    // This calculation is numerically instable. This is why we use a set to make sure that
    // we obtain pairwise different thresholds.
    thresholdSet.insert(std::ceil(numPredictions * (1.0 * i / (numThresholds - 1))));
  }
  thresholds.assign(thresholdSet.begin(), thresholdSet.end());
  // The extraction of statistical measures requires sorted predictions
  LinkPredictor::sortByScore(predictions);
  this->predictions = predictions;
  calculateStatisticalMeasures();
  generatedPoints = generatePoints();
  return generatedPoints;
{'fraction_non_alphanumeric': 0.12779824312836496, 'fraction_numerical': 0.011901388495324454, 'mean_word_length': 4.893155258764608, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 18, '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': '669312', 'n_tokens_mistral': 1116, 'n_tokens_neox': 1014, 'n_words': 267}