<commit_before>/**
 * Non-metric Space Library
 *
 * Authors: <NAME> (https://github.com/bileg), <NAME> (http://boytsov.info).
 * With contributions from Lawrence Cayton (http://lcayton.com/).
 *
 * For the complete list of contributors and further details see:
 * https://github.com/searchivarius/NonMetricSpaceLib 
 * 
 * Copyright (c) 2014
 *
 * This code is released under the
 * Apache License Version 2.0 http://www.apache.org/licenses/.
 *
 */

#ifndef _META_ANALYSIS_H
#define _META_ANALYSIS_H

#include <vector>
#include <string>

#include "utils.h"

namespace similarity {

using std::vector;
using std::string;

class MetaAnalysis {
public:
  MetaAnalysis(size_t TestSetQty, double zVal = 1.96) : zVal_(zVal) {
    Recall_         .resize(TestSetQty);
    LogRelPosError_ .resize(TestSetQty);
    NumCloser_      .resize(TestSetQty);
    QueryTime_      .resize(TestSetQty);
    DistComp_       .resize(TestSetQty);
    ImprEfficiency_ .resize(TestSetQty);
    ImprDistComp_   .resize(TestSetQty);
    Mem_            .resize(TestSetQty);
  }

  // Let's protect Add* functions, b/c them can be called from different threads  
  void AddRecall(size_t SetId, double Recall) {
    Recall_[SetId].push_back(Recall);
  }
  void AddLogRelPosError(size_t SetId, double LogRelPosError) {
    LogRelPosError_[SetId].push_back(LogRelPosError);
  }
  void AddNumCloser(size_t SetId, double NumCloser) {
    NumCloser_[SetId].push_back(NumCloser);
  }
  void AddQueryTime(size_t SetId, double QueryTime) {
    QueryTime_[SetId].push_back(QueryTime);
  }
  void AddDistComp(size_t SetId, double DistComp) {
    DistComp_[SetId].push_back(DistComp);
  }
  void SetMem(size_t SetId, double Mem) {
    Mem_[SetId] = Mem;
  }
  void SetImprEfficiency(size_t SetId, double ImprEfficiency) {
    ImprEfficiency_[SetId] = ImprEfficiency;
  }
  void SetImprDistComp(size_t SetId, double ImprDistComp) {
    ImprDistComp_[SetId] = ImprDistComp;
  }

  void ComputeAll() {
    ComputeOneMeta("Recall", Recall_, RecallAvg, RecallConfMin, RecallConfMax);
    ComputeOneMeta("LogRelPosError", LogRelPosError_, LogRelPosErrorAvg, LogRelPosErrorConfMin, LogRelPosErrorConfMax);
    ComputeOneMeta("NumCloser", NumCloser_, NumCloserAvg, NumCloserConfMin, NumCloserConfMax);
    ComputeOneMeta("QueryTime", QueryTime_, QueryTimeAvg, QueryTimeConfMin, QueryTimeConfMax);
    ComputeOneMeta("DistComp", DistComp_, DistCompAvg, DistCompConfMin, DistCompConfMax);

    ComputeOneSimple("ImprEfficiency", ImprEfficiency_, ImprEfficiencyAvg, ImprEfficiencyConfMin, ImprEfficiencyConfMax);
    ComputeOneSimple("ImprDistComp", ImprDistComp_, ImprDistCompAvg, ImprDistCompConfMin, ImprDistCompConfMax);
    ComputeOneSimple("MemUsage", Mem_, MemAvg, MemConfMin, MemConfMax);
  }

  double GetRecallAvg() const { return RecallAvg;} 
  double GetRecallConfMin() const{return RecallConfMin;}; 
  double GetRecallConfMax() const { return RecallConfMax;}

  double GetRelPosErrorAvg() const { return exp(LogRelPosErrorAvg);} 
  double GetRelPosErrorConfMin() const{return exp(LogRelPosErrorConfMin);}; 
  double GetRelPosErrorConfMax() const { return exp(LogRelPosErrorConfMax);}

  double GetNumCloserAvg() const { return NumCloserAvg;} 
  double GetNumCloserConfMin() const{return NumCloserConfMin;}; 
  double GetNumCloserConfMax() const { return NumCloserConfMax;}

  double GetImprEfficiencyAvg() const { return ImprEfficiencyAvg;} 
  double GetImprEfficiencyConfMin() const{return ImprEfficiencyConfMin;}; 
  double GetImprEfficiencyConfMax() const { return ImprEfficiencyConfMax;}

  double GetImprDistCompAvg() const { return ImprDistCompAvg;} 
  double GetImprDistCompConfMin() const{return ImprDistCompConfMin;}; 
  double GetImprDistCompConfMax() const { return ImprDistCompConfMax;}

  double GetMemAvg() const { return MemAvg;} 
  double GetMemConfMin() const{return MemConfMin;}; 
  double GetMemConfMax() const { return MemConfMax;}

  double GetQueryTimeAvg() const { return QueryTimeAvg;} 
  double GetQueryTimeConfMin() const{return QueryTimeConfMin;}; 
  double GetQueryTimeConfMax() const { return QueryTimeConfMax;}

  double GetDistCompAvg() const { return DistCompAvg;} 
  double GetDistCompConfMin() const{return DistCompConfMin;}; 
  double GetDistCompConfMax() const { return DistCompConfMax;}
private:
double RecallAvg, RecallConfMin, RecallConfMax;
double LogRelPosErrorAvg, LogRelPosErrorConfMin, LogRelPosErrorConfMax;
double NumCloserAvg, NumCloserConfMin, NumCloserConfMax;
double QueryTimeAvg, QueryTimeConfMin, QueryTimeConfMax;
double DistCompAvg, DistCompConfMin, DistCompConfMax;
double ImprEfficiencyAvg, ImprEfficiencyConfMin, ImprEfficiencyConfMax;
double ImprDistCompAvg, ImprDistCompConfMin, ImprDistCompConfMax;
double MemAvg, MemConfMin, MemConfMax;
double zVal_;

vector<vector<double>>   Recall_; 
vector<vector<double>>   LogRelPosError_; 
vector<vector<double>>   NumCloser_; 
vector<vector<double>>   QueryTime_; 
vector<vector<double>>   DistComp_; 
vector<double>           ImprEfficiency_; 
vector<double>           ImprDistComp_; 
vector<double>           Mem_; 

MetaAnalysis(){} // be private!

void ComputeOneSimple(const string& Name, 
                      const vector<double>& vals, double& avg, double& ConfMin, double& ConfMax) {
  avg = Mean(&vals[0], vals.size());<commit_msg>In certain corner cases, violation of normality assumption leads to incorrect aggregation. Let's not use meta-analysis unless we deal with times.<commit_after>/**
 * Non-metric Space Library
 *
 * Authors: <NAME> (https://github.com/bileg), <NAME> (http://boytsov.info).
 * With contributions from Lawrence Cayton (http://lcayton.com/).
 *
 * For the complete list of contributors and further details see:
 * https://github.com/searchivarius/NonMetricSpaceLib 
 * 
 * Copyright (c) 2014
 *
 * This code is released under the
 * Apache License Version 2.0 http://www.apache.org/licenses/.
 *
 */

#ifndef _META_ANALYSIS_H
#define _META_ANALYSIS_H

#include <vector>
#include <string>

#include "utils.h"

namespace similarity {

using std::vector;
using std::string;

class MetaAnalysis {
public:
  MetaAnalysis(size_t TestSetQty, double zVal = 1.96) : zVal_(zVal) {
    Recall_         .resize(TestSetQty);
    LogRelPosError_ .resize(TestSetQty);
    NumCloser_      .resize(TestSetQty);
    QueryTime_      .resize(TestSetQty);
    DistComp_       .resize(TestSetQty);
    ImprEfficiency_ .resize(TestSetQty);
    ImprDistComp_   .resize(TestSetQty);
    Mem_            .resize(TestSetQty);
  }

  // Let's protect Add* functions, b/c them can be called from different threads  
  void AddRecall(size_t SetId, double Recall) {
    Recall_[SetId].push_back(Recall);
  }
  void AddLogRelPosError(size_t SetId, double LogRelPosError) {
    LogRelPosError_[SetId].push_back(LogRelPosError);
  }
  void AddNumCloser(size_t SetId, double NumCloser) {
    NumCloser_[SetId].push_back(NumCloser);
  }
  void AddQueryTime(size_t SetId, double QueryTime) {
    QueryTime_[SetId].push_back(QueryTime);
  }
  void AddDistComp(size_t SetId, double DistComp) {
    DistComp_[SetId].push_back(DistComp);
  }
  void SetMem(size_t SetId, double Mem) {
    Mem_[SetId] = Mem;
  }
  void SetImprEfficiency(size_t SetId, double ImprEfficiency) {
    ImprEfficiency_[SetId] = ImprEfficiency;
  }
  void SetImprDistComp(size_t SetId, double ImprDistComp) {
    ImprDistComp_[SetId] = ImprDistComp;
  }

  void ComputeAll() {
    ComputeOneSimple("Recall", Recall_, RecallAvg, RecallConfMin, RecallConfMax);
    ComputeOneSimple("LogRelPosError", LogRelPosError_, LogRelPosErrorAvg, LogRelPosErrorConfMin, LogRelPosErrorConfMax);
    ComputeOneSimple("NumCloser", NumCloser_, NumCloserAvg, NumCloserConfMin, NumCloserConfMax);
    ComputeOneMeta("QueryTime", QueryTime_, QueryTimeAvg, QueryTimeConfMin, QueryTimeConfMax);
    ComputeOneMeta("DistComp", DistComp_, DistCompAvg, DistCompConfMin, DistCompConfMax);

    ComputeOneSimple("ImprEfficiency", ImprEfficiency_, ImprEfficiencyAvg, ImprEfficiencyConfMin, ImprEfficiencyConfMax);
    ComputeOneSimple("ImprDistComp", ImprDistComp_, ImprDistCompAvg, ImprDistCompConfMin, ImprDistCompConfMax);
    ComputeOneSimple("MemUsage", Mem_, MemAvg, MemConfMin, MemConfMax);
  }

  double GetRecallAvg() const { return RecallAvg;} 
  double GetRecallConfMin() const{return RecallConfMin;}; 
  double GetRecallConfMax() const { return RecallConfMax;}

  double GetRelPosErrorAvg() const { return exp(LogRelPosErrorAvg);} 
  double GetRelPosErrorConfMin() const{return exp(LogRelPosErrorConfMin);}; 
  double GetRelPosErrorConfMax() const { return exp(LogRelPosErrorConfMax);}

  double GetNumCloserAvg() const { return NumCloserAvg;} 
  double GetNumCloserConfMin() const{return NumCloserConfMin;}; 
  double GetNumCloserConfMax() const { return NumCloserConfMax;}

  double GetImprEfficiencyAvg() const { return ImprEfficiencyAvg;} 
  double GetImprEfficiencyConfMin() const{return ImprEfficiencyConfMin;}; 
  double GetImprEfficiencyConfMax() const { return ImprEfficiencyConfMax;}

  double GetImprDistCompAvg() const { return ImprDistCompAvg;} 
  double GetImprDistCompConfMin() const{return ImprDistCompConfMin;}; 
  double GetImprDistCompConfMax() const { return ImprDistCompConfMax;}

  double GetMemAvg() const { return MemAvg;} 
  double GetMemConfMin() const{return MemConfMin;}; 
  double GetMemConfMax() const { return MemConfMax;}

  double GetQueryTimeAvg() const { return QueryTimeAvg;} 
  double GetQueryTimeConfMin() const{return QueryTimeConfMin;}; 
  double GetQueryTimeConfMax() const { return QueryTimeConfMax;}

  double GetDistCompAvg() const { return DistCompAvg;} 
  double GetDistCompConfMin() const{return DistCompConfMin;}; 
  double GetDistCompConfMax() const { return DistCompConfMax;}
private:
double RecallAvg, RecallConfMin, RecallConfMax;
double LogRelPosErrorAvg, LogRelPosErrorConfMin, LogRelPosErrorConfMax;
double NumCloserAvg, NumCloserConfMin, NumCloserConfMax;
double QueryTimeAvg, QueryTimeConfMin, QueryTimeConfMax;
double DistCompAvg, DistCompConfMin, DistCompConfMax;
double ImprEfficiencyAvg, ImprEfficiencyConfMin, ImprEfficiencyConfMax;
double ImprDistCompAvg, ImprDistCompConfMin, ImprDistCompConfMax;
double MemAvg, MemConfMin, MemConfMax;
double zVal_;

vector<vector<double>>   Recall_; 
vector<vector<double>>   LogRelPosError_; 
vector<vector<double>>   NumCloser_; 
vector<vector<double>>   QueryTime_; 
vector<vector<double>>   DistComp_; 
vector<double>           ImprEfficiency_; 
vector<double>           ImprDistComp_; 
vector<double>           Mem_; 

MetaAnalysis(){} // be private!

void ComputeOneSimple(const string &Name, 
                    const vector<vector<double>>& vals, double& avg, double& ConfMin, double& ConfMax) {
  vector<double> avgVals;
  for (size_t i = 0; i < vals.size(); ++i) {
    avgVals.push_back(Mean(&vals[i][0], vals[i].size()));
  }
  ComputeOneSimple(Name, avgVals, avg, ConfMin, ConfMax);
}

void ComputeOneSimple(const string& Name, 
                      const vector<double>& vals, double& avg, double& ConfMin, double& ConfMax) {
  avg = Mean(&vals[0], vals.size());
{'fraction_non_alphanumeric': 0.09947738331230853, 'fraction_numerical': 0.0019823391602090466, 'mean_word_length': 5.335045662100456, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 42, 'https://': 4, 'lorem ipsum': 0, 'www.': 2, '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': '3213045', 'n_tokens_mistral': 3754, 'n_tokens_neox': 3571, 'n_words': 784}